Skip to content

Commit 43f6013

Browse files
agattidpgeorge
authored andcommitted
py/asmxtensa: Extend BCC range to 18 bits.
This commit lets the native emitter backend extends the range of the BCC family of opcodes (BALL, BANY, BBC, BBS, BEQ, BGE, BGEU, BLT, BLTU, BNALL, BNE, BNONE) from 8 bits to 18 bits. The test suite contains some test files that, when compiled into native code, would require BCC jumps outside the (signed) 8 bits range. In this case either the MicroPython interpreter or mpy-cross would raise an exception, not running the test when using the "--via-mpy --emit native" command line options with the test runner. This comes with a 3 bytes penalty on each forward jump, bringing the footprint of those jumps to 6 bytes each, as a longer opcode sequence has to be emitted to let jumps access a larger range. However, this is slightly offset by the fact that backward jumps can be emitted with a single opcode if the range is small enough (8-bits offset). Signed-off-by: Alessandro Gatti <[email protected]>
1 parent 80b823b commit 43f6013

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

py/asmxtensa.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,27 @@ void asm_xtensa_bccz_reg_label(asm_xtensa_t *as, uint cond, uint reg, uint label
192192
}
193193

194194
void asm_xtensa_bcc_reg_reg_label(asm_xtensa_t *as, uint cond, uint reg1, uint reg2, uint label) {
195-
uint32_t dest = get_label_dest(as, label);
196-
int32_t rel = dest - as->base.code_offset - 4;
197-
if (as->base.pass == MP_ASM_PASS_EMIT && !SIGNED_FIT8(rel)) {
195+
ptrdiff_t rel = 0;
196+
bool can_emit_short_jump = calculate_branch_displacement(as, label, &rel);
197+
198+
if (can_emit_short_jump && SIGNED_FIT8(rel)) {
199+
// Backwards BCC opcodes with an offset that fits in 8 bits can
200+
// be emitted without any change.
201+
asm_xtensa_op_bcc(as, cond, reg1, reg2, rel);
202+
return;
203+
}
204+
205+
// Range is effectively extended to 18 bits, as a more complex jump code
206+
// sequence is emitted.
207+
if (as->base.pass == MP_ASM_PASS_EMIT && !SIGNED_FIT18(rel - 6)) {
198208
mp_raise_msg_varg(&mp_type_RuntimeError, ET_OUT_OF_RANGE, MP_QSTR_bcc);
199209
}
200-
asm_xtensa_op_bcc(as, cond, reg1, reg2, rel);
210+
211+
// ~BCC skip ; +0 <- Condition is flipped here (EQ -> NE, etc.)
212+
// J addr ; +3
213+
// skip: ; +6
214+
asm_xtensa_op_bcc(as, cond ^ 8, reg1, reg2, 6 - 4);
215+
asm_xtensa_op_j(as, rel - 3);
201216
}
202217

203218
// convenience function; reg_dest must be different from reg_src[12]

0 commit comments

Comments
 (0)