implements PR-08.5
This commit is contained in:
+75
-29
@@ -1,11 +1,16 @@
|
||||
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 Map<IntrinsicKey, Integer> FINAL_ID_BY_INTRINSIC = createRegistry();
|
||||
private static final String REGISTRY_RESOURCE = "/intrinsics/registry-v1.csv";
|
||||
private static final Map<IntrinsicKey, Integer> FINAL_ID_BY_INTRINSIC = loadRegistry();
|
||||
|
||||
private IRVMIntrinsicRegistry() {
|
||||
}
|
||||
@@ -23,38 +28,79 @@ final class IRVMIntrinsicRegistry {
|
||||
return OptionalInt.of(resolved);
|
||||
}
|
||||
|
||||
private static Map<IntrinsicKey, Integer> createRegistry() {
|
||||
static Map<String, Integer> snapshotByCanonicalIdentity() {
|
||||
final var snapshot = new HashMap<String, Integer>(FINAL_ID_BY_INTRINSIC.size());
|
||||
for (final var entry : FINAL_ID_BY_INTRINSIC.entrySet()) {
|
||||
snapshot.put(canonicalIdentity(entry.getKey().canonicalName(), entry.getKey().canonicalVersion()), entry.getValue());
|
||||
}
|
||||
return Map.copyOf(snapshot);
|
||||
}
|
||||
|
||||
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>();
|
||||
register(registry, "vec2.dot", 1, 0x1000);
|
||||
register(registry, "vec2.length", 1, 0x1001);
|
||||
|
||||
register(registry, "input.pad", 1, 0x2000);
|
||||
register(registry, "input.touch", 1, 0x2001);
|
||||
|
||||
register(registry, "input.pad.up", 1, 0x2010);
|
||||
register(registry, "input.pad.down", 1, 0x2011);
|
||||
register(registry, "input.pad.left", 1, 0x2012);
|
||||
register(registry, "input.pad.right", 1, 0x2013);
|
||||
register(registry, "input.pad.a", 1, 0x2014);
|
||||
register(registry, "input.pad.b", 1, 0x2015);
|
||||
register(registry, "input.pad.x", 1, 0x2016);
|
||||
register(registry, "input.pad.y", 1, 0x2017);
|
||||
register(registry, "input.pad.l", 1, 0x2018);
|
||||
register(registry, "input.pad.r", 1, 0x2019);
|
||||
register(registry, "input.pad.start", 1, 0x201A);
|
||||
register(registry, "input.pad.select", 1, 0x201B);
|
||||
|
||||
register(registry, "input.touch.button", 1, 0x2020);
|
||||
register(registry, "input.touch.x", 1, 0x2021);
|
||||
register(registry, "input.touch.y", 1, 0x2022);
|
||||
|
||||
register(registry, "input.button.pressed", 1, 0x2030);
|
||||
register(registry, "input.button.released", 1, 0x2031);
|
||||
register(registry, "input.button.down", 1, 0x2032);
|
||||
register(registry, "input.button.hold", 1, 0x2033);
|
||||
final var identityByFinalId = new HashMap<Integer, String>();
|
||||
try (final var reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
|
||||
String rawLine;
|
||||
int lineNumber = 0;
|
||||
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);
|
||||
}
|
||||
final int finalId = parseFinalId(columns[2].trim(), 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));
|
||||
}
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
throw new IllegalStateException("failed to read intrinsic registry resource: " + REGISTRY_RESOURCE, ex);
|
||||
}
|
||||
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) {
|
||||
return canonicalName + "@" + canonicalVersion;
|
||||
}
|
||||
|
||||
private static void register(
|
||||
final Map<IntrinsicKey, Integer> registry,
|
||||
final String canonicalName,
|
||||
|
||||
Reference in New Issue
Block a user