
    Phz                        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	m
Z
mZmZmZmZ ddlmZmZmZmZ ej(                   G d d             Zej(                   G d d             Zd	ej,                  d
efdZ G d d      Zdeddd
efdZd
efdZd
efdZd
efdZd
ee   fdZd
ee   fdZd
ee   fdZ	 dUde de d
eee    ee e gdf   f   fdZ!d Z"de d
ee    fdZ#de fdZ$ej(                   G d d             Z%de d
ee    fd Z&d!ee    d
e fd"Z'd#ee%   d
dfd$Z(d%e)d
ee%   fd&Z*d#ee%   d
e)fd'Z+d(ee   d)e d
ee)e)f   fd*Z,d+e
e ef   d,e fd-Z-dVd.Z. e/ej`                        Z1d/ed
dfd0Z2d(ee   d1e fd2Z3d3 Z4d4e)d(ee   fd5Z5d(ee   d
ee%   fd6Z6d#ee   d
dfd7Z7d(ee   d
dfd8Z8d(ee   fd9Z9d(ee   d
dfd:Z:d(ee   d
ee   fd;Z;d(ee   d
dfd<Z<d=ejz                  d(ee   d
dfd>Z>d(ee   d
e fd?Z?d@ddd@d@dAdBddCdd@dDZ@d
e fdEZAdVdFZBdVdGZCd
eDfdHZEdI ZF e/ej                        ZH e/ej                        ZJ e/ej                        ZL e/ej                        ZNd
e fdJZOdWd(ee   fdKZPd
eeD   fdLZQdXd
ejz                  fdMZRd(ee   dNeeD   dOe
eDef   d
eee   ejz                  f   fdPZSdQ ZTdXd
ee   fdRZU ej                         ZWd
eDfdSZXd=ejz                  d
eYfdTZZy)Y    N)AnyCallablecastDictIteratorListOptionalTuple   )get_indexofpropagate_line_numsremove_extra_line_numsstacksize_analysisc                   X    e Zd ZU ded<   ded<   ded<   eed<   eed<   defdZdefd	Zy
)InstructionExnTabEntryInstructionstartendtargetdepthlastireturnc                     d| j                   j                          d| j                  j                          d| j                  j                          d| j                   d| j
                   dS )NzInstructionExnTabEntry(start=z, end=z	, target=z, depth=z, lasti=))r   short_inst_reprr   r   r   r   selfs    pC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/_dynamo/bytecode_transformation.py__repr__zInstructionExnTabEntry.__repr__   sl    +DJJ,F,F,H+I J88++-. /kk1134 5ZZLA7	
    c                    | j                   |j                   u xrh | j                  |j                  u xrN | j                  |j                  u xr4 | j                  |j                  k(  xr | j                  |j                  k(  S N)r   r   r   r   r   )r   os     r   __eq__zInstructionExnTabEntry.__eq__!   so    JJ!''! &AEE!&qxx'& 

agg%& 

agg%	
r    N)	__name__
__module____qualname____annotations__intboolstrr   r$    r    r   r   r      s6    	JK
# 

4 
r    r   c                       e Zd ZU dZeed<   eed<   ee   ed<   eed<   dZ	ee   ed<   dZ
ee   ed<   d	Zeed
<   dZed   ed<   dZed    ed<   dZee   ed<   defdZdefdZdefdZy)r   z$A mutable version of dis.InstructionopcodeopnameargargvalNoffsetstarts_lineFis_jump_targetdis.Positions	positionsr   exn_tab_entryr   c                     t        |       S r"   idr   s    r   __hash__zInstruction.__hash__;   s    $xr    c                 0    t        |       t        |      k(  S r"   r9   )r   others     r   r$   zInstruction.__eq__>   s    $x2e9$$r    c                 <    d| j                    d| j                   dS )NzInstruction(opname=z	, offset=r   )r/   r2   r   s    r   r   zInstruction.short_inst_reprA   s    $T[[M4;;-qIIr    )r%   r&   r'   __doc__r)   r(   r+   r	   r   r2   r3   r4   r*   r6   r   r7   r   r;   r$   r   r,   r    r   r   r   +   s    .KK	#K FHSM !%K#% ND +/Ix(/&*FH]#*6:M823:# %t %J Jr    r   ir   c                     t        | j                  | j                  | j                  | j                  | j
                  | j                  | j                  t        | dd             S )Nr6   )	r   r.   r/   r0   r1   r2   r3   r4   getattr)r@   s    r   convert_instructionrC   E   sN    							;%	 	r    c                       e Zd ZdefdZy)_NotProvidedr   c                      y)NrE   r,   r   s    r   r   z_NotProvided.__repr__S   s    r    N)r%   r&   r'   r+   r   r,   r    r   rE   rE   R   s    # r    rE   )r0   r1   r   c                    | dk7  sJ |du|t         uz   |duz   }|dkD  rt        d      |t        |t              st        d      t	        t
        j                  |    | |||      S )a  
    At most one of `arg`, `argval`, and `target` can be not None/_NotProvided.
    This is to prevent ambiguity, e.g. does
        create_instruction("LOAD_CONST", 5)
    mean load the constant at co_consts[5], or load the constant 5?

    If `arg` is not provided, it will be computed during assembly from
    `argval` or `target`.

    Do not use for LOAD_GLOBAL - use create_load_global instead.
    LOAD_GLOBALNr   z@only one of arg, argval, and target can be not None/_NotProvidedz#instruction arg must be int or None)r.   r/   r0   r1   r   )rE   RuntimeError
isinstancer)   r   disopmap)namer0   r1   r   cnts        r   create_instructionrO   W   s     =   d?v\9
:fD>P
QC
QwN
 	
 z#s3@AAyytVF r    c                 J    t         j                  dk\  rdnd}t        ||       S )N      JUMP_FORWARDJUMP_ABSOLUTEr   sysversion_inforO   )r   insts     r   create_jump_absoluter[   s   s#     --8>oDd622r    c                 B    t        t        j                  d   d||       S )a  
    `name` is the name of the global to be loaded.
    `push_null` specifies whether or not a NULL should be pushed to the stack
    before the global (Python 3.11+ only).

    Python 3.11 changed the LOAD_GLOBAL instruction in that the first bit of
    the instruction arg specifies whether a NULL should be pushed to the stack
    before the global. The remaining bits of the instruction arg contain the
    name index. See `create_call_function` for why this NULL is needed.

    The instruction's `arg` is actually computed when assembling the bytecode.
    For Python 3.11, push_null information is propagated through the arg.

    NOTE: we don't use create_instruction since LOAD_GLOBAL is the only instruction
    where both arg and argval need to be specified.
    rH   )r.   r/   r0   r1   )r   rK   rL   )rM   	push_nulls     r   create_load_globalr^   x   s&    " yy'	 r    c                  X    t         j                  dk\  rt        dd      S t        d      S )NrQ   COPYr   r0   DUP_TOPrW   r,   r    r   create_dup_toprc      s(    
7"!&a00i((r    c                 |   | dk  rg S t         j                  dk\  r&t        | dd      D cg c]  }t        d|       c}S t         j                  dk  r| dk\  rt	        d|  d	      t         j                  d
k  r| dk\  rt	        d|  d      | dk  rt        dg d| dz
     z         gS t        d|       gS c c}w )a  
    Returns a "simple" sequence of instructions that rotates TOS to the n-th
    position in the stack. For Python < 3.11, returns a single ROT_*
    instruction. If no such instruction exists, an error is raised and the
    caller is expected to generate an equivalent sequence of instructions.
    For Python >= 3.11, any rotation can be expressed as a simple sequence of
    swaps.
    r   rQ   SWAPra   )rR         zrotate z not supported for Python < 3.8rR   
      z  not supported for Python < 3.10ROT_)TWOTHREEFOUR   ROT_N)rX   rY   rangerO   AttributeError)nr@   s     r   create_rot_nru      s     	Av	
7" <AAr?K?a"6q1?KK & Q!Vwqc)HIJJ
'!a1fwqc)IJKKAv"6,DQU,K#KLMMwA.// Ls   B9c                 .   t         j                  dk\  rug }|r7|j                  t        d             |j	                  t        | dz                |j                  t        d|              |j                  t        d|              |S t        d|       gS )a  
    Creates a sequence of instructions that makes a function call.

    `push_null` is used in Python 3.11+ only. It is used in codegen when
    a function call is intended to be made with the NULL + fn convention,
    and we know that the NULL has not been pushed yet. We will push a
    NULL and rotate it to the correct position immediately before making
    the function call.
    push_null should default to True unless you know you are calling a function
    that you codegen'd with a null already pushed, for example
    (assume `math` is available in the global scope),

    create_load_global("math", True)  # pushes a null
    create_instruction("LOAD_ATTR", argval="sqrt")
    create_instruction("LOAD_CONST", argval=25)
    create_call_function(1, False)
    rQ   	PUSH_NULLrp   PRECALLra   CALLCALL_FUNCTION)rX   rY   appendrO   extendru   )nargsr]   outputs      r   create_call_functionr      s~    $ 7"MM,[9:MM,uqy12(>?(U;<E:;;r    c                 x    t         j                  dk\  rt        d|       t        d|       gS t        d|       gS )NrQ   rx   ra   ry   CALL_METHODrW   )r}   s    r   create_call_methodr      s@    
7"ye4v51
 	
 }%899r    linenobytenoc                 L     t         j                  dk  sJ g  fd}|fS )z
    Used to create typing.CodeType.co_lnotab
    See https://github.com/python/cpython/blob/main/Objects/lnotab_notes.txt
    This is the internal format of the line number table if Python < 3.10
    ri   c                     |k7  s| k7  rkt        dt        |z
  d            }t        dt        | z
  d            }|dk7  s|dk7  sJ |z  |z  j                  ||dz  f       |k7  rd| k7  rjy y )Nr      i   maxminr|   )
lineno_new
byteno_newbyte_offsetline_offsetr   r   lnotabs       r   updatezlnotab_writer.<locals>.update   s    F"jF&:aZ&%8#!>?KdC
V(;S$ABK!#{a'777k!Fk!FMM;d(:;< F"jF&:r    rX   rY   )r   r   r   r   s   `` @r   lnotab_writerr      s/     g%%%F= 6>r    c                     t         j                  dk\  rt         j                  dk  sJ g | ddfdfd}fd}||fS )z
    Used to create typing.CodeType.co_linetable
    See https://github.com/python/cpython/blob/main/Objects/lnotab_notes.txt
    This is the internal format of the line number table for Python 3.10
    ri   rQ   r   c                     | dk7  s|dk7  ret        dt        | d            }t        dt        |d            }|dk7  s|dk7  sJ | |z  } ||z  }j                  ||dz  f       | dk7  r^|dk7  rdy y )Nr      ir   r   r   )byteno_deltalineno_deltar   r   	linetables       r   _updatez%linetable_310_writer.<locals>._update   s    a<1#4a\3!78KdCc$:;K!#{a'777K'LK'Lk;+=>? a<1#4r    c                 4    |z
  }| |       | z
  | y r"   r,   )r   r   r   r   r   r   r   s      r   r   z$linetable_310_writer.<locals>.update  s+    !F*l+!F*r    c                      | z
         y r"   r,   )total_bytesr   r   r   s    r   r   z!linetable_310_writer.<locals>.end  s    f$l3r    r   )first_linenor   r   r   r   r   r   r   s      @@@@@r   linetable_310_writerr      sY     w&3+;+;g+EEEIFLF@4 fc!!r    rt   c                     | dk\  sJ | dz  g}| dz  } | dkD  r,|dxx   dz  cc<   |j                  | dz         | dz  } | dkD  r,|S )z~
    6-bit chunk encoding of an unsigned integer
    See https://github.com/python/cpython/blob/3.11/Objects/locations.md
    r   ?      re   @   )r{   )rt   bs     r   encode_varintr     s`    
 6M6	
