aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2024-01-22 19:08:45 +0000
committerMichael Williamson <mike@zwobble.org>2024-01-22 19:08:45 +0000
commitd3bca8fad9f9cb6b6e8f960031887590a97d5dc1 (patch)
tree10da8d75937018f74d33f303b40900398a85de69 /src/main/java
parent6d380ac1b8aefb00e6be1655a416588e5559bfa0 (diff)
Extract PatioIntValue
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/llvm/codegen/PatioToLlvm.java12
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java16
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioSerializer.java13
-rw-r--r--src/main/java/org/shedlang/compiler/patio/operands/PatioIntConst.java8
-rw-r--r--src/main/java/org/shedlang/compiler/patio/types/PatioDataValueInt.java4
-rw-r--r--src/main/java/org/shedlang/compiler/patio/types/PatioDataValues.java4
-rw-r--r--src/main/java/org/shedlang/compiler/patio/values/PatioIntValue.java4
-rw-r--r--src/main/java/org/shedlang/compiler/patio/values/PatioIntValueLiteral.java4
-rw-r--r--src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java2
-rw-r--r--src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioEffects.java10
-rw-r--r--src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java2
-rw-r--r--src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioInts.java2
-rw-r--r--src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioStructs.java2
-rw-r--r--src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioTuples.java2
14 files changed, 65 insertions, 20 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/codegen/PatioToLlvm.java b/src/main/java/org/shedlang/compiler/backends/llvm/codegen/PatioToLlvm.java
index 71212189..27056756 100644
--- a/src/main/java/org/shedlang/compiler/backends/llvm/codegen/PatioToLlvm.java
+++ b/src/main/java/org/shedlang/compiler/backends/llvm/codegen/PatioToLlvm.java
@@ -15,6 +15,8 @@ import org.shedlang.compiler.patio.*;
import org.shedlang.compiler.patio.instructions.*;
import org.shedlang.compiler.patio.operands.*;
import org.shedlang.compiler.patio.types.*;
+import org.shedlang.compiler.patio.values.PatioIntValue;
+import org.shedlang.compiler.patio.values.PatioIntValueLiteral;
import org.shedlang.compiler.util.Lazy;
import org.shedlang.compiler.util.Lists;
import org.shedlang.compiler.util.P;
@@ -1150,13 +1152,19 @@ public class PatioToLlvm {
new LlvmVariableGlobal(patioFunctionPtrConst.name());
case PatioIntConst patioIntConst ->
- new LlvmOperandInt(patioIntConst.value());
+ patioIntValueToLlvmOperand(patioIntConst.value());
case PatioLocalRef patioLocalRef ->
context.patioLocalToOperand(patioLocalRef);
};
}
+ private static LlvmOperandInt patioIntValueToLlvmOperand(PatioIntValue patioIntConst) {
+ return switch (patioIntConst) {
+ case PatioIntValueLiteral literal ->
+ new LlvmOperandInt(literal.value());
+ };
+ }
private static LlvmType patioTypeToLlvmType(
PatioType patioType,
@@ -1305,7 +1313,7 @@ public class PatioToLlvm {
new LlvmOperandNull();
case PatioDataValueInt patioDataValueInt ->
- new LlvmOperandInt(patioDataValueInt.value());
+ patioIntValueToLlvmOperand(patioDataValueInt.value());
case PatioDataValueStruct patioDataValueStruct ->
new LlvmOperandStructure(
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java b/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java
index 66519767..04c81d2a 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java
@@ -12,6 +12,8 @@ import org.shedlang.compiler.patio.*;
import org.shedlang.compiler.patio.instructions.*;
import org.shedlang.compiler.patio.operands.*;
import org.shedlang.compiler.patio.types.*;
+import org.shedlang.compiler.patio.values.PatioIntValue;
+import org.shedlang.compiler.patio.values.PatioIntValueLiteral;
import org.shedlang.compiler.util.Lists;
import org.shedlang.compiler.util.P;
@@ -580,7 +582,7 @@ public class PatioToWasm {
case PatioIntConst patioIntConst ->
context.addInstruction(wasmIntConst(
context.target.patioIntType(),
- patioIntConst.value()
+ generateIntValue(patioIntConst.value())
));
case PatioLocalRef patioLocalRef -> {
@@ -590,6 +592,13 @@ public class PatioToWasm {
};
}
+ private static int generateIntValue(PatioIntValue value) {
+ return switch (value) {
+ case PatioIntValueLiteral literal ->
+ literal.value();
+ };
+ }
+
private static int sizeOf(PatioDataLayout layout, WasmTarget target) {
return switch (layout) {
case PatioDataLayoutArray patioDataLayoutArray ->
@@ -709,12 +718,13 @@ public class PatioToWasm {
bytes.putInt(0);
}
case PatioDataValueInt patioDataValueInt -> {
+ var intValue = generateIntValue(patioDataValueInt.value());
switch (target) {
case WASM32 -> {
- bytes.putInt(patioDataValueInt.value());
+ bytes.putInt(intValue);
}
case WASM64 -> {
- bytes.putLong(patioDataValueInt.value());
+ bytes.putLong(intValue);
}
}
}
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioSerializer.java b/src/main/java/org/shedlang/compiler/patio/PatioSerializer.java
index c770cd21..2a869f9e 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioSerializer.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioSerializer.java
@@ -3,6 +3,8 @@ package org.shedlang.compiler.patio;
import org.shedlang.compiler.patio.instructions.*;
import org.shedlang.compiler.patio.operands.*;
import org.shedlang.compiler.patio.types.*;
+import org.shedlang.compiler.patio.values.PatioIntValue;
+import org.shedlang.compiler.patio.values.PatioIntValueLiteral;
import org.shedlang.compiler.treetext.Tree;
import java.util.List;
@@ -431,7 +433,7 @@ public class PatioSerializer {
treeList(treeIdentifier("function-ptr-const"), treeString(functionPtr.name()));
case PatioIntConst int_ ->
- treeList(treeIdentifier("int-const"), treeInt(int_.value()));
+ treeList(treeIdentifier("int-const"), intValueToTree(int_.value()));
case PatioLocalRef localRef ->
treeList(treeIdentifier("local"), treeString(localRef.name()));
@@ -533,7 +535,7 @@ public class PatioSerializer {
case PatioDataValueInt int_ ->
treeList(
treeIdentifier("int"),
- treeIdentifier(Integer.toString(int_.value()))
+ intValueToTree(int_.value())
);
case PatioDataValueStruct struct ->
@@ -548,6 +550,13 @@ public class PatioSerializer {
};
}
+ private static Tree intValueToTree(PatioIntValue intValue) {
+ return switch (intValue) {
+ case PatioIntValueLiteral literal ->
+ treeInt(literal.value());
+ };
+ }
+
private static Tree offsetToTree(List<Integer> memoryLoad) {
return treeList(
memoryLoad.stream()
diff --git a/src/main/java/org/shedlang/compiler/patio/operands/PatioIntConst.java b/src/main/java/org/shedlang/compiler/patio/operands/PatioIntConst.java
index d7734096..5b82844c 100644
--- a/src/main/java/org/shedlang/compiler/patio/operands/PatioIntConst.java
+++ b/src/main/java/org/shedlang/compiler/patio/operands/PatioIntConst.java
@@ -1,4 +1,10 @@
package org.shedlang.compiler.patio.operands;
-public record PatioIntConst(int value) implements PatioOperand {
+import org.shedlang.compiler.patio.values.PatioIntValue;
+import org.shedlang.compiler.patio.values.PatioIntValueLiteral;
+
+public record PatioIntConst(PatioIntValue value) implements PatioOperand {
+ public static PatioIntConst of(int value) {
+ return new PatioIntConst(new PatioIntValueLiteral(value));
+ }
}
diff --git a/src/main/java/org/shedlang/compiler/patio/types/PatioDataValueInt.java b/src/main/java/org/shedlang/compiler/patio/types/PatioDataValueInt.java
index 54053200..9fa2a091 100644
--- a/src/main/java/org/shedlang/compiler/patio/types/PatioDataValueInt.java
+++ b/src/main/java/org/shedlang/compiler/patio/types/PatioDataValueInt.java
@@ -1,6 +1,8 @@
package org.shedlang.compiler.patio.types;
-public record PatioDataValueInt(int value) implements PatioDataValue {
+import org.shedlang.compiler.patio.values.PatioIntValue;
+
+public record PatioDataValueInt(PatioIntValue value) implements PatioDataValue {
@Override
public PatioDataLayout layout() {
return PatioDataLayouts.INT;
diff --git a/src/main/java/org/shedlang/compiler/patio/types/PatioDataValues.java b/src/main/java/org/shedlang/compiler/patio/types/PatioDataValues.java
index fe1f6844..621489b7 100644
--- a/src/main/java/org/shedlang/compiler/patio/types/PatioDataValues.java
+++ b/src/main/java/org/shedlang/compiler/patio/types/PatioDataValues.java
@@ -1,5 +1,7 @@
package org.shedlang.compiler.patio.types;
+import org.shedlang.compiler.patio.values.PatioIntValueLiteral;
+
import java.util.List;
public class PatioDataValues {
@@ -25,7 +27,7 @@ public class PatioDataValues {
}
public static PatioDataValue int_(int value) {
- return new PatioDataValueInt(value);
+ return new PatioDataValueInt(new PatioIntValueLiteral(value));
}
public static PatioDataValue struct(List<PatioDataValue> elements) {
diff --git a/src/main/java/org/shedlang/compiler/patio/values/PatioIntValue.java b/src/main/java/org/shedlang/compiler/patio/values/PatioIntValue.java
new file mode 100644
index 00000000..3a95fe7c
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/patio/values/PatioIntValue.java
@@ -0,0 +1,4 @@
+package org.shedlang.compiler.patio.values;
+
+public sealed interface PatioIntValue permits PatioIntValueLiteral {
+}
diff --git a/src/main/java/org/shedlang/compiler/patio/values/PatioIntValueLiteral.java b/src/main/java/org/shedlang/compiler/patio/values/PatioIntValueLiteral.java
new file mode 100644
index 00000000..31f5437c
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/patio/values/PatioIntValueLiteral.java
@@ -0,0 +1,4 @@
+package org.shedlang.compiler.patio.values;
+
+public record PatioIntValueLiteral(int value) implements PatioIntValue {
+}
diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java
index 438ebac3..b83fc428 100644
--- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java
+++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java
@@ -1008,7 +1008,7 @@ public class ShidToPatio {
context = localResult.context();
return context
- .addInstruction(new PatioLocalStore(local, new PatioIntConst(value), source))
+ .addInstruction(new PatioLocalStore(local, PatioIntConst.of(value), source))
.temporaryPush(local);
}
}
diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioEffects.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioEffects.java
index 51f198e3..7467be19 100644
--- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioEffects.java
+++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioEffects.java
@@ -187,8 +187,8 @@ public class ShidToPatioEffects {
Optional.of(handlerMethod),
FIND_HANDLER_METHOD_DECLARATION.name(),
List.of(
- new PatioIntConst(bodyContext.effectTag(effect)),
- new PatioIntConst(method.fieldIndex())
+ PatioIntConst.of(bodyContext.effectTag(effect)),
+ PatioIntConst.of(method.fieldIndex())
),
source
));
@@ -307,8 +307,8 @@ public class ShidToPatioEffects {
Optional.of(effectHandler),
PUSH_HANDLER_DECLARATION.name(),
List.of(
- new PatioIntConst(context.effectTag(effect)),
- new PatioIntConst(effectInfo.methodCount())
+ PatioIntConst.of(context.effectTag(effect)),
+ PatioIntConst.of(effectInfo.methodCount())
),
source
));
@@ -319,7 +319,7 @@ public class ShidToPatioEffects {
SET_HANDLER_METHOD_DECLARATION.name(),
List.of(
effectHandler,
- new PatioIntConst(handlerMethodInfo.fieldIndex()),
+ PatioIntConst.of(handlerMethodInfo.fieldIndex()),
handlerMethodsByName.get(handlerMethodInfo.name())
),
source
diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java
index 32971b8b..918e04d4 100644
--- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java
+++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java
@@ -164,7 +164,7 @@ public class ShidToPatioGc {
return context.addInstruction(new PatioCall(
Optional.of(destination),
ALLOC.name(),
- List.of(size, offset, new PatioIntConst(childrenCount)),
+ List.of(size, offset, PatioIntConst.of(childrenCount)),
source
));
}
diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioInts.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioInts.java
index fee88bec..68b9547c 100644
--- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioInts.java
+++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioInts.java
@@ -81,7 +81,7 @@ public class ShidToPatioInts {
.addInstruction(new PatioCall(
Optional.of(destination),
INT_CREATE_DECLARATION.name(),
- List.of(new PatioIntConst(value.intValueExact())),
+ List.of(PatioIntConst.of(value.intValueExact())),
source
));
}
diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioStructs.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioStructs.java
index 0e36f05c..356859ec 100644
--- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioStructs.java
+++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioStructs.java
@@ -59,7 +59,7 @@ public class ShidToPatioStructs {
valuePointer,
structDataLayout,
TAG_OFFSET,
- new PatioIntConst(context.structTag(type)),
+ PatioIntConst.of(context.structTag(type)),
source
));
diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioTuples.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioTuples.java
index e86bc366..a5dacdd2 100644
--- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioTuples.java
+++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioTuples.java
@@ -31,7 +31,7 @@ public class ShidToPatioTuples {
if (elements.size() == 0) {
context = context.addInstruction(new PatioLocalStore(
tuple,
- new PatioIntConst(0),
+ PatioIntConst.of(0),
source
));