Skip to content

Commit 65cad12

Browse files
committed
py: Add option to compiler to specify default code emitter.
Also add command line option to unix port to select emitter.
1 parent deed087 commit 65cad12

8 files changed

Lines changed: 41 additions & 26 deletions

File tree

py/builtinevex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ STATIC mp_obj_t parse_compile_execute(mp_obj_t o_in, mp_parse_input_kind_t parse
3333
}
3434

3535
// compile the string
36-
mp_obj_t module_fun = mp_compile(pn, source_name, false);
36+
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, false);
3737
mp_parse_node_free(pn);
3838

3939
if (module_fun == mp_const_none) {

py/builtinimport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void do_load(mp_obj_t module_obj, vstr_t *file) {
103103
}
104104

105105
// compile the imported script
106-
mp_obj_t module_fun = mp_compile(pn, source_name, false);
106+
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, false);
107107
mp_parse_node_free(pn);
108108

109109
if (module_fun == mp_const_none) {

py/compile.c

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ typedef enum {
3636
#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
3737
#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
3838

39-
#define EMIT_OPT_NONE (0)
40-
#define EMIT_OPT_BYTE_CODE (1)
41-
#define EMIT_OPT_NATIVE_PYTHON (2)
42-
#define EMIT_OPT_VIPER (3)
43-
#define EMIT_OPT_ASM_THUMB (4)
44-
4539
typedef struct _compiler_t {
4640
qstr source_file;
4741
bool is_repl;
@@ -1000,16 +994,16 @@ STATIC bool compile_built_in_decorator(compiler_t *comp, int name_len, mp_parse_
1000994

1001995
qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
1002996
if (attr == MP_QSTR_byte_code) {
1003-
*emit_options = EMIT_OPT_BYTE_CODE;
997+
*emit_options = MP_EMIT_OPT_BYTE_CODE;
1004998
#if MICROPY_EMIT_NATIVE
1005999
} else if (attr == MP_QSTR_native) {
1006-
*emit_options = EMIT_OPT_NATIVE_PYTHON;
1000+
*emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
10071001
} else if (attr == MP_QSTR_viper) {
1008-
*emit_options = EMIT_OPT_VIPER;
1002+
*emit_options = MP_EMIT_OPT_VIPER;
10091003
#endif
10101004
#if MICROPY_EMIT_INLINE_THUMB
10111005
} else if (attr == MP_QSTR_asm_thumb) {
1012-
*emit_options = EMIT_OPT_ASM_THUMB;
1006+
*emit_options = MP_EMIT_OPT_ASM_THUMB;
10131007
#endif
10141008
} else {
10151009
compile_syntax_error(comp, "invalid micropython decorator");
@@ -1601,7 +1595,7 @@ void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
16011595
// this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
16021596
// this is actually slower, but uses no heap memory
16031597
// for viper it will be much, much faster
1604-
if (/*comp->scope_cur->emit_options == EMIT_OPT_VIPER &&*/ MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_power)) {
1598+
if (/*comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER &&*/ MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_power)) {
16051599
mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
16061600
if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
16071601
&& MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
@@ -3187,7 +3181,7 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
31873181
}
31883182
}
31893183

3190-
mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
3184+
mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is_repl) {
31913185
compiler_t *comp = m_new(compiler_t, 1);
31923186

31933187
comp->source_file = source_file;
@@ -3208,7 +3202,7 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
32083202
pn = fold_constants(pn);
32093203

32103204
// set the outer scope
3211-
scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE);
3205+
scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
32123206

32133207
// compile pass 1
32143208
comp->emit = emit_pass1_new();
@@ -3219,7 +3213,7 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
32193213
for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
32203214
if (false) {
32213215
#if MICROPY_EMIT_INLINE_THUMB
3222-
} else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
3216+
} else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
32233217
compile_scope_inline_asm(comp, s, PASS_1);
32243218
#endif
32253219
} else {
@@ -3255,7 +3249,7 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
32553249
// dummy
32563250

32573251
#if MICROPY_EMIT_INLINE_THUMB
3258-
} else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
3252+
} else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
32593253
// inline assembly for thumb
32603254
if (emit_inline_thumb == NULL) {
32613255
emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
@@ -3279,8 +3273,8 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
32793273
switch (s->emit_options) {
32803274

32813275
#if MICROPY_EMIT_NATIVE
3282-
case EMIT_OPT_NATIVE_PYTHON:
3283-
case EMIT_OPT_VIPER:
3276+
case MP_EMIT_OPT_NATIVE_PYTHON:
3277+
case MP_EMIT_OPT_VIPER:
32843278
#if MICROPY_EMIT_X64
32853279
if (emit_native == NULL) {
32863280
emit_native = emit_native_x64_new(max_num_labels);
@@ -3293,7 +3287,7 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
32933287
comp->emit_method_table = &emit_native_thumb_method_table;
32943288
#endif
32953289
comp->emit = emit_native;
3296-
comp->emit_method_table->set_native_types(comp->emit, s->emit_options == EMIT_OPT_VIPER);
3290+
comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
32973291
break;
32983292
#endif // MICROPY_EMIT_NATIVE
32993293

py/compile.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl);
1+
enum {
2+
MP_EMIT_OPT_NONE,
3+
MP_EMIT_OPT_BYTE_CODE,
4+
MP_EMIT_OPT_NATIVE_PYTHON,
5+
MP_EMIT_OPT_VIPER,
6+
MP_EMIT_OPT_ASM_THUMB,
7+
};
8+
9+
mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is_repl);

stm/pyexec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ bool parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bo
142142

143143
mp_lexer_free(lex);
144144

145-
mp_obj_t module_fun = mp_compile(pn, source_name, is_repl);
145+
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, is_repl);
146146
mp_parse_node_free(pn);
147147