RA!GA
a%	"	R	a a% Hr    r   c                 N    t         j                  dk\  sJ g | dfd}|fS )z
    Used to create typing.CodeType.co_linetable
    See https://github.com/python/cpython/blob/3.11/Objects/locations.md
    This is the internal format of the line number table for Python 3.11
    rQ   c                       r j                   nd } fd}|d}n|z
  }||dkD  r ||d       |dz  }|dkD  r |||       y )Nc                    d|cxk  rdk  sJ  J d}r~j                   rj                  fj                  Zj                  Nj	                  d|z   dz
         j                  j                   z
  j                  dz   j                  dz   f}nj	                  d|z   dz
         | dk  r
|  dz  dz  } n| dz  } j                  t        |              |D ]  }j                  t        |              y )Nr   rg   r,      r      )r   
end_lineno
col_offsetend_col_offsetr{   r|   r   )deltasizeother_varintsrt   r   r6   s       r   r   z5linetable_311_writer.<locals>.update.<locals>._update2  s   t=q= = = .0M$$0((4((4,,8  !4q!89 ((9+;+;;((1,,,q0!   !4q!89qy &Q!+!]512"  q!12 #r    r   rg   )r   )r6   	inst_sizer   r   r   r   r   s   `    r   r   z$linetable_311_writer.<locals>.update.  sc    )2Y%%
	3@ L%.LF!mL!$NI !m 	i(r    )r6   r5   r   )r   r   r   r   s     @@r   linetable_311_writerr   $  s6     w&&&IF,)\ fr    c                   @    e Zd ZU eed<   eed<   eed<   eed<   eed<   y)ExceptionTableEntryr   r   r   r   r   N)r%   r&   r'   r)   r(   r*   r,   r    r   r   r   _  s    J	HKJKr    r   c                     | dk\  sJ | dz  g}| dz  } | dkD  r|j                  | dz         | dz  } | dkD  rt        t        |            }t        t	        |      dz
        D ]  }||xx   dz  cc<    |S )zR
    Similar to `encode_varint`, but the 6-bit chunks are ordered in reverse.
    r   r   r   r   r   )r{   listreversedrr   len)rt   r   r@   s      r   encode_exception_table_varintr   h  s     6M6	
