Skip to content

Commit e37dcaa

Browse files
committed
py: Allow to properly disable builtin "set" object.
This patch makes MICROPY_PY_BUILTINS_SET compile-time option fully disable the builtin set object (when set to 0). This includes removing set constructor/comprehension from the grammar, the compiler and the emitters. Now, enabling set costs 8168 bytes on unix x64, and 3576 bytes on stmhal.
1 parent 3b74c91 commit e37dcaa

10 files changed

Lines changed: 37 additions & 3 deletions

File tree

py/compile.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2761,7 +2761,7 @@ STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
27612761

27622762
// first element sets whether it's a dict or set
27632763
bool is_dict;
2764-
if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
2764+
if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
27652765
// a dictionary
27662766
EMIT_ARG(build_map, 1 + n);
27672767
compile_node(comp, pns->nodes[0]);
@@ -2792,13 +2792,15 @@ STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
27922792
}
27932793
}
27942794

2795+
#if MICROPY_PY_BUILTINS_SET
27952796
// if it's a set, build it
27962797
if (!is_dict) {
27972798
EMIT_ARG(build_set, 1 + n);
27982799
}
2800+
#endif
27992801
} else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
28002802
// dict/set comprehension
2801-
if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
2803+
if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
28022804
// a dictionary comprehension
28032805
compile_comprehension(comp, pns, SCOPE_DICT_COMP);
28042806
} else {
@@ -2816,8 +2818,12 @@ STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
28162818
} else {
28172819
// set with one element
28182820
set_with_one_element:
2821+
#if MICROPY_PY_BUILTINS_SET
28192822
compile_node(comp, pn);
28202823
EMIT_ARG(build_set, 1);
2824+
#else
2825+
assert(0);
2826+
#endif
28212827
}
28222828
}
28232829

@@ -3111,8 +3117,10 @@ STATIC void compile_scope_comp_iter(compiler_t *comp, mp_parse_node_t pn_iter, m
31113117
EMIT_ARG(list_append, for_depth + 2);
31123118
} else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
31133119
EMIT_ARG(map_add, for_depth + 2);
3120+
#if MICROPY_PY_BUILTINS_SET
31143121
} else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
31153122
EMIT_ARG(set_add, for_depth + 2);
3123+
#endif
31163124
} else {
31173125
EMIT(yield_value);
31183126
EMIT(pop_top);
@@ -3305,8 +3313,10 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
33053313
EMIT_ARG(build_list, 0);
33063314
} else if (scope->kind == SCOPE_DICT_COMP) {
33073315
EMIT_ARG(build_map, 0);
3316+
#if MICROPY_PY_BUILTINS_SET
33083317
} else if (scope->kind == SCOPE_SET_COMP) {
33093318
EMIT_ARG(build_set, 0);
3319+
#endif
33103320
}
33113321

33123322
uint l_end = comp_next_label(comp);

py/emit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,10 @@ typedef struct _emit_method_table_t {
128128
void (*build_map)(emit_t *emit, mp_uint_t n_args);
129129
void (*store_map)(emit_t *emit);
130130
void (*map_add)(emit_t *emit, mp_uint_t map_stack_index);
131+
#if MICROPY_PY_BUILTINS_SET
131132
void (*build_set)(emit_t *emit, mp_uint_t n_args);
132133
void (*set_add)(emit_t *emit, mp_uint_t set_stack_index);
134+
#endif
133135
void (*build_slice)(emit_t *emit, mp_uint_t n_args);
134136
void (*unpack_sequence)(emit_t *emit, mp_uint_t n_args);
135137
void (*unpack_ex)(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right);

py/emitbc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,7 @@ STATIC void emit_bc_map_add(emit_t *emit, mp_uint_t map_stack_index) {
782782
emit_write_bytecode_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index);
783783
}
784784

785+
#if MICROPY_PY_BUILTINS_SET
785786
STATIC void emit_bc_build_set(emit_t *emit, mp_uint_t n_args) {
786787
emit_bc_pre(emit, 1 - n_args);
787788
emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SET, n_args);
@@ -791,6 +792,7 @@ STATIC void emit_bc_set_add(emit_t *emit, mp_uint_t set_stack_index) {
791792
emit_bc_pre(emit, -1);
792793
emit_write_bytecode_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
793794
}
795+
#endif
794796

795797
STATIC void emit_bc_build_slice(emit_t *emit, mp_uint_t n_args) {
796798
emit_bc_pre(emit, 1 - n_args);
@@ -960,8 +962,10 @@ const emit_method_table_t emit_bc_method_table = {
960962
emit_bc_build_map,
961963
emit_bc_store_map,
962964
emit_bc_map_add,
965+
#if MICROPY_PY_BUILTINS_SET
963966
emit_bc_build_set,
964967
emit_bc_set_add,
968+
#endif
965969
emit_bc_build_slice,
966970
emit_bc_unpack_sequence,
967971
emit_bc_unpack_ex,

py/emitnative.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,6 +2064,7 @@ STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
20642064
emit_post(emit);
20652065
}
20662066

2067+
#if MICROPY_PY_BUILTINS_SET
20672068
STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
20682069
emit_native_pre(emit);
20692070
emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
@@ -2081,6 +2082,7 @@ STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
20812082
emit_call(emit, MP_F_STORE_SET);
20822083
emit_post(emit);
20832084
}
2085+
#endif
20842086

