Skip to content

Commit 02696a6

Browse files
committed
use a synthetic function call to calculate the function id of a virtual call.
1 parent db002f8 commit 02696a6

File tree

15 files changed

+79
-34
lines changed

15 files changed

+79
-34
lines changed

src/de/inetsoftware/jwebassembly/JWebAssembly.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
import de.inetsoftware.jwebassembly.binary.BinaryModuleWriter;
4141
import de.inetsoftware.jwebassembly.module.ModuleGenerator;
4242
import de.inetsoftware.jwebassembly.module.ModuleWriter;
43+
import de.inetsoftware.jwebassembly.module.WasmOptions;
4344
import de.inetsoftware.jwebassembly.module.WasmTarget;
4445
import de.inetsoftware.jwebassembly.text.TextModuleWriter;
45-
import de.inetsoftware.jwebassembly.wasm.WasmOptions;
4646

4747
/**
4848
* The main class of the compiler.

src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import de.inetsoftware.jwebassembly.module.ModuleWriter;
3333
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
3434
import de.inetsoftware.jwebassembly.module.ValueTypeConvertion;
35+
import de.inetsoftware.jwebassembly.module.WasmOptions;
3536
import de.inetsoftware.jwebassembly.module.WasmTarget;
3637
import de.inetsoftware.jwebassembly.sourcemap.SourceMapWriter;
3738
import de.inetsoftware.jwebassembly.sourcemap.SourceMapping;
@@ -44,7 +45,6 @@
4445
import de.inetsoftware.jwebassembly.wasm.ValueType;
4546
import de.inetsoftware.jwebassembly.wasm.VariableOperator;
4647
import de.inetsoftware.jwebassembly.wasm.WasmBlockOperator;
47-
import de.inetsoftware.jwebassembly.wasm.WasmOptions;
4848

4949
/**
5050
* Module Writer for binary format. http://webassembly.org/docs/binary-encoding/

src/de/inetsoftware/jwebassembly/module/CodeOptimizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*
2626
* @author Volker Berlin
2727
*/
28-
public class CodeOptimizer {
28+
class CodeOptimizer {
2929

3030
/**
3131
* Optimize the code before writing.

src/de/inetsoftware/jwebassembly/module/FunctionManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*
3535
* @author Volker Berlin
3636
*/
37-
public class FunctionManager {
37+
class FunctionManager {
3838

3939
private final Map<FunctionName, FunctionState> states = new LinkedHashMap<>();
4040

src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
4747
import de.inetsoftware.jwebassembly.wasm.StructOperator;
4848
import de.inetsoftware.jwebassembly.wasm.ValueType;
49-
import de.inetsoftware.jwebassembly.wasm.WasmOptions;
5049
import de.inetsoftware.jwebassembly.watparser.WatParser;
5150

5251
/**

src/de/inetsoftware/jwebassembly/module/ModuleWriter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import de.inetsoftware.jwebassembly.wasm.ValueType;
3434
import de.inetsoftware.jwebassembly.wasm.VariableOperator;
3535
import de.inetsoftware.jwebassembly.wasm.WasmBlockOperator;
36-
import de.inetsoftware.jwebassembly.wasm.WasmOptions;
3736

3837
/**
3938
* Module Writer base class.

src/de/inetsoftware/jwebassembly/module/StringManager.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@
2727

2828
import de.inetsoftware.jwebassembly.api.annotation.WasmTextCode;
2929
import de.inetsoftware.jwebassembly.wasm.ValueType;
30-
import de.inetsoftware.jwebassembly.wasm.WasmOptions;
3130

3231
/**
3332
* Handle all the constant strings. The constant strings will be write into the data section.
3433
*
3534
* @author Volker Berlin
3635
*/
37-
public class StringManager extends LinkedHashMap<String, Integer> {
36+
class StringManager extends LinkedHashMap<String, Integer> {
3837

3938
/**
4039
* Signature of method stringConstant.
@@ -54,7 +53,7 @@ public class StringManager extends LinkedHashMap<String, Integer> {
5453
* @param options
5554
* compiler properties and shared managers
5655
*/
57-
public StringManager( WasmOptions options ) {
56+
StringManager( WasmOptions options ) {
5857
this.functions = options.functions;
5958
}
6059

src/de/inetsoftware/jwebassembly/module/TypeManager.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.util.List;
2424
import java.util.Map;
2525

26+
import javax.annotation.Nonnull;
27+
2628
import de.inetsoftware.classparser.ClassFile;
2729
import de.inetsoftware.classparser.ClassFile.Type;
2830
import de.inetsoftware.classparser.ConstantClass;
@@ -34,7 +36,6 @@
3436
import de.inetsoftware.jwebassembly.wasm.ArrayType;
3537
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
3638
import de.inetsoftware.jwebassembly.wasm.ValueType;
37-
import de.inetsoftware.jwebassembly.wasm.WasmOptions;
3839

3940
/**
4041
* Manage the written and to write types (classes)
@@ -60,7 +61,7 @@ public class TypeManager {
6061
* @param options
6162
* compiler properties
6263
*/
63-
public TypeManager( WasmOptions options ) {
64+
TypeManager( WasmOptions options ) {
6465
this.options = options;
6566
}
6667

@@ -87,7 +88,7 @@ void prepareFinish( ModuleWriter writer, FunctionManager functions, ClassFileLoa
8788
* Get the StructType. If needed an instance is created.
8889
*
8990
* @param name
90-
* the type name
91+
* the type name like java/lang/Object
9192
* @return the struct type
9293
*/
9394
public StructType valueOf( String name ) {
@@ -123,6 +124,24 @@ public ArrayType arrayType( AnyType arrayType ) {
123124
return type;
124125
}
125126

127+
/**
128+
* Get the FunctionName for a virtual call and mark it as used. The function has 2 parameters (THIS,
129+
* virtualfunctionIndex) and returns the index of the function.
130+
*
131+
* @return the name
132+
*/
133+
@Nonnull
134+
WatCodeSyntheticFunctionName getCallVirtualGC() {
135+
return new WatCodeSyntheticFunctionName( //
136+
"callVirtual", "local.get 0 " // THIS
137+
+ "struct.get java/lang/Object .vtable " // vtable is on index 0
138+
+ "local.get 1 " // virtualFunctionIndex
139+
+ "i32.add " //
140+
+ "i32.load offset=0 align=4 " //
141+
+ "return " //
142+
, valueOf( "java/lang/Object" ), ValueType.i32, null, ValueType.i32 ); //
143+
}
144+
126145
/**
127146
* A reference to a type.
128147
*

src/de/inetsoftware/jwebassembly/module/WasmCallInterfaceInstruction.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@
2222

2323
import de.inetsoftware.jwebassembly.WasmException;
2424
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
25-
import de.inetsoftware.jwebassembly.wasm.MemoryOperator;
26-
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
27-
import de.inetsoftware.jwebassembly.wasm.StructOperator;
2825
import de.inetsoftware.jwebassembly.wasm.ValueType;
29-
import de.inetsoftware.jwebassembly.wasm.VariableOperator;
30-
import de.inetsoftware.jwebassembly.wasm.WasmOptions;
3126

3227
/**
3328
* WasmInstruction for a function call.

src/de/inetsoftware/jwebassembly/module/WasmCallVirtualInstruction.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import de.inetsoftware.jwebassembly.wasm.StructOperator;
2727
import de.inetsoftware.jwebassembly.wasm.ValueType;
2828
import de.inetsoftware.jwebassembly.wasm.VariableOperator;
29-
import de.inetsoftware.jwebassembly.wasm.WasmOptions;
3029

3130
/**
3231
* WasmInstruction for a function call.
@@ -96,14 +95,9 @@ public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
9695
// duplicate this on the stack
9796
writer.writeLocal( VariableOperator.get, getVariableIndexOfThis() );
9897

98+
writer.writeConst( virtualFunctionIdx * 4, ValueType.i32 );
99+
writer.writeFunctionCall( options.getCallVirtual() );
99100
StructType type = getThisType();
100-
if( options.useGC() ) {
101-
writer.writeStructOperator( StructOperator.GET, type, new NamedStorageType( type, "", "vtable" ), 0 ); // vtable is ever on position 0
102-
} else {
103-
writer.writeConst( 0, ValueType.i32 ); // vtable is ever on position 0
104-
writer.writeFunctionCall( WasmCodeBuilder.GET_I32 );
105-
}
106-
writer.writeMemoryOperator( MemoryOperator.load, ValueType.i32, virtualFunctionIdx * 4, 2 );
107101
writer.writeVirtualFunctionCall( getFunctionName(), type );
108102
}
109103
}

0 commit comments

Comments
 (0)