
    Vh                        d Z 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Zddlm	Z	m
Z
 ddlmZmZmZmZmZ ddlmZ ddlmZmZmZmZ dd	lmZ eej4                   G d
 d                    Zeej4                   G d d                    Zej:                  dk\  rdej8                  defdZn4ej:                  dk\  rdej8                  defdZndej8                  defdZ G d d      Zej:                  dk\  rd Z nej:                  dk\  rd Z nd Z dedddefdZ!defdZ"dndefdZ#defdZ$de%e   fd Z&d!eee%e   f   de%e   fd"Z'd!eee%e   f   de%e   fd#Z(de%e   fd$Z)de%e   fd%Z*defd&Z+defd'Z,de%e   fd(Z-	 dod)e.d*e.de/e%e.   ee.e.gdf   f   fd+Z0d, Z1d-e.de%e.   fd.Z2d/e.fd0Z3eej4                   G d1 d2                    Z4d-e.de%e.   fd3Z5d4e	e.   de.fd5Z6d6e%e4   ddfd7Z7d8e8de%e4   fd9Z9d6e%e4   de8fd:Z:d;e%e   d<e.de/e8e8f   fd=Z;d>e<e.ef   d?e.fd@Z=dpdAZ> e?ej                        ZAdBeddfdCZBd;e%e   dDe.fdEZCdF ZDdGe8d;e%e   fdHZEd;e%e   de%e4   fdIZFd6e%e   ddfdJZGd;e%e   ddfdKZHd;e%e   fdLZId;e%e   ddfdMZJdN ZKd;e%e   de%e   fdOZLd;e%e   ddfdPZMd;e%e   ddfdQZNdRdSdTdUZOd;e%e   ddfdVZPdWej                  d;e%e   ddfdXZRd;e%e   de.fdYZSde.fdZZTdpd[ZUdpd\ZVdeWfd]ZXd^ ZY e?ej                        Z[ e?ej                        Z] e?ej                        Z_ e?ej                        Zade.fd_Zbdqd;e%e   fd`Zcda Zdde%eW   fdbZedrdej                  fdcZfd;e%e   dde%eW   dee<eWef   de/e%e   ej                  f   fdfZgdg Zhdrde%e   fdhZidi Zjej                  drde
e   fdj       Zl ej                         ZndeWfdkZodWej                  depfdlZqdsdmZry)ta  
This module provides utilities for analyzing, transforming and manipulating Python bytecode.
It includes functionality for:
- Converting between different bytecode formats and versions
- Virtualizing jumps and managing jump targets
- Handling exception tables and their entries
- Managing instruction offsets and extended arguments
- Providing a clean API for bytecode modification and transformation
- Supporting Python version-specific bytecode features
- Generating bytecode from template functions

The module is designed to work across different Python versions (3.7+) and handles
version-specific bytecode differences transparently.
    N)IteratorSequence)AnyCallablecastOptionalUnion   )dataclass_slots   )get_indexofpropagate_line_numsremove_extra_line_numsstacksize_analysis)is_safe_constantc                   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    U/home/dcms/DCMS/lib/python3.12/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__7   so    JJ!''! &AEE!&qxx'& 

agg%& 

agg%	
r"   N)	__name__
__module____qualname____annotations__intboolstrr!   r&    r"   r    r   r   &   s8     	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Zee   ed<   defdZdefdZdefdZd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_entryargreprr   c                     t        |       S r$   idr   s    r    __hash__zInstruction.__hash__S   s    $xr"   c                 0    t        |       t        |      k(  S r$   r<   r   others     r    r&   zInstruction.__eq__V   s    $x2e9$$r"   c                 <    d| j                    d| j                   dS )NzInstruction(opname=z	, offset=r   )r1   r4   r   s    r    r   zInstruction.short_inst_reprY   s    $T[[M4;;-qIIr"   c                 H    |j                   | _         |j                  | _        y r$   )r5   r8   r@   s     r    copy_positionszInstruction.copy_positions\   s     ,,r"   )rA   r   r   N)r'   r(   r)   __doc__r+   r*   r-   r   r   r4   r5   r6   r,   r8   r   r9   r   r:   r>   r&   r   rD   r.   r"   r    r   r   A   s     /KK	#K FHSM !%K#% ND +/Ix(/&*FH]#*6:M823:!GXc]!# %t %J J)r"   r         ir   c           
          t        | j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                        S r$   )	r   r0   r1   r2   r3   r4   line_numberr6   r8   rI   s    r    convert_instructionrM   c   I    HHHHEEHHHHMMKK	
 		
r"   rG      c           
          t        | j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                        S r$   )	r   r0   r1   r2   r3   r4   r5   r6   r8   rL   s    r    rM   rM   q   rN   r"   c           
          t        | j                  | j                  | j                  | j                  | j
                  | j                  | j                  d       S r$   )r   r0   r1   r2   r3   r4   r5   r6   rL   s    r    rM   rM      sE    HHHHEEHHHHMM	
 		
r"   c                       e Zd ZdefdZy)_NotProvidedr   c                      y)NrT   r.   r   s    r    r!   z_NotProvided.__repr__   s    r"   N)r'   r(   r)   r-   r!   r.   r"   r    rT   rT      s    # r"   rT   rG      c                 
    | dv S )N)	LOAD_ATTRLOAD_GLOBALLOAD_SUPER_ATTRr.   names    r    inst_has_op_bitsr^      s    FFFr"   c                     | dk(  S )NrZ   r.   r\   s    r    r^   r^      s    }$$r"   c                      y)NFr.   r\   s    r    r^   r^      s    r"   )r2   r3   r   c                    t        |       r|t        d      |%d}n"|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`.

    Bits in the args of instructions LOAD_GLOBAL, LOAD_ATTR (3.12+), and LOAD_SUPER_ATTR
    modify the behavior of the instruction. In this case, we allow both `arg`
    and `argval` to be set. The value of `arg` here is expected to be the value of
    the op bits and the true value of `arg` will be computed during assembly.
    If `arg` is not set, the bits are assumed to be 0.
    Nz*target cannot be specified for instructionr   r   z@only one of arg, argval, and target can be not None/_NotProvidedz#instruction arg must be int or None)r0   r1   r2   r3   r   )r^   RuntimeErrorrT   
