aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-20 14:30:00 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-20 14:30:00 +0100
commita5a2588692d7c421d30ede8439e1f31df6f8c216 (patch)
tree07145048e068aec7aa3b294f964f0a13b0a6ca1f /src/main/java
parent9fce582cb0aa3cbd73c9a398f66d75bf39c360de (diff)
Push closure value onto stack
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java17
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java19
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java1
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java7
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioDataPtrToInt.java11
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java2
6 files changed, 48 insertions, 9 deletions
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<DefineFunctionInstruction.Param> positionalParams,
List<DefineFunctionInstruction.Param> 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();
}