diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-19 22:32:31 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-19 22:32:31 +0100 |
| commit | d0a9c780ea36a0ff0b70529c1c6ab86c61a5eb0f (patch) | |
| tree | a191ab3f0e8cccc29da7ac8d6f49b8ecf2228c89 /src/main/java | |
| parent | 41008c20a7f57dbe72487aec02727edf5e624d4a (diff) | |
Refer to locals in call-pointer Patio instruction
Diffstat (limited to 'src/main/java')
10 files changed, 54 insertions, 41 deletions
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java index 6d07971f..1419402a 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java @@ -28,7 +28,7 @@ public class PatioCodeGen { context = argResult.context(); context = context.addInstruction( - new PatioLocalStore(arg.name(), stackIrInstruction.source()) + new PatioLocalStore(arg, stackIrInstruction.source()) ); namedArgsByName.add(Map.entry(argName, arg)); @@ -44,7 +44,7 @@ public class PatioCodeGen { context = argResult.context(); context = context.addInstruction( - new PatioLocalStore(arg.name(), stackIrInstruction.source()) + new PatioLocalStore(arg, stackIrInstruction.source()) ); positionalArgs.add(arg); @@ -56,7 +56,7 @@ public class PatioCodeGen { context = receiverResult.context(); context = context.addInstruction(new PatioLocalStore( - receiver.name(), + receiver, stackIrInstruction.source() )); @@ -73,10 +73,18 @@ public class PatioCodeGen { context.addInstruction(new PatioDiscard(discard.source())); case LocalLoadInstruction localLoad -> - context.addInstruction(new PatioLocalLoad(localLoad.name(), localLoad.source())); + context.addInstruction(new PatioLocalLoad( + // TODO: allocate local name + new PatioLocal(localLoad.name()), + localLoad.source() + )); case LocalStoreInstruction localStore -> - context.addInstruction(new PatioLocalStore(localStore.name(), localStore.source())); + // TODO: allocate local name + context.addInstruction(new PatioLocalStore( + new PatioLocal(localStore.name()), + localStore.source() + )); case PushBoolInstruction pushBool -> context.addInstruction(new PatioPushInt(pushBool.value() ? 1 : 0, pushBool.source())); diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java index 0cd31616..6e92d076 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java @@ -1,10 +1,9 @@ package org.shedlang.compiler.patio; -import org.shedlang.compiler.patio.instructions.PatioCallPointer; -import org.shedlang.compiler.patio.instructions.PatioLocalLoad; -import org.shedlang.compiler.patio.instructions.PatioMemoryLoad; +import org.shedlang.compiler.patio.instructions.*; import org.shedlang.compiler.sources.Source; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -22,26 +21,28 @@ class PatioCodeGenClosures { Source source, PatioCodeGenContext context ) { - // TODO: WASM expects the receiver to be pushed last, but we push it first. - // Do we change our instruction set to match WASM? Or have a call - // mechanism less reliant on stack order? + var functionPointerResult = context.allocateLocal(PatioCodeGenLocalSpace.CALL_FUNCTION_POINTER); + var functionPointer = functionPointerResult.value(); + context = functionPointerResult.context(); + context = context - .addInstruction(new PatioLocalLoad(closure.name(), source)) - .addInstruction(new PatioMemoryLoad(FUNCTION_POINTER_OFFSET, source)); + .addInstruction(new PatioLocalLoad(closure, source)) + .addInstruction(new PatioMemoryLoad(FUNCTION_POINTER_OFFSET, source)) + .addInstruction(new PatioLocalStore(functionPointer, source)); - context = context.addInstruction(new PatioLocalLoad(closure.name(), source)); + var args = new ArrayList<PatioLocal>(); - for (var positionalArg : positionalArgs) { - context = context.addInstruction(new PatioLocalLoad(positionalArg.name(), source)); - } + args.addAll(positionalArgs); for (var namedArg : sortedBy(namedArgsByName, x -> x.getKey())) { - context = context.addInstruction(new PatioLocalLoad(namedArg.getValue().name(), source)); + args.add(namedArg.getValue()); } - var argCount = positionalArgs.size() + namedArgsByName.size(); - return context - .addInstruction(new PatioCallPointer(argCount, source)); + .addInstruction(new PatioCallPointer( + 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 7ac7b263..471ca600 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java @@ -3,6 +3,7 @@ package org.shedlang.compiler.patio; 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.util.P; import java.util.Optional; diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java index dfbcf4ee..04a56095 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java @@ -3,6 +3,7 @@ package org.shedlang.compiler.patio; public enum PatioCodeGenLocalSpace { CALL_ARG_POSITIONAL("callArgPositional"), CALL_ARG_NAMED("callArgNamed"), + CALL_FUNCTION_POINTER("callFunctionPointer"), RECEIVER("receiver");; 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 4ebb08d0..f9f29d28 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioInstructionSerializer.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioInstructionSerializer.java @@ -37,7 +37,12 @@ public class PatioInstructionSerializer { case PatioCallPointer callPointer -> treeList( treeIdentifier("call-pointer"), - treeNamed("arg-count", treeInt(callPointer.argCount())) + treeString(callPointer.functionPointer().name()), + treeList( + callPointer.args().stream() + .map(arg -> treeString(arg.name())) + .toList() + ) ); case PatioDiscard discard -> @@ -46,13 +51,13 @@ public class PatioInstructionSerializer { case PatioLocalLoad localLoad -> treeList( treeIdentifier("local-load"), - treeString(localLoad.name()) + treeString(localLoad.local().name()) ); case PatioLocalStore localStore -> treeList( treeIdentifier("local-store"), - treeString(localStore.name()) + treeString(localStore.local().name()) ); case PatioMemoryLoad memoryLoad -> diff --git a/src/main/java/org/shedlang/compiler/patio/PatioLocal.java b/src/main/java/org/shedlang/compiler/patio/PatioLocal.java deleted file mode 100644 index bdd1a7ab..00000000 --- a/src/main/java/org/shedlang/compiler/patio/PatioLocal.java +++ /dev/null @@ -1,4 +0,0 @@ -package org.shedlang.compiler.patio; - -public record PatioLocal(String name) { -} 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 07d3aa43..f6d03916 100644 --- a/src/main/java/org/shedlang/compiler/patio/instructions/PatioCallPointer.java +++ b/src/main/java/org/shedlang/compiler/patio/instructions/PatioCallPointer.java @@ -2,18 +2,15 @@ package org.shedlang.compiler.patio.instructions; import org.shedlang.compiler.sources.Source; +import java.util.List; + /** - * Pop a receiver and arguments from the stack and call the receiver with those + * Call the function pointed to by the function pointer with the given * arguments. - * - * <p>Specifically, the values should have been pushed onto the stack in the - * following order:</p> - * - * <ul> - * <li>The receiver i.e. a pointer to the function to be called.</li> - * <li>The arguments, from left to right.</li> - * </ul> - * */ -public record PatioCallPointer(int argCount, Source source) implements PatioInstruction { +public record PatioCallPointer( + PatioLocal functionPointer, + List<PatioLocal> args, + Source source +) implements PatioInstruction { } diff --git a/src/main/java/org/shedlang/compiler/patio/instructions/PatioLocal.java b/src/main/java/org/shedlang/compiler/patio/instructions/PatioLocal.java new file mode 100644 index 00000000..97590ffe --- /dev/null +++ b/src/main/java/org/shedlang/compiler/patio/instructions/PatioLocal.java @@ -0,0 +1,4 @@ +package org.shedlang.compiler.patio.instructions; + +public record PatioLocal(String name) { +} diff --git a/src/main/java/org/shedlang/compiler/patio/instructions/PatioLocalLoad.java b/src/main/java/org/shedlang/compiler/patio/instructions/PatioLocalLoad.java index 4ec34a12..63e77277 100644 --- a/src/main/java/org/shedlang/compiler/patio/instructions/PatioLocalLoad.java +++ b/src/main/java/org/shedlang/compiler/patio/instructions/PatioLocalLoad.java @@ -5,5 +5,5 @@ import org.shedlang.compiler.sources.Source; /** * Load the local with the given name and push it onto the stack. */ -public record PatioLocalLoad(String name, Source source) implements PatioInstruction { +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 index 317a1172..b874144f 100644 --- a/src/main/java/org/shedlang/compiler/patio/instructions/PatioLocalStore.java +++ b/src/main/java/org/shedlang/compiler/patio/instructions/PatioLocalStore.java @@ -5,5 +5,5 @@ 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(String name, Source source) implements PatioInstruction { +public record PatioLocalStore(PatioLocal local, Source source) implements PatioInstruction { } |
