Browse Source

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.
pull/1018/merge
Damien George 10 years ago
parent
commit
83204f3406
  1. 2
      py/compile.c
  2. 2
      py/emit.h
  3. 4
      py/emitbc.c
  4. 4
      py/emitnative.c
  5. 2
      py/emitpass1.c
  6. 4
      py/grammar.h
  7. 2
      py/vmentrytable.h

2
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

2
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);

4
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,

4
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,

2
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,

4
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)* [',']

2
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,

Loading…
Cancel
Save