U
    |e5                     @   sf  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	d
 Zdd Zdd ZeejZeejZeeB ZeeddgZejZejZG dd deZdZdZ dZ!ej"#dZ$dd Z%dd Z&G dd deZ'G dd deZ(dd Z)G dd  d e(Z*ed!kr8e*Z+ned!k rHe(Z+ne,eG d"d# d#ej-Z.dS )$    )
namedtupleOrderedDictN)CodeType
ModuleType)errorsutils	serialize)	PYVERSIONopcode_infoZargsize   c                 C   s   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attr r   P/var/www/website-v5/atlas_env/lib/python3.8/site-packages/numba/core/bytecode.pyget_function_object   s    
r   c                 C   s   t | dt | ddS )z"Shamelessly borrowed from llpython__code__	func_codeNr   )r   r   r   r   get_code_object   s    r   c                 C   s0   g }| D ]"}t j|}|d k	r|| q|S N)disopmapgetappend)seqlstscr   r   r   _as_opcodes#   s    r    RETURN_VALUERAISE_VARARGSc                   @   sP   e Zd ZdZdZdd Zedd Zedd Zd	d
 Z	dd Z
ed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                 C   s.   || _ || _|| _tj| | _|| _d| _d S )N)r$   r%   r&   r   r'   r(   r)   selfr$   r&   r(   
nextoffsetr   r   r   __init__C   s    zByteCodeInst.__init__c                 C   s
   | j tkS r   )r&   JUMP_OPSr,   r   r   r   is_jumpK   s    zByteCodeInst.is_jumpc                 C   s
   | j tkS r   )r&   TERM_OPSr0   r   r   r   is_terminatorO   s    zByteCodeInst.is_terminatorc                 C   s   | j s
ttdkr<| jdd dD krL| j| jd d  S ntdkrLtttdkr| jtkrn| j| jd  S | jt	ks|t| jd d S n*| jtkr| j| j S | jt	kst| jS d S )N      c                 s   s   | ]}t j| V  qd S r   )r   r   ).0kr   r   r   	<genexpr>^   s   z/ByteCodeInst.get_jump_target.<locals>.<genexpr>)ZJUMP_BACKWARDZPOP_JUMP_BACKWARD_IF_TRUEZPOP_JUMP_BACKWARD_IF_FALSEZPOP_JUMP_BACKWARD_IF_NONEZPOP_JUMP_BACKWARD_IF_NOT_NONE   r   )r5   
   )
r1   AssertionErrorr	   r&   r$   r(   NotImplementedErrorJREL_OPSr%   JABS_OPSr0   r   r   r   get_jump_targetS   s"    	



zByteCodeInst.get_jump_targetc                 C   s   d| j | j| jf S )Nz%s(arg=%s, lineno=%d))r'   r(   r)   r0   r   r   r   __repr__u   s    zByteCodeInst.__repr__c                 C   s&   | j drdS | j dkrdS dS dS )zREffect of the block stack
        Returns +1 (push), 0 (none) or -1 (pop)
        ZSETUP_r:   	POP_BLOCKr*   r   N)r'   
startswithr0   r   r   r   block_effectx   s
    
zByteCodeInst.block_effectN)__name__
__module____qualname____doc__	__slots__r.   propertyr1   r3   r@   rA   rD   r   r   r   r   r#   4   s   

"r#   r:   NOPc                 c   s   d}t | }d }}||k r| | }|t7 }|tkr| | |B }ttD ]}|| ||  d| > O }qH|t7 }|tkr|t||fV  |dt > }|}qnd}|t7 }d}||||fV  |}q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(   jr   r   r   _unpack_opargs   s*    r[   c                 c   sJ   dt dtfV  | D ]2\}}}}|tkr.|t7 }|t |||t fV  qdS )zpPatch the bytecode stream.

    - Adds a NOP bytecode at the start to avoid jump target being at the entry.
    r   N)rS   _FIXED_OFFSETr?   )Z	bc_streamr$   r&   r(   r-   r   r   r   _patched_opargs   s
    r]   c                   @   s8   e Zd Zdd Zdd Zdd Zdd ZeZd	d
 ZdS )ByteCodeIterc                 C   s    || _ ttt| j j| _d S r   )rU   iterr]   r[   co_code)r,   rU   r   r   r   r.      s    zByteCodeIter.__init__c                 C   s   | S r   r   r0   r   r   r   __iter__   s    zByteCodeIter.__iter__c                 C   s
   t | jS r   )r%   r_   r0   r   r   r   _fetch_opcode   s    zByteCodeIter._fetch_opcodec                 C   s$   |   \}}}}|t||||dfS )N)r$   r&   r(   r-   )rb   r#   r+   r   r   r   r%      s    
zByteCodeIter.nextc                 C   s4   d}t |D ]"}t| j\}}||d| > O }q|S )Nr   rL   )rP   r%   r_   )r,   sizebufrX   Z_offsetbyter   r   r   read_arg   s
    zByteCodeIter.read_argN)	rE   rF   rG   r.   ra   rb   r%   __next__rf   r   r   r   r   r^      s   r^   c                   @   s\   e Zd ZdZdZdd Zedd Zdd Zd	d
 Z	dd Z
