
    3j
                       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  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!\RD                  S:  a  S\RB                  S\!4S jr#O7\RD                  S:  a  S\RB                  S\!4S jr#OS\RB                  S\!4S jr# " S S5      r$\RD                  S:  a
  S\%S\&4S jr'O#\RD                  S:  a
  S\%S\&4S jr'O	S\%S\&4S jr'S\$SS.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\!4S) jr/S*\(S\-\!   4S+ jr0S,\!\-\!   -  S\-\!   4S- jr1S,\!\-\!   -  S\-\!   4S. jr2S/\(S0\&S\-\!   4S1 jr3 SS2\&S0\&S3\&S\-\!   4S4 jjr4S/\(S\-\!   4S5 jr5S\%S\!4S6 jr6S!\!S\!4S7 jr7S*\(S\-\!   4S8 jr8 SS9\(S-  S:\(S-  S;\&S\-\!   4S< jjr9S\(S\-\!   4S= jr:S>\(S\-\!   4S? jr;S@\S\-\!   4SA jr<S\!4SB jr=S*\(S\!4SC jr>SD\(S\?\-\(   \
\(\(/S4   \
\(/S4   4   4SE jr@S*\(S\-\(   4SF jrA\RD                  S:  a%  SD\(S\?\-\(   \
\R                  S-  \(/S4   4   4SG jrC\R>                  " S
S9 " SH SI5      5       rDS*\(S\-\(   4SJ jrESK\\(   S\(4SL jrFSM\-\D   SS4SN jrGSO\HS\-\D   4SP jrISM\-\D   S\H4SQ jrJSR\-\!   SS\(S\?\H\H4   4ST jrKSU\L\(\!4   SV\(S\!S-  4SW jrMSR\\!   SS4SX jrN\O" \R                  5      rQSY\!SS4SZ jrRSR\-\!   S[\(S\!4S\ jrSSR\-\!   SS4S] jrTS^\HSR\-\!   SS4S_ jrUSR\-\!   S\-\D   4S` jrVSM\-\    Sa\L\!\(4   SS4Sb jrWSR\-\!   SS4Sc jrXSR\-\!   SS4Sd jrYSR\-\!   SS4Se jrZSf\!Sg\-\!   S\-\!   4Sh jr[SR\-\!   S\-\!   4Si jr\SR\-\!   SS4Sj jr]SR\-\!   SS4Sk jr^SlSmSnSoSp.r_SR\-\!   SS4Sq jr`SR\-\!   SS4Sr jraSR\-\!   SS4Ss jrbSt\R                  SR\-\!   SS4Su jrdSR\-\!   S\(4Sv jreSw\!S\(4Sx jrfSR\\!   SS4Sy jrgSR\\!   SS4Sz jrhS{\HS\%4S| jriSt\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 SSR\-\!   S~\L\%\4   S\
S\4   S-  SS4S jjrtSR\-\!   SS4S jru\R                  S\-\%   4S j5       rw SSt\R                  S\
\-\!   \L\%\4   /\S   4   S\&S\?\R                  \S   4   4S jjrxSR\-\!   S\-\%   S~\L\%\4   S\?\-\!   \R                  4   4S jrySR\\!   S\SS4S jrzSSt\R                  S\&S\-\!   4S jjr{SR\\!   S\-\!   4S jr|\R                   SSt\R                  S\&S\\!   4S jj5       r}\R                  " 5       rSS\%S\&S\%4S jjrSt\R                  S\&4S jr   SS\
S\4   S\\\4   S-  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   )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/wildlama/miniconda3/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'   Sr	\S-  \S'   Sr
\S-  \S	'   S
r\\S'   Sr\S   \S'   Sr\S    \S'   Sr\S-  \S'   Sr\S-  \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opnameNargargval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  $ rF   rG   r#   rK   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 rF   )r@   rB   rM   s     r$   copy_positionsInstruction.copy_positions`   s     ,,r'   )rB   r@   )rK   r   r   N)r/   r0   r1   r2   __doc__r4   r3   r6   r   r?   r@   rA   r5   rB   r
   r   rC   r   rD   rI   r7   r,   r!   rR   r8   r.   r'   r$   r   r   E   s    .KK	tOKFC$J"Kt" ND +/Ix(/&*FH]#*37M)D07GS4Z# %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      $ rF   )	r   r;   r<   r=   r>   r?   line_numberrA   rB   rX   s    r$   convert_instructionr\   g   I    HHHHEEHHHHMMKK	
 		
r'   rV      c           
          [        U R                  U R                  U R                  U R                  U R
                  U R                  U R                  U R                  5      $ rF   )	r   r;   r<   r=   r>   r?   r@   rA   rB   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      $ rF   )r   r;   r<   r=   r>   r?   r@   rA   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)Nrc   r.   r"   s    r$   r%   _NotProvided.__repr__   s    r'   r.   N)r/   r0   r1   r2   r6   r%   r8   r.   r'   r$   rc   rc      s    # r'   rc   rV      namec                     U S;   $ )N)	LOAD_ATTRLOAD_GLOBALLOAD_SUPER_ATTRr.   ri   s    r$   inst_has_op_bitsro      s    FFFr'   c                     U S:H  $ )Nrl   r.   rn   s    r$   ro   ro      s    }$$r'   c                     gr*   r.   rn   s    r$   ro   ro      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   )ro   RuntimeErrorrc   r+   r4   r   disopmap)ri   r=   r>   r   cnts        r$   create_instructionrw      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_inforw   )r   insts     r$   create_jump_absoluter      s!     --8>oDd22r'   c                      U R                   S;   $ )N)ry   rz   )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   rw   )r   r   s     r$   create_load_constr      s2     $$>(8&>>$l377r'   c                  8    SSK Jn   [        SU S9[        S5      /$ )a  
Create instructions that trigger the bytecode debugger to stop.

Usage:
    codegen.extend_output(create_breakpoint())

When the bytecode debugger is active, execution will pause at this point.
At runtime, these instructions have no effect on program state.
r   BREAKPOINT_MARKERr   r   POP_TOP)bytecode_debuggerr   rw   r   s    r$   create_breakpointr      s&     5 	<0AB9% r'   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~   rangerw   )r   rX   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'   rU   r   	PUSH_NULLrg   r   rl   r^   )
r+   r   listr4   r5   r}   r~   ro   r<   rw   )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^   rU   r   r   rl   r   r   )r+   r   r   r}   r~   r<   r=   rw   )r   r   r   s      r$   add_push_null_call_function_exr   N  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   rU   r   r   rg   PRECALLr   CALLCALL_FUNCTION)r}   r~   appendrw   extendr   )r   r   outputrotss       r$   create_call_functionr   l  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^   rV      r   TrU   r   rV   CALL_FUNCTION_EXr   )r}   r~   r   rw   r4   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/$ )Nrg   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$ )Nrg   rk   r   )r=   r>   LOAD_METHODr   r|   rn   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_TWOrV   	ROT_THREE
BUILD_LISTr   r   r   r   STORE_SUBSCRreverser   r   UNPACK_SEQUENCE)r}   r~   rw   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   rg   STORE_SLICEBINARY_SLICEBINARY_SUBSCRBUILD_SLICEr   r   )r}   r~   rw   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~   rw   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   rw   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   rw   r   r   )r   s    r$   create_print_valuer   G  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   P  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   rw   r   s    r$   create_build_tupler   W  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   )rV   
   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>._updatek  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 rF   r.   )r   r  r   r   bytenolinenor   s      r$   update linetable_writer.<locals>.updatet  s+    !F*l+!F*r'   total_bytesc                    > T" U T-
  T5        g rF   r.   )r  r   r  r   s    r$   r   linetable_writer.<locals>.end|  s    f$l3r'   )r}   r~   r4   )r   r  r   r   r  r  r   r   s      @@@@@r$   linetable_writerr
  ]  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    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^   rB   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   rB   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  r4   )rB   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~   rt   	Positionsr4   )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)r/   r0   r1   r2   r4   r3   r5   r8   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  rX   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+  rX   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*  r5   r   r#  StopIterationr-  )
r.  exntab_iterr+  r   lengthr   r   dlr   r   s
             r$   parse_exception_tabler5    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_entryr3  r4  s         r$   assemble_exception_tabler;    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   rV   r   r   )r   r   )r}   r~   r!  	enumerater<   rB   instruction_sizer=   r   r;   r   r
  r@   r%  r8  )r<  r=  codelnotabupdate_linenonum_extrX   r   r  jr=   _r   s                r$   assemblerI  +  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;   rt   r?  )rJ  r?   r   s      r$   _get_instruction_by_offsetrM  Q  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;   rt   hasjabshasjrelrM  r>   r   )r<  r   jump_targetss      r$   virtualize_jumpsrR  ]  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~   rs   r<   replaceAttributeErrorrt   ru   r;   
_REL_JUMPS)rS  s    r$   flip_jump_directionrZ  k  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@  rL  )r<  r   r   r?   s       r$   _get_instruction_frontr\  x  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.11rU  rV  r   z+Python 3.11+ should not have absolute jumpszto )setrt   rO  unionrP  r;   r   r?   r}   r~   rs   r<   rZ  update_offsetsr   r\  r4   r=   absrB  r>   rD   )r<  jumpsr   indexofr   s        r$   devirtualize_jumpsrd    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   rM  r   r   r   r   )r9  
end_offset
inst_entryend_offset_idxexn_tab_iterrJ  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)r5  r	   r4   r?   sortedkeysr0  tupler#  r   r   r   copyrC   r1  )re  r<  exn_tabr   rm  r9  ri  rj  rk  rJ  rl  s          @@@@r$   virtualize_exception_tablert    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   )r}  exn_dictrs  	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   rC   r\  r   r?   r	   r4   r   rB  r   r   r   ro  rp  r   r   r#  r-  )r<  rc  r   r   r   r   r}  r   keys_sortedr  leftr  rs  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'   rc  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+  rc  entry_stackr9  r}  s        r$   !check_inst_exn_tab_entries_nestedr  4  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$ rw  r.   rx  s    r$   rz  2propagate_inst_exn_table_entries.<locals>.<lambda>X  s    adQqTE]r'   r|  r   N)	r   rC   r   r   ro  rp  r  r   rr  )r<  rc  entriesr   r}  sorted_entriesr9  rX   s           r$    propagate_inst_exn_table_entriesr  G  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   r^  rA  rC   r}   r~   rH   addr   r   r   )r<  rc  exn_tab_entry_setrX   r   r9  s         r$    check_inst_exn_tab_entries_validr  b  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 rF   rL  )r<  rX   s     r$   strip_extended_argsr  z  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   )
rC   r   r%  rr  rB   r;   r<   r=   r>   r   )r  r  r   s      r$   overwrite_instructionr    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^   rk   r   )r   r   )r}   r~   r<   rt   ru   r;   )r<  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   rg   rU  POP_JUMP_FORWARD_IF_TRUEPOP_JUMP_BACKWARD_IF_TRUEr{   POP_JUMP_IF_TRUEr   r   )r<   rw   r4   r=   r>   r}   r~   r   r   r  r   )r<  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   rw   rW  rC   r   rr  rB   rt   ru   r;   r=   r>   )r<  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>   rw   r   r  r   )r<  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<   rw   r>   r   r  r   )r<  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   rC   r   r   r   )r<  r  r   	next_instrX   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'   rC  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.   Nrl   superr   r   rg   r   r^   r   r   	__class__
LOAD_DEREFr   r  r   )co_cellvarsco_freevarsr%  co_varnamesrA  r   r<   r>   r=   r}   r~   rw   )	rC  r<  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;   rt   r?  r  )r   rH  r   s     r$   maybe_pop_n&fix_extended_args.<locals>.maybe_pop_nI  s6    qAv&*++s/?/??

 r'   r   i rV   r?     r      r  i  r   r   r   )r4   r;   rt   r?  r=   r   rw   r%  )r<  r  r   addedr   s       @r$   fix_extended_argsr  E  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$   rB  rB  f  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?   rB  r<  r?   r   s      r$   check_offsetsr  n  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`  u  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>  s     (Cqb6Cs   zbytes mismatch

)r   r   mapr%  r   zipr4   join)r  indexar  r=   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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 rF   r.   )r  ys     r$   rz  debug_checks.<locals>.<lambda>  s    tr'   T)safeN)transform_code_objectco_coder  )rC  doderH  s      r$   debug_checksr    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   )rA  r%  )r  r   rX   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_namesri   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%  )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r^   r  r  r  c                  .   > TT    R                   [        L$ rF   )r>   rc   )rX   r<  s   r$   should_compute_arg$fix_vars.<locals>.should_compute_arg  s    ?))==r'   rl   r   r   rk   rg   rm   rU   c              3   D   >#    U  H  nUT;   a  TU   OTU   v   M     g 7frF   r.   )r  ri   	freenamesvarnamess     r$   r  fix_vars.<locals>.<genexpr>  s,      2D #'("2	$G2s    r   r      )rA  r6   r4   r}   r~   callabler  count
IndexErrorr   r%  r5   r<   r>   rc   r=   r	   r  r+   rq  r;   	HAS_LOCALHAS_NAMEHAS_FREE	HAS_CONSTr  )r<  r  r  r   ri   r
  allnamesr  	arg_tupler  rX   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)rl   rk   rm   )	r>   rc   r;   r  r  r  r  r<   r=   )r<  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~   )rp  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)
rC  r.  r  rp  kr  r<  tracer_outputrH  bytecodes
             r$   r  r  K  s|     ?D156Awt''L6|M*+|L/IIII'3L%#L?M1,lSKA"" 7s   A,rp  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`  rd  r5   r  r   rI  r   r^  rp  r;  r  )	r<  rp  r  r  r3  tmp_codedirtyr5  rD  s	            r$   r2  r2  _  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>   )r<  r<  r   s      r$   populate_kw_names_argvalr?    s&    ;;*$ *DK r'   c                 .    [        X5      n[        U5      $ rF   )_cached_cleaned_instructions_clone_instructionsrC  r  r<  s      r$   r1  r1    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 rF   )r   r;   r<   r=   r>   r?   r@   rA   rB   r   rC   rD   dictr  r   r   r   r   r   )r<  rX   copiedremapr9  s        r$   rB  rB    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^   rg   rU   )r   r  r\   rt   get_instructionsr   r  r}   r~   r?  r  rt  r,  rR  r  r  r  r  r  r  r   &debug_force_graph_break_on_leaf_returnr  r`  rd  rC  s      r$   rA  rA    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$ )NrH  -)r)  _unique_id_counteruuiduuid4rW  )ri   rK  rets      r$   	unique_idrR    sE    F!D+,-
.C4::<.!))#s33Jr'   c                 *    SnU R                   U-  S:  $ )N    r   )r&  )rC  co_generators     r$   is_generatorrV    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   Nrg   RETURN_CONSTr   RETURN_VALUEr   r   r  )r1  __code__r  rA  r<   r@   rB   r>   r}   r~   rt   ru   r;   r   rw   r%  r  r=   rc   r   r   )
rW  rX  rY  rZ  r   rX   r   r  returns	jump_insts
             r$   bytecode_from_templaterb    s1   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)FrF   )NTT)rT   rr  dataclassesrt   	functoolsr  r}   r7  rO  collections.abcr   r   r   r   r   typingr   r	   r
   r    r   bytecode_analysisr   r   r   r   utilsr   output_graphr   	dataclassr   r   r~   r\   rc   r6   r5   ro   r4   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rq  r
  r  r   r!  r#  r&  r*  r-  r8  r5  r;  rI  rE  rM  rR  r^  rP  rY  rZ  r\  rd  rt  r  r  r  r  r  r  r  r  r  r  r  r  r  r8  r  r  rB  r  r`  r  r  haslocalr  hasnamer  hasfreer  hasconstr  r  r  r  	lru_cacher-  r  r2  r?  r1  rB  rA  r  rN  rR  rV  rb  r.   r'   r$   <module>rq     s
     
   
   K K 5 5   $ 0 T"
 
 #
6 T") ) #)> w

s 

; 

 	 

s 

; 



s 

; 

 
 wGs Gt G 	 %s %t %
s t  %!%'
' 
t' $J	'
 $' 'V3 3 3
>[ >T >83 8 8 84, $) )0C 0D- 0.8k!228	+8vk!22	+<4< 4< 4<k9J 4<p GLII!%I?CI	+ID:c :d;&7 ::S :[ :5k 5k 5
+3 +4, +^ 7<
:
$J
/3
	+
>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	4	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 	. 59l*{#l*sCx.l* !c*T1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~~ .$ . -1	gcgc"T)g g 	g
 
+gr'   