aboutsummaryrefslogtreecommitdiff
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
parent74f4965ab78cfb8e27654b34c263a3a925221e1a (diff)
Add tags when generating Patio code for structs
-rw-r--r--snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenDefineStructTests]/[method:canGenerateCodeForDefineStruct(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected15
-rw-r--r--snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenFieldLoadTests]/[method:canGenerateCodeForStructFieldLoad(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected4
-rw-r--r--snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenSwitchTests]/[method:canGenerateCodeForSwitchWithSingleResult(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected12
-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
7 files changed, 97 insertions, 12 deletions
diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenDefineStructTests]/[method:canGenerateCodeForDefineStruct(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenDefineStructTests]/[method:canGenerateCodeForDefineStruct(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
index c7b77936..a840e5a7 100644
--- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenDefineStructTests]/[method:canGenerateCodeForDefineStruct(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
+++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenDefineStructTests]/[method:canGenerateCodeForDefineStruct(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
@@ -7,19 +7,26 @@ Compilation unit elements:
locals=((param "closurePointer__0" data-ptr) (param "local__isActive" int) (param "local__name" int) (val "structValuePointer__0" data-ptr) (val "structValue__0" int))
return-type=int
body=(
- (alloc-heap (local "structValuePointer__0") (struct (int int)))
+ (alloc-heap (local "structValuePointer__0") (struct (int (struct (int int)))))
(
memory-store
pointer=(local "structValuePointer__0")
- layout=(struct (int int))
+ layout=(struct (int (struct (int int))))
offset=(0)
+ value=(int 0)
+ )
+ (
+ memory-store
+ pointer=(local "structValuePointer__0")
+ layout=(struct (int (struct (int int))))
+ offset=(1 0)
value=(local "local__name")
)
(
memory-store
pointer=(local "structValuePointer__0")
- layout=(struct (int int))
- offset=(1)
+ layout=(struct (int (struct (int int))))
+ offset=(1 1)
value=(local "local__isActive")
)
(data-ptr-to-int (local "structValue__0") (local "structValuePointer__0"))
diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenFieldLoadTests]/[method:canGenerateCodeForStructFieldLoad(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenFieldLoadTests]/[method:canGenerateCodeForStructFieldLoad(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
index 4ceff319..17dc142c 100644
--- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenFieldLoadTests]/[method:canGenerateCodeForStructFieldLoad(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
+++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenFieldLoadTests]/[method:canGenerateCodeForStructFieldLoad(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
@@ -13,8 +13,8 @@ Instructions:
memory-load
(local "tmp__id__0")
pointer=(local "structValuePointer__0")
- layout=(struct (int int int))
- offset=(1)
+ layout=(struct (int (struct (int int int))))
+ offset=(1 1)
)
)
diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenSwitchTests]/[method:canGenerateCodeForSwitchWithSingleResult(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenSwitchTests]/[method:canGenerateCodeForSwitchWithSingleResult(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
index af600e66..ce72c24e 100644
--- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenSwitchTests]/[method:canGenerateCodeForSwitchWithSingleResult(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
+++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenSwitchTests]/[method:canGenerateCodeForSwitchWithSingleResult(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
@@ -3,6 +3,8 @@ Compilation unit elements:
Locals:
* (val "tmp__0" int)
+ * (val "switchTag__0" int)
+ * (val "structValuePointer__0" data-ptr)
* (val "tmp__1" int)
* (val "tmp__2" int)
* (val "tmp__3" int)
@@ -13,10 +15,18 @@ Locals:
Instructions:
(
+ (int-to-data-ptr (local "structValuePointer__0") (local "condition"))
+ (
+ memory-load
+ (local "switchTag__0")
+ pointer=(local "structValuePointer__0")
+ layout=(struct (int))
+ offset=(0)
+ )
(
switch
((local "tmp__0"))
- (local "condition")
+ (local "switchTag__0")
(
(
case
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;
}