
    xKg!                     r    d dl mZmZ d dl Zd dlmZmZmZ  G d de      Z	 G d de      Z
 G d d	e      Zy)
    )Constant	IRBuilderN)typesconfigcgutilsc                   "    e Zd ZdZd Zd Zd Zy)_ArgManagerzA
    A utility class to handle argument unboxing and cleanup
    c                 f    || _         || _        || _        || _        d| _        g | _        || _        y )Nr   )contextbuilderapienv_manager	arg_countcleanupsnextblk)selfr   r   r   r   endblknargss          Z/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/numba/core/callwrapper.py__init__z_ArgManager.__init__   s4    &    c                      j                   j                  |      t        j                   j                  j
                        5   j                  j                   j                         ddd        fd} j                  j                  |        j                  j                  d j                  z        } j                  j                  |      5   |         j                  j                   j                         ddd       | _         xj                  dz  c_        j                  S # 1 sw Y   xY w# 1 sw Y   =xY w)ak  
        Unbox argument and emit code that handles any error during unboxing.
        Args are cleaned up in reverse order of the parameter list, and
        cleanup begins as soon as unboxing of any argument fails. E.g. failure
        on arg2 will result in control flow going through:

            arg2.err -> arg1.err -> arg0.err -> arg.end (returns)
        Nc                  D   j                   j                   j                  j                          j                   j	                          j
                  j                  r<j
                  j                  j                  j                   j                         y y N)
r   reflect_native_valuevaluer   cleanupr   
enable_nrtnrtdecrefr   )nativer   tys   r   cleanup_argz(_ArgManager.add_arg.<locals>.cleanup_arg&   sq    HH))"fllD<L<LM ~~) 
 ||&&  ''b&,,G 'r   z	arg%d.err   )r   to_native_valuer   if_unlikelyr   is_errorbranchr   r   appendappend_basic_blockr   
goto_blockr   )r   objr"   r#   
cleanupblkr!   s   ` `  @r   add_argz_ArgManager.add_arg   s     ))"c2   v?LL- @	H 	[) \\44$..(*
\\$$Z0MLL- 1
 "!||= @?. 10s   
&D:-E:EEc                 4    | j                   D ]	  } |         y)zR
        Emit the cleanup code after returning from the wrapped function.
        N)r   )r   dtors     r   emit_cleanupz_ArgManager.emit_cleanupB   s     MMDF "r   N)__name__
__module____qualname____doc__r   r.   r1    r   r   r	   r	      s    ,\r   r	   c                       e Zd ZdZd Zd Zy)_GilManagerzY
    A utility class to handle releasing the GIL and then re-acquiring it
    again.
    c                 X    || _         || _        || _        |j                         | _        y r   )r   r   argmansave_threadthread_state)r   r   r   r:   s       r   r   z_GilManager.__init__P   s&    OO-r   c                     | j                   j                  | j                         | j                  j	                          y r   )r   restore_threadr<   r:   r1   )r   s    r   r1   z_GilManager.emit_cleanupV   s*     1 12  "r   N)r2   r3   r4   r5   r   r1   r6   r   r   r8   r8   J   s    
.#r   r8   c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)PyCallWrapperc                 X    || _         || _        || _        || _        || _        || _        y r   )r   modulefuncfndescenvrelease_gil)r   r   rB   rC   rD   rE   call_helperrF   s           r   r   zPyCallWrapper.__init__\   s-    	&r   c                    | j                   j                  }| j                  j                  t        j
                        }t        j                  j                  ||||g      }t        j                  j                  | j                  ||      }t        |j                  d            }|j                  \  }}}d|_        d|_        d|_        | j                  j                  |      }	| j!                  |	||||       ||	fS )N)nameentry
py_closurepy_argspy_kws)rD   llvm_cpython_wrapper_namer   get_argument_typer   pyobjectllvmliteirFunctionTypeFunctionrB   r   r*   argsrI   get_python_apibuild_wrapper)
r   wrapnamepyobjwraptywrapperr   closurerU   kwsr   s
             r   buildzPyCallWrapper.builde   s    ;;88 ..u~~>))%%1FG++&&t{{F&JG66w?@ %\\s#	ll))'23$<|r   c                    t        | j                  j                        }t        |      D cg c]  }|j	                          }} |j
                  || j                  j                  ||g| }	|j                  d|	t        |	j                  d             }
