
    sh                     ~   S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKJ	r	J
r
  SSKJrJrJrJrJr  SSKJr  SSKJrJrJrJr  SS	KJr  \\R4                   " S
 S5      5       5       r\\R4                   " S S5      5       5       r\R:                  S:  a  S\R8                  S\4S jrO7\R:                  S:  a  S\R8                  S\4S jrOS\R8                  S\4S jr " S S5      r\R:                  S:  a  S r O\R:                  S:  a  S r OS r S\SS.S\4S jjr!S\4S jr"SnS\4S jjr#S\4S jr$S\%\   4S  jr&S!\\\%\   4   S\%\   4S" jr'S!\\\%\   4   S\%\   4S# jr(S\%\   4S$ jr)S\%\   4S% jr*S\4S& jr+S\4S' jr,S\%\   4S( jr- SoS)\.S*\.S\/\%\.   \\.\./S4   4   4S+ jjr0S, r1S-\.S\%\.   4S. jr2S/\.4S0 jr3\\R4                   " S1 S25      5       5       r4S-\.S\%\.   4S3 jr5S4\	\.   S\.4S5 jr6S6\%\4   SS4S7 jr7S8\8S\%\4   4S9 jr9S6\%\4   S\84S: jr:S;\%\   S<\.S\/\8\84   4S= jr;S>\<\.\4   S?\.4S@ jr=SpSA jr>\?" \R                  5      rASB\SS4SC jrBS;\%\   SD\.4SE jrCSF rDSG\8S;\%\   4SH jrES;\%\   S\%\4   4SI jrFS6\%\   SS4SJ jrGS;\%\   SS4SK jrHS;\%\   4SL jrIS;\%\   SS4SM jrJSN rKS;\%\   S\%\   4SO jrLS;\%\   SS4SP jrMS;\%\   SS4SQ jrNSRSSSTSU.rOS;\%\   SS4SV jrPSW\R                  S;\%\   SS4SX jrRS;\%\   S\.4SY jrSS\.4SZ jrTSpS[ jrUSpS\ jrVS\W4S] jrXS^ rY\?" \R                  5      r[\?" \R                  5      r]\?" \R                  5      r_\?" \R                  5      raS\.4S_ jrbSqS;\%\   4S` jjrcSa rdS\%\W   4Sb jreSrS\R                  4Sc jjrfS;\%\   Sd\%\W   Se\<\W\4   S\/\%\   \R                  4   4Sf jrgSg rhSrS\%\   4Sh jjriSi rj\R                  SrS\
\   4Sj jj5       rl\R                  " 5       rnS\W4Sk jroSW\R                  S\p4Sl jrqSsSm jrrg)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                   d    \ rS rSr% S\S'   S\S'   S\S'   \\S'   \\S'   S\4S	 jrS\4S
 jr	Sr
g)InstructionExnTabEntry&   Instructionstartendtargetdepthlastireturnc                     SU R                   R                  5        SU R                  R                  5        SU R                  R                  5        SU R                   SU R
                   S3$ )NzInstructionExnTabEntry(start=z, end=z	, target=z, depth=z, lasti=))r   short_inst_reprr   r   r   r   selfs    y/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/_dynamo/bytecode_transformation.py__repr__InstructionExnTabEntry.__repr__/   sl    +DJJ,F,F,H+I J88++-. /kk1134 5ZZLA7	
    c                 .   U R                   UR                   L =(       aw    U R                  UR                  L =(       aX    U R                  UR                  L =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ N)r   r   r   r   r   )r    os     r!   __eq__InstructionExnTabEntry.__eq__7   so    JJ!''! &AEE!&qxx'& 

agg%& 

agg%	
r$    N)__name__
__module____qualname____firstlineno____annotations__intboolstrr"   r(   __static_attributes__r*   r$   r!   r   r   &   s8     	JK
# 

4 
r$   r   c                      \ rS rSr% Sr\\S'   \\S'   \\   \S'   \	\S'   Sr
\\   \S'   Sr\\   \S	'   S
r\\S'   Sr\S   \S'   Sr\S    \S'   Sr\\   \S'   Sr\\   \S'   S\4S jrS\4S jrS\4S jrSS jrSrg)r   A   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                     [        U 5      $ r&   idr   s    r!   __hash__Instruction.__hash__S   s    $xr$   c                 0    [        U 5      [        U5      :H  $ r&   rB   r    others     r!   r(   Instruction.__eq__V   s    $x2e9$$r$   c                 <    SU R                    SU R                   S3$ )NzInstruction(opname=z	, offset=r   )r7   r:   r   s    r!   r   Instruction.short_inst_reprY   s    $T[[M4;;-qIIr$   c                 H    UR                   U l         UR                  U l        g r&   )r;   r>   rG   s     r!   copy_positionsInstruction.copy_positions\   s     ,,r$   )r>   r;   )rH   r   r   N)r+   r,   r-   r.   __doc__r0   r/   r2   r   r   r:   r;   r<   r1   r>   r   r?   r   r@   rD   r(   r   rM   r3   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           
          [        U R                  U R                  U R                  U R                  U R
                  U R                  U R                  U R                  5      $ r&   )	r   r6   r7   r8   r9   r:   line_numberr<   r>   rS   s    r!   convert_instructionrW   c   I    HHHHEEHHHHMMKK	
 		
r$   rQ      c           
          [        U R                  U R                  U R                  U R                  U R
                  U R                  U R                  U R                  5      $ r&   )	r   r6   r7   r8   r9   r:   r;   r<   r>   rV   s    r!   rW   rW   q   rX   r$   c           
          [        U R                  U R                  U R                  U R                  U R
                  U R                  U R                  S 5      $ r&   )r   r6   r7   r8   r9   r:   r;   r<   rV   s    r!   rW   rW      sE    HHHHEEHHHHMM	
 		
r$   c                   "    \ rS rSrS\4S jrSrg)_NotProvided   r   c                     g)Nr^   r*   r   s    r!   r"   _NotProvided.__repr__   s    r$   r*   N)r+   r,   r-   r.   r2   r"   r3   r*   r$   r!   r^   r^      s    # r$   r^   rQ      c                     U S;   $ )N)	LOAD_ATTRLOAD_GLOBALLOAD_SUPER_ATTRr*   names    r!   inst_has_op_bitsrj      s    FFFr$   c                     U S:H  $ )Nrf   r*   rh   s    r!   rj   rj      s    }$$r$   c                     g)NFr*   rh   s    r!   rj   rj      s    r$   )r8   r9   r   c                   [        U 5      (       a  Ub  [        S5      eUc  SnO#USLU[        L-   USL-   nUS:  a  [        S5      eUb   [        U[        5      (       d  [        S5      e[        [        R                  U    XX#S9$ )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)r6   r7   r8   r9   r   )rj   RuntimeErrorr^   
isinstancer0   r   disopmap)ri   r8   r9   r   cnts        r!   create_instructionrs      s    ( KLL;C$6#=>&PTBTU7R  z#s33@AAyytV r$   c                 D    [         R                  S:  a  SOSn[        XS9$ )NrY   JUMP_FORWARDJUMP_ABSOLUTEr   sysversion_infors   )r   insts     r!   create_jump_absoluter|      s!     --8>oDd22r$   c                 X    U(       a  [        U 5      (       d
   SU  35       e[        SU S9$ )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r9   )r   rs   )valcheckeds     r!   create_load_constr      s2     $$>(8&>>$l377r$   c                  T    [         R                  S:  a
  [        SSS9$ [        S5      $ )NrY   COPYr   r8   DUP_TOPrx   r*   r$   r!   create_dup_topr      s(    
7"!&a00i((r$   c                 2   U S::  a  / $ [         R                  S:  a&  [        U SS5       Vs/ s H  n[        SUS9PM     sn$ [         R                  S:  a  U S:  a  [	        SU  S	35      eU S
::  a  [        S/ SQU S-
     -   5      /$ [        SU S9/$ s  snf )aj  
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   rY   SWAPr   rQ   
      zrotate z  not supported for Python < 3.10   ROT_)TWOTHREEFOURr
   ROT_N)ry   rz   rangers   AttributeError)nrS   s     r!   create_rot_nr      s     	Av	
7" <AAr?K?a"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                   ^ [        U [        5      (       a  U /mOU mU4S jnU4S jn[        R                  S:  aF  [	        TS   R
                  5      (       a  U" S5      (       d
  U" S5        T$ T[        S5      /-   m T$ [        R                  S:  ap  [	        TS   R
                  5      (       a  U" S5      (       d
  U" S5        T$ TS   R
                  S:X  a  U" S5      (       d
  U" S5        T$ [        S5      /T-   m T$ [        R                  S	:  aC  [	        TS   R
                  5      (       a  U" S5      (       d
  U" S5        T$ [        S5      /T-   mT$ )
ag  
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                 R   > TU    R                   c   eTU    R                   S-  S:H  $ Nr   r   idxinstss    r!   inst_has_bit_set'add_push_null.<locals>.inst_has_bit_set  s/    Sz~~)))Sz~~!Q&&r$   c                 Z   > TU    R                   c   eTU    =R                   S-  sl         g r   r   r   s    r!   set_inst_bit#add_push_null.<locals>.set_inst_bit  s)    Sz~~)))c
!r$   rP   r   	PUSH_NULLrb   r   rf   rY   )ro   r   ry   rz   rj   r7   rs   )r   r   r   r   s      @r!   add_push_nullr      sa   " ---' 7"E"I,,--6Fr6J6J & L# /<==E" L! 
		W	$ E"I,,--6Fr6J6J L 1X__-6Fq6I6IO L (45=E L 
		W	$E!HOO,,5Ea5H5HO L (45=ELr$   c                    [        U [        5      (       a  U /nOU n[        R                  S:  a  U$ [        R                  S:  a  SOSnX   R                  S:X  a?  X   R
                  c   eX   R
                  S-  S:X  a  X   =R
                  S-  sl        U$ [        R                  S:  a  U[        S5      /-   nU$ [        S5      /U-   nU$ )z~Like add_push_null, but the low bit of LOAD_ATTR/LOAD_SUPER_ATTR
is not set, due to an expected CALL_FUNCTION_EX instruction.
rY   rP   r   r   rf   r   r   )ro   r   ry   rz   r7   r8   rs   )r   r   r   s      r!   add_push_null_call_function_exr   3  s     ---
'!  G+"CzM)z~~))):>>A"JNNaNL
7"+K899 L $K01E9Lr$   c                    [         R                  S:  a  / nU(       aR  UR                  [        S5      5        [         R                  S:  a  U S-   OU S-   nUR	                  [        U5      5        [         R                  S:  a  UR                  [        SU S95        UR                  [        S	U S95        U$ [        S
U S9/$ )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 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),
        )
rY   r   rP   r   r
   rb   PRECALLr   CALLCALL_FUNCTION)ry   rz   appendrs   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                     [         R                  S:  a  [        SU S9/$ [         R                  S:  a  [        SU S9[        SU S9/$ [        SU S9/$ )Nrb   r   r   rY   r   CALL_METHODrx   )r   s    r!   create_call_methodr     s^    
7""6u566
7"ye4v51
 	
 }%899r$   c                 T    [         R                  S:  a  [        SSU S9$ [        SU S9$ )Nrb   re   r   )r8   r9   LOAD_METHODr   rx   rh   s    r!   create_load_methodr     s,    
7"!+1TBBmD99r$   c                 D    [         R                  S:  a  SOSn[        XS9$ )NrY   BEFORE_WITH
SETUP_WITHrw   rx   )r   r7   s     r!   create_setup_withr     s!    !..'9]|Ff44r$   c                 x   [         R                  S:  a  [        SU S9/$ U S:X  a  / $  [        SU S-
  S9[        S5      [        SSS	9[        S
5      [        S5      [        S5      [        S5      [        SSS	9[        S5      [        S5      [        S5      /[	        S5      Q[        S5      P[        SU S-
  S9P$ )NrY   r   r   r   
BUILD_LISTr   r~   r   r   BINARY_SUBSCR	ROT_THREESTORE_SUBSCRreverser   POP_TOPUNPACK_SEQUENCE)ry   rz   rs   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                 P   ^ ^^ [         R                  S:  d   e/ mUU U4S jnTU4$ )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                    > UT:w  d  U T:w  ap  [        S[        UT-
  S5      5      n[        S[        U T-
  S5      5      nUS:w  d  US:w  d   eTU-  mTU-  mTR                  X#S-  45        UT:w  a  Mg  U T:w  a  Mo  g g )Nr      i   maxminr   )
lineno_new
byteno_newbyte_offsetline_offsetr   r   lnotabs       r!   update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$   ry   rz   )r   r   r   r   s   `` @r!   lnotab_writerr     s/     g%%%F= 6>r$   c                    ^^^^^ [         R                  S:  a  [         R                  S:  d   e/ mU mSmSmU4S jmUUUU4S jnUUU4S jnTX4$ )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   rY   r   c                    > U S:w  d  US:w  ah  [        S[        U S5      5      n[        S[        US5      5      nUS:w  d  US:w  d   eX-  n X-  nTR                  X#S-  45        U S:w  a  M_  US:w  a  Mg  g g )Nr      ir   r   r   )byteno_deltalineno_deltar   r   	linetables       r!   _update%linetable_310_writer.<locals>._update  s    a<1#4a\3!78KdCc$:;K!#{a'777'L'Lk+=>? a<1#4r$   c                 4   > UT-
  nUmT" UT5        U T-
  mU mg r&   r*   )r   r   r   r   r   r   r   s      r!   r   $linetable_310_writer.<locals>.update  s+    !F*l+!F*r$   c                    > T" U T-
  T5        g r&   r*   )total_bytesr   r   r   s    r!   r   !linetable_310_writer.<locals>.end  s    f$l3r$   r   )first_linenor   r   r   r   r   r   r   s      @@@@@r!   linetable_310_writerr     s\     w&3+;+;g+EEEIFLF@ 4 f!!r$   r   c                     U S:  d   eU S-  /nU S-  n U S:  a.  US==   S-  ss'   UR                  U S-  5        U S-  n U S:  a  M.  U$ )zr
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                 T   ^^ [         R                  S:  d   e/ mU mSUU4S jjnTU4$ )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
rY   c                    >^  T (       a  T R                   OS nUU 4S jnUc  SnOUT-
  nUmUS:  a  U" US5        US-  nUS:  a  M  U" XA5        g )Nc                   > SUs=:  a  S::  d   e   eSnT(       a  TR                   bu  TR                  bh  TR                  b[  TR                  bN  TR	                  SU-   S-
  5        TR                  TR                   -
  TR                  S-   TR                  S-   4nOTR	                  SU-   S-
  5        U S:  a
  U * S-  S-  n OU S-  n TR                  [        U 5      5        U H  nTR                  [        U5      5        M     g )Nr      r*      r      )r   
end_lineno
col_offsetend_col_offsetr   r   r   )deltasizeother_varintsr   r   r>   s       r!   r   5linetable_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"  q!12 #r$   r   r   )r   )r>   	inst_sizer   r   r   r   r   s   `    r!   r   $linetable_311_writer.<locals>.update  sa    )2Y%%
	3@ L%.LF!mL!$NI !m 	(r$   )r>   r=   r   )r   r   r   r   s     @@r!   linetable_311_writerr     s;     w&&&IF,) ,)\ fr$   c                   H    \ rS rSr% \\S'   \\S'   \\S'   \\S'   \\S'   Srg)	ExceptionTableEntryiM  r   r   r   r   r   r*   N)r+   r,   r-   r.   r0   r/   r1   r3   r*   r$   r!   r   r   M  s     J	HKJKr$   r   c                     U S:  d   eU S-  /nU S-  n U S:  a!  UR                  U S-  5        U S-  n U S:  a  M!  UR                  5         [        [        U5      S-
  5       H  nX==   S-  ss'   M     U$ )zJ
