U
    9%e                    @   sB  d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZmZm	Z	 d dl
mZmZmZ d dlmZmZ d dlmZmZmZ d dlmZmZmZmZ d dlmZ d dlmZ G d	d
 d
eZe e!Z"G dd deZ#dd Z$dd Z%dd Z&dd Z'dd Z(dd Z)dd Z*dd Z+dd Z,dd  Z-d!d" Z.G d#d$ d$eZ/dS )%    N)errorsirconfig)NotDefinedErrorUnsupportedErrorerror_extras)get_definitionguard)	PYVERSIONBINOPS_TO_OPERATORSINPLACE_BINOPS_TO_OPERATORS)FlowAdaptDFAAdaptCFA	BlockKind)eh)unpack_single_tuplec                   @   s    e Zd ZdZdd Zdd ZdS )_UNKNOWN_VALUEzNRepresents an unknown value, this is for ease of debugging purposes only.
    c                 C   s
   || _ d S N)_varname)selfvarname r   U/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/numba/core/interpreter.py__init__   s    z_UNKNOWN_VALUE.__init__c                 C   s   d | jS )Nz_UNKNOWN_VALUE({}))formatr   r   r   r   r   __repr__   s    z_UNKNOWN_VALUE.__repr__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s   r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	Assignera  
    This object keeps track of potential assignment simplifications
    inside a code block.
    For example `$O.1 = x` followed by `y = $0.1` can be simplified
    into `y = x`, but it's not possible anymore if we have `x = z`
    in-between those two instructions.

    NOTE: this is not only an optimization, but is actually necessary
    due to certain limitations of Numba - such as only accepting the
    returning of an array passed as function argument.
    c                 C   s   i | _ tt| _t | _d S r   )dest_to_srccollectionsdefaultdictlistsrc_invalidatesetunused_destsr   r   r   r   r   -   s    zAssigner.__init__c                 C   sx   |j }|j }|| jkr4| j|D ]}| j| q"|| jkrH| j| }|jrt|| j|< | j| | | j| |S )z
        Assign *srcvar* to *destvar*. Return either *srcvar* or a possible
        simplified assignment source (earlier assigned to *srcvar*).
        )namer'   popr#   is_tempappendr)   add)r   ZsrcvarZdestvarsrcnamedestnamedr   r   r   assign5   s    



zAssigner.assignc                 C   s$   || j kr| j | S | j| dS )zu
        Get a possible assignment source (a ir.Var instance) to replace
        *destname*, otherwise None.
        N)r#   r)   discard)r   r0   r   r   r   get_assignment_sourceI   s    

zAssigner.get_assignment_sourceN)r   r   r    r!   r   r2   r4   r   r   r   r   r"       s   r"   c                 C   s\   | | j j}| | j}||j| krD|j| | || | n||| krXtddS )a  
    Deletes the definition defined for old_body at index idx
    from func_ir. We assume this stmt will be deleted from
    new_body.

    In some optimizations we may update the same variable multiple times.
    In this situation, we only need to delete a particular definition once,
    this is tracked in already_deleted_def, which is a map from
    assignment name to the set of values that have already been
    deleted.
    zInconsistency found in the definitions while executing a peephole optimization. This suggests an internal error or inconsistency elsewhere in the compiler.N)targetr*   value_definitionsremover.   r   )old_bodyidxfunc_iralready_deleted_defslhsrhsr   r   r   _remove_assignment_definitionT   s    
r?   c           
      C   sP   |j  }|j}|  D ]\}}	|||	 d f||	< qd||< t| ||| |S )a  
    Extracts the kws args passed as varkwarg
    for CALL_FUNCTION_EX. This pass is taken when
    n_kws <= 15 and the bytecode looks like:

        # Start for each argument
        LOAD_FAST  # Load each argument.
        # End for each argument
        ...
        BUILD_CONST_KEY_MAP # Build a map

    In the generated IR, the varkwarg refers
    to a single build_map that contains all of the
    kws. In addition to returning the kws, this
    function updates new_body to remove all usage
    of the map.
       N)itemscopyvalue_indexesr?   )
r9   Zkeyword_exprnew_bodybuildmap_idxr;   r<   kwsrC   keyindexr   r   r   #_call_function_ex_replace_kws_smallm   s    
   rI   c                 C   s  d||< t | ||| g }|d }	|	|kr| |	 }
t|
tjrNt|
jtjsVt||
jj}|
jj}|	d7 }	d}|	|kr|s| |	 }t|tjrt|jtj	r|jj
dkr|jjj|kr|jjdkrd}qr|	d7 }	qr|r|	|krt|| |	d  }t|tjr\t|jtj	r\|jj
dkr\|jjj|jjkr\t|jjdkr\|jjd	 j|ksdt||jjd }|||f d||	< d||	d < t | |	|| t | |	d || |	d7 }	q"|S )
a  
    Extracts the kws args passed as varkwarg
    for CALL_FUNCTION_EX. This pass is taken when
    n_kws > 15 and the bytecode looks like:

        BUILD_MAP # Construct the map
        # Start for each argument
        LOAD_CONST # Load a constant for the name of the argument
        LOAD_FAST  # Load each argument.
        MAP_ADD # Append the (key, value) pair to the map
        # End for each argument

    In the IR generated, the initial build map is empty and a series
    of setitems are applied afterwards. THE IR looks like:

        $build_map_var = build_map(items=[])
        $constvar = const(str, ...) # create the const key
        # CREATE THE ARGUMENT, This may take multiple lines.
        $created_arg = ...
        $var = getattr(
            value=$build_map_var,
            attr=__setitem__,
        )
        $unused_var = call $var($constvar, $created_arg)

    We iterate through the IR, deleting all usages of the buildmap
    from the new_body, and adds the kws to a new kws list.
    Nr@   Fgetattr__setitem__Tcall   r   )r?   
isinstancer   Assignr6   Constr   r5   r*   Expropattrfunclenargsr-   )r9   Zbuildmap_namerE   
search_endrD   r;   errmsgr<   rF   search_startZ
const_stmtZkey_var_nameZkey_valZfound_getattrgetattr_stmtZsetitem_stmtZarg_varr   r   r   #_call_function_ex_replace_kws_large   s    '   









      
r[   c                 C   s   d||< t | ||| |jS )a  
    Extracts the args passed as vararg
    for CALL_FUNCTION_EX. This pass is taken when
    n_args <= 30 and the bytecode looks like:

        # Start for each argument
        LOAD_FAST  # Load each argument.
        # End for each argument
        ...
        BUILD_TUPLE # Create a tuple of the arguments

    In the IR generated, the vararg refer
    to a single build_tuple that contains all of the
    args. In addition to returning the args, this
    function updates new_body to remove all usage
    of the tuple.
    N)r?   rA   )r9   Z
tuple_exprrD   Zbuildtuple_idxr;   r<   r   r   r   $_call_function_ex_replace_args_small!  s       r\   c                 C   sf  d}g }t |tjrJt |jtjrJ|jj}	d||< t| ||| |d8 }ntd||krF| | }
t |
tjr|
jj|	krt |
jtj	r|
jj
dkr|
jjsd||< t| ||| qFqR||kst |
tjr|
jj|	krt |
jtj	r|
jj
dkr|
jjtjkst||
jjj}|
jjj}| |d  }t |tjrtt |jtj	rt|jj
dkrtt|jjdks|t||jj|krtdn|jj|kr|}	nt|||jjd  d||< d||d < t| ||| t| |d || |d8 }d}||krR|rR| | }t |tjr8|jj|	kr8d	}n|d8 }qqR||krXt||ddd
 S )a  
    Extracts the args passed as vararg
    for CALL_FUNCTION_EX. This pass is taken when
    n_args > 30 and the bytecode looks like:

        BUILD_TUPLE # Create a list to append to
        # Start for each argument
        LOAD_FAST  # Load each argument.
        LIST_APPEND # Add the argument to the list
        # End for each argument
        ...
        LIST_TO_TUPLE # Convert the args to a tuple.

    In the IR generated, the tuple is created by concatenating
    together several 1 element tuples to an initial empty tuple.
    We traverse backwards in the IR, collecting args, until we
    find the original empty tuple. For example, the IR might
    look like:

        $orig_tuple = build_tuple(items=[])
        $first_var = build_tuple(items=[Var(arg0, test.py:6)])
        $next_tuple = $orig_tuple + $first_var
        ...
        $final_var = build_tuple(items=[Var(argn, test.py:6)])
        $final_tuple = $prev_tuple + $final_var
        $varargs_var = $final_tuple
    r   Nr@   unreachablebuild_tuplebinoprM   TF)rN   r   rO   r6   Varr*   r?   AssertionErrorr5   rQ   rR   rA   fnoperatorr.   r   r=   r>   rU   r-   )r9   Zvararg_stmtrD   rW   r;   rX   r<   rY   Z
total_argsZtarget_nameZconcat_stmtZlhs_nameZrhs_nameZarg_tuple_stmtZkeep_lookingZ	next_stmtr   r   r   $_call_function_ex_replace_args_largeD  s    &
   




   

 	




 	

      

re   c                 C   sx  t d}tt}| j D ]R}d}g }t|jD ]\}}t	|t
jrt	|jt
jr|jjdkr|jjdk	rd}|j}|j}	|j}
|j}|j}|d }|}d}d}|dkr|s|j| }t	|t
jr|jj|jkrd}q|d8 }q|
s|rt	|jt
jr|jjdks"t||jjrDt|j|j||| |}
nt|j|j||d || ||}
|}|dk	rV|	r~t||}d}d}|dkr|s|j| }t	|t
jr|jj|jkrd}n|d8 }q|st|t	|jt
jr|jjd	krt|j|j||| |}	n>t	|jt
jr@|jjd
kr@t|nt|j|||| ||}	t
jj|j|	|
|j|jd}t|j|| | t
||j|j}| j|jj  | nt	|t
jrBt	|jt
jrB|jjdkrB|jjdk	rB|j}|jj}|| jkrBt!| j| dkrB| j| d }t	|t
jrB|jd
krBt|| | q6|r|j"  |j#dd |D  q| S )a_  
    This peephole rewrites a bytecode sequence unique to Python 3.10
    where CALL_FUNCTION_EX is used instead of CALL_FUNCTION_KW because of
    stack limitations set by CPython. This limitation is imposed whenever
    a function call has too many arguments or keyword arguments.

    https://github.com/python/cpython/blob/a58ebcc701dd6c43630df941481475ff0f615a81/Python/compile.c#L55
    https://github.com/python/cpython/blob/a58ebcc701dd6c43630df941481475ff0f615a81/Python/compile.c#L4442

    In particular, this change is imposed whenever (n_args / 2) + n_kws > 15.

    Different bytecode is generated for args depending on if n_args > 30
    or n_args <= 30 and similarly if n_kws > 15 or n_kws <= 15.

    This function unwraps the *args and **kwargs in the function call
    and places these values directly into the args and kwargs of the call.
    a  
        CALL_FUNCTION_EX with **kwargs not supported.
        If you are not using **kwargs this may indicate that
        you have a large number of kwargs and are using inlined control
        flow. You can resolve this issue by moving the control flow out of
        the function call. For example, if you have

            f(a=1 if flag else 0, ...)

        Replace that with:

            a_val = 1 if flag else 0
            f(a=a_val, ...)FrL   NTr@   r   	build_mapr^   list_to_tuple)r5   c                 S   s   g | ]}|d k	r|qS r   r   .0xr   r   r   
<listcomp>  s      zBpeep_hole_call_function_ex_to_call_function_kw.<locals>.<listcomp>)$textwrapdedentr$   r%   r(   blocksvalues	enumeratebodyrN   r   rO   r6   rQ   rR   varkwargrV   rF   varargr5   r*   r   rA   rI   r[   r\   re   rL   rT   locr?   r7   r-   rU   clearextend)r;   rX   r<   blkblk_changedrD   istmtrL   rV   rF   rs   rr   Zstart_searchZvarkwarg_locZkeyword_deffoundZ
vararg_locZargs_defZnew_callZvararg_nameexprr   r   r   .peep_hole_call_function_ex_to_call_function_kw  s   














	


       


	
r}   c              	      s$  d}j  D ]\}  fdd}| }|dk	r j|d d |d d  }|rxtd |D ]}t| qbtd g }g }|d d }	|	jj}
|D ]x}t|tjrt|j	tj
r|j	}|jd	kr|j	j|
kr|jd
kr||jj q|jdkr||jj qdstqg fdd}|	j}|r@td    |d d }|}|D ]}t|tjr^t|j	tj
rR|j	}|jd	kr|jj|ks|jj|krj|jj qTn
| q\|jdkr|jj}||ks||kr|jd }t|tjrd||jf }||kr6tj
|g|j}nTtjdt|jd}|jjd|jd}tj|||jd tj
j||fd|jd}t|j||j}t|||j}|| |}tj
jtj|||jd}t||j|j}|| |j}n
| nR|jdkrF|jj|
krFtj
|j|j}t||j|j}|| n
| n
| n
| qT|t||j|j |rtd D ]}t| q jdd }|d|d d  } j|d d d d }| | } j   j | |rtd    qqqqS )a  
    This peephole rewrites a bytecode sequence new to Python 3.9 that looks
    like e.g.:

    def foo(a):
        return (*a,)

    41          0 BUILD_LIST               0
                2 LOAD_FAST                0 (a)
                4 LIST_EXTEND              1
                6 LIST_TO_TUPLE
                8 RETURN_VAL

    essentially, the unpacking of tuples is written as a list which is appended
    to/extended and then "magicked" into a tuple by the new LIST_TO_TUPLE
    opcode.

    This peephole repeatedly analyses the bytecode in a block looking for a
    window between a `LIST_TO_TUPLE` and `BUILD_LIST` and...

    1. Turns the BUILD_LIST into a BUILD_TUPLE
    2. Sets an accumulator's initial value as the target of the BUILD_TUPLE
    3. Searches for 'extend' on the original list and turns these into binary
       additions on the accumulator.
    4. Searches for 'append' on the original list and turns these into a
       `BUILD_TUPLE` which is then appended via binary addition to the
       accumulator.
    5. Assigns the accumulator to the variable that exits the peephole and the
       rest of the block/code refers to as the result of the unpack operation.
    6. Patches up
    Fc                     s   d} t tt jD ]|} j| }t|tjrb|j}t|tjrb|j	dkrb|j
d }d} ||f}| rt|tjr|jj|kr|||ff}|  S qd S )NFrg   r   T)reversedrangerU   rq   rN   r   rO   r6   rQ   rR   infor5   r*   )r{   r:   rz   r6   Ztarget_listbtregion)rw   r   r   find_postive_region  s     

z4peep_hole_list_to_tuple.<locals>.find_postive_regionNr@   r   z
WINDOW: rJ   rv   r-   c                    s^    |  | jj jkrDt j| jj dks2t j| jj    j| jj  | j dS )z. Adds to the new_hole and fixes up definitionsr@   N)r-   r5   r*   r7   rU   rb   ru   r6   rj   )r;   new_holer   r   append_and_fixJ  s
    
z/peep_hole_list_to_tuple.<locals>.append_and_fixz
BLOCK:rL   z	%s_var_%stupler*   r6   rt   $_list_extend_gv_tuplert   )r5   r6   rt   r   rc   r=   r>   rt   
build_listz

NEW HOLE:z
DUMP post hole:)!rn   rA   rq   printr5   r*   rN   r   rO   r6   rQ   rR   rS   r-   rb   dumpr7   r+   rT   rV   ra   r^   rt   Globalr   scoperedefinerL   r_   rd   r.   ru   rv   )r;   _DEBUGoffsetr   r   Z	peep_holerj   appendsZextendsinit
const_listr|   r   Zthe_build_listZt2l_agnaccfnameargtmp_namer   gv_tuple	tuple_varvarZasgnnewcpyheadtailtmpr   )rw   r;   r   r   peep_hole_list_to_tuple  s     










   





r   c                 C   s   t  }| j D ]}|jD ]P}t | }|D ]}|jdr.|| q.||@ rt|t	j
r||j qg }|jD ]}t | |@ sv|| qv|j  |j| q| S )zh
    This rewrite removes variables used to store the `__exit__` function
    loaded by SETUP_WITH.
    z$setup_with_exitfn)r(   rn   ro   rq   	list_varsr*   
startswithr.   rN   r   rO   r5   r-   ru   rv   )r;   Z	dead_varsrw   rz   usedvrD   r   r   r   peep_hole_delete_with_exit  s"    


r   c              	   C   s8  t d}tt}| j D ]}g }i }tt}i }d}t|j	D ]\}	}
|
}d}t
|
tjrxt
|
jtjrx|
jjdkr|
jj}|	||
jj< ||
jj |	 |
jj ||
jj< n|
jjdkrx|	dkrx|
jjj}|j	|	d  }|
jj}t
|tjrx|jj|krxt
|jtjrx|jjdkrx|jjd	krx|jjj}|jj}|d
kr||kr|| | || |	d |	g n|dkrx|d j}||krp||krp|| ||  || ||  || |	d  || D ]}t|j	|| | d||< q||= ||= ||= t|j	|	| | t| ||j	|| || }||   || |	 d}nt|t
|
tjrt
|
jtjr|
jjdkr|
jjj|kr|
jjd	ks|
 D ]6}|j|kr|j|kr||j= ||j= ||j= q|| qH|r|j	  |j	dd |D  q| S )a  
    This rewrite removes d1._update_from_bytecode(d2)
    calls that are between two dictionaries, d1 and d2,
    in the same basic block. This pattern can appear as a
    result of Python 3.10 bytecode emission changes, which
    prevent large constant literal dictionaries
    (> 15 elements) from being constant. If both dictionaries
    are constant dictionaries defined in the same block and
    neither is used between the update call, then we replace d1
    with a new definition that combines the two dictionaries. At
    the bytecode translation stage we convert DICT_UPDATE into
    _update_from_bytecode, so we know that _update_from_bytecode
    always comes from the bytecode change and not user code.

    Python 3.10 may also rewrite the individual dictionaries
    as an empty build_map + many map_add. Here we again look
    for an _update_from_bytecode, and if so we replace these
    with a single constant dictionary.

    When running this algorithm we can always safely remove d2.

    This is the relevant section of the CPython 3.10 that causes
    this bytecode change:
    https://github.com/python/cpython/blob/3.10/Python/compile.c#L4048
    a  
        A DICT_UPDATE op-code was encountered that could not be replaced.
        If you have created a large constant dictionary, this may
        be an an indication that you are using inlined control
        flow. You can resolve this issue by moving the control flow out of
        the dicitonary constructor. For example, if you have

            d = {a: 1 if flag else 0, ...)

        Replace that with:

            a_val = 1 if flag else 0
            d = {a: a_val, ...)FNrf   rL   r   r@   rJ   )rK   _update_from_bytecoderK   r   Tc                 S   s   g | ]}|d k	r|qS r   r   rh   r   r   r   rk     s      z3peep_hole_fuse_dict_add_updates.<locals>.<listcomp>)rl   rm   r$   r%   r(   rn   ro   r&   rp   rq   rN   r   rO   r6   rQ   rR   r5   r*   r-   rA   rB   rT   rV   rS   rv   r?   _build_new_build_mapru   r   r   )r;   rX   r<   rw   rD   Zlit_map_def_idxZlit_map_use_idxZmap_updatesrx   ry   rz   Znew_instZstmt_build_map_out	func_namerZ   rV   Zupdate_map_namerS   Zd2_map_nameZlinenumr   r   r   r   peep_hole_fuse_dict_add_updates  s    B







	




   	



r   c                 C   s~  i }t | j }t|D ]P\}\}}g }t|jD ]\}}t|tjr6|| q6|rg }	|D ]d}tj	|j
|jd}
|
j|jd|  |	|
 tj	|j
