Skip to content

Commit 1a6633a

Browse files
committed
Implement more thumb branch instructions.
1 parent a53f694 commit 1a6633a

4 files changed

Lines changed: 45 additions & 36 deletions

File tree

py/asmthumb.c

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -301,29 +301,16 @@ void asm_thumb_b_n(asm_thumb_t *as, int label) {
301301
}
302302
}
303303

304-
#define OP_BEQ_N(byte_offset) (0xd000 | (((byte_offset) >> 1) & 0x00ff))
305-
#define OP_BNE_N(byte_offset) (0xd100 | (((byte_offset) >> 1) & 0x00ff))
306-
#define OP_BCS_N(byte_offset) (0xd200 | (((byte_offset) >> 1) & 0x00ff))
307-
#define OP_BCC_N(byte_offset) (0xd300 | (((byte_offset) >> 1) & 0x00ff))
308-
#define OP_BMI_N(byte_offset) (0xd400 | (((byte_offset) >> 1) & 0x00ff))
309-
#define OP_BPL_N(byte_offset) (0xd500 | (((byte_offset) >> 1) & 0x00ff))
310-
#define OP_BVS_N(byte_offset) (0xd600 | (((byte_offset) >> 1) & 0x00ff))
311-
#define OP_BVC_N(byte_offset) (0xd700 | (((byte_offset) >> 1) & 0x00ff))
312-
#define OP_BHI_N(byte_offset) (0xd800 | (((byte_offset) >> 1) & 0x00ff))
313-
#define OP_BLS_N(byte_offset) (0xd900 | (((byte_offset) >> 1) & 0x00ff))
314-
#define OP_BGE_N(byte_offset) (0xda00 | (((byte_offset) >> 1) & 0x00ff))
315-
#define OP_BLT_N(byte_offset) (0xdb00 | (((byte_offset) >> 1) & 0x00ff))
316-
#define OP_BGT_N(byte_offset) (0xdc00 | (((byte_offset) >> 1) & 0x00ff))
317-
#define OP_BLE_N(byte_offset) (0xdd00 | (((byte_offset) >> 1) & 0x00ff))
318-
319-
void asm_thumb_bgt_n(asm_thumb_t *as, int label) {
304+
#define OP_BCC_N(cond, byte_offset) (0xd000 | ((cond) << 8) | (((byte_offset) >> 1) & 0x00ff))
305+
306+
void asm_thumb_bcc_n(asm_thumb_t *as, int cond, int label) {
320307
int dest = get_label_dest(as, label);
321308
int rel = dest - as->code_offset;
322309
rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
323310
if (SIGNED_FIT9(rel)) {
324-
asm_thumb_write_op16(as, OP_BGT_N(rel));
311+
asm_thumb_write_op16(as, OP_BCC_N(cond, rel));
325312
} else {
326-
printf("asm_thumb_bgt: branch does not fit in 9 bits\n");
313+
printf("asm_thumb_bcc_n: branch does not fit in 9 bits\n");
327314
}
328315
}
329316

@@ -408,32 +395,25 @@ void asm_thumb_b_label(asm_thumb_t *as, int label) {
408395
}
409396

410397
// all these bit arithmetics need coverage testing!
411-
#define OP_BEQ(byte_offset) (0xd000 | (((byte_offset) >> 1) & 0x00ff))
412-
#define OP_BEQW_HI(byte_offset) (0xf000 | (((byte_offset) >> 10) & 0x0400) | (((byte_offset) >> 14) & 0x003f))
413-
#define OP_BEQW_LO(byte_offset) (0x8000 | ((byte_offset) & 0x2000) | (((byte_offset) >> 1) & 0x0fff))
414-
415-
void asm_thumb_cmp_reg_bz_label(asm_thumb_t *as, uint rlo, int label) {
416-
assert(rlo < REG_R8);
417-
418-
// compare reg with 0
419-
asm_thumb_write_op16(as, OP_CMP_RLO_I8(rlo, 0));
398+
#define OP_BCC_W_HI(cond, byte_offset) (0xf000 | ((cond) << 6) | (((byte_offset) >> 10) & 0x0400) | (((byte_offset) >> 14) & 0x003f))
399+
#define OP_BCC_W_LO(byte_offset) (0x8000 | ((byte_offset) & 0x2000) | (((byte_offset) >> 1) & 0x0fff))
420400

421-
// branch if equal
401+
void asm_thumb_bcc_label(asm_thumb_t *as, int cond, int label) {
422402
int dest = get_label_dest(as, label);
423403
int rel = dest - as->code_offset;
424404
rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
425405
if (dest >= 0 && rel <= -4) {
426406
// is a backwards jump, so we know the size of the jump on the first pass
427407
// calculate rel assuming 9 bit relative jump
428408
if (SIGNED_FIT9(rel)) {
429-
asm_thumb_write_op16(as, OP_BEQ(rel));
409+
asm_thumb_write_op16(as, OP_BCC_N(cond, rel));
430410
} else {
431411
goto large_jump;
432412
}
433413
} else {
434414
// is a forwards jump, so need to assume it's large
435415
large_jump:
436-
asm_thumb_write_op32(as, OP_BEQW_HI(rel), OP_BEQW_LO(rel));
416+
asm_thumb_write_op32(as, OP_BCC_W_HI(cond, rel), OP_BCC_W_LO(rel));
437417
}
438418
}
439419

py/asmthumb.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@
2626
#define REG_ARG_3 REG_R2
2727
#define REG_ARG_4 REG_R3
2828

29+
#define THUMB_CC_EQ (0x0)
30+
#define THUMB_CC_NE (0x1)
31+
#define THUMB_CC_CS (0x2)
32+
#define THUMB_CC_CC (0x3)
33+
#define THUMB_CC_MI (0x4)
34+
#define THUMB_CC_PL (0x5)
35+
#define THUMB_CC_VS (0x6)
36+
#define THUMB_CC_VC (0x7)
37+
#define THUMB_CC_HI (0x8)
38+
#define THUMB_CC_LS (0x9)
39+
#define THUMB_CC_GE (0xa)
40+
#define THUMB_CC_LT (0xb)
41+
#define THUMB_CC_GT (0xc)
42+
#define THUMB_CC_LE (0xd)
43+
2944
typedef struct _asm_thumb_t asm_thumb_t;
3045

3146
asm_thumb_t *asm_thumb_new(uint max_num_labels);
@@ -50,7 +65,7 @@ void asm_thumb_mov_reg_reg(asm_thumb_t *as, uint reg_dest, uint reg_src);
5065
void asm_thumb_subs_rlo_rlo_i3(asm_thumb_t *as, uint rlo_dest, uint rlo_src, int i3_src);
5166
void asm_thumb_cmp_rlo_i8(asm_thumb_t *as, uint rlo, int i8);
5267
void asm_thumb_b_n(asm_thumb_t *as, int label);
53-
void asm_thumb_bgt_n(asm_thumb_t *as, int label);
68+
void asm_thumb_bcc_n(asm_thumb_t *as, int cond, int label);
5469

5570
void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, machine_uint_t i32_src); // convenience
5671
void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32_src); // convenience
@@ -63,6 +78,6 @@ void asm_thumb_cmp_reg_reg(asm_thumb_t *as, uint rlo_a, uint rlo_b); // convenie
6378
void asm_thumb_ite_ge(asm_thumb_t *as); // convenience ?
6479

