blob: 26cc56754b20977ba463ee51883bb053cfbaa7f6 [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
281each item in the 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
286last operation of a print statement, unless the statement ends
287with 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}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000291Terminates a loop due to a 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
Fred Drake456035f1997-12-03 04:06:57 +0000304\begin{opcodedesc}{EXEC_STMT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000305Implements \code{exec TOS2,TOS1,TOS}. The compiler fills
306missing optional parameters with None.
Fred Drake456035f1997-12-03 04:06:57 +0000307\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000308
Fred Drake456035f1997-12-03 04:06:57 +0000309\begin{opcodedesc}{POP_BLOCK}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000310Removes one block from the block stack. Per frame, there is a
311stack of blocks, denoting nested loops, try statements, and such.
Fred Drake456035f1997-12-03 04:06:57 +0000312\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000313
Fred Drake456035f1997-12-03 04:06:57 +0000314\begin{opcodedesc}{END_FINALLY}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000315Terminates a finally-block. The interpreter recalls whether the
316exception has to be re-raised, or whether the function returns,
317and continues with the outer-next block.
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}{BUILD_CLASS}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000321Creates a new class object. TOS is the methods dictionary, TOS1
322the tuple of the names of the base classes, and TOS2 the class name.
Fred Drake456035f1997-12-03 04:06:57 +0000323\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000324
325All of the following opcodes expect arguments. An argument is two
326bytes, with the more significant byte last.
327
Fred Drake456035f1997-12-03 04:06:57 +0000328\begin{opcodedesc}{STORE_NAME}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000329Implements \code{name = TOS}. \var{namei} is the index of \var{name}
Fred Drakedff21a61998-04-03 05:42:10 +0000330in the attribute \member{co_names} of the code object.
Fred Drake456035f1997-12-03 04:06:57 +0000331The compiler tries to use \code{STORE_LOCAL} or \code{STORE_GLOBAL}
332if possible.
333\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000334
Fred Drake456035f1997-12-03 04:06:57 +0000335\begin{opcodedesc}{DELETE_NAME}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000336Implements \code{del name}, where \var{namei} is the index into
Fred Drakedff21a61998-04-03 05:42:10 +0000337\member{co_names} attribute of the code object.
Fred Drake456035f1997-12-03 04:06:57 +0000338\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000339
Fred Drake456035f1997-12-03 04:06:57 +0000340\begin{opcodedesc}{UNPACK_TUPLE}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000341Unpacks TOS into \var{count} individual values, which are put onto
342the stack right-to-left.
Fred Drake456035f1997-12-03 04:06:57 +0000343\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000344
Fred Drake456035f1997-12-03 04:06:57 +0000345\begin{opcodedesc}{UNPACK_LIST}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000346Unpacks TOS into \var{count} individual values.
Fred Drake456035f1997-12-03 04:06:57 +0000347\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000348
Fred Drake456035f1997-12-03 04:06:57 +0000349%\begin{opcodedesc}{UNPACK_ARG}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000350%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000351%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000352
Fred Drake456035f1997-12-03 04:06:57 +0000353\begin{opcodedesc}{STORE_ATTR}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000354Implements \code{TOS.name = TOS1}, where \var{namei} is the index
Fred Drakedff21a61998-04-03 05:42:10 +0000355of name in \member{co_names}.
Fred Drake456035f1997-12-03 04:06:57 +0000356\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000357
Fred Drake456035f1997-12-03 04:06:57 +0000358\begin{opcodedesc}{DELETE_ATTR}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000359Implements \code{del TOS.name}, using \var{namei} as index into
Fred Drakedff21a61998-04-03 05:42:10 +0000360\member{co_names}.
Fred Drake456035f1997-12-03 04:06:57 +0000361\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000362
Fred Drake456035f1997-12-03 04:06:57 +0000363\begin{opcodedesc}{STORE_GLOBAL}{namei}
364Works as \code{STORE_NAME}, but stores the name as a global.
365\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000366
Fred Drake456035f1997-12-03 04:06:57 +0000367\begin{opcodedesc}{DELETE_GLOBAL}{namei}
368Works as \code{DELETE_NAME}, but deletes a global name.
369\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000370
Fred Drake456035f1997-12-03 04:06:57 +0000371%\begin{opcodedesc}{UNPACK_VARARG}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000372%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000373%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000374
Fred Drake456035f1997-12-03 04:06:57 +0000375\begin{opcodedesc}{LOAD_CONST}{consti}
Fred Drakedff21a61998-04-03 05:42:10 +0000376Pushes \samp{co_consts[\var{consti}]} onto the stack.
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}{LOAD_NAME}{namei}
Fred Drakedff21a61998-04-03 05:42:10 +0000380Pushes the value associated with \samp{co_names[\var{namei}]} onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000381\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000382
Fred Drake456035f1997-12-03 04:06:57 +0000383\begin{opcodedesc}{BUILD_TUPLE}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000384Creates a tuple consuming \var{count} items from the stack, and pushes
385the resulting tuple onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000386\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000387
Fred Drake456035f1997-12-03 04:06:57 +0000388\begin{opcodedesc}{BUILD_LIST}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000389Works as \code{BUILD_TUPLE}, but creates a list.
Fred Drake456035f1997-12-03 04:06:57 +0000390\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000391
Fred Drake456035f1997-12-03 04:06:57 +0000392\begin{opcodedesc}{BUILD_MAP}{zero}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000393Pushes an empty dictionary object onto the stack. The argument is ignored
394and set to zero by the compiler.
Fred Drake456035f1997-12-03 04:06:57 +0000395\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000396
Fred Drake456035f1997-12-03 04:06:57 +0000397\begin{opcodedesc}{LOAD_ATTR}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000398Replaces TOS with \code{getattr(TOS,co_names[\var{namei}]}.
Fred Drake456035f1997-12-03 04:06:57 +0000399\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000400
Fred Drake456035f1997-12-03 04:06:57 +0000401\begin{opcodedesc}{COMPARE_OP}{opname}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000402Performs a boolean operation. The operation name can be found
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000403in \code{cmp_op[\var{opname}]}.
Fred Drake456035f1997-12-03 04:06:57 +0000404\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000405
Fred Drake456035f1997-12-03 04:06:57 +0000406\begin{opcodedesc}{IMPORT_NAME}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000407Imports the module \code{co_names[\var{namei}]}. The module object is
408pushed onto the stack. The current name space is not affected: for a
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000409proper import statement, a subsequent \code{STORE_FAST} instruction
410modifies the name space.
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}{IMPORT_FROM}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000414Imports the attribute \code{co_names[\var{namei}]}. The module to import
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000415from is found in TOS and left there.
Fred Drake456035f1997-12-03 04:06:57 +0000416\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000417
Fred Drake456035f1997-12-03 04:06:57 +0000418\begin{opcodedesc}{JUMP_FORWARD}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000419Increments byte code counter by \var{delta}.
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}{JUMP_IF_TRUE}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000423If TOS is true, increment the byte code counter by \var{delta}. TOS is
424left on the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000425\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000426
Fred Drake456035f1997-12-03 04:06:57 +0000427\begin{opcodedesc}{JUMP_IF_FALSE}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000428If TOS is false, increment the byte code counter by \var{delta}. TOS
429is not changed.
Fred Drake456035f1997-12-03 04:06:57 +0000430\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000431
Fred Drake456035f1997-12-03 04:06:57 +0000432\begin{opcodedesc}{JUMP_ABSOLUTE}{target}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000433Set byte code counter to \var{target}.
Fred Drake456035f1997-12-03 04:06:57 +0000434\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000435
Fred Drake456035f1997-12-03 04:06:57 +0000436\begin{opcodedesc}{FOR_LOOP}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000437Iterate over a sequence. TOS is the current index, TOS1 the sequence.
438First, the next element is computed. If the sequence is exhausted,
439increment byte code counter by \var{delta}. Otherwise, push the
440sequence, the incremented counter, and the current item onto the stack.
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}{LOAD_LOCAL}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000444%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000445%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000446
Fred Drake456035f1997-12-03 04:06:57 +0000447\begin{opcodedesc}{LOAD_GLOBAL}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000448Loads the global named \code{co_names[\var{namei}]} onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000449\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000450
Fred Drake456035f1997-12-03 04:06:57 +0000451%\begin{opcodedesc}{SET_FUNC_ARGS}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000452%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000453%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000454
Fred Drake456035f1997-12-03 04:06:57 +0000455\begin{opcodedesc}{SETUP_LOOP}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000456Pushes a block for a loop onto the block stack. The block spans
457from the current instruction with a size of \var{delta} bytes.
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}{SETUP_EXCEPT}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000461Pushes a try block from a try-except clause onto the block stack.
462\var{delta} points to the first except block.
Fred Drake456035f1997-12-03 04:06:57 +0000463\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000464
Fred Drake456035f1997-12-03 04:06:57 +0000465\begin{opcodedesc}{SETUP_FINALLY}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000466Pushes a try block from a try-except clause onto the block stack.
467\var{delta} points to the finally block.
Fred Drake456035f1997-12-03 04:06:57 +0000468\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000469
Fred Drake456035f1997-12-03 04:06:57 +0000470\begin{opcodedesc}{LOAD_FAST}{var_num}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000471Pushes a reference to the local \code{co_varnames[\var{var_num}]} onto
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000472the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000473\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000474
Fred Drake456035f1997-12-03 04:06:57 +0000475\begin{opcodedesc}{STORE_FAST}{var_num}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000476Stores TOS into the local \code{co_varnames[\var{var_num}]}.
Fred Drake456035f1997-12-03 04:06:57 +0000477\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000478
Fred Drake456035f1997-12-03 04:06:57 +0000479\begin{opcodedesc}{DELETE_FAST}{var_num}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000480Deletes local \code{co_varnames[\var{var_num}]}.
Fred Drake456035f1997-12-03 04:06:57 +0000481\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000482
Fred Drake338da931999-05-17 20:57:07 +0000483\begin{opcodedesc}{SET_LINENO}{lineno}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000484Sets the current line number to \var{lineno}.
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}{RAISE_VARARGS}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000488Raises an exception. \var{argc} indicates the number of parameters
489to the raise statement, ranging from 1 to 3. The handler will find
490the traceback as TOS2, the parameter as TOS1, and the exception
491as TOS.
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}{CALL_FUNCTION}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000495Calls a function. The low byte of \var{argc} indicates the number of
496positional parameters, the high byte the number of keyword parameters.
497On the stack, the opcode finds the keyword parameters first. For each
498keyword argument, the value is on top of the key. Below the keyword
499parameters, the positional parameters are on the stack, with the
500right-most parameter on top. Below the parameters, the function object
501to call is on the stack.
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}{MAKE_FUNCTION}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000505Pushes a new function object on the stack. TOS is the code associated
506with the function. The function object is defined to have \var{argc}
507default parameters, which are found below TOS.
Fred Drake456035f1997-12-03 04:06:57 +0000508\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000509
Fred Drake456035f1997-12-03 04:06:57 +0000510\begin{opcodedesc}{BUILD_SLICE}{argc}
Guido van Rossumda623981998-02-12 03:53:02 +0000511Pushes a slice object on the stack. \var{argc} must be 2 or 3. If it
512is 2, \code{slice(TOS1, TOS)} is pushed; if it is 3,
513\code{slice(TOS2, TOS1, TOS)} is pushed.
Fred Drake19479911998-02-13 06:58:54 +0000514See the \code{slice()}\bifuncindex{slice} built-in function.
Fred Drake456035f1997-12-03 04:06:57 +0000515\end{opcodedesc}