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
@@ -15,6 +15,7 @@ public class LowerToIRVMService {
private static final Comparator<IRBackendExecutableFunction> FUNCTION_ORDER = Comparator
.comparing(IRBackendExecutableFunction::moduleKey)
.thenComparing(IRBackendExecutableFunction::callableName)
.thenComparingInt(IRBackendExecutableFunction::callableId)
.thenComparingInt(IRBackendExecutableFunction::sourceStart);
private final IRVMValidator validator;
@@ -35,11 +36,14 @@ public class LowerToIRVMService {
}
final var ordered = orderFunctions(backend.getExecutableFunctions());
final var funcIdByModuleAndName = new HashMap<String, Integer>();
final var funcIdByCallableId = new HashMap<Integer, Integer>();
for (var i = 0; i < ordered.size(); i++) {
final var fn = ordered.get(i);
final var key = callableKey(fn.moduleKey(), fn.callableName());
funcIdByModuleAndName.putIfAbsent(key, i);
if (funcIdByCallableId.putIfAbsent(fn.callableId(), i) != null) {
throw new IRVMLoweringException(
IRVMLoweringErrorCode.LOWER_IRVM_MISSING_CALLEE,
"duplicate callable id in executable set: " + fn.callableId());
}
}
final var loweredFunctions = new ArrayList<IRVMFunction>(ordered.size());
@@ -66,12 +70,16 @@ public class LowerToIRVMService {
functionPc += IRVMOp.RET.immediateSize() + 2;
}
case CALL_FUNC -> {
final var key = callableKey(instr.calleeModuleKey(), instr.calleeCallableName());
final var calleeId = funcIdByModuleAndName.get(key);
if (instr.calleeCallableId() == null) {
throw new IRVMLoweringException(
IRVMLoweringErrorCode.LOWER_IRVM_MISSING_CALLEE,
"missing callee callable id");
}
final var calleeId = funcIdByCallableId.get(instr.calleeCallableId());
if (calleeId == null) {
throw new IRVMLoweringException(
IRVMLoweringErrorCode.LOWER_IRVM_MISSING_CALLEE,
"missing callee function: " + key);
"missing callee function for callable_id=" + instr.calleeCallableId());
}
final var calleeFunction = ordered.get(calleeId);
if (instr.expectedArgSlots() != null
@@ -79,7 +87,7 @@ public class LowerToIRVMService {
throw new IRVMLoweringException(
IRVMLoweringErrorCode.LOWER_IRVM_CALL_ARG_SLOTS_MISMATCH,
"call arg_slots mismatch for %s. expected=%d actual=%d".formatted(
key,
instr.calleeCallableName(),
instr.expectedArgSlots(),
calleeFunction.paramSlots()));
}
@@ -88,7 +96,7 @@ public class LowerToIRVMService {
throw new IRVMLoweringException(
IRVMLoweringErrorCode.LOWER_IRVM_CALL_RET_SLOTS_MISMATCH,
"call ret_slots mismatch for %s. expected=%d actual=%d".formatted(
key,
instr.calleeCallableName(),
instr.expectedRetSlots(),
calleeFunction.returnSlots()));
}
@@ -213,12 +221,6 @@ public class LowerToIRVMService {
return ReadOnlyList.wrap(ordered);
}
private String callableKey(
final String moduleKey,
final String callableName) {
return (moduleKey == null ? "" : moduleKey) + "::" + callableName;
}
private BytecodeModule.SourceSpan toBytecodeSpan(
final int fallbackFileId,
final Span span) {
@@ -17,10 +17,10 @@ class LowerToIRVMServiceTest {
void lowerMustAssignEntrypointIdZeroAndSortRemainingDeterministically() {
final var backend = IRBackend.builder()
.executableFunctions(ReadOnlyList.from(
fn("aux", "app", ReadOnlyList.from(
callFunc("app", "main"),
fn("aux", "app", 11, ReadOnlyList.from(
callFunc("app", "main", 10),
ret())),
fn("main", "app", ReadOnlyList.from(
fn("main", "app", 10, ReadOnlyList.from(
ret()))))
.build();
@@ -37,7 +37,7 @@ class LowerToIRVMServiceTest {
void lowerMustMapHostAndIntrinsicCallsites() {
final var backend = IRBackend.builder()
.executableFunctions(ReadOnlyList.from(
fn("main", "app", ReadOnlyList.from(
fn("main", "app", 10, ReadOnlyList.from(
callHost("gfx", "draw_pixel", 1, 2, 0),
callIntrinsic("core.color.pack", 1, 0x2001),
ret()))))
@@ -59,8 +59,8 @@ class LowerToIRVMServiceTest {
void lowerMustRejectUnterminatedFunction() {
final var backend = IRBackend.builder()
.executableFunctions(ReadOnlyList.from(
fn("main", "app", ReadOnlyList.from(
callFunc("app", "main")))))
fn("main", "app", 10, ReadOnlyList.from(
callFunc("app", "main", 10)))))
.build();
final var thrown = assertThrows(IRVMValidationException.class, () -> new LowerToIRVMService().lower(backend));
@@ -71,8 +71,8 @@ class LowerToIRVMServiceTest {
void lowerMustRejectMissingCallee() {
final var backend = IRBackend.builder()
.executableFunctions(ReadOnlyList.from(
fn("main", "app", ReadOnlyList.from(
callFunc("app", "missing"),
fn("main", "app", 10, ReadOnlyList.from(
callFunc("app", "missing", 77),
ret()))))
.build();
@@ -84,9 +84,9 @@ class LowerToIRVMServiceTest {
void lowerMustRejectCallArgSlotMismatch() {
final var backend = IRBackend.builder()
.executableFunctions(ReadOnlyList.from(
fn("callee", "app", ReadOnlyList.from(ret())),
fn("main", "app", ReadOnlyList.from(
callFuncWithExpected("app", "callee", 2, 0),
fn("callee", "app", 20, ReadOnlyList.from(ret())),
fn("main", "app", 10, ReadOnlyList.from(
callFuncWithExpected("app", "callee", 20, 2, 0),
ret()))))
.build();
@@ -98,7 +98,7 @@ class LowerToIRVMServiceTest {
void lowerMustResolveJumpTargetsFromLabels() {
final var backend = IRBackend.builder()
.executableFunctions(ReadOnlyList.from(
fn("main", "app", ReadOnlyList.from(
fn("main", "app", 10, ReadOnlyList.from(
label("entry"),
jmp("exit"),
label("exit"),
@@ -117,7 +117,7 @@ class LowerToIRVMServiceTest {
void lowerMustRejectMissingJumpTargetLabel() {
final var backend = IRBackend.builder()
.executableFunctions(ReadOnlyList.from(
fn("main", "app", ReadOnlyList.from(
fn("main", "app", 10, ReadOnlyList.from(
jmp("missing"),
ret()))))
.build();
@@ -129,11 +129,13 @@ class LowerToIRVMServiceTest {
private static IRBackendExecutableFunction fn(
final String name,
final String moduleKey,
final int callableId,
final ReadOnlyList<IRBackendExecutableFunction.Instruction> instructions) {
return new IRBackendExecutableFunction(
new FileId(0),
moduleKey,
name,
callableId,
0,
10,
0,
@@ -154,11 +156,15 @@ class LowerToIRVMServiceTest {
Span.none());
}
private static IRBackendExecutableFunction.Instruction callFunc(final String moduleKey, final String name) {
private static IRBackendExecutableFunction.Instruction callFunc(
final String moduleKey,
final String name,
final int calleeCallableId) {
return new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.CALL_FUNC,
moduleKey,
name,
calleeCallableId,
null,
null,
Span.none());
@@ -167,12 +173,14 @@ class LowerToIRVMServiceTest {
private static IRBackendExecutableFunction.Instruction callFuncWithExpected(
final String moduleKey,
final String name,
final int calleeCallableId,
final int expectedArgSlots,
final int expectedRetSlots) {
return new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.CALL_FUNC,
moduleKey,
name,
calleeCallableId,
null,
null,
expectedArgSlots,
@@ -215,6 +223,7 @@ class LowerToIRVMServiceTest {
"",
null,
null,
null,
label,
"",
null,
@@ -229,6 +238,7 @@ class LowerToIRVMServiceTest {
"",
null,
null,
null,
"",
target,
null,
@@ -178,6 +178,7 @@ class BackendGateIIntegrationTest {
new FileId(1),
"app/main",
name,
1,
0,
10,
0,
@@ -29,6 +29,7 @@ class BackendSafetyGateSUTest {
new FileId(1),
"app",
"main",
1,
0,
10,
0,
@@ -40,6 +41,7 @@ class BackendSafetyGateSUTest {
IRBackendExecutableFunction.InstructionKind.CALL_FUNC,
"app",
"missing",
99,
null,
null,
0,
@@ -112,6 +114,7 @@ class BackendSafetyGateSUTest {
new FileId(1),
"app",
"main",
1,
0,
10,
0,
@@ -38,6 +38,7 @@ class LowerToIRVMPipelineStageTest {
new FileId(0),
"app",
"main",
1,
0,
10,
0,