From 23c81ac853c4e757382afebd473b54c5dcd56e11 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 20 Nov 2023 22:01:49 +0000 Subject: Add alloc-stack instruction to Patio --- .../compiler/backends/llvm/LlvmPatioCodeGen.java | 11 +++++++++++ .../compiler/backends/wasm/WasmPatioCodeGen.java | 9 +++++++++ .../org/shedlang/compiler/patio/ast/PatioSerializer.java | 7 +++++++ .../compiler/patio/ast/instructions/PatioAllocStack.java | 16 ++++++++++++++++ .../patio/ast/instructions/PatioInstruction.java | 2 +- 5 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioAllocStack.java (limited to 'src/main/java') 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(); } -- cgit v1.2.3