Content-Length: 745832 | pFad | http://github.com/micropython/micropython/commit/2283b6d68fe77b72e7beeb1bf24778834231b190

46 py: Pass in address to compiled module instead of returning it. · micropython/micropython@2283b6d · GitHub
Skip to content

Commit 2283b6d

Browse files
committed
py: Pass in address to compiled module instead of returning it.
This change makes it so the compiler and persistent code loader take a mp_compiled_module_t* as their last argument, instead of returning this struct. This eliminates a duplicate context variable for all callers of these functions (because the context is now stored in the mp_compiled_module_t by the caller), and also eliminates any confusion about which context to use after the mp_compile_to_raw_code or mp_raw_code_load function returns (because there is now only one context, that stored in mp_compiled_module_t.context). Reduces code size by 16 bytes on ARM Cortex-based ports. Signed-off-by: Damien George <damien@micropython.org>
1 parent 96c2343 commit 2283b6d

File tree

8 files changed

+39
-41
lines changed

8 files changed

+39
-41
lines changed

docs/develop/compiler.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ The most relevant method you should know about is this:
152152
context->module.globals = mp_globals_get();
153153
154154
// Compile the input parse_tree to a raw-code structure.
155-
mp_compiled_module_t cm = mp_compile_to_raw_code(parse_tree, source_file, is_repl, context);
155+
mp_compiled_module_t cm;
156+
cm.context = context;
157+
mp_compile_to_raw_code(parse_tree, source_file, is_repl, &cm);
156158
157159
// Create and return a function object that executes the outer module.
158160
return mp_make_function_from_raw_code(cm.rc, cm.context, NULL);

