aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-12-15 15:31:04 +0000
committerMichael Williamson <mike@zwobble.org>2023-12-15 15:31:04 +0000
commit549ec911f67484c875a09d4b5b8a9a4fc0ecc806 (patch)
tree1940d5adcdb5db95712d8edcbd924cd436b21fb3 /src/main/java
parenta2d7af1fdcc64f117d5db91761cb2610854e9e60 (diff)
Store Patio ints as pointers
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java4
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenInts.java188
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java8
3 files changed, 184 insertions, 16 deletions
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
index 3e3a2aa7..0345a529 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
@@ -554,7 +554,7 @@ public class PatioCodeGen {
var target = targetResult.value();
context = targetResult.context();
- context = PatioCodeGenInts.store(
+ context = PatioCodeGenInts.storeConst(
target,
pushInt.value(),
pushInt.source(),
@@ -860,7 +860,7 @@ public class PatioCodeGen {
context = leftResult.context();
var targetResult = context.allocateVal(
- PatioCodeGenLocalSpace.TEMPORARY,
+ PatioCodeGenLocalSpace.BINARY_OP_RESULT,
PatioCodeGenTypes.VALUE_TYPE
);
var target = targetResult.value();
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenInts.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenInts.java
index 037bd64b..0178033f 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenInts.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenInts.java
@@ -4,28 +4,38 @@ 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.PatioDataLayout;
+import org.shedlang.compiler.patio.ast.types.PatioDataLayouts;
import org.shedlang.compiler.patio.ast.types.PatioType;
+import org.shedlang.compiler.patio.ast.types.PatioTypes;
import org.shedlang.compiler.sources.Source;
import java.math.BigInteger;
+import java.util.List;
public class PatioCodeGenInts {
public static final PatioType INT_TYPE = PatioCodeGenTypes.VALUE_TYPE;
+ public static final PatioDataLayout INT_DATA_LAYOUT =
+ PatioDataLayouts.struct(List.of(PatioDataLayouts.INT));
+
+ public static final List<Integer> VALUE_OFFSET = List.of(0);
+
private PatioCodeGenInts() {
}
- public static PatioCodeGenContext store(
+ public static PatioCodeGenContext storeConst(
PatioLocalRef target,
BigInteger value,
Source source,
PatioCodeGenContext context
) {
- return context.addInstruction(new PatioLocalStore(
+ return store(
target,
new PatioInt(value.intValueExact()),
- source
- ));
+ source,
+ context
+ );
}
public static PatioCodeGenContext add(
@@ -35,12 +45,14 @@ public class PatioCodeGenInts {
Source source,
PatioCodeGenContext context
) {
- return context.addInstruction(new PatioIntAdd(
+ return binaryOpToInt(
+ PatioIntAdd::new,
target,
left,
right,
- source
- ));
+ source,
+ context
+ );
}
public static PatioCodeGenContext sub(
@@ -50,12 +62,14 @@ public class PatioCodeGenInts {
Source source,
PatioCodeGenContext context
) {
- return context.addInstruction(new PatioIntSub(
+ return binaryOpToInt(
+ PatioIntSub::new,
target,
left,
right,
- source
- ));
+ source,
+ context
+ );
}
public static PatioCodeGenContext equals(
@@ -65,12 +79,14 @@ public class PatioCodeGenInts {
Source source,
PatioCodeGenContext context
) {
- return context.addInstruction(new PatioIntEquals(
+ return binaryOpToBool(
+ PatioIntEquals::new,
target,
left,
right,
- source
- ));
+ source,
+ context
+ );
}
public static PatioCodeGenContext notEqual(
@@ -80,11 +96,155 @@ public class PatioCodeGenInts {
Source source,
PatioCodeGenContext context
) {
- return context.addInstruction(new PatioIntNotEqual(
+ return binaryOpToBool(
+ PatioIntNotEqual::new,
target,
left,
right,
+ source,
+ context
+ );
+ }
+
+ private interface BinaryInstruction {
+ PatioInstruction create(
+ PatioLocalRef target,
+ PatioOperand left,
+ PatioOperand right,
+ Source source
+ );
+ }
+
+ private static PatioCodeGenContext binaryOpToBool(
+ BinaryInstruction instruction,
+ PatioLocalRef target,
+ PatioOperand left,
+ PatioOperand right,
+ Source source,
+ PatioCodeGenContext context
+ ) {
+ var leftValueResult = loadIntValue(left, source, context);
+ var leftValue = leftValueResult.value();
+ context = leftValueResult.context();
+
+ var rightValueResult = loadIntValue(right, source, context);
+ var rightValue = rightValueResult.value();
+ context = rightValueResult.context();
+
+ context = context.addInstruction(instruction.create(
+ target,
+ leftValue,
+ rightValue,
source
));
+
+ return context;
+ }
+
+ private static PatioCodeGenContext binaryOpToInt(
+ BinaryInstruction instruction,
+ PatioLocalRef target,
+ PatioOperand left,
+ PatioOperand right,
+ Source source,
+ PatioCodeGenContext context
+ ) {
+ var leftValueResult = loadIntValue(left, source, context);
+ var leftValue = leftValueResult.value();
+ context = leftValueResult.context();
+
+ var rightValueResult = loadIntValue(right, source, context);
+ var rightValue = rightValueResult.value();
+ context = rightValueResult.context();
+
+ var targetValueResult = context.allocateVal(
+ PatioCodeGenLocalSpace.INT_BINARY_OP_RESULT_VALUE,
+ PatioTypes.INT
+ );
+ var targetValue = targetValueResult.value();
+ context = targetValueResult.context();
+
+ context = context.addInstruction(instruction.create(
+ targetValue,
+ leftValue,
+ rightValue,
+ source
+ ));
+
+ context = store(target, targetValue, source, context);
+
+ return context;
+ }
+
+ private static PatioCodeGenResult<PatioOperand> loadIntValue(
+ PatioOperand genericValue,
+ Source source,
+ PatioCodeGenContext context
+ ) {
+ var pointerResult = context.allocateVal(
+ PatioCodeGenLocalSpace.INT_POINTER,
+ PatioTypes.DATA_POINTER
+ );
+ var pointer = pointerResult.value();
+ context = pointerResult.context();
+
+ context = context.addInstruction(new PatioIntToDataPtr(
+ pointer,
+ genericValue,
+ source
+ ));
+
+ var valueResult = context.allocateVal(
+ PatioCodeGenLocalSpace.INT_BINARY_OP_LEFT_VALUE,
+ PatioTypes.INT
+ );
+ var value = valueResult.value();
+ context = valueResult.context();
+
+ context = context.addInstruction(new PatioMemoryLoad(
+ value,
+ pointer,
+ INT_DATA_LAYOUT,
+ VALUE_OFFSET,
+ source
+ ));
+
+ return context.toResult(value);
+ }
+
+ private static PatioCodeGenContext store(
+ PatioLocalRef target,
+ PatioOperand value,
+ Source source,
+ PatioCodeGenContext context
+ ) {
+ var targetPointerResult = context.allocateVal(
+ PatioCodeGenLocalSpace.INT_POINTER,
+ PatioTypes.DATA_POINTER
+ );
+ var targetPointer = targetPointerResult.value();
+ context = targetPointerResult.context();
+
+ context = context.addInstruction(new PatioAllocHeap(
+ targetPointer,
+ INT_DATA_LAYOUT,
+ source
+ ));
+
+ context = context.addInstruction(new PatioMemoryStore(
+ targetPointer,
+ INT_DATA_LAYOUT,
+ VALUE_OFFSET,
+ value,
+ source
+ ));
+
+ context = context.addInstruction(new PatioDataPtrToInt(
+ target,
+ targetPointer,
+ 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 89a47708..7f042ffc 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java
@@ -1,6 +1,7 @@
package org.shedlang.compiler.patio;
public enum PatioCodeGenLocalSpace {
+ BINARY_OP_RESULT("binaryOpResult"),
CALL_ARG_POSITIONAL("callArgPositional"),
CALL_ARG_NAMED("callArgNamed"),
CALL_FUNCTION_POINTER("callFunctionPointer"),
@@ -12,6 +13,13 @@ public enum PatioCodeGenLocalSpace {
EFFECT_HANDLER_METHOD("effectHandlerMethod"),
EFFECT_HANDLER_METHOD_POINTER("effectHandlerMethodPointer"),
INT("int"),
+ INT_POINTER("intPointer"),
+ INT_BINARY_OP_LEFT_POINTER("intBinaryOpLeftPointer"),
+ INT_BINARY_OP_LEFT_VALUE("intBinaryOpLeftValue"),
+ INT_BINARY_OP_RIGHT_POINTER("intBinaryOpRightPointer"),
+ INT_BINARY_OP_RIGHT_VALUE("intBinaryOpRightValue"),
+ INT_BINARY_OP_RESULT_POINTER("intBinaryOpResultPointer"),
+ INT_BINARY_OP_RESULT_VALUE("intBinaryOpResultValue"),
MAIN("main"),
NAMESPACE_IS_INITED_POINTER("namespaceIsInitedPointer"),
NAMESPACE_POINTER("namespacePointer"),