Skip to content

Commit 0bb9713

Browse files
committed
py: Transform assert logic in compiler to save code space.
Saves about 250 code bytes for Thumb2 archs.
1 parent 4d77e1a commit 0bb9713

1 file changed

Lines changed: 40 additions & 82 deletions

File tree

py/compile.c

Lines changed: 40 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -221,17 +221,15 @@ STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_m
221221
if (!(arg1 >= (mp_int_t)BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
222222
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
223223
}
224-
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
224+
} else {
225+
assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)); // should be
225226
// int >> int
226227
if (arg1 >= (mp_int_t)BITS_PER_WORD) {
227228
// Shifting to big amounts is underfined behavior
228229
// in C and is CPU-dependent; propagate sign bit.
229230
arg1 = BITS_PER_WORD - 1;
230231
}
231232
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
232-
} else {
233-
// shouldn't happen
234-
assert(0);
235233
}
236234
}
237235
break;
@@ -244,12 +242,10 @@ STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_m
244242
if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
245243
// int + int
246244
arg0 += arg1;
247-
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
245+
} else {
246+
assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)); // should be
248247
// int - int
249248
arg0 -= arg1;
250-
} else {
251-
// shouldn't happen
252-
assert(0);
253249
}
254250
if (MP_SMALL_INT_FITS(arg0)) {
255251
//printf("%ld + %ld\n", arg0, arg1);
@@ -276,14 +272,12 @@ STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_m
276272
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
277273
// int%int
278274
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
279-
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
275+
} else {
276+
assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)); // should be
280277
if (arg1 != 0) {
281278
// int // int
282279
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_floor_divide(arg0, arg1));
283280
}
284-
} else {
285-
// shouldn't happen
286-
assert(0);
287281
}
288282
}
289283
break;
@@ -297,12 +291,10 @@ STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_m
297291
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
298292
// -int
299293
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
300-
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
294+
} else {
295+
assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
301296
// ~int
302297
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
303-
} else {
304-
// shouldn't happen
305-
assert(0);
306298
}
307299
}
308300
break;
@@ -1015,18 +1007,15 @@ STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
10151007
pn_colon = MP_PARSE_NODE_NULL;
10161008
pn_equal = MP_PARSE_NODE_NULL;
10171009

1018-
} else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
1010+
} else {
10191011
// this parameter has a colon and/or equal specifier
10201012

1013+
assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)); // should be
1014+
10211015
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
10221016
pn_id = pns->nodes[0];
10231017
pn_colon = pns->nodes[1];
10241018
pn_equal = pns->nodes[2];
1025-
1026-
} else {
1027-
// XXX what to do here?
1028-
assert(0);
1029-
return;
10301019
}
10311020

10321021
if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
@@ -1238,11 +1227,9 @@ STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
12381227
qstr body_name = 0;
12391228
if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
12401229
body_name = compile_funcdef_helper(comp, pns_body, emit_options);
1241-
} else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
1242-
body_name = compile_classdef_helper(comp, pns_body, emit_options);
12431230
} else {
1244-
// shouldn't happen
1245-
assert(0);
1231+
assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
1232+
body_name = compile_classdef_helper(comp, pns_body, emit_options);
12461233
}
12471234

12481235
// call each decorator
@@ -1434,9 +1421,10 @@ STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
14341421
*q_base = q_full;
14351422
}
14361423
EMIT_ARG(import_name, q_full);
1437-
} else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1424+
} else {
1425+
assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
14381426
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1439-
if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
1427+
{
14401428
// a name of the form a.b.c
14411429
if (!is_as) {
14421430
*q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
@@ -1464,13 +1452,7 @@ STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
14641452
EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
14651453
}
14661454
}
1467-
} else {
1468-
// shouldn't happen
1469-
assert(0);
14701455
}
1471-
} else {
1472-
// shouldn't happen
1473-
assert(0);
14741456
}
14751457
}
14761458

@@ -2067,7 +2049,8 @@ STATIC void compile_try_finally(compiler_t *comp, mp_parse_node_t pn_body, int n
20672049
}
20682050

20692051
STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
2070-
if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2052+
assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be
2053+
{
20712054
mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
20722055
if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
20732056
// just try-finally
@@ -2089,9 +2072,6 @@ STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
20892072
int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
20902073
compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
20912074
}
2092-
} else {
2093-
// shouldn't happen
2094-
assert(0);
20952075
}
20962076
}
20972077

