implements dup_x1 instruction

This commit is contained in:
Volker Berlin 2020-04-25 19:31:30 +02:00
parent f0828196b7
commit f15fb1cdf9
4 changed files with 35 additions and 7 deletions

View File

@ -306,8 +306,10 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder {
addDupInstruction( codePos, lineNumber );
break;
case 90: // dup_x1
case 91: // dup_x2
case 93: // dup2_x1
addDupX1Instruction( codePos, lineNumber );
break;
case 91: // dup_x2
case 94: // dup2_x2
case 95: // swap
// can be do with functions with more as one return value in future WASM standard

View File

@ -333,6 +333,33 @@ public abstract class WasmCodeBuilder {
}
}
/**
* Simulate the dup_x1 Java byte code instruction.<p>
*
* ..., value2, value1 ..., value1, value2, value1
*
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
protected void addDupX1Instruction( int javaCodePos, int lineNumber ) {
AnyType type1 = findValueTypeFromStack( 1, javaCodePos );
AnyType type2 = findValueTypeFromStack( 2, javaCodePos );
int varIndex1 = getTempVariable( type1, javaCodePos, javaCodePos + 1 );
int varIndex2 = getTempVariable( type2, javaCodePos, javaCodePos + 1 );;
// save in temp variables
instructions.add( new WasmLocalInstruction( VariableOperator.set, varIndex1, localVariables, javaCodePos, lineNumber ) );
instructions.add( new WasmLocalInstruction( VariableOperator.set, varIndex2, localVariables, javaCodePos, lineNumber ) );
// and restore it in new order on the stack
instructions.add( new WasmLocalInstruction( VariableOperator.get, varIndex1, localVariables, javaCodePos, lineNumber ) );
instructions.add( new WasmLocalInstruction( VariableOperator.get, varIndex2, localVariables, javaCodePos, lineNumber ) );
instructions.add( new WasmLocalInstruction( VariableOperator.get, varIndex1, localVariables, javaCodePos, lineNumber ) );
}
/**
* Add a global instruction
*

View File

@ -132,9 +132,8 @@ public class StructsGC extends AbstractBaseTest {
static int multipleAssign() {
Abc2 val = new Abc2();
for( int i = 0; i < 1_000; i++ ) {
val.a = 42;
// TODO
//val = val.abc = new Abc2();
val = val.abc = new Abc2();
val.a = i;
}
return val.a;
}

View File

@ -26,6 +26,7 @@ import org.junit.runners.Parameterized.Parameters;
import de.inetsoftware.jwebassembly.ScriptEngine;
import de.inetsoftware.jwebassembly.WasmRule;
import de.inetsoftware.jwebassembly.api.annotation.Export;
import de.inetsoftware.jwebassembly.runtime.StructsGC.Abc2;
import de.inetsoftware.jwebassembly.web.JSObject;
public class StructsNonGC extends AbstractBaseTest {
@ -140,9 +141,8 @@ public class StructsNonGC extends AbstractBaseTest {
static int multipleAssign() {
Abc2 val = new Abc2();
for( int i = 0; i < 1_000; i++ ) {
val.a = 42;
// TODO
//val = val.abc = new Abc2();
val = val.abc = new Abc2();
val.a = i;
}
return val.a;
}