
    xKg^R                        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)d Z*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#k(  re0Z1ned"k  re-Z1n ee       G d$ d%ejd                        Z3y)&    )
namedtupleOrderedDictN)CodeType
ModuleType)errorsutils	serialize)	PYVERSION      )_inline_cache_entries   r   	   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     W/home/alanp/www/video.onchill/myenv/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_opcodesr2   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)r7   r8   r9   r*   r:   r;   r<   selfr7   r9   r;   
nextoffsets        r!   __init__zByteCodeInst.__init__Q   s5    	jj(r#   c                 &    | j                   t        v S r)   )r9   JUMP_OPSr@   s    r!   is_jumpzByteCodeInst.is_jumpY       {{h&&r#   c                 &    | j                   t        v S r)   )r9   TERM_OPSrE   s    r!   is_terminatorzByteCodeInst.is_terminator]   rG   r#   c                    | j                   sJ 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        dv rK| j                  t        v r| j                  | j                  z   S | j                  t        v sJ | j                  S t        t              )Nr   c              3   B   K   | ]  }t         j                  |     y wr)   r*   r+   .0ks     r!   	<genexpr>z/ByteCodeInst.get_jump_target.<locals>.<genexpr>l   s       ;(91  #yy|(9   )JUMP_BACKWARD   r   )r   c              3   B   K   | ]  }t         j                  |     y wr)   rM   rN   s     r!   rQ   z/ByteCodeInst.get_jump_target.<locals>.<genexpr>p   s'      L)K1  #yy|)K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   )r   )	rF   r
   r9   r7   r;   NotImplementedErrorJREL_OPSr8   JABS_OPSrE   s    r!   get_jump_targetzByteCodeInst.get_jump_targeta   sG    |||#{{ ;(9; ;{{dhhla%777+%{{ L)KL L {{dhhla%777++%i0033{{h&yy488a<//{{h...xx!|a'')#{{h&yy488++{{h...xx%i00r#   c                 N    d| j                   | j                  | j                  fz  S )Nz%s(arg=%s, lineno=%d))r:   r;   r<   rE   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_rT   	POP_BLOCKr>   r   )r:   
startswithrE   s    r!   block_effectzByteCodeInst.block_effect   s+    
 ;;!!(+[[K'r#   N)__name__
__module____qualname____doc__	__slots__rB   propertyrF   rJ   r^   r`   re    r#   r!   r6   r6   B   s\     FI ' ' ' '(1TN 	 	r#   r6   rT   NOPc              #   &  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)zd
    Returns a 4-int-tuple of
    (bytecode offset, opcode, argument, offset of next bytecode).
    r      r   r   N)lenCODE_LENHAVE_ARGUMENTrangeARG_LENr
   r   	INSTR_LENr[   EXTENDED_ARG
OPCODE_NOP
NO_ARG_LEN)codeextended_argnr7   iopr;   js           r!   _unpack_opargsr      sL    
 LD	ANFQ
a%!W	Xq'L(C7^tAE{q1u-- $LAK'
 *2.::88))44\! z322"a'k1COAK'
 *2.::88))44r3""[ a%s   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)rw   _FIXED_OFFSETr]   )	bc_streamr7   r9   r;   rA   s        r!   _patched_opargsr      sU      j$
..+4'ZX= C}$fc:3MMM	 ,5s   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)   )ry   iterr   r   co_code)r@   ry   s     r!   rB   zByteCodeIter.__init__   s)    			8I8I)JKL	r#   c                     | S r)   rl   rE   s    r!   __iter__zByteCodeIter.__iter__   s    r#   c                 ,    t        | j                        S r)   )r8   r   rE   s    r!   _fetch_opcodezByteCodeIter._fetch_opcode   s    DIIr#   c                 N    | j                         \  }}}}|t        ||||      fS )N)r7   r9   r;   rA   )r   r6   r?   s        r!   r8   zByteCodeIter.next   s6    *.*<*<*>'Z|6&c/9; ; 	;r#   c                 p    d}t        |      D ]%  }t        | j                        \  }}||d|z  z  z  }' |S )Nr   ro   )rs   r8   r   )r@   sizebufr|   _offsetbytes         r!   read_argzByteCodeIter.read_arg   s?    tA OMGT4AE?"C  
r#   N)	rf   rg   rh   rB   r   r   r8   __next__r   rl   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     M0L1Q&0Ls   r   )ry   setr*   
findlabelsr   addr   r   _compute_linenor   r   r   r   r   r   r   sortedr   )r@   r   ry   r   r   s        r!   rB   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 ]%  }|j                  dk\  r|j                  }||_        ' |S )zI
        Compute the line numbers for all bytecode instructions.
        r   )r*   findlinestartsr   r<   co_firstlinenovalues)clsr   ry   r7   r<   
adj_offsetknowninsts           r!   r   z_ByteCode._compute_lineno   s|    
 "006NFF-/JU"+1j!( 7 ##LLND{{a#	 #
 r#   c                 H    t        | j                  j                               S r)   )r   r   r   rE   s    r!   r   z_ByteCode.__iter__3  s    DJJ%%'((r#   c                      | j                   |   S r)   r   r@   r7   s     r!   __getitem__z_ByteCode.__getitem__6  s    zz&!!r#   c                     || j                   v S r)   r   r   s     r!   __contains__z_ByteCode.__contains__9  s    ##r#   c                 t      fddj                  fd j                  j                         D              S )Nc                 >    | d   j                   j                  v ryy)NrT   > )r7   r   )r|   r@   s    r!   label_markerz$_ByteCode.dump.<locals>.label_marker=  s    t{{dkk)r#   
c              3   `   K   | ]%  }|d    j                   dk7  rd |      f|z   z   ' yw)rT   CACHEz
%s %10s	%sN)r:   )rO   r|   r   s     r!   rQ   z!_ByteCode.dump.<locals>.<genexpr>C  s:      4"4QaDKK72 '<?*<q*@A"4s   +.)joinr   items)r@   r   s   `@r!   dumpz_ByteCode.dump<  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   r:   _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_globalsG  s       99^U^^<h
+((HLLND{{m+ 4TXX >?q=/ %d $AdG # B"h'&|B'7822435<<N 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   rE   s    r!   get_used_globalsz_ByteCode.get_used_globalsd  s8    
 ))$,,*;*;TZZ*...$--I 	Ir#   N)rf   rg   rh   ri   rj   rB   classmethodr   r   r   r   r   r   r   rl   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   rT   rZ   )r
   r[   )r;   s    r!   r   r   m  s-    &&ax	'	'
!),,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)   )	superrB   r*   Bytecodery   r   tuplemapfixup_eh)r@   r   entries	__class__s      r!   rB   zByteCodePy311.__init__x  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
        rT   N)r   r   r   r-   r   max)r@   r7   
candidatesr   s       r!   find_exception_entryz"ByteCodePy311.find_exception_entry  sb     
))CyyF,SWW,!!399c"23 * j/!$CJ r#   )rf   rg   rh   rB   staticmethodr   r   __classcell__r   s   @r!   r   r   v  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   rB   _ordered_offsetsr*   r   ry   r   r   remove_build_list_swap_patternis_generatorr   r   r   )r@   r   er   max_exception_targetr   s        r!   rB   zByteCodePy312.__init__  s    ! !% <<-??? )*4==#? 	  55g> #&''B'Q''B#C  #*N'QQXX9M-Mq'G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  s7    $$04

$;
1Q
$;D!$$$ %<s   	8c                    dt         dt        fd}d}|rvd}|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(  s| j                  j	                  |j                        }| j                  | j                  |dz
        }|j                  dk(  s| j                  | j                  |      }|j                  dk(  s|j                  d	k(  rR |||      }d}_ |rv|S )a   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.
        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 )NrT   r   )	indexrp   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)?)?? 'G ANAs   	"C/TF)
BUILD_LIST	BUILD_MAP	BUILD_SETrT   SWAPr   FOR_ITEREND_FOR)
listr   copyr   r   r   r   r:   r;   r   )r@   r   r  work_remainingentryr   	curr_inst	next_insts           r!   r   z,ByteCodePy312.remove_build_list_swap_pattern  s   "	d 	6J	2  #N  ,,225;;? !JJt';';E'BC	## ,9 9  !JJt';';EAI'FG	 ''61immq6H JJt';';EAI'FG	 '':5
 ,,22599= JJt';';EAI'FG	 ''94 JJt';';E'BC	 ''61immq6H 37EB!%M (	 V r#   )rf   rg   rh   rB   rk   r   r   r   r   s   @r!   r   r     s"    04 % %Vr#   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).
    rT   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   ByteCodeSupportErrorrh   AttributeErrorrf   r   func_qualnamesplit	func_namery   inspect	getmodulemodule_dynamic_modnamemodnameisgeneratorfunctionr   pysigco_filenamefilenamer   firstlinenorp   
parameters	arg_countr  	arg_namesr8   _unique_idsformatunique_name	unique_id)r   pyfuncr   ry   r"  r  r@   uids           r!   from_functionzFunctionIdentity.from_function%  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   rE   s    r!   derivezFunctionIdentity.deriveO  s     !!$)),,r#   c                 .    t        | j                        S )4
        NOTE: part of ReduceMixin protocol
        )r-  )dictr   rE   s    r!   _reduce_stateszFunctionIdentity._reduce_statesT  s     499%%r#   c                 $    | j                  |      S )r3  )r/  )r   r-  s     r!   _rebuildzFunctionIdentity._rebuildZ  s    
   ((r#   N)rf   rg   rh   ri   	itertoolscountr)  r   r/  r1  r5  r7  rl   r#   r!   r  r    sJ     ")//!$K' 'R-
& ) )r#   r  )4collectionsr   r   r*   r  r8  typesr   r   
numba.corer   r   r	   numba.core.utilsr
   r9   r   ru   r[   r   r   r   r"   r'   r2   	frozensethasjrelr\   hasjabsr]   rD   rI   rv   rr   objectr6   rq   rt   rx   r:   r   rw   r   r   r   r   r   r   r   ByteCodeReduceMixinr  rl   r#   r!   <module>rD     s   / 
   & / / & ,I,,
i
(( 4!"8"@B  	E
 S[[!S[[!h[./!BCD!!V6 Vr 

ZZe$
5pN6 4fI fIR-I >yM yx H'HH
i
((D)y,, D)r#   