Skip to content

Commit 2ab06b6

Browse files
agattidpgeorge
authored andcommitted
py/emitnative: Let emitters know the compiled entity's name.
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>
1 parent 3a97175 commit 2ab06b6

File tree

9 files changed

+67
-34
lines changed

9 files changed

+67
-34
lines changed

py/asmarm.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,12 @@ void asm_arm_bx_reg(asm_arm_t *as, uint reg_src);
164164
// Holds a pointer to mp_fun_table
165165
#define REG_FUN_TABLE ASM_ARM_REG_FUN_TABLE
166166

167-
#define ASM_T asm_arm_t
168-
#define ASM_END_PASS asm_arm_end_pass
169-
#define ASM_ENTRY asm_arm_entry
170-
#define ASM_EXIT asm_arm_exit
167+
#define ASM_T asm_arm_t
168+
#define ASM_END_PASS asm_arm_end_pass
169+
#define ASM_ENTRY(as, num_locals, name) asm_arm_entry((as), (num_locals))
170+
#define ASM_EXIT asm_arm_exit
171171

172-
#define ASM_JUMP asm_arm_b_label
172+
#define ASM_JUMP asm_arm_b_label
173173
#define ASM_JUMP_IF_REG_ZERO(as, reg, label, bool_test) \
174174
do { \
175175
asm_arm_cmp_reg_i8(as, reg, 0); \

py/asmrv32.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ void asm_rv32_emit_optimised_xor(asm_rv32_t *state, mp_uint_t rd, mp_uint_t rs);
718718
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);
719719

720720
#define ASM_T asm_rv32_t
721-
#define ASM_ENTRY(state, labels) asm_rv32_entry(state, labels)
721+
#define ASM_ENTRY(state, labels, name) asm_rv32_entry(state, labels)
722722
#define ASM_EXIT(state) asm_rv32_exit(state)
723723
#define ASM_END_PASS(state) asm_rv32_end_pass(state)
724724

py/asmthumb.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -403,12 +403,12 @@ void asm_thumb_b_rel12(asm_thumb_t *as, int rel);
403403

404404
#define REG_FUN_TABLE ASM_THUMB_REG_FUN_TABLE
405405

406-
#define ASM_T asm_thumb_t
407-
#define ASM_END_PASS asm_thumb_end_pass
408-
#define ASM_ENTRY asm_thumb_entry
409-
#define ASM_EXIT asm_thumb_exit
406+
#define ASM_T asm_thumb_t
407+
#define ASM_END_PASS asm_thumb_end_pass
408+
#define ASM_ENTRY(as, num_locals, name) asm_thumb_entry((as), (num_locals))
409+
#define ASM_EXIT asm_thumb_exit
410410

