diff --git a/src/de/inetsoftware/jwebassembly/module/BranchManger.java b/src/de/inetsoftware/jwebassembly/module/BranchManger.java index 221497c..d78d358 100644 --- a/src/de/inetsoftware/jwebassembly/module/BranchManger.java +++ b/src/de/inetsoftware/jwebassembly/module/BranchManger.java @@ -73,8 +73,8 @@ class BranchManger { * the values of the cases * @param positions * the code positions - * @param the - * code position of the default block + * @param defaultPosition + * the code position of the default block */ void startSwitch( int startPosition, int offset, int lineNumber, int[] keys, int[] positions, int 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 */ private void caculateSwitch( BranchNode parent, SwitchParsedBlock switchBlock, List parsedOperations ) { - int startPosition = switchBlock.startPosition; + int startPosition = ((ParsedBlock)switchBlock).startPosition; int posCount = switchBlock.positions.length; boolean isTable = switchBlock.keys.length == 1; if( isTable ) { @@ -228,13 +228,16 @@ class BranchManger { Arrays.sort( cases, (a, b) -> Integer.compare( a.position, b.position ) ); int blockCount = 0; int lastPosition = -1; - BranchNode brTableNode = new BranchNode( startPosition, startPosition, WasmBlockOperator.BR_TABLE, null ); - BranchNode blockNode = brTableNode; + BranchNode brTableNode = null; + BranchNode blockNode = null; for( int i = 0; i < cases.length; i++ ) { switchCase = cases[i]; switchCase.block = blockCount; int currentPosition = switchCase.position; if( lastPosition != currentPosition ) { + if( blockNode == null ) { + blockNode = brTableNode = new BranchNode( currentPosition, currentPosition, WasmBlockOperator.BR_TABLE, null ); + } lastPosition = currentPosition; blockCount++; BranchNode node = new BranchNode( startPosition, startPosition + currentPosition, WasmBlockOperator.BLOCK, WasmBlockOperator.END ); @@ -303,9 +306,9 @@ class BranchManger { private static class ParsedBlock { private JavaBlockOperator op; - int startPosition; + private int startPosition; - int endPosition; + private int endPosition; private int lineNumber; diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java index 3e148b6..d4dafa6 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java @@ -53,6 +53,8 @@ public abstract class ModuleWriter implements Closeable { private BranchManger branchManager = new BranchManger(); + private ValueStackManger stackManager = new ValueStackManger(); + /** * Prepare the content of the class. * @@ -133,6 +135,7 @@ public abstract class ModuleWriter implements Closeable { locals.clear(); localTable = code.getLocalVariableTable(); + stackManager.reset(); branchManager.reset(); for( CodeInputStream byteCode : code.getByteCodes() ) { prepareBranchManager( byteCode, lineNumber = byteCode.getLineNumber() ); @@ -296,12 +299,30 @@ public abstract class ModuleWriter implements Closeable { while( byteCode.available() > 0 ) { int op = byteCode.readUnsignedByte(); 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 18: // ldc - case 21: //iload - case 22: //lload - case 23: //fload - case 24: //dload case 25: //aload case 54: // istore case 55: // lstore @@ -351,7 +372,7 @@ public abstract class ModuleWriter implements Closeable { } startPosition--; - int defaultPosition = offset = byteCode.readInt(); + int defaultPosition = startPosition + byteCode.readInt(); int[] keys; int[] positions; if( op == 171 ) { // lookupswitch @@ -360,7 +381,7 @@ public abstract class ModuleWriter implements Closeable { positions = new int[nPairs]; for( int i = 0; i < nPairs; i++ ) { keys[i] = byteCode.readInt(); - offset = Math.max( offset, positions[i] = byteCode.readInt() ); + positions[i] = startPosition + byteCode.readInt(); } } else { int low = byteCode.readInt(); @@ -368,10 +389,11 @@ public abstract class ModuleWriter implements Closeable { int count = byteCode.readInt() - low + 1; positions = new int[count]; 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; } }