From 3c4abe6920253f63621a314890878b6037335a73 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 25 Jun 2023 15:47:47 +0100 Subject: Add WASM code gen for push-unit instruction --- .../compiler/backends/wasm/WasmCodeGen.java | 6 ++--- .../backends/wasm/WasmCodeGenPushUnitTests.java | 27 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenPushUnitTests.java (limited to 'src') 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 443393d1..e71d1f15 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java @@ -36,9 +36,9 @@ public class WasmCodeGen { case PushStringInstruction pushStringInstruction -> { throw new UnsupportedOperationException("TODO"); } - case PushUnitInstruction pushUnitInstruction -> { - throw new UnsupportedOperationException("TODO"); - } + case PushUnitInstruction pushUnit -> + context.addInstruction(new WasmI32Const(0)); + case ReturnInstruction returnInstruction -> { throw new UnsupportedOperationException("TODO"); } diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenPushUnitTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenPushUnitTests.java new file mode 100644 index 00000000..919f57f8 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenPushUnitTests.java @@ -0,0 +1,27 @@ +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 WasmCodeGenPushUnitTests { + // TODO: handle larger ints + + @Test + public void pushIntIsCompiledToOperandInStack(Snapshotter snapshotter) throws IOException, InterruptedException { + var instruction = Instructions.pushUnit(); + var context = WasmCodeGenContext.EMPTY; + + var result = WasmCodeGen.generateInstruction(instruction, context); + + var module = result.toFunction().toModule(); + snapshotter.assertSnapshot(objdump(module)); + } +} -- cgit v1.2.3