isinstancer+   r   disopmap)r]   r2   r3   r   cnts        r    create_instructionrg      s    ( KLL;C$6#=>&PTBTU7R  z#s3@AAyytVF r"   c                 J    t         j                  dk\  rdnd}t        ||       S )NrO   JUMP_FORWARDJUMP_ABSOLUTEr   sysversion_inforg   )r   insts     r    create_jump_absoluterp      s#     --8>oDd622r"   c                 J    |rt        |       s
J d|         t        d|       S )z
    In general we should only create `LOAD_CONST` for immutable objects, but
    sometimes it's convenient _and safe_ for Dynamo create `LOAD_CONST` for
    mutable objects. In such cases, use `checked=False`.
    zunsafe constant 
LOAD_CONSTr3   )r   rg   )valcheckeds     r    create_load_constrv      s/     $>(8&>>$l377r"   c                  X    t         j                  dk\  rt        dd      S t        d      S )NrO   COPYr   r2   DUP_TOPrl   r.   r"   r    create_dup_topr{      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	      | 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   rO   SWAPry   rG   
      zrotate z  not supported for Python < 3.10   ROT_)TWOTHREEFOURr
   ROT_N)rm   rn   rangerg   AttributeError)nrI   s     r    create_rot_nr      s     	Av	
7" <AAr?Ka"6q1KK '!a1fwqc)IJKKAv"6,DQU,K#KLMMwA.// Ls   Binst_or_instsc                 `   t        | t              r| gn| fd}fd}t        j                  dk\  r;t	        d   j
                        r |d      s
 |d       S t        d      gz   S t        j                  dk\  r_t	        d   j
                        r |d      s
 |d       S d   j
                  dk(  r |d      s
 |d       S t        d      gz   S t        j                  d	k\  r9t	        d   j
                        r |d      s
 |d       S t        d      gz   S )
a  
    Appends or prepends a PUSH_NULL instruction to `inst_or_insts`,
    depending on Python version. Used when you know that
    `inst_or_insts` generates a callable that will be called.

    NOTE: Assumes `inst_or_insts` is a single instruction or sequence of
    instructions that pushes exactly 1 object to the stack that is to
    be called. It is important that you include ALL instructions that
    construct the callable - not just the first instruction/a prefix.

    Will attempt to use the NULL push bit for instructions
    with such bits (LOAD_GLOBAL 3.11+, LOAD_ATTR 3.12+, LOAD_SUPER_ATTR).
    In this case, instructions WILL be modified.
    c                 P    |    j                   J |    j                   dz  dk(  S Nr   ry   idxinstss    r    inst_has_bit_setz'add_push_null.<locals>.inst_has_bit_set  s/    Sz~~)))Sz~~!Q&&r"   c                 X    |    j                   J |    xj                   dz  c_         y r   ry   r   s    r    set_inst_bitz#add_push_null.<locals>.set_inst_bit  s)    Sz~~)))c
!r"   rF   r}   	PUSH_NULLrV   r   rZ   rO   )rc   r   rm   rn   r^   r1   rg   )r   r   r   r   s      @r    add_push_nullr      sI   " --' 7"E"I,,-6Fr6J & L# /<==E" L! 
		W	$ E"I,,-6Fr6J L 1X__-6Fq6IO L (45=E L 
		W	$E!HOO,5Ea5HO L (45=ELr"   c                    t        | t              r| g}n| }t        j                  dk  r|S t        j                  dk\  rdnd}||   j                  dk(  r@||   j
                  J ||   j
                  dz  dk(  r||   xj
                  dz  c_        |S t        j                  dk\  r|t        d      gz   }|S t        d      g|z   }|S )zLike add_push_null, but the low bit of LOAD_ATTR/LOAD_SUPER_ATTR
    is not set, due to an expected CALL_FUNCTION_EX instruction.
    rO   rF   r}   r   rZ   r   r   )rc   r   rm   rn   r1   r2   rg   )r   r   r   s      r    add_push_null_call_function_exr   3  s     --
