aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-28 20:30:54 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-28 20:30:54 +0100
commit970da5376491712089e46b4ff4d27291a02bfa18 (patch)
treec9a494c7ffa412808d3253e83e3b96b834093f73 /src/main/java
parent42c1a57cd441d1cefd0aea277758b5be83ecf97e (diff)
Get hello-world example working on WASM
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenNamespaces.java23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenNamespaces.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenNamespaces.java
index f2fcbebf..f28ad61e 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenNamespaces.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenNamespaces.java
@@ -4,6 +4,7 @@ import org.shedlang.compiler.backends.wasm.structure.ast.instructions.*;
import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmMemoryAddress;
import org.shedlang.compiler.backends.wasm.structure.ast.modules.*;
import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType;
+import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmValueType;
import org.shedlang.compiler.types.NamespaceName;
import java.util.List;
@@ -47,7 +48,27 @@ public class WasmCodeGenNamespaces {
return context.createFunction(
globalNameNamespaceInit(namespaceName),
new WasmFuncType(List.of(), List.of()),
- generateInitBody
+ initContext -> {
+
+ // TODO: handle default locals more gracefully
+ var printResult = initContext.addShedLocal("print");
+ var print = printResult.value();
+ initContext = printResult.context();
+
+ initContext = initContext
+ .addImport(new WasmImport(
+ "env",
+ "shed__runtime__print_closure__get",
+ new WasmImportDescriptor.Func(
+ "shed__runtime__print_closure__get",
+ new WasmFuncType(List.of(), List.of(WasmValueType.I32))
+ )
+ ))
+ .addInstruction(new WasmCall(new WasmFuncName("shed__runtime__print_closure__get")))
+ .addInstruction(new WasmLocalSet(print));
+
+ return generateInitBody.apply(initContext);
+ }
).createFunction(
globalNameNamespaceImport(namespaceName),
new WasmFuncType(List.of(), List.of(WasmCodeGenTypes.VALUE_TYPE)),