Skip to content

Commit

Permalink
going on
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaTP committed Sep 24, 2024
1 parent 079c61d commit 22226f5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
18 changes: 9 additions & 9 deletions aot/src/main/java/com/dylibso/chicory/aot/AotMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -531,16 +531,16 @@ private static MethodHandle adaptSignature(FunctionType type, MethodHandle handl
argHandlers[i] = unboxerHandle(argTypes.get(i));
}
MethodHandle result = filterArguments(handle, 0, argHandlers);
result = result.asSpreader(Value[].class, argTypes.size());
result = result.asSpreader(long[].class, argTypes.size());

if (type.returns().isEmpty()) {
return result.asType(result.type().changeReturnType(Value[].class));
return result.asType(result.type().changeReturnType(long[].class));
}
if (type.returns().size() > 1) {
return result;
}
result = filterReturnValue(result, boxerHandle(type.returns().get(0)));
return filterReturnValue(result, ValueWrapper.HANDLE);
return filterReturnValue(result, LongArrayWrapper.HANDLE);
}

private static void emitConstructor(ClassVisitor writer) {
Expand Down Expand Up @@ -596,7 +596,7 @@ private static void compileHostFunction(int funcId, FunctionType type, MethodVis

private static void emitBoxArguments(MethodVisitor asm, List<ValueType> types) {
int slot = 0;
// box the arguments into Value[]
// box the arguments into long[]
asm.visitLdcInsn(types.size());
asm.visitTypeInsn(Opcodes.ANEWARRAY, getInternalName(Value.class));
for (int i = 0; i < types.size(); i++) {
Expand All @@ -614,12 +614,12 @@ private static void emitUnboxResult(FunctionType type, MethodVisitor asm) {
Class<?> returnType = jvmReturnType(type);
if (returnType == void.class) {
asm.visitInsn(Opcodes.RETURN);
} else if (returnType == Value[].class) {
} else if (returnType == long[].class) {
asm.visitInsn(Opcodes.ARETURN);
} else {
// unbox the result from Value[0]
// unbox the result from long[0]
asm.visitLdcInsn(0);
asm.visitInsn(Opcodes.AALOAD);
asm.visitInsn(Opcodes.LALOAD); // verify!
emitInvokeVirtual(asm, unboxer(type.returns().get(0)));
asm.visitInsn(returnTypeOpcode(type));
}
Expand Down Expand Up @@ -883,7 +883,7 @@ private FunctionType blockType(Instruction ins) {
}

private static MethodType valueMethodType(List<ValueType> types) {
return methodType(Value[].class, jvmTypes(types));
return methodType(long[].class, jvmTypes(types));
}

private static String valueMethodName(List<ValueType> types) {
Expand All @@ -895,7 +895,7 @@ private static String valueMethodName(List<ValueType> types) {

private static int returnTypeOpcode(FunctionType type) {
Class<?> returnType = jvmReturnType(type);
if (returnType == Value[].class) {
if (returnType == long[].class) {
return Opcodes.ARETURN;
}
if (returnType == int.class) {
Expand Down
3 changes: 1 addition & 2 deletions aot/src/main/java/com/dylibso/chicory/aot/AotMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.dylibso.chicory.wasm.exceptions.ChicoryException;
import com.dylibso.chicory.wasm.types.Element;
import com.dylibso.chicory.wasm.types.FunctionType;
import com.dylibso.chicory.wasm.types.Value;
import java.lang.reflect.Method;
import java.util.List;

Expand Down Expand Up @@ -66,7 +65,7 @@ public final class AotMethods {
int.class,
Instance.class);
INSTANCE_CALL_HOST_FUNCTION =
Instance.class.getMethod("callHostFunction", int.class, Value[].class);
Instance.class.getMethod("callHostFunction", int.class, long[].class);
INSTANCE_READ_GLOBAL = Instance.class.getMethod("readGlobal", int.class);
INSTANCE_WRITE_GLOBAL = Instance.class.getMethod("writeGlobal", int.class, long.class);
INSTANCE_SET_ELEMENT = Instance.class.getMethod("setElement", int.class, Element.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static java.lang.invoke.MethodHandles.lookup;

import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.wasm.types.Value;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;

Expand All @@ -15,7 +14,7 @@ final class HostFunctionInvoker {
HANDLE =
lookup().unreflect(
HostFunctionInvoker.class.getMethod(
"invoke", Instance.class, int.class, Value[].class));
"invoke", Instance.class, int.class, long[].class));
} catch (NoSuchMethodException | IllegalAccessException e) {
throw new LinkageError(e.getMessage(), e);
}
Expand All @@ -27,7 +26,7 @@ public static MethodHandle handleFor(Instance instance, int funcId) {
return MethodHandles.insertArguments(HANDLE, 0, instance, funcId);
}

public static Value[] invoke(Instance instance, int funcId, Value[] args) {
public static long[] invoke(Instance instance, int funcId, long[] args) {
return instance.callHostFunction(funcId, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@

import static java.lang.invoke.MethodHandles.lookup;

import com.dylibso.chicory.wasm.types.Value;
import java.lang.invoke.MethodHandle;

final class ValueWrapper {
final class LongArrayWrapper {
public static final MethodHandle HANDLE;

static {
try {
HANDLE = lookup().unreflect(ValueWrapper.class.getMethod("wrap", Value.class));
HANDLE = lookup().unreflect(LongArrayWrapper.class.getMethod("wrap", long.class));
} catch (NoSuchMethodException | IllegalAccessException e) {
throw new LinkageError(e.getMessage(), e);
}
}

private ValueWrapper() {}
private LongArrayWrapper() {}

public static Value[] wrap(Value value) {
return new Value[] {value};
public static long[] wrap(long value) {
return new long[] {value};
}
}

0 comments on commit 22226f5

Please sign in to comment.