pass the type to a virtual function call

This commit is contained in:
Volker Berlin 2019-05-20 21:28:46 +02:00
parent fd43aca97b
commit 625e5fbef7
5 changed files with 18 additions and 10 deletions

View File

@ -1001,10 +1001,10 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
protected void writeVirtualFunctionCall( FunctionName name, int virtualFunctionIdx ) throws IOException { protected void writeVirtualFunctionCall( FunctionName name, AnyType type, int virtualFunctionIdx ) throws IOException {
callIndirect = true; callIndirect = true;
codeStream.writeOpCode( STRUCT_GET ); codeStream.writeOpCode( STRUCT_GET );
codeStream.writeValueType( ValueType.i32 ); codeStream.writeValueType( type );
codeStream.writeVaruint32( 0 ); // vtable is ever on position 0 codeStream.writeVaruint32( 0 ); // vtable is ever on position 0
codeStream.writeOpCode( I32_LOAD ); codeStream.writeOpCode( I32_LOAD );

View File

@ -229,12 +229,14 @@ public abstract class ModuleWriter implements Closeable {
* *
* @param name * @param name
* the function name * the function name
* @param type
* the base type that should be called
* @param virtualFunctionIdx * @param virtualFunctionIdx
* the index of the virtual method in the object. If the value < 0 a direct call should be used. * 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 writeVirtualFunctionCall( FunctionName name, int virtualFunctionIdx ) throws IOException; protected abstract void writeVirtualFunctionCall( FunctionName name, AnyType type, int virtualFunctionIdx ) throws IOException;
/** /**
* Write a block/branch code * Write a block/branch code

View File

@ -20,6 +20,8 @@ import java.io.IOException;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
/** /**
* WasmInstruction for a function call. * WasmInstruction for a function call.
* *
@ -30,6 +32,8 @@ class WasmCallIndirectInstruction extends WasmCallInstruction {
private int virtualFunctionIdx = -1; private int virtualFunctionIdx = -1;
private StructType type;
/** /**
* Create an instance of a function call instruction * Create an instance of a function call instruction
* *
@ -40,8 +44,9 @@ class WasmCallIndirectInstruction extends WasmCallInstruction {
* @param lineNumber * @param lineNumber
* the line number in the Java source code * the line number in the Java source code
*/ */
WasmCallIndirectInstruction( FunctionName name, int javaCodePos, int lineNumber ) { WasmCallIndirectInstruction( FunctionName name, @Nonnull StructType type, int javaCodePos, int lineNumber ) {
super( name, javaCodePos, lineNumber ); super( name, javaCodePos, lineNumber );
this.type = type;
} }
/** /**
@ -66,10 +71,10 @@ class WasmCallIndirectInstruction extends WasmCallInstruction {
*/ */
@Override @Override
public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
if( virtualFunctionIdx < 0 ) { if( virtualFunctionIdx < 0 || true ) {
super.writeTo( writer ); super.writeTo( writer );
} else { } else {
writer.writeVirtualFunctionCall( getFunctionName(), virtualFunctionIdx ); writer.writeVirtualFunctionCall( getFunctionName(), type, virtualFunctionIdx );
} }
} }

View File

@ -284,7 +284,8 @@ public abstract class WasmCodeBuilder {
* the line number in the Java source code * the line number in the Java source code
*/ */
protected void addCallVirtualInstruction( FunctionName name, int javaCodePos, int lineNumber ) { protected void addCallVirtualInstruction( FunctionName name, int javaCodePos, int lineNumber ) {
instructions.add( new WasmCallIndirectInstruction( name, javaCodePos, lineNumber ) ); StructType type = types.valueOf( name.className );
instructions.add( new WasmCallIndirectInstruction( name, type, javaCodePos, lineNumber ) );
} }
/** /**

View File

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