
    ȅi                       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rSSK	J
r
JrJr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  \(       a  SS	KJr  \R@                  " S
S9 " S S5      5       r!\R@                  " S
S9 " S S5      5       r"\RF                  S:  a  S\RD                  S\"4S jr$O7\RF                  S:  a  S\RD                  S\"4S jr$OS\RD                  S\"4S jr$ " S S5      r%\RF                  S:  a
  S\&S\'4S jr(O#\RF                  S:  a
  S\&S\'4S jr(O	S\&S\'4S jr(S\%SS.S\&S\\)   S \\   S!\\"   S\"4
S" jjr*S!\"S\"4S# jr+S!\"S\'4S$ jr,SS%\S&\'S\"4S' jjr-S\"4S( jr.S)\)S\/\"   4S* jr0S+\\"\/\"   4   S\/\"   4S, jr1S+\\"\/\"   4   S\/\"   4S- jr2S.\)S/\'S\/\"   4S0 jr3 SS1\'S/\'S2\'S\/\"   4S3 jjr4S.\)S\/\"   4S4 jr5S\&S\"4S5 jr6S!\"S\"4S6 jr7S)\)S\/\"   4S7 jr8 SS8\\)   S9\\)   S:\'S\/\"   4S; jjr9S\)S\/\"   4S< jr:S=\)S\/\"   4S> jr;S?\S\/\"   4S@ jr<S\"4SA jr=S)\)S\"4SB jr>SC\)S\?\/\)   \
\)\)/S4   \
\)/S4   4   4SD jr@S)\)S\/\)   4SE jrA\RF                  S:  a%  SC\)S\?\/\)   \
\R                  S-  \)/S4   4   4SF jrC\R@                  " S
S9 " SG SH5      5       rDS)\)S\/\)   4SI jrESJ\\)   S\)4SK jrFSL\/\D   SS4SM jrGSN\HS\/\D   4SO jrISL\/\D   S\H4SP jrJSQ\/\"   SR\)S\?\H\H4   4SS jrKST\L\)\"4   SU\)S\\"   4SV jrMSQ\\"   SS4SW jrN\O" \R                  5      rQSX\"SS4SY jrRSQ\/\"   SZ\)S\"4S[ jrSSQ\/\"   SS4S\ jrTS]\HSQ\/\"   SS4S^ jrUSQ\/\"   S\/\D   4S_ jrVSL\/\!   S`\L\"\)4   SS4Sa jrWSQ\/\"   SS4Sb jrXSQ\/\"   SS4Sc jrYSQ\/\"   SS4Sd jrZSe\"Sf\/\"   S\/\"   4Sg jr[SQ\/\"   S\/\"   4Sh jr\SQ\/\"   SS4Si jr]SQ\/\"   SS4Sj jr^SkSlSmSnSo.r_SQ\/\"   SS4Sp jr`SQ\/\"   SS4Sq jraSQ\/\"   SS4Sr jrbSs\R                  SQ\/\"   SS4St jrdSQ\/\"   S\)4Su jreSv\"S\)4Sw jrfSQ\\"   SS4Sx jrgSQ\\"   SS4Sy jrhSz\HS\&4S{ jriSs\R                  SS4S| jrj\O" \R                  5      rl\O" \R                  5      rn\O" \R                  5      rp\O" \R                  5      rrS}\L\&\4   S%\S\)4S~ jrs SSQ\/\"   S}\L\&\4   S\\
S\4      SS4S jjrtSQ\/\"   SS4S jru\R                  S\/\&   4S j5       rw SSs\R                  S\
\/\"   \L\&\4   /\S   4   S\'S\?\R                  \S   4   4S jjrxSQ\/\"   S\/\&   S}\L\&\4   S\?\/\"   \R                  4   4S jrySQ\\"   S\SS4S jrzSSs\R                  S\'S\/\"   4S jjr{SQ\\"   S\/\"   4S jr|\R                   SSs\R                  S\'S\\"   4S jj5       r}\R                  " 5       rSS\&S\'S\&4S jjrSs\R                  S\'4S jr   SS\
S\4   S\\\\4      S\'S\'S\/\"   4
S jjrg)a  
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)CallableIterableIteratorMappingSequence)AnycastOptionalTYPE_CHECKINGUnion   )config)get_indexofpropagate_line_numsremove_extra_line_numsstacksize_analysis)is_safe_constant)DynamoTracerOutputT)slotsc                   h    \ rS rSr% S\S'   S\S'   S\S'   \\S'   \\S'   S\4S	 jrS
\	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    _/home/james-whalen/.local/lib/python3.13/site-packages/torch/_dynamo/bytecode_transformation.py__repr__InstructionExnTabEntry.__repr__1   sl    +DJJ,F,F,H+I J88++-. /kk1134 5ZZLA7	
    oc                 Z   [        U[        5      (       d  g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  $ NF)
isinstancer   r   r   r   r   r   )r$   r)   s     r%   __eq__InstructionExnTabEntry.__eq__9   s    !344JJ!''! &AEE!&qxx'& 