|jd}|j|j|  |	| qb|}|	D ]0}|||< |d }|jtj||jd |}q|d t|k r4|||d  d kr4tdtj	|j
|jd}|j|j|d d d  |||< q| j| | S )z
    Split blocks that contain ir.PopBlock.

    This rewrite restores the IR structure to pre 3.11 so that withlifting
    can work correctly.
    r   Nr@   r   zPOP_BLOCK peephole failedr`   )sortedrn   rA   rp   rq   rN   r   PopBlockr-   Blockr   rt   rv   JumprU   r   ZInternalErrorupdate)r;   Znew_block_mapZsorted_blocksZblk_idxlabelrw   Zpop_block_locsry   instZ
new_blocksZ
before_blkZ
popblk_blkZ
prev_labelZnewblkZ
next_labelZtail_blkr   r   r   peep_hole_split_at_pop_block  s<    


r   c                 C   s.  || }|j }|j}g }g }	|D ]z}
|
\}}tt| |}t|tjtjtjfrZ|	|j tt| |}t|tjtjtjfr|		|j q |		t
|j q i }t|t|krdd t||	D }t|D ]\}}|||< qnd}tjj|t||||jd}| j| 	| t|t|j||j|jS )zg
    Create a new build_map with a new set of key/value items
    but all the other info the same.
    c                 S   s   i | ]\}}||qS r   r   ri   rj   yr   r   r   
<dictcomp>  s      z(_build_new_build_map.<locals>.<dictcomp>NrA   sizeliteral_valuerC   rt   )r5   r6   r	   r   rN   r   rP   r   FreeVarr-   r   r*   rU   ziprp   rQ   rf   rt   r7   rO   ra   r   )r;   r*   r9   Z
old_linenoZ	new_itemsZ
old_assignZ
old_targetZold_bmliteral_keysro   pairkr   Zkey_defZ	value_defrC   r   ry   Znew_bmr   r   r   r     sB    
  r   c                   @   s  e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zd]ddZdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zed*d+ Zed,d- Zed.d/ Zed0d1 Zed2d3 Zed4d5 Zd6d7 Zd^d8d9Z d:d; Z!d_d<d=Z"d>d? Z#d@dA Z$dBdC Z%dDdE Z&dFdG Z'dHdI Z(dJdK Z)dLdM Z*dNdO Z+dPdQ Z,dRdS Z-dTdU Z.dVdW Z/dXdY Z0dZd[ Z1d\d] Z2d^d_ Z3d`da Z4dbdc Z5ddde Z6dfdg Z7dhdi Z8djdk Z9dldm Z:dndo Z;dpdq Z<drds Z=dtdu Z>e>Z?e>Z@dvdw ZAdxdy ZBdzd{ ZCd|d} ZDeEd~kr8dd ZFneEd~k rLdd ZFneGeEdd ZHeEd~krpdd ZIneEd~k rdd ZIneGeEeEd~krdd ZJdd ZKneEd~k rdd ZKneGeEdd ZLd`ddZMdd ZNdd ZOdd ZPdd ZQdd ZRdd ZSdd ZTdd ZUdd ZVdd ZWdd ZXdd ZYdd ZZdd Z[dd Z\dd Z]dd Z^dd Z_dd Z`dd Zadd Zbdd Zcdd Zddd Zedd ZfddÄ Zgddń ZhddǄ ZiddɄ Zjdd˄ Zkdd̈́ Zlddτ Zmddф Znddӄ ZoddՄ Zpddׄ Zqddل Zrddۄ Zsdd݄ Ztdd߄ Zudd Zvdd Zwdd Zxdd Zydd Zzdd Z{dd Z|dd Z}dd Z~dd Zdd Zdd Zdd Zdd Zdd Zdd Zd d Zdd Zdd Zdd Zdd	 Zd
d Zdd Zdd Zdd Zdd ZdaddZdd Zdd Zdd Zdd ZdbddZd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Zd<d= Zd>d? Zd@dA ZdBdC ZdDdE ZdFdG ZdHdI ZdJdK ZdLdM ZeEd~krdNdO Zn eEd~k rdPdO ZneGeEdQdR ZdSdT ZdUdV ZdWdX ZdYdZ Zd[d\ ZdS (c  Interpreterz2A bytecode interpreter that builds up the IR.
    Fc                 C   s`   || _ | jrt|j |j| _|j| _tj| | _	| _
|j| _i | _tt| _t | _d S r   )func_id_DEBUG_PRINTr   rT   	arg_count	arg_namesr   ZLocZfrom_function_idrt   	first_locis_generatorrn   r$   r%   r&   definitionsr(   _exception_vars)r   r   r   r   r   r   7  s    
zInterpreter.__init__c           	   	   C   sl  || _ g | _tjd| jd}| j| t|}|  t|| _	t
|| _tjr\| j  d| _d| _g | _d| _| jtj| j| jd |  D ]\}}| || qtdkr|   ntdkrtt|   t| j| j| j| j| j| j | j!}t"#|$  g }tdkr |t% tdkr4|t& |t' tdkr\|t( |t) | *||}|S )z0
        Generate IR for this bytecode.
        N)parentrt         ))r   	   r   
   r   )r   r   )+bytecodescopesr   ZScopert   r-   r   runr   dfar   cfar   ZDUMP_CFGr   current_blockcurrent_block_offsetsyntax_blocksdfainfocurrent_scope
_iter_inst	_dispatchr
   _end_try_blocksNotImplementedError_legalize_exception_varsZ
FunctionIRrn   r   r   r   r   r   r   _loggerdebugZdump_to_stringr   r   r   r}   r   post_process)	r   r   Zglobal_scopeZflowr   rF   r;   	peepholesZpost_processed_irr   r   r   	interpretH  sP    



  







zInterpreter.interpretc                 C   s   |D ]}||}q|S r   r   )r   r   r;   Zpeepr   r   r   r     s    
zInterpreter.post_processc                    s   t dkstjj}j D ]\} jj| j}|	|D ]v\}}jj| j}t
t||D ]\}\}}	||	krb qqbtt|t|} fdd}
|
t||d r> qq>qdS )a=  Closes all try blocks by inserting the required marker at the
        exception handler

        This is only needed for py3.11 because of the changes in exception
        handling. This merely maps the new py3.11 semantics back to the old way.

        What the code does:

        - For each block, compute the difference of blockstack to its incoming
          blocks' blockstack.
        - If the incoming blockstack has an extra TRY, the current block must
          be the EXCEPT block and we need to insert a marker.

        See also: _insert_try_block_end
        r   c                    sT   | rP|   }|d tdkr  _t j} j     j| dS q d S )NkindTRYT)r+   r   r   r&   rq   ru   _insert_try_block_endrv   )ZremainentZoldbodyblockr   r   r   	do_change  s    

z.Interpreter._end_try_blocks.<locals>.do_changeN)r
   rb   r   graphrn   rA   r   infosZ
blockstackZpredecessorsrp   r   minrU   r&   )r   r   r   Zcur_bsinc_Zinc_bsry   rj   r   r   r   r   r   r     s    zInterpreter._end_try_blocksc           
      C   s   | j  }| j D ]6\}}|D ](}t|tjr |j}||kr || q qt	t
dd |}|r|d }| j|j}d}	tj|	||ddS )zsSearch for unsupported use of exception variables.
        Note, they cannot be stored into user variable.
        c                 S   s   |  d S )N$)r   r   r   r   r   <lambda>      z6Interpreter._legalize_exception_vars.<locals>.<lambda>r   z5Exception object cannot be stored into variable ({}).r   N)r   rB   r   rA   rN   r   ra   r*   r.   r&   filterr   getrt   r   r   r   )
r   Zexcvarsr   Zdefnvarsr   r   Zuservarfirstrt   msgr   r   r   r     s    
z$Interpreter._legalize_exception_varsc                 C   s6   t | jD ]&\}}tj||| jd}| || q
d S )N)rH   r*   rt   )rp   r   r   ZArgrt   store)r   rH   r*   valr   r   r   init_first_block  s    zInterpreter.init_first_blockc                 c   s   t | j D ]~\}}| j|j }| j|j| _| |j |dkrN| 	  | j
jD ],\}}| j| }| j|j| _||fV  qV|   qd S )Nr   )rp   r   Ziterliveblocksr   r   rt   Zwith_linenolineno_start_new_blockr   r   Zinsts_end_current_block)r   Zblkctr   Z	firstinstr   rF   r   r   r   r   r     s    
zInterpreter._iter_instc                 C   sd  | j }| | | jr| jjnd }|d k	r|js|d k	rv|d | jj krvtj	| 
d|d || jd}|| ntj|| jd}|| | jj| j | _t | _tdkr| jr|| jd jkr| j }t|tjr| j t| j qqq| jj}|d k	r`||k	r`|   nBtdk rX| jr`|| jd jkrV| j  nq`q(nttd S )Nend$exception_checkZcondtruebrfalsebrrt   r   r   r`   )r   insert_blockr   active_try_blockis_terminatedr   r   Znodesr   Branchr   rt   r-   r   r   r   r   r"   assignerr
   r   exitr+   rN   Withr   _insert_try_block_beginr   )r   r   ZoldblocktryblkbranchjmpZsynblkZ	newtryblkr   r   r   r     sB    








zInterpreter._start_new_blockc                 C   s4   | j js | jj}|d k	r |   |   |   d S r   )r   r  r   r  _insert_exception_check_remove_unused_temporaries_insert_outgoing_phis)r   r  r   r   r   r     s    zInterpreter._end_current_blockNc                 C   s`   t j||| jd}| j||dd t jj| |dd| jd}|pJd|}| j||dd dS )a  A helper function to inject a call to *func* which is a python
        function.
        Parameters
        ----------
        func : callable
            The function object to be called.
        gv_name : str
            The variable name to be used to store the function object.
        res_name : str; optional
            The variable name to be used to store the call result.
            If ``None``, a name is created automatically.
        r   Tr6   r*   r   r   z$callres_{}N)r   r   rt   r   rQ   rL   r   r   )r   rT   gv_nameZres_namegv_fncallresr   r   r   _inject_call   s
    zInterpreter._inject_callc                 C   s   |  tjd dS )z<Insert IR-nodes to mark the start of a `try` block.
        mark_try_blockN)r  r   r  r   r   r   r   r
  3  s    z#Interpreter._insert_try_block_beginc                 C   s   |  tjd dS )z:Insert IR-nodes to mark the end of a `try` block.
        end_try_blockN)r  r   r  r   r   r   r   r   8  s    z!Interpreter._insert_try_block_endc                 C   sl   | j j}|d }| j j|}|rhtjd| jd}|D ]0}|| jkrLtd| j	||d | j
| q6dS )z?Insert IR-nodes to initialize the exception variables.
        r   Nr6   rt   z2exception variable CANNOT be defined by other coder6   r*   )r   r  Zoutgoing_edgepushedr   r   rP   rt   r   rb   r   r   r.   )r   r  ZendblkZ
edgepushed
const_noner   r   r   r   _insert_exception_variables=  s    
z'Interpreter._insert_exception_variablesc                 C   s   |    | tjdd dS )zECalled before the end of a block to inject checks if raised.
        exception_checkr   N)r  r  r   r  r   r   r   r   r  Q  s    
z#Interpreter._insert_exception_checkc                 C   s  g }i }| j jD ]}t|tjtjfrF|jj|krF||jj |_nPt|tjr|j	j
rn|j	j| jjkrnqt|jtjr|jj|kr||jj |_|| qt|jtjr|jjdkr|jjj|kr||jjj |j_|| qt|jtjr|jj
r|rt|d tjr|d }|j	j|jjkr| |j	j|js|j	||jj< |j	|_	| j|j	j |j | j|j	j | j|jj q|| q|| j _dS )zb
        Remove assignments to unused temporary variables from the
        current block.
        exhaust_iterr`   N)r   rq   rN   r   SetItemSetAttrr6   r*   rO   r5   r,   r  r)   ra   r-   rQ   rR   _var_used_in_binopr   r8   rv   r+   )r   rD   Zreplaced_varr   Zprev_assignr   r   r   r  Y  s^    




 z&Interpreter._remove_unused_temporariesc                 C   s.   t |tjo,|jdko,||jjkp,||jjkS )zhreturn True if 'expr' is a binary expression and 'varname' is used
        in it as an argument
        )r_   inplace_binop)rN   r   rQ   rR   r=   r*   r>   )r   r   r|   r   r   r   r     s
    zInterpreter._var_used_in_binopc              	   C   s   | j j D ]\}}| jj|| jd}z| |}W n6 tjk
