From 625e5fbef728a7039d57bafcd8a3ea0aa43d3a62 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Mon, 20 May 2019 21:28:46 +0200 Subject: [PATCH] pass the type to a virtual function call --- .../jwebassembly/binary/BinaryModuleWriter.java | 4 ++-- .../jwebassembly/module/ModuleWriter.java | 6 ++++-- .../module/WasmCallIndirectInstruction.java | 11 ++++++++--- .../jwebassembly/module/WasmCodeBuilder.java | 3 ++- .../jwebassembly/text/TextModuleWriter.java | 4 ++-- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index 9a14593..d4a81fd 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -1001,10 +1001,10 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod * {@inheritDoc} */ @Override - protected void writeVirtualFunctionCall( FunctionName name, int virtualFunctionIdx ) throws IOException { + protected void writeVirtualFunctionCall( FunctionName name, AnyType type, int virtualFunctionIdx ) throws IOException { callIndirect = true; codeStream.writeOpCode( STRUCT_GET ); - codeStream.writeValueType( ValueType.i32 ); + codeStream.writeValueType( type ); codeStream.writeVaruint32( 0 ); // vtable is ever on position 0 codeStream.writeOpCode( I32_LOAD ); diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java index 62d9dae..a2ad815 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java @@ -229,12 +229,14 @@ public abstract class ModuleWriter implements Closeable { * * @param name * the function name + * @param type + * the base type that should be called * @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 * 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 diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCallIndirectInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmCallIndirectInstruction.java index 54e0e9b..08d54db 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCallIndirectInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCallIndirectInstruction.java @@ -20,6 +20,8 @@ import java.io.IOException; import javax.annotation.Nonnull; +import de.inetsoftware.jwebassembly.module.TypeManager.StructType; + /** * WasmInstruction for a function call. * @@ -30,6 +32,8 @@ class WasmCallIndirectInstruction extends WasmCallInstruction { private int virtualFunctionIdx = -1; + private StructType type; + /** * Create an instance of a function call instruction * @@ -40,8 +44,9 @@ class WasmCallIndirectInstruction extends WasmCallInstruction { * @param lineNumber * 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 ); + this.type = type; } /** @@ -66,10 +71,10 @@ class WasmCallIndirectInstruction extends WasmCallInstruction { */ @Override public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { - if( virtualFunctionIdx < 0 ) { + if( virtualFunctionIdx < 0 || true ) { super.writeTo( writer ); } else { - writer.writeVirtualFunctionCall( getFunctionName(), virtualFunctionIdx ); + writer.writeVirtualFunctionCall( getFunctionName(), type, virtualFunctionIdx ); } } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java index 9f43eec..3b4e413 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java @@ -284,7 +284,8 @@ public abstract class WasmCodeBuilder { * the line number in the Java source code */ 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 ) ); } /** diff --git a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java index c1396b3..9bc6118 100644 --- a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java @@ -529,10 +529,10 @@ public class TextModuleWriter extends ModuleWriter { * {@inheritDoc} */ @Override - protected void writeVirtualFunctionCall( FunctionName name, int virtualFunctionIdx ) throws IOException { + protected void writeVirtualFunctionCall( FunctionName name, AnyType type, int virtualFunctionIdx ) throws IOException { callIndirect = true; 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 ); methodOutput.append( "i32.load offset=" ).append( virtualFunctionIdx * 4 ); // use default alignment newline( methodOutput );