Skip to content

Commit 6f355fd

Browse files
committed
py: Make labels unsigned ints (converted from int).
Labels should never be negative, and this modified type signature reflects that.
1 parent bf8ae4d commit 6f355fd

8 files changed

Lines changed: 108 additions & 108 deletions

File tree

py/asmthumb.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct _asm_thumb_t {
2222
byte *code_base;
2323
byte dummy_data[8];
2424

25-
int max_num_labels;
25+
uint max_num_labels;
2626
int *label_offsets;
2727
int num_locals;
2828
uint push_reglist;
@@ -212,7 +212,7 @@ void asm_thumb_exit(asm_thumb_t *as) {
212212
asm_thumb_write_op16(as, OP_POP_RLIST_PC(as->push_reglist));
213213
}
214214

215-
void asm_thumb_label_assign(asm_thumb_t *as, int label) {
215+
void asm_thumb_label_assign(asm_thumb_t *as, uint label) {
216216
assert(label < as->max_num_labels);
217217
if (as->pass == ASM_THUMB_PASS_2) {
218218
// assign label offset
@@ -225,7 +225,7 @@ void asm_thumb_label_assign(asm_thumb_t *as, int label) {
225225
}
226226
}
227227

228-
STATIC int get_label_dest(asm_thumb_t *as, int label) {
228+
STATIC int get_label_dest(asm_thumb_t *as, uint label) {
229229
assert(label < as->max_num_labels);
230230
return as->label_offsets[label];
231231
}
@@ -308,7 +308,7 @@ void asm_thumb_ite_ge(asm_thumb_t *as) {
308308

309309
#define OP_B_N(byte_offset) (0xe000 | (((byte_offset) >> 1) & 0x07ff))
310310

311-
void asm_thumb_b_n(asm_thumb_t *as, int label) {
311+
void asm_thumb_b_n(asm_thumb_t *as, uint label) {
312312
int dest = get_label_dest(as, label);
313313
int rel = dest - as->code_offset;
314314
rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
@@ -321,7 +321,7 @@ void asm_thumb_b_n(asm_thumb_t *as, int label) {
321321

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

324-
void asm_thumb_bcc_n(asm_thumb_t *as, int cond, int label) {
324+
void asm_thumb_bcc_n(asm_thumb_t *as, int cond, uint label) {
325325
int dest = get_label_dest(as, label);
326326
int rel = dest - as->code_offset;
327327
rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
@@ -380,7 +380,7 @@ void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num)
380380
#define OP_BW_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
381381
#define OP_BW_LO(byte_offset) (0xb800 | (((byte_offset) >> 1) & 0x07ff))
382382

383-
void asm_thumb_b_label(asm_thumb_t *as, int label) {
383+
void asm_thumb_b_label(asm_thumb_t *as, uint label) {
384384
int dest = get_label_dest(as, label);
385385
int rel = dest - as->code_offset;
386386
rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
@@ -403,7 +403,7 @@ void asm_thumb_b_label(asm_thumb_t *as, int label) {
403403
#define OP_BCC_W_HI(cond, byte_offset) (0xf000 | ((cond) << 6) | (((byte_offset) >> 10) & 0x0400) | (((byte_offset) >> 14) & 0x003f))
404404
#define OP_BCC_W_LO(byte_offset) (0x8000 | ((byte_offset) & 0x2000) | (((byte_offset) >> 1) & 0x0fff))
405405

406-
void asm_thumb_bcc_label(asm_thumb_t *as, int cond, int label) {
406+
void asm_thumb_bcc_label(asm_thumb_t *as, int cond, uint label) {
407407
int dest = get_label_dest(as, label);
408408
int rel = dest - as->code_offset;
409409
rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction

py/asmthumb.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void *asm_thumb_get_code(asm_thumb_t *as);
5353
void asm_thumb_entry(asm_thumb_t *as, int num_locals);
5454
void asm_thumb_exit(asm_thumb_t *as);
5555

56-
void asm_thumb_label_assign(asm_thumb_t *as, int label);
56+
void asm_thumb_label_assign(asm_thumb_t *as, uint label);
5757

5858
// argument order follows ARM, in general dest is first
5959
// note there is a difference between movw and mov.w, and many others!
@@ -67,16 +67,16 @@ void asm_thumb_subs_rlo_rlo_i3(asm_thumb_t *as, uint rlo_dest, uint rlo_src, int
6767
void asm_thumb_cmp_reg_reg(asm_thumb_t *as, uint rlo_a, uint rlo_b);
6868
void asm_thumb_cmp_rlo_i8(asm_thumb_t *as, uint rlo, int i8);
6969
void asm_thumb_ite_ge(asm_thumb_t *as);
70-
void asm_thumb_b_n(asm_thumb_t *as, int label);
71-
void asm_thumb_bcc_n(asm_thumb_t *as, int cond, int label);
70+
void asm_thumb_b_n(asm_thumb_t *as, uint label);
71+
void asm_thumb_bcc_n(asm_thumb_t *as, int cond, uint label);
7272

7373
void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, machine_uint_t i32_src); // convenience
7474
void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32_src); // convenience
7575
void asm_thumb_mov_local_reg(asm_thumb_t *as, int local_num_dest, uint rlo_src); // convenience
7676
void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num); // convenience
7777
void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num); // convenience
7878

79-
void asm_thumb_b_label(asm_thumb_t *as, int label); // convenience ?
80-
void asm_thumb_bcc_label(asm_thumb_t *as, int cc, int label); // convenience: picks narrow or wide branch
79+
void asm_thumb_b_label(asm_thumb_t *as, uint label); // convenience ?
80+
void asm_thumb_bcc_label(asm_thumb_t *as, int cc, uint label); // convenience: picks narrow or wide branch
8181
void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp); // convenience ?
8282

py/compile.c

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ typedef struct _compiler_t {
4343
uint8_t had_error; // try to keep compiler clean from nlr
4444
uint8_t func_arg_is_super; // used to compile special case of super() function call
4545

46-
int next_label;
46+
uint next_label;
4747

48-
int break_label;
49-
int continue_label;
48+
uint break_label;
49+
uint continue_label;
5050
int break_continue_except_level;
5151
uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
5252

@@ -196,7 +196,7 @@ mp_parse_node_t fold_constants(mp_parse_node_t pn) {
196196
STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra);
197197
STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
198198

199-
STATIC int comp_next_label(compiler_t *comp) {
199+
STATIC uint comp_next_label(compiler_t *comp) {
200200
return comp->next_label++;
201201
}
202202

@@ -467,7 +467,7 @@ STATIC void cpython_c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if
467467
int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
468468
if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
469469
if (jump_if == false) {
470-
int label2 = comp_next_label(comp);
470+
uint label2 = comp_next_label(comp);
471471
for (int i = 0; i < n - 1; i++) {
472472
cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
473473
}
@@ -485,7 +485,7 @@ STATIC void cpython_c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if
485485
cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
486486
}
487487
} else {
488-
int label2 = comp_next_label(comp);
488+
uint label2 = comp_next_label(comp);
489489
for (int i = 0; i < n - 1; i++) {
490490
cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
491491
}
@@ -528,7 +528,7 @@ STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int la
528528
int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
529529
if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
530530
if (jump_if == false) {
531-
int label2 = comp_next_label(comp);
531+
uint label2 = comp_next_label(comp);
532532
for (int i = 0; i < n - 1; i++) {
533533
c_if_cond(comp, pns->nodes[i], true, label2);
534534
}
@@ -546,7 +546,7 @@ STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int la
546546
c_if_cond(comp, pns->nodes[i], false, label);
547547
}
548548
} else {
549-
int label2 = comp_next_label(comp);
549+
uint label2 = comp_next_label(comp);
550550
for (int i = 0; i < n - 1; i++) {
551551
c_if_cond(comp, pns->nodes[i], false, label2);
552552
}
@@ -1202,7 +1202,7 @@ void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
12021202
mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
12031203
mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns_test_if_expr->nodes[1];
12041204

1205-
int l_fail = comp_next_label(comp);
1205+
uint l_fail = comp_next_label(comp);
12061206
c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
12071207
compile_node(comp, pns_test_if_expr->nodes[0]); // success value
12081208
EMIT(return_value);
@@ -1451,7 +1451,7 @@ void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
14511451
}
14521452

14531453
void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1454-
int l_end = comp_next_label(comp);
1454+
uint l_end = comp_next_label(comp);
14551455
c_if_cond(comp, pns->nodes[0], true, l_end);
14561456
EMIT_ARG(load_global, MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
14571457
if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
@@ -1466,9 +1466,9 @@ void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
14661466
void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
14671467
// TODO proper and/or short circuiting
14681468

1469-
int l_end = comp_next_label(comp);
1469+
uint l_end = comp_next_label(comp);
14701470

1471-
int l_fail = comp_next_label(comp);
1471+
uint l_fail = comp_next_label(comp);
14721472
c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
14731473

14741474
compile_node(comp, pns->nodes[1]); // if block
@@ -1529,10 +1529,10 @@ void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
15291529
}
15301530

15311531
#define START_BREAK_CONTINUE_BLOCK \
1532-
int old_break_label = comp->break_label; \
1533-
int old_continue_label = comp->continue_label; \
1534-
int break_label = comp_next_label(comp); \
1535-
int continue_label = comp_next_label(comp); \
1532+
uint old_break_label = comp->break_label; \
1533+
uint old_continue_label = comp->continue_label; \
1534+
uint break_label = comp_next_label(comp); \
1535+
uint continue_label = comp_next_label(comp); \
15361536
comp->break_label = break_label; \
15371537
comp->continue_label = continue_label; \
15381538
comp->break_continue_except_level = comp->cur_except_level;
@@ -1547,7 +1547,7 @@ void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
15471547

15481548
// compared to CPython, we have an optimised version of while loops
15491549
#if MICROPY_EMIT_CPYTHON
1550-
int done_label = comp_next_label(comp);
1550+
uint done_label = comp_next_label(comp);
15511551
EMIT_ARG(setup_loop, break_label);
15521552
EMIT_ARG(label_assign, continue_label);
15531553
c_if_cond(comp, pns->nodes[0], false, done_label); // condition
@@ -1562,7 +1562,7 @@ void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
15621562
EMIT(pop_block);
15631563
}
15641564
#else
1565-
int top_label = comp_next_label(comp);
1565+
uint top_label = comp_next_label(comp);
15661566
EMIT_ARG(jump, continue_label);
15671567
EMIT_ARG(label_assign, top_label);
15681568
compile_node(comp, pns->nodes[1]); // body
@@ -1584,8 +1584,8 @@ void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
15841584
void compile_for_stmt_optimised_range(compiler_t *comp, mp_parse_node_t pn_var, mp_parse_node_t pn_start, mp_parse_node_t pn_end, mp_parse_node_t pn_step, mp_parse_node_t pn_body, mp_parse_node_t pn_else) {
15851585
START_BREAK_CONTINUE_BLOCK
15861586

1587-
int top_label = comp_next_label(comp);
1588-
int entry_label = comp_next_label(comp);
1587+
uint top_label = comp_next_label(comp);
1588+
uint entry_label = comp_next_label(comp);
15891589

15901590
// compile: start, duplicated on stack
15911591
compile_node(comp, pn_start);
@@ -1682,8 +1682,8 @@ void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
16821682

16831683
START_BREAK_CONTINUE_BLOCK
16841684

1685-
int pop_label = comp_next_label(comp);
1686-
int end_label = comp_next_label(comp);
1685+
uint pop_label = comp_next_label(comp);
1686+
uint end_label = comp_next_label(comp);
16871687

16881688
// I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
16891689
#if MICROPY_EMIT_CPYTHON
@@ -1721,8 +1721,8 @@ void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_except,
17211721

17221722
// setup code
17231723
int stack_size = EMIT(get_stack_size);
1724-
int l1 = comp_next_label(comp);
1725-
int success_label = comp_next_label(comp);
1724+
uint l1 = comp_next_label(comp);
1725+
uint success_label = comp_next_label(comp);
17261726

17271727
EMIT_ARG(setup_except, l1);
17281728
compile_increase_except_level(comp);
@@ -1731,14 +1731,14 @@ void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_except,
17311731
EMIT(pop_block);
17321732
EMIT_ARG(jump, success_label);
17331733
EMIT_ARG(label_assign, l1);
1734-
int l2 = comp_next_label(comp);
1734+
uint l2 = comp_next_label(comp);
17351735

17361736
for (int i = 0; i < n_except; i++) {
17371737
assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
17381738
mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
17391739

17401740
qstr qstr_exception_local = 0;
1741-
int end_finally_label = comp_next_label(comp);
1741+
uint end_finally_label = comp_next_label(comp);
17421742

17431743
if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
17441744
// this is a catch all exception handler
@@ -1773,7 +1773,7 @@ void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_except,
17731773

17741774
EMIT(pop_top);
17751775

1776-
int l3 = 0;
1776+
uint l3 = 0;
17771777
if (qstr_exception_local != 0) {
17781778
l3 = comp_next_label(comp);
17791779
EMIT_ARG(setup_finally, l3);
@@ -1810,7 +1810,7 @@ void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_except,
18101810
void compile_try_finally(compiler_t *comp, mp_parse_node_t pn_body, int n_except, mp_parse_node_t *pn_except, mp_parse_node_t pn_else, mp_parse_node_t pn_finally) {
18111811
// don't understand how the stack works with exceptions, so we force it to return to the correct value
18121812
int stack_size = EMIT(get_stack_size);
1813-
int l_finally_block = comp_next_label(comp);
1813+
uint l_finally_block = comp_next_label(comp);
18141814

18151815
EMIT_ARG(setup_finally, l_finally_block);
18161816
compile_increase_except_level(comp);
@@ -1866,7 +1866,7 @@ void compile_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *nodes, m
18661866
// no more pre-bits, compile the body of the with
18671867
compile_node(comp, body);
18681868
} else {
1869-
int l_end = comp_next_label(comp);
1869+
uint l_end = comp_next_label(comp);
18701870
if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
18711871
// this pre-bit is of the form "a as b"
18721872
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
@@ -2024,8 +2024,8 @@ void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
20242024
mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
20252025

20262026
int stack_size = EMIT(get_stack_size);
2027-
int l_fail = comp_next_label(comp);
2028-
int l_end = comp_next_label(comp);
2027+
uint l_fail = comp_next_label(comp);
2028+
uint l_end = comp_next_label(comp);
20292029
c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
20302030
compile_node(comp, pns->nodes[0]); // success value
20312031
EMIT_ARG(jump, l_end);
@@ -2055,7 +2055,7 @@ void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
20552055
}
20562056

20572057
void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
2058-
int l_end = comp_next_label(comp);
2058+
uint l_end = comp_next_label(comp);
20592059
int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
20602060
for (int i = 0; i < n; i += 1) {
20612061
compile_node(comp, pns->nodes[i]);
@@ -2067,7 +2067,7 @@ void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
20672067
}
20682068

20692069
void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
2070-
int l_end = comp_next_label(comp);
2070+
uint l_end = comp_next_label(comp);
20712071
int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
20722072
for (int i = 0; i < n; i += 1) {
20732073
compile_node(comp, pns->nodes[i]);
@@ -2088,7 +2088,7 @@ void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
20882088
int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
20892089
compile_node(comp, pns->nodes[0]);
20902090
bool multi = (num_nodes > 3);
2091-
int l_fail = 0;
2091+
uint l_fail = 0;
20922092
if (multi) {
20932093
l_fail = comp_next_label(comp);
20942094
}
@@ -2135,7 +2135,7 @@ void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
21352135
}
21362136
}
21372137
if (multi) {
2138-
int l_end = comp_next_label(comp);
2138+
uint l_end = comp_next_label(comp);
21392139
EMIT_ARG(jump, l_end);
21402140
EMIT_ARG(label_assign, l_fail);
21412141
EMIT(rot_two);
@@ -2835,8 +2835,8 @@ void compile_scope_comp_iter(compiler_t *comp, mp_parse_node_t pn_iter, mp_parse
28352835
// for loop
28362836
mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
28372837
compile_node(comp, pns_comp_for2->nodes[1]);
2838-
int l_end2 = comp_next_label(comp);
2839-
int l_top2 = comp_next_label(comp);
2838+
uint l_end2 = comp_next_label(comp);
2839+
uint l_top2 = comp_next_label(comp);
28402840
EMIT(get_iter);
28412841
EMIT_ARG(label_assign, l_top2);
28422842
EMIT_ARG(for_iter, l_end2);
@@ -2982,8 +2982,8 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
29822982
EMIT_ARG(build_set, 0);
29832983
}
29842984

2985-
int l_end = comp_next_label(comp);
2986-
int l_top = comp_next_label(comp);
2985+
uint l_end = comp_next_label(comp);
2986+
uint l_top = comp_next_label(comp);
29872987
EMIT_ARG(load_id, qstr_arg);
29882988
EMIT_ARG(label_assign, l_top);
29892989
EMIT_ARG(for_iter, l_end);
@@ -3102,7 +3102,7 @@ void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass
31023102
compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
31033103
return;
31043104
}
3105-
int lab = comp_next_label(comp);
3105+
uint lab = comp_next_label(comp);
31063106
if (pass > PASS_1) {
31073107
EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
31083108
}

0 commit comments

Comments
 (0)