Reuse the temp variable of a DUP operation for further DUP operations

This commit is contained in:
Volker Berlin 2020-03-29 21:24:09 +02:00
parent 7a9750afdd
commit f95b21a1f8
4 changed files with 13 additions and 13 deletions

View File

@ -19,7 +19,6 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function; import java.util.function.Function;
import javax.annotation.Nonnegative; 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 ) { protected void addLoadStoreInstruction( AnyType valueType, boolean load, @Nonnegative int javaIdx, int javaCodePos, int lineNumber ) {
localVariables.use( valueType, javaIdx, javaCodePos ); 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 //alternate we need to create a new locale variable
if( varIndex < 0 ) { if( varIndex < 0 ) {
varIndex = getTempVariable( type, javaCodePos, javaCodePos + 1 ); 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 // 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 ) // new SyntheticFunctionName( "dup" + storeType, "local.get 0 local.get 0 return", storeType, null, storeType, storeType ), codePos, lineNumber )
} else { } else {
@ -473,9 +473,9 @@ public abstract class WasmCodeBuilder {
if( struct.getOperator() == StructOperator.NEW_DEFAULT ) { 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 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 ); instr = instructions.get( ++i );
if( instr.getType() == Type.Dup ) { // 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 // instructions.remove( i ); // dup of the instance reference if it is later assign, missing if the object after the constructor is never assign
} // }
break; break;
} }
} }

View File

@ -35,7 +35,7 @@ abstract class WasmInstruction {
* Type of instruction to faster differ as with instanceof. * Type of instruction to faster differ as with instanceof.
*/ */
static enum Type { 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; private int javaCodePos;

View File

@ -21,7 +21,7 @@ import static de.inetsoftware.jwebassembly.wasm.VariableOperator.set;
import javax.annotation.Nonnegative; import javax.annotation.Nonnegative;
import de.inetsoftware.jwebassembly.wasm.AnyType; import de.inetsoftware.jwebassembly.wasm.VariableOperator;
/** /**
* WasmInstruction for load and store local variables. * WasmInstruction for load and store local variables.
@ -34,8 +34,8 @@ class WasmLoadStoreInstruction extends WasmLocalInstruction {
/** /**
* Create an instance of a load/store instruction * Create an instance of a load/store instruction
* *
* @param load * @param op
* true: if load * the operation
* @param idx * @param idx
* the memory/slot idx of the variable * the memory/slot idx of the variable
* @param localVariables * @param localVariables
@ -45,8 +45,8 @@ class WasmLoadStoreInstruction extends WasmLocalInstruction {
* @param lineNumber * @param lineNumber
* the line number in the Java source code * the line number in the Java source code
*/ */
WasmLoadStoreInstruction( boolean load, @Nonnegative int idx, LocaleVariableManager localVariables, int javaCodePos, int lineNumber ) { WasmLoadStoreInstruction( VariableOperator op, @Nonnegative int idx, LocaleVariableManager localVariables, int javaCodePos, int lineNumber ) {
super( load ? get : set, idx, localVariables, javaCodePos, lineNumber ); super( op, idx, localVariables, javaCodePos, lineNumber );
} }
/** /**

View File

@ -112,7 +112,7 @@ class WasmLocalInstruction extends WasmInstruction {
*/ */
@Override @Override
AnyType getPushValueType() { AnyType getPushValueType() {
return op == get ? localVariables.getValueType( getIndex() ) : null; return op != set ? localVariables.getValueType( getIndex() ) : null;
} }
/** /**