The different writeConst summarized to one method

This commit is contained in:
Volker 2018-08-05 18:27:06 +02:00
parent b1eb27c474
commit ff3662db43
5 changed files with 85 additions and 119 deletions

View File

@ -307,36 +307,27 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
protected void writeConstInt( int value ) throws IOException { protected void writeConst( Number value, ValueType valueType ) throws IOException {
switch( valueType ) {
case i32:
codeStream.write( I32_CONST ); codeStream.write( I32_CONST );
codeStream.writeVarint( value ); codeStream.writeVarint( value.intValue() );
} break;
case i64:
/**
* {@inheritDoc}
*/
@Override
protected void writeConstLong( long value ) throws IOException {
codeStream.write( I64_CONST ); codeStream.write( I64_CONST );
codeStream.writeVarint( value ); codeStream.writeVarint( value.longValue() );
} break;
case f32:
/**
* {@inheritDoc}
*/
@Override
protected void writeConstFloat( float value ) throws IOException {
codeStream.write( F32_CONST ); codeStream.write( F32_CONST );
codeStream.writeFloat( value ); codeStream.writeFloat( value.floatValue() );
} break;
case f64:
/**
* {@inheritDoc}
*/
@Override
protected void writeConstDouble( double value ) throws IOException {
codeStream.write( F64_CONST ); codeStream.write( F64_CONST );
codeStream.writeDouble( value ); codeStream.writeDouble( value.doubleValue() );
break;
default:
throw new Error( valueType + " " + value );
}
} }
/** /**

View File

@ -301,31 +301,31 @@ public class ModuleGenerator {
case 7: // iconst_4 case 7: // iconst_4
case 8: // iconst_5 case 8: // iconst_5
stackManager.add( ValueType.i32, codePos ); stackManager.add( ValueType.i32, codePos );
instr = new WasmConstInstruction( Integer.valueOf( op - 3 ), codePos ); instr = new WasmConstInstruction( Integer.valueOf( op - 3 ), ValueType.i32, codePos );
break; break;
case 9: // lconst_0 case 9: // lconst_0
case 10: // lconst_1 case 10: // lconst_1
stackManager.add( ValueType.i64, codePos ); stackManager.add( ValueType.i64, codePos );
instr = new WasmConstInstruction( Long.valueOf( op - 9 ), codePos ); instr = new WasmConstInstruction( Long.valueOf( op - 9 ), ValueType.i64, codePos );
break; break;
case 11: // fconst_0 case 11: // fconst_0
case 12: // fconst_1 case 12: // fconst_1
case 13: // fconst_2 case 13: // fconst_2
stackManager.add( ValueType.f32, codePos ); stackManager.add( ValueType.f32, codePos );
instr = new WasmConstInstruction( Float.valueOf( op - 11 ), codePos ); instr = new WasmConstInstruction( Float.valueOf( op - 11 ), ValueType.f32, codePos );
break; break;
case 14: // dconst_0 case 14: // dconst_0
case 15: // dconst_1 case 15: // dconst_1
stackManager.add( ValueType.f64, codePos ); stackManager.add( ValueType.f64, codePos );
instr = new WasmConstInstruction( Double.valueOf( op - 14 ), codePos ); instr = new WasmConstInstruction( Double.valueOf( op - 14 ), ValueType.f64, codePos );
break; break;
case 16: // bipush case 16: // bipush
stackManager.add( ValueType.i32, codePos ); stackManager.add( ValueType.i32, codePos );
instr = new WasmConstInstruction( Integer.valueOf( byteCode.readByte() ), codePos ); instr = new WasmConstInstruction( Integer.valueOf( byteCode.readByte() ), ValueType.i32, codePos );
break; break;
case 17: // sipush case 17: // sipush
stackManager.add( ValueType.i32, codePos ); stackManager.add( ValueType.i32, codePos );
instr = new WasmConstInstruction( Integer.valueOf( byteCode.readShort() ), codePos ); instr = new WasmConstInstruction( Integer.valueOf( byteCode.readShort() ), ValueType.i32, codePos );
break; break;
case 18: // ldc case 18: // ldc
stackManager.add( null, codePos ); stackManager.add( null, codePos );
@ -507,11 +507,11 @@ public class ModuleGenerator {
//TODO can be implemented with a helper function like: (a - (long)(a / b) * (double)b) //TODO can be implemented with a helper function like: (a - (long)(a / b) * (double)b)
throw new WasmException( "Modulo/Remainder for floating numbers is not supported in WASM. Use int or long data types." + op, sourceFile, byteCode.getLineNumber() ); throw new WasmException( "Modulo/Remainder for floating numbers is not supported in WASM. Use int or long data types." + op, sourceFile, byteCode.getLineNumber() );
case 116: // ineg case 116: // ineg
instructions.add( new WasmConstInstruction( -1, codePos ) ); instructions.add( new WasmConstInstruction( -1, ValueType.i32, codePos ) );
instr = new WasmNumericInstruction( NumericOperator.mul, ValueType.i32, codePos ); instr = new WasmNumericInstruction( NumericOperator.mul, ValueType.i32, codePos );
break; break;
case 117: // lneg case 117: // lneg
instructions.add( new WasmConstInstruction( (long)-1, codePos ) ) ; instructions.add( new WasmConstInstruction( (long)-1, ValueType.i64, codePos ) ) ;
instr = new WasmNumericInstruction( NumericOperator.mul, ValueType.i64, codePos ); instr = new WasmNumericInstruction( NumericOperator.mul, ValueType.i64, codePos );
break; break;
case 118: // fneg case 118: // fneg
@ -562,7 +562,7 @@ public class ModuleGenerator {
case 132: // iinc case 132: // iinc
int idx = byteCode.readUnsignedByte(); int idx = byteCode.readUnsignedByte();
instructions.add( new WasmLoadStoreInstruction( true, idx, localVariables, codePos ) ); instructions.add( new WasmLoadStoreInstruction( true, idx, localVariables, codePos ) );
instructions.add( new WasmConstInstruction( (int)byteCode.readByte(), codePos ) ); instructions.add( new WasmConstInstruction( (int)byteCode.readByte(), ValueType.i32, codePos ) );
instructions.add( new WasmNumericInstruction( NumericOperator.add, ValueType.i32, codePos) ); instructions.add( new WasmNumericInstruction( NumericOperator.add, ValueType.i32, codePos) );
instr = new WasmLoadStoreInstruction( false, idx, localVariables, codePos ); instr = new WasmLoadStoreInstruction( false, idx, localVariables, codePos );
break; break;
@ -606,7 +606,7 @@ public class ModuleGenerator {
instr = new WasmConvertInstruction( ValueTypeConvertion.i2b, codePos ); instr = new WasmConvertInstruction( ValueTypeConvertion.i2b, codePos );
break; break;
case 146: // i2c case 146: // i2c
instructions.add( new WasmConstInstruction( 0xFFFF, codePos ) ); instructions.add( new WasmConstInstruction( 0xFFFF, ValueType.i32, codePos ) );
instr = new WasmNumericInstruction( NumericOperator.and, ValueType.i32, codePos ); instr = new WasmNumericInstruction( NumericOperator.and, ValueType.i32, codePos );
break; break;
case 147: // i2s case 147: // i2s
@ -778,7 +778,7 @@ public class ModuleGenerator {
for( int i = 0; i < positions.length; i++ ) { for( int i = 0; i < positions.length; i++ ) {
if( positions[i] == currentPos ) { if( positions[i] == currentPos ) {
instructions.add( new WasmLoadStoreInstruction( true, tempI32, localVariables, codePos ) ); instructions.add( new WasmLoadStoreInstruction( true, tempI32, localVariables, codePos ) );
instructions.add( new WasmConstInstruction( keys[i], codePos ) ); instructions.add( new WasmConstInstruction( keys[i], ValueType.i32, codePos ) );
instructions.add( new WasmNumericInstruction( NumericOperator.eq, ValueType.i32, codePos ) ); instructions.add( new WasmNumericInstruction( NumericOperator.eq, ValueType.i32, codePos ) );
instructions.add( new WasmBlockInstruction( WasmBlockOperator.BR_IF, block, codePos ) ); instructions.add( new WasmBlockInstruction( WasmBlockOperator.BR_IF, block, codePos ) );
} }
@ -798,7 +798,7 @@ public class ModuleGenerator {
positions[i] = startPosition + byteCode.readInt(); positions[i] = startPosition + byteCode.readInt();
} }
if( low != 0 ) { // the br_table starts ever with the value 0. That we need to subtract the start value if it different if( low != 0 ) { // the br_table starts ever with the value 0. That we need to subtract the start value if it different
instructions.add( new WasmConstInstruction( low, codePos ) ); instructions.add( new WasmConstInstruction( low, ValueType.i32, codePos ) );
instructions.add( new WasmNumericInstruction( NumericOperator.sub, ValueType.i32, codePos ) ); instructions.add( new WasmNumericInstruction( NumericOperator.sub, ValueType.i32, codePos ) );
} }
} }
@ -862,7 +862,7 @@ public class ModuleGenerator {
* if any I/O errors occur. * if any I/O errors occur.
*/ */
private void opIfCondition( NumericOperator compareOp, CodeInputStream byteCode, int codePos ) throws IOException { private void opIfCondition( NumericOperator compareOp, CodeInputStream byteCode, int codePos ) throws IOException {
instructions.add( new WasmConstInstruction( 0, codePos ) ); instructions.add( new WasmConstInstruction( 0, ValueType.i32, codePos ) );
opIfCompareCondition( compareOp, byteCode, codePos ); opIfCompareCondition( compareOp, byteCode, codePos );
} }