Similar to `encode_varint`, but the 6-bit chunks are ordered in reverse.
r   r   r   r   r   )r   r   r   len)r   r   rS   s      r!   encode_exception_table_varintr  W  s~     6M6	
RA!GA
a%	R	a a% IIK3q6A:	
 Hr$   
bytes_iterc                     [        U 5      nUS-  nUS-  (       a#  US-  n[        U 5      nX!S-  -  nUS-  (       a  M#  U$ )z-
Inverse of `encode_exception_table_varint`.
r   r   r   )next)r  r   r   s      r!   decode_exception_table_varintr  g  sN     	ZA
b&C
b&	2v b&& Jr$   tabc                 
   [        [        U 5      S-
  5       Hh  nX   R                  X   R                  ::  aG  X   R                  XS-      R                  :  a&  XS-      R                  XS-      R                  ::  a  Mh   e   g)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  rS   s     r!   check_exception_tabler  t  sq    
 3s8a< FLLCFJJ&

SQZ---E
  CAJNN2	
3 !r$   exntabc           	      4   [        U 5      n/ n  [        U5      S-  n[        U5      S-  nX4-   S-
  n[        U5      S-  n[        U5      nUS-	  n[        US-  5      n	UR                  [	        X5XhU	5      5        Mm  ! [
         a    [        U5        Us $ f = f)zy
Parse the exception table according to
https://github.com/python/cpython/blob/3.11/Objects/exception_handling_notes.txt
r
   r   )iterr  r1   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*5veLM   c"
s   A.A= =BBc                    / nU  H  n[        UR                  S-  5      nUS==   S-  ss'   UR                  U5        UR                  UR                  -
  S-   nUR                  [        US-  5      5        UR                  [        UR                  S-  5      5        UR
                  S-  UR                  -   nUR                  [        U5      5        M     [        U5      $ )zX
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3EKK14DEA& 	U[[(1,	.v{;<	.u||q/@ABkkQ%++-	.r23  8Or$   instructionsfirstlinenoc                    / n[         R                  S:  Ga  [        U5      u  p4Sn[        U 5       H  u  pgUR                  S:X  a>  SnUS-  nS H0  n	XU	-      R                  S:w  d  M  XU	-      R
                  Ul          O   O[        U5      S-  U-   nSnU" UR
                  U5        SnUR                  =(       d    Sn
UR                  UR                  U
S-  45        [        [        U5      S-  S-
  5       H  nUR                  S5        M     M     O[         R                  S	:  a  [        U5      u  p4O[        U5      u  p4nU  Ha  nUR                  b  U" UR                  [        U5      5        UR                  =(       d    Sn
UR                  UR                  U
S-  45        Mc     [         R                  S	:  a  W" [        U5      5        [        U5      [        U5      4$ )
z)Do the opposite of dis.get_instructions()rY   r   EXTENDED_ARGr   r   r
   rQ   r
   r   )r   r   r   )ry   rz   r   	enumerater7   r>   instruction_sizer8   r   r6   r   r   r   r;   r   r  )r  r  coder   update_linenonum_extrS   r{   r   jr8   _r   s                r!   assembler#    s   D
7" 4[ A .GA{{n,	1"A#E*11^C)5!e)<)F)F #
 -T2a7'A	$..)4G((-aCKKcDj12+D1Q6:;F# <! /& g%$1+$>!FM)=k)J&F3 D+d..D	:((-aCKKcDj12	 ! w&D	N;f%%r$   offset_to_instr:   c                 n    S H/  nXU-      R                   [        R                  :w  d  M(  XU-      s  $    g)zM
Get the instruction located at a given offset, accounting for EXTENDED_ARGs
)r   r
   r   r   Nr6   rp   r  )r$  r:   r   s      r!   _get_instruction_by_offsetr'    s;     1*%,,0@0@@!1*--  r$   c                    U  Vs0 s H  oR                   U_M     nnU  H[  nUR                  [        R                  ;   d   UR                  [        R                  ;   d  MA  [        X!R                  5      Ul        M]     gs  snf )z9Replace jump targets with pointers to make editing easierN)r:   r6   rp   hasjabshasjrelr'  r9   r   )r  r{   jump_targetss      r!   virtualize_jumpsr,    s\    2>?,$KK%,L?;;#++%)C4\;;ODK  @s   A?instructionc                    [         R                  S:  a  [        S5      eSU R                  ;   a"  U R                  R	                  SS5      U l        O=SU R                  ;   a"  U R                  R	                  SS5      U l        O[        S5      e[        R                  U R                     U l        U R                  [        ;   d   eg )NrY   z+Cannot flip jump direction in Python < 3.11FORWARDBACKWARDz-Instruction is not a forward or backward jump)
ry   rz   rn   r7   replacer   rp   rq   r6   
_REL_JUMPS)r-  s    r!   flip_jump_directionr3    s    
'!HIIK&&&(//77	:N	{))	)(//77
INLMM;#5#56K+++r$   r   c                     X   nS H5  nX:  a,  XU-
     R                   [        R                  :X  a	  XU-
     nM4    U$    U$ )zd
i.e. get the first EXTENDED_ARG instruction (if any) when targeting
instructions[idx] with a jump.
r  r&  )r  r   r   r:   s       r!   _get_instruction_frontr5    sP    
 F=\,7>>#BRBRR!,/FM 
 Mr$   c           	         [        [        R                  5      R                  [        [        R                  5      5      nU  H  nUR
                  U;   d  M  UR
                  [        R                  ;  d  M5  UR                  R                  UR                  :  a>  [        R                  S:  a  [        S5      eSUR                  ;   a  [        U5        M  M  [        R                  S:  d  M  SUR                  ;   d  M  [        U5        M     [        U 5        [        U 5      nU  GH9  nUR
                  U;   d  M  [        XUR                     5      nUR
                  [        R                  ;   ac  [        R                  S:  a  UR                  Ul        O[        R                  S:  a  [#        UR                  S-  5      Ul        Op[        S5      e[%        [#        UR                  UR                  -
  ['        U5      -
  5      5      Ul        [        R                  S:  a  U=R                   S-  sl        UR                  Ul        SUR                   3Ul        GM<     g	)
zJFill in args for virtualized jump target after instructions may have movedrY   z*Got negative jump offset for Python < 3.11r/  r0  r   r
   z+Python 3.11+ should not have absolute jumpszto N)setrp   r)  unionr*  r6   r   r:   ry   rz   rn   r7   r3  update_offsetsr   r5  r8   r0   absr  r9   r@   )r  jumpsr{   indexofr   s        r!   devirtualize_jumpsr=    s   ""3s{{#34E ;;%{{#++-;;%%3'''1*+WXX DKK/+D1 0 ''72zT[[7P+D1  < ,'G ;;%+L$++:NOF{{ckk)##g-%}}DH%%/  #6==1#45DH&'TUU 36Ft6LLM ##w.HHNH --DK 0DL+ r$   exn_tab_bytesc                   ^^^	^
 [        U 5      nU Vs0 s H  n[        [        UR                  5      U_M      snm	[	        T	R                  5       5      m
Sm[        U5      m UUU	U
4S jnU" 5       u  pVU Hy  nUR                  UR                  :  a%  U" 5       u  pVUR                  UR                  :  a  M%  UR                  UR                  :  d  M^  [        R                  " U5      Ul
        M{     gs  snf ! [         a     gf = f)zDReplace exception table entries with pointers to make editing easierr   c                    > [        T5      n T[        T5      :  a<  TT   U R                  ::  a)  TS-  mT[        T5      :  a  TT   U R                  ::  a  M)  TS:  d   eTTS-
     n[        [	        TU R
                  5      [	        TU5      [	        TU R                  5      U R                  U R                  5      nX4$ )Nr   r   )	r  r   r   r   r'  r   r   r   r   )r  
end_offset
inst_entryend_offset_idxexn_tab_iterr$  offsetss      r!   step(virtualize_exception_table.<locals>.step=  s    &E W-'.2IUYY2V!# W-'.2IUYY2V "A%%% !!34J/*>5;;G*>:F*>5<<HJ $$r$   N)r  r   r0   r:   sortedkeysr  r   r   copyr?   r  )r>  r  exn_tabr{   rF  r  rB  rC  rD  r$  rE  s          @@@@r!   virtualize_exception_tablerL  4  s    #M2G?KL|td3,d2|LN^((*+GN=L	% 	%, !F D++		)$(F! ++		){{ekk)%)YYz%:"	 !; MD  s$   %C5#AC: 9C: C: :
DDc           	      b  ^^^^ 0 m[        U 5      nU  GH  nUR                  (       d  M  [        XUR                  R                     5      R                  n[        [        UR                  R                  R                  5      [        UR                  R                  5      -   S-
  n[        XUR                  R                     5      R                  nX44nXRR                  R                  UR                  R                  4nUT;   a  TU   U:X  d   eUTU'   GM     [        TR                  5       S S9nSm/ m/ mUUUU4S jn	U H  nT(       a.  TS   S   US   :  a  U	" 5         T(       a  TS   S   US   :  a  M  T(       aq  TS   S   US   s=::  a  US   s=::  a  TS   S   ::  d   e   e[        TTS   S   5      n
XS   :  a'  TR                  [!        XS   S-
  /TTS      Q76 5        US   mTR                  U5        M     T(       a  U	" 5         T(       a  M  [#        T5        T$ )zMCompute exception table in list format from instructions with exn_tab_entriesr
   c                     U S   U S   * 4$ Nr   r   r*   ts    r!   <lambda>)compute_exception_table.<locals>.<lambda>  s    1!ur$   keyr   c                     > T(       aR  TR                  5       n TU S   ::  a8  TR                  [        [        U S   T5      U S   /TU    Q76 5        U S   S-   mggg)zD
Pop the key_stack and append an exception table entry if possible.
r   r   r
   N)popr   r   r   )rU  exn_dictrK  	key_stacknextis    r!   rW  $compute_exception_table.<locals>.pop  se    
 --/CA'CFE(:CFSXc]S A
	  r$   r   r   )r   r?   r5  r   r:   r   r0   r   r  r   r   r   rH  rI  r   r   r   r  )r  r<  r{   r   r   r   rU  r   keys_sortedrW  leftrX  rK  rY  rZ  s              @@@@r!   compute_exception_tabler^  ]  s3    >@H,'G*d&8&8&>&>?f 
 S$,,00778"4#5#5#9#9:; 
 ,d&8&8&?&?@f  ,C--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!f}'!fqjS8IbM;RS FE   )'"Nr$   c                 T   / nU  H  nXR                      XR                     4nU(       a7  US   S   US   :  a(  UR                  5         U(       a  US   S   US   :  a  M(  U(       a+  US   S   US   s=::  a  US   s=::  a  US   S   ::  d   e   eUR                  U5        M     g)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   rW  r   )r  r<  entry_stackr  rU  s        r!   !check_inst_exn_tab_entries_nestedra    s     *,K{{#WYY%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                 "   [        U 5      n0 nU  Hm  nUR                  (       d  M  XR                  R                     XR                  R                     4nXB;   a  UR                  X$   :X  d   eUR                  X$'   Mo     [	        UR                  5       S S9 Vs/ s H  oBU   PM	     nn[        XQ5        U HN  n[        XR                     XR                     S-   5       H   n[        R                  " U5      X   l        M"     MP     gs  snf )zr
Copies exception table entries to all instructions in an entry's range.
Supports nested exception table entries.
c                     U S   U S   * 4$ rO  r*   rP  s    r!   rR  2propagate_inst_exn_table_entries.<locals>.<lambda>  s    adQqTE]r$   rT  r   N)	r   r?   r   r   rH  rI  ra  r   rJ  )r  r<  entriesr{   rU  sorted_entriesr  rS   s           r!    propagate_inst_exn_table_entriesrg    s    
 ,'G=?G**001**../C ~))W\999--GL  !'w||~;R S S S   &n>  w{{+WYY-?!-CDA,0IIe,<LO) E  s   Dc                    [        U 5      n[        5       n[        U 5       H  u  p4UR                  (       d  M  [        R
                  S:  d   e[        UR                  5      U;  d   eUR                  [        UR                  5      5        UR                  nUR                  U;   d   eUR                  U;   d   eUR                  U;   d   eXR                     Us=::  a  XR                     ::  a  M   e   e   g)a.  
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.
rY   N)r   r7  r  r?   ry   rz   rC   addr   r   r   )r  r<  exn_tab_entry_setrS   r{   r  s         r!    check_inst_exn_tab_entries_validrk    s     ,'G\*##w...d(()1BBBB!!"T%7%7"89&&E;;')))99'''<<7***;;'1B		0BBBBBB +r$   c                 x    U  Vs/ s H$  oR                   [        R                  :w  d  M"  UPM&     snU S S & g s  snf r&   r&  )r  rS   s     r!   strip_extended_argsrm    s,    ".O,Q((c>N>N2Nq,OLOOs   !77c                    U R                   (       a<  U R                   R                  U L a#  [        U5      S:  a  US   U R                   l        USS   H9  n[        R                  " U R                   5      Ul         U R                  Ul        M;     US   R
                  U l        US   R                  U l        US   R                  U l        US   R                  U l        US   R                  U l	        U /USS  -   $ )Nr   r   r   )
r?   r   r   rJ  r>   r6   r7   r8   r9   r   )old_inst	new_instsr{   s      r!   overwrite_instructionrq    s     	""&&(2	NQ%.r]"!"!YYx'='=>!++   l))HOl))HOQ<##HLl))HOl))HO:	!"%%r$   c                     [         R                  S:  d   eSSS.nU  HJ  nUR                  U;   d  M  XR                     Ul        [        R                  UR                     Ul        ML     U $ )zFLOAD_METHOD puts a NULL on the stack which causes issues, so remove itrY   re   r   )r   r   )ry   rz   r7   rp   rq   r6   )r  rewritesr{   s      r!   remove_load_call_methodrt    s`    g%%%*?KH;;("";;/DK))DKK0DK  r$   c                    / nU  H  nSUR                   ;   a  [        S[        SUR                   ;   5      S9nUR                  Ul        [
        R                  S:  a'  [        SUR                   ;   a  SOSUR                  S	9nO[        S
UR                  S	9n[        SS S9UU/nUR                  [        X%5      5        M  UR                  U5        M     XS S & g )N_NONEIS_OPNOTr   rb   r/  POP_JUMP_FORWARD_IF_TRUEPOP_JUMP_BACKWARD_IF_TRUErw   POP_JUMP_IF_TRUEr~   r   )r7   rs   r0   r8   r9   ry   rz   r   r   rq  r   )r  rp  r{   is_opjump_opreplace_instss         r!   remove_jump_if_noner    s    Idkk!&wC8L4MNE 99EL'), %3 38;; --?T #<=M
 24GHT"3 4  Or$   c                    / nU  H  nUR                  U5        UR                  S;   d  M&  [        UR                  R                  SS5      5      nUR                  (       a)  UR                  R
                  UL a  X2R                  l        [        R                  " UR                  5      Ul        UR                  Ul        [        R                  S   Ul
        SUl        SUl        SUl        UR                  U5        M     XS S & g )N)BINARY_SLICESTORE_SLICESLICESUBSCRBUILD_SLICEr
   )r   r7   rs   r1  r?   r   rJ  r>   rp   rq   r6   r8   r9   )r  rp  r{   subscr_insts       r!   remove_binary_store_slicer  5  s    I;;99,T[[-@-@(-STK!!d&8&8&<&<&D)4""&(,		$2D2D(EK%$(NNK!))M2DK'DKDHDK[)   Or$   )	LOAD_FASTr  )
STORE_FASTr  )r  r  )LOAD_FAST_LOAD_FASTSTORE_FAST_STORE_FASTSTORE_FAST_LOAD_FASTc                    / nU  Hy  nUR                   [        ;   aQ  [        UR                      u  p4UR                  u  pV[        X5S9[        XFS9/nUR	                  [        X'5      5        Mh  UR                  U5        M{     XS S & g )Nr   )r7   FUSED_INSTSr9   rs   r   rq  r   )r  rp  r{   inst0inst1argval0argval1r~  s           r!   remove_fused_load_storer  P  s    I;;+%&t{{3LE#{{G #59"59M 24GHT"   Or$   r  c                    U R                   =(       d    SU R                  =(       d    S-   n[        U R                  5      (       d  g/ n[	        U5       GHv  u  pEUR                  U5        UR                  S:X  d  M)  UR                  S:X  d  M;  XS-      nUR                  S:X  d  MT  [        R                  S:  a  UR                  S:X  d`  [        R                  S	:  a$  [        R                  S:  a  UR                  S
:X  d(  [        R                  S	:  d  M  UR                  S:X  d  M  SU;   d   eUR                  [        SSS95        U R                  S   nXr;   a  UR                  [        SUS95        OUR                  [        SUS95        SUl        SUl        UR                  S
:X  d  GMa  XS-      nSUl        SUl        GMy     X1SS& g)z3convert super() with no args into explicit arg formr*   Nrf   superr   r   rb   r   rY   r   r   	__class__
LOAD_DEREFr   r  r
   )co_cellvarsco_freevarsr   co_varnamesr  r   r7   r9   r8   ry   rz   rs   )	r  r  cell_and_freer   r   r{   rZ  	first_var	call_insts	            r!   explicit_superr  a  s~   %%+0@0@0FBGMt  F|,	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, ,1W 5I$%IM'(I$5 -8 Or$   c                   ^ / mU4S jnU  GH  nUR                   [        R                  :X  a	  SUl        GOaUR                  (       a  UR                  S:  a{  U" S5        TR	                  [        SUR                  S-	  S95        TR	                  [        SUR                  S-	  S95        TR	                  [        SUR                  S	-	  S95        OUR                  (       ae  UR                  S
:  aU  U" S5        TR	                  [        SUR                  S-	  S95        TR	                  [        SUR                  S	-	  S95        OOUR                  (       a>  UR                  S:  a.  U" S5        TR	                  [        SUR                  S	-	  S95        TR	                  U5        GM     [        T5      [        U 5      -
  nUS:  d   eTU SS& U$ )z,Fill in correct argvals for EXTENDED_ARG opsc                    > [        U 5       H?  nT(       d  M  TS   R                  [        R                  :X  d  M/  TR	                  5         MA     g )Nr   )r   r6   rp   r  rW  )r   r"  r   s     r!   maybe_pop_n&fix_extended_args.<locals>.maybe_pop_n  s6    qAv&*++s/?/??

 r$   r   i rQ   r     r      r   i  r
   r   r   N)r6   rp   r  r8   r   rs   r   )r  r  r{   addedr   s       @r!   fix_extended_argsr    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$   c                     SS K n[        R                  S:  a=  SUR                  R                  R
                  R                  U R                     S-   -  $ g)Nr   rY   r
   r   )torchry   rz   _C_dynamo
eval_framepy_opcode_cachesr6   )r{   r  s     r!   r  r    sF    
7"EHH$$//@@MPQQRRr$   c                 Z    SnU  H#  nUR                   U:X  d   eU[        U5      -  nM%     g Nr   r:   r  r  r:   r{   s      r!   check_offsetsr    s3    F{{f$$$"4(( r$   c                 B    SnU  H  nXl         U[        U5      -  nM     g r  r  r  s      r!   r9  r9    s&    F"4(( r$   c            	      V   [        [        [        [        U 5      5      5      nU/[	        U 5      -   [        U S   U S   5       VVs/ s H  u  p#[        X#:g  5      PM     snn/-    Vs/ s H  nSR                  S U 5       5      PM     nnSSR                  U5      -   $ s  snnf s  snf )Nr    c              3   (   #    U  H  oS  v   M
     g7f)03Nr*   ).0xs     r!   	<genexpr>debug_bytes.<locals>.<genexpr>  s     (Cqb6Cs   zbytes mismatch

)r   r   mapr   listzipr0   join)argsindexar   r8   results         r!   debug_bytesr    s    #c#tn%&E 7
t*$'R$r($;<$;DACK$;<
=>>C 	(C((>   		& 111 =	s   B ("B&c                    [        U S SS9nU R                  UR                  :X  d%   [        U R                  UR                  5      5       eU R                  UR                  :X  d%   [        U R                  UR                  5      5       eg)z<Make sure our assembler produces same bytes as we start withc                     g r&   r*   )r  ys     r!   rR  debug_checks.<locals>.<lambda>  s    Dr$   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                 ~    [        U S   5       H  u  p#XL d  M  Us  $    U S==   U4-  ss'   [        U S   5      S-
  $ )N	co_constsr   )r  r   )code_optionsr   rS   vs       r!   get_const_indexr    sM    ,{34
 8H 5 #'|K()A--r$   c                 
  ^ ^^	^
^^ [        TS   5       VVs0 s H  u  p4XC_M	     snnmS[        4UU4S jjn[        R                  S:  aR  Ub   e[        TS   5       VVs0 s H  u  p4XC_M	     snnm[        TS   TS   -   5       VVs0 s H  u  p4XC_M	     snnm	Ot[	        U5      (       d   e0 n[
        R                  " 5        H  n U" U5      nX6U'   M     TS    Vs0 s H  oDXd   _M	     snmTS   TS   -    Vs0 s H  nXFU   _M
     snm	[        [        T 5      5       GH  m
U
U 4S jnT T
   R                  S	:X  a  T T
   R                  [        Ld   e[        R                  S:  aU  T T
   R                  c   eU" T T
   R                  5      S
-  [        [        T T
   R                  5      S-  -   T T
   l        M  U" T T
   R                  5      T T
   l        M  T T
   R                  S:X  a  T T
   R                  [        Ld   e[        R                  S:  aV  T T
   R                  c   eU" T T
   R                  5      S
-  [        [        T T
   R                  5      S-  -   T T
   l        GMS  U" T T
   R                  5      T T
   l        GMs  T T
   R                  S:X  aq  T T
   R                  c   eT T
   R                  [        Ld   eU" T T
   R                  5      S-  [        [        T T
   R                  5      S-  -   S-   T T
   l        GM  T T
   R                  [        ;   a  [        R                  S:  d   e[!        T T
   R                  ["        5      (       d   e[        T T
   R                  5      S:X  d   e[#        U	U4S jT T
   R                   5       5      nUS   S-  US
   S-  -   T T
   l        GM  T T
   R$                  [&        ;   ap  U" 5       (       aa  [        R                  S:  a0  T T
   R                  T;  a  T	T T
   R                     T T
   l        GM  TT T
   R                     T T
   l        GM)  GM,  T T
   R$                  [(        ;   a/  U" 5       (       a   U" T T
   R                  5      T T
   l        GMo  GMr  T T
   R$                  [*        ;   a,  U" 5       (       a  T	T T
   R                     T T
   l        GM  GM  T T
   R$                  [,        ;   d  GM  T T
   R                  b  GM  [/        TT T
   R                  5      nUS:  d   eUT T
   l        GM     g s  snnf s  snnf s  snnf ! [         a       GM  f = fs  snf s  snf )Nco_namesr   c                    >  TU    nU$ ! [          a>    [        T5      =nTU '   / TS   QU P7TS'   [        TS   5      [        T5      :X  d   e U$ f = f)Nr  )KeyErrorr   )ri   r   r  namess     r!   get_name_index fix_vars.<locals>.get_name_index  su    	?+C 
  	? #E
*C%+'Hj)A'H4'HL$|J/0CJ>>>
	?s   
 AAArY   r  r  r  c                  .   > TT    R                   [        L$ r&   )r9   r^   )rS   r  s   r!   should_compute_arg$fix_vars.<locals>.should_compute_arg
  s    ?))==r$   rf   r   r
   re   rb   rg   rP   c              3   D   >#    U  H  nUT;   a  TU   OTU   v   M     g 7fr&   r*   )r  ri   	freenamesvarnamess     r!   r  fix_vars.<locals>.<genexpr>/  s,      2D #'("2	$G2s    r   r      )r  r0   ry   rz   callable	itertoolscount
IndexErrorr   r   r7   r9   r^   r8   r   r  ro   tupler6   	HAS_LOCALHAS_NAMEHAS_FREE	HAS_CONSTr  )r  r  varname_from_opargr   ri   r  allnamesr  	arg_tupler  rS   r  r  s   ``       @@@@r!   fix_varsr    sb   (1,z2J(KL(K93TY(KLE   '!!)))/8m9T/UV/U)#DI/UV ']+l=.II
	 I
	 *++++??$C)#.!$ % 6B-5PQ5PT(.(5PQ %]3l=6QQ
Q 4. Q
	 3|$%	> ?!!]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 &G M W
  Q
s/   T:#U UU#UU$
UUc                    U  H  nUR                   [        Ld  M  UR                  [        ;   d>  UR                  [        ;   d*  UR                  [
        ;   d  UR                  [        ;   d  Mj  UR                  S;  d  M|  S Ul        M     g )N)rf   re   rg   )	r9   r^   r6   r  r  r  r  r7   r8   )r  r{   s     r!   clear_instruction_argsr  N  s^     KK|+y(;;(*;;(*;;)+#RRDH r$   c                     S/n U R                  S5        U R                  / SQ5        [        R                  S:  a  U R                  S5        U R                  S5        [        R                  S:  a  U R                  S5        OU R                  S	5        [        R                  S:  a  U R                  S
5        U R                  SS/5        U $ )Nco_argcountco_posonlyargcount)
co_kwonlyargcount
co_nlocalsco_stacksizeco_flagsr  r  r  r  co_filenameco_namerY   co_qualnameco_firstlinenor   co_linetabler  co_exceptiontabler  r  )r   r   ry   rz   )rI  s    r!   get_code_keysr   _  s     ?DKK$%KK	
 7"M"KK !
7"N#K 
7"'(KK	
 Kr$   c                     [        5       nU Vs0 s H  oD[        X5      _M     nn[        US   5      US   :X  d   e[        X5      n[	        U5        U" Xe5        [        XcU5      S   $ s  snf )Nr  r  r   )r   getattrr   cleaned_instructionsr   clean_and_assemble_instructions)r  transformationsr  rI  kr  r  s          r!   r  r    ss    ?D156Awt''L6|M*+|L/IIII'3L%L/*<|LQOO 7s   A*rI  r  c                    [        U 5        [        US   5      US'   S n[        R                  S:  a2  [        R
                  " U Vs/ s H  oBU   PM	     sn6 nUR                  n[        XUS9  SnU(       a3  [        U 5        [        U 5        [        [        U 5      5      nU(       a  M3  [        U 5        [        XS   5      u  px[        R                  S:  a  XS'   OXS	'   XrS
'   [        U 5      US'   [        U5      S1-
  [        UR!                  5       5      S1-
  :X  d   e[        R                  S:  a  [#        [%        U 5      5      US'   U [        R
                  " U Vs/ s H  oBU   PM	     sn6 4$ s  snf s  snf )Nr  r  rY   )r  Tr  r   r  r  r  r  r  r  )rk  r   ry   rz   typesCodeType_varname_from_opargr  r9  r=  r1   r  r   r#  r   r7  rI  r  r^  )	r  rI  r  r  r  tmp_codedirtybytecoder   s	            r!   r  r    s    %\2!$\-%@!AL
7">>T#BTOT#BC%99\<NOE
|$<(&|45	 % <(;K.LMH
'!$*[!'-^$&#5l#CL t9,--\5F5F5H1IM 2    7",D#L1-
() 4)H4aq/4)HIII9 $C8 *Is   E8%E=
c                 `    U  H(  nUR                   S:X  d  M  XR                     Ul        M*     g )NKW_NAMES)r7   r8   r9   )r  constsr{   s      r!   populate_kw_names_argvalr    s&    ;;*$ *DK r$   c                 .    [        X5      n[        U5      $ r&   )_cached_cleaned_instructions_clone_instructionsr  r  r  s      r!   r  r    s    /;L |,,r$   c                 b   U  Vs/ s H  n[        UR                  UR                  UR                  UR                  UR
                  UR                  UR                  UR                  UR                  UR                  UR                  5      PM     nn[        [        X5      5      nS US '   U Hw  nX1R                     Ul	        UR                  =n(       d  M+  [        X4R                     X4R                      X4R                     UR"                  UR$                  5      Ul
        My     U$ s  snf r&   )r   r6   r7   r8   r9   r:   r;   r<   r>   r   r?   r@   dictr  r   r   r   r   r   )r  rS   copiedremapr  s        r!   r  r    s    "  A 	HHHHEEHHHHMMKKHHOOII	
   " \*+EE$K?OO#5#4kk"ii ll#AO  M?s   BD,c                    [        [        [        [        R                  " U 5      5      5      n[        U5        [        R                  S:  a+  [        X R                  5        [        U R                  U5        [        U5        [        U5        U(       d  [        R                  S:  a  [        U5        [        R                  S:  a  [        X5        [        R                  S:  aI  [!        U5        [        R                  S:  a  [#        U5        [        R                  S:  a  [%        U5        [        R                  S:  a  ['        U5        [)        U5        U$ )NrY   rb   rP   )r  r  rW   rp   get_instructionsr  ry   rz   r  r  rL  r  r,  rm  rt  r  r  r  r  r9  r=  r  s      r!   r  r    s    /1E1Ed1KLML,
7" ~~>"4#9#9<H\"%g%#L1g%4.w&-7*),77*'5
7"|$<(r$   c                 *    U  S[        [        5       3$ )Nr"  )r  _unique_id_counterrh   s    r!   	unique_idr    s    V1T,-.//r$   c                 *    SnU R                   U-  S:  $ )N    r   )r  )r  co_generators     r!   is_generatorr"    s    LMML(A--r$   c                    [        U R                  5      n[        U5        U(       a-  [        U5       H  u  pVUR                  S:X  d  M  XES-   S n  O   U H8  nSUl        U(       d  M  UR                  U;   d  M%  XR                     Ul        M:     U(       Ga  [        R                  S:  az  / nU Hp  nUR                  S:X  aL  [        R                  S   Ul        SUl        UR                  U5        UR                  [        S5      5        M_  UR                  U5        Mr     Un/ nU H&  nUR                  S:X  d  M  UR                  U5        M(     [        U5      S:X  a  US   US	   L a  UR                  S	5        U$ [        U5      S:  a  US	   US	   L aO  S
US	   l        [        R                  S
   US	   l        SUS	   l        ["        US	   l        UR                  S	5        OUR                  [        S
5      5        U Hf  n[%        US	   5      n	U	R                  Ul        U	R                  Ul        U	R                   Ul        U	R                  Ul        U	R&                  Ul        Mh     U$ )a7  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   Nrb   RETURN_CONSTr~   RETURN_VALUEr   r   NOP)r  __code__r  r  r7   r;   r9   ry   rz   rp   rq   r6   r   rs   r   rW  r8   r^   r|   r   )
fnvarname_mapnoreturnnoprefixr   rS   r{   rp  returns	jump_insts
             r!   bytecode_from_templater/    s(   D !-E5! 'GA{{h&!eg (
   ;4;;+5%kk2DK  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)srO   rJ  dataclassesrp   	functoolsr  ry   r  collections.abcr   r   typingr   r   r   r   r	   utils._backport_slotsr   bytecode_analysisr   r   r   r   utilsr   	dataclassr   r   rz   rW   r^   rj   rs   r|   r   r   r  r   r   r   r   r   r   r   r   r0   r  r   r   r   r   r   r  r  r  r  r  r  r#  r  r'  r,  r7  r*  r2  r3  r5  r=  rL  r^  ra  rg  rk  rm  rq  rt  r  r  r  r  r	  r  r  r  r  r9  r2   r  r  haslocalr  hasnamer  hasfreer  hasconstr  r  r  r  r   r  r  r  r  r  	lru_cacher  r  r  r  r1   r"  r/  r*   r$   r!   <module>r=     su     
   
  . 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 __& 0s 0.u~~ .$ .
`r$   