@@ -2195,7 +2175,8 @@ STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
21952175
}
21962176
c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
21972177
}
2198-
} else if (kind == PN_expr_stmt_assign) {
2178+
} else {
2179+
assert(kind == PN_expr_stmt_assign); // should be
21992180
if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
22002181
&& MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
22012182
&& MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
@@ -2239,9 +2220,6 @@ STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
22392220
compile_node(comp, pns1->nodes[0]); // rhs
22402221
c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
22412222
}
2242-
} else {
2243-
// shouldn't happen
2244-
assert(0);
22452223
}
22462224
}
22472225
}
@@ -2344,24 +2322,20 @@ STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
23442322
case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
23452323
}
23462324
EMIT_ARG(binary_op, op);
2347-
} else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2325+
} else {
2326+
assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be
23482327
mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
23492328
int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
23502329
if (kind == PN_comp_op_not_in) {
23512330
EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
2352-
} else if (kind == PN_comp_op_is) {
2331+
} else {
2332+
assert(kind == PN_comp_op_is); // should be
23532333
if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
23542334
EMIT_ARG(binary_op, MP_BINARY_OP_IS);
23552335
} else {
23562336
EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
23572337
}
2358-
} else {
2359-
// shouldn't happen
2360-
assert(0);
23612338
}
2362-
} else {
2363-
// shouldn't happen
2364-
assert(0);
23652339
}
23662340
if (i + 2 < num_nodes) {
23672341
EMIT_ARG(jump_if_false_or_pop, l_fail);
@@ -2401,11 +2375,9 @@ STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
24012375
compile_node(comp, pns->nodes[i + 1]);
24022376
if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
24032377
EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
2404-
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
2405-
EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
24062378
} else {
2407-
// shouldn't happen
2408-
assert(0);
2379+
assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
2380+
EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
24092381
}
24102382
}
24112383
}
@@ -2417,11 +2389,9 @@ STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
24172389
compile_node(comp, pns->nodes[i + 1]);
24182390
if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
24192391
EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
2420-
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
2421-
EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
24222392
} else {
2423-
// shouldn't happen
2424-
assert(0);
2393+
assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
2394+
EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
24252395
}
24262396
}
24272397
}
@@ -2437,11 +2407,9 @@ STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
24372407
EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
24382408
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
24392409
EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
2440-
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
2441-
EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
24422410
} else {
2443-
// shouldn't happen
2444-
assert(0);
2411+
assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
2412+
EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
24452413
}
24462414
}
24472415
}
@@ -2452,11 +2420,9 @@ STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
24522420
EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
24532421
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
24542422
EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
2455-
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
2456-
EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
24572423
} else {
2458-
// shouldn't happen
2459-
assert(0);
2424+
assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
2425+
EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
24602426
}
24612427
}
24622428

@@ -2793,7 +2759,8 @@ STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
27932759
EMIT_ARG(build_set, 1 + n);
27942760
}
27952761
#endif
2796-
} else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
2762+
} else {
2763+
assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be
27972764
// dict/set comprehension
27982765
if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
27992766
// a dictionary comprehension
@@ -2802,9 +2769,6 @@ STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
28022769
// a set comprehension
28032770
compile_comprehension(comp, pns, SCOPE_SET_COMP);
28042771
}
2805-
} else {
2806-
// shouldn't happen
2807-
assert(0);
28082772
}
28092773
} else {
28102774
// set with one element
@@ -3038,22 +3002,18 @@ STATIC void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn
30383002
// named star
30393003
comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
30403004
param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3041-
} else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
3005+
} else {
3006+
assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be
30423007
// named star with possible annotation
30433008
comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
30443009
pns = (mp_parse_node_struct_t*)pns->nodes[0];
30453010
param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
3046-
} else {
3047-
// shouldn't happen
3048-
assert(0);
30493011
}
3050-
} else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
3012+
} else {
3013+
assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be
30513014
param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
30523015
param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
30533016
comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
3054-
} else {
3055-
// TODO anything to implement?
3056-
assert(0);
30573017
}
30583018
}
30593019

@@ -3147,7 +3107,8 @@ STATIC void compile_scope_comp_iter(compiler_t *comp, mp_parse_node_t pn_iter, m
31473107
c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
31483108
pn_iter = pns_comp_if->nodes[1];
31493109
goto tail_recursion;
3150-
} else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
3110+
} else {
3111+
assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be
31513112
// for loop
31523113
mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
31533114
compile_node(comp, pns_comp_for2->nodes[1]);
@@ -3161,9 +3122,6 @@ STATIC void compile_scope_comp_iter(compiler_t *comp, mp_parse_node_t pn_iter, m
31613122
EMIT_ARG(jump, l_top2);
31623123
EMIT_ARG(label_assign, l_end2);
31633124
EMIT(for_iter_end);
3164-
} else {
3165-
// shouldn't happen
3166-
assert(0);
31673125
}
31683126
}
31693127

0 commit comments

Comments
 (0)