RA!GA
a%	R	a a% 	Xa[A3q6A:	!
 Hr    
bytes_iterc                 l    t        |       }|dz  }|dz  r|dz  }t        |       }||dz  z  }|dz  r|S )z5
    Inverse of `encode_exception_table_varint`.
    r   r   r   )next)r   r   vals      r   decode_exception_table_varintr   x  sN     	ZA
b&C
b&	q2v b& Jr    tabc                    t        t        |       dz
        D ]j  }| |   j                  | |   j                  k  rH| |   j                  | |dz      j                  k  r&| |dz      j                  | |dz      j                  k  rjJ  y)z
    Verifies that a list of ExceptionTableEntries will make a well-formed
    jump table: entries are non-empty, sorted, and do not overlap.
    r   N)rr   r   r   r   )r   r@   s     r   check_exception_tabler     s}    
 3s8a< FLLCFJJ&A

SQZ---AE
  CAJNN2	
3 !r    exntabc           	      6   t        |       }g }	 	 t        |      dz  }t        |      dz  }||z   dz
  }t        |      dz  }t        |      }|dz	  }t        |dz        }	|j                  t	        |||||	             o# t
        $ r t        |       |cY S w xY w)z
    Parse the exception table according to
    https://github.com/python/cpython/blob/3.11/Objects/exception_handling_notes.txt
    rp   r   )iterr   r*   r{   r   StopIterationr   )
