hardens IRGlobal structural metadata for PBS lifecycle lowering

This commit is contained in:
2026-03-26 20:00:16 +00:00
parent 14eaa0e0c0
commit 64762ca227
11 changed files with 185 additions and 38 deletions
@@ -5,6 +5,8 @@ import p.studio.compiler.models.IRBackendExecutableFunction;
import p.studio.compiler.models.IRBackendFile;
import p.studio.compiler.models.IRFunction;
import p.studio.compiler.models.IRGlobal;
import p.studio.compiler.models.IRGlobalOrigin;
import p.studio.compiler.models.IRGlobalVisibility;
import p.studio.compiler.models.IRReservedMetadata;
import p.studio.compiler.models.IRSyntheticCallableKind;
import p.studio.compiler.models.IRSyntheticFunction;
@@ -243,6 +245,7 @@ public final class PbsFrontendCompiler {
final ModuleId moduleId,
final PbsAst.File ast) {
final var globals = new ArrayList<IRGlobal>();
var nextSlot = 0;
for (final var topDecl : ast.topDecls()) {
if (!(topDecl instanceof PbsAst.GlobalDecl globalDecl)) {
continue;
@@ -252,6 +255,16 @@ public final class PbsFrontendCompiler {
moduleId,
globalDecl.name(),
PbsReservedMetadataExtractor.typeSurfaceKey(globalDecl.explicitType()),
nextSlot++,
IRGlobalVisibility.MODULE,
false,
p.studio.compiler.models.IRHiddenGlobalKind.NONE,
new IRGlobalOrigin(
fileId,
moduleId,
globalDecl.name(),
globalDecl.span(),
globalDecl.name()),
globalDecl.span()));
}
return ReadOnlyList.wrap(globals);
@@ -5,6 +5,10 @@ import p.studio.compiler.messages.BuildingIssueSink;
import p.studio.compiler.messages.FrontendPhaseContext;
import p.studio.compiler.models.IRBackend;
import p.studio.compiler.models.IRBackendExecutableFunction;
import p.studio.compiler.models.IRGlobal;
import p.studio.compiler.models.IRGlobalOrigin;
import p.studio.compiler.models.IRGlobalVisibility;
import p.studio.compiler.models.IRHiddenGlobalKind;
import p.studio.compiler.models.IRSyntheticCallableKind;
import p.studio.compiler.pbs.PbsFrontendCompiler;
import p.studio.compiler.pbs.PbsReservedMetadataExtractor;
@@ -285,22 +289,34 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
if (globals.stream().noneMatch(global ->
moduleIdsMatch(entryPointModuleId, global.moduleId())
&& PbsFrontendCompiler.bootGuardGlobalName(entryPointModuleId).equals(global.name()))) {
globals.add(new p.studio.compiler.models.IRGlobal(
&& global.hiddenKind() == IRHiddenGlobalKind.BOOT_GUARD)) {
globals.add(new IRGlobal(
frameExecutable.fileId(),
entryPointModuleId,
PbsFrontendCompiler.bootGuardGlobalName(entryPointModuleId),
"bool",
0,
IRGlobalVisibility.MODULE,
true,
IRHiddenGlobalKind.BOOT_GUARD,
new IRGlobalOrigin(
frameExecutable.fileId(),
entryPointModuleId,
frameAnchorsByModule.getOrDefault(entryPointModuleId, frameExecutable.callableName()),
frameExecutable.span(),
"BOOT_GUARD"),
frameExecutable.span()));
}
}
final var normalizedGlobals = normalizeGlobalSlots(globals);
return IRBackend.builder()
.entryPointCallableName(entryPointCallableName)
.entryPointModuleId(entryPointModuleId)
.functions(baseBackend.getFunctions())
.syntheticFunctions(baseBackend.getSyntheticFunctions())
.globals(ReadOnlyList.wrap(globals))
.globals(ReadOnlyList.wrap(normalizedGlobals))
.executableFunctions(ReadOnlyList.wrap(executableFunctions))
.modulePool(baseBackend.getModulePool())
.callableSignatures(ReadOnlyList.wrap(callableSignatures))
@@ -309,6 +325,28 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
.build();
}
private ArrayList<IRGlobal> normalizeGlobalSlots(final ArrayList<IRGlobal> globals) {
final var nextSlotByModule = new LinkedHashMap<ModuleId, Integer>();
final var normalized = new ArrayList<IRGlobal>(globals.size());
for (final var global : globals) {
final var moduleId = normalizeModuleId(global.moduleId());
final var slot = nextSlotByModule.getOrDefault(moduleId, 0);
nextSlotByModule.put(moduleId, slot + 1);
normalized.add(new IRGlobal(
global.fileId(),
moduleId,
global.name(),
global.declaredTypeSurface(),
slot,
global.visibility(),
global.isHidden(),
global.hiddenKind(),
global.origin(),
global.span()));
}
return normalized;
}
private IRBackendExecutableFunction resolveExecutable(
final ArrayList<IRBackendExecutableFunction> executableFunctions,
final ModuleId moduleId,
@@ -2,6 +2,7 @@ package p.studio.compiler.pbs;
import org.junit.jupiter.api.Test;
import p.studio.compiler.messages.HostAdmissionContext;
import p.studio.compiler.models.IRHiddenGlobalKind;
import p.studio.compiler.models.IRSyntheticCallableKind;
import p.studio.compiler.models.SourceKind;
import p.studio.compiler.pbs.lexer.LexErrors;
@@ -12,6 +13,7 @@ import p.studio.compiler.source.identifiers.FileId;
import p.studio.utilities.structures.ReadOnlyList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class PbsFrontendCompilerTest {
@@ -60,6 +62,10 @@ class PbsFrontendCompilerTest {
assertTrue(diagnostics.isEmpty(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
assertEquals(1, fileBackend.globals().size());
assertEquals("SCORE", fileBackend.globals().getFirst().name());
assertEquals(0, fileBackend.globals().getFirst().slot());
assertFalse(fileBackend.globals().getFirst().isHidden());
assertEquals(IRHiddenGlobalKind.NONE, fileBackend.globals().getFirst().hiddenKind());
assertEquals("SCORE", fileBackend.globals().getFirst().origin().derivedFromUserSymbol());
assertEquals(4, fileBackend.syntheticFunctions().size());
assertTrue(fileBackend.syntheticFunctions().stream().anyMatch(fn -> fn.kind() == IRSyntheticCallableKind.FILE_INIT_FRAGMENT));
assertTrue(fileBackend.syntheticFunctions().stream().anyMatch(fn -> fn.kind() == IRSyntheticCallableKind.MODULE_INIT));
@@ -5,6 +5,7 @@ import org.junit.jupiter.api.io.TempDir;
import p.studio.compiler.messages.BuildingIssueSink;
import p.studio.compiler.messages.FrontendPhaseContext;
import p.studio.compiler.models.BuildStack;
import p.studio.compiler.models.IRHiddenGlobalKind;
import p.studio.compiler.models.ProjectDescriptor;
import p.studio.compiler.models.SourceHandle;
import p.studio.compiler.models.SourceKind;
@@ -362,8 +363,12 @@ class PBSFrontendPhaseServiceTest {
assertTrue(diagnostics.isEmpty(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
assertTrue(irBackend.getEntryPointCallableName().startsWith("__pbs.frame_wrapper$m"));
assertTrue(irBackend.getGlobals().stream().anyMatch(global ->
global.name().startsWith("__pbs.boot_guard$m")));
final var bootGuard = irBackend.getGlobals().stream()
.filter(global -> global.hiddenKind() == IRHiddenGlobalKind.BOOT_GUARD)
.findFirst()
.orElseThrow();
assertTrue(bootGuard.isHidden());
assertTrue(bootGuard.name().startsWith("__pbs.boot_guard$m"));
final var wrapper = irBackend.getExecutableFunctions().stream()
.filter(function -> irBackend.getEntryPointCallableName().equals(function.callableName()))
.findFirst()
@@ -439,7 +444,7 @@ class PBSFrontendPhaseServiceTest {
assertTrue(irBackend.getExecutableFunctions().stream().anyMatch(function ->
function.callableName().startsWith("__pbs.project_init$m")));
assertTrue(irBackend.getGlobals().stream().anyMatch(global -> "SCORE".equals(global.name())));
assertTrue(irBackend.getGlobals().stream().anyMatch(global -> global.name().startsWith("__pbs.boot_guard$m")));
assertTrue(irBackend.getGlobals().stream().anyMatch(global -> global.hiddenKind() == IRHiddenGlobalKind.BOOT_GUARD));
}
@Test