Skip to content

Commit 03d4124

Browse files
committed
Add b_n opcode to inline thumb asm.
1 parent b14de21 commit 03d4124

3 files changed

Lines changed: 28 additions & 5 deletions

File tree

py/asmthumb.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,19 @@ void asm_thumb_cmp_rlo_i8(asm_thumb_t *as, uint rlo, int i8) {
288288
asm_thumb_write_op16(as, OP_CMP_RLO_I8(rlo, i8));
289289
}
290290

291+
#define OP_B_N(byte_offset) (0xe000 | (((byte_offset) >> 1) & 0x07ff))
292+
293+
void asm_thumb_b_n(asm_thumb_t *as, int label) {
294+
int dest = get_label_dest(as, label);
295+
int rel = dest - as->code_offset;
296+
rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
297+
if (SIGNED_FIT12(rel)) {
298+
asm_thumb_write_op16(as, OP_B_N(rel));
299+
} else {
300+
printf("asm_thumb_b_n: branch does not fit in 12 bits\n");
301+
}
302+
}
303+
291304
#define OP_BEQ_N(byte_offset) (0xd000 | (((byte_offset) >> 1) & 0x00ff))
292305
#define OP_BNE_N(byte_offset) (0xd100 | (((byte_offset) >> 1) & 0x00ff))
293306
#define OP_BCS_N(byte_offset) (0xd200 | (((byte_offset) >> 1) & 0x00ff))
@@ -371,7 +384,6 @@ void asm_thumb_ite_ge(asm_thumb_t *as) {
371384
asm_thumb_write_op16(as, 0xbfac);
372385
}
373386

374-
#define OP_B(byte_offset) (0xe000 | (((byte_offset) >> 1) & 0x07ff))
375387
// this could be wrong, because it should have a range of +/- 16MiB...
376388
#define OP_BW_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
377389
#define OP_BW_LO(byte_offset) (0xb800 | (((byte_offset) >> 1) & 0x07ff))
@@ -384,7 +396,7 @@ void asm_thumb_b_label(asm_thumb_t *as, int label) {
384396
// is a backwards jump, so we know the size of the jump on the first pass
385397
// calculate rel assuming 12 bit relative jump
386398
if (SIGNED_FIT12(rel)) {
387-
asm_thumb_write_op16(as, OP_B(rel));
399+
asm_thumb_write_op16(as, OP_B_N(rel));
388400
} else {
389401
goto large_jump;
390402
}

py/asmthumb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void asm_thumb_movt_reg_i16(asm_thumb_t *as, uint reg_dest, int i16_src);
4949
void asm_thumb_mov_reg_reg(asm_thumb_t *as, uint reg_dest, uint reg_src);
5050
void asm_thumb_subs_rlo_rlo_i3(asm_thumb_t *as, uint rlo_dest, uint rlo_src, int i3_src);
5151
void asm_thumb_cmp_rlo_i8(asm_thumb_t *as, uint rlo, int i8);
52+
void asm_thumb_b_n(asm_thumb_t *as, int label);
5253
void asm_thumb_bgt_n(asm_thumb_t *as, int label);
5354

5455
void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, machine_uint_t i32_src); // convenience

py/emitinlinethumb.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static void emit_inline_thumb_end_pass(emit_inline_asm_t *emit) {
5252

5353
static int emit_inline_thumb_count_params(emit_inline_asm_t *emit, int n_params, py_parse_node_t *pn_params) {
5454
if (n_params > 4) {
55-
printf("SyntaxError: can only have up to 3 parameters to inline assembler\n");
55+
printf("SyntaxError: can only have up to 4 parameters to inline thumb assembly\n");
5656
return 0;
5757
}
5858
for (int i = 0; i < n_params; i++) {
@@ -62,7 +62,7 @@ static int emit_inline_thumb_count_params(emit_inline_asm_t *emit, int n_params,
6262
}
6363
const char *p = qstr_str(PY_PARSE_NODE_LEAF_ARG(pn_params[i]));
6464
if (!(strlen(p) == 2 && p[0] == 'r' && p[1] == '0' + i)) {
65-
printf("SyntaxError: parameter %d to inline assembler must be r%d\n", i, i);
65+
printf("SyntaxError: parameter %d to inline assembler must be r%d\n", i + 1, i);
6666
return 0;
6767
}
6868
}
@@ -128,14 +128,24 @@ static int get_arg_label(emit_inline_asm_t *emit, qstr op, py_parse_node_t *pn_a
128128

129129
static void emit_inline_thumb_op(emit_inline_asm_t *emit, qstr op, int n_args, py_parse_node_t *pn_args) {
130130
// TODO perhaps make two tables:
131+
// one_args =
132+
// "b", LAB, asm_thumb_b_n,
133+
// "bgt", LAB, asm_thumb_bgt_n,
131134
// two_args =
132135
// "movs", RLO, I8, asm_thumb_movs_reg_i8
133136
// "movw", REG, REG, asm_thumb_movw_reg_i16
134137
// three_args =
135138
// "subs", RLO, RLO, I3, asm_thumb_subs_reg_reg_i3
136139

137140
// 1 arg
138-
if (strcmp(qstr_str(op), "bgt") == 0) {
141+
if (strcmp(qstr_str(op), "b") == 0) {
142+
if (!check_n_arg(op, n_args, 1)) {
143+
return;
144+
}
145+
int label_num = get_arg_label(emit, op, pn_args, 0);
146+
// TODO check that this succeeded, ie branch was within range
147+
asm_thumb_b_n(emit->as, label_num);
148+
} else if (strcmp(qstr_str(op), "bgt") == 0) {
139149
if (!check_n_arg(op, n_args, 1)) {
140150
return;
141151
}

0 commit comments

Comments
 (0)