Add writeOpCode(int) for simpler handling of 2 byte opcodes

This commit is contained in:
Volker 2018-08-11 16:18:01 +02:00
parent d7c13c018e
commit 3e9c26f198
2 changed files with 35 additions and 24 deletions

View File

@ -26,7 +26,6 @@ import java.util.Map;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import de.inetsoftware.classparser.MethodInfo;
import de.inetsoftware.jwebassembly.WasmException; import de.inetsoftware.jwebassembly.WasmException;
import de.inetsoftware.jwebassembly.module.FunctionName; import de.inetsoftware.jwebassembly.module.FunctionName;
import de.inetsoftware.jwebassembly.module.ModuleWriter; import de.inetsoftware.jwebassembly.module.ModuleWriter;
@ -310,19 +309,19 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
protected void writeConst( Number value, ValueType valueType ) throws IOException { protected void writeConst( Number value, ValueType valueType ) throws IOException {
switch( valueType ) { switch( valueType ) {
case i32: case i32:
codeStream.write( I32_CONST ); codeStream.writeOpCode( I32_CONST );
codeStream.writeVarint( value.intValue() ); codeStream.writeVarint( value.intValue() );
break; break;
case i64: case i64:
codeStream.write( I64_CONST ); codeStream.writeOpCode( I64_CONST );
codeStream.writeVarint( value.longValue() ); codeStream.writeVarint( value.longValue() );
break; break;
case f32: case f32:
codeStream.write( F32_CONST ); codeStream.writeOpCode( F32_CONST );
codeStream.writeFloat( value.floatValue() ); codeStream.writeFloat( value.floatValue() );
break; break;
case f64: case f64:
codeStream.write( F64_CONST ); codeStream.writeOpCode( F64_CONST );
codeStream.writeDouble( value.doubleValue() ); codeStream.writeDouble( value.doubleValue() );
break; break;
default: default:
@ -335,7 +334,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
*/ */
@Override @Override
protected void writeLoad( int idx ) throws IOException { protected void writeLoad( int idx ) throws IOException {
codeStream.write( GET_LOCAL ); codeStream.writeOpCode( GET_LOCAL );
codeStream.writeVaruint32( idx ); codeStream.writeVaruint32( idx );
} }
@ -344,7 +343,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
*/ */
@Override @Override
protected void writeStore( int idx ) throws IOException { protected void writeStore( int idx ) throws IOException {
codeStream.write( SET_LOCAL ); codeStream.writeOpCode( SET_LOCAL );
codeStream.writeVaruint32( idx ); codeStream.writeVaruint32( idx );
} }
@ -599,7 +598,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
if( op == 0 ) { if( op == 0 ) {
throw new Error( valueType + "." + numOp ); throw new Error( valueType + "." + numOp );
} }
codeStream.write( op ); codeStream.writeOpCode( op );
} }
/** /**
@ -654,10 +653,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
default: default:
throw new Error( "Unknown cast: " + cast ); throw new Error( "Unknown cast: " + cast );
} }
if( op > 255 ) { codeStream.writeOpCode( op );
codeStream.write( op >> 8 );
}
codeStream.write( op );
} }
/** /**
@ -677,7 +673,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
throw new WasmException( "Call to unknown function: " + name, null, -1 ); throw new WasmException( "Call to unknown function: " + name, null, -1 );
} }
} }
codeStream.write( CALL ); codeStream.writeOpCode( CALL );
codeStream.writeVaruint32( id ); codeStream.writeVaruint32( id );
} }
@ -688,35 +684,35 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
protected void writeBlockCode( @Nonnull WasmBlockOperator op, @Nullable Object data ) throws IOException { protected void writeBlockCode( @Nonnull WasmBlockOperator op, @Nullable Object data ) throws IOException {
switch( op ) { switch( op ) {
case RETURN: case RETURN:
codeStream.write( RETURN ); codeStream.writeOpCode( RETURN );
break; break;
case IF: case IF:
codeStream.write( IF ); codeStream.writeOpCode( IF );
codeStream.write( ((ValueType)data).getCode() ); codeStream.write( ((ValueType)data).getCode() );
break; break;
case ELSE: case ELSE:
codeStream.write( ELSE ); codeStream.writeOpCode( ELSE );
break; break;
case END: case END:
codeStream.write( END ); codeStream.writeOpCode( END );
break; break;
case DROP: case DROP:
codeStream.write( DROP ); codeStream.writeOpCode( DROP );
break; break;
case BLOCK: case BLOCK:
codeStream.write( BLOCK ); codeStream.writeOpCode( BLOCK );
codeStream.write( ValueType.empty.getCode() ); // void; the return type of the block. currently we does not use it codeStream.write( ValueType.empty.getCode() ); // void; the return type of the block. currently we does not use it
break; break;
case BR: case BR:
codeStream.write( BR ); codeStream.writeOpCode( BR );
codeStream.writeVaruint32( (Integer)data ); codeStream.writeVaruint32( (Integer)data );
break; break;
case BR_IF: case BR_IF:
codeStream.write( BR_IF ); codeStream.writeOpCode( BR_IF );
codeStream.writeVaruint32( (Integer)data ); codeStream.writeVaruint32( (Integer)data );
break; break;
case BR_TABLE: case BR_TABLE:
codeStream.write( BR_TABLE ); codeStream.writeOpCode( BR_TABLE );
int[] targets = (int[])data; int[] targets = (int[])data;
codeStream.writeVaruint32( targets.length - 1 ); codeStream.writeVaruint32( targets.length - 1 );
for( int i : targets ) { for( int i : targets ) {
@ -724,11 +720,11 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
} }
break; break;
case LOOP: case LOOP:
codeStream.write( LOOP ); codeStream.writeOpCode( LOOP );
codeStream.write( ValueType.empty.getCode() ); // void; the return type of the loop. currently we does not use it codeStream.write( ValueType.empty.getCode() ); // void; the return type of the loop. currently we does not use it
break; break;
case UNREACHABLE: case UNREACHABLE:
codeStream.write( UNREACHABLE ); codeStream.writeOpCode( UNREACHABLE );
break; break;
default: default:
throw new Error( "Unknown block: " + op ); throw new Error( "Unknown block: " + op );

View File

@ -45,6 +45,21 @@ class WasmOutputStream extends FilterOutputStream {
super( output ); super( output );
} }
/**
* Write a binary operation code.
*
* @param op
* a constant from {@link InstructionOpcodes}
* @throws IOException
* if an I/O error occurs.
*/
public void writeOpCode( int op ) throws IOException {
if( op > 255 ) {
write( op >> 8 );
}
write( op );
}
/** /**
* Write a integer little endian (ever 4 bytes) * Write a integer little endian (ever 4 bytes)
* *