Commit 83204f34 authored by Damien George's avatar Damien George
Browse files

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.
parent e37dcaaf
......@@ -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
......
......@@ -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);
......
......@@ -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,
......
......@@ -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,
......
......@@ -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,
......
......@@ -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)* [',']
......
......@@ -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,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment