aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-25 15:46:25 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-25 15:46:25 +0100
commitb44683eb40df4920e51d927088823d29d44faee6 (patch)
tree332cdc1ebb5b93b48d59db2d699855eda26e44f4
parentb91f3ecbba9954f7f46d954918d87266d1518af7 (diff)
Add WASM code gen for push-int instruction
-rw-r--r--snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.WasmCodeGenPushIntTests]/[method:pushIntIsCompiledToOperandInStack(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected24
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java6
-rw-r--r--src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenPushIntTests.java27
3 files changed, 54 insertions, 3 deletions
diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.WasmCodeGenPushIntTests]/[method:pushIntIsCompiledToOperandInStack(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.WasmCodeGenPushIntTests]/[method:pushIntIsCompiledToOperandInStack(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
new file mode 100644
index 00000000..65cddd82
--- /dev/null
+++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.WasmCodeGenPushIntTests]/[method:pushIntIsCompiledToOperandInStack(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
@@ -0,0 +1,24 @@
+
+module.wasm: file format wasm 0x1
+
+Section Details:
+
+Type[1]:
+ - type[0] () -> nil
+Function[1]:
+ - func[0] sig=0 <f>
+Code[1]:
+ - func[0] size=4 <f>
+Custom:
+ - name: "name"
+ - func[0] <f>
+Custom:
+ - name: "linking"
+ - symbol table [count=1]
+ - 0: F <f> func=0 [ binding=global vis=default ]
+
+Code Disassembly:
+
+000026 func[0] <f>:
+ 000027: 41 2a | i32.const 42
+ 000029: 0b | end \ No newline at end of file
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 b6e2a09f..443393d1 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java
@@ -30,9 +30,9 @@ public class WasmCodeGen {
case PushBoolInstruction pushBool ->
context.addInstruction(new WasmI32Const(pushBool.value() ? 1 : 0));
- case PushIntInstruction pushIntInstruction -> {
- throw new UnsupportedOperationException("TODO");
- }
+ case PushIntInstruction pushInt ->
+ context.addInstruction(new WasmI32Const(pushInt.value().intValueExact()));
+
case PushStringInstruction pushStringInstruction -> {
throw new UnsupportedOperationException("TODO");
}
diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenPushIntTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenPushIntTests.java
new file mode 100644
index 00000000..e461f5f6
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenPushIntTests.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 WasmCodeGenPushIntTests {
+ // TODO: handle larger ints
+
+ @Test
+ public void pushIntIsCompiledToOperandInStack(Snapshotter snapshotter) throws IOException, InterruptedException {
+ var instruction = Instructions.pushInt(42);
+ var context = WasmCodeGenContext.EMPTY;
+
+ var result = WasmCodeGen.generateInstruction(instruction, context);
+
+ var module = result.toFunction().toModule();
+ snapshotter.assertSnapshot(objdump(module));
+ }
+}