mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-25 15:37:52 +01:00
use the ValueStackManager to start the switch block on the value
declaration
This commit is contained in:
parent
f75b770200
commit
0ece5f2dfe
@ -73,8 +73,8 @@ class BranchManger {
|
|||||||
* the values of the cases
|
* the values of the cases
|
||||||
* @param positions
|
* @param positions
|
||||||
* the code positions
|
* the code positions
|
||||||
* @param the
|
* @param defaultPosition
|
||||||
* code position of the default block
|
* the code position of the default block
|
||||||
*/
|
*/
|
||||||
void startSwitch( int startPosition, int offset, int lineNumber, int[] keys, int[] positions, int defaultPosition ) {
|
void startSwitch( int startPosition, int offset, int lineNumber, int[] keys, int[] positions, int defaultPosition ) {
|
||||||
allParsedOperations.add( new SwitchParsedBlock( startPosition, offset, lineNumber, keys, positions, defaultPosition ) );
|
allParsedOperations.add( new SwitchParsedBlock( startPosition, offset, lineNumber, keys, positions, defaultPosition ) );
|
||||||
@ -209,7 +209,7 @@ class BranchManger {
|
|||||||
* the not consumed operations in the parent branch
|
* the not consumed operations in the parent branch
|
||||||
*/
|
*/
|
||||||
private void caculateSwitch( BranchNode parent, SwitchParsedBlock switchBlock, List<ParsedBlock> parsedOperations ) {
|
private void caculateSwitch( BranchNode parent, SwitchParsedBlock switchBlock, List<ParsedBlock> parsedOperations ) {
|
||||||
int startPosition = switchBlock.startPosition;
|
int startPosition = ((ParsedBlock)switchBlock).startPosition;
|
||||||
int posCount = switchBlock.positions.length;
|
int posCount = switchBlock.positions.length;
|
||||||
boolean isTable = switchBlock.keys.length == 1;
|
boolean isTable = switchBlock.keys.length == 1;
|
||||||
if( isTable ) {
|
if( isTable ) {
|
||||||
@ -228,13 +228,16 @@ class BranchManger {
|
|||||||
Arrays.sort( cases, (a, b) -> Integer.compare( a.position, b.position ) );
|
Arrays.sort( cases, (a, b) -> Integer.compare( a.position, b.position ) );
|
||||||
int blockCount = 0;
|
int blockCount = 0;
|
||||||
int lastPosition = -1;
|
int lastPosition = -1;
|
||||||
BranchNode brTableNode = new BranchNode( startPosition, startPosition, WasmBlockOperator.BR_TABLE, null );
|
BranchNode brTableNode = null;
|
||||||
BranchNode blockNode = brTableNode;
|
BranchNode blockNode = null;
|
||||||
for( int i = 0; i < cases.length; i++ ) {
|
for( int i = 0; i < cases.length; i++ ) {
|
||||||
switchCase = cases[i];
|
switchCase = cases[i];
|
||||||
switchCase.block = blockCount;
|
switchCase.block = blockCount;
|
||||||
int currentPosition = switchCase.position;
|
int currentPosition = switchCase.position;
|
||||||
if( lastPosition != currentPosition ) {
|
if( lastPosition != currentPosition ) {
|
||||||
|
if( blockNode == null ) {
|
||||||
|
blockNode = brTableNode = new BranchNode( currentPosition, currentPosition, WasmBlockOperator.BR_TABLE, null );
|
||||||
|
}
|
||||||
lastPosition = currentPosition;
|
lastPosition = currentPosition;
|
||||||
blockCount++;
|
blockCount++;
|
||||||
BranchNode node = new BranchNode( startPosition, startPosition + currentPosition, WasmBlockOperator.BLOCK, WasmBlockOperator.END );
|
BranchNode node = new BranchNode( startPosition, startPosition + currentPosition, WasmBlockOperator.BLOCK, WasmBlockOperator.END );
|
||||||
@ -303,9 +306,9 @@ class BranchManger {
|
|||||||
private static class ParsedBlock {
|
private static class ParsedBlock {
|
||||||
private JavaBlockOperator op;
|
private JavaBlockOperator op;
|
||||||
|
|
||||||
int startPosition;
|
private int startPosition;
|
||||||
|
|
||||||
int endPosition;
|
private int endPosition;
|
||||||
|
|
||||||
private int lineNumber;
|
private int lineNumber;
|
||||||
|
|
||||||
|
@ -53,6 +53,8 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
|
|
||||||
private BranchManger branchManager = new BranchManger();
|
private BranchManger branchManager = new BranchManger();
|
||||||
|
|
||||||
|
private ValueStackManger stackManager = new ValueStackManger();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepare the content of the class.
|
* Prepare the content of the class.
|
||||||
*
|
*
|
||||||
@ -133,6 +135,7 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
locals.clear();
|
locals.clear();
|
||||||
localTable = code.getLocalVariableTable();
|
localTable = code.getLocalVariableTable();
|
||||||
|
|
||||||
|
stackManager.reset();
|
||||||
branchManager.reset();
|
branchManager.reset();
|
||||||
for( CodeInputStream byteCode : code.getByteCodes() ) {
|
for( CodeInputStream byteCode : code.getByteCodes() ) {
|
||||||
prepareBranchManager( byteCode, lineNumber = byteCode.getLineNumber() );
|
prepareBranchManager( byteCode, lineNumber = byteCode.getLineNumber() );
|
||||||
@ -296,12 +299,30 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
while( byteCode.available() > 0 ) {
|
while( byteCode.available() > 0 ) {
|
||||||
int op = byteCode.readUnsignedByte();
|
int op = byteCode.readUnsignedByte();
|
||||||
switch( op ) {
|
switch( op ) {
|
||||||
|
case 21: // iload
|
||||||
|
stackManager.add( ValueType.i32, byteCode.getCodePosition() - 1 );
|
||||||
|
byteCode.skip(1);
|
||||||
|
break;
|
||||||
|
case 22: // lload
|
||||||
|
stackManager.add( ValueType.i64, byteCode.getCodePosition() - 1 );
|
||||||
|
byteCode.skip(1);
|
||||||
|
break;
|
||||||
|
case 23: // fload
|
||||||
|
stackManager.add( ValueType.f32, byteCode.getCodePosition() - 1 );
|
||||||
|
byteCode.skip(1);
|
||||||
|
break;
|
||||||
|
case 24: // dload
|
||||||
|
stackManager.add( ValueType.f64, byteCode.getCodePosition() - 1 );
|
||||||
|
byteCode.skip(1);
|
||||||
|
break;
|
||||||
|
case 26: // iload_0
|
||||||
|
case 27: // iload_1
|
||||||
|
case 28: // iload_2
|
||||||
|
case 29: // iload_3
|
||||||
|
stackManager.add( ValueType.i32, byteCode.getCodePosition() - 1 );
|
||||||
|
break;
|
||||||
case 16: // bipush
|
case 16: // bipush
|
||||||
case 18: // ldc
|
case 18: // ldc
|
||||||
case 21: //iload
|
|
||||||
case 22: //lload
|
|
||||||
case 23: //fload
|
|
||||||
case 24: //dload
|
|
||||||
case 25: //aload
|
case 25: //aload
|
||||||
case 54: // istore
|
case 54: // istore
|
||||||
case 55: // lstore
|
case 55: // lstore
|
||||||
@ -351,7 +372,7 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
}
|
}
|
||||||
startPosition--;
|
startPosition--;
|
||||||
|
|
||||||
int defaultPosition = offset = byteCode.readInt();
|
int defaultPosition = startPosition + byteCode.readInt();
|
||||||
int[] keys;
|
int[] keys;
|
||||||
int[] positions;
|
int[] positions;
|
||||||
if( op == 171 ) { // lookupswitch
|
if( op == 171 ) { // lookupswitch
|
||||||
@ -360,7 +381,7 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
positions = new int[nPairs];
|
positions = new int[nPairs];
|
||||||
for( int i = 0; i < nPairs; i++ ) {
|
for( int i = 0; i < nPairs; i++ ) {
|
||||||
keys[i] = byteCode.readInt();
|
keys[i] = byteCode.readInt();
|
||||||
offset = Math.max( offset, positions[i] = byteCode.readInt() );
|
positions[i] = startPosition + byteCode.readInt();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
int low = byteCode.readInt();
|
int low = byteCode.readInt();
|
||||||
@ -368,10 +389,11 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
int count = byteCode.readInt() - low + 1;
|
int count = byteCode.readInt() - low + 1;
|
||||||
positions = new int[count];
|
positions = new int[count];
|
||||||
for( int i = 0; i < count; i++ ) {
|
for( int i = 0; i < count; i++ ) {
|
||||||
offset = Math.max( offset, positions[i] = byteCode.readInt() );
|
positions[i] = startPosition + byteCode.readInt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
branchManager.startSwitch( startPosition, offset, lineNumber, keys, positions, defaultPosition );
|
int switchValuestartPosition = stackManager.getCodePosition( 0 );
|
||||||
|
branchManager.startSwitch( switchValuestartPosition, 0, lineNumber, keys, positions, defaultPosition );
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user