aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-11-20 22:01:49 +0000
committerMichael Williamson <mike@zwobble.org>2023-11-20 22:01:49 +0000
commit23c81ac853c4e757382afebd473b54c5dcd56e11 (patch)
tree2a9a8830ee6cef1b11bfcc4f61889a6e8da2b131 /src/main/java/org
parent9b528a87f563b439235bb926d6213d5b2d957991 (diff)
Add alloc-stack instruction to Patio
Diffstat (limited to 'src/main/java/org')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGen.java11
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java9
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java7
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioAllocStack.java16
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java2
5 files changed, 44 insertions, 1 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGen.java b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGen.java
index 54e8f66b..e9329115 100644
--- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGen.java
@@ -198,6 +198,17 @@ public class LlvmPatioCodeGen {
));
}
+ case PatioAllocStack patioAllocStack -> {
+ var targetResult = context.addPatioLocalToLlvmLocal(patioAllocStack.target());
+ var target = targetResult.value;
+ context = targetResult.context;
+
+ yield context.addInstruction(new LlvmAlloca(
+ target,
+ patioDataLayoutToLlvmType(patioAllocStack.layout())
+ ));
+ }
+
case PatioBoolToInt patioBoolToInt -> {
var targetResult = context.addPatioLocalToLlvmLocal(patioBoolToInt.target());
var target = targetResult.value;
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java
index 3da39d47..7e15fa18 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java
@@ -137,6 +137,15 @@ public class WasmPatioCodeGen {
yield context;
}
+ case PatioAllocStack patioAllocStack -> {
+ context = context
+ .addInstruction(new WasmI32Const(sizeOf(patioAllocStack.layout())))
+ .addInstruction(new WasmCall(new WasmFuncName(ALLOC_NAME)))
+ .addInstruction(new WasmLocalSet(context.wasmLocalIndex(patioAllocStack.target())));
+
+ yield context;
+ }
+
case PatioBoolToInt patioBoolToInt ->
generateOperandToPatioLocal(
patioBoolToInt.target(),
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java b/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java
index 3476ff99..2b052800 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java
@@ -140,6 +140,13 @@ public class PatioSerializer {
dataLayoutToTree(allocHeap.layout())
);
+ case PatioAllocStack allocStack ->
+ treeList(
+ treeIdentifier("alloc-stack"),
+ operandToTree(allocStack.target()),
+ dataLayoutToTree(allocStack.layout())
+ );
+
case PatioBoolToInt boolToInt ->
treeList(
treeIdentifier("bool-to-int"),
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioAllocStack.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioAllocStack.java
new file mode 100644
index 00000000..9f85013d
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioAllocStack.java
@@ -0,0 +1,16 @@
+package org.shedlang.compiler.patio.ast.instructions;
+
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
+import org.shedlang.compiler.patio.ast.types.PatioDataLayout;
+import org.shedlang.compiler.sources.Source;
+
+/**
+ * Allocate space on the stack for the given data layout, and store the pointer
+ * to the allocated memory in the target local.
+ */
+public record PatioAllocStack(
+ PatioLocalRef target,
+ PatioDataLayout layout,
+ Source source
+) implements PatioInstruction {
+}
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java
index f473afdd..842063ab 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java
@@ -2,6 +2,6 @@ package org.shedlang.compiler.patio.ast.instructions;
import org.shedlang.compiler.sources.Source;
-public sealed interface PatioInstruction permits PatioAllocHeap, PatioBoolToInt, PatioCall, PatioCallPointer, PatioDataPtrToInt, PatioIf, PatioIntAdd, PatioIntEquals, PatioIntNotEqual, PatioIntSub, PatioIntToBool, PatioIntToDataPtr, PatioLocalStore, PatioLoop, PatioLoopBreak, PatioLoopContinue, PatioMemoryLoad, PatioMemoryStore, PatioReturn, PatioSwitch, PatioUnreachable, PatioVarLoad, PatioVarStore {
+public sealed interface PatioInstruction permits PatioAllocHeap, PatioAllocStack, PatioBoolToInt, PatioCall, PatioCallPointer, PatioDataPtrToInt, PatioIf, PatioIntAdd, PatioIntEquals, PatioIntNotEqual, PatioIntSub, PatioIntToBool, PatioIntToDataPtr, PatioLocalStore, PatioLoop, PatioLoopBreak, PatioLoopContinue, PatioMemoryLoad, PatioMemoryStore, PatioReturn, PatioSwitch, PatioUnreachable, PatioVarLoad, PatioVarStore {
Source source();
}