mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-25 07:27:52 +01:00
implements dup_x2 instruction
This commit is contained in:
parent
f15fb1cdf9
commit
11f338eea8
@ -311,9 +311,10 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder {
|
|||||||
break;
|
break;
|
||||||
case 91: // dup_x2
|
case 91: // dup_x2
|
||||||
case 94: // dup2_x2
|
case 94: // dup2_x2
|
||||||
|
addDupX2Instruction( codePos, lineNumber );
|
||||||
|
break;
|
||||||
case 95: // swap
|
case 95: // swap
|
||||||
// can be do with functions with more as one return value in future WASM standard
|
throw new WasmException( "Stack swap is not supported in current WASM. try to save immediate values in a local variable: "
|
||||||
throw new WasmException( "Stack duplicate is not supported in current WASM. try to save immediate values in a local variable: "
|
|
||||||
+ op, lineNumber );
|
+ op, lineNumber );
|
||||||
case 96: // iadd
|
case 96: // iadd
|
||||||
addNumericInstruction( NumericOperator.add, ValueType.i32, codePos, lineNumber );
|
addNumericInstruction( NumericOperator.add, ValueType.i32, codePos, lineNumber );
|
||||||
|
@ -348,7 +348,7 @@ public abstract class WasmCodeBuilder {
|
|||||||
AnyType type2 = findValueTypeFromStack( 2, javaCodePos );
|
AnyType type2 = findValueTypeFromStack( 2, javaCodePos );
|
||||||
|
|
||||||
int varIndex1 = getTempVariable( type1, javaCodePos, javaCodePos + 1 );
|
int varIndex1 = getTempVariable( type1, javaCodePos, javaCodePos + 1 );
|
||||||
int varIndex2 = getTempVariable( type2, javaCodePos, javaCodePos + 1 );;
|
int varIndex2 = getTempVariable( type2, javaCodePos, javaCodePos + 1 );
|
||||||
|
|
||||||
// save in temp variables
|
// save in temp variables
|
||||||
instructions.add( new WasmLocalInstruction( VariableOperator.set, varIndex1, localVariables, javaCodePos, lineNumber ) );
|
instructions.add( new WasmLocalInstruction( VariableOperator.set, varIndex1, localVariables, javaCodePos, lineNumber ) );
|
||||||
@ -360,6 +360,37 @@ public abstract class WasmCodeBuilder {
|
|||||||
instructions.add( new WasmLocalInstruction( VariableOperator.get, varIndex1, localVariables, javaCodePos, lineNumber ) );
|
instructions.add( new WasmLocalInstruction( VariableOperator.get, varIndex1, localVariables, javaCodePos, lineNumber ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simulate the dup_x2 Java byte code instruction.<p>
|
||||||
|
*
|
||||||
|
* ..., value3, value2, value1 → ..., value1, value3, value2, value1
|
||||||
|
*
|
||||||
|
* @param javaCodePos
|
||||||
|
* the code position/offset in the Java method
|
||||||
|
* @param lineNumber
|
||||||
|
* the line number in the Java source code
|
||||||
|
*/
|
||||||
|
protected void addDupX2Instruction( int javaCodePos, int lineNumber ) {
|
||||||
|
AnyType type1 = findValueTypeFromStack( 1, javaCodePos );
|
||||||
|
AnyType type2 = findValueTypeFromStack( 2, javaCodePos );
|
||||||
|
AnyType type3 = findValueTypeFromStack( 3, javaCodePos );
|
||||||
|
|
||||||
|
int varIndex1 = getTempVariable( type1, javaCodePos, javaCodePos + 1 );
|
||||||
|
int varIndex2 = getTempVariable( type2, javaCodePos, javaCodePos + 1 );
|
||||||
|
int varIndex3 = getTempVariable( type3, 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 ) );
|
||||||
|
instructions.add( new WasmLocalInstruction( VariableOperator.set, varIndex3, 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, varIndex3, 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
|
* Add a global instruction
|
||||||
*
|
*
|
||||||
|
@ -59,6 +59,7 @@ public class ArrayOperations extends AbstractBaseTest {
|
|||||||
addParam( list, script, "loopObject" );
|
addParam( list, script, "loopObject" );
|
||||||
addParam( list, script, "copyBack2Front" );
|
addParam( list, script, "copyBack2Front" );
|
||||||
addParam( list, script, "copyFront2Back" );
|
addParam( list, script, "copyFront2Back" );
|
||||||
|
addParam( list, script, "dup_x2" );
|
||||||
}
|
}
|
||||||
rule.setTestParameters( list );
|
rule.setTestParameters( list );
|
||||||
return list;
|
return list;
|
||||||
@ -183,5 +184,18 @@ public class ArrayOperations extends AbstractBaseTest {
|
|||||||
crc.update( a );
|
crc.update( a );
|
||||||
return crc.getValue();
|
return crc.getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Export
|
||||||
|
static int dup_x2() {
|
||||||
|
Object[] data = {null,null,null};
|
||||||
|
int index = 1;
|
||||||
|
Object value = null;
|
||||||
|
|
||||||
|
if ((data[index] = value) != null) { // test for instruction dup_x2
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user