U
    hâËdš5  ã                   @   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.
    Z	__numba__N©Úgetattr)ÚobjÚattr© r   úL/home/sam/Atlas/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_OPSr/   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s°t‚| 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   )r4   é
   )
r0   ÚAssertionErrorr	   r%   r#   r'   ÚNotImplementedErrorÚJREL_OPSr$   ÚJABS_OPSr/   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(   r/   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_r9   Ú	POP_BLOCKr)   r   N)r&   Ú
startswithr/   r   r   r   Úblock_effectx   s
    
zByteCodeInst.block_effectN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r-   Úpropertyr0   r2   r?   r@   rC   r   r   r   r   r"   4   s   

"r"   r9   Ú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*    rZ   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)rR   Ú_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   )rT   Úiterr\   rZ   Úco_code)r+   rT   r   r   r   r-   Á   s    zByteCodeIter.__init__c                 C   s   | S r   r   r/   r   r   r   Ú__iter__Å   s    zByteCodeIter.__iter__c                 C   s
   t | jƒS r   )r$   r^   r/   r   r   r   Ú_fetch_opcodeÈ   s    zByteCodeIter._fetch_opcodec                 C   s$   |   ¡ \}}}}|t||||d�fS )N)r#   r%   r'   r,   )ra   r"   r*   r   r   r   r$   Ë   s    
ÿzByteCodeIter.nextc                 C   s4   d}t |ƒD ]"}t| jƒ\}}||d| > O }q|S )Nr   rK   )rO   r$   r^   )r+   ÚsizeÚbufrW   Ú_offsetÚbyter   r   r   Úread_argÒ   s
    zByteCodeIter.read_argN)	rD   rE   rF   r-   r`   ra   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[   )r6   Úxr   r   r   r8   å   s     z%_ByteCode.__init__.<locals>.<genexpr>r   )rT   Úsetr   Ú
findlabelsr_   Úaddr   r]   Ú_compute_linenori   rj   rk   rl   rm   rn   rp   Úsortedrq   )r+   ri   rT   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   rT   r#   r(   Z
adj_offsetZknownÚinstr   r   r   rv   ö   s    
z_ByteCode._compute_linenoc                 C   s   t | j ¡ ƒS r   )r^   rp   rz   r/   r   r   r   r`   	  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 )Nr9   ú>ú )r#   rq   )rW   r/   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 )r9   ZCACHEz
%s %10s	%sN)r&   )r6   rW   )rƒ   r   r   r8     s   þz!_ByteCode.dump.<locals>.<genexpr>)Újoinrp   Úitemsr/   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ƒrŒt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   ÚdZ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   r/   r   r   r   Úget_used_globals:  s     ÿz_ByteCode.get_used_globalsN)rD   rE   rF   rG   rH   r-   Úclassmethodrv   r`   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 )Nr3   r9   )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   ÚBytecoderT   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
        r9   N)ro   r›   rœ   r   rž   Úmax)r+   r#   Ú
candidatesr    r   r   r   Úfind_exception_entry\  s    
z"ByteCodePy311.find_exception_entry)rD   rE   rF   r-   r¬   Ú__classcell__r   r   r¨   r   r™   J  s   r™   r3   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).
    r9   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r–t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   Zpysignaturer   ZByteCodeSupportErrorrF   ÚAttributeErrorrD   r’   Úfunc_qualnameÚsplitÚ	func_namerT   ÚinspectÚ	getmoduleÚmoduleZ_dynamic_modnameÚmodnameÚisgeneratorfunctionZis_generatorÚpysigÚco_filenameÚfilenamery   ZfirstlinenorL   Ú
parametersÚ	arg_countÚlistÚ	arg_namesr$   Ú_unique_idsÚformatZunique_nameÚ	unique_id)r{   Úpyfuncr’   rT   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’   r/   r   r   r   Úderive¥  s    zFunctionIdentity.derivec                 C   s   t | jd�S )ú4
        NOTE: part of ReduceMixin protocol
        )rÃ   )Údictr’   r/   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)rD   rE   rF   rG   Ú	itertoolsÚcountrÀ   r˜   rÅ   rÆ   rÉ   rÊ   r   r   r   r   r®   q  s   

)r®   )/Úcollectionsr   r   r   r´   rË   Útypesr   r   Z
numba.corer   r   r   Znumba.core.utilsr	   r
   r[   r   r   r   Ú	frozensetÚhasjrelr=   Úhasjabsr>   r.   r1   rQ   rN   Úobjectr"   rM   rP   rS   r&   ÚindexrR   rZ   r\   r]   rh   rŽ   r™   ZByteCoder<   ZReduceMixinr®   r   r   r   r   Ú<module>   sD   	

Q$i