'!  G+"CSzM)Sz~~))):>>A"#JNNaNL
7"+K899 L $K01E9Lr"   c                    t         j                  dk\  rg }|rQ|j                  t        d             t         j                  dk\  r| dz   n| dz   }|j	                  t        |             t         j                  dk  r|j                  t        d|              |j                  t        d	|              |S t        d
|       gS )aS  
    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 be True if no NULL is pushed for the callable.
    Conversely, `push_null` should be False if a NULL was pushed for the callable.
    Prefer using `push_null=False` when possible since we will not need to rotate
    NULL to the right place, which is less efficient.

    Generally, you should codegen a function by using `add_push_null` then
    `create_call_function` with `push_null=False`.

    Example of when to set push_null False:

    insts = [
        create_instruction("LOAD_GLOBAL", argval="torch"),
        create_instruction("LOAD_ATTR", argval="nn"),
        create_instruction("LOAD_ATTR", argval="functional"),
        create_instruction("LOAD_ATTR", argval="relu"),
    ]
    insts = add_push_null(insts)
    insts.append(create_instruction("LOAD_FAST", argval="x"))
    insts.extend(create_call_function(1, False))

    Example of when to set push_null True:

    insts = [create_instruction("LOAD_FAST", x)]
    for should_wrap, wrapper_name in wrappers:
        if should_wrap:
            insts.extend([
                create_instruction("LOAD_GLOBAL", argval="wrapper1"),
                create_instruction("SWAP", arg=2),
                *create_call_function(1, True),
            )
    rO   r   rF   r   r
   rV   PRECALLry   CALLCALL_FUNCTION)rm   rn   appendrg   extendr   )nargs	push_nulloutputrotss       r    create_call_functionr   P  s    R 7"MM,[9: # 0 0G ;519DMM,t,-g%MM,YEBC(U;<E:;;r"   c                     t         j                  dk\  rt        d|       gS t         j                  dk\  rt        d|       t        d|       gS t        d|       gS )NrV   r   ry   rO   r   CALL_METHODrl   )r   s    r    create_call_methodr     s^    
7""6u566
7"ye4v51
 	
 }%899r"   c                 ^    t         j                  dk\  rt        dd|       S t        d|       S )NrV   rY   r   )r2   r3   LOAD_METHODrs   rl   r\   s    r    create_load_methodr     s,    
7"!+1TBBmD99r"   c                 J    t         j                  dk\  rdnd}t        ||       S )NrO   BEFORE_WITH
SETUP_WITHrk   rl   )r   r1   s     r    create_setup_withr     s#    !..'9]|FfV44r"   c                    t         j                  dk\  rt        d|       gS | dk(  rg S 	 t        d| dz
        t        d      t        dd	      t        d
      t        d      t        d      t        d      t        dd	      t        d      t        d      t        d      gt	        d      t        d      t        d| dz
        S )NrO   r~   ry   r   
BUILD_LISTrz   rr   r}   rs   BINARY_SUBSCR	ROT_THREESTORE_SUBSCRreverser   POP_TOPUNPACK_SEQUENCE)rm   rn   rg   r   r   )r   s    r    create_swapr     s    
7""6q122Av	& 	<QU39%<3?+;'9%;'<3>*9%9% 
A	 	9% 	,!a%8 r"   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
    r   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"   rm   rn   )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
    r   rO   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"   r   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   ?      r}   @   )r   )r   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
    rO   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      r.      r      )r   