r   exntab_iterr   r   lengthr   r   dlr   r   s
             r   parse_exception_tabler     s    
 v,K
C1+>BE2;?!CF&.1$C2;?!CF.{;B!GEaLEJJ*5#vueLM   c"
s   A0A? ?BBc                    g }| D ]  }t        |j                  dz        }|dxx   dz  cc<   |j                  |       |j                  |j                  z
  dz   }|j                  t        |dz               |j                  t        |j                  dz               |j
                  dz  |j                  z   }|j                  t        |              t        |      S )zd
    Inverse of parse_exception_table - encodes list of exception
    table entries into bytes.
    rp   r      r   )r   r   r|   r   r   r   r   bytes)r   r   entryfirst_entryr   r   s         r   assemble_exception_tabler     s    
 	A3EKK14DEA& 	U[[(1,	.v{;<	.u||q/@ABkkQ%++-	.r23  8Or    instructionsfirstlinenoc                    g }t         j                  dk\  rt        |      \  }}d}t        |       D ]  \  }}|j                  dk(  r=d}|dz  }dD ]0  }	| ||	z      j                  dk7  s| ||	z      j
                  |_         n nt        |      dz  |z   }d} ||j
                  |       d}|j                  xs d}
|j                  |j                  |
dz  f       t        t        |      dz  dz
        D ]  }|j                  d         nt         j                  d	k  rt        |      \  }}nt        |      \  }}}| D ]Z  }|j                   ||j                  t        |             |j                  xs d}
