
    pia                        d dl Z 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j6                        Z eej:                        Zeez  Z e eddg            Z ejB                  Z!ejD                  Z" G d de#      Z$dZ%dZ&dZ'ejP                  jS                  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.      Z0 G d# d$e0      Z1ed%k(  re0Z2nedv re1Z2ned%k  re.Z2n ee       G d& d'ejf                        Z4y)(    N)
namedtupleOrderedDict)CodeType
ModuleType)errorsutils	serialize)	PYVERSION)      r      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     U/opt/services/ai/voice_agent/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_opcodesr3   2   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)r8   r9   r:   r+   r;   r<   r=   selfr8   r:   r<   
nextoffsets        r"   __init__zByteCodeInst.__init__R   s5    	jj(r$   c                 &    | j                   t        v S r*   )r:   JUMP_OPSrA   s    r"   is_jumpzByteCodeInst.is_jumpZ       {{h&&r$   c                 &    | j                   t        v S r*   )r:   TERM_OPSrF   s    r"   is_terminatorzByteCodeInst.is_terminator^   rH   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   r   c              3   B   K   | ]  }t         j                  |     y wr*   r+   r,   .0ks     r"   	<genexpr>z/ByteCodeInst.get_jump_target.<locals>.<genexpr>m   s"      H#$  #yy| H   )JUMP_BACKWARDJUMP_BACKWARD_NO_INTERRUPTr   r   c              3   B   K   | ]  }t         j                  |     y wr*   rO   rP   s     r"   rS   z/ByteCodeInst.get_jump_target.<locals>.<genexpr>r   s       ;#$  #yy| ;rT   )rU      )r   c              3   B   K   | ]  }t         j                  |     y wr*   rO   rP   s     r"   rS   z/ByteCodeInst.get_jump_target.<locals>.<genexpr>v   s"      L#$  #yy| LrT   )rU   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   )	rG   r
   r:   r9   r<   r8   NotImplementedErrorJREL_OPSJABS_OPSrF   s    r"   get_jump_targetzByteCodeInst.get_jump_targetb   sE    |||**{{ 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EE{{h&yy488a<//{{h...xx!|a''%i00r$   c                 N    d| j                   | j                  | j                  fz  S )Nz%s(arg=%s, lineno=%d))r;   r<   r=   rF   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_rY   	POP_BLOCKr?   r   )r;   
startswithrF   s    r"   block_effectzByteCodeInst.block_effect   s+    
 ;;!!(+[[K'r$   N)__name__
__module____qualname____doc__	__slots__rC   propertyrG   rK   rc   re   rj    r$   r"   r7   r7   C   s\     FI ' ' ' ''1RN 	 	r$   r7   rY   NOPrM   c              #     K   g }t        j                  |       D ]  \  }}}}|j                  |||f        t        |      D ]9  \  }\  }}}|dz   t	        |      k  r||dz      d   }nt	        |       }||||f ; y wNrY   r   )r+   _unpack_opargsr.   	enumeratelen)codebufistart_offsetopr<   next_offsets          r"   ru   ru      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      rW   r   N)rw   CODE_LENHAVE_ARGUMENTrangeARG_LENr
   r   	INSTR_LENr`   EXTENDED_ARG
OPCODE_NOP
NO_ARG_LEN)rx   extended_argnr8   rz   r|   r<   js           r"   ru   ru      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_OFFSETrb   )	bc_streamr8   r:   r<   rB   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*   )rx   iterr   ru   co_code)rA   rx   s     r"   rC   zByteCodeIter.__init__   s)    			8I8I)JKL	r$   c                     | S r*   rq   rF   s    r"   __iter__zByteCodeIter.__iter__  s    r$   c                 ,    t        | j                        S r*   )r9   r   rF   s    r"   _fetch_opcodezByteCodeIter._fetch_opcode  s    DIIr$   c                 N    | j                         \  }}}}|t        ||||      fS )N)r8   r:   r<   rB   )r   r7   r@   s        r"   r9   zByteCodeIter.next	  s6    *.*<*<*>'Z|6&c/9; ; 	;r$   c                 p    d}t        |      D ]%  }t        | j                        \  }}||d|z  z  z  }' |S )Nr   r   )r   r9   r   )rA   sizery   rz   _offsetbytes         r"   read_argzByteCodeIter.read_arg  sD    t 	#A OMGT4AE?"C	# 
r$   N)	rk   rl   rm   rC   r   r   r9   __next__r   rq   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   )rQ   xs     r"   rS   z%_ByteCode.__init__.<locals>.<genexpr>#  s     M1Q&Ms   r   )rx   setr+   
findlabelsr   addr   r   _compute_linenor   r   r   r   r   r   r   sortedr   )rA   r   rx   r   r   s        r"   rC   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   rx   r8   r=   
adj_offsetknowninsts           r"   r   z_ByteCode._compute_lineno4  s    
 "006 	2NFF-/JU"+1j!(	2 ##LLN 	$D{{&4;;!+;#		$
 r$   c                 H    t        | j                  j                               S r*   )r   r   r   rF   s    r"   r   z_ByteCode.__iter__G  s    DJJ%%'((r$   c                      | j                   |   S r*   r   rA   r8   s     r"   __getitem__z_ByteCode.__getitem__J  s    zz&!!r$   c                     || j                   v S r*   r   r   s     r"   __contains__z_ByteCode.__contains__M  s    ##r$   c                 t      fddj                  fd j                  j                         D              S )Nc                 >    | d   j                   j                  v ryy)NrY   > )r8   r   )rz   rA   s    r"   label_markerz$_ByteCode.dump.<locals>.label_markerQ  s    t{{dkk)r$   
c              3   `   K   | ]%  }|d    j                   dk7  rd |      f|z   z   ' yw)rY   CACHEz
%s %10s	%sN)r;   )rQ   rz   r   s     r"   rS   z!_ByteCode.dump.<locals>.<genexpr>W  s:      4aDKK72 '<?*<q*@A 4s   +.)joinr   items)rA   r   s   `@r"   dumpz_ByteCode.dumpP  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_globals[  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   rF   s    r"   get_used_globalsz_ByteCode.get_used_globalsx  s8    
 ))$,,*;*;TZZ*...$--I 	Ir$   N)rk   rl   rm   rn   ro   rC   classmethodr   r   r   r   r   r   r   rq   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   r   rY   r_   )r
   r`   )r<   s    r"   r   r     s,    88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*   )	superrC   r+   Bytecoderx   r   tuplemapfixup_eh)rA   r   entries	__class__s      r"   rC   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
        rY   N)r   r   r   r.   r   max)rA   r8   
candidatesr   s       r"   find_exception_entryz"ByteCodePy311.find_exception_entry  sg     
)) 	4CyyF,SWW,!!399c"23	4 j/!$CJ r$   )rk   rl   rm   rC   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   rC   _ordered_offsetsr+   r   rx   r   r   remove_build_list_swap_patternis_generatorr   r   r   )rA   r   er   max_exception_targetr   s        r"   rC   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   )rA   os     r"   ordered_offsetszByteCodePy312.ordered_offsets  s3    $$04

$;1Q$;D!$$$ %<s   	8c                    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(  rFt        j                  dd dk7  r|j                  |	       | j                  | j                  |dz         }	|	j                  dk(  st        dv r| j                  j                  |j                        }| j                  | j                  |d	z
        }|j                  dk(  st| j                  | j                  |dz
        }	t        dv r|	j                  dk(  s*t        dv r|	j                  dk(  st        t              | 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m| j                  | j                  |      }	|	j                  dk(  s |	j                  d	k(  rt        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 rt   )	indexrw   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_SETrY   SWAPr   GET_ITERNr   )r   r      FOR_ITERrM   END_FOR)r   POP_TOP)r   POP_ITERr~   rr   )listr   r   copyr  r
  r   r   r;   r<   sysversion_infor   r
   r   r`   r7   r8   r+   r,   r9   )rA   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 #  O3 ,,225;;? !JJt';';E'BC	## ,9 9  !JJt';';EAI'FG	 ''61immq6H JJt';';EAI'FG	
 ##z1 ''+z9)--i8 $

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

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

