From 429c677f1c22c400064c0f9d69e69b340e61fdb4 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 15 Dec 2023 10:23:08 +0000 Subject: Use common code for Patio block code gen --- .../org/shedlang/compiler/patio/PatioCodeGen.java | 59 +++++++++++++++------- .../compiler/patio/PatioCodeGenContext.java | 19 +++++++ .../patio/ast/instructions/PatioBlock.java | 2 +- 3 files changed, 60 insertions(+), 20 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java index ae645a95..5ddcc13c 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java @@ -102,7 +102,7 @@ public class PatioCodeGen { context = PatioCodeGenNamespaces.define( shidNamespace.namespaceName(), initContext -> - generateBlockInstructions(shidNamespace.body(), initContext), + generateBlockNoResults(shidNamespace.body(), initContext), context ); @@ -126,8 +126,18 @@ public class PatioCodeGen { PatioCodeGenContext context ) { return switch (shidInstruction) { - case ShidBlock block -> { - context = generateBlockInstructions(block, context); + case ShidBlock shidBlock -> { + var patioBlockResult = generateBlock(shidBlock, context); + var patioBlock = patioBlockResult.value(); + context = patioBlockResult.context(); + + for (var instruction : patioBlock.body()) { + context = context.addInstruction(instruction); + } + + for (var result : patioBlock.results()) { + context = context.temporaryPush(result); + } yield context; } @@ -231,7 +241,7 @@ public class PatioCodeGen { Function generateBody = functionContext -> { // TODO: explicitly store locals on a function? - return generateBlockInstructions(defineFunction.body(), functionContext); + return generateBlockNoResults(defineFunction.body(), functionContext); }; context = switch (defineFunction.variant()) { @@ -381,11 +391,11 @@ public class PatioCodeGen { targets.add(target); } - var trueBodyResult = generateBlock(if_.resultCount(), if_.trueBody(), context); + var trueBodyResult = generateBlock(if_.trueBody(), context); var trueBody = trueBodyResult.value(); context = trueBodyResult.context(); - var falseBodyResult = generateBlock(if_.resultCount(), if_.falseBody(), context); + var falseBodyResult = generateBlock(if_.falseBody(), context); var falseBody = falseBodyResult.value(); context = falseBodyResult.context(); @@ -483,7 +493,7 @@ public class PatioCodeGen { case ShidLoop loop -> { context = context.enterBlock(); - context = generateBlockInstructions(loop.body(), context); + context = generateBlockNoResults(loop.body(), context); var bodyInstructionsResult = context.exitBlock(); var bodyInstructions = bodyInstructionsResult.value(); @@ -625,7 +635,6 @@ public class PatioCodeGen { for (var shidCase : switch_.cases()) { var bodyResult = generateBlock( - 1, shidCase.body(), context ); @@ -681,7 +690,7 @@ public class PatioCodeGen { try_.handledEffect(), handlerMethodsByName, bodyContext -> - generateBlock(1, try_.body(), bodyContext), + generateBlock(try_.body(), bodyContext), try_.source(), context ); @@ -844,25 +853,37 @@ public class PatioCodeGen { ).temporaryPush(target); } + private static PatioCodeGenContext generateBlockNoResults( + ShidBlock shidBlock, + PatioCodeGenContext context + ) { + // TODO: just use a block in Patio instructions even where there are no targets? + var blockResult = generateBlock(shidBlock, context); + var block = blockResult.value(); + context = blockResult.context(); + + if (!block.results().isEmpty()) { + // TODO: better error + throw new RuntimeException("block has results"); + } + + for (var instruction : block.body()) { + context = context.addInstruction(instruction); + } + return context; + } private static PatioCodeGenResult generateBlock( - int resultCount, ShidBlock shidBlock, PatioCodeGenContext context ) { context = context.enterBlock(); context = generateBlockInstructions(shidBlock, context); - var targets = new ArrayList(); - for (var i = 0; i < resultCount; i++) { - var targetResult = context.temporaryPop(); - var target = targetResult.value(); - context = targetResult.context(); - - targets.add(target); - } - Collections.reverse(targets); + var targetsResult = context.temporaryPopAll(); + var targets = targetsResult.value().reversed(); + context = targetsResult.context(); var instructionsResult = context.exitBlock(); var instructions = instructionsResult.value(); diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java index f2e881e9..c123b34f 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java @@ -210,6 +210,18 @@ public record PatioCodeGenContext( ).toResult(this.temporaryStack.get(0)); } + @CheckReturnValue + public Result> temporaryPopAll() { + return new FunctionContext( + P.stack(), + this.instructions, + this.localsByName, + this.shidNameToLocal, + this.waitingForShidLocal, + this.stashedEffectHandler + ).toResult(this.temporaryStack); + } + @CheckReturnValue public FunctionContext temporaryPush(PatioOperand value) { return new FunctionContext( @@ -488,6 +500,13 @@ public record PatioCodeGenContext( .addInstruction(new PatioLocalStore(target, result.value, source)); } + @CheckReturnValue + public PatioCodeGenResult> temporaryPopAll() { + var result = this.functionContext.temporaryPopAll(); + + return withFunctionContextResult(result); + } + @CheckReturnValue public PatioCodeGenContext temporaryPush(PatioOperand value) { var result = this.functionContext.temporaryPush(value); diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioBlock.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioBlock.java index 50c59fbf..21c2c218 100644 --- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioBlock.java +++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioBlock.java @@ -8,7 +8,7 @@ import java.util.List; public record PatioBlock( List body, - List results, + List results, Source source ) { public PatioBlock addInstruction(PatioInstruction instruction) { -- cgit v1.2.3