Skip to content
Draft
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 @@ -49,6 +49,7 @@
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.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,7 +1757,9 @@ 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);

Expand All @@ -1769,7 +1772,9 @@ 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);

Expand Down
22 changes: 12 additions & 10 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 @@ -29,11 +30,10 @@ public final class Shaded {

private Shaded() {}

public static long[] callIndirect(long[] args, int typeId, int funcId, Instance instance) {
int actualTypeIdx = instance.functionType(funcId);
if (actualTypeIdx != typeId
&& !ValType.heapTypeSubtype(
actualTypeIdx, typeId, instance.module().typeSection())) {
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);
Expand All @@ -49,11 +49,13 @@ public static CallResult callIndirectOnInterpreterWithRefs(
}

public static CallResult callIndirectWithRefs(
long[] args, Object[] refArgs, int typeId, int funcId, Instance instance) {
int actualTypeIdx = instance.functionType(funcId);
if (actualTypeIdx != typeId
&& !ValType.heapTypeSubtype(
actualTypeIdx, typeId, instance.module().typeSection())) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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 {

Expand All @@ -23,6 +24,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 @@ -208,7 +210,11 @@ public final class ShadedRefs {
CHECK_INTERRUPTION = Shaded.class.getMethod("checkInterruption");
CALL_INDIRECT =
Shaded.class.getMethod(
"callIndirect", long[].class, int.class, int.class, Instance.class);
"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 +238,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 @@ -831,7 +838,7 @@ public final class ShadedRefs {
"callIndirectWithRefs",
long[].class,
Object[].class,
int.class,
FunctionType.class,
int.class,
Instance.class);

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
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
Loading
Loading