20852087
STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
20862088
DEBUG_printf("build_slice %d\n", n_args);
@@ -2330,8 +2332,10 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
23302332
emit_native_build_map,
23312333
emit_native_store_map,
23322334
emit_native_map_add,
2335+
#if MICROPY_PY_BUILTINS_SET
23332336
emit_native_build_set,
23342337
emit_native_set_add,
2338+
#endif
23352339
emit_native_build_slice,
23362340
emit_native_unpack_sequence,
23372341
emit_native_unpack_ex,

py/emitpass1.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,10 @@ const emit_method_table_t emit_pass1_method_table = {
201201
(void*)emit_pass1_dummy,
202202
(void*)emit_pass1_dummy,
203203
(void*)emit_pass1_dummy,
204+
#if MICROPY_PY_BUILTINS_SET
204205
(void*)emit_pass1_dummy,
205206
(void*)emit_pass1_dummy,
207+
#endif
206208
(void*)emit_pass1_dummy,
207209
(void*)emit_pass1_dummy,
208210
(void*)emit_pass1_dummy,

py/grammar.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,12 @@ DEF_RULE(exprlist_2, nc, or(2), rule(star_expr), rule(expr))
287287
DEF_RULE(testlist, c(generic_tuple), list_with_end, rule(test), tok(DEL_COMMA))
288288
// TODO dictorsetmaker lets through more than is allowed
289289
DEF_RULE(dictorsetmaker, nc, and(2), rule(dictorsetmaker_item), opt_rule(dictorsetmaker_tail))
290+
#if MICROPY_PY_BUILTINS_SET
290291
DEF_RULE(dictorsetmaker_item, c(dictorsetmaker_item), and(2), rule(test), opt_rule(dictorsetmaker_colon))
291292
DEF_RULE(dictorsetmaker_colon, nc, ident | and(2), tok(DEL_COLON), rule(test))
293+
#else
294+
DEF_RULE(dictorsetmaker_item, c(dictorsetmaker_item), and(3), rule(test), tok(DEL_COLON), rule(test))
295+
#endif
292296
DEF_RULE(dictorsetmaker_tail, nc, or(2), rule(comp_for), rule(dictorsetmaker_list))
293297
DEF_RULE(dictorsetmaker_list, nc, and(2), tok(DEL_COMMA), opt_rule(dictorsetmaker_list2))
294298
DEF_RULE(dictorsetmaker_list2, nc, list_with_end, rule(dictorsetmaker_item), tok(DEL_COMMA))

py/map.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t
253253
/******************************************************************************/
254254
/* set */
255255

256+
#if MICROPY_PY_BUILTINS_SET
257+
256258
void mp_set_init(mp_set_t *set, mp_uint_t n) {
257259
set->alloc = n;
258260
set->used = 0;
@@ -368,6 +370,8 @@ void mp_set_clear(mp_set_t *set) {
368370
set->table = NULL;
369371
}
370372

373+
#endif // MICROPY_PY_BUILTINS_SET
374+
371375
#if defined(DEBUG_PRINT) && DEBUG_PRINT
372376
void mp_map_dump(mp_map_t *map) {
373377
for (mp_uint_t i = 0; i < map->alloc; i++) {

py/modbuiltins.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ STATIC const mp_map_elem_t mp_module_builtins_globals_table[] = {
583583
#if MICROPY_PY_BUILTINS_FLOAT
584584
{ MP_OBJ_NEW_QSTR(MP_QSTR_float), (mp_obj_t)&mp_type_float },
585585
#endif
586-
#if MICROPY_PY_BUILTINS_FROZENSET
586+
#if MICROPY_PY_BUILTINS_SET && MICROPY_PY_BUILTINS_FROZENSET
587587
{ MP_OBJ_NEW_QSTR(MP_QSTR_frozenset), (mp_obj_t)&mp_type_frozenset },
588588
#endif
589589
{ MP_OBJ_NEW_QSTR(MP_QSTR_int), (mp_obj_t)&mp_type_int },

py/vmentrytable.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ static void* entry_table[256] = {
8686
[MP_BC_BUILD_MAP] = &&entry_MP_BC_BUILD_MAP,
8787
[MP_BC_STORE_MAP] = &&entry_MP_BC_STORE_MAP,
8888
[MP_BC_MAP_ADD] = &&entry_MP_BC_MAP_ADD,
89+
#if MICROPY_PY_BUILTINS_SET
8990
[MP_BC_BUILD_SET] = &&entry_MP_BC_BUILD_SET,
9091
[MP_BC_SET_ADD] = &&entry_MP_BC_SET_ADD,
92+
#endif
9193
[MP_BC_BUILD_SLICE] = &&entry_MP_BC_BUILD_SLICE,
9294
[MP_BC_UNPACK_SEQUENCE] = &&entry_MP_BC_UNPACK_SEQUENCE,
9395
[MP_BC_UNPACK_EX] = &&entry_MP_BC_UNPACK_EX,

unix/qstrdefsport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Q(as_bytearray)
4848
Q(callback)
4949
Q(func)
5050
Q(var)
51+
Q(get)
52+
Q(set)
5153

5254
Q(input)
5355
Q(utime)

0 commit comments

Comments
 (0)