aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-20 23:31:04 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-20 23:31:04 +0100
commitfceeffe7558fc4cbab8d2d5062ad6cf2930ce850 (patch)
tree4ecdb55f5dd33c89d15cc6b51b6bd3fcdc76bc3f /src/main/java
parent413ad79ca75bb508ac2b6b333571f08f3d21c8a8 (diff)
Introduce operands to Patio
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java2
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java2
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java1
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/PatioLocalRef.java4
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java62
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioAlloc.java2
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioCallPointer.java2
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioDataPtrGet.java2
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioDataPtrToInt.java2
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioFunctionPointerGet.java2
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIf.java2
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntAdd.java2
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntGet.java2
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntToDataPtr.java2
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioLocalCopy.java2
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryLoad.java2
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryStore.java2
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioReturn.java2
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/operands/PatioLocalRef.java6
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/operands/PatioOperand.java5
20 files changed, 63 insertions, 45 deletions
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
index c4f040dc..d7ed13d6 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
@@ -1,7 +1,7 @@
package org.shedlang.compiler.patio;
import org.shedlang.compiler.patio.ast.PatioCompilationUnit;
-import org.shedlang.compiler.patio.ast.PatioLocalRef;
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
import org.shedlang.compiler.patio.ast.instructions.PatioIntAdd;
import org.shedlang.compiler.patio.ast.instructions.PatioIntGet;
import org.shedlang.compiler.patio.ast.instructions.PatioReturn;
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java
index 8e108a45..2374ed2c 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java
@@ -3,7 +3,7 @@ package org.shedlang.compiler.patio;
import org.shedlang.compiler.ast.typed.FreeVariable;
import org.shedlang.compiler.ast.typed.FreeVariables;
import org.shedlang.compiler.patio.ast.instructions.*;
-import org.shedlang.compiler.patio.ast.PatioLocalRef;
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
import org.shedlang.compiler.patio.ast.types.PatioDataLayout;
import org.shedlang.compiler.patio.ast.types.PatioStruct;
import org.shedlang.compiler.patio.ast.types.PatioType;
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java
index 3b5b3ba1..f85d97a1 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java
@@ -8,6 +8,7 @@ import org.shedlang.compiler.backends.llvm.NameAllocator;
import org.shedlang.compiler.patio.ast.*;
import org.shedlang.compiler.patio.ast.instructions.PatioInstruction;
import org.shedlang.compiler.patio.ast.instructions.PatioLocalCopy;
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
import org.shedlang.compiler.patio.ast.types.PatioDataLayout;
import org.shedlang.compiler.patio.ast.types.PatioType;
import org.shedlang.compiler.sources.Source;
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/PatioLocalRef.java b/src/main/java/org/shedlang/compiler/patio/ast/PatioLocalRef.java
deleted file mode 100644
index 1911ee04..00000000
--- a/src/main/java/org/shedlang/compiler/patio/ast/PatioLocalRef.java
+++ /dev/null
@@ -1,4 +0,0 @@
-package org.shedlang.compiler.patio.ast;
-
-public record PatioLocalRef(String name) {
-}
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 0e26ef08..8edf1464 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java
@@ -1,6 +1,8 @@
package org.shedlang.compiler.patio.ast;
import org.shedlang.compiler.patio.ast.instructions.*;
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
+import org.shedlang.compiler.patio.ast.operands.PatioOperand;
import org.shedlang.compiler.patio.ast.types.*;
import org.shedlang.compiler.treetext.Tree;
@@ -51,6 +53,11 @@ public class PatioSerializer {
serializeTree(tree, builder, 0);
}
+ public static void serializeOperand(PatioOperand operand, StringBuilder builder) {
+ var tree = operandToTree(operand);
+ serializeTree(tree, builder, 0);
+ }
+
public static void serializeType(PatioType type, StringBuilder builder) {
var tree = typeToTree(type);
serializeTree(tree, builder, 0);
@@ -107,18 +114,18 @@ public class PatioSerializer {
case PatioAlloc alloc ->
treeList(
treeIdentifier("alloc"),
- localRefToTree(alloc.target()),
+ operandToTree(alloc.target()),
dataLayoutToTree(alloc.layout())
);
case PatioCallPointer callPointer ->
treeList(
treeIdentifier("call-pointer"),
- localRefToTree(callPointer.target()),
- localRefToTree(callPointer.functionPointer()),
+ operandToTree(callPointer.target()),
+ operandToTree(callPointer.functionPointer()),
treeList(
callPointer.args().stream()
- .map(arg -> localRefToTree(arg))
+ .map(arg -> operandToTree(arg))
.toList()
)
);
@@ -126,21 +133,21 @@ public class PatioSerializer {
case PatioDataPtrGet dataPtrGet ->
treeList(
treeIdentifier("data-ptr-get"),
- localRefToTree(dataPtrGet.target()),
+ operandToTree(dataPtrGet.target()),
treeString(dataPtrGet.name())
);
case PatioDataPtrToInt dataPtrToInt ->
treeList(
treeIdentifier("data-ptr-to-int"),
- localRefToTree(dataPtrToInt.target()),
- localRefToTree(dataPtrToInt.value())
+ operandToTree(dataPtrToInt.target()),
+ operandToTree(dataPtrToInt.value())
);
case PatioIf if_ ->
treeListIndented(
treeIdentifier("if"),
- localRefToTree(if_.condition()),
+ operandToTree(if_.condition()),
instructionsToTree(if_.trueBody()),
instructionsToTree(if_.falseBody())
);
@@ -148,37 +155,37 @@ public class PatioSerializer {
case PatioIntAdd intAdd ->
treeList(
treeIdentifier("int-add"),
- localRefToTree(intAdd.target()),
- localRefToTree(intAdd.left()),
- localRefToTree(intAdd.right())
+ operandToTree(intAdd.target()),
+ operandToTree(intAdd.left()),
+ operandToTree(intAdd.right())
);
case PatioIntGet intGet ->
treeList(
treeIdentifier("int-get"),
- localRefToTree(intGet.target()),
+ operandToTree(intGet.target()),
treeInt(intGet.value())
);
case PatioIntToDataPtr intToDataPtr ->
treeList(
treeIdentifier("int-to-data-ptr"),
- localRefToTree(intToDataPtr.target()),
- localRefToTree(intToDataPtr.value())
+ operandToTree(intToDataPtr.target()),
+ operandToTree(intToDataPtr.value())
);
case PatioLocalCopy localCopy ->
treeList(
treeIdentifier("local-copy"),
- localRefToTree(localCopy.target()),
- localRefToTree(localCopy.value())
+ operandToTree(localCopy.target()),
+ operandToTree(localCopy.value())
);
case PatioMemoryLoad memoryLoad ->
treeListIndented(
treeIdentifier("memory-load"),
- localRefToTree(memoryLoad.target()),
- treeNamed("pointer", localRefToTree(memoryLoad.pointer())),
+ operandToTree(memoryLoad.target()),
+ treeNamed("pointer", operandToTree(memoryLoad.pointer())),
treeNamed("layout", dataLayoutToTree(memoryLoad.layout())),
treeNamed("offset", treeList(
memoryLoad.offset().stream()
@@ -190,27 +197,27 @@ public class PatioSerializer {
case PatioMemoryStore memoryStore ->
treeListIndented(
treeIdentifier("memory-store"),
- treeNamed("pointer", localRefToTree(memoryStore.pointer())),
+ treeNamed("pointer", operandToTree(memoryStore.pointer())),
treeNamed("layout", dataLayoutToTree(memoryStore.layout())),
treeNamed("offset", treeList(
memoryStore.offset().stream()
.map(offset -> treeInt(offset))
.toList()
)),
- treeNamed("value", localRefToTree(memoryStore.value()))
+ treeNamed("value", operandToTree(memoryStore.value()))
);
case PatioFunctionPointerGet functionPointerGet ->
treeList(
treeIdentifier("function-pointer-get"),
- localRefToTree(functionPointerGet.target()),
+ operandToTree(functionPointerGet.target()),
treeString(functionPointerGet.name())
);
case PatioReturn return_ ->
treeList(
treeIdentifier("return"),
- localRefToTree(return_.value())
+ operandToTree(return_.value())
);
};
}
@@ -219,6 +226,13 @@ public class PatioSerializer {
return treeList(treeString(local.name()), typeToTree(local.type()));
}
+ private static Tree operandToTree(PatioOperand operand) {
+ return switch (operand) {
+ case PatioLocalRef localRef ->
+ treeList(treeIdentifier("local"), treeString(localRef.name()));
+ };
+ }
+
private static Tree typeToTree(PatioType type) {
return switch (type) {
case PatioBoolType boolType ->
@@ -254,8 +268,4 @@ public class PatioSerializer {
);
};
}
-
- private static Tree localRefToTree(PatioLocalRef local) {
- return treeString(local.name());
- }
}
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioAlloc.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioAlloc.java
index 279ae0db..dd5bde56 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioAlloc.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioAlloc.java
@@ -1,6 +1,6 @@
package org.shedlang.compiler.patio.ast.instructions;
-import org.shedlang.compiler.patio.ast.PatioLocalRef;
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
import org.shedlang.compiler.patio.ast.types.PatioDataLayout;
import org.shedlang.compiler.sources.Source;
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioCallPointer.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioCallPointer.java
index a3e30c02..5373a95d 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioCallPointer.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioCallPointer.java
@@ -1,6 +1,6 @@
package org.shedlang.compiler.patio.ast.instructions;
-import org.shedlang.compiler.patio.ast.PatioLocalRef;
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
import org.shedlang.compiler.sources.Source;
import java.util.List;
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioDataPtrGet.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioDataPtrGet.java
index ec9451b3..ec417b4c 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioDataPtrGet.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioDataPtrGet.java
@@ -1,6 +1,6 @@
package org.shedlang.compiler.patio.ast.instructions;
-import org.shedlang.compiler.patio.ast.PatioLocalRef;
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
import org.shedlang.compiler.sources.Source;
/**
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
index d7239044..42372cbb 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioDataPtrToInt.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioDataPtrToInt.java
@@ -1,6 +1,6 @@
package org.shedlang.compiler.patio.ast.instructions;
-import org.shedlang.compiler.patio.ast.PatioLocalRef;
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
import org.shedlang.compiler.sources.Source;
public record PatioDataPtrToInt(
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioFunctionPointerGet.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioFunctionPointerGet.java
index 01c361cf..694b8da6 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioFunctionPointerGet.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioFunctionPointerGet.java
@@ -1,6 +1,6 @@
package org.shedlang.compiler.patio.ast.instructions;
-import org.shedlang.compiler.patio.ast.PatioLocalRef;
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
import org.shedlang.compiler.sources.Source;
/**
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIf.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIf.java
index 4446f3c6..87de0735 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIf.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIf.java
@@ -1,6 +1,6 @@
package org.shedlang.compiler.patio.ast.instructions;
-import org.shedlang.compiler.patio.ast.PatioLocalRef;
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
import org.shedlang.compiler.sources.Source;
import java.util.List;
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntAdd.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntAdd.java
index eae6a3fa..ae1f67b7 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntAdd.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntAdd.java
@@ -1,6 +1,6 @@
package org.shedlang.compiler.patio.ast.instructions;
-import org.shedlang.compiler.patio.ast.PatioLocalRef;
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
import org.shedlang.compiler.sources.Source;
public record PatioIntAdd(
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntGet.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntGet.java
index 89d6a82c..beba06d2 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntGet.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntGet.java
@@ -1,6 +1,6 @@
package org.shedlang.compiler.patio.ast.instructions;
-import org.shedlang.compiler.patio.ast.PatioLocalRef;
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
import org.shedlang.compiler.sources.Source;
/**
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntToDataPtr.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntToDataPtr.java
index 8c8000d5..7680645a 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntToDataPtr.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntToDataPtr.java
@@ -1,6 +1,6 @@
package org.shedlang.compiler.patio.ast.instructions;
-import org.shedlang.compiler.patio.ast.PatioLocalRef;
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
import org.shedlang.compiler.sources.Source;
public record PatioIntToDataPtr(
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioLocalCopy.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioLocalCopy.java
index 58dbe369..34d5bbaa 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioLocalCopy.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioLocalCopy.java
@@ -1,6 +1,6 @@
package org.shedlang.compiler.patio.ast.instructions;
-import org.shedlang.compiler.patio.ast.PatioLocalRef;
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
import org.shedlang.compiler.sources.Source;
/**
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryLoad.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryLoad.java
index 7a5f49ce..16037653 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryLoad.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryLoad.java
@@ -1,6 +1,6 @@
package org.shedlang.compiler.patio.ast.instructions;
-import org.shedlang.compiler.patio.ast.PatioLocalRef;
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
import org.shedlang.compiler.patio.ast.types.PatioDataLayout;
import org.shedlang.compiler.sources.Source;
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryStore.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryStore.java
index 0a9b35a6..9abc16da 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryStore.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryStore.java
@@ -1,6 +1,6 @@
package org.shedlang.compiler.patio.ast.instructions;
-import org.shedlang.compiler.patio.ast.PatioLocalRef;
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
import org.shedlang.compiler.patio.ast.types.PatioDataLayout;
import org.shedlang.compiler.sources.Source;
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioReturn.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioReturn.java
index d2fe33b8..285395e0 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioReturn.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioReturn.java
@@ -1,6 +1,6 @@
package org.shedlang.compiler.patio.ast.instructions;
-import org.shedlang.compiler.patio.ast.PatioLocalRef;
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
import org.shedlang.compiler.sources.Source;
/**
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/operands/PatioLocalRef.java b/src/main/java/org/shedlang/compiler/patio/ast/operands/PatioLocalRef.java
new file mode 100644
index 00000000..c0a37c9d
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/patio/ast/operands/PatioLocalRef.java
@@ -0,0 +1,6 @@
+package org.shedlang.compiler.patio.ast.operands;
+
+import org.shedlang.compiler.patio.ast.operands.PatioOperand;
+
+public record PatioLocalRef(String name) implements PatioOperand {
+}
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/operands/PatioOperand.java b/src/main/java/org/shedlang/compiler/patio/ast/operands/PatioOperand.java
new file mode 100644
index 00000000..e7526d44
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/patio/ast/operands/PatioOperand.java
@@ -0,0 +1,5 @@
+package org.shedlang.compiler.patio.ast.operands;
+
+public sealed interface PatioOperand permits PatioLocalRef {
+
+}