implements PR-O4.1

This commit is contained in:
2026-03-24 13:42:28 +00:00
parent bbcb65b048
commit d4f6d96437
15 changed files with 404 additions and 56 deletions
@@ -16,6 +16,8 @@ import p.studio.compiler.pbs.semantics.PbsSemanticsErrors;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.diagnostics.DiagnosticPhase;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.compiler.source.tables.CallableSignatureRef;
import p.studio.compiler.source.tables.CallableTable;
import p.studio.compiler.source.tables.IntrinsicTable;
import p.studio.utilities.structures.ReadOnlyList;
@@ -121,13 +123,18 @@ public final class PbsFrontendCompiler {
? ReadOnlyList.empty()
: lowerFunctions(fileId, ast);
final var loweringErrorBaseline = diagnostics.errorCount();
final ReadOnlyList<IRBackendExecutableFunction> executableFunctions = sourceKind == SourceKind.SDK_INTERFACE
? ReadOnlyList.empty()
final var executableLowering = sourceKind == SourceKind.SDK_INTERFACE
? new ExecutableLoweringResult(ReadOnlyList.empty(), ReadOnlyList.empty())
: lowerExecutableFunctions(fileId, ast, moduleKey, reservedMetadata, diagnostics);
if (diagnostics.errorCount() > loweringErrorBaseline) {
return IRBackendFile.empty(fileId);
}
return new IRBackendFile(fileId, functions, executableFunctions, reservedMetadata);
return new IRBackendFile(
fileId,
functions,
executableLowering.executableFunctions(),
reservedMetadata,
executableLowering.callableSignatures());
}
private ReadOnlyList<IRFunction> lowerFunctions(final FileId fileId, final PbsAst.File ast) {
@@ -143,12 +150,13 @@ public final class PbsFrontendCompiler {
return ReadOnlyList.wrap(functions);
}
private ReadOnlyList<IRBackendExecutableFunction> lowerExecutableFunctions(
private ExecutableLoweringResult lowerExecutableFunctions(
final FileId fileId,
final PbsAst.File ast,
final String moduleKey,
final IRReservedMetadata reservedMetadata,
final DiagnosticSink diagnostics) {
final var normalizedModuleKey = moduleKey == null ? "" : moduleKey;
final var hostByMethodName = new HashMap<String, IRReservedMetadata.HostMethodBinding>();
for (final var hostBinding : reservedMetadata.hostMethodBindings()) {
hostByMethodName.put(hostBinding.sourceMethodName(), hostBinding);
@@ -159,21 +167,32 @@ public final class PbsFrontendCompiler {
intrinsicByMethodName.put(intrinsicSurface.sourceMethodName(), intrinsicSurface);
}
}
final var returnSlotsByCallableAndArity = new HashMap<String, Integer>();
final var callableIdTable = new CallableTable();
final var callableIdsByNameAndArity = new HashMap<String, List<Integer>>();
final var callableIdByDeclaration = new HashMap<PbsAst.FunctionDecl, Integer>();
final var returnSlotsByCallableId = new HashMap<Integer, Integer>();
final var intrinsicIdTable = new IntrinsicTable();
for (final var declaredFn : ast.functions()) {
final var key = callableArityKey(declaredFn.name(), declaredFn.parameters().size());
final var callableId = callableIdTable.register(
normalizedModuleKey,
declaredFn.name(),
declaredFn.parameters().size(),
callableShapeKey(declaredFn))
.getId();
callableIdByDeclaration.put(declaredFn, callableId);
callableIdsByNameAndArity
.computeIfAbsent(callableArityKey(declaredFn.name(), declaredFn.parameters().size()), ignored -> new ArrayList<>())
.add(callableId);
final var retSlots = returnSlotsFor(declaredFn);
if (returnSlotsByCallableAndArity.containsKey(key)
&& !returnSlotsByCallableAndArity.get(key).equals(retSlots)) {
returnSlotsByCallableAndArity.put(key, null);
} else {
returnSlotsByCallableAndArity.put(key, retSlots);
}
returnSlotsByCallableId.put(callableId, retSlots);
}
final var executableFunctions = new ArrayList<IRBackendExecutableFunction>(ast.functions().size());
for (final var fn : ast.functions()) {
final var functionCallableId = callableIdByDeclaration.get(fn);
if (functionCallableId == null) {
continue;
}
final var instructions = new ArrayList<IRBackendExecutableFunction.Instruction>();
final var callsites = new ArrayList<PbsAst.CallExpr>();
collectCallsFromBlock(fn.body(), callsites);
@@ -230,14 +249,37 @@ public final class PbsFrontendCompiler {
continue;
}
final var candidateCallableIds = callableIdsByNameAndArity.get(callableArityKey(calleeName, callExpr.arguments().size()));
if (candidateCallableIds == null || candidateCallableIds.isEmpty()) {
diagnostics.error(
DiagnosticPhase.STATIC_SEMANTICS,
PbsSemanticsErrors.E_SEM_EXEC_LOWERING_UNRESOLVED_CALLEE.name(),
PbsSemanticsErrors.E_SEM_EXEC_LOWERING_UNRESOLVED_CALLEE.name(),
Map.of(),
"executable lowering requires resolvable callee identity",
callExpr.span());
continue;
}
if (candidateCallableIds.size() > 1) {
diagnostics.error(
DiagnosticPhase.STATIC_SEMANTICS,
PbsSemanticsErrors.E_SEM_EXEC_LOWERING_UNRESOLVED_CALLEE.name(),
PbsSemanticsErrors.E_SEM_EXEC_LOWERING_UNRESOLVED_CALLEE.name(),
Map.of(),
"executable lowering found ambiguous callable identity",
callExpr.span());
continue;
}
final var calleeCallableId = candidateCallableIds.getFirst();
instructions.add(new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.CALL_FUNC,
moduleKey == null ? "" : moduleKey,
normalizedModuleKey,
calleeName,
calleeCallableId,
null,
null,
callExpr.arguments().size(),
returnSlotsByCallableAndArity.get(callableArityKey(calleeName, callExpr.arguments().size())),
returnSlotsByCallableId.get(calleeCallableId),
callExpr.span()));
}
instructions.add(new IRBackendExecutableFunction.Instruction(
@@ -246,6 +288,7 @@ public final class PbsFrontendCompiler {
"",
null,
null,
null,
fn.span()));
final var returnSlots = returnSlotsFor(fn);
@@ -253,8 +296,9 @@ public final class PbsFrontendCompiler {
final var end = safeToInt(fn.span().getEnd());
executableFunctions.add(new IRBackendExecutableFunction(
fileId,
moduleKey == null ? "" : moduleKey,
normalizedModuleKey,
fn.name(),
functionCallableId,
start,
end,
fn.parameters().size(),
@@ -264,7 +308,13 @@ public final class PbsFrontendCompiler {
ReadOnlyList.wrap(instructions),
fn.span()));
}
return ReadOnlyList.wrap(executableFunctions);
final var callableSignatures = new ArrayList<CallableSignatureRef>(callableIdTable.size());
for (final var callableId : callableIdTable.identifiers()) {
callableSignatures.add(callableIdTable.get(callableId));
}
return new ExecutableLoweringResult(
ReadOnlyList.wrap(executableFunctions),
ReadOnlyList.wrap(callableSignatures));
}
private int returnSlotsFor(final PbsAst.FunctionDecl functionDecl) {
@@ -280,6 +330,60 @@ public final class PbsFrontendCompiler {
return callableName + "#" + arity;
}
private String callableShapeKey(final PbsAst.FunctionDecl functionDecl) {
final var builder = new StringBuilder();
builder.append('(');
for (var i = 0; i < functionDecl.parameters().size(); i++) {
if (i > 0) {
builder.append(',');
}
builder.append(typeKey(functionDecl.parameters().get(i).typeRef()));
}
builder.append(")->");
builder.append(outputShapeKey(functionDecl.returnKind(), functionDecl.returnType(), functionDecl.resultErrorType()));
return builder.toString();
}
private String outputShapeKey(
final PbsAst.ReturnKind returnKind,
final PbsAst.TypeRef returnType,
final PbsAst.TypeRef resultErrorType) {
return switch (returnKind) {
case INFERRED_UNIT, EXPLICIT_UNIT -> "unit";
case PLAIN -> typeKey(returnType);
case RESULT -> "result<" + typeKey(resultErrorType) + ">" + typeKey(returnType);
};
}
private String typeKey(final PbsAst.TypeRef typeRef) {
final var unwrapped = unwrapGroup(typeRef);
if (unwrapped == null) {
return "unit";
}
return switch (unwrapped.kind()) {
case SIMPLE -> "simple:" + unwrapped.name();
case SELF -> "self";
case OPTIONAL -> "optional(" + typeKey(unwrapped.inner()) + ")";
case UNIT -> "unit";
case GROUP -> typeKey(unwrapped.inner());
case NAMED_TUPLE -> "tuple(" + unwrapped.fields().stream()
.map(field -> typeKey(field.typeRef()))
.reduce((left, right) -> left + "," + right)
.orElse("") + ")";
case ERROR -> "error";
};
}
private PbsAst.TypeRef unwrapGroup(final PbsAst.TypeRef typeRef) {
if (typeRef == null) {
return null;
}
if (typeRef.kind() != PbsAst.TypeRefKind.GROUP) {
return typeRef;
}
return unwrapGroup(typeRef.inner());
}
private int safeToInt(final long value) {
if (value > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
@@ -443,4 +547,9 @@ public final class PbsFrontendCompiler {
default -> null;
};
}
private record ExecutableLoweringResult(
ReadOnlyList<IRBackendExecutableFunction> executableFunctions,
ReadOnlyList<CallableSignatureRef> callableSignatures) {
}
}