From b3efe37e3ee8ed6a7d044051f37faaea70f681fe Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 26 Dec 2023 22:44:00 +0000 Subject: Generate correct code for Patio int ops on wasm64 --- .../compiler/backends/wasm/PatioToWasm.java | 76 +++++++++++++++++++--- 1 file changed, 66 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 4c8f7dd3..bef80075 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java @@ -162,14 +162,14 @@ public class PatioToWasm { return switch (patioInstruction) { case PatioAllocHeap patioAllocHeap -> { context = context - .addInstruction(wasmConst( + .addInstruction(wasmIntConst( platformTarget.sizeT(), sizeOf( patioAllocHeap.layout(), platformTarget ) )) - .addInstruction(wasmConst( + .addInstruction(wasmIntConst( platformTarget.sizeT(), offsetOf( patioAllocHeap.layout(), @@ -177,7 +177,7 @@ public class PatioToWasm { platformTarget ) )) - .addInstruction(wasmConst( + .addInstruction(wasmIntConst( platformTarget.sizeT(), patioAllocHeap.gcChildrenCount() )) @@ -189,7 +189,7 @@ public class PatioToWasm { case PatioAllocStack patioAllocStack -> { context = context - .addInstruction(wasmConst( + .addInstruction(wasmIntConst( platformTarget.sizeT(), sizeOf(patioAllocStack.layout(), platformTarget) )) @@ -276,16 +276,32 @@ public class PatioToWasm { } case PatioIntAdd patioIntAdd -> - generateBinaryOperation(patioIntAdd, new WasmI32Add(), context); + generateBinaryOperation( + patioIntAdd, + wasmIntAdd(platformTarget.patioIntType()), + context + ); case PatioIntEquals patioIntEquals -> - generateBinaryOperation(patioIntEquals, new WasmI32Eq(), context); + generateBinaryOperation( + patioIntEquals, + wasmIntEq(platformTarget.patioIntType()), + context + ); case PatioIntNotEqual patioIntNotEqual -> - generateBinaryOperation(patioIntNotEqual, new WasmI32Ne(), context); + generateBinaryOperation( + patioIntNotEqual, + wasmIntNe(platformTarget.patioIntType()), + context + ); case PatioIntSub patioIntSub -> - generateBinaryOperation(patioIntSub, new WasmI32Sub(), context); + generateBinaryOperation( + patioIntSub, + wasmIntSub(platformTarget.patioIntType()), + context + ); case PatioIntToBool patioIntToBool -> generateOperandToPatioLocal( @@ -542,7 +558,7 @@ public class PatioToWasm { } case PatioInt patioInt -> - context.addInstruction(wasmConst( + context.addInstruction(wasmIntConst( context.platformTarget.patioIntType(), patioInt.value() )); @@ -726,7 +742,7 @@ public class PatioToWasm { : List.of(patioTypeToWasmValueType(type, platformTarget)); } - private static WasmInstruction wasmConst(WasmValueType wasmValueType, int value) { + private static WasmInstruction wasmIntConst(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); @@ -737,6 +753,46 @@ public class PatioToWasm { } } + private static WasmInstruction wasmIntEq(WasmValueType wasmValueType) { + if (wasmValueType == WasmValueType.I32) { + return new WasmI32Eq(); + } else if (wasmValueType == WasmValueType.I64) { + return new WasmI64Eq(); + } else { + throw new IllegalArgumentException("Cannot create int const for " + wasmValueType); + } + } + + private static WasmInstruction wasmIntNe(WasmValueType wasmValueType) { + if (wasmValueType == WasmValueType.I32) { + return new WasmI32Ne(); + } else if (wasmValueType == WasmValueType.I64) { + return new WasmI64Ne(); + } else { + throw new IllegalArgumentException("Cannot create int const for " + wasmValueType); + } + } + + private static WasmInstruction wasmIntAdd(WasmValueType wasmValueType) { + if (wasmValueType == WasmValueType.I32) { + return new WasmI32Add(); + } else if (wasmValueType == WasmValueType.I64) { + return new WasmI64Add(); + } else { + throw new IllegalArgumentException("Cannot create int const for " + wasmValueType); + } + } + + private static WasmInstruction wasmIntSub(WasmValueType wasmValueType) { + if (wasmValueType == WasmValueType.I32) { + return new WasmI32Sub(); + } else if (wasmValueType == WasmValueType.I64) { + return new WasmI64Sub(); + } else { + throw new IllegalArgumentException("Cannot create int const for " + wasmValueType); + } + } + private static class Context { public static Context forFunction( WasmTarget platformTarget, -- cgit v1.2.3