|j                  |j                  |
dz  f       \ t         j                  d	k\  r t        |             t        |      t        |      fS )
z)Do the opposite of dis.get_instructions()rQ   r   EXTENDED_ARGr   r   rp   rR   rp   r   )r   r   ri   )rX   rY   r   	enumerater/   r6   instruction_sizer0   r|   r.   rr   r   r   r3   r   r   )r   r   coder   update_linenonum_extr@   rZ   r   jr0   _r   s                r   assembler     s   D
7" 4[ A .GAt{{n,	1"A#AE*11^C)5a!e)<)F)F #
 -T2a7'A	$..)4G((-aCKKcDj12+D1Q6:;F# <! /& g%$1+$>!FM)=k)J&FM3 D+d..D	:((-aCKKcDj12	 ! w&D	N;f%%r    offset_to_instr2   c                 l    dD ]/  }| ||z      j                   t        j                  k7  s'| ||z      c S  y)zU
    Get the instruction located at a given offset, accounting for EXTENDED_ARGs
    )r   rp   rh   r   Nr.   rK   r   )r   r2   rt   s      r   _get_instruction_by_offsetr     s?     &1*%,,0@0@@!&1*--  r    c                     | D ci c]  }|j                   | }}| D ]V  }|j                  t        j                  v s|j                  t        j                  v s<t        ||j                        |_        X yc c}w )z9Replace jump targets with pointers to make editing easierN)r2   r.   rK   hasjabshasjrelr   r1   r   )r   rZ   jump_targetss      r   virtualize_jumpsr     s`    2>?,$DKK%,L?;;#++%)C4\4;;ODK  @s   A7instructionc                    t         j                  dk  rt        d      d| j                  v r"| j                  j	                  dd      | _        n;d| j                  v r"| j                  j	                  dd      | _        nt        d      t        j                  | j                     | _        | j                  t        v sJ y )NrQ   z+Cannot flip jump direction in Python < 3.11FORWARDBACKWARDz-Instruction is not a forward or backward jump)
rX   rY   rI   r/   replacers   rK   rL   r.   
_REL_JUMPS)r   s    r   flip_jump_directionr     s    
'!HIIK&&&(//77	:N	{))	)(//77
INLMM;#5#56K+++r    idxc                     | |   }dD ]5  }||k\  r,| ||z
     j                   t        j                  k(  r	| ||z
     }4 |S  |S )zp
    i.e. get the first EXTENDED_ARG instruction (if any) when targeting
    instructions[idx] with a jump.
    r   r   )r   r   r   r2   s       r   _get_instruction_frontr     sX    
 #F&=\#,7>>#BRBRR!#,/FM 
 Mr    c                    t        |       }t        t        j                        j	                  t        t        j
                              }| D ]  }|j                  |v st        | ||j                           }|j                  t        j                  v rbt        j                  dk  r|j                  |_        n+t        j                  dk  rt        |j                  dz        |_        nt        d      t        |j                  |j                  z
  t        |      z
        |_        |j                  dk  rJt        j                  dk  rt        d      |j                   |_        d|j                   v rGt#        |       n;|j                  dkD  r,t        j                  dk\  rd|j                   v rt#        |       t        j                  dk\  r|xj                  dz  c_        |j                  |_        d	|j                   |_         y
)zJFill in args for virtualized jump target after instructions may have movedri   rQ   rp   z+Python 3.11+ should not have absolute jumpsr   z*Got negative jump offset for Python < 3.11r   r   zto N)r   setrK   r   unionr   r.   r   r   rX   rY   r2   r0   r)   rI   r   r/   r   r1   argrepr)r   indexofjumpsrZ   r   s        r   devirtualize_jumpsr     s   ,'G""3s{{#34E;;%+L'$++:NOF{{ckk)##g-%}}DH%%/  #6==1#45DH&'TUU v}}t{{:=Md=SST88a<'''1*+WXX $yDH DKK/+D1XX\''72zT[[7P+D1##w.HHNH --DK 0DL= r    exn_tab_bytesc                   	
 t        |       }|D ci c]  }t        t        |j                        | c}	t	        	j                               
dt        |      	 	
fd} |       \  }}|D ]s  }|j                  |j                  kD  r$ |       \  }}|j                  |j                  kD  r$|j                  |j                  k\  sZt        j                  |      |_
        u yc c}w # t        $ r Y yw xY w)zDReplace exception table entries with pointers to make editing easierr   c                  v   t              } t              k  r8   | j                  k  r&dz  t              k  r   | j                  k  r&dkD  sJ dz
     }t        t	        | j
                        t	        |      t	        | j                        | j                  | j                        }| |fS )Nr   r   )	r   r   r   r   r   r   r   r   r   )r   
end_offset
inst_entryend_offset_idxexn_tab_iterr   offsetss      r   stepz(virtualize_exception_table.<locals>.stepD  s    &E W-'.2IUYY2V!# W-'.2IUYY2V "A%%% !!34J/*>5;;G*>:F*>5<<HJ *$$r    N)r   r   r)   r2   sortedkeysr   r   r   copyr7   r   )r   r   exn_tabrZ   r   r   r   r   r   r   r   s          @@@@r   virtualize_exception_tabler  ;  s    #M2G?KL|td3,d2|LN^((*+GN=L	%, !Fz D++		)$(F!z ++		){{ekk)%)YYz%:"	 !; MD  s$   "C+ AC0 4C0 C0 0	C<;C<c           	         i t        |       }| D ]  }|j                  st        | ||j                  j                           j                  }t        t        |j                  j                  j                        t        |j                  j                        z   dz
  }t        | ||j                  j                           j                  }||f}||j                  j                  |j                  j                  f}|v r
|   |k(  sJ ||<    t        j                         d       }dg g fd}	|D ]  }r&d   d   |d   k  r |	        rd   d   |d   k  rrod   d   |d   cxk  r|d   cxk  rd   d   k  sJ  J t        d   d         }
|
|d   k  r(j                  t!        |
|d   dz
  gd              |d   j                  |        r
 |	        r
t#               S )zMCompute exception table in list format from instructions with exn_tab_entriesrp   c                     | d   | d    fS Nr   r   r,   ts    r   <lambda>z)compute_exception_table.<locals>.<lambda>  s    1!ur    keyr   c                      rQj                         } | d   k  r8j                  t        t        | d         | d   g|            | d   dz   yyy)zT
        Pop the key_stack and append an exception table entry if possible.
        r   r   rp   N)popr{   r   r   )r  exn_dictr  	key_stacknextis    r   r  z$compute_exception_table.<locals>.pop  se    
 --/CA'CFE(:CFSXc]S A
	  r    re   r   )r   r7   r   r   r2   r   r)   r   r   r   r   r   r   r  r   r{   r   r   )r   r   rZ   r   r   r   r  r   keys_sortedr  leftr  r  r  r  s              @@@@r   compute_exception_tabler  d  s5    >@H,'G*gd&8&8&>&>?f 
 S$,,00778"4#5#5#9#9:; 
 ,gd&8&8&?&?@f  #,C4--33T5G5G5M5MNCh}+++HSM' 6 .EFKE')I)+G# IbM!,s1v5E IbM!,s1v5R=#s1vKQK9R=;KKKKKKuimA./Dc!f}'c!fqjS8IbM;RS FE   '"Nr    c                 *   g }| D ]  }||j                      ||j                     f}|r/|d   d   |d   k  r!|j                          |r|d   d   |d   k  r!|r(|d   d   |d   cxk  r|d   cxk  r|d   d   k  sJ  J |j                  |        y)z
    Checks `tab` is a properly sorted list of nested InstructionExnTabEntry's,
    i.e. no entries partially overlap.
    "Properly sorted" means entries are sorted by increasing starts, then
    decreasing ends.
    re   r   r   N)r   r   r  r{   )r   r   entry_stackr   r  s        r   !check_inst_exn_tab_entries_nestedr    s     *,Ku{{#WUYY%78k"oa03q69OO k"oa03q69r?1%QO3q6O[_Q=OOOOOO3 r    c                    t        |       }i }| D ]h  }|j                  s||j                  j                     ||j                  j                     f}||v r|j                  ||   k(  sJ |j                  ||<   j t	        |j                         d       D cg c]  }||   	 }}t        ||       |D ]M  }t        ||j                     ||j                     dz         D ]  }t        j                  |      | |   _        ! O yc c}w )z~
    Copies exception table entries to all instructions in an entry's range.
    Supports nested exception table entries.
    c                     | d   | d    fS r  r,   r  s    r   r
  z2propagate_inst_exn_table_entries.<locals>.<lambda>  s    adQqTE]r    r  r   N)	r   r7   r   r   r   r  r  rr   r  )r   r   entriesrZ   r  sorted_entriesr   r@   s           r    propagate_inst_exn_table_entriesr    s   
 ,'G=?G**001**../C g~))WS\999--GCL  !'w||~;R S S S   &ng>  wu{{+WUYY-?!-CDA,0IIe,<LO) E  s   Dc                    t        |       }t               }t        |       D ]  \  }}|j                  st        j
                  dk\  sJ t        |j                        |vsJ |j                  t        |j                               |j                  }|j                  |v sJ |j                  |v sJ |j                  |v sJ ||j                     |cxk  r||j                     k  rJ  J  y)aJ  
    Checks that exn_tab_entries of instructions are valid.
    An entry's start, end, and target must be in instructions.
    Instructions with an exn_tab_entry are located within
    the entry's start and end instructions.
    Instructions do not share exn_tab_entries.

    Implicitly checks for no duplicate instructions.
    rQ   N)r   r   r   r7   rX   rY   r:   addr   r   r   )r   r   exn_tab_entry_setr@   rZ   r   s         r    check_inst_exn_tab_entries_validr     s     ,'G\*4##w...d(()1BBBB!!"T%7%7"89&&E;;')))99'''<<7***5;;'1B		0BBBBBB +r    c                 p    | D cg c]"  }|j                   t        j                  k7  s!|$ c}| d d  y c c}w r"   r   )r   r@   s     r   strip_extended_argsr"    s.    ".O,Q!((c>N>N2Nq,OLOOs   "33c                     ddd}| D ]G  }|j                   |v s||j                      |_         t        j                  |j                      |_        I | S )zFLOAD_METHOD puts a NULL on the stack which causes issues, so remove it	LOAD_ATTRrz   )LOAD_METHODr   )r/   rK   rL   r.   )r   rewritesrZ   s      r   remove_load_call_methodr'    sN    *?KH;;(""4;;/DK))DKK0DK  r    c                    g }| D ]  }|j                  |       d|j                  v s#t        dt        d|j                  v             }|j                  |_        t        d|j                  v rdnd|j                        }t        j                  d	   |_	        d	|_        d |_        d |_        |j                  ||g        || d d  y )
N_NONEIS_OPNOTra   r   POP_JUMP_FORWARD_IF_TRUEPOP_JUMP_BACKWARD_IF_TRUErV   
LOAD_CONST)r{   r/   rO   r)   r0   r1   r   rK   rL   r.   r|   )r   	new_instsrZ   is_opjump_ops        r   remove_jump_if_noner2    s    Idkk!&wC8L4MNE 99EL(+ +0{{	G ))L1DK&DKDHDKeW-.! "  LOr    r   c                    | j                   xs
 t               | j                  xs
 t               z   }g }t        |      D ]   \  }}|j	                  |       |j
                  dk(  s(|j                  dk(  s8||dz      }|j
                  dv sO|j                  dk(  s_d|v sJ |j	                  t        dd             | j                  d   }||v r|j	                  t        d|             n|j	                  t        d	|             d
|_        d
|_        |j
                  dk(  s||d
z      }d
|_        d
|_         ||dd y)z3convert super() with no args into explicit arg formrH   superr   )rz   rx   r   	__class__
LOAD_DEREF)r1   	LOAD_FASTrp   rx   N)
co_cellvarstupleco_freevarsr   r{   r/   r1   r0   rO   co_varnames)	r   r   cell_and_freer~   r   rZ   r  	first_var	call_insts	            r   explicit_superr?    s*   %%0T5E5E5PQMF|,	Td;;-'DKK7,B q)E||;;		Q"m3330kRS ,,Q/	-MM"4\)"TUMM"4["ST	 <<9, ,S1W 5I$%IM'(I$% -( LOr    c                    g fd}| D ]  }|j                   t        j                  k(  r	d|_        na|j                  r|j                  dkD  r |d       j	                  t        d|j                  dz	               j	                  t        d|j                  dz	               j	                  t        d|j                  d	z	               n|j                  rj|j                  d
kD  r[ |d       j	                  t        d|j                  dz	               j	                  t        d|j                  d	z	               nL|j                  r@|j                  dkD  r1 |d       j	                  t        d|j                  d	z	               j	                  |        t              t        |       z
  }|dk\  sJ | dd |S )z,Fill in correct argvals for EXTENDED_ARG opsc                     t        |       D ]6  }sd   j                  t        j                  k(  s'j	                          8 y )Nre   )rr   r.   rK   r   r  )rt   r   r~   s     r   maybe_pop_nz&fix_extended_args.<locals>.maybe_pop_n7  s4    qA&*++s/?/??

 r    r   i rR   r      ra      rg   i  rp   r   r   N)r.   rK   r   r0   r{   rO   r   )r   rB  rZ   addedr~   s       @r   fix_extended_argsrF  3  s_    "F
 ;;#***DHXX$((X-NMM,^RPQMM,^RPQMM,^QOPXX$((V+NMM,^RPQMM,^QOPXX$((T/NMM,^QOPd! $ K#l++EA::LOLr    rh   rp   rk   rj   )BINARY_SUBSCRSTORE_SUBSCRUNPACK_SEQUENCE
STORE_ATTRr$  
COMPARE_OPrH   	BINARY_OPr%  rx   ry   c                     t         j                  dk\  r7dt        j                  t        j
                  | j                     d      dz   z  S y)NrQ   rp   r   r   )rX   rY   _PYOPCODE_CACHESgetrK   r/   r.   )rZ   s    r   r   r   e  s>    
7"$((DKK)@!DqHIIr    c                 T    d}| D ]!  }|j                   |k(  sJ |t        |      z  }# y Nr   r2   r   r   r2   rZ   s      r   check_offsetsrT  k  s3    F{{f$$$"4(( r    c                 @    d}| D ]  }||_         |t        |      z  } y rQ  rR  rS  s      r   update_offsetsrV  r  s(    F"4(( r    c            	      Z   t        t        t        t        |                   }g }|gt	        |       z   t        | d   | d         D cg c]  \  }}t        ||k7         c}}gz   D ])  }|j                  dj                  d |D                     + ddj                  |      z   S c c}}w )Nre    c              3   $   K   | ]  }|d  
 yw)03Nr,   ).0xs     r   	<genexpr>zdebug_bytes.<locals>.<genexpr>  s     6#Q!By#s   zbytes mismatch

)	rr   r   mapr   r   zipr)   r{   join)argsindexresultar   r0   s         r   debug_bytesrg  y  s    #c#tn%&EF	$t*T"XtBx9P Q9PAQ!V9P QRR 	 	chh6#667 	S 		& 111	 !Rs   B'c                    t        | d d      }| j                  |j                  k(  s%J t        | j                  |j                               | j                  |j                  k(  s%J t        | j                  |j                               y)z<Make sure our assembler produces same bytes as we start withc                      y r"   r,   )r]  ys     r   r
  zdebug_checks.<locals>.<lambda>  s    Dr    T)safeN)transform_code_objectco_coderg  	co_lnotab)r   dodes     r   debug_checksrp    se     '8tDD<<4<<'PT\\4<<)PP'>>T^^+X[-XX+r    c                 |    t        | d         D ]  \  }}||u s|c S  | dxx   |fz  cc<   t        | d         dz
  S )N	co_constsr   )r   r   )code_optionsr   r@   vs       r   get_const_indexru    sQ    ,{341
 !8H 5 #'|K()A--r    c                    
 t        |d         D ci c]  \  }}||
 }}}t        j                  dk  rM|J t        |d         D ci c]  \  }}||
 }}}t        |d   |d   z         D ci c]  \  }}||
 }}}nht        |      sJ i }t	        j
                         D ]  }	  ||      }|||<    |d   D ci c]  }|||   
 }}|d   |d   z   D ci c]  }|||   
 }}t        t                     D ]  

 fd}	 
   j                  dk(  r 
   j                  J  
   j                  t        usJ t        j                  dk\  r?| 
   j                     dz  t        t         
   j                        d	z  z    
   _
        | 
   j                      
   _
         
   j                  t         v r# |	       s| 
   j                      
   _
         
   j                  t"        v r% |	       s| 
   j                      
   _
        # 
   j                  t$        v r% |	       sA| 
   j                      
   _
        ] 
   j                  t&        v st 
   j                  t)        | 
   j                        }|d
k\  sJ | 
   _
         y c c}}w c c}}w c c}}w # t        $ r Y  w xY wc c}w c c}w )Nco_namesrQ   r;  r8  r:  c                  .        j                   t        uS r"   )r1   rE   )r@   r   s   r   should_compute_argz$fix_vars.<locals>.should_compute_arg  s    ?))==r    rH   r   rp   r   )r   rX   rY   callable	itertoolscount
IndexErrorrr   r   r/   r0   r1   rE   r   r)   r.   	HAS_LOCALHAS_NAMEHAS_FREE	HAS_CONSTru  )r   rs  varname_from_opargr   rM   namesvarnames	freenamesallnamesry  r@   s   `         @r   fix_varsr    s/   (1,z2J(KL(K93T3Y(KEL
'!!)))/8m9T/UV/U)#tD#I/UV ']+l=.II
	T #I 	 
 *+++??$C)#.!$ % 6B-5PQ5PTD(4.(5PQ %]3l=6QQ
Q (4. Q 	 
 3|$%	> ?!!]2?&&222?))===7*',\!_-C-C'D'Il1o112Q6'Q# ',LO,B,B&CQ#!_##y0!#&.|A/E/E&FQ#!_##x/!#&+LO,B,B&CQ#!_##x/!#&/Q0F0F&GQ#!_##y0 A""*%lLO4J4JKaxx&)Q#A &1 M W
  Q