agg%& 

agg%	
r(    N)__name__
__module____qualname____firstlineno____annotations__intboolstrr&   objectr-   __static_attributes__r/   r(   r%   r   r   )   s=    	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\S\4S jrS\4S jrSS jrSrg)r   E   z$A mutable version of dis.InstructionopcodeopnameargargvalNoffsetstarts_lineFis_jump_targetzdis.Positions	positionsr   exn_tab_entryargreprr   c                     [        U 5      $ Nidr#   s    r%   __hash__Instruction.__hash__W   s    $xr(   otherc                 0    [        U 5      [        U5      :H  $ rG   rH   r$   rL   s     r%   r-   Instruction.__eq__Z   s    $x2e9$$r(   c                 <    SU R                    SU R                   S3$ )NzInstruction(opname=z	, offset=r!   )r=   r@   r#   s    r%   r"   Instruction.short_inst_repr]   s    $T[[M4;;-qIIr(   c                 H    UR                   U l         UR                  U l        g rG   )rA   rC   rN   s     r%   copy_positionsInstruction.copy_positions`   s     ,,r(   )rC   rA   )rL   r   r   N)r0   r1   r2   r3   __doc__r5   r4   r7   r
   r   r@   rA   rB   r6   rC   r   rD   r   rE   rJ   r8   r-   r"   rS   r9   r/   r(   r%   r   r   E   s    .KK	#K FHSM !%K#% ND +/Ix(/&*FH]#*6:M823:!GXc]!# %F %t %J J)r(   r         ir   c           
          [        U R                  U R                  U R                  U R                  U R
                  U R                  U R                  U R                  5      $ rG   )	r   r<   r=   r>   r?   r@   line_numberrB   rC   rY   s    r%   convert_instructionr]   g   I    HHHHEEHHHHMMKK	
 		
r(   rW      c           
          [        U R                  U R                  U R                  U R                  U R
                  U R                  U R                  U R                  5      $ rG   )	r   r<   r=   r>   r?   r@   rA   rB   rC   r\   s    r%   r]   r]   u   r^   r(   c           
          [        U R                  U R                  U R                  U R                  U R
                  U R                  U R                  S 5      $ rG   )r   r<   r=   r>   r?   r@   rA   rB   r\   s    r%   r]   r]      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)Nrd   r/   r#   s    r%   r&   _NotProvided.__repr__   s    r(   r/   N)r0   r1   r2   r3   r7   r&   r9   r/   r(   r%   rd   rd      s    # r(   rd   rW      namec                     U S;   $ )N)	LOAD_ATTRLOAD_GLOBALLOAD_SUPER_ATTRr/   rj   s    r%   inst_has_op_bitsrp      s    FFFr(   c                     U S:H  $ )Nrm   r/   ro   s    r%   rp   rp      s    }$$r(   c                     gr+   r/   ro   s    r%   rp   rp      s    r(   )r>   r?   r   r>   r?   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)r<   r=   r>   r?   r   )rp   RuntimeErrorrd   r,   r5   r   disopmap)rj   r>   r?   r   cnts        r%   create_instructionrx      s    0 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$ )Nr_   JUMP_FORWARDJUMP_ABSOLUTEr   sysversion_inforx   )r   insts     r%   create_jump_absoluter      s!     --8>oDd22r(   c                      U R                   S;   $ )N)rz   r{   )r=   r|   s    r%   is_jump_absoluter      s    =====r(   valcheckedc                 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r?   )r   rx   )r   r   s     r%   create_load_constr      s2     $$>(8&>>$l377r(   c                  T    [         R                  S:  a
  [        SSS9$ [        S5      $ )Nr_   COPYr   r>   DUP_TOPr}   r/   r(   r%   create_dup_topr      s(    
7"!&a00i((r(   nc                     U S::  a  / $ [         R                  S:  a&  [        U SS5       Vs/ s H  n[        SUS9PM     sn$ 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   r_   SWAPr      ROT_)TWOTHREEFOUR   ROT_N)r~   r   rangerx   )r   rY   s     r%   create_rot_nr      s     	Av	
7" <AAr?K?a"6q1?KKAv"6,DQU,K#KLMMwA.//	 Ls   A+inst_or_instsc                   ^ [        U [        5      (       a  U /mO[        U [        5      (       d   eU mS[        S[        4U4S jjnS[        SS4U4S j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.
idxr   c                 R   > TU    R                   c   eTU    R                   S-  S:H  $ Nr   r   r   instss    r%   inst_has_bit_set'add_push_null.<locals>.inst_has_bit_set  s/    Sz~~)))Sz~~!Q&&r(   Nc                 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(   rV   r   	PUSH_NULLrh   r   rm   r_   )
r,   r   listr5   r6   r~   r   rp   r=   rx   )r   r   r   r   s      @r%   add_push_nullr     s   " ---$1?-....'c 'd '# $  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 [        5      (       d   e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.
r_   rV   r   r   rm   r   r   )r,   r   r   r~   r   r=   r>   rx   )r   r   r   s      r%   add_push_null_call_function_exr   <  s     ---$1?-....
'!  G+"CzM)z~~))):>>A"JNNaNL
7"+K899 L $K01E9Lr(   nargs	push_nullc                    [         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),
        )
r_   r   rV   r   r   rh   PRECALLr   CALLCALL_FUNCTION)r~   r   appendrx   extendr   )r   r   outputrotss       r%   create_call_functionr   Z  s    R 7"MM,[9: # 0 0G ;519DMM,t,-g%MM,YEBC(U;<E:;;r(   
has_kwargsignore_314_kwargs_pushc                    [         R                  S:  a  / n[         R                  S:  a*  U (       d#  U(       d  UR                  [        S5      5        Sn U(       ad  UR                  [        S5      5        [         R                  S:  a  [	        U 5      S-   O[	        U 5      S-   nUR                  [        U5      5        UR                  [        S[	        U 5      S	95        U$ [        S[	        U 5      S	9/$ )
a  
Assumes that in 3.14+, if has_kwargs=False, there is NOT a NULL
on the TOS for the kwargs. This utility function will add a PUSH_NULL.

If the caller has already pushed a NULL for the kwargs, then set ignore_314_kwargs_push=True
so we don't push another NULL for the kwargs.
r_   rW      r   TrV   r   rW   CALL_FUNCTION_EXr   )r~   r   r   rx   r5   r   r   )r   r   r   r   r   s        r%   create_call_function_exr     s     7"'*MM,[9:JMM,[9:
 ##w. J!#_q( 
 MM,t,-();ZQR1s:GHHr(   c                     [         R                  S:  a  [        SU S9/$ [         R                  S:  a  [        SU S9[        SU S9/$ [        SU S9/$ )Nrh   r   r   r_   r   CALL_METHODr}   )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$ )Nrh   rl   r   )r>   r?   LOAD_METHODr   r}   ro   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$ )Nr_   BEFORE_WITH
SETUP_WITHr|   r}   )r   r=   s     r%   create_setup_withr     s!    !..'9]|Ff44r(   c                    [         R                  S:  a  [        SU S9/$ U S:X  a  / $ U S:X  a  [        S5      /$ U S:X  a  [        S5      [        S5      /$  [        S	U S-
  S9[        S
5      [        SSS9[        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$ )Nr_   r   r   r   r   ROT_TWOrW   	ROT_THREE
BUILD_LISTr   r   r   r   STORE_SUBSCRreverser   POP_TOPUNPACK_SEQUENCE)r~   r   rx   create_binary_subscrr   r   r   s    r%   create_swapr     s   
7""6q122Av		
a"9-..	
a";/1CI1NOO& 	<QU39%<3;'9%;'<3>*9%9% 
A	 	9% 	,!a%8 r(   r   r   storec                 t   [         R                  S:  a2  U(       a  [        S5      O	[        5       n[	        [        X5      5      U/$ [         R                  S:  a+  U(       a  SOSn[	        U 5      [	        U5      [        U5      /$ U(       a  SOSn[	        U 5      [	        U5      [        SSS	9[        U5      /$ )
zI
BINARY_SLICE and STORE_SLICE (if `set` is True) for all Python versions
r   r   rh   STORE_SLICEBINARY_SLICEBINARY_SUBSCRBUILD_SLICEr   r   )r~   r   rx   r   r   slice)r   r   r   subscr_inst	inst_names        r%   create_binary_slicer     s     7"27~.=Q=S 	 eE/0
 	
 
		W	$%*M	e$c"y)
 	
 ',N	e$c"}!4y)	
 	
r(   c                     [         R                  S:  a  [        SU S9/$ U S:X  a  [        S5      /$ / [        U 5      Q[	        5       P[        U S-   5      Q[        S5      Q$ )Nr_   r   r   r   r   r   )r~   r   rx   r   r   r\   s    r%   create_copyr     sw    
7""6q122Av"9-..	Q 
QU	 
Q	 r(   r   c                     / [        [        S[        S95      Q[        U [        R
                  S:  a  SOS-   5      Q[        SS5      Q[        S5      P$ )Nr   r   r_   r   r   Fr   )r   rx   printr   r~   r   r   )r   s    r%   create_print_on_stackr   +  sb    	),uE	F	U3#3#3w#>aAF	G 
a	' 	9%	 r(   valuec                 v    / [        [        S[        S95      Q[        SU S9P[        SS5      Q[        S5      P$ )Nr   r   r   Fr   )r   rx   r   r   )r   s    r%   create_print_valuer   5  sP    	),uE	F<6 
a	' 	9%	 r(   c                  T    [         R                  S:  a  [        S5      $ [        SSS9$ )Nr   r   	BINARY_OP   r   r}   r/   r(   r%   r   r   >  s(    
'!!/22kr22r(   c                 `    [         R                  S:  a  U S:X  a  [        S5      $ [        SU S9$ )Nr   r   r/   BUILD_TUPLEr   )r~   r   r   rx   r   s    r%   create_build_tupler   E  s.    
7"qAv $$m33r(   first_linenoc                    ^^^^^ [         R                  SS S:X  d   e/ mU mSmSmS[        S[        SS4U4S jjmS	[        S
[        SS4UUUU4S jjnS[        SS4UUU4S j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
Nr   )rW   
   r   byteno_deltalineno_deltar   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      i      )maxminr   )r   r   byte_offsetline_offset	linetables       r%   _update!linetable_writer.<locals>._updateY  s    a<1#4a\3!78KdCc$:;K!#{a'777'L'Lk+=>? a<1#4r(   
lineno_new
byteno_newc                 4   > UT-
  nUmT" UT5        U T-
  mU mg rG   r/   )r   r   r   r   bytenolinenor   s      r%   update linetable_writer.<locals>.updateb  s+    !F*l+!F*r(   total_bytesc                    > T" U T-
  T5        g rG   r/   )r  r   r   r   s    r%   r   linetable_writer.<locals>.endj  s    f$l3r(   )r~   r   r5   )r   r  r   r   r   r   r   r   s      @@@@@r%   linetable_writerr  K  s     BQ7***IFLF@c @ @ @3 C D  4 4 4 4 f!!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  p  s`    
 6M6	
RA!GA
a%	"	R	a a% Hr(   c                    ^^ [         R                  S:  d   e/ mU mS[        R                  S-  S[        SS4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
r_   rC   N	inst_sizer   c                    >^  T (       a  T R                   OS nS[        S[        SS 4UU 4S j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 )Ndeltasizer   c                   > 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  )r  r  other_varintsr   r   rC   s       r%   r   5linetable_311_writer.<locals>.update.<locals>._update  s   4}1}$}$} 24!((4!,,8!,,8!00<$$\D%81%<= ",,y/?/??!,,q0!0014%M $$\D%81%<=19$f]a/EaKE  u!56&A$$]1%56 'r(   r   r  )r   r5   )rC   r  r   r   r   r   r   s   `    r%   r  $linetable_311_writer.<locals>.update  s{    -6))DJ7s 7# 7$ 7 7@ ! )F2#a-a(Q	 a- L,r(   )r~   r   ru   	Positionsr5   )r   r  r   r   s     @@r%   linetable_311_writerr    sY     7***	,	-cmmd2 ,	-s ,	-t ,	- ,	-\ &  r(   c                   H    \ rS rSr% \\S'   \\S'   \\S'   \\S'   \\S'   Srg)	ExceptionTableEntryi  r   r   r   r   r   r/   N)r0   r1   r2   r3   r5   r4   r6   r9   r/   r(   r%   r  r    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  rY   s      r%   encode_exception_table_varintr"    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&    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'  rY   s     r%   check_exception_tabler)    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&  r6   r   r  StopIterationr)  )
r*  exntab_iterr'  r   lengthr   r   dlr   r   s
             r%   parse_exception_tabler1    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/  r0  s         r%   assemble_exception_tabler7    s    
 	A3EKK14DEA& 	U[[(1,	.v{;<	.u||q/@ABkkQ%++-	.r23  8Or(   instructionsfirstlinenoc                 j   / 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[        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     U" [        U5      5        [        U5      [        U5      4$ )	z)Do the opposite of dis.get_instructions()r_   r   EXTENDED_ARGr   r   r   rW   r   r   )r   r   )r~   r   r  	enumerater=   rC   instruction_sizer>   r   r<   r   r  rA   r!  r4  )r8  r9  codelnotabupdate_linenonum_extrY   r   r  jr>   _r   s                r%   assemblerE    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# <! /& &6k%B"s D+d..D	:((-aCKKcDj12	 ! 	CI;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r<   ru   r;  )rF  r@   r   s      r%   _get_instruction_by_offsetrI  ?  s;     1*%,,0@0@@!1*--  r(   c                 &   U  Vs0 s H  oR                   c  M  U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@   r<   ru   hasjabshasjrelrI  r?   r   )r8  r   jump_targetss      r%   virtualize_jumpsrN  K  sp     '3&2dkkTl   ;;#++%)C4\;;ODK 	s
   BB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 )Nr_   z+Cannot flip jump direction in Python < 3.11FORWARDBACKWARDz-Instruction is not a forward or backward jump)
r~   r   rt   r=   replaceAttributeErrorru   rv   r<   
_REL_JUMPS)rO  s    r%   flip_jump_directionrV  Y  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<  rH  )r8  r   r   r@   s       r%   _get_instruction_frontrX  f  sP    
 F=\,7>>#BRBRR!,/FM 
 Mr(   c           	      4   [        [        R                  5      R                  [        [        R                  5      5      nU  H  nUR
                  U;   d  M  UR
                  [        R                  ;  d  M5  UR                  b$  UR                  R                  b  UR                  c   e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  UR                  c   e[        XUR                     5      nUR
                  [        R                  ;   aL  [        R                  S:  a-  UR                  c   e[!        UR                  S-  5      Ul        Ox[        S5      eUR                  b  UR                  c   e[%        [!        UR                  UR                  -
  ['        U5      -
  5      5      Ul        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 movedNr_   z*Got negative jump offset for Python < 3.11rQ  rR  r   z+Python 3.11+ should not have absolute jumpszto )setru   rK  unionrL  r<   r   r@   r~   r   rt   r=   rV  update_offsetsr   rX  r5   r>   absr>  r?   rE   )r8  jumpsr   indexofr   s        r%   devirtualize_jumpsr`  t  s   ""3s{{#34E ;;%{{#++-KK+**6/0 ;;%%3'''1*+WXX DKK/+D1 0 ''72zT[[7P+D1# ( < ,'G ;;%;;***+L$++:NOF{{ckk)##g- "==444"6==1#45DH&'TUU }}0T[[5LLL36Ft6LLM Q --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 S[        [        [        4   4UUU	U
4S jjnU" 5       u  pVU H  nUR                  c   eUR                  UR                  :  a%  U" 5       u  pVUR                  UR                  :  a  M%  UR                  UR                  :  d  Mm  [        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   r   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   rI  r   r   r   r   )r5  
end_offset
inst_entryend_offset_idxexn_tab_iterrF  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)r1  r	   r5   r@   sortedkeysr,  tupler  r   r   r   copyrD   r-  )ra  r8  exn_tabr   ri  r5  re  rf  rg  rF  rh  s          @@@@r%   virtualize_exception_tablerp    s    $M2G?KL|td3,d2|LN^((*+GN=L 	%e/1GGH 	% 	%, !F D;;***++		)$(F! ++		){{ekk)%)YYz%:" !; MF  s$   %D#A8D D 9D 
D+*D+c           	        ^^^^ 0 m[        U 5      nU  GH  nUR                  (       d  M  [        XUR                  R                     5      R                  nUc   e[        [        UR                  R                  R                  5      [        UR                  R                  5      -   S-
  nUc   e[        XUR                  R                     5      R                  nUc   e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SUUUU4S j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   )ry  exn_dictro  	key_stacknextis    r%   r{  $compute_exception_table.<locals>.pop  se    
 --/CA'CFE(:CFSXc]S A
	  r(   r   r   )r   N)r   rD   rX  r   r@   r	   r5   r   r>  r   r   r   rk  rl  r   r   r  r)  )r8  r_  r   r   r   r   ry  r   keys_sortedr{  leftr|  ro  r}  r~  s              @@@@r%   compute_exception_tabler    sW    >@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- < .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(   r_  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   r{  r   )r'  r_  entry_stackr5  ry  s        r%   !check_inst_exn_tab_entries_nestedr  "  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$ rs  r/   rt  s    r%   rv  2propagate_inst_exn_table_entries.<locals>.<lambda>F  s    adQqTE]r(   rx  r   N)	r   rD   r   r   rk  rl  r  r   rn  )r8  r_  entriesr   ry  sorted_entriesr5  rY   s           r%    propagate_inst_exn_table_entriesr  5  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.
r_   N)r   rZ  r=  rD   r~   r   rI   addr   r   r   )r8  r_  exn_tab_entry_setrY   r   r5  s         r%    check_inst_exn_tab_entries_validr  P  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 rG   rH  )r8  rY   s     r%   strip_extended_argsr  h  s,    ".O,Q((c>N>N2Nq,OLOOs   !77old_inst	new_instsc                    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   )
rD   r   r!  rn  rC   r<   r=   r>   r?   r   )r  r  r   s      r%   overwrite_instructionr  q  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 itr_   rl   r   )r   r   )r~   r   r=   ru   rv   r<   )r8  rewritesr   s      r%   remove_load_call_methodr    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   rh   rQ  POP_JUMP_FORWARD_IF_TRUEPOP_JUMP_BACKWARD_IF_TRUEr|   POP_JUMP_IF_TRUEr   r   )r=   rx   r5   r>   r?   r~   r   r   r   r  r   )r8  r  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                 p   / nU  GH)  nUR                  U5        UR                  S;   d  M'  [        R                  S:  a  UR                  S:X  a  [	        5       nO%[        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        GM,     XS S & g )N)r   r   r   r   SLICESUBSCRr   r   )r   r=   r~   r   r   rx   rS  rD   r   rn  rC   ru   rv   r<   r>   r?   )r8  r  r   r   s       r%   remove_binary_store_slicer    s    I;;997*t{{n/L2401D1DWh1WX!!d&8&8&<&<&D)4""&(,		$2D2D(EK%$(NNK!))M2DK'DKDHDK[)# $  Or(   )	LOAD_FASTr  )LOAD_FAST_BORROWr  )
STORE_FASTr  )r  r  )LOAD_FAST_LOAD_FAST!LOAD_FAST_BORROW_LOAD_FAST_BORROW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   )r=   FUSED_INSTSr?   rx   r   r  r   )r8  r  r   inst0inst1argval0argval1r  s           r%   remove_fused_load_storer    s    I;;+%&t{{3LE#{{G #59"59M 24GHT"   Or(   c                     / nU  Hh  nSUR                   ;   aD  [        SSS9[        UR                   UR                  S9/nUR                  [	        X#5      5        MW  UR                  U5        Mj     XS S & g )NRETURNNOPGRAPH_BREAK_IF_LEAFr   )r=   rx   r?   r   r  r   )r8  r  r   r  s       r%   $add_graph_break_if_leaf_instructionsr    sn    It{{""51FG"4;;t{{CM 24GHT"   Or(   c                    / n[         R                  " U 5       GH1  u  p#UR                  S:X  Ga	  UR                  S:X  a  UR                  R	                  S5      (       a  [        [        U 5      5       H  nX   R                  UL a	  X0U   l        X   R                  (       d  M2  X   R                  R                  UL a  X0U   R                  l	        X   R                  R                  UL a  X0U   R                  l
        X   R                  R                  UL d  M  X0U   R                  l        M     GM   UR                  U5        GM4     UR                  U S   5        XS S & g )Nr  r  r  r   )	itertoolspairwiser=   r?   
startswithr   r!  r   rD   r   r   r   )r8  r  r   	next_instrY   s        r%   'remove_graph_break_if_leaf_instructionsr    s,   I$--l;KK5 44  ++H55 3|,-?))T1-6O*?000 $44::dB>GQ55;#4488D@<EQ559#44;;tC?HQ55< . 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/   Nrm   superr   r   rh   r   r_   r   r   	__class__
LOAD_DEREFr   r  r   )co_cellvarsco_freevarsr!  co_varnamesr=  r   r=   r?   r>   r~   r   rx   )	r?  r8  cell_and_freer   r   r   r~  	first_var	call_insts	            r%   explicit_superr    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S[         SS4U4S j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 opsr   r   Nc                    > [        U 5       H?  nT(       d  M  TS   R                  [        R                  :X  d  M/  TR	                  5         MA     g )Nr   )r   r<   ru   r;  r{  )r   rD  r   s     r%   maybe_pop_n&fix_extended_args.<locals>.maybe_pop_n7  s6    qAv&*++s/?/??

 r(   r   i rW   r;     r      r  i  r   r   r   )r5   r<   ru   r;  r>   r   rx   r!  )r8  r  r   addedr   s       @r%   fix_extended_argsr  3  sm    "Fs t 
 ;;#***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(   r   c                     SS K n[        R                  S:  a=  SUR                  R                  R
                  R                  U R                     S-   -  $ g)Nr   r_   r   r   )torchr~   r   _C_dynamo
eval_framepy_opcode_cachesr<   )r   r  s     r%   r>  r>  T  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>  r8  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%   r\  r\  c  s&    F"4(( r(   argsc            	      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>n  s     (Cqb6Cs   zbytes mismatch

)r   r   mapr!  r   zipr5   join)r  indexar  r>   results         r%   debug_bytesr  k  s    #c#tn%&E 7
t*$'R$r($;<$;DACK$;<
=>>C 	(C((>   		& 111 =	s   B ("B&c                     [        U S SS9u  p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 rG   r/   )r  ys     r%   rv  debug_checks.<locals>.<lambda>y  s    tr(   T)safeN)transform_code_objectco_coder  )r?  doderD  s      r%   debug_checksr  w  s=    #D*;$GGD<<4<<'PT\\4<<)PP'r(   code_optionsc                 ~    [        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!  )r  r   rY   vs       r%   get_const_indexr    sM    ,{34
 8H 5 #'|K()A--r(   varname_from_oparg.c                 
  ^ ^^	^
^^ [        TS   5       VVs0 s H  u  p4XC_M	     snnmS[        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
S[        4U
U 4S	 j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M[  U" T T
   R                  5      T T
   l        GM{  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M1  GM4  T T
   R(                  [,        ;   a/  U" 5       (       a   U" T T
   R                  5      T T
   l        GMw  GMz  T T
   R(                  [.        ;   a,  U" 5       (       a  T	T T
   R                     T T
   l        GM  GM  T T
   R(                  [0        ;   d  GM  T T
   R                  b  GM  [3        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_namesrj   r   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!  )rj   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r_   r  r  r  c                  .   > TT    R                   [        L$ rG   )r?   rd   )rY   r8  s   r%   should_compute_arg$fix_vars.<locals>.should_compute_arg  s    ?))==r(   rm   r   r   rl   rh   rn   rV   c              3   D   >#    U  H  nUT;   a  TU   OTU   v   M     g 7frG   r/   )r  rj   	freenamesvarnamess     r%   r  fix_vars.<locals>.<genexpr>  s,      2D #'("2	$G2s    r   r      )r=  r7   r5   r~   r   callabler  count
IndexErrorr   r!  r6   r=   r?   rd   r>   r	   r  r,   rm  r<   	HAS_LOCALHAS_NAMEHAS_FREE	HAS_CONSTr  )r8  r  r   r   rj   r  allnamesr	  	arg_tupler  rY   r  r  s   ``       @@@@r%   fix_varsr    sw    )2,z2J(KL(K93TY(KLES S   '!!)))/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|$%	>D 	> 	> ?!!]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/   U)UUU)U-U2
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)rm   rl   rn   )	r?   rd   r<   r  r  r  r  r=   r>   )r8  r   s     r%   clear_instruction_argsr    s^     KK|+y(;;(*;;(*;;)+#RRDH r(   c                  R   S/n U R                  S5        U R                  / SQ5        [        R                  S:  a  U R                  S5        U R                  S5        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_namer_   co_qualnameco_firstlinenoco_linetableco_exceptiontabler  r  )r   r   r~   r   )rl  s    r%   get_code_keysr)    s    
 ?DKK$%KK	
 7"M"KK !KK
7"'(KK	
 Kr(   transformationsr   r  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      n[        XcU5      u  pX4$ s  snf )Nr  r   )r)  getattrr!  cleaned_instructionsr   clean_and_assemble_instructions)
r?  r*  r  rl  kr  r8  tracer_outputrD  bytecodes
             r%   r  r  9  s|     ?D156Awt''L6|M*+|L/IIII'3L%#L?M1,lSKA"" 7s   A,rl  c                    [        U 5        [        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  pxXS'   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   r_   )r   Tr&  r'  r  r!  r  r(  )r  r  r!  r~   r   typesCodeType_varname_from_opargr  r\  r`  r6   r  r   rE  r   rZ  rl  r7  r  )	r8  rl  r  r   r/  tmp_codedirtyr1  r@  s	            r%   r.  r.  M  su    ,L9$\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I3 $C2 *Is   E*E/
constsc                 `    U  H(  nUR                   S:X  d  M  XR                     Ul        M*     g )NKW_NAMES)r=   r>   r?   )r8  r8  r   s      r%   populate_kw_names_argvalr;  t  s&    ;;*$ *DK r(   c                 .    [        X5      n[        U5      $ rG   )_cached_cleaned_instructions_clone_instructionsr?  r  r8  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 rG   )r   r<   r=   r>   r?   r@   rA   rB   rC   r   rD   rE   dictr  r   r   r   r   r   )r8  rY   copiedremapr5  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        [        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*                  (       a  [-        U5        [        R                  S:  a  [/        U5        [1        U5        U$ )Nr_   rh   rV   )r   r  r]   ru   get_instructionsr   r  r~   r   r;  r  rp  r(  rN  r  r  r  r  r  r  r   &debug_force_graph_break_on_leaf_returnr  r\  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880>
7"|$<(r(   	with_uuidc                     U  S[        [        5       3nU(       a+  US[        R                  " 5        3R	                  SS5      -  nU$ )NrD  -)r%  _unique_id_counteruuiduuid4rS  )rj   rG  rets      r%   	unique_idrN    sE    F!D+,-
.C4::<.!))#s33Jr(   c                 *    SnU R                   U-  S:  $ )N    r   )r"  )r?  co_generators     r%   is_generatorrR    s    LMML(A--r(   fnvarname_mapnoreturnnoprefixc                    [        U R                  5      n[        U5        U(       a-  [        U5       H  u  pVUR                  S:X  d  M  XES-   S n  O   U H?  nSUl        SUl        U(       d  M  UR                  U;   d  M,  XR                     Ul        MA     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   Nrh   RETURN_CONSTr   RETURN_VALUEr   r   r  )r-  __code__r  r=  r=   rA   rC   r?   r~   r   ru   rv   r<   r   rx   r!  r{  r>   rd   r   r   )
rS  rT  rU  rV  r   rY   r   r  returns	jump_insts
             r%   bytecode_from_templater^    s/   N !-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)FrG   )NTT)rU   rn  dataclassesru   	functoolsr  r~   r3  rK  collections.abcr   r   r   r   r   typingr   r	   r
   r   r    r   bytecode_analysisr   r   r   r   utilsr   output_graphr   	dataclassr   r   r   r]   rd   r7   r6   rp   r5   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rm  r  r  r  r  r  r"  r&  r)  r4  r1  r7  rE  rA  rI  rN  rZ  rL  rU  rV  rX  r`  rp  r  r  r  r  r  r  r  r  r  r  r  r  r  r4  r  r  r>  r  r\  r  r  haslocalr  hasnamer  hasfreer  hasconstr  r  r  r  	lru_cacher)  r  r.  r;  r-  r>  r=  r  rJ  rN  rR  r^  r/   r(   r%   <module>rm     s
     
   
   K K < <   $ 0 T"
 
 #
6 T") ) #)> w

s 

; 

 	 

s 

; 



s 

; 

 
 wGs Gt G 	 %s %t %
s t  ($('
' 
#' SM	'
 [!' 'V3 3 3
>[ >T >83 8 8 8) )0C 0D- 0.8d;&7788	+8vd;&778	+<4< 4< 4<k9J 4<p GLII!%I?CI	+ID:c :d;&7 ::S :[ :5k 5k 5
+3 +4, +^ =B
C=
'}
59
	+
>3 4, ( k): c d;&7 3k 34# 4+ 4""""
49hSz4/0(C5$;2GGH""JS T#Y  w:!:!	tCy(CMMD$8##>#DEE	F:!z T"  #S T#Y  
hsm 
 


t$78 

T 

% D1D,E ,$':";  $#&4, #&3 #&5PUCV #&L	k)*	47	k	P8K#8 PT P 

,[ 
,T 
,k):   21T+%6 214 21j))(,[(9)	)XJ{#J	
JZ 	$	% 04[#5E0F 	 &=43D = =6C43D C C0Pd;&7 PD P&&&*;&7&	+&.${*; [@Q  d;&7  D  > D,=  $  0 6)Q97	 ${*;    & tK7H  T   ${:K  PT  6# #tK7H #T #LD$5 # B; 3 ) 5 )$ ))+!6 )4 )	2u 	2 	2Qu~~ Q$ Q 	s{{s{{		.$sCx. 	.s 	.s 	. 8<l*{#l*sCx.l* !#s(!34l* 
	l*^k): t " !tCy ! !R #
..#	k	DcN+X6J-KK#
 # 5>>8$899:#($J{#$J+/9$JDHcN$J
4enn,-$JN+8K+@ +# +RV +-u~~ -T -d;FW -"h{&; "[@Q "J ',
.. $k > __& C D S .u~~ .$ . 04	fcf'#s(+,f f 	f
 
+fr(   