From 1812ae776a4e1906b20c7845e17d8a0e103eb275 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 3 Dec 2023 20:22:40 +0000 Subject: Use walloc to allocate on wasm32 --- runtime/meson.build | 8 ++++++++ runtime/src/shed.c | 14 ++----------- runtime/wasm32.meson | 2 +- .../compiler/backends/wasm/WasmBackend.java | 23 +++++++++------------- 4 files changed, 20 insertions(+), 27 deletions(-) diff --git a/runtime/meson.build b/runtime/meson.build index 41b9382b..dbef403a 100644 --- a/runtime/meson.build +++ b/runtime/meson.build @@ -7,3 +7,11 @@ static_library( 'src/strings.c', 'src/namespaces/Shed/Builtins.c', ) + +if meson.get_external_property('shed_runtime_is_wasm', false) + static_library( + 'walloc', + 'deps/walloc/walloc.c', + override_options : ['warning_level=0'], + ) +endif diff --git a/runtime/src/shed.c b/runtime/src/shed.c index f6d63d0b..478f51c2 100644 --- a/runtime/src/shed.c +++ b/runtime/src/shed.c @@ -10,20 +10,10 @@ ShedObject const shed_unit = 0; #ifdef __WASM__ -uint32_t const PAGE_SIZE = 65536; +void* malloc(unsigned long); -uint32_t wasm_memory_grow(uint32_t pages); - -void* shed__runtime__alloc(size_t size) { - // TODO: implement a more sensible scheme. This allocates a new page on every request. - uint32_t previous_page_count = wasm_memory_grow((size + PAGE_SIZE - 1) / PAGE_SIZE); - return (void*) (previous_page_count * PAGE_SIZE); -} - -#else +#endif void* shed__runtime__alloc(size_t size) { return malloc(size); } - -#endif diff --git a/runtime/wasm32.meson b/runtime/wasm32.meson index 6adb248b..5afedb96 100644 --- a/runtime/wasm32.meson +++ b/runtime/wasm32.meson @@ -6,4 +6,4 @@ ar = 'llvm-ar-16' [properties] c_args = [ '--target=wasm32', '-D__WASM__' ] c_link_args = [ '--target=wasm32' ] - +shed_runtime_is_wasm = true diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java index 97ea0a60..2006fe83 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java @@ -2,15 +2,18 @@ package org.shedlang.compiler.backends.wasm; import org.shedlang.compiler.backends.Backend; import org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormat; -import org.shedlang.compiler.backends.wasm.structure.ast.instructions.*; -import org.shedlang.compiler.backends.wasm.structure.ast.modules.*; -import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmValueType; +import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmCall; +import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmDrop; +import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmExpression; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFuncName; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFunction; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule; import org.shedlang.compiler.config.ProjectConfig; import org.shedlang.compiler.filesystem.ShedCompilerFiles; import org.shedlang.compiler.patio.PatioCodeGen; import org.shedlang.compiler.patio.PatioCodeGenContext; -import org.shedlang.compiler.sources.Sources; import org.shedlang.compiler.shid.ShidNamespace; +import org.shedlang.compiler.sources.Sources; import org.shedlang.compiler.typechecker.TypeInfos; import org.shedlang.compiler.types.NamespaceName; import org.shedlang.compiler.util.processes.ProcessError; @@ -79,6 +82,7 @@ public class WasmBackend implements Backend { } ldArgs.add(ShedCompilerFiles.getRoot().resolve("runtime/target/wasm32/libshed.a").toString()); + ldArgs.add(ShedCompilerFiles.getRoot().resolve("runtime/target/wasm32/libwalloc.a").toString()); runInheritIo(ldArgs); } @@ -117,16 +121,7 @@ public class WasmBackend implements Backend { * Generates the parts of the runtime that are written in native WASM. */ public static WasmModule generateNativeRuntime(WasmModule wasmModule) { - return wasmModule.addFunction(new WasmFunction( - "wasm_memory_grow", - List.of(new WasmLocal("pages", WasmValueType.I32)), - List.of(), - List.of(WasmValueType.I32), - new WasmExpression(List.of( - new WasmLocalGet(new WasmLocalIndex(0)), - new WasmMemoryGrow() - )) - )); + return wasmModule; } private static Path writeObjectFile(WasmModule module, Path workingDirectory, String name) throws IOException { -- cgit v1.2.3