Skip to content

Commit c610199

Browse files
agattidpgeorge
authored andcommitted
py/asmarm: Fix halfword loads with larger offsets.
This commit fixes code generation for loading halfwords using an offset greater than 255. The old code blindly encoded the offset into a `LDRH Rd, [Rn, #imm]` opcode, but only the lowest 8 bits would be put into the opcode itself. This commit instead generates a two-opcodes sequence, a constant load into R8, and then `LDRH Rd, [Rn, R8]`. This fixes `tests/extmod/vfs_rom.py` for the qemu/SABRELITE board. Signed-off-by: Alessandro Gatti <[email protected]>
1 parent 928c716 commit c610199

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

py/asmarm.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,15 @@ void asm_arm_ldrh_reg_reg(asm_arm_t *as, uint rd, uint rn) {
344344
}
345345

346346
void asm_arm_ldrh_reg_reg_offset(asm_arm_t *as, uint rd, uint rn, uint byte_offset) {
347-
// ldrh rd, [rn, #off]
348-
emit_al(as, 0x1d000b0 | (rn << 16) | (rd << 12) | ((byte_offset & 0xf0) << 4) | (byte_offset & 0xf));
347+
if (byte_offset < 0x100) {
348+
// ldrh rd, [rn, #off]
349+
emit_al(as, 0x1d000b0 | (rn << 16) | (rd << 12) | ((byte_offset & 0xf0) << 4) | (byte_offset & 0xf));
350+
} else {
351+
// mov r8, #off
352+
// ldrh rd, [rn, r8]
353+
asm_arm_mov_reg_i32_optimised(as, ASM_ARM_REG_R8, byte_offset);
354+
emit_al(as, 0x19000b0 | (rn << 16) | (rd << 12) | ASM_ARM_REG_R8);
355+
}
349356
}
350357

351358
void asm_arm_ldrb_reg_reg(asm_arm_t *as, uint rd, uint rn) {

0 commit comments

Comments
 (0)