mpy-cross/main.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ STATIC int compile_and_save(const char *file, const char *output_file, const cha
7373
#endif
7474

7575
mp_parse_tree_t parse_tree = mp_parse(lex, MP_PARSE_FILE_INPUT);
76-
mp_module_context_t *ctx = m_new_obj(mp_module_context_t);
77-
mp_compiled_module_t cm = mp_compile_to_raw_code(&parse_tree, source_name, false, ctx);
76+
mp_compiled_module_t cm;
77+
cm.context = m_new_obj(mp_module_context_t);
78+
mp_compile_to_raw_code(&parse_tree, source_name, false, &cm);
7879

7980
vstr_t vstr;
8081
vstr_init(&vstr, 16);

py/bc.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ typedef struct _mp_module_context_t {
214214

215215
// Outer level struct defining a compiled module.
216216
typedef struct _mp_compiled_module_t {
217-
const mp_module_context_t *context;
217+
mp_module_context_t *context;
218218
const struct _mp_raw_code_t *rc;
219219
#if MICROPY_PERSISTENT_CODE_SAVE
220220
bool has_native;

py/builtinimport.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,9 @@ STATIC void do_load(mp_module_context_t *module_obj, vstr_t *file) {
245245
// the correct format and, if so, load and execute the file.
246246
#if MICROPY_HAS_FILE_READER && MICROPY_PERSISTENT_CODE_LOAD
247247
if (file_str[file->len - 3] == 'm') {
248-
mp_compiled_module_t cm = mp_raw_code_load_file(file_str, module_obj);
248+
mp_compiled_module_t cm;
249+
cm.context = module_obj;
250+
mp_raw_code_load_file(file_str, &cm);
249251
do_execute_raw_code(cm.context, cm.rc, file_str);
250252
return;
251253
}

py/compile.c

+13-16
Original file line numberDiff line numberDiff line change
@@ -3427,7 +3427,7 @@ STATIC void scope_compute_things(scope_t *scope) {
34273427
#if !MICROPY_PERSISTENT_CODE_SAVE
34283428
STATIC
34293429
#endif
3430-
mp_compiled_module_t mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, bool is_repl, mp_module_context_t *context) {
3430+
void mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, bool is_repl, mp_compiled_module_t *cm) {
34313431
// put compiler state on the stack, it's relatively small
34323432
compiler_t comp_state = {0};
34333433
compiler_t *comp = &comp_state;
@@ -3568,34 +3568,32 @@ mp_compiled_module_t mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr so
35683568
}
35693569

35703570
// construct the global qstr/const table for this module
3571-
mp_compiled_module_t cm;
3572-
cm.rc = module_scope->raw_code;
3573-
cm.context = context;
3571+
cm->rc = module_scope->raw_code;
35743572
#if MICROPY_PERSISTENT_CODE_SAVE
3575-
cm.has_native = false;
3573+
cm->has_native = false;
35763574
#if MICROPY_EMIT_NATIVE
35773575
if (emit_native != NULL) {
3578-
cm.has_native = true;
3576+
cm->has_native = true;
35793577
}
35803578
#endif
35813579
#if MICROPY_EMIT_INLINE_ASM
35823580
if (comp->emit_inline_asm != NULL) {
3583-
cm.has_native = true;
3581+
cm->has_native = true;
35843582
}
35853583
#endif
3586-
cm.n_qstr = comp->emit_common.qstr_map.used;
3587-
cm.n_obj = comp->emit_common.const_obj_list.len;
3584+
cm->n_qstr = comp->emit_common.qstr_map.used;
3585+
cm->n_obj = comp->emit_common.const_obj_list.len;
35883586
#endif
35893587
if (comp->compile_error == MP_OBJ_NULL) {
3590-
mp_emit_common_populate_module_context(&comp->emit_common, source_file, context);
3588+
mp_emit_common_populate_module_context(&comp->emit_common, source_file, cm->context);
35913589

35923590
#if MICROPY_DEBUG_PRINTERS
35933591
// now that the module context is valid, the raw codes can be printed
35943592
if (mp_verbose_flag >= 2) {
35953593
for (scope_t *s = comp->scope_head; s != NULL; s = s->next) {
35963594
mp_raw_code_t *rc = s->raw_code;
35973595
if (rc->kind == MP_CODE_BYTECODE) {
3598-
mp_bytecode_print(&mp_plat_print, rc, &cm.context->constants);
3596+
mp_bytecode_print(&mp_plat_print, rc, &cm->context->constants);
35993597
}
36003598
}
36013599
}
@@ -3629,14 +3627,13 @@ mp_compiled_module_t mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr so
36293627
if (comp->compile_error != MP_OBJ_NULL) {
36303628
nlr_raise(comp->compile_error);
36313629
}
3632-
3633-
return cm;
36343630
}
36353631

36363632
mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, bool is_repl) {
3637-
mp_module_context_t *context = m_new_obj(mp_module_context_t);
3638-
context->module.globals = mp_globals_get();
3639-
mp_compiled_module_t cm = mp_compile_to_raw_code(parse_tree, source_file, is_repl, context);
3633+
mp_compiled_module_t cm;
3634+
cm.context = m_new_obj(mp_module_context_t);
3635+
cm.context->module.globals = mp_globals_get();
3636+
mp_compile_to_raw_code(parse_tree, source_file, is_repl, &cm);
36403637
// return function that executes the outer module
36413638
return mp_make_function_from_raw_code(cm.rc, cm.context, NULL);
36423639
}

py/compile.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, bool is_repl)
3737

3838
#if MICROPY_PERSISTENT_CODE_SAVE
3939
// this has the same semantics as mp_compile
40-
mp_compiled_module_t mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, bool is_repl, mp_module_context_t *globals);
40+
void mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, bool is_repl, mp_compiled_module_t *cm);
4141
#endif
4242

4343
// this is implemented in runtime.c

py/persistentcode.c

+12-16
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, mp_module_context_t *co
390390
return rc;
391391
}
392392