s/   J3J93J?+KKK	KKc                     dg} | j                  d       | j                  g d       t        j                  dk\  r| j                  d       | j                  d       t        j                  dk\  r| j                  d       n| j                  d	       t        j                  dk\  r| j                  d
       | j                  ddg       | S )Nco_argcountco_posonlyargcount)
co_kwonlyargcount
co_nlocalsco_stacksizeco_flagsrm  rr  rw  r;  co_filenameco_namerQ   co_qualnameco_firstlinenori   co_linetablern  co_exceptiontabler:  r8  )r{   r|   rX   rY   )r  s    r   get_code_keysr    s     ?DKK$%KK	
 7"M"KK !
7"N#K 
7"'(KK	
 Kr    c                     t               }|D ci c]  }|t        | |       }}t        |d         |d   k(  sJ t        | |      }t	        |        |||       t        |||      d   S c c}w )Nr;  r  r   )r  rB   r   cleaned_instructionsr   clean_and_assemble_instructions)r   transformationsrk  r  krs  r   s          r   rl  rl    s}    ?D156AAwtQ''L6|M*+|L/IIII'd3L%L,/*<|LQOO 7s   A+r  rs  c                    t        |        t        |d         |d<   d }t        j                  dk\  r/t	        j
                  |D cg c]  }||   	 c} }|j                  }t        | ||       d}|r-t        |        t        |        t        t        |             }|r-t        |        t        | |d         \  }}t        j                  dk  r||d<   n||d	<   ||d
