Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@
import static run.endive.compiler.internal.ShadedRefs.AOT_INTERPRETER_MACHINE_CALL;
import static run.endive.compiler.internal.ShadedRefs.CALL_HOST_FUNCTION;
import static run.endive.compiler.internal.ShadedRefs.CALL_HOST_FUNCTION_WITH_REFS;
import static run.endive.compiler.internal.ShadedRefs.CALL_INDIRECT;
import static run.endive.compiler.internal.ShadedRefs.CALL_INDIRECT_CROSS_MODULE;
import static run.endive.compiler.internal.ShadedRefs.CALL_INDIRECT_ON_INTERPRETER;
import static run.endive.compiler.internal.ShadedRefs.CALL_INDIRECT_ON_INTERPRETER_WITH_REFS;
import static run.endive.compiler.internal.ShadedRefs.CALL_INDIRECT_WITH_REFS;
import static run.endive.compiler.internal.ShadedRefs.CALL_INDIRECT_WITH_REFS_CROSS_MODULE;
import static run.endive.compiler.internal.ShadedRefs.CHECK_INTERRUPTION;
import static run.endive.compiler.internal.ShadedRefs.INSTANCE_GET_TYPE;
import static run.endive.compiler.internal.ShadedRefs.INSTANCE_MEMORY;
import static run.endive.compiler.internal.ShadedRefs.INSTANCE_TABLE;
import static run.endive.compiler.internal.ShadedRefs.TABLE_INSTANCE;
Expand Down Expand Up @@ -1756,11 +1757,13 @@ private void compileCallIndirect(
emitBoxArgumentsWithRefs(asm, type.params());
// stack: long[], Object[]
}
asm.load(instance, OBJECT_TYPE);
asm.iconst(typeId);
emitInvokeVirtual(asm, INSTANCE_GET_TYPE);
asm.load(funcId, INT_TYPE);
asm.load(refInstance, OBJECT_TYPE);

emitInvokeStatic(asm, CALL_INDIRECT_WITH_REFS);
emitInvokeStatic(asm, CALL_INDIRECT_WITH_REFS_CROSS_MODULE);
// returns CallResult
emitUnboxCallResult(type, asm);
} else {
Expand All @@ -1769,11 +1772,13 @@ private void compileCallIndirect(
} else {
emitBoxArguments(asm, type.params());
}
asm.load(instance, OBJECT_TYPE);
asm.iconst(typeId);
emitInvokeVirtual(asm, INSTANCE_GET_TYPE);
asm.load(funcId, INT_TYPE);
asm.load(refInstance, OBJECT_TYPE);

emitInvokeStatic(asm, CALL_INDIRECT);
emitInvokeStatic(asm, CALL_INDIRECT_CROSS_MODULE);

emitUnboxResult(type, asm);
}
Expand Down
23 changes: 23 additions & 0 deletions compiler/src/main/java/run/endive/compiler/internal/Shaded.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import run.endive.runtime.WasmStruct;
import run.endive.wasm.InvalidException;
import run.endive.wasm.WasmEngineException;
import run.endive.wasm.types.FunctionType;
import run.endive.wasm.types.ValType;

