implements PR-08.5

This commit is contained in:
2026-03-24 13:42:33 +00:00
parent 505a4f72ce
commit 12a6602b0a
3 changed files with 262 additions and 29 deletions
@@ -0,0 +1,163 @@
package p.studio.compiler.backend.irvm;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
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.fail;
class IRVMIntrinsicRegistryParityTest {
private static final Path RUNTIME_INTRINSICS_FILE = Path.of("../runtime/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+),");
private static final Pattern ID_PATTERN = Pattern.compile("id:\\s*(0x[0-9A-Fa-f]+|\\d+),");
private static final String STRICT_PARITY_ENV = "PROMETEU_INTRINSIC_PARITY_STRICT";
@Test
void registryResourceMustExposeExpectedCanonicalEntries() {
final var snapshot = IRVMIntrinsicRegistry.snapshotByCanonicalIdentity();
assertFalse(snapshot.isEmpty());
assertEquals(23, snapshot.size());
assertEquals(0x2000, snapshot.get("input.pad@1"));
assertEquals(0x2033, snapshot.get("input.button.hold@1"));
}
@Test
void registryMustStayInSyncWithRuntimeBuiltinsTable() throws IOException {
final var compilerSnapshot = IRVMIntrinsicRegistry.snapshotByCanonicalIdentity();
if (!Files.exists(RUNTIME_INTRINSICS_FILE)) {
if (strictParityEnabled()) {
fail("runtime intrinsic table not found: " + RUNTIME_INTRINSICS_FILE.toAbsolutePath());
}
return;
}
final var runtimeSnapshot = parseRuntimeIntrinsicSnapshot(RUNTIME_INTRINSICS_FILE);
assertEquals(runtimeSnapshot, compilerSnapshot, parityDiff(runtimeSnapshot, compilerSnapshot));
}
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>();
var insideIntrinsicsArray = false;
var insideEntry = false;
String owner = null;
String name = null;
Long version = null;
Integer id = null;
for (final var rawLine : runtimeSource) {
final var line = rawLine.trim();
if (!insideIntrinsicsArray) {
if (line.startsWith("const INTRINSICS:")) {
insideIntrinsicsArray = true;
}
continue;
}
if (line.equals("];")) {
break;
}
if (line.startsWith("IntrinsicMeta {")) {
insideEntry = true;
owner = null;
name = null;
version = null;
id = null;
continue;
}
if (!insideEntry) {
continue;
}
final var ownerMatcher = OWNER_PATTERN.matcher(line);
if (ownerMatcher.find()) {
owner = ownerMatcher.group(1);
}
final var nameMatcher = NAME_PATTERN.matcher(line);
if (nameMatcher.find()) {
name = nameMatcher.group(1);
}
final var versionMatcher = VERSION_PATTERN.matcher(line);
if (versionMatcher.find()) {
version = Long.parseLong(versionMatcher.group(1));
}
final var idMatcher = ID_PATTERN.matcher(line);
if (idMatcher.find()) {
id = parseRuntimeId(idMatcher.group(1));
}
if (line.equals("},")) {
if (owner == null || name == null || version == null || id == null) {
throw new IllegalStateException("invalid runtime intrinsic entry near line: " + rawLine);
}
final var identity = owner + "." + name + "@" + version;
final var previous = mapped.putIfAbsent(identity, id);
if (previous != null && previous != id) {
throw new IllegalStateException(
"runtime intrinsic identity collision for " + identity + ": " + previous + " vs " + id);
}
insideEntry = false;
}
}
return Map.copyOf(mapped);
}
private int parseRuntimeId(final String rawId) {
if (rawId.startsWith("0x") || rawId.startsWith("0X")) {
return Integer.parseUnsignedInt(rawId.substring(2), 16);
}
return Integer.parseUnsignedInt(rawId, 10);
}
private boolean strictParityEnabled() {
final var strict = System.getenv(STRICT_PARITY_ENV);
if (isTruthy(strict)) {
return true;
}
return isTruthy(System.getenv("CI"));
}
private boolean isTruthy(final String value) {
if (value == null) {
return false;
}
return switch (value.trim().toLowerCase()) {
case "1", "true", "yes", "on" -> true;
default -> false;
};
}
private String parityDiff(
final Map<String, Integer> runtimeSnapshot,
final Map<String, Integer> compilerSnapshot) {
final var missingInCompiler = new ArrayList<String>();
final var missingInRuntime = new ArrayList<String>();
final var mismatchedIds = new ArrayList<String>();
for (final var runtimeEntry : runtimeSnapshot.entrySet()) {
final var compilerId = compilerSnapshot.get(runtimeEntry.getKey());
if (compilerId == null) {
missingInCompiler.add(runtimeEntry.getKey());
continue;
}
if (!runtimeEntry.getValue().equals(compilerId)) {
mismatchedIds.add(runtimeEntry.getKey() + " runtime=" + runtimeEntry.getValue() + " compiler=" + compilerId);
}
}
for (final var compilerEntry : compilerSnapshot.entrySet()) {
if (!runtimeSnapshot.containsKey(compilerEntry.getKey())) {
missingInRuntime.add(compilerEntry.getKey());
}
}
return "intrinsic registry parity mismatch"
+ "\nmissingInCompiler=" + List.copyOf(missingInCompiler)
+ "\nmissingInRuntime=" + List.copyOf(missingInRuntime)
+ "\nidMismatches=" + List.copyOf(mismatchedIds);
}
}