From 2c64c7173ecda4031b77ce351516ca510b7d8fdb Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Mon, 9 Oct 2023 16:48:57 +0100 Subject: [PATCH] Fix spotless config (#14) * Fix spotless configuration * Bulk reformatting --- README.md | 3 +- pom.xml | 52 +- runtime/pom.xml | 32 +- .../dylibso/chicory/runtime/HostFunction.java | 73 +- .../com/dylibso/chicory/runtime/Instance.java | 24 +- .../com/dylibso/chicory/runtime/MStack.java | 3 +- .../com/dylibso/chicory/runtime/Machine.java | 1503 +++++++++-------- .../com/dylibso/chicory/runtime/Memory.java | 37 +- .../com/dylibso/chicory/runtime/Module.java | 28 +- .../dylibso/chicory/runtime/StackFrame.java | 17 +- .../chicory/runtime/TrapException.java | 1 - .../exceptions/WASMRuntimeException.java | 2 + .../dylibso/chicory/runtime/ModuleTest.java | 163 +- test-gen-plugin/pom.xml | 115 +- .../chicory/maven/JavaParserMavenUtils.java | 34 +- .../dylibso/chicory/maven/JavaTestGen.java | 112 +- .../dylibso/chicory/maven/StringUtils.java | 1 - .../dylibso/chicory/maven/TarExtractor.java | 10 +- .../dylibso/chicory/maven/TestGenMojo.java | 49 +- .../chicory/maven/TestSuiteDownloader.java | 14 +- .../chicory/maven/Wast2JsonWrapper.java | 46 +- .../dylibso/chicory/maven/wast/Action.java | 21 +- .../chicory/maven/wast/ActionType.java | 1 + .../dylibso/chicory/maven/wast/Command.java | 29 +- .../chicory/maven/wast/CommandType.java | 1 + .../dylibso/chicory/maven/wast/WasmValue.java | 24 +- .../chicory/maven/wast/WasmValueType.java | 1 + .../com/dylibso/chicory/maven/wast/Wast.java | 2 - wasm/README.md | 3 +- wasm/pom.xml | 4 +- .../com/dylibso/chicory/wasm/ControlFlow.java | 138 +- .../com/dylibso/chicory/wasm/Encoding.java | 3 +- .../java/com/dylibso/chicory/wasm/Module.java | 1 - .../dylibso/chicory/wasm/ModuleBuilder.java | 4 +- .../java/com/dylibso/chicory/wasm/Parser.java | 287 ++-- .../wasm/exceptions/ChicoryException.java | 2 + .../wasm/exceptions/InvalidException.java | 2 + .../wasm/exceptions/MalformedException.java | 2 + .../com/dylibso/chicory/wasm/types/Ast.java | 54 +- .../chicory/wasm/types/CodeSection.java | 8 +- .../chicory/wasm/types/CustomSection.java | 2 +- .../chicory/wasm/types/DataSection.java | 4 +- .../chicory/wasm/types/ElementSection.java | 4 +- .../chicory/wasm/types/ElementType.java | 15 +- .../chicory/wasm/types/ExportDescType.java | 17 +- .../chicory/wasm/types/ExportSection.java | 4 +- .../chicory/wasm/types/FunctionSection.java | 4 +- .../chicory/wasm/types/GlobalSection.java | 4 +- .../chicory/wasm/types/ImportDesc.java | 10 +- .../chicory/wasm/types/ImportDescType.java | 17 +- .../chicory/wasm/types/ImportSection.java | 4 +- .../chicory/wasm/types/Instruction.java | 1 - .../chicory/wasm/types/MemorySection.java | 4 +- .../chicory/wasm/types/MutabilityType.java | 16 +- .../dylibso/chicory/wasm/types/OpCode.java | 732 ++++---- .../chicory/wasm/types/StartSection.java | 2 +- .../chicory/wasm/types/TableSection.java | 4 +- .../chicory/wasm/types/TypeSection.java | 4 +- .../com/dylibso/chicory/wasm/types/Value.java | 104 +- .../dylibso/chicory/wasm/types/ValueType.java | 16 +- .../com/dylibso/chicory/wasm/ParserTest.java | 72 +- .../dylibso/chicory/wasm/types/ValueTest.java | 5 +- 62 files changed, 2155 insertions(+), 1796 deletions(-) diff --git a/README.md b/README.md index 827937e93..87e76fa0e 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ sh scripts/compile-tests.sh # This pulls down the wasm-testsuite in /tmp and copies over some needed # files to our src/test/resources directory. We only need to run -# this is if we need to update the v1 spec data +# this is if we need to update the v1 spec data sh scripts/sync-test-suite.sh # This takes the wasm test suite data and generates Junit tests @@ -107,4 +107,3 @@ sh scripts/sync-test-suite.sh # If we are updating the specs or changing the code generator ruby scripts/generate-java.rb ``` - diff --git a/pom.xml b/pom.xml index 897a9adba..d6867ef5a 100644 --- a/pom.xml +++ b/pom.xml @@ -1,16 +1,19 @@ - - + 4.0.0 com.dylibso.chickory chicory 1.0-SNAPSHOT + pom chicory http://www.example.com - pom + + + test-gen-plugin + wasm + runtime + UTF-8 @@ -24,12 +27,6 @@ 2.40.0 - - test-gen-plugin - wasm - runtime - - @@ -54,9 +51,9 @@ maven-compiler-plugin ${maven-compiler-plugin.version} - ${maven.compiler.source} - ${maven.compiler.target} - ${maven.compiler.target} + ${maven.compiler.source} + ${maven.compiler.target} + ${maven.compiler.target} @@ -77,11 +74,11 @@ - **/src/main/java/**/*.java - **/src/test/java/**/*.java + *.md + .gitignore - - + + true 2 @@ -93,18 +90,29 @@ **/src/main/java/**/*.java **/src/test/java/**/*.java - - + + 1.18.1 + + true + false + + + + + + **/pom.xml + + + format - process-sources check - apply + process-sources diff --git a/runtime/pom.xml b/runtime/pom.xml index a08f0133a..e93b6d942 100644 --- a/runtime/pom.xml +++ b/runtime/pom.xml @@ -1,8 +1,5 @@ - - + 4.0.0 @@ -18,20 +15,29 @@ com.dylibso.chickory wasm - + junit junit - com.dylibso.chickory test-gen-plugin ${project.version} + + i32.wast, + i64.wast, + return.wast + SpecV1ReturnTest.test6, + SpecV1ReturnTest.test7, + SpecV1ReturnTest.test9, + SpecV1ReturnTest.test49, + SpecV1ReturnTest.test55 + @@ -39,20 +45,6 @@ - - - i32.wast, - i64.wast, - return.wast - - - SpecV1ReturnTest.test6, - SpecV1ReturnTest.test7, - SpecV1ReturnTest.test9, - SpecV1ReturnTest.test49, - SpecV1ReturnTest.test55 - - diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/HostFunction.java b/runtime/src/main/java/com/dylibso/chicory/runtime/HostFunction.java index 9ff6eb4ef..249dfd2f6 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/HostFunction.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/HostFunction.java @@ -1,42 +1,45 @@ package com.dylibso.chicory.runtime; import com.dylibso.chicory.wasm.types.ValueType; - import java.util.List; public class HostFunction { - private final WasmFunctionHandle handle; - private final String moduleName; - private final String fieldName; - private final List paramTypes; - private final List returnTypes; - - HostFunction(WasmFunctionHandle handle, String moduleName, String fieldName, List paramTypes, List returnTypes) { - this.handle = handle; - this.moduleName = moduleName; - this.fieldName = fieldName; - this.paramTypes = paramTypes; - this.returnTypes = returnTypes; - } - - public WasmFunctionHandle getHandle() { - return handle; - } - - public String getModuleName() { - return moduleName; - } - - public String getFieldName() { - return fieldName; - } - - public List getParamTypes() { - return paramTypes; - } - - public List getReturnTypes() { - return returnTypes; - } - + private final WasmFunctionHandle handle; + private final String moduleName; + private final String fieldName; + private final List paramTypes; + private final List returnTypes; + + HostFunction( + WasmFunctionHandle handle, + String moduleName, + String fieldName, + List paramTypes, + List returnTypes) { + this.handle = handle; + this.moduleName = moduleName; + this.fieldName = fieldName; + this.paramTypes = paramTypes; + this.returnTypes = returnTypes; + } + + public WasmFunctionHandle getHandle() { + return handle; + } + + public String getModuleName() { + return moduleName; + } + + public String getFieldName() { + return fieldName; + } + + public List getParamTypes() { + return paramTypes; + } + + public List getReturnTypes() { + return returnTypes; + } } 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 104deb3be..d69d02087 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/Instance.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/Instance.java @@ -2,8 +2,6 @@ import com.dylibso.chicory.wasm.types.*; -import java.util.Arrays; - public class Instance { private Module module; private Machine machine; @@ -13,17 +11,17 @@ public class Instance { private Value[] globals; private FunctionType[] types; private int[] functionTypes; - private HostFunction[] imports; + private HostFunction[] imports; - public Instance(Module module, - Global[] globalInitalizers, - Value[] globals, - Memory memory, - FunctionBody[] functions, - FunctionType[] types, - int[] functionTypes, - HostFunction[] imports - ) { + public Instance( + Module module, + Global[] globalInitalizers, + Value[] globals, + Memory memory, + FunctionBody[] functions, + FunctionType[] types, + int[] functionTypes, + HostFunction[] imports) { this.module = module; this.globalInitalizers = globalInitalizers; this.globals = globals; @@ -39,7 +37,7 @@ public ExportFunction getExport(String name) { var export = module.getExport(name); var funcId = (int) export.getDesc().getIndex(); return (args) -> { - //System.out.println("Args: " + Arrays.toString(args)); + // System.out.println("Args: " + Arrays.toString(args)); try { return machine.call(funcId, args, true); } catch (Exception e) { diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/MStack.java b/runtime/src/main/java/com/dylibso/chicory/runtime/MStack.java index 4b42ee560..8e15a28b4 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/MStack.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/MStack.java @@ -1,9 +1,8 @@ package com.dylibso.chicory.runtime; -import com.dylibso.chicory.wasm.types.Value; +import com.dylibso.chicory.wasm.types.Value; import java.util.Stack; - /** * A temporary class that gives us a little more control over the interface. * It allows us to assert non-nulls as well as throw stack under and overflow exceptions 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 0a21ef781..c55576dd0 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java @@ -1,9 +1,8 @@ package com.dylibso.chicory.runtime; -import com.dylibso.chicory.wasm.exceptions.ChicoryException; import com.dylibso.chicory.runtime.exceptions.WASMRuntimeException; +import com.dylibso.chicory.wasm.exceptions.ChicoryException; import com.dylibso.chicory.wasm.types.*; - import java.util.*; /** @@ -60,721 +59,845 @@ 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); case NOP: break; case LOOP: - case BLOCK: { - frame.blockDepth++; - break; - } - case IF: { - frame.blockDepth++; - var pred = this.stack.pop().asInt(); - if (pred == 0) { - frame.pc = instruction.getLabelFalse(); - } else { - frame.pc = instruction.getLabelTrue(); + case BLOCK: + { + frame.blockDepth++; + break; + } + case IF: + { + frame.blockDepth++; + var pred = this.stack.pop().asInt(); + if (pred == 0) { + frame.pc = instruction.getLabelFalse(); + } else { + frame.pc = instruction.getLabelTrue(); + } + break; } - break; - } case ELSE: - case BR: { - frame.pc = instruction.getLabelTrue(); - break; - } - case BR_IF: { - var pred = this.stack.pop().asInt(); - if (pred == 0) { - frame.pc = instruction.getLabelFalse(); - } else { + case BR: + { frame.pc = instruction.getLabelTrue(); + break; } - break; - } - case BR_TABLE: { - var pred = this.stack.pop().asInt(); - if (pred < 0 || pred >= instruction.getLabelTable().length - 1) { - // choose default - frame.pc = instruction.getLabelTable()[instruction.getLabelTable().length - 1]; - } else { - frame.pc = instruction.getLabelTable()[pred]; + case BR_IF: + { + var pred = this.stack.pop().asInt(); + if (pred == 0) { + frame.pc = instruction.getLabelFalse(); + } else { + frame.pc = instruction.getLabelTrue(); + } + break; + } + case BR_TABLE: + { + var pred = this.stack.pop().asInt(); + if (pred < 0 || pred >= instruction.getLabelTable().length - 1) { + // choose default + frame.pc = + instruction + .getLabelTable()[ + instruction.getLabelTable().length - 1]; + } else { + frame.pc = instruction.getLabelTable()[pred]; + } + break; } - break; - } 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().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 DROP: this.stack.pop(); break; - case SELECT: { - var pred = this.stack.pop().asInt(); - var b = this.stack.pop(); - var a = this.stack.pop(); - if (pred == 0) { - this.stack.push(b); - } else { - this.stack.push(a); + case SELECT: + { + var pred = this.stack.pop().asInt(); + var b = this.stack.pop(); + var a = this.stack.pop(); + if (pred == 0) { + this.stack.push(b); + } else { + this.stack.push(a); + } + break; } - break; - } - case END: { - frame.blockDepth--; - // if this is the last end, then we're done with - // the function - if (frame.blockDepth == 0) { - break loop; + case END: + { + frame.blockDepth--; + // if this is the last end, then we're done with + // the function + if (frame.blockDepth == 0) { + break loop; + } + break; + } + case LOCAL_GET: + { + this.stack.push(frame.getLocal((int) operands[0])); + break; + } + case LOCAL_SET: + { + frame.setLocal((int) operands[0], this.stack.pop()); + break; + } + case LOCAL_TEE: + { + // here we peek instead of pop, leaving it on the stack + frame.setLocal((int) operands[0], this.stack.peek()); + break; + } + case GLOBAL_GET: + { + var val = instance.getGlobal((int) operands[0]); + this.stack.push(val); + break; + } + case GLOBAL_SET: + { + var id = (int) operands[0]; + var global = instance.getGlobalInitalizers()[id]; + if (global.getMutabilityType() == MutabilityType.Const) + throw new RuntimeException( + "Can't call GLOBAL_SET on immutable global"); + var val = this.stack.pop(); + instance.setGlobal(id, val); + break; + } + // TODO signed and unsigned are the same right now + case I32_LOAD: + { + var ptr = (int) (operands[0] + this.stack.pop().asInt()); + var val = instance.getMemory().getI32(ptr); + this.stack.push(val); + break; + } + case I64_LOAD: + { + var ptr = (int) (operands[0] + this.stack.pop().asInt()); + var val = instance.getMemory().getI64(ptr); + this.stack.push(val); + break; + } + case F32_LOAD: + { + var ptr = (int) (operands[0] + this.stack.pop().asInt()); + var val = instance.getMemory().getF32(ptr); + this.stack.push(val); + break; + } + case F64_LOAD: + { + var ptr = (int) (operands[0] + this.stack.pop().asInt()); + var val = instance.getMemory().getF64(ptr); + this.stack.push(val); + break; + } + case I32_LOAD8_S: + { + var ptr = (int) (operands[0] + this.stack.pop().asInt()); + var val = instance.getMemory().getI8(ptr); + this.stack.push(val); + break; + } + case I64_LOAD8_S: + { + var ptr = (int) (operands[0] + this.stack.pop().asInt()); + var val = instance.getMemory().getI8(ptr); + // TODO a bit hacky + this.stack.push(Value.i64(val.asInt())); + break; + } + case I32_LOAD8_U: + { + var ptr = (int) (operands[0] + this.stack.pop().asInt()); + var val = instance.getMemory().getI8U(ptr); + this.stack.push(val); + break; + } + case I64_LOAD8_U: + { + var ptr = (int) (operands[0] + this.stack.pop().asInt()); + var val = instance.getMemory().getI8U(ptr); + // TODO a bit hacky + this.stack.push(Value.i64(val.asInt())); + break; + } + case I32_LOAD16_S: + { + var ptr = (int) (operands[0] + this.stack.pop().asInt()); + var val = instance.getMemory().getI16(ptr); + this.stack.push(val); + break; + } + case I64_LOAD16_S: + { + var ptr = (int) (operands[0] + this.stack.pop().asInt()); + var val = instance.getMemory().getI16(ptr); + // TODO this is a bit hacky + this.stack.push(Value.i64(val.asInt())); + break; + } + case I32_LOAD16_U: + { + var ptr = (int) (operands[0] + this.stack.pop().asInt()); + var val = instance.getMemory().getU16(ptr); + this.stack.push(val); + break; + } + case I64_LOAD16_U: + { + var ptr = (int) (operands[0] + this.stack.pop().asInt()); + var val = instance.getMemory().getU16(ptr); + // TODO this is a bit hacky + this.stack.push(Value.i64(val.asInt())); + break; + } + case I64_LOAD32_S: + { + var ptr = (int) (operands[0] + this.stack.pop().asInt()); + var val = instance.getMemory().getI32(ptr); + // TODO this is a bit hacky + this.stack.push(Value.i64(val.asInt())); + break; + } + case I64_LOAD32_U: + { + var ptr = (int) (operands[0] + this.stack.pop().asInt()); + var val = instance.getMemory().getU32(ptr); + this.stack.push(val); + break; + } + case I32_STORE: + { + var value = this.stack.pop().asInt(); + var ptr = (int) (operands[0] + this.stack.pop().asInt()); + instance.getMemory().putI32(ptr, value); + break; } - break; - } - case LOCAL_GET: { - this.stack.push(frame.getLocal((int) operands[0])); - break; - } - case LOCAL_SET: { - frame.setLocal((int) operands[0], this.stack.pop()); - break; - } - case LOCAL_TEE: { - // here we peek instead of pop, leaving it on the stack - frame.setLocal((int) operands[0], this.stack.peek()); - break; - } - case GLOBAL_GET: { - var val = instance.getGlobal((int) operands[0]); - this.stack.push(val); - break; - } - case GLOBAL_SET: { - var id = (int) operands[0]; - var global = instance.getGlobalInitalizers()[id]; - if (global.getMutabilityType() == MutabilityType.Const) - throw new RuntimeException("Can't call GLOBAL_SET on immutable global"); - var val = this.stack.pop(); - instance.setGlobal(id, val); - break; - } - // TODO signed and unsigned are the same right now - case I32_LOAD: { - var ptr = (int) (operands[0] + this.stack.pop().asInt()); - var val = instance.getMemory().getI32(ptr); - this.stack.push(val); - break; - } - case I64_LOAD: { - var ptr = (int) (operands[0] + this.stack.pop().asInt()); - var val = instance.getMemory().getI64(ptr); - this.stack.push(val); - break; - } - case F32_LOAD: { - var ptr = (int) (operands[0] + this.stack.pop().asInt()); - var val = instance.getMemory().getF32(ptr); - this.stack.push(val); - break; - } - case F64_LOAD: { - var ptr = (int) (operands[0] + this.stack.pop().asInt()); - var val = instance.getMemory().getF64(ptr); - this.stack.push(val); - break; - } - case I32_LOAD8_S: { - var ptr = (int) (operands[0] + this.stack.pop().asInt()); - var val = instance.getMemory().getI8(ptr); - this.stack.push(val); - break; - } - case I64_LOAD8_S: { - var ptr = (int) (operands[0] + this.stack.pop().asInt()); - var val = instance.getMemory().getI8(ptr); - // TODO a bit hacky - this.stack.push(Value.i64(val.asInt())); - break; - } - case I32_LOAD8_U: { - var ptr = (int) (operands[0] + this.stack.pop().asInt()); - var val = instance.getMemory().getI8U(ptr); - this.stack.push(val); - break; - } - case I64_LOAD8_U: { - var ptr = (int) (operands[0] + this.stack.pop().asInt()); - var val = instance.getMemory().getI8U(ptr); - // TODO a bit hacky - this.stack.push(Value.i64(val.asInt())); - break; - } - case I32_LOAD16_S: { - var ptr = (int) (operands[0] + this.stack.pop().asInt()); - var val = instance.getMemory().getI16(ptr); - this.stack.push(val); - break; - } - case I64_LOAD16_S: { - var ptr = (int) (operands[0] + this.stack.pop().asInt()); - var val = instance.getMemory().getI16(ptr); - // TODO this is a bit hacky - this.stack.push(Value.i64(val.asInt())); - break; - } - case I32_LOAD16_U: { - var ptr = (int) (operands[0] + this.stack.pop().asInt()); - var val = instance.getMemory().getU16(ptr); - this.stack.push(val); - break; - } - case I64_LOAD16_U: { - var ptr = (int) (operands[0] + this.stack.pop().asInt()); - var val = instance.getMemory().getU16(ptr); - // TODO this is a bit hacky - this.stack.push(Value.i64(val.asInt())); - break; - } - case I64_LOAD32_S: { - var ptr = (int) (operands[0] + this.stack.pop().asInt()); - var val = instance.getMemory().getI32(ptr); - // TODO this is a bit hacky - this.stack.push(Value.i64(val.asInt())); - break; - } - case I64_LOAD32_U: { - var ptr = (int) (operands[0] + this.stack.pop().asInt()); - var val = instance.getMemory().getU32(ptr); - this.stack.push(val); - break; - } - case I32_STORE: { - var value = this.stack.pop().asInt(); - var ptr = (int) (operands[0] + this.stack.pop().asInt()); - instance.getMemory().putI32(ptr, value); - break; - } case I32_STORE16: - case I64_STORE16: { - var value = this.stack.pop().asShort(); - var ptr = (int) (operands[0] + this.stack.pop().asInt()); - instance.getMemory().putShort(ptr, value); - break; - } - case I64_STORE: { - var value = this.stack.pop().asLong(); - var ptr = (int) (operands[0] + this.stack.pop().asInt()); - instance.getMemory().putI64(ptr, value); - break; - } - case MEMORY_GROW: { - instance.getMemory().grow(); - break; - } + case I64_STORE16: + { + var value = this.stack.pop().asShort(); + var ptr = (int) (operands[0] + this.stack.pop().asInt()); + instance.getMemory().putShort(ptr, value); + break; + } + case I64_STORE: + { + var value = this.stack.pop().asLong(); + var ptr = (int) (operands[0] + this.stack.pop().asInt()); + instance.getMemory().putI64(ptr, value); + break; + } + case MEMORY_GROW: + { + instance.getMemory().grow(); + break; + } case I32_STORE8: - case I64_STORE8: { - var value = this.stack.pop().asByte(); - var ptr = (int) (operands[0] + this.stack.pop().asInt()); - instance.getMemory().putByte(ptr, value); - break; - } - case I64_STORE32: { - var value = this.stack.pop().asInt(); - var ptr = (int) (operands[0] + this.stack.pop().asInt()); - instance.getMemory().putI32(ptr, value); - break; - } - case MEMORY_SIZE: { - var sz = instance.getMemory().getInitialSize(); - this.stack.push(Value.i32(sz)); - break; - } - // TODO 32bit and 64 bit operations are the same for now - case I32_CONST: { - this.stack.push(Value.i32(operands[0])); - break; - } - case I64_CONST: { - this.stack.push(Value.i64(operands[0])); - break; - } - case F32_CONST: { - this.stack.push(Value.f32(operands[0])); - break; - } - case F64_CONST: { - this.stack.push(Value.f64(operands[0])); - break; - } - case I32_EQ: { - var a = stack.pop().asInt(); - var b = stack.pop().asInt(); - this.stack.push(a == b ? Value.TRUE : Value.FALSE); - break; - } - case I64_EQ: { - var a = this.stack.pop().asLong(); - var b = this.stack.pop().asLong(); - this.stack.push(a == b ? Value.TRUE : Value.FALSE); - break; - } - case I32_NE: { - var a = this.stack.pop().asInt(); - var b = this.stack.pop().asInt(); - this.stack.push(a == b ? Value.FALSE : Value.TRUE); - break; - } - case I64_NE: { - var a = this.stack.pop().asLong(); - var b = this.stack.pop().asLong(); - this.stack.push(a == b ? Value.FALSE : Value.TRUE); - break; - } - case I32_EQZ: { - var a = this.stack.pop().asInt(); - this.stack.push(a == 0 ? Value.TRUE : Value.FALSE); - break; - } - case I64_EQZ: { - var a = this.stack.pop().asLong(); - this.stack.push(a == 0L ? Value.TRUE : Value.FALSE); - break; - } - case I32_LT_S: { - var b = this.stack.pop().asInt(); - var a = this.stack.pop().asInt(); - this.stack.push(a < b ? Value.TRUE : Value.FALSE); - break; - } - case I32_LT_U: { - var b = this.stack.pop().asUInt(); - var a = this.stack.pop().asUInt(); - this.stack.push(a < b ? Value.TRUE : Value.FALSE); - break; - } - case I64_LT_S: { - var b = this.stack.pop().asLong(); - var a = this.stack.pop().asLong(); - this.stack.push(a < b ? Value.TRUE : Value.FALSE); - break; - } - case I64_LT_U: { - var b = this.stack.pop().asULong(); - var a = this.stack.pop().asULong(); - this.stack.push(a.compareTo(b) < 0 ? Value.TRUE : Value.FALSE); - break; - } - case I32_GT_S: { - var b = this.stack.pop().asInt(); - var a = this.stack.pop().asInt(); - this.stack.push(a > b ? Value.TRUE : Value.FALSE); - break; - } - case I32_GT_U: { - var b = this.stack.pop().asUInt(); - var a = this.stack.pop().asUInt(); - this.stack.push(a > b ? Value.TRUE : Value.FALSE); - break; - } - case I64_GT_S: { - var b = this.stack.pop().asLong(); - var a = this.stack.pop().asLong(); - this.stack.push(a > b ? Value.TRUE : Value.FALSE); - break; - } - case I64_GT_U: { - var b = this.stack.pop().asULong(); - var a = this.stack.pop().asULong(); - this.stack.push(a.compareTo(b) > 0 ? Value.TRUE : Value.FALSE); - break; - } - case I32_GE_S: { - var b = this.stack.pop().asInt(); - var a = this.stack.pop().asInt(); - this.stack.push(a >= b ? Value.TRUE : Value.FALSE); - break; - } - case I32_GE_U: { - var b = this.stack.pop().asUInt(); - var a = this.stack.pop().asUInt(); - this.stack.push(a >= b ? Value.TRUE : Value.FALSE); - break; - } - case I64_GE_U: { - var b = this.stack.pop().asULong(); - var a = this.stack.pop().asULong(); - this.stack.push(a.compareTo(b) >= 0 ? Value.TRUE : Value.FALSE); - break; - } - case I64_GE_S: { - var b = this.stack.pop().asLong(); - var a = this.stack.pop().asLong(); - this.stack.push(a >= b ? Value.TRUE : Value.FALSE); - break; - } - case I32_LE_S: { - var b = this.stack.pop().asInt(); - var a = this.stack.pop().asInt(); - this.stack.push(a <= b ? Value.TRUE : Value.FALSE); - break; - } - case I32_LE_U: { - var b = this.stack.pop().asUInt(); - var a = this.stack.pop().asUInt(); - this.stack.push(a <= b ? Value.TRUE : Value.FALSE); - break; - } - case I64_LE_S: { - var b = this.stack.pop().asLong(); - var a = this.stack.pop().asLong(); - this.stack.push(a <= b ? Value.TRUE : Value.FALSE); - break; - } - case I64_LE_U: { - var b = this.stack.pop().asULong(); - var a = this.stack.pop().asULong(); - this.stack.push(a.compareTo(b) <= 0 ? Value.TRUE : Value.FALSE); - break; - } - case F32_EQ: { - var a = this.stack.pop().asFloat(); - var b = this.stack.pop().asFloat(); - this.stack.push(a == b ? Value.TRUE : Value.FALSE); - break; - } - case F64_EQ: { - var a = this.stack.pop().asDouble(); - var b = this.stack.pop().asDouble(); - this.stack.push(a == b ? Value.TRUE : Value.FALSE); - break; - } - case I32_CLZ: { - var tos = this.stack.pop().asInt(); - var count = Integer.numberOfLeadingZeros(tos); - this.stack.push(Value.i32(count)); - break; - } - case I32_CTZ: { - var tos = this.stack.pop().asInt(); - var count = Integer.numberOfTrailingZeros(tos); - this.stack.push(Value.i32(count)); - break; - } - case I32_POPCNT: { - var tos = this.stack.pop().asInt(); - var count = Integer.bitCount(tos); - this.stack.push(Value.i32(count)); - break; - } - case I32_ADD: { - var a = this.stack.pop().asInt(); - var b = this.stack.pop().asInt(); - this.stack.push(Value.i32(a + b)); - break; - } - case I64_ADD: { - var a = this.stack.pop().asLong(); - var b = this.stack.pop().asLong(); - this.stack.push(Value.i64(a + b)); - break; - } - case I32_SUB: { - var a = this.stack.pop().asInt(); - var b = this.stack.pop().asInt(); - this.stack.push(Value.i32(b - a)); - break; - } - case I64_SUB: { - var a = this.stack.pop().asLong(); - var b = this.stack.pop().asLong(); - this.stack.push(Value.i64(b - a)); - break; - } - case I32_MUL: { - var a = this.stack.pop().asInt(); - var b = this.stack.pop().asInt(); - this.stack.push(Value.i32(a * b)); - break; - } - case I64_MUL: { - var a = this.stack.pop().asLong(); - var b = this.stack.pop().asLong(); - this.stack.push(Value.i64(a * b)); - break; - } - case I32_DIV_S: { - var b = this.stack.pop().asInt(); - var a = this.stack.pop().asInt(); - if (a == Integer.MIN_VALUE && b == -1) { - throw new WASMRuntimeException("integer overflow"); - } - this.stack.push(Value.i32(a / b)); - break; - } - case I32_DIV_U: { - var b = this.stack.pop().asUInt(); - var a = this.stack.pop().asUInt(); - this.stack.push(Value.i32(a / b)); - break; - } - case I64_DIV_S: { - var b = this.stack.pop().asLong(); - var a = this.stack.pop().asLong(); - if (a == Long.MIN_VALUE && b == -1L) { - throw new WASMRuntimeException("integer overflow"); - } - this.stack.push(Value.i64(a / b)); - break; - } - case I64_DIV_U: { - var b = this.stack.pop().asLong(); - var a = this.stack.pop().asLong(); - this.stack.push(Value.i64(Long.divideUnsigned(a, b))); - break; - } - case I32_REM_S: { - var b = this.stack.pop().asInt(); - var a = this.stack.pop().asInt(); - this.stack.push(Value.i32(a % b)); - break; - } - case I32_REM_U: { - var b = this.stack.pop().asUInt(); - var a = this.stack.pop().asUInt(); - this.stack.push(Value.i32(a % b)); - break; - } - case I64_AND: { - var a = this.stack.pop().asLong(); - var b = this.stack.pop().asLong(); - this.stack.push(Value.i64(a & b)); - break; - } - case I64_OR: { - var a = this.stack.pop().asLong(); - var b = this.stack.pop().asLong(); - this.stack.push(Value.i64(a | b)); - break; - } - case I64_XOR: { - var a = this.stack.pop().asLong(); - var b = this.stack.pop().asLong(); - this.stack.push(Value.i64(a ^ b)); - break; - } - case I64_SHL: { - var c = this.stack.pop().asLong(); - var v = this.stack.pop().asLong(); - this.stack.push(Value.i64(v << c)); - break; - } - case I64_SHR_S: { - var c = this.stack.pop().asLong(); - var v = this.stack.pop().asLong(); - this.stack.push(Value.i64(v >> c)); - break; - } - case I64_SHR_U: { - var c = this.stack.pop().asLong(); - var v = this.stack.pop().asLong(); - this.stack.push(Value.i64(v >>> c)); - break; - } - case I64_REM_S: { - var b = this.stack.pop().asLong(); - var a = this.stack.pop().asLong(); - this.stack.push(Value.i64(a % b)); - break; - } - case I64_REM_U: { - var b = this.stack.pop().asLong(); - var a = this.stack.pop().asLong(); - this.stack.push(Value.i64(Long.remainderUnsigned(a, b))); - break; - } - case I64_ROTL: { - var c = this.stack.pop().asLong(); - var v = this.stack.pop().asLong(); - var z = (v << c) | (v >>> (64 - c)); - this.stack.push(Value.i64(z)); - break; - } - case I64_ROTR: { - var c = this.stack.pop().asLong(); - var v = this.stack.pop().asLong(); - var z = (v >>> c) | (v << (64 - c)); - this.stack.push(Value.i64(z)); - break; - } - case I64_CLZ: { - var tos = this.stack.pop(); - var count = Long.numberOfLeadingZeros(tos.asLong()); - this.stack.push(Value.i64(count)); - break; - } - case I64_CTZ: { - var tos = this.stack.pop(); - var count = Long.numberOfTrailingZeros(tos.asLong()); - this.stack.push(Value.i64(count)); - break; - } - case I64_POPCNT: { - var tos = this.stack.pop().asLong(); - var count = Long.bitCount(tos); - this.stack.push(Value.i64(count)); - break; - } - case F32_NEG: { - var tos = this.stack.pop().asFloat(); - this.stack.push(Value.fromFloat(-1.0f * tos)); - break; - } - case F64_NEG: { - var tos = this.stack.pop().asDouble(); - this.stack.push(Value.fromDouble(-1.0d * tos)); - break; - } - case CALL: { - var funcId = (int) operands[0]; - 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 I32_AND: { - var a = this.stack.pop().asInt(); - var b = this.stack.pop().asInt(); - this.stack.push(Value.i32(a & b)); - break; - } - case I32_OR: { - var a = this.stack.pop().asInt(); - var b = this.stack.pop().asInt(); - this.stack.push(Value.i32(a | b)); - break; - } - case I32_XOR: { - var a = this.stack.pop().asInt(); - var b = this.stack.pop().asInt(); - this.stack.push(Value.i32(a ^ b)); - break; - } - case I32_SHL: { - var c = this.stack.pop().asInt(); - var v = this.stack.pop().asInt(); - this.stack.push(Value.i32(v << c)); - break; - } - case I32_SHR_S: { - var c = this.stack.pop().asInt(); - var v = this.stack.pop().asInt(); - this.stack.push(Value.i32(v >> c)); - break; - } - case I32_SHR_U: { - var c = this.stack.pop().asInt(); - var v = this.stack.pop().asInt(); - this.stack.push(Value.i32(v >>> c)); - break; - } - case I32_ROTL: { - var c = this.stack.pop().asInt(); - var v = this.stack.pop().asInt(); - var z = (v << c) | (v >>> (32 - c)); - this.stack.push(Value.i32(z)); - break; - } - case I32_ROTR: { - var c = this.stack.pop().asInt(); - var v = this.stack.pop().asInt(); - var z = (v >>> c) | (v << (32 - c)); - this.stack.push(Value.i32(z)); - break; - } - case F64_ADD: { - var a = this.stack.pop().asDouble(); - var b = this.stack.pop().asDouble(); - this.stack.push(Value.fromDouble(a + b)); - break; - } - // For the extend_* operations, note that java - // automatically does this when casting from - // smaller to larger primitives - case I32_EXTEND_8_S: { - var tos = this.stack.pop().asByte(); - this.stack.push(Value.i32(tos)); - break; - } - case I32_EXTEND_16_S: { - var original = this.stack.pop().asInt() & 0xFFFF; - if ((original & 0x8000) != 0) original |= 0xFFFF0000; - this.stack.push(Value.i32(original & 0xFFFFFFFFL)); - break; - } - case I64_EXTEND_8_S: { - var tos = this.stack.pop().asByte(); - this.stack.push(Value.i64(tos)); - break; - } - case I64_EXTEND_16_S: { - var tos = this.stack.pop().asShort(); - this.stack.push(Value.i64(tos)); - break; - } - case I64_EXTEND_32_S: { - var tos = this.stack.pop().asInt(); - this.stack.push(Value.i64(tos)); - break; - } - case F64_CONVERT_I64_U: { - var tos = this.stack.pop(); - this.stack.push(Value.i64(tos.asLong())); - break; - } - case F64_CONVERT_I32_U: { - var tos = this.stack.pop(); - this.stack.push(Value.i32(tos.asUInt())); - break; - } - case F64_CONVERT_I32_S: { - var tos = this.stack.pop(); - this.stack.push(Value.i32(tos.asInt())); - break; - } - case F64_PROMOTE_F32: { - var tos = this.stack.pop(); - this.stack.push(Value.f64(tos.asUInt())); - break; - } - case F64_REINTERPRET_I64: { - var tos = this.stack.pop(); - this.stack.push(Value.i64(tos.asLong())); - break; - } + case I64_STORE8: + { + var value = this.stack.pop().asByte(); + var ptr = (int) (operands[0] + this.stack.pop().asInt()); + instance.getMemory().putByte(ptr, value); + break; + } + case I64_STORE32: + { + var value = this.stack.pop().asInt(); + var ptr = (int) (operands[0] + this.stack.pop().asInt()); + instance.getMemory().putI32(ptr, value); + break; + } + case MEMORY_SIZE: + { + var sz = instance.getMemory().getInitialSize(); + this.stack.push(Value.i32(sz)); + break; + } + // TODO 32bit and 64 bit operations are the same for now + case I32_CONST: + { + this.stack.push(Value.i32(operands[0])); + break; + } + case I64_CONST: + { + this.stack.push(Value.i64(operands[0])); + break; + } + case F32_CONST: + { + this.stack.push(Value.f32(operands[0])); + break; + } + case F64_CONST: + { + this.stack.push(Value.f64(operands[0])); + break; + } + case I32_EQ: + { + var a = stack.pop().asInt(); + var b = stack.pop().asInt(); + this.stack.push(a == b ? Value.TRUE : Value.FALSE); + break; + } + case I64_EQ: + { + var a = this.stack.pop().asLong(); + var b = this.stack.pop().asLong(); + this.stack.push(a == b ? Value.TRUE : Value.FALSE); + break; + } + case I32_NE: + { + var a = this.stack.pop().asInt(); + var b = this.stack.pop().asInt(); + this.stack.push(a == b ? Value.FALSE : Value.TRUE); + break; + } + case I64_NE: + { + var a = this.stack.pop().asLong(); + var b = this.stack.pop().asLong(); + this.stack.push(a == b ? Value.FALSE : Value.TRUE); + break; + } + case I32_EQZ: + { + var a = this.stack.pop().asInt(); + this.stack.push(a == 0 ? Value.TRUE : Value.FALSE); + break; + } + case I64_EQZ: + { + var a = this.stack.pop().asLong(); + this.stack.push(a == 0L ? Value.TRUE : Value.FALSE); + break; + } + case I32_LT_S: + { + var b = this.stack.pop().asInt(); + var a = this.stack.pop().asInt(); + this.stack.push(a < b ? Value.TRUE : Value.FALSE); + break; + } + case I32_LT_U: + { + var b = this.stack.pop().asUInt(); + var a = this.stack.pop().asUInt(); + this.stack.push(a < b ? Value.TRUE : Value.FALSE); + break; + } + case I64_LT_S: + { + var b = this.stack.pop().asLong(); + var a = this.stack.pop().asLong(); + this.stack.push(a < b ? Value.TRUE : Value.FALSE); + break; + } + case I64_LT_U: + { + var b = this.stack.pop().asULong(); + var a = this.stack.pop().asULong(); + this.stack.push(a.compareTo(b) < 0 ? Value.TRUE : Value.FALSE); + break; + } + case I32_GT_S: + { + var b = this.stack.pop().asInt(); + var a = this.stack.pop().asInt(); + this.stack.push(a > b ? Value.TRUE : Value.FALSE); + break; + } + case I32_GT_U: + { + var b = this.stack.pop().asUInt(); + var a = this.stack.pop().asUInt(); + this.stack.push(a > b ? Value.TRUE : Value.FALSE); + break; + } + case I64_GT_S: + { + var b = this.stack.pop().asLong(); + var a = this.stack.pop().asLong(); + this.stack.push(a > b ? Value.TRUE : Value.FALSE); + break; + } + case I64_GT_U: + { + var b = this.stack.pop().asULong(); + var a = this.stack.pop().asULong(); + this.stack.push(a.compareTo(b) > 0 ? Value.TRUE : Value.FALSE); + break; + } + case I32_GE_S: + { + var b = this.stack.pop().asInt(); + var a = this.stack.pop().asInt(); + this.stack.push(a >= b ? Value.TRUE : Value.FALSE); + break; + } + case I32_GE_U: + { + var b = this.stack.pop().asUInt(); + var a = this.stack.pop().asUInt(); + this.stack.push(a >= b ? Value.TRUE : Value.FALSE); + break; + } + case I64_GE_U: + { + var b = this.stack.pop().asULong(); + var a = this.stack.pop().asULong(); + this.stack.push(a.compareTo(b) >= 0 ? Value.TRUE : Value.FALSE); + break; + } + case I64_GE_S: + { + var b = this.stack.pop().asLong(); + var a = this.stack.pop().asLong(); + this.stack.push(a >= b ? Value.TRUE : Value.FALSE); + break; + } + case I32_LE_S: + { + var b = this.stack.pop().asInt(); + var a = this.stack.pop().asInt(); + this.stack.push(a <= b ? Value.TRUE : Value.FALSE); + break; + } + case I32_LE_U: + { + var b = this.stack.pop().asUInt(); + var a = this.stack.pop().asUInt(); + this.stack.push(a <= b ? Value.TRUE : Value.FALSE); + break; + } + case I64_LE_S: + { + var b = this.stack.pop().asLong(); + var a = this.stack.pop().asLong(); + this.stack.push(a <= b ? Value.TRUE : Value.FALSE); + break; + } + case I64_LE_U: + { + var b = this.stack.pop().asULong(); + var a = this.stack.pop().asULong(); + this.stack.push(a.compareTo(b) <= 0 ? Value.TRUE : Value.FALSE); + break; + } + case F32_EQ: + { + var a = this.stack.pop().asFloat(); + var b = this.stack.pop().asFloat(); + this.stack.push(a == b ? Value.TRUE : Value.FALSE); + break; + } + case F64_EQ: + { + var a = this.stack.pop().asDouble(); + var b = this.stack.pop().asDouble(); + this.stack.push(a == b ? Value.TRUE : Value.FALSE); + break; + } + case I32_CLZ: + { + var tos = this.stack.pop().asInt(); + var count = Integer.numberOfLeadingZeros(tos); + this.stack.push(Value.i32(count)); + break; + } + case I32_CTZ: + { + var tos = this.stack.pop().asInt(); + var count = Integer.numberOfTrailingZeros(tos); + this.stack.push(Value.i32(count)); + break; + } + case I32_POPCNT: + { + var tos = this.stack.pop().asInt(); + var count = Integer.bitCount(tos); + this.stack.push(Value.i32(count)); + break; + } + case I32_ADD: + { + var a = this.stack.pop().asInt(); + var b = this.stack.pop().asInt(); + this.stack.push(Value.i32(a + b)); + break; + } + case I64_ADD: + { + var a = this.stack.pop().asLong(); + var b = this.stack.pop().asLong(); + this.stack.push(Value.i64(a + b)); + break; + } + case I32_SUB: + { + var a = this.stack.pop().asInt(); + var b = this.stack.pop().asInt(); + this.stack.push(Value.i32(b - a)); + break; + } + case I64_SUB: + { + var a = this.stack.pop().asLong(); + var b = this.stack.pop().asLong(); + this.stack.push(Value.i64(b - a)); + break; + } + case I32_MUL: + { + var a = this.stack.pop().asInt(); + var b = this.stack.pop().asInt(); + this.stack.push(Value.i32(a * b)); + break; + } + case I64_MUL: + { + var a = this.stack.pop().asLong(); + var b = this.stack.pop().asLong(); + this.stack.push(Value.i64(a * b)); + break; + } + case I32_DIV_S: + { + var b = this.stack.pop().asInt(); + var a = this.stack.pop().asInt(); + if (a == Integer.MIN_VALUE && b == -1) { + throw new WASMRuntimeException("integer overflow"); + } + this.stack.push(Value.i32(a / b)); + break; + } + case I32_DIV_U: + { + var b = this.stack.pop().asUInt(); + var a = this.stack.pop().asUInt(); + this.stack.push(Value.i32(a / b)); + break; + } + case I64_DIV_S: + { + var b = this.stack.pop().asLong(); + var a = this.stack.pop().asLong(); + if (a == Long.MIN_VALUE && b == -1L) { + throw new WASMRuntimeException("integer overflow"); + } + this.stack.push(Value.i64(a / b)); + break; + } + case I64_DIV_U: + { + var b = this.stack.pop().asLong(); + var a = this.stack.pop().asLong(); + this.stack.push(Value.i64(Long.divideUnsigned(a, b))); + break; + } + case I32_REM_S: + { + var b = this.stack.pop().asInt(); + var a = this.stack.pop().asInt(); + this.stack.push(Value.i32(a % b)); + break; + } + case I32_REM_U: + { + var b = this.stack.pop().asUInt(); + var a = this.stack.pop().asUInt(); + this.stack.push(Value.i32(a % b)); + break; + } + case I64_AND: + { + var a = this.stack.pop().asLong(); + var b = this.stack.pop().asLong(); + this.stack.push(Value.i64(a & b)); + break; + } + case I64_OR: + { + var a = this.stack.pop().asLong(); + var b = this.stack.pop().asLong(); + this.stack.push(Value.i64(a | b)); + break; + } + case I64_XOR: + { + var a = this.stack.pop().asLong(); + var b = this.stack.pop().asLong(); + this.stack.push(Value.i64(a ^ b)); + break; + } + case I64_SHL: + { + var c = this.stack.pop().asLong(); + var v = this.stack.pop().asLong(); + this.stack.push(Value.i64(v << c)); + break; + } + case I64_SHR_S: + { + var c = this.stack.pop().asLong(); + var v = this.stack.pop().asLong(); + this.stack.push(Value.i64(v >> c)); + break; + } + case I64_SHR_U: + { + var c = this.stack.pop().asLong(); + var v = this.stack.pop().asLong(); + this.stack.push(Value.i64(v >>> c)); + break; + } + case I64_REM_S: + { + var b = this.stack.pop().asLong(); + var a = this.stack.pop().asLong(); + this.stack.push(Value.i64(a % b)); + break; + } + case I64_REM_U: + { + var b = this.stack.pop().asLong(); + var a = this.stack.pop().asLong(); + this.stack.push(Value.i64(Long.remainderUnsigned(a, b))); + break; + } + case I64_ROTL: + { + var c = this.stack.pop().asLong(); + var v = this.stack.pop().asLong(); + var z = (v << c) | (v >>> (64 - c)); + this.stack.push(Value.i64(z)); + break; + } + case I64_ROTR: + { + var c = this.stack.pop().asLong(); + var v = this.stack.pop().asLong(); + var z = (v >>> c) | (v << (64 - c)); + this.stack.push(Value.i64(z)); + break; + } + case I64_CLZ: + { + var tos = this.stack.pop(); + var count = Long.numberOfLeadingZeros(tos.asLong()); + this.stack.push(Value.i64(count)); + break; + } + case I64_CTZ: + { + var tos = this.stack.pop(); + var count = Long.numberOfTrailingZeros(tos.asLong()); + this.stack.push(Value.i64(count)); + break; + } + case I64_POPCNT: + { + var tos = this.stack.pop().asLong(); + var count = Long.bitCount(tos); + this.stack.push(Value.i64(count)); + break; + } + case F32_NEG: + { + var tos = this.stack.pop().asFloat(); + this.stack.push(Value.fromFloat(-1.0f * tos)); + break; + } + case F64_NEG: + { + var tos = this.stack.pop().asDouble(); + this.stack.push(Value.fromDouble(-1.0d * tos)); + break; + } + case CALL: + { + var funcId = (int) operands[0]; + 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 I32_AND: + { + var a = this.stack.pop().asInt(); + var b = this.stack.pop().asInt(); + this.stack.push(Value.i32(a & b)); + break; + } + case I32_OR: + { + var a = this.stack.pop().asInt(); + var b = this.stack.pop().asInt(); + this.stack.push(Value.i32(a | b)); + break; + } + case I32_XOR: + { + var a = this.stack.pop().asInt(); + var b = this.stack.pop().asInt(); + this.stack.push(Value.i32(a ^ b)); + break; + } + case I32_SHL: + { + var c = this.stack.pop().asInt(); + var v = this.stack.pop().asInt(); + this.stack.push(Value.i32(v << c)); + break; + } + case I32_SHR_S: + { + var c = this.stack.pop().asInt(); + var v = this.stack.pop().asInt(); + this.stack.push(Value.i32(v >> c)); + break; + } + case I32_SHR_U: + { + var c = this.stack.pop().asInt(); + var v = this.stack.pop().asInt(); + this.stack.push(Value.i32(v >>> c)); + break; + } + case I32_ROTL: + { + var c = this.stack.pop().asInt(); + var v = this.stack.pop().asInt(); + var z = (v << c) | (v >>> (32 - c)); + this.stack.push(Value.i32(z)); + break; + } + case I32_ROTR: + { + var c = this.stack.pop().asInt(); + var v = this.stack.pop().asInt(); + var z = (v >>> c) | (v << (32 - c)); + this.stack.push(Value.i32(z)); + break; + } + case F64_ADD: + { + var a = this.stack.pop().asDouble(); + var b = this.stack.pop().asDouble(); + this.stack.push(Value.fromDouble(a + b)); + break; + } + // For the extend_* operations, note that java + // automatically does this when casting from + // smaller to larger primitives + case I32_EXTEND_8_S: + { + var tos = this.stack.pop().asByte(); + this.stack.push(Value.i32(tos)); + break; + } + case I32_EXTEND_16_S: + { + var original = this.stack.pop().asInt() & 0xFFFF; + if ((original & 0x8000) != 0) original |= 0xFFFF0000; + this.stack.push(Value.i32(original & 0xFFFFFFFFL)); + break; + } + case I64_EXTEND_8_S: + { + var tos = this.stack.pop().asByte(); + this.stack.push(Value.i64(tos)); + break; + } + case I64_EXTEND_16_S: + { + var tos = this.stack.pop().asShort(); + this.stack.push(Value.i64(tos)); + break; + } + case I64_EXTEND_32_S: + { + var tos = this.stack.pop().asInt(); + this.stack.push(Value.i64(tos)); + break; + } + case F64_CONVERT_I64_U: + { + var tos = this.stack.pop(); + this.stack.push(Value.i64(tos.asLong())); + break; + } + case F64_CONVERT_I32_U: + { + var tos = this.stack.pop(); + this.stack.push(Value.i32(tos.asUInt())); + break; + } + case F64_CONVERT_I32_S: + { + var tos = this.stack.pop(); + this.stack.push(Value.i32(tos.asInt())); + break; + } + case F64_PROMOTE_F32: + { + var tos = this.stack.pop(); + this.stack.push(Value.f64(tos.asUInt())); + break; + } + case F64_REINTERPRET_I64: + { + var tos = this.stack.pop(); + this.stack.push(Value.i64(tos.asLong())); + break; + } default: - throw new RuntimeException("Machine doesn't recognize Instruction " + instruction); + throw new RuntimeException( + "Machine doesn't recognize Instruction " + instruction); } } } catch (ChicoryException e) { // propagate ChicoryExceptions throw e; } catch (ArithmeticException e) { - if (e.getMessage().equalsIgnoreCase("/ by zero") || - e.getMessage().contains("divide by zero")) { // On Linux i64 throws "BigInteger divide by zero" + if (e.getMessage().equalsIgnoreCase("/ by zero") + || e.getMessage() + .contains("divide by zero")) { // On Linux i64 throws "BigInteger divide + // by zero" throw new WASMRuntimeException("integer divide by zero: " + e.getMessage(), e); } throw new WASMRuntimeException(e.getMessage(), e); @@ -791,7 +914,7 @@ public void printStackTrace() { } Value[] extractArgsForParams(ValueType[] params) { - if (params == null) return new Value[]{}; + if (params == null) return new Value[] {}; var args = new Value[params.length]; for (var i = 0; i < params.length; i++) { var p = this.stack.pop(); diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/Memory.java b/runtime/src/main/java/com/dylibso/chicory/runtime/Memory.java index b1ce4bd7f..dc7578854 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/Memory.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/Memory.java @@ -1,14 +1,10 @@ package com.dylibso.chicory.runtime; import com.dylibso.chicory.wasm.types.*; - import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; import java.util.Arrays; -import java.util.List; - /** * Represents the linear memory in the Wasm program. Can be shared @@ -24,7 +20,8 @@ public class Memory { public Memory(MemoryLimits limits) { this.limits = limits; - this.buffer = ByteBuffer.allocate(PAGE_SIZE * limits.getInitial()).order(ByteOrder.LITTLE_ENDIAN); + this.buffer = + ByteBuffer.allocate(PAGE_SIZE * limits.getInitial()).order(ByteOrder.LITTLE_ENDIAN); this.nPages = limits.getInitial(); } @@ -36,7 +33,8 @@ public Memory(MemoryLimits limits, DataSegment[] dataSegments) { public void grow() { // TODO if max is null then we just let it grow as much as it wants? - if (limits.getMaximum() != null && this.nPages > limits.getMaximum()) throw new RuntimeException("Program exceeded max pages: " + limits.getMaximum()); + if (limits.getMaximum() != null && this.nPages > limits.getMaximum()) + throw new RuntimeException("Program exceeded max pages: " + limits.getMaximum()); var capacity = this.buffer.capacity() + PAGE_SIZE; var result = ByteBuffer.allocate(capacity).order(ByteOrder.LITTLE_ENDIAN); var position = this.buffer.position(); @@ -62,7 +60,8 @@ public void reinstantiate() { var offsetInstr = offsetExpr[0]; // TODO how flexible can this be? Do we need to dynamically eval the expression? if (offsetInstr.getOpcode() != OpCode.I32_CONST) { - throw new RuntimeException("Don't support data segment expressions other than i32.const yet"); + throw new RuntimeException( + "Don't support data segment expressions other than i32.const yet"); } var data = segment.getData(); var offset = (int) offsetInstr.getOperands()[0]; @@ -91,7 +90,7 @@ public void put(int offset, String data) { } public void put(int offset, byte[] data) { - //System.out.println("mem-write@" + offset + " " + data); + // System.out.println("mem-write@" + offset + " " + data); for (int i = 0, j = offset; i < data.length; i++, j++) { byte b = data[i]; this.buffer.put(j, b); @@ -121,52 +120,52 @@ public void putI64(int offset, long data) { } public void putByte(int offset, byte data) { - //System.out.println("mem-write@" + offset + " " + data); + // System.out.println("mem-write@" + offset + " " + data); this.buffer.put(offset, data); } public byte get(int offset) { - //System.out.println("mem-read@" + offset); + // System.out.println("mem-read@" + offset); return this.buffer.get(offset); } public Value getI32(int offset) { - //System.out.println("mem-read@" + offset); + // System.out.println("mem-read@" + offset); return Value.i32(this.buffer.getInt(offset)); } public Value getU32(int offset) { - //System.out.println("mem-read@" + offset); + // System.out.println("mem-read@" + offset); return Value.i64(this.buffer.getLong(offset) & 0xffffffffL); } public Value getI64(int offset) { - //System.out.println("mem-read@" + offset); + // System.out.println("mem-read@" + offset); return Value.i64(this.buffer.getLong(offset)); } public Value getI16(int offset) { - //System.out.println("mem-read@" + offset); + // System.out.println("mem-read@" + offset); return Value.i32(this.buffer.getShort(offset)); } public Value getU16(int offset) { - //System.out.println("mem-read@" + offset); + // System.out.println("mem-read@" + offset); return Value.i32(this.buffer.getInt(offset) & 0xffff); } public Value getI8U(int offset) { - //System.out.println("mem-read@" + offset); + // System.out.println("mem-read@" + offset); return Value.i32(this.buffer.get(offset) & 0xff); } public Value getI8(int offset) { - //System.out.println("mem-read@" + offset); + // System.out.println("mem-read@" + offset); return Value.i32(this.buffer.get(offset)); } public Value getF32(int offset) { - //System.out.println("mem-read@" + offset); + // System.out.println("mem-read@" + offset); return Value.f32(this.buffer.getInt(offset)); } @@ -176,7 +175,7 @@ public Value getF64(int offset) { } public void zero() { - Arrays.fill(this.buffer.array(), (byte)0); + Arrays.fill(this.buffer.array(), (byte) 0); this.buffer.position(0); } } 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 72001ee16..96f761f37 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/Module.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/Module.java @@ -1,10 +1,9 @@ package com.dylibso.chicory.runtime; +import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.exceptions.ChicoryException; import com.dylibso.chicory.wasm.exceptions.InvalidException; -import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.types.*; - import java.util.HashMap; public class Module { @@ -41,18 +40,20 @@ public Instance instantiate() { } public Instance instantiate(HostFunction[] hostFunctions) { - var globalInitializers = new Global[]{}; + var globalInitializers = new Global[] {}; if (this.module.getGlobalSection() != null) { globalInitializers = this.module.getGlobalSection().getGlobals(); } var globals = new Value[globalInitializers.length]; for (var i = 0; i < globalInitializers.length; i++) { var g = globalInitializers[i]; - if (g.getInit().length > 2) throw new RuntimeException("We don't support this global initializer"); + 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"); + 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]); @@ -98,7 +99,8 @@ public Instance instantiate(HostFunction[] hostFunctions) { if (module.getImportSection() != null) { imports = new Import[module.getImportSection().getImports().length]; for (var imprt : module.getImportSection().getImports()) { - if (imprt.getDesc().getType() != ImportDescType.FuncIdx) throw new ChicoryException("We don't support non-function imports yet"); + if (imprt.getDesc().getType() != ImportDescType.FuncIdx) + throw new ChicoryException("We don't support non-function imports yet"); var type = (int) imprt.getDesc().getIndex(); functionTypes[funcId] = type; // The global function id increases on this table @@ -127,7 +129,15 @@ public Instance instantiate(HostFunction[] hostFunctions) { exports.put("_start", export); } - return new Instance(this, globalInitializers, globals, memory, functions, types, functionTypes, hostFuncs); + return new Instance( + this, + globalInitializers, + globals, + memory, + functions, + types, + functionTypes, + hostFuncs); } private HostFunction[] mapHostFunctions(Import[] imports, HostFunction[] hostFunctions) { @@ -135,11 +145,11 @@ private HostFunction[] mapHostFunctions(Import[] imports, HostFunction[] hostFun for (var f : hostFunctions) { Integer foundId = null; for (var i : imports) { - if (i.getModuleName().equals(f.getModuleName()) && i.getFieldName().equals(f.getFieldName())) { + if (i.getModuleName().equals(f.getModuleName()) + && i.getFieldName().equals(f.getFieldName())) { foundId = (int) i.getDesc().getIndex(); break; } - } if (foundId == null) throw new RuntimeException("Couldn't map import to function"); hostImports[foundId] = f; diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/StackFrame.java b/runtime/src/main/java/com/dylibso/chicory/runtime/StackFrame.java index 197c88ff0..ea5dce205 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/StackFrame.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/StackFrame.java @@ -2,7 +2,6 @@ import com.dylibso.chicory.wasm.types.Value; import com.dylibso.chicory.wasm.types.ValueType; - import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -67,13 +66,13 @@ public Value getLocal(int i) { } public String toString() { - return "func=" + - funcId + - " " + - "pc=" + - pc + - " " + - "locals=" + - Arrays.toString(locals.values().toArray()); + return "func=" + + funcId + + " " + + "pc=" + + pc + + " " + + "locals=" + + Arrays.toString(locals.values().toArray()); } } diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/TrapException.java b/runtime/src/main/java/com/dylibso/chicory/runtime/TrapException.java index bb17819a7..593428cfd 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/TrapException.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/TrapException.java @@ -1,7 +1,6 @@ package com.dylibso.chicory.runtime; import com.dylibso.chicory.wasm.exceptions.ChicoryException; - import java.util.List; import java.util.Stack; diff --git a/runtime/src/main/java/com/dylibso/chicory/runtime/exceptions/WASMRuntimeException.java b/runtime/src/main/java/com/dylibso/chicory/runtime/exceptions/WASMRuntimeException.java index 72fa270f2..d093635de 100644 --- a/runtime/src/main/java/com/dylibso/chicory/runtime/exceptions/WASMRuntimeException.java +++ b/runtime/src/main/java/com/dylibso/chicory/runtime/exceptions/WASMRuntimeException.java @@ -6,9 +6,11 @@ public class WASMRuntimeException extends ChicoryException { public WASMRuntimeException(String msg) { super(msg); } + public WASMRuntimeException(Throwable cause) { super(cause); } + public WASMRuntimeException(String msg, Throwable cause) { super(msg, cause); } 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 ab01a34a3..d0d7eebb0 100644 --- a/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java +++ b/runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java @@ -1,12 +1,11 @@ package com.dylibso.chicory.runtime; +import static org.junit.Assert.*; + import com.dylibso.chicory.wasm.types.Value; import com.dylibso.chicory.wasm.types.ValueType; -import org.junit.Test; - import java.util.List; - -import static org.junit.Assert.*; +import org.junit.Test; class Printer { @@ -16,9 +15,11 @@ class Printer { Printer() { this.expected = null; } + Printer(String expected) { this.expected = expected; } + public void println(String msg) { if (expected == null || expected.equals(msg)) { count++; @@ -31,14 +32,12 @@ public int times() { } } -public class ModuleTest -{ +public class ModuleTest { /** * Rigorous Test :-) */ @Test - public void shouldWorkFactorial() - { + public void shouldWorkFactorial() { var module = Module.build("src/test/resources/wasm/iterfact.wat.wasm"); var instance = module.instantiate(); var iterFact = instance.getExport("iterFact"); @@ -86,27 +85,27 @@ public void shouldExerciseBranches() { @Test 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(); - var message = memory.getString(offset, len); - printer.println(message); - return null; - }, - "console", - "log", - List.of(ValueType.I32, ValueType.I32), - List.of() - ); - var funcs = new HostFunction[]{func}; - var instance = Module.build("src/test/resources/wasm/host-function.wat.wasm").instantiate(funcs); + var func = + new HostFunction( + (Memory memory, Value... args) -> { + var offset = args[0].asInt(); + var len = args[1].asInt(); + var message = memory.getString(offset, len); + printer.println(message); + return null; + }, + "console", + "log", + List.of(ValueType.I32, ValueType.I32), + List.of()); + var funcs = new HostFunction[] {func}; + var instance = + Module.build("src/test/resources/wasm/host-function.wat.wasm").instantiate(funcs); var logIt = instance.getExport("logIt"); logIt.apply(); assertEquals(10, printer.times()); } - @Test public void shouldComputeFactorial() { var module = Module.build("src/test/resources/wasm/iterfact.wat.wasm").instantiate(); @@ -128,29 +127,27 @@ private static long factorial(int number) { return result; } - @Test public void shouldWorkWithStartFunction() { var printer = new Printer("gotit 42"); - var func = new HostFunction( - (Memory memory, Value... args) -> { - var val = args[0]; - printer.println("gotit " + val.asInt()); - return null; - }, - "env", - "gotit", - List.of(ValueType.I32), - List.of() - ); - var funcs = new HostFunction[]{func}; + var func = + new HostFunction( + (Memory memory, Value... args) -> { + var val = args[0]; + printer.println("gotit " + val.asInt()); + return null; + }, + "env", + "gotit", + List.of(ValueType.I32), + List.of()); + var funcs = new HostFunction[] {func}; var module = Module.build("src/test/resources/wasm/start.wat.wasm").instantiate(funcs); var start = module.getExport("_start"); start.apply(); assertTrue(printer.times() > 0); } - @Test public void shouldTrapOnUnreachable() { var instance = Module.build("src/test/resources/wasm/trap.wat.wasm").instantiate(); @@ -166,43 +163,44 @@ public void shouldSupportGlobals() { assertEquals(42, result.asInt()); } -// @Test -// public void shouldCountVowels() { -// var instance = Module.build("src/test/resources/wasm/count_vowels.rs.wasm").instantiate(); -// var countVowels = instance.getExport("count_vowels"); -// //var memory = module.getMemory(); -// //memory.put(60000, "Hello World!"); -// //var result = countVowels.apply(Value.i32(60000)); -// var result = countVowels.apply(); -// assertEquals(3, result.asInt()); -// } - -// @Test -// 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(); -// assertEquals(42, result.asInt()); -// } - -// @Test -// public void shouldRunComplexFunction() { -// // check with: wasmtime src/test/resources/wasm/complex.c.wasm --invoke run -// var instance = Module.build("src/test/resources/wasm/complex.c.wasm").instantiate(); -// var run = instance.getExport("run"); -// var result = run.apply(); -// assertEquals(-679, result.asInt()); -// } - -// @Test -// public void shouldRunMemoryProgramInC() { -// // check with: wasmtime src/test/resources/wasm/memory.c.wasm --invoke run -// var instance = Module.build("src/test/resources/wasm/memory.c.wasm").instantiate(); -// var run = instance.getExport("run"); -// var result = run.apply(); -// assertEquals(11, result.asInt()); -// } + // @Test + // public void shouldCountVowels() { + // var instance = + // Module.build("src/test/resources/wasm/count_vowels.rs.wasm").instantiate(); + // var countVowels = instance.getExport("count_vowels"); + // //var memory = module.getMemory(); + // //memory.put(60000, "Hello World!"); + // //var result = countVowels.apply(Value.i32(60000)); + // var result = countVowels.apply(); + // assertEquals(3, result.asInt()); + // } + + // @Test + // 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(); + // assertEquals(42, result.asInt()); + // } + + // @Test + // public void shouldRunComplexFunction() { + // // check with: wasmtime src/test/resources/wasm/complex.c.wasm --invoke run + // var instance = Module.build("src/test/resources/wasm/complex.c.wasm").instantiate(); + // var run = instance.getExport("run"); + // var result = run.apply(); + // assertEquals(-679, result.asInt()); + // } + + // @Test + // public void shouldRunMemoryProgramInC() { + // // check with: wasmtime src/test/resources/wasm/memory.c.wasm --invoke run + // var instance = Module.build("src/test/resources/wasm/memory.c.wasm").instantiate(); + // var run = instance.getExport("run"); + // var result = run.apply(); + // assertEquals(11, result.asInt()); + // } @Test public void shouldWorkWithMemoryOps() { @@ -238,11 +236,12 @@ public void shouldRunKitchenSink() { assertEquals(6, run.apply(Value.i32(100)).asInt()); } -// @Test -// public void shouldOperateMemoryOps() { -// // check with: wasmtime src/test/resources/wasm/memories.wat.wasm --invoke run 100 -// var instance = Module.build("src/test/resources/wasm/memories.wat.wasm").instantiate(); -// var run = instance.getExport("run"); -// assertEquals(-25438, run.apply(Value.i32(100)).asInt()); -// } + // @Test + // public void shouldOperateMemoryOps() { + // // check with: wasmtime src/test/resources/wasm/memories.wat.wasm --invoke run 100 + // var instance = + // Module.build("src/test/resources/wasm/memories.wat.wasm").instantiate(); + // var run = instance.getExport("run"); + // assertEquals(-25438, run.apply(Value.i32(100)).asInt()); + // } } diff --git a/test-gen-plugin/pom.xml b/test-gen-plugin/pom.xml index b1fe7ad7a..3b7eb88cb 100644 --- a/test-gen-plugin/pom.xml +++ b/test-gen-plugin/pom.xml @@ -1,8 +1,5 @@ - - + 4.0.0 @@ -12,60 +9,58 @@ test-gen-plugin maven-plugin - - A Maven plugin to generate JUnit tests out of wast files - - - com.github.javaparser - javaparser-core - 3.24.4 - - - org.apache.commons - commons-compress - 1.24.0 - - - com.fasterxml.jackson.core - jackson-databind - 2.12.7.1 - - - org.eclipse.jgit - org.eclipse.jgit - 6.7.0.202309050840-r - - - org.apache.maven - maven-plugin-api - 3.6.3 - - - org.apache.maven.plugin-tools - maven-plugin-annotations - 3.6.0 - provided - - - org.apache.maven - maven-project - 3.0-alpha-2 - - - - - - org.apache.maven.plugins - maven-plugin-plugin - 3.9.0 - - - default-descriptor - process-classes - - - - - - \ No newline at end of file + + + com.github.javaparser + javaparser-core + 3.24.4 + + + org.apache.commons + commons-compress + 1.24.0 + + + com.fasterxml.jackson.core + jackson-databind + 2.12.7.1 + + + org.eclipse.jgit + org.eclipse.jgit + 6.7.0.202309050840-r + + + org.apache.maven + maven-plugin-api + 3.6.3 + + + org.apache.maven.plugin-tools + maven-plugin-annotations + 3.6.0 + provided + + + org.apache.maven + maven-project + 3.0-alpha-2 + + + + + + org.apache.maven.plugins + maven-plugin-plugin + 3.9.0 + + + default-descriptor + process-classes + + + + + + diff --git a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/JavaParserMavenUtils.java b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/JavaParserMavenUtils.java index 3b4461822..702e1a8c1 100644 --- a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/JavaParserMavenUtils.java +++ b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/JavaParserMavenUtils.java @@ -1,29 +1,31 @@ package com.dylibso.chicory.maven; -import org.apache.maven.plugin.logging.Log; - import java.util.function.Supplier; +import org.apache.maven.plugin.logging.Log; /** * Things to make JavaParser and Maven interact better */ public class JavaParserMavenUtils { public static void makeJavaParserLogToMavenOutput(Log log) { - com.github.javaparser.utils.Log.setAdapter(new com.github.javaparser.utils.Log.Adapter() { - @Override - public void info(Supplier message) { - log.info(message.get()); - } + com.github.javaparser.utils.Log.setAdapter( + new com.github.javaparser.utils.Log.Adapter() { + @Override + public void info(Supplier message) { + log.info(message.get()); + } - @Override - public void trace(Supplier message) { - log.debug(message.get()); - } + @Override + public void trace(Supplier message) { + log.debug(message.get()); + } - @Override - public void error(Supplier throwableSupplier, Supplier messageSupplier) { - log.error(messageSupplier.get(), throwableSupplier.get()); - } - }); + @Override + public void error( + Supplier throwableSupplier, + Supplier messageSupplier) { + log.error(messageSupplier.get(), throwableSupplier.get()); + } + }); } } 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 0832665d0..f8c6bbcd5 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 @@ -1,5 +1,9 @@ package com.dylibso.chicory.maven; +import static com.dylibso.chicory.maven.StringUtils.*; +import static com.dylibso.chicory.maven.wast.ActionType.INVOKE; +import static com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType; + import com.dylibso.chicory.maven.wast.Command; import com.dylibso.chicory.maven.wast.CommandType; import com.dylibso.chicory.maven.wast.WasmValue; @@ -13,20 +17,14 @@ import com.github.javaparser.ast.expr.Expression; import com.github.javaparser.ast.expr.NameExpr; import com.github.javaparser.ast.expr.VariableDeclarationExpr; -import com.github.javaparser.ast.type.ClassOrInterfaceType; import com.github.javaparser.utils.SourceRoot; -import org.apache.maven.plugin.logging.Log; - import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; - -import static com.dylibso.chicory.maven.StringUtils.*; -import static com.dylibso.chicory.maven.wast.ActionType.INVOKE; -import static com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType; +import org.apache.maven.plugin.logging.Log; public class JavaTestGen { @@ -85,8 +83,12 @@ public void generate(File specFile, File wasmFilesFolder) { MethodDeclaration method; int testNumber = 0; int moduleInstantiationNumber = 0; - for (var cmd: wast.getCommands()) { - var excludedMethods = excludedTests.stream().filter(t -> t.startsWith(testName)).map(t -> t.replace(testName + ".", "")).collect(Collectors.toList()); + for (var cmd : wast.getCommands()) { + var excludedMethods = + excludedTests.stream() + .filter(t -> t.startsWith(testName)) + .map(t -> t.replace(testName + ".", "")) + .collect(Collectors.toList()); switch (cmd.getType()) { case MODULE: @@ -100,12 +102,13 @@ public void generate(File specFile, File wasmFilesFolder) { method = createTestMethod(testClass, testNumber++, excludedMethods); var varName = escapedCamelCase(cmd.getAction().getField()); - var fieldExport = generateFieldExport(varName, cmd, (moduleInstantiationNumber - 1)); + var fieldExport = + generateFieldExport(varName, cmd, (moduleInstantiationNumber - 1)); if (fieldExport.isPresent()) { method.getBody().get().addStatement(fieldExport.get()); } - for (var expr: generateAssert(varName, cmd)) { + for (var expr : generateAssert(varName, cmd)) { method.getBody().get().addStatement(expr); } break; @@ -113,7 +116,7 @@ public void generate(File specFile, File wasmFilesFolder) { case ASSERT_MALFORMED: method = createTestMethod(testClass, testNumber++, excludedMethods); - for (var expr: generateAssertThrows(cmd, wasmFilesFolder)) { + for (var expr : generateAssertThrows(cmd, wasmFilesFolder)) { method.getBody().get().addStatement(expr); } break; @@ -124,7 +127,8 @@ public void generate(File specFile, File wasmFilesFolder) { dest.saveAll(); } - private MethodDeclaration createTestMethod(ClassOrInterfaceDeclaration testClass, int testNumber, List excludedTests) { + private MethodDeclaration createTestMethod( + ClassOrInterfaceDeclaration testClass, int testNumber, List excludedTests) { var methodName = "test" + testNumber; var method = testClass.addMethod("test" + testNumber, Modifier.Keyword.PUBLIC); if (excludedTests.contains(methodName)) { @@ -135,12 +139,20 @@ private MethodDeclaration createTestMethod(ClassOrInterfaceDeclaration testClass return method; } - private Optional generateFieldExport(String varName, Command cmd, int instanceNumber) { + private Optional generateFieldExport( + String varName, Command cmd, int instanceNumber) { if (cmd.getAction() != null && cmd.getAction().getField() != null) { - var declarator = new VariableDeclarator() - .setName(varName) - .setType(parseClassOrInterfaceType("ExportFunction")) - .setInitializer(new NameExpr(INSTANCE_NAME + instanceNumber + ".getExport(\"" + cmd.getAction().getField() + "\")")); + var declarator = + new VariableDeclarator() + .setName(varName) + .setType(parseClassOrInterfaceType("ExportFunction")) + .setInitializer( + new NameExpr( + INSTANCE_NAME + + instanceNumber + + ".getExport(\"" + + cmd.getAction().getField() + + "\")")); Expression varDecl = new VariableDeclarationExpr(declarator); return Optional.of(varDecl); } else { @@ -149,7 +161,8 @@ private Optional generateFieldExport(String varName, Command cmd, in } private List generateAssert(String varName, Command cmd) { - assert(cmd.getType() == CommandType.ASSERT_RETURN || cmd.getType() == CommandType.ASSERT_TRAP); + assert (cmd.getType() == CommandType.ASSERT_RETURN + || cmd.getType() == CommandType.ASSERT_TRAP); var returnVar = "null"; var typeConversion = ""; @@ -167,17 +180,36 @@ private List generateAssert(String varName, Command cmd) { String invocationMethod; if (cmd.getAction().getType() == INVOKE) { - var args = Arrays.stream(cmd.getAction().getArgs()).map(WasmValue::toWasmValue).collect(Collectors.joining(", ")); + 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()); + throw new IllegalArgumentException( + "Unhandled action type " + cmd.getAction().getType()); } switch (cmd.getType()) { case ASSERT_RETURN: - return List.of(new NameExpr("assertEquals(" + returnVar + ", "+ varName + invocationMethod + typeConversion + deltaParam + ")")); + return List.of( + new NameExpr( + "assertEquals(" + + returnVar + + ", " + + varName + + invocationMethod + + typeConversion + + deltaParam + + ")")); case ASSERT_TRAP: - var assertDecl = new NameExpr("var exception = assertThrows(WASMRuntimeException.class, () -> "+ varName + invocationMethod + typeConversion + ")"); + var assertDecl = + new NameExpr( + "var exception = assertThrows(WASMRuntimeException.class, () -> " + + varName + + invocationMethod + + typeConversion + + ")"); if (cmd.getText() != null) { return List.of(assertDecl, exceptionMessageMatch(cmd.getText())); } else { @@ -189,21 +221,27 @@ private List generateAssert(String varName, Command cmd) { } private Expression generateModuleInstantiation(Command cmd, File folder) { - assert(cmd.getType() == CommandType.MODULE); + assert (cmd.getType() == CommandType.MODULE); - var relativeFile = folder.toPath().resolve(cmd.getFilename()).toFile().getAbsolutePath() - .replaceFirst(baseDir.getAbsolutePath() + File.pathSeparator, "") - .replace("\\", "\\\\"); // Win compat + var relativeFile = + folder.toPath() + .resolve(cmd.getFilename()) + .toFile() + .getAbsolutePath() + .replaceFirst(baseDir.getAbsolutePath() + File.pathSeparator, "") + .replace("\\", "\\\\"); // Win compat var additionalParam = ""; if (cmd.getModuleType() != null) { additionalParam = ", ModuleType." + cmd.getModuleType().toUpperCase(); } - return new NameExpr("Module.build(\"" + relativeFile + "\"" + additionalParam + ").instantiate()"); + return new NameExpr( + "Module.build(\"" + relativeFile + "\"" + additionalParam + ").instantiate()"); } private List generateAssertThrows(Command cmd, File wasmFilesFolder) { - assert(cmd.getType() == CommandType.ASSERT_INVALID || cmd.getType() == CommandType.ASSERT_MALFORMED); + assert (cmd.getType() == CommandType.ASSERT_INVALID + || cmd.getType() == CommandType.ASSERT_MALFORMED); var exceptionType = ""; if (cmd.getType() == CommandType.ASSERT_INVALID) { @@ -214,7 +252,14 @@ private List generateAssertThrows(Command cmd, File wasmFilesFolder) var assignementStmt = (cmd.getText() != null) ? "var exception = " : ""; - var assertThrows = new NameExpr(assignementStmt + "assertThrows(" + exceptionType + ".class, () -> " + generateModuleInstantiation(cmd, wasmFilesFolder) + ")"); + var assertThrows = + new NameExpr( + assignementStmt + + "assertThrows(" + + exceptionType + + ".class, () -> " + + generateModuleInstantiation(cmd, wasmFilesFolder) + + ")"); if (cmd.getText() != null) { return List.of(assertThrows, exceptionMessageMatch(cmd.getText())); @@ -224,6 +269,11 @@ private List generateAssertThrows(Command cmd, File wasmFilesFolder) } private Expression exceptionMessageMatch(String text) { - return new NameExpr("assertTrue(\"'\" + exception.getMessage() + \"' doesn't contains: '" + text + "'\", exception.getMessage().contains(\"" + text + "\"))"); + return new NameExpr( + "assertTrue(\"'\" + exception.getMessage() + \"' doesn't contains: '" + + text + + "'\", exception.getMessage().contains(\"" + + text + + "\"))"); } } diff --git a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/StringUtils.java b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/StringUtils.java index bcfb4b437..0eb5df0ea 100644 --- a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/StringUtils.java +++ b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/StringUtils.java @@ -31,5 +31,4 @@ public static String escapedCamelCase(String in) { return sb.toString(); } - } diff --git a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/TarExtractor.java b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/TarExtractor.java index 236d61987..77a8865c6 100644 --- a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/TarExtractor.java +++ b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/TarExtractor.java @@ -1,14 +1,13 @@ package com.dylibso.chicory.maven; -import org.apache.commons.compress.archivers.ArchiveEntry; -import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; -import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; - import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; +import org.apache.commons.compress.archivers.ArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; +import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; public class TarExtractor { @@ -24,7 +23,8 @@ public TarExtractor(InputStream in, Path destination) throws IOException { public void untar() throws IOException { try (BufferedInputStream inputStream = new BufferedInputStream(this.tarStream); - TarArchiveInputStream tar = new TarArchiveInputStream(new GzipCompressorInputStream(inputStream))) { + TarArchiveInputStream tar = + new TarArchiveInputStream(new GzipCompressorInputStream(inputStream))) { ArchiveEntry entry; while ((entry = tar.getNextEntry()) != null) { Path extractTo = this.destination.resolve(entry.getName()); diff --git a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/TestGenMojo.java b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/TestGenMojo.java index ee6db761a..72b3adbe3 100644 --- a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/TestGenMojo.java +++ b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/TestGenMojo.java @@ -1,5 +1,10 @@ package com.dylibso.chicory.maven; +import static com.dylibso.chicory.maven.Constants.SPEC_JSON; + +import java.io.File; +import java.util.List; +import java.util.stream.Collectors; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.logging.Log; @@ -9,12 +14,6 @@ import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; -import java.io.File; -import java.util.List; -import java.util.stream.Collectors; - -import static com.dylibso.chicory.maven.Constants.SPEC_JSON; - /** * This plugin should generate the testsuite out of wast files */ @@ -49,7 +48,9 @@ public class TestGenMojo extends AbstractMojo { /** * Location for the junit generated sources. */ - @Parameter(required = true, defaultValue = "${project.build.directory}/generated-test-sources/test-gen") + @Parameter( + required = true, + defaultValue = "${project.build.directory}/generated-test-sources/test-gen") private File sourceDestinationFolder; @Parameter(required = true, defaultValue = "${project.build.directory}/compiled-wast") @@ -61,8 +62,11 @@ public class TestGenMojo extends AbstractMojo { @Parameter(required = true, defaultValue = "1.0.33") private String wabtVersion; - @Parameter(required = true, defaultValue = "https://github.com/WebAssembly/wabt/releases/download/") + @Parameter( + required = true, + defaultValue = "https://github.com/WebAssembly/wabt/releases/download/") private String wabtReleasesURL; + @Parameter(required = true, defaultValue = "${project.build.directory}/wabt") private File wabtDownloadTargetFolder; @@ -85,7 +89,9 @@ public class TestGenMojo extends AbstractMojo { private MavenProject project; private List clean(List in) { - return in.stream().map(t -> t.replace("\n", "").replace("\r", "").trim()).collect(Collectors.toList()); + return in.stream() + .map(t -> t.replace("\n", "").replace("\r", "").trim()) + .collect(Collectors.toList()); } @Override @@ -93,8 +99,17 @@ public void execute() throws MojoExecutionException { JavaParserMavenUtils.makeJavaParserLogToMavenOutput(getLog()); // Instantiate the utilities var testSuiteDownloader = new TestSuiteDownloader(log); - var wast2Json = new Wast2JsonWrapper(log, wabtDownloadTargetFolder, wabtReleasesURL, wabtVersion, osName, compiledWastTargetFolder); - var testGen = new JavaTestGen(log, project.getBasedir(), sourceDestinationFolder, clean(excludedTests)); + var wast2Json = + new Wast2JsonWrapper( + log, + wabtDownloadTargetFolder, + wabtReleasesURL, + wabtVersion, + osName, + compiledWastTargetFolder); + var testGen = + new JavaTestGen( + log, project.getBasedir(), sourceDestinationFolder, clean(excludedTests)); // Create destination folders compiledWastTargetFolder.mkdirs(); @@ -106,14 +121,17 @@ public void execute() throws MojoExecutionException { wast2Json.fetch(); // generate the tests - for (var spec: clean(wastToProcess)) { + for (var spec : clean(wastToProcess)) { log.debug("TestGen processing " + spec); var wastFile = testsuiteFolder.toPath().resolve(spec).toFile(); if (!wastFile.exists()) { - throw new IllegalArgumentException("Wast file " + wastFile.getAbsolutePath() + " not found"); + throw new IllegalArgumentException( + "Wast file " + wastFile.getAbsolutePath() + " not found"); } - var wasmFilesFolder = wast2Json.execute(testsuiteFolder.toPath().resolve(spec).toFile()); - testGen.generate(wasmFilesFolder.toPath().resolve(SPEC_JSON).toFile(), wasmFilesFolder); + var wasmFilesFolder = + wast2Json.execute(testsuiteFolder.toPath().resolve(spec).toFile()); + testGen.generate( + wasmFilesFolder.toPath().resolve(SPEC_JSON).toFile(), wasmFilesFolder); } } catch (Exception e) { throw new RuntimeException(e); @@ -122,5 +140,4 @@ public void execute() throws MojoExecutionException { // Add the generated tests to the source root project.addTestCompileSourceRoot(sourceDestinationFolder.getAbsolutePath()); } - } diff --git a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/TestSuiteDownloader.java b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/TestSuiteDownloader.java index 50f200b47..9a9430e1e 100644 --- a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/TestSuiteDownloader.java +++ b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/TestSuiteDownloader.java @@ -1,14 +1,13 @@ package com.dylibso.chicory.maven; -import org.apache.maven.plugin.logging.Log; -import org.eclipse.jgit.api.Git; +import static java.util.Collections.singleton; import java.io.File; import java.nio.file.Files; import java.nio.file.Path; import java.util.Comparator; - -import static java.util.Collections.singleton; +import org.apache.maven.plugin.logging.Log; +import org.eclipse.jgit.api.Git; public class TestSuiteDownloader { @@ -18,8 +17,10 @@ public class TestSuiteDownloader { this.log = log; } - public void downloadTestsuite(String testSuiteRepo, String testSuiteRepoRef, File testSuiteFolder) throws Exception { - if (testSuiteFolder.exists() && testSuiteFolder.list((dir, name) -> name.endsWith(".wast")).length == 0) { + public void downloadTestsuite( + String testSuiteRepo, String testSuiteRepoRef, File testSuiteFolder) throws Exception { + if (testSuiteFolder.exists() + && testSuiteFolder.list((dir, name) -> name.endsWith(".wast")).length == 0) { log.warn("Testsuite folder exists but looks corrupted, replacing."); Files.walk(testSuiteFolder.toPath()) .sorted(Comparator.reverseOrder()) @@ -39,5 +40,4 @@ public void downloadTestsuite(String testSuiteRepo, String testSuiteRepoRef, Fil .call(); } } - } diff --git a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/Wast2JsonWrapper.java b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/Wast2JsonWrapper.java index 019dc8599..98e259e07 100644 --- a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/Wast2JsonWrapper.java +++ b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/Wast2JsonWrapper.java @@ -1,6 +1,6 @@ package com.dylibso.chicory.maven; -import org.apache.maven.plugin.logging.Log; +import static com.dylibso.chicory.maven.Constants.SPEC_JSON; import java.io.File; import java.io.FileInputStream; @@ -15,8 +15,7 @@ import java.util.Locale; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; - -import static com.dylibso.chicory.maven.Constants.SPEC_JSON; +import org.apache.maven.plugin.logging.Log; public class Wast2JsonWrapper { @@ -30,7 +29,13 @@ public class Wast2JsonWrapper { private String wast2JsonCmd = WAST2JSON; - public Wast2JsonWrapper(Log log, File wabtDownloadTargetFolder, String wabtReleasesURL, String wabtVersion, String osName, File compiledWastTargetFolder) { + public Wast2JsonWrapper( + Log log, + File wabtDownloadTargetFolder, + String wabtReleasesURL, + String wabtVersion, + String osName, + File compiledWastTargetFolder) { this.log = log; this.wabtDownloadTargetFolder = wabtDownloadTargetFolder; this.wabtReleasesURL = wabtReleasesURL; @@ -54,11 +59,8 @@ private File executeWast2Json(File wastFile) { targetFolder.mkdirs(); - var command = List.of( - wast2JsonCmd, - wastFile.getAbsolutePath(), - "-o", - destFile.getAbsolutePath()); + var command = + List.of(wast2JsonCmd, wastFile.getAbsolutePath(), "-o", destFile.getAbsolutePath()); log.info("Going to execute command: " + command.stream().collect(Collectors.joining(" "))); ProcessBuilder pb = new ProcessBuilder(command); @@ -75,7 +77,7 @@ private File executeWast2Json(File wastFile) { } if (ps.exitValue() != 0) { - System.err.println("wast2json exiting with:"+ ps.exitValue()); + System.err.println("wast2json exiting with:" + ps.exitValue()); System.err.println(ps.getErrorStream().toString()); throw new RuntimeException("Failed to execute wast2json program."); } @@ -85,10 +87,11 @@ private File executeWast2Json(File wastFile) { private void downloadAndExtract(URL url) { wabtDownloadTargetFolder.mkdirs(); - final File finalDestination = new File(wabtDownloadTargetFolder, new File(url.getFile()).getName()); + final File finalDestination = + new File(wabtDownloadTargetFolder, new File(url.getFile()).getName()); try (ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream()); - FileOutputStream fileOutputStream = new FileOutputStream(finalDestination)) { + FileOutputStream fileOutputStream = new FileOutputStream(finalDestination)) { fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE); } catch (IOException e) { throw new IllegalArgumentException("Error downloading : " + url, e); @@ -115,10 +118,17 @@ private String getWast2Json() { } // Downloading locally WABT - var binary = wabtDownloadTargetFolder.toPath().resolve("wabt-" + wabtVersion).resolve("bin").resolve(WAST2JSON); + var binary = + wabtDownloadTargetFolder + .toPath() + .resolve("wabt-" + wabtVersion) + .resolve("bin") + .resolve(WAST2JSON); if (binary.toFile().exists()) { - log.warn("cached `wast2json` exists trying to use it, please run `mvn clean` if this doesn't succeed"); + log.warn( + "cached `wast2json` exists trying to use it, please run `mvn clean` if this" + + " doesn't succeed"); return binary.toFile().getAbsolutePath(); } @@ -132,7 +142,13 @@ private String getWast2Json() { throw new RuntimeException(e); } - try (FileInputStream fis = new FileInputStream(wabtDownloadTargetFolder.toPath().resolve(fileName).toFile().getAbsolutePath())) { + try (FileInputStream fis = + new FileInputStream( + wabtDownloadTargetFolder + .toPath() + .resolve(fileName) + .toFile() + .getAbsolutePath())) { new TarExtractor(fis, wabtDownloadTargetFolder.toPath()).untar(); } catch (FileNotFoundException e) { throw new RuntimeException(e); diff --git a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/Action.java b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/Action.java index a83dae74d..668a76234 100644 --- a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/Action.java +++ b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/Action.java @@ -1,12 +1,8 @@ package com.dylibso.chicory.maven.wast; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonIgnoreType; import com.fasterxml.jackson.annotation.JsonProperty; - import java.util.Arrays; -import java.util.Optional; -import java.util.Set; @JsonIgnoreProperties public class Action { @@ -19,8 +15,7 @@ public class Action { @JsonProperty("args") WasmValue[] args; - public Action() { - } + public Action() {} public ActionType getType() { @@ -37,10 +32,14 @@ public WasmValue[] getArgs() { @Override public String toString() { - return "Action{" + - "type=" + type + - ", field='" + field + '\'' + - ", args=" + Arrays.toString(args) + - '}'; + return "Action{" + + "type=" + + type + + ", field='" + + field + + '\'' + + ", args=" + + Arrays.toString(args) + + '}'; } } diff --git a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/ActionType.java b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/ActionType.java index 8539edf01..526c8103b 100644 --- a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/ActionType.java +++ b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/ActionType.java @@ -8,6 +8,7 @@ public enum ActionType { INVOKE("invoke"); private String value; + ActionType(String value) { this.value = value; } diff --git a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/Command.java b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/Command.java index 5c597025c..e28693fe7 100644 --- a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/Command.java +++ b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/Command.java @@ -2,7 +2,6 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; - import java.util.Arrays; @JsonIgnoreProperties @@ -18,15 +17,25 @@ public class Command { @Override public String toString() { - return "Command{" + - "type=" + type + - ", line=" + line + - ", filename='" + filename + '\'' + - ", action=" + action + - ", expected=" + Arrays.toString(expected) + - ", text='" + text + '\'' + - ", moduleType='" + moduleType + '\'' + - '}'; + return "Command{" + + "type=" + + type + + ", line=" + + line + + ", filename='" + + filename + + '\'' + + ", action=" + + action + + ", expected=" + + Arrays.toString(expected) + + ", text='" + + text + + '\'' + + ", moduleType='" + + moduleType + + '\'' + + '}'; } @JsonProperty("action") diff --git a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/CommandType.java b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/CommandType.java index 404a39e5a..a30f6e266 100644 --- a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/CommandType.java +++ b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/CommandType.java @@ -16,6 +16,7 @@ public enum CommandType { ASSERT_MALFORMED("assert_malformed"); private String value; + CommandType(String value) { this.value = value; } diff --git a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/WasmValue.java b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/WasmValue.java index 41240cbe4..64e826aa8 100644 --- a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/WasmValue.java +++ b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/WasmValue.java @@ -20,10 +20,14 @@ public String getValue() { public String toJavaValue() { switch (type) { - case I32: return "Integer.parseUnsignedInt(\"" + value + "\")"; - case I64: return "Long.parseUnsignedLong(\"" + value + "\")"; - case F32: return "Float.parseFloat(\"" + value + "\")"; - case F64: return "Double.parseDouble(\"" + value + "\")"; + case I32: + return "Integer.parseUnsignedInt(\"" + value + "\")"; + case I64: + return "Long.parseUnsignedLong(\"" + value + "\")"; + case F32: + return "Float.parseFloat(\"" + value + "\")"; + case F64: + return "Double.parseDouble(\"" + value + "\")"; default: throw new IllegalArgumentException("Type not recognized " + type); } @@ -31,10 +35,14 @@ public String toJavaValue() { public String toWasmValue() { switch (type) { - case I32: return "Value.i32(Integer.parseUnsignedInt(\"" + value + "\"))"; - case I64: return "Value.i64(Long.parseUnsignedLong(\"" + value + "\"))"; - case F32: return "Value.fromFloat(Float.parseFloat(\"" + value + "\"))"; - case F64: return "Value.fromDouble(Double.parseDouble(\"" + value + "\"))"; + case I32: + return "Value.i32(Integer.parseUnsignedInt(\"" + value + "\"))"; + case I64: + return "Value.i64(Long.parseUnsignedLong(\"" + value + "\"))"; + case F32: + return "Value.fromFloat(Float.parseFloat(\"" + value + "\"))"; + case F64: + return "Value.fromDouble(Double.parseDouble(\"" + value + "\"))"; default: throw new IllegalArgumentException("Type not recognized " + type); } diff --git a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/WasmValueType.java b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/WasmValueType.java index 636c475e0..a57a9d395 100644 --- a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/WasmValueType.java +++ b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/WasmValueType.java @@ -14,6 +14,7 @@ public enum WasmValueType { F64("f64"); private String value; + WasmValueType(String value) { this.value = value; } diff --git a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/Wast.java b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/Wast.java index e92b6a754..0dc844d3b 100644 --- a/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/Wast.java +++ b/test-gen-plugin/src/main/java/com/dylibso/chicory/maven/wast/Wast.java @@ -1,9 +1,7 @@ package com.dylibso.chicory.maven.wast; import com.fasterxml.jackson.annotation.JsonProperty; - import java.io.File; -import java.util.Set; public class Wast { diff --git a/wasm/README.md b/wasm/README.md index b0d4e373f..2972b6d1c 100644 --- a/wasm/README.md +++ b/wasm/README.md @@ -28,7 +28,7 @@ var parser = new Parser("/tmp/code.wasm"); parser.includeSection(SectionId.CUSTOM); // parser.includeSection(SectionId.CODE); // call for each section you want -// implement the listener +// implement the listener parser.setListener(section -> { if (section.getSectionId() == SectionId.CUSTOM) { var customSection = (CustomSection) section; @@ -42,4 +42,3 @@ parser.setListener(section -> { // call parse() instead of parseModule() parser.parse(); ``` - diff --git a/wasm/pom.xml b/wasm/pom.xml index 2ec98b2fa..ffcdfc6ad 100644 --- a/wasm/pom.xml +++ b/wasm/pom.xml @@ -1,7 +1,5 @@ - + 4.0.0 diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/ControlFlow.java b/wasm/src/main/java/com/dylibso/chicory/wasm/ControlFlow.java index fec9efcff..5da27f689 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/ControlFlow.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/ControlFlow.java @@ -2,7 +2,6 @@ import com.dylibso.chicory.wasm.types.Instruction; import com.dylibso.chicory.wasm.types.OpCode; - import java.util.List; import java.util.function.Function; @@ -52,67 +51,104 @@ public class ControlFlow { public static void labelBranches(List instructions) { Function clamp = (i) -> Math.min(i, instructions.size()); - for (int i = 0; i < instructions.size(); i++) { + for (int i = 0; i < instructions.size(); i++) { var instr = instructions.get(i); switch (instr.getOpcode()) { - case IF: { - instr.setLabelTrue(clamp.apply(i + 1)); - // find the matching ELSE (which is optional) - var next = findNext(instructions, OpCode.ELSE, instr.getDepth(), clamp.apply(i + 1)); - if (next != null) { - // point to instruction after the ELSE - instr.setLabelFalse(clamp.apply(next + 1)); - } else { - var end = findNext(instructions, OpCode.END, instr.getDepth(), clamp.apply(i + 1)); - instr.setLabelFalse(clamp.apply(end + 1)); + case IF: + { + instr.setLabelTrue(clamp.apply(i + 1)); + // find the matching ELSE (which is optional) + var next = + findNext( + instructions, + OpCode.ELSE, + instr.getDepth(), + clamp.apply(i + 1)); + if (next != null) { + // point to instruction after the ELSE + instr.setLabelFalse(clamp.apply(next + 1)); + } else { + var end = + findNext( + instructions, + OpCode.END, + instr.getDepth(), + clamp.apply(i + 1)); + instr.setLabelFalse(clamp.apply(end + 1)); + } + break; } - break; - } - case ELSE: { - var end = findNext(instructions, OpCode.END, instr.getDepth(), clamp.apply(i + 1)); - instr.setLabelTrue(clamp.apply(end + 1)); - break; - } - case BR: { - var d = (int) instr.getOperands()[0]; - var end = findNext(instructions, OpCode.END, instr.getDepth() - d, clamp.apply(i + 1)); - var endI = instructions.get(end); - if (endI.getScope() == OpCode.LOOP) { - var loop = findLast(instructions, OpCode.LOOP, instr.getDepth() - d, i); - instr.setLabelTrue(loop + 1); - } else { + case ELSE: + { + var end = + findNext( + instructions, + OpCode.END, + instr.getDepth(), + clamp.apply(i + 1)); instr.setLabelTrue(clamp.apply(end + 1)); + break; } - break; - } - case BR_IF: { - instr.setLabelFalse(clamp.apply(i + 1)); - var d = (int) instr.getOperands()[0]; - var end = findNext(instructions, OpCode.END, instr.getDepth() - d, clamp.apply(i + 1)); - var endI = instructions.get(end); - if (endI.getScope() == OpCode.LOOP) { - var loop = findLast(instructions, OpCode.LOOP, instr.getDepth() - d, i); - instr.setLabelTrue(loop + 1); - } else { - instr.setLabelTrue(clamp.apply(end + 1)); + case BR: + { + var d = (int) instr.getOperands()[0]; + var end = + findNext( + instructions, + OpCode.END, + instr.getDepth() - d, + clamp.apply(i + 1)); + var endI = instructions.get(end); + if (endI.getScope() == OpCode.LOOP) { + var loop = findLast(instructions, OpCode.LOOP, instr.getDepth() - d, i); + instr.setLabelTrue(loop + 1); + } else { + instr.setLabelTrue(clamp.apply(end + 1)); + } + break; } - break; - } - case BR_TABLE: { - instr.setLabelTable(new int[instr.getOperands().length]); - for (var idx = 0; idx < instr.getLabelTable().length; idx++) { - var d = (int) instr.getOperands()[idx]; - var end = findNext(instructions, OpCode.END, instr.getDepth() - d, clamp.apply(i + 1)); + case BR_IF: + { + instr.setLabelFalse(clamp.apply(i + 1)); + var d = (int) instr.getOperands()[0]; + var end = + findNext( + instructions, + OpCode.END, + instr.getDepth() - d, + clamp.apply(i + 1)); var endI = instructions.get(end); if (endI.getScope() == OpCode.LOOP) { var loop = findLast(instructions, OpCode.LOOP, instr.getDepth() - d, i); - instr.getLabelTable()[idx] = loop + 1; + instr.setLabelTrue(loop + 1); } else { - instr.getLabelTable()[idx] = clamp.apply(end + 1); + instr.setLabelTrue(clamp.apply(end + 1)); + } + break; + } + case BR_TABLE: + { + instr.setLabelTable(new int[instr.getOperands().length]); + for (var idx = 0; idx < instr.getLabelTable().length; idx++) { + var d = (int) instr.getOperands()[idx]; + var end = + findNext( + instructions, + OpCode.END, + instr.getDepth() - d, + clamp.apply(i + 1)); + var endI = instructions.get(end); + if (endI.getScope() == OpCode.LOOP) { + var loop = + findLast( + instructions, OpCode.LOOP, instr.getDepth() - d, i); + instr.getLabelTable()[idx] = loop + 1; + } else { + instr.getLabelTable()[idx] = clamp.apply(end + 1); + } } + break; } - break; - } } } } @@ -146,6 +182,4 @@ static Integer findLast(List instructions, OpCode opcode, int depth } return null; } - - } diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/Encoding.java b/wasm/src/main/java/com/dylibso/chicory/wasm/Encoding.java index d6c2dc12b..a0b75248d 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/Encoding.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/Encoding.java @@ -54,7 +54,6 @@ public static long readSigned32Leb128(ByteBuffer byteBuffer) { return result; } - public static long readSigned64Leb128(ByteBuffer byteBuffer) { long result = 0; int shift = 0; @@ -91,7 +90,7 @@ public static int computeLeb128Size(int value) { public static float longToFloat(long x) { ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES).order(ByteOrder.LITTLE_ENDIAN); - buffer.putInt((int)x); + buffer.putInt((int) x); buffer.rewind(); return buffer.getFloat(); } diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/Module.java b/wasm/src/main/java/com/dylibso/chicory/wasm/Module.java index 6b21a60dc..070202965 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/Module.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/Module.java @@ -1,7 +1,6 @@ package com.dylibso.chicory.wasm; import com.dylibso.chicory.wasm.types.*; - import java.util.ArrayList; import java.util.List; diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/ModuleBuilder.java b/wasm/src/main/java/com/dylibso/chicory/wasm/ModuleBuilder.java index a58e68085..5c0ede0a5 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/ModuleBuilder.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/ModuleBuilder.java @@ -9,7 +9,9 @@ public ModuleBuilder() { this.module = new Module(); } - public Module getModule() { return module; } + public Module getModule() { + return module; + } @Override public void onSection(Section s) { diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/Parser.java b/wasm/src/main/java/com/dylibso/chicory/wasm/Parser.java index fc0e90b07..e048abbf4 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/Parser.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/Parser.java @@ -2,7 +2,6 @@ import com.dylibso.chicory.wasm.exceptions.MalformedException; import com.dylibso.chicory.wasm.types.*; - import java.io.FileInputStream; import java.io.IOException; import java.nio.ByteBuffer; @@ -53,7 +52,9 @@ private ByteBuffer readByteBuffer() { public Module parseModule() { if (this.listener != null) { - throw new IllegalArgumentException("This method overrides your custom listener. You probably want to use Module#parse() instead"); + throw new IllegalArgumentException( + "This method overrides your custom listener. You probably want to use" + + " Module#parse() instead"); } var builder = new ModuleBuilder(); setListener(builder); @@ -63,18 +64,24 @@ public Module parseModule() { public void parse() { if (this.listener == null) { - throw new IllegalArgumentException("Missing ParserListener. Use Module#setListener to set a listener;"); + throw new IllegalArgumentException( + "Missing ParserListener. Use Module#setListener to set a listener;"); } var buffer = readByteBuffer(); int magicNumber = buffer.getInt(); if (magicNumber != MAGIC_BYTES) { - throw new MalformedException("unexpected token: magic number mismatch, found: " + magicNumber + " expected: " + MAGIC_BYTES); + throw new MalformedException( + "unexpected token: magic number mismatch, found: " + + magicNumber + + " expected: " + + MAGIC_BYTES); } int version = buffer.getInt(); if (version != 1) { - throw new MalformedException("unexpected token: unsupported version, found: " + version + " expected: " + 1); + throw new MalformedException( + "unexpected token: unsupported version, found: " + version + " expected: " + 1); } while (buffer.hasRemaining()) { @@ -84,71 +91,85 @@ public void parse() { if (shouldParseSection(sectionId)) { // Process different section types based on the sectionId switch (sectionId) { - case SectionId.CUSTOM: { - var customSection = parseCustomSection(buffer, sectionId, sectionSize); - listener.onSection(customSection); - break; - } - case SectionId.TYPE: { - var typeSection = parseTypeSection(buffer, sectionId, sectionSize); - listener.onSection(typeSection); - break; - } - case SectionId.IMPORT: { - var importSection = parseImportSection(buffer, sectionId, sectionSize); - listener.onSection(importSection); - break; - } - case SectionId.FUNCTION: { - var funcSection = parseFunctionSection(buffer, sectionId, sectionSize); - listener.onSection(funcSection); - break; - } - case SectionId.TABLE: { - var tableSection = parseTableSection(buffer, sectionId, sectionSize); - listener.onSection(tableSection); - break; - } - case SectionId.MEMORY: { - var memorySection = parseMemorySection(buffer, sectionId, sectionSize); - listener.onSection(memorySection); - break; - } - case SectionId.GLOBAL: { - var globalSection = parseGlobalSection(buffer, sectionId, sectionSize); - listener.onSection(globalSection); - break; - } - case SectionId.EXPORT: { - var exportSection = parseExportSection(buffer, sectionId, sectionSize); - listener.onSection(exportSection); - break; - } - case SectionId.START: { - var startSection = parseStartSection(buffer, sectionId, sectionSize); - listener.onSection(startSection); - break; - } - case SectionId.ELEMENT: { - var elementSection = parseElementSection(buffer, sectionId, sectionSize); - listener.onSection(elementSection); - break; - } - case SectionId.CODE: { - var codeSection = parseCodeSection(buffer, sectionId, sectionSize); - listener.onSection(codeSection); - break; - } - case SectionId.DATA: { - var dataSection = parseDataSection(buffer, sectionId, sectionSize); - listener.onSection(dataSection); - break; - } - default: { - System.out.println("Skipping Unknown Section with ID: " + sectionId); - buffer.position((int) (buffer.position() + sectionSize)); - break; - } + case SectionId.CUSTOM: + { + var customSection = parseCustomSection(buffer, sectionId, sectionSize); + listener.onSection(customSection); + break; + } + case SectionId.TYPE: + { + var typeSection = parseTypeSection(buffer, sectionId, sectionSize); + listener.onSection(typeSection); + break; + } + case SectionId.IMPORT: + { + var importSection = parseImportSection(buffer, sectionId, sectionSize); + listener.onSection(importSection); + break; + } + case SectionId.FUNCTION: + { + var funcSection = parseFunctionSection(buffer, sectionId, sectionSize); + listener.onSection(funcSection); + break; + } + case SectionId.TABLE: + { + var tableSection = parseTableSection(buffer, sectionId, sectionSize); + listener.onSection(tableSection); + break; + } + case SectionId.MEMORY: + { + var memorySection = parseMemorySection(buffer, sectionId, sectionSize); + listener.onSection(memorySection); + break; + } + case SectionId.GLOBAL: + { + var globalSection = parseGlobalSection(buffer, sectionId, sectionSize); + listener.onSection(globalSection); + break; + } + case SectionId.EXPORT: + { + var exportSection = parseExportSection(buffer, sectionId, sectionSize); + listener.onSection(exportSection); + break; + } + case SectionId.START: + { + var startSection = parseStartSection(buffer, sectionId, sectionSize); + listener.onSection(startSection); + break; + } + case SectionId.ELEMENT: + { + var elementSection = + parseElementSection(buffer, sectionId, sectionSize); + listener.onSection(elementSection); + break; + } + case SectionId.CODE: + { + var codeSection = parseCodeSection(buffer, sectionId, sectionSize); + listener.onSection(codeSection); + break; + } + case SectionId.DATA: + { + var dataSection = parseDataSection(buffer, sectionId, sectionSize); + listener.onSection(dataSection); + break; + } + default: + { + System.out.println("Skipping Unknown Section with ID: " + sectionId); + buffer.position((int) (buffer.position() + sectionSize)); + break; + } } } else { System.out.println("Skipping Section with ID due to configuration: " + sectionId); @@ -163,7 +184,8 @@ private boolean shouldParseSection(int sectionId) { return false; } - private static CustomSection parseCustomSection(ByteBuffer buffer, long sectionId, long sectionSize) { + private static CustomSection parseCustomSection( + ByteBuffer buffer, long sectionId, long sectionSize) { var customSection = new CustomSection(sectionId, sectionSize); var name = readName(buffer); customSection.setName(name); @@ -174,16 +196,20 @@ private static CustomSection parseCustomSection(ByteBuffer buffer, long sectionI return customSection; } - private static TypeSection parseTypeSection(ByteBuffer buffer, long sectionId, long sectionSize) { + private static TypeSection parseTypeSection( + ByteBuffer buffer, long sectionId, long sectionSize) { var typeCount = readVarUInt32(buffer); - var types = new FunctionType[(int)typeCount]; + var types = new FunctionType[(int) typeCount]; // Parse individual types in the type section for (int i = 0; i < typeCount; i++) { var form = readVarUInt32(buffer); if (form != 0x60) { - throw new RuntimeException("We don't support non func types. Form " + String.format("0x%02X", form) + " was given but we expected 0x60"); + throw new RuntimeException( + "We don't support non func types. Form " + + String.format("0x%02X", form) + + " was given but we expected 0x60"); } // Parse function types (form = 0x60) @@ -209,9 +235,10 @@ private static TypeSection parseTypeSection(ByteBuffer buffer, long sectionId, l return new TypeSection(sectionId, sectionSize, types); } - private static ImportSection parseImportSection(ByteBuffer buffer, long sectionId, long sectionSize) { + private static ImportSection parseImportSection( + ByteBuffer buffer, long sectionId, long sectionSize) { var importCount = readVarUInt32(buffer); - var imports = new Import[(int)importCount]; + var imports = new Import[(int) importCount]; // Parse individual imports in the import section for (int i = 0; i < importCount; i++) { @@ -226,9 +253,10 @@ private static ImportSection parseImportSection(ByteBuffer buffer, long sectionI return new ImportSection(sectionId, sectionSize, imports); } - private static FunctionSection parseFunctionSection(ByteBuffer buffer, long sectionId, long sectionSize) { + private static FunctionSection parseFunctionSection( + ByteBuffer buffer, long sectionId, long sectionSize) { var functionCount = readVarUInt32(buffer); - var typeIndices = new int[(int)functionCount]; + var typeIndices = new int[(int) functionCount]; // Parse individual functions in the function section for (int i = 0; i < functionCount; i++) { @@ -239,9 +267,10 @@ private static FunctionSection parseFunctionSection(ByteBuffer buffer, long sect return new FunctionSection(sectionId, sectionSize, typeIndices); } - private static TableSection parseTableSection(ByteBuffer buffer, long sectionId, long sectionSize) { + private static TableSection parseTableSection( + ByteBuffer buffer, long sectionId, long sectionSize) { var tableCount = readVarUInt32(buffer); - var tables = new Table[(int)tableCount]; + var tables = new Table[(int) tableCount]; // Parse individual functions in the function section for (int i = 0; i < tableCount; i++) { @@ -259,9 +288,10 @@ private static TableSection parseTableSection(ByteBuffer buffer, long sectionId, return new TableSection(sectionId, sectionSize, tables); } - private static MemorySection parseMemorySection(ByteBuffer buffer, long sectionId, long sectionSize) { + private static MemorySection parseMemorySection( + ByteBuffer buffer, long sectionId, long sectionSize) { var memoryCount = readVarUInt32(buffer); - var memories = new Memory[(int)memoryCount]; + var memories = new Memory[(int) memoryCount]; // Parse individual functions in the function section for (int i = 0; i < memoryCount; i++) { @@ -278,9 +308,10 @@ private static MemorySection parseMemorySection(ByteBuffer buffer, long sectionI return new MemorySection(sectionId, sectionSize, memories); } - private static GlobalSection parseGlobalSection(ByteBuffer buffer, long sectionId, long sectionSize) { + private static GlobalSection parseGlobalSection( + ByteBuffer buffer, long sectionId, long sectionSize) { var globalCount = readVarUInt32(buffer); - var globals = new Global[(int)globalCount]; + var globals = new Global[(int) globalCount]; // Parse individual globals for (int i = 0; i < globalCount; i++) { @@ -293,9 +324,10 @@ private static GlobalSection parseGlobalSection(ByteBuffer buffer, long sectionI return new GlobalSection(sectionId, sectionSize, globals); } - private static ExportSection parseExportSection(ByteBuffer buffer, long sectionId, long sectionSize) { + private static ExportSection parseExportSection( + ByteBuffer buffer, long sectionId, long sectionSize) { var exportCount = readVarUInt32(buffer); - var exports = new Export[(int)exportCount]; + var exports = new Export[(int) exportCount]; // Parse individual functions in the function section for (int i = 0; i < exportCount; i++) { @@ -309,21 +341,23 @@ private static ExportSection parseExportSection(ByteBuffer buffer, long sectionI return new ExportSection(sectionId, sectionSize, exports); } - private static StartSection parseStartSection(ByteBuffer buffer, long sectionId, long sectionSize) { + private static StartSection parseStartSection( + ByteBuffer buffer, long sectionId, long sectionSize) { var startSection = new StartSection(sectionId, sectionSize); startSection.setStartIndex(readVarUInt32(buffer)); return startSection; } - private static ElementSection parseElementSection(ByteBuffer buffer, long sectionId, long sectionSize) { + private static ElementSection parseElementSection( + ByteBuffer buffer, long sectionId, long sectionSize) { var elementCount = readVarUInt32(buffer); - var elements = new Element[(int)elementCount]; + var elements = new Element[(int) elementCount]; for (var i = 0; i < elementCount; i++) { var tableIndex = readVarUInt32(buffer); var expr = parseExpression(buffer); var funcIndexCount = readVarUInt32(buffer); - var funcIndices = new long[(int)funcIndexCount]; + var funcIndices = new long[(int) funcIndexCount]; for (var j = 0; j < funcIndexCount; j++) { funcIndices[j] = readVarUInt32(buffer); } @@ -333,9 +367,10 @@ private static ElementSection parseElementSection(ByteBuffer buffer, long sectio return new ElementSection(sectionId, sectionSize, elements); } - private static CodeSection parseCodeSection(ByteBuffer buffer, long sectionId, long sectionSize) { + private static CodeSection parseCodeSection( + ByteBuffer buffer, long sectionId, long sectionSize) { var funcBodyCount = readVarUInt32(buffer); - var functionBodies = new FunctionBody[(int)funcBodyCount]; + var functionBodies = new FunctionBody[(int) funcBodyCount]; // Parse individual function bodies in the code section for (int i = 0; i < funcBodyCount; i++) { @@ -356,29 +391,33 @@ private static CodeSection parseCodeSection(ByteBuffer buffer, long sectionId, l switch (instruction.getOpcode()) { case BLOCK: case LOOP: - case IF: { - instruction.setDepth(++depth); - blockScope.push(instruction.getOpcode()); - instruction.setScope(blockScope.peek()); - break; - } - case END: { - instruction.setDepth(depth); - depth--; - if (blockScope.isEmpty()) { - instruction.setScope(OpCode.END); - } else { - instruction.setScope(blockScope.pop()); + case IF: + { + instruction.setDepth(++depth); + blockScope.push(instruction.getOpcode()); + instruction.setScope(blockScope.peek()); + break; + } + case END: + { + instruction.setDepth(depth); + depth--; + if (blockScope.isEmpty()) { + instruction.setScope(OpCode.END); + } else { + instruction.setScope(blockScope.pop()); + } + break; + } + default: + { + instruction.setDepth(depth); + break; } - break; - } - default: { - instruction.setDepth(depth); - break; - } } instructions.add(instruction); - //System.out.println(Integer.toHexString(instruction.getAddress()) + " " + instruction); + // System.out.println(Integer.toHexString(instruction.getAddress()) + " " + + // instruction); } while (buffer.position() < funcEndPoint); // label the instructions with jumps @@ -390,10 +429,10 @@ private static CodeSection parseCodeSection(ByteBuffer buffer, long sectionId, l return new CodeSection(sectionId, sectionSize, functionBodies); } - - private static DataSection parseDataSection(ByteBuffer buffer, long sectionId, long sectionSize) { + private static DataSection parseDataSection( + ByteBuffer buffer, long sectionId, long sectionSize) { var dataSegmentCount = readVarUInt32(buffer); - var dataSegments = new DataSegment[(int)dataSegmentCount]; + var dataSegments = new DataSegment[(int) dataSegmentCount]; for (var i = 0; i < dataSegmentCount; i++) { var idx = readVarUInt32(buffer); @@ -406,7 +445,6 @@ private static DataSection parseDataSection(ByteBuffer buffer, long sectionId, l return new DataSection(sectionId, sectionSize, dataSegments); } - private static Instruction parseInstruction(ByteBuffer buffer) { var address = buffer.position(); var b = buffer.get() & 0xff; @@ -414,10 +452,10 @@ private static Instruction parseInstruction(ByteBuffer buffer) { if (op == null) { throw new IllegalArgumentException("Can't find opcode for op value " + b); } - //System.out.println("b: " + b + " op: " + op); + // System.out.println("b: " + b + " op: " + op); var signature = OpCode.getSignature(op); if (signature.length == 0) { - return new Instruction(address, op, new long[]{}); + return new Instruction(address, op, new long[] {}); } var operands = new ArrayList(); for (var sig : signature) { @@ -437,13 +475,14 @@ private static Instruction parseInstruction(ByteBuffer buffer) { case FLOAT32: operands.add(readFloat32(buffer)); break; - case VEC_VARUINT: { - var vcount = (int) readVarUInt32(buffer); - for (var j = 0; j < vcount; j++) { - operands.add(readVarUInt32(buffer)); + case VEC_VARUINT: + { + var vcount = (int) readVarUInt32(buffer); + for (var j = 0; j < vcount; j++) { + operands.add(readVarUInt32(buffer)); + } + break; } - break; - } } } var operandsArray = new long[operands.size()]; diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/exceptions/ChicoryException.java b/wasm/src/main/java/com/dylibso/chicory/wasm/exceptions/ChicoryException.java index d325ad6d8..9e284adcb 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/exceptions/ChicoryException.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/exceptions/ChicoryException.java @@ -4,9 +4,11 @@ public class ChicoryException extends RuntimeException { public ChicoryException(String msg) { super(msg); } + public ChicoryException(Throwable cause) { super(cause); } + public ChicoryException(String msg, Throwable cause) { super(msg, cause); } diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/exceptions/InvalidException.java b/wasm/src/main/java/com/dylibso/chicory/wasm/exceptions/InvalidException.java index 2db9dd49b..26fd9f4e9 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/exceptions/InvalidException.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/exceptions/InvalidException.java @@ -4,9 +4,11 @@ public class InvalidException extends ChicoryException { public InvalidException(String msg) { super(msg); } + public InvalidException(Throwable cause) { super(cause); } + public InvalidException(String msg, Throwable cause) { super(msg, cause); } diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/exceptions/MalformedException.java b/wasm/src/main/java/com/dylibso/chicory/wasm/exceptions/MalformedException.java index 6cdd78b51..aac3ece47 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/exceptions/MalformedException.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/exceptions/MalformedException.java @@ -4,9 +4,11 @@ public class MalformedException extends ChicoryException { public MalformedException(String msg) { super(msg); } + public MalformedException(Throwable cause) { super(cause); } + public MalformedException(String msg, Throwable cause) { super(msg, cause); } diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/Ast.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/Ast.java index 8af04bfb9..980994439 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/Ast.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/Ast.java @@ -5,7 +5,8 @@ public class Ast { private CodeBlock root; private Stack stack; - //private List instructions; + + // private List instructions; public Ast() { this.root = new CodeBlock(BlockType.BLOCK); @@ -20,26 +21,30 @@ public CodeBlock getRoot() { public void addInstruction(Instruction i) { var current = peek(); switch (i.getOpcode()) { - case BLOCK: { - current.addInstruction(i); - var next = new CodeBlock(BlockType.BLOCK); - i.setCodeBlock(next); - push(next); - break; - } - case LOOP: { - current.addInstruction(i); - var next = new CodeBlock(BlockType.LOOP); - i.setCodeBlock(next); - push(next); - break; - } - case END: { + case BLOCK: + { + current.addInstruction(i); + var next = new CodeBlock(BlockType.BLOCK); + i.setCodeBlock(next); + push(next); + break; + } + case LOOP: + { + current.addInstruction(i); + var next = new CodeBlock(BlockType.LOOP); + i.setCodeBlock(next); + push(next); + break; + } + case END: + { + current.addInstruction(i); + pop(); + break; + } + default: current.addInstruction(i); - pop(); - break; - } - default: current.addInstruction(i); } } @@ -61,12 +66,15 @@ public void print() { private void printAst(CodeBlock block, int depth) { for (var i : block.getInstructions()) { - System.out.println("0x" + Integer.toHexString(i.getAddress()) + " | " + "\t".repeat(depth) + i.toString()); + System.out.println( + "0x" + + Integer.toHexString(i.getAddress()) + + " | " + + "\t".repeat(depth) + + i.toString()); if (i.getCodeBlock() != null) { printAst(i.getCodeBlock(), depth + 1); } } } - - } diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/CodeSection.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/CodeSection.java index 61d2bfdb3..5946683c3 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/CodeSection.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/CodeSection.java @@ -4,9 +4,11 @@ public class CodeSection extends Section { private FunctionBody[] functionBodies; public CodeSection(long id, long size, FunctionBody[] functionBodies) { - super(id, size); - this.functionBodies = functionBodies; + super(id, size); + this.functionBodies = functionBodies; } - public FunctionBody[] getFunctionBodies() { return functionBodies; } + public FunctionBody[] getFunctionBodies() { + return functionBodies; + } } diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/CustomSection.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/CustomSection.java index 161142658..0d5e7615a 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/CustomSection.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/CustomSection.java @@ -6,7 +6,7 @@ public class CustomSection extends Section { private byte[] bytes; public CustomSection(long id, long size) { - super(id, size); + super(id, size); } public String getName() { diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/DataSection.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/DataSection.java index b9ee8b9ef..0872cdc2b 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/DataSection.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/DataSection.java @@ -4,8 +4,8 @@ public class DataSection extends Section { private DataSegment[] dataSegments; public DataSection(long id, long size, DataSegment[] dataSegments) { - super(id, size); - this.dataSegments = dataSegments; + super(id, size); + this.dataSegments = dataSegments; } public DataSegment[] getDataSegments() { diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/ElementSection.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/ElementSection.java index 7db89c65e..9404d6db8 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/ElementSection.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/ElementSection.java @@ -4,8 +4,8 @@ public class ElementSection extends Section { private Element[] elements; public ElementSection(long id, long size, Element[] elements) { - super(id, size); - this.elements = elements; + super(id, size); + this.elements = elements; } public Element[] getElements() { diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/ElementType.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/ElementType.java index 5961bf2ae..141d84a44 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/ElementType.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/ElementType.java @@ -8,16 +8,21 @@ public enum ElementType { private final long id; - ElementType(long id) { this.id = id; } + ElementType(long id) { + this.id = id; + } - public long id() { return id; } + public long id() { + return id; + } private static final Map byId = new HashMap<>(1); static { - for (ElementType e : ElementType.values()) - byId.put(e.id(), e); + for (ElementType e : ElementType.values()) byId.put(e.id(), e); } - public static ElementType byId(long id) { return byId.get(id); } + public static ElementType byId(long id) { + return byId.get(id); + } } diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/ExportDescType.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/ExportDescType.java index 4be025468..5f93ed68a 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/ExportDescType.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/ExportDescType.java @@ -10,15 +10,22 @@ public enum ExportDescType { GlobalIdx(0x03); private final long id; - ExportDescType(long id) { this.id = id; } - public long id() { return id; } + ExportDescType(long id) { + this.id = id; + } + + public long id() { + return id; + } private static final Map byId = new HashMap(4); static { - for (ExportDescType e : ExportDescType.values()) - byId.put(e.id(), e); + for (ExportDescType e : ExportDescType.values()) byId.put(e.id(), e); + } + + public static ExportDescType byId(long id) { + return byId.get(id); } - public static ExportDescType byId(long id) { return byId.get(id); } } diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/ExportSection.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/ExportSection.java index 9b61d7cb8..949b80ddb 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/ExportSection.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/ExportSection.java @@ -4,8 +4,8 @@ public class ExportSection extends Section { private Export[] exports; public ExportSection(long id, long size, Export[] exports) { - super(id, size); - this.exports = exports; + super(id, size); + this.exports = exports; } public Export[] getExports() { diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/FunctionSection.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/FunctionSection.java index a4925c36a..3e274433f 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/FunctionSection.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/FunctionSection.java @@ -4,8 +4,8 @@ public class FunctionSection extends Section { private int[] typeIndices; public FunctionSection(long id, long size, int[] typeIndices) { - super(id, size); - this.typeIndices = typeIndices; + super(id, size); + this.typeIndices = typeIndices; } public int[] getTypeIndices() { diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/GlobalSection.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/GlobalSection.java index 37dec1d92..58dd11322 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/GlobalSection.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/GlobalSection.java @@ -4,8 +4,8 @@ public class GlobalSection extends Section { private Global[] globals; public GlobalSection(long id, long size, Global[] globals) { - super(id, size); - this.globals = globals; + super(id, size); + this.globals = globals; } public Global[] getGlobals() { diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/ImportDesc.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/ImportDesc.java index 75438980b..410935a68 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/ImportDesc.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/ImportDesc.java @@ -20,15 +20,15 @@ public ImportDescType getType() { public String toString() { switch (type) { case FuncIdx: - return "func["+index+"]"; + return "func[" + index + "]"; case TableIdx: - return "table["+index+"]"; + return "table[" + index + "]"; case MemIdx: - return "memory["+index+"]"; + return "memory[" + index + "]"; case GlobalIdx: - return "global["+index+"]"; + return "global[" + index + "]"; default: - return "unknown["+index+"]"; + return "unknown[" + index + "]"; } // if (type instanceof FuncIdx) { // return "func["+index+"]"; diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/ImportDescType.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/ImportDescType.java index 43bd1a088..def8a1283 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/ImportDescType.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/ImportDescType.java @@ -10,15 +10,22 @@ public enum ImportDescType { GlobalIdx(0x03); private final long id; - ImportDescType(long id) { this.id = id; } - public long id() { return id; } + ImportDescType(long id) { + this.id = id; + } + + public long id() { + return id; + } private static final Map byId = new HashMap(4); static { - for (ImportDescType e : ImportDescType.values()) - byId.put(e.id(), e); + for (ImportDescType e : ImportDescType.values()) byId.put(e.id(), e); + } + + public static ImportDescType byId(long id) { + return byId.get(id); } - public static ImportDescType byId(long id) { return byId.get(id); } } diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/ImportSection.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/ImportSection.java index 8ea1eba55..de68b1c03 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/ImportSection.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/ImportSection.java @@ -4,8 +4,8 @@ public class ImportSection extends Section { private Import[] imports; public ImportSection(long id, long size, Import[] imports) { - super(id, size); - this.imports = imports; + super(id, size); + this.imports = imports; } public Import[] getImports() { diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/Instruction.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/Instruction.java index 0a0d2f1aa..59d6b7196 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/Instruction.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/Instruction.java @@ -1,7 +1,6 @@ package com.dylibso.chicory.wasm.types; import java.util.Arrays; -import java.util.List; public class Instruction { private int address; diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/MemorySection.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/MemorySection.java index 84efcfeb9..c014ccae4 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/MemorySection.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/MemorySection.java @@ -4,8 +4,8 @@ public class MemorySection extends Section { private Memory[] memories; public MemorySection(long id, long size, Memory[] memories) { - super(id, size); - this.memories = memories; + super(id, size); + this.memories = memories; } public Memory[] getMemories() { diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/MutabilityType.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/MutabilityType.java index d469b38d6..5f32bd8d2 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/MutabilityType.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/MutabilityType.java @@ -9,15 +9,21 @@ public enum MutabilityType { private final long id; - MutabilityType(long id) { this.id = id; } + MutabilityType(long id) { + this.id = id; + } - public long id() { return id; } + public long id() { + return id; + } private static final Map byId = new HashMap<>(4); static { - for (MutabilityType e : MutabilityType.values()) - byId.put(e.id(), e); + for (MutabilityType e : MutabilityType.values()) byId.put(e.id(), e); + } + + public static MutabilityType byId(long id) { + return byId.get(id); } - public static MutabilityType byId(long id) { return byId.get(id); } } 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 81353895a..5c6d49477 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 @@ -7,198 +7,203 @@ * This file is generated by gen-instr.rb. Do not edit by hand. */ public enum OpCode { - UNREACHABLE(0x00), // unreachable - NOP(0x01), // nop - BLOCK(0x02), // block - LOOP(0x03), // loop - IF(0x04), // if - ELSE(0x05), // else - END(0x0B), // end - BR(0x0C), // br - BR_IF(0x0D), // br_if - BR_TABLE(0x0E), // br_table vec() - RETURN(0x0F), // return - CALL(0x10), // call - CALL_INDIRECT(0x11), // call_indirect - DROP(0x1A), // drop - SELECT(0x1B), // select - LOCAL_GET(0x20), // local.get - LOCAL_SET(0x21), // local.set - LOCAL_TEE(0x22), // local.tee - GLOBAL_GET(0x23), // global.get - GLOBAL_SET(0x24), // global.set - I32_LOAD(0x28), // i32.load - I64_LOAD(0x29), // i64.load - F32_LOAD(0x2A), // f32.load - F64_LOAD(0x2B), // f64.load - I32_LOAD8_S(0x2C), // i32.load8_s - I32_LOAD8_U(0x2D), // i32.load8_u - I32_LOAD16_S(0x2E), // i32.load16_s - I32_LOAD16_U(0x2F), // i32.load16_u - I64_LOAD8_S(0x30), // i64.load8_s - I64_LOAD8_U(0x31), // i64.load8_u - I64_LOAD16_S(0x32), // i64.load16_s - I64_LOAD16_U(0x33), // i64.load16_u - I64_LOAD32_S(0x34), // i64.load32_s - I64_LOAD32_U(0x35), // i64.load32_u - I32_STORE(0x36), // i32.store - I64_STORE(0x37), // i64.store - F32_STORE(0x38), // f32.store - F64_STORE(0x39), // f64.store - I32_STORE8(0x3A), // i32.store8 - I32_STORE16(0x3B), // i32.store16 - I64_STORE8(0x3C), // i64.store8 - I64_STORE16(0x3D), // i64.store16 - I64_STORE32(0x3E), // i64.store32 - MEMORY_SIZE(0x3F), // memory.size - MEMORY_GROW(0x40), // memory.grow - I32_CONST(0x41), // i32.const - I64_CONST(0x42), // i64.const - F32_CONST(0x43), // f32.const - F64_CONST(0x44), // f64.const - I32_EQZ(0x45), // i32.eqz - I32_EQ(0x46), // i32.eq - I32_NE(0x47), // i32.ne - I32_LT_S(0x48), // i32.lt_s - I32_LT_U(0x49), // i32.lt_u - I32_GT_S(0x4A), // i32.gt_s - I32_GT_U(0x4B), // i32.gt_u - I32_LE_S(0x4C), // i32.le_s - I32_LE_U(0x4D), // i32.le_u - I32_GE_S(0x4E), // i32.ge_s - I32_GE_U(0x4F), // i32.ge_u - I64_EQZ(0x50), // i64.eqz - I64_EQ(0x51), // i64.eq - I64_NE(0x52), // i64.ne - I64_LT_S(0x53), // i64.lt_s - I64_LT_U(0x54), // i64.lt_u - I64_GT_S(0x55), // i64.gt_s - I64_GT_U(0x56), // i64.gt_u - I64_LE_S(0x57), // i64.le_s - I64_LE_U(0x58), // i64.le_u - I64_GE_S(0x59), // i64.ge_s - I64_GE_U(0x5A), // i64.ge_u - F32_EQ(0x5B), // f32.eq - F32_NE(0x5C), // f32.ne - F32_LT(0x5D), // f32.lt - F32_GT(0x5E), // f32.gt - F32_LE(0x5F), // f32.le - F32_GE(0x60), // f32.ge - F64_EQ(0x61), // f64.eq - F64_NE(0x62), // f64.ne - F64_LT(0x63), // f64.lt - F64_GT(0x64), // f64.gt - F64_LE(0x65), // f64.le - F64_GE(0x66), // f64.ge - I32_CLZ(0x67), // i32.clz - I32_CTZ(0x68), // i32.ctz - I32_POPCNT(0x69), // i32.popcnt - I32_ADD(0x6A), // i32.add - I32_SUB(0x6B), // i32.sub - I32_MUL(0x6C), // i32.mul - I32_DIV_S(0x6D), // i32.div_s - I32_DIV_U(0x6E), // i32.div_u - I32_REM_S(0x6F), // i32.rem_s - I32_REM_U(0x70), // i32.rem_u - I32_AND(0x71), // i32.and - I32_OR(0x72), // i32.or - I32_XOR(0x73), // i32.xor - I32_SHL(0x74), // i32.shl - I32_SHR_S(0x75), // i32.shr_s - I32_SHR_U(0x76), // i32.shr_u - I32_ROTL(0x77), // i32.rotl - I32_ROTR(0x78), // i32.rotr - I64_CLZ(0x79), // i64.clz - I64_CTZ(0x7A), // i64.ctz - I64_POPCNT(0x7B), // i64.popcnt - I64_ADD(0x7C), // i64.add - I64_SUB(0x7D), // i64.sub - I64_MUL(0x7E), // i64.mul - I64_DIV_S(0x7F), // i64.div_s - I64_DIV_U(0x80), // i64.div_u - I64_REM_S(0x81), // i64.rem_s - I64_REM_U(0x82), // i64.rem_u - I64_AND(0x83), // i64.and - I64_OR(0x84), // i64.or - I64_XOR(0x85), // i64.xor - I64_SHL(0x86), // i64.shl - I64_SHR_S(0x87), // i64.shr_s - I64_SHR_U(0x88), // i64.shr_u - I64_ROTL(0x89), // i64.rotl - I64_ROTR(0x8A), // i64.rotr - F32_ABS(0x8B), // f32.abs - F32_NEG(0x8C), // f32.neg - F32_CEIL(0x8D), // f32.ceil - F32_FLOOR(0x8E), // f32.floor - F32_TRUNC(0x8F), // f32.trunc - F32_NEAREST(0x90), // f32.nearest - F32_SQRT(0x91), // f32.sqrt - F32_ADD(0x92), // f32.add - F32_SUB(0x93), // f32.sub - F32_MUL(0x94), // f32.mul - F32_DIV(0x95), // f32.div - F32_MIN(0x96), // f32.min - F32_MAX(0x97), // f32.max - F32_COPYSIGN(0x98), // f32.copysign - F64_ABS(0x99), // f64.abs - F64_NEG(0x9A), // f64.neg - F64_CEIL(0x9B), // f64.ceil - F64_FLOOR(0x9C), // f64.floor - F64_TRUNC(0x9D), // f64.trunc - F64_NEAREST(0x9E), // f64.nearest - F64_SQRT(0x9F), // f64.sqrt - F64_ADD(0xA0), // f64.add - F64_SUB(0xA1), // f64.sub - F64_MUL(0xA2), // f64.mul - F64_DIV(0xA3), // f64.div - F64_MIN(0xA4), // f64.min - F64_MAX(0xA5), // f64.max - F64_COPYSIGN(0xA6), // f64.copysign - I32_WRAP_I64(0xA7), // i32.wrap_i64 - I32_TRUNC_F32_S(0xA8), // i32.trunc_f32_s - I32_TRUNC_F32_U(0xA9), // i32.trunc_f32_u - I32_TRUNC_F64_S(0xAA), // i32.trunc_f64_s - I32_TRUNC_F64_U(0xAB), // i32.trunc_f64_u - I64_EXTEND_I32_S(0xAC), // i64.extend_i32_s - I64_EXTEND_I32_U(0xAD), // i64.extend_i32_u - I64_TRUNC_F32_S(0xAE), // i64.trunc_f32_s - I64_TRUNC_F32_U(0xAF), // i64.trunc_f32_u - I64_TRUNC_F64_S(0xB0), // i64.trunc_f64_s - I64_TRUNC_F64_U(0xB1), // i64.trunc_f64_u - F32_CONVERT_I32_S(0xB2), // f32.convert_i32_s - F32_CONVERT_I32_U(0xB3), // f32.convert_i32_u - F32_CONVERT_I64_S(0xB4), // f32.convert_i64_s - F32_CONVERT_I64_U(0xB5), // f32.convert_i64_u - F32_DEMOTE_F64(0xB6), // f32.demote_f64 - F64_CONVERT_I32_S(0xB7), // f64.convert_i32_s - F64_CONVERT_I32_U(0xB8), // f64.convert_i32_u - F64_CONVERT_I64_S(0xB9), // f64.convert_i64_s - F64_CONVERT_I64_U(0xBA), // f64.convert_i64_u - F64_PROMOTE_F32(0xBB), // f64.promote_f32 - I32_REINTERPRET_F32(0xBC), // i32.reinterpret_f32 - I64_REINTERPRET_F64(0xBD), // i64.reinterpret_f64 - F32_REINTERPRET_I32(0xBE), // f32.reinterpret_i32 - F64_REINTERPRET_I64(0xBF), // f64.reinterpret_i64 - I32_EXTEND_8_S(0xC0), // i32.extend_8_s - I32_EXTEND_16_S(0xC1), // i32.extend_16_s - I64_EXTEND_8_S(0xC2), // i64.extend_8_s - I64_EXTEND_16_S(0xC3), // i64.extend_16_s - I64_EXTEND_32_S(0xC4); // i64.extend_32_s + UNREACHABLE(0x00), // unreachable + NOP(0x01), // nop + BLOCK(0x02), // block + LOOP(0x03), // loop + IF(0x04), // if + ELSE(0x05), // else + END(0x0B), // end + BR(0x0C), // br + BR_IF(0x0D), // br_if + BR_TABLE(0x0E), // br_table vec() + RETURN(0x0F), // return + CALL(0x10), // call + CALL_INDIRECT(0x11), // call_indirect + DROP(0x1A), // drop + SELECT(0x1B), // select + LOCAL_GET(0x20), // local.get + LOCAL_SET(0x21), // local.set + LOCAL_TEE(0x22), // local.tee + GLOBAL_GET(0x23), // global.get + GLOBAL_SET(0x24), // global.set + I32_LOAD(0x28), // i32.load + I64_LOAD(0x29), // i64.load + F32_LOAD(0x2A), // f32.load + F64_LOAD(0x2B), // f64.load + I32_LOAD8_S(0x2C), // i32.load8_s + I32_LOAD8_U(0x2D), // i32.load8_u + I32_LOAD16_S(0x2E), // i32.load16_s + I32_LOAD16_U(0x2F), // i32.load16_u + I64_LOAD8_S(0x30), // i64.load8_s + I64_LOAD8_U(0x31), // i64.load8_u + I64_LOAD16_S(0x32), // i64.load16_s + I64_LOAD16_U(0x33), // i64.load16_u + I64_LOAD32_S(0x34), // i64.load32_s + I64_LOAD32_U(0x35), // i64.load32_u + I32_STORE(0x36), // i32.store + I64_STORE(0x37), // i64.store + F32_STORE(0x38), // f32.store + F64_STORE(0x39), // f64.store + I32_STORE8(0x3A), // i32.store8 + I32_STORE16(0x3B), // i32.store16 + I64_STORE8(0x3C), // i64.store8 + I64_STORE16(0x3D), // i64.store16 + I64_STORE32(0x3E), // i64.store32 + MEMORY_SIZE(0x3F), // memory.size + MEMORY_GROW(0x40), // memory.grow + I32_CONST(0x41), // i32.const + I64_CONST(0x42), // i64.const + F32_CONST(0x43), // f32.const + F64_CONST(0x44), // f64.const + I32_EQZ(0x45), // i32.eqz + I32_EQ(0x46), // i32.eq + I32_NE(0x47), // i32.ne + I32_LT_S(0x48), // i32.lt_s + I32_LT_U(0x49), // i32.lt_u + I32_GT_S(0x4A), // i32.gt_s + I32_GT_U(0x4B), // i32.gt_u + I32_LE_S(0x4C), // i32.le_s + I32_LE_U(0x4D), // i32.le_u + I32_GE_S(0x4E), // i32.ge_s + I32_GE_U(0x4F), // i32.ge_u + I64_EQZ(0x50), // i64.eqz + I64_EQ(0x51), // i64.eq + I64_NE(0x52), // i64.ne + I64_LT_S(0x53), // i64.lt_s + I64_LT_U(0x54), // i64.lt_u + I64_GT_S(0x55), // i64.gt_s + I64_GT_U(0x56), // i64.gt_u + I64_LE_S(0x57), // i64.le_s + I64_LE_U(0x58), // i64.le_u + I64_GE_S(0x59), // i64.ge_s + I64_GE_U(0x5A), // i64.ge_u + F32_EQ(0x5B), // f32.eq + F32_NE(0x5C), // f32.ne + F32_LT(0x5D), // f32.lt + F32_GT(0x5E), // f32.gt + F32_LE(0x5F), // f32.le + F32_GE(0x60), // f32.ge + F64_EQ(0x61), // f64.eq + F64_NE(0x62), // f64.ne + F64_LT(0x63), // f64.lt + F64_GT(0x64), // f64.gt + F64_LE(0x65), // f64.le + F64_GE(0x66), // f64.ge + I32_CLZ(0x67), // i32.clz + I32_CTZ(0x68), // i32.ctz + I32_POPCNT(0x69), // i32.popcnt + I32_ADD(0x6A), // i32.add + I32_SUB(0x6B), // i32.sub + I32_MUL(0x6C), // i32.mul + I32_DIV_S(0x6D), // i32.div_s + I32_DIV_U(0x6E), // i32.div_u + I32_REM_S(0x6F), // i32.rem_s + I32_REM_U(0x70), // i32.rem_u + I32_AND(0x71), // i32.and + I32_OR(0x72), // i32.or + I32_XOR(0x73), // i32.xor + I32_SHL(0x74), // i32.shl + I32_SHR_S(0x75), // i32.shr_s + I32_SHR_U(0x76), // i32.shr_u + I32_ROTL(0x77), // i32.rotl + I32_ROTR(0x78), // i32.rotr + I64_CLZ(0x79), // i64.clz + I64_CTZ(0x7A), // i64.ctz + I64_POPCNT(0x7B), // i64.popcnt + I64_ADD(0x7C), // i64.add + I64_SUB(0x7D), // i64.sub + I64_MUL(0x7E), // i64.mul + I64_DIV_S(0x7F), // i64.div_s + I64_DIV_U(0x80), // i64.div_u + I64_REM_S(0x81), // i64.rem_s + I64_REM_U(0x82), // i64.rem_u + I64_AND(0x83), // i64.and + I64_OR(0x84), // i64.or + I64_XOR(0x85), // i64.xor + I64_SHL(0x86), // i64.shl + I64_SHR_S(0x87), // i64.shr_s + I64_SHR_U(0x88), // i64.shr_u + I64_ROTL(0x89), // i64.rotl + I64_ROTR(0x8A), // i64.rotr + F32_ABS(0x8B), // f32.abs + F32_NEG(0x8C), // f32.neg + F32_CEIL(0x8D), // f32.ceil + F32_FLOOR(0x8E), // f32.floor + F32_TRUNC(0x8F), // f32.trunc + F32_NEAREST(0x90), // f32.nearest + F32_SQRT(0x91), // f32.sqrt + F32_ADD(0x92), // f32.add + F32_SUB(0x93), // f32.sub + F32_MUL(0x94), // f32.mul + F32_DIV(0x95), // f32.div + F32_MIN(0x96), // f32.min + F32_MAX(0x97), // f32.max + F32_COPYSIGN(0x98), // f32.copysign + F64_ABS(0x99), // f64.abs + F64_NEG(0x9A), // f64.neg + F64_CEIL(0x9B), // f64.ceil + F64_FLOOR(0x9C), // f64.floor + F64_TRUNC(0x9D), // f64.trunc + F64_NEAREST(0x9E), // f64.nearest + F64_SQRT(0x9F), // f64.sqrt + F64_ADD(0xA0), // f64.add + F64_SUB(0xA1), // f64.sub + F64_MUL(0xA2), // f64.mul + F64_DIV(0xA3), // f64.div + F64_MIN(0xA4), // f64.min + F64_MAX(0xA5), // f64.max + F64_COPYSIGN(0xA6), // f64.copysign + I32_WRAP_I64(0xA7), // i32.wrap_i64 + I32_TRUNC_F32_S(0xA8), // i32.trunc_f32_s + I32_TRUNC_F32_U(0xA9), // i32.trunc_f32_u + I32_TRUNC_F64_S(0xAA), // i32.trunc_f64_s + I32_TRUNC_F64_U(0xAB), // i32.trunc_f64_u + I64_EXTEND_I32_S(0xAC), // i64.extend_i32_s + I64_EXTEND_I32_U(0xAD), // i64.extend_i32_u + I64_TRUNC_F32_S(0xAE), // i64.trunc_f32_s + I64_TRUNC_F32_U(0xAF), // i64.trunc_f32_u + I64_TRUNC_F64_S(0xB0), // i64.trunc_f64_s + I64_TRUNC_F64_U(0xB1), // i64.trunc_f64_u + F32_CONVERT_I32_S(0xB2), // f32.convert_i32_s + F32_CONVERT_I32_U(0xB3), // f32.convert_i32_u + F32_CONVERT_I64_S(0xB4), // f32.convert_i64_s + F32_CONVERT_I64_U(0xB5), // f32.convert_i64_u + F32_DEMOTE_F64(0xB6), // f32.demote_f64 + F64_CONVERT_I32_S(0xB7), // f64.convert_i32_s + F64_CONVERT_I32_U(0xB8), // f64.convert_i32_u + F64_CONVERT_I64_S(0xB9), // f64.convert_i64_s + F64_CONVERT_I64_U(0xBA), // f64.convert_i64_u + F64_PROMOTE_F32(0xBB), // f64.promote_f32 + I32_REINTERPRET_F32(0xBC), // i32.reinterpret_f32 + I64_REINTERPRET_F64(0xBD), // i64.reinterpret_f64 + F32_REINTERPRET_I32(0xBE), // f32.reinterpret_i32 + F64_REINTERPRET_I64(0xBF), // f64.reinterpret_i64 + I32_EXTEND_8_S(0xC0), // i32.extend_8_s + I32_EXTEND_16_S(0xC1), // i32.extend_16_s + I64_EXTEND_8_S(0xC2), // i64.extend_8_s + I64_EXTEND_16_S(0xC3), // i64.extend_16_s + I64_EXTEND_32_S(0xC4); // i64.extend_32_s private final int opcode; - OpCode(int opcode) { this.opcode = opcode; } + OpCode(int opcode) { + this.opcode = opcode; + } - public int opcode() { return opcode; } + public int opcode() { + return opcode; + } private static final Map byOpCode = new HashMap<>(); static { - for (OpCode e : OpCode.values()) - byOpCode.put(e.opcode(), e); + for (OpCode e : OpCode.values()) byOpCode.put(e.opcode(), e); } - public static OpCode byOpCode(int opcode) { return byOpCode.get(opcode); } + public static OpCode byOpCode(int opcode) { + return byOpCode.get(opcode); + } private static final Map signature = new HashMap<>(); @@ -207,182 +212,189 @@ public static WasmEncoding[] getSignature(OpCode o) { } static { - signature.put(UNREACHABLE, new WasmEncoding[]{}); - signature.put(NOP, new WasmEncoding[]{}); - signature.put(BLOCK, new WasmEncoding[]{WasmEncoding.VARUINT}); - signature.put(LOOP, new WasmEncoding[]{WasmEncoding.VARUINT}); - signature.put(IF, new WasmEncoding[]{WasmEncoding.VARUINT}); - signature.put(ELSE, new WasmEncoding[]{}); - signature.put(END, new WasmEncoding[]{}); - signature.put(BR, new WasmEncoding[]{WasmEncoding.VARUINT}); - signature.put(BR_IF, new WasmEncoding[]{WasmEncoding.VARUINT}); - signature.put(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(DROP, new WasmEncoding[]{}); - signature.put(SELECT, new WasmEncoding[]{}); - signature.put(LOCAL_GET, new WasmEncoding[]{WasmEncoding.VARUINT}); - signature.put(LOCAL_SET, new WasmEncoding[]{WasmEncoding.VARUINT}); - signature.put(LOCAL_TEE, new WasmEncoding[]{WasmEncoding.VARUINT}); - signature.put(GLOBAL_GET, new WasmEncoding[]{WasmEncoding.VARUINT}); - signature.put(GLOBAL_SET, new WasmEncoding[]{WasmEncoding.VARUINT}); - signature.put(I32_LOAD, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(I64_LOAD, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(F32_LOAD, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(F64_LOAD, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(I32_LOAD8_S, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(I32_LOAD8_U, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(I32_LOAD16_S, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(I32_LOAD16_U, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(I64_LOAD8_S, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(I64_LOAD8_U, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(I64_LOAD16_S, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(I64_LOAD16_U, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(I64_LOAD32_S, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(I64_LOAD32_U, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(I32_STORE, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(I64_STORE, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(F32_STORE, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(F64_STORE, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(I32_STORE8, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(I32_STORE16, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(I64_STORE8, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(I64_STORE16, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(I64_STORE32, new WasmEncoding[]{WasmEncoding.VARUINT,WasmEncoding.VARUINT}); - signature.put(MEMORY_SIZE, new WasmEncoding[]{}); - signature.put(MEMORY_GROW, new WasmEncoding[]{}); - signature.put(I32_CONST, new WasmEncoding[]{WasmEncoding.VARSINT32}); - signature.put(I64_CONST, new WasmEncoding[]{WasmEncoding.VARSINT64}); - signature.put(F32_CONST, new WasmEncoding[]{WasmEncoding.FLOAT32}); - signature.put(F64_CONST, new WasmEncoding[]{WasmEncoding.FLOAT64}); - signature.put(I32_EQZ, new WasmEncoding[]{}); - signature.put(I32_EQ, new WasmEncoding[]{}); - signature.put(I32_NE, new WasmEncoding[]{}); - signature.put(I32_LT_S, new WasmEncoding[]{}); - signature.put(I32_LT_U, new WasmEncoding[]{}); - signature.put(I32_GT_S, new WasmEncoding[]{}); - signature.put(I32_GT_U, new WasmEncoding[]{}); - signature.put(I32_LE_S, new WasmEncoding[]{}); - signature.put(I32_LE_U, new WasmEncoding[]{}); - signature.put(I32_GE_S, new WasmEncoding[]{}); - signature.put(I32_GE_U, new WasmEncoding[]{}); - signature.put(I64_EQZ, new WasmEncoding[]{}); - signature.put(I64_EQ, new WasmEncoding[]{}); - signature.put(I64_NE, new WasmEncoding[]{}); - signature.put(I64_LT_S, new WasmEncoding[]{}); - signature.put(I64_LT_U, new WasmEncoding[]{}); - signature.put(I64_GT_S, new WasmEncoding[]{}); - signature.put(I64_GT_U, new WasmEncoding[]{}); - signature.put(I64_LE_S, new WasmEncoding[]{}); - signature.put(I64_LE_U, new WasmEncoding[]{}); - signature.put(I64_GE_S, new WasmEncoding[]{}); - signature.put(I64_GE_U, new WasmEncoding[]{}); - signature.put(F32_EQ, new WasmEncoding[]{}); - signature.put(F32_NE, new WasmEncoding[]{}); - signature.put(F32_LT, new WasmEncoding[]{}); - signature.put(F32_GT, new WasmEncoding[]{}); - signature.put(F32_LE, new WasmEncoding[]{}); - signature.put(F32_GE, new WasmEncoding[]{}); - signature.put(F64_EQ, new WasmEncoding[]{}); - signature.put(F64_NE, new WasmEncoding[]{}); - signature.put(F64_LT, new WasmEncoding[]{}); - signature.put(F64_GT, new WasmEncoding[]{}); - signature.put(F64_LE, new WasmEncoding[]{}); - signature.put(F64_GE, new WasmEncoding[]{}); - signature.put(I32_CLZ, new WasmEncoding[]{}); - signature.put(I32_CTZ, new WasmEncoding[]{}); - signature.put(I32_POPCNT, new WasmEncoding[]{}); - signature.put(I32_ADD, new WasmEncoding[]{}); - signature.put(I32_SUB, new WasmEncoding[]{}); - signature.put(I32_MUL, new WasmEncoding[]{}); - signature.put(I32_DIV_S, new WasmEncoding[]{}); - signature.put(I32_DIV_U, new WasmEncoding[]{}); - signature.put(I32_REM_S, new WasmEncoding[]{}); - signature.put(I32_REM_U, new WasmEncoding[]{}); - signature.put(I32_AND, new WasmEncoding[]{}); - signature.put(I32_OR, new WasmEncoding[]{}); - signature.put(I32_XOR, new WasmEncoding[]{}); - signature.put(I32_SHL, new WasmEncoding[]{}); - signature.put(I32_SHR_S, new WasmEncoding[]{}); - signature.put(I32_SHR_U, new WasmEncoding[]{}); - signature.put(I32_ROTL, new WasmEncoding[]{}); - signature.put(I32_ROTR, new WasmEncoding[]{}); - signature.put(I64_CLZ, new WasmEncoding[]{}); - signature.put(I64_CTZ, new WasmEncoding[]{}); - signature.put(I64_POPCNT, new WasmEncoding[]{}); - signature.put(I64_ADD, new WasmEncoding[]{}); - signature.put(I64_SUB, new WasmEncoding[]{}); - signature.put(I64_MUL, new WasmEncoding[]{}); - signature.put(I64_DIV_S, new WasmEncoding[]{}); - signature.put(I64_DIV_U, new WasmEncoding[]{}); - signature.put(I64_REM_S, new WasmEncoding[]{}); - signature.put(I64_REM_U, new WasmEncoding[]{}); - signature.put(I64_AND, new WasmEncoding[]{}); - signature.put(I64_OR, new WasmEncoding[]{}); - signature.put(I64_XOR, new WasmEncoding[]{}); - signature.put(I64_SHL, new WasmEncoding[]{}); - signature.put(I64_SHR_S, new WasmEncoding[]{}); - signature.put(I64_SHR_U, new WasmEncoding[]{}); - signature.put(I64_ROTL, new WasmEncoding[]{}); - signature.put(I64_ROTR, new WasmEncoding[]{}); - signature.put(F32_ABS, new WasmEncoding[]{}); - signature.put(F32_NEG, new WasmEncoding[]{}); - signature.put(F32_CEIL, new WasmEncoding[]{}); - signature.put(F32_FLOOR, new WasmEncoding[]{}); - signature.put(F32_TRUNC, new WasmEncoding[]{}); - signature.put(F32_NEAREST, new WasmEncoding[]{}); - signature.put(F32_SQRT, new WasmEncoding[]{}); - signature.put(F32_ADD, new WasmEncoding[]{}); - signature.put(F32_SUB, new WasmEncoding[]{}); - signature.put(F32_MUL, new WasmEncoding[]{}); - signature.put(F32_DIV, new WasmEncoding[]{}); - signature.put(F32_MIN, new WasmEncoding[]{}); - signature.put(F32_MAX, new WasmEncoding[]{}); - signature.put(F32_COPYSIGN, new WasmEncoding[]{}); - signature.put(F64_ABS, new WasmEncoding[]{}); - signature.put(F64_NEG, new WasmEncoding[]{}); - signature.put(F64_CEIL, new WasmEncoding[]{}); - signature.put(F64_FLOOR, new WasmEncoding[]{}); - signature.put(F64_TRUNC, new WasmEncoding[]{}); - signature.put(F64_NEAREST, new WasmEncoding[]{}); - signature.put(F64_SQRT, new WasmEncoding[]{}); - signature.put(F64_ADD, new WasmEncoding[]{}); - signature.put(F64_SUB, new WasmEncoding[]{}); - signature.put(F64_MUL, new WasmEncoding[]{}); - signature.put(F64_DIV, new WasmEncoding[]{}); - signature.put(F64_MIN, new WasmEncoding[]{}); - signature.put(F64_MAX, new WasmEncoding[]{}); - signature.put(F64_COPYSIGN, new WasmEncoding[]{}); - signature.put(I32_WRAP_I64, new WasmEncoding[]{}); - signature.put(I32_TRUNC_F32_S, new WasmEncoding[]{}); - signature.put(I32_TRUNC_F32_U, new WasmEncoding[]{}); - signature.put(I32_TRUNC_F64_S, new WasmEncoding[]{}); - signature.put(I32_TRUNC_F64_U, new WasmEncoding[]{}); - signature.put(I64_EXTEND_I32_S, new WasmEncoding[]{}); - signature.put(I64_EXTEND_I32_U, new WasmEncoding[]{}); - signature.put(I64_TRUNC_F32_S, new WasmEncoding[]{}); - signature.put(I64_TRUNC_F32_U, new WasmEncoding[]{}); - signature.put(I64_TRUNC_F64_S, new WasmEncoding[]{}); - signature.put(I64_TRUNC_F64_U, new WasmEncoding[]{}); - signature.put(F32_CONVERT_I32_S, new WasmEncoding[]{}); - signature.put(F32_CONVERT_I32_U, new WasmEncoding[]{}); - signature.put(F32_CONVERT_I64_S, new WasmEncoding[]{}); - signature.put(F32_CONVERT_I64_U, new WasmEncoding[]{}); - signature.put(F32_DEMOTE_F64, new WasmEncoding[]{}); - signature.put(F64_CONVERT_I32_S, new WasmEncoding[]{}); - signature.put(F64_CONVERT_I32_U, new WasmEncoding[]{}); - signature.put(F64_CONVERT_I64_S, new WasmEncoding[]{}); - signature.put(F64_CONVERT_I64_U, new WasmEncoding[]{}); - signature.put(F64_PROMOTE_F32, new WasmEncoding[]{}); - signature.put(I32_REINTERPRET_F32, new WasmEncoding[]{}); - signature.put(I64_REINTERPRET_F64, new WasmEncoding[]{}); - signature.put(F32_REINTERPRET_I32, new WasmEncoding[]{}); - signature.put(F64_REINTERPRET_I64, new WasmEncoding[]{}); - signature.put(I32_EXTEND_8_S, new WasmEncoding[]{}); - signature.put(I32_EXTEND_16_S, new WasmEncoding[]{}); - signature.put(I64_EXTEND_8_S, new WasmEncoding[]{}); - signature.put(I64_EXTEND_16_S, new WasmEncoding[]{}); - signature.put(I64_EXTEND_32_S, new WasmEncoding[]{}); - } + signature.put(UNREACHABLE, new WasmEncoding[] {}); + signature.put(NOP, new WasmEncoding[] {}); + signature.put(BLOCK, new WasmEncoding[] {WasmEncoding.VARUINT}); + signature.put(LOOP, new WasmEncoding[] {WasmEncoding.VARUINT}); + signature.put(IF, new WasmEncoding[] {WasmEncoding.VARUINT}); + signature.put(ELSE, new WasmEncoding[] {}); + signature.put(END, new WasmEncoding[] {}); + signature.put(BR, new WasmEncoding[] {WasmEncoding.VARUINT}); + signature.put(BR_IF, new WasmEncoding[] {WasmEncoding.VARUINT}); + signature.put( + 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(DROP, new WasmEncoding[] {}); + signature.put(SELECT, new WasmEncoding[] {}); + signature.put(LOCAL_GET, new WasmEncoding[] {WasmEncoding.VARUINT}); + signature.put(LOCAL_SET, new WasmEncoding[] {WasmEncoding.VARUINT}); + signature.put(LOCAL_TEE, new WasmEncoding[] {WasmEncoding.VARUINT}); + signature.put(GLOBAL_GET, new WasmEncoding[] {WasmEncoding.VARUINT}); + signature.put(GLOBAL_SET, new WasmEncoding[] {WasmEncoding.VARUINT}); + signature.put(I32_LOAD, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put(I64_LOAD, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put(F32_LOAD, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put(F64_LOAD, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put(I32_LOAD8_S, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put(I32_LOAD8_U, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put( + I32_LOAD16_S, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put( + I32_LOAD16_U, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put(I64_LOAD8_S, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put(I64_LOAD8_U, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put( + I64_LOAD16_S, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put( + I64_LOAD16_U, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put( + I64_LOAD32_S, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put( + I64_LOAD32_U, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put(I32_STORE, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put(I64_STORE, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put(F32_STORE, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put(F64_STORE, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put(I32_STORE8, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put(I32_STORE16, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put(I64_STORE8, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put(I64_STORE16, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put(I64_STORE32, new WasmEncoding[] {WasmEncoding.VARUINT, WasmEncoding.VARUINT}); + signature.put(MEMORY_SIZE, new WasmEncoding[] {}); + signature.put(MEMORY_GROW, new WasmEncoding[] {}); + signature.put(I32_CONST, new WasmEncoding[] {WasmEncoding.VARSINT32}); + signature.put(I64_CONST, new WasmEncoding[] {WasmEncoding.VARSINT64}); + signature.put(F32_CONST, new WasmEncoding[] {WasmEncoding.FLOAT32}); + signature.put(F64_CONST, new WasmEncoding[] {WasmEncoding.FLOAT64}); + signature.put(I32_EQZ, new WasmEncoding[] {}); + signature.put(I32_EQ, new WasmEncoding[] {}); + signature.put(I32_NE, new WasmEncoding[] {}); + signature.put(I32_LT_S, new WasmEncoding[] {}); + signature.put(I32_LT_U, new WasmEncoding[] {}); + signature.put(I32_GT_S, new WasmEncoding[] {}); + signature.put(I32_GT_U, new WasmEncoding[] {}); + signature.put(I32_LE_S, new WasmEncoding[] {}); + signature.put(I32_LE_U, new WasmEncoding[] {}); + signature.put(I32_GE_S, new WasmEncoding[] {}); + signature.put(I32_GE_U, new WasmEncoding[] {}); + signature.put(I64_EQZ, new WasmEncoding[] {}); + signature.put(I64_EQ, new WasmEncoding[] {}); + signature.put(I64_NE, new WasmEncoding[] {}); + signature.put(I64_LT_S, new WasmEncoding[] {}); + signature.put(I64_LT_U, new WasmEncoding[] {}); + signature.put(I64_GT_S, new WasmEncoding[] {}); + signature.put(I64_GT_U, new WasmEncoding[] {}); + signature.put(I64_LE_S, new WasmEncoding[] {}); + signature.put(I64_LE_U, new WasmEncoding[] {}); + signature.put(I64_GE_S, new WasmEncoding[] {}); + signature.put(I64_GE_U, new WasmEncoding[] {}); + signature.put(F32_EQ, new WasmEncoding[] {}); + signature.put(F32_NE, new WasmEncoding[] {}); + signature.put(F32_LT, new WasmEncoding[] {}); + signature.put(F32_GT, new WasmEncoding[] {}); + signature.put(F32_LE, new WasmEncoding[] {}); + signature.put(F32_GE, new WasmEncoding[] {}); + signature.put(F64_EQ, new WasmEncoding[] {}); + signature.put(F64_NE, new WasmEncoding[] {}); + signature.put(F64_LT, new WasmEncoding[] {}); + signature.put(F64_GT, new WasmEncoding[] {}); + signature.put(F64_LE, new WasmEncoding[] {}); + signature.put(F64_GE, new WasmEncoding[] {}); + signature.put(I32_CLZ, new WasmEncoding[] {}); + signature.put(I32_CTZ, new WasmEncoding[] {}); + signature.put(I32_POPCNT, new WasmEncoding[] {}); + signature.put(I32_ADD, new WasmEncoding[] {}); + signature.put(I32_SUB, new WasmEncoding[] {}); + signature.put(I32_MUL, new WasmEncoding[] {}); + signature.put(I32_DIV_S, new WasmEncoding[] {}); + signature.put(I32_DIV_U, new WasmEncoding[] {}); + signature.put(I32_REM_S, new WasmEncoding[] {}); + signature.put(I32_REM_U, new WasmEncoding[] {}); + signature.put(I32_AND, new WasmEncoding[] {}); + signature.put(I32_OR, new WasmEncoding[] {}); + signature.put(I32_XOR, new WasmEncoding[] {}); + signature.put(I32_SHL, new WasmEncoding[] {}); + signature.put(I32_SHR_S, new WasmEncoding[] {}); + signature.put(I32_SHR_U, new WasmEncoding[] {}); + signature.put(I32_ROTL, new WasmEncoding[] {}); + signature.put(I32_ROTR, new WasmEncoding[] {}); + signature.put(I64_CLZ, new WasmEncoding[] {}); + signature.put(I64_CTZ, new WasmEncoding[] {}); + signature.put(I64_POPCNT, new WasmEncoding[] {}); + signature.put(I64_ADD, new WasmEncoding[] {}); + signature.put(I64_SUB, new WasmEncoding[] {}); + signature.put(I64_MUL, new WasmEncoding[] {}); + signature.put(I64_DIV_S, new WasmEncoding[] {}); + signature.put(I64_DIV_U, new WasmEncoding[] {}); + signature.put(I64_REM_S, new WasmEncoding[] {}); + signature.put(I64_REM_U, new WasmEncoding[] {}); + signature.put(I64_AND, new WasmEncoding[] {}); + signature.put(I64_OR, new WasmEncoding[] {}); + signature.put(I64_XOR, new WasmEncoding[] {}); + signature.put(I64_SHL, new WasmEncoding[] {}); + signature.put(I64_SHR_S, new WasmEncoding[] {}); + signature.put(I64_SHR_U, new WasmEncoding[] {}); + signature.put(I64_ROTL, new WasmEncoding[] {}); + signature.put(I64_ROTR, new WasmEncoding[] {}); + signature.put(F32_ABS, new WasmEncoding[] {}); + signature.put(F32_NEG, new WasmEncoding[] {}); + signature.put(F32_CEIL, new WasmEncoding[] {}); + signature.put(F32_FLOOR, new WasmEncoding[] {}); + signature.put(F32_TRUNC, new WasmEncoding[] {}); + signature.put(F32_NEAREST, new WasmEncoding[] {}); + signature.put(F32_SQRT, new WasmEncoding[] {}); + signature.put(F32_ADD, new WasmEncoding[] {}); + signature.put(F32_SUB, new WasmEncoding[] {}); + signature.put(F32_MUL, new WasmEncoding[] {}); + signature.put(F32_DIV, new WasmEncoding[] {}); + signature.put(F32_MIN, new WasmEncoding[] {}); + signature.put(F32_MAX, new WasmEncoding[] {}); + signature.put(F32_COPYSIGN, new WasmEncoding[] {}); + signature.put(F64_ABS, new WasmEncoding[] {}); + signature.put(F64_NEG, new WasmEncoding[] {}); + signature.put(F64_CEIL, new WasmEncoding[] {}); + signature.put(F64_FLOOR, new WasmEncoding[] {}); + signature.put(F64_TRUNC, new WasmEncoding[] {}); + signature.put(F64_NEAREST, new WasmEncoding[] {}); + signature.put(F64_SQRT, new WasmEncoding[] {}); + signature.put(F64_ADD, new WasmEncoding[] {}); + signature.put(F64_SUB, new WasmEncoding[] {}); + signature.put(F64_MUL, new WasmEncoding[] {}); + signature.put(F64_DIV, new WasmEncoding[] {}); + signature.put(F64_MIN, new WasmEncoding[] {}); + signature.put(F64_MAX, new WasmEncoding[] {}); + signature.put(F64_COPYSIGN, new WasmEncoding[] {}); + signature.put(I32_WRAP_I64, new WasmEncoding[] {}); + signature.put(I32_TRUNC_F32_S, new WasmEncoding[] {}); + signature.put(I32_TRUNC_F32_U, new WasmEncoding[] {}); + signature.put(I32_TRUNC_F64_S, new WasmEncoding[] {}); + signature.put(I32_TRUNC_F64_U, new WasmEncoding[] {}); + signature.put(I64_EXTEND_I32_S, new WasmEncoding[] {}); + signature.put(I64_EXTEND_I32_U, new WasmEncoding[] {}); + signature.put(I64_TRUNC_F32_S, new WasmEncoding[] {}); + signature.put(I64_TRUNC_F32_U, new WasmEncoding[] {}); + signature.put(I64_TRUNC_F64_S, new WasmEncoding[] {}); + signature.put(I64_TRUNC_F64_U, new WasmEncoding[] {}); + signature.put(F32_CONVERT_I32_S, new WasmEncoding[] {}); + signature.put(F32_CONVERT_I32_U, new WasmEncoding[] {}); + signature.put(F32_CONVERT_I64_S, new WasmEncoding[] {}); + signature.put(F32_CONVERT_I64_U, new WasmEncoding[] {}); + signature.put(F32_DEMOTE_F64, new WasmEncoding[] {}); + signature.put(F64_CONVERT_I32_S, new WasmEncoding[] {}); + signature.put(F64_CONVERT_I32_U, new WasmEncoding[] {}); + signature.put(F64_CONVERT_I64_S, new WasmEncoding[] {}); + signature.put(F64_CONVERT_I64_U, new WasmEncoding[] {}); + signature.put(F64_PROMOTE_F32, new WasmEncoding[] {}); + signature.put(I32_REINTERPRET_F32, new WasmEncoding[] {}); + signature.put(I64_REINTERPRET_F64, new WasmEncoding[] {}); + signature.put(F32_REINTERPRET_I32, new WasmEncoding[] {}); + signature.put(F64_REINTERPRET_I64, new WasmEncoding[] {}); + signature.put(I32_EXTEND_8_S, new WasmEncoding[] {}); + signature.put(I32_EXTEND_16_S, new WasmEncoding[] {}); + signature.put(I64_EXTEND_8_S, new WasmEncoding[] {}); + signature.put(I64_EXTEND_16_S, new WasmEncoding[] {}); + signature.put(I64_EXTEND_32_S, new WasmEncoding[] {}); + } } diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/StartSection.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/StartSection.java index e2746fdee..86b6d5ae5 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/StartSection.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/StartSection.java @@ -4,7 +4,7 @@ public class StartSection extends Section { private long startIndex; public StartSection(long id, long size) { - super(id, size); + super(id, size); } public long getStartIndex() { diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/TableSection.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/TableSection.java index 3410cfcc7..3e6fd75bb 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/TableSection.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/TableSection.java @@ -4,8 +4,8 @@ public class TableSection extends Section { private Table[] tables; public TableSection(long id, long size, Table[] tables) { - super(id, size); - this.tables = tables; + super(id, size); + this.tables = tables; } public Table[] getTables() { diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/TypeSection.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/TypeSection.java index 82882e392..7982d71ba 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/TypeSection.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/TypeSection.java @@ -4,8 +4,8 @@ public class TypeSection extends Section { private FunctionType[] types; public TypeSection(long id, long size, FunctionType[] types) { - super(id, size); - this.types = types; + super(id, size); + this.types = types; } public FunctionType[] getTypes() { 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 837eed7be..f4d4b03ed 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 @@ -1,16 +1,14 @@ package com.dylibso.chicory.wasm.types; -import com.dylibso.chicory.wasm.Encoding; - import java.math.BigInteger; import java.nio.ByteBuffer; -import java.nio.ByteOrder; public class Value { private final ValueType type; private final byte[] data; public static Value TRUE; public static Value FALSE; + static { TRUE = Value.i32(1); FALSE = Value.i32(0); @@ -40,9 +38,13 @@ public static Value f64(long data) { return new Value(ValueType.F64, data); } - public ValueType getType() { return this.type; } + public ValueType getType() { + return this.type; + } - public byte[] getData() { return this.data; } + public byte[] getData() { + return this.data; + } public Value(ValueType type, byte[] data) { this.type = type; @@ -61,29 +63,31 @@ public Value(ValueType type, long data) { this.type = type; switch (type) { case I32: - case F32: { - this.data = new byte[4]; - this.data[0] = (byte) (data >> 24); - this.data[1] = (byte) (data >> 16); - this.data[2] = (byte) (data >> 8); - this.data[3] = (byte) data; - break; - } + case F32: + { + this.data = new byte[4]; + this.data[0] = (byte) (data >> 24); + this.data[1] = (byte) (data >> 16); + this.data[2] = (byte) (data >> 8); + this.data[3] = (byte) data; + break; + } case I64: - case F64: { - this.data = new byte[8]; - this.data[0] = (byte) (data >> 56); - this.data[1] = (byte) (data >> 48); - this.data[2] = (byte) (data >> 40); - this.data[3] = (byte) (data >> 32); - this.data[4] = (byte) (data >> 24); - this.data[5] = (byte) (data >> 16); - this.data[6] = (byte) (data >> 8); - this.data[7] = (byte) data; - break; - } + case F64: + { + this.data = new byte[8]; + this.data[0] = (byte) (data >> 56); + this.data[1] = (byte) (data >> 48); + this.data[2] = (byte) (data >> 40); + this.data[3] = (byte) (data >> 32); + this.data[4] = (byte) (data >> 24); + this.data[5] = (byte) (data >> 16); + this.data[6] = (byte) (data >> 8); + this.data[7] = (byte) data; + break; + } default: - this.data = new byte[]{}; + this.data = new byte[] {}; break; } } @@ -97,7 +101,8 @@ public int asInt() { case I64: case F64: return ByteBuffer.wrap(this.data, 4, 4).getInt(); - }; + } + ; throw new IllegalArgumentException("Can't turn wasm value of type " + type + " to a int"); } @@ -111,7 +116,8 @@ public long asUInt() { case I64: case F64: return ByteBuffer.wrap(this.data, 4, 4).getInt() & 0xFFFFFFFFL; - }; + } + ; throw new IllegalArgumentException("Can't turn wasm value of type " + type + " to a uint"); } @@ -139,7 +145,8 @@ public short asShort() { return ByteBuffer.wrap(this.data, 2, 2).getShort(); case I64: return ByteBuffer.wrap(this.data, 6, 2).getShort(); - }; + } + ; throw new IllegalArgumentException("Can't turn wasm value of type " + type + " to a short"); } @@ -153,29 +160,32 @@ public double asDouble() { public String toString() { switch (this.type) { - case I32: { - return this.asInt() + "@i32"; - } - case I64: { - return this.asLong() + "@i64"; - } - case F32: { - return this.asFloat() + "@f32"; - } - case F64: { - return this.asDouble() + "@f64"; - } - default: throw new RuntimeException("TODO handle float"); + case I32: + { + return this.asInt() + "@i32"; + } + case I64: + { + return this.asLong() + "@i64"; + } + case F32: + { + return this.asFloat() + "@f32"; + } + case F64: + { + return this.asDouble() + "@f64"; + } + default: + throw new RuntimeException("TODO handle float"); } } @Override public boolean equals(Object v) { - if (v == this) - return true; - if (!(v instanceof Value)) - return false; - Value other = (Value)v; + if (v == this) return true; + if (!(v instanceof Value)) return false; + Value other = (Value) v; return type.equals(other.type) && data.equals(other.data); } } diff --git a/wasm/src/main/java/com/dylibso/chicory/wasm/types/ValueType.java b/wasm/src/main/java/com/dylibso/chicory/wasm/types/ValueType.java index 8df6c6fb7..3d1b63f2a 100644 --- a/wasm/src/main/java/com/dylibso/chicory/wasm/types/ValueType.java +++ b/wasm/src/main/java/com/dylibso/chicory/wasm/types/ValueType.java @@ -11,15 +11,21 @@ public enum ValueType { private final long id; - ValueType(long id) { this.id = id; } + ValueType(long id) { + this.id = id; + } - public long id() { return id; } + public long id() { + return id; + } private static final Map byId = new HashMap<>(4); static { - for (ValueType e : ValueType.values()) - byId.put(e.id(), e); + for (ValueType e : ValueType.values()) byId.put(e.id(), e); + } + + public static ValueType byId(long id) { + return byId.get(id); } - public static ValueType byId(long id) { return byId.get(id); } } diff --git a/wasm/src/test/java/com/dylibso/chicory/wasm/ParserTest.java b/wasm/src/test/java/com/dylibso/chicory/wasm/ParserTest.java index 6132e8fdd..e6c4283eb 100644 --- a/wasm/src/test/java/com/dylibso/chicory/wasm/ParserTest.java +++ b/wasm/src/test/java/com/dylibso/chicory/wasm/ParserTest.java @@ -1,21 +1,17 @@ package com.dylibso.chicory.wasm; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import com.dylibso.chicory.wasm.types.CustomSection; import com.dylibso.chicory.wasm.types.OpCode; import com.dylibso.chicory.wasm.types.SectionId; -import com.dylibso.chicory.wasm.Encoding; -import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.types.ValueType; -import org.junit.Test; - import java.io.File; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertThrows; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.fail; +import org.junit.Test; public class ParserTest { @@ -44,7 +40,7 @@ public void shouldParseFile() { var segment = dataSegments[0]; assertEquals(0, segment.getIdx()); assertEquals(OpCode.I32_CONST, segment.getOffset()[0].getOpcode()); - assertArrayEquals(new byte[]{0x00,0x01,0x02,0x03}, segment.getData()); + assertArrayEquals(new byte[] {0x00, 0x01, 0x02, 0x03}, segment.getData()); // check start section var startSection = module.getStartSection(); @@ -54,12 +50,12 @@ public void shouldParseFile() { var funcSection = module.getFunctionSection(); var typeIndices = funcSection.getTypeIndices(); assertEquals(1, typeIndices.length); - assertEquals(1L, (long)typeIndices[0]); + assertEquals(1L, (long) typeIndices[0]); // check export section -// var exportSection = module.getExportSection(); -// var exports = exportSection.getExports(); -// assertEquals(1, exports.size()); + // var exportSection = module.getExportSection(); + // var exports = exportSection.getExports(); + // assertEquals(1, exports.size()); // check memory section var memorySection = module.getMemorySection(); @@ -79,9 +75,9 @@ public void shouldParseFile() { assertEquals("0x00000032: I32_CONST [42]", instructions.get(0).toString()); assertEquals(OpCode.I32_CONST, instructions.get(0).getOpcode()); - assertEquals(42L, (long)instructions.get(0).getOperands()[0]); + assertEquals(42L, (long) instructions.get(0).getOperands()[0]); assertEquals(OpCode.CALL, instructions.get(1).getOpcode()); - assertEquals(0L, (long)instructions.get(1).getOperands()[0]); + assertEquals(0L, (long) instructions.get(1).getOperands()[0]); assertEquals(OpCode.END, instructions.get(2).getOpcode()); } @@ -100,7 +96,7 @@ public void shouldParseIterfact() { var funcSection = module.getFunctionSection(); var typeIndices = funcSection.getTypeIndices(); assertEquals(1, typeIndices.length); - assertEquals(0L, (long)typeIndices[0]); + assertEquals(0L, (long) typeIndices[0]); var codeSection = module.getCodeSection(); var functionBodies = codeSection.getFunctionBodies(); @@ -127,27 +123,28 @@ public void shouldParseAllFiles() { public void shouldSupportCustomListener() { var parser = new Parser("src/test/resources/wasm/code.wasm"); parser.includeSection(SectionId.CUSTOM); - parser.setListener(s -> { - if (s.getSectionId() == SectionId.CUSTOM) { - var customSection = (CustomSection) s; - var name = customSection.getName(); - assertTrue(name.length() > 0); - } else { - fail("Should not have received section with id: " + s.getSectionId()); - } - }); + parser.setListener( + s -> { + if (s.getSectionId() == SectionId.CUSTOM) { + var customSection = (CustomSection) s; + var name = customSection.getName(); + assertTrue(name.length() > 0); + } else { + fail("Should not have received section with id: " + s.getSectionId()); + } + }); parser.parse(); } -// @Test -// public void shouldParseAst() { -// var parser = new Parser("src/test/resources/wasm/code.wasm"); -// var module = parser.parseModule(); -// var codeSection = module.getCodeSection(); -// var fbody = codeSection.getFunctionBodies()[0]; -// var ast = fbody.getAst(); -// ast.print(); -// } + // @Test + // public void shouldParseAst() { + // var parser = new Parser("src/test/resources/wasm/code.wasm"); + // var module = parser.parseModule(); + // var codeSection = module.getCodeSection(); + // var fbody = codeSection.getFunctionBodies()[0]; + // var ast = fbody.getAst(); + // ast.print(); + // } @Test public void shouldParseFloats() { @@ -161,7 +158,6 @@ public void shouldParseFloats() { assertEquals(0.123456789012345d, f64, 0.00000000000001d); } - @Test public void shouldProperlyParseSignedValue() { var parser = new Parser("src/test/resources/wasm/i32.wat.wasm"); diff --git a/wasm/src/test/java/com/dylibso/chicory/wasm/types/ValueTest.java b/wasm/src/test/java/com/dylibso/chicory/wasm/types/ValueTest.java index 0f489b144..25b61686e 100644 --- a/wasm/src/test/java/com/dylibso/chicory/wasm/types/ValueTest.java +++ b/wasm/src/test/java/com/dylibso/chicory/wasm/types/ValueTest.java @@ -1,10 +1,9 @@ package com.dylibso.chicory.wasm.types; -import com.dylibso.chicory.wasm.types.Value; -import org.junit.Test; - import static org.junit.Assert.*; +import org.junit.Test; + public class ValueTest { @Test public void shouldEncodeValuesFromLong() {