aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-08 12:31:44 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-08 12:31:44 +0100
commitaf30cfaef7e34136b3f2e01f5cc30b3590587add (patch)
treea19ad7bd6a5b1bf8039a683c84e1ea5a0c51f2e2
parent1bac5891bdd6db84340c0de650d2c02d3d251207 (diff)
Generate WASM for string add
-rw-r--r--snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.WasmCodeGenStringAddTests]/[method:stringAddIsCompiledToCallToRuntimeStringAdd(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected35
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java2
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenAlloc.java2
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenStrings.java24
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenTypes.java1
-rw-r--r--src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenStringAddTests.java24
6 files changed, 86 insertions, 2 deletions
diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.WasmCodeGenStringAddTests]/[method:stringAddIsCompiledToCallToRuntimeStringAdd(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.WasmCodeGenStringAddTests]/[method:stringAddIsCompiledToCallToRuntimeStringAdd(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
new file mode 100644
index 00000000..e7e8e6ba
--- /dev/null
+++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.WasmCodeGenStringAddTests]/[method:stringAddIsCompiledToCallToRuntimeStringAdd(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
@@ -0,0 +1,35 @@
+
+module.wasm: file format wasm 0x1
+
+Section Details:
+
+Type[2]:
+ - type[0] (i32, i32) -> i32
+ - type[1] () -> nil
+Import[3]:
+ - table[0] type=funcref initial=0 <- env.__indirect_function_table
+ - func[0] sig=0 <shed__runtime__string_add> <- env.shed__runtime__string_add
+ - memory[0] pages: initial=0 <- env.__linear_memory
+Function[1]:
+ - func[1] sig=1 <f>
+Code[1]:
+ - func[1] size=8 <f>
+Custom:
+ - name: "linking"
+ - symbol table [count=2]
+ - 0: F <shed__runtime__string_add> func=0 [ undefined explicit_name binding=global vis=default ]
+ - 1: F <f> func=1 [ binding=global vis=default ]
+Custom:
+ - name: "reloc.CODE"
+ - relocations for section: 3 (Code) [1]
+ - R_WASM_FUNCTION_INDEX_LEB offset=0x000008(file=0x00008e) symbol=0 <shed__runtime__string_add>
+Custom:
+ - name: "name"
+ - func[0] <shed__runtime__string_add>
+ - func[1] <f>
+
+Code Disassembly:
+
+00008c func[1] <f>:
+ 00008d: 10 80 80 80 80 00 | call 0 <shed__runtime__string_add>
+ 000093: 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 dbf667c2..a9ee98ac 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java
@@ -243,7 +243,7 @@ public class WasmCodeGen {
context.addInstruction(new WasmReturn());
case StringAddInstruction stringAdd ->
- throw new UnsupportedOperationException("TODO");
+ WasmCodeGenStrings.add(context);
};
}
}
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenAlloc.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenAlloc.java
index 9d74e60c..96363570 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenAlloc.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenAlloc.java
@@ -20,7 +20,7 @@ public class WasmCodeGenAlloc {
return context
.addImport(new WasmImport(
"env",
- "shed__runtime__alloc",
+ allocName,
new WasmImportDescriptor.Func(
allocName,
new WasmFuncType(List.of(WasmValueType.I32), List.of(WasmValueType.I32))
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenStrings.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenStrings.java
index a7266091..d596c53c 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenStrings.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenStrings.java
@@ -1,11 +1,17 @@
package org.shedlang.compiler.backends.wasm;
+import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmCall;
import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmMemoryAddress;
import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmAlignment;
+import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFuncName;
+import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmImport;
+import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmImportDescriptor;
+import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
+import java.util.List;
public class WasmCodeGenStrings {
private WasmCodeGenStrings() {
@@ -27,4 +33,22 @@ public class WasmCodeGenStrings {
WasmAlignment.ALIGN_32_BIT
);
}
+
+ public static WasmCodeGenContext add(WasmCodeGenContext context) {
+ var runtimeAddName = "shed__runtime__string_add";
+
+ return context
+ .addImport(new WasmImport(
+ "env",
+ runtimeAddName,
+ new WasmImportDescriptor.Func(
+ runtimeAddName,
+ new WasmFuncType(
+ List.of(WasmCodeGenTypes.STRING_TYPE, WasmCodeGenTypes.STRING_TYPE),
+ List.of(WasmCodeGenTypes.STRING_TYPE)
+ )
+ )
+ ))
+ .addInstruction(new WasmCall(new WasmFuncName(runtimeAddName)));
+ }
}
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenTypes.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenTypes.java
index 5822f1b6..27010de9 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenTypes.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenTypes.java
@@ -7,4 +7,5 @@ public class WasmCodeGenTypes {
}
public static final WasmValueType VALUE_TYPE = WasmValueType.I32;
+ public static final WasmValueType STRING_TYPE = VALUE_TYPE;
}
diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenStringAddTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenStringAddTests.java
new file mode 100644
index 00000000..2e4d9ed6
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenStringAddTests.java
@@ -0,0 +1,24 @@
+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.WasmCodeGenTesting.objdumpContext;
+
+@ExtendWith({SnapshotResolver.class})
+public class WasmCodeGenStringAddTests {
+ @Test
+ public void stringAddIsCompiledToCallToRuntimeStringAdd(Snapshotter snapshotter) throws IOException, InterruptedException {
+ var instruction = Instructions.stringAdd();
+ var context = WasmCodeGenContext.EMPTY;
+
+ var result = WasmCodeGen.generateInstruction(instruction, context);
+
+ snapshotter.assertSnapshot(objdumpContext(result));
+ }
+}