aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-09-23 17:04:31 +0100
committerMichael Williamson <mike@zwobble.org>2023-09-26 20:01:32 +0100
commit537aefec06307139899e3f2f2cd74842a3af6345 (patch)
tree3af4c0729cf61225b4d4da10109a8eda497b83d3
parentac51f8470a58caeb611a4c55bb19c1c2281c4960 (diff)
Generate code for zero-element tuple without alloc
-rw-r--r--snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenTupleCreateTests]/[method:canGenerateCodeForTupleCreate(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected2
-rw-r--r--snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenTupleCreateTests]/[method:zeroElementTupleIsCreatedWithoutAlloc(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected14
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenTuples.java25
-rw-r--r--src/test/java/org/shedlang/compiler/patio/PatioCodeGenTupleCreateTests.java10
4 files changed, 43 insertions, 8 deletions
diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenTupleCreateTests]/[method:canGenerateCodeForTupleCreate(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenTupleCreateTests]/[method:canGenerateCodeForTupleCreate(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
index cd70b0de..25f64be8 100644
--- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenTupleCreateTests]/[method:canGenerateCodeForTupleCreate(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
+++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenTupleCreateTests]/[method:canGenerateCodeForTupleCreate(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
@@ -5,8 +5,8 @@ Locals:
* (val "tupleElement__2__0" int)
* (val "tupleElement__1__0" int)
* (val "tupleElement__0__0" int)
- * (val "tuplePointer__0" data-ptr)
* (val "tuple__0" int)
+ * (val "tuplePointer__0" data-ptr)
Instructions:
diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenTupleCreateTests]/[method:zeroElementTupleIsCreatedWithoutAlloc(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenTupleCreateTests]/[method:zeroElementTupleIsCreatedWithoutAlloc(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
new file mode 100644
index 00000000..e900dcac
--- /dev/null
+++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenTupleCreateTests]/[method:zeroElementTupleIsCreatedWithoutAlloc(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
@@ -0,0 +1,14 @@
+Compilation unit elements:
+
+
+Locals:
+ * (val "tuple__0" int)
+
+Instructions:
+
+(
+ (local-store (local "tuple__0") (int 0))
+)
+
+Temporary stack:
+ * tuple__0
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenTuples.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenTuples.java
index b7198683..c3e1c964 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenTuples.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenTuples.java
@@ -1,6 +1,7 @@
package org.shedlang.compiler.patio;
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.operands.PatioOperand;
import org.shedlang.compiler.patio.ast.types.PatioDataLayout;
@@ -20,6 +21,23 @@ public class PatioCodeGenTuples {
Source source,
PatioCodeGenContext context
) {
+ var tupleResult = context.allocateVal(
+ PatioCodeGenLocalSpace.TUPLE,
+ PatioCodeGenTypes.VALUE_TYPE
+ );
+ var tuple = tupleResult.value();
+ context = tupleResult.context();
+
+ if (elements.size() == 0) {
+ context = context.addInstruction(new PatioLocalStore(
+ tuple,
+ new PatioInt(0),
+ source
+ ));
+
+ return context.toResult(tuple);
+ }
+
var tuplePointerResult = context.allocateVal(
PatioCodeGenLocalSpace.TUPLE_POINTER,
PatioTypes.DATA_POINTER
@@ -47,13 +65,6 @@ public class PatioCodeGenTuples {
));
}
- var tupleResult = context.allocateVal(
- PatioCodeGenLocalSpace.TUPLE,
- PatioCodeGenTypes.VALUE_TYPE
- );
- var tuple = tupleResult.value();
- context = tupleResult.context();
-
context = context.addInstruction(new PatioDataPtrToInt(
tuple,
tuplePointer,
diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenTupleCreateTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenTupleCreateTests.java
index b95ca4fb..e489fb37 100644
--- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenTupleCreateTests.java
+++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenTupleCreateTests.java
@@ -26,4 +26,14 @@ public class PatioCodeGenTupleCreateTests {
snapshotter.assertSnapshot(serialize(result));
}
+
+ @Test
+ public void zeroElementTupleIsCreatedWithoutAlloc(Snapshotter snapshotter) throws IOException {
+ var stackIr = Instructions.tupleCreate(0);
+ var context = PatioCodeGenContext.EMPTY;
+
+ var result = PatioCodeGen.generateInstruction(stackIr, context);
+
+ snapshotter.assertSnapshot(serialize(result));
+ }
}