aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java76
1 files changed, 66 insertions, 10 deletions
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,