Read the jalr base before writing the link
4799a8d707ead5e838d27140f5780879b2b51be3384148556f9892448138e260
The interpreter wrote rd before computing the target from rs1, so a jalr with rd equal to rs1, as in `auipc ra, hi; jalr ra, lo(ra)`, jumped relative to the return address. Translated code was correct. Assisted-By: Claude:opus-5.5 Change-Id: I19f1625d6abda2545b5de4e6b77c26ddc1828aaa
1 parent
df02cea0
emulator.c
+3 -2
| 1681 | 1681 | break; |
|
| 1682 | 1682 | } |
|
| 1683 | 1683 | ||
| 1684 | 1684 | case OP_JALR: { |
|
| 1685 | 1685 | i32 imm = get_i_imm(ins); |
|
| 1686 | + | /* Calculate target address in full 64-bit precision, before |
|
| 1687 | + | * writing rd, which may be rs1. */ |
|
| 1688 | + | u64 jalr_target = (cpu->regs[ins.i.rs1] + (i64)imm) & ~(u64)1; |
|
| 1686 | 1689 | if (ins.i.rd != 0) { |
|
| 1687 | 1690 | cpu->regs[ins.i.rd] = pc_next; |
|
| 1688 | 1691 | cpu->modified = (reg_t)ins.i.rd; |
|
| 1689 | 1692 | } |
|
| 1690 | - | /* Calculate target address in full 64-bit precision. */ |
|
| 1691 | - | u64 jalr_target = (cpu->regs[ins.i.rs1] + (i64)imm) & ~(u64)1; |
|
| 1692 | 1693 | /* Check if this is a RET instruction (jalr x0, ra, 0). */ |
|
| 1693 | 1694 | if (ins.i.rd == 0 && ins.i.rs1 == 1 && imm == 0 && jalr_target == 0) { |
|
| 1694 | 1695 | cpu->running = false; |
|
| 1695 | 1696 | if (!headless) { |
|
| 1696 | 1697 | ui_render(cpu, display); |
test/jit_calls.c
+6 -3
| 1 | 1 | /* Writes a raw RV64 image for comparing translated and interpreted runs. |
|
| 2 | 2 | * |
|
| 3 | - | * The program makes calls through jal and through auipc and jalr, returns |
|
| 4 | - | * through ra, saves ra in a non-leaf function, branches around ebreak as a |
|
| 5 | - | * bounds check does, and uses LR and SC. Translated code follows these |
|
| 3 | + | * The program makes calls through jal and through auipc and jalr, also with |
|
| 4 | + | * a jalr that reads the register it writes. It returns through ra, saves ra |
|
| 5 | + | * in a non-leaf function, branches around ebreak as a bounds check does, and |
|
| 6 | + | * uses LR and SC. Translated code follows these |
|
| 6 | 7 | * calls and checks the returns in place, so both runs must end with the |
|
| 7 | 8 | * same status after the same number of instructions. */ |
|
| 8 | 9 | #include <stdint.h> |
|
| 9 | 10 | #include <stdio.h> |
|
| 10 | 11 |
| 94 | 95 | addi(A1, S1, 0); |
|
| 95 | 96 | ref(0, RA << 7 | 0x6f, LEAF); /* jal ra, leaf */ |
|
| 96 | 97 | ref(0, RA << 7 | 0x6f, NEST); /* jal ra, nest */ |
|
| 97 | 98 | emit(T5 << 7 | 0x17); /* auipc t5, 0 */ |
|
| 98 | 99 | ref(2, i_type(0x67, RA, 0, T5, 0), LEAF); /* jalr ra, leaf(t5) */ |
|
| 100 | + | emit(RA << 7 | 0x17); /* auipc ra, 0 */ |
|
| 101 | + | ref(2, i_type(0x67, RA, 0, RA, 0), LEAF); /* jalr ra, leaf(ra) */ |
|
| 99 | 102 | addi(S1, S1, -1); |
|
| 100 | 103 | ref(1, r_type(0x63, 0, 1, S1, ZERO, 0), LOOP); /* bnez s1, loop */ |
|
| 101 | 104 | ||
| 102 | 105 | /* Add 7 to a doubleword with LR and SC, then fold it into a0. */ |
|
| 103 | 106 | emit(0x1000302f | T6 << 15 | T0 << 7); /* lr.d t0, (t6) */ |