
    qhl_                        d dl mZmZ d dlZd dlZd dlZd dlmZmZ d dl	m
Z
mZmZ d dlmZ edv r	d dlmZ dZned	v rn ee       ed
dg      Z edd      ZdZd Zd Zd Z eej4                        Z eej8                        Zeez  Z e eddg            Zej@                  Z ejB                  Z! G d de"      Z#dZ$dZ%dZ&ejN                  jQ                  d      Z)edv rd Z*nedv rd Z*n ee      d Z+ G d de"      Z, G d de"      Z-d  Z. G d! d"e-      Z/ G d# d$e/      Z0ed%k(  re/Z1nedv re0Z1ned%k  re-Z1n ee       G d& d'ejd                        Z3y)(    )
namedtupleOrderedDictN)CodeType
ModuleType)errorsutils	serialize)	PYVERSION)      r      )_inline_cache_entries   r   
   r      opcode_infoargsize_ExceptionTableEntryzstart end target depth lastic                 <    t        | dd      }|rt        | |      S | S )z
    Objects that wraps function should provide a "__numba__" magic attribute
    that contains a name of an attribute that contains the actual python
    function object.
    	__numba__Ngetattr)objattrs     M/var/www/html/engine/venv/lib/python3.12/site-packages/numba/core/bytecode.pyget_function_objectr!       s'     3T*DsD!!J    c           	      2    t        | dt        | dd            S )z"Shamelessly borrowed from llpython__code__	func_codeNr   )r   s    r    get_code_objectr&   ,   s    3
GCd$CDDr"   c                 ~    g }| D ]5  }t         j                  j                  |      }|%|j                  |       7 |S N)disopmapgetappend)seqlstscs       r    _as_opcodesr1   1   s?    
C IIMM!=JJqM Jr"   RETURN_VALUERAISE_VARARGSc                   V    e Zd ZdZdZd Zed        Zed        Zd Z	d Z
ed        Zy	)
ByteCodeInstz
    Attributes
    ----------
    - offset:
        byte offset of opcode
    - opcode:
        opcode integer value
    - arg:
        instruction arg
    - lineno:
        -1 means unknown
    )offsetnextopcodeopnamearglinenoc                 z    || _         || _        || _        t        j                  |   | _        || _        d| _        y )N)r6   r7   r8   r)   r9   r:   r;   selfr6   r8   r:   
nextoffsets        r    __init__zByteCodeInst.__init__Q   s5    	jj(r"   c                 &    | j                   t        v S r(   )r8   JUMP_OPSr?   s    r    is_jumpzByteCodeInst.is_jumpY       {{h&&r"   c                 &    | j                   t        v S r(   )r8   TERM_OPSrD   s    r    is_terminatorzByteCodeInst.is_terminator]   rF   r"   c                    | j                   sJ t        dv r1| j                  d dD        v r| j                  | j                  dz  z
  S t        dv r4| j                  d dD        v rs| j
                  | j                  dz
  dz  z
  S t        d	v r4| j                  d
 dD        v r7| j
                  | j                  dz
  dz  z
  S t        dv rnt        t              t        dv rT| j                  t        v r| j                  | j                  dz  z   S | j                  t        v sJ | j                  dz  dz
  S t        t              )Nr   c              3   B   K   | ]  }t         j                  |     y wr(   r)   r*   .0ks     r    	<genexpr>z/ByteCodeInst.get_jump_target.<locals>.<genexpr>l   s"      H#$  #yy| H   )JUMP_BACKWARDJUMP_BACKWARD_NO_INTERRUPTr   r   c              3   B   K   | ]  }t         j                  |     y wr(   rM   rN   s     r    rQ   z/ByteCodeInst.get_jump_target.<locals>.<genexpr>q   s       ;#$  #yy| ;rR   )rS      )r   c              3   B   K   | ]  }t         j                  |     y wr(   rM   rN   s     r    rQ   z/ByteCodeInst.get_jump_target.<locals>.<genexpr>u   s"      L#$  #yy| LrR   )rS   POP_JUMP_BACKWARD_IF_TRUEPOP_JUMP_BACKWARD_IF_FALSEPOP_JUMP_BACKWARD_IF_NONEPOP_JUMP_BACKWARD_IF_NOT_NONEr   )r   r   r   r   )	rE   r
   r8   r7   r:   r6   NotImplementedErrorJREL_OPSJABS_OPSrD   s    r    get_jump_targetzByteCodeInst.get_jump_targeta   sD    |||