/**
Expand All @@ -39,6 +40,15 @@ public static long[] callIndirect(long[] args, int typeId, int funcId, Instance
return instance.getMachine().call(funcId, args);
}

public static long[] callIndirect(
long[] args, FunctionType expectedType, int funcId, Instance instance) {
FunctionType actualType = instance.type(instance.functionType(funcId));
if (!FunctionType.matches(actualType, expectedType, instance.module().typeSection())) {
throw throwIndirectCallTypeMismatch();
}
return instance.getMachine().call(funcId, args);
}

public static long[] callIndirect(long[] args, int funcId, Instance instance) {
return instance.getMachine().call(funcId, args);
}
Expand All @@ -59,6 +69,19 @@ public static CallResult callIndirectWithRefs(
return instance.getMachine().callWithRefs(funcId, args, refArgs);
}

public static CallResult callIndirectWithRefs(
long[] args,
Object[] refArgs,
FunctionType expectedType,
int funcId,
Instance instance) {
FunctionType actualType = instance.type(instance.functionType(funcId));
if (!FunctionType.matches(actualType, expectedType, instance.module().typeSection())) {
throw throwIndirectCallTypeMismatch();
}
return instance.getMachine().callWithRefs(funcId, args, refArgs);
}

public static long[] callHostFunction(Instance instance, int funcId, long[] args) {
var imprt = instance.imports().function(funcId);
return imprt.handle().apply(instance, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import run.endive.runtime.WasmException;
import run.endive.runtime.internal.CompilerInterpreterMachine;
import run.endive.wasm.types.Element;
import run.endive.wasm.types.FunctionType;

public final class ShadedRefs {

static final Method CHECK_INTERRUPTION;
static final Method CALL_INDIRECT;
static final Method CALL_INDIRECT_CROSS_MODULE;
static final Method CALL_INDIRECT_ON_INTERPRETER;
static final Method CALL_INDIRECT_ON_INTERPRETER_WITH_REFS;
static final Method INSTANCE_MEMORY;
Expand All @@ -23,6 +25,7 @@ public final class ShadedRefs {
static final Method WRITE_GLOBAL_REF;
static final Method INSTANCE_SET_ELEMENT;
static final Method INSTANCE_TABLE;
static final Method INSTANCE_GET_TYPE;
static final Method MEMORY_COPY;
static final Method MEMORY_COPY_2;
static final Method MEMORY_FILL;
Expand Down Expand Up @@ -155,6 +158,7 @@ public final class ShadedRefs {
// WithRefs overloads for cross-module/host calls
static final Method CALL_HOST_FUNCTION_WITH_REFS;
static final Method CALL_INDIRECT_WITH_REFS;
static final Method CALL_INDIRECT_WITH_REFS_CROSS_MODULE;

// GC
static final Method STRUCT_NEW;
Expand Down Expand Up @@ -209,6 +213,13 @@ public final class ShadedRefs {
CALL_INDIRECT =
Shaded.class.getMethod(
"callIndirect", long[].class, int.class, int.class, Instance.class);
CALL_INDIRECT_CROSS_MODULE =
Shaded.class.getMethod(
"callIndirect",
long[].class,
FunctionType.class,
int.class,
Instance.class);
CALL_INDIRECT_ON_INTERPRETER =
Shaded.class.getMethod("callIndirect", long[].class, int.class, Instance.class);
CALL_INDIRECT_ON_INTERPRETER_WITH_REFS =
Expand All @@ -232,6 +243,7 @@ public final class ShadedRefs {
"writeGlobalRef", Object.class, int.class, Instance.class);
INSTANCE_SET_ELEMENT = Instance.class.getMethod("setElement", int.class, Element.class);
INSTANCE_TABLE = Instance.class.getMethod("table", int.class);
INSTANCE_GET_TYPE = Instance.class.getMethod("type", int.class);
MEMORY_COPY =
Shaded.class.getMethod(
"memoryCopy", int.class, int.class, int.class, Memory.class);
Expand Down Expand Up @@ -834,6 +846,14 @@ public final class ShadedRefs {
int.class,
int.class,
Instance.class);
CALL_INDIRECT_WITH_REFS_CROSS_MODULE =
Shaded.class.getMethod(
"callIndirectWithRefs",
long[].class,
Object[].class,
FunctionType.class,
int.class,
Instance.class);

// GC
STRUCT_NEW =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,12 @@ public final class run/endive/$gen/CompiledMachine implements run/endive/runtime
ILOAD 1
I2L
LASTORE
ALOAD 5
ICONST_0
INVOKEVIRTUAL run/endive/runtime/Instance.type (I)Lrun/endive/wasm/types/FunctionType;
ILOAD 7
ALOAD 8
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JIILrun/endive/runtime/Instance;)[J
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JLrun/endive/wasm/types/FunctionType;ILrun/endive/runtime/Instance;)[J
ICONST_0
LALOAD
L2I
Expand Down Expand Up @@ -154,10 +156,12 @@ public final class run/endive/$gen/CompiledMachine implements run/endive/runtime
L1
ALOAD 0
ACONST_NULL
ALOAD 4
ICONST_1
INVOKEVIRTUAL run/endive/runtime/Instance.type (I)Lrun/endive/wasm/types/FunctionType;
ILOAD 6
ALOAD 7
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirectWithRefs ([J[Ljava/lang/Object;IILrun/endive/runtime/Instance;)Lrun/endive/runtime/CallResult;
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirectWithRefs ([J[Ljava/lang/Object;Lrun/endive/wasm/types/FunctionType;ILrun/endive/runtime/Instance;)Lrun/endive/runtime/CallResult;
ICONST_0
INVOKEVIRTUAL run/endive/runtime/CallResult.longResult (I)J
L2I
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,12 @@ public final class run/endive/$gen/CompiledMachine implements run/endive/runtime
ILOAD 0
I2L
LASTORE
ALOAD 4

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the compiler generates both branches (local lookupswitch + cross-module fallback) unconditionally now

ICONST_0
INVOKEVIRTUAL run/endive/runtime/Instance.type (I)Lrun/endive/wasm/types/FunctionType;
ILOAD 6
ALOAD 7
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JIILrun/endive/runtime/Instance;)[J
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JLrun/endive/wasm/types/FunctionType;ILrun/endive/runtime/Instance;)[J
ICONST_0
LALOAD
L2I
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ public final class run/endive/$gen/CompiledMachine implements run/endive/runtime
ILOAD 0
I2L
LASTORE
ALOAD 4
ICONST_0
INVOKEVIRTUAL run/endive/runtime/Instance.type (I)Lrun/endive/wasm/types/FunctionType;
ILOAD 6
ALOAD 7
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JIILrun/endive/runtime/Instance;)[J
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JLrun/endive/wasm/types/FunctionType;ILrun/endive/runtime/Instance;)[J
ICONST_0
LALOAD
L2I
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ public final class run/endive/$gen/CompiledMachine implements run/endive/runtime
ILOAD 0
I2L
LASTORE
ALOAD 4
ICONST_0
INVOKEVIRTUAL run/endive/runtime/Instance.type (I)Lrun/endive/wasm/types/FunctionType;
ILOAD 6
ALOAD 7
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JIILrun/endive/runtime/Instance;)[J
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JLrun/endive/wasm/types/FunctionType;ILrun/endive/runtime/Instance;)[J
ICONST_0
LALOAD
L2I
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ public final class run/endive/$gen/CompiledMachine implements run/endive/runtime
L1
ICONST_0
NEWARRAY T_LONG
ALOAD 3
ICONST_0
INVOKEVIRTUAL run/endive/runtime/Instance.type (I)Lrun/endive/wasm/types/FunctionType;
ILOAD 5
ALOAD 6
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JIILrun/endive/runtime/Instance;)[J
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JLrun/endive/wasm/types/FunctionType;ILrun/endive/runtime/Instance;)[J
RETURN
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,12 @@ public final class run/endive/$gen/CompiledMachine implements run/endive/runtime
ILOAD 3
I2L
LASTORE
ALOAD 7
ICONST_0
INVOKEVIRTUAL run/endive/runtime/Instance.type (I)Lrun/endive/wasm/types/FunctionType;
ILOAD 9
ALOAD 10
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JIILrun/endive/runtime/Instance;)[J
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JLrun/endive/wasm/types/FunctionType;ILrun/endive/runtime/Instance;)[J
ICONST_0
LALOAD
L2I
Expand Down Expand Up @@ -159,10 +161,12 @@ public final class run/endive/$gen/CompiledMachine implements run/endive/runtime
L1
ICONST_0
NEWARRAY T_LONG
ALOAD 3
ICONST_1
INVOKEVIRTUAL run/endive/runtime/Instance.type (I)Lrun/endive/wasm/types/FunctionType;
ILOAD 5
ALOAD 6
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JIILrun/endive/runtime/Instance;)[J
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JLrun/endive/wasm/types/FunctionType;ILrun/endive/runtime/Instance;)[J
RETURN
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ public final class run/endive/$gen/CompiledMachine implements run/endive/runtime
L1
ICONST_0
NEWARRAY T_LONG
ALOAD 3
ICONST_0
INVOKEVIRTUAL run/endive/runtime/Instance.type (I)Lrun/endive/wasm/types/FunctionType;
ILOAD 5
ALOAD 6
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JIILrun/endive/runtime/Instance;)[J
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JLrun/endive/wasm/types/FunctionType;ILrun/endive/runtime/Instance;)[J
RETURN
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ public final class FOO implements run/endive/runtime/Machine {
L1
ICONST_0
NEWARRAY T_LONG
ALOAD 3
ICONST_0
INVOKEVIRTUAL run/endive/runtime/Instance.type (I)Lrun/endive/wasm/types/FunctionType;
ILOAD 5
ALOAD 6
INVOKESTATIC FOOShaded.callIndirect ([JIILrun/endive/runtime/Instance;)[J
INVOKESTATIC FOOShaded.callIndirect ([JLrun/endive/wasm/types/FunctionType;ILrun/endive/runtime/Instance;)[J
RETURN
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ public final class run/endive/$gen/CompiledMachine implements run/endive/runtime
ILOAD 0
I2L
LASTORE
ALOAD 4
ICONST_0
INVOKEVIRTUAL run/endive/runtime/Instance.type (I)Lrun/endive/wasm/types/FunctionType;
ILOAD 6
ALOAD 7
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JIILrun/endive/runtime/Instance;)[J
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JLrun/endive/wasm/types/FunctionType;ILrun/endive/runtime/Instance;)[J
ICONST_0
LALOAD
L2I
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ public final class run/endive/$gen/CompiledMachine implements run/endive/runtime
ILOAD 0
I2L
LASTORE
ALOAD 4
ICONST_0
INVOKEVIRTUAL run/endive/runtime/Instance.type (I)Lrun/endive/wasm/types/FunctionType;
ILOAD 6
ALOAD 7
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JIILrun/endive/runtime/Instance;)[J
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JLrun/endive/wasm/types/FunctionType;ILrun/endive/runtime/Instance;)[J
ICONST_0
LALOAD
L2I
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ public final class run/endive/$gen/CompiledMachine implements run/endive/runtime
ILOAD 0
I2L
LASTORE
ALOAD 4
ICONST_0
INVOKEVIRTUAL run/endive/runtime/Instance.type (I)Lrun/endive/wasm/types/FunctionType;
ILOAD 6
ALOAD 7
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JIILrun/endive/runtime/Instance;)[J
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JLrun/endive/wasm/types/FunctionType;ILrun/endive/runtime/Instance;)[J
ICONST_0
LALOAD
L2I
Expand Down Expand Up @@ -146,10 +148,12 @@ public final class run/endive/$gen/CompiledMachine implements run/endive/runtime
ICONST_0
LLOAD 0
LASTORE
ALOAD 5
ICONST_1
INVOKEVIRTUAL run/endive/runtime/Instance.type (I)Lrun/endive/wasm/types/FunctionType;
ILOAD 7
ALOAD 8
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JIILrun/endive/runtime/Instance;)[J
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JLrun/endive/wasm/types/FunctionType;ILrun/endive/runtime/Instance;)[J
ICONST_0
LALOAD
LRETURN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ public final class run/endive/$gen/CompiledMachine implements run/endive/runtime
ILOAD 0
I2L
LASTORE
ALOAD 4
ICONST_0
INVOKEVIRTUAL run/endive/runtime/Instance.type (I)Lrun/endive/wasm/types/FunctionType;
ILOAD 6
ALOAD 7
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JIILrun/endive/runtime/Instance;)[J
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JLrun/endive/wasm/types/FunctionType;ILrun/endive/runtime/Instance;)[J
RETURN

public static call_indirect_1(IILrun/endive/runtime/Memory;Lrun/endive/runtime/Instance;)V
Expand Down Expand Up @@ -138,10 +140,12 @@ public final class run/endive/$gen/CompiledMachine implements run/endive/runtime
L1
ICONST_0
NEWARRAY T_LONG
ALOAD 3
ICONST_1
INVOKEVIRTUAL run/endive/runtime/Instance.type (I)Lrun/endive/wasm/types/FunctionType;
ILOAD 5
ALOAD 6
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JIILrun/endive/runtime/Instance;)[J
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JLrun/endive/wasm/types/FunctionType;ILrun/endive/runtime/Instance;)[J
RETURN
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,12 @@ public final class run/endive/$gen/CompiledMachine implements run/endive/runtime
ILOAD 2
I2L
LASTORE
ALOAD 6
ICONST_0
INVOKEVIRTUAL run/endive/runtime/Instance.type (I)Lrun/endive/wasm/types/FunctionType;
ILOAD 8
ALOAD 9
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JIILrun/endive/runtime/Instance;)[J
INVOKESTATIC run/endive/$gen/CompiledMachineShaded.callIndirect ([JLrun/endive/wasm/types/FunctionType;ILrun/endive/runtime/Instance;)[J
ICONST_0
LALOAD
L2I
Expand Down
Loading
Loading