end_lineno
col_offsetend_col_offsetr   r   r   )deltasizeother_varintsr   r   r8   s       r    r   z5linetable_311_writer.<locals>.update.<locals>._update   s   t=q= = = .0M$$0((4((4,,8  !4q!89 ((9+;+;;((1,,,q0!   !4q!89qy &Q!+!]512" 3  q!123r"   r   r   )r   )r8   	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"   )r8   r7   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   M  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|j                          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   r   r   len)r   r   rI   s      r    encode_exception_table_varintr   W  s     6M6	
RA!GA
a%	R	a a% IIK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   rt   s      r    decode_exception_table_varintr   g  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)r   r   r   r   )r   rI   s     r    check_exception_tabler   t  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
    r
   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.
    r
   r      r   )r   r   r   r   r   r   r   bytes)r   r   entryfirst_entryr   r   s         r    assemble_exception_tabler    s    
 	A 43EKK14DEA& 	U[[(1,	.v{;<	.u||q/@ABkkQ%++-	.r234 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()rO   r   EXTENDED_ARGr   r   r
   rG   r
   r   )r   r   r   )rm   rn   r   	enumerater1   r8   instruction_sizer2   r   r0   r   r   r   r5   r   r   )r  r  coder   update_linenonum_extrI   ro   r   jr2   _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  	3D+d..D	:((-aCKKcDj12		3 w&D	N;f%%r"   offset_to_instr4   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   r
   r   r   Nr0   rd   r  )r  r4   r   s      r    _get_instruction_by_offsetr    sD      .&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)r4   r0   rd   hasjabshasjrelr  r3   r   )r  ro   jump_targetss      r    virtualize_jumpsr    sf    2>?$DKK%?L? P;;#++%)C4\4;;ODKP @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 )NrO   z+Cannot flip jump direction in Python < 3.11FORWARDBACKWARDz-Instruction is not a forward or backward jump)
rm   rn   rb   r1   replacer   rd   re   r0   
_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"   r   c                     | |   }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   r4   s       r    _get_instruction_frontr!    s]    
 #F &=\#,7>>#BRBRR!#,/FM
 Mr"   c           	         t        t        j                        j                  t        t        j                              }| D ]  }|j
                  |v s|j
                  t        j                  vs/|j                  j                  |j                  k  r9t        j                  dk  rt        d      d|j                  v st        |       t        j                  dk\  sd|j                  v st        |        t        |        t        |       }| D ]1  }|j
                  |v st        | ||j                           }|j
                  t        j                  v rat        j                  dk  r|j                  |_        nt        j                  dk  rt#        |j                  dz        |_        not        d      t%        t#        |j                  |j                  z
  t'        |      z
              |_        t        j                  dk\  r|xj                   dz  c_        |j                  |_        d|j                   |_        4 y	)
zJFill in args for virtualized jump target after instructions may have movedrO   z*Got negative jump offset for Python < 3.11r  r  r   r
   z+Python 3.11+ should not have absolute jumpszto N)setrd   r  unionr  r0   r   r4   rm   rn   rb   r1   r  update_offsetsr   r!  r2   r+   absr	  r3   r:   )r  jumpsro   indexofr   s        r    devirtualize_jumpsr)    s   ""3s{{#34E  2;;%{{#++-;;%%3'''1*+WXX DKK/+D1 ''72zT[[7P+D12 < ,'G  1;;%+L'$++:NOF{{ckk)##g-%}}DH%%/  #6==1#45DH&'TUU 36Ft6LLM ##w.HHNH --DK 0DL+1r"   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>.step=  s    &E W-'.2IUYY2V!# W-'.2IUYY2V "A%%% !!34J/*>5;;G*>:F*>5<<HJ *$$r"   N)r   r   r+   r4   sortedkeysr   r   r   copyr9   r   )r*  r  exn_tabro   r2  r   r.  r/  r0  r  r1  s          @@@@r    virtualize_exception_tabler7  4  s    #M2G?KLtd3,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_entriesr
   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   r
   N)popr   r   r   )r?  exn_dictr6  	key_stacknextis    r    rA  z$compute_exception_table.<locals>.pop  se    
 --/CA'CFE(:CFSXc]S A
	  r"   r}   r   )r   r9   r!  r   r4   r   r+   r   r	  r   r   r   r3  r4  r   r   r   r   )r  r(  ro   r   r   r   r?  rt   keys_sortedrA  leftrB  r6  rC  rD  s              @@@@r    compute_exception_tablerG  ]  s?    >@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.
    r}   r   r   N)r   r   rA  r   )r   r(  entry_stackr   r?  s        r    !check_inst_exn_tab_entries_nestedrJ    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   r9   r   r   r3  r4  rJ  r   r5  )r  r(  entriesro   r?  sorted_entriesr   rI   s           r     propagate_inst_exn_table_entriesrO    s   
 ,'G=?G .**001**../C g~))WS\999--GCL. !'w||~;R SN  &ng>   =wu{{+WUYY-?!-CD 	=A,0IIe,<LO)	==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.
    rO   N)r   r#  r  r9   rm   rn   r=   addr   r   r   )r  r(  exn_tab_entry_setrI   ro   r   s         r     check_inst_exn_tab_entries_validrS    s     ,'G\* 	C4##w...d(()1BBBB!!"T%7%7"89&&E;;')))99'''<<7***5;;'1B		0BBBBBB	Cr"   c                 p    | D cg c]"  }|j                   t        j                  k7  s!|$ c}| d d  y c c}w r$   r  )r  rI   s     r    strip_extended_argsrU    s*    ".OQ!((c>N>N2NqOLOOs   "33c                    | j                   r:| j                   j                  | u r"t        |      dkD  r|d   | j                   _        |dd  D ]7  }t        j                  | j                         |_         | j                  |_        9 |d   j
                  | _        |d   j                  | _        |d   j                  | _        |d   j                  | _        |d   j                  | _	        | g|dd  z   S )Nr   r}   r   )
