From d40cdf49825e4fc449a5e172f4e14088bb467606 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 26 Dec 2023 22:24:19 +0000 Subject: Use correct types for heap alloc call --- .../compiler/backends/wasm/PatioToWasm.java | 40 ++++++++++++++++------ 1 file changed, 30 insertions(+), 10 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java b/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java index a3c2e944..890217e8 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java @@ -162,16 +162,25 @@ public class PatioToWasm { return switch (patioInstruction) { case PatioAllocHeap patioAllocHeap -> { context = context - .addInstruction(new WasmI32Const(sizeOf( - patioAllocHeap.layout(), - platformTarget - ))) - .addInstruction(new WasmI32Const(offsetOf( - patioAllocHeap.layout(), - patioAllocHeap.gcChildrenOffset(), - platformTarget - ))) - .addInstruction(new WasmI32Const(patioAllocHeap.gcChildrenCount())) + .addInstruction(wasmConst( + platformTarget.sizeT(), + sizeOf( + patioAllocHeap.layout(), + platformTarget + ) + )) + .addInstruction(wasmConst( + platformTarget.sizeT(), + offsetOf( + patioAllocHeap.layout(), + patioAllocHeap.gcChildrenOffset(), + platformTarget + ) + )) + .addInstruction(wasmConst( + platformTarget.sizeT(), + patioAllocHeap.gcChildrenCount() + )) .addInstruction(new WasmCall(new WasmFuncName(ALLOC_NAME))) .addInstruction(new WasmLocalSet(context.wasmLocalIndex(patioAllocHeap.target()))); @@ -711,6 +720,17 @@ public class PatioToWasm { : List.of(patioTypeToWasmValueType(type, platformTarget)); } + private static WasmInstruction wasmConst(WasmValueType wasmValueType, int value) { + // TODO: find a better way of doing this. Turn the number types into an enum? + if (wasmValueType == WasmValueType.I32) { + return new WasmI32Const(value); + } else if (wasmValueType == WasmValueType.I64) { + return new WasmI64Const(value); + } else { + throw new IllegalArgumentException("Cannot create int const for " + wasmValueType); + } + } + private static class Context { public static Context forFunction( WasmTarget platformTarget, -- cgit v1.2.3