implements PR-08.2
This commit is contained in:
+41
-8
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user