aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-10-08 16:17:26 +0100
committerMichael Williamson <mike@zwobble.org>2023-10-08 16:17:26 +0100
commita42befd9b422fe6f16c76b67e07988b36a2ff7c7 (patch)
tree0c3aea3e499944188d5416e5c177147459d44529 /src/main/java
parent74f4965ab78cfb8e27654b34c263a3a925221e1a (diff)
Add tags when generating Patio code for structs
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java16
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java2
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenStructs.java59
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenTypes.java1
4 files changed, 73 insertions, 5 deletions
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
index 66c2faf4..faa8007e 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
@@ -524,6 +524,20 @@ public class PatioCodeGen {
var value = valueResult.value();
context = valueResult.context();
+ var tagResult = context.allocateVal(
+ PatioCodeGenLocalSpace.SWITCH_TAG,
+ PatioCodeGenTypes.TAG_TYPE
+ );
+ var tag = tagResult.value();
+ context = tagResult.context();
+
+ context = PatioCodeGenStructs.tagLoad(
+ tag,
+ value,
+ switch_.source(),
+ context
+ );
+
var cases = new ArrayList<PatioSwitch.Case>();
for (var stackIrCase : switch_.cases()) {
@@ -545,7 +559,7 @@ public class PatioCodeGen {
context = context.addInstruction(new PatioSwitch(
List.of(target),
- value,
+ tag,
cases,
new PatioBlock(
List.of(
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java
index 6a5f40e1..cbe6c9b5 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java
@@ -14,9 +14,11 @@ public enum PatioCodeGenLocalSpace {
LOCAL("local"),
RECEIVER("receiver"),
STRING("string"),
+ STRUCT_TAG_POINTER("structTagPointer"),
STRUCT_TYPE("structType"),
STRUCT_VALUE("structValue"),
STRUCT_VALUE_POINTER("structValuePointer"),
+ SWITCH_TAG("switchTag"),
TEMPORARY("tmp"),
TUPLE("tuple"),
TUPLE_ELEMENT("tupleElement"),
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenStructs.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenStructs.java
index e5f645f2..0f0b2920 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenStructs.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenStructs.java
@@ -2,6 +2,7 @@ package org.shedlang.compiler.patio;
import org.shedlang.compiler.ast.typed.FreeVariables;
import org.shedlang.compiler.patio.ast.instructions.*;
+import org.shedlang.compiler.patio.ast.operands.PatioInt;
import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
import org.shedlang.compiler.patio.ast.types.PatioDataLayout;
import org.shedlang.compiler.patio.ast.types.PatioDataLayouts;
@@ -49,6 +50,15 @@ public class PatioCodeGenStructs {
source
));
+ bodyContext = bodyContext
+ .addInstruction(new PatioMemoryStore(
+ valuePointer,
+ structDataLayout,
+ List.of(0),
+ new PatioInt(context.tag(type)),
+ source
+ ));
+
for (var fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++) {
var field = fields.get(fieldIndex);
@@ -56,7 +66,7 @@ public class PatioCodeGenStructs {
.addInstruction(new PatioMemoryStore(
valuePointer,
structDataLayout,
- List.of(fieldIndex),
+ List.of(1, fieldIndex),
bodyContext.stackIrLocalNameToLocal(field.name()),
source
));
@@ -114,22 +124,63 @@ public class PatioCodeGenStructs {
target,
valuePointer,
structDataLayout,
- List.of(fieldIndex),
+ List.of(1, fieldIndex),
source
));
return context;
}
+ public static PatioCodeGenContext tagLoad(
+ PatioLocalRef target,
+ PatioLocalRef receiver,
+ Source source,
+ PatioCodeGenContext context
+ ) {
+ var valuePointerResult = context.allocateVal(
+ PatioCodeGenLocalSpace.STRUCT_VALUE_POINTER,
+ PatioTypes.DATA_POINTER
+ );
+ var valuePointer = valuePointerResult.value();
+ context = valuePointerResult.context();
+
+ context = context.addInstruction(new PatioIntToDataPtr(
+ valuePointer,
+ receiver,
+ source
+ ));
+
+ var structDataLayout = unknownStructDataLayout();
+
+ context = context.addInstruction(new PatioMemoryLoad(
+ target,
+ valuePointer,
+ structDataLayout,
+ List.of(0),
+ source
+ ));
+
+ return context;
+ }
+
+ private static PatioDataLayout unknownStructDataLayout() {
+ return PatioDataLayouts.struct(List.of(
+ PatioDataLayouts.INT
+ ));
+ }
+
private static PatioDataLayout structDataLayout(
StructType type,
PatioCodeGenContext context
) {
var structTypeInfo = context.structTypeInfo(type);
- return PatioDataLayouts.struct(Lists.repeat(
+ return PatioDataLayouts.struct(List.of(
PatioDataLayouts.INT,
- structTypeInfo.fieldCount()
+ PatioDataLayouts.struct(Lists.repeat(
+ PatioDataLayouts.INT,
+ structTypeInfo.fieldCount()
+ ))
));
}
}
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenTypes.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenTypes.java
index d399fa08..26d164fe 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenTypes.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenTypes.java
@@ -6,5 +6,6 @@ import org.shedlang.compiler.patio.ast.types.PatioTypes;
public class PatioCodeGenTypes {
private PatioCodeGenTypes() {}
+ public static final PatioType TAG_TYPE = PatioTypes.INT;
public static final PatioType VALUE_TYPE = PatioTypes.INT;
}