From dd4571832f0b2f8492cd659a1eaf97125658a8f9 Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Thu, 12 Oct 2023 16:04:04 +0100 Subject: [PATCH] Fix the remaining assertion tests (#29) * Fix the remaining assertion tests * finishing up * fmt --- runtime/pom.xml | 11 ------ .../com/dylibso/chicory/runtime/Machine.java | 35 ++++++++++++++----- .../dylibso/chicory/runtime/ModuleTest.java | 6 ++-- .../com/dylibso/chicory/wasm/types/Value.java | 2 +- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/runtime/pom.xml b/runtime/pom.xml index 2ab41ff63..7ab56240a 100644 --- a/runtime/pom.xml +++ b/runtime/pom.xml @@ -45,17 +45,6 @@ address.wast - SpecV1LocalGetTest.test18, - SpecV1LocalSetTest.test18, - SpecV1LocalTeeTest.test6, - SpecV1LocalTeeTest.test7, - SpecV1LocalTeeTest.test44, - SpecV1MemoryTest.test25, - SpecV1NopTest.test40, - SpecV1NopTest.test41, - SpecV1NopTest.test42, - SpecV1NopTest.test43, - SpecV1NopTest.test44, SpecV1LocalTeeTest.test31, SpecV1LocalTeeTest.test32, 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 685f85ab6..36016de7a 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java @@ -59,8 +59,15 @@ void eval(List code) throws ChicoryException { var instruction = code.get(frame.pc++); var opcode = instruction.getOpcode(); var operands = instruction.getOperands(); - // System.out.println("func="+frame.funcId + "@"+frame.pc + ": " + instruction + " - // stack="+this.stack); + // System.out.println( + // "func=" + // + frame.funcId + // + "@" + // + frame.pc + // + ": " + // + instruction + // + "stack=" + // + this.stack); switch (opcode) { case UNREACHABLE: throw new TrapException("Trapped on unreachable instruction", callStack); @@ -1030,25 +1037,29 @@ void eval(List code) throws ChicoryException { case F64_CONVERT_I64_U: { var tos = this.stack.pop(); - this.stack.push(Value.i64(tos.asLong())); + this.stack.push( + Value.fromDouble(Long.valueOf(tos.asLong()).doubleValue())); break; } case F64_CONVERT_I32_U: { var tos = this.stack.pop(); - this.stack.push(Value.i32(tos.asUInt())); + this.stack.push( + Value.fromDouble(Long.valueOf(tos.asUInt()).doubleValue())); break; } case F64_CONVERT_I32_S: { var tos = this.stack.pop(); - this.stack.push(Value.i32(tos.asInt())); + this.stack.push( + Value.fromDouble(Long.valueOf(tos.asInt()).doubleValue())); break; } case F64_PROMOTE_F32: { var tos = this.stack.pop(); - this.stack.push(Value.f64(tos.asUInt())); + this.stack.push( + Value.fromDouble(Float.valueOf(tos.asFloat()).doubleValue())); break; } case F64_REINTERPRET_I64: @@ -1057,6 +1068,12 @@ void eval(List code) throws ChicoryException { this.stack.push(Value.i64(tos.asLong())); break; } + case I64_TRUNC_F64_S: + { + var tos = this.stack.pop(); + this.stack.push(Value.i64(Double.valueOf(tos.asDouble()).longValue())); + break; + } default: throw new RuntimeException( "Machine doesn't recognize Instruction " + instruction); @@ -1088,13 +1105,13 @@ public void printStackTrace() { Value[] extractArgsForParams(ValueType[] params) { if (params == null) return new Value[] {}; var args = new Value[params.length]; - for (var i = 0; i < params.length; i++) { + for (var i = params.length; i > 0; i--) { var p = this.stack.pop(); - var t = params[i]; + var t = params[i - 1]; if (p.getType() != t) { throw new RuntimeException("Type error when extracting args."); } - args[i] = p; + args[i - 1] = p; } return args; } 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 f8aa0c7d8..3f31f30e9 100644 --- a/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java +++ b/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java @@ -87,9 +87,9 @@ public void shouldConsoleLogWithString() { var printer = new Printer("Hello, World!"); var func = new HostFunction( - (Memory memory, Value... args) -> { - var offset = args[0].asInt(); - var len = args[1].asInt(); + (Memory memory, Value... args) -> { // decompiled is: console_log(13, 0); + var len = args[0].asInt(); + var offset = args[1].asInt(); var message = memory.getString(offset, len); printer.println(message); return null; diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/Value.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/Value.java index 2576d2815..ce54f035e 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/Value.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/Value.java @@ -123,7 +123,7 @@ public long asUInt() { // TODO memoize these public long asLong() { - return ByteBuffer.wrap(this.data).getLong(); + return new BigInteger(this.data).longValue(); } public BigInteger asULong() {