
    ܙd5                     B   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dg          ZdZd	 Zd
 Zd Z eej                  Z eej                  Zeez  Z e eddg                    Zej        Zej        Z G d de          ZdZdZ dZ!ej"        #                    d          Z$d Z%d Z& G d de          Z' G d de          Z(d Z) G d de(          Z*edk    re*Z+nedk     re(Z+n e,e           G d dej-                  Z.dS )    )
namedtupleOrderedDictN)CodeType
ModuleType)errorsutils	serialize	PYVERSIONopcode_infoargsize   c                 L    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     3lib/python3.11/site-packages/numba/core/bytecode.pyget_function_objectr      s2     3T**D "sD!!!J    c           	      B    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$CDDDr   c                     g }| D ]8}t           j                            |          }||                    |           9|S N)disopmapgetappend)seqlstscs       r   _as_opcodesr&   #   sC    
C  IMM!=JJqMMMJr   RETURN_VALUERAISE_VARARGSc                   j    e Zd ZdZdZd Zed             Zed             Zd Z	d Z
ed             Zd	S )
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| _        d S )N)r+   r,   r-   r   r.   r/   r0   selfr+   r-   r/   
nextoffsets        r   __init__zByteCodeInst.__init__C   s7    	j(r   c                     | j         t          v S r   )r-   JUMP_OPSr4   s    r   is_jumpzByteCodeInst.is_jumpK       {h&&r   c                     | j         t          v S r   )r-   TERM_OPSr9   s    r   is_terminatorzByteCodeInst.is_terminatorO   r;   r   c                    | j         sJ t          dk    r)| j        d dD             v r| j        | j        dz
  dz  z
  S nt          dk    rt          t                    t          dk    r=| j        t          v r| j        | j        dz  z   S | j        t          v sJ | j        dz  dz
  S | j        t          v r| j        | j        z   S | j        t          v sJ | j        S )N      c              3   :   K   | ]}t           j        |         V  d S r   )r   r   ).0ks     r   	<genexpr>z/ByteCodeInst.get_jump_target.<locals>.<genexpr>^   sD       L L#$  #y| L L L L L Lr   )JUMP_BACKWARDPOP_JUMP_BACKWARD_IF_TRUEPOP_JUMP_BACKWARD_IF_FALSEPOP_JUMP_BACKWARD_IF_NONEPOP_JUMP_BACKWARD_IF_NOT_NONE   r   )rA   
   )	r:   r   r-   r+   r/   NotImplementedErrorJREL_OPSr,   JABS_OPSr9   s    r   get_jump_targetzByteCodeInst.get_jump_targetS   s    |{ L L)KL L L L L {dhla%777L   %i000{h&&y48a<//{h....x!|a''{h&&y48++{h....xr   c                 0    d| j         | j        | j        fz  S )Nz%s(arg=%s, lineno=%d))r.   r/   r0   r9   s    r   __repr__zByteCodeInst.__repr__u   s    &$+tx)MMMr   c                 X    | j                             d          rdS | j         dk    rdS dS )zREffect of the block stack
        Returns +1 (push), 0 (none) or -1 (pop)
        SETUP_rL   	POP_BLOCKr2   r   )r.   
startswithr9   s    r   block_effectzByteCodeInst.block_effectx   s8    
 ;!!(++ 	1[K''21r   N)__name__
__module____qualname____doc__	__slots__r6   propertyr:   r>   rQ   rS   rX    r   r   r*   r*   4   s          FI   ' ' X' ' ' X'        DN N N 	 	 X	 	 	r   r*   rL   NOPc              #     K   d}t          |           }dx}}||k     r| |         }|t          z  }|t          k    ri| |         |z  }t          t                    D ]}|| ||z            d|z  z  z  }|t          z  }|t
          k    r|t          ||fV  |dt          z  z  }|}nd}|t          z  }d}||||fV  |}||k     dS dS )zd
    Returns a 4-int-tuple of
    (bytecode offset, opcode, argument, offset of next bytecode).
    r      N)lenCODE_LENHAVE_ARGUMENTrangeARG_LENEXTENDED_ARG
OPCODE_NOP
NO_ARG_LEN)codeextended_argnr+   iopr/   js           r   _unpack_opargsrq      s     
 LD		ANFQ
