aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-11-20 22:27:15 +0000
committerMichael Williamson <mike@zwobble.org>2023-11-20 22:28:34 +0000
commit86f7fa2960fe0eaa49f0cebdedd8cc04f31ebd76 (patch)
tree58a0d944f8f87c4821aaba4cc232b7c2491994a9 /src/main/java
parentd90a1e3828856f4c790b3527299e17e42cf13552 (diff)
Implement stack IR vars using alloc-stack in Patio
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java30
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java6
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java32
3 files changed, 48 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 26928a24..7420b69b 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
@@ -8,6 +8,7 @@ import org.shedlang.compiler.patio.ast.instructions.*;
import org.shedlang.compiler.patio.ast.operands.PatioInt;
import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
import org.shedlang.compiler.patio.ast.operands.PatioOperand;
+import org.shedlang.compiler.patio.ast.types.PatioDataLayouts;
import org.shedlang.compiler.patio.ast.types.PatioTypes;
import org.shedlang.compiler.sources.Source;
import org.shedlang.compiler.sources.Sources;
@@ -695,9 +696,11 @@ public class PatioCodeGen {
var varValue = varValueResult.value();
context = varValueResult.context();
- context = context.addInstruction(new PatioVarLoad(
+ context = context.addInstruction(new PatioMemoryLoad(
varValue,
context.stackIrLocalNameToLocal(varLoad.name()),
+ PatioDataLayouts.fromType(PatioCodeGenTypes.VALUE_TYPE),
+ List.of(),
varLoad.source()
));
@@ -711,8 +714,10 @@ public class PatioCodeGen {
var value = valueResult.value();
context = valueResult.context();
- context = context.addInstruction(new PatioVarStore(
+ context = context.addInstruction(new PatioMemoryStore(
context.stackIrLocalNameToLocal(varStore.name()),
+ PatioDataLayouts.fromType(PatioCodeGenTypes.VALUE_TYPE),
+ List.of(),
value,
varStore.source()
));
@@ -855,9 +860,26 @@ public class PatioCodeGen {
private static PatioCodeGenContext declareStackIrLocals(List<Instruction> body, PatioCodeGenContext context) {
for (var boundVariable : findBoundVariables(body, P.orderedSet())) {
if (boundVariable.isMutable()) {
- context = context.defineStackIrVar(boundVariable.name());
+ var patioLocalResult = context.declareStackIrVal(
+ boundVariable.name(),
+ PatioTypes.DATA_POINTER
+ );
+ var patioLocal = patioLocalResult.value();
+ context = patioLocalResult.context();
+
+ // TODO: proper source (probably by storing variables on the function/namespace)
+ context = context.addInstruction(new PatioAllocStack(
+ patioLocal,
+ PatioDataLayouts.fromType(PatioCodeGenTypes.VALUE_TYPE),
+ Sources.NULL
+ ));
+
+ context = context.initStackIrLocal(boundVariable.name());
} else {
- context = context.declareStackIrVal(boundVariable.name()).context();
+ context = context.declareStackIrVal(
+ boundVariable.name(),
+ PatioCodeGenTypes.VALUE_TYPE
+ ).context();
}
}
return context;
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java
index cd2f1555..5ff66e0f 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java
@@ -80,7 +80,11 @@ class PatioCodeGenClosures {
}
for (var freeVariable : freeVariables) {
- var freeVariableLocalResult = bodyContext.declareStackIrVal(freeVariable.name());
+ var freeVariableLocalResult = bodyContext.declareStackIrVal(
+ freeVariable.name(),
+ // TODO: vary based on mutability
+ PatioCodeGenTypes.VALUE_TYPE
+ );
var freeVariableLocal = freeVariableLocalResult.value();
bodyContext = freeVariableLocalResult.context();
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java
index 3f0b43bb..9509036c 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java
@@ -1,6 +1,9 @@
package org.shedlang.compiler.patio;
-import org.pcollections.*;
+import org.pcollections.OrderedPMap;
+import org.pcollections.PMap;
+import org.pcollections.PStack;
+import org.pcollections.PVector;
import org.shedlang.compiler.backends.NameAllocator;
import org.shedlang.compiler.patio.ast.*;
import org.shedlang.compiler.patio.ast.instructions.PatioInstruction;
@@ -511,32 +514,31 @@ public record PatioCodeGenContext(
@CheckReturnValue
private PatioCodeGenContext declareStackIrParam(String name) {
- return declareStackIrLocal(name, PatioLocalType.PARAM).context();
- }
-
- @CheckReturnValue
- public PatioCodeGenResult<PatioLocalRef> declareStackIrVal(String name) {
- return declareStackIrLocal(name, PatioLocalType.VAL);
- }
-
- @CheckReturnValue
- public PatioCodeGenContext defineStackIrVar(String name) {
- return declareStackIrVar(name).initStackIrLocal(name);
+ return declareStackIrLocal(
+ name,
+ PatioCodeGenTypes.VALUE_TYPE,
+ PatioLocalType.PARAM
+ ).context();
}
@CheckReturnValue
- private PatioCodeGenContext declareStackIrVar(String name) {
- return declareStackIrLocal(name, PatioLocalType.VAR).context();
+ public PatioCodeGenResult<PatioLocalRef> declareStackIrVal(String name, PatioType type) {
+ return declareStackIrLocal(
+ name,
+ type,
+ PatioLocalType.VAL
+ );
}
@CheckReturnValue
private PatioCodeGenResult<PatioLocalRef> declareStackIrLocal(
String name,
+ PatioType type,
PatioLocalType patioLocalType
) {
return withFunctionContextResult(this.functionContext.declareStackIrLocal(
name,
- PatioCodeGenTypes.VALUE_TYPE,
+ type,
patioLocalType
));
}