diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-19 23:45:53 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-19 23:45:53 +0100 |
| commit | 119f7541e02913903469d6b49da6b21649047be1 (patch) | |
| tree | 2324c9731b6bdc70c9a1ff96ac6686b75630139c /src/main/java | |
| parent | 24625ebf4de88cfe3b773b5b4cdb32e212fdd66b (diff) | |
Use locals instead of temporary stack in Patio
Diffstat (limited to 'src/main/java')
19 files changed, 179 insertions, 113 deletions
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java index d4a31c3f..fe74e70b 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java @@ -1,6 +1,7 @@ package org.shedlang.compiler.patio; import org.shedlang.compiler.patio.instructions.*; +import org.shedlang.compiler.sources.Source; import org.shedlang.compiler.stackir.instructions.*; import java.util.ArrayList; @@ -27,8 +28,9 @@ public class PatioCodeGen { var arg = argResult.value(); context = argResult.context(); - context = context.addInstruction( - new PatioLocalStore(arg, stackIrInstruction.source()) + context = context.temporaryPopIntoLocal( + arg, + stackIrInstruction.source() ); namedArgsByName.add(Map.entry(argName, arg)); @@ -43,8 +45,9 @@ public class PatioCodeGen { var arg = argResult.value(); context = argResult.context(); - context = context.addInstruction( - new PatioLocalStore(arg, stackIrInstruction.source()) + context = context.temporaryPopIntoLocal( + arg, + stackIrInstruction.source() ); positionalArgs.add(arg); @@ -55,32 +58,34 @@ public class PatioCodeGen { var receiver = receiverResult.value(); context = receiverResult.context(); - context = context.addInstruction(new PatioLocalStore( + context = context.temporaryPopIntoLocal( receiver, stackIrInstruction.source() - )); + ); + + var targetResult = context.allocateLocal(PatioCodeGenLocalSpace.TEMPORARY); + var target = targetResult.value(); + context = targetResult.context(); yield PatioCodeGenClosures.call( + target, receiver, positionalArgs, namedArgsByName, stackIrInstruction.source(), context - ); + ).temporaryPush(target); } case DiscardInstruction discard -> - context.addInstruction(new PatioDiscard(discard.source())); + context.temporaryPop().context(); case LocalLoadInstruction localLoad -> { var localResult = context.stackIrLocalNameToLocal(localLoad.name()); var local = localResult.value(); context = localResult.context(); - yield context.addInstruction(new PatioLocalLoad( - local, - localLoad.source() - )); + yield context.temporaryPush(local); } case LocalStoreInstruction localStore -> { @@ -88,26 +93,38 @@ public class PatioCodeGen { var local = localResult.value(); context = localResult.context(); - yield context.addInstruction(new PatioLocalStore( - local, - localStore.source() - )); + yield context.temporaryPopIntoLocal(local, localStore.source()); } case PushBoolInstruction pushBool -> - context.addInstruction(new PatioPushInt(pushBool.value() ? 1 : 0, pushBool.source())); + pushInt(pushBool.value() ? 1 : 0, pushBool.source(), context); case PushIntInstruction pushInt -> - context.addInstruction(new PatioPushInt(pushInt.value().intValueExact(), pushInt.source())); + pushInt(pushInt.value().intValueExact(), pushInt.source(), context); case PushUnitInstruction pushUnit -> - context.addInstruction(new PatioPushInt(0, pushUnit.source())); + pushInt(0, pushUnit.source(), context); + + case ReturnInstruction return_ -> { + var valueResult = context.temporaryPop(); + var value = valueResult.value(); + context = valueResult.context(); - case ReturnInstruction return_ -> - context.addInstruction(new PatioReturn(return_.source())); + yield context.addInstruction(new PatioReturn(value, return_.source())); + } default -> throw new IllegalStateException("Unexpected value: " + stackIrInstruction); }; } + + private static PatioCodeGenContext pushInt(int value, Source source, PatioCodeGenContext context) { + var localResult = context.allocateLocal(PatioCodeGenLocalSpace.TEMPORARY); + var local = localResult.value(); + context = localResult.context(); + + return context + .addInstruction(new PatioInt(local, value, source)) + .temporaryPush(local); + } } diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java index b87a85c2..b2bf66f7 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java @@ -15,6 +15,7 @@ class PatioCodeGenClosures { private static final int FUNCTION_POINTER_OFFSET = 0; static PatioCodeGenContext call( + PatioLocal target, PatioLocal closure, List<PatioLocal> positionalArgs, List<Map.Entry<String, PatioLocal>> namedArgsByName, @@ -25,10 +26,12 @@ class PatioCodeGenClosures { var functionPointer = functionPointerResult.value(); context = functionPointerResult.context(); - context = context - .addInstruction(new PatioLocalLoad(closure, source)) - .addInstruction(new PatioMemoryLoad(FUNCTION_POINTER_OFFSET, source)) - .addInstruction(new PatioLocalStore(functionPointer, source)); + context = context.addInstruction(new PatioMemoryLoad( + functionPointer, + closure, + FUNCTION_POINTER_OFFSET, + source + )); var args = new ArrayList<PatioLocal>(); @@ -42,6 +45,7 @@ class PatioCodeGenClosures { return context .addInstruction(new PatioCallPointer( + target, functionPointer, args, source diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java index c10eced7..1c7a6a77 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java @@ -1,10 +1,13 @@ package org.shedlang.compiler.patio; import org.pcollections.PMap; +import org.pcollections.PStack; import org.pcollections.PVector; import org.shedlang.compiler.backends.llvm.NameAllocator; import org.shedlang.compiler.patio.instructions.PatioInstruction; import org.shedlang.compiler.patio.instructions.PatioLocal; +import org.shedlang.compiler.patio.instructions.PatioLocalCopy; +import org.shedlang.compiler.sources.Source; import org.shedlang.compiler.util.P; import java.util.Optional; @@ -13,18 +16,39 @@ public record PatioCodeGenContext( FunctionContext functionContext ) { public record FunctionContext( + PStack<PatioLocal> temporaryStack, PVector<PatioInstruction> instructions, NameAllocator<PatioCodeGenLocalSpace> localNames, PMap<String, PatioLocal> stackIrNameToLocal ) { public static final FunctionContext EMPTY = new FunctionContext( + P.stack(), P.vector(), NameAllocator.empty(), P.map() ); + public Result<PatioLocal> temporaryPop() { + return new FunctionContext( + this.temporaryStack.minus(0), + this.instructions, + this.localNames, + this.stackIrNameToLocal + ).toResult(this.temporaryStack.get(0)); + } + + public FunctionContext temporaryPush(PatioLocal value) { + return new FunctionContext( + this.temporaryStack.plus(value), + this.instructions, + this.localNames, + this.stackIrNameToLocal + ); + } + public FunctionContext addInstruction(PatioInstruction instruction) { return new FunctionContext( + this.temporaryStack, this.instructions.plus(instruction), this.localNames, this.stackIrNameToLocal @@ -35,6 +59,7 @@ public record PatioCodeGenContext( var result = this.localNames.allocateName(space, name); return new FunctionContext( + this.temporaryStack, this.instructions, result.allocator(), this.stackIrNameToLocal @@ -48,6 +73,7 @@ public record PatioCodeGenContext( var result = this.localNames.allocateFixedName(PatioCodeGenLocalSpace.LOCAL, name); context = new FunctionContext( + this.temporaryStack, this.instructions, result.allocator(), this.stackIrNameToLocal.plus(name, new PatioLocal(result.name())) @@ -68,6 +94,25 @@ public record PatioCodeGenContext( FunctionContext.EMPTY ); + public PatioCodeGenResult<PatioLocal> temporaryPop() { + var result = this.functionContext.temporaryPop(); + + return new PatioCodeGenContext(result.context).toResult(result.value); + } + + public PatioCodeGenContext temporaryPopIntoLocal(PatioLocal target, Source source) { + var result = this.functionContext.temporaryPop(); + + return new PatioCodeGenContext(result.context) + .addInstruction(new PatioLocalCopy(target, result.value, source)); + } + + public PatioCodeGenContext temporaryPush(PatioLocal value) { + var result = this.functionContext.temporaryPush(value); + + return new PatioCodeGenContext(result); + } + public PatioCodeGenContext addInstruction(PatioInstruction instruction) { return new PatioCodeGenContext( this.functionContext.addInstruction(instruction) diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java index 942ae6f3..cad33bdd 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java @@ -5,7 +5,8 @@ public enum PatioCodeGenLocalSpace { CALL_ARG_NAMED("callArgNamed"), CALL_FUNCTION_POINTER("callFunctionPointer"), LOCAL("local"), - RECEIVER("receiver"); + RECEIVER("receiver"), + TEMPORARY("tmp"); private final String name; diff --git a/src/main/java/org/shedlang/compiler/patio/PatioInstructionSerializer.java b/src/main/java/org/shedlang/compiler/patio/PatioInstructionSerializer.java index f9f29d28..8b0be0d3 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioInstructionSerializer.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioInstructionSerializer.java @@ -32,60 +32,70 @@ public class PatioInstructionSerializer { private static Tree instructionToTree(PatioInstruction instruction) { return switch (instruction) { case PatioAlloc alloc -> - treeList(treeIdentifier("alloc"), treeInt(alloc.bytes())); + treeList( + treeIdentifier("alloc"), + localToTree(alloc.target()), + treeInt(alloc.bytes()) + ); case PatioCallPointer callPointer -> treeList( treeIdentifier("call-pointer"), - treeString(callPointer.functionPointer().name()), + localToTree(callPointer.target()), + localToTree(callPointer.functionPointer()), treeList( callPointer.args().stream() - .map(arg -> treeString(arg.name())) + .map(arg -> localToTree(arg)) .toList() ) ); - case PatioDiscard discard -> - treeList(treeIdentifier("discard")); - - case PatioLocalLoad localLoad -> + case PatioInt int_ -> treeList( - treeIdentifier("local-load"), - treeString(localLoad.local().name()) + treeIdentifier("int"), + localToTree(int_.target()), + treeInt(int_.value()) ); - case PatioLocalStore localStore -> + case PatioLocalCopy localCopy -> treeList( - treeIdentifier("local-store"), - treeString(localStore.local().name()) + treeIdentifier("local-copy"), + localToTree(localCopy.target()), + localToTree(localCopy.value()) ); case PatioMemoryLoad memoryLoad -> treeList( treeIdentifier("memory-load"), + localToTree(memoryLoad.target()), + treeNamed("pointer", localToTree(memoryLoad.pointer())), treeNamed("offset", treeInt(memoryLoad.offset())) ); case PatioMemoryStore memoryStore -> treeList( treeIdentifier("memory-store"), - treeNamed("offset", treeInt(memoryStore.offset())) + treeNamed("pointer", localToTree(memoryStore.pointer())), + treeNamed("offset", treeInt(memoryStore.offset())), + treeNamed("value", localToTree(memoryStore.value())) ); - case PatioPushFunctionPointer pushFunctionPointer -> + case PatioFunctionPointer functionPointer -> treeList( - treeIdentifier("push-function-pointer"), - treeString(pushFunctionPointer.name()) + treeIdentifier("function-pointer"), + localToTree(functionPointer.target()), + treeString(functionPointer.name()) ); - case PatioPushInt pushInt -> + case PatioReturn return_ -> treeList( - treeIdentifier("push-int"), - treeInt(pushInt.value()) + treeIdentifier("return"), + localToTree(return_.value()) ); - - case PatioReturn return_ -> - treeList(treeIdentifier("return")); }; } + + private static Tree localToTree(PatioLocal local) { + return treeString(local.name()); + } } diff --git a/src/main/java/org/shedlang/compiler/patio/instructions/PatioAlloc.java b/src/main/java/org/shedlang/compiler/patio/instructions/PatioAlloc.java index c5ea9594..ae5599e0 100644 --- a/src/main/java/org/shedlang/compiler/patio/instructions/PatioAlloc.java +++ b/src/main/java/org/shedlang/compiler/patio/instructions/PatioAlloc.java @@ -3,8 +3,8 @@ package org.shedlang.compiler.patio.instructions; import org.shedlang.compiler.sources.Source; /** - * Allocate the given number of bytes, and push the pointer to the allocated - * memory onto the stack. + * Allocate the given number of bytes, and store the pointer to the allocated + * memory in the target local. */ -public record PatioAlloc(int bytes, Source source) implements PatioInstruction { +public record PatioAlloc(PatioLocal target, int bytes, Source source) implements PatioInstruction { } diff --git a/src/main/java/org/shedlang/compiler/patio/instructions/PatioCallPointer.java b/src/main/java/org/shedlang/compiler/patio/instructions/PatioCallPointer.java index f6d03916..279a0c71 100644 --- a/src/main/java/org/shedlang/compiler/patio/instructions/PatioCallPointer.java +++ b/src/main/java/org/shedlang/compiler/patio/instructions/PatioCallPointer.java @@ -6,9 +6,10 @@ import java.util.List; /** * Call the function pointed to by the function pointer with the given - * arguments. + * arguments, and store the returned value in the target local. */ public record PatioCallPointer( + PatioLocal target, PatioLocal functionPointer, List<PatioLocal> args, Source source diff --git a/src/main/java/org/shedlang/compiler/patio/instructions/PatioDiscard.java b/src/main/java/org/shedlang/compiler/patio/instructions/PatioDiscard.java deleted file mode 100644 index 18cff4c3..00000000 --- a/src/main/java/org/shedlang/compiler/patio/instructions/PatioDiscard.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.shedlang.compiler.patio.instructions; - -import org.shedlang.compiler.sources.Source; - -/** - * Pop a value from the stack and discard it. - */ -public record PatioDiscard(Source source) implements PatioInstruction { -} diff --git a/src/main/java/org/shedlang/compiler/patio/instructions/PatioFunctionPointer.java b/src/main/java/org/shedlang/compiler/patio/instructions/PatioFunctionPointer.java new file mode 100644 index 00000000..2081d3be --- /dev/null +++ b/src/main/java/org/shedlang/compiler/patio/instructions/PatioFunctionPointer.java @@ -0,0 +1,9 @@ +package org.shedlang.compiler.patio.instructions; + +import org.shedlang.compiler.sources.Source; + +/** + * Store a pointer to the function with the given name in the target local. + */ +public record PatioFunctionPointer(PatioLocal target, String name, Source source) implements PatioInstruction { +} diff --git a/src/main/java/org/shedlang/compiler/patio/instructions/PatioInstruction.java b/src/main/java/org/shedlang/compiler/patio/instructions/PatioInstruction.java index efea5694..57d0fb05 100644 --- a/src/main/java/org/shedlang/compiler/patio/instructions/PatioInstruction.java +++ b/src/main/java/org/shedlang/compiler/patio/instructions/PatioInstruction.java @@ -2,6 +2,6 @@ package org.shedlang.compiler.patio.instructions; import org.shedlang.compiler.sources.Source; -public sealed interface PatioInstruction permits PatioAlloc, PatioCallPointer, PatioDiscard, PatioLocalLoad, PatioLocalStore, PatioMemoryLoad, PatioMemoryStore, PatioPushFunctionPointer, PatioPushInt, PatioReturn { +public sealed interface PatioInstruction permits PatioAlloc, PatioCallPointer, PatioFunctionPointer, PatioInt, PatioLocalCopy, PatioMemoryLoad, PatioMemoryStore, PatioReturn { Source source(); } diff --git a/src/main/java/org/shedlang/compiler/patio/instructions/PatioInt.java b/src/main/java/org/shedlang/compiler/patio/instructions/PatioInt.java new file mode 100644 index 00000000..f02c6146 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/patio/instructions/PatioInt.java @@ -0,0 +1,9 @@ +package org.shedlang.compiler.patio.instructions; + +import org.shedlang.compiler.sources.Source; + +/** + * Store the given value in the target local. + */ +public record PatioInt(PatioLocal target, int value, Source source) implements PatioInstruction { +} diff --git a/src/main/java/org/shedlang/compiler/patio/instructions/PatioLocalCopy.java b/src/main/java/org/shedlang/compiler/patio/instructions/PatioLocalCopy.java new file mode 100644 index 00000000..da3d92e2 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/patio/instructions/PatioLocalCopy.java @@ -0,0 +1,13 @@ +package org.shedlang.compiler.patio.instructions; + +import org.shedlang.compiler.sources.Source; + +/** + * Copy the given value into the target local. + */ +public record PatioLocalCopy( + PatioLocal target, + PatioLocal value, + Source source +) implements PatioInstruction { +} diff --git a/src/main/java/org/shedlang/compiler/patio/instructions/PatioLocalLoad.java b/src/main/java/org/shedlang/compiler/patio/instructions/PatioLocalLoad.java deleted file mode 100644 index 63e77277..00000000 --- a/src/main/java/org/shedlang/compiler/patio/instructions/PatioLocalLoad.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.shedlang.compiler.patio.instructions; - -import org.shedlang.compiler.sources.Source; - -/** - * Load the local with the given name and push it onto the stack. - */ -public record PatioLocalLoad(PatioLocal local, Source source) implements PatioInstruction { -} diff --git a/src/main/java/org/shedlang/compiler/patio/instructions/PatioLocalStore.java b/src/main/java/org/shedlang/compiler/patio/instructions/PatioLocalStore.java deleted file mode 100644 index b874144f..00000000 --- a/src/main/java/org/shedlang/compiler/patio/instructions/PatioLocalStore.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.shedlang.compiler.patio.instructions; - -import org.shedlang.compiler.sources.Source; - -/** - * Pop a value from the stack and store it as the local with the given name. - */ -public record PatioLocalStore(PatioLocal local, Source source) implements PatioInstruction { -} diff --git a/src/main/java/org/shedlang/compiler/patio/instructions/PatioMemoryLoad.java b/src/main/java/org/shedlang/compiler/patio/instructions/PatioMemoryLoad.java index 7068fcbe..cab95107 100644 --- a/src/main/java/org/shedlang/compiler/patio/instructions/PatioMemoryLoad.java +++ b/src/main/java/org/shedlang/compiler/patio/instructions/PatioMemoryLoad.java @@ -3,8 +3,13 @@ package org.shedlang.compiler.patio.instructions; import org.shedlang.compiler.sources.Source; /** - * Pop a value from the stack and assume it is a pointer. Load the value at the - * given offset from that pointer and push it onto the stack. + * Load the value at the given offset from the given pointer and store it in the + * target local. */ -public record PatioMemoryLoad(int offset, Source source) implements PatioInstruction { +public record PatioMemoryLoad( + PatioLocal target, + PatioLocal pointer, + int offset, + Source source +) implements PatioInstruction { } diff --git a/src/main/java/org/shedlang/compiler/patio/instructions/PatioMemoryStore.java b/src/main/java/org/shedlang/compiler/patio/instructions/PatioMemoryStore.java index 7d7d4a83..67776381 100644 --- a/src/main/java/org/shedlang/compiler/patio/instructions/PatioMemoryStore.java +++ b/src/main/java/org/shedlang/compiler/patio/instructions/PatioMemoryStore.java @@ -3,15 +3,12 @@ package org.shedlang.compiler.patio.instructions; import org.shedlang.compiler.sources.Source; /** - * Pop a pointer and a value from the stack. Store the value at the given offset - * from that pointer. - * - * <p>Specifically, the values should have been pushed onto the stack in the following order:</p> - * - * <ul> - * <li>The pointer.</li> - * <li>The value to store.</li> - * </ul> + * Store the given value at the given offset from the given pointer. */ -public record PatioMemoryStore(int offset, Source source) implements PatioInstruction { +public record PatioMemoryStore( + PatioLocal pointer, + int offset, + PatioLocal value, + Source source +) implements PatioInstruction { } diff --git a/src/main/java/org/shedlang/compiler/patio/instructions/PatioPushFunctionPointer.java b/src/main/java/org/shedlang/compiler/patio/instructions/PatioPushFunctionPointer.java deleted file mode 100644 index d01bd035..00000000 --- a/src/main/java/org/shedlang/compiler/patio/instructions/PatioPushFunctionPointer.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.shedlang.compiler.patio.instructions; - -import org.shedlang.compiler.sources.Source; - -/** - * Push a pointer to the function with the given name onto the stack. - */ -public record PatioPushFunctionPointer(String name, Source source) implements PatioInstruction { -} diff --git a/src/main/java/org/shedlang/compiler/patio/instructions/PatioPushInt.java b/src/main/java/org/shedlang/compiler/patio/instructions/PatioPushInt.java deleted file mode 100644 index ef455051..00000000 --- a/src/main/java/org/shedlang/compiler/patio/instructions/PatioPushInt.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.shedlang.compiler.patio.instructions; - -import org.shedlang.compiler.sources.Source; - -/** - * Push an integer onto the stack. - */ -public record PatioPushInt(int value, Source source) implements PatioInstruction { -} diff --git a/src/main/java/org/shedlang/compiler/patio/instructions/PatioReturn.java b/src/main/java/org/shedlang/compiler/patio/instructions/PatioReturn.java index cf7e7990..81327631 100644 --- a/src/main/java/org/shedlang/compiler/patio/instructions/PatioReturn.java +++ b/src/main/java/org/shedlang/compiler/patio/instructions/PatioReturn.java @@ -3,7 +3,7 @@ package org.shedlang.compiler.patio.instructions; import org.shedlang.compiler.sources.Source; /** - * Pop a value from the stack and return with that value from the current function. + * Return with the given value from the current function. */ -public record PatioReturn(Source source) implements PatioInstruction { +public record PatioReturn(PatioLocal value, Source source) implements PatioInstruction { } |
