3
^2'                 @   s0  d Z ddlmZ ddlmZmZmZmZ ddlm	Z	 ddlm
Z
mZ dJZdZdj ZdKddZdLddZdd ZdMddZdPd!d"ZdQd#d$ZdRd%d&ZdSd'd(ZdTd)d*ZdWd+d,ZdZd-d.Zd/d0 Zd[d2d3Zd]d4d5Zd6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"d@dA Z#dBdC Z$dDdE Z%dFdG Z&dHdI Z'dS )^z
Common routines for entering and classifiying opcodes. Inspired by,
limited by, and somewhat compatible with the corresponding
Python opcode.py structures
    )deepcopy)findlinestarts
findlabelsget_jump_target_mapsget_jump_targets)wordcode)IS_PYPYPYTHON_VERSION<<===!=>>=innot-inisis-notexception-matchBADZ   z|
hascompare hascondition
hasconst hasfree hasjabs hasjrel haslocal
hasname hasnargs hasstore hasvargs oppop oppush
nofollow
NFc             C   s   |r|| d< || d< t | d< t| d< t| d< |dkrNt| d< t| d< t| d	< ntj| d< tj| d< tj| d	< t|j| d
< t|j	| d< xt
D ]}tt||| |< qW dS )zSets up a number of the structures found in Python's
    opcode.py. Python opcode.py routines assign attributes to modules.
    In order to do this in a modular way here, the local dictionary
    for the module is passed.
    python_versionis_pypycmp_opHAVE_ARGUMENTr   g      @r   r   r   opmapopnameN)r   r   r   r   r   r   r   r   r   r   fields2copylistgetattr)lZfrom_modversionr   field r#   N/home/psgendb/BIRCHDEV/python/lib/python3.6/site-packages/xdis/opcodes/base.pyinit_opdata+   s"    




r%         c             C   s"   t | |||| | d j| d S )N
hascompare)def_opappend)r    nameoppoppushr#   r#   r$   
compare_opH   s    r/   c             C   s   | d j | d S )Nr(   )r*   )r    r+   r,   r#   r#   r$   conditional_opL   s    r0   c             C   s"   t | |||| | d j| d S )Nhasconst)r)   r*   )r    r+   r,   r-   r.   r#   r#   r$   const_opO   s    r2   Tc             C   sF   || d |< || d |< || d |< || d |< |sB| d j | d S )Nr   r   ZoppushZoppopnofollow)r*   )r    op_nameopcoder-   r.   fallthroughr#   r#   r$   r)   S   s    r)   c             C   s"   t | |||| | d j| d S )Nhasfree)r)   r*   )r    r+   r,   r-   r.   r#   r#   r$   free_op[   s    r8   c             C   s8   t | |||||d | d j| |r4| d j| d S )N)r6   hasjabshascondition)r)   r*   )r    r+   r,   r-   r.   conditionalr6   r#   r#   r$   jabs_op_   s    r<   c             C   s4   t | |||| | d j| |r0| d j| d S )Nhasjrelr:   )r)   r*   )r    r+   r,   r-   r.   r;   r6   r#   r#   r$   jrel_ope   s    r>   c             C   s"   t | |||| | d j| d S )Nhaslocal)r)   r*   )r    r+   r,   r-   r.   r#   r#   r$   local_opk   s    r@   c             C   s"   t | |||| | d j| d S )Nhasname)r)   r*   )r    r4   op_coder-   r.   r#   r#   r$   name_opo   s    rC   c             C   s&   t | |||||d | d j| d S )N)r6   hasnargs)r)   r*   )r    r+   r,   r-   r.   r6   r#   r#   r$   nargs_ops   s    rE   c             C   sr  d| | d |< || d kr*| d j | || d krD| d j | || d kr^| d j | || d krx| d j | || d kr| d j | || d kr| d j | || d	 kr| d	 j | || d
 kr| d
 j | || d kr| d j | || d kr| d j | || d kr2| d j | || d krN| d j | | d | |ksdt| d |= dS )zRemove an opcode. This is used when basing a new Python release off
    of another one, and there is an opcode that is in the old release
    that was removed in the new release.
    We are pretty aggressive about removing traces of the op.
    z<%s>r   r1   r(   r:   r7   r9   rA   r=   r?   rD   hasvargsr3   r   N)removeAssertionError)r    r+   r,   r#   r#   r$   rm_opw   s6    rI   defc             C   s|   |dkrt | |||| nP|dkr4t| |||| n6|dkrNt| |||| n|dksZtt| |||| | d j| d S )Nr+   localfreerJ   hasstore)rC   r@   r8   rH   r)   r*   )r    r+   r,   r-   r.   Zis_typer#   r#   r$   store_op   s    rN   c             C   s"   t | |||| | d j| d S )NrF   )r)   r*   )r    r4   rB   r-   r.   r#   r#   r$   
