From 83204f34067b765293b791563bdd5756bdfc5aa4 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 27 Dec 2014 17:20:41 +0000 Subject: [PATCH] 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. --- py/compile.c | 2 ++ py/emit.h | 2 ++ py/emitbc.c | 4 ++++ py/emitnative.c | 4 ++++ py/emitpass1.c | 2 ++ py/grammar.h | 4 ++++ py/vmentrytable.h | 2 ++ 7 files changed, 20 insertions(+) diff --git a/py/compile.c b/py/compile.c index 25bf64e3b9..66e2996c44 100644 --- a/py/compile.c +++ b/py/compile.c @@ -2842,6 +2842,7 @@ STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get } +#if MICROPY_PY_BUILTINS_SLICE STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) { assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be 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) { EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); compile_subscript_3_helper(comp, pns); } +#endif // MICROPY_PY_BUILTINS_SLICE STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) { // if this is called then we are compiling a dict key:value pair diff --git a/py/emit.h b/py/emit.h index bc72b3cb3b..927e33ea73 100644 --- a/py/emit.h +++ b/py/emit.h @@ -132,7 +132,9 @@ typedef struct _emit_method_table_t { void (*build_set)(emit_t *emit, mp_uint_t n_args); void (*set_add)(emit_t *emit, mp_uint_t set_stack_index); #endif + #if MICROPY_PY_BUILTINS_SLICE void (*build_slice)(emit_t *emit, mp_uint_t n_args); + #endif void (*unpack_sequence)(emit_t *emit, mp_uint_t n_args); void (*unpack_ex)(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right); void (*make_function)(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults); diff --git a/py/emitbc.c b/py/emitbc.c index 723d5eda25..f55a16634a 100644 --- a/py/emitbc.c +++ b/py/emitbc.c @@ -794,10 +794,12 @@ STATIC void emit_bc_set_add(emit_t *emit, mp_uint_t set_stack_index) { } #endif +#if MICROPY_PY_BUILTINS_SLICE STATIC void emit_bc_build_slice(emit_t *emit, mp_uint_t n_args) { emit_bc_pre(emit, 1 - n_args); emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SLICE, n_args); } +#endif STATIC void emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) { emit_bc_pre(emit, -1 + n_args); @@ -966,7 +968,9 @@ const emit_method_table_t emit_bc_method_table = { emit_bc_build_set, emit_bc_set_add, #endif + #if MICROPY_PY_BUILTINS_SLICE emit_bc_build_slice, + #endif emit_bc_unpack_sequence, emit_bc_unpack_ex, emit_bc_make_function, diff --git a/py/emitnative.c b/py/emitnative.c index 9f5733425c..c2fe951be8 100644 --- a/py/emitnative.c +++ b/py/emitnative.c @@ -2084,6 +2084,7 @@ STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) { } #endif +#if MICROPY_PY_BUILTINS_SLICE STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) { DEBUG_printf("build_slice %d\n", n_args); if (n_args == 2) { @@ -2104,6 +2105,7 @@ STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) { emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); } } +#endif STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) { DEBUG_printf("unpack_sequence %d\n", n_args); @@ -2336,7 +2338,9 @@ const emit_method_table_t EXPORT_FUN(method_table) = { emit_native_build_set, emit_native_set_add, #endif + #if MICROPY_PY_BUILTINS_SLICE emit_native_build_slice, + #endif emit_native_unpack_sequence, emit_native_unpack_ex, emit_native_make_function, diff --git a/py/emitpass1.c b/py/emitpass1.c index fea3e7ff07..9e471ef59f 100644 --- a/py/emitpass1.c +++ b/py/emitpass1.c @@ -205,7 +205,9 @@ const emit_method_table_t emit_pass1_method_table = { (void*)emit_pass1_dummy, (void*)emit_pass1_dummy, #endif + #if MICROPY_PY_BUILTINS_SLICE (void*)emit_pass1_dummy, + #endif (void*)emit_pass1_dummy, (void*)emit_pass1_dummy, (void*)emit_pass1_dummy, diff --git a/py/grammar.h b/py/grammar.h index 1df0e938ec..eb2f03c669 100644 --- a/py/grammar.h +++ b/py/grammar.h @@ -269,6 +269,7 @@ DEF_RULE(trailer_period, c(trailer_period), and(2), tok(DEL_PERIOD), tok(NAME)) // subscript: test | [test] ':' [test] [sliceop] // sliceop: ':' [test] +#if MICROPY_PY_BUILTINS_SLICE DEF_RULE(subscriptlist, c(generic_tuple), list_with_end, rule(subscript), tok(DEL_COMMA)) DEF_RULE(subscript, nc, or(2), rule(subscript_3), rule(subscript_2)) 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)) DEF_RULE(subscript_3c, nc, and(2), tok(DEL_COLON), opt_rule(test)) DEF_RULE(subscript_3d, nc, and(2), rule(test), opt_rule(sliceop)) DEF_RULE(sliceop, nc, and(2), tok(DEL_COLON), opt_rule(test)) +#else +DEF_RULE(subscriptlist, c(generic_tuple), list_with_end, rule(test), tok(DEL_COMMA)) +#endif // exprlist: (expr|star_expr) (',' (expr|star_expr))* [','] // testlist: test (',' test)* [','] diff --git a/py/vmentrytable.h b/py/vmentrytable.h index 71d6f5b172..d3de975882 100644 --- a/py/vmentrytable.h +++ b/py/vmentrytable.h @@ -90,7 +90,9 @@ static void* entry_table[256] = { [MP_BC_BUILD_SET] = &&entry_MP_BC_BUILD_SET, [MP_BC_SET_ADD] = &&entry_MP_BC_SET_ADD, #endif + #if MICROPY_PY_BUILTINS_SLICE [MP_BC_BUILD_SLICE] = &&entry_MP_BC_BUILD_SLICE, + #endif [MP_BC_UNPACK_SEQUENCE] = &&entry_MP_BC_UNPACK_SEQUENCE, [MP_BC_UNPACK_EX] = &&entry_MP_BC_UNPACK_EX, [MP_BC_MAKE_FUNCTION] = &&entry_MP_BC_MAKE_FUNCTION,