dd Zedd Zd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                 C   s   |j }tdd t|jD }|d tt|}| || || _	|j
| _
|j| _|j| _|j| _|j| _|| _t|| _d S )Nc                 s   s   | ]}|t  V  qd S r   )r\   )r7   xr   r   r   r9      s     z%_ByteCode.__init__.<locals>.<genexpr>r   )rU   setr   
findlabelsr`   addr   r^   _compute_linenori   rj   rk   rl   rm   rn   rp   sortedrq   )r,   ri   rU   rq   rp   r   r   r   r.      s    
z_ByteCode.__init__c                 C   s^   t |D ]"\}}|t }||kr
||| _q
|j}| D ]}|jdkrR|j}q<||_q<|S )zI
        Compute the line numbers for all bytecode instructions.
        r   )r   findlinestartsr\   r)   co_firstlinenovalues)clsrp   rU   r$   r)   Z
adj_offsetZknowninstr   r   r   rv      s    
z_ByteCode._compute_linenoc                 C   s   t | j S r   )r_   rp   rz   r0   r   r   r   ra   	  s    z_ByteCode.__iter__c                 C   s
   | j | S r   rp   r,   r$   r   r   r   __getitem__  s    z_ByteCode.__getitem__c                 C   s
   || j kS r   r}   r~   r   r   r   __contains__  s    z_ByteCode.__contains__c                    s*   fdd d  fddj D S )Nc                    s   | d j  jkrdS dS d S )Nr:   > )r$   rq   )rX   r0   r   r   label_marker  s    z$_ByteCode.dump.<locals>.label_marker
c                 3   s.   | ]&}|d  j dkrd |f|  V  qdS )r:   CACHEz
%s %10s	%sN)r'   )r7   rX   )r   r   r   r9     s   z!_ByteCode.dump.<locals>.<genexpr>)joinrp   itemsr0   r   )r   r,   r   dump  s    z_ByteCode.dumpc              	   C   s   i }|j }|dtj}t|tr(|j}| D ]V}|jdkr0|t	|j
 }	|	|kr0z||	 }
W n tk
r|   ||	 }
Y nX |
||	< q0|D ]4}t|trtt|}|| |||j|j q|S )za
        Compute the globals used by the function with the given
        bytecode table.
        __builtins__LOAD_GLOBAL)__globals__r   r   builtins
isinstancer   __dict__rz   r'   _fix_LOAD_GLOBAL_argr(   KeyErrorr   r   r^   update_compute_used_globalsrl   rj   )r{   funcrp   rl   rj   dglobsr   r|   namevaluecoZsubtabler   r   r   r     s,    



 z_ByteCode._compute_used_globalsc                 C   s   |  | 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   ri   r   rp   rl   rj   r0   r   r   r   get_used_globals:  s     z_ByteCode.get_used_globalsN)rE   rF   rG   rH   rI   r.   classmethodrv   ra   r   r   r   r   r   r   r   r   r   rh      s   

rh   c                 C   s    t dkrt dkst| d? S | S )Nr4   r:   )r	   r<   )r(   r   r   r   r   C  s    r   c                       s$   e Zd Z fddZdd Z  ZS )ByteCodePy311c                    s6   t  | dd }t|jj}tt||| _d S )Nc                 S   s8   ddl m} || jt | jt | jt | j| jd}|S )Nr   )_ExceptionTableEntry)startendtargetdepthlasti)r   r   r   r\   r   r   r   r   )entr   outr   r   r   fixup_ehN  s      z(ByteCodePy311.__init__.<locals>.fixup_eh)superr.   r   BytecoderU   ro   tuplemap)r,   ri   r   entries	__class__r   r   r.   K  s    zByteCodePy311.__init__c                 C   sT   g }| j D ]0}|j|  kr&|jkr
n q
||j|f q
|rPt|d }|S dS )zN
        Returns the exception entry for the given instruction offset
        r:   N)ro   r   r   r   r   max)r,   r$   
candidatesr   r   r   r   find_exception_entry\  s    
z"ByteCodePy311.find_exception_entry)rE   rF   rG   r.   r   __classcell__r   r   r   r   r   J  s   r   r4   c                   @   sB   e Zd ZdZedZedd Zdd Z	dd Z
ed	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).
    r:   c                 C   s   t |}t|}t|}|s,td| z
|j}W n tk
rP   |j}Y nX |  }||_	||_
|dd |_||_t||_|jdkrtjn|jj|_t||_||_|j|_|j|_t|j|_t|j|_t| j }d!|j
||_"||_#|S )zD
        Create the FunctionIdentity of the given function.
        z %s does not provide its bytecode.r*   Nz{}${})$r   r   r   pysignaturer   ByteCodeSupportErrorrG   AttributeErrorrE   r   func_qualnamesplit	func_namerU   inspect	getmodulemodule_dynamic_modnamemodnameisgeneratorfunctionis_generatorpysigco_filenamefilenamery   firstlinenorM   
parameters	arg_countlist	arg_namesr%   _unique_idsformatunique_name	unique_id)r{   pyfuncr   rU   r   r   r,   uidr   r   r   from_function{  s>    


zFunctionIdentity.from_functionc                 C   s   |  | jS )z:Copy the object and increment the unique counter.
        )r   r   r0   r   r   r   derive  s    zFunctionIdentity.derivec                 C   s   t | jdS )4
        NOTE: part of ReduceMixin protocol
        )r   )dictr   r0   r   r   r   _reduce_states  s    zFunctionIdentity._reduce_statesc                 C   s
   |  |S )r   )r   )r{   r   r   r   r   _rebuild  s    zFunctionIdentity._rebuildN)rE   rF   rG   rH   	itertoolscountr   r   r   r   r   r   r   r   r   r   r   q  s   

)r   )/collectionsr   r   r   r   r   typesr   r   
numba.corer   r   r   Znumba.core.utilsr	   r
   r\   r   r   r    	frozensethasjrelr>   hasjabsr?   r/   r2   rR   rO   objectr#   rN   rQ   rT   r'   indexrS   r[   r]   r^   rh   r   r   ByteCoder=   ReduceMixinr   r   r   r   r   <module>   sD   	

Q$i