<   t        |       |d<   t        |      dhz
  t        |j!                               dhz
  k(  sJ t        j                  dk\  rt#        t%        |             |d<   | t	        j
                  |D cg c]  }||   	 c} fS c c}w c c}w )Nr;  r  rQ   )r  Tr  ri   rn  r  rm  r  r  r  )r   r   rX   rY   typesCodeType_varname_from_opargr  rV  r   r*   rF  r   r   r   r   r  r   r  )	r   r  rs  r  r  tmp_codedirtybytecoder   s	            r   r  r    s    %\2!$\-%@!AL
7">>T#BTLOT#BC%99\<<NOE
|$<(&|45	  <(l;K.LMHf
'!$*[!'-^$&L#5l#CL t9,--\5F5F5H1IM 2    7",D#L1-
() 4)H4a,q/4)HIII7 $C6 *Is   E,E1
c                 Z    | D ]&  }|j                   dk(  s||j                     |_        ( y )NKW_NAMES)r/   r0   r1   )r   constsrZ   s      r   populate_kw_names_argvalr  5  s(    ;;*$ *DK r    c                    t        t        t        t        j                  |                   }t        |       t        j                  dk\  r,t        || j                         t        | j                  |       t        |       t        |       |sLt        j                  dk  rt        |       n!t        |       t!        |       t#        |       t%        | |       |S )NrQ   )r   r`  rC   rK   get_instructionsrT  rX   rY   r  rr  r  r  r   r"  r'  r2  rV  r   r?  )r   rk  r   s      r   r  r  ;  s    /1E1Ed1KLML,
7" t~~>"4#9#9<H\"%g%#L1-<(|,t\*r    c                 *    |  dt        t               S )Nr   )r   _unique_id_counter)rM   s    r   	unique_idr  Q  s    V1T,-.//r    c                 *    d}| j                   |z  dkD  S )N    r   )r  )r   co_generators     r   is_generatorr  U  s    LMML(A--r    )r   )r   Nr"   )F)[r  dataclassesrK   r{  rX   r  typingr   r   r   r   r   r   r	   r
   bytecode_analysisr   r   r   r   	dataclassr   r   rC   rE   rO   r[   r^   rc   ru   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  r  r?  rF  rN  r   rT  rV  r+   rg  rp  haslocalr~  hasnamer  hasfreer  hasconstr  ru  r  r  rl  r  r  r  r|  r  r  r*   r  r,   r    r   <module>r     s     
  
  M M M  
 
 
2 J J J2
3?? 
{ 
  l483K 3
; 2) )0tK( 0:<d;.? <::k!2 :  !
49hSz4/0010 "FS T#Y 8s 8v   S T#Y  
hsm 
 


t$78 

T 

% D1D,E ,$':";  $'&4, '&3 '&5PUCV '&TtC4D/E s P 

,[ 
,T 
,k):  #1L&e &4CT &RG{#G	
GT 	$	% 	 &=43D = =6C43D C0Pd;&7 PD P${*; [@Q  d;&7  D  , tK7H T 6D$5 # H  c ))2# 2Y 	s{{s{{		.# 	.:*4, :*z$tCy $N	P 	P%J{#%J+/9%JDHcN%J
4enn,-%JP+d;.? & %Y__& 0s 0.u~~ .$ .r    