From f95b21a1f840e48c8da0563c685cae9882ae9415 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sun, 29 Mar 2020 21:24:09 +0200 Subject: [PATCH] Reuse the temp variable of a DUP operation for further DUP operations --- .../jwebassembly/module/WasmCodeBuilder.java | 12 ++++++------ .../jwebassembly/module/WasmInstruction.java | 2 +- .../module/WasmLoadStoreInstruction.java | 10 +++++----- .../jwebassembly/module/WasmLocalInstruction.java | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java index 5be9b18..385805d 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java @@ -19,7 +19,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import java.util.function.BiFunction; import java.util.function.Function; import javax.annotation.Nonnegative; @@ -258,7 +257,7 @@ public abstract class WasmCodeBuilder { */ protected void addLoadStoreInstruction( AnyType valueType, boolean load, @Nonnegative int javaIdx, int javaCodePos, int lineNumber ) { localVariables.use( valueType, javaIdx, javaCodePos ); - instructions.add( new WasmLoadStoreInstruction( load, javaIdx, localVariables, javaCodePos, lineNumber ) ); + instructions.add( new WasmLoadStoreInstruction( load ? VariableOperator.get : VariableOperator.set, javaIdx, localVariables, javaCodePos, lineNumber ) ); } /** @@ -305,7 +304,8 @@ public abstract class WasmCodeBuilder { //alternate we need to create a new locale variable if( varIndex < 0 ) { varIndex = getTempVariable( type, javaCodePos, javaCodePos + 1 ); - instructions.add( new WasmDupInstruction( varIndex, type, localVariables, javaCodePos, lineNumber ) ); + instructions.add( new WasmLoadStoreInstruction( VariableOperator.tee, varIndex, localVariables, javaCodePos, lineNumber ) ); + instructions.add( new WasmLoadStoreInstruction( VariableOperator.get, varIndex, localVariables, javaCodePos, lineNumber ) ); // an alternative solution can be a function call with multiple return values but this seems to be slower // new SyntheticFunctionName( "dup" + storeType, "local.get 0 local.get 0 return", storeType, null, storeType, storeType ), codePos, lineNumber ) } else { @@ -473,9 +473,9 @@ public abstract class WasmCodeBuilder { if( struct.getOperator() == StructOperator.NEW_DEFAULT ) { instructions.set( i, new WasmNopInstruction( struct.getCodePosition(), struct.getLineNumber() ) ); // replace NEW_DEFAULT with Nop, Nop because the code position can be needed for the branch manager instr = instructions.get( ++i ); - if( instr.getType() == Type.Dup ) { - instructions.remove( i ); // dup of the instance reference if it is later assign, missing if the object after the constructor is never assign - } +// if( instr.getType() == Type.Dup ) { +// instructions.remove( i ); // dup of the instance reference if it is later assign, missing if the object after the constructor is never assign +// } break; } } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java index e040ff4..dcf1728 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java @@ -35,7 +35,7 @@ abstract class WasmInstruction { * Type of instruction to faster differ as with instanceof. */ static enum Type { - Const, Convert, Local, Global, Table, Memory, Block, Numeric, Nop, Jump, Call, CallVirtual, CallInterface, Array, Struct, Dup, DupThis; + Const, Convert, Local, Global, Table, Memory, Block, Numeric, Nop, Jump, Call, CallVirtual, CallInterface, Array, Struct, DupThis; } private int javaCodePos; diff --git a/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java index 5dfd211..0e4a93c 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java @@ -21,7 +21,7 @@ import static de.inetsoftware.jwebassembly.wasm.VariableOperator.set; import javax.annotation.Nonnegative; -import de.inetsoftware.jwebassembly.wasm.AnyType; +import de.inetsoftware.jwebassembly.wasm.VariableOperator; /** * WasmInstruction for load and store local variables. @@ -34,8 +34,8 @@ class WasmLoadStoreInstruction extends WasmLocalInstruction { /** * Create an instance of a load/store instruction * - * @param load - * true: if load + * @param op + * the operation * @param idx * the memory/slot idx of the variable * @param localVariables @@ -45,8 +45,8 @@ class WasmLoadStoreInstruction extends WasmLocalInstruction { * @param lineNumber * the line number in the Java source code */ - WasmLoadStoreInstruction( boolean load, @Nonnegative int idx, LocaleVariableManager localVariables, int javaCodePos, int lineNumber ) { - super( load ? get : set, idx, localVariables, javaCodePos, lineNumber ); + WasmLoadStoreInstruction( VariableOperator op, @Nonnegative int idx, LocaleVariableManager localVariables, int javaCodePos, int lineNumber ) { + super( op, idx, localVariables, javaCodePos, lineNumber ); } /** diff --git a/src/de/inetsoftware/jwebassembly/module/WasmLocalInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmLocalInstruction.java index 52d11e3..ae52f68 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmLocalInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmLocalInstruction.java @@ -112,7 +112,7 @@ class WasmLocalInstruction extends WasmInstruction { */ @Override AnyType getPushValueType() { - return op == get ? localVariables.getValueType( getIndex() ) : null; + return op != set ? localVariables.getValueType( getIndex() ) : null; } /**