implements PR-O3.1

This commit is contained in:
2026-03-24 13:42:27 +00:00
parent ea27561e65
commit a38fedc591
7 changed files with 251 additions and 1 deletions
@@ -143,6 +143,30 @@ class BytecodeEmitterTest {
assertEquals(14, module.debugInfo().pcToSpan().get(3).pc());
}
@Test
void emitMustEncodeJumpOpcodesWithU32Immediate() {
final var emitter = new BytecodeEmitter();
final var module = emitter.emit(new BytecodeEmitter.EmissionPlan(
0,
ReadOnlyList.empty(),
ReadOnlyList.empty(),
ReadOnlyList.from(new BytecodeEmitter.FunctionPlan(
"main",
0,
0,
0,
2,
ReadOnlyList.from(
BytecodeEmitter.Operation.jmp(12, new BytecodeModule.SourceSpan(1, 0, 1)),
BytecodeEmitter.Operation.jmpIfFalse(2, new BytecodeModule.SourceSpan(1, 2, 3)),
BytecodeEmitter.Operation.ret(new BytecodeModule.SourceSpan(1, 4, 5)))))));
assertEquals(0x02, readU16(module.code(), 0));
assertEquals(12, readU32(module.code(), 2));
assertEquals(0x03, readU16(module.code(), 6));
assertEquals(2, readU32(module.code(), 8));
}
private static int readU16(final byte[] bytes, final int offset) {
return ByteBuffer.wrap(bytes, offset, 2).order(ByteOrder.LITTLE_ENDIAN).getShort() & 0xFFFF;
}
@@ -94,6 +94,38 @@ class LowerToIRVMServiceTest {
assertEquals(IRVMLoweringErrorCode.LOWER_IRVM_CALL_ARG_SLOTS_MISMATCH, thrown.code());
}
@Test
void lowerMustResolveJumpTargetsFromLabels() {
final var backend = IRBackend.builder()
.executableFunctions(ReadOnlyList.from(
fn("main", "app", ReadOnlyList.from(
label("entry"),
jmp("exit"),
label("exit"),
ret()))))
.build();
final var lowered = new LowerToIRVMService().lower(backend);
final var emitted = lowered.module().functions().getFirst().instructions();
assertEquals(IRVMOp.JMP, emitted.get(0).op());
assertEquals(6, emitted.get(0).immediate());
assertEquals(IRVMOp.RET, emitted.get(1).op());
}
@Test
void lowerMustRejectMissingJumpTargetLabel() {
final var backend = IRBackend.builder()
.executableFunctions(ReadOnlyList.from(
fn("main", "app", ReadOnlyList.from(
jmp("missing"),
ret()))))
.build();
final var thrown = assertThrows(IRVMLoweringException.class, () -> new LowerToIRVMService().lower(backend));
assertEquals(IRVMLoweringErrorCode.LOWER_IRVM_MISSING_JUMP_TARGET, thrown.code());
}
private static IRBackendExecutableFunction fn(
final String name,
final String moduleKey,
@@ -175,4 +207,32 @@ class LowerToIRVMServiceTest {
new IRBackendExecutableFunction.IntrinsicCallMetadata(canonicalName, canonicalVersion, intrinsicId),
Span.none());
}
private static IRBackendExecutableFunction.Instruction label(final String label) {
return new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.LABEL,
"",
"",
null,
null,
label,
"",
null,
null,
Span.none());
}
private static IRBackendExecutableFunction.Instruction jmp(final String target) {
return new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.JMP,
"",
"",
null,
null,
"",
target,
null,
null,
Span.none());
}
}