U
    9%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.
    Z	__numba__Ngetattr)objattr r   R/var/www/html/Darija-Ai-API/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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   )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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   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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   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

