diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-06-25 15:55:14 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-06-25 15:55:14 +0100 |
| commit | 6aa1d4c0dd57790b65f90b2c69b484ad64ce74bc (patch) | |
| tree | 5e6f6f6a5296c32294b368f458dec80b0e21cffb /src | |
| parent | 3c4abe6920253f63621a314890878b6037335a73 (diff) | |
Add WASM code gen for return instruction
Diffstat (limited to 'src')
6 files changed, 40 insertions, 10 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java index e71d1f15..b572f77e 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java @@ -1,6 +1,7 @@ package org.shedlang.compiler.backends.wasm; import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmI32Const; +import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmReturn; import org.shedlang.compiler.stackir.instructions.*; public class WasmCodeGen { @@ -39,9 +40,8 @@ public class WasmCodeGen { case PushUnitInstruction pushUnit -> context.addInstruction(new WasmI32Const(0)); - case ReturnInstruction returnInstruction -> { - throw new UnsupportedOperationException("TODO"); - } + case ReturnInstruction returnInstruction -> + context.addInstruction(new WasmReturn()); }; } } diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java index 4f33a003..b9cbd672 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java @@ -1,4 +1,4 @@ package org.shedlang.compiler.backends.wasm.structure.ast.instructions; -public sealed interface WasmInstruction permits WasmI32Const, WasmI64Const { +public sealed interface WasmInstruction permits WasmI32Const, WasmI64Const, WasmReturn { } diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmReturn.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmReturn.java new file mode 100644 index 00000000..6566468e --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmReturn.java @@ -0,0 +1,4 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.instructions; + +public record WasmReturn() implements WasmInstruction { +} 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 3ed6b618..4bd9bb9a 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,10 +1,7 @@ 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 org.shedlang.compiler.backends.wasm.structure.ast.instructions.*; import java.io.IOException; @@ -17,6 +14,12 @@ public class WasmBinaryFormatInstructions { private static void writeInstruction(WasmInstruction instruction, BinaryWriter writer) { switch (instruction) { + // == Control Instructions == + + case WasmReturn ignored -> { + writer.writeByte(0x0f); + } + // == Numeric Instructions == case WasmI32Const i32Const -> { diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenPushUnitTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenPushUnitTests.java index 919f57f8..ed9f3221 100644 --- a/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenPushUnitTests.java +++ b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenPushUnitTests.java @@ -12,8 +12,6 @@ import static org.shedlang.compiler.backends.wasm.structure.WasmObjdump.objdump; @ExtendWith({SnapshotResolver.class}) public class WasmCodeGenPushUnitTests { - // TODO: handle larger ints - @Test public void pushIntIsCompiledToOperandInStack(Snapshotter snapshotter) throws IOException, InterruptedException { var instruction = Instructions.pushUnit(); diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenReturnTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenReturnTests.java new file mode 100644 index 00000000..01c26402 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenReturnTests.java @@ -0,0 +1,25 @@ +package org.shedlang.compiler.backends.wasm; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.stackir.instructions.Instructions; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.backends.wasm.structure.WasmObjdump.objdump; + +@ExtendWith({SnapshotResolver.class}) +public class WasmCodeGenReturnTests { + @Test + public void returnCompilesToReturnInstruction(Snapshotter snapshotter) throws IOException, InterruptedException { + var instruction = Instructions.return_(); + var context = WasmCodeGenContext.EMPTY; + + var result = WasmCodeGen.generateInstruction(instruction, context); + + var module = result.toFunction().toModule(); + snapshotter.assertSnapshot(objdump(module)); + } +} |
