Skip to content

Commit 9f142f0

Browse files
committed
py: For inline assembler, add bcc_n and bcc_w ops.
Addresses issue adafruit#1143.
1 parent 565da3f commit 9f142f0

3 files changed

Lines changed: 19 additions & 12 deletions

File tree

py/asmthumb.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -307,15 +307,24 @@ bool asm_thumb_b_n_label(asm_thumb_t *as, uint label) {
307307

308308
#define OP_BCC_N(cond, byte_offset) (0xd000 | ((cond) << 8) | (((byte_offset) >> 1) & 0x00ff))
309309

310-
bool asm_thumb_bcc_n_label(asm_thumb_t *as, int cond, uint label) {
310+
// all these bit arithmetics need coverage testing!
311+
#define OP_BCC_W_HI(cond, byte_offset) (0xf000 | ((cond) << 6) | (((byte_offset) >> 10) & 0x0400) | (((byte_offset) >> 14) & 0x003f))
312+
#define OP_BCC_W_LO(byte_offset) (0x8000 | ((byte_offset) & 0x2000) | (((byte_offset) >> 1) & 0x0fff))
313+
314+
bool asm_thumb_bcc_nw_label(asm_thumb_t *as, int cond, uint label, bool wide) {
311315
mp_uint_t dest = get_label_dest(as, label);
312316
mp_int_t rel = dest - as->code_offset;
313317
rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
314-
if (SIGNED_FIT9(rel)) {
315-
asm_thumb_op16(as, OP_BCC_N(cond, rel));
316-
return true;
318+
if (!wide) {
319+
if (SIGNED_FIT9(rel)) {
320+
asm_thumb_op16(as, OP_BCC_N(cond, rel));
321+
return true;
322+
} else {
323+
return false;
324+
}
317325
} else {
318-
return false;
326+
asm_thumb_op32(as, OP_BCC_W_HI(cond, rel), OP_BCC_W_LO(rel));
327+
return true;
319328
}
320329
}
321330

@@ -416,10 +425,6 @@ void asm_thumb_b_label(asm_thumb_t *as, uint label) {
416425
}
417426
}
418427

419-
// all these bit arithmetics need coverage testing!
420-
#define OP_BCC_W_HI(cond, byte_offset) (0xf000 | ((cond) << 6) | (((byte_offset) >> 10) & 0x0400) | (((byte_offset) >> 14) & 0x003f))
421-
#define OP_BCC_W_LO(byte_offset) (0x8000 | ((byte_offset) & 0x2000) | (((byte_offset) >> 1) & 0x0fff))
422-
423428
void asm_thumb_bcc_label(asm_thumb_t *as, int cond, uint label) {
424429
mp_uint_t dest = get_label_dest(as, label);
425430
mp_int_t rel = dest - as->code_offset;

py/asmthumb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ void asm_thumb_mov_reg_i16(asm_thumb_t *as, uint mov_op, uint reg_dest, int i16_
216216

217217
// these return true if the destination is in range, false otherwise
218218
bool asm_thumb_b_n_label(asm_thumb_t *as, uint label);
219-
bool asm_thumb_bcc_n_label(asm_thumb_t *as, int cond, uint label);
219+
bool asm_thumb_bcc_nw_label(asm_thumb_t *as, int cond, uint label, bool wide);
220220
bool asm_thumb_bl_label(asm_thumb_t *as, uint label);
221221

222222
void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, mp_uint_t i32_src); // convenience

py/emitinlinethumb.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,9 @@ STATIC void emit_inline_thumb_op(emit_inline_asm_t *emit, qstr op, mp_uint_t n_a
378378
} else if (strcmp(op_str, "bx") == 0) {
379379
mp_uint_t r = get_arg_reg(emit, op_str, pn_args[0], 15);
380380
asm_thumb_op16(emit->as, 0x4700 | (r << 3));
381-
} else if (op_str[0] == 'b' && op_len == 3) {
381+
} else if (op_str[0] == 'b' && (op_len == 3
382+
|| (op_len == 5 && op_str[3] == '_'
383+
&& (op_str[4] == 'n' || op_str[4] == 'w')))) {
382384
mp_uint_t cc = -1;
383385
for (mp_uint_t i = 0; i < MP_ARRAY_SIZE(cc_name_table); i++) {
384386
if (op_str[1] == cc_name_table[i].name[0] && op_str[2] == cc_name_table[i].name[1]) {
@@ -389,7 +391,7 @@ STATIC void emit_inline_thumb_op(emit_inline_asm_t *emit, qstr op, mp_uint_t n_a
389391
goto unknown_op;
390392
}
391393
int label_num = get_arg_label(emit, op_str, pn_args[0]);
392-
if (!asm_thumb_bcc_n_label(emit->as, cc, label_num)) {
394+
if (!asm_thumb_bcc_nw_label(emit->as, cc, label_num, op_len == 5 && op_str[4] == 'w')) {
393395
goto branch_not_in_range;
394396
}
395397
} else if (op_str[0] == 'i' && op_str[1] == 't') {

0 commit comments

Comments
 (0)