From 5681b7c13a4877e759d36e2f0e1b5cef5d12c701 Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Tue, 17 Oct 2023 12:35:17 +0100 Subject: [PATCH 1/6] Handle multiple return values and execute the call spec --- runtime/pom.xml | 3 +- .../chicory/runtime/ExportFunction.java | 2 +- .../com/dylibso/chicory/runtime/Machine.java | 10 ++- .../dylibso/chicory/runtime/ModuleTest.java | 43 ++++----- .../dylibso/chicory/maven/JavaTestGen.java | 87 +++++++++---------- 5 files changed, 74 insertions(+), 71 deletions(-) diff --git a/runtime/pom.xml b/runtime/pom.xml index 8f7f62c00..186c4b66f 100644 --- a/runtime/pom.xml +++ b/runtime/pom.xml @@ -56,7 +56,8 @@ fac.wast, data.wast, forward.wast, - float_literals.wast + float_literals.wast, + call.wast memory_grow.wast, memory_size.wast, stack.wast diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/ExportFunction.java b/runtime/src/main/java/com/dylibso/chicory/runtime/ExportFunction.java index 38da7dd5c..541fb2133 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/ExportFunction.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/ExportFunction.java @@ -8,5 +8,5 @@ */ @FunctionalInterface public interface ExportFunction { - Value apply(Value... args) throws ChicoryException; + Value[] apply(Value... args) throws ChicoryException; } diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java b/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java index 5280566b8..522c2890b 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java @@ -19,7 +19,7 @@ public Machine(Instance instance) { this.callStack = new Stack<>(); } - public Value call(int funcId, Value[] args, boolean popResults) throws ChicoryException { + public Value[] call(int funcId, Value[] args, boolean popResults) throws ChicoryException { var func = instance.getFunction(funcId); if (func != null) { this.callStack.push(new StackFrame(funcId, 0, args, func.getLocals())); @@ -46,7 +46,13 @@ public Value call(int funcId, Value[] args, boolean popResults) throws ChicoryEx var type = instance.getTypes()[typeId]; if (type.getReturns().length == 0) return null; if (this.stack.size() == 0) return null; - return this.stack.pop(); + + var totalResults = type.getReturns().length; + var results = new Value[totalResults]; + for (var i = totalResults - 1; i >= 0; i--) { + results[i] = this.stack.pop(); + } + return results; } void eval(List code) throws ChicoryException { diff --git a/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java b/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java index b1e5387a0..7919e2ea8 100644 --- a/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java +++ b/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java @@ -41,7 +41,7 @@ public void shouldWorkFactorial() { var module = Module.build("src/test/resources/wasm/iterfact.wat.wasm"); var instance = module.instantiate(); var iterFact = instance.getExport("iterFact"); - var result = iterFact.apply(Value.i32(5)); + var result = iterFact.apply(Value.i32(5))[0]; assertEquals(120, result.asInt()); } @@ -49,19 +49,19 @@ public void shouldWorkFactorial() { public void shouldSupportBrTable() { var instance = Module.build("src/test/resources/wasm/br_table.wat.wasm").instantiate(); var switchLike = instance.getExport("switch_like"); - var result = switchLike.apply(Value.i32(0)); + var result = switchLike.apply(Value.i32(0))[0]; assertEquals(102, result.asInt()); - result = switchLike.apply(Value.i32(1)); + result = switchLike.apply(Value.i32(1))[0]; assertEquals(101, result.asInt()); - result = switchLike.apply(Value.i32(2)); + result = switchLike.apply(Value.i32(2))[0]; assertEquals(100, result.asInt()); - result = switchLike.apply(Value.i32(-1)); + result = switchLike.apply(Value.i32(-1))[0]; assertEquals(103, result.asInt()); - result = switchLike.apply(Value.i32(3)); + result = switchLike.apply(Value.i32(3))[0]; assertEquals(103, result.asInt()); - result = switchLike.apply(Value.i32(4)); + result = switchLike.apply(Value.i32(4))[0]; assertEquals(103, result.asInt()); - result = switchLike.apply(Value.i32(100)); + result = switchLike.apply(Value.i32(100))[0]; assertEquals(103, result.asInt()); } @@ -70,14 +70,14 @@ public void shouldExerciseBranches() { var module = Module.build("src/test/resources/wasm/branching.wat.wasm").instantiate(); var foo = module.getExport("foo"); - var result = foo.apply(Value.i32(0)); + var result = foo.apply(Value.i32(0))[0]; assertEquals(42, result.asInt()); - result = foo.apply(Value.i32(1)); + result = foo.apply(Value.i32(1))[0]; assertEquals(99, result.asInt()); for (var i = 2; i < 100; i++) { - result = foo.apply(Value.i32(i)); + result = foo.apply(Value.i32(i))[0]; assertEquals(7, result.asInt()); } } @@ -113,7 +113,7 @@ public void shouldComputeFactorial() { // don't make this too big we will overflow 32 bits for (var i = 0; i < 10; i++) { - var result = iterFact.apply(Value.i32(i)); + var result = iterFact.apply(Value.i32(i))[0]; // test against an oracle Java implementation assertEquals(factorial(i), result.asInt()); } @@ -159,7 +159,7 @@ public void shouldTrapOnUnreachable() { public void shouldSupportGlobals() { var instance = Module.build("src/test/resources/wasm/globals.wat.wasm").instantiate(); var doit = instance.getExport("doit"); - var result = doit.apply(Value.i32(32)); + var result = doit.apply(Value.i32(32))[0]; assertEquals(42, result.asInt()); } @@ -180,7 +180,7 @@ public void shouldRunBasicCProgram() { // check with: wasmtime src/test/resources/wasm/basic.c.wasm --invoke run var instance = Module.build("src/test/resources/wasm/basic.c.wasm").instantiate(); var run = instance.getExport("run"); - var result = run.apply(); + var result = run.apply()[0]; assertEquals(42, result.asInt()); } @@ -206,25 +206,26 @@ public void shouldRunBasicCProgram() { public void shouldWorkWithMemoryOps() { var instance = Module.build("src/test/resources/wasm/memory.wat.wasm").instantiate(); var run = instance.getExport("run32"); - var result = run.apply(Value.i32(42)); + var results = run.apply(Value.i32(42)); + var result = results[0]; assertEquals(42, result.asInt()); - result = run.apply(Value.i32(Integer.MAX_VALUE)); + result = run.apply(Value.i32(Integer.MAX_VALUE))[0]; assertEquals(Integer.MAX_VALUE, result.asInt()); - result = run.apply(Value.i32(Integer.MIN_VALUE)); + result = run.apply(Value.i32(Integer.MIN_VALUE))[0]; assertEquals(Integer.MIN_VALUE, result.asInt()); run = instance.getExport("run64"); - result = run.apply(Value.i64(42)); + result = run.apply(Value.i64(42))[0]; assertEquals(42L, result.asLong()); run = instance.getExport("run64"); - result = run.apply(Value.i64(Long.MIN_VALUE)); + result = run.apply(Value.i64(Long.MIN_VALUE))[0]; assertEquals(Long.MIN_VALUE, result.asLong()); run = instance.getExport("run64"); - result = run.apply(Value.i64(Long.MAX_VALUE)); + result = run.apply(Value.i64(Long.MAX_VALUE))[0]; assertEquals(Long.MAX_VALUE, result.asLong()); } @@ -233,7 +234,7 @@ public void shouldRunKitchenSink() { // check with: wasmtime src/test/resources/wasm/kitchensink.wat.wasm --invoke run 100 var instance = Module.build("src/test/resources/wasm/kitchensink.wat.wasm").instantiate(); var run = instance.getExport("run"); - assertEquals(6, run.apply(Value.i32(100)).asInt()); + assertEquals(6, run.apply(Value.i32(100))[0].asInt()); } // @Test diff --git a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/JavaTestGen.java b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/JavaTestGen.java index 00642b754..b9a0a3077 100644 --- a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/JavaTestGen.java +++ b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/JavaTestGen.java @@ -25,6 +25,7 @@ import com.github.javaparser.utils.StringEscapeUtils; import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Optional; @@ -138,6 +139,7 @@ public CompilationUnit generate( break; case ASSERT_INVALID: case ASSERT_MALFORMED: + case ASSERT_UNINSTANTIABLE: testNumber++; // method = createTestMethod(testClass, testNumber++, // excludedMethods); @@ -202,61 +204,54 @@ private Optional generateFieldExport( private List generateAssert(String varName, Command cmd) { assert (cmd.getType() == CommandType.ASSERT_RETURN || cmd.getType() == CommandType.ASSERT_TRAP); - - var returnVar = "null"; - var typeConversion = ""; - var deltaParam = ""; - if (cmd.getExpected() != null && cmd.getExpected().length > 0) { - if (cmd.getExpected().length == 1) { - var expected = cmd.getExpected()[0]; - returnVar = expected.toJavaValue(); - typeConversion = expected.extractType(); - deltaParam = expected.getDelta(); + assert (cmd.getExpected() != null); + assert (cmd.getExpected().length > 0); + assert (cmd.getAction().getType() == INVOKE); + + var args = + Arrays.stream(cmd.getAction().getArgs()) + .map(WasmValue::toWasmValue) + .collect(Collectors.joining(", ")); + + var invocationMethod = ".apply(" + args + ")"; + if (cmd.getType() == CommandType.ASSERT_TRAP) { + var assertDecl = + new NameExpr( + "var exception =" + + " assertThrows(WASMRuntimeException.class, () -> " + + varName + + invocationMethod + + ")"); + if (cmd.getText() != null) { + return List.of(assertDecl, exceptionMessageMatch(cmd.getText())); } else { - throw new RuntimeException("Multiple expected return, implement me!"); + return List.of(assertDecl); } - } - - String invocationMethod; - if (cmd.getAction().getType() == INVOKE) { - var args = - Arrays.stream(cmd.getAction().getArgs()) - .map(WasmValue::toWasmValue) - .collect(Collectors.joining(", ")); - invocationMethod = ".apply(" + args + ")"; - } else { - throw new IllegalArgumentException( - "Unhandled action type " + cmd.getAction().getType()); - } - - switch (cmd.getType()) { - case ASSERT_RETURN: - return List.of( + } else if (cmd.getType() == CommandType.ASSERT_RETURN) { + List exprs = new ArrayList(); + exprs.add(new NameExpr("var results = " + varName + ".apply(" + args + ")")); + + for (int i = 0; i < cmd.getExpected().length; i++) { + var expected = cmd.getExpected()[i]; + var returnVar = expected.toJavaValue(); + var typeConversion = expected.extractType(); + var deltaParam = expected.getDelta(); + exprs.add( new NameExpr( "assertEquals(" + returnVar - + ", " - + varName - + invocationMethod + + ", results[" + + i + + "]" + typeConversion + deltaParam + ")")); - case ASSERT_TRAP: - var assertDecl = - new NameExpr( - "var exception = assertThrows(WASMRuntimeException.class, () -> " - + varName - + invocationMethod - + typeConversion - + ")"); - if (cmd.getText() != null) { - return List.of(assertDecl, exceptionMessageMatch(cmd.getText())); - } else { - return List.of(assertDecl); - } - } + } - throw new RuntimeException("Unreachable"); + return exprs; + } else { + throw new IllegalArgumentException("Unhandled command type " + cmd.getType()); + } } private Expression generateModuleInstantiation(Command cmd, File folder) { From d9f1a2c4b274bc9fee843d1ef534b5cb40348241 Mon Sep 17 00:00:00 2001 From: Benjamin Eckel Date: Fri, 13 Oct 2023 13:48:11 -0500 Subject: [PATCH 2/6] Support call indirect --- .../com/dylibso/chicory/runtime/Instance.java | 9 ++++- .../com/dylibso/chicory/runtime/Machine.java | 33 +++++++------------ .../com/dylibso/chicory/runtime/Module.java | 21 +++++++++++- .../com/dylibso/chicory/wasm/types/Table.java | 15 ++++++++- 4 files changed, 54 insertions(+), 24 deletions(-) diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/Instance.java b/runtime/src/main/java/com/dylibso/chicory/runtime/Instance.java index d69d02087..592d2f691 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/Instance.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/Instance.java @@ -12,6 +12,7 @@ public class Instance { private FunctionType[] types; private int[] functionTypes; private HostFunction[] imports; + private Table table; public Instance( Module module, @@ -21,7 +22,8 @@ public Instance( FunctionBody[] functions, FunctionType[] types, int[] functionTypes, - HostFunction[] imports) { + HostFunction[] imports, + Table table) { this.module = module; this.globalInitalizers = globalInitalizers; this.globals = globals; @@ -31,6 +33,7 @@ public Instance( this.functionTypes = functionTypes; this.imports = imports; this.machine = new Machine(this); + this.table = table; } public ExportFunction getExport(String name) { @@ -87,4 +90,8 @@ public int[] getFunctionTypes() { public HostFunction[] getImports() { return imports; } + + public Table getTable() { + return table; + } } diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java b/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java index 522c2890b..32e355f8e 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java @@ -129,27 +129,18 @@ void eval(List code) throws ChicoryException { case RETURN: shouldReturn = true; break; - // case CALL_INDIRECT: - // { - // // var index = - // this.stack.pop().asInt(); - // // var funcId = - // // instance.getTable().getFuncRef(index); - // // var typeId = - // // instance.getFunctionTypes().get(funcId); - // // var type = - // instance.getTypes().get(typeId); - // // // given a list of param - // types, let's pop those - // // params off the stack - // // // and pass as args to - // the function call - // // var args = - // // extractArgsForParams(type.paramTypes()); - // // call(funcId, args, - // false); - // break; - // } + case CALL_INDIRECT: + { + var index = this.stack.pop().asInt(); + var funcId = instance.getTable().getFuncRef(index); + var typeId = instance.getFunctionTypes()[funcId]; + var type = instance.getTypes()[typeId]; + // given a list of param types, let's pop those params off the stack + // and pass as args to the function call + var args = extractArgsForParams(type.getParams()); + call(funcId, args, false); + break; + } case DROP: this.stack.pop(); break; diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/Module.java b/runtime/src/main/java/com/dylibso/chicory/runtime/Module.java index 1af173f10..093e20d0b 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/Module.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/Module.java @@ -137,6 +137,24 @@ public Instance instantiate(HostFunction[] hostFunctions) { exports.put("_start", export); } + Table table = null; + if (module.getTableSection() != null) { + if (module.getTableSection().getTables().length > 1) { + throw new ChicoryException("We don't currently support more than 1 table"); + } + table = module.getTableSection().getTables()[0]; + if (module.getElementSection() != null) { + for (var el : module.getElementSection().getElements()) { + var idx = el.getTableIndex(); + if (idx != 0) + throw new ChicoryException("We don't currently support more than 1 table"); + for (var fi : el.getFuncIndices()) { + table.addFuncRef((int) fi); + } + } + } + } + return new Instance( this, globalInitializers, @@ -145,7 +163,8 @@ public Instance instantiate(HostFunction[] hostFunctions) { functions, types, functionTypes, - hostFuncs); + hostFuncs, + table); } private HostFunction[] mapHostFunctions(Import[] imports, HostFunction[] hostFunctions) { diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/Table.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/Table.java index 4639b9002..bc53200ec 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/Table.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/Table.java @@ -1,14 +1,19 @@ package com.dylibso.chicory.wasm.types; +import java.util.ArrayList; +import java.util.List; + public class Table { private ElementType elementType; private long limitMin; - private Long limitMax; + private long limitMax; + private List funcRefs; public Table(ElementType elementType, long limitMin, Long limitMax) { this.elementType = elementType; this.limitMin = limitMin; this.limitMax = limitMax; + this.funcRefs = new ArrayList<>(); } public ElementType getElementType() { @@ -22,4 +27,12 @@ public long getLimitMin() { public Long getLimitMax() { return limitMax; } + + public void addFuncRef(int funcRef) { + this.funcRefs.add(funcRef); + } + + public int getFuncRef(int index) { + return this.funcRefs.get(index); + } } From 038a2a07f1a6c8e180643c1818da45eef9a47be5 Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Tue, 17 Oct 2023 13:33:16 +0100 Subject: [PATCH 3/6] minor fixes --- runtime/pom.xml | 7 ++++--- .../src/main/java/com/dylibso/chicory/runtime/Machine.java | 2 ++ .../main/java/com/dylibso/chicory/wasm/types/Table.java | 4 +++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/runtime/pom.xml b/runtime/pom.xml index 186c4b66f..dcb719709 100644 --- a/runtime/pom.xml +++ b/runtime/pom.xml @@ -56,11 +56,11 @@ fac.wast, data.wast, forward.wast, - float_literals.wast, - call.wast + float_literals.wast memory_grow.wast, memory_size.wast, - stack.wast + stack.wast, + call.wast SpecV1MemoryGrowTest.test67, @@ -70,6 +70,7 @@ SpecV1MemoryGrowTest.test65, SpecV1MemoryGrowTest.test66, + SpecV1CallTest.test51, SpecV1LocalTeeTest.test31, SpecV1LocalTeeTest.test32, SpecV1LocalTeeTest.test33, diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java b/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java index 32e355f8e..31b93d6fa 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java @@ -59,6 +59,7 @@ void eval(List code) throws ChicoryException { try { var frame = callStack.peek(); boolean shouldReturn = false; + loop: while (frame.pc < code.size()) { if (shouldReturn) return; @@ -139,6 +140,7 @@ void eval(List code) throws ChicoryException { // and pass as args to the function call var args = extractArgsForParams(type.getParams()); call(funcId, args, false); + frame.pc = instruction.getAddress(); break; } case DROP: diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/Table.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/Table.java index bc53200ec..fda8af672 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/Table.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/Table.java @@ -12,7 +12,9 @@ public class Table { public Table(ElementType elementType, long limitMin, Long limitMax) { this.elementType = elementType; this.limitMin = limitMin; - this.limitMax = limitMax; + if (limitMax != null) { + this.limitMax = limitMax; + } this.funcRefs = new ArrayList<>(); } From 247d5b5c6cb7296a14c9037fd53fae517be40e5b Mon Sep 17 00:00:00 2001 From: Benjamin Eckel Date: Thu, 19 Oct 2023 10:15:23 -0500 Subject: [PATCH 4/6] Remove for now --- runtime/pom.xml | 2 ++ runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/runtime/pom.xml b/runtime/pom.xml index dcb719709..ac8829849 100644 --- a/runtime/pom.xml +++ b/runtime/pom.xml @@ -71,6 +71,8 @@ SpecV1MemoryGrowTest.test66, SpecV1CallTest.test51, + SpecV1CallTest.test49, + SpecV1CallTest.test50, SpecV1LocalTeeTest.test31, SpecV1LocalTeeTest.test32, SpecV1LocalTeeTest.test33, diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java b/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java index 31b93d6fa..334befe72 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java @@ -140,7 +140,6 @@ void eval(List code) throws ChicoryException { // and pass as args to the function call var args = extractArgsForParams(type.getParams()); call(funcId, args, false); - frame.pc = instruction.getAddress(); break; } case DROP: From e694b9d1c402bce714df00b46a9e43380eb3ce3f Mon Sep 17 00:00:00 2001 From: Benjamin Eckel Date: Fri, 20 Oct 2023 16:14:18 -0500 Subject: [PATCH 5/6] fix call indirect --- runtime/pom.xml | 4 +--- .../java/com/dylibso/chicory/runtime/Machine.java | 10 +++++++--- .../java/com/dylibso/chicory/runtime/Module.java | 15 +++++++++------ wasm/scripts/instructions.tsv | 2 +- .../com/dylibso/chicory/wasm/types/OpCode.java | 5 +++-- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/runtime/pom.xml b/runtime/pom.xml index ac8829849..ec11636d9 100644 --- a/runtime/pom.xml +++ b/runtime/pom.xml @@ -70,9 +70,6 @@ SpecV1MemoryGrowTest.test65, SpecV1MemoryGrowTest.test66, - SpecV1CallTest.test51, - SpecV1CallTest.test49, - SpecV1CallTest.test50, SpecV1LocalTeeTest.test31, SpecV1LocalTeeTest.test32, SpecV1LocalTeeTest.test33, @@ -117,6 +114,7 @@ SpecV1AddressTest.test251, SpecV1AddressTest.test254, SpecV1AlignTest.test129, + SpecV1CallTest.test51, SpecV1LocalGetTest.test29, SpecV1LocalGetTest.test30, diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java b/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java index 334befe72..47d020494 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java @@ -132,9 +132,13 @@ void eval(List code) throws ChicoryException { break; case CALL_INDIRECT: { - var index = this.stack.pop().asInt(); - var funcId = instance.getTable().getFuncRef(index); - var typeId = instance.getFunctionTypes()[funcId]; + var tableIdx = operands[1]; + if (tableIdx != 0) + throw new ChicoryException( + "We only support a table index of 0 in call-indirect"); + var funcTableIdx = this.stack.pop().asInt(); + var funcId = instance.getTable().getFuncRef(funcTableIdx); + var typeId = (int) operands[0]; var type = instance.getTypes()[typeId]; // given a list of param types, let's pop those params off the stack // and pass as args to the function call diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/Module.java b/runtime/src/main/java/com/dylibso/chicory/runtime/Module.java index 093e20d0b..17d1f48a7 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/Module.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/Module.java @@ -50,15 +50,18 @@ public Instance instantiate(HostFunction[] hostFunctions) { if (g.getInit().length > 2) throw new RuntimeException("We don't support this global initializer"); var instr = g.getInit()[0]; - // TODO we're assuming this is a const value, do we need to eval it? - if (instr.getOpcode() != OpCode.I32_CONST && instr.getOpcode() != OpCode.I64_CONST) { - throw new RuntimeException( - "We only support I32_CONST and I64_CONST on global initializers right now"); - } if (instr.getOpcode() == OpCode.I32_CONST) { globals[i] = Value.i32(instr.getOperands()[0]); - } else { + } else if (instr.getOpcode() == OpCode.I64_CONST) { globals[i] = Value.i64(instr.getOperands()[0]); + } else if (instr.getOpcode() == OpCode.F32_CONST) { + globals[i] = Value.f32(instr.getOperands()[0]); + } else if (instr.getOpcode() == OpCode.F64_CONST) { + globals[i] = Value.f64(instr.getOperands()[0]); + } else { + throw new RuntimeException( + "We only support i32,i64,f32,f64 const opcodes on global initializers right" + + " now"); } } diff --git a/wasm/scripts/instructions.tsv b/wasm/scripts/instructions.tsv index b7788fa34..d6fc85766 100644 --- a/wasm/scripts/instructions.tsv +++ b/wasm/scripts/instructions.tsv @@ -10,7 +10,7 @@ br_if $0D labelidx br_table vec() $0E return $0F call $10 -call_indirect $11 +call_indirect $11 drop $1A [t]→[] select $1B [t t i32]→[t] local.get $20 diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/OpCode.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/OpCode.java index a9d01d447..0b65ebc7f 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/OpCode.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/OpCode.java @@ -19,7 +19,7 @@ public enum OpCode { BR_TABLE(0x0E), // br_table vec() RETURN(0x0F), // return CALL(0x10), // call - CALL_INDIRECT(0x11), // call_indirect + CALL_INDIRECT(0x11), // call_indirect DROP(0x1A), // drop SELECT(0x1B), // select LOCAL_GET(0x20), // local.get @@ -225,7 +225,8 @@ public static WasmEncoding[] getSignature(OpCode o) { BR_TABLE, new WasmEncoding[] {WasmEncoding.VEC_VARUINT, WasmEncoding.VARUINT}); signature.put(RETURN, new WasmEncoding[] {}); signature.put(CALL, new WasmEncoding[] {WasmEncoding.VARUINT}); - signature.put(CALL_INDIRECT, new WasmEncoding[] {WasmEncoding.VARUINT}); + signature.put( + CALL_INDIRECT, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); signature.put(DROP, new WasmEncoding[] {}); signature.put(SELECT, new WasmEncoding[] {}); signature.put(LOCAL_GET, new WasmEncoding[] {WasmEncoding.VARUINT}); From 62594f17bc0025ddd8203be5e58533d2a128124b Mon Sep 17 00:00:00 2001 From: Benjamin Eckel Date: Fri, 20 Oct 2023 16:25:17 -0500 Subject: [PATCH 6/6] fix moduletest --- .../src/test/java/com/dylibso/chicory/runtime/ModuleTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java b/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java index 7e203b836..85c574156 100644 --- a/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java +++ b/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java @@ -172,11 +172,11 @@ public void shouldCountVowels() { var memory = instance.getMemory(); var message = "Hello, World!"; var len = message.getBytes().length; - var ptr = alloc.apply(Value.i32(len)).asInt(); + var ptr = alloc.apply(Value.i32(len))[0].asInt(); memory.put(ptr, message); var result = countVowels.apply(Value.i32(ptr), Value.i32(len)); dealloc.apply(Value.i32(ptr), Value.i32(len)); - assertEquals(3, result.asInt()); + assertEquals(3, result[0].asInt()); } @Test