View File

@ -113,44 +113,16 @@ public abstract class ModuleWriter implements Closeable {
protected abstract void writeMethodFinish( List<ValueType> locals ) throws IOException; protected abstract void writeMethodFinish( List<ValueType> locals ) throws IOException;
/** /**
* Write a constant integer value * Write a constant number value
* *
* @param value * @param value
* the value * the value
* @param valueType
* the data type of the number
* @throws IOException * @throws IOException
* if any I/O error occur * if any I/O error occur
*/ */
protected abstract void writeConstInt( int value ) throws IOException; protected abstract void writeConst( Number value, ValueType valueType ) throws IOException;
/**
* Write a constant long value
*
* @param value
* the value
* @throws IOException
* if any I/O error occur
*/
protected abstract void writeConstLong( long value ) throws IOException;
/**
* Write a constant float value
*
* @param value
* the value
* @throws IOException
* if any I/O error occur
*/
protected abstract void writeConstFloat( float value ) throws IOException;
/**
* Write a constant double value
*
* @param value
* the value
* @throws IOException
* if any I/O error occur
*/
protected abstract void writeConstDouble( double value ) throws IOException;
/** /**
* Write a variable load. * Write a variable load.

View File

@ -32,6 +32,24 @@ class WasmConstInstruction extends WasmInstruction {
private final Number value; private final Number value;
private final ValueType valueType;
/**
* Create an instance of a constant instruction
*
* @param value
* the constant value
* @param valueType
* the data type of the number
* @param javaCodePos
* the code position/offset in the Java method
*/
WasmConstInstruction( Number value, ValueType valueType, int javaCodePos ) {
super( javaCodePos );
this.value = value;
this.valueType = valueType;
}
/** /**
* Create an instance of a constant instruction * Create an instance of a constant instruction
* *
@ -41,25 +59,37 @@ class WasmConstInstruction extends WasmInstruction {
* the code position/offset in the Java method * the code position/offset in the Java method
*/ */
WasmConstInstruction( Number value, int javaCodePos ) { WasmConstInstruction( Number value, int javaCodePos ) {
super( javaCodePos ); this( value, getValueType( value ), javaCodePos );
this.value = value; }
/**
* Find the matching ValueType for the given value.
*
* @param value
* the constant value
* @return the ValueType
*/
@Nonnull
private static ValueType getValueType( Number value ) {
Class<?> clazz = value.getClass();
if( clazz == Integer.class ) {
return ValueType.i32;
} else if( clazz == Long.class ) {
return ValueType.i64;
} else if( clazz == Float.class ) {
return ValueType.f32;
} else if( clazz == Double.class ) {
return ValueType.f64;
} else {
throw new WasmException( "Not supported constant type: " + clazz, null, -1 );
}
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
Class<?> clazz = value.getClass(); writer.writeConst( value, valueType );
if( clazz == Integer.class ) {
writer.writeConstInt( ((Integer)value).intValue() );
} else if( clazz == Long.class ) {
writer.writeConstLong( ((Long)value).longValue() );
} else if( clazz == Float.class ) {
writer.writeConstFloat( ((Float)value).floatValue() );
} else if( clazz == Double.class ) {
writer.writeConstDouble( ((Double)value).doubleValue() );
} else {
throw new WasmException( "Not supported constant type: " + clazz, null, -1 );
}
} }
} }

View File

@ -130,36 +130,9 @@ public class TextModuleWriter extends ModuleWriter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
protected void writeConstInt( int value ) throws IOException { protected void writeConst( Number value, ValueType valueType ) throws IOException {
newline( methodOutput ); newline( methodOutput );
methodOutput.append( "i32.const " ).append( Integer.toString( value ) ); methodOutput.append( valueType ).append( ".const " ).append( value );
}
/**
* {@inheritDoc}
*/
@Override
protected void writeConstLong( long value ) throws IOException {
newline( methodOutput );
methodOutput.append( "i64.const " ).append( Long.toString( value ) );
}
/**
* {@inheritDoc}
*/
@Override
protected void writeConstFloat( float value ) throws IOException {
newline( methodOutput );
methodOutput.append( "f32.const " ).append( Float.toString( value ) );
}
/**
* {@inheritDoc}
*/
@Override
protected void writeConstDouble( double value ) throws IOException {
newline( methodOutput );
methodOutput.append( "f64.const " ).append( Double.toString( value ) );
} }
/** /**