varargs_op   s    rO   c             C   s   | d d | d< d| kr d| d< d| d > d | d< | d d | d< t | d | d< x | d D ]}| d | | |< q^W t| d | d  | d	< t| d
 | d< t|  d S )Nr   EXTENDED_ARGZEXTENDED_ARG_SHIFT   r'   ZARG_MAX_VALUEr=   r9   ZJUMP_OPsr3   ZNOFOLLOW)fix_opcode_names	frozensetopcode_check)r    r,   r#   r#   r$   finalize_opcodes   s    rU   c             C   s   t dd | j D S )z
    Python stupidly named some OPCODES with a + which prevents using opcode name
    directly as an attribute, e.g. SLICE+3. So we turn that into SLICE_3 so we
    can then use opcode_23.SLICE_3.  Later Python's fix this.
    c             S   s    g | ]\}}|j d d|fqS )+_)replace).0kvr#   r#   r$   
<listcomp>   s   z$fix_opcode_names.<locals>.<listcomp>)dictitems)r   r#   r#   r$   rR      s    rR   c             C   s8   | j d|d d i | j d|d d i t| d S )NPJIFr   POP_JUMP_IF_FALSEPJITPOP_JUMP_IF_TRUE)updateupdate_sets)gr    r#   r#   r$   
update_pj3   s    rf   c             C   s8   | j d|d d i | j d|d d i t| d S )Nr_   r   ZJUMP_IF_FALSEra   ZJUMP_IF_TRUE)rc   rd   )re   r    r#   r#   r$   
update_pj2   s    rg   c             C   s&  t | d | d< t | d | d< t | d | d< t | d | d< t | d	 | d
< t | d | d< t | d d | d d g| d< | d dk rt | d d g| d< n
t  | d< t | d | d< | d | d
 B | d B | d B | d< t | d | d< t | d | d< t | d | d< t | d | d< d S ) Nr(   ZCOMPARE_OPSr:   ZCONDITION_OPSr1   Z	CONST_OPSr7   ZFREE_OPSr=   ZJREL_OPSr9   ZJABS_OPSr   JUMP_ABSOLUTEJUMP_FORWARDZJUMP_UNCONDITONALr   gffffff@
SETUP_LOOPZLOOP_OPSr?   Z	LOCAL_OPSZJUMP_OPSrA   ZNAME_OPSrD   Z	NARGS_OPSrF   Z	VARGS_OPSrM   Z	STORE_OPS)rS   )r    r#   r#   r$   rd      s"    
$rd   c             C   s   t | d S )Nr'   rQ   i   )str)argr#   r#   r$   format_extended_arg   s    rm   c             C   s   t | d\}}d||f S )zEncoded positional and named args. Used to
    up to about 3.6 where wordcodes are used and
    a different encoding occurs. Pypy36 though
    sticks to this encoded version though.   z%d positional, %d named)divmod)argcname_defaultpos_argsr#   r#   r$   %format_CALL_FUNCTION_pos_name_encoded   s    rs   c             C   s*   t | d\}}t |d\}}d|||f S )Nrn   z2%d positional, %d name and default, %d annotations)ro   )rp   Zname_and_annotaterr   Zannotate_argsrq   r#   r#   r$   format_MAKE_FUNCTION_arg  s    rt   c          
      s   t t d  dkrt d kryVddl}t|jtfdd d j D sVtt fd	dj D sttW n   ddl	}Y nX dS )
zWhen the version of Python we are running happens
    to have the same opcode set as the opcode we are
    importing, we perform checks to make sure our opcode
    set matches exactly.
    r   g{Gz?r   r   Nc             3   s   | ]}| j  kV  qd S )N)r^   )rY   item)r   r#   r$   	<genexpr>  s    zopcode_check.<locals>.<genexpr>r   c             3   s   | ]}| d  j  kV  qdS )r   N)r^   )rY   ru   )r    r#   r$   rv     s    )
absr	   r   disrR   r   allr^   rH   sys)r    rx   rz   r#   )r    r   r$   rT     s    
""rT   c             C   sT   i }x| j  D ]}||| | < qW x,t|j  D ]}tdt||| f  q0W dS )zUtility for dumping opcodesz%-3s %sN)keyssortedprintrk   )r   Zop2namerZ   ir#   r#   r$   dump_opcodes  s
    r   )r
   r   r   r   r   r   r   r   r   r   r   r   )NF)r&   r'   )r   r'   r   )r   r   T)r   r'   )r   r   FT)r   r   FT)r   r'   r   r   )r   r   r   r   )r   r   T)r   r'   rJ   )r   r'   )(__doc__copyr   Zxdis.cross_disr   r   r   r   xdisr   r   r	   r   r   splitr   r%   r/   r0   r2   r)   r8   r<   r>   r@   rC   rE   rI   rN   rO   rU   rR   rf   rg   rd   rm   rs   rt   rT   r   r#   r#   r#   r$   <module>   s@    









&


