aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-20 14:02:20 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-20 14:02:20 +0100
commit1d947a749a5772b25c30cad1c8e3f8ec6d7b22ca (patch)
tree15ed00f03a7cd5f279cbc8fb58721c650d367b7e /src/main/java
parent2c9f1042aaf2b1c6c666af2ecd853d076484cd4b (diff)
Include data layout when loading from and storing to memory
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java9
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java33
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryLoad.java2
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryStore.java2
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/types/PatioDataLayout.java4
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/types/PatioStruct.java6
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/types/PatioTypes.java6
7 files changed, 56 insertions, 6 deletions
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java
index 048d235d..37cdaa30 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java
@@ -4,6 +4,8 @@ import org.shedlang.compiler.ast.typed.FreeVariables;
import org.shedlang.compiler.patio.ast.instructions.PatioCallPointer;
import org.shedlang.compiler.patio.ast.PatioLocalRef;
import org.shedlang.compiler.patio.ast.instructions.PatioMemoryLoad;
+import org.shedlang.compiler.patio.ast.types.PatioDataLayout;
+import org.shedlang.compiler.patio.ast.types.PatioStruct;
import org.shedlang.compiler.patio.ast.types.PatioType;
import org.shedlang.compiler.patio.ast.types.PatioTypes;
import org.shedlang.compiler.sources.Source;
@@ -23,6 +25,12 @@ class PatioCodeGenClosures {
private static final int FUNCTION_POINTER_OFFSET = 0;
+ private static PatioDataLayout closureDataLayout() {
+ return new PatioStruct(List.of(
+ PatioTypes.FUNCTION_POINTER
+ ));
+ }
+
public static PatioCodeGenContext create(
String name,
List<DefineFunctionInstruction.Param> positionalParams,
@@ -64,6 +72,7 @@ class PatioCodeGenClosures {
context = context.addInstruction(new PatioMemoryLoad(
functionPointer,
closure,
+ closureDataLayout(),
FUNCTION_POINTER_OFFSET,
source
));
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 27dc2045..9447ab14 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java
@@ -1,10 +1,7 @@
package org.shedlang.compiler.patio.ast;
import org.shedlang.compiler.patio.ast.instructions.*;
-import org.shedlang.compiler.patio.ast.types.PatioDataPointerType;
-import org.shedlang.compiler.patio.ast.types.PatioFunctionPointerType;
-import org.shedlang.compiler.patio.ast.types.PatioIntType;
-import org.shedlang.compiler.patio.ast.types.PatioType;
+import org.shedlang.compiler.patio.ast.types.*;
import org.shedlang.compiler.treetext.Tree;
import java.util.List;
@@ -23,6 +20,14 @@ public class PatioSerializer {
serializeTree(tree, builder, 0);
}
+ public static void serializeDataLayout(
+ PatioDataLayout layout,
+ StringBuilder builder
+ ) {
+ var tree = dataLayoutToTree(layout);
+ serializeTree(tree, builder, 0);
+ }
+
public static void serializeInstructions(List<PatioInstruction> instructions, StringBuilder builder) {
var tree = instructionsToTree(instructions);
serializeTree(tree, builder, 0);
@@ -98,17 +103,19 @@ public class PatioSerializer {
);
case PatioMemoryLoad memoryLoad ->
- treeList(
+ treeListIndented(
treeIdentifier("memory-load"),
localRefToTree(memoryLoad.target()),
treeNamed("pointer", localRefToTree(memoryLoad.pointer())),
+ treeNamed("layout", dataLayoutToTree(memoryLoad.layout())),
treeNamed("offset", treeInt(memoryLoad.offset()))
);
case PatioMemoryStore memoryStore ->
- treeList(
+ treeListIndented(
treeIdentifier("memory-store"),
treeNamed("pointer", localRefToTree(memoryStore.pointer())),
+ treeNamed("layout", dataLayoutToTree(memoryStore.layout())),
treeNamed("offset", treeInt(memoryStore.offset())),
treeNamed("value", localRefToTree(memoryStore.value()))
);
@@ -145,6 +152,20 @@ public class PatioSerializer {
};
}
+ private static Tree dataLayoutToTree(PatioDataLayout layout) {
+ return switch (layout) {
+ case PatioStruct struct ->
+ treeList(
+ treeIdentifier("struct"),
+ treeList(
+ struct.elements().stream()
+ .map(element -> typeToTree(element))
+ .toList()
+ )
+ );
+ };
+ }
+
private static Tree localRefToTree(PatioLocalRef local) {
return treeString(local.name());
}
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryLoad.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryLoad.java
index b11cb114..430296ee 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryLoad.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryLoad.java
@@ -1,6 +1,7 @@
package org.shedlang.compiler.patio.ast.instructions;
import org.shedlang.compiler.patio.ast.PatioLocalRef;
+import org.shedlang.compiler.patio.ast.types.PatioDataLayout;
import org.shedlang.compiler.sources.Source;
/**
@@ -10,6 +11,7 @@ import org.shedlang.compiler.sources.Source;
public record PatioMemoryLoad(
PatioLocalRef target,
PatioLocalRef pointer,
+ PatioDataLayout layout,
int offset,
Source source
) implements PatioInstruction {
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryStore.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryStore.java
index 79e2ebae..b32899db 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryStore.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryStore.java
@@ -1,6 +1,7 @@
package org.shedlang.compiler.patio.ast.instructions;
import org.shedlang.compiler.patio.ast.PatioLocalRef;
+import org.shedlang.compiler.patio.ast.types.PatioDataLayout;
import org.shedlang.compiler.sources.Source;
/**
@@ -8,6 +9,7 @@ import org.shedlang.compiler.sources.Source;
*/
public record PatioMemoryStore(
PatioLocalRef pointer,
+ PatioDataLayout layout,
int offset,
PatioLocalRef value,
Source source
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/types/PatioDataLayout.java b/src/main/java/org/shedlang/compiler/patio/ast/types/PatioDataLayout.java
new file mode 100644
index 00000000..ac7d3594
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/patio/ast/types/PatioDataLayout.java
@@ -0,0 +1,4 @@
+package org.shedlang.compiler.patio.ast.types;
+
+public sealed interface PatioDataLayout permits PatioStruct {
+}
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/types/PatioStruct.java b/src/main/java/org/shedlang/compiler/patio/ast/types/PatioStruct.java
new file mode 100644
index 00000000..065fc026
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/patio/ast/types/PatioStruct.java
@@ -0,0 +1,6 @@
+package org.shedlang.compiler.patio.ast.types;
+
+import java.util.List;
+
+public record PatioStruct(List<PatioType> elements) implements PatioDataLayout {
+}
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/types/PatioTypes.java b/src/main/java/org/shedlang/compiler/patio/ast/types/PatioTypes.java
index 804bf894..7ecc371a 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/types/PatioTypes.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/types/PatioTypes.java
@@ -1,5 +1,7 @@
package org.shedlang.compiler.patio.ast.types;
+import java.util.List;
+
public class PatioTypes {
private PatioTypes() {
}
@@ -7,4 +9,8 @@ public class PatioTypes {
public static final PatioDataPointerType DATA_POINTER = PatioDataPointerType.INSTANCE;
public static final PatioFunctionPointerType FUNCTION_POINTER = PatioFunctionPointerType.INSTANCE;
public static final PatioIntType INT = PatioIntType.INSTANCE;
+
+ public static PatioDataLayout struct(List<PatioType> elements) {
+ return new PatioStruct(elements);
+ }
}