py/emitnative: Let emitters know the compiled entity's name.
Some checks failed
JavaScript code lint and formatting with Biome / eslint (push) Has been cancelled
Check code formatting / code-formatting (push) Has been cancelled
Check spelling with codespell / codespell (push) Has been cancelled
Build docs / build (push) Has been cancelled
Check examples / embedding (push) Has been cancelled
Package mpremote / build (push) Has been cancelled
.mpy file format and tools / test (push) Has been cancelled
Build ports metadata / build (push) Has been cancelled
alif port / build_alif (alif_ae3_build) (push) Has been cancelled
cc3200 port / build (push) Has been cancelled
esp32 port / build_idf (esp32_build_cmod_spiram_s2) (push) Has been cancelled
esp32 port / build_idf (esp32_build_s3_c3) (push) Has been cancelled
esp8266 port / build (push) Has been cancelled
mimxrt port / build (push) Has been cancelled
nrf port / build (push) Has been cancelled
powerpc port / build (push) Has been cancelled
qemu port / build_and_test_arm (bigendian) (push) Has been cancelled
qemu port / build_and_test_arm (sabrelite) (push) Has been cancelled
qemu port / build_and_test_arm (thumb) (push) Has been cancelled
qemu port / build_and_test_rv32 (push) Has been cancelled
renesas-ra port / build_renesas_ra_board (push) Has been cancelled
rp2 port / build (push) Has been cancelled
samd port / build (push) Has been cancelled
stm32 port / build_stm32 (stm32_misc_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_nucleo_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_pyb_build) (push) Has been cancelled
unix port / minimal (push) Has been cancelled
unix port / reproducible (push) Has been cancelled
unix port / standard (push) Has been cancelled
unix port / standard_v2 (push) Has been cancelled
unix port / coverage (push) Has been cancelled
unix port / coverage_32bit (push) Has been cancelled
unix port / nanbox (push) Has been cancelled
unix port / float (push) Has been cancelled
unix port / stackless_clang (push) Has been cancelled
unix port / float_clang (push) Has been cancelled
unix port / settrace (push) Has been cancelled
unix port / settrace_stackless (push) Has been cancelled
unix port / macos (push) Has been cancelled
unix port / qemu_mips (push) Has been cancelled
unix port / qemu_arm (push) Has been cancelled
unix port / qemu_riscv64 (push) Has been cancelled
unix port / sanitize_address (push) Has been cancelled
unix port / sanitize_undefined (push) Has been cancelled
webassembly port / build (push) Has been cancelled
windows port / build-vs (Debug, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x64, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x64, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x86, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x86, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x86, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, dev) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, standard) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, dev) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, standard) (push) Has been cancelled
windows port / cross-build-on-linux (push) Has been cancelled
zephyr port / build (push) Has been cancelled
Python code lint and formatting with ruff / ruff (push) Has been cancelled

This commit introduces an optional feature to provide to native emitters
the fully qualified name of the entity they are compiling.

This is achieved by altering the generic ASM API to provide a third
argument to the entry function, containing the name of the entity being
compiled.  Currently only the debug emitter uses this feature, as it is
not really useful for other emitters for the time being; in fact the
macros in question just strip the name away.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
This commit is contained in:
Alessandro Gatti
2025-06-26 21:56:26 +02:00
committed by Damien George
parent 3a97175f5f
commit 2ab06b61b3
9 changed files with 67 additions and 34 deletions

View File

