Skip to content

Commit

Permalink
partial AOT implementation... need help
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaTP committed Sep 18, 2024
1 parent abe7593 commit 5c171e6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
12 changes: 8 additions & 4 deletions aot/src/main/java/com/dylibso/chicory/aot/AotMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,9 @@ private static List<FunctionType> getFunctionTypes(Module module) {
}

@Override
public Value[] call(int funcId, Value[] args) throws ChicoryException {
public long[] call(int funcId, long[] args) throws ChicoryException {
try {
return (Value[]) compiledFunctions[funcId].invokeExact(args);
return (long[]) compiledFunctions[funcId].invokeExact(args);
} catch (ChicoryException e) {
// propagate ChicoryExceptions
throw e;
Expand Down Expand Up @@ -567,15 +567,19 @@ private static void compileCallIndirect(MethodVisitor asm, int typeId, FunctionT
int slot = type.params().stream().mapToInt(AotUtil::slotCount).sum();

// parameters: arguments, funcTableIdx, tableIdx, instance
emitBoxArguments(asm, type.params());
// emitBoxArguments(asm, type.params());
// asm.visitLdcInsn(LONG);
for (int i = 0; i < type.params().size(); i++) {
asm.visitVarInsn(Opcodes.LLOAD, i);
}
asm.visitLdcInsn(typeId);
asm.visitVarInsn(Opcodes.ILOAD, slot); // funcTableIdx
asm.visitVarInsn(Opcodes.ILOAD, slot + 1); // tableIdx
asm.visitVarInsn(Opcodes.ALOAD, slot + 2); // instance

emitInvokeStatic(asm, AotMethods.CALL_INDIRECT);

emitUnboxResult(type, asm);
asm.visitInsn(returnTypeOpcode(type));
}

private static void compileHostFunction(int funcId, FunctionType type, MethodVisitor asm) {
Expand Down
15 changes: 9 additions & 6 deletions aot/src/main/java/com/dylibso/chicory/aot/AotMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ public final class AotMethods {
CALL_INDIRECT =
AotMethods.class.getMethod(
"callIndirect",
Value[].class,
long[].class,
int.class,
int.class,
int.class,
Instance.class);
INSTANCE_CALL_HOST_FUNCTION =
Instance.class.getMethod("callHostFunction", int.class, Value[].class);
INSTANCE_READ_GLOBAL = Instance.class.getMethod("readGlobal", int.class);
INSTANCE_WRITE_GLOBAL = Instance.class.getMethod("writeGlobal", int.class, Value.class);
INSTANCE_WRITE_GLOBAL = Instance.class.getMethod("writeGlobal", int.class, long.class);
INSTANCE_SET_ELEMENT = Instance.class.getMethod("setElement", int.class, Element.class);
MEMORY_COPY =
AotMethods.class.getMethod(
Expand Down Expand Up @@ -165,13 +165,13 @@ public final class AotMethods {
private AotMethods() {}

@UsedByGeneratedCode
public static Value[] callIndirect(
Value[] args, int typeId, int funcTableIdx, int tableIdx, Instance instance) {
public static long[] callIndirect(
long[] args, int typeId, int funcTableIdx, int tableIdx, Instance instance) {
TableInstance table = instance.table(tableIdx);

instance = requireNonNullElse(table.instance(funcTableIdx), instance);

int funcId = table.ref(funcTableIdx).asFuncRef();
int funcId = (int) table.ref(funcTableIdx);
if (funcId == REF_NULL_VALUE) {
throw new ChicoryException("uninitialized element " + funcTableIdx);
}
Expand All @@ -183,6 +183,9 @@ public static Value[] callIndirect(
}

checkInterruption();
// TODO: verify
// here we should not pass through the external "call" method
// but directly emit the invocation of the underlying function
return instance.getMachine().call(funcId, args);
}

Expand All @@ -193,7 +196,7 @@ public static boolean isRefNull(int ref) {

@UsedByGeneratedCode
public static int tableGet(int index, int tableIndex, Instance instance) {
return OpcodeImpl.TABLE_GET(instance, tableIndex, index).asFuncRef();
return (int) OpcodeImpl.TABLE_GET(instance, tableIndex, index);
}

@UsedByGeneratedCode
Expand Down
8 changes: 4 additions & 4 deletions aot/src/main/java/com/dylibso/chicory/aot/AotUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public enum StackSize {
BOX_I64 = Value.class.getMethod("i64", long.class);
BOX_F32 = Value.class.getMethod("fromFloat", float.class);
BOX_F64 = Value.class.getMethod("fromDouble", double.class);
BOX_EXTREF = Value.class.getMethod("externRef", int.class);
BOX_FUNCREF = Value.class.getMethod("funcRef", int.class);
BOX_EXTREF = Value.class.getMethod("externRef", long.class);
BOX_FUNCREF = Value.class.getMethod("funcRef", long.class);
} catch (NoSuchMethodException e) {
throw new AssertionError(e);
}
Expand All @@ -64,9 +64,9 @@ public enum StackSize {
public static Class<?> jvmType(ValueType type) {
switch (type) {
case I32:
return int.class;
case ExternRef:
case FuncRef:
return int.class;
case I64:
return long.class;
case F32:
Expand Down Expand Up @@ -202,7 +202,7 @@ public static Class<?> jvmReturnType(FunctionType type) {
case 1:
return jvmType(type.returns().get(0));
default:
return Value[].class;
return long[].class;
}
}

Expand Down

0 comments on commit 5c171e6

Please sign in to comment.