Add support for floating constants

This commit is contained in:
Volker Berlin 2017-04-09 12:44:01 +02:00
parent cd2c51fc0a
commit 0328322724
3 changed files with 77 additions and 1 deletions

View File

@ -243,6 +243,24 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
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 );
}
/**
* {@inheritDoc}
*/

View File

@ -249,8 +249,11 @@ public abstract class ModuleWriter implements Closeable {
case 16: //bipush
writeConstInt( byteCode.readByte() );
break;
case 18: //ldc
writeConst( constantPool.get( byteCode.readUnsignedByte() ) );
break;
case 20: //ldc2_w
writeConstLong( (Long)constantPool.get( byteCode.readUnsignedShort() ) );
writeConst( constantPool.get( byteCode.readUnsignedShort() ) );
break;
case 26: // iload_0
case 27: // iload_1
@ -269,6 +272,8 @@ public abstract class ModuleWriter implements Closeable {
break;
case 172: // ireturn
case 173: // lreturn
case 174: // freturn
case 175: // dreturn
case 177: // return void
writeReturn();
break;
@ -281,6 +286,21 @@ public abstract class ModuleWriter implements Closeable {
}
}
private void writeConst( Object value ) throws IOException, WasmException {
Class<?> clazz = value.getClass();
if( clazz == Integer.class ) {
writeConstInt( ((Integer)value).intValue() );
} else if( clazz == Long.class ) {
writeConstLong( ((Long)value).longValue() );
} else if( clazz == Float.class ) {
writeConstFloat( ((Float)value).floatValue() );
} else if( clazz == Double.class ) {
writeConstDouble( ((Double)value).doubleValue() );
} else {
throw new WasmException( "Not supported constant type: " + clazz, -1 );
}
}
/**
* Write a constant integer value
*
@ -301,6 +321,26 @@ public abstract class ModuleWriter implements Closeable {
*/
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 or Load a local variable.
*

View File

@ -120,6 +120,24 @@ public class TextModuleWriter extends ModuleWriter {
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 ) );
}
/**
* {@inheritDoc}
*/