rj   t	dksVt
dtjd| jd}Y nX tj||| jd}| j|j |j | jjs| j| q| j| qdS )zd
        Add assignments to forward requested outgoing values
        to subsequent blocks.
        r   r   zunexpected missing definitionNr  r6   r5   rt   )r   Zoutgoing_phisrA   r   get_or_definert   r   r   r   r
   rb   rP   rO   r   r*   r-   r6   r   r  Zinsert_before_terminator)r   Zphinamer   r5   r   rz   r   r   r   r    s     z!Interpreter._insert_outgoing_phisc                 C   s8   z| j jj| W S  tk
r2   tt|tj Y S X dS )z
        Get a global value from the func_global (first) or
        as a builtins (second).  If both failed, return a ir.UNDEFINED.
        N)r   rT   __globals__KeyErrorrJ   builtinsr   	UNDEFINED)r   r*   r   r   r   get_global_value  s    zInterpreter.get_global_valuec                 C   s6   | j jj| }z|jW S  tk
r0   tj Y S X dS )z|
        Get a value from the cell contained in this function's closure.
        If not set, return a ir.UNDEFINED.
        N)r   rT   __closure__cell_contents
ValueErrorr   r'  )r   rH   cellr   r   r   get_closure_value  s
    zInterpreter.get_closure_valuec                 C   s
   | j d S )Nr`   )r   r   r   r   r   r     s    zInterpreter.current_scopec                 C   s   | j jS r   )r   	co_constsr   r   r   r   code_consts  s    zInterpreter.code_constsc                 C   s   | j jS r   )r   co_varnamesr   r   r   r   code_locals  s    zInterpreter.code_localsc                 C   s   | j jS r   )r   co_namesr   r   r   r   
code_names  s    zInterpreter.code_namesc                 C   s   | j jS r   )r   co_cellvarsr   r   r   r   code_cellvars  s    zInterpreter.code_cellvarsc                 C   s   | j jS r   )r   co_freevarsr   r   r   r   code_freevars  s    zInterpreter.code_freevarsc           	   
   C   s2  | j rt| | jd k	sttdkrn| jr~| jd }t|tjr~|j	|j
kr~| jtj| jd | j  ntdkr~ttd|jdd }zt| |}W n tk
r   t|Y ntX z||f|W S  tjk
r, } z@|jd kr| j}n|j}tj|j|d}tjs|d n|W 5 d }~X Y nX d S )Nr   r`   r   zop_%s+r   )r   r   r   rb   r
   r   rN   r   r	  r   r  r-   r   rt   r+   r   opnamereplacerJ   AttributeErrorr   r   r*   r   ZFULL_TRACEBACKS)	r   r   rF   topr   rc   ert   errr   r   r   r     s6    

zInterpreter._dispatchc                 C   s   |s| j | jjkr2|| jk}| jj|| j|d}n| jj|| jd}t|t	j
r^| j||}t	j||| jd}| j| | j|j | |S )z
        Store *value* (a Expr or Var instance) into the variable named *name*
        (a str object). Returns the target variable.
        )rt   renamer   r"  )r   r   Zbackboner5  r   r   rt   r#  rN   r   ra   r  r2   rO   r   r-   r   r*   )r   r6   r*   r   r?  r5   rz   r   r   r   r     s    
zInterpreter.storec                 C   sR   |d dkr.|dd   r.d|dd }| j|}|dkrN| j|}|S )zJ
        Get the variable (a Var instance) with the given *name*.
        r   .r@   Nz
implicit{})isdigitr   r  r4   r   r   )r   r*   r   r   r   r   r     s    zInterpreter.getc                 C   s<   |p| j }|p| j}tj||d}|| j|< || _|| _|S )N)r   rt   )r   rt   r   r   rn   r   r   )r   r   r   rt   rw   r   r   r   r  )  s    


zInterpreter.insert_blockc                 C   s   d S r   r   r   r   r   r   r   op_NOP4  s    zInterpreter.op_NOPc                 C   s   d S r   r   rB  r   r   r   	op_RESUME7  s    zInterpreter.op_RESUMEc                 C   s   d S r   r   rB  r   r   r   op_CACHE:  s    zInterpreter.op_CACHEc                 C   s   d S r   r   rB  r   r   r   
op_PRECALL=  s    zInterpreter.op_PRECALLc                 C   s   d S r   r   rB  r   r   r   op_PUSH_NULL@  s    zInterpreter.op_PUSH_NULLc                 C   s   d S r   r   rB  r   r   r   op_RETURN_GENERATORC  s    zInterpreter.op_RETURN_GENERATORc                 C   sZ   |  |}tjdt| jd}| j||d tjj|  ||fd| jd}| j||d d S Nr   r   r  r   )r   r   r   r   rt   r   rQ   rL   )r   r   itemprintvarresprintgvrL   r   r   r   op_PRINT_ITEMF  s
    
zInterpreter.op_PRINT_ITEMc                 C   sN   t jdt| jd}| j||d t jj| |dd| jd}| j||d d S rI  )r   r   r   rt   r   rQ   rL   r   )r   r   rK  rL  rM  rL   r   r   r   op_PRINT_NEWLINEM  s    zInterpreter.op_PRINT_NEWLINEc           
      C   sn   t |}tjj| || j|d}| j||d t|D ]0\}}tjj| ||d | jd}	| |	| q8d S )N)r6   rt   count)r*   r6   )rH   Z	index_varrt   )	rU   r   rQ   r  r   rt   r   rp   Zstatic_getitem)
r   r   iterableZstoresZtupleobjrP  tupry   str|   r   r   r   op_UNPACK_SEQUENCES  s     zInterpreter.op_UNPACK_SEQUENCEc                 C   sZ   |  |}tjdt| jd}| j||d tjj|  ||fd| jd}| j||d dS )z
        FORMAT_VALUE(flags): flags argument specifies format spec which is not
        supported yet. Currently, str() is simply called on the value.
        https://docs.python.org/3/library/dis.html#opcode-FORMAT_VALUE
        strr   r  r   N)r   r   r   rU  rt   r   rQ   rL   )r   r   r6   rL  ZstrvarZstrgvrL   r   r   r   op_FORMAT_VALUEa  s
    
zInterpreter.op_FORMAT_VALUEc           
      C   s   |j }|dkr2tjd| jd}| ||d  dS | |d }t|dd |D ]@\}}| |}tjjt	j
||| jd}	| |	| | |}qRdS )z
        BUILD_STRING(count): Concatenates count strings.
        Required for supporting f-strings.
        https://docs.python.org/3/library/dis.html#opcode-BUILD_STRING
        r   r   r   r`   Nr@   r=   r>   rt   )r   r   rP   rt   r   r   r   rQ   r_   rd   r.   )
r   r   stringsZtmpsrP  constprevotherr   r|   r   r   r   op_BUILD_STRINGm  s     
   zInterpreter.op_BUILD_STRINGc           	      C   s   |  |}|  |}tjdt| jd}| j||d |d kr^tjj|  |||fd| jd}n,|  |}tjj|  ||||fd| jd}| j||d d S Nslicer   r  r   )r   r   r   r^  rt   r   rQ   rL   )	r   r   startstopsteprL  slicevarslicegvZ	sliceinstr   r   r   op_BUILD_SLICE  s    


 zInterpreter.op_BUILD_SLICEc                 C   s   |  |}tjdt| jd}| j||d tjd | jd}| j||d |  |}	tjj|  ||	|	fd| jd}
| j|
|d tjj	||  || jd}| j||d d S r]  )
r   r   r   r^  rt   r   rP   rQ   rL   getitem)r   r   baserL  rb  indexvarnonevarrc  nonegvnonerH   r|   r   r   r   
op_SLICE_0  s    

 zInterpreter.op_SLICE_0c                 C   s   |  |}|  |}tjd | jd}| j||d |  |}	tjdt| jd}
| j|
|d tjj|  |||	fd| jd}| j||d tjj	||  || jd}| j||d d S Nr   r  r^  r   
r   r   rP   rt   r   r   r^  rQ   rL   re  )r   r   rf  r_  rh  rL  rb  rg  ri  rj  rc  rH   r|   r   r   r   
op_SLICE_1  s    


zInterpreter.op_SLICE_1c                 C   s   |  |}|  |}tjd | jd}| j||d |  |}	tjdt| jd}
| j|
|d tjj|  ||	|fd| jd}| j||d tjj	||  || jd}| j||d d S rl  rm  )r   r   rf  rh  r`  rL  rb  rg  ri  rj  rc  rH   r|   r   r   r   
op_SLICE_2  s    


