canonical path for intrinsics
This commit is contained in:
+178
-5
@@ -7,17 +7,21 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Locale;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
class IRVMIntrinsicRegistryParityTest {
|
||||
private static final Path RUNTIME_INTRINSICS_FILE = Path.of("../runtime/crates/console/prometeu-vm/src/builtins.rs");
|
||||
private static final String VM_ARCH_INTRINSICS_PATH = "docs/vm-arch/INTRINSICS.csv";
|
||||
private static final String RUNTIME_INTRINSICS_PATH = "crates/console/prometeu-vm/src/builtins.rs";
|
||||
private static final Pattern OWNER_PATTERN = Pattern.compile("owner:\\s*\"([^\"]+)\",");
|
||||
private static final Pattern NAME_PATTERN = Pattern.compile("name:\\s*\"([^\"]+)\",");
|
||||
private static final Pattern VERSION_PATTERN = Pattern.compile("version:\\s*(\\d+),");
|
||||
@@ -25,7 +29,7 @@ class IRVMIntrinsicRegistryParityTest {
|
||||
private static final String STRICT_PARITY_ENV = "PROMETEU_INTRINSIC_PARITY_STRICT";
|
||||
|
||||
@Test
|
||||
void registryResourceMustExposeExpectedCanonicalEntries() {
|
||||
void registryDefinitionsMustExposeExpectedCanonicalEntries() {
|
||||
final var snapshot = IRVMIntrinsicRegistry.snapshotByCanonicalIdentity();
|
||||
assertFalse(snapshot.isEmpty());
|
||||
assertEquals(23, snapshot.size());
|
||||
@@ -33,19 +37,69 @@ class IRVMIntrinsicRegistryParityTest {
|
||||
assertEquals(0x2033, snapshot.get("input.button.hold@1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void registryDefinitionsMustStayInSyncWithVmArchCsv() throws IOException {
|
||||
final var specPath = locateRepoRoot().resolve(VM_ARCH_INTRINSICS_PATH);
|
||||
assertTrue(Files.isRegularFile(specPath), "intrinsics spec is missing: " + specPath);
|
||||
|
||||
final var expected = parseVmArchIntrinsicRows(specPath);
|
||||
final var actual = Arrays.stream(IRVMIntrinsicDefinition.values())
|
||||
.map(IntrinsicCsvRow::fromDefinition)
|
||||
.toList();
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
void registryMustStayInSyncWithRuntimeBuiltinsTable() throws IOException {
|
||||
final var compilerSnapshot = IRVMIntrinsicRegistry.snapshotByCanonicalIdentity();
|
||||
if (!Files.exists(RUNTIME_INTRINSICS_FILE)) {
|
||||
final var runtimeIntrinsicsFile = locateRuntimeIntrinsicsFile();
|
||||
if (!Files.exists(runtimeIntrinsicsFile)) {
|
||||
if (strictParityEnabled()) {
|
||||
fail("runtime intrinsic table not found: " + RUNTIME_INTRINSICS_FILE.toAbsolutePath());
|
||||
fail("runtime intrinsic table not found: " + runtimeIntrinsicsFile.toAbsolutePath());
|
||||
}
|
||||
return;
|
||||
}
|
||||
final var runtimeSnapshot = parseRuntimeIntrinsicSnapshot(RUNTIME_INTRINSICS_FILE);
|
||||
final var runtimeSnapshot = parseRuntimeIntrinsicSnapshot(runtimeIntrinsicsFile);
|
||||
assertEquals(runtimeSnapshot, compilerSnapshot, parityDiff(runtimeSnapshot, compilerSnapshot));
|
||||
}
|
||||
|
||||
private List<IntrinsicCsvRow> parseVmArchIntrinsicRows(final Path specPath) throws IOException {
|
||||
final var lines = Files.readAllLines(specPath, StandardCharsets.UTF_8);
|
||||
final var rows = new ArrayList<IntrinsicCsvRow>();
|
||||
for (int index = 0; index < lines.size(); index++) {
|
||||
final var rawLine = lines.get(index);
|
||||
final var line = rawLine.trim();
|
||||
if (line.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
if (index == 0) {
|
||||
assertEquals(
|
||||
"final_id_hex,final_id_dec,canonical_name,canonical_version,owner,name,arg_slots,ret_slots,arg_layout,ret_layout,deterministic,may_allocate",
|
||||
line,
|
||||
"unexpected intrinsic csv header");
|
||||
continue;
|
||||
}
|
||||
final var columns = line.split(",", -1);
|
||||
if (columns.length != 12) {
|
||||
fail("invalid intrinsics csv row at line " + (index + 1) + ": " + rawLine);
|
||||
}
|
||||
rows.add(new IntrinsicCsvRow(
|
||||
normalizeHexId(columns[0]),
|
||||
parseUnsignedInt(columns[1], "final_id_dec", index + 1),
|
||||
requireNonBlank(columns[2], "canonical_name", index + 1),
|
||||
parseLong(columns[3], "canonical_version", index + 1),
|
||||
requireNonBlank(columns[4], "owner", index + 1),
|
||||
requireNonBlank(columns[5], "name", index + 1),
|
||||
parseInt(columns[6], "arg_slots", index + 1),
|
||||
parseInt(columns[7], "ret_slots", index + 1),
|
||||
columns[8].trim(),
|
||||
columns[9].trim(),
|
||||
parseBoolean(columns[10], "deterministic", index + 1),
|
||||
parseBoolean(columns[11], "may_allocate", index + 1)));
|
||||
}
|
||||
return List.copyOf(rows);
|
||||
}
|
||||
|
||||
private Map<String, Integer> parseRuntimeIntrinsicSnapshot(final Path runtimeFile) throws IOException {
|
||||
final var runtimeSource = Files.readAllLines(runtimeFile, StandardCharsets.UTF_8);
|
||||
final var mapped = new LinkedHashMap<String, Integer>();
|
||||
@@ -109,6 +163,27 @@ class IRVMIntrinsicRegistryParityTest {
|
||||
return Map.copyOf(mapped);
|
||||
}
|
||||
|
||||
private Path locateRepoRoot() {
|
||||
var cursor = Path.of(System.getProperty("user.dir")).toAbsolutePath().normalize();
|
||||
while (cursor != null) {
|
||||
if (Files.isDirectory(cursor.resolve("docs"))
|
||||
&& Files.isDirectory(cursor.resolve("prometeu-compiler"))
|
||||
&& Files.exists(cursor.resolve("settings.gradle.kts"))) {
|
||||
return cursor;
|
||||
}
|
||||
cursor = cursor.getParent();
|
||||
}
|
||||
fail("could not locate repository root from working directory");
|
||||
throw new IllegalStateException("unreachable");
|
||||
}
|
||||
|
||||
private Path locateRuntimeIntrinsicsFile() {
|
||||
return locateRepoRoot()
|
||||
.getParent()
|
||||
.resolve("runtime")
|
||||
.resolve(RUNTIME_INTRINSICS_PATH);
|
||||
}
|
||||
|
||||
private int parseRuntimeId(final String rawId) {
|
||||
if (rawId.startsWith("0x") || rawId.startsWith("0X")) {
|
||||
return Integer.parseUnsignedInt(rawId.substring(2), 16);
|
||||
@@ -116,6 +191,73 @@ class IRVMIntrinsicRegistryParityTest {
|
||||
return Integer.parseUnsignedInt(rawId, 10);
|
||||
}
|
||||
|
||||
private String requireNonBlank(
|
||||
final String value,
|
||||
final String fieldName,
|
||||
final int lineNumber) {
|
||||
final var normalized = value.trim();
|
||||
if (normalized.isBlank()) {
|
||||
fail("blank " + fieldName + " at line " + lineNumber);
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
private int parseInt(
|
||||
final String rawValue,
|
||||
final String fieldName,
|
||||
final int lineNumber) {
|
||||
try {
|
||||
return Integer.parseInt(rawValue.trim());
|
||||
} catch (NumberFormatException ex) {
|
||||
fail("invalid " + fieldName + " at line " + lineNumber + ": " + rawValue);
|
||||
throw new IllegalStateException("unreachable", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private long parseLong(
|
||||
final String rawValue,
|
||||
final String fieldName,
|
||||
final int lineNumber) {
|
||||
try {
|
||||
return Long.parseLong(rawValue.trim());
|
||||
} catch (NumberFormatException ex) {
|
||||
fail("invalid " + fieldName + " at line " + lineNumber + ": " + rawValue);
|
||||
throw new IllegalStateException("unreachable", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private int parseUnsignedInt(
|
||||
final String rawValue,
|
||||
final String fieldName,
|
||||
final int lineNumber) {
|
||||
try {
|
||||
return Integer.parseUnsignedInt(rawValue.trim(), 10);
|
||||
} catch (NumberFormatException ex) {
|
||||
fail("invalid " + fieldName + " at line " + lineNumber + ": " + rawValue);
|
||||
throw new IllegalStateException("unreachable", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean parseBoolean(
|
||||
final String rawValue,
|
||||
final String fieldName,
|
||||
final int lineNumber) {
|
||||
return switch (rawValue.trim().toLowerCase(Locale.ROOT)) {
|
||||
case "true" -> true;
|
||||
case "false" -> false;
|
||||
default -> {
|
||||
fail("invalid " + fieldName + " at line " + lineNumber + ": " + rawValue);
|
||||
yield false;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private String normalizeHexId(final String rawValue) {
|
||||
final var trimmed = rawValue.trim();
|
||||
final var parsed = parseRuntimeId(trimmed);
|
||||
return "0x" + Integer.toUnsignedString(parsed, 16).toUpperCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
private boolean strictParityEnabled() {
|
||||
final var strict = System.getenv(STRICT_PARITY_ENV);
|
||||
if (isTruthy(strict)) {
|
||||
@@ -160,4 +302,35 @@ class IRVMIntrinsicRegistryParityTest {
|
||||
+ "\nmissingInRuntime=" + List.copyOf(missingInRuntime)
|
||||
+ "\nidMismatches=" + List.copyOf(mismatchedIds);
|
||||
}
|
||||
|
||||
private record IntrinsicCsvRow(
|
||||
String finalIdHex,
|
||||
int finalIdDec,
|
||||
String canonicalName,
|
||||
long canonicalVersion,
|
||||
String owner,
|
||||
String name,
|
||||
int argSlots,
|
||||
int retSlots,
|
||||
String argLayout,
|
||||
String retLayout,
|
||||
boolean deterministic,
|
||||
boolean mayAllocate) {
|
||||
|
||||
private static IntrinsicCsvRow fromDefinition(final IRVMIntrinsicDefinition definition) {
|
||||
return new IntrinsicCsvRow(
|
||||
"0x" + Integer.toUnsignedString(definition.finalId(), 16).toUpperCase(Locale.ROOT),
|
||||
definition.finalId(),
|
||||
definition.canonicalName(),
|
||||
definition.canonicalVersion(),
|
||||
definition.owner(),
|
||||
definition.intrinsicName(),
|
||||
definition.argSlots(),
|
||||
definition.retSlots(),
|
||||
definition.argLayout(),
|
||||
definition.retLayout(),
|
||||
definition.deterministic(),
|
||||
definition.mayAllocate());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user