148148
if (module_fun == mp_const_none) {

stmhal/pyexec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ bool parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bo
4444

4545
mp_lexer_free(lex);
4646

47-
mp_obj_t module_fun = mp_compile(pn, source_name, is_repl);
47+
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, is_repl);
4848
mp_parse_node_free(pn);
4949

5050
if (module_fun == mp_const_none) {

teensy/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ bool do_file(const char *filename) {
359359

360360
mp_lexer_free(lex);
361361

362-
mp_obj_t module_fun = mp_compile(pn, source_name, false);
362+
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, false);
363363
mp_parse_node_free(pn);
364364

365365
if (module_fun == mp_const_none) {
@@ -422,7 +422,7 @@ void do_repl(void) {
422422
} else {
423423
// parse okay
424424
mp_lexer_free(lex);
425-
mp_obj_t module_fun = mp_compile(pn, source_name, true);
425+
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true);
426426
if (module_fun != mp_const_none) {
427427
nlr_buf_t nlr;
428428
uint32_t start = micros();

unix/main.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
#include <readline/history.h>
2929
#endif
3030

31+
// Default emit options
32+
uint emit_opt = MP_EMIT_OPT_NONE;
33+
3134
#if MICROPY_ENABLE_GC
3235
// Heap size of GC heap (if enabled)
3336
// Make it larger on a 64 bit machine, because pointers are larger.
@@ -76,7 +79,7 @@ STATIC void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind
7679
printf("----------------\n");
7780
*/
7881

79-
mp_obj_t module_fun = mp_compile(pn, source_name, is_repl);
82+
mp_obj_t module_fun = mp_compile(pn, source_name, emit_opt, is_repl);
8083

8184
if (module_fun == mp_const_none) {
8285
// compile error
@@ -221,6 +224,10 @@ int usage(char **argv) {
221224
"Implementation specific options:\n", argv[0]
222225
);
223226
int impl_opts_cnt = 0;
227+
printf(
228+
" emit={bytecode,native,viper} -- set the default code emitter\n"
229+
);
230+
impl_opts_cnt++;
224231
#if MICROPY_ENABLE_GC
225232
printf(
226233
" heapsize=<n> -- set the heap size for the GC\n"
@@ -265,6 +272,12 @@ void pre_process_options(int argc, char **argv) {
265272
exit(usage(argv));
266273
}
267274
if (0) {
275+
} else if (strcmp(argv[a + 1], "emit=bytecode") == 0) {
276+
emit_opt = MP_EMIT_OPT_BYTE_CODE;
277+
} else if (strcmp(argv[a + 1], "emit=native") == 0) {
278+
emit_opt = MP_EMIT_OPT_NATIVE_PYTHON;
279+
} else if (strcmp(argv[a + 1], "emit=viper") == 0) {
280+
emit_opt = MP_EMIT_OPT_VIPER;
268281
#if MICROPY_ENABLE_GC
269282
} else if (strncmp(argv[a + 1], "heapsize=", sizeof("heapsize=") - 1) == 0) {
270283
heap_size = strtol(argv[a + 1] + sizeof("heapsize=") - 1, NULL, 0);

0 commit comments

Comments
 (0)