zInterpreter.op_SLICE_2c                 C   s   |  |}|  |}|  |}tjdt| jd}| j||d tjj|  |||fd| jd}	| j|	|d tjj||  || jd}
| j|
|d d S r]  )	r   r   r   r^  rt   r   rQ   rL   re  )r   r   rf  r_  r`  rL  rb  rg  rc  rH   r|   r   r   r   
op_SLICE_3  s    


zInterpreter.op_SLICE_3c                 C   s   |  |}tjdt| jd}| j||d tjd | jd}| j||d |  |}	tjj|  ||	|	fd| jd}
| j|
|d tj	||  ||  || jd}| j
| d S r]  )r   r   r   r^  rt   r   rP   rQ   rL   r  r   r-   )r   r   rf  r6   rb  rg  rh  rc  ri  rj  rH   rz   r   r   r   op_STORE_SLICE_0  s    

 zInterpreter.op_STORE_SLICE_0c                 C   s   |  |}|  |}tjd | jd}| j||d |  |}	tjdt| jd}
| j|
|d tjj|  |||	fd| jd}| j||d tj	||  ||  || jd}| j
| d S rl  r   r   rP   rt   r   r   r^  rQ   rL   r  r   r-   )r   r   rf  r_  rh  r6   rb  rg  ri  rj  rc  rH   rz   r   r   r   op_STORE_SLICE_1  s    


zInterpreter.op_STORE_SLICE_1c                 C   s   |  |}|  |}tjd | jd}| j||d |  |}	tjdt| jd}
| j|
|d tjj|  ||	|fd| jd}| j||d tj	||  ||  || jd}| j
| d S rl  rr  )r   r   rf  rh  r`  r6   rb  rg  ri  rj  rc  rH   rz   r   r   r   op_STORE_SLICE_2  s    


zInterpreter.op_STORE_SLICE_2c                 C   s   |  |}|  |}|  |}tjdt| jd}| j||d tjj|  |||fd| jd}	| j|	|d tj||  ||  || jd}
| j	
|
 d S r]  )r   r   r   r^  rt   r   rQ   rL   r  r   r-   )r   r   rf  r_  r`  r6   rb  rg  rc  rH   rz   r   r   r   op_STORE_SLICE_3  s    


zInterpreter.op_STORE_SLICE_3c                 C   s   |  |}tjdt| jd}| j||d tjd | jd}| j||d |  |}tjj|  |||fd| jd}	| j|	|d tj	||  || jd}
| j
|
 d S r]  )r   r   r   r^  rt   r   rP   rQ   rL   DelItemr   r-   )r   r   rf  rb  rg  rh  rc  ri  rj  rH   rz   r   r   r   op_DELETE_SLICE_0  s    

 zInterpreter.op_DELETE_SLICE_0c                 C   s   |  |}|  |}tjd | jd}| j||d |  |}tjdt| jd}	| j|	|d tjj|  |||fd| jd}
| j|
|d tj	||  || jd}| j
| d S rl  r   r   rP   rt   r   r   r^  rQ   rL   rv  r   r-   )r   r   rf  r_  rh  rb  rg  ri  rj  rc  rH   rz   r   r   r   op_DELETE_SLICE_1/  s    


zInterpreter.op_DELETE_SLICE_1c                 C   s   |  |}|  |}tjd | jd}| j||d |  |}tjdt| jd}	| j|	|d tjj|  |||fd| jd}
| j|
|d tj	||  || jd}| j
| d S rl  rx  )r   r   rf  rh  r`  rb  rg  ri  rj  rc  rH   rz   r   r   r   op_DELETE_SLICE_2A  s    


zInterpreter.op_DELETE_SLICE_2c           
      C   s   |  |}|  |}|  |}tjdt| jd}| j||d tjj|  |||fd| jd}| j||d tj||  || jd}	| j	
|	 d S r]  )r   r   r   r^  rt   r   rQ   rL   rv  r   r-   )
r   r   rf  r_  r`  rb  rg  rc  rH   rz   r   r   r   op_DELETE_SLICE_3S  s    


zInterpreter.op_DELETE_SLICE_3c                 C   s$   | j |j }| j| ||d d S Nr  )r1  r   r   r   )r   r   rL  r/   r   r   r   op_LOAD_FASTa  s    zInterpreter.op_LOAD_FASTc                 C   s(   | j |j }| |}| j||d d S r|  )r1  r   r   r   )r   r   r6   dstnamer   r   r   op_STORE_FASTe  s    
zInterpreter.op_STORE_FASTc                 C   s(   | j |j }| jtj|| jd d S Nr   )r1  r   r   r-   r   Delrt   )r   r   r~  r   r   r   op_DELETE_FASTj  s    zInterpreter.op_DELETE_FASTc                 C   s,   t ||D ]\}}| j| ||d q
d S r|  )r   r   r   )r   r   origZdupedsrcdstr   r   r   op_DUP_TOPXn  s    zInterpreter.op_DUP_TOPXc                 C   s<   | j |j }tj| || ||| jd}| j| d S )N)r5   r6   rS   rt   )r3  r   r   r  r   rt   r   r-   )r   r   r5   r6   rS   sar   r   r   op_STORE_ATTRu  s     zInterpreter.op_STORE_ATTRc                 C   s4   | j |j }tj| ||| jd}| j| d S )N)r5   rS   rt   )r3  r   r   ZDelAttrr   rt   r   r-   )r   r   r5   rS   r  r   r   r   op_DELETE_ATTR{  s    zInterpreter.op_DELETE_ATTRc                 C   s:   |  |}| j|j }tjj||| jd}| || d S r  )r   r3  r   r   rQ   rJ   rt   r   )r   r   rJ  rL  rS   rJ   r   r   r   op_LOAD_ATTR  s    
zInterpreter.op_LOAD_ATTRc           
      C   s   | j |j }t|trng }|D ]:}dt| }tj|| jd}| j||dd}|	| qtj
j|| jd}	nrt|trg }|D ]:}dt| }tj|| jd}| j||dd}|	| qtj
j|| jd}	ntj|| jd}	| |	| d S )Nz	$const_%sr   T)r*   r   )r/  r   rN   r   rU  r   rP   rt   r   r-   rQ   r^   	frozenset	build_set)
r   r   rL  r6   rS  rj   nmZ	val_constr5   rY  r   r   r   op_LOAD_CONST  s&    

zInterpreter.op_LOAD_CONSTr   c                 C   s6   | j | }| |}tj||| jd}| || d S r  )r3  r(  r   r   rt   r   )r   r   r:   rL  r*   r6   glr   r   r   op_LOAD_GLOBAL  s    

zInterpreter.op_LOAD_GLOBALc                 C   s8   | j |j }| |}tj||| jd}| || d S r  )r3  r   r(  r   r   rt   r   )r   r   rL  r*   r6   r  r   r   r   r    s    
c                 C   s   d S r   r   rB  r   r   r   op_COPY_FREE_VARS  s    zInterpreter.op_COPY_FREE_VARSc                 C   sl   | j jj|j}|| jkr(| |}n4|| jkr\| j|}| 	|}t
j|||| jd}| || d S r  )r   rT   __code___varname_from_opargr   r5  r   r7  rH   r-  r   r   rt   r   )r   r   rL  r*   r  r:   r6   r   r   r   op_LOAD_DEREF  s    


zInterpreter.op_LOAD_DEREFc                 C   sn   t | j}|j|k r,| j|j }| |}n2|j| }| j| }| |}tj|||| jd}| 	|| d S r  )
rU   r5  r   r   r7  r-  r   r   rt   r   )r   r   rL  
n_cellvarsr*   r  r:   r6   r   r   r   r    s    




c                 C   s   d S r   r   rB  r   r   r   op_MAKE_CELL  s    zInterpreter.op_MAKE_CELLc                 C   s.   | j jj|j}| |}| j||d d S r|  )r   rT   r  r  r   r   r   )r   r   r6   r*   r   r   r   op_STORE_DEREF  s    
zInterpreter.op_STORE_DEREFc                 C   sN   t | j}|j|k r"| j|j }n| j|j|  }| |}| j||d d S r|  )rU   r5  r   r7  r   r   )r   r   r6   r  r~  r   r   r   r    s    


c                 C   s>   | j |j | jksttj|j|j|j d}| j	| d S )Nr  )
rn   r   r   rb   r   Loopnextr   r   r-   )r   r   loopr   r   r   op_SETUP_LOOP  s    zInterpreter.op_SETUP_LOOPc                 C   s   | j |j | jkst|j|j }tj|j|d}| j	| | 
|}| j	tj||j|| jd tjd | jd}| j||d d S Nr  )contextmanagerbeginr   rt   r   r  )rn   r   r   rb   r  r   r   r	  r   r-   r   	EnterWithrt   rP   r   )r   r   r  exitfnZexitptwthctxmgrexit_fn_objr   r   r   op_SETUP_WITH  s    
 zInterpreter.op_SETUP_WITHc                 C   s|   | j |j | jksttj|j|d}| j| | |}| jtj	||j|| j
d tjd | j
d}| j||d d S r  )rn   r   r   rb   r   r	  r   r-   r   r  rt   rP   r   )r   r   r  r  r   r  r  r  r   r   r   op_BEFORE_WITH  s    
 zInterpreter.op_BEFORE_WITHc                 C   s   |    d S r   )r
  rB  r   r   r   op_SETUP_FINALLY  s    zInterpreter.op_SETUP_FINALLYc                 C   s   dS zno-opNr   rB  r   r   r   op_WITH_CLEANUP	  s    zInterpreter.op_WITH_CLEANUPc                 C   s   dS r  r   rB  r   r   r   op_WITH_CLEANUP_START	  s    z!Interpreter.op_WITH_CLEANUP_STARTc                 C   s   dS r  r   rB  r   r   r   op_WITH_CLEANUP_FINISH	  s    z"Interpreter.op_WITH_CLEANUP_FINISHc                 C   s   dS r  r   rB  r   r   r   op_END_FINALLY
	  s    zInterpreter.op_END_FINALLYc                 C   s8   t jd | jd}|D ]}| j||d | j| qd S )Nr   )r*   )r   rP   rt   r   r   r.   )r   r   tempsr  r   r   r   r   op_BEGIN_FINALLY	  s    zInterpreter.op_BEGIN_FINALLYc           	         s     |} fdd|D }|d k	r^ j| }tt||t| d  }|d t|  }nd}tjj||| jd} 	|| d S )Nc                    s   g | ]}  |qS r   r   rh   r   r   r   rk   	  s     z'Interpreter.op_CALL.<locals>.<listcomp>r   r   )
r   r/  r&   r   rU   r   rQ   rL   rt   r   )	r   r   rT   rV   Zkw_namesrL  nameskwargsr|   r   r   r   op_CALL	  s    

zInterpreter.op_CALLc                    sB     |} fdd|D }tjj||d jd} || d S )Nc                    s   g | ]}  |qS r   r  rh   r   r   r   rk   #	  s     z0Interpreter.op_CALL_FUNCTION.<locals>.<listcomp>r   r   r   r   rQ   rL   rt   r   )r   r   rT   rV   rL  r|   r   r   r   op_CALL_FUNCTION!	  s    
zInterpreter.op_CALL_FUNCTIONc                    s    |} fdd|D }  |} jjD ]}t|tjr.|j|kr. j| g }|jj	D ]D} jjd d d D ],}||jkrv j| |
|jj  q`qvq`|}	 qq.t|	}
|d |
  }||
 d  }tt|	|}tjj||| jd} || d S )Nc                    s   g | ]}  |qS r   r  rh   r   r   r   rk   )	  s     z3Interpreter.op_CALL_FUNCTION_KW.<locals>.<listcomp>r`   r   )r   r   rq   rN   r   rO   r5   r8   r6   rA   r-   rU   r&   r   rQ   rL   rt   r   )r   r   rT   rV   r  rL  named_itemsrj   r   keysZnkeysZposvalsZkwvalsZ	keyvaluesr|   r   r   r   op_CALL_FUNCTION_KW'	  s*    