r9   r   r   r5  r8   r0   r1   r2   r3   r   )old_inst	new_instsro   s      r    overwrite_instructionrY    s     	""&&(2	NQ%.r]"!" ,!YYx'='=>!++,  l))HOl))HOQ<##HLl))HOl))HO:	!"%%r"   c                     t         j                  dk  sJ 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 itrO   rY   r   )r   r   )rm   rn   r1   rd   re   r0   )r  rewritesro   s      r    remove_load_call_methodr\    sg    g%%%*?KH 1;;(""4;;/DK))DKK0DK1 r"   c                    g }| D ]  }d|j                   v rt        dt        d|j                   v             }|j                  |_        t
        j                  dk  r(t        d|j                   v rdnd|j                  	      }nt        d
|j                  	      }t        dd       ||g}|j                  t        ||             |j                  |        || d d  y )N_NONEIS_OPNOTry   rV   r  POP_JUMP_FORWARD_IF_TRUEPOP_JUMP_BACKWARD_IF_TRUErk   POP_JUMP_IF_TRUErr   rs   )r1   rg   r+   r2   r3   rm   rn   r   r   rY  r   )r  rX  ro   is_opjump_opreplace_instss         r    remove_jump_if_nonerg    s    I #dkk!&wC8L4MNE 99EL'), %3 38;; --?T #<=M
 24GHT"3#4  LOr"   c                    g }| D ]  }|j                  |       |j                  dv s#t        |j                  j                  dd            }|j                  r)|j                  j
                  |u r||j                  _        t        j                  |j                        |_        |j                  |_        t        j                  d   |_
        d|_        d|_        d|_        |j                  |        || d d  y )N)BINARY_SLICESTORE_SLICESLICESUBSCRBUILD_SLICEr
   )r   r1   rg   r  r9   r   r5  r8   rd   re   r0   r2   r3   )r  rX  ro   subscr_insts       r    remove_binary_store_slicero  5  s    I *;;99,T[[-@-@(-STK!!d&8&8&<&<&D)4""&(,		$2D2D(EK%$(NNK!))M2DK'DKDHDK[)*  LOr"   )	LOAD_FASTrp  )
STORE_FASTrq  )rq  rp  )LOAD_FAST_LOAD_FASTSTORE_FAST_STORE_FASTSTORE_FAST_LOAD_FASTc                    g }| D ]  }|j                   t        v r[t        |j                      \  }}|j                  \  }}t        ||      t        ||      g}|j	                  t        ||             p|j                  |        || d d  y )Nrs   )r1   FUSED_INSTSr3   rg   r   rY  r   )r  rX  ro   inst0inst1argval0argval1rf  s           r    remove_fused_load_storer{  P  s    I #;;+%&t{{3LE5#{{GW #59"59M 24GHT"#  LOr"   r
  c                 x   | j                   xs d| j                  xs dz   }t        | j                        syg }t	        |      D ]m  \  }}|j                  |       |j                  dk(  s(|j                  dk(  s8||dz      }|j                  dk(  sPt        j                  dk\  r|j                  dk(  sYt        j                  d	k\  r"t        j                  dk  r|j                  d
k(  s$t        j                  d	k  s|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X||dz      }d|_        d|_        p ||dd y)z3convert super() with no args into explicit arg formr.   NrZ   superr   r   rV   r   rO   r   r   	__class__
LOAD_DEREFrs   rp  r
   )co_cellvarsco_freevarsr   co_varnamesr  r   r1   r3   r2   rm   rn   rg   )	r
  r  cell_and_freer   r   ro   rD  	first_var	call_insts	            r    explicit_superr  a  s   %%+0@0@0FBGMt F|, )	Td;;-'DKK7,B q)EyyA~!!W,1G$$/((72	1$$w.5<<?3R"m3330kRS ,,Q/	-MM"4\)"TUMM"4["ST	 <<9, ,S1W 5I$%IM'(I$5)8 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 )Nr}   )r   r0   rd   r  rA  )r   r  r   s     r    maybe_pop_nz&fix_extended_args.<locals>.maybe_pop_n  s9    q 	A&*++s/?/??

	r"   r   i rG   r     ry      r   i  r
   r   r   N)r0   rd   r  r2   r   rg   r   )r  r  ro   addedr   s       @r    fix_extended_argsr    sd    "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"   c                     dd l }t        j                  dk\  r=d|j                  j                  j
                  j                  | j                     dz   z  S y)Nr   rO   r
   r   )torchrm   rn   _C_dynamo
