extract method writeSwitchCode()

This commit is contained in:
Volker Berlin 2018-05-12 09:48:09 +02:00
parent 6c971c6525
commit 6a7744e228

View File

@ -831,6 +831,42 @@ public abstract class ModuleWriter implements Closeable {
break;
case 170: // tableswitch
case 171: // lookupswitch
writeSwitchCode( byteCode, op == 171 );
break;
case 172: // ireturn
case 173: // lreturn
case 174: // freturn
case 175: // dreturn
case 177: // return void
writeBlockCode( WasmBlockOperator.RETURN, null );
break;
case 184: // invokestatic
idx = byteCode.readUnsignedShort();
ConstantRef method = (ConstantRef)constantPool.get( idx );
writeFunctionCall( method.getConstantClass().getName() + '.' + method.getName() + method.getType() );
break;
default:
throw new WasmException( "Unimplemented Java byte code operation: " + op, sourceFile, lineNumber );
}
}
} catch( WasmException ex ) {
throw ex;
} catch( Exception ex ) {
throw WasmException.create( ex, sourceFile, lineNumber );
}
}
/**
* Write the both switch operation codes
*
* @param byteCode
* the current stream with a position after the operation code
* @param isLookupSwitch
* true, if the operation was a loopupswitch; false, if the operation was a tableswitch
* @throws IOException
* if any I/O error occur
*/
private void writeSwitchCode( CodeInputStream byteCode, boolean isLookupSwitch ) throws IOException {
int startPosition = byteCode.getCodePosition();
int padding = startPosition % 4;
if( padding > 0 ) {
@ -839,7 +875,7 @@ public abstract class ModuleWriter implements Closeable {
startPosition--;
int defaultPosition = byteCode.readInt();
if( op == 171 ) { // lookupswitch
if( isLookupSwitch ) { // lookupswitch
int count = byteCode.readInt();
int[] keys = new int[count];
int[] positions = new int[count];
@ -891,30 +927,17 @@ public abstract class ModuleWriter implements Closeable {
writeNumericOperator( NumericOperator.sub, ValueType.i32 );
}
}
break;
case 172: // ireturn
case 173: // lreturn
case 174: // freturn
case 175: // dreturn
case 177: // return void
writeBlockCode( WasmBlockOperator.RETURN, null );
break;
case 184: // invokestatic
idx = byteCode.readUnsignedShort();
ConstantRef method = (ConstantRef)constantPool.get( idx );
writeFunctionCall( method.getConstantClass().getName() + '.' + method.getName() + method.getType() );
break;
default:
throw new WasmException( "Unimplemented Java byte code operation: " + op, sourceFile, lineNumber );
}
}
} catch( WasmException ex ) {
throw ex;
} catch( Exception ex ) {
throw WasmException.create( ex, sourceFile, lineNumber );
}
}
/**
* Find the next higher value.
*
* @param current
* the current value
* @param values
* the unordered list of values
* @return the next value or current value if not found.
*/
private static int findNext( int current, int[] values ) {
boolean find = false;
int next = Integer.MAX_VALUE;