canonical path for intrinsics
This commit is contained in:
+142
@@ -0,0 +1,142 @@
|
||||
package p.studio.compiler.backend.irvm;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
enum IRVMIntrinsicDefinition {
|
||||
VEC2_DOT("vec2.dot", 1, 0x1000, "vec2", "dot", 4, 1, "float|float|float|float", "float", true, false),
|
||||
VEC2_LENGTH("vec2.length", 1, 0x1001, "vec2", "length", 2, 1, "float|float", "float", true, false),
|
||||
INPUT_PAD("input.pad", 1, 0x2000, "input", "pad", 0, 1, "", "builtin:input.pad", true, false),
|
||||
INPUT_TOUCH("input.touch", 1, 0x2001, "input", "touch", 0, 1, "", "builtin:input.touch", true, false),
|
||||
INPUT_PAD_UP("input.pad.up", 1, 0x2010, "input.pad", "up", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
|
||||
INPUT_PAD_DOWN("input.pad.down", 1, 0x2011, "input.pad", "down", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
|
||||
INPUT_PAD_LEFT("input.pad.left", 1, 0x2012, "input.pad", "left", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
|
||||
INPUT_PAD_RIGHT("input.pad.right", 1, 0x2013, "input.pad", "right", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
|
||||
INPUT_PAD_A("input.pad.a", 1, 0x2014, "input.pad", "a", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
|
||||
INPUT_PAD_B("input.pad.b", 1, 0x2015, "input.pad", "b", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
|
||||
INPUT_PAD_X("input.pad.x", 1, 0x2016, "input.pad", "x", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
|
||||
INPUT_PAD_Y("input.pad.y", 1, 0x2017, "input.pad", "y", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
|
||||
INPUT_PAD_L("input.pad.l", 1, 0x2018, "input.pad", "l", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
|
||||
INPUT_PAD_R("input.pad.r", 1, 0x2019, "input.pad", "r", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
|
||||
INPUT_PAD_START("input.pad.start", 1, 0x201A, "input.pad", "start", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
|
||||
INPUT_PAD_SELECT("input.pad.select", 1, 0x201B, "input.pad", "select", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
|
||||
INPUT_TOUCH_BUTTON("input.touch.button", 1, 0x2020, "input.touch", "button", 1, 1, "builtin:input.touch", "builtin:input.button", true, false),
|
||||
INPUT_TOUCH_X("input.touch.x", 1, 0x2021, "input.touch", "x", 1, 1, "builtin:input.touch", "int", true, false),
|
||||
INPUT_TOUCH_Y("input.touch.y", 1, 0x2022, "input.touch", "y", 1, 1, "builtin:input.touch", "int", true, false),
|
||||
INPUT_BUTTON_PRESSED("input.button.pressed", 1, 0x2030, "input.button", "pressed", 1, 1, "builtin:input.button", "bool", true, false),
|
||||
INPUT_BUTTON_RELEASED("input.button.released", 1, 0x2031, "input.button", "released", 1, 1, "builtin:input.button", "bool", true, false),
|
||||
INPUT_BUTTON_DOWN("input.button.down", 1, 0x2032, "input.button", "down", 1, 1, "builtin:input.button", "bool", true, false),
|
||||
INPUT_BUTTON_HOLD("input.button.hold", 1, 0x2033, "input.button", "hold", 1, 1, "builtin:input.button", "int", true, false);
|
||||
|
||||
private final String canonicalName;
|
||||
private final long canonicalVersion;
|
||||
private final int finalId;
|
||||
private final String owner;
|
||||
private final String name;
|
||||
private final int argSlots;
|
||||
private final int retSlots;
|
||||
private final String argLayout;
|
||||
private final String retLayout;
|
||||
private final boolean deterministic;
|
||||
private final boolean mayAllocate;
|
||||
|
||||
IRVMIntrinsicDefinition(
|
||||
final String canonicalName,
|
||||
final long canonicalVersion,
|
||||
final int finalId,
|
||||
final String owner,
|
||||
final String name,
|
||||
final int argSlots,
|
||||
final int retSlots,
|
||||
final String argLayout,
|
||||
final String retLayout,
|
||||
final boolean deterministic,
|
||||
final boolean mayAllocate) {
|
||||
this.canonicalName = requireNonBlank(canonicalName, "canonicalName");
|
||||
this.canonicalVersion = requireNonNegative(canonicalVersion, "canonicalVersion");
|
||||
this.finalId = finalId;
|
||||
this.owner = requireNonBlank(owner, "owner");
|
||||
this.name = requireNonBlank(name, "name");
|
||||
this.argSlots = requireNonNegative(argSlots, "argSlots");
|
||||
this.retSlots = requireNonNegative(retSlots, "retSlots");
|
||||
this.argLayout = Objects.requireNonNull(argLayout, "argLayout");
|
||||
this.retLayout = Objects.requireNonNull(retLayout, "retLayout");
|
||||
this.deterministic = deterministic;
|
||||
this.mayAllocate = mayAllocate;
|
||||
}
|
||||
|
||||
String canonicalName() {
|
||||
return canonicalName;
|
||||
}
|
||||
|
||||
long canonicalVersion() {
|
||||
return canonicalVersion;
|
||||
}
|
||||
|
||||
int finalId() {
|
||||
return finalId;
|
||||
}
|
||||
|
||||
String owner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
String intrinsicName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
int argSlots() {
|
||||
return argSlots;
|
||||
}
|
||||
|
||||
int retSlots() {
|
||||
return retSlots;
|
||||
}
|
||||
|
||||
String argLayout() {
|
||||
return argLayout;
|
||||
}
|
||||
|
||||
String retLayout() {
|
||||
return retLayout;
|
||||
}
|
||||
|
||||
boolean deterministic() {
|
||||
return deterministic;
|
||||
}
|
||||
|
||||
boolean mayAllocate() {
|
||||
return mayAllocate;
|
||||
}
|
||||
|
||||
String canonicalIdentity() {
|
||||
return canonicalName + "@" + canonicalVersion;
|
||||
}
|
||||
|
||||
private static String requireNonBlank(
|
||||
final String value,
|
||||
final String fieldName) {
|
||||
final var normalized = Objects.requireNonNull(value, fieldName);
|
||||
if (normalized.isBlank()) {
|
||||
throw new IllegalArgumentException(fieldName + " must not be blank");
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
private static int requireNonNegative(
|
||||
final int value,
|
||||
final String fieldName) {
|
||||
if (value < 0) {
|
||||
throw new IllegalArgumentException(fieldName + " must be non-negative");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private static long requireNonNegative(
|
||||
final long value,
|
||||
final String fieldName) {
|
||||
if (value < 0) {
|
||||
throw new IllegalArgumentException(fieldName + " must be non-negative");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
+15
-69
@@ -1,15 +1,10 @@
|
||||
package p.studio.compiler.backend.irvm;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.OptionalInt;
|
||||
|
||||
final class IRVMIntrinsicRegistry {
|
||||
private static final String REGISTRY_RESOURCE = "/intrinsics/registry-v1.csv";
|
||||
private static final Map<IntrinsicKey, Integer> FINAL_ID_BY_INTRINSIC = loadRegistry();
|
||||
|
||||
private IRVMIntrinsicRegistry() {
|
||||
@@ -37,77 +32,28 @@ final class IRVMIntrinsicRegistry {
|
||||
}
|
||||
|
||||
private static Map<IntrinsicKey, Integer> loadRegistry() {
|
||||
final var stream = IRVMIntrinsicRegistry.class.getResourceAsStream(REGISTRY_RESOURCE);
|
||||
if (stream == null) {
|
||||
throw new IllegalStateException("missing intrinsic registry resource: " + REGISTRY_RESOURCE);
|
||||
}
|
||||
|
||||
final var registry = new HashMap<IntrinsicKey, Integer>();
|
||||
final var identityByFinalId = new HashMap<Integer, String>();
|
||||
try (final var reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
|
||||
String rawLine;
|
||||
int lineNumber = 0;
|
||||
Integer previousFinalId = null;
|
||||
while ((rawLine = reader.readLine()) != null) {
|
||||
lineNumber++;
|
||||
final var line = rawLine.trim();
|
||||
if (line.isBlank() || line.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
final var columns = line.split(",", -1);
|
||||
if (columns.length != 3) {
|
||||
throw new IllegalStateException("invalid intrinsic registry row at line " + lineNumber + ": " + line);
|
||||
}
|
||||
final var canonicalName = columns[0].trim();
|
||||
final long canonicalVersion;
|
||||
try {
|
||||
canonicalVersion = Long.parseLong(columns[1].trim());
|
||||
} catch (NumberFormatException ex) {
|
||||
throw new IllegalStateException("invalid intrinsic version at line " + lineNumber + ": " + line, ex);
|
||||
}
|
||||
if (canonicalName.isBlank()) {
|
||||
throw new IllegalStateException("blank intrinsic name at line " + lineNumber + ": " + line);
|
||||
}
|
||||
if (canonicalVersion < 0) {
|
||||
throw new IllegalStateException("negative intrinsic version at line " + lineNumber + ": " + line);
|
||||
}
|
||||
final int finalId = parseFinalId(columns[2].trim(), lineNumber, line);
|
||||
if (previousFinalId != null && Integer.compareUnsigned(finalId, previousFinalId) <= 0) {
|
||||
throw new IllegalStateException(
|
||||
"intrinsic final ids must be strictly increasing for deterministic evolution at line "
|
||||
+ lineNumber + ": " + line);
|
||||
}
|
||||
register(registry, canonicalName, canonicalVersion, finalId);
|
||||
final var identity = canonicalIdentity(canonicalName, canonicalVersion);
|
||||
final var previous = identityByFinalId.putIfAbsent(finalId, identity);
|
||||
if (previous != null && !previous.equals(identity)) {
|
||||
throw new IllegalStateException(
|
||||
"duplicate intrinsic final id 0x%08X for %s and %s".formatted(finalId, previous, identity));
|
||||
}
|
||||
previousFinalId = finalId;
|
||||
Integer previousFinalId = null;
|
||||
for (final var definition : IRVMIntrinsicDefinition.values()) {
|
||||
final var finalId = definition.finalId();
|
||||
if (previousFinalId != null && Integer.compareUnsigned(finalId, previousFinalId) <= 0) {
|
||||
throw new IllegalStateException(
|
||||
"intrinsic final ids must be strictly increasing for deterministic evolution: "
|
||||
+ definition.canonicalIdentity());
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
throw new IllegalStateException("failed to read intrinsic registry resource: " + REGISTRY_RESOURCE, ex);
|
||||
register(registry, definition.canonicalName(), definition.canonicalVersion(), finalId);
|
||||
final var identity = definition.canonicalIdentity();
|
||||
final var previous = identityByFinalId.putIfAbsent(finalId, identity);
|
||||
if (previous != null && !previous.equals(identity)) {
|
||||
throw new IllegalStateException(
|
||||
"duplicate intrinsic final id 0x%08X for %s and %s".formatted(finalId, previous, identity));
|
||||
}
|
||||
previousFinalId = finalId;
|
||||
}
|
||||
return Map.copyOf(registry);
|
||||
}
|
||||
|
||||
private static int parseFinalId(
|
||||
final String rawId,
|
||||
final int lineNumber,
|
||||
final String line) {
|
||||
try {
|
||||
if (rawId.startsWith("0x") || rawId.startsWith("0X")) {
|
||||
return Integer.parseUnsignedInt(rawId.substring(2), 16);
|
||||
}
|
||||
return Integer.parseUnsignedInt(rawId, 10);
|
||||
} catch (NumberFormatException ex) {
|
||||
throw new IllegalStateException(
|
||||
"invalid intrinsic final id at line %d: %s".formatted(lineNumber, line),
|
||||
ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static String canonicalIdentity(
|
||||
final String canonicalName,
|
||||
final long canonicalVersion) {
|
||||
|
||||
Reference in New Issue
Block a user