Skip to content

Commit 5345743

Browse files
committed
py: Make inline assembler raise exception when branch not in range.
Addresses issue adafruit#1132.
1 parent 11aa6ba commit 5345743

3 files changed

Lines changed: 41 additions & 29 deletions

File tree

py/asmthumb.c

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -293,27 +293,44 @@ void asm_thumb_mov_reg_i16(asm_thumb_t *as, uint mov_op, uint reg_dest, int i16_
293293

294294
#define OP_B_N(byte_offset) (0xe000 | (((byte_offset) >> 1) & 0x07ff))
295295

296-
void asm_thumb_b_n(asm_thumb_t *as, uint label) {
296+
bool asm_thumb_b_n_label(asm_thumb_t *as, uint label) {
297297
mp_uint_t dest = get_label_dest(as, label);
298298
mp_int_t rel = dest - as->code_offset;
299299
rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
300300
if (SIGNED_FIT12(rel)) {
301301
asm_thumb_op16(as, OP_B_N(rel));
302+
return true;
302303
} else {
303-
printf("asm_thumb_b_n: branch does not fit in 12 bits\n");
304+
return false;
304305
}
305306
}
306307

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

309-
void asm_thumb_bcc_n(asm_thumb_t *as, int cond, uint label) {
310+
bool asm_thumb_bcc_n_label(asm_thumb_t *as, int cond, uint label) {
310311
mp_uint_t dest = get_label_dest(as, label);
311312
mp_int_t rel = dest - as->code_offset;
312313
rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
313314
if (SIGNED_FIT9(rel)) {
314315
asm_thumb_op16(as, OP_BCC_N(cond, rel));
316+
return true;
315317
} else {
316-
printf("asm_thumb_bcc_n: branch does not fit in 9 bits\n");
318+
return false;
319+
}
320+
}
321+
322+
#define OP_BL_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
323+
#define OP_BL_LO(byte_offset) (0xf800 | (((byte_offset) >> 1) & 0x07ff))
324+
325+
bool asm_thumb_bl_label(asm_thumb_t *as, uint label) {
326+
mp_uint_t dest = get_label_dest(as, label);
327+
mp_int_t rel = dest - as->code_offset;
328+
rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
329+
if (SIGNED_FIT23(rel)) {
330+
asm_thumb_op32(as, OP_BL_HI(rel), OP_BL_LO(rel));
331+
return true;
332+
} else {
333+
return false;
317334
}
318335
}
319336

@@ -422,20 +439,6 @@ void asm_thumb_bcc_label(asm_thumb_t *as, int cond, uint label) {
422439
}
423440
}
424441

425-
#define OP_BL_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
426-
#define OP_BL_LO(byte_offset) (0xf800 | (((byte_offset) >> 1) & 0x07ff))
427-
428-
void asm_thumb_bl(asm_thumb_t *as, uint label) {
429-
mp_uint_t dest = get_label_dest(as, label);
430-
mp_int_t rel = dest - as->code_offset;
431-
rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
432-
if (SIGNED_FIT23(rel)) {
433-
asm_thumb_op32(as, OP_BL_HI(rel), OP_BL_LO(rel));
434-
} else {
435-
printf("asm_thumb_bl: branch does not fit in 23 bits\n");
436-
}
437-
}
438-
439442
#define OP_BLX(reg) (0x4780 | ((reg) << 3))
440443
#define OP_SVC(arg) (0xdf00 | (arg))
441444

py/asmthumb.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,10 @@ static inline void asm_thumb_ldrh_rlo_rlo_i5(asm_thumb_t *as, uint rlo_dest, uin
214214
void asm_thumb_mov_reg_reg(asm_thumb_t *as, uint reg_dest, uint reg_src);
215215
void asm_thumb_mov_reg_i16(asm_thumb_t *as, uint mov_op, uint reg_dest, int i16_src);
216216

217-
void asm_thumb_b_n(asm_thumb_t *as, uint label);
218-
void asm_thumb_bcc_n(asm_thumb_t *as, int cond, uint label);
217+
// these return true if the destination is in range, false otherwise
218+
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);
220+
bool asm_thumb_bl_label(asm_thumb_t *as, uint label);
219221

220222
void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, mp_uint_t i32_src); // convenience
221223
void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32_src); // convenience
@@ -224,9 +226,8 @@ void asm_thumb_mov_local_reg(asm_thumb_t *as, int local_num_dest, uint rlo_src);
224226
void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num); // convenience
225227
void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num); // convenience
226228

227-
void asm_thumb_b_label(asm_thumb_t *as, uint label); // convenience ?
229+
void asm_thumb_b_label(asm_thumb_t *as, uint label); // convenience: picks narrow or wide branch
228230
void asm_thumb_bcc_label(asm_thumb_t *as, int cc, uint label); // convenience: picks narrow or wide branch
229-
void asm_thumb_bl(asm_thumb_t *as, uint label);
230-
void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp); // convenience ?
231+
void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp); // convenience
231232

232233
#endif // __MICROPY_INCLUDED_PY_ASMTHUMB_H__

py/emitinlinethumb.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,14 @@ STATIC void emit_inline_thumb_op(emit_inline_asm_t *emit, qstr op, mp_uint_t n_a
367367
} else if (n_args == 1) {
368368
if (strcmp(op_str, "b") == 0) {
369369
int label_num = get_arg_label(emit, op_str, pn_args[0]);
370-
// TODO check that this succeeded, ie branch was within range
371-
asm_thumb_b_n(emit->as, label_num);
370+
if (!asm_thumb_b_n_label(emit->as, label_num)) {
371+
goto branch_not_in_range;
372+
}
372373
} else if (strcmp(op_str, "bl") == 0) {
373374
int label_num = get_arg_label(emit, op_str, pn_args[0]);
374-
// TODO check that this succeeded, ie branch was within range
375-
asm_thumb_bl(emit->as, label_num);
375+
if (!asm_thumb_bl_label(emit->as, label_num)) {
376+
goto branch_not_in_range;
377+
}
376378
} else if (strcmp(op_str, "bx") == 0) {
377379
mp_uint_t r = get_arg_reg(emit, op_str, pn_args[0], 15);
378380
asm_thumb_op16(emit->as, 0x4700 | (r << 3));
@@ -387,8 +389,9 @@ STATIC void emit_inline_thumb_op(emit_inline_asm_t *emit, qstr op, mp_uint_t n_a
387389
goto unknown_op;
388390
}
389391
int label_num = get_arg_label(emit, op_str, pn_args[0]);
390-
// TODO check that this succeeded, ie branch was within range
391-
asm_thumb_bcc_n(emit->as, cc, label_num);
392+
if (!asm_thumb_bcc_n_label(emit->as, cc, label_num)) {
393+
goto branch_not_in_range;
394+
}
392395
} else if (op_str[0] == 'i' && op_str[1] == 't') {
393396
const char *arg_str = get_arg_str(pn_args[0]);
394397
mp_uint_t cc = -1;
@@ -608,6 +611,11 @@ STATIC void emit_inline_thumb_op(emit_inline_asm_t *emit, qstr op, mp_uint_t n_a
608611

609612
unknown_op:
610613
emit_inline_thumb_error_exc(emit, mp_obj_new_exception_msg_varg(&mp_type_SyntaxError, "unsupported Thumb instruction '%s' with %d arguments", op_str, n_args));
614+
return;
615+
616+
branch_not_in_range:
617+
emit_inline_thumb_error_msg(emit, "branch not in range");
618+
return;
611619
}
612620

613621
const emit_inline_asm_method_table_t emit_inline_thumb_method_table = {

0 commit comments

Comments
 (0)