py/emitnative: Put a pointer to the native prelude in child_table array.

Some architectures (like esp32 xtensa) cannot read byte-wise from
executable memory.  This means the prelude for native functions -- which is
usually located after the machine code for the native function -- must be
placed in separate memory that can be read byte-wise.  Prior to this commit
this was achieved by enabling N_PRELUDE_AS_BYTES_OBJ for the emitter and
MICROPY_EMIT_NATIVE_PRELUDE_AS_BYTES_OBJ for the runtime.  The prelude was
then placed in a bytes object, pointed to by the module's constant table.

This behaviour is changed by this commit so that a pointer to the prelude
is stored either in mp_obj_fun_bc_t.child_table, or in
mp_obj_fun_bc_t.child_table[num_children] if num_children > 0.  The reasons
for doing this are:

1. It decouples the native emitter from runtime requirements, the emitted
   code no longer needs to know if the system it runs on can/can't read
   byte-wise from executable memory.

2. It makes all ports have the same emitter behaviour, there is no longer
   the N_PRELUDE_AS_BYTES_OBJ option.

3. The module's constant table is now used only for actual constants in the
   Python code.  This allows further optimisations to be done with the
   constants (eg constant deduplication).

Code size change for those ports that enable the native emitter:
   unix x64:   +80 +0.015%
      stm32:   +24 +0.004% PYBV10
    esp8266:   +88 +0.013% GENERIC
      esp32:   -20 -0.002% GENERIC[incl -112(data)]
        rp2:   +32 +0.005% PICO

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George
2022-05-10 13:56:24 +10:00
parent 8725a32f41
commit 1fb01bd6c5
7 changed files with 93 additions and 83 deletions

View File

@@ -348,7 +348,7 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader) {
// Load children if any.
if (has_children) {
n_children = read_uint(reader);
children = m_new(mp_raw_code_t *, n_children);
children = m_new(mp_raw_code_t *, n_children + (kind == MP_CODE_NATIVE_PY));
for (size_t i = 0; i < n_children; ++i) {
children[i] = load_raw_code(reader);
}
@@ -372,6 +372,17 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader) {
#if MICROPY_EMIT_MACHINE_CODE
} else {
const uint8_t *prelude_ptr;
#if MICROPY_EMIT_NATIVE_PRELUDE_SEPARATE_FROM_MACHINE_CODE
if (kind == MP_CODE_NATIVE_PY) {
// Executable code cannot be accessed byte-wise on this architecture, so copy
// the prelude to a separate memory region that is byte-wise readable.
void *buf = fun_data + prelude_offset;
size_t n = fun_data_len - prelude_offset;
prelude_ptr = memcpy(m_new(uint8_t, n), buf, n);
}
#endif
// Relocate and commit code to executable address space
reloc_info_t ri = {reader, rodata, bss};
#if defined(MP_PLAT_COMMIT_EXEC)
@@ -395,6 +406,17 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader) {
}
#endif
if (kind == MP_CODE_NATIVE_PY) {
#if !MICROPY_EMIT_NATIVE_PRELUDE_SEPARATE_FROM_MACHINE_CODE
prelude_ptr = fun_data + prelude_offset;
#endif
if (n_children == 0) {
children = (void *)prelude_ptr;
} else {
children[n_children] = (void *)prelude_ptr;
}
}
// Assign native code to raw code object
mp_emit_glue_assign_native(rc, kind,
fun_data, fun_data_len,