From d90a1e3828856f4c790b3527299e17e42cf13552 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 20 Nov 2023 22:11:25 +0000 Subject: Extract WasmBackend.generateNativeRuntime() --- .../compiler/backends/wasm/WasmBackend.java | 28 +++++++++++++--------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'src/main/java') 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 3a2d134d..43a27267 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java @@ -97,17 +97,7 @@ public class WasmBackend implements Backend { var wasmModule = WasmPatioCodeGen.generateCompilationUnit(patioCompilationUnit); - // TODO: put these in separate module, remove duplication - wasmModule = 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() - )) - )); + wasmModule = generateNativeRuntime(wasmModule); wasmModule = wasmModule.addFunction(new WasmFunction( "_start", @@ -123,6 +113,22 @@ public class WasmBackend implements Backend { return wasmModule; } + /** + * 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() + )) + )); + } + private static Path writeObjectFile(WasmModule module, Path workingDirectory, String name) throws IOException { var objectPath = workingDirectory.resolve(name + ".o"); Files.createDirectories(objectPath.getParent()); -- cgit v1.2.3