implements PR-19.7 explicit backend globals and synthetic callable model

This commit is contained in:
2026-03-26 19:31:21 +00:00
parent 7fc38010a2
commit c61b9db786
8 changed files with 238 additions and 0 deletions
@@ -21,6 +21,10 @@ public class IRBackend {
@Builder.Default
private final ReadOnlyList<IRFunction> functions = ReadOnlyList.empty();
@Builder.Default
private final ReadOnlyList<IRSyntheticFunction> syntheticFunctions = ReadOnlyList.empty();
@Builder.Default
private final ReadOnlyList<IRGlobal> globals = ReadOnlyList.empty();
@Builder.Default
private final ReadOnlyList<IRBackendExecutableFunction> executableFunctions = ReadOnlyList.empty();
@Builder.Default
private final ReadOnlyList<ModuleReference> modulePool = ReadOnlyList.empty();
@@ -39,6 +43,8 @@ public class IRBackend {
private String entryPointCallableName;
private ModuleId entryPointModuleId = ModuleId.none();
private final ArrayList<IRFunction> functions = new ArrayList<>();
private final ArrayList<IRSyntheticFunction> syntheticFunctions = new ArrayList<>();
private final ArrayList<IRGlobal> globals = new ArrayList<>();
private final ArrayList<IRBackendExecutableFunction> executableFunctions = new ArrayList<>();
private final ModuleTable moduleTable = new ModuleTable();
private final CallableTable callableTable = new CallableTable();
@@ -54,6 +60,21 @@ public class IRBackend {
}
functions.addAll(backendFile.functions().asList());
final var moduleRemap = reindexModules(backendFile.modulePool());
for (final var syntheticFunction : backendFile.syntheticFunctions()) {
syntheticFunctions.add(new IRSyntheticFunction(
remapModuleId(syntheticFunction.moduleId(), moduleRemap, "synthetic function"),
syntheticFunction.callableName(),
syntheticFunction.kind(),
syntheticFunction.origin()));
}
for (final var global : backendFile.globals()) {
globals.add(new IRGlobal(
global.fileId(),
remapModuleId(global.moduleId(), moduleRemap, "global"),
global.name(),
global.declaredTypeSurface(),
global.span()));
}
final var callableRemap = reindexCallables(backendFile.callableSignatures(), moduleRemap);
final var intrinsicRemap = reindexIntrinsics(backendFile.intrinsicPool());
for (final var function : backendFile.executableFunctions()) {
@@ -248,6 +269,8 @@ public class IRBackend {
.entryPointCallableName(resolveEntryPointCallableName())
.entryPointModuleId(resolveEntryPointModuleId())
.functions(ReadOnlyList.wrap(functions))
.syntheticFunctions(ReadOnlyList.wrap(syntheticFunctions))
.globals(ReadOnlyList.wrap(globals))
.executableFunctions(ReadOnlyList.wrap(executableFunctions))
.modulePool(emitModulePool())
.callableSignatures(emitCallableSignatures())
@@ -278,6 +301,8 @@ public class IRBackend {
.append('@')
.append(entryPointModuleId == null || entryPointModuleId.isNone() ? "-" : entryPointModuleId.getIndex())
.append(", functions=").append(functions.size())
.append(", syntheticFunctions=").append(syntheticFunctions.size())
.append(", globals=").append(globals.size())
.append(", executableFunctions=").append(executableFunctions.size())
.append(", modulePool=").append(modulePool.size())
.append(", callableSignatures=").append(callableSignatures.size())
@@ -11,6 +11,8 @@ import java.util.Objects;
public record IRBackendFile(
FileId fileId,
ReadOnlyList<IRFunction> functions,
ReadOnlyList<IRSyntheticFunction> syntheticFunctions,
ReadOnlyList<IRGlobal> globals,
ReadOnlyList<IRBackendExecutableFunction> executableFunctions,
IRReservedMetadata reservedMetadata,
ReadOnlyList<ModuleReference> modulePool,
@@ -19,6 +21,8 @@ public record IRBackendFile(
public IRBackendFile {
fileId = Objects.requireNonNull(fileId, "fileId");
functions = functions == null ? ReadOnlyList.empty() : functions;
syntheticFunctions = syntheticFunctions == null ? ReadOnlyList.empty() : syntheticFunctions;
globals = globals == null ? ReadOnlyList.empty() : globals;
executableFunctions = executableFunctions == null ? ReadOnlyList.empty() : executableFunctions;
reservedMetadata = reservedMetadata == null ? IRReservedMetadata.empty() : reservedMetadata;
modulePool = modulePool == null ? ReadOnlyList.empty() : modulePool;
@@ -33,6 +37,8 @@ public record IRBackendFile(
fileId,
functions,
ReadOnlyList.empty(),
ReadOnlyList.empty(),
ReadOnlyList.empty(),
IRReservedMetadata.empty(),
ReadOnlyList.empty(),
ReadOnlyList.empty(),
@@ -44,6 +50,8 @@ public record IRBackendFile(
fileId,
ReadOnlyList.empty(),
ReadOnlyList.empty(),
ReadOnlyList.empty(),
ReadOnlyList.empty(),
IRReservedMetadata.empty(),
ReadOnlyList.empty(),
ReadOnlyList.empty(),
@@ -58,6 +66,8 @@ public record IRBackendFile(
fileId,
functions,
ReadOnlyList.empty(),
ReadOnlyList.empty(),
ReadOnlyList.empty(),
reservedMetadata,
ReadOnlyList.empty(),
ReadOnlyList.empty(),
@@ -74,10 +84,32 @@ public record IRBackendFile(
this(
fileId,
functions,
ReadOnlyList.empty(),
ReadOnlyList.empty(),
executableFunctions,
reservedMetadata,
ReadOnlyList.empty(),
callableSignatures,
intrinsicPool);
}
public IRBackendFile(
final FileId fileId,
final ReadOnlyList<IRFunction> functions,
final ReadOnlyList<IRBackendExecutableFunction> executableFunctions,
final IRReservedMetadata reservedMetadata,
final ReadOnlyList<ModuleReference> modulePool,
final ReadOnlyList<CallableSignatureRef> callableSignatures,
final ReadOnlyList<IntrinsicReference> intrinsicPool) {
this(
fileId,
functions,
ReadOnlyList.empty(),
ReadOnlyList.empty(),
executableFunctions,
reservedMetadata,
modulePool,
callableSignatures,
intrinsicPool);
}
}
@@ -0,0 +1,23 @@
package p.studio.compiler.models;
import p.studio.compiler.source.Span;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.compiler.source.identifiers.ModuleId;
import java.util.Objects;
public record IRGlobal(
FileId fileId,
ModuleId moduleId,
String name,
String declaredTypeSurface,
Span span) {
public IRGlobal {
fileId = Objects.requireNonNull(fileId, "fileId");
moduleId = moduleId == null ? ModuleId.none() : moduleId;
name = Objects.requireNonNull(name, "name");
declaredTypeSurface = declaredTypeSurface == null ? "" : declaredTypeSurface;
span = span == null ? Span.none() : span;
}
}
@@ -0,0 +1,9 @@
package p.studio.compiler.models;
public enum IRSyntheticCallableKind {
FILE_INIT_FRAGMENT,
MODULE_INIT,
PROJECT_INIT,
PUBLISHED_FRAME_WRAPPER
}
@@ -0,0 +1,19 @@
package p.studio.compiler.models;
import p.studio.compiler.source.identifiers.ModuleId;
import java.util.Objects;
public record IRSyntheticFunction(
ModuleId moduleId,
String callableName,
IRSyntheticCallableKind kind,
IRSyntheticOrigin origin) {
public IRSyntheticFunction {
moduleId = moduleId == null ? ModuleId.none() : moduleId;
callableName = Objects.requireNonNull(callableName, "callableName");
kind = Objects.requireNonNull(kind, "kind");
origin = Objects.requireNonNull(origin, "origin");
}
}
@@ -0,0 +1,18 @@
package p.studio.compiler.models;
import p.studio.compiler.source.Span;
import p.studio.compiler.source.identifiers.FileId;
import java.util.Objects;
public record IRSyntheticOrigin(
FileId fileId,
String anchorCallableName,
Span span) {
public IRSyntheticOrigin {
fileId = Objects.requireNonNull(fileId, "fileId");
anchorCallableName = anchorCallableName == null ? "" : anchorCallableName;
span = span == null ? Span.none() : span;
}
}