From 970da5376491712089e46b4ff4d27291a02bfa18 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 28 Jun 2023 20:30:54 +0100 Subject: Get hello-world example working on WASM --- .../backends/wasm/WasmCodeGenNamespaces.java | 23 +++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'src/main/java') 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)), -- cgit v1.2.3