diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-12-15 10:23:08 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-12-15 10:23:08 +0000 |
| commit | 429c677f1c22c400064c0f9d69e69b340e61fdb4 (patch) | |
| tree | bc067dfeda5a8f21c015d91170047053855525ad /src/main/java | |
| parent | e48644920ff6b5e785be9c03665b781629f5995d (diff) | |
Use common code for Patio block code gen
Diffstat (limited to 'src/main/java')
3 files changed, 60 insertions, 20 deletions
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<PatioCodeGenContext, PatioCodeGenContext> 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<PatioBlock> generateBlock( - int resultCount, ShidBlock shidBlock, PatioCodeGenContext context ) { context = context.enterBlock(); context = generateBlockInstructions(shidBlock, context); - var targets = new ArrayList<PatioOperand>(); - 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 @@ -211,6 +211,18 @@ public record PatioCodeGenContext( } @CheckReturnValue + public Result<List<PatioOperand>> 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( this.temporaryStack.plus(value), @@ -489,6 +501,13 @@ public record PatioCodeGenContext( } @CheckReturnValue + public PatioCodeGenResult<List<PatioOperand>> 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<PatioInstruction> body, - List<PatioOperand> results, + List<? extends PatioOperand> results, Source source ) { public PatioBlock addInstruction(PatioInstruction instruction) { |