411-
#define ASM_JUMP asm_thumb_b_label
411+
#define ASM_JUMP asm_thumb_b_label
412412
#define ASM_JUMP_IF_REG_ZERO(as, reg, label, bool_test) \
413413
do { \
414414
asm_thumb_cmp_rlo_i8(as, reg, 0); \

py/asmx64.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,12 @@ void asm_x64_call_ind(asm_x64_t *as, size_t fun_id, int temp_r32);
154154
// Holds a pointer to mp_fun_table
155155
#define REG_FUN_TABLE ASM_X64_REG_FUN_TABLE
156156

157-
#define ASM_T asm_x64_t
158-
#define ASM_END_PASS asm_x64_end_pass
159-
#define ASM_ENTRY asm_x64_entry
160-
#define ASM_EXIT asm_x64_exit
157+
#define ASM_T asm_x64_t
158+
#define ASM_END_PASS asm_x64_end_pass
159+
#define ASM_ENTRY(as, num_locals, name) asm_x64_entry((as), (num_locals))
160+
#define ASM_EXIT asm_x64_exit
161161

162-
#define ASM_JUMP asm_x64_jmp_label
162+
#define ASM_JUMP asm_x64_jmp_label
163163
#define ASM_JUMP_IF_REG_ZERO(as, reg, label, bool_test) \
164164
do { \
165165
if (bool_test) { \

py/asmx86.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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
149149
// Holds a pointer to mp_fun_table
150150
#define REG_FUN_TABLE ASM_X86_REG_FUN_TABLE
151151

152-
#define ASM_T asm_x86_t
153-
#define ASM_END_PASS asm_x86_end_pass
154-
#define ASM_ENTRY asm_x86_entry
155-
#define ASM_EXIT asm_x86_exit
152+
#define ASM_T asm_x86_t
153+
#define ASM_END_PASS asm_x86_end_pass
154+
#define ASM_ENTRY(as, num_locals, name) asm_x86_entry((as), (num_locals))
155+
#define ASM_EXIT asm_x86_exit
156156

157-
#define ASM_JUMP asm_x86_jmp_label
157+
#define ASM_JUMP asm_x86_jmp_label
158158
#define ASM_JUMP_IF_REG_ZERO(as, reg, label, bool_test) \
159159
do { \
160160
if (bool_test) { \

py/asmxtensa.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,9 @@ void asm_xtensa_l32r(asm_xtensa_t *as, mp_uint_t reg, mp_uint_t label);
340340
#define ASM_NUM_REGS_SAVED ASM_XTENSA_NUM_REGS_SAVED
341341
#define REG_FUN_TABLE ASM_XTENSA_REG_FUN_TABLE
342342

343-
#define ASM_ENTRY(as, nlocal) asm_xtensa_entry((as), (nlocal))
344-
#define ASM_EXIT(as) asm_xtensa_exit((as))
345-
#define ASM_CALL_IND(as, idx) asm_xtensa_call_ind((as), (idx))
343+
#define ASM_ENTRY(as, nlocal, name) asm_xtensa_entry((as), (nlocal))
344+
#define ASM_EXIT(as) asm_xtensa_exit((as))
345+
#define ASM_CALL_IND(as, idx) asm_xtensa_call_ind((as), (idx))
346346

347347
#else
348348
// 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);
370370
#define ASM_NUM_REGS_SAVED ASM_XTENSA_NUM_REGS_SAVED_WIN
371371
#define REG_FUN_TABLE ASM_XTENSA_REG_FUN_TABLE_WIN
372372

373-
#define ASM_ENTRY(as, nlocal) asm_xtensa_entry_win((as), (nlocal))
374-
#define ASM_EXIT(as) asm_xtensa_exit_win((as))
375-
#define ASM_CALL_IND(as, idx) asm_xtensa_call_ind_win((as), (idx))
373+
#define ASM_ENTRY(as, nlocal, name) asm_xtensa_entry_win((as), (nlocal))
374+
#define ASM_EXIT(as) asm_xtensa_exit_win((as))
375+
#define ASM_CALL_IND(as, idx) asm_xtensa_call_ind_win((as), (idx))
376376

377377
#endif
378378

py/emitnative.c

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,30 @@ static void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
419419
emit->stack_info[i].vtype = VTYPE_UNBOUND;
420420
}
421421

422+
char *qualified_name = NULL;
423+
424+
#if N_DEBUG
425+
scope_t *current_scope = scope;
426+
vstr_t *qualified_name_vstr = vstr_new(qstr_len(current_scope->simple_name));
427+
size_t fragment_length = 0;
428+
const byte *fragment_pointer;
429+
for (;;) {
430+
fragment_pointer = qstr_data(current_scope->simple_name, &fragment_length);
431+
vstr_hint_size(qualified_name_vstr, fragment_length);
432+
memmove(qualified_name_vstr->buf + fragment_length, qualified_name_vstr->buf, qualified_name_vstr->len);
433+
memcpy(qualified_name_vstr->buf, fragment_pointer, fragment_length);
434+
qualified_name_vstr->len += fragment_length;
435+
if (current_scope->parent == NULL || current_scope->parent->simple_name == MP_QSTR__lt_module_gt_) {
436+
break;
437+
}
438+
vstr_ins_char(qualified_name_vstr, 0, '.');
439+
current_scope = current_scope->parent;
440+
}
441+
qualified_name = vstr_null_terminated_str(qualified_name_vstr);
442+
#else
443+
(void)qualified_name;
444+
#endif
445+
422446
mp_asm_base_start_pass(&emit->as->base, pass == MP_PASS_EMIT ? MP_ASM_PASS_EMIT : MP_ASM_PASS_COMPUTE);
423447

424448
// 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
465489
}
466490

467491
// Entry to function
468-
ASM_ENTRY(emit->as, emit->stack_start + emit->n_state - num_locals_in_regs);
492+
ASM_ENTRY(emit->as, emit->stack_start + emit->n_state - num_locals_in_regs, qualified_name);
469493

470494
#if N_X86
471495
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
536560

537561
if (emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
538562
mp_asm_base_data(&emit->as->base, ASM_WORD_SIZE, (uintptr_t)emit->start_offset);
539-
ASM_ENTRY(emit->as, emit->code_state_start);
563+
ASM_ENTRY(emit->as, emit->code_state_start, qualified_name);
540564

541565
// Reset the state size for the state pointed to by REG_GENERATOR_STATE
542566
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
568592
emit->stack_start = emit->code_state_start + SIZEOF_CODE_STATE;
569593

570594
// Allocate space on C-stack for code_state structure, which includes state
571-
ASM_ENTRY(emit->as, emit->stack_start + emit->n_state);
595+
ASM_ENTRY(emit->as, emit->stack_start + emit->n_state, qualified_name);
572596

573597
// Prepare incoming arguments for call to mp_setup_code_state
574598

@@ -634,6 +658,10 @@ static void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
634658
}
635659
}
636660
}
661+
662+
#if N_DEBUG
663+
vstr_free(qualified_name_vstr);
664+
#endif
637665
}
638666

639667
static inline void emit_native_write_code_info_byte(emit_t *emit, byte val) {

py/emitndebug.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ static void asm_debug_end_pass(asm_debug_t *as) {
108108
(void)as;
109109
}
110110

111-
static void asm_debug_entry(asm_debug_t *as, int num_locals) {
112-
asm_debug_printf(as, "ENTRY(num_locals=%d)\n", num_locals);
111+
static void asm_debug_entry(asm_debug_t *as, int num_locals, char *name) {
112+
asm_debug_printf(as, "ENTRY(%s, num_locals=%d)\n", name != NULL ? name : "?", num_locals);
113113
}
114114

115115
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
195195

196196
#define ASM_T asm_debug_t
197197
#define ASM_END_PASS asm_debug_end_pass
198-
#define ASM_ENTRY(as, num_locals) \
199-
asm_debug_entry(as, num_locals)
198+
#define ASM_ENTRY(as, num_locals, name) \
199+
asm_debug_entry(as, num_locals, name)
200200
#define ASM_EXIT(as) \
201201
asm_debug_exit(as)
202202

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,11 @@
426426
#define MICROPY_EMIT_INLINE_RV32 (0)
427427
#endif
428428

429+
// Whether to enable the human-readable native instructions emitter
430+
#ifndef MICROPY_EMIT_NATIVE_DEBUG
431+
#define MICROPY_EMIT_NATIVE_DEBUG (0)
432+
#endif
433+
429434
// Convenience definition for whether any native emitter is enabled
430435
#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)
431436

0 commit comments

Comments
 (0)