"{{ H)GH H yyDHHqL11*${{ ;(9; ;{{dhhla%777+%{{ L)KL L {{dhhla%777*$%i00<<{{h&yy488a<//{{h...xx!|a''%i00r"   c                 N    d| j                   | j                  | j                  fz  S )Nz%s(arg=%s, lineno=%d))r9   r:   r;   rD   s    r    __repr__zByteCodeInst.__repr__   s     &$++txx)MMMr"   c                 \    | j                   j                  d      ry| j                   dk(  ryy)zREffect of the block stack
        Returns +1 (push), 0 (none) or -1 (pop)
        SETUP_rW   	POP_BLOCKr=   r   )r9   
startswithrD   s    r    block_effectzByteCodeInst.block_effect   s+    
 ;;!!(+[[K'r"   N)__name__
__module____qualname____doc__	__slots__rA   propertyrE   rI   ra   rc   rh    r"   r    r5   r5   B   s\     FI ' ' ' ''1RN 	 	r"   r5   rW   NOPrK   c              #     K   g }t        j                  |       D ]  \  }}}}|j                  |||f        t        |      D ]9  \  }\  }}}|dz   t	        |      k  r||dz      d   }nt	        |       }||||f ; y wNrW   r   )r)   _unpack_opargsr,   	enumeratelen)codebufistart_offsetopr:   next_offsets          r    rs   rs      s     (+(:(:4(@ 	0$A|RJJb#./	0*3C. 	7&A&b#1us3x!!a%jm!$iS+66	7s   A>B r   r   r   c              #   &  K   d}t        |       }dx}}||k  r| |   }|t        z  }|t        k\  r| |   |z  }t        t              D ]  }|| ||z      d|z  z  z  } |t        z  }t
        dv r|t        |   t        z  z  }nt
        dv rnt        t
              |t        k(  rZ|t        ||f |dt        z  z  }|}d}|t        z  }t
        dv r|t        |   t        z  z  }nt
        dv rnt        t
              d}||||f |}||k  ryyw)zp
        Returns a 4-int-tuple of
        (bytecode offset, opcode, argument, offset of next bytecode).
        r      rU   r   N)ru   CODE_LENHAVE_ARGUMENTrangeARG_LENr
   r   	INSTR_LENr^   EXTENDED_ARG
OPCODE_NOP
NO_ARG_LEN)rv   extended_argnr6   rx   rz   r:   js           r    rs   rs      sQ    
 I!eaBMA]"1g,w 2A4A;1q511C2W
