From 3d8f49c79a9087becc0104debe59a90545c29cfc Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sat, 29 Feb 2020 16:14:32 +0100 Subject: [PATCH] simplify the scan and the compiler structure, remove some old hacks. --- .../jwebassembly/module/ModuleGenerator.java | 32 ++----------------- .../module/WasmCallInstruction.java | 10 ------ .../module/WasmCallVirtualInstruction.java | 17 ++-------- .../jwebassembly/module/WasmCodeBuilder.java | 4 ++- 4 files changed, 7 insertions(+), 56 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index 2136ff2..65a8e8a 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -199,7 +199,7 @@ public class ModuleGenerator { JWebAssembly.LOGGER.fine( '\t' + next.methodName + next.signature ); SyntheticFunctionName synth = (SyntheticFunctionName)next; if( synth.hasWasmCode() ) { - scanMethod( synth.getCodeBuilder( watParser ) ); + synth.getCodeBuilder( watParser ); } else { functions.markAsImport( synth, synth.getAnnotation() ); } @@ -219,7 +219,7 @@ public class ModuleGenerator { method = functions.replace( next, null ); } if( method != null ) { - scanMethod( createInstructions( functions.replace( next, method ) ) ); + createInstructions( functions.replace( next, method ) ); boolean needThisParameter = !method.isStatic() || "".equals( method.getName() ); functions.markAsScanned( next, needThisParameter ); continue; @@ -304,30 +304,6 @@ public class ModuleGenerator { writer.prepareFinish(); } - /** - * Scan the method and list all needed methods. - * - * @param codeBuilder - * the codeBuilder with instructions of the method - * @throws IOException - * if any I/O error occur - */ - private void scanMethod( WasmCodeBuilder codeBuilder ) throws IOException { - if( codeBuilder == null ) { - return; - } - List instructions = codeBuilder.getInstructions(); - for( WasmInstruction instruction : instructions ) { - switch( instruction.getType() ) { - case Call: - case CallVirtual: - ((WasmCallInstruction)instruction).markAsNeeded( functions ); - break; - default: - } - } - } - /** * Finish the code generation. * @@ -548,10 +524,6 @@ public class ModuleGenerator { default: } break; - case Call: - case CallVirtual: - ((WasmCallInstruction)instruction).markAsNeeded( functions ); - break; case Struct: if( !writer.options.useGC() ) { break; diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java index 5e10953..848123f 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java @@ -89,16 +89,6 @@ class WasmCallInstruction extends WasmInstruction { return types; } - /** - * Mark the function as needed in the functions manager and replace the function name with a possible super name. - * - * @param functions - * the function manager - */ - void markAsNeeded( @Nonnull FunctionManager functions ) { - name = functions.markAsNeeded( name ); - } - /** * {@inheritDoc} */ diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCallVirtualInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmCallVirtualInstruction.java index b940376..be85082 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCallVirtualInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCallVirtualInstruction.java @@ -21,9 +21,6 @@ import java.io.IOException; import javax.annotation.Nonnull; import de.inetsoftware.jwebassembly.module.TypeManager.StructType; -import de.inetsoftware.jwebassembly.wasm.MemoryOperator; -import de.inetsoftware.jwebassembly.wasm.NamedStorageType; -import de.inetsoftware.jwebassembly.wasm.StructOperator; import de.inetsoftware.jwebassembly.wasm.ValueType; import de.inetsoftware.jwebassembly.wasm.VariableOperator; @@ -35,8 +32,6 @@ import de.inetsoftware.jwebassembly.wasm.VariableOperator; */ class WasmCallVirtualInstruction extends WasmCallIndirectInstruction { - private int virtualFunctionIdx = -1; - private final WasmOptions options; /** @@ -66,22 +61,13 @@ class WasmCallVirtualInstruction extends WasmCallIndirectInstruction { return Type.CallVirtual; } - /** - * {@inheritDoc} - */ - @Override - void markAsNeeded( FunctionManager functions ) { - super.markAsNeeded( functions ); - virtualFunctionIdx = functions.getFunctionIndex( getFunctionName() ); - } - /** * if this call is executed virtual or if is was optimized. * * @return true, virtual call */ boolean isVirtual() { - return virtualFunctionIdx > 0; + return options.functions.getFunctionIndex( getFunctionName() ) > 0; } /** @@ -89,6 +75,7 @@ class WasmCallVirtualInstruction extends WasmCallIndirectInstruction { */ @Override public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { + int virtualFunctionIdx = options.functions.getFunctionIndex( getFunctionName() ); if( virtualFunctionIdx < 0 ) { super.writeTo( writer ); } else { diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java index a36742e..0aceb8c 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java @@ -30,7 +30,6 @@ import de.inetsoftware.classparser.Member; import de.inetsoftware.classparser.MethodInfo; import de.inetsoftware.jwebassembly.WasmException; import de.inetsoftware.jwebassembly.javascript.JavaScriptNewMultiArrayFunctionName; -import de.inetsoftware.jwebassembly.javascript.JavaScriptSyntheticFunctionName; import de.inetsoftware.jwebassembly.javascript.NonGC; import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; import de.inetsoftware.jwebassembly.wasm.AnyType; @@ -416,6 +415,7 @@ public abstract class WasmCodeBuilder { * the line number in the Java source code */ protected void addCallInstruction( FunctionName name, int javaCodePos, int lineNumber ) { + name = functions.markAsNeeded( name ); boolean needThisParameter = functions.needThisParameter( name ); WasmCallInstruction instruction = new WasmCallInstruction( name, javaCodePos, lineNumber, types, needThisParameter ); @@ -506,6 +506,7 @@ public abstract class WasmCodeBuilder { * the line number in the Java source code */ protected void addCallVirtualInstruction( FunctionName name, int javaCodePos, int lineNumber ) { + name = functions.markAsNeeded( name ); addCallIndirectInstruction( new WasmCallVirtualInstruction( name, javaCodePos, lineNumber, types, options ) ); options.getCallVirtual(); // mark the function as needed } @@ -520,6 +521,7 @@ public abstract class WasmCodeBuilder { * the line number in the Java source code */ protected void addCallInterfaceInstruction( FunctionName name, int javaCodePos, int lineNumber ) { + //TODO name = functions.markAsNeeded( name ); addCallIndirectInstruction( new WasmCallInterfaceInstruction( name, javaCodePos, lineNumber, types, options ) ); }