From 537aefec06307139899e3f2f2cd74842a3af6345 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 23 Sep 2023 17:04:31 +0100 Subject: Generate code for zero-element tuple without alloc --- .../compiler/patio/PatioCodeGenTuples.java | 25 ++++++++++++++++------ .../patio/PatioCodeGenTupleCreateTests.java | 10 +++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) (limited to 'src') 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)); + } } -- cgit v1.2.3