diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-06-28 20:30:54 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-06-28 20:30:54 +0100 |
| commit | 970da5376491712089e46b4ff4d27291a02bfa18 (patch) | |
| tree | c9a494c7ffa412808d3253e83e3b96b834093f73 | |
| parent | 42c1a57cd441d1cefd0aea277758b5be83ecf97e (diff) | |
Get hello-world example working on WASM
| -rw-r--r-- | runtime/src/shed.c | 27 | ||||
| -rw-r--r-- | runtime/src/shed.h | 8 | ||||
| -rw-r--r-- | src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenNamespaces.java | 23 | ||||
| -rw-r--r-- | src/test/java/org/shedlang/compiler/backends/wasm/WasmExampleTests.java | 2 |
4 files changed, 57 insertions, 3 deletions
diff --git a/runtime/src/shed.c b/runtime/src/shed.c index cf5af0bd..2b69eeae 100644 --- a/runtime/src/shed.c +++ b/runtime/src/shed.c @@ -24,6 +24,33 @@ void* shed__runtime__alloc(uint32_t size) { return wasm_memory_grow((size + PAGE_SIZE - 1) / PAGE_SIZE); } +typedef struct __wasi_ciovec_t { + uint8_t * buf; + size_t buf_len; +} __wasi_ciovec_t; + +uint16_t wasi_fd_write(uint32_t fd, __wasi_ciovec_t* iovs_ptr, uint32_t iovs_len, uint32_t *nwritten) + __attribute__(( + __import_module__("wasi_snapshot_preview1"), + __import_name__("fd_write"), + )); + +ShedObject shed__runtime__print(ShedClosurePointer closurePointer, ShedString value) { + uint32_t stdout = 1; + __wasi_ciovec_t iov = {.buf = value->data, .buf_len = value->length}; + uint32_t iovs_len = 1; + uint32_t nwritten; + wasi_fd_write(stdout, &iov, iovs_len, &nwritten); + return shed_unit; +} + +// TODO: Work out a good way to access the variable directly from our generated +// WASM (or perhaps just expose this as a builtin module and use the import +// machinery)? +ShedObject shed__runtime__print_closure__get() { + return (ShedObject) &shed__runtime__print_closure; +} + #else void* shed__runtime__alloc(uint64_t size) { diff --git a/runtime/src/shed.h b/runtime/src/shed.h index 1fd8f44d..313dd3bf 100644 --- a/runtime/src/shed.h +++ b/runtime/src/shed.h @@ -3,12 +3,18 @@ #include <stdint.h> +#ifdef __WASM__ +typedef uint32_t ShedObject; +typedef uint32_t StringLength; +#else typedef uint64_t ShedObject; +typedef uint64_t StringLength; +#endif extern ShedObject const shed_unit; struct ShedString { - uint64_t length; + StringLength length; uint8_t data[]; }; 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)), diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/WasmExampleTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/WasmExampleTests.java index c85ae984..fdbc7e4f 100644 --- a/src/test/java/org/shedlang/compiler/backends/wasm/WasmExampleTests.java +++ b/src/test/java/org/shedlang/compiler/backends/wasm/WasmExampleTests.java @@ -23,6 +23,6 @@ public class WasmExampleTests extends ExampleTests { @Override protected boolean isDisabled(String testName) { - return testName.equals("hello-world"); + return false; } } |
