Skip to content

Commit dd5353a

Browse files
committed
py: Add MICROPY_ENABLE_COMPILER and MICROPY_PY_BUILTINS_EVAL_EXEC opts.
MICROPY_ENABLE_COMPILER can be used to enable/disable the entire compiler, which is useful when only loading of pre-compiled bytecode is supported. It is enabled by default. MICROPY_PY_BUILTINS_EVAL_EXEC controls support of eval and exec builtin functions. By default they are only included if MICROPY_ENABLE_COMPILER is enabled. Disabling both options saves about 40k of code size on 32-bit x86.
1 parent ab8012b commit dd5353a

12 files changed

Lines changed: 60 additions & 1 deletion

File tree

py/builtinevex.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_compile_obj, 3, 6, mp_builtin_com
107107

108108
#endif // MICROPY_PY_BUILTINS_COMPILE
109109

110+
#if MICROPY_PY_BUILTINS_EVAL_EXEC
111+
110112
STATIC mp_obj_t eval_exec_helper(mp_uint_t n_args, const mp_obj_t *args, mp_parse_input_kind_t parse_input_kind) {
111113
// work out the context
112114
mp_obj_dict_t *globals = mp_globals_get();
@@ -155,6 +157,8 @@ STATIC mp_obj_t mp_builtin_exec(mp_uint_t n_args, const mp_obj_t *args) {
155157
}
156158
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec);
157159

160+
#endif // MICROPY_PY_BUILTINS_EVAL_EXEC
161+
158162
#if MICROPY_PY_BUILTINS_EXECFILE
159163
STATIC mp_obj_t mp_builtin_execfile(mp_uint_t n_args, const mp_obj_t *args) {
160164
// MP_PARSE_SINGLE_INPUT is used to indicate a file input

py/builtinimport.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ STATIC mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *d
120120
#endif
121121
}
122122

123+
#if MICROPY_ENABLE_COMPILER
123124
STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex, const char *fname) {
124125

125126
if (lex == NULL) {
@@ -141,6 +142,7 @@ STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex, const char
141142
mp_obj_dict_t *mod_globals = mp_obj_module_get_globals(module_obj);
142143
mp_parse_compile_execute(lex, MP_PARSE_FILE_INPUT, mod_globals, mod_globals);
143144
}
145+
#endif
144146

145147
#if MICROPY_PERSISTENT_CODE_LOAD
146148
STATIC void do_execute_raw_code(mp_obj_t module_obj, mp_raw_code_t *raw_code) {
@@ -182,16 +184,24 @@ STATIC void do_execute_raw_code(mp_obj_t module_obj, mp_raw_code_t *raw_code) {
182184
STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
183185
// create the lexer
184186
char *file_str = vstr_null_terminated_str(file);
187+
185188
#if MICROPY_PERSISTENT_CODE_LOAD
186189
if (file_str[file->len - 3] == 'm') {
187190
mp_raw_code_t *raw_code = mp_raw_code_load_file(file_str);
188191
do_execute_raw_code(module_obj, raw_code);
189-
} else
192+
return;
193+
}
190194
#endif
195+
196+
#if MICROPY_ENABLE_COMPILER
191197
{
192198
mp_lexer_t *lex = mp_lexer_new_from_file(file_str);
193199
do_load_from_lexer(module_obj, lex, file_str);
194200
}
201+
#else
202+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError,
203+
"script compilation not supported"));
204+
#endif
195205
}
196206

197207
STATIC void chop_component(const char *start, const char **end) {

py/compile.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#include "py/compile.h"
3636
#include "py/runtime.h"
3737

38+
#if MICROPY_ENABLE_COMPILER
39+
3840
// TODO need to mangle __attr names
3941

4042
typedef enum {
@@ -3310,3 +3312,5 @@ mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt
33103312
// return function that executes the outer module
33113313
return mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL);
33123314
}
3315+
3316+
#endif // MICROPY_ENABLE_COMPILER

py/emitbc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include "py/emit.h"
3535
#include "py/bc0.h"
3636

37+
#if MICROPY_ENABLE_COMPILER
38+
3739
#define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7)
3840
#define DUMMY_DATA_SIZE (BYTES_FOR_INT)
3941

@@ -1070,3 +1072,5 @@ const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_delete_id_ops = {
10701072
mp_emit_bc_delete_global,
10711073
};
10721074
#endif
1075+
1076+
#endif //MICROPY_ENABLE_COMPILER

py/emitcommon.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
#include "py/emit.h"
3030

