Skip to content

Commit fcaa15d

Browse files
VolkerVolker
authored andcommitted
Fix SWITCH start position detection and replace erroneous ValueStackManager
1 parent ae39ccb commit fcaa15d

File tree

3 files changed

+26
-132
lines changed

3 files changed

+26
-132
lines changed

src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ public class ModuleGenerator {
5353

5454
private BranchManger branchManager = new BranchManger( instructions );
5555

56-
private ValueStackManger stackManager = new ValueStackManger();
57-
5856
/**
5957
* Create a new generator.
6058
*
@@ -162,7 +160,6 @@ private void writeMethod( MethodInfo method ) throws WasmException {
162160
writeMethodSignature( method );
163161

164162
localVariables.reset();
165-
stackManager.reset();
166163
branchManager.reset();
167164

168165
byteCode = code.getByteCode();
@@ -300,40 +297,32 @@ private void writeCode( CodeInputStream byteCode, ConstantPool constantPool ) t
300297
case 6: // iconst_3
301298
case 7: // iconst_4
302299
case 8: // iconst_5
303-
stackManager.add( ValueType.i32, codePos );
304300
instr = new WasmConstInstruction( Integer.valueOf( op - 3 ), ValueType.i32, codePos );
305301
break;
306302
case 9: // lconst_0
307303
case 10: // lconst_1
308-
stackManager.add( ValueType.i64, codePos );
309304
instr = new WasmConstInstruction( Long.valueOf( op - 9 ), ValueType.i64, codePos );
310305
break;
311306
case 11: // fconst_0
312307
case 12: // fconst_1
313308
case 13: // fconst_2
314-
stackManager.add( ValueType.f32, codePos );
315309
instr = new WasmConstInstruction( Float.valueOf( op - 11 ), ValueType.f32, codePos );
316310
break;
317311
case 14: // dconst_0
318312
case 15: // dconst_1
319-
stackManager.add( ValueType.f64, codePos );
320313
instr = new WasmConstInstruction( Double.valueOf( op - 14 ), ValueType.f64, codePos );
321314
break;
322315
case 16: // bipush
323-
stackManager.add( ValueType.i32, codePos );
324316
instr = new WasmConstInstruction( Integer.valueOf( byteCode.readByte() ), ValueType.i32, codePos );
325317
break;
326318
case 17: // sipush
327-
stackManager.add( ValueType.i32, codePos );
328319
instr = new WasmConstInstruction( Integer.valueOf( byteCode.readShort() ), ValueType.i32, codePos );
329320
break;
330321
case 18: // ldc
331-
stackManager.add( null, codePos );
332322
instr = new WasmConstInstruction( (Number)constantPool.get( byteCode.readUnsignedByte() ), codePos );
333323
break;
334324
case 19: // ldc_w
335325
case 20: // ldc2_w
336-
stackManager.add( null, codePos );
337326
instr = new WasmConstInstruction( (Number)constantPool.get( byteCode.readUnsignedShort() ), codePos );
338327
break;
339328
case 21: // iload
@@ -436,7 +425,6 @@ private void writeCode( CodeInputStream byteCode, ConstantPool constantPool ) t
436425
//TODO case 86: // sastore
437426
case 87: // pop
438427
case 88: // pop2
439-
stackManager.remove();
440428
instr = new WasmBlockInstruction( WasmBlockOperator.DROP, null, codePos );
441429
break;
442430
case 89: // dup: duplicate the value on top of the stack
@@ -706,11 +694,6 @@ private void writeCode( CodeInputStream byteCode, ConstantPool constantPool ) t
706694
case 184: // invokestatic
707695
idx = byteCode.readUnsignedShort();
708696
ConstantRef method = (ConstantRef)constantPool.get( idx );
709-
String signature = method.getType();
710-
type = getValueType( signature, signature.indexOf( ')' ) + 1 );
711-
if( type != null ) {
712-
stackManager.add( type, codePos );
713-
}
714697
instr = new WasmCallInstruction( method, codePos );
715698
break;
716699
//TODO case 185: // invokeinterface
@@ -762,12 +745,13 @@ private void writeSwitchCode( CodeInputStream byteCode, boolean isLookupSwitch )
762745
byteCode.skip( 4 - padding );
763746
}
764747
startPosition--;
748+
int switchValuestartPosition = findPreviousPushCodePosition();
765749

766750
int defaultPosition = startPosition + byteCode.readInt();
767751
int[] keys;
768752
int[] positions;
769753
if( isLookupSwitch ) { // lookupswitch
770-
localVariables.useTempI32();
754+
localVariables.useTempI32(); //TODO
771755
int count = byteCode.readInt();
772756
keys = new int[count];
773757
positions = new int[count];
@@ -821,10 +805,31 @@ private void writeSwitchCode( CodeInputStream byteCode, boolean isLookupSwitch )
821805
instructions.add( new WasmNumericInstruction( NumericOperator.sub, ValueType.i32, codePos ) );
822806
}
823807
}
824-
int switchValuestartPosition = stackManager.getCodePosition( 0 );
825808
branchManager.addSwitchOperator( switchValuestartPosition, 0, lineNumber, keys, positions, defaultPosition );
826809
}
827810

811+
/**
812+
* We need one value from the stack inside of a block. We need to find the code position on which the block can
813+
* start. If this a function call or numeric expression this can be complex to find the right point.
814+
*
815+
* @return the code position
816+
*/
817+
private int findPreviousPushCodePosition() {
818+
int valueCount = 0;
819+
for( int i = instructions.size() - 1; i >= 0; i-- ) {
820+
WasmInstruction instr = instructions.get( i );
821+
ValueType valueType = instr.getPushValueType();
822+
if( valueType != null ) {
823+
valueCount++;
824+
}
825+
valueCount -= instr.getPopCount();
826+
if( valueCount == 1 ) {
827+
return instr.getCodePosition();
828+
}
829+
}
830+
throw new WasmException( "Switch start position not found", sourceFile, -1 ); // should never occur
831+
}
832+
828833
/**
829834
* Find the next higher value.
830835
*
@@ -861,7 +866,6 @@ private static int findNext( int current, int[] values ) {
861866
*/
862867
@Nonnull
863868
private WasmLoadStoreInstruction loadStore( ValueType valueType, boolean load, @Nonnegative int idx, int codePos ) {
864-
stackManager.add( valueType, codePos );
865869
localVariables.use( valueType, idx );
866870
return new WasmLoadStoreInstruction( load, idx, localVariables, codePos );
867871
}

src/de/inetsoftware/jwebassembly/module/ValueStackManger.java

Lines changed: 0 additions & 110 deletions
This file was deleted.

test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,12 @@ static int ifCompare() {
193193

194194
@Export
195195
static int switchDirect() {
196-
return tableSwitch(10) + (tableSwitch( 9 ) * 10) + + (tableSwitch( -1 ) * 100) + (lookupSwitch(Integer.MAX_VALUE) * 1000) + (lookupSwitch(0) * 10000 );
196+
return tableSwitch(10) + (tableSwitch( 9 ) * 10) + (tableSwitch( -1 ) * 100) + (lookupSwitch(Integer.MAX_VALUE) * 1000) + (lookupSwitch(0) * 10000 );
197197
}
198198

199199
private static int tableSwitch( int a ) {
200200
int b;
201-
switch(a){
201+
switch( 1 + a - 1 ){
202202
case 8:
203203
case 9:
204204
b = 2;

0 commit comments

Comments
 (0)