py/emitnative: Load native fun table ptr from const table for all archs. · csamuelson/circuitpython@0066710 · GitHub
Skip to content

Commit 0066710

Browse files
committed
py/emitnative: Load native fun table ptr from const table for all archs.
All architectures now have a dedicated register to hold the pointer to the native function table mp_fun_table, and so they all need to load this register at the start of the native function. This commit makes the loading of this register uniform across architectures by passing the pointer in the constant table for the native function, and then loading the register from the constant table. Doing it this way means that the pointer is not stored in the assembly code, helping to make the code more portable.
1 parent 355eb8e commit 0066710

10 files changed

Lines changed: 54 additions & 36 deletions

File tree

py/asmarm.h

Lines changed: 6 additions & 0 deletions

py/asmthumb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ void asm_thumb_bcc_label(asm_thumb_t *as, int cond, uint label) {
383383

384384
void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp) {
385385
// Load ptr to function from table, indexed by fun_id, then call it
386-
asm_thumb_ldr_reg_reg_i12_optimised(as, reg_temp, ASM_THUMB_REG_R7, fun_id);
386+
asm_thumb_ldr_reg_reg_i12_optimised(as, reg_temp, ASM_THUMB_REG_FUN_TABLE, fun_id);
387387
asm_thumb_op16(as, OP_BLX(reg_temp));
388388
}
389389

py/asmthumb.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ void asm_thumb_b_label(asm_thumb_t *as, uint label); // convenience: picks narro
261261
void asm_thumb_bcc_label(asm_thumb_t *as, int cc, uint label); // convenience: picks narrow or wide branch
262262
void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp); // convenience
263263

264+
// Holds a pointer to mp_fun_table
265+
#define ASM_THUMB_REG_FUN_TABLE ASM_THUMB_REG_R7
266+
264267
#if GENERIC_ASM_API
265268

266269
// The following macros provide a (mostly) arch-independent API to
@@ -284,6 +287,8 @@ void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp
284287
#define REG_LOCAL_3 ASM_THUMB_REG_R6
285288
#define REG_LOCAL_NUM (3)
286289

290+
#define REG_FUN_TABLE ASM_THUMB_REG_FUN_TABLE
291+
287292
#define ASM_T asm_thumb_t
288293
#define ASM_END_PASS asm_thumb_end_pass
289294
#define ASM_ENTRY asm_thumb_entry