31+
#if MICROPY_ENABLE_COMPILER
32+
3133
void mp_emit_common_get_id_for_load(scope_t *scope, qstr qst) {
3234
// name adding/lookup
3335
bool added;
@@ -77,3 +79,5 @@ void mp_emit_common_id_op(emit_t *emit, const mp_emit_method_table_id_ops_t *emi
7779
emit_method_table->deref(emit, qst, id->local_num);
7880
}
7981
}
82+
83+
#endif // MICROPY_ENABLE_COMPILER

py/lexer.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include "py/lexer.h"
3232
#include "py/runtime.h"
3333

34+
#if MICROPY_ENABLE_COMPILER
35+
3436
#define TAB_SIZE (8)
3537

3638
// TODO seems that CPython allows NULL byte in the input stream
@@ -785,3 +787,5 @@ void mp_lexer_show_token(const mp_lexer_t *lex) {
785787
printf("\n");
786788
}
787789
#endif
790+
791+
#endif // MICROPY_ENABLE_COMPILER

py/lexerstr.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
#include "py/lexer.h"
2828

29+
#if MICROPY_ENABLE_COMPILER
30+
2931
typedef struct _mp_lexer_str_buf_t {
3032
mp_uint_t free_len; // if > 0, src_beg will be freed when done by: m_free(src_beg, free_len)
3133
const char *src_beg; // beginning of source
@@ -59,3 +61,5 @@ mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, mp_uint_t
5961
sb->src_end = str + len;
6062
return mp_lexer_new(src_name, sb, (mp_lexer_stream_next_byte_t)str_buf_next_byte, (mp_lexer_stream_close_t)str_buf_free);
6163
}
64+
65+
#endif // MICROPY_ENABLE_COMPILER

py/modbuiltins.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,10 @@ STATIC const mp_rom_map_elem_t mp_module_builtins_globals_table[] = {
630630
{ MP_ROM_QSTR(MP_QSTR_chr), MP_ROM_PTR(&mp_builtin_chr_obj) },
631631
{ MP_ROM_QSTR(MP_QSTR_dir), MP_ROM_PTR(&mp_builtin_dir_obj) },
632632
{ MP_ROM_QSTR(MP_QSTR_divmod), MP_ROM_PTR(&mp_builtin_divmod_obj) },
633+
#if MICROPY_PY_BUILTINS_EVAL_EXEC
633634
{ MP_ROM_QSTR(MP_QSTR_eval), MP_ROM_PTR(&mp_builtin_eval_obj) },
634635
{ MP_ROM_QSTR(MP_QSTR_exec), MP_ROM_PTR(&mp_builtin_exec_obj) },
636+
#endif
635637
#if MICROPY_PY_BUILTINS_EXECFILE
636638
{ MP_ROM_QSTR(MP_QSTR_execfile), MP_ROM_PTR(&mp_builtin_execfile_obj) },
637639
#endif

py/mpconfig.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@
278278
/*****************************************************************************/
279279
/* Compiler configuration */
280280

281+
// Whether to include the compiler
282+
#ifndef MICROPY_ENABLE_COMPILER
283+
#define MICROPY_ENABLE_COMPILER (1)
284+
#endif
285+
281286
// Whether to enable constant folding; eg 1+2 rewritten as 3
282287
#ifndef MICROPY_COMP_CONST_FOLDING
283288
#define MICROPY_COMP_CONST_FOLDING (1)
@@ -590,6 +595,12 @@ typedef double mp_float_t;
590595
#define MICROPY_PY_BUILTINS_ENUMERATE (1)
591596
#endif
592597

598+
// Whether to support eval and exec functions
599+
// By default they are supported if the compiler is enabled
600+
#ifndef MICROPY_PY_BUILTINS_EVAL_EXEC
601+
#define MICROPY_PY_BUILTINS_EVAL_EXEC (MICROPY_ENABLE_COMPILER)
602+
#endif
603+
593604
// Whether to support the Python 2 execfile function
594605
#ifndef MICROPY_PY_BUILTINS_EXECFILE
595606
#define MICROPY_PY_BUILTINS_EXECFILE (0)

py/parse.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#include "py/runtime.h"
3939
#include "py/builtin.h"
4040

41+
#if MICROPY_ENABLE_COMPILER
42+
4143
#define RULE_ACT_ARG_MASK (0x0f)
4244
#define RULE_ACT_KIND_MASK (0x30)
4345
#define RULE_ACT_ALLOW_IDENT (0x40)
@@ -1079,3 +1081,5 @@ void mp_parse_tree_clear(mp_parse_tree_t *tree) {
10791081
chunk = next;
10801082
}
10811083
}
1084+
1085+
#endif // MICROPY_ENABLE_COMPILER

0 commit comments

Comments
 (0)