blob: e0698ffbb8d16112931e9ae032e9f7e9d7be3803 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{dis} ---
2 Disassembler.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{dis}
4
5\modulesynopsis{Disassembler.}
6
Guido van Rossumb62b6d11997-11-18 15:10:53 +00007
Fred Drakedff21a61998-04-03 05:42:10 +00008The \module{dis} module supports the analysis of Python byte code by
Guido van Rossumb62b6d11997-11-18 15:10:53 +00009disassembling it. Since there is no Python assembler, this module
10defines the Python assembly language. The Python byte code which
11this module takes as an input is defined in the file
Fred Drake456035f1997-12-03 04:06:57 +000012\file{Include/opcode.h} and used by the compiler and the interpreter.
Guido van Rossumb62b6d11997-11-18 15:10:53 +000013
Fred Drakedff21a61998-04-03 05:42:10 +000014Example: Given the function \function{myfunc}:
Guido van Rossumb62b6d11997-11-18 15:10:53 +000015
Fred Drake19479911998-02-13 06:58:54 +000016\begin{verbatim}
Guido van Rossumb62b6d11997-11-18 15:10:53 +000017def myfunc(alist):
Fred Drakedff21a61998-04-03 05:42:10 +000018 return len(alist)
Fred Drake19479911998-02-13 06:58:54 +000019\end{verbatim}
Guido van Rossumb62b6d11997-11-18 15:10:53 +000020
Fred Drakedff21a61998-04-03 05:42:10 +000021the following command can be used to get the disassembly of
22\function{myfunc()}:
Guido van Rossumb62b6d11997-11-18 15:10:53 +000023
24\begin{verbatim}
25>>> dis.dis(myfunc)
26 0 SET_LINENO 1
27
28 3 SET_LINENO 2
29 6 LOAD_GLOBAL 0 (len)
30 9 LOAD_FAST 0 (alist)
31 12 CALL_FUNCTION 1
32 15 RETURN_VALUE
33 16 LOAD_CONST 0 (None)
34 19 RETURN_VALUE
35\end{verbatim}
36
Fred Drakedff21a61998-04-03 05:42:10 +000037The \module{dis} module defines the following functions:
Guido van Rossumb62b6d11997-11-18 15:10:53 +000038
Guido van Rossumb62b6d11997-11-18 15:10:53 +000039\begin{funcdesc}{dis}{\optional{bytesource}}
40Disassemble the \var{bytesource} object. \var{bytesource} can denote
41either a class, a method, a function, or a code object. For a class,
42it disassembles all methods. For a single code sequence, it prints
43one line per byte code instruction. If no object is provided, it
44disassembles the last traceback.
45\end{funcdesc}
46
47\begin{funcdesc}{distb}{\optional{tb}}
48Disassembles the top-of-stack function of a traceback, using the last
49traceback if none was passed. The instruction causing the exception
50is indicated.
51\end{funcdesc}
52
Fred Drakecce10901998-03-17 06:33:25 +000053\begin{funcdesc}{disassemble}{code\optional{, lasti}}
Guido van Rossumb62b6d11997-11-18 15:10:53 +000054Disassembles a code object, indicating the last instruction if \var{lasti}
55was provided. The output is divided in the following columns:
Fred Drake810349b1998-04-07 14:16:41 +000056
Fred Drake3e7a48e1998-04-13 16:15:02 +000057\begin{enumerate}
Fred Drakedff21a61998-04-03 05:42:10 +000058\item the current instruction, indicated as \samp{-->},
59\item a labelled instruction, indicated with \samp{>>},
Guido van Rossumb62b6d11997-11-18 15:10:53 +000060\item the address of the instruction,
61\item the operation code name,
62\item operation parameters, and
63\item interpretation of the parameters in parentheses.
Fred Drake3e7a48e1998-04-13 16:15:02 +000064\end{enumerate}
Fred Drake810349b1998-04-07 14:16:41 +000065
Guido van Rossumb62b6d11997-11-18 15:10:53 +000066The parameter interpretation recognizes local and global
67variable names, constant values, branch targets, and compare
68operators.
69\end{funcdesc}
70
Fred Drakecce10901998-03-17 06:33:25 +000071\begin{funcdesc}{disco}{code\optional{, lasti}}
Guido van Rossumb62b6d11997-11-18 15:10:53 +000072A synonym for disassemble. It is more convenient to type, and kept
73for compatibility with earlier Python releases.
74\end{funcdesc}
75
76\begin{datadesc}{opname}
77Sequence of a operation names, indexable using the byte code.
78\end{datadesc}
79
80\begin{datadesc}{cmp_op}
81Sequence of all compare operation names.
82\end{datadesc}
83
84\begin{datadesc}{hasconst}
85Sequence of byte codes that have a constant parameter.
86\end{datadesc}
87
88\begin{datadesc}{hasname}
89Sequence of byte codes that access a attribute by name.
90\end{datadesc}
91
92\begin{datadesc}{hasjrel}
93Sequence of byte codes that have a relative jump target.
94\end{datadesc}
95
96\begin{datadesc}{hasjabs}
97Sequence of byte codes that have an absolute jump target.
98\end{datadesc}
99
100\begin{datadesc}{haslocal}
101Sequence of byte codes that access a a local variable.
102\end{datadesc}
103
104\begin{datadesc}{hascompare}
105Sequence of byte codes of boolean operations.
106\end{datadesc}
107
108\subsection{Python Byte Code Instructions}
Fred Drake83efb541998-02-19 20:07:39 +0000109\label{bytecodes}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000110
111The Python compiler currently generates the following byte code
112instructions.
113
Fred Drake19479911998-02-13 06:58:54 +0000114\setindexsubitem{(byte code insns)}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000115
Fred Drake456035f1997-12-03 04:06:57 +0000116\begin{opcodedesc}{STOP_CODE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000117Indicates end-of-code to the compiler, not used by the interpreter.
Fred Drake456035f1997-12-03 04:06:57 +0000118\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000119
Fred Drake456035f1997-12-03 04:06:57 +0000120\begin{opcodedesc}{POP_TOP}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000121Removes the top-of-stack (TOS) item.
Fred Drake456035f1997-12-03 04:06:57 +0000122\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000123
Fred Drake456035f1997-12-03 04:06:57 +0000124\begin{opcodedesc}{ROT_TWO}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000125Swaps the two top-most stack items.
Fred Drake456035f1997-12-03 04:06:57 +0000126\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000127
Fred Drake456035f1997-12-03 04:06:57 +0000128\begin{opcodedesc}{ROT_THREE}{}
129Lifts second and third stack item one position up, moves top down
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000130to position three.
Fred Drake456035f1997-12-03 04:06:57 +0000131\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000132
Fred Drake456035f1997-12-03 04:06:57 +0000133\begin{opcodedesc}{DUP_TOP}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000134Duplicates the reference on top of the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000135\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000136
137Unary Operations take the top of the stack, apply the operation, and
138push the result back on the stack.
139
Fred Drake456035f1997-12-03 04:06:57 +0000140\begin{opcodedesc}{UNARY_POSITIVE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000141Implements \code{TOS = +TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000142\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000143
Fred Drake456035f1997-12-03 04:06:57 +0000144\begin{opcodedesc}{UNARY_NEG}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000145Implements \code{TOS = -TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000146\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000147
Fred Drake456035f1997-12-03 04:06:57 +0000148\begin{opcodedesc}{UNARY_NOT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000149Implements \code{TOS = not TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000150\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000151
Fred Drake456035f1997-12-03 04:06:57 +0000152\begin{opcodedesc}{UNARY_CONVERT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000153Implements \code{TOS = `TOS`}.
Fred Drake456035f1997-12-03 04:06:57 +0000154\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000155
Fred Drake456035f1997-12-03 04:06:57 +0000156\begin{opcodedesc}{UNARY_INVERT}{}
Fred Drake9e759df2000-06-15 18:44:30 +0000157Implements \code{TOS = \~{}TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000158\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000159
160Binary operations remove the top of the stack (TOS) and the second top-most
161stack item (TOS1) from the stack. They perform the operation, and put the
162result back on the stack.
163
Fred Drake456035f1997-12-03 04:06:57 +0000164\begin{opcodedesc}{BINARY_POWER}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000165Implements \code{TOS = TOS1 ** TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000166\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000167
Fred Drake456035f1997-12-03 04:06:57 +0000168\begin{opcodedesc}{BINARY_MULTIPLY}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000169Implements \code{TOS = TOS1 * TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000170\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000171
Fred Drake456035f1997-12-03 04:06:57 +0000172\begin{opcodedesc}{BINARY_DIVIDE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000173Implements \code{TOS = TOS1 / TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000174\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000175
Fred Drake456035f1997-12-03 04:06:57 +0000176\begin{opcodedesc}{BINARY_MODULO}{}
Fred Drake9e759df2000-06-15 18:44:30 +0000177Implements \code{TOS = TOS1 \%{} TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000178\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000179
Fred Drake456035f1997-12-03 04:06:57 +0000180\begin{opcodedesc}{BINARY_ADD}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000181Implements \code{TOS = TOS1 + TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000182\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000183
Fred Drake456035f1997-12-03 04:06:57 +0000184\begin{opcodedesc}{BINARY_SUBTRACT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000185Implements \code{TOS = TOS1 - TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000186\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000187
Fred Drake456035f1997-12-03 04:06:57 +0000188\begin{opcodedesc}{BINARY_SUBSCR}{}
Fred Drake1cf87491997-12-04 04:57:56 +0000189Implements \code{TOS = TOS1[TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000190\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000191
Fred Drake456035f1997-12-03 04:06:57 +0000192\begin{opcodedesc}{BINARY_LSHIFT}{}
Fred Drake1cf87491997-12-04 04:57:56 +0000193Implements \code{TOS = TOS1 << TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000194\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000195
Fred Drake456035f1997-12-03 04:06:57 +0000196\begin{opcodedesc}{BINARY_RSHIFT}{}
Fred Drake1cf87491997-12-04 04:57:56 +0000197Implements \code{TOS = TOS1 >> TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000198\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000199
Fred Drake456035f1997-12-03 04:06:57 +0000200\begin{opcodedesc}{BINARY_AND}{}
Fred Drake9e759df2000-06-15 18:44:30 +0000201Implements \code{TOS = TOS1 \&\ TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000202\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000203
Fred Drake456035f1997-12-03 04:06:57 +0000204\begin{opcodedesc}{BINARY_XOR}{}
Fred Draked7feffd1997-12-29 20:02:55 +0000205Implements \code{TOS = TOS1 \^\ TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000206\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000207
Fred Drake456035f1997-12-03 04:06:57 +0000208\begin{opcodedesc}{BINARY_OR}{}
Fred Drake9e759df2000-06-15 18:44:30 +0000209Implements \code{TOS = TOS1 | TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000210\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000211
212The slice opcodes take up to three parameters.
213
Fred Drake456035f1997-12-03 04:06:57 +0000214\begin{opcodedesc}{SLICE+0}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000215Implements \code{TOS = 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}{SLICE+1}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000219Implements \code{TOS = TOS1[TOS:]}.
Fred Drake456035f1997-12-03 04:06:57 +0000220\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000221
Fred Drake456035f1997-12-03 04:06:57 +0000222\begin{opcodedesc}{SLICE+2}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000223Implements \code{TOS = TOS1[:TOS1]}.
Fred Drake456035f1997-12-03 04:06:57 +0000224\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000225
Fred Drake456035f1997-12-03 04:06:57 +0000226\begin{opcodedesc}{SLICE+3}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000227Implements \code{TOS = TOS2[TOS1:TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000228\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000229
230Slice assignment needs even an additional parameter. As any statement,
231they put nothing on the stack.
232
Fred Drake456035f1997-12-03 04:06:57 +0000233\begin{opcodedesc}{STORE_SLICE+0}{}
Fred Drake7381e281997-12-04 04:51:12 +0000234Implements \code{TOS[:] = TOS1}.
Fred Drake456035f1997-12-03 04:06:57 +0000235\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000236
Fred Drake456035f1997-12-03 04:06:57 +0000237\begin{opcodedesc}{STORE_SLICE+1}{}
Fred Drake7381e281997-12-04 04:51:12 +0000238Implements \code{TOS1[TOS:] = TOS2}.
Fred Drake456035f1997-12-03 04:06:57 +0000239\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000240
Fred Drake456035f1997-12-03 04:06:57 +0000241\begin{opcodedesc}{STORE_SLICE+2}{}
Fred Drake7381e281997-12-04 04:51:12 +0000242Implements \code{TOS1[:TOS] = TOS2}.
Fred Drake456035f1997-12-03 04:06:57 +0000243\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000244
Fred Drake456035f1997-12-03 04:06:57 +0000245\begin{opcodedesc}{STORE_SLICE+3}{}
Fred Drake7381e281997-12-04 04:51:12 +0000246Implements \code{TOS2[TOS1:TOS] = TOS3}.
Fred Drake456035f1997-12-03 04:06:57 +0000247\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000248
Fred Drake456035f1997-12-03 04:06:57 +0000249\begin{opcodedesc}{DELETE_SLICE+0}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000250Implements \code{del TOS[:]}.
Fred Drake456035f1997-12-03 04:06:57 +0000251\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000252
Fred Drake456035f1997-12-03 04:06:57 +0000253\begin{opcodedesc}{DELETE_SLICE+1}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000254Implements \code{del TOS1[TOS:]}.
Fred Drake456035f1997-12-03 04:06:57 +0000255\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000256
Fred Drake456035f1997-12-03 04:06:57 +0000257\begin{opcodedesc}{DELETE_SLICE+2}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000258Implements \code{del TOS1[:TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000259\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000260
Fred Drake456035f1997-12-03 04:06:57 +0000261\begin{opcodedesc}{DELETE_SLICE+3}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000262Implements \code{del TOS2[TOS1:TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000263\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000264
Fred Drake456035f1997-12-03 04:06:57 +0000265\begin{opcodedesc}{STORE_SUBSCR}{}
Fred Drake7381e281997-12-04 04:51:12 +0000266Implements \code{TOS1[TOS] = TOS2}.
Fred Drake456035f1997-12-03 04:06:57 +0000267\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000268
Fred Drake456035f1997-12-03 04:06:57 +0000269\begin{opcodedesc}{DELETE_SUBSCR}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000270Implements \code{del TOS1[TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000271\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000272
Fred Drake456035f1997-12-03 04:06:57 +0000273\begin{opcodedesc}{PRINT_EXPR}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000274Implements the expression statement for the interactive mode. TOS is
275removed from the stack and printed. In non-interactive mode, an
Fred Drake456035f1997-12-03 04:06:57 +0000276expression statement is terminated with \code{POP_STACK}.
277\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000278
Fred Drake456035f1997-12-03 04:06:57 +0000279\begin{opcodedesc}{PRINT_ITEM}{}
Barry Warsaw90876882000-08-21 17:19:00 +0000280Prints TOS to the file-like object bound to \code{sys.stdout}. There
281is one such instruction for each item in the \keyword{print} statement.
282\end{opcodedesc}
283
284\begin{opcodedesc}{PRINT_ITEM_TO}{}
285Like \code{PRINT_ITEM}, but prints the item second from TOS to the
286file-like object at TOS. This is used by the extended print statement.
Fred Drake456035f1997-12-03 04:06:57 +0000287\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000288
Fred Drake456035f1997-12-03 04:06:57 +0000289\begin{opcodedesc}{PRINT_NEWLINE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000290Prints a new line on \code{sys.stdout}. This is generated as the
Fred Drake304faf92000-08-18 02:15:55 +0000291last operation of a \keyword{print} statement, unless the statement
292ends with a comma.
Fred Drake456035f1997-12-03 04:06:57 +0000293\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000294
Barry Warsaw90876882000-08-21 17:19:00 +0000295\begin{opcodedesc}{PRINT_NEWLINE_TO}{}
296Like \code{PRINT_NEWLINE}, but prints the new line on the file-like
297object on the TOS. This is used by the extended print statement.
298\end{opcodedesc}
299
Fred Drake456035f1997-12-03 04:06:57 +0000300\begin{opcodedesc}{BREAK_LOOP}{}
Fred Drake304faf92000-08-18 02:15:55 +0000301Terminates a loop due to a \keyword{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
Thomas Wouters52152252000-08-17 22:55:00 +0000314\begin{opcodedesc}{IMPORT_STAR}{}
Fred Drake304faf92000-08-18 02:15:55 +0000315Loads all symbols not starting with \character{_} directly from the module TOS
Thomas Wouters52152252000-08-17 22:55:00 +0000316to the local namespace. The module is popped after loading all names.
Fred Drake304faf92000-08-18 02:15:55 +0000317This opcode implements \code{from module import *}.
318\end{opcodedesc}
Thomas Wouters52152252000-08-17 22:55:00 +0000319
Fred Drake456035f1997-12-03 04:06:57 +0000320\begin{opcodedesc}{EXEC_STMT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000321Implements \code{exec TOS2,TOS1,TOS}. The compiler fills
Fred Drake304faf92000-08-18 02:15:55 +0000322missing optional parameters with \code{None}.
Fred Drake456035f1997-12-03 04:06:57 +0000323\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000324
Fred Drake456035f1997-12-03 04:06:57 +0000325\begin{opcodedesc}{POP_BLOCK}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000326Removes one block from the block stack. Per frame, there is a
327stack of blocks, denoting nested loops, try statements, and such.
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}{END_FINALLY}{}
Fred Drake304faf92000-08-18 02:15:55 +0000331Terminates a \keyword{finally} clause. The interpreter recalls
332whether the exception has to be re-raised, or whether the function
333returns, and continues with the outer-next block.
Fred Drake456035f1997-12-03 04:06:57 +0000334\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000335
Fred Drake456035f1997-12-03 04:06:57 +0000336\begin{opcodedesc}{BUILD_CLASS}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000337Creates a new class object. TOS is the methods dictionary, TOS1
338the tuple of the names of the base classes, and TOS2 the class name.
Fred Drake456035f1997-12-03 04:06:57 +0000339\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000340
341All of the following opcodes expect arguments. An argument is two
342bytes, with the more significant byte last.
343
Fred Drake456035f1997-12-03 04:06:57 +0000344\begin{opcodedesc}{STORE_NAME}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000345Implements \code{name = TOS}. \var{namei} is the index of \var{name}
Fred Drakedff21a61998-04-03 05:42:10 +0000346in the attribute \member{co_names} of the code object.
Fred Drake456035f1997-12-03 04:06:57 +0000347The compiler tries to use \code{STORE_LOCAL} or \code{STORE_GLOBAL}
348if possible.
349\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000350
Fred Drake456035f1997-12-03 04:06:57 +0000351\begin{opcodedesc}{DELETE_NAME}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000352Implements \code{del name}, where \var{namei} is the index into
Fred Drakedff21a61998-04-03 05:42:10 +0000353\member{co_names} attribute of the code object.
Fred Drake456035f1997-12-03 04:06:57 +0000354\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000355
Thomas Wouters0be5aab2000-08-11 22:15:52 +0000356\begin{opcodedesc}{UNPACK_SEQUENCE}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000357Unpacks TOS into \var{count} individual values, which are put onto
358the stack right-to-left.
Fred Drake456035f1997-12-03 04:06:57 +0000359\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000360
Thomas Wouters0be5aab2000-08-11 22:15:52 +0000361%\begin{opcodedesc}{UNPACK_LIST}{count}
362%This opcode is obsolete.
363%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000364
Fred Drake456035f1997-12-03 04:06:57 +0000365%\begin{opcodedesc}{UNPACK_ARG}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000366%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000367%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000368
Fred Drake456035f1997-12-03 04:06:57 +0000369\begin{opcodedesc}{STORE_ATTR}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000370Implements \code{TOS.name = TOS1}, where \var{namei} is the index
Fred Drakedff21a61998-04-03 05:42:10 +0000371of name in \member{co_names}.
Fred Drake456035f1997-12-03 04:06:57 +0000372\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000373
Fred Drake456035f1997-12-03 04:06:57 +0000374\begin{opcodedesc}{DELETE_ATTR}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000375Implements \code{del TOS.name}, using \var{namei} as index into
Fred Drakedff21a61998-04-03 05:42:10 +0000376\member{co_names}.
Fred Drake456035f1997-12-03 04:06:57 +0000377\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000378
Fred Drake456035f1997-12-03 04:06:57 +0000379\begin{opcodedesc}{STORE_GLOBAL}{namei}
380Works as \code{STORE_NAME}, but stores the name as a global.
381\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000382
Fred Drake456035f1997-12-03 04:06:57 +0000383\begin{opcodedesc}{DELETE_GLOBAL}{namei}
384Works as \code{DELETE_NAME}, but deletes a global name.
385\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000386
Fred Drake456035f1997-12-03 04:06:57 +0000387%\begin{opcodedesc}{UNPACK_VARARG}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000388%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000389%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000390
Fred Drake456035f1997-12-03 04:06:57 +0000391\begin{opcodedesc}{LOAD_CONST}{consti}
Fred Drakedff21a61998-04-03 05:42:10 +0000392Pushes \samp{co_consts[\var{consti}]} onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000393\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000394
Fred Drake456035f1997-12-03 04:06:57 +0000395\begin{opcodedesc}{LOAD_NAME}{namei}
Fred Drakedff21a61998-04-03 05:42:10 +0000396Pushes the value associated with \samp{co_names[\var{namei}]} onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000397\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000398
Fred Drake456035f1997-12-03 04:06:57 +0000399\begin{opcodedesc}{BUILD_TUPLE}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000400Creates a tuple consuming \var{count} items from the stack, and pushes
401the resulting tuple onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000402\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000403
Fred Drake456035f1997-12-03 04:06:57 +0000404\begin{opcodedesc}{BUILD_LIST}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000405Works as \code{BUILD_TUPLE}, but creates a list.
Fred Drake456035f1997-12-03 04:06:57 +0000406\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000407
Fred Drake456035f1997-12-03 04:06:57 +0000408\begin{opcodedesc}{BUILD_MAP}{zero}
Fred Drake304faf92000-08-18 02:15:55 +0000409Pushes a new empty dictionary object onto the stack. The argument is
410ignored and set to zero by the compiler.
Fred Drake456035f1997-12-03 04:06:57 +0000411\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000412
Fred Drake456035f1997-12-03 04:06:57 +0000413\begin{opcodedesc}{LOAD_ATTR}{namei}
Fred Drake304faf92000-08-18 02:15:55 +0000414Replaces TOS with \code{getattr(TOS, co_names[\var{namei}]}.
Fred Drake456035f1997-12-03 04:06:57 +0000415\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000416
Fred Drake456035f1997-12-03 04:06:57 +0000417\begin{opcodedesc}{COMPARE_OP}{opname}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000418Performs a boolean operation. The operation name can be found
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000419in \code{cmp_op[\var{opname}]}.
Fred Drake456035f1997-12-03 04:06:57 +0000420\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000421
Fred Drake456035f1997-12-03 04:06:57 +0000422\begin{opcodedesc}{IMPORT_NAME}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000423Imports the module \code{co_names[\var{namei}]}. The module object is
424pushed onto the stack. The current name space is not affected: for a
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000425proper import statement, a subsequent \code{STORE_FAST} instruction
426modifies the name space.
Fred Drake456035f1997-12-03 04:06:57 +0000427\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000428
Fred Drake456035f1997-12-03 04:06:57 +0000429\begin{opcodedesc}{IMPORT_FROM}{namei}
Thomas Wouters52152252000-08-17 22:55:00 +0000430Loads the attribute \code{co_names[\var{namei}]} from the module found in
431TOS. The resulting object is pushed onto the stack, to be subsequently
432stored by a \code{STORE_FAST} instruction.
Fred Drake456035f1997-12-03 04:06:57 +0000433\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000434
Fred Drake456035f1997-12-03 04:06:57 +0000435\begin{opcodedesc}{JUMP_FORWARD}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000436Increments byte code counter by \var{delta}.
Fred Drake456035f1997-12-03 04:06:57 +0000437\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000438
Fred Drake456035f1997-12-03 04:06:57 +0000439\begin{opcodedesc}{JUMP_IF_TRUE}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000440If TOS is true, increment the byte code counter by \var{delta}. TOS is
441left on 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}{JUMP_IF_FALSE}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000445If TOS is false, increment the byte code counter by \var{delta}. TOS
446is not changed.
Fred Drake456035f1997-12-03 04:06:57 +0000447\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000448
Fred Drake456035f1997-12-03 04:06:57 +0000449\begin{opcodedesc}{JUMP_ABSOLUTE}{target}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000450Set byte code counter to \var{target}.
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}{FOR_LOOP}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000454Iterate over a sequence. TOS is the current index, TOS1 the sequence.
455First, the next element is computed. If the sequence is exhausted,
456increment byte code counter by \var{delta}. Otherwise, push the
457sequence, the incremented counter, and the current item onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000458\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000459
Fred Drake456035f1997-12-03 04:06:57 +0000460%\begin{opcodedesc}{LOAD_LOCAL}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000461%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000462%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000463
Fred Drake456035f1997-12-03 04:06:57 +0000464\begin{opcodedesc}{LOAD_GLOBAL}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000465Loads the global named \code{co_names[\var{namei}]} onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000466\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000467
Fred Drake456035f1997-12-03 04:06:57 +0000468%\begin{opcodedesc}{SET_FUNC_ARGS}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000469%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000470%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000471
Fred Drake456035f1997-12-03 04:06:57 +0000472\begin{opcodedesc}{SETUP_LOOP}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000473Pushes a block for a loop onto the block stack. The block spans
474from the current instruction with a size of \var{delta} bytes.
Fred Drake456035f1997-12-03 04:06:57 +0000475\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000476
Fred Drake456035f1997-12-03 04:06:57 +0000477\begin{opcodedesc}{SETUP_EXCEPT}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000478Pushes a try block from a try-except clause onto the block stack.
479\var{delta} points to the first except block.
Fred Drake456035f1997-12-03 04:06:57 +0000480\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000481
Fred Drake456035f1997-12-03 04:06:57 +0000482\begin{opcodedesc}{SETUP_FINALLY}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000483Pushes a try block from a try-except clause onto the block stack.
484\var{delta} points to the finally block.
Fred Drake456035f1997-12-03 04:06:57 +0000485\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000486
Fred Drake456035f1997-12-03 04:06:57 +0000487\begin{opcodedesc}{LOAD_FAST}{var_num}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000488Pushes a reference to the local \code{co_varnames[\var{var_num}]} onto
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000489the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000490\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000491
Fred Drake456035f1997-12-03 04:06:57 +0000492\begin{opcodedesc}{STORE_FAST}{var_num}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000493Stores TOS into the local \code{co_varnames[\var{var_num}]}.
Fred Drake456035f1997-12-03 04:06:57 +0000494\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000495
Fred Drake456035f1997-12-03 04:06:57 +0000496\begin{opcodedesc}{DELETE_FAST}{var_num}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000497Deletes local \code{co_varnames[\var{var_num}]}.
Fred Drake456035f1997-12-03 04:06:57 +0000498\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000499
Fred Drake338da931999-05-17 20:57:07 +0000500\begin{opcodedesc}{SET_LINENO}{lineno}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000501Sets the current line number to \var{lineno}.
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}{RAISE_VARARGS}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000505Raises an exception. \var{argc} indicates the number of parameters
Fred Drake304faf92000-08-18 02:15:55 +0000506to the raise statement, ranging from 0 to 3. The handler will find
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000507the traceback as TOS2, the parameter as TOS1, and the exception
508as 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}{CALL_FUNCTION}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000512Calls a function. The low byte of \var{argc} indicates the number of
513positional parameters, the high byte the number of keyword parameters.
514On the stack, the opcode finds the keyword parameters first. For each
515keyword argument, the value is on top of the key. Below the keyword
516parameters, the positional parameters are on the stack, with the
517right-most parameter on top. Below the parameters, the function object
518to call is on the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000519\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000520
Fred Drake456035f1997-12-03 04:06:57 +0000521\begin{opcodedesc}{MAKE_FUNCTION}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000522Pushes a new function object on the stack. TOS is the code associated
523with the function. The function object is defined to have \var{argc}
524default parameters, which are found below TOS.
Fred Drake456035f1997-12-03 04:06:57 +0000525\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000526
Fred Drake456035f1997-12-03 04:06:57 +0000527\begin{opcodedesc}{BUILD_SLICE}{argc}
Guido van Rossumda623981998-02-12 03:53:02 +0000528Pushes a slice object on the stack. \var{argc} must be 2 or 3. If it
529is 2, \code{slice(TOS1, TOS)} is pushed; if it is 3,
530\code{slice(TOS2, TOS1, TOS)} is pushed.
Fred Drake304faf92000-08-18 02:15:55 +0000531See the \code{slice()}\bifuncindex{slice} built-in function for more
532information.
Fred Drake456035f1997-12-03 04:06:57 +0000533\end{opcodedesc}
Fred Drake25699f92000-08-17 22:19:26 +0000534
535\begin{opcodedesc}{CALL_FUNCTION_VAR}{argc}
536Calls a function. \var{argc} is interpreted as in \code{CALL_FUNCTION}.
537The top element on the stack contains the variable argument list, followed
538by keyword and positional arguments.
539\end{opcodedesc}
540
541\begin{opcodedesc}{CALL_FUNCTION_KW}{argc}
542Calls a function. \var{argc} is interpreted as in \code{CALL_FUNCTION}.
543The top element on the stack contains the keyword arguments dictionary,
544followed by explicit keyword and positional arguments.
545\end{opcodedesc}
546
547\begin{opcodedesc}{CALL_FUNCTION_VAR_KW}{argc}
548Calls a function. \var{argc} is interpreted as in
549\code{CALL_FUNCTION}. The top element on the stack contains the
550keyword arguments dictionary, followed by the variable-arguments
551tuple, followed by explicit keyword and positional arguments.
552\end{opcodedesc}