6580
void asm_thumb_b_label(asm_thumb_t *as, int label); // convenience ?
66-
void asm_thumb_cmp_reg_bz_label(asm_thumb_t *as, uint rlo, int label); // convenience ?
81+
void asm_thumb_bcc_label(asm_thumb_t *as, int cc, int label); // convenience: picks narrow or wide branch
6782
void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp); // convenience ?
6883

py/emitinlinethumb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static void emit_inline_thumb_op(emit_inline_asm_t *emit, qstr op, int n_args, p
154154
}
155155
int label_num = get_arg_label(emit, op, pn_args, 0);
156156
// TODO check that this succeeded, ie branch was within range
157-
asm_thumb_bgt_n(emit->as, label_num);
157+
asm_thumb_bcc_n(emit->as, THUMB_CC_GT, label_num);
158158

159159
// 2 args
160160
} else if (strcmp(qstr_str(op), "movs") == 0) {

py/emitnative.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ static void emit_native_jump(emit_t *emit, int label) {
871871
emit_post(emit);
872872
}
873873

874-
static void emit_native_pop_jump_if_false(emit_t *emit, int label) {
874+
static void emit_native_pop_jump_pre_helper(emit_t *emit, int label) {
875875
vtype_kind_t vtype = peek_vtype(emit);
876876
if (vtype == VTYPE_BOOL) {
877877
emit_pre_pop_reg(emit, &vtype, REG_RET);
@@ -882,18 +882,32 @@ static void emit_native_pop_jump_if_false(emit_t *emit, int label) {
882882
printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
883883
assert(0);
884884
}
885+
}
886+
887+
static void emit_native_pop_jump_if_false(emit_t *emit, int label) {
888+
emit_native_pop_jump_pre_helper(emit, label);
885889
#if N_X64
886890
asm_x64_test_r8_with_r8(emit->as, REG_RET, REG_RET);
887891
asm_x64_jcc_label(emit->as, JCC_JZ, label);
888892
#elif N_THUMB
889-
asm_thumb_cmp_reg_bz_label(emit->as, REG_RET, label);
893+
asm_thumb_cmp_rlo_i8(emit->as, REG_RET, 0);
894+
asm_thumb_bcc_label(emit->as, THUMB_CC_EQ, label);
890895
#endif
891896
emit_post(emit);
892897
}
893898

894899
static void emit_native_pop_jump_if_true(emit_t *emit, int label) {
895-
assert(0);
900+
emit_native_pop_jump_pre_helper(emit, label);
901+
#if N_X64
902+
asm_x64_test_r8_with_r8(emit->as, REG_RET, REG_RET);
903+
asm_x64_jcc_label(emit->as, JCC_JNZ, label);
904+
#elif N_THUMB
905+
asm_thumb_cmp_rlo_i8(emit->as, REG_RET, 0);
906+
asm_thumb_bcc_label(emit->as, THUMB_CC_NE, label);
907+
#endif
908+
emit_post(emit);
896909
}
910+
897911
static void emit_native_jump_if_true_or_pop(emit_t *emit, int label) {
898912
assert(0);
899913
}

0 commit comments

Comments
 (0)