Skip to content

Commit d2d64f0

Browse files
committed
py: Add "default" to switches to allow better code flow analysis.
This helps compiler produce smaller code. Saves 124 bytes on stmhal and bare-arm.
1 parent 65ef6b7 commit d2d64f0

File tree

7 files changed

+19
-26
lines changed

7 files changed

+19
-26
lines changed

py/compile.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,7 @@ STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_
860860
EMIT_ARG(store_id, arg);
861861
break;
862862
case ASSIGN_AUG_LOAD:
863+
default:
863864
EMIT_ARG(load_id, arg);
864865
break;
865866
}
@@ -2185,8 +2186,7 @@ STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
21852186
case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
21862187
case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
21872188
case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
2188-
case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
2189-
default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
2189+
case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break;
21902190
}
21912191
EMIT_ARG(binary_op, op);
21922192
c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
@@ -2350,8 +2350,7 @@ STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
23502350
case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
23512351
case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
23522352
case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2353-
case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2354-
default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
2353+
case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break;
23552354
}
23562355
EMIT_ARG(binary_op, op);
23572356
} else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
@@ -2982,7 +2981,7 @@ STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
29822981
case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
29832982
case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
29842983
case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
2985-
case MP_PARSE_NODE_TOKEN:
2984+
case MP_PARSE_NODE_TOKEN: default:
29862985
if (arg == MP_TOKEN_NEWLINE) {
29872986
// this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
29882987
// or when single_input lets through a NEWLINE (user enters a blank line)
@@ -2991,7 +2990,6 @@ STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
29912990
EMIT_ARG(load_const_tok, arg);
29922991
}
29932992
break;
2994-
default: assert(0);
29952993
}
29962994
} else {
29972995
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;

py/emitbc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,9 @@ STATIC void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
454454
case MP_TOKEN_KW_FALSE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
455455
case MP_TOKEN_KW_NONE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_NONE); break;
456456
case MP_TOKEN_KW_TRUE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
457+
no_other_choice:
457458
case MP_TOKEN_ELLIPSIS: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_ELLIPSIS); break;
458-
default: assert(0);
459+
default: assert(0); goto no_other_choice; // to help flow control analysis
459460
}
460461
}
461462

py/emitglue.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ mp_obj_t mp_make_function_from_raw_code(mp_raw_code_t *rc, mp_obj_t def_args, mp
110110
mp_obj_t fun;
111111
switch (rc->kind) {
112112
case MP_CODE_BYTECODE:
113+
no_other_choice:
113114
fun = mp_obj_new_fun_bc(rc->scope_flags, rc->n_pos_args, rc->n_kwonly_args, def_args, def_kw_args, rc->u_byte.code);
114115
break;
115116
#if MICROPY_EMIT_NATIVE
@@ -128,7 +129,7 @@ mp_obj_t mp_make_function_from_raw_code(mp_raw_code_t *rc, mp_obj_t def_args, mp
128129
default:
129130
// raw code was never set (this should not happen)
130131
assert(0);
131-
return mp_const_none;
132+
goto no_other_choice; // to help flow control analysis
132133
}
133134

134135
// check for generator functions and if so wrap in generator object

py/objdict.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,17 +415,14 @@ STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) {
415415
} else {
416416
switch (self->kind) {
417417
case MP_DICT_VIEW_ITEMS:
418-
{
418+
default: {
419419
mp_obj_t items[] = {next->key, next->value};
420420
return mp_obj_new_tuple(2, items);
421421
}
422422
case MP_DICT_VIEW_KEYS:
423423
return next->key;
424424
case MP_DICT_VIEW_VALUES:
425425
return next->value;
426-
default:
427-
assert(0); /* can't happen */
428-
return mp_const_none;
429426
}
430427
}
431428
}

py/objfun.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,8 @@ STATIC mp_obj_t fun_builtin_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n
8383
return ((mp_fun_2_t)self->fun)(args[0], args[1]);
8484

8585
case 3:
86-
return ((mp_fun_3_t)self->fun)(args[0], args[1], args[2]);
87-
8886
default:
89-
assert(0);
90-
return mp_const_none;
87+
return ((mp_fun_3_t)self->fun)(args[0], args[1], args[2]);
9188
}
9289

9390
} else {

py/objgenerator.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_
120120

121121
switch (ret_kind) {
122122
case MP_VM_RETURN_NORMAL:
123+
default:
123124
// Explicitly mark generator as completed. If we don't do this,
124125
// subsequent next() may re-execute statements after last yield
125126
// again and again, leading to side effects.
@@ -137,11 +138,6 @@ mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_
137138
self->code_state.ip = 0;
138139
*ret_val = self->code_state.state[self->code_state.n_state - 1];
139140
break;
140-
141-
default:
142-
assert(0);
143-
*ret_val = mp_const_none;
144-
break;
145141
}
146142

147143
return ret_kind;
@@ -151,6 +147,7 @@ STATIC mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_o
151147
mp_obj_t ret;
152148
switch (mp_obj_gen_resume(self_in, send_value, throw_value, &ret)) {
153149
case MP_VM_RETURN_NORMAL:
150+
default:
154151
// Optimize return w/o value in case generator is used in for loop
155152
if (ret == mp_const_none || ret == MP_OBJ_STOP_ITERATION) {
156153
return MP_OBJ_STOP_ITERATION;
@@ -173,10 +170,6 @@ STATIC mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_o
173170
} else {
174171
nlr_raise(ret);
175172
}
176-
177-
default:
178-
assert(0);
179-
return mp_const_none;
180173
}
181174
}
182175

py/parse.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,11 +456,13 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p
456456
}
457457
break;
458458
case RULE_ARG_RULE:
459+
rule_or_no_other_choice:
459460
push_rule(&parser, rule_src_line, rule, i + 1); // save this or-rule
460461
push_rule_from_arg(&parser, rule->arg[i]); // push child of or-rule
461462
goto next_rule;
462463
default:
463464
assert(0);
465+
goto rule_or_no_other_choice; // to help flow control analysis
464466
}
465467
}
466468
if ((rule->arg[i] & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
@@ -520,14 +522,16 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p
520522
}
521523
}
522524
break;
523-
}
525+
}
524526
case RULE_ARG_RULE:
525527
case RULE_ARG_OPT_RULE:
528+
rule_and_no_other_choice:
526529
push_rule(&parser, rule_src_line, rule, i + 1); // save this and-rule
527530
push_rule_from_arg(&parser, rule->arg[i]); // push child of and-rule
528531
goto next_rule;
529532
default:
530533
assert(0);
534+
goto rule_and_no_other_choice; // to help flow control analysis
531535
}
532536
}
533537

@@ -674,11 +678,13 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p
674678
}
675679
break;
676680
case RULE_ARG_RULE:
681+
rule_list_no_other_choice:
677682
push_rule(&parser, rule_src_line, rule, i + 1); // save this list-rule
678683
push_rule_from_arg(&parser, arg); // push child of list-rule
679684
goto next_rule;
680685
default:
681686
assert(0);
687+
goto rule_list_no_other_choice; // to help flow control analysis
682688
}
683689
}
684690
}

0 commit comments

Comments
 (0)