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
@@ -2,6 +2,8 @@ package p.studio.compiler.models;
import lombok.Builder;
import lombok.Getter;
import p.studio.compiler.source.tables.CallableSignatureRef;
import p.studio.compiler.source.tables.CallableTable;
import p.studio.utilities.structures.ReadOnlyList;
import java.util.ArrayList;
@@ -15,6 +17,8 @@ public class IRBackend {
@Builder.Default
private final ReadOnlyList<IRBackendExecutableFunction> executableFunctions = ReadOnlyList.empty();
@Builder.Default
private final ReadOnlyList<CallableSignatureRef> callableSignatures = ReadOnlyList.empty();
@Builder.Default
private final IRReservedMetadata reservedMetadata = IRReservedMetadata.empty();
public static IRBackendAggregator aggregator() {
@@ -24,6 +28,7 @@ public class IRBackend {
public static final class IRBackendAggregator {
private final ArrayList<IRFunction> functions = new ArrayList<>();
private final ArrayList<IRBackendExecutableFunction> executableFunctions = new ArrayList<>();
private final CallableTable callableTable = new CallableTable();
private final ArrayList<IRReservedMetadata.HostMethodBinding> hostMethodBindings = new ArrayList<>();
private final ArrayList<IRReservedMetadata.BuiltinTypeSurface> builtinTypeSurfaces = new ArrayList<>();
private final ArrayList<IRReservedMetadata.BuiltinConstSurface> builtinConstSurfaces = new ArrayList<>();
@@ -34,7 +39,10 @@ public class IRBackend {
return;
}
functions.addAll(backendFile.functions().asList());
executableFunctions.addAll(backendFile.executableFunctions().asList());
final var callableRemap = reindexCallables(backendFile.callableSignatures());
for (final var function : backendFile.executableFunctions()) {
executableFunctions.add(remapExecutableFunction(function, callableRemap));
}
final var metadata = backendFile.reservedMetadata();
hostMethodBindings.addAll(metadata.hostMethodBindings().asList());
builtinTypeSurfaces.addAll(metadata.builtinTypeSurfaces().asList());
@@ -42,11 +50,80 @@ public class IRBackend {
requiredCapabilities.addAll(metadata.requiredCapabilities().asList());
}
private int[] reindexCallables(final ReadOnlyList<CallableSignatureRef> localCallableSignatures) {
if (localCallableSignatures == null || localCallableSignatures.isEmpty()) {
return new int[0];
}
final var remap = new int[localCallableSignatures.size()];
for (var i = 0; i < localCallableSignatures.size(); i++) {
remap[i] = callableTable.register(localCallableSignatures.get(i)).getId();
}
return remap;
}
private IRBackendExecutableFunction remapExecutableFunction(
final IRBackendExecutableFunction function,
final int[] callableRemap) {
final var remappedInstructions = new ArrayList<IRBackendExecutableFunction.Instruction>(function.instructions().size());
for (final var instruction : function.instructions()) {
final Integer remappedCallee;
if (instruction.calleeCallableId() == null) {
remappedCallee = null;
} else {
remappedCallee = remapCallableId(instruction.calleeCallableId(), callableRemap, "callee");
}
remappedInstructions.add(new IRBackendExecutableFunction.Instruction(
instruction.kind(),
instruction.calleeModuleKey(),
instruction.calleeCallableName(),
remappedCallee,
instruction.hostCall(),
instruction.intrinsicCall(),
instruction.label(),
instruction.targetLabel(),
instruction.expectedArgSlots(),
instruction.expectedRetSlots(),
instruction.span()));
}
return new IRBackendExecutableFunction(
function.fileId(),
function.moduleKey(),
function.callableName(),
remapCallableId(function.callableId(), callableRemap, "function"),
function.sourceStart(),
function.sourceEnd(),
function.paramSlots(),
function.localSlots(),
function.returnSlots(),
function.maxStackSlots(),
ReadOnlyList.wrap(remappedInstructions),
function.span());
}
private int remapCallableId(
final int localCallableId,
final int[] callableRemap,
final String label) {
if (localCallableId < 0 || localCallableId >= callableRemap.length) {
throw new IllegalArgumentException("invalid local " + label + " callable id: " + localCallableId);
}
return callableRemap[localCallableId];
}
private ReadOnlyList<CallableSignatureRef> emitCallableSignatures() {
final var signatures = new ArrayList<CallableSignatureRef>(callableTable.size());
for (final var callableId : callableTable.identifiers()) {
signatures.add(callableTable.get(callableId));
}
return ReadOnlyList.wrap(signatures);
}
public IRBackend emit() {
return IRBackend
.builder()
.functions(ReadOnlyList.wrap(functions))
.executableFunctions(ReadOnlyList.wrap(executableFunctions))
.callableSignatures(emitCallableSignatures())
.reservedMetadata(new IRReservedMetadata(
ReadOnlyList.wrap(hostMethodBindings),
ReadOnlyList.wrap(builtinTypeSurfaces),
@@ -61,6 +138,7 @@ public class IRBackend {
final var sb = new StringBuilder();
sb.append("IRBackend{functions=").append(functions.size())
.append(", executableFunctions=").append(executableFunctions.size())
.append(", callableSignatures=").append(callableSignatures.size())
.append(", hostBindings=").append(reservedMetadata.hostMethodBindings().size())
.append(", builtinTypes=").append(reservedMetadata.builtinTypeSurfaces().size())
.append(", builtinConsts=").append(reservedMetadata.builtinConstSurfaces().size())
@@ -10,6 +10,7 @@ public record IRBackendExecutableFunction(
FileId fileId,
String moduleKey,
String callableName,
int callableId,
int sourceStart,
int sourceEnd,
int paramSlots,
@@ -26,6 +27,9 @@ public record IRBackendExecutableFunction(
if (callableName.isBlank()) {
throw new IllegalArgumentException("callableName must not be blank");
}
if (callableId < 0) {
throw new IllegalArgumentException("callableId must be non-negative");
}
if (sourceStart < 0 || sourceEnd < 0 || sourceEnd < sourceStart) {
throw new IllegalArgumentException("invalid source span bounds");
}
@@ -46,6 +50,7 @@ public record IRBackendExecutableFunction(
InstructionKind kind,
String calleeModuleKey,
String calleeCallableName,
Integer calleeCallableId,
HostCallMetadata hostCall,
IntrinsicCallMetadata intrinsicCall,
String label,
@@ -57,10 +62,34 @@ public record IRBackendExecutableFunction(
final InstructionKind kind,
final String calleeModuleKey,
final String calleeCallableName,
final Integer calleeCallableId,
final HostCallMetadata hostCall,
final IntrinsicCallMetadata intrinsicCall,
final Span span) {
this(kind, calleeModuleKey, calleeCallableName, hostCall, intrinsicCall, null, null, null, null, span);
this(kind, calleeModuleKey, calleeCallableName, calleeCallableId, hostCall, intrinsicCall, null, null, null, null, span);
}
public Instruction(
final InstructionKind kind,
final String calleeModuleKey,
final String calleeCallableName,
final Integer calleeCallableId,
final HostCallMetadata hostCall,
final IntrinsicCallMetadata intrinsicCall,
final Integer expectedArgSlots,
final Integer expectedRetSlots,
final Span span) {
this(kind, calleeModuleKey, calleeCallableName, calleeCallableId, hostCall, intrinsicCall, null, null, expectedArgSlots, expectedRetSlots, span);
}
public Instruction(
final InstructionKind kind,
final String calleeModuleKey,
final String calleeCallableName,
final HostCallMetadata hostCall,
final IntrinsicCallMetadata intrinsicCall,
final Span span) {
this(kind, calleeModuleKey, calleeCallableName, null, hostCall, intrinsicCall, null, null, null, null, span);
}
public Instruction(
@@ -72,7 +101,7 @@ public record IRBackendExecutableFunction(
final Integer expectedArgSlots,
final Integer expectedRetSlots,
final Span span) {
this(kind, calleeModuleKey, calleeCallableName, hostCall, intrinsicCall, null, null, expectedArgSlots, expectedRetSlots, span);
this(kind, calleeModuleKey, calleeCallableName, null, hostCall, intrinsicCall, null, null, expectedArgSlots, expectedRetSlots, span);
}
public Instruction {
@@ -82,6 +111,9 @@ public record IRBackendExecutableFunction(
calleeCallableName = calleeCallableName == null ? "" : calleeCallableName;
label = label == null ? "" : label;
targetLabel = targetLabel == null ? "" : targetLabel;
if (calleeCallableId != null && calleeCallableId < 0) {
throw new IllegalArgumentException("calleeCallableId must be non-negative");
}
if (expectedArgSlots != null && expectedArgSlots < 0) {
throw new IllegalArgumentException("expectedArgSlots must be non-negative");
}
@@ -90,8 +122,8 @@ public record IRBackendExecutableFunction(
}
switch (kind) {
case CALL_FUNC -> {
if (calleeCallableName.isBlank()) {
throw new IllegalArgumentException("CALL_FUNC requires calleeCallableName");
if (calleeCallableId == null) {
throw new IllegalArgumentException("CALL_FUNC requires calleeCallableId");
}
if (hostCall != null || intrinsicCall != null) {
throw new IllegalArgumentException("CALL_FUNC must not carry host or intrinsic metadata");
@@ -101,6 +133,9 @@ public record IRBackendExecutableFunction(
if (hostCall == null) {
throw new IllegalArgumentException("CALL_HOST requires hostCall metadata");
}
if (calleeCallableId != null) {
throw new IllegalArgumentException("CALL_HOST must not carry calleeCallableId");
}
if (intrinsicCall != null) {
throw new IllegalArgumentException("CALL_HOST must not carry intrinsic metadata");
}
@@ -109,12 +144,15 @@ public record IRBackendExecutableFunction(
if (intrinsicCall == null) {
throw new IllegalArgumentException("CALL_INTRINSIC requires intrinsic metadata");
}
if (calleeCallableId != null) {
throw new IllegalArgumentException("CALL_INTRINSIC must not carry calleeCallableId");
}
if (hostCall != null) {
throw new IllegalArgumentException("CALL_INTRINSIC must not carry host metadata");
}
}
case HALT, RET -> {
if (!calleeCallableName.isBlank() || hostCall != null || intrinsicCall != null) {
if (!calleeCallableName.isBlank() || calleeCallableId != null || hostCall != null || intrinsicCall != null) {
throw new IllegalArgumentException(kind + " must not carry callsite metadata");
}
if (expectedArgSlots != null || expectedRetSlots != null) {
@@ -127,6 +165,7 @@ public record IRBackendExecutableFunction(
}
if (!targetLabel.isBlank()
|| !calleeCallableName.isBlank()
|| calleeCallableId != null
|| hostCall != null
|| intrinsicCall != null
|| expectedArgSlots != null
@@ -140,6 +179,7 @@ public record IRBackendExecutableFunction(
}
if (!label.isBlank()
|| !calleeCallableName.isBlank()
|| calleeCallableId != null
|| hostCall != null
|| intrinsicCall != null
|| expectedArgSlots != null
@@ -1,6 +1,7 @@
package p.studio.compiler.models;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.compiler.source.tables.CallableSignatureRef;
import p.studio.utilities.structures.ReadOnlyList;
import java.util.Objects;
@@ -9,28 +10,30 @@ public record IRBackendFile(
FileId fileId,
ReadOnlyList<IRFunction> functions,
ReadOnlyList<IRBackendExecutableFunction> executableFunctions,
IRReservedMetadata reservedMetadata) {
IRReservedMetadata reservedMetadata,
ReadOnlyList<CallableSignatureRef> callableSignatures) {
public IRBackendFile {
fileId = Objects.requireNonNull(fileId, "fileId");
functions = functions == null ? ReadOnlyList.empty() : functions;
executableFunctions = executableFunctions == null ? ReadOnlyList.empty() : executableFunctions;
reservedMetadata = reservedMetadata == null ? IRReservedMetadata.empty() : reservedMetadata;
callableSignatures = callableSignatures == null ? ReadOnlyList.empty() : callableSignatures;
}
public IRBackendFile(
final FileId fileId,
final ReadOnlyList<IRFunction> functions) {
this(fileId, functions, ReadOnlyList.empty(), IRReservedMetadata.empty());
this(fileId, functions, ReadOnlyList.empty(), IRReservedMetadata.empty(), ReadOnlyList.empty());
}
public static IRBackendFile empty(final FileId fileId) {
return new IRBackendFile(fileId, ReadOnlyList.empty(), ReadOnlyList.empty(), IRReservedMetadata.empty());
return new IRBackendFile(fileId, ReadOnlyList.empty(), ReadOnlyList.empty(), IRReservedMetadata.empty(), ReadOnlyList.empty());
}
public IRBackendFile(
final FileId fileId,
final ReadOnlyList<IRFunction> functions,
final IRReservedMetadata reservedMetadata) {
this(fileId, functions, ReadOnlyList.empty(), reservedMetadata);
this(fileId, functions, ReadOnlyList.empty(), reservedMetadata, ReadOnlyList.empty());
}
}