blob: 1fc63613b42c6fe3af1abd8afaf1b99fd75a6657 [file] [log] [blame]
Guido van Rossumb62b6d11997-11-18 15:10:53 +00001\section{Standard module \sectcode{dis}} % If implemented in Python
2\stmodindex{dis}
3
4\label{module-dis}
5
6The \code{dis} module supports the analysis of Python byte code by
7disassembling it. Since there is no Python assembler, this module
8defines the Python assembly language. The Python byte code which
9this module takes as an input is defined in the file
Fred Drake456035f1997-12-03 04:06:57 +000010\file{Include/opcode.h} and used by the compiler and the interpreter.
Guido van Rossumb62b6d11997-11-18 15:10:53 +000011
12Example: Given the function myfunc
13
14\bcode\begin{verbatim}
15def myfunc(alist):
16 return len(alist)
17\end{verbatim}\ecode
18
Fred Drake456035f1997-12-03 04:06:57 +000019the following command can be used to get the disassembly of \code{myfunc()}:
Guido van Rossumb62b6d11997-11-18 15:10:53 +000020
21\begin{verbatim}
22>>> dis.dis(myfunc)
23 0 SET_LINENO 1
24
25 3 SET_LINENO 2
26 6 LOAD_GLOBAL 0 (len)
27 9 LOAD_FAST 0 (alist)
28 12 CALL_FUNCTION 1
29 15 RETURN_VALUE
30 16 LOAD_CONST 0 (None)
31 19 RETURN_VALUE
32\end{verbatim}
33
34The \code{dis} module defines the following functions:
35
36\renewcommand{\indexsubitem}{(in module dis)}
37
38% ---- 3.2. ----
39% For each function, use a ``funcdesc'' block. This has exactly two
40% parameters (each parameters is contained in a set of curly braces):
41% the first parameter is the function name (this automatically
42% generates an index entry); the second parameter is the function's
43% argument list. If there are no arguments, use an empty pair of
44% curly braces. If there is more than one argument, separate the
45% arguments with backslash-comma. Optional parts of the parameter
46% list are contained in \optional{...} (this generates a set of square
47% brackets around its parameter). Arguments are automatically set in
48% italics in the parameter list. Each argument should be mentioned at
49% least once in the description; each usage (even inside \code{...})
50% should be enclosed in \var{...}.
51
52\begin{funcdesc}{dis}{\optional{bytesource}}
53Disassemble the \var{bytesource} object. \var{bytesource} can denote
54either a class, a method, a function, or a code object. For a class,
55it disassembles all methods. For a single code sequence, it prints
56one line per byte code instruction. If no object is provided, it
57disassembles the last traceback.
58\end{funcdesc}
59
60\begin{funcdesc}{distb}{\optional{tb}}
61Disassembles the top-of-stack function of a traceback, using the last
62traceback if none was passed. The instruction causing the exception
63is indicated.
64\end{funcdesc}
65
66\begin{funcdesc}{disassemble}{code\optional{\, lasti}}
67Disassembles a code object, indicating the last instruction if \var{lasti}
68was provided. The output is divided in the following columns:
69\begin{itemize}
70\item the current instruction, indicated as \code{-->},
71\item a labelled instruction, indicated with \code{>>},
72\item the address of the instruction,
73\item the operation code name,
74\item operation parameters, and
75\item interpretation of the parameters in parentheses.
76\end{itemize}
77The parameter interpretation recognizes local and global
78variable names, constant values, branch targets, and compare
79operators.
80\end{funcdesc}
81
82\begin{funcdesc}{disco}{code\optional{\, lasti}}
83A synonym for disassemble. It is more convenient to type, and kept
84for compatibility with earlier Python releases.
85\end{funcdesc}
86
87\begin{datadesc}{opname}
88Sequence of a operation names, indexable using the byte code.
89\end{datadesc}
90
91\begin{datadesc}{cmp_op}
92Sequence of all compare operation names.
93\end{datadesc}
94
95\begin{datadesc}{hasconst}
96Sequence of byte codes that have a constant parameter.
97\end{datadesc}
98
99\begin{datadesc}{hasname}
100Sequence of byte codes that access a attribute by name.
101\end{datadesc}
102
103\begin{datadesc}{hasjrel}
104Sequence of byte codes that have a relative jump target.
105\end{datadesc}
106
107\begin{datadesc}{hasjabs}
108Sequence of byte codes that have an absolute jump target.
109\end{datadesc}
110
111\begin{datadesc}{haslocal}
112Sequence of byte codes that access a a local variable.
113\end{datadesc}
114
115\begin{datadesc}{hascompare}
116Sequence of byte codes of boolean operations.
117\end{datadesc}
118
119\subsection{Python Byte Code Instructions}
120
121The Python compiler currently generates the following byte code
122instructions.
123
124\renewcommand{\indexsubitem}{(byte code insns)}
125
Fred Drake456035f1997-12-03 04:06:57 +0000126\begin{opcodedesc}{STOP_CODE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000127Indicates end-of-code to the compiler, not used by the interpreter.
Fred Drake456035f1997-12-03 04:06:57 +0000128\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000129
Fred Drake456035f1997-12-03 04:06:57 +0000130\begin{opcodedesc}{POP_TOP}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000131Removes the top-of-stack (TOS) item.
Fred Drake456035f1997-12-03 04:06:57 +0000132\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000133
Fred Drake456035f1997-12-03 04:06:57 +0000134\begin{opcodedesc}{ROT_TWO}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000135Swaps the two top-most stack items.
Fred Drake456035f1997-12-03 04:06:57 +0000136\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000137
Fred Drake456035f1997-12-03 04:06:57 +0000138\begin{opcodedesc}{ROT_THREE}{}
139Lifts second and third stack item one position up, moves top down
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000140to position three.
Fred Drake456035f1997-12-03 04:06:57 +0000141\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000142
Fred Drake456035f1997-12-03 04:06:57 +0000143\begin{opcodedesc}{DUP_TOP}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000144Duplicates the reference on top of the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000145\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000146
147Unary Operations take the top of the stack, apply the operation, and
148push the result back on the stack.
149
Fred Drake456035f1997-12-03 04:06:57 +0000150\begin{opcodedesc}{UNARY_POSITIVE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000151Implements \code{TOS = +TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000152\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000153
Fred Drake456035f1997-12-03 04:06:57 +0000154\begin{opcodedesc}{UNARY_NEG}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000155Implements \code{TOS = -TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000156\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000157
Fred Drake456035f1997-12-03 04:06:57 +0000158\begin{opcodedesc}{UNARY_NOT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000159Implements \code{TOS = not TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000160\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000161
Fred Drake456035f1997-12-03 04:06:57 +0000162\begin{opcodedesc}{UNARY_CONVERT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000163Implements \code{TOS = `TOS`}.
Fred Drake456035f1997-12-03 04:06:57 +0000164\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000165
Fred Drake456035f1997-12-03 04:06:57 +0000166\begin{opcodedesc}{UNARY_INVERT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000167Implements \code{TOS = ~TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000168\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000169
170Binary operations remove the top of the stack (TOS) and the second top-most
171stack item (TOS1) from the stack. They perform the operation, and put the
172result back on the stack.
173
Fred Drake456035f1997-12-03 04:06:57 +0000174\begin{opcodedesc}{BINARY_POWER}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000175Implements \code{TOS = TOS1 ** TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000176\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000177
Fred Drake456035f1997-12-03 04:06:57 +0000178\begin{opcodedesc}{BINARY_MULTIPLY}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000179Implements \code{TOS = TOS1 * TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000180\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000181
Fred Drake456035f1997-12-03 04:06:57 +0000182\begin{opcodedesc}{BINARY_DIVIDE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000183Implements \code{TOS = TOS1 / TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000184\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000185
Fred Drake456035f1997-12-03 04:06:57 +0000186\begin{opcodedesc}{BINARY_MODULO}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000187Implements \code{TOS = TOS1 \% TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000188\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000189
Fred Drake456035f1997-12-03 04:06:57 +0000190\begin{opcodedesc}{BINARY_ADD}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000191Implements \code{TOS = TOS1 + TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000192\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000193
Fred Drake456035f1997-12-03 04:06:57 +0000194\begin{opcodedesc}{BINARY_SUBTRACT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000195Implements \code{TOS = TOS1 - TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000196\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000197
Fred Drake456035f1997-12-03 04:06:57 +0000198\begin{opcodedesc}{BINARY_SUBSCR}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000199Implements \code{TOS = TOS1[TOS] }.
Fred Drake456035f1997-12-03 04:06:57 +0000200\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000201
Fred Drake456035f1997-12-03 04:06:57 +0000202\begin{opcodedesc}{BINARY_LSHIFT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000203Implements \code{TOS = TOS1 << TOS }.
Fred Drake456035f1997-12-03 04:06:57 +0000204\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000205
Fred Drake456035f1997-12-03 04:06:57 +0000206\begin{opcodedesc}{BINARY_RSHIFT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000207Implements \code{TOS = TOS1 << TOS }.
Fred Drake456035f1997-12-03 04:06:57 +0000208\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000209
Fred Drake456035f1997-12-03 04:06:57 +0000210\begin{opcodedesc}{BINARY_AND}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000211Implements \code{TOS = TOS1 and TOS }.
Fred Drake456035f1997-12-03 04:06:57 +0000212\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000213
Fred Drake456035f1997-12-03 04:06:57 +0000214\begin{opcodedesc}{BINARY_XOR}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000215Implements \code{TOS = TOS1 \^{ }TOS }.
Fred Drake456035f1997-12-03 04:06:57 +0000216\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000217
Fred Drake456035f1997-12-03 04:06:57 +0000218\begin{opcodedesc}{BINARY_OR}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000219Implements \code{TOS = TOS1 or TOS }.
Fred Drake456035f1997-12-03 04:06:57 +0000220\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000221
222The slice opcodes take up to three parameters.
223
Fred Drake456035f1997-12-03 04:06:57 +0000224\begin{opcodedesc}{SLICE+0}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000225Implements \code{TOS = TOS[:]}.
Fred Drake456035f1997-12-03 04:06:57 +0000226\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000227
Fred Drake456035f1997-12-03 04:06:57 +0000228\begin{opcodedesc}{SLICE+1}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000229Implements \code{TOS = TOS1[TOS:]}.
Fred Drake456035f1997-12-03 04:06:57 +0000230\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000231
Fred Drake456035f1997-12-03 04:06:57 +0000232\begin{opcodedesc}{SLICE+2}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000233Implements \code{TOS = TOS1[:TOS1]}.
Fred Drake456035f1997-12-03 04:06:57 +0000234\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000235
Fred Drake456035f1997-12-03 04:06:57 +0000236\begin{opcodedesc}{SLICE+3}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000237Implements \code{TOS = TOS2[TOS1:TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000238\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000239
240Slice assignment needs even an additional parameter. As any statement,
241they put nothing on the stack.
242
Fred Drake456035f1997-12-03 04:06:57 +0000243\begin{opcodedesc}{STORE_SLICE+0}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000244Implements \code{TOS[:]=TOS1}.
Fred Drake456035f1997-12-03 04:06:57 +0000245\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000246
Fred Drake456035f1997-12-03 04:06:57 +0000247\begin{opcodedesc}{STORE_SLICE+1}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000248Implements \code{TOS1[TOS:]=TOS2}.
Fred Drake456035f1997-12-03 04:06:57 +0000249\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000250
Fred Drake456035f1997-12-03 04:06:57 +0000251\begin{opcodedesc}{STORE_SLICE+2}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000252Implements \code{TOS1[:TOS]=TOS2}.
Fred Drake456035f1997-12-03 04:06:57 +0000253\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000254
Fred Drake456035f1997-12-03 04:06:57 +0000255\begin{opcodedesc}{STORE_SLICE+3}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000256Implements \code{TOS2[TOS1:TOS]=TOS3}.
Fred Drake456035f1997-12-03 04:06:57 +0000257\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000258
Fred Drake456035f1997-12-03 04:06:57 +0000259\begin{opcodedesc}{DELETE_SLICE+0}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000260Implements \code{del TOS[:]}.
Fred Drake456035f1997-12-03 04:06:57 +0000261\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000262
Fred Drake456035f1997-12-03 04:06:57 +0000263\begin{opcodedesc}{DELETE_SLICE+1}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000264Implements \code{del TOS1[TOS:]}.
Fred Drake456035f1997-12-03 04:06:57 +0000265\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000266
Fred Drake456035f1997-12-03 04:06:57 +0000267\begin{opcodedesc}{DELETE_SLICE+2}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000268Implements \code{del TOS1[:TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000269\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000270
Fred Drake456035f1997-12-03 04:06:57 +0000271\begin{opcodedesc}{DELETE_SLICE+3}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000272Implements \code{del TOS2[TOS1:TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000273\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000274
Fred Drake456035f1997-12-03 04:06:57 +0000275\begin{opcodedesc}{STORE_SUBSCR}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000276Implements \code{TOS1[TOS]=TOS2}.
Fred Drake456035f1997-12-03 04:06:57 +0000277\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000278
Fred Drake456035f1997-12-03 04:06:57 +0000279\begin{opcodedesc}{DELETE_SUBSCR}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000280Implements \code{del TOS1[TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000281\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000282
Fred Drake456035f1997-12-03 04:06:57 +0000283\begin{opcodedesc}{PRINT_EXPR}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000284Implements the expression statement for the interactive mode. TOS is
285removed from the stack and printed. In non-interactive mode, an
Fred Drake456035f1997-12-03 04:06:57 +0000286expression statement is terminated with \code{POP_STACK}.
287\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000288
Fred Drake456035f1997-12-03 04:06:57 +0000289\begin{opcodedesc}{PRINT_ITEM}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000290Prints TOS. There is one such instruction for
291each item in the print statement.
Fred Drake456035f1997-12-03 04:06:57 +0000292\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000293
Fred Drake456035f1997-12-03 04:06:57 +0000294\begin{opcodedesc}{PRINT_NEWLINE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000295Prints a new line on \code{sys.stdout}. This is generated as the
296last operation of a print statement, unless the statement ends
297with a comma.
Fred Drake456035f1997-12-03 04:06:57 +0000298\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000299
Fred Drake456035f1997-12-03 04:06:57 +0000300\begin{opcodedesc}{BREAK_LOOP}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000301Terminates a loop due to a break statement.
Fred Drake456035f1997-12-03 04:06:57 +0000302\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000303
Fred Drake456035f1997-12-03 04:06:57 +0000304\begin{opcodedesc}{LOAD_LOCALS}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000305Pushes a reference to the locals of the current scope on the stack.
306This is used in the code for a class definition: After the class body
307is evaluated, the locals are passed to the class definition.
Fred Drake456035f1997-12-03 04:06:57 +0000308\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000309
Fred Drake456035f1997-12-03 04:06:57 +0000310\begin{opcodedesc}{RETURN_VALUE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000311Returns with TOS to the caller of the function.
Fred Drake456035f1997-12-03 04:06:57 +0000312\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000313
Fred Drake456035f1997-12-03 04:06:57 +0000314\begin{opcodedesc}{EXEC_STMT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000315Implements \code{exec TOS2,TOS1,TOS}. The compiler fills
316missing optional parameters with None.
Fred Drake456035f1997-12-03 04:06:57 +0000317\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000318
Fred Drake456035f1997-12-03 04:06:57 +0000319\begin{opcodedesc}{POP_BLOCK}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000320Removes one block from the block stack. Per frame, there is a
321stack of blocks, denoting nested loops, try statements, and such.
Fred Drake456035f1997-12-03 04:06:57 +0000322\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000323
Fred Drake456035f1997-12-03 04:06:57 +0000324\begin{opcodedesc}{END_FINALLY}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000325Terminates a finally-block. The interpreter recalls whether the
326exception has to be re-raised, or whether the function returns,
327and continues with the outer-next block.
Fred Drake456035f1997-12-03 04:06:57 +0000328\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000329
Fred Drake456035f1997-12-03 04:06:57 +0000330\begin{opcodedesc}{BUILD_CLASS}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000331Creates a new class object. TOS is the methods dictionary, TOS1
332the tuple of the names of the base classes, and TOS2 the class name.
Fred Drake456035f1997-12-03 04:06:57 +0000333\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000334
335All of the following opcodes expect arguments. An argument is two
336bytes, with the more significant byte last.
337
Fred Drake456035f1997-12-03 04:06:57 +0000338\begin{opcodedesc}{STORE_NAME}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000339Implements \code{name = TOS}. \var{namei} is the index of \var{name}
340in the attribute \code{co_names} of the code object.
Fred Drake456035f1997-12-03 04:06:57 +0000341The compiler tries to use \code{STORE_LOCAL} or \code{STORE_GLOBAL}
342if possible.
343\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000344
Fred Drake456035f1997-12-03 04:06:57 +0000345\begin{opcodedesc}{DELETE_NAME}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000346Implements \code{del name}, where \var{namei} is the index into
347\code{co_names} attribute of the code object.
Fred Drake456035f1997-12-03 04:06:57 +0000348\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000349
Fred Drake456035f1997-12-03 04:06:57 +0000350\begin{opcodedesc}{UNPACK_TUPLE}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000351Unpacks TOS into \var{count} individual values, which are put onto
352the stack right-to-left.
Fred Drake456035f1997-12-03 04:06:57 +0000353\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000354
Fred Drake456035f1997-12-03 04:06:57 +0000355\begin{opcodedesc}{UNPACK_LIST}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000356Unpacks TOS into \var{count} individual values.
Fred Drake456035f1997-12-03 04:06:57 +0000357\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000358
Fred Drake456035f1997-12-03 04:06:57 +0000359%\begin{opcodedesc}{UNPACK_ARG}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000360%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000361%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000362
Fred Drake456035f1997-12-03 04:06:57 +0000363\begin{opcodedesc}{STORE_ATTR}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000364Implements \code{TOS.name = TOS1}, where \var{namei} is the index
365of name in \code{co_names}.
Fred Drake456035f1997-12-03 04:06:57 +0000366\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000367
Fred Drake456035f1997-12-03 04:06:57 +0000368\begin{opcodedesc}{DELETE_ATTR}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000369Implements \code{del TOS.name}, using \var{namei} as index into
370\code{co_names}.
Fred Drake456035f1997-12-03 04:06:57 +0000371\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000372
Fred Drake456035f1997-12-03 04:06:57 +0000373\begin{opcodedesc}{STORE_GLOBAL}{namei}
374Works as \code{STORE_NAME}, but stores the name as a global.
375\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000376
Fred Drake456035f1997-12-03 04:06:57 +0000377\begin{opcodedesc}{DELETE_GLOBAL}{namei}
378Works as \code{DELETE_NAME}, but deletes a global name.
379\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000380
Fred Drake456035f1997-12-03 04:06:57 +0000381%\begin{opcodedesc}{UNPACK_VARARG}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000382%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000383%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000384
Fred Drake456035f1997-12-03 04:06:57 +0000385\begin{opcodedesc}{LOAD_CONST}{consti}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000386Pushes \code{co_consts[consti]} onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000387\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000388
Fred Drake456035f1997-12-03 04:06:57 +0000389\begin{opcodedesc}{LOAD_NAME}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000390Pushes the value associated with \code{co_names[namei]} onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000391\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000392
Fred Drake456035f1997-12-03 04:06:57 +0000393\begin{opcodedesc}{BUILD_TUPLE}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000394Creates a tuple consuming \var{count} items from the stack, and pushes
395the resulting tuple onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000396\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000397
Fred Drake456035f1997-12-03 04:06:57 +0000398\begin{opcodedesc}{BUILD_LIST}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000399Works as \code{BUILD_TUPLE}, but creates a list.
Fred Drake456035f1997-12-03 04:06:57 +0000400\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000401
Fred Drake456035f1997-12-03 04:06:57 +0000402\begin{opcodedesc}{BUILD_MAP}{zero}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000403Pushes an empty dictionary object onto the stack. The argument is ignored
404and set to zero by the compiler.
Fred Drake456035f1997-12-03 04:06:57 +0000405\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000406
Fred Drake456035f1997-12-03 04:06:57 +0000407\begin{opcodedesc}{LOAD_ATTR}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000408Replaces TOS with \code{getattr(TOS,co_names[namei]}.
Fred Drake456035f1997-12-03 04:06:57 +0000409\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000410
Fred Drake456035f1997-12-03 04:06:57 +0000411\begin{opcodedesc}{COMPARE_OP}{opname}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000412Performs a boolean operation. The operation name can be found
413in \code{cmp_op[opname]}.
Fred Drake456035f1997-12-03 04:06:57 +0000414\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000415
Fred Drake456035f1997-12-03 04:06:57 +0000416\begin{opcodedesc}{IMPORT_NAME}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000417Imports the module \code{co_names[namei]}. The module object is
418pushed onto the stack. The current name space is not affect: for a
419proper import statement, a subsequent \code{STORE_FAST} instruction
420modifies the name space.
Fred Drake456035f1997-12-03 04:06:57 +0000421\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000422
Fred Drake456035f1997-12-03 04:06:57 +0000423\begin{opcodedesc}{IMPORT_FROM}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000424Imports the attribute \code{co_names[namei]}. The module to import
425from is found in TOS and left there.
Fred Drake456035f1997-12-03 04:06:57 +0000426\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000427
Fred Drake456035f1997-12-03 04:06:57 +0000428\begin{opcodedesc}{JUMP_FORWARD}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000429Increments byte code counter by \var{delta}.
Fred Drake456035f1997-12-03 04:06:57 +0000430\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000431
Fred Drake456035f1997-12-03 04:06:57 +0000432\begin{opcodedesc}{JUMP_IF_TRUE}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000433If TOS is true, increment the byte code counter by \var{delta}. TOS is
434left on the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000435\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000436
Fred Drake456035f1997-12-03 04:06:57 +0000437\begin{opcodedesc}{JUMP_IF_FALSE}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000438If TOS is false, increment the byte code counter by \var{delta}. TOS
439is not changed.
Fred Drake456035f1997-12-03 04:06:57 +0000440\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000441
Fred Drake456035f1997-12-03 04:06:57 +0000442\begin{opcodedesc}{JUMP_ABSOLUTE}{target}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000443Set byte code counter to \var{target}.
Fred Drake456035f1997-12-03 04:06:57 +0000444\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000445
Fred Drake456035f1997-12-03 04:06:57 +0000446\begin{opcodedesc}{FOR_LOOP}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000447Iterate over a sequence. TOS is the current index, TOS1 the sequence.
448First, the next element is computed. If the sequence is exhausted,
449increment byte code counter by \var{delta}. Otherwise, push the
450sequence, the incremented counter, and the current item onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000451\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000452
Fred Drake456035f1997-12-03 04:06:57 +0000453%\begin{opcodedesc}{LOAD_LOCAL}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000454%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000455%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000456
Fred Drake456035f1997-12-03 04:06:57 +0000457\begin{opcodedesc}{LOAD_GLOBAL}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000458Loads the global named \code{co_names[namei]} onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000459\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000460
Fred Drake456035f1997-12-03 04:06:57 +0000461%\begin{opcodedesc}{SET_FUNC_ARGS}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000462%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000463%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000464
Fred Drake456035f1997-12-03 04:06:57 +0000465\begin{opcodedesc}{SETUP_LOOP}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000466Pushes a block for a loop onto the block stack. The block spans
467from the current instruction with a size of \var{delta} bytes.
Fred Drake456035f1997-12-03 04:06:57 +0000468\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000469
Fred Drake456035f1997-12-03 04:06:57 +0000470\begin{opcodedesc}{SETUP_EXCEPT}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000471Pushes a try block from a try-except clause onto the block stack.
472\var{delta} points to the first except block.
Fred Drake456035f1997-12-03 04:06:57 +0000473\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000474
Fred Drake456035f1997-12-03 04:06:57 +0000475\begin{opcodedesc}{SETUP_FINALLY}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000476Pushes a try block from a try-except clause onto the block stack.
477\var{delta} points to the finally block.
Fred Drake456035f1997-12-03 04:06:57 +0000478\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000479
Fred Drake456035f1997-12-03 04:06:57 +0000480\begin{opcodedesc}{LOAD_FAST}{var_num}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000481Pushes a reference to the local \code{co_varnames[var_num]} onto
482the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000483\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000484
Fred Drake456035f1997-12-03 04:06:57 +0000485\begin{opcodedesc}{STORE_FAST}{var_num}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000486Stores TOS into the local \code{co_varnames[var_num]}.
Fred Drake456035f1997-12-03 04:06:57 +0000487\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000488
Fred Drake456035f1997-12-03 04:06:57 +0000489\begin{opcodedesc}{DELETE_FAST}{var_num}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000490Deletes local \code{co_varnames[var_num]}.
Fred Drake456035f1997-12-03 04:06:57 +0000491\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000492
Fred Drake456035f1997-12-03 04:06:57 +0000493\begin{opcodedesc}{SET_LINE_NO}{lineno}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000494Sets the current line number to \var{lineno}.
Fred Drake456035f1997-12-03 04:06:57 +0000495\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000496
Fred Drake456035f1997-12-03 04:06:57 +0000497\begin{opcodedesc}{RAISE_VARARGS}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000498Raises an exception. \var{argc} indicates the number of parameters
499to the raise statement, ranging from 1 to 3. The handler will find
500the traceback as TOS2, the parameter as TOS1, and the exception
501as TOS.
Fred Drake456035f1997-12-03 04:06:57 +0000502\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000503
Fred Drake456035f1997-12-03 04:06:57 +0000504\begin{opcodedesc}{CALL_FUNCTION}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000505Calls a function. The low byte of \var{argc} indicates the number of
506positional parameters, the high byte the number of keyword parameters.
507On the stack, the opcode finds the keyword parameters first. For each
508keyword argument, the value is on top of the key. Below the keyword
509parameters, the positional parameters are on the stack, with the
510right-most parameter on top. Below the parameters, the function object
511to call is on the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000512\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000513
Fred Drake456035f1997-12-03 04:06:57 +0000514\begin{opcodedesc}{MAKE_FUNCTION}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000515Pushes a new function object on the stack. TOS is the code associated
516with the function. The function object is defined to have \var{argc}
517default parameters, which are found below TOS.
Fred Drake456035f1997-12-03 04:06:57 +0000518\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000519
Fred Drake456035f1997-12-03 04:06:57 +0000520\begin{opcodedesc}{BUILD_SLICE}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000521Pushes a slice object on the stack. If \var{argc} is three, creates
522\code{TOS3[TOS2:TOS1:TOS]}. Otherwise, expects three arguments.
Fred Drake456035f1997-12-03 04:06:57 +0000523\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000524
525