aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2024-01-21 19:31:31 +0000
committerMichael Williamson <mike@zwobble.org>2024-01-21 19:31:56 +0000
commit8462c0ecc99be62339fe1829363c15beb8c5c0f1 (patch)
tree4dced037b32cdcf463480e00e03b69556ba5175b /src/main/java
parentd6d1411a6f836a5d72bb3ef3c27619569f8b5131 (diff)
Remove alloc-heap from Patio
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/llvm/codegen/PatioToLlvm.java69
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java40
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioSerializer.java7
-rw-r--r--src/main/java/org/shedlang/compiler/patio/instructions/PatioAllocHeap.java24
-rw-r--r--src/main/java/org/shedlang/compiler/patio/instructions/PatioInstruction.java2
-rw-r--r--src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java39
-rw-r--r--src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioLocalSpace.java2
7 files changed, 37 insertions, 146 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/codegen/PatioToLlvm.java b/src/main/java/org/shedlang/compiler/backends/llvm/codegen/PatioToLlvm.java
index 2809c16b..7d70a90b 100644
--- a/src/main/java/org/shedlang/compiler/backends/llvm/codegen/PatioToLlvm.java
+++ b/src/main/java/org/shedlang/compiler/backends/llvm/codegen/PatioToLlvm.java
@@ -27,18 +27,6 @@ import java.util.Optional;
import java.util.stream.StreamSupport;
public class PatioToLlvm {
- private static LlvmFunctionDeclaration allocDeclaration(LlvmTarget target) {
- return new LlvmFunctionDeclaration(
- "shed__runtime__gc_alloc",
- LlvmTypes.pointer(LlvmTypes.INT_8),
- List.of(
- new LlvmParam(target.sizeT(), "size"),
- new LlvmParam(target.sizeT(), "children_offset"),
- new LlvmParam(target.sizeT(), "children_count")
- )
- );
- }
-
private static final LlvmType EXCEPTION_TAG_TYPE = LlvmTypes.INT_32;
private static LlvmType exceptionValueType(LlvmTarget target) {
@@ -117,7 +105,6 @@ public class PatioToLlvm {
PatioToLlvmGlobalContext globalContext
) {
var context = globalContext.enterModule()
- .addTopLevelEntity(allocDeclaration(globalContext.target()))
.addTopLevelEntity(setjmpDeclaration(globalContext.target()))
.addTopLevelEntity(EXCEPTION_HANDLER_DECLARATION)
.addTopLevelEntity(exceptionValueDeclaration(globalContext.target()))
@@ -308,34 +295,6 @@ public class PatioToLlvm {
PatioToLlvmFunctionContext context
) {
return switch (patioInstruction) {
- case PatioAllocHeap patioAllocHeap -> {
- var sizeResult = sizeOf(patioAllocHeap.layout(), context);
- var size = sizeResult.value();
- context = sizeResult.context();
-
- var destinationResult = context.addPatioLocalToLlvmLocal(patioAllocHeap.destination());
- var destination = destinationResult.value();
- context = destinationResult.context();
-
- var offsetResult = offsetOf(
- patioAllocHeap.layout(),
- patioAllocHeap.gcChildrenOffset(),
- context
- );
- var offset = offsetResult.value();
- context = offsetResult.context();
-
- var allocDeclaration = allocDeclaration(context.target());
- yield context.addInstruction(allocDeclaration.call(
- Optional.of(destination),
- List.of(
- size,
- offset,
- new LlvmOperandInt(patioAllocHeap.gcChildrenCount())
- )
- ));
- }
-
case PatioAllocStack patioAllocStack -> {
var destinationResult = context.addPatioLocalToLlvmLocal(patioAllocStack.destination());
var destination = destinationResult.value();
@@ -1360,17 +1319,6 @@ public class PatioToLlvm {
};
}
- private static PatioToLlvmFunctionContext.Result<LlvmOperand> sizeOf(
- PatioDataLayout layout,
- PatioToLlvmFunctionContext context
- ) {
- var sizeResult = context.allocateLocalName(LlvmCodeGenLocalVariableSpace.SIZE);
- var size = sizeResult.value();
- context = sizeResult.context();
-
- return sizeOf(size, layout, context).toResult(size);
- }
-
private static PatioToLlvmFunctionContext sizeOf(
LlvmVariable destination,
PatioDataLayout layout,
@@ -1411,23 +1359,6 @@ public class PatioToLlvm {
);
}
- private static PatioToLlvmFunctionContext.Result<LlvmOperand> offsetOf(
- PatioDataLayout patioDataLayout,
- List<Integer> patioOffset,
- PatioToLlvmFunctionContext context
- ) {
- var offsetResult = context.allocateLocalName(LlvmCodeGenLocalVariableSpace.OFFSET);
- var offset = offsetResult.value();
- context = offsetResult.context();
-
- return offsetOf(
- offset,
- patioDataLayout,
- patioOffset,
- context
- ).toResult(offset);
- }
-
private static PatioToLlvmFunctionContext offsetOf(
LlvmVariable destination,
PatioDataLayout patioDataLayout,
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 03cbeee9..fcf55e79 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java
@@ -21,24 +21,13 @@ import java.util.ArrayList;
import java.util.List;
public class PatioToWasm {
- public static final String ALLOC_NAME = "shed__runtime__gc_alloc";
public static final String STACK_ALLOC_NAME = "malloc";
public static WasmModule generateCompilationUnit(
PatioCompilationUnit patioCompilationUnit,
WasmTarget target
) {
- var module = WasmModule.EMPTY.addImport(new WasmImport(
- "env",
- ALLOC_NAME,
- new WasmImportDescriptor.Func(
- ALLOC_NAME,
- new WasmFuncType(
- List.of(target.sizeT(), target.sizeT(), target.sizeT()),
- List.of(target.dataPointerType())
- )
- )
- ));
+ var module = WasmModule.EMPTY;
module = module.addImport(new WasmImport(
"env",
@@ -160,33 +149,6 @@ public class PatioToWasm {
var target = context.target;
return switch (patioInstruction) {
- case PatioAllocHeap patioAllocHeap -> {
- context = context
- .addInstruction(wasmIntConst(
- target.sizeT(),
- sizeOf(
- patioAllocHeap.layout(),
- target
- )
- ))
- .addInstruction(wasmIntConst(
- target.sizeT(),
- offsetOf(
- patioAllocHeap.layout(),
- patioAllocHeap.gcChildrenOffset(),
- target
- )
- ))
- .addInstruction(wasmIntConst(
- target.sizeT(),
- patioAllocHeap.gcChildrenCount()
- ))
- .addInstruction(new WasmCall(new WasmFuncName(ALLOC_NAME)))
- .addInstruction(new WasmLocalSet(context.wasmLocalIndex(patioAllocHeap.destination())));
-
- yield context;
- }
-
case PatioAllocStack patioAllocStack -> {
context = context
.addInstruction(wasmIntConst(
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioSerializer.java b/src/main/java/org/shedlang/compiler/patio/PatioSerializer.java
index 2d65bf1b..3203993b 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioSerializer.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioSerializer.java
@@ -140,13 +140,6 @@ public class PatioSerializer {
private static Tree instructionToTree(PatioInstruction instruction) {
return switch (instruction) {
- case PatioAllocHeap allocHeap ->
- treeList(
- treeIdentifier("alloc-heap"),
- operandToTree(allocHeap.destination()),
- dataLayoutToTree(allocHeap.layout())
- );
-
case PatioAllocStack allocStack ->
treeList(
treeIdentifier("alloc-stack"),
diff --git a/src/main/java/org/shedlang/compiler/patio/instructions/PatioAllocHeap.java b/src/main/java/org/shedlang/compiler/patio/instructions/PatioAllocHeap.java
deleted file mode 100644
index 96be900b..00000000
--- a/src/main/java/org/shedlang/compiler/patio/instructions/PatioAllocHeap.java
+++ /dev/null
@@ -1,24 +0,0 @@
-package org.shedlang.compiler.patio.instructions;
-
-import org.shedlang.compiler.patio.operands.PatioLocalRef;
-import org.shedlang.compiler.patio.types.PatioDataLayout;
-import org.shedlang.compiler.sources.Source;
-
-import java.util.List;
-
-/**
- * Allocate space on the heap for the given data layout, and store the pointer
- * to the allocated memory in the destination local.
- */
-public record PatioAllocHeap(
- PatioLocalRef destination,
- PatioDataLayout layout,
- List<Integer> gcChildrenOffset,
- int gcChildrenCount,
- Source source
-) implements PatioInstruction {
- @Override
- public boolean isBlockTerminator() {
- return false;
- }
-}
diff --git a/src/main/java/org/shedlang/compiler/patio/instructions/PatioInstruction.java b/src/main/java/org/shedlang/compiler/patio/instructions/PatioInstruction.java
index 72d62c99..0c17f02f 100644
--- a/src/main/java/org/shedlang/compiler/patio/instructions/PatioInstruction.java
+++ b/src/main/java/org/shedlang/compiler/patio/instructions/PatioInstruction.java
@@ -2,7 +2,7 @@ package org.shedlang.compiler.patio.instructions;
import org.shedlang.compiler.sources.Source;
-public sealed interface PatioInstruction permits PatioAllocHeap, PatioAllocStack, PatioBlock, PatioBoolToInt, PatioByteOffsetOf, PatioByteSizeOf, PatioCall, PatioCallPointer, PatioDataPtrToInt, PatioIf, PatioIntAdd, PatioIntEquals, PatioIntNotEqual, PatioIntSub, PatioIntToBool, PatioIntToDataPtr, PatioLocalStore, PatioLoop, PatioLoopBreak, PatioLoopContinue, PatioMemoryLoad, PatioMemoryStore, PatioResume, PatioReturn, PatioReturnVoid, PatioSwitch, PatioThrow, PatioTry, PatioUnreachable, PatioVarLoad, PatioVarStore {
+public sealed interface PatioInstruction permits PatioAllocStack, PatioBlock, PatioBoolToInt, PatioByteOffsetOf, PatioByteSizeOf, PatioCall, PatioCallPointer, PatioDataPtrToInt, PatioIf, PatioIntAdd, PatioIntEquals, PatioIntNotEqual, PatioIntSub, PatioIntToBool, PatioIntToDataPtr, PatioLocalStore, PatioLoop, PatioLoopBreak, PatioLoopContinue, PatioMemoryLoad, PatioMemoryStore, PatioResume, PatioReturn, PatioReturnVoid, PatioSwitch, PatioThrow, PatioTry, PatioUnreachable, PatioVarLoad, PatioVarStore {
boolean isBlockTerminator();
Source source();
}
diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java
index 689cf68d..8dd36463 100644
--- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java
+++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java
@@ -2,8 +2,10 @@ package org.shedlang.compiler.shidtopatio;
import org.shedlang.compiler.patio.PatioFunctionDeclaration;
import org.shedlang.compiler.patio.PatioLocal;
-import org.shedlang.compiler.patio.instructions.PatioAllocHeap;
+import org.shedlang.compiler.patio.instructions.PatioByteOffsetOf;
+import org.shedlang.compiler.patio.instructions.PatioByteSizeOf;
import org.shedlang.compiler.patio.instructions.PatioCall;
+import org.shedlang.compiler.patio.operands.PatioInt;
import org.shedlang.compiler.patio.operands.PatioLocalRef;
import org.shedlang.compiler.patio.operands.PatioOperand;
import org.shedlang.compiler.patio.types.*;
@@ -125,6 +127,18 @@ public class ShidToPatioGc {
// == Allocation ==
+ private static final PatioFunctionDeclaration ALLOC = new PatioFunctionDeclaration(
+ "shed__runtime__gc_alloc",
+ List.of(
+ PatioLocal.param("size", PatioTypes.INT),
+ PatioLocal.param("children_offset", PatioTypes.INT),
+ PatioLocal.param("children_count", PatioTypes.INT)
+ ),
+ PatioTypes.DATA_POINTER,
+ Sources.RUNTIME
+ );
+
+
public static ShidToPatioContext alloc(
PatioLocalRef destination,
PatioDataLayout layout,
@@ -133,11 +147,24 @@ public class ShidToPatioGc {
Source source,
ShidToPatioContext context
) {
- return context.addInstruction(new PatioAllocHeap(
- destination,
- layout,
- childrenOffset,
- childrenCount,
+ context = context.ensureCompilationUnitElement(ALLOC);
+
+ var sizeResult = context.allocateVal(ShidToPatioLocalSpace.BYTE_SIZE, PatioTypes.INT);
+ var size = sizeResult.value();
+ context = sizeResult.context();
+
+ context = context.addInstruction(new PatioByteSizeOf(size, layout, source));
+
+ var offsetResult = context.allocateVal(ShidToPatioLocalSpace.BYTE_OFFSET, PatioTypes.INT);
+ var offset = offsetResult.value();
+ context = offsetResult.context();
+
+ context = context.addInstruction(new PatioByteOffsetOf(offset, layout, childrenOffset, source));
+
+ return context.addInstruction(new PatioCall(
+ Optional.of(destination),
+ ALLOC.name(),
+ List.of(size, offset, new PatioInt(childrenCount)),
source
));
}
diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioLocalSpace.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioLocalSpace.java
index d5d592ef..cc3c6a59 100644
--- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioLocalSpace.java
+++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioLocalSpace.java
@@ -2,6 +2,8 @@ package org.shedlang.compiler.shidtopatio;
public enum ShidToPatioLocalSpace {
BINARY_OP_RESULT("binaryOpResult"),
+ BYTE_OFFSET("byteOffset"),
+ BYTE_SIZE("byteSize"),
CALL_ARG_POSITIONAL("callArgPositional"),
CALL_ARG_NAMED("callArgNamed"),
CALL_FUNCTION_POINTER("callFunctionPointer"),