Skip to content

Commit 0bd7d1f

Browse files
committed
py/persistentcode: Move loading of rodata/bss to before obj/raw-code.
This makes the loading of viper-code-with-relocations a bit neater and easier to understand, by treating the rodata/bss like a special object to be loaded into the constant table (which is how it behaves).
1 parent 04e7aa0 commit 0bd7d1f

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

py/persistentcode.c

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,18 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, qstr_window_t *qw) {
438438

439439
// Allocate constant table
440440
size_t n_alloc = prelude.n_pos_args + prelude.n_kwonly_args + n_obj + n_raw_code;
441+
#if MICROPY_EMIT_MACHINE_CODE
441442
if (kind != MP_CODE_BYTECODE) {
442443
++n_alloc; // additional entry for mp_fun_table
444+
if (prelude.scope_flags & MP_SCOPE_FLAG_VIPERRODATA) {
445+
++n_alloc; // additional entry for rodata
446+
}
447+
if (prelude.scope_flags & MP_SCOPE_FLAG_VIPERBSS) {
448+
++n_alloc; // additional entry for BSS
449+
}
443450
}
451+
#endif
452+
444453
const_table = m_new(mp_uint_t, n_alloc);
445454
mp_uint_t *ct = const_table;
446455

@@ -454,6 +463,21 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, qstr_window_t *qw) {
454463
if (kind != MP_CODE_BYTECODE) {
455464
// Populate mp_fun_table entry
456465
*ct++ = (mp_uint_t)(uintptr_t)&mp_fun_table;
466+
467+
// Allocate and load rodata if needed
468+
if (prelude.scope_flags & MP_SCOPE_FLAG_VIPERRODATA) {
469+
size_t size = read_uint(reader, NULL);
470+
uint8_t *rodata = m_new(uint8_t, size);
471+
read_bytes(reader, rodata, size);
472+
*ct++ = (uintptr_t)rodata;
473+
}
474+
475+
// Allocate BSS if needed
476+
if (prelude.scope_flags & MP_SCOPE_FLAG_VIPERBSS) {
477+
size_t size = read_uint(reader, NULL);
478+
uint8_t *bss = m_new0(uint8_t, size);
479+
*ct++ = (uintptr_t)bss;
480+
}
457481
}
458482
#endif
459483

@@ -469,6 +493,7 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, qstr_window_t *qw) {
469493
// Create raw_code and return it
470494
mp_raw_code_t *rc = mp_emit_glue_new_raw_code();
471495
if (kind == MP_CODE_BYTECODE) {
496+
// Assign bytecode to raw code object
472497
mp_emit_glue_assign_bytecode(rc, fun_data,
473498
#if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
474499
fun_data_len,
@@ -481,18 +506,7 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, qstr_window_t *qw) {
481506

482507
#if MICROPY_EMIT_MACHINE_CODE
483508
} else {
484-
mp_uint_t *ct = &const_table[1];
485-
if (prelude.scope_flags & MP_SCOPE_FLAG_VIPERRODATA) {
486-
size_t size = read_uint(reader, NULL);
487-
uint8_t *rodata = m_new(uint8_t, size);
488-
read_bytes(reader, rodata, size);
489-
*ct++ = (uintptr_t)rodata;
490-
}
491-
if (prelude.scope_flags & MP_SCOPE_FLAG_VIPERBSS) {
492-
size_t size = read_uint(reader, NULL);
493-
uint8_t *bss = m_new0(uint8_t, size);
494-
*ct++ = (uintptr_t)bss;
495-
}
509+
// Relocate and commit code to executable address space
496510
reloc_info_t ri = {reader, const_table};
497511
#if defined(MP_PLAT_COMMIT_EXEC)
498512
void *opt_ri = (prelude.scope_flags & MP_SCOPE_FLAG_VIPERRELOC) ? &ri : NULL;
@@ -503,6 +517,7 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, qstr_window_t *qw) {
503517
}
504518
#endif
505519

520+
// Assign native code to raw code object
506521
mp_emit_glue_assign_native(rc, kind,
507522
fun_data, fun_data_len, const_table,
508523
#if MICROPY_PERSISTENT_CODE_SAVE

tools/mpy_ld.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -765,14 +765,6 @@ def write_qstr(self, s):
765765
self.write_uint(len(s) << 1)
766766
self.write_bytes(s)
767767

768-
def write_obj(self, o):
769-
if o is Ellipsis:
770-
self.write_bytes(b'e')
771-
return
772-
else:
773-
# Other Python types not implemented
774-
assert 0
775-
776768
def write_reloc(self, base, offset, dest, n):
777769
need_offset = not (base == self.prev_base and offset == self.prev_offset + 1)
778770
self.prev_offset = offset + n - 1
@@ -845,17 +837,11 @@ def build_mpy(env, entry_offset, fmpy, native_qstr_vals, native_qstr_objs):
845837
out.write_uint(scope_flags)
846838

847839
# MPY: n_obj
848-
out.write_uint(bool(len(env.full_rodata)) + bool(len(env.full_bss)))
840+
out.write_uint(0)
849841

850842
# MPY: n_raw_code
851843
out.write_uint(0)
852844

853-
# MPY: optional bytes object with rodata
854-
if len(env.full_rodata):
855-
out.write_obj(Ellipsis)
856-
if len(env.full_bss):
857-
out.write_obj(Ellipsis)
858-
859845
# MPY: rodata and/or bss
860846
if len(env.full_rodata):
861847
rodata_const_table_idx = 1

0 commit comments

Comments
 (0)