implements PR-19.7 explicit backend globals and synthetic callable model
This commit is contained in:
+84
@@ -3,7 +3,11 @@ package p.studio.compiler.pbs;
|
||||
import p.studio.compiler.messages.HostAdmissionContext;
|
||||
import p.studio.compiler.models.IRBackendFile;
|
||||
import p.studio.compiler.models.IRFunction;
|
||||
import p.studio.compiler.models.IRGlobal;
|
||||
import p.studio.compiler.models.IRReservedMetadata;
|
||||
import p.studio.compiler.models.IRSyntheticCallableKind;
|
||||
import p.studio.compiler.models.IRSyntheticFunction;
|
||||
import p.studio.compiler.models.IRSyntheticOrigin;
|
||||
import p.studio.compiler.models.SourceKind;
|
||||
import p.studio.compiler.pbs.ast.PbsAst;
|
||||
import p.studio.compiler.pbs.lexer.PbsLexer;
|
||||
@@ -149,6 +153,12 @@ public final class PbsFrontendCompiler {
|
||||
final ReadOnlyList<IRFunction> functions = sourceKind == SourceKind.SDK_INTERFACE
|
||||
? ReadOnlyList.empty()
|
||||
: lowerFunctions(fileId, ast);
|
||||
final ReadOnlyList<IRSyntheticFunction> syntheticFunctions = sourceKind == SourceKind.SDK_INTERFACE
|
||||
? ReadOnlyList.empty()
|
||||
: lowerSyntheticFunctions(fileId, effectiveModuleId, ast);
|
||||
final ReadOnlyList<IRGlobal> globals = sourceKind == SourceKind.SDK_INTERFACE
|
||||
? ReadOnlyList.empty()
|
||||
: lowerGlobals(fileId, effectiveModuleId, ast);
|
||||
final var loweringErrorBaseline = diagnostics.errorCount();
|
||||
final var executableLowering = executableLoweringService.lower(
|
||||
fileId,
|
||||
@@ -165,6 +175,8 @@ public final class PbsFrontendCompiler {
|
||||
return new IRBackendFile(
|
||||
fileId,
|
||||
functions,
|
||||
syntheticFunctions,
|
||||
globals,
|
||||
executableLowering.executableFunctions(),
|
||||
reservedMetadata,
|
||||
effectiveModulePool,
|
||||
@@ -210,6 +222,78 @@ public final class PbsFrontendCompiler {
|
||||
return ReadOnlyList.wrap(functions);
|
||||
}
|
||||
|
||||
private ReadOnlyList<IRGlobal> lowerGlobals(
|
||||
final FileId fileId,
|
||||
final ModuleId moduleId,
|
||||
final PbsAst.File ast) {
|
||||
final var globals = new ArrayList<IRGlobal>();
|
||||
for (final var topDecl : ast.topDecls()) {
|
||||
if (!(topDecl instanceof PbsAst.GlobalDecl globalDecl)) {
|
||||
continue;
|
||||
}
|
||||
globals.add(new IRGlobal(
|
||||
fileId,
|
||||
moduleId,
|
||||
globalDecl.name(),
|
||||
PbsReservedMetadataExtractor.typeSurfaceKey(globalDecl.explicitType()),
|
||||
globalDecl.span()));
|
||||
}
|
||||
return ReadOnlyList.wrap(globals);
|
||||
}
|
||||
|
||||
private ReadOnlyList<IRSyntheticFunction> lowerSyntheticFunctions(
|
||||
final FileId fileId,
|
||||
final ModuleId moduleId,
|
||||
final PbsAst.File ast) {
|
||||
final var syntheticFunctions = new ArrayList<IRSyntheticFunction>();
|
||||
PbsAst.FunctionDecl initDecl = null;
|
||||
PbsAst.FunctionDecl frameDecl = null;
|
||||
PbsAst.GlobalDecl firstGlobalDecl = null;
|
||||
for (final var topDecl : ast.topDecls()) {
|
||||
if (topDecl instanceof PbsAst.FunctionDecl functionDecl) {
|
||||
if (functionDecl.lifecycleMarker() == PbsAst.LifecycleMarker.INIT && initDecl == null) {
|
||||
initDecl = functionDecl;
|
||||
}
|
||||
if (functionDecl.lifecycleMarker() == PbsAst.LifecycleMarker.FRAME && frameDecl == null) {
|
||||
frameDecl = functionDecl;
|
||||
}
|
||||
}
|
||||
if (topDecl instanceof PbsAst.GlobalDecl globalDecl && firstGlobalDecl == null) {
|
||||
firstGlobalDecl = globalDecl;
|
||||
}
|
||||
}
|
||||
|
||||
final var fileInitAnchorSpan = initDecl != null ? initDecl.span() : firstGlobalDecl == null ? null : firstGlobalDecl.span();
|
||||
final var fileInitAnchorName = initDecl != null ? initDecl.name() : firstGlobalDecl == null ? "" : firstGlobalDecl.name();
|
||||
if (fileInitAnchorSpan != null) {
|
||||
syntheticFunctions.add(new IRSyntheticFunction(
|
||||
moduleId,
|
||||
"__pbs.file_init",
|
||||
IRSyntheticCallableKind.FILE_INIT_FRAGMENT,
|
||||
new IRSyntheticOrigin(fileId, fileInitAnchorName, fileInitAnchorSpan)));
|
||||
syntheticFunctions.add(new IRSyntheticFunction(
|
||||
moduleId,
|
||||
"__pbs.module_init",
|
||||
IRSyntheticCallableKind.MODULE_INIT,
|
||||
new IRSyntheticOrigin(fileId, fileInitAnchorName, fileInitAnchorSpan)));
|
||||
}
|
||||
if (initDecl != null && frameDecl != null) {
|
||||
syntheticFunctions.add(new IRSyntheticFunction(
|
||||
moduleId,
|
||||
"__pbs.project_init",
|
||||
IRSyntheticCallableKind.PROJECT_INIT,
|
||||
new IRSyntheticOrigin(fileId, initDecl.name(), initDecl.span())));
|
||||
}
|
||||
if (frameDecl != null) {
|
||||
syntheticFunctions.add(new IRSyntheticFunction(
|
||||
moduleId,
|
||||
"__pbs.frame_wrapper",
|
||||
IRSyntheticCallableKind.PUBLISHED_FRAME_WRAPPER,
|
||||
new IRSyntheticOrigin(fileId, frameDecl.name(), frameDecl.span())));
|
||||
}
|
||||
return ReadOnlyList.wrap(syntheticFunctions);
|
||||
}
|
||||
|
||||
private IRReservedMetadata mergeReservedMetadata(
|
||||
final IRReservedMetadata primary,
|
||||
final IRReservedMetadata imported) {
|
||||
|
||||
Reference in New Issue
Block a user