pass virtual function index

This commit is contained in:
Volker Berlin 2019-05-18 21:37:19 +02:00
parent 5826d6dded
commit 30efaaed95
4 changed files with 23 additions and 7 deletions

View File

@ -1001,8 +1001,16 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
protected void writeFunctionCallIndirect( FunctionName name ) throws IOException { protected void writeVirtualFunctionCall( FunctionName name, int virtualFunctionIdx ) throws IOException {
callIndirect = true; callIndirect = true;
codeStream.writeOpCode( STRUCT_GET );
codeStream.writeValueType( ValueType.i32 );
codeStream.writeVaruint32( 0 ); // vtable is ever on position 0
codeStream.writeOpCode( I32_LOAD );
codeStream.write( 2 ); // 32 alignment
codeStream.writeVaruint32( virtualFunctionIdx * 4 );
Function func = getFunction( name ); Function func = getFunction( name );
codeStream.writeOpCode( CALL_INDIRECT ); codeStream.writeOpCode( CALL_INDIRECT );
codeStream.writeVaruint32( func.typeId ); codeStream.writeVaruint32( func.typeId );

View File

@ -225,14 +225,16 @@ public abstract class ModuleWriter implements Closeable {
protected abstract void writeFunctionCall( FunctionName name ) throws IOException; protected abstract void writeFunctionCall( FunctionName name ) throws IOException;
/** /**
* Write a call indirect to a function. * Write a function call to an instance function. On the stack there must be the object.
* *
* @param name * @param name
* the function name * the function name
* @param virtualFunctionIdx
* the index of the virtual method in the object. If the value < 0 a direct call should be used.
* @throws IOException * @throws IOException
* if any I/O error occur * if any I/O error occur
*/ */
protected abstract void writeFunctionCallIndirect( FunctionName name ) throws IOException; protected abstract void writeVirtualFunctionCall( FunctionName name, int virtualFunctionIdx ) throws IOException;
/** /**
* Write a block/branch code * Write a block/branch code

View File

@ -28,6 +28,8 @@ import javax.annotation.Nonnull;
*/ */
class WasmCallIndirectInstruction extends WasmCallInstruction { class WasmCallIndirectInstruction extends WasmCallInstruction {
private int virtualFunctionIdx = -1;
/** /**
* Create an instance of a function call instruction * Create an instance of a function call instruction
* *
@ -55,10 +57,10 @@ class WasmCallIndirectInstruction extends WasmCallInstruction {
*/ */
@Override @Override
public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
if( true ) { // TODO if( virtualFunctionIdx < 0 ) {
super.writeTo( writer ); super.writeTo( writer );
} else { } else {
writer.writeFunctionCallIndirect( getFunctionName() ); writer.writeVirtualFunctionCall( getFunctionName(), virtualFunctionIdx );
} }
} }

View File

@ -529,10 +529,14 @@ public class TextModuleWriter extends ModuleWriter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
protected void writeFunctionCallIndirect( FunctionName name ) throws IOException { protected void writeVirtualFunctionCall( FunctionName name, int virtualFunctionIdx ) throws IOException {
callIndirect = true; callIndirect = true;
newline( methodOutput ); newline( methodOutput );
methodOutput.append( "call_indirect $" ).append( normalizeName( name ) ); methodOutput.append( "struct.get i32 0 ;;vtable" ); // vtable is ever on position 0
newline( methodOutput );
methodOutput.append( "i32.load offset=" ).append( virtualFunctionIdx * 4 ); // use default alignment
newline( methodOutput );
methodOutput.append( "call_indirect (type $" ).append( normalizeName( name ) ).append( ')' );
} }
/** /**