implements PR-08.2

This commit is contained in:
2026-03-24 13:42:32 +00:00
parent 40188b67ab
commit 0490f64870
11 changed files with 520 additions and 23 deletions
@@ -6,19 +6,15 @@ import p.studio.compiler.models.IRBackend;
import p.studio.compiler.models.IRBackendExecutableFunction;
import p.studio.compiler.source.Span;
import p.studio.compiler.source.identifiers.CallableId;
import p.studio.compiler.source.identifiers.ModuleId;
import p.studio.compiler.source.tables.ModuleReference;
import p.studio.utilities.structures.ReadOnlyList;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.StringJoiner;
public class LowerToIRVMService {
private static final Comparator<IRBackendExecutableFunction> FUNCTION_ORDER = Comparator
.comparing(IRBackendExecutableFunction::moduleKey)
.thenComparing(IRBackendExecutableFunction::callableName)
.thenComparingInt(function -> function.callableId().getIndex())
.thenComparingInt(IRBackendExecutableFunction::sourceStart);
private final IRVMValidator validator;
public LowerToIRVMService() {
@@ -301,7 +297,23 @@ public class LowerToIRVMService {
"frontend IRBackend entrypoint declaration is missing");
}
final var sorted = new ArrayList<>(backend.getExecutableFunctions().asList());
sorted.sort(FUNCTION_ORDER);
sorted.sort((left, right) -> {
final var leftModuleKey = moduleSortKey(backend, left.moduleId(), left.moduleKey());
final var rightModuleKey = moduleSortKey(backend, right.moduleId(), right.moduleKey());
final var moduleCmp = leftModuleKey.compareTo(rightModuleKey);
if (moduleCmp != 0) {
return moduleCmp;
}
final var nameCmp = left.callableName().compareTo(right.callableName());
if (nameCmp != 0) {
return nameCmp;
}
final var idCmp = Integer.compare(left.callableId().getIndex(), right.callableId().getIndex());
if (idCmp != 0) {
return idCmp;
}
return Integer.compare(left.sourceStart(), right.sourceStart());
});
final var entrypoints = sorted.stream()
.filter(candidate -> entryPointCallableName.equals(candidate.callableName()))
.toList();
@@ -328,6 +340,27 @@ public class LowerToIRVMService {
return ReadOnlyList.wrap(ordered);
}
private String moduleSortKey(
final IRBackend backend,
final ModuleId moduleId,
final String fallbackModuleKey) {
if (moduleId != null && !moduleId.isNone()) {
final var index = moduleId.getIndex();
if (index >= 0 && index < backend.getModulePool().size()) {
return canonicalModuleSortKey(backend.getModulePool().get(index));
}
}
return fallbackModuleKey == null ? "" : fallbackModuleKey;
}
private String canonicalModuleSortKey(final ModuleReference reference) {
final var joiner = new StringJoiner("/", reference.project() + ":", "");
for (final var segment : reference.pathSegments()) {
joiner.add(segment);
}
return joiner.toString();
}
private BytecodeModule.SourceSpan toBytecodeSpan(
final int fallbackFileId,
final Span span) {
@@ -7,7 +7,9 @@ import p.studio.compiler.source.Span;
import p.studio.compiler.source.identifiers.CallableId;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.compiler.source.identifiers.IntrinsicId;
import p.studio.compiler.source.identifiers.ModuleId;
import p.studio.compiler.source.tables.IntrinsicReference;
import p.studio.compiler.source.tables.ModuleReference;
import p.studio.utilities.structures.ReadOnlyList;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -199,6 +201,27 @@ class LowerToIRVMServiceTest {
assertEquals(IRVMLoweringErrorCode.LOWER_IRVM_ENTRYPOINT_AMBIGUOUS, thrown.code());
}
@Test
void lowerMustUseModulePoolCanonicalIdentityForNonEntrypointOrdering() {
final var backend = IRBackend.builder()
.entryPointCallableName("frame")
.modulePool(ReadOnlyList.from(
new ModuleReference("app", ReadOnlyList.from("alpha")),
new ModuleReference("app", ReadOnlyList.from("zeta"))))
.executableFunctions(ReadOnlyList.from(
fnWithModule("helper_in_zeta", "legacy/zeta", 1, 21, ReadOnlyList.from(ret())),
fnWithModule("frame", "legacy/zeta", 1, 20, ReadOnlyList.from(ret())),
fnWithModule("helper_in_alpha", "legacy/alpha", 0, 22, ReadOnlyList.from(ret()))))
.build();
final var lowered = new LowerToIRVMService().lower(backend);
assertEquals(3, lowered.module().functions().size());
assertEquals("frame", lowered.module().functions().get(0).name());
assertEquals("helper_in_alpha", lowered.module().functions().get(1).name());
assertEquals("helper_in_zeta", lowered.module().functions().get(2).name());
}
private static IRBackendExecutableFunction fn(
final String name,
final String moduleKey,
@@ -219,6 +242,28 @@ class LowerToIRVMServiceTest {
Span.none());
}
private static IRBackendExecutableFunction fnWithModule(
final String name,
final String moduleKey,
final int moduleId,
final int callableId,
final ReadOnlyList<IRBackendExecutableFunction.Instruction> instructions) {
return new IRBackendExecutableFunction(
new FileId(0),
new ModuleId(moduleId),
moduleKey,
name,
new CallableId(callableId),
0,
10,
0,
0,
0,
4,
instructions,
Span.none());
}
private static IRBackendExecutableFunction.Instruction ret() {
return new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.RET,