* .r2Y>>A"44-i88% ":sA66#&!g+#5LFZ
* .r2Y>>A"44-i88L2sA&&Fa !es   DDDc              #      K   dt         dt        f | D ].  \  }}}}|t        v r	|t        z  }|t        z   |||t        z   f 0 yw)zpPatch the bytecode stream.

    - Adds a NOP bytecode at the start to avoid jump target being at the entry.
    r   N)r   _FIXED_OFFSETr`   )	bc_streamr6   r8   r:   r@   s        r    _patched_opargsr      s\      j$
..+4 N'ZX= C}$fc:3MMM	Ns   AAc                   .    e Zd Zd Zd Zd Zd ZeZd Zy)ByteCodeIterc                 ~    || _         t        t        t        | j                   j                                    | _        y r(   )rv   iterr   rs   co_code)r?   rv   s     r    rA   zByteCodeIter.__init__   s)    			8I8I)JKL	r"   c                     | S r(   ro   rD   s    r    __iter__zByteCodeIter.__iter__  s    r"   c                 ,    t        | j                        S r(   )r7   r   rD   s    r    _fetch_opcodezByteCodeIter._fetch_opcode  s    DIIr"   c                 N    | j                         \  }}}}|t        ||||      fS )N)r6   r8   r:   r@   )r   r5   r>   s        r    r7   zByteCodeIter.next  s6    *.*<*<*>'Z|6&c/9; ; 	;r"   c                 p    d}t        |      D ]%  }t        | j                        \  }}||d|z  z  z  }' |S )Nr   r~   )r   r7   r   )r?   sizerw   rx   _offsetbytes         r    read_argzByteCodeIter.read_arg  sD    t 	#A OMGT4AE?"C	# 
r"   N)	ri   rj   rk   rA   r   r   r7   __next__r   ro   r"   r    r   r      s#    M;
 Hr"   r   c                   X    e Zd ZdZdZd Zed        Zd Zd Z	d Z
d Zed	        Zd
 Zy)	_ByteCodezF
    The decoded bytecode of a function, and related information.
    )	func_idco_namesco_varnames	co_constsco_cellvarsco_freevarsexception_entriestablelabelsc                    |j                   }t        d t        j                  |j                        D              }|j                  d       t        t        |            }| j                  ||       || _	        |j                  | _
        |j                  | _        |j                  | _        |j                  | _        |j                  | _        || _        t!        |      | _        y )Nc              3   .   K   | ]  }|t         z     y wr(   )r   )rO   xs     r    rQ   z%_ByteCode.__init__.<locals>.<genexpr>"  s     M1Q&Ms   r   )rv   setr)   
findlabelsr   addr   r   _compute_linenor   r   r   r   r   r   r   sortedr   )r?   r   rv   r   r   s        r    rA   z_ByteCode.__init__  s    ||Mt||0LMM

1 L./UD)++++++
Vnr"   c                    t        j                  |      D ]  \  }}|t        z   }||v s|||   _         |j                  }|j                         D ]1  }|j                  |j                  dk\  r|j                  }+||_        3 |S )zI
        Compute the line numbers for all bytecode instructions.
        r   )r)   findlinestartsr   r;   co_firstlinenovalues)clsr   rv   r6   r;   
adj_offsetknowninsts           r    r   z_ByteCode._compute_lineno3  s    
 "006 	2NFF-/JU"+1j!(	2 ##LLN 	$D{{&4;;!+;#		$
 r"   c                 H    t        | j                  j                               S r(   )r   r   r   rD   s    r    r   z_ByteCode.__iter__F  s    DJJ%%'((r"   c                      | j                   |   S r(   r   r?   r6   s     r    __getitem__z_ByteCode.__getitem__I  s    zz&!!r"   c                     || j                   v S r(   r   r   s     r    __contains__z_ByteCode.__contains__L  s    ##r"   c                 t      fddj                  fd j                  j                         D              S )Nc                 >    | d   j                   j                  v ryy)NrW   > )r6   r   )rx   r?   s    r    label_markerz$_ByteCode.dump.<locals>.label_markerP  s    t{{dkk)r"   
c              3   `   K   | ]%  }|d    j                   dk7  rd |      f|z   z   ' yw)rW   CACHEz
%s %10s	%sN)r9   )rO   rx   r   s     r    rQ   z!_ByteCode.dump.<locals>.<genexpr>V  s:      4aDKK72 '<?*<q*@A 4s   +.)joinr   items)r?   r   s   `@r    dumpz_ByteCode.dumpO  s5    	 yy 4"&**"2"2"44 4 	4r"   c           	      "   i }|j                   }|j                  dt        j                        }t	        |t
              r|j                  }|j                         D ]:  }|j                  dk(  s|t        |j                           }	|	|vs0	 ||	   }
|
||	<   < |D ]^  }t	        |t              st        t        |            }|j                  | j!                  |||j"                  |j$                               ` |S # t        $ r ||	   }
Y |w xY w)za
        Compute the globals used by the function with the given
        bytecode table.
        __builtins__LOAD_GLOBAL)__globals__r+   r   builtins
isinstancer   __dict__r   r9   _fix_LOAD_GLOBAL_argr:   KeyErrorr   r   r   update_compute_used_globalsr   r   )r   funcr   r   r   dglobsr   r   namevaluecosubtables                r    r   z_ByteCode._compute_used_globalsZ  s      99^U^^<h
+((HLLN 	$D{{m+ 4TXX >?q=/ %d $AdG	$  	OB"h'&|B'7822435<<N O	O
  $ / (/s   C==DDc                     | j                  | j                  j                  | j                  | j                  | j
                        S )zv
        Get a {name: value} map of the globals used by this code
        object and any nested code objects.
        )r   r   r   r   r   r   rD   s    r    get_used_globalsz_ByteCode.get_used_globalsw  s8    
 ))$,,*;*;TZZ*...$--I 	Ir"   N)ri   rj   rk   rl   rm   rA   classmethodr   r   r   r   r   r   r   ro   r"   r    r   r     sU    $I%(  $)"$	4  8Ir"   r   c                 N    t         dv r| dz	  S t         dv r| S t        t               )N)r   r   r   rW   r]   )r
   r^   )r:   s    r    r   r     s,    //ax	j	 