4+?+?	+J KI K/(//9<$"k1(//:=$1)<< $

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_O3 n " 	>D&24;;3699U3C373799'>DJJt{{#	>
 r$   )rk   rl   rm   rC   rp   r  r   r   r   s   @r"   r   r     s"    04 % %S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).
    rY   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   ByteCodeSupportErrorrm   AttributeErrorrk   r   func_qualnamesplit	func_namerx   inspect	getmodulemodule_dynamic_modnamemodnameisgeneratorfunctionr  pysigco_filenamefilenamer   firstlinenorw   
parameters	arg_countr  	arg_namesr9   _unique_idsformatunique_name	unique_id)r   pyfuncr   rx   r5  r,  rA   uids           r"   from_functionzFunctionIdentity.from_functionv  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.
        )rB  r   rF   s    r"   derivezFunctionIdentity.derive  s     !!$)),,r$   c                 .    t        | j                        S )4
        NOTE: part of ReduceMixin protocol
        )r@  )dictr   rF   s    r"   _reduce_stateszFunctionIdentity._reduce_states  s     499%%r$   c                 $    | j                  |      S )rF  )rB  )r   r@  s     r"   _rebuildzFunctionIdentity._rebuild  s    
   ((r$   N)rk   rl   rm   rn   	itertoolscountr<  r   rB  rD  rH  rJ  rq   r$   r"   r&  r&  l  sJ     ")//!$K' 'R-
& ) )r$   r&  )5r  collectionsr   r   r+   r/  rK  typesr   r   
numba.corer   r   r	   numba.core.utilsr
   r:   r   r   r`   r   r   r   r#   r(   r3   	frozensethasjrelra   hasjabsrb   rE   rJ   r   r   objectr7   r   r   r   r;   r
  r   ru   r   r   r   r   r   r   ByteCodeReduceMixinr&  rq   r$   r"   <module>rW     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 >vM vr H--HH
i
((D)y,, D)r$   