From 5e15348d603b824f7d8725cb33852881294935ab Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 24 Jun 2023 23:01:33 +0100 Subject: Add WasmExpression --- .../backends/wasm/structure/ast/instructions/WasmExpression.java | 6 ++++++ .../compiler/backends/wasm/structure/ast/modules/WasmFunction.java | 4 ++-- .../wasm/structure/binary/WasmBinaryFormatInstructions.java | 6 +++--- .../backends/wasm/structure/WasmBinaryFormatModuleTests.java | 7 ++++--- 4 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmExpression.java (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmExpression.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmExpression.java new file mode 100644 index 00000000..ef0969e5 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmExpression.java @@ -0,0 +1,6 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.instructions; + +import java.util.List; + +public record WasmExpression(List instructions) { +} diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFunction.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFunction.java index 2e5cf592..b63e6fc2 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFunction.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFunction.java @@ -1,6 +1,6 @@ package org.shedlang.compiler.backends.wasm.structure.ast.modules; -import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmInstruction; +import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmExpression; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmValueType; @@ -11,7 +11,7 @@ public record WasmFunction( List params, List locals, List resultTypes, - List body + WasmExpression body ) { public WasmFuncType type() { return new WasmFuncType( diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java index 58d3d28c..3ed6b618 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java @@ -1,12 +1,12 @@ package org.shedlang.compiler.backends.wasm.structure.binary; import org.shedlang.compiler.backends.wasm.structure.BinaryWriter; +import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmExpression; import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmI32Const; import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmI64Const; import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmInstruction; import java.io.IOException; -import java.util.List; public class WasmBinaryFormatInstructions { // Corresponds to https://webassembly.github.io/spec/core/binary/instructions.html @@ -33,10 +33,10 @@ public class WasmBinaryFormatInstructions { // == Expressions == static void writeExpression( - List instructions, + WasmExpression expression, BinaryWriter writer ) throws IOException { - for (var instruction : instructions) { + for (var instruction : expression.instructions()) { writeInstruction(instruction, writer); } writer.writeByte(0x0b); diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java index 29c0feba..7cf142a4 100644 --- a/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java +++ b/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java @@ -2,6 +2,7 @@ package org.shedlang.compiler.backends.wasm.structure; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmExpression; import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmI32Const; import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmI64Const; import org.shedlang.compiler.backends.wasm.structure.ast.modules.*; @@ -55,7 +56,7 @@ public class WasmBinaryFormatModuleTests { new WasmLocal("localB", WasmValueType.F64) ), List.of(WasmValueType.I32), - List.of(new WasmI32Const(0)) + new WasmExpression(List.of(new WasmI32Const(0))) )) .addFunction(new WasmFunction( "f2", @@ -66,7 +67,7 @@ public class WasmBinaryFormatModuleTests { new WasmLocal("localA", WasmValueType.I64) ), List.of(WasmValueType.I64), - List.of(new WasmI64Const(0)) + new WasmExpression(List.of(new WasmI64Const(0))) )); var result = serializeModule(module); @@ -103,7 +104,7 @@ public class WasmBinaryFormatModuleTests { ), List.of(), List.of(WasmValueType.I32), - List.of(new WasmI32Const(0)) + new WasmExpression(List.of(new WasmI32Const(0))) )) .addImport(new WasmImport( "ModuleName1", -- cgit v1.2.3