py/asmx64.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ void asm_x64_call_i1(asm_x64_t *as, void* func, int i1) {
623623

624624
void asm_x64_call_ind(asm_x64_t *as, size_t fun_id, int temp_r64) {
625625
assert(temp_r64 < 8);
626-
asm_x64_mov_mem64_to_r64(as, ASM_X64_REG_RBP, fun_id * WORD_SIZE, temp_r64);
626+
asm_x64_mov_mem64_to_r64(as, ASM_X64_REG_FUN_TABLE, fun_id * WORD_SIZE, temp_r64);
627627
asm_x64_write_byte_2(as, OPCODE_CALL_RM32, MODRM_R64(2) | MODRM_RM_REG | MODRM_RM_R64(temp_r64));
628628
}
629629

py/asmx64.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ void asm_x64_mov_local_addr_to_r64(asm_x64_t* as, int local_num, int dest_r64);
116116
void asm_x64_mov_reg_pcrel(asm_x64_t *as, int dest_r64, mp_uint_t label);
117117
void asm_x64_call_ind(asm_x64_t* as, size_t fun_id, int temp_r32);
118118

119+
// Holds a pointer to mp_fun_table
120+
#define ASM_X64_REG_FUN_TABLE ASM_X64_REG_RBP
121+
119122
#if GENERIC_ASM_API
120123

121124
// The following macros provide a (mostly) arch-independent API to
@@ -141,6 +144,9 @@ void asm_x64_call_ind(asm_x64_t* as, size_t fun_id, int temp_r32);
141144
#define REG_LOCAL_3 ASM_X64_REG_R13
142145
#define REG_LOCAL_NUM (3)
143146

147+
// Holds a pointer to mp_fun_table
148+
#define REG_FUN_TABLE ASM_X64_REG_FUN_TABLE
149+
144150
#define ASM_T asm_x64_t
145151
#define ASM_END_PASS asm_x64_end_pass
146152
#define ASM_ENTRY asm_x64_entry

py/asmx86.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ void asm_x86_call_ind(asm_x86_t *as, size_t fun_id, mp_uint_t n_args, int temp_r
514514
}
515515

516516
// Load the pointer to the function and make the call
517-
asm_x86_mov_mem32_to_r32(as, ASM_X86_REG_EBP, fun_id * WORD_SIZE, temp_r32);
517+
asm_x86_mov_mem32_to_r32(as, ASM_X86_REG_FUN_TABLE, fun_id * WORD_SIZE, temp_r32);
518518
asm_x86_write_byte_2(as, OPCODE_CALL_RM32, MODRM_R32(2) | MODRM_RM_REG | MODRM_RM_R32(temp_r32));
519519

520520
// the caller must clean up the stack

py/asmx86.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ void asm_x86_mov_local_addr_to_r32(asm_x86_t* as, int local_num, int dest_r32);
114114
void asm_x86_mov_reg_pcrel(asm_x86_t *as, int dest_r64, mp_uint_t label);
115115
void asm_x86_call_ind(asm_x86_t* as, size_t fun_id, mp_uint_t n_args, int temp_r32);
116116

117+
// Holds a pointer to mp_fun_table
118+
#define ASM_X86_REG_FUN_TABLE ASM_X86_REG_EBP
119+
117120
#if GENERIC_ASM_API
118121

119122
// The following macros provide a (mostly) arch-independent API to
@@ -139,6 +142,9 @@ void asm_x86_call_ind(asm_x86_t* as, size_t fun_id, mp_uint_t n_args, int temp_r
139142
#define REG_LOCAL_3 ASM_X86_REG_EDI
140143
#define REG_LOCAL_NUM (3)
141144

145+
// Holds a pointer to mp_fun_table
146+
#define REG_FUN_TABLE ASM_X86_REG_FUN_TABLE
147+
142148
#define ASM_T asm_x86_t
143149
#define ASM_END_PASS asm_x86_end_pass
144150
#define ASM_ENTRY asm_x86_entry

py/asmxtensa.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,9 @@ void asm_xtensa_mov_reg_pcrel(asm_xtensa_t *as, uint reg_dest, uint label) {
213213

214214
void asm_xtensa_call_ind(asm_xtensa_t *as, uint idx) {
215215
if (idx < 16) {
216-
asm_xtensa_op_l32i_n(as, ASM_XTENSA_REG_A0, ASM_XTENSA_REG_A15, idx);
216+
asm_xtensa_op_l32i_n(as, ASM_XTENSA_REG_A0, ASM_XTENSA_REG_FUN_TABLE, idx);
217217
} else {
218-
asm_xtensa_op_l32i(as, ASM_XTENSA_REG_A0, ASM_XTENSA_REG_A15, idx);
218+
asm_xtensa_op_l32i(as, ASM_XTENSA_REG_A0, ASM_XTENSA_REG_FUN_TABLE, idx);
219219
}
220220
asm_xtensa_op_callx0(as, ASM_XTENSA_REG_A0);
221221
}

py/asmxtensa.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ void asm_xtensa_mov_reg_local_addr(asm_xtensa_t *as, uint reg_dest, int local_nu
245245
void asm_xtensa_mov_reg_pcrel(asm_xtensa_t *as, uint reg_dest, uint label);
246246
void asm_xtensa_call_ind(asm_xtensa_t *as, uint idx);
247247

248+
// Holds a pointer to mp_fun_table
249+
#define ASM_XTENSA_REG_FUN_TABLE ASM_XTENSA_REG_A15
250+
248251
#if GENERIC_ASM_API
249252

250253
// The following macros provide a (mostly) arch-independent API to
@@ -268,6 +271,8 @@ void asm_xtensa_call_ind(asm_xtensa_t *as, uint idx);
268271
#define REG_LOCAL_3 ASM_XTENSA_REG_A14
269272
#define REG_LOCAL_NUM (3)
270273

274+
#define REG_FUN_TABLE ASM_XTENSA_REG_FUN_TABLE
275+
271276
#define ASM_T asm_xtensa_t
272277
#define ASM_END_PASS asm_xtensa_end_pass
273278
#define ASM_ENTRY asm_xtensa_entry

py/emitnative.c

Lines changed: 21 additions & 31 deletions

0 commit comments

Comments
 (0)