a%%!W	Xq'L(C7^^ . .tAE{q1u--LA\!! z32222"a'k1 " COAr3""""3 a%%%%%%r   c              #      K   dt           dt          fV  | D ]2\  }}}}|t          v r
|t          z  }|t          z   |||t          z   fV  3dS )zpPatch the bytecode stream.

    - Adds a NOP bytecode at the start to avoid jump target being at the entry.
    r   N)ri   _FIXED_OFFSETrP   )	bc_streamr+   r-   r/   r5   s        r   _patched_opargsru      sz       j$
....+4 N N'ZX= C}$fc:3MMMMMM	N Nr   c                   0    e Zd Zd Zd Zd Zd ZeZd ZdS )ByteCodeIterc                     || _         t          t          t          | j         j                                      | _        d S r   )rk   iterru   rq   co_code)r4   rk   s     r   r6   zByteCodeIter.__init__   s1    		8I)J)JKKLL			r   c                     | S r   r_   r9   s    r   __iter__zByteCodeIter.__iter__   s    r   c                 *    t          | j                  S r   )r,   ry   r9   s    r   _fetch_opcodezByteCodeIter._fetch_opcode   s    DIr   c                 ^    |                                  \  }}}}|t          ||||          fS )N)r+   r-   r/   r5   )r~   r*   r3   s        r   r,   zByteCodeIter.next   sC    *.*<*<*>*>'Z|6&c/9; ; ; ; 	;r   c                 r    d}t          |          D ]$}t          | j                  \  }}||d|z  z  z  }%|S )Nr   rb   )rf   r,   ry   )r4   sizebufrn   _offsetbytes         r   read_argzByteCodeIter.read_arg   sF    t 	# 	#A OOMGT4AE?"CC
r   N)	rY   rZ   r[   r6   r|   r~   r,   __next__r   r_   r   r   rw   rw      si        M M M    ; ; ;
 H    r   rw   c                   f    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dS )	_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                       }|                    d           t          t          |                    }|                     ||           || _	        |j
        | _
        |j        | _        |j        | _        |j        | _        |j        | _        || _        t!          |          | _        d S )Nc              3   *   K   | ]}|t           z   V  d S r   )rs   )rD   xs     r   rF   z%_ByteCode.__init__.<locals>.<genexpr>   s'      MM1Q&MMMMMMr   r   )rk   setr   
findlabelsrz   addr   rw   _compute_linenor   r   r   r   r   r   r   sortedr   )r4   r   rk   r   r   s        r   r6   z_ByteCode.__init__   s    |MMt|0L0LMMMMM

1 L..//UD)))+++
Vnnr   c                     t          j        |          D ] \  }}|t          z   }||v r|||         _        !|j        }|                                D ]}|j        dk    r|j        }||_        |S )zI
        Compute the line numbers for all bytecode instructions.
        r   )r   findlinestartsrs   r0   co_firstlinenovalues)clsr   rk   r+   r0   
adj_offsetknowninsts           r   r   z_ByteCode._compute_lineno   s    
 "066 	2 	2NFF-/JU""+1j!( #LLNN 	$ 	$D{a#r   c                 N    t          | j                                                  S r   )ry   r   r   r9   s    r   r|   z_ByteCode.__iter__	  s    DJ%%''(((r   c                     | j         |         S r   r   r4   r+   s     r   __getitem__z_ByteCode.__getitem__  s    z&!!r   c                     || j         v S r   r   r   s     r   __contains__z_ByteCode.__contains__  s    ##r   c                       fdd                     fd j                                        D                       S )Nc                 4    | d         j         j        v rdS dS )NrL   > )r+   r   )rn   r4   s    r   label_markerz$_ByteCode.dump.<locals>.label_marker  s     t{dk))ssr   
c              3   ^   K   | ]'}|d          j         dk    d |          f|z   z  V  (dS )rL   CACHEz
%s %10s	%sN)r.   )rD   rn   r   s     r   rF   z!_ByteCode.dump.<locals>.<genexpr>  sU       4 4aDK722 '<<??*<q*@A22224 4r   )joinr   items)r4   r   s   `@r   dumpz_ByteCode.dump  si    	 	 	 	 	 yy 4 4 4 4"&*"2"2"4"44 4 4 4 4 	4r   c           	      4   i }|j         }|                    dt          j                  }t	          |t
                    r|j        }|                                D ]R}|j        dk    rE|t          |j
                           }	|	|vr'	 ||	         }
n# t          $ r ||	         }
Y nw xY w|
||	<   S|D ]h}t	          |t                    rQt          t          |                    }|                    |                     |||j        |j                             i|S )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   rw   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LLNN 	$ 	$D{m++ 4TX > >?q==/ %d# / / / (/#AdG 	O 	OB"h'' O&|B'7'78822435<N N O O Os   BB$#B$c                 d    |                      | 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   r9   s    r   get_used_globalsz_ByteCode.get_used_globals:  s3    
 ))$,*;TZ*..$-I I 	Ir   N)rY   rZ   r[   r\   r]   r6   classmethodr   r|   r   r   r   r   r   r_   r   r   r   r      s         $I% % %(   [$) ) )" " "$ $ $	4 	4 	4   [8I I I I Ir   r   c                 @    t           dk    rt           dk    sJ | dz	  S | S )Nr@   rL   r
   )r/   s    r   r   r   C  s,    GG####axJr   c                   $     e Zd Z fdZd Z xZS )ByteCodePy311c                     t                                          |           d }t          j        |j                  j        }t          t          ||                    | _        d S )Nc                     ddl m}  || j        t          z   | j        t          z   | j        t          z   | j        | j                  }|S )Nr   )_ExceptionTableEntry)startendtargetdepthlasti)r   r   r   rs   r   r   r   r   )entr   outs      r   fixup_ehz(ByteCodePy311.__init__.<locals>.fixup_ehN  sZ    000000 '&i-/SW}5LzM1isy  C
 Jr   )superr6   r   Bytecoderk   r   tuplemap)r4   r   r   entries	__class__s       r   r6   zByteCodePy311.__init__K  s`    !!!		 		 		 ,w|,,>!&s8W'='=!>!>r   c                     g }| j         D ]8}|j        |cxk    r|j        k    rn |                    |j        |f           9|rt          |          d         }|S dS )zN
        Returns the exception entry for the given instruction offset
        rL   N)r   r   r   r!   r   max)r4   r+   
candidatesr   s       r   find_exception_entryz"ByteCodePy311.find_exception_entry\  s     
) 	4 	4CyF----cg-----!!39c"2333 	j//!$CJ	 	r   )rY   rZ   r[   r6   r   __classcell__)r   s   @r   r   r   J  sG        ? ? ? ? ?"
 
 
 
 
 
 
r   r   r@   c                   j    e Zd ZdZ ej        d          Zed             Zd Z	d Z
ed             ZdS )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, e.g. in the `@generated_jit` case).
    rL   c                    t          |          }t          |          }t          j        |          }|st	          j        d|z            	 |j        }n# t          $ r
 |j        }Y nw xY w |             }||_	        ||_
        |                    d          d         |_        ||_        t          j        |          |_        |j        t          j        n|j        j        |_        t          j        |          |_        ||_        |j        |_        |j        |_        t5          |j                  |_        t;          |j                  |_        t?          | j                   }d!                    |j
        |          |_"        ||_#        |S )zD
        Create the FunctionIdentity of the given function.
        z %s does not provide its bytecode.r2   Nz{}${})$r   r   r   pysignaturer   ByteCodeSupportErrorr[   AttributeErrorrY   r   func_qualnamesplit	func_namerk   inspect	getmodulemodule_dynamic_modnamemodnameisgeneratorfunctionis_generatorpysigco_filenamefilenamer   firstlinenorc   
parameters	arg_countlist	arg_namesr,   _unique_idsformatunique_name	unique_id)r   pyfuncr   rk   r   r   r4   uids           r   from_functionzFunctionIdentity.from_function{  s   
 #6**t$$!$'' 	;-2T9; ; ;	* -MM 	* 	* 	* MMMM	* suu	*&,,S11"5	'--;. ..![1 	 $7==
(.U-..e.//
 3?##">>$*<cBBs   A A)(A)c                 6    |                      | j                  S )z:Copy the object and increment the unique counter.
        )r  r   r9   s    r   derivezFunctionIdentity.derive  s     !!$),,,r   c                 ,    t          | j                  S )4
        NOTE: part of ReduceMixin protocol
        )r   )dictr   r9   s    r   _reduce_stateszFunctionIdentity._reduce_states  s     49%%%%r   c                 ,    |                      |          S )r  )r  )r   r   s     r   _rebuildzFunctionIdentity._rebuild  s    
   (((r   N)rY   rZ   r[   r\   	itertoolscountr   r   r  r  r  r	  r_   r   r   r   r   q  s          ")/!$$K' ' ['R- - -
& & & ) ) [) ) )r   r   )/collectionsr   r   r   r   r
  typesr   r   
numba.corer   r   r	   numba.core.utilsr   r   rs   r   r   r&   	frozensethasjrelrO   hasjabsrP   r8   r=   rh   re   objectr*   rd   rg   rj   r.   indexri   rq   ru   rw   r   r   r   ByteCoderN   ReduceMixinr   r_   r   r   <module>r     s   / / / / / / / / 



      & & & & & & & & / / / / / / / / / / & & & & & &j44
 	 	 	E E E
   9S[!!9S[!!h9[[./!BCCDD!N N N N N6 N N Nb 

Ze$$
! ! !HN N N    6   4fI fI fI fI fI fI fI fIR      I   > HHHH

i
(
((D) D) D) D) D)y, D) D) D) D) D)r   