From a5a2588692d7c421d30ede8439e1f31df6f8c216 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 20 Jul 2023 14:30:00 +0100 Subject: Push closure value onto stack --- .../org/shedlang/compiler/patio/PatioCodeGen.java | 17 +++++++++++++++-- .../shedlang/compiler/patio/PatioCodeGenClosures.java | 19 +++++++++++++------ .../compiler/patio/PatioCodeGenLocalSpace.java | 1 + .../shedlang/compiler/patio/ast/PatioSerializer.java | 7 +++++++ .../patio/ast/instructions/PatioDataPtrToInt.java | 11 +++++++++++ .../patio/ast/instructions/PatioInstruction.java | 2 +- 6 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioDataPtrToInt.java (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 58280925..49351b7d 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java @@ -99,8 +99,16 @@ public class PatioCodeGen { ).temporaryPush(target); } - case DefineFunctionInstruction defineFunction -> - PatioCodeGenClosures.create( + case DefineFunctionInstruction defineFunction -> { + var targetResult = context.allocateLocal( + PatioCodeGenLocalSpace.CLOSURE, + PatioCodeGenTypes.VALUE_TYPE + ); + var target = targetResult.value(); + context = targetResult.context(); + + context = PatioCodeGenClosures.create( + target, defineFunction.name(), defineFunction.positionalParams(), defineFunction.namedParams(), @@ -111,6 +119,11 @@ public class PatioCodeGen { context ); + context = context.temporaryPush(target); + + yield context; + } + case DiscardInstruction discard -> context.temporaryPop().context(); diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java index cdeb1dac..e7bef0d0 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java @@ -39,6 +39,7 @@ class PatioCodeGenClosures { } public static PatioCodeGenContext create( + PatioLocalRef target, String name, List positionalParams, List namedParams, @@ -60,14 +61,14 @@ class PatioCodeGenClosures { return bodyContext; }); - var closureResult = context.allocateLocal( - PatioCodeGenLocalSpace.CLOSURE, + var closurePointerResult = context.allocateLocal( + PatioCodeGenLocalSpace.CLOSURE_POINTER, PatioTypes.DATA_POINTER ); - var closure = closureResult.value(); - context = closureResult.context(); + var closurePointer = closurePointerResult.value(); + context = closurePointerResult.context(); - context = context.addInstruction(new PatioAlloc(closure, layout, source)); + context = context.addInstruction(new PatioAlloc(closurePointer, layout, source)); var functionPointerResult = context.allocateLocal( PatioCodeGenLocalSpace.CLOSURE_FUNCTION_POINTER, @@ -83,13 +84,19 @@ class PatioCodeGenClosures { )); context = context.addInstruction(new PatioMemoryStore( - closure, + closurePointer, layout, FUNCTION_POINTER_OFFSET, functionPointer, source )); + context = context.addInstruction(new PatioDataPtrToInt( + target, + closurePointer, + source + )); + return context; } diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java index d3827cb7..d719cf22 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java @@ -5,6 +5,7 @@ public enum PatioCodeGenLocalSpace { CALL_ARG_NAMED("callArgNamed"), CALL_FUNCTION_POINTER("callFunctionPointer"), CLOSURE("closure"), + CLOSURE_POINTER("closurePointer"), CLOSURE_FUNCTION_POINTER("closureFunctionPointer"), LOCAL("local"), RECEIVER("receiver"), diff --git a/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java b/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java index 428994a9..946c6eb6 100644 --- a/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java +++ b/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java @@ -88,6 +88,13 @@ public class PatioSerializer { ) ); + case PatioDataPtrToInt dataPtrToInt -> + treeList( + treeIdentifier("data-ptr-to-int"), + localRefToTree(dataPtrToInt.target()), + localRefToTree(dataPtrToInt.value()) + ); + case PatioIntGet intGet -> treeList( treeIdentifier("int-get"), diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioDataPtrToInt.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioDataPtrToInt.java new file mode 100644 index 00000000..d7239044 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioDataPtrToInt.java @@ -0,0 +1,11 @@ +package org.shedlang.compiler.patio.ast.instructions; + +import org.shedlang.compiler.patio.ast.PatioLocalRef; +import org.shedlang.compiler.sources.Source; + +public record PatioDataPtrToInt( + PatioLocalRef target, + PatioLocalRef value, + Source source +) implements PatioInstruction { +} diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java index 4cff67b4..aa814121 100644 --- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java +++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java @@ -2,6 +2,6 @@ package org.shedlang.compiler.patio.ast.instructions; import org.shedlang.compiler.sources.Source; -public sealed interface PatioInstruction permits PatioAlloc, PatioCallPointer, PatioFunctionPointerGet, PatioIntGet, PatioLocalCopy, PatioMemoryLoad, PatioMemoryStore, PatioReturn { +public sealed interface PatioInstruction permits PatioAlloc, PatioCallPointer, PatioDataPtrToInt, PatioFunctionPointerGet, PatioIntGet, PatioLocalCopy, PatioMemoryLoad, PatioMemoryStore, PatioReturn { Source source(); } -- cgit v1.2.3