blob: 984ef1ce9725cb87e54c8359e18b6023eb999d85 [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}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000280Prints TOS. There is one such instruction for
Fred Drake304faf92000-08-18 02:15:55 +0000281each item in the \keyword{print} statement.
Fred Drake456035f1997-12-03 04:06:57 +0000282\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000283
Fred Drake456035f1997-12-03 04:06:57 +0000284\begin{opcodedesc}{PRINT_NEWLINE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000285Prints a new line on \code{sys.stdout}. This is generated as the
Fred Drake304faf92000-08-18 02:15:55 +0000286last operation of a \keyword{print} statement, unless the statement
287ends with a comma.
Fred Drake456035f1997-12-03 04:06:57 +0000288\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000289
Fred Drake456035f1997-12-03 04:06:57 +0000290\begin{opcodedesc}{BREAK_LOOP}{}
Fred Drake304faf92000-08-18 02:15:55 +0000291Terminates a loop due to a \keyword{break} 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}{LOAD_LOCALS}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000295Pushes a reference to the locals of the current scope on the stack.
296This is used in the code for a class definition: After the class body
297is evaluated, the locals are passed to the class definition.
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}{RETURN_VALUE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000301Returns with TOS to the caller of the function.
Fred Drake456035f1997-12-03 04:06:57 +0000302\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000303
Thomas Wouters52152252000-08-17 22:55:00 +0000304\begin{opcodedesc}{IMPORT_STAR}{}
Fred Drake304faf92000-08-18 02:15:55 +0000305Loads all symbols not starting with \character{_} directly from the module TOS
Thomas Wouters52152252000-08-17 22:55:00 +0000306to the local namespace. The module is popped after loading all names.
Fred Drake304faf92000-08-18 02:15:55 +0000307This opcode implements \code{from module import *}.
308\end{opcodedesc}
Thomas Wouters52152252000-08-17 22:55:00 +0000309
Fred Drake456035f1997-12-03 04:06:57 +0000310\begin{opcodedesc}{EXEC_STMT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000311Implements \code{exec TOS2,TOS1,TOS}. The compiler fills
Fred Drake304faf92000-08-18 02:15:55 +0000312missing optional parameters with \code{None}.
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}{POP_BLOCK}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000316Removes one block from the block stack. Per frame, there is a
317stack of blocks, denoting nested loops, try statements, and such.
Fred Drake456035f1997-12-03 04:06:57 +0000318\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000319
Fred Drake456035f1997-12-03 04:06:57 +0000320\begin{opcodedesc}{END_FINALLY}{}
Fred Drake304faf92000-08-18 02:15:55 +0000321Terminates a \keyword{finally} clause. The interpreter recalls
322whether the exception has to be re-raised, or whether the function
323returns, and continues with the outer-next block.
Fred Drake456035f1997-12-03 04:06:57 +0000324\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000325
Fred Drake456035f1997-12-03 04:06:57 +0000326\begin{opcodedesc}{BUILD_CLASS}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000327Creates a new class object. TOS is the methods dictionary, TOS1
328the tuple of the names of the base classes, and TOS2 the class name.
Fred Drake456035f1997-12-03 04:06:57 +0000329\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000330
331All of the following opcodes expect arguments. An argument is two
332bytes, with the more significant byte last.
333
Fred Drake456035f1997-12-03 04:06:57 +0000334\begin{opcodedesc}{STORE_NAME}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000335Implements \code{name = TOS}. \var{namei} is the index of \var{name}
Fred Drakedff21a61998-04-03 05:42:10 +0000336in the attribute \member{co_names} of the code object.
Fred Drake456035f1997-12-03 04:06:57 +0000337The compiler tries to use \code{STORE_LOCAL} or \code{STORE_GLOBAL}
338if possible.
339\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000340
Fred Drake456035f1997-12-03 04:06:57 +0000341\begin{opcodedesc}{DELETE_NAME}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000342Implements \code{del name}, where \var{namei} is the index into
Fred Drakedff21a61998-04-03 05:42:10 +0000343\member{co_names} attribute of the code object.
Fred Drake456035f1997-12-03 04:06:57 +0000344\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000345
Thomas Wouters0be5aab2000-08-11 22:15:52 +0000346\begin{opcodedesc}{UNPACK_SEQUENCE}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000347Unpacks TOS into \var{count} individual values, which are put onto
348the stack right-to-left.
Fred Drake456035f1997-12-03 04:06:57 +0000349\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000350
Thomas Wouters0be5aab2000-08-11 22:15:52 +0000351%\begin{opcodedesc}{UNPACK_LIST}{count}
352%This opcode is obsolete.
353%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000354
Fred Drake456035f1997-12-03 04:06:57 +0000355%\begin{opcodedesc}{UNPACK_ARG}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000356%This opcode is obsolete.
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}{STORE_ATTR}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000360Implements \code{TOS.name = TOS1}, where \var{namei} is the index
Fred Drakedff21a61998-04-03 05:42:10 +0000361of name in \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}{DELETE_ATTR}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000365Implements \code{del TOS.name}, using \var{namei} as index into
Fred Drakedff21a61998-04-03 05:42:10 +0000366\member{co_names}.
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_GLOBAL}{namei}
370Works as \code{STORE_NAME}, but stores the name as a global.
371\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000372
Fred Drake456035f1997-12-03 04:06:57 +0000373\begin{opcodedesc}{DELETE_GLOBAL}{namei}
374Works as \code{DELETE_NAME}, but deletes a global name.
375\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000376
Fred Drake456035f1997-12-03 04:06:57 +0000377%\begin{opcodedesc}{UNPACK_VARARG}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000378%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000379%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000380
Fred Drake456035f1997-12-03 04:06:57 +0000381\begin{opcodedesc}{LOAD_CONST}{consti}
Fred Drakedff21a61998-04-03 05:42:10 +0000382Pushes \samp{co_consts[\var{consti}]} onto the stack.
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_NAME}{namei}
Fred Drakedff21a61998-04-03 05:42:10 +0000386Pushes the value associated with \samp{co_names[\var{namei}]} 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_TUPLE}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000390Creates a tuple consuming \var{count} items from the stack, and pushes
391the resulting tuple onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000392\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000393
Fred Drake456035f1997-12-03 04:06:57 +0000394\begin{opcodedesc}{BUILD_LIST}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000395Works as \code{BUILD_TUPLE}, but creates a list.
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_MAP}{zero}
Fred Drake304faf92000-08-18 02:15:55 +0000399Pushes a new empty dictionary object onto the stack. The argument is
400ignored and set to zero by the compiler.
Fred Drake456035f1997-12-03 04:06:57 +0000401\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000402
Fred Drake456035f1997-12-03 04:06:57 +0000403\begin{opcodedesc}{LOAD_ATTR}{namei}
Fred Drake304faf92000-08-18 02:15:55 +0000404Replaces TOS with \code{getattr(TOS, co_names[\var{namei}]}.
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}{COMPARE_OP}{opname}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000408Performs a boolean operation. The operation name can be found
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000409in \code{cmp_op[\var{opname}]}.
Fred Drake456035f1997-12-03 04:06:57 +0000410\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000411
Fred Drake456035f1997-12-03 04:06:57 +0000412\begin{opcodedesc}{IMPORT_NAME}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000413Imports the module \code{co_names[\var{namei}]}. The module object is
414pushed onto the stack. The current name space is not affected: for a
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000415proper import statement, a subsequent \code{STORE_FAST} instruction
416modifies the name space.
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}{IMPORT_FROM}{namei}
Thomas Wouters52152252000-08-17 22:55:00 +0000420Loads the attribute \code{co_names[\var{namei}]} from the module found in
421TOS. The resulting object is pushed onto the stack, to be subsequently
422stored by a \code{STORE_FAST} instruction.
Fred Drake456035f1997-12-03 04:06:57 +0000423\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000424
Fred Drake456035f1997-12-03 04:06:57 +0000425\begin{opcodedesc}{JUMP_FORWARD}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000426Increments byte code counter by \var{delta}.
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}{JUMP_IF_TRUE}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000430If TOS is true, increment the byte code counter by \var{delta}. TOS is
431left on the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000432\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000433
Fred Drake456035f1997-12-03 04:06:57 +0000434\begin{opcodedesc}{JUMP_IF_FALSE}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000435If TOS is false, increment the byte code counter by \var{delta}. TOS
436is not changed.
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_ABSOLUTE}{target}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000440Set byte code counter to \var{target}.
Fred Drake456035f1997-12-03 04:06:57 +0000441\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000442
Fred Drake456035f1997-12-03 04:06:57 +0000443\begin{opcodedesc}{FOR_LOOP}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000444Iterate over a sequence. TOS is the current index, TOS1 the sequence.
445First, the next element is computed. If the sequence is exhausted,
446increment byte code counter by \var{delta}. Otherwise, push the
447sequence, the incremented counter, and the current item onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000448\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000449
Fred Drake456035f1997-12-03 04:06:57 +0000450%\begin{opcodedesc}{LOAD_LOCAL}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000451%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000452%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000453
Fred Drake456035f1997-12-03 04:06:57 +0000454\begin{opcodedesc}{LOAD_GLOBAL}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000455Loads the global named \code{co_names[\var{namei}]} onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000456\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000457
Fred Drake456035f1997-12-03 04:06:57 +0000458%\begin{opcodedesc}{SET_FUNC_ARGS}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000459%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000460%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000461
Fred Drake456035f1997-12-03 04:06:57 +0000462\begin{opcodedesc}{SETUP_LOOP}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000463Pushes a block for a loop onto the block stack. The block spans
464from the current instruction with a size of \var{delta} bytes.
Fred Drake456035f1997-12-03 04:06:57 +0000465\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000466
Fred Drake456035f1997-12-03 04:06:57 +0000467\begin{opcodedesc}{SETUP_EXCEPT}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000468Pushes a try block from a try-except clause onto the block stack.
469\var{delta} points to the first except block.
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_FINALLY}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000473Pushes a try block from a try-except clause onto the block stack.
474\var{delta} points to the finally block.
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}{LOAD_FAST}{var_num}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000478Pushes a reference to the local \code{co_varnames[\var{var_num}]} onto
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000479the stack.
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}{STORE_FAST}{var_num}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000483Stores TOS into the local \code{co_varnames[\var{var_num}]}.
Fred Drake456035f1997-12-03 04:06:57 +0000484\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000485
Fred Drake456035f1997-12-03 04:06:57 +0000486\begin{opcodedesc}{DELETE_FAST}{var_num}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000487Deletes local \code{co_varnames[\var{var_num}]}.
Fred Drake456035f1997-12-03 04:06:57 +0000488\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000489
Fred Drake338da931999-05-17 20:57:07 +0000490\begin{opcodedesc}{SET_LINENO}{lineno}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000491Sets the current line number to \var{lineno}.
Fred Drake456035f1997-12-03 04:06:57 +0000492\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000493
Fred Drake456035f1997-12-03 04:06:57 +0000494\begin{opcodedesc}{RAISE_VARARGS}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000495Raises an exception. \var{argc} indicates the number of parameters
Fred Drake304faf92000-08-18 02:15:55 +0000496to the raise statement, ranging from 0 to 3. The handler will find
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000497the traceback as TOS2, the parameter as TOS1, and the exception
498as TOS.
Fred Drake456035f1997-12-03 04:06:57 +0000499\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000500
Fred Drake456035f1997-12-03 04:06:57 +0000501\begin{opcodedesc}{CALL_FUNCTION}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000502Calls a function. The low byte of \var{argc} indicates the number of
503positional parameters, the high byte the number of keyword parameters.
504On the stack, the opcode finds the keyword parameters first. For each
505keyword argument, the value is on top of the key. Below the keyword
506parameters, the positional parameters are on the stack, with the
507right-most parameter on top. Below the parameters, the function object
508to call is on the stack.
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}{MAKE_FUNCTION}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000512Pushes a new function object on the stack. TOS is the code associated
513with the function. The function object is defined to have \var{argc}
514default parameters, which are found below TOS.
Fred Drake456035f1997-12-03 04:06:57 +0000515\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000516
Fred Drake456035f1997-12-03 04:06:57 +0000517\begin{opcodedesc}{BUILD_SLICE}{argc}
Guido van Rossumda623981998-02-12 03:53:02 +0000518Pushes a slice object on the stack. \var{argc} must be 2 or 3. If it
519is 2, \code{slice(TOS1, TOS)} is pushed; if it is 3,
520\code{slice(TOS2, TOS1, TOS)} is pushed.
Fred Drake304faf92000-08-18 02:15:55 +0000521See the \code{slice()}\bifuncindex{slice} built-in function for more
522information.
Fred Drake456035f1997-12-03 04:06:57 +0000523\end{opcodedesc}
Fred Drake25699f92000-08-17 22:19:26 +0000524
525\begin{opcodedesc}{CALL_FUNCTION_VAR}{argc}
526Calls a function. \var{argc} is interpreted as in \code{CALL_FUNCTION}.
527The top element on the stack contains the variable argument list, followed
528by keyword and positional arguments.
529\end{opcodedesc}
530
531\begin{opcodedesc}{CALL_FUNCTION_KW}{argc}
532Calls a function. \var{argc} is interpreted as in \code{CALL_FUNCTION}.
533The top element on the stack contains the keyword arguments dictionary,
534followed by explicit keyword and positional arguments.
535\end{opcodedesc}
536
537\begin{opcodedesc}{CALL_FUNCTION_VAR_KW}{argc}
538Calls a function. \var{argc} is interpreted as in
539\code{CALL_FUNCTION}. The top element on the stack contains the
540keyword arguments dictionary, followed by the variable-arguments
541tuple, followed by explicit keyword and positional arguments.
542\end{opcodedesc}