@@ -164,12 +164,12 @@ void asm_arm_bx_reg(asm_arm_t *as, uint reg_src);
// Holds a pointer to mp_fun_table
#define REG_FUN_TABLE ASM_ARM_REG_FUN_TABLE
#define ASM_T asm_arm_t
#define ASM_END_PASS asm_arm_end_pass
#define ASM_ENTRY asm_arm_entry
#define ASM_EXIT asm_arm_exit
#define ASM_T asm_arm_t
#define ASM_END_PASS asm_arm_end_pass
#define ASM_ENTRY(as, num_locals, name) asm_arm_entry((as), (num_locals))
#define ASM_EXIT asm_arm_exit
#define ASM_JUMP asm_arm_b_label
#define ASM_JUMP asm_arm_b_label
#define ASM_JUMP_IF_REG_ZERO(as, reg, label, bool_test) \
do { \
asm_arm_cmp_reg_i8(as, reg, 0); \

View File

@@ -718,7 +718,7 @@ void asm_rv32_emit_optimised_xor(asm_rv32_t *state, mp_uint_t rd, mp_uint_t rs);
void asm_rv32_emit_store_reg_reg_offset(asm_rv32_t *state, mp_uint_t source, mp_uint_t base, int32_t offset, mp_uint_t operation_size);
#define ASM_T asm_rv32_t
#define ASM_ENTRY(state, labels) asm_rv32_entry(state, labels)
#define ASM_ENTRY(state, labels, name) asm_rv32_entry(state, labels)
#define ASM_EXIT(state) asm_rv32_exit(state)
#define ASM_END_PASS(state) asm_rv32_end_pass(state)

View File

@@ -403,12 +403,12 @@ void asm_thumb_b_rel12(asm_thumb_t *as, int rel);
#define REG_FUN_TABLE ASM_THUMB_REG_FUN_TABLE
#define ASM_T asm_thumb_t
#define ASM_END_PASS asm_thumb_end_pass
#define ASM_ENTRY asm_thumb_entry
#define ASM_EXIT asm_thumb_exit
#define ASM_T asm_thumb_t
#define ASM_END_PASS asm_thumb_end_pass
#define ASM_ENTRY(as, num_locals, name) asm_thumb_entry((as), (num_locals))
#define ASM_EXIT asm_thumb_exit
#define ASM_JUMP asm_thumb_b_label
#define ASM_JUMP asm_thumb_b_label
#define ASM_JUMP_IF_REG_ZERO(as, reg, label, bool_test) \
do { \
asm_thumb_cmp_rlo_i8(as, reg, 0); \

View File

@@ -154,12 +154,12 @@ void asm_x64_call_ind(asm_x64_t *as, size_t fun_id, int temp_r32);
// Holds a pointer to mp_fun_table
#define REG_FUN_TABLE ASM_X64_REG_FUN_TABLE
#define ASM_T asm_x64_t
#define ASM_END_PASS asm_x64_end_pass
#define ASM_ENTRY asm_x64_entry
#define ASM_EXIT asm_x64_exit
#define ASM_T asm_x64_t
#define ASM_END_PASS asm_x64_end_pass
#define ASM_ENTRY(as, num_locals, name) asm_x64_entry((as), (num_locals))
#define ASM_EXIT asm_x64_exit
#define ASM_JUMP asm_x64_jmp_label
#define ASM_JUMP asm_x64_jmp_label
#define ASM_JUMP_IF_REG_ZERO(as, reg, label, bool_test) \
do { \
if (bool_test) { \

View File

@@ -149,12 +149,12 @@ void asm_x86_call_ind(asm_x86_t *as, size_t fun_id, mp_uint_t n_args, int temp_r
// Holds a pointer to mp_fun_table
#define REG_FUN_TABLE ASM_X86_REG_FUN_TABLE
#define ASM_T asm_x86_t
#define ASM_END_PASS asm_x86_end_pass
#define ASM_ENTRY asm_x86_entry
#define ASM_EXIT asm_x86_exit
#define ASM_T asm_x86_t
#define ASM_END_PASS asm_x86_end_pass
#define ASM_ENTRY(as, num_locals, name) asm_x86_entry((as), (num_locals))
#define ASM_EXIT asm_x86_exit
#define ASM_JUMP asm_x86_jmp_label
#define ASM_JUMP asm_x86_jmp_label
#define ASM_JUMP_IF_REG_ZERO(as, reg, label, bool_test) \
do { \
if (bool_test) { \

View File

@@ -340,9 +340,9 @@ void asm_xtensa_l32r(asm_xtensa_t *as, mp_uint_t reg, mp_uint_t label);
#define ASM_NUM_REGS_SAVED ASM_XTENSA_NUM_REGS_SAVED
#define REG_FUN_TABLE ASM_XTENSA_REG_FUN_TABLE
#define ASM_ENTRY(as, nlocal) asm_xtensa_entry((as), (nlocal))
#define ASM_EXIT(as) asm_xtensa_exit((as))
#define ASM_CALL_IND(as, idx) asm_xtensa_call_ind((as), (idx))
#define ASM_ENTRY(as, nlocal, name) asm_xtensa_entry((as), (nlocal))
#define ASM_EXIT(as) asm_xtensa_exit((as))
#define ASM_CALL_IND(as, idx) asm_xtensa_call_ind((as), (idx))
#else
// Configuration for windowed calls with window size 8
@@ -370,9 +370,9 @@ void asm_xtensa_l32r(asm_xtensa_t *as, mp_uint_t reg, mp_uint_t label);
#define ASM_NUM_REGS_SAVED ASM_XTENSA_NUM_REGS_SAVED_WIN
#define REG_FUN_TABLE ASM_XTENSA_REG_FUN_TABLE_WIN
#define ASM_ENTRY(as, nlocal) asm_xtensa_entry_win((as), (nlocal))
#define ASM_EXIT(as) asm_xtensa_exit_win((as))
#define ASM_CALL_IND(as, idx) asm_xtensa_call_ind_win((as), (idx))
#define ASM_ENTRY(as, nlocal, name) asm_xtensa_entry_win((as), (nlocal))
#define ASM_EXIT(as) asm_xtensa_exit_win((as))
#define ASM_CALL_IND(as, idx) asm_xtensa_call_ind_win((as), (idx))
#endif

View File

@@ -419,6 +419,30 @@ static void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
emit->stack_info[i].vtype = VTYPE_UNBOUND;
}
char *qualified_name = NULL;
#if N_DEBUG
scope_t *current_scope = scope;
vstr_t *qualified_name_vstr = vstr_new(qstr_len(current_scope->simple_name));
size_t fragment_length = 0;
const byte *fragment_pointer;
for (;;) {
fragment_pointer = qstr_data(current_scope->simple_name, &fragment_length);
vstr_hint_size(qualified_name_vstr, fragment_length);
memmove(qualified_name_vstr->buf + fragment_length, qualified_name_vstr->buf, qualified_name_vstr->len);
memcpy(qualified_name_vstr->buf, fragment_pointer, fragment_length);
qualified_name_vstr->len += fragment_length;
if (current_scope->parent == NULL || current_scope->parent->simple_name == MP_QSTR__lt_module_gt_) {
break;
}
vstr_ins_char(qualified_name_vstr, 0, '.');
current_scope = current_scope->parent;
}
qualified_name = vstr_null_terminated_str(qualified_name_vstr);
#else
(void)qualified_name;
#endif
mp_asm_base_start_pass(&emit->as->base, pass == MP_PASS_EMIT ? MP_ASM_PASS_EMIT : MP_ASM_PASS_COMPUTE);
// generate code for entry to function
@@ -465,7 +489,7 @@ static void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
}
// Entry to function
ASM_ENTRY(emit->as, emit->stack_start + emit->n_state - num_locals_in_regs);
ASM_ENTRY(emit->as, emit->stack_start + emit->n_state - num_locals_in_regs, qualified_name);
#if N_X86
asm_x86_mov_arg_to_r32(emit->as, 0, REG_PARENT_ARG_1);
@@ -536,7 +560,7 @@ static void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
if (emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
mp_asm_base_data(&emit->as->base, ASM_WORD_SIZE, (uintptr_t)emit->start_offset);
ASM_ENTRY(emit->as, emit->code_state_start);
ASM_ENTRY(emit->as, emit->code_state_start, qualified_name);
// Reset the state size for the state pointed to by REG_GENERATOR_STATE
emit->code_state_start = 0;
@@ -568,7 +592,7 @@ static void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
emit->stack_start = emit->code_state_start + SIZEOF_CODE_STATE;
// Allocate space on C-stack for code_state structure, which includes state
ASM_ENTRY(emit->as, emit->stack_start + emit->n_state);
ASM_ENTRY(emit->as, emit->stack_start + emit->n_state, qualified_name);
// Prepare incoming arguments for call to mp_setup_code_state
@@ -634,6 +658,10 @@ static void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
}
}
}
#if N_DEBUG
vstr_free(qualified_name_vstr);
#endif
}
static inline void emit_native_write_code_info_byte(emit_t *emit, byte val) {

View File

@@ -108,8 +108,8 @@ static void asm_debug_end_pass(asm_debug_t *as) {
(void)as;
}
static void asm_debug_entry(asm_debug_t *as, int num_locals) {
asm_debug_printf(as, "ENTRY(num_locals=%d)\n", num_locals);
static void asm_debug_entry(asm_debug_t *as, int num_locals, char *name) {
asm_debug_printf(as, "ENTRY(%s, num_locals=%d)\n", name != NULL ? name : "?", num_locals);
}
static void asm_debug_exit(asm_debug_t *as) {
@@ -195,8 +195,8 @@ static void asm_debug_setcc_reg_reg_reg(asm_debug_t *as, int op, int reg1, int r
#define ASM_T asm_debug_t
#define ASM_END_PASS asm_debug_end_pass
#define ASM_ENTRY(as, num_locals) \
asm_debug_entry(as, num_locals)
#define ASM_ENTRY(as, num_locals, name) \
asm_debug_entry(as, num_locals, name)
#define ASM_EXIT(as) \
asm_debug_exit(as)

View File

@@ -426,6 +426,11 @@
#define MICROPY_EMIT_INLINE_RV32 (0)
#endif
// Whether to enable the human-readable native instructions emitter
#ifndef MICROPY_EMIT_NATIVE_DEBUG
#define MICROPY_EMIT_NATIVE_DEBUG (0)
#endif
// Convenience definition for whether any native emitter is enabled
#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_X86 || MICROPY_EMIT_THUMB || MICROPY_EMIT_ARM || MICROPY_EMIT_XTENSA || MICROPY_EMIT_XTENSAWIN || MICROPY_EMIT_RV32 || MICROPY_EMIT_NATIVE_DEBUG)