Skip to content

Commit 83204f3

Browse files
committed
py: Allow to properly disable builtin slice operation.
This patch makes the MICROPY_PY_BUILTINS_SLICE compile-time option fully disable the builtin slice operation (when set to 0). This includes removing the slice sytanx from the grammar. Now, enabling slice costs 4228 bytes on unix x64, and 1816 bytes on stmhal.
1 parent e37dcaa commit 83204f3

7 files changed

Lines changed: 20 additions & 0 deletions

File tree

py/compile.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2842,6 +2842,7 @@ STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns
28422842
EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
28432843
}
28442844

2845+
#if MICROPY_PY_BUILTINS_SLICE
28452846
STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
28462847
assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
28472848
mp_parse_node_t pn = pns->nodes[0];
@@ -2897,6 +2898,7 @@ STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
28972898
EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
28982899
compile_subscript_3_helper(comp, pns);
28992900
}
2901+
#endif // MICROPY_PY_BUILTINS_SLICE
29002902

29012903
STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
29022904
// if this is called then we are compiling a dict key:value pair

py/emit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ typedef struct _emit_method_table_t {
132132
void (*build_set)(emit_t *emit, mp_uint_t n_args);
133133
void (*set_add)(emit_t *emit, mp_uint_t set_stack_index);
134134
#endif
135+
#if MICROPY_PY_BUILTINS_SLICE
135136
void (*build_slice)(emit_t *emit, mp_uint_t n_args);
137+
#endif
136138
void (*unpack_sequence)(emit_t *emit, mp_uint_t n_args);
137139
void (*unpack_ex)(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right);
138140
void (*make_function)(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults);

py/emitbc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,10 +794,12 @@ STATIC void emit_bc_set_add(emit_t *emit, mp_uint_t set_stack_index) {
794794
}
795795
#endif
796796

797+
#if MICROPY_PY_BUILTINS_SLICE
797798
STATIC void emit_bc_build_slice(emit_t *emit, mp_uint_t n_args) {
798799
emit_bc_pre(emit, 1 - n_args);
799800
emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SLICE, n_args);
800801
}
802+
#endif
801803

802804
STATIC void emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
803805
emit_bc_pre(emit, -1 + n_args);
@@ -966,7 +968,9 @@ const emit_method_table_t emit_bc_method_table = {
966968
emit_bc_build_set,
967969
emit_bc_set_add,
968970
#endif
971+
#if MICROPY_PY_BUILTINS_SLICE
969972
emit_bc_build_slice,
973+
#endif
970974
emit_bc_unpack_sequence,
971975
emit_bc_unpack_ex,
972976
emit_bc_make_function,

py/emitnative.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,6 +2084,7 @@ STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
20842084
}
20852085
#endif
20862086

2087+
#if MICROPY_PY_BUILTINS_SLICE
20872088
STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
20882089
DEBUG_printf("build_slice %d\n", n_args);
20892090
if (n_args == 2) {
@@ -2104,6 +2105,7 @@ STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
21042105
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
21052106
}
21062107
}
2108+
#endif
21072109

21082110
STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
21092111
DEBUG_printf("unpack_sequence %d\n", n_args);
@@ -2336,7 +2338,9 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
23362338
emit_native_build_set,
23372339
emit_native_set_add,
23382340
#endif
2341+
#if MICROPY_PY_BUILTINS_SLICE
23392342
emit_native_build_slice,
2343+
#endif
23402344
emit_native_unpack_sequence,
23412345
emit_native_unpack_ex,
23422346
emit_native_make_function,

py/emitpass1.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,9 @@ const emit_method_table_t emit_pass1_method_table = {
205205
(void*)emit_pass1_dummy,
206206
(void*)emit_pass1_dummy,
207207
#endif
208+
#if MICROPY_PY_BUILTINS_SLICE
208209
(void*)emit_pass1_dummy,
210+
#endif
209211
(void*)emit_pass1_dummy,
210212
(void*)emit_pass1_dummy,
211213
(void*)emit_pass1_dummy,

py/grammar.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ DEF_RULE(trailer_period, c(trailer_period), and(2), tok(DEL_PERIOD), tok(NAME))
269269
// subscript: test | [test] ':' [test] [sliceop]
270270
// sliceop: ':' [test]
271271

272+
#if MICROPY_PY_BUILTINS_SLICE
272273
DEF_RULE(subscriptlist, c(generic_tuple), list_with_end, rule(subscript), tok(DEL_COMMA))
273274
DEF_RULE(subscript, nc, or(2), rule(subscript_3), rule(subscript_2))
274275
DEF_RULE(subscript_2, c(subscript_2), and(2), rule(test), opt_rule(subscript_3))
@@ -277,6 +278,9 @@ DEF_RULE(subscript_3b, nc, or(2), rule(subscript_3c), rule(subscript_3d))
277278
DEF_RULE(subscript_3c, nc, and(2), tok(DEL_COLON), opt_rule(test))
278279
DEF_RULE(subscript_3d, nc, and(2), rule(test), opt_rule(sliceop))
279280
DEF_RULE(sliceop, nc, and(2), tok(DEL_COLON), opt_rule(test))
281+
#else
282+
DEF_RULE(subscriptlist, c(generic_tuple), list_with_end, rule(test), tok(DEL_COMMA))
283+
#endif
280284

281285
// exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
282286
// testlist: test (',' test)* [',']

py/vmentrytable.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ static void* entry_table[256] = {
9090
[MP_BC_BUILD_SET] = &&entry_MP_BC_BUILD_SET,
9191
[MP_BC_SET_ADD] = &&entry_MP_BC_SET_ADD,
9292
#endif
93+
#if MICROPY_PY_BUILTINS_SLICE
9394
[MP_BC_BUILD_SLICE] = &&entry_MP_BC_BUILD_SLICE,
95+
#endif
9496
[MP_BC_UNPACK_SEQUENCE] = &&entry_MP_BC_UNPACK_SEQUENCE,
9597
[MP_BC_UNPACK_EX] = &&entry_MP_BC_UNPACK_EX,
9698
[MP_BC_MAKE_FUNCTION] = &&entry_MP_BC_MAKE_FUNCTION,

0 commit comments

Comments
 (0)