aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-11-20 22:11:25 +0000
committerMichael Williamson <mike@zwobble.org>2023-11-20 22:11:25 +0000
commitd90a1e3828856f4c790b3527299e17e42cf13552 (patch)
tree6bced215d979ad995d6e3bebcd680538197c8547 /src/main/java
parent1748d528ba9886c663ca1d24c2eac848e89161c5 (diff)
Extract WasmBackend.generateNativeRuntime()
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java28
1 files changed, 17 insertions, 11 deletions
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());