zInterpreter.op_CALL_FUNCTION_KWc                 C   sP   |  |}|  |}|d k	r&|  |}tjj|g g | j||d}| || d S )N)rt   rs   rr   r  )r   r   rT   rs   rr   rL  r|   r   r   r   op_CALL_FUNCTION_EXC	  s    


     zInterpreter.op_CALL_FUNCTION_EXc                 C   s  |  |d }|rhd}tj|t| jd}| j||dd tjj|  ||fd| jd}| ||d  n| j}	tt	| j |dd  |D ]\}
}tjd	t
|	d
}| j|ddd}tjj||
fd|	d}| j|ddd}tjjtj||  |j| jd}| || |  |}qd S )Nr   r   r   Tr  r   rV   rF   rt   r@   r   r   r   )r   z	$_tuplifyr   )r   r   r   r   rt   r   rQ   rL   r   mapr   r_   rd   r.   r*   )r   r   tuplesr  	is_assignr   r  r  excrt   r[  r   r   r   Ztuplify_valZtuplify_varoutr   r   r   _build_tuple_unpackM	  sL    "       
zInterpreter._build_tuple_unpackc                 C   s   |  |||| d S r   r  r   r   r  r  r  r   r   r   op_BUILD_TUPLE_UNPACK_WITH_CALLq	  s    z+Interpreter.op_BUILD_TUPLE_UNPACK_WITH_CALLc                 C   s   |  |||| d S r   r  r  r   r   r   op_BUILD_TUPLE_UNPACKu	  s    z!Interpreter.op_BUILD_TUPLE_UNPACKc                 C   s&   t jjd|f| jd}| || d S )Nrg   r   )r   rQ   dummyrt   r   )r   r   r   rL  r|   r   r   r   op_LIST_TO_TUPLEx	  s    zInterpreter.op_LIST_TO_TUPLEc                    s   |}jjD ]}t|tjr|j|krj| g }|jj	D ]D}jjd d d D ],}	||	jkrZj|	 |
|	jj  qDqZqD|}
 qqt|
