
    shB                        S r SSKJr  SSKJrJr  SSKJrJr  \R                  " \R                  5      r
\R                  " \R                  " S5      5      r\R                  " \
\\\\
/5      r\R                  " \R                   " 5       \/5      r\R                  " \\/5      rS rS rS rSrS	 rS
 rS rS rS rg)z%
Dynamically generate the NRT module
    )config)typescgutils)irbinding   c                 X   [         R                  " U [        S5      n[        R                  " UR                  5       5      nUR                  u  nUR                  U[        R                  5       5      nUR                  [         R                  " X$SS5      5      nUR                  U5        g)zh
Implement NRT_MemInfo_data_fast in the module.  This allows LLVM
to inline lookup of the data pointer.
NRT_MemInfo_data_fastr      N)r   get_or_insert_functionmeminfo_data_tyr   	IRBuilderappend_basic_blockargsbitcast_meminfo_struct_type
as_pointerloadgepret)modulefnbuilderptr
struct_ptrdata_ptrs         p/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/numba/core/runtime/nrtdynmod.py_define_nrt_meminfo_datar      s    
 
	'	'(?
ABll20023GGGES&:&E&E&GHJ||GKKQBCHKK    c                    [         R                  " U [        S5      nUR                  R	                  S5        [
        R                  " UR                  5       5      nUR                  u  nUR                  SU[         R                  " UR                  5      5      n[         R                  " X55         UR                  5         SSS5        UR                  XAR                  S   R                  5      n[        R                   (       a(  [         R"                  " USUR%                  U5      U5        UR'                  X/5        UR                  5         g! , (       d  f       N= f)z$
Implement NRT_incref in the module

NRT_increfnoinline==Nr   z*** NRT_Incref %zu [%p]
)r   r   incref_decref_ty
attributesaddr   r   r   r   icmp_unsignedget_null_valuetypeif_unlikelyret_voidr   r   	DEBUG_NRTprintfr   call)r   atomic_incr	fn_increfr   r   is_nullword_ptrs          r   _define_nrt_increfr3   )   s    ..v7G.:<I Z(ll9779:GNNES##D#w/E/Echh/OPG			W	. 
/ s$4$4Q$7$<$<=Hw ;W\\(=S	LLj) 
/	.s   1E
E c                 |   [         R                  " U [        S5      nUR                  R	                  S5        [
        R                  " U [
        R                  " [
        R                  " 5       [        /5      SS9n[
        R                  " UR                  5       5      nUR                  u  nUR                  SU[         R                  " UR                  5      5      n[         R                   " XF5         UR#                  5         SSS5        UR%                  S5        UR'                  XQR                  S   R                  5      n[(        R*                  (       a(  [         R,                  " US	UR/                  U5      U5        UR1                  UU/5      nUR                  SU[
        R2                  " UR                  S5      5      n	[         R                   " XI5         UR%                  S
5        UR1                  X5/5        SSS5        UR#                  5         g! , (       d  f       GN= f! , (       d  f       N1= f)z$
Implement NRT_decref in the module

NRT_decrefr"   NRT_MemInfo_call_dtornamer#   Nreleaser   z*** NRT_Decref %zu [%p]
acquire)r   r   r$   r%   r&   r   FunctionFunctionTypeVoidType_pointer_typer   r   r   r'   r(   r)   r*   r+   fencer   r   r,   r-   r   r.   Constant)
r   atomic_decr	fn_decrefcalldtorr   r   r1   r2   newrefct
refct_eq_0s
             r   _define_nrt_decrefrF   ?   s    ..v7G/;=I Z({{6??2;;==/J 79H ll9779:GNNES##D#w/E/Echh/OPG			W	. 
/ MM)s$4$4Q$7$<$<=Hw ;W\\(=S	||K%J(H &&tX'){{8==!'DFJ			W	1 	i Xu%	 
2
 3 
/	.( 
2	1s   4H$H-
H*-
H;c                 N   [         R                  " [        [        R                  5       /5      n[         R                  " XSR                  U5      S9nUR                  u  nUR                  5       n[         R                  " U5      n[         R                  " [        S5      n[        (       d4  UR                  XXS9n	[        Xq5      " X5      n
UR                  U
5        U$ UR                  U5      n	[        Xq5      " X5      nUR                  X5        UR                  U	5        U$ )zDefine a llvm function for atomic increment/decrement to the given module
Argument ``op`` is the operation "add"/"sub".  Argument ``ordering`` is
the memory ordering.  The generated function returns the new value.
znrt_atomic_{0}r7      ordering)r   r<   
_word_typer   r;   formatr   r   r   r@   _disable_atomicity
atomic_rmwgetattrr   r   store)r   oprJ   ftype	fn_atomicr   bbr   ONEoldvalresnewvals               r   _define_atomic_inc_decrY   o   s    
 OOJ)>)>)@(ABEF0@0G0G0KLINNES		%	%	'Bll2G
++j!
$C##BS#D g"6/C  c"%f2f"Fr   c                    [         R                  " [         R                  " S5      [        R	                  5       [        [        [        R	                  5       /5      n[         R
                  " XSS9nUR                  u  pEpgUR                  5       n[         R                  " U5      n	U	R                  XEXaS9n
[        R                  " XS5      u  pU	R                  X5        U	R                  U	R                  XR                  5      5        U$ )a  Define a llvm function for atomic compare-and-swap.
The generated function is a direct wrapper of the LLVM cmpxchg with the
difference that the a int indicate success (1) or failure (0) is returned
and the last argument is a output pointer for storing the old value.

Note
----
On failure, the generated function behaves like an atomic load.  The loaded
value is stored to the last argument.
    nrt_atomic_casr7   rI      )r   r<   IntTyperK   r   r;   r   r   r   cmpxchgr   unpack_tuplerP   r   zextreturn_type)r   rJ   rR   fn_casr   cmpreploldptrrT   r   outtupoldoks                r   _define_atomic_casrj      s     OOBJJrNZ-B-B-D-7-7-B-B-D-F GE [[-=>F%{{St		"	"	$Bll2G__St_?F""7A6GCMM#KKR!2!234Mr   c                    U R                   R                  [        R                  S5      n[        R
                  " XSS9nUR                  5       n[        R                  " U5      nSnU R                   R                  U[        U45        U$ )z
Defines an abort function due to unresolved symbol.

The function takes no args and will always raise an exception.
It should be safe to call this function with incorrect number of arguments.
 nrt_unresolved_abortr7   z6numba jitted function aborted due to unresolved symbol)
	call_convget_function_typer   noner   r;   r   r   return_user_excRuntimeError)ctxr   fntyr   rT   r   msgs          r   _define_nrt_unresolved_abortrv      sm     ==**5::r:D	V(>	?B				 Bll2G
BCMM!!'<#@Ir   c                    U R                  5       nUR                  S5      nUR                  S5      n[        USSS9n[        USSS9n[	        USS9  [        U5        [        X45        [        X55        [        X5        X24$ )z`
Create an IR module defining the LLVM NRT functions.
A (IR module, library) tuple is returned.
nrt
nrt_moduler&   	monotonicrI   sub)	codegencreate_librarycreate_ir_modulerY   rj   r   r3   rF   rv   )rs   r|   libraryir_mod
atomic_inc
atomic_decs         r   create_nrt_moduler      s    
 kkmG$$U+G %%l3F'LJ'LJv4V$v*v* -?r   c                 b    [        U 5      u  pUR                  U5        UR                  5         U$ )z}
Compile all LLVM NRT functions and return a library containing them.
The library is created using the given target context.
)r   add_ir_modulefinalize)rs   r   r   s      r   compile_nrt_functionsr      s/    
 (,OF&!Nr   N)__doc__
numba.corer   r   r   llvmliter   r   r^   MACHINE_BITSrK   PointerTyper>   LiteralStructTyper   r<   r=   r$   r   r   r3   rF   rM   rY   rj   rv   r   r   rl   r   r   <module>r      s   
  %   ZZ++,
rzz!}-++-   ??2;;==/B //--A,(Z  66 0
r   