eval_framepy_opcode_cachesr0   )ro   r  s     r    r	  r	    sF    
7"EHH$$//@@MPQQRRr"   c                 T    d}| D ]!  }|j                   |k(  sJ |t        |      z  }# y Nr   r4   r	  r  r4   ro   s      r    check_offsetsr    s8    F ){{f$$$"4(()r"   c                 @    d}| D ]  }||_         |t        |      z  } y r  r  r  s      r    r%  r%    s-    F )"4(()r"   c            	      N   t        t        t        t        |                   }|gt	        |       z   t        | d   | d         D cg c]  \  }}t        ||k7         c}}gz   D cg c]  }dj                  d |D               }}ddj                  |      z   S c c}}w c c}w )Nr}    c              3   $   K   | ]  }|d  
 yw)03Nr.   ).0xs     r    	<genexpr>zdebug_bytes.<locals>.<genexpr>  s     (qAb6(s   zbytes mismatch

)r   r   mapr   listzipr+   join)argsindexar   r2   results         r    debug_bytesr    s    #c#tn%&E 7
t*$'R$r($;<DAqCQK<
=> 	(C((F  		& 111 =	s   B'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    r"   T)safeN)transform_code_objectco_coder  	co_lnotab)r
  dodes     r    debug_checksr    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_optionsrt   rI   vs       r    get_const_indexr    sV    ,{34 1
 !8H #'|K()A--r"   c                 &
   	
 t        d         D ci c]  \  }}||
 c}}dt        ffd}t        j                  dk  rO|J t        d         D ci c]  \  }}||
 c}}t        d   d   z         D ci c]  \  }}||
 c}}	njt	        |      sJ i }t        j                         D ]  }	  ||      }|||<    d   D ci c]  }|||   
 c}d   d   z   D ci c]  }|||   
 c}	t        t                     D ]  

 fd} 
   j                  d	k(  r 
   j                  t        usJ t        j                  dk\  rS 
   j                  J  | 
   j                        d
z  t        t         
   j                        dz  z    
   _         | 
   j                         
   _         
   j                  dk(  r 
   j                  t        usJ t        j                  dk\  rT 
   j                  J  | 
   j                        d
z  t        t         
   j                        dz  z    
   _        G | 
   j                         
   _        f 
   j                  dk(  rn 
   j                  J  
   j                  t        usJ  | 
   j                        dz  t        t         
   j                        dz  z   dz    
   _         
   j                  t        v rt        j                  dk\  sJ t!         
   j                  t"              sJ t         
   j                        dk(  sJ t#        	fd 
   j                  D              }|d   dz  |d
   dz  z    
   _         
   j$                  t&        v re |       st        j                  dk\  r- 
   j                  vr	 
   j                      
   _         
   j                      
   _         
   j$                  t(        v r( |       s! | 
   j                         
   _        @ 
   j$                  t*        v r% |       s^	 
   j                      
   _        z 
   j$                  t,        v s 
   j                  t/         
   j                        }|dk\  sJ | 
   _         y c c}}w c c}}w c c}}w # t        $ r Y  7w xY wc c}w c c}w )Nco_namesr   c                     	 |    }|S # t         $ r= t              x}| <   g d   | d<   t        d         t              k(  sJ Y |S w xY w)Nr  )KeyErrorr   )r]   r   r  namess     r    get_name_indexz fix_vars.<locals>.get_name_index  su    	?+C 
  	? #E
*C%+'Hj)A'H4'HL$|J/0CJ>>>
	?s   
 AAArO   r  r  r  c                  .        j                   t        uS r$   )r3   rT   )rI   r  s   r    should_compute_argz$fix_vars.<locals>.should_compute_arg
  s    ?))==r"   rZ   r   r
   rY   rV   r[   rF   c              3   :   K   | ]  }|v r|   n|     y wr$   r.   )r  r]   	freenamesvarnamess     r    r  zfix_vars.<locals>.<genexpr>/  s-       #'("2	$Gs   r   r      )r  r+   rm   rn   callable	itertoolscount
IndexErrorr   r   r1   r3   rT   r2   r   rv  rc   tupler0   	HAS_LOCALHAS_NAMEHAS_FREE	HAS_CONSTr  )r  r  varname_from_opargr   r]   r  allnamesr  	arg_tupler  rI   r  r  s   ``       @@@@r    fix_varsr    s=   (1,z2J(KL93T3YLE  '!!)))/8m9T/UV)#tD#IV ']+l=.II
T #I
	 *+++??$ 	C)#.!$	 6B-5PQTD(4.(Q %]3l=6QQ
 (4. 
	 3|$% C*	> ?!!]2?))===7*#A**666'5l1o6L6L'MQR'Rl1o112Q6'Q# '5\!_5K5K&LQ#!_##{2?))===7*#A**666'5l1o6L6L'MQR'Rl1o112Q6'Q# '5\!_5K5K&LQ#!_##'88?&&222?))===  Q 6 671<\!_001A57 O
 !_##{2##w...l1o44e<<<|A--.!333 (O22 I $-Q<1#419J"KLO!_##y0!#$$/$Q..h> +4LO4J4J*KLO'*2<?3I3I*JLO'!_##x/!#&4\!_5K5K&LQ#!_##x/!#&/Q0F0F&GQ#!_##y0 A""*%lLO4J4JKaxx&)Q#GC*G M W
  Q
s/   S&S,S2?S8T	2T8	TTc                     | D ]t  }|j                   t        us|j                  t        v s7|j                  t        v s%|j                  t
        v s|j                  t        v s_|j                  dvsnd |_        v y )N)rZ   rY   r[   )	r3   rT   r0   r  r  r  r  r1   r2   )r  ro   s     r    clear_instruction_argsr  N  sc      KK|+y(;;(*;;(*;;)+#RRDHr"   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_flagsr  r  r  r  co_filenameco_namerO   co_qualnameco_firstlinenor   co_linetabler  co_exceptiontabler  r  )r   r   rm   rn   )r4  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  getattrr   cleaned_instructionsr   clean_and_assemble_instructions)r
  transformationsr  r4  kr  r  s          r    r  r    s|    ?D156AAwtQ''6L6|M*+|L/IIII'd3L%L,/*<|LQOO 7s   A+r4  r  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  rO   )r  Tr  r   r  r  r  r  r  r  )rS  r   rm   rn   typesCodeType_varname_from_opargr  r%  r)  r,   r  r   r  r   r#  r4  r  rG  )	r  r4  r  r  r  tmp_codedirtybytecoder   s	            r    r  r    s    %\2!$\-%@!AL
7">>T#BLO#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)Ha,q/)HIII9 $C8 *Is   E,E1
c                 Z    | D ]&  }|j                   dk(  s||j                     |_        ( y )NKW_NAMES)r1   r2   r3   )r  constsro   s      r    populate_kw_names_argvalr    s-     +;;*$ *DK+r"   c                 0    t        | |      }t        |      S r$   )_cached_cleaned_instructions_clone_instructionsr
  r  r  s      r    r  r    s    /d;L |,,r"   c                 V   | D cg c]  }t        |j                  |j                  |j                  |j                  |j
                  |j                  |j                  |j                  |j                  |j                  |j                         }}t        t        | |            }d |d <   |D ]t  }||j                     |_	        |j                  x}s&t        ||j                     ||j                      ||j                     |j"                  |j$                        |_
        v |S c c}w r$   )r   r0   r1   r2   r3   r4   r5   r6   r8   r   r9   r:   dictr  r   r   r   r   r   )r  rI   copiedremapr   s        r    r  r    s
   "   	HHHHEEHHHHMMKKHHOOII	