t|kstfdd|
D }t||D ]\}}|| qtttj |tj |}g }|D ]L}j| }t|dkr" qN|d }t|tjs> qN|
|j q fdd t|t|kr fdd	t|
|D }nd
d	 t|
|D }i }t|
D ]\}}|||< qtjj|d||jd}|| d S )Nr`   c                    s   g | ]}t j| jd qS )r  )r   rP   rt   rh   r   r   r   rk   	  s     z6Interpreter.op_BUILD_CONST_KEY_MAP.<locals>.<listcomp>r@   r   c                    sP    j |  }t|dkr&t | jS |d }t|tjsJt | jS |jS )Nr@   r   )	r   rU   r   r   r*   rN   r   rP   r6   )r   defnsdefnr   r   r   resolve_const	  s    
z9Interpreter.op_BUILD_CONST_KEY_MAP.<locals>.resolve_constc                    s   i | ]\}}| |qS r   r   r   )r  r   r   r   	  s      z6Interpreter.op_BUILD_CONST_KEY_MAP.<locals>.<dictcomp>c                 S   s   i | ]\}}||qS r   r   r   r   r   r   r   	  s      rM   r   )r   r   rq   rN   r   rO   r5   r8   r6   rA   r-   rU   rb   r   r   r&   r  r   rP   rp   rQ   rf   rt   )r   r   r  Zkeytmpsro   rL  Zkeyvarr  rj   r   ZkeytupZ	keyconstsZkvalr   rA   literal_itemsr   r  r  literal_dictrC   ry   r   r|   r   )r  r   r   op_BUILD_CONST_KEY_MAP|	  sV    


	
z"Interpreter.op_BUILD_CONST_KEY_MAPc                 C   s(   t jj| || jd}| || d S )Nr  )r   rQ   Zgetiterr   rt   r   r   r   r6   rL  r|   r   r   r   op_GET_ITER	  s    zInterpreter.op_GET_ITERc                 C   s   |j | jkstd| |}tjj|| jd}| || tjj	| || jd}| || tjj
| || jd}	| |	| tj| ||j| | jd}
| j|
 dS )z:
        Assign new block other this instruction.
        zFOR_ITER must be block headr  r   N)r   rn   rb   r   r   rQ   iternextrt   r   Z
pair_firstZpair_secondr  r  get_jump_targetr   r-   )r   r   iteratorr   Zindvalpredr   Zpairvalr  Zisvalidbrr   r   r   op_FOR_ITER	  s    
zInterpreter.op_FOR_ITERc                 C   s8   |  |}|  |}tjj||| jd}| || d S )N)rH   rt   )r   r   rQ   re  rt   r   )r   r   r5   rH   rL  r|   r   r   r   op_BINARY_SUBSCR	  s    

zInterpreter.op_BINARY_SUBSCRc                 C   sB   |  |}|  |}|  |}tj|||| jd}| j| d S )N)r5   rH   r6   rt   )r   r   r  rt   r   r-   )r   r   r5   rH   r6   rz   r   r   r   op_STORE_SUBSCR	  s    



zInterpreter.op_STORE_SUBSCRc                 C   s6   |  |}|  |}tj||| jd}| j| d S )N)r5   rH   rt   )r   r   rv  rt   r   r-   )r   r   r5   rH   rz   r   r   r   op_DELETE_SUBSCR	  s    

zInterpreter.op_DELETE_SUBSCRc                    s0   t jj fdd|D  jd} || d S )Nc                    s   g | ]}  |qS r   r  rh   r   r   r   rk   	  s     z.Interpreter.op_BUILD_TUPLE.<locals>.<listcomp>rA   rt   )r   rQ   r^   rt   r   r   r   rA   rL  r|   r   r   r   op_BUILD_TUPLE	  s    zInterpreter.op_BUILD_TUPLEc                    s0   t jj fdd|D  jd} || d S )Nc                    s   g | ]}  |qS r   r  rh   r   r   r   rk   	  s     z-Interpreter.op_BUILD_LIST.<locals>.<listcomp>r  )r   rQ   r   rt   r   r  r   r   r   op_BUILD_LIST	  s    zInterpreter.op_BUILD_LISTc                    s0   t jj fdd|D  jd} || d S )Nc                    s   g | ]}  |qS r   r  rh   r   r   r   rk   	  s     z,Interpreter.op_BUILD_SET.<locals>.<listcomp>r  )r   rQ   r  rt   r   r  r   r   r   op_BUILD_SET	  s    zInterpreter.op_BUILD_SETc                 C   sf   |  |}|  |}tjj|d| jd}| j||d tjj|  ||fd| jd}| j||d d S )Nr   r   r  r   r   r   rQ   rJ   rt   r   rL   r   r   r5   r6   Z	updatevarrL  Z
updateattrZ
updateinstr   r   r   op_SET_UPDATE	  s    

zInterpreter.op_SET_UPDATEc                 C   sf   |  |}|  |}tjj|d| jd}| j||d tjj|  ||fd| jd}| j||d d S )Nr   r   r  r   r  r  r   r   r   op_DICT_UPDATE
  s    

  zInterpreter.op_DICT_UPDATEc                    s   fdd|D } fdd}|dd |D }|dd |D }t |t |k}	t |t |k}
i }|	st|
std }nf|	r|
sdd	 t||D }t|D ]\}}|||< qn.d
d	 t||D }t|D ]\}}|||< qtjj|||| jd} || d S )Nc                    s$   g | ]\}}  |  |fqS r   r  )ri   r   r   r   r   r   rk   
  s     z,Interpreter.op_BUILD_MAP.<locals>.<listcomp>c                    sd   g } fdd| D }|D ]D} j |j }t|dkr: q`|d }t|tjsR q`||j q|S )Nc                    s   g | ]}  |jqS r   )r   r*   )ri   r   r   r   r   rk   
  s     zBInterpreter.op_BUILD_MAP.<locals>.get_literals.<locals>.<listcomp>r@   r   )r   r*   rU   rN   r   rP   r-   r6   )r5   r  ro   r   r  r  r   r   r   get_literals
  s    z.Interpreter.op_BUILD_MAP.<locals>.get_literalsc                 s   s   | ]}|d  V  qdS )r   Nr   rh   r   r   r   	<genexpr>*
  s     z+Interpreter.op_BUILD_MAP.<locals>.<genexpr>c                 s   s   | ]}|d  V  qdS )r@   Nr   rh   r   r   r   r  +
  s     c                 S   s   i | ]\}}|t |d  qS )r@   )r   r   r   r   r   r   4
  s      z,Interpreter.op_BUILD_MAP.<locals>.<dictcomp>c                 S   s   i | ]\}}||qS r   r   r   r   r   r   r   9
  s      r   )rU   r   rp   r   rQ   rf   rt   r   )r   r   rA   r   rL  Z	got_itemsr  r   Zliteral_valuesZhas_literal_keysZhas_literal_valuesrC   r  ry   r   r|   r   r   r   op_BUILD_MAP
  s0    

zInterpreter.op_BUILD_MAPc                 C   s6   t j| || || || jd}| j| d S )N)dctrG   r6   rt   )r   ZStoreMapr   rt   r   r-   )r   r   r  rG   r6   rz   r   r   r   op_STORE_MAPC
  s
     zInterpreter.op_STORE_MAPc                 C   s*   |  |}tjjd|| jd}| ||S )N-r  r   r   rQ   unaryrt   r   r  r   r   r   op_UNARY_NEGATIVEH
  s    
zInterpreter.op_UNARY_NEGATIVEc                 C   s*   |  |}tjjd|| jd}| ||S )Nr8  r  r  r  r   r   r   op_UNARY_POSITIVEM
  s    
zInterpreter.op_UNARY_POSITIVEc                 C   s*   |  |}tjjd|| jd}| ||S )N~r  r  r  r   r   r   op_UNARY_INVERTR
  s    
zInterpreter.op_UNARY_INVERTc                 C   s*   |  |}tjjd|| jd}| ||S )Nnotr  r  r  r   r   r   op_UNARY_NOTW
  s    
zInterpreter.op_UNARY_NOTc                 C   sB   t | }| |}| |}tjj|||| jd}| || d S )NrW  )r   r   r   rQ   r_   rt   r   )r   rR   r=   r>   rL  r|   r   r   r   _binop\
  s
    

zInterpreter._binopc                 C   sP   t | }t|d  }| |}| |}tjj||||| jd}| || d S )N=rW  )r   r   r   r   rQ   r!  rt   r   )r   rR   r=   r>   rL  Zimmuopr|   r   r   r   _inplace_binopc
  s    

zInterpreter._inplace_binopc                 C   s6   d|kr"|  |d d ||| n| |||| d S )Nr  r`   )r  r  )r   r   rR   r=   r>   rL  r   r   r   op_BINARY_OPl
  s    zInterpreter.op_BINARY_OPc                 C   s   |  d||| d S Nr8  r  r   r   r=   r>   rL  r   r   r   op_BINARY_ADDr
  s    zInterpreter.op_BINARY_ADDc                 C   s   |  d||| d S Nr  r  r  r   r   r   op_BINARY_SUBTRACTu
  s    zInterpreter.op_BINARY_SUBTRACTc                 C   s   |  d||| d S N*r  r  r   r   r   op_BINARY_MULTIPLYx
  s    zInterpreter.op_BINARY_MULTIPLYc                 C   s   |  d||| d S Nz/?r  r  r   r   r   op_BINARY_DIVIDE{
  s    zInterpreter.op_BINARY_DIVIDEc                 C   s   |  d||| d S N/r  r  r   r   r   op_BINARY_TRUE_DIVIDE~
  s    z!Interpreter.op_BINARY_TRUE_DIVIDEc                 C   s   |  d||| d S Nz//r  r  r   r   r   op_BINARY_FLOOR_DIVIDE
  s    z"Interpreter.op_BINARY_FLOOR_DIVIDEc                 C   s   |  d||| d S N%r  r  r   r   r   op_BINARY_MODULO
  s    zInterpreter.op_BINARY_MODULOc                 C   s   |  d||| d S Nz**r  r  r   r   r   op_BINARY_POWER
  s    zInterpreter.op_BINARY_POWERc                 C   s   |  d||| d S N@r  r  r   r   r   op_BINARY_MATRIX_MULTIPLY
  s    z%Interpreter.op_BINARY_MATRIX_MULTIPLYc                 C   s   |  d||| d S Nz<<r  r  r   r   r   op_BINARY_LSHIFT
  s    zInterpreter.op_BINARY_LSHIFTc                 C   s   |  d||| d S Nz>>r  r  r   r   r   op_BINARY_RSHIFT
  s    zInterpreter.op_BINARY_RSHIFTc                 C   s   |  d||| d S N&r  r  r   r   r   op_BINARY_AND
  s    zInterpreter.op_BINARY_ANDc                 C   s   |  d||| d S N|r  r  r   r   r   op_BINARY_OR
  s    zInterpreter.op_BINARY_ORc                 C   s   |  d||| d S N^r  r  r   r   r   op_BINARY_XOR
  s    zInterpreter.op_BINARY_XORc                 C   s   |  d||| d S r  r  r  r   r   r   op_INPLACE_ADD
  s    zInterpreter.op_INPLACE_ADDc                 C   s   |  d||| d S r  r  r  r   r   r   op_INPLACE_SUBTRACT
  s    zInterpreter.op_INPLACE_SUBTRACTc                 C   s   |  d||| d S r  r  r  r   r   r   op_INPLACE_MULTIPLY
  s    zInterpreter.op_INPLACE_MULTIPLYc                 C   s   |  d||| d S r  r  r  r   r   r   op_INPLACE_DIVIDE
  s    zInterpreter.op_INPLACE_DIVIDEc                 C   s   |  d||| d S r  r  r  r   r   r   op_INPLACE_TRUE_DIVIDE
  s    z"Interpreter.op_INPLACE_TRUE_DIVIDEc                 C   s   |  d||| d S r   r  r  r   r   r   op_INPLACE_FLOOR_DIVIDE
  s    z#Interpreter.op_INPLACE_FLOOR_DIVIDEc                 C   s   |  d||| d S r  r  r  r   r   r   op_INPLACE_MODULO
  s    zInterpreter.op_INPLACE_MODULOc                 C   s   |  d||| d S r  r  r  r   r   r   op_INPLACE_POWER
  s    zInterpreter.op_INPLACE_POWERc                 C   s   |  d||| d S r  r  r  r   r   r   op_INPLACE_MATRIX_MULTIPLY
  s    z&Interpreter.op_INPLACE_MATRIX_MULTIPLYc                 C   s   |  d||| d S r
  r  r  r   r   r   op_INPLACE_LSHIFT
  s    zInterpreter.op_INPLACE_LSHIFTc                 C   s   |  d||| d S r  r  r  r   r   r   op_INPLACE_RSHIFT
  s    zInterpreter.op_INPLACE_RSHIFTc                 C   s   |  d||| d S r  r  r  r   r   r   op_INPLACE_AND
  s    zInterpreter.op_INPLACE_ANDc                 C   s   |  d||| d S r  r  r  r   r   r   op_INPLACE_OR
  s    zInterpreter.op_INPLACE_ORc                 C   s   |  d||| d S r  r  r  r   r   r   op_INPLACE_XOR
  s    zInterpreter.op_INPLACE_XORc                 C   s$   t j| | jd}| j| d S r  r   r   r  rt   r   r-   r   r   r  r   r   r   op_JUMP_ABSOLUTE
  s    zInterpreter.op_JUMP_ABSOLUTEc                 C   s$   t j| | jd}| j| d S r  r&  r'  r   r   r   op_JUMP_FORWARD
  s    zInterpreter.op_JUMP_FORWARDc                 C   s$   t j| | jd}| j| d S r  r&  r'  r   r   r   op_JUMP_BACKWARD
  s    zInterpreter.op_JUMP_BACKWARDc                 C   sL   |d kr| j   n4|dkr8tj| jd}| j| n|dkrH|   d S )Nwithr   try)r   r+   r   r   rt   r   r-   r   )r   r   r   r1   r   r   r   op_POP_BLOCK
  s    zInterpreter.op_POP_BLOCKc                 C   sF   |  tjj| || jd| tj| || jd}| j| d S r  )	r   r   rQ   castr   rt   Returnr   r-   )r   r   retvalZcastvalretr   r   r   op_RETURN_VALUE
  s     zInterpreter.op_RETURN_VALUEc                 C   s   t j|j }|dks|dkr&|| }}|dkrj| d||| | |}tjjd|| jd}| 	|| n|dkrtj
dtj| jd}d}	| j	||	d	d
 | |}| |}tjj| |	||fd| jd}
| 	|
| n| |||| d S )Ninznot inr  r  zexception matchexception_matchr   
$exc_matchTr  r   r  )discmp_opr   r  r   r   rQ   r  rt   r   r   r   r4  rL   )r   r   r=   r>   rL  rR   r   r  r  exc_match_namer  r   r   r   op_COMPARE_OP
  s4    

  

   zInterpreter.op_COMPARE_OPc                 C   s&   |j dkrdnd}| |||| d S )Nr@   is notis)r   r  )r   r   r=   r>   rL  rR   r   r   r   op_IS_OP
  s    zInterpreter.op_IS_OPc                 C   sR   || }}|  d||| |jdkrN| |}tjjd|| jd}| || d S )Nr3  r@   r  r  )r  r   r   r   rQ   r  rt   r   )r   r   r=   r>   rL  r   r  r   r   r   op_CONTAINS_OP
  s    


zInterpreter.op_CONTAINS_OPc                 C   sH   |d kr(| j d }t|tjs"t|j}tj|| jd}| j	| d S )Nr`   )r5   rt   )
r   rN   r   r  rb   r  r   rt   r   r-   )r   r   r   r  r  r   r   r   op_BREAK_LOOP  s    
zInterpreter.op_BREAK_LOOPc                 C   s   |  |jd}|| }||  }d|j }tjdt| jd}| j||d tjj	| 
|| 
|fd| jd}	d|j }
| j|	|
d}tj|||| jd}| j| d S )	N)TFzbool%sboolr   r  r   z$%spredr   )r  r  r   r   r   r?  rt   r   rQ   rL   r   r  r   r-   )r   r   r  iftrueZbrsr  r  r*   r  r  pname	predicatebrar   r   r   _op_JUMP_IF  s"    



zInterpreter._op_JUMP_IFc                 C   s   | j ||dd d S NFr  r@  rD  r   r   r  r   r   r   op_JUMP_IF_FALSE$  s    zInterpreter.op_JUMP_IF_FALSEc                 C   s   | j ||dd d S NTrF  rG  rH  r   r   r   op_JUMP_IF_TRUE'  s    zInterpreter.op_JUMP_IF_TRUEc                 C   s  |j }| }|std }ntd }| jtjd | jdd|j d}| |}tj	j
|||| jd}	d|j }
| j|	|
d d|j }tjd	t| jd}| j||d tj	j| || |
fd
| jd}d|j }| j||d}tj|||| jd}| j| d S )Nr;  r:  r   z
$constNoner  rW  z
$maybeNonez$boolr?  r   z$predr   )r  r  r   r   r   rP   rt   r   r   rQ   r_   r   r?  rL   r  r   r-   )r   r   r  r@  r  r  rR   r>   r=   ZisnoneZ	maybeNoner*   r  r  rA  rB  r  r   r   r   _jump_if_none*  s4    