t        j                  ||
      5  |j                  |j                                d d d        |j                  d      }|j                  |      5  |j                  |j                                d d d        | j!                  ||      }t#        | j$                  |||||      }g }t'        || j                  j                        D ]c  \  }}t)        |t*        j,                        r|j/                  d        2|j1                  |j3                  |      |      }|j/                  |       e | j4                  rt7        |||      }| j$                  j8                  j;                  || j<                  | j                  j>                  | j                  j                  |d      \  }}| jA                  |d       |jC                          | jA                  |d       |jE                  |jF                  d      5  |jE                  |jH                        5  |jK                          d d d        | jM                         }|jO                  |||      }|j                  |       d d d        | j$                  j8                  jQ                  |||       |j                  |j                                y c c}w # 1 sw Y   xY w# 1 sw Y   RxY w# 1 sw Y   xY w# 1 sw Y   {xY w)	Nz==zarg.end)noinline)attrsz# callwrapper: emit_cleanupz# callwrapper: emit_cleanup endT)likely))lenrD   argtypesrange
alloca_objunpack_tuplequalnameicmp_unsignedr   typer   r&   retget_null_objectr*   r+   get_envr	   r   zip
isinstancer   Omittedr)   r.   loadrF   r8   	call_convcall_functionrC   restypedebug_printr1   if_thenis_okis_nonereturn_none_simplified_return_typefrom_native_returnraise_error)r   r   r   r\   rU   r]   r   _objsparseokpredr   r   cleanup_manager	innerargsr,   r"   valstatusretvalrettys                        r   rW   zPyCallWrapper.build_wrapper~   s   DKK(()*/,7,Q ,7"#""4)=)=#(%8268 $$W\\4(*   $/KK++-. 0 ++I6'KK++-. ( ll30%dllGS&165B 	4!5!56GC"emm,  &%--gll3.?D  % 7 )'3HO //==TYY 3 3T[[5I5I] > , 	"?@$$&"CD __V\\$_70! 1 002E((DCKK 8 	**7C@C'')*E 8 0/
 ('X 10 87sA   M- M7 M'N 9M4
<N M$'M14M=	9N  N	c                    | j                   j                  | j                        }| j                   j                  |j                  |      }|j                  |      }| j                   j                  ||      }|j                  |d| j                  j                         |j                  | j                  ||      }|S )zpGet the Environment object which is declared as a global
        in the module of the wrapped function.
        T)return_pyobject	debug_msg)r   get_env_namerD   declare_env_globalrB   rq   get_env_bodyemit_environment_sentryenv_nameget_env_managerrE   )r   r   r   envnamegvptrenvptrenv_bodyr   s           r   rm   zPyCallWrapper.get_env   s     ,,++DKK8//He$<<,,Wf=##FD.2kk.B.B 	$ 	D))$((HfEr   c                 ~    | j                   j                  }t        |t        j                        r|j
                  S |S )z
        The NPM callconv has already converted simplified optional types.
        We can simply use the value type from it.
        )rD   rt   ro   r   Optionalrj   )r   rt   s     r   rz   z%PyCallWrapper._simplified_return_type   s0    
 ++%%gu~~.<<Nr   c                 |    t         j                  r,| j                  j                  |dj	                  |             y y )NzDEBUGJIT: {0})r   	DEBUG_JITr   ru   format)r   r   msgs      r   ru   zPyCallWrapper.debug_print   s/    LL$$Wo.D.DS.IJ r   N)	r2   r3   r4   r   r^   rW   rm   rz   ru   r6   r   r   r@   r@   [   s#    '2E+N
Kr   r@   )llvmlite.irr   r   rQ   
numba.corer   r   r   objectr	   r8   r@   r6   r   r   <module>r      s<    +  - -@& @F#& #"GKF GKr   