From ff3662db43f115845cf222beb7a8d0aad83a0558 Mon Sep 17 00:00:00 2001 From: Volker Date: Sun, 5 Aug 2018 18:27:06 +0200 Subject: [PATCH] The different writeConst summarized to one method --- .../binary/BinaryModuleWriter.java | 51 +++++++--------- .../jwebassembly/module/ModuleGenerator.java | 26 ++++---- .../jwebassembly/module/ModuleWriter.java | 36 ++--------- .../module/WasmConstInstruction.java | 60 ++++++++++++++----- .../jwebassembly/text/TextModuleWriter.java | 31 +--------- 5 files changed, 85 insertions(+), 119 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index 79be627..a622708 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -307,36 +307,27 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod * {@inheritDoc} */ @Override - protected void writeConstInt( int value ) throws IOException { - codeStream.write( I32_CONST ); - codeStream.writeVarint( value ); - } - - /** - * {@inheritDoc} - */ - @Override - protected void writeConstLong( long value ) throws IOException { - codeStream.write( I64_CONST ); - codeStream.writeVarint( value ); - } - - /** - * {@inheritDoc} - */ - @Override - protected void writeConstFloat( float value ) throws IOException { - codeStream.write( F32_CONST ); - codeStream.writeFloat( value ); - } - - /** - * {@inheritDoc} - */ - @Override - protected void writeConstDouble( double value ) throws IOException { - codeStream.write( F64_CONST ); - codeStream.writeDouble( value ); + protected void writeConst( Number value, ValueType valueType ) throws IOException { + switch( valueType ) { + case i32: + codeStream.write( I32_CONST ); + codeStream.writeVarint( value.intValue() ); + break; + case i64: + codeStream.write( I64_CONST ); + codeStream.writeVarint( value.longValue() ); + break; + case f32: + codeStream.write( F32_CONST ); + codeStream.writeFloat( value.floatValue() ); + break; + case f64: + codeStream.write( F64_CONST ); + codeStream.writeDouble( value.doubleValue() ); + break; + default: + throw new Error( valueType + " " + value ); + } } /** diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index df0cbe1..4be0728 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -301,31 +301,31 @@ public class ModuleGenerator { case 7: // iconst_4 case 8: // iconst_5 stackManager.add( ValueType.i32, codePos ); - instr = new WasmConstInstruction( Integer.valueOf( op - 3 ), codePos ); + instr = new WasmConstInstruction( Integer.valueOf( op - 3 ), ValueType.i32, codePos ); break; case 9: // lconst_0 case 10: // lconst_1 stackManager.add( ValueType.i64, codePos ); - instr = new WasmConstInstruction( Long.valueOf( op - 9 ), codePos ); + instr = new WasmConstInstruction( Long.valueOf( op - 9 ), ValueType.i64, codePos ); break; case 11: // fconst_0 case 12: // fconst_1 case 13: // fconst_2 stackManager.add( ValueType.f32, codePos ); - instr = new WasmConstInstruction( Float.valueOf( op - 11 ), codePos ); + instr = new WasmConstInstruction( Float.valueOf( op - 11 ), ValueType.f32, codePos ); break; case 14: // dconst_0 case 15: // dconst_1 stackManager.add( ValueType.f64, codePos ); - instr = new WasmConstInstruction( Double.valueOf( op - 14 ), codePos ); + instr = new WasmConstInstruction( Double.valueOf( op - 14 ), ValueType.f64, codePos ); break; case 16: // bipush stackManager.add( ValueType.i32, codePos ); - instr = new WasmConstInstruction( Integer.valueOf( byteCode.readByte() ), codePos ); + instr = new WasmConstInstruction( Integer.valueOf( byteCode.readByte() ), ValueType.i32, codePos ); break; case 17: // sipush stackManager.add( ValueType.i32, codePos ); - instr = new WasmConstInstruction( Integer.valueOf( byteCode.readShort() ), codePos ); + instr = new WasmConstInstruction( Integer.valueOf( byteCode.readShort() ), ValueType.i32, codePos ); break; case 18: // ldc 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) 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 - instructions.add( new WasmConstInstruction( -1, codePos ) ); + instructions.add( new WasmConstInstruction( -1, ValueType.i32, codePos ) ); instr = new WasmNumericInstruction( NumericOperator.mul, ValueType.i32, codePos ); break; 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 ); break; case 118: // fneg @@ -562,7 +562,7 @@ public class ModuleGenerator { case 132: // iinc int idx = byteCode.readUnsignedByte(); 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) ); instr = new WasmLoadStoreInstruction( false, idx, localVariables, codePos ); break; @@ -606,7 +606,7 @@ public class ModuleGenerator { instr = new WasmConvertInstruction( ValueTypeConvertion.i2b, codePos ); break; 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 ); break; case 147: // i2s @@ -778,7 +778,7 @@ public class ModuleGenerator { for( int i = 0; i < positions.length; i++ ) { if( positions[i] == currentPos ) { 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 WasmBlockInstruction( WasmBlockOperator.BR_IF, block, codePos ) ); } @@ -798,7 +798,7 @@ public class ModuleGenerator { 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 - instructions.add( new WasmConstInstruction( low, codePos ) ); + instructions.add( new WasmConstInstruction( low, 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. */ 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 ); } diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java index 034509c..386ed95 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java @@ -113,44 +113,16 @@ public abstract class ModuleWriter implements Closeable { protected abstract void writeMethodFinish( List locals ) throws IOException; /** - * Write a constant integer value + * Write a constant number value * * @param value * the value + * @param valueType + * the data type of the number * @throws IOException * if any I/O error occur */ - protected abstract void writeConstInt( int value ) 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; + protected abstract void writeConst( Number value, ValueType valueType ) throws IOException; /** * Write a variable load. diff --git a/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java index e1796d0..668309f 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java @@ -30,7 +30,25 @@ import de.inetsoftware.jwebassembly.WasmException; */ 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 @@ -41,25 +59,37 @@ class WasmConstInstruction extends WasmInstruction { * the code position/offset in the Java method */ WasmConstInstruction( Number value, int javaCodePos ) { - super( javaCodePos ); - this.value = value; + this( value, getValueType( value ), javaCodePos ); + } + + /** + * 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} */ public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { - Class clazz = value.getClass(); - 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 ); - } + writer.writeConst( value, valueType ); } } diff --git a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java index 07710cf..640fd0c 100644 --- a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java @@ -130,36 +130,9 @@ public class TextModuleWriter extends ModuleWriter { * {@inheritDoc} */ @Override - protected void writeConstInt( int value ) throws IOException { + protected void writeConst( Number value, ValueType valueType ) throws IOException { newline( methodOutput ); - methodOutput.append( "i32.const " ).append( Integer.toString( 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 ) ); + methodOutput.append( valueType ).append( ".const " ).append( value ); } /**