@@ -461,6 +461,25 @@ void patch_x86_64_trampoline(unsigned char *location, int ordinal, jit_state *st
461461 #define DATA_ALIGN 1
462462#endif
463463
464+ // Get the trampoline memory location for a given symbol ordinal.
465+ static unsigned char *
466+ get_trampoline_slot (int ordinal , jit_state * state )
467+ {
468+ const uint32_t symbol_mask = 1 << (ordinal % 32 );
469+ const uint32_t trampoline_mask = state -> trampolines .mask [ordinal / 32 ];
470+ assert (symbol_mask & trampoline_mask );
471+
472+ // Count the number of set bits in the trampoline mask lower than ordinal
473+ int index = _Py_popcount32 (trampoline_mask & (symbol_mask - 1 ));
474+ for (int i = 0 ; i < ordinal / 32 ; i ++ ) {
475+ index += _Py_popcount32 (state -> trampolines .mask [i ]);
476+ }
477+
478+ unsigned char * trampoline = state -> trampolines .mem + index * TRAMPOLINE_SIZE ;
479+ assert ((size_t )(index + 1 ) * TRAMPOLINE_SIZE <= state -> trampolines .size );
480+ return trampoline ;
481+ }
482+
464483// Generate and patch AArch64 trampolines. The symbols to jump to are stored
465484// in the jit_stencils.h in the symbols_map.
466485void
@@ -477,20 +496,8 @@ patch_aarch64_trampoline(unsigned char *location, int ordinal, jit_state *state)
477496 return ;
478497 }
479498
480- // Masking is done modulo 32 as the mask is stored as an array of uint32_t
481- const uint32_t symbol_mask = 1 << (ordinal % 32 );
482- const uint32_t trampoline_mask = state -> trampolines .mask [ordinal / 32 ];
483- assert (symbol_mask & trampoline_mask );
484-
485- // Count the number of set bits in the trampoline mask lower than ordinal,
486- // this gives the index into the array of trampolines.
487- int index = _Py_popcount32 (trampoline_mask & (symbol_mask - 1 ));
488- for (int i = 0 ; i < ordinal / 32 ; i ++ ) {
489- index += _Py_popcount32 (state -> trampolines .mask [i ]);
490- }
491-
492- uint32_t * p = (uint32_t * )(state -> trampolines .mem + index * TRAMPOLINE_SIZE );
493- assert ((size_t )(index + 1 ) * TRAMPOLINE_SIZE <= state -> trampolines .size );
499+ // Out of range - need a trampoline
500+ uint32_t * p = (uint32_t * )get_trampoline_slot (ordinal , state );
494501
495502
496503 /* Generate the trampoline
@@ -521,18 +528,7 @@ patch_x86_64_trampoline(unsigned char *location, int ordinal, jit_state *state)
521528 }
522529
523530 // Out of range - need a trampoline
524- const uint32_t symbol_mask = 1 << (ordinal % 32 );
525- const uint32_t trampoline_mask = state -> trampolines .mask [ordinal / 32 ];
526- assert (symbol_mask & trampoline_mask );
527-
528- // Count the number of set bits in the trampoline mask lower than ordinal
529- int index = _Py_popcount32 (trampoline_mask & (symbol_mask - 1 ));
530- for (int i = 0 ; i < ordinal / 32 ; i ++ ) {
531- index += _Py_popcount32 (state -> trampolines .mask [i ]);
532- }
533-
534- unsigned char * trampoline = state -> trampolines .mem + index * TRAMPOLINE_SIZE ;
535- assert ((size_t )(index + 1 ) * TRAMPOLINE_SIZE <= state -> trampolines .size );
531+ unsigned char * trampoline = get_trampoline_slot (ordinal , state );
536532
537533 /* Generate the trampoline (14 bytes, padded to 16):
538534 0: ff 25 00 00 00 00 jmp *(%rip)
0 commit comments