implements PLN-0118

This commit is contained in:
2026-07-15 12:13:40 +01:00
parent bde999f1fe
commit e8079b67fb
8 changed files with 185 additions and 14 deletions
@@ -40,7 +40,7 @@ public class LowerToIRVMService {
"IRBackend has no executable functions");
}
validatePbsLifecycleStructure(backend);
validateCommonLifecycleStructure(backend);
final var ordered = orderFunctions(backend);
final var funcIdByCallableId = new HashMap<CallableId, Integer>();
for (var i = 0; i < ordered.size(); i++) {
@@ -499,7 +499,7 @@ public class LowerToIRVMService {
return ReadOnlyList.wrap(ordered);
}
private void validatePbsLifecycleStructure(final IRBackend backend) {
private void validateCommonLifecycleStructure(final IRBackend backend) {
if (backend.getSyntheticFunctions().isEmpty()) {
return;
}
@@ -229,6 +229,35 @@ class LowerToIRVMServiceTest {
assertTrue(emissionOps.stream().allMatch(op -> op.span() != null));
}
@Test
void lowerMustAcceptManuallyConstructedCommonIRBackend() {
final var backend = IRBackend.builder()
.entryPointCallableName("tick")
.entryPointModuleId(new ModuleId(0))
.modulePool(ReadOnlyList.from(new ModuleReference("game", ReadOnlyList.from("runtime"))))
.executableFunctions(ReadOnlyList.from(
fnWithModule("tick", "game/runtime", 0, 10, ReadOnlyList.from(
callHost("timer", "poll", 1, 0, 0),
callIntrinsic("input.pad", 1, 0),
callFuncWithExpected("game/runtime", "helper", 20, 0, 0),
ret())),
fnWithModule("helper", "game/runtime", 0, 20, ReadOnlyList.from(
ret()))))
.intrinsicPool(ReadOnlyList.from(new IntrinsicReference("input.pad", 1)))
.build();
final var lowered = new LowerToIRVMService().lower(backend);
assertEquals(2, lowered.module().functions().size());
assertEquals("tick", lowered.module().functions().getFirst().name());
final var instructions = lowered.module().functions().getFirst().instructions();
assertEquals(IRVMOp.HOSTCALL, instructions.get(0).op());
assertEquals(IRVMOp.INTRINSIC, instructions.get(1).op());
assertEquals(IRVMOp.CALL, instructions.get(2).op());
assertEquals(1, instructions.get(2).immediate());
assertEquals(IRVMOp.RET, instructions.get(3).op());
}
@Test
void lowerMustRejectUnterminatedFunction() {
final var backend = IRBackend.builder()
@@ -36,6 +36,9 @@ class BackendConformanceMatrixSpecTest {
"G19-5.2.6",
"G19-5.2.7",
"G19-5.2.8",
"G20-4.1.1",
"G20-4.1.2",
"G20-4.1.3",
"G20-6.2",
"G20-6.3",
"G20-6.4",
@@ -0,0 +1,60 @@
package p.studio.compiler.specs;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
class CommonBackendArchitectureTest {
private static final String BACKEND_SOURCE_ROOT =
"prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend";
private static final String FORBIDDEN_PBS_PACKAGE = "p.studio.compiler.pbs";
@Test
void commonBackendMustNotImportPbsFrontendPackages() throws IOException {
final var backendRoot = locateRepoRoot().resolve(BACKEND_SOURCE_ROOT);
final var violations = new ArrayList<String>();
try (final var paths = Files.walk(backendRoot)) {
paths.filter(path -> path.toString().endsWith(".java"))
.forEach(path -> assertNoPbsImport(path, violations));
}
assertTrue(
violations.isEmpty(),
"common backend must not import PBS frontend packages: " + violations);
}
private static void assertNoPbsImport(
final Path path,
final ArrayList<String> violations) {
try {
final var content = Files.readString(path);
if (content.contains("import " + FORBIDDEN_PBS_PACKAGE)
|| content.contains("import static " + FORBIDDEN_PBS_PACKAGE)) {
violations.add(locateRepoRoot().relativize(path).toString());
}
} catch (IOException e) {
throw new IllegalStateException("failed to read source file: " + path, e);
}
}
private static Path locateRepoRoot() {
var cursor = Path.of(System.getProperty("user.dir")).toAbsolutePath().normalize();
while (cursor != null) {
final var hasDocs = Files.isDirectory(cursor.resolve("docs/specs/compiler"));
final var hasCompiler = Files.isDirectory(cursor.resolve("prometeu-compiler"));
if (hasDocs && hasCompiler) {
return cursor;
}
cursor = cursor.getParent();
}
fail("could not locate repository root from working directory");
throw new IllegalStateException("unreachable");
}
}
@@ -11,9 +11,75 @@ import p.studio.compiler.source.tables.IntrinsicReference;
import p.studio.compiler.source.tables.ModuleReference;
import p.studio.utilities.structures.ReadOnlyList;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class IRBackendExecutableContractTest {
private static final String FORBIDDEN_PBS_PACKAGE = "p.studio.compiler.pbs";
private static final List<Class<?>> PUBLIC_CONTRACT_TYPES = List.of(
IRBackend.class,
IRBackend.IRBackendAggregator.class,
IRBackendReader.class,
IRBackendFile.class,
IRBackendExecutableFunction.class,
IRBackendExecutableFunction.Instruction.class,
IRBackendExecutableFunction.InstructionKind.class,
IRBackendExecutableFunction.HostCallMetadata.class,
IRBackendExecutableFunction.IntrinsicCallMetadata.class,
IRFunction.class,
IRSyntheticFunction.class,
IRSyntheticCallableKind.class,
IRSyntheticOrigin.class,
IRGlobal.class,
IRGlobalOrigin.class,
IRGlobalVisibility.class,
IRHiddenGlobalKind.class,
IRReservedMetadata.class,
IRReservedMetadata.HostMethodBinding.class,
IRReservedMetadata.BuiltinTypeSurface.class,
IRReservedMetadata.BuiltinFieldSurface.class,
IRReservedMetadata.IntrinsicSurface.class,
IRReservedMetadata.BuiltinConstSurface.class);
@Test
void publicIRBackendContractMustNotExposePbsTypes() {
final var violations = new ArrayList<String>();
for (final var contractType : PUBLIC_CONTRACT_TYPES) {
inspectType(contractType.getName(), contractType, violations);
for (final var field : contractType.getFields()) {
inspectType(contractType.getName() + "#" + field.getName(), field.getGenericType(), violations);
}
for (final var constructor : contractType.getConstructors()) {
for (final var parameterType : constructor.getGenericParameterTypes()) {
inspectType(contractType.getName() + "#<init>", parameterType, violations);
}
}
for (final var method : contractType.getMethods()) {
if (method.getDeclaringClass().equals(Object.class)
|| !Modifier.isPublic(method.getModifiers())) {
continue;
}
inspectType(contractType.getName() + "#" + method.getName(), method.getGenericReturnType(), violations);
for (final var parameterType : method.getGenericParameterTypes()) {
inspectType(contractType.getName() + "#" + method.getName(), parameterType, violations);
}
}
if (contractType.isRecord()) {
for (final var component : contractType.getRecordComponents()) {
inspectType(contractType.getName() + "#" + component.getName(), component.getGenericType(), violations);
}
}
}
assertTrue(
violations.isEmpty(),
"public IRBackend contract must not expose PBS frontend types: " + violations);
}
@Test
void callInstructionMustRequireCategorySpecificMetadata() {
@@ -366,4 +432,17 @@ class IRBackendExecutableContractTest {
assertEquals(0, backend.getCallableSignatures().get(0).moduleId().getIndex());
assertEquals(1, backend.getCallableSignatures().get(1).moduleId().getIndex());
}
private static void inspectType(
final String owner,
final Type type,
final ArrayList<String> violations) {
if (type == null) {
return;
}
final var typeName = type.getTypeName();
if (typeName.contains(FORBIDDEN_PBS_PACKAGE)) {
violations.add(owner + " -> " + typeName);
}
}
}