zInterpreter._jump_if_nonec                 C   s   |  ||d d S NTrL  rH  r   r   r   op_POP_JUMP_FORWARD_IF_NONEL  s    z'Interpreter.op_POP_JUMP_FORWARD_IF_NONEc                 C   s   |  ||d d S NFrN  rH  r   r   r   op_POP_JUMP_FORWARD_IF_NOT_NONEO  s    z+Interpreter.op_POP_JUMP_FORWARD_IF_NOT_NONEc                 C   s   |  ||d d S rM  rN  rH  r   r   r   op_POP_JUMP_BACKWARD_IF_NONER  s    z(Interpreter.op_POP_JUMP_BACKWARD_IF_NONEc                 C   s   |  ||d d S rP  rN  rH  r   r   r    op_POP_JUMP_BACKWARD_IF_NOT_NONEU  s    z,Interpreter.op_POP_JUMP_BACKWARD_IF_NOT_NONEc                 C   s   | j ||dd d S rE  rG  rH  r   r   r   op_POP_JUMP_FORWARD_IF_FALSEX  s    z(Interpreter.op_POP_JUMP_FORWARD_IF_FALSEc                 C   s   | j ||dd d S rJ  rG  rH  r   r   r   op_POP_JUMP_FORWARD_IF_TRUE[  s    z'Interpreter.op_POP_JUMP_FORWARD_IF_TRUEc                 C   s   | j ||dd d S rE  rG  rH  r   r   r   op_POP_JUMP_BACKWARD_IF_FALSE^  s    z)Interpreter.op_POP_JUMP_BACKWARD_IF_FALSEc                 C   s   | j ||dd d S rJ  rG  rH  r   r   r   op_POP_JUMP_BACKWARD_IF_TRUEa  s    z(Interpreter.op_POP_JUMP_BACKWARD_IF_TRUEc                 C   s   | j ||dd d S rE  rG  rH  r   r   r   op_POP_JUMP_IF_FALSEd  s    z Interpreter.op_POP_JUMP_IF_FALSEc                 C   s   | j ||dd d S rJ  rG  rH  r   r   r   op_POP_JUMP_IF_TRUEg  s    zInterpreter.op_POP_JUMP_IF_TRUEc                 C   s   | j ||dd d S rE  rG  rH  r   r   r   op_JUMP_IF_FALSE_OR_POPj  s    z#Interpreter.op_JUMP_IF_FALSE_OR_POPc                 C   s   | j ||dd d S rJ  rG  rH  r   r   r   op_JUMP_IF_TRUE_OR_POPm  s    z"Interpreter.op_JUMP_IF_TRUE_OR_POPc           
      C   sl   t jdtj| jd}d}| j||dd | |}| |}t jj| |||fd| jd}	| |	| d S )Nr4  r   r5  Tr  r   r  )	r   r   r   r4  rt   r   r   rQ   rL   )
r   r   r  tostos1r  r8  r=   r>   r  r   r   r   op_CHECK_EXC_MATCHp  s       

   zInterpreter.op_CHECK_EXC_MATCHc                 C   s   |j }| }tjdtj| jd}d}| j||dd | |}	| |}
tj	j
| ||	|
fd| jd}| ||}tj|||| jd}| j| d S )	Nr4  r   r5  Tr  r   r  r   )r  r  r   r   r   r4  rt   r   r   rQ   rL   r  r   r-   )r   r   r  r\  r]  r  r  r  r8  r=   r>   r  rB  rC  r   r   r   op_JUMP_IF_NOT_EXC_MATCH}  s,      

   
z$Interpreter.op_JUMP_IF_NOT_EXC_MATCHc                 C   s   | j j}|d k	rRtjd | jd}| j| |   | jtj|d | jd n,dt	d  }t
t|f| j}| j| d S )N	exceptionrt   r   r   z8Unreachable condition reached (op code RERAISE executed)Z
reportable)r   r  r   TryRaisert   r   r-   r   r   r   ZStaticRaiserb   )r   r   r  r  rz   r   r   r   r   
op_RERAISE  s    zInterpreter.op_RERAISEc                 C   s   |d k	r|  |}| jj}|d k	rdtj|| jd}| j| |   | jtj	|d | jd ntj
|| jd}| j| d S )Nr`  r   r   )r   r   r  r   rb  rt   r   r-   r   r   Raise)r   r   r  r  rz   r   r   r   op_RAISE_VARARGS  s    
zInterpreter.op_RAISE_VARARGSc                 C   s(   d }t j| ||| jd}| ||S )N)r6   rH   rt   )r   Yieldr   rt   r   )r   r   r6   rL  rH   r   r   r   op_YIELD_VALUE  s    zInterpreter.op_YIELD_VALUEc	                    s   |d k	rd}	t |	|rDt|tr:t fdd|D }n
 |} j| d }
t|
tjsrd}	tj|	 j	d|
j
}|r |}|r |}tj|||| j	} || d S )Nz3op_MAKE_FUNCTION with kwdefaults is not implementedc                    s   g | ]}  |qS r   r  )ri   r*   r   r   r   rk     s     z0Interpreter.op_MAKE_FUNCTION.<locals>.<listcomp>r   z_Unsupported use of closure. Probably caused by complex control-flow constructs; e.g. try-exceptr   )r   rN   r   r   r   r   rP   r   r   rt   r6   rQ   Zmake_functionr   )r   r   r*   codeclosureannotations
kwdefaultsdefaultsrL  r   Zassume_code_constZfcoder|   r   r   r   op_MAKE_FUNCTION  s&    



zInterpreter.op_MAKE_FUNCTIONc	           	   
   C   s   |  |||||||| d S r   )rm  )	r   r   r*   rh  ri  rj  rk  rl  rL  r   r   r   op_MAKE_CLOSURE  s
      zInterpreter.op_MAKE_CLOSUREc                 C   s   | j jj|j}|| jkrNz| |}W q tk
rJ   d}t|Y qX nB|| j	kr| j	
|}| |}tj|||| jd}ndstd| || d S )N.Unsupported use of op_LOAD_CLOSURE encounteredr   r   r]   )r   rT   r  r  r   r5  r   r   r   r7  rH   r-  r   r   rt   rb   r   )r   r   rL  r*   r  r   r:   r6   r   r   r   op_LOAD_CLOSURE  s    


zInterpreter.op_LOAD_CLOSUREc           	      C   s   t | j}|j|k rR| j|j }z| |}W q tk
rN   d}t|Y qX n2|j| }| j| }| |}tj	|||| j
d}| || d S )Nro  r   )rU   r5  r   r   r   r   r7  r-  r   r   rt   r   )	r   r   rL  r  r*   r  r   r:   r6   r   r   r   rp    s    




c                 C   sf   |  |}|  |}tjj|d| jd}| j||d tjj|  ||fd| jd}| j||d d S )Nr-   r   r  r   r  )r   r   r5   r6   Z	appendvarrL  Z
appendattr
appendinstr   r   r   op_LIST_APPEND  s    

zInterpreter.op_LIST_APPENDc                 C   sz  |  |}|  |}d}| jjs*t|| jjd }t|jtjoN|jj	dk}d }	|rt
| jjd d D ]^}t|tjsd} qt|jtjrqlqlt|jtjr|j|kr|}	|jj } qqld} qql|r|	d krt||r(| jj}
| jjd }|
|
|
|	 |j}|	j}|j|_nNtjj|d| jd}| j||d tjj|  ||fd| jd}| j||d d S )	NzAn unsupported bytecode sequence has been encountered: op_LIST_EXTEND at the start of a block.

This could be due to the use of a branch in a tuple unpacking statement.r`   r^   Frv   r   r  r   )r   r   rq   r   r   rN   r6   r   rQ   rR   r~   rO   rP   r5   rA   r-   r+   rH   rJ   rt   r   rL   )r   r   r5   r6   Z	extendvarrL  r   rz   okZbuild_empty_listZstmtsZbuild_tuple_asgnr^   r   Z
extendattrZ
extendinstr   r   r   op_LIST_EXTEND  sH    





zInterpreter.op_LIST_EXTENDc           	      C   sr   |  |}|  |}|  |}tjj|d| jd}| j||d tjj|  |||fd| jd}| j||d d S )NrK   r   r  r   r  )	r   r   r5   rG   r6   Z
setitemvarrL  Zsetitemattrrq  r   r   r   
op_MAP_ADDD  s    


zInterpreter.op_MAP_ADDc                 C   s$   t jdt| jd}| j||d d S )Nrb   r   r  )r   r   rb   rt   r   )r   r   rL  r  r   r   r   op_LOAD_ASSERTION_ERRORN  s    z#Interpreter.op_LOAD_ASSERTION_ERRORc                 O   s   | j || d S r   )r  r   rV   rF   r   r   r   op_LOAD_METHODZ  s    zInterpreter.op_LOAD_METHODc                 O   s   | j || d S r   )r  rw  r   r   r   op_CALL_METHOD]  s    zInterpreter.op_CALL_METHOD)N)F)NN)N)N)N)r   r   r    r!   r   r   r   r   r   r   r   r   r   r   r  r
  r   r  r  r  r   r  r(  r-  propertyr   r/  r1  r3  r5  r7  r   r   r   r  rC  rD  rE  rF  rG  rH  rN  rO  rT  rV  r\  rd  rk  rn  ro  rp  rq  rs  rt  ru  rw  ry  rz  r{  r}  r  r  r  Z
op_DUP_TOPZop_DUP_TOP_TWOr  r  r  r  r
   r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r	  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r!  r"  r#  r$  r%  r(  r)  r*  r-  r2  r9  r<  r=  r>  rD  rI  rK  rL  rO  rQ  rR  rS  rT  rU  rV  rW  rX  rY  rZ  r[  r^  r_  rc  re  rg  rm  rn  rp  rr  rt  ru  rv  rx  ry  r   r   r   r   r   1  s  </4
=






$





	





	
$C	,			"

	A
r   )0r&  r$   r6  rd   loggingrl   Z
numba.corer   r   r   Znumba.core.errorsr   r   r   Znumba.core.ir_utilsr   r	   Znumba.core.utilsr
   r   r   Znumba.core.byteflowr   r   r   r   Znumba.core.unsafer   Znumba.cpython.unsafe.tupler   objectr   	getLoggerr   r   r"   r?   rI   r[   r\   re   r}   r   r   r   r   r   r   r   r   r   r   <module>   sB   
4) # -   _  _15