mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-25 15:37:52 +01:00
Add support for floating constants
This commit is contained in:
parent
cd2c51fc0a
commit
0328322724
@ -243,6 +243,24 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
|||||||
codeStream.writeVarint( value );
|
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}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
|
@ -249,8 +249,11 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
case 16: //bipush
|
case 16: //bipush
|
||||||
writeConstInt( byteCode.readByte() );
|
writeConstInt( byteCode.readByte() );
|
||||||
break;
|
break;
|
||||||
|
case 18: //ldc
|
||||||
|
writeConst( constantPool.get( byteCode.readUnsignedByte() ) );
|
||||||
|
break;
|
||||||
case 20: //ldc2_w
|
case 20: //ldc2_w
|
||||||
writeConstLong( (Long)constantPool.get( byteCode.readUnsignedShort() ) );
|
writeConst( constantPool.get( byteCode.readUnsignedShort() ) );
|
||||||
break;
|
break;
|
||||||
case 26: // iload_0
|
case 26: // iload_0
|
||||||
case 27: // iload_1
|
case 27: // iload_1
|
||||||
@ -269,6 +272,8 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
break;
|
break;
|
||||||
case 172: // ireturn
|
case 172: // ireturn
|
||||||
case 173: // lreturn
|
case 173: // lreturn
|
||||||
|
case 174: // freturn
|
||||||
|
case 175: // dreturn
|
||||||
case 177: // return void
|
case 177: // return void
|
||||||
writeReturn();
|
writeReturn();
|
||||||
break;
|
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
|
* Write a constant integer value
|
||||||
*
|
*
|
||||||
@ -301,6 +321,26 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
*/
|
*/
|
||||||
protected abstract void writeConstLong( long value ) throws IOException;
|
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.
|
* Write or Load a local variable.
|
||||||
*
|
*
|
||||||
|
@ -120,6 +120,24 @@ public class TextModuleWriter extends ModuleWriter {
|
|||||||
methodOutput.append( "i64.const " ).append( Long.toString( value ) );
|
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}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user