!),,r"   c                   4     e Zd Z fdZed        Zd Z xZS )ByteCodePy311c                     t         |   |       t        j                  |j                        j
                  }t        t        | j                  |            | _        y r(   )	superrA   r)   Bytecoderv   r   tuplemapfixup_eh)r?   r   entries	__class__s      r    rA   zByteCodePy311.__init__  s@    !,,w||,>>!&s4==''B!Cr"   c                     t        j                  | j                  t        z   | j                  t        z   | j
                  t        z   | j                  | j                        }|S )N)startendtargetdepthlasti)r)   r   r   r   r   r   r   r   )entouts     r    r   zByteCodePy311.fixup_eh  sK     &&))m+=1H::-))399

 
r"   c                     g }| j                   D ]B  }|j                  |cxk  r|j                  k  s#n &|j                  |j                  |f       D |rt        |      d   }|S y)zN
        Returns the exception entry for the given instruction offset
        rW   N)r   r   r   r,   r   max)r?   r6   
candidatesr   s       r    find_exception_entryz"ByteCodePy311.find_exception_entry  sg     
)) 	4CyyF,SWW,!!399c"23	4 j/!$CJ r"   )ri   rj   rk   rA   staticmethodr   r   __classcell__r   s   @r    r   r     s"    D
  
r"   r   c                   4     e Zd Z fdZed        Zd Z xZS )ByteCodePy312c                    t         |   |       d | _        t        j                  |j
                        j                  D cg c]  }| j                  |       }}| j                  |      }|j                  rBt        |D cg c]  }|j                   c}      }|D cg c]  }|j                  |k7  s| }}t        |      | _        y c c}w c c}w c c}w r(   )r   rA   _ordered_offsetsr)   r   rv   r   r   remove_build_list_swap_patternis_generatorr   r   r   )r?   r   er   max_exception_targetr   s        r    rA   zByteCodePy312.__init__  s    ! !% <<-??4==#   55g> #&''BQ'B#C  #*NQQXX9M-MqNGN!&w# (C Os   CC!C6Cc                 z    | j                   s| j                  D cg c]  }| c}| _         | j                   S c c}w r(   )r   r   )r?   os     r    ordered_offsetszByteCodePy312.ordered_offsets  s3    $$04

$;1Q$;D!$$$ %<s   	8c                 0   dt         dt        fd}t               }d}|rd}t               }|j                         D ]  }| j                  j                  |j                        }| j                  | j                  |      }|j                  dvrT| j                  | j                  |dz         }	|	j                  dk(  s|	j                  d	k(  r| j                  | j                  |d	z         }	|	j                  d
k(  r0|j                  |	       | j                  | j                  |dz         }	|	j                  dk(  st        dv r| j                  j                  |j                        }| j                  | j                  |d	z
        }|j                  dk(  s^| j                  | j                  |dz
        }	|	j                  dk(  s| j                  | j                  |      }	|	j                  dk(  s|	j                  d	k(  rt        dv r| j                  j                  |j                        }| j                  | j                  |dz
        }|j                  dk(  s'| j                  | j                  |      }	|	j                  dk(  s |	j                  d	k(  rct        t               |||      }d}||z  } |r|D ]L  }
t        |
j                  t         j"                  d   d|
j$                        | j                  |
j                  <   N |S )at   Find the following bytecode pattern:

            BUILD_{LIST, MAP, SET}
            SWAP(2)
            FOR_ITER
            ...
            END_FOR
            SWAP(2)

            This pattern indicates that a list/dict/set comprehension has
            been inlined. In this case we can skip the exception blocks
            entirely along with the dead exceptions that it points to.
            A pair of exception that sandwiches these exception will
            also be merged into a single exception.

            Update for Python 3.13, the ending of the pattern has a extra
            POP_TOP:

            ...
            END_FOR
            POP_TOP
            SWAP(2)

            Update for Python 3.13.1, there's now a GET_ITER before FOR_ITER.
            This patch the GET_ITER to NOP to minimize changes downstream
            (e.g. array-comprehension).
        r   entry_to_removec                    | j                  |      dz
  }| j                  |      dz   }|dk\  r|t        |       k  rx| |   }| |   }|j                  |j                  k(  rUt        |j                  |j
                  |j                  |j                  |j                        | |<   | j                  |       | j                  |       | D cg c]  }|j                  |j                  k(  s| } }| S c c}w rr   )	indexru   r   r   r   r   r   r   remove)r   r  lower_entry_idxupper_entry_idxlower_entryupper_entryr   s          r    pop_and_merge_exceptionszNByteCodePy312.remove_build_list_swap_pattern.<locals>.pop_and_merge_exceptions  s    %mmO<q@O%mmO<q@O !##g,(F%o6%o6%%););;/C#))##**#))#))0+GO, NN;/ NN?+") AQgg)?)??  AG ANAs   	"C/TF)
