simplify the initializing

This commit is contained in:
Volker Berlin 2020-02-24 21:08:29 +01:00
parent f2547bd0d2
commit 2f2c39a07a
7 changed files with 43 additions and 32 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019 Volker Berlin (i-net software) * Copyright 2019 - 2020 Volker Berlin (i-net software)
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,7 +25,7 @@ import de.inetsoftware.jwebassembly.wasm.VariableOperator;
* *
* @author Volker Berlin * @author Volker Berlin
*/ */
class CodeOptimizer { public class CodeOptimizer {
/** /**
* Optimize the code before writing. * Optimize the code before writing.
@ -33,7 +33,7 @@ class CodeOptimizer {
* @param instructions * @param instructions
* the list of instructions * the list of instructions
*/ */
public void optimze( List<WasmInstruction> instructions ) { void optimze( List<WasmInstruction> instructions ) {
for( int i = instructions.size()-1; i >= 0; i-- ) { for( int i = instructions.size()-1; i >= 0; i-- ) {
WasmInstruction instr = instructions.get( i ); WasmInstruction instr = instructions.get( i );
switch( instr.getType() ) { switch( instr.getType() ) {

View File

@ -70,13 +70,13 @@ public class ModuleGenerator {
private String className; private String className;
private FunctionManager functions = new FunctionManager(); private final FunctionManager functions;
private TypeManager types = new TypeManager(); private final TypeManager types;
private StringManager strings = new StringManager(); private final StringManager strings;
private CodeOptimizer optimizer = new CodeOptimizer(); private final CodeOptimizer optimizer;
/** /**
* Create a new generator. * Create a new generator.
@ -95,10 +95,12 @@ public class ModuleGenerator {
this.javaScript = new JavaScriptWriter( target ); this.javaScript = new JavaScriptWriter( target );
this.classFileLoader = new ClassFileLoader( new URLClassLoader( libraries.toArray( new URL[libraries.size()] ) ) ); this.classFileLoader = new ClassFileLoader( new URLClassLoader( libraries.toArray( new URL[libraries.size()] ) ) );
WasmOptions options = writer.options; WasmOptions options = writer.options;
types.init( options ); functions = options.functions;
strings.init( functions ); types = options.types;
javaCodeBuilder.init( types, functions, strings, options, classFileLoader ); strings = options.strings;
((WasmCodeBuilder)watParser).init( types, functions, strings, options, classFileLoader ); optimizer = options.optimizer;
javaCodeBuilder.init( options, classFileLoader );
((WasmCodeBuilder)watParser).init( options, classFileLoader );
scanLibraries( libraries ); scanLibraries( libraries );
} }

View File

@ -27,13 +27,14 @@ import javax.annotation.Nonnull;
import de.inetsoftware.jwebassembly.api.annotation.WasmTextCode; import de.inetsoftware.jwebassembly.api.annotation.WasmTextCode;
import de.inetsoftware.jwebassembly.wasm.ValueType; import de.inetsoftware.jwebassembly.wasm.ValueType;
import de.inetsoftware.jwebassembly.wasm.WasmOptions;
/** /**
* Handle all the constant strings. The constant strings will be write into the data section. * Handle all the constant strings. The constant strings will be write into the data section.
* *
* @author Volker Berlin * @author Volker Berlin
*/ */
class StringManager extends LinkedHashMap<String, Integer> { public class StringManager extends LinkedHashMap<String, Integer> {
/** /**
* Signature of method stringConstant. * Signature of method stringConstant.
@ -48,13 +49,13 @@ class StringManager extends LinkedHashMap<String, Integer> {
private int stringMemoryOffset = -1; private int stringMemoryOffset = -1;
/** /**
* Initialize the string manager. * Create a new instance.
* *
* @param functions * @param options
* the function manager * compiler properties and shared managers
*/ */
void init( FunctionManager functions ) { public StringManager( WasmOptions options ) {
this.functions = functions; this.functions = options.functions;
} }
/** /**

View File

@ -52,7 +52,7 @@ public class TypeManager {
private boolean isFinish; private boolean isFinish;
private WasmOptions options; private final WasmOptions options;
/** /**
* Initialize the type manager. * Initialize the type manager.
@ -60,7 +60,7 @@ public class TypeManager {
* @param options * @param options
* compiler properties * compiler properties
*/ */
void init( WasmOptions options ) { public TypeManager( WasmOptions options ) {
this.options = options; this.options = options;
} }

View File

@ -75,20 +75,16 @@ public abstract class WasmCodeBuilder {
/** /**
* Initialize the code builder; * Initialize the code builder;
* *
* @param types
* the type manager
* @param functions
* the function manager
* @param strings
* the string manager
* @param options * @param options
* compiler properties * compiler properties
* @param classFileLoader
* for loading the class files
*/ */
void init( TypeManager types, FunctionManager functions, StringManager strings, WasmOptions options, ClassFileLoader classFileLoader ) { void init( WasmOptions options, ClassFileLoader classFileLoader ) {
this.localVariables.init( types ); this.localVariables.init( options.types );
this.types = types; this.types = options.types;
this.functions = functions; this.functions = options.functions;
this.strings = strings; this.strings = options.strings;
this.options = options; this.options = options;
this.classFileLoader = classFileLoader; this.classFileLoader = classFileLoader;
} }

View File

@ -20,7 +20,11 @@ import java.util.HashMap;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import de.inetsoftware.jwebassembly.JWebAssembly; import de.inetsoftware.jwebassembly.JWebAssembly;
import de.inetsoftware.jwebassembly.module.CodeOptimizer;
import de.inetsoftware.jwebassembly.module.FunctionManager;
import de.inetsoftware.jwebassembly.module.FunctionName; import de.inetsoftware.jwebassembly.module.FunctionName;
import de.inetsoftware.jwebassembly.module.StringManager;
import de.inetsoftware.jwebassembly.module.TypeManager;
/** /**
* The option/properties for the behavior of the compiler. * The option/properties for the behavior of the compiler.
@ -29,6 +33,14 @@ import de.inetsoftware.jwebassembly.module.FunctionName;
*/ */
public class WasmOptions { public class WasmOptions {
public final FunctionManager functions = new FunctionManager();
public final TypeManager types = new TypeManager( this );
public final StringManager strings = new StringManager( this );
public final CodeOptimizer optimizer = new CodeOptimizer();
private final boolean debugNames; private final boolean debugNames;
private final boolean useGC; private final boolean useGC;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2018 - 2019 Volker Berlin (i-net software) * Copyright 2018 - 2020 Volker Berlin (i-net software)
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -40,7 +40,7 @@ public class WatParserTest {
WasmOptions options = new WasmOptions( new HashMap<>() ); WasmOptions options = new WasmOptions( new HashMap<>() );
WatParser parser = new WatParser(); WatParser parser = new WatParser();
WasmCodeBuilder codeBuilder = parser; WasmCodeBuilder codeBuilder = parser;
codeBuilder.init( null, null, null, options, null ); codeBuilder.init( options, null );
parser.parse( wat, null, 100 ); parser.parse( wat, null, 100 );
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
ModuleWriter writer = new TextModuleWriter( new WasmTarget( builder ), options ); ModuleWriter writer = new TextModuleWriter( new WasmTarget( builder ), options );