aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-12-15 15:47:58 +0000
committerMichael Williamson <mike@zwobble.org>2023-12-15 15:47:58 +0000
commit3ed2ca517aa5f2424f238817844d8a8eb98e64c2 (patch)
tree8dc70ba88ce76b22f2539adc932deab0c00ac0c1 /src/main/java/org
parent549ec911f67484c875a09d4b5b8a9a4fc0ecc806 (diff)
Implement int ops in runtime
Diffstat (limited to 'src/main/java/org')
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenInts.java282
1 files changed, 94 insertions, 188 deletions
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenInts.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenInts.java
index 0178033f..b4b480ab 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenInts.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenInts.java
@@ -1,25 +1,71 @@
package org.shedlang.compiler.patio;
-import org.shedlang.compiler.patio.ast.instructions.*;
+import org.shedlang.compiler.patio.ast.PatioFunctionDeclaration;
+import org.shedlang.compiler.patio.ast.PatioLocal;
+import org.shedlang.compiler.patio.ast.instructions.PatioCall;
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 org.shedlang.compiler.sources.Sources;
import java.math.BigInteger;
import java.util.List;
+import java.util.Optional;
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 PatioFunctionDeclaration INT_CREATE_DECLARATION = new PatioFunctionDeclaration(
+ "shed__runtime__int_create",
+ List.of(
+ PatioLocal.param("value", PatioTypes.INT)
+ ),
+ PatioCodeGenTypes.VALUE_TYPE,
+ Sources.RUNTIME
+ );
+
+ public static final PatioFunctionDeclaration INT_ADD_DECLARATION = new PatioFunctionDeclaration(
+ "shed__runtime__int_add",
+ List.of(
+ PatioLocal.param("left", PatioCodeGenTypes.VALUE_TYPE),
+ PatioLocal.param("right", PatioCodeGenTypes.VALUE_TYPE)
+ ),
+ PatioCodeGenTypes.VALUE_TYPE,
+ Sources.RUNTIME
+ );
+
+ public static final PatioFunctionDeclaration INT_SUB_DECLARATION = new PatioFunctionDeclaration(
+ "shed__runtime__int_sub",
+ List.of(
+ PatioLocal.param("left", PatioCodeGenTypes.VALUE_TYPE),
+ PatioLocal.param("right", PatioCodeGenTypes.VALUE_TYPE)
+ ),
+ PatioCodeGenTypes.VALUE_TYPE,
+ Sources.RUNTIME
+ );
+
+ public static final PatioFunctionDeclaration INT_EQUALS_DECLARATION = new PatioFunctionDeclaration(
+ "shed__runtime__int_equals",
+ List.of(
+ PatioLocal.param("left", PatioCodeGenTypes.VALUE_TYPE),
+ PatioLocal.param("right", PatioCodeGenTypes.VALUE_TYPE)
+ ),
+ PatioTypes.BOOL,
+ Sources.RUNTIME
+ );
+
+ public static final PatioFunctionDeclaration INT_NOT_EQUAL_DECLARATION = new PatioFunctionDeclaration(
+ "shed__runtime__int_not_equal",
+ List.of(
+ PatioLocal.param("left", PatioCodeGenTypes.VALUE_TYPE),
+ PatioLocal.param("right", PatioCodeGenTypes.VALUE_TYPE)
+ ),
+ PatioTypes.BOOL,
+ Sources.RUNTIME
+ );
- public static final List<Integer> VALUE_OFFSET = List.of(0);
+ public static final PatioType INT_TYPE = PatioCodeGenTypes.VALUE_TYPE;
private PatioCodeGenInts() {
}
@@ -30,12 +76,14 @@ public class PatioCodeGenInts {
Source source,
PatioCodeGenContext context
) {
- return store(
- target,
- new PatioInt(value.intValueExact()),
- source,
- context
- );
+ return context
+ .ensureCompilationUnitElement(INT_CREATE_DECLARATION)
+ .addInstruction(new PatioCall(
+ Optional.of(target),
+ INT_CREATE_DECLARATION.name(),
+ List.of(new PatioInt(value.intValueExact())),
+ source
+ ));
}
public static PatioCodeGenContext add(
@@ -45,14 +93,14 @@ public class PatioCodeGenInts {
Source source,
PatioCodeGenContext context
) {
- return binaryOpToInt(
- PatioIntAdd::new,
- target,
- left,
- right,
- source,
- context
- );
+ return context
+ .ensureCompilationUnitElement(INT_ADD_DECLARATION)
+ .addInstruction(new PatioCall(
+ Optional.of(target),
+ INT_ADD_DECLARATION.name(),
+ List.of(left, right),
+ source
+ ));
}
public static PatioCodeGenContext sub(
@@ -62,14 +110,14 @@ public class PatioCodeGenInts {
Source source,
PatioCodeGenContext context
) {
- return binaryOpToInt(
- PatioIntSub::new,
- target,
- left,
- right,
- source,
- context
- );
+ return context
+ .ensureCompilationUnitElement(INT_SUB_DECLARATION)
+ .addInstruction(new PatioCall(
+ Optional.of(target),
+ INT_SUB_DECLARATION.name(),
+ List.of(left, right),
+ source
+ ));
}
public static PatioCodeGenContext equals(
@@ -79,14 +127,14 @@ public class PatioCodeGenInts {
Source source,
PatioCodeGenContext context
) {
- return binaryOpToBool(
- PatioIntEquals::new,
- target,
- left,
- right,
- source,
- context
- );
+ return context
+ .ensureCompilationUnitElement(INT_EQUALS_DECLARATION)
+ .addInstruction(new PatioCall(
+ Optional.of(target),
+ INT_EQUALS_DECLARATION.name(),
+ List.of(left, right),
+ source
+ ));
}
public static PatioCodeGenContext notEqual(
@@ -96,155 +144,13 @@ public class PatioCodeGenInts {
Source source,
PatioCodeGenContext context
) {
- 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;
+ return context
+ .ensureCompilationUnitElement(INT_NOT_EQUAL_DECLARATION)
+ .addInstruction(new PatioCall(
+ Optional.of(target),
+ INT_NOT_EQUAL_DECLARATION.name(),
+ List.of(left, right),
+ source
+ ));
}
}