BUILD_LIST	BUILD_MAP	BUILD_SETrW   SWAPr   GET_ITERr   FOR_ITERrK   END_FORPOP_TOPr|   rp   N)listr   r   copyr  r  r   r   r9   r:   r   r
   r   r^   r5   r6   r)   r*   r7   )r?   r   r  change_to_nopwork_remainingcurrent_nop_fixesentryr  	curr_inst	next_instr   s              r    r   z,ByteCodePy312.remove_build_list_swap_pattern  s   8	d 	6J	2  #N #  E3 ,,225;;? !JJt';';E'BC	## ,9 9  !JJt';';EAI'FG	 ''61immq6H JJt';';EAI'FG	
 ##z1%)))4 $

4+?+?	+J KI '':5
* !0066uyyAE $

4+?+?	+J KI$++y8  $

4+?+?	+J KI$++y8  $

4+?+?+F GI$++v5)--1:L "== !0066uyyAE $

4+?+?	+J KI$++y8  $

4+?+?+F GI$++v5)--1:L -i88 37EB!% !22KE3 Z " 	>D&24;;3699U3C373799'>DJJt{{#	>
 r"   )ri   rj   rk   rA   rn   r  r   r   r   s   @r    r   r     s"    04 % %Ir"   r   r   c                   `    e Zd ZdZ ej
                  d      Zed        Zd Z	d Z
ed        Zy)FunctionIdentityz
    A function's identity and metadata.

    Note this typically represents a function whose bytecode is
    being compiled, not necessarily the top-level user function
    (the two might be distinct).
    rW   c                 V   t        |      }t        |      }t        j                  |      }|st	        j
                  d|z        	 |j                  } |        }||_	        ||_
        |j                  d      d   |_        ||_        t        j                  |      |_        |j                   t        j"                  n|j                   j                  |_        t        j&                  |      |_        ||_        |j,                  |_        |j0                  |_        t5        |j6                        |_        t;        |j6                        |_        t?        | j@                        }djC                  |j                  |      |_"        ||_#        |S # t        $ r |j                  }Y Tw xY w)zD
        Create the FunctionIdentity of the given function.
        z %s does not provide its bytecode.r=   z{}${})$r!   r&   r   pysignaturer   ByteCodeSupportErrorrk   AttributeErrorri   r   func_qualnamesplit	func_namerv   inspect	getmodulemodule_dynamic_modnamemodnameisgeneratorfunctionr   pysigco_filenamefilenamer   firstlinenoru   
parameters	arg_countr  	arg_namesr7   _unique_idsformatunique_name	unique_id)r   pyfuncr   rv   r/  r&  r?   uids           r    from_functionzFunctionIdentity.from_functionk  sq   
 #6*t$!!$'--2T9; ;	* --M u	*&,,S1"5	''-;;. ..![[11 	 $77=
((..U--.e../
 3??#">>$*<*<cB5  	* MMM	*s   F F('F(c                 8    | j                  | j                        S )z:Copy the object and increment the unique counter.
        )r<  r   rD   s    r    derivezFunctionIdentity.derive  s     !!$)),,r"   c                 .    t        | j                        S )4
        NOTE: part of ReduceMixin protocol
        )r:  )dictr   rD   s    r    _reduce_stateszFunctionIdentity._reduce_states  s     499%%r"   c                 $    | j                  |      S )r@  )r<  )r   r:  s     r    _rebuildzFunctionIdentity._rebuild  s    
   ((r"   N)ri   rj   rk   rl   	itertoolscountr6  r   r<  r>  rB  rD  ro   r"   r    r   r   a  sJ     ")//!$K' 'R-
& ) )r"   r   )4collectionsr   r   r)   r)  rE  typesr   r   
numba.corer   r   r	   numba.core.utilsr
   r8   r   r   r^   r   r   r   r!   r&   r1   	frozensethasjrelr_   hasjabsr`   rC   rH   r   r   objectr5   r   r   r   r9   r  r   rs   r   r   r   r   r   r   ByteCodeReduceMixinr   ro   r"   r    <module>rQ     s   / 
   & / / & "",I$$
i
(( 4!"8"@B  	E
 S[[!S[[!h[./!BCD!!U6 Up 

ZZe$
 
	7 --8t i
((N6 4fI fIR-I >lM l^ H%%HH
i
((D)y,, D)r"   