393-
mp_compiled_module_t mp_raw_code_load(mp_reader_t *reader, mp_module_context_t *context) {
393+
void mp_raw_code_load(mp_reader_t *reader, mp_compiled_module_t *cm) {
394394
byte header[4];
395395
read_bytes(reader, header, sizeof(header));
396396
byte arch = MPY_FEATURE_DECODE_ARCH(header[2]);
@@ -414,46 +414,42 @@ mp_compiled_module_t mp_raw_code_load(mp_reader_t *reader, mp_module_context_t *
414414

415415
size_t n_qstr = read_uint(reader);
416416
size_t n_obj = read_uint(reader);
417-
mp_module_context_alloc_tables(context, n_qstr, n_obj);
417+
mp_module_context_alloc_tables(cm->context, n_qstr, n_obj);
418418

419419
// Load qstrs.
420420
for (size_t i = 0; i < n_qstr; ++i) {
421-
context->constants.qstr_table[i] = load_qstr(reader);
421+
cm->context->constants.qstr_table[i] = load_qstr(reader);
422422
}
423423

424424
// Load constant objects.
425425
for (size_t i = 0; i < n_obj; ++i) {
426-
context->constants.obj_table[i] = load_obj(reader);
426+
cm->context->constants.obj_table[i] = load_obj(reader);
427427
}
428428

429429
// Load top-level module.
430-
mp_compiled_module_t cm2;
431-
cm2.rc = load_raw_code(reader, context);
432-
cm2.context = context;
430+
cm->rc = load_raw_code(reader, cm->context);
433431

434432
#if MICROPY_PERSISTENT_CODE_SAVE
435-
cm2.has_native = MPY_FEATURE_DECODE_ARCH(header[2]) != MP_NATIVE_ARCH_NONE;
436-
cm2.n_qstr = n_qstr;
437-
cm2.n_obj = n_obj;
433+
cm->has_native = MPY_FEATURE_DECODE_ARCH(header[2]) != MP_NATIVE_ARCH_NONE;
434+
cm->n_qstr = n_qstr;
435+
cm->n_obj = n_obj;
438436
#endif
439437

440438
reader->close(reader->data);
441-
442-
return cm2;
443439
}
444440

445-
mp_compiled_module_t mp_raw_code_load_mem(const byte *buf, size_t len, mp_module_context_t *context) {
441+
void mp_raw_code_load_mem(const byte *buf, size_t len, mp_compiled_module_t *context) {
446442
mp_reader_t reader;
447443
mp_reader_new_mem(&reader, buf, len, 0);
448-
return mp_raw_code_load(&reader, context);
444+
mp_raw_code_load(&reader, context);
449445
}
450446

451447
#if MICROPY_HAS_FILE_READER
452448

453-
mp_compiled_module_t mp_raw_code_load_file(const char *filename, mp_module_context_t *context) {
449+
void mp_raw_code_load_file(const char *filename, mp_compiled_module_t *context) {
454450
mp_reader_t reader;
455451
mp_reader_new_file(&reader, filename);
456-
return mp_raw_code_load(&reader, context);
452+
mp_raw_code_load(&reader, context);
457453
}
458454

459455
#endif // MICROPY_HAS_FILE_READER

py/persistentcode.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ enum {
111111
MP_PERSISTENT_OBJ_TUPLE,
112112
};
113113

114-
mp_compiled_module_t mp_raw_code_load(mp_reader_t *reader, mp_module_context_t *ctx);
115-
mp_compiled_module_t mp_raw_code_load_mem(const byte *buf, size_t len, mp_module_context_t *ctx);
116-
mp_compiled_module_t mp_raw_code_load_file(const char *filename, mp_module_context_t *ctx);
114+
void mp_raw_code_load(mp_reader_t *reader, mp_compiled_module_t *ctx);
115+
void mp_raw_code_load_mem(const byte *buf, size_t len, mp_compiled_module_t *ctx);
116+
void mp_raw_code_load_file(const char *filename, mp_compiled_module_t *ctx);
117117

118118
void mp_raw_code_save(mp_compiled_module_t *cm, mp_print_t *print);
119119
void mp_raw_code_save_file(mp_compiled_module_t *cm, const char *filename);

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/micropython/micropython/commit/2283b6d68fe77b72e7beeb1bf24778834231b190

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy