blob: 0340c4f1bb77c9d0ebd3e3ced2ef6d4292764d5a [file] [log] [blame]
Fred Drakedff21a61998-04-03 05:42:10 +00001\section{Standard Module \module{dis}}
Guido van Rossumb62b6d11997-11-18 15:10:53 +00002\stmodindex{dis}
Guido van Rossumb62b6d11997-11-18 15:10:53 +00003\label{module-dis}
4
Fred Drakedff21a61998-04-03 05:42:10 +00005The \module{dis} module supports the analysis of Python byte code by
Guido van Rossumb62b6d11997-11-18 15:10:53 +00006disassembling it. Since there is no Python assembler, this module
7defines the Python assembly language. The Python byte code which
8this module takes as an input is defined in the file
Fred Drake456035f1997-12-03 04:06:57 +00009\file{Include/opcode.h} and used by the compiler and the interpreter.
Guido van Rossumb62b6d11997-11-18 15:10:53 +000010
Fred Drakedff21a61998-04-03 05:42:10 +000011Example: Given the function \function{myfunc}:
Guido van Rossumb62b6d11997-11-18 15:10:53 +000012
Fred Drake19479911998-02-13 06:58:54 +000013\begin{verbatim}
Guido van Rossumb62b6d11997-11-18 15:10:53 +000014def myfunc(alist):
Fred Drakedff21a61998-04-03 05:42:10 +000015 return len(alist)
Fred Drake19479911998-02-13 06:58:54 +000016\end{verbatim}
Guido van Rossumb62b6d11997-11-18 15:10:53 +000017
Fred Drakedff21a61998-04-03 05:42:10 +000018the following command can be used to get the disassembly of
19\function{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
Fred Drakedff21a61998-04-03 05:42:10 +000034The \module{dis} module defines the following functions:
Guido van Rossumb62b6d11997-11-18 15:10:53 +000035
Guido van Rossumb62b6d11997-11-18 15:10:53 +000036\begin{funcdesc}{dis}{\optional{bytesource}}
37Disassemble the \var{bytesource} object. \var{bytesource} can denote
38either a class, a method, a function, or a code object. For a class,
39it disassembles all methods. For a single code sequence, it prints
40one line per byte code instruction. If no object is provided, it
41disassembles the last traceback.
42\end{funcdesc}
43
44\begin{funcdesc}{distb}{\optional{tb}}
45Disassembles the top-of-stack function of a traceback, using the last
46traceback if none was passed. The instruction causing the exception
47is indicated.
48\end{funcdesc}
49
Fred Drakecce10901998-03-17 06:33:25 +000050\begin{funcdesc}{disassemble}{code\optional{, lasti}}
Guido van Rossumb62b6d11997-11-18 15:10:53 +000051Disassembles a code object, indicating the last instruction if \var{lasti}
52was provided. The output is divided in the following columns:
Fred Drake810349b1998-04-07 14:16:41 +000053
54\newcounter{discounter}
55\begin{list}{\arabic{discounter}.}{%
56 \usecounter{discounter}
57 \setlength{\leftmargin}{4.5mm}
58 \setlength{\labelsep}{1.5mm}}
Fred Drakedff21a61998-04-03 05:42:10 +000059\item the current instruction, indicated as \samp{-->},
60\item a labelled instruction, indicated with \samp{>>},
Guido van Rossumb62b6d11997-11-18 15:10:53 +000061\item the address of the instruction,
62\item the operation code name,
63\item operation parameters, and
64\item interpretation of the parameters in parentheses.
Fred Drake810349b1998-04-07 14:16:41 +000065\end{list}
66
Guido van Rossumb62b6d11997-11-18 15:10:53 +000067The parameter interpretation recognizes local and global
68variable names, constant values, branch targets, and compare
69operators.
70\end{funcdesc}
71
Fred Drakecce10901998-03-17 06:33:25 +000072\begin{funcdesc}{disco}{code\optional{, lasti}}
Guido van Rossumb62b6d11997-11-18 15:10:53 +000073A synonym for disassemble. It is more convenient to type, and kept
74for compatibility with earlier Python releases.
75\end{funcdesc}
76
77\begin{datadesc}{opname}
78Sequence of a operation names, indexable using the byte code.
79\end{datadesc}
80
81\begin{datadesc}{cmp_op}
82Sequence of all compare operation names.
83\end{datadesc}
84
85\begin{datadesc}{hasconst}
86Sequence of byte codes that have a constant parameter.
87\end{datadesc}
88
89\begin{datadesc}{hasname}
90Sequence of byte codes that access a attribute by name.
91\end{datadesc}
92
93\begin{datadesc}{hasjrel}
94Sequence of byte codes that have a relative jump target.
95\end{datadesc}
96
97\begin{datadesc}{hasjabs}
98Sequence of byte codes that have an absolute jump target.
99\end{datadesc}
100
101\begin{datadesc}{haslocal}
102Sequence of byte codes that access a a local variable.
103\end{datadesc}
104
105\begin{datadesc}{hascompare}
106Sequence of byte codes of boolean operations.
107\end{datadesc}
108
109\subsection{Python Byte Code Instructions}
Fred Drake83efb541998-02-19 20:07:39 +0000110\label{bytecodes}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000111
112The Python compiler currently generates the following byte code
113instructions.
114
Fred Drake19479911998-02-13 06:58:54 +0000115\setindexsubitem{(byte code insns)}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000116
Fred Drake456035f1997-12-03 04:06:57 +0000117\begin{opcodedesc}{STOP_CODE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000118Indicates end-of-code to the compiler, not used by the interpreter.
Fred Drake456035f1997-12-03 04:06:57 +0000119\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000120
Fred Drake456035f1997-12-03 04:06:57 +0000121\begin{opcodedesc}{POP_TOP}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000122Removes the top-of-stack (TOS) item.
Fred Drake456035f1997-12-03 04:06:57 +0000123\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000124
Fred Drake456035f1997-12-03 04:06:57 +0000125\begin{opcodedesc}{ROT_TWO}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000126Swaps the two top-most stack items.
Fred Drake456035f1997-12-03 04:06:57 +0000127\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000128
Fred Drake456035f1997-12-03 04:06:57 +0000129\begin{opcodedesc}{ROT_THREE}{}
130Lifts second and third stack item one position up, moves top down
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000131to position three.
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}{DUP_TOP}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000135Duplicates the reference on top of the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000136\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000137
138Unary Operations take the top of the stack, apply the operation, and
139push the result back on the stack.
140
Fred Drake456035f1997-12-03 04:06:57 +0000141\begin{opcodedesc}{UNARY_POSITIVE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000142Implements \code{TOS = +TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000143\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000144
Fred Drake456035f1997-12-03 04:06:57 +0000145\begin{opcodedesc}{UNARY_NEG}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000146Implements \code{TOS = -TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000147\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000148
Fred Drake456035f1997-12-03 04:06:57 +0000149\begin{opcodedesc}{UNARY_NOT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000150Implements \code{TOS = not TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000151\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000152
Fred Drake456035f1997-12-03 04:06:57 +0000153\begin{opcodedesc}{UNARY_CONVERT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000154Implements \code{TOS = `TOS`}.
Fred Drake456035f1997-12-03 04:06:57 +0000155\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000156
Fred Drake456035f1997-12-03 04:06:57 +0000157\begin{opcodedesc}{UNARY_INVERT}{}
Guido van Rossume903aab1997-12-18 16:28:56 +0000158Implements \code{TOS = \~TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000159\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000160
161Binary operations remove the top of the stack (TOS) and the second top-most
162stack item (TOS1) from the stack. They perform the operation, and put the
163result back on the stack.
164
Fred Drake456035f1997-12-03 04:06:57 +0000165\begin{opcodedesc}{BINARY_POWER}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000166Implements \code{TOS = TOS1 ** TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000167\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000168
Fred Drake456035f1997-12-03 04:06:57 +0000169\begin{opcodedesc}{BINARY_MULTIPLY}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000170Implements \code{TOS = TOS1 * TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000171\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000172
Fred Drake456035f1997-12-03 04:06:57 +0000173\begin{opcodedesc}{BINARY_DIVIDE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000174Implements \code{TOS = TOS1 / TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000175\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000176
Fred Drake456035f1997-12-03 04:06:57 +0000177\begin{opcodedesc}{BINARY_MODULO}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000178Implements \code{TOS = TOS1 \% TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000179\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000180
Fred Drake456035f1997-12-03 04:06:57 +0000181\begin{opcodedesc}{BINARY_ADD}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000182Implements \code{TOS = TOS1 + TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000183\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000184
Fred Drake456035f1997-12-03 04:06:57 +0000185\begin{opcodedesc}{BINARY_SUBTRACT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000186Implements \code{TOS = TOS1 - TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000187\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000188
Fred Drake456035f1997-12-03 04:06:57 +0000189\begin{opcodedesc}{BINARY_SUBSCR}{}
Fred Drake1cf87491997-12-04 04:57:56 +0000190Implements \code{TOS = TOS1[TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000191\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000192
Fred Drake456035f1997-12-03 04:06:57 +0000193\begin{opcodedesc}{BINARY_LSHIFT}{}
Fred Drake1cf87491997-12-04 04:57:56 +0000194Implements \code{TOS = TOS1 << TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000195\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000196
Fred Drake456035f1997-12-03 04:06:57 +0000197\begin{opcodedesc}{BINARY_RSHIFT}{}
Fred Drake1cf87491997-12-04 04:57:56 +0000198Implements \code{TOS = TOS1 >> TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000199\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000200
Fred Drake456035f1997-12-03 04:06:57 +0000201\begin{opcodedesc}{BINARY_AND}{}
Fred Drake1cf87491997-12-04 04:57:56 +0000202Implements \code{TOS = TOS1 and TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000203\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000204
Fred Drake456035f1997-12-03 04:06:57 +0000205\begin{opcodedesc}{BINARY_XOR}{}
Fred Draked7feffd1997-12-29 20:02:55 +0000206Implements \code{TOS = TOS1 \^\ TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000207\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000208
Fred Drake456035f1997-12-03 04:06:57 +0000209\begin{opcodedesc}{BINARY_OR}{}
Fred Drake1cf87491997-12-04 04:57:56 +0000210Implements \code{TOS = TOS1 or TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000211\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000212
213The slice opcodes take up to three parameters.
214
Fred Drake456035f1997-12-03 04:06:57 +0000215\begin{opcodedesc}{SLICE+0}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000216Implements \code{TOS = TOS[:]}.
Fred Drake456035f1997-12-03 04:06:57 +0000217\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000218
Fred Drake456035f1997-12-03 04:06:57 +0000219\begin{opcodedesc}{SLICE+1}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000220Implements \code{TOS = TOS1[TOS:]}.
Fred Drake456035f1997-12-03 04:06:57 +0000221\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000222
Fred Drake456035f1997-12-03 04:06:57 +0000223\begin{opcodedesc}{SLICE+2}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000224Implements \code{TOS = TOS1[:TOS1]}.
Fred Drake456035f1997-12-03 04:06:57 +0000225\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000226
Fred Drake456035f1997-12-03 04:06:57 +0000227\begin{opcodedesc}{SLICE+3}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000228Implements \code{TOS = TOS2[TOS1:TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000229\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000230
231Slice assignment needs even an additional parameter. As any statement,
232they put nothing on the stack.
233
Fred Drake456035f1997-12-03 04:06:57 +0000234\begin{opcodedesc}{STORE_SLICE+0}{}
Fred Drake7381e281997-12-04 04:51:12 +0000235Implements \code{TOS[:] = TOS1}.
Fred Drake456035f1997-12-03 04:06:57 +0000236\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000237
Fred Drake456035f1997-12-03 04:06:57 +0000238\begin{opcodedesc}{STORE_SLICE+1}{}
Fred Drake7381e281997-12-04 04:51:12 +0000239Implements \code{TOS1[TOS:] = TOS2}.
Fred Drake456035f1997-12-03 04:06:57 +0000240\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000241
Fred Drake456035f1997-12-03 04:06:57 +0000242\begin{opcodedesc}{STORE_SLICE+2}{}
Fred Drake7381e281997-12-04 04:51:12 +0000243Implements \code{TOS1[:TOS] = TOS2}.
Fred Drake456035f1997-12-03 04:06:57 +0000244\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000245
Fred Drake456035f1997-12-03 04:06:57 +0000246\begin{opcodedesc}{STORE_SLICE+3}{}
Fred Drake7381e281997-12-04 04:51:12 +0000247Implements \code{TOS2[TOS1:TOS] = TOS3}.
Fred Drake456035f1997-12-03 04:06:57 +0000248\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000249
Fred Drake456035f1997-12-03 04:06:57 +0000250\begin{opcodedesc}{DELETE_SLICE+0}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000251Implements \code{del TOS[:]}.
Fred Drake456035f1997-12-03 04:06:57 +0000252\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000253
Fred Drake456035f1997-12-03 04:06:57 +0000254\begin{opcodedesc}{DELETE_SLICE+1}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000255Implements \code{del TOS1[TOS:]}.
Fred Drake456035f1997-12-03 04:06:57 +0000256\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000257
Fred Drake456035f1997-12-03 04:06:57 +0000258\begin{opcodedesc}{DELETE_SLICE+2}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000259Implements \code{del TOS1[:TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000260\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000261
Fred Drake456035f1997-12-03 04:06:57 +0000262\begin{opcodedesc}{DELETE_SLICE+3}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000263Implements \code{del TOS2[TOS1:TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000264\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000265
Fred Drake456035f1997-12-03 04:06:57 +0000266\begin{opcodedesc}{STORE_SUBSCR}{}
Fred Drake7381e281997-12-04 04:51:12 +0000267Implements \code{TOS1[TOS] = TOS2}.
Fred Drake456035f1997-12-03 04:06:57 +0000268\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000269
Fred Drake456035f1997-12-03 04:06:57 +0000270\begin{opcodedesc}{DELETE_SUBSCR}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000271Implements \code{del TOS1[TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000272\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000273
Fred Drake456035f1997-12-03 04:06:57 +0000274\begin{opcodedesc}{PRINT_EXPR}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000275Implements the expression statement for the interactive mode. TOS is
276removed from the stack and printed. In non-interactive mode, an
Fred Drake456035f1997-12-03 04:06:57 +0000277expression statement is terminated with \code{POP_STACK}.
278\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000279
Fred Drake456035f1997-12-03 04:06:57 +0000280\begin{opcodedesc}{PRINT_ITEM}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000281Prints TOS. There is one such instruction for
282each item in the print statement.
Fred Drake456035f1997-12-03 04:06:57 +0000283\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000284
Fred Drake456035f1997-12-03 04:06:57 +0000285\begin{opcodedesc}{PRINT_NEWLINE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000286Prints a new line on \code{sys.stdout}. This is generated as the
287last operation of a print statement, unless the statement ends
288with a comma.
Fred Drake456035f1997-12-03 04:06:57 +0000289\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000290
Fred Drake456035f1997-12-03 04:06:57 +0000291\begin{opcodedesc}{BREAK_LOOP}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000292Terminates a loop due to a break statement.
Fred Drake456035f1997-12-03 04:06:57 +0000293\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000294
Fred Drake456035f1997-12-03 04:06:57 +0000295\begin{opcodedesc}{LOAD_LOCALS}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000296Pushes a reference to the locals of the current scope on the stack.
297This is used in the code for a class definition: After the class body
298is evaluated, the locals are passed to the class definition.
Fred Drake456035f1997-12-03 04:06:57 +0000299\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000300
Fred Drake456035f1997-12-03 04:06:57 +0000301\begin{opcodedesc}{RETURN_VALUE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000302Returns with TOS to the caller of the function.
Fred Drake456035f1997-12-03 04:06:57 +0000303\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000304
Fred Drake456035f1997-12-03 04:06:57 +0000305\begin{opcodedesc}{EXEC_STMT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000306Implements \code{exec TOS2,TOS1,TOS}. The compiler fills
307missing optional parameters with None.
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}{POP_BLOCK}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000311Removes one block from the block stack. Per frame, there is a
312stack of blocks, denoting nested loops, try statements, and such.
Fred Drake456035f1997-12-03 04:06:57 +0000313\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000314
Fred Drake456035f1997-12-03 04:06:57 +0000315\begin{opcodedesc}{END_FINALLY}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000316Terminates a finally-block. The interpreter recalls whether the
317exception has to be re-raised, or whether the function returns,
318and continues with the outer-next block.
Fred Drake456035f1997-12-03 04:06:57 +0000319\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000320
Fred Drake456035f1997-12-03 04:06:57 +0000321\begin{opcodedesc}{BUILD_CLASS}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000322Creates a new class object. TOS is the methods dictionary, TOS1
323the tuple of the names of the base classes, and TOS2 the class name.
Fred Drake456035f1997-12-03 04:06:57 +0000324\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000325
326All of the following opcodes expect arguments. An argument is two
327bytes, with the more significant byte last.
328
Fred Drake456035f1997-12-03 04:06:57 +0000329\begin{opcodedesc}{STORE_NAME}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000330Implements \code{name = TOS}. \var{namei} is the index of \var{name}
Fred Drakedff21a61998-04-03 05:42:10 +0000331in the attribute \member{co_names} of the code object.
Fred Drake456035f1997-12-03 04:06:57 +0000332The compiler tries to use \code{STORE_LOCAL} or \code{STORE_GLOBAL}
333if possible.
334\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000335
Fred Drake456035f1997-12-03 04:06:57 +0000336\begin{opcodedesc}{DELETE_NAME}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000337Implements \code{del name}, where \var{namei} is the index into
Fred Drakedff21a61998-04-03 05:42:10 +0000338\member{co_names} attribute of the code object.
Fred Drake456035f1997-12-03 04:06:57 +0000339\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000340
Fred Drake456035f1997-12-03 04:06:57 +0000341\begin{opcodedesc}{UNPACK_TUPLE}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000342Unpacks TOS into \var{count} individual values, which are put onto
343the stack right-to-left.
Fred Drake456035f1997-12-03 04:06:57 +0000344\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000345
Fred Drake456035f1997-12-03 04:06:57 +0000346\begin{opcodedesc}{UNPACK_LIST}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000347Unpacks TOS into \var{count} individual values.
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_ARG}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000351%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000352%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000353
Fred Drake456035f1997-12-03 04:06:57 +0000354\begin{opcodedesc}{STORE_ATTR}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000355Implements \code{TOS.name = TOS1}, where \var{namei} is the index
Fred Drakedff21a61998-04-03 05:42:10 +0000356of name in \member{co_names}.
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}{DELETE_ATTR}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000360Implements \code{del TOS.name}, using \var{namei} as index into
Fred Drakedff21a61998-04-03 05:42:10 +0000361\member{co_names}.
Fred Drake456035f1997-12-03 04:06:57 +0000362\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000363
Fred Drake456035f1997-12-03 04:06:57 +0000364\begin{opcodedesc}{STORE_GLOBAL}{namei}
365Works as \code{STORE_NAME}, but stores the name as a global.
366\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000367
Fred Drake456035f1997-12-03 04:06:57 +0000368\begin{opcodedesc}{DELETE_GLOBAL}{namei}
369Works as \code{DELETE_NAME}, but deletes a global name.
370\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000371
Fred Drake456035f1997-12-03 04:06:57 +0000372%\begin{opcodedesc}{UNPACK_VARARG}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000373%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000374%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000375
Fred Drake456035f1997-12-03 04:06:57 +0000376\begin{opcodedesc}{LOAD_CONST}{consti}
Fred Drakedff21a61998-04-03 05:42:10 +0000377Pushes \samp{co_consts[\var{consti}]} onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000378\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000379
Fred Drake456035f1997-12-03 04:06:57 +0000380\begin{opcodedesc}{LOAD_NAME}{namei}
Fred Drakedff21a61998-04-03 05:42:10 +0000381Pushes the value associated with \samp{co_names[\var{namei}]} onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000382\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000383
Fred Drake456035f1997-12-03 04:06:57 +0000384\begin{opcodedesc}{BUILD_TUPLE}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000385Creates a tuple consuming \var{count} items from the stack, and pushes
386the resulting tuple 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}{BUILD_LIST}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000390Works as \code{BUILD_TUPLE}, but creates a list.
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_MAP}{zero}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000394Pushes an empty dictionary object onto the stack. The argument is ignored
395and set to zero by the compiler.
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}{LOAD_ATTR}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000399Replaces TOS with \code{getattr(TOS,co_names[\var{namei}]}.
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}{COMPARE_OP}{opname}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000403Performs a boolean operation. The operation name can be found
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000404in \code{cmp_op[\var{opname}]}.
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}{IMPORT_NAME}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000408Imports the module \code{co_names[\var{namei}]}. The module object is
409pushed onto the stack. The current name space is not affected: for a
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000410proper import statement, a subsequent \code{STORE_FAST} instruction
411modifies the name space.
Fred Drake456035f1997-12-03 04:06:57 +0000412\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000413
Fred Drake456035f1997-12-03 04:06:57 +0000414\begin{opcodedesc}{IMPORT_FROM}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000415Imports the attribute \code{co_names[\var{namei}]}. The module to import
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000416from is found in TOS and left there.
Fred Drake456035f1997-12-03 04:06:57 +0000417\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000418
Fred Drake456035f1997-12-03 04:06:57 +0000419\begin{opcodedesc}{JUMP_FORWARD}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000420Increments byte code counter by \var{delta}.
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}{JUMP_IF_TRUE}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000424If TOS is true, increment the byte code counter by \var{delta}. TOS is
425left on the stack.
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_IF_FALSE}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000429If TOS is false, increment the byte code counter by \var{delta}. TOS
430is not changed.
Fred Drake456035f1997-12-03 04:06:57 +0000431\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000432
Fred Drake456035f1997-12-03 04:06:57 +0000433\begin{opcodedesc}{JUMP_ABSOLUTE}{target}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000434Set byte code counter to \var{target}.
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}{FOR_LOOP}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000438Iterate over a sequence. TOS is the current index, TOS1 the sequence.
439First, the next element is computed. If the sequence is exhausted,
440increment byte code counter by \var{delta}. Otherwise, push the
441sequence, the incremented counter, and the current item onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000442\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000443
Fred Drake456035f1997-12-03 04:06:57 +0000444%\begin{opcodedesc}{LOAD_LOCAL}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000445%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000446%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000447
Fred Drake456035f1997-12-03 04:06:57 +0000448\begin{opcodedesc}{LOAD_GLOBAL}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000449Loads the global named \code{co_names[\var{namei}]} onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000450\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000451
Fred Drake456035f1997-12-03 04:06:57 +0000452%\begin{opcodedesc}{SET_FUNC_ARGS}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000453%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000454%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000455
Fred Drake456035f1997-12-03 04:06:57 +0000456\begin{opcodedesc}{SETUP_LOOP}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000457Pushes a block for a loop onto the block stack. The block spans
458from the current instruction with a size of \var{delta} bytes.
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}{SETUP_EXCEPT}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000462Pushes a try block from a try-except clause onto the block stack.
463\var{delta} points to the first except block.
Fred Drake456035f1997-12-03 04:06:57 +0000464\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000465
Fred Drake456035f1997-12-03 04:06:57 +0000466\begin{opcodedesc}{SETUP_FINALLY}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000467Pushes a try block from a try-except clause onto the block stack.
468\var{delta} points to the finally block.
Fred Drake456035f1997-12-03 04:06:57 +0000469\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000470
Fred Drake456035f1997-12-03 04:06:57 +0000471\begin{opcodedesc}{LOAD_FAST}{var_num}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000472Pushes a reference to the local \code{co_varnames[\var{var_num}]} onto
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000473the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000474\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000475
Fred Drake456035f1997-12-03 04:06:57 +0000476\begin{opcodedesc}{STORE_FAST}{var_num}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000477Stores TOS into the local \code{co_varnames[\var{var_num}]}.
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}{DELETE_FAST}{var_num}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000481Deletes local \code{co_varnames[\var{var_num}]}.
Fred Drake456035f1997-12-03 04:06:57 +0000482\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000483
Fred Drake456035f1997-12-03 04:06:57 +0000484\begin{opcodedesc}{SET_LINE_NO}{lineno}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000485Sets the current line number to \var{lineno}.
Fred Drake456035f1997-12-03 04:06:57 +0000486\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000487
Fred Drake456035f1997-12-03 04:06:57 +0000488\begin{opcodedesc}{RAISE_VARARGS}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000489Raises an exception. \var{argc} indicates the number of parameters
490to the raise statement, ranging from 1 to 3. The handler will find
491the traceback as TOS2, the parameter as TOS1, and the exception
492as TOS.
Fred Drake456035f1997-12-03 04:06:57 +0000493\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000494
Fred Drake456035f1997-12-03 04:06:57 +0000495\begin{opcodedesc}{CALL_FUNCTION}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000496Calls a function. The low byte of \var{argc} indicates the number of
497positional parameters, the high byte the number of keyword parameters.
498On the stack, the opcode finds the keyword parameters first. For each
499keyword argument, the value is on top of the key. Below the keyword
500parameters, the positional parameters are on the stack, with the
501right-most parameter on top. Below the parameters, the function object
502to call is on the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000503\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000504
Fred Drake456035f1997-12-03 04:06:57 +0000505\begin{opcodedesc}{MAKE_FUNCTION}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000506Pushes a new function object on the stack. TOS is the code associated
507with the function. The function object is defined to have \var{argc}
508default parameters, which are found below TOS.
Fred Drake456035f1997-12-03 04:06:57 +0000509\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000510
Fred Drake456035f1997-12-03 04:06:57 +0000511\begin{opcodedesc}{BUILD_SLICE}{argc}
Guido van Rossumda623981998-02-12 03:53:02 +0000512Pushes a slice object on the stack. \var{argc} must be 2 or 3. If it
513is 2, \code{slice(TOS1, TOS)} is pushed; if it is 3,
514\code{slice(TOS2, TOS1, TOS)} is pushed.
Fred Drake19479911998-02-13 06:58:54 +0000515See the \code{slice()}\bifuncindex{slice} built-in function.
Fred Drake456035f1997-12-03 04:06:57 +0000516\end{opcodedesc}