F " \6*+EE$K 	?OO#5#4ekk"eii ell#AO	 M?s   B
D&c                    t        t        t        t        j                  |                   }t        |       t        j                  dk\  r,t        || j                         t        | j                  |       t        |       t        |       |st        j                  dk  rt        |       t        j                  dk  rt        | |       t        j                  dk\  rGt!        |       t        j                  dk\  rt#        |       t        j                  dk\  rt%        |       t        j                  dk\  rt'        |       t)        |       |S )NrO   rV   rF   )r  r  rM   rd   get_instructionsr  rm   rn   r  r  r7  r  r  rU  r\  r  rg  ro  r{  r%  r)  r  s      r    r  r    s    /1E1Ed1KLML,
7" t~~>"4#9#9<H\"%g%#L1g%4.w&-7*),77*'5
7"|$<(r"   c                 *    |  dt        t               S )Nr  )r   _unique_id_counterr\   s    r    	unique_idr     s    V1T,-.//r"   c                 *    d}| j                   |z  dkD  S )N    r   )r  )r
  co_generators     r    is_generatorr    s    LMML(A--r"   c                    t        | j                        }t        |       |r,t        |      D ]  \  }}|j                  dk(  s||dz   d } n |D ]/  }d|_        |s|j                  |v s||j                     |_        1 |rt        j                  dk\  rvg }|D ]m  }|j                  dk(  rKt        j                  d   |_        d|_        |j                  |       |j                  t        d             ]|j                  |       o |}g }|D ]#  }|j                  dk(  s|j                  |       % t        |      dk(  r|d   |d	   u r|j                  d	       |S t        |      dkD  r|d	   |d	   u rOd
|d	   _        t        j                  d
   |d	   _        d|d	   _        t"        |d	   _        |j                  d	       n|j                  t        d
             |D ]e  }t%        |d	         }	|	j                  |_        |	j                  |_        |	j                   |_        |	j                  |_        |	j&                  |_        g |S )a  Generates bytecode from a template function `fn` for use in
    dynamo bytecode generation.

    For example, we can generate Python-version-independent bytecode
    for looping through a dictionary and copying the values to a new dictionary.

    def template(d1, d2):
        for k, v in d1.items():
            d2[k] = v


    or a try block:

    def template():
        try:
            dummy1
        except:
            dummy2
            raise
        dummy3

    Args:
        fn: a function template to generate bytecode from
        varname_map: a mapping of `fn`'s varnames to new names. This
            map will be applied to the generated bytecode's varnames.
            For example, local variables in `fn` can be replaced with
            new names that are generated by `OutputGraph.new_var`.
        noreturn: remove all RETURN_* bytecodes and replace them with a jump
            to the end of the bytecode. NOTE: any items pushed to the stack
            for return WILL remain on the stack! Append a POP_TOP if you don't want
            that item to be present.
        noprefix: remove prefix bytecodes (all bytecode before the first RESUME, inclusive).
    RESUMEr   NrV   RETURN_CONSTrr   RETURN_VALUEr   r}   NOP)r  __code__r  r  r1   r5   r3   rm   rn   rd   re   r0   r   rg   r   rA  r2   rT   rp   r   )
fnvarname_mapnoreturnnoprefixr   rI   ro   rX  returns	jump_insts
             r    bytecode_from_templater    sE   D !-E5! ' 	GAt{{h&a!eg	
  3  4;;+5%dkk2DK3 w&I +;;.0"%))L"9DK".DK$$T*$$%7%GH$$T*+ E 	%D{{n,t$	% w<1uRy!8IIbM0 L/ \A RyGBK'#(b	 #&99U#3b	  $b	#/b	 B/67   / 1r;	'..'..$=='..'../ Lr"   )T)r   )r   Nr$   )F)NTT)srE   r5  dataclassesrd   	functoolsr  rm   r  collections.abcr   r   typingr   r   r   r   r	   utils._backport_slotsr   bytecode_analysisr   r   r   r   utilsr   	dataclassr   r   rn   rM   rT   r^   rg   rp   rv   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)  r7  rG  rJ  rO  rS  rU  rY  r\  rg  ro  rv  r{  r  r  r  r	  r  r%  r-   r  r  haslocalr  hasnamer  hasfreer  hasconstr  r  r  r  r  r  r  r  r  r  	lru_cacher  r  r  r   r,   r  r  r.   r"   r    <module>r     sw     
   
  . 7 7 3  $ 
 
  
2 ) )  )< w

s 

; 

 	 

s 

; 



s 

; 

 
 wG 	 %

 l4##N3K 3
8K 8) )0tK( 067d;&7787	+7td;&778	+:4<d;.? 4<n:k!2 :: :5 5
'd;' 'V  !
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):  -1`&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  > D,=  $  * 697 ${*;    "# #tK7H #T #LD$5 # Bc ))	2# 	2Y 	s{{s{{		.# 	.h*4, h*V"$tCy $N	P 	P&J{#&J+/9&JDHcN&J
4enn,-&JR+-d;.? -"J h{6K  2 %Y__& 0s 0.u~~ .$ .
`r"   