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>
This commit is contained in:
Damien George
2022-12-07 14:42:35 +11:00
parent 96c23432f6
commit 2283b6d68f
8 changed files with 39 additions and 41 deletions

View File

@@ -390,7 +390,7 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, mp_module_context_t *co
return rc;
}
mp_compiled_module_t mp_raw_code_load(mp_reader_t *reader, mp_module_context_t *context) {
void mp_raw_code_load(mp_reader_t *reader, mp_compiled_module_t *cm) {
byte header[4];
read_bytes(reader, header, sizeof(header));
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 *
size_t n_qstr = read_uint(reader);
size_t n_obj = read_uint(reader);
mp_module_context_alloc_tables(context, n_qstr, n_obj);
mp_module_context_alloc_tables(cm->context, n_qstr, n_obj);
// Load qstrs.
for (size_t i = 0; i < n_qstr; ++i) {
context->constants.qstr_table[i] = load_qstr(reader);
cm->context->constants.qstr_table[i] = load_qstr(reader);
}
// Load constant objects.
for (size_t i = 0; i < n_obj; ++i) {
context->constants.obj_table[i] = load_obj(reader);
cm->context->constants.obj_table[i] = load_obj(reader);
}
// Load top-level module.
mp_compiled_module_t cm2;
cm2.rc = load_raw_code(reader, context);
cm2.context = context;
cm->rc = load_raw_code(reader, cm->context);
#if MICROPY_PERSISTENT_CODE_SAVE
cm2.has_native = MPY_FEATURE_DECODE_ARCH(header[2]) != MP_NATIVE_ARCH_NONE;
cm2.n_qstr = n_qstr;
cm2.n_obj = n_obj;
cm->has_native = MPY_FEATURE_DECODE_ARCH(header[2]) != MP_NATIVE_ARCH_NONE;
cm->n_qstr = n_qstr;
cm->n_obj = n_obj;
#endif
reader->close(reader->data);
return cm2;
}
mp_compiled_module_t mp_raw_code_load_mem(const byte *buf, size_t len, mp_module_context_t *context) {
void mp_raw_code_load_mem(const byte *buf, size_t len, mp_compiled_module_t *context) {
mp_reader_t reader;
mp_reader_new_mem(&reader, buf, len, 0);
return mp_raw_code_load(&reader, context);
mp_raw_code_load(&reader, context);
}
#if MICROPY_HAS_FILE_READER
mp_compiled_module_t mp_raw_code_load_file(const char *filename, mp_module_context_t *context) {
void mp_raw_code_load_file(const char *filename, mp_compiled_module_t *context) {
mp_reader_t reader;
mp_reader_new_file(&reader, filename);
return mp_raw_code_load(&reader, context);
mp_raw_code_load(&reader, context);
}
#endif // MICROPY_HAS_FILE_READER