blob: 9c7fee6e9fa76a31ccb59d2de7b813c8a1cc9495 [file] [log] [blame]
Fred Drakedff21a61998-04-03 05:42:10 +00001\section{Standard Module \module{dis}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{standard}{dis}
3
4\modulesynopsis{Disassembler.}
5
Guido van Rossumb62b6d11997-11-18 15:10:53 +00006
Fred Drakedff21a61998-04-03 05:42:10 +00007The \module{dis} module supports the analysis of Python byte code by
Guido van Rossumb62b6d11997-11-18 15:10:53 +00008disassembling it. Since there is no Python assembler, this module
9defines the Python assembly language. The Python byte code which
10this module takes as an input is defined in the file
Fred Drake456035f1997-12-03 04:06:57 +000011\file{Include/opcode.h} and used by the compiler and the interpreter.
Guido van Rossumb62b6d11997-11-18 15:10:53 +000012
Fred Drakedff21a61998-04-03 05:42:10 +000013Example: Given the function \function{myfunc}:
Guido van Rossumb62b6d11997-11-18 15:10:53 +000014
Fred Drake19479911998-02-13 06:58:54 +000015\begin{verbatim}
Guido van Rossumb62b6d11997-11-18 15:10:53 +000016def myfunc(alist):
Fred Drakedff21a61998-04-03 05:42:10 +000017 return len(alist)
Fred Drake19479911998-02-13 06:58:54 +000018\end{verbatim}
Guido van Rossumb62b6d11997-11-18 15:10:53 +000019
Fred Drakedff21a61998-04-03 05:42:10 +000020the following command can be used to get the disassembly of
21\function{myfunc()}:
Guido van Rossumb62b6d11997-11-18 15:10:53 +000022
23\begin{verbatim}
24>>> dis.dis(myfunc)
25 0 SET_LINENO 1
26
27 3 SET_LINENO 2
28 6 LOAD_GLOBAL 0 (len)
29 9 LOAD_FAST 0 (alist)
30 12 CALL_FUNCTION 1
31 15 RETURN_VALUE
32 16 LOAD_CONST 0 (None)
33 19 RETURN_VALUE
34\end{verbatim}
35
Fred Drakedff21a61998-04-03 05:42:10 +000036The \module{dis} module defines the following functions:
Guido van Rossumb62b6d11997-11-18 15:10:53 +000037
Guido van Rossumb62b6d11997-11-18 15:10:53 +000038\begin{funcdesc}{dis}{\optional{bytesource}}
39Disassemble the \var{bytesource} object. \var{bytesource} can denote
40either a class, a method, a function, or a code object. For a class,
41it disassembles all methods. For a single code sequence, it prints
42one line per byte code instruction. If no object is provided, it
43disassembles the last traceback.
44\end{funcdesc}
45
46\begin{funcdesc}{distb}{\optional{tb}}
47Disassembles the top-of-stack function of a traceback, using the last
48traceback if none was passed. The instruction causing the exception
49is indicated.
50\end{funcdesc}
51
Fred Drakecce10901998-03-17 06:33:25 +000052\begin{funcdesc}{disassemble}{code\optional{, lasti}}
Guido van Rossumb62b6d11997-11-18 15:10:53 +000053Disassembles a code object, indicating the last instruction if \var{lasti}
54was provided. The output is divided in the following columns:
Fred Drake810349b1998-04-07 14:16:41 +000055
Fred Drake3e7a48e1998-04-13 16:15:02 +000056\begin{enumerate}
Fred Drakedff21a61998-04-03 05:42:10 +000057\item the current instruction, indicated as \samp{-->},
58\item a labelled instruction, indicated with \samp{>>},
Guido van Rossumb62b6d11997-11-18 15:10:53 +000059\item the address of the instruction,
60\item the operation code name,
61\item operation parameters, and
62\item interpretation of the parameters in parentheses.
Fred Drake3e7a48e1998-04-13 16:15:02 +000063\end{enumerate}
Fred Drake810349b1998-04-07 14:16:41 +000064
Guido van Rossumb62b6d11997-11-18 15:10:53 +000065The parameter interpretation recognizes local and global
66variable names, constant values, branch targets, and compare
67operators.
68\end{funcdesc}
69
Fred Drakecce10901998-03-17 06:33:25 +000070\begin{funcdesc}{disco}{code\optional{, lasti}}
Guido van Rossumb62b6d11997-11-18 15:10:53 +000071A synonym for disassemble. It is more convenient to type, and kept
72for compatibility with earlier Python releases.
73\end{funcdesc}
74
75\begin{datadesc}{opname}
76Sequence of a operation names, indexable using the byte code.
77\end{datadesc}
78
79\begin{datadesc}{cmp_op}
80Sequence of all compare operation names.
81\end{datadesc}
82
83\begin{datadesc}{hasconst}
84Sequence of byte codes that have a constant parameter.
85\end{datadesc}
86
87\begin{datadesc}{hasname}
88Sequence of byte codes that access a attribute by name.
89\end{datadesc}
90
91\begin{datadesc}{hasjrel}
92Sequence of byte codes that have a relative jump target.
93\end{datadesc}
94
95\begin{datadesc}{hasjabs}
96Sequence of byte codes that have an absolute jump target.
97\end{datadesc}
98
99\begin{datadesc}{haslocal}
100Sequence of byte codes that access a a local variable.
101\end{datadesc}
102
103\begin{datadesc}{hascompare}
104Sequence of byte codes of boolean operations.
105\end{datadesc}
106
107\subsection{Python Byte Code Instructions}
Fred Drake83efb541998-02-19 20:07:39 +0000108\label{bytecodes}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000109
110The Python compiler currently generates the following byte code
111instructions.
112
Fred Drake19479911998-02-13 06:58:54 +0000113\setindexsubitem{(byte code insns)}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000114
Fred Drake456035f1997-12-03 04:06:57 +0000115\begin{opcodedesc}{STOP_CODE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000116Indicates end-of-code to the compiler, not used by the interpreter.
Fred Drake456035f1997-12-03 04:06:57 +0000117\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000118
Fred Drake456035f1997-12-03 04:06:57 +0000119\begin{opcodedesc}{POP_TOP}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000120Removes the top-of-stack (TOS) item.
Fred Drake456035f1997-12-03 04:06:57 +0000121\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000122
Fred Drake456035f1997-12-03 04:06:57 +0000123\begin{opcodedesc}{ROT_TWO}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000124Swaps the two top-most stack items.
Fred Drake456035f1997-12-03 04:06:57 +0000125\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000126
Fred Drake456035f1997-12-03 04:06:57 +0000127\begin{opcodedesc}{ROT_THREE}{}
128Lifts second and third stack item one position up, moves top down
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000129to position three.
Fred Drake456035f1997-12-03 04:06:57 +0000130\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000131
Fred Drake456035f1997-12-03 04:06:57 +0000132\begin{opcodedesc}{DUP_TOP}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000133Duplicates the reference on top of the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000134\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000135
136Unary Operations take the top of the stack, apply the operation, and
137push the result back on the stack.
138
Fred Drake456035f1997-12-03 04:06:57 +0000139\begin{opcodedesc}{UNARY_POSITIVE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000140Implements \code{TOS = +TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000141\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000142
Fred Drake456035f1997-12-03 04:06:57 +0000143\begin{opcodedesc}{UNARY_NEG}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000144Implements \code{TOS = -TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000145\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000146
Fred Drake456035f1997-12-03 04:06:57 +0000147\begin{opcodedesc}{UNARY_NOT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000148Implements \code{TOS = not TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000149\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000150
Fred Drake456035f1997-12-03 04:06:57 +0000151\begin{opcodedesc}{UNARY_CONVERT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000152Implements \code{TOS = `TOS`}.
Fred Drake456035f1997-12-03 04:06:57 +0000153\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000154
Fred Drake456035f1997-12-03 04:06:57 +0000155\begin{opcodedesc}{UNARY_INVERT}{}
Guido van Rossume903aab1997-12-18 16:28:56 +0000156Implements \code{TOS = \~TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000157\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000158
159Binary operations remove the top of the stack (TOS) and the second top-most
160stack item (TOS1) from the stack. They perform the operation, and put the
161result back on the stack.
162
Fred Drake456035f1997-12-03 04:06:57 +0000163\begin{opcodedesc}{BINARY_POWER}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000164Implements \code{TOS = TOS1 ** TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000165\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000166
Fred Drake456035f1997-12-03 04:06:57 +0000167\begin{opcodedesc}{BINARY_MULTIPLY}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000168Implements \code{TOS = TOS1 * TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000169\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000170
Fred Drake456035f1997-12-03 04:06:57 +0000171\begin{opcodedesc}{BINARY_DIVIDE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000172Implements \code{TOS = TOS1 / TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000173\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000174
Fred Drake456035f1997-12-03 04:06:57 +0000175\begin{opcodedesc}{BINARY_MODULO}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000176Implements \code{TOS = TOS1 \% TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000177\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000178
Fred Drake456035f1997-12-03 04:06:57 +0000179\begin{opcodedesc}{BINARY_ADD}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000180Implements \code{TOS = TOS1 + TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000181\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000182
Fred Drake456035f1997-12-03 04:06:57 +0000183\begin{opcodedesc}{BINARY_SUBTRACT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000184Implements \code{TOS = TOS1 - TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000185\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000186
Fred Drake456035f1997-12-03 04:06:57 +0000187\begin{opcodedesc}{BINARY_SUBSCR}{}
Fred Drake1cf87491997-12-04 04:57:56 +0000188Implements \code{TOS = TOS1[TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000189\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000190
Fred Drake456035f1997-12-03 04:06:57 +0000191\begin{opcodedesc}{BINARY_LSHIFT}{}
Fred Drake1cf87491997-12-04 04:57:56 +0000192Implements \code{TOS = TOS1 << TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000193\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000194
Fred Drake456035f1997-12-03 04:06:57 +0000195\begin{opcodedesc}{BINARY_RSHIFT}{}
Fred Drake1cf87491997-12-04 04:57:56 +0000196Implements \code{TOS = TOS1 >> TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000197\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000198
Fred Drake456035f1997-12-03 04:06:57 +0000199\begin{opcodedesc}{BINARY_AND}{}
Fred Drake1cf87491997-12-04 04:57:56 +0000200Implements \code{TOS = TOS1 and TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000201\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000202
Fred Drake456035f1997-12-03 04:06:57 +0000203\begin{opcodedesc}{BINARY_XOR}{}
Fred Draked7feffd1997-12-29 20:02:55 +0000204Implements \code{TOS = TOS1 \^\ TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000205\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000206
Fred Drake456035f1997-12-03 04:06:57 +0000207\begin{opcodedesc}{BINARY_OR}{}
Fred Drake1cf87491997-12-04 04:57:56 +0000208Implements \code{TOS = TOS1 or TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000209\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000210
211The slice opcodes take up to three parameters.
212
Fred Drake456035f1997-12-03 04:06:57 +0000213\begin{opcodedesc}{SLICE+0}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000214Implements \code{TOS = TOS[:]}.
Fred Drake456035f1997-12-03 04:06:57 +0000215\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000216
Fred Drake456035f1997-12-03 04:06:57 +0000217\begin{opcodedesc}{SLICE+1}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000218Implements \code{TOS = TOS1[TOS:]}.
Fred Drake456035f1997-12-03 04:06:57 +0000219\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000220
Fred Drake456035f1997-12-03 04:06:57 +0000221\begin{opcodedesc}{SLICE+2}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000222Implements \code{TOS = TOS1[:TOS1]}.
Fred Drake456035f1997-12-03 04:06:57 +0000223\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000224
Fred Drake456035f1997-12-03 04:06:57 +0000225\begin{opcodedesc}{SLICE+3}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000226Implements \code{TOS = TOS2[TOS1:TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000227\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000228
229Slice assignment needs even an additional parameter. As any statement,
230they put nothing on the stack.
231
Fred Drake456035f1997-12-03 04:06:57 +0000232\begin{opcodedesc}{STORE_SLICE+0}{}
Fred Drake7381e281997-12-04 04:51:12 +0000233Implements \code{TOS[:] = TOS1}.
Fred Drake456035f1997-12-03 04:06:57 +0000234\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000235
Fred Drake456035f1997-12-03 04:06:57 +0000236\begin{opcodedesc}{STORE_SLICE+1}{}
Fred Drake7381e281997-12-04 04:51:12 +0000237Implements \code{TOS1[TOS:] = TOS2}.
Fred Drake456035f1997-12-03 04:06:57 +0000238\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000239
Fred Drake456035f1997-12-03 04:06:57 +0000240\begin{opcodedesc}{STORE_SLICE+2}{}
Fred Drake7381e281997-12-04 04:51:12 +0000241Implements \code{TOS1[:TOS] = TOS2}.
Fred Drake456035f1997-12-03 04:06:57 +0000242\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000243
Fred Drake456035f1997-12-03 04:06:57 +0000244\begin{opcodedesc}{STORE_SLICE+3}{}
Fred Drake7381e281997-12-04 04:51:12 +0000245Implements \code{TOS2[TOS1:TOS] = TOS3}.
Fred Drake456035f1997-12-03 04:06:57 +0000246\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000247
Fred Drake456035f1997-12-03 04:06:57 +0000248\begin{opcodedesc}{DELETE_SLICE+0}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000249Implements \code{del TOS[:]}.
Fred Drake456035f1997-12-03 04:06:57 +0000250\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000251
Fred Drake456035f1997-12-03 04:06:57 +0000252\begin{opcodedesc}{DELETE_SLICE+1}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000253Implements \code{del TOS1[TOS:]}.
Fred Drake456035f1997-12-03 04:06:57 +0000254\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000255
Fred Drake456035f1997-12-03 04:06:57 +0000256\begin{opcodedesc}{DELETE_SLICE+2}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000257Implements \code{del TOS1[:TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000258\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000259
Fred Drake456035f1997-12-03 04:06:57 +0000260\begin{opcodedesc}{DELETE_SLICE+3}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000261Implements \code{del TOS2[TOS1:TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000262\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000263
Fred Drake456035f1997-12-03 04:06:57 +0000264\begin{opcodedesc}{STORE_SUBSCR}{}
Fred Drake7381e281997-12-04 04:51:12 +0000265Implements \code{TOS1[TOS] = TOS2}.
Fred Drake456035f1997-12-03 04:06:57 +0000266\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000267
Fred Drake456035f1997-12-03 04:06:57 +0000268\begin{opcodedesc}{DELETE_SUBSCR}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000269Implements \code{del TOS1[TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000270\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000271
Fred Drake456035f1997-12-03 04:06:57 +0000272\begin{opcodedesc}{PRINT_EXPR}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000273Implements the expression statement for the interactive mode. TOS is
274removed from the stack and printed. In non-interactive mode, an
Fred Drake456035f1997-12-03 04:06:57 +0000275expression statement is terminated with \code{POP_STACK}.
276\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000277
Fred Drake456035f1997-12-03 04:06:57 +0000278\begin{opcodedesc}{PRINT_ITEM}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000279Prints TOS. There is one such instruction for
280each item in the print statement.
Fred Drake456035f1997-12-03 04:06:57 +0000281\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000282
Fred Drake456035f1997-12-03 04:06:57 +0000283\begin{opcodedesc}{PRINT_NEWLINE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000284Prints a new line on \code{sys.stdout}. This is generated as the
285last operation of a print statement, unless the statement ends
286with a comma.
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}{BREAK_LOOP}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000290Terminates a loop due to a break statement.
Fred Drake456035f1997-12-03 04:06:57 +0000291\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000292
Fred Drake456035f1997-12-03 04:06:57 +0000293\begin{opcodedesc}{LOAD_LOCALS}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000294Pushes a reference to the locals of the current scope on the stack.
295This is used in the code for a class definition: After the class body
296is evaluated, the locals are passed to the class definition.
Fred Drake456035f1997-12-03 04:06:57 +0000297\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000298
Fred Drake456035f1997-12-03 04:06:57 +0000299\begin{opcodedesc}{RETURN_VALUE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000300Returns with TOS to the caller of the function.
Fred Drake456035f1997-12-03 04:06:57 +0000301\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000302
Fred Drake456035f1997-12-03 04:06:57 +0000303\begin{opcodedesc}{EXEC_STMT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000304Implements \code{exec TOS2,TOS1,TOS}. The compiler fills
305missing optional parameters with None.
Fred Drake456035f1997-12-03 04:06:57 +0000306\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000307
Fred Drake456035f1997-12-03 04:06:57 +0000308\begin{opcodedesc}{POP_BLOCK}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000309Removes one block from the block stack. Per frame, there is a
310stack of blocks, denoting nested loops, try statements, and such.
Fred Drake456035f1997-12-03 04:06:57 +0000311\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000312
Fred Drake456035f1997-12-03 04:06:57 +0000313\begin{opcodedesc}{END_FINALLY}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000314Terminates a finally-block. The interpreter recalls whether the
315exception has to be re-raised, or whether the function returns,
316and continues with the outer-next block.
Fred Drake456035f1997-12-03 04:06:57 +0000317\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000318
Fred Drake456035f1997-12-03 04:06:57 +0000319\begin{opcodedesc}{BUILD_CLASS}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000320Creates a new class object. TOS is the methods dictionary, TOS1
321the tuple of the names of the base classes, and TOS2 the class name.
Fred Drake456035f1997-12-03 04:06:57 +0000322\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000323
324All of the following opcodes expect arguments. An argument is two
325bytes, with the more significant byte last.
326
Fred Drake456035f1997-12-03 04:06:57 +0000327\begin{opcodedesc}{STORE_NAME}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000328Implements \code{name = TOS}. \var{namei} is the index of \var{name}
Fred Drakedff21a61998-04-03 05:42:10 +0000329in the attribute \member{co_names} of the code object.
Fred Drake456035f1997-12-03 04:06:57 +0000330The compiler tries to use \code{STORE_LOCAL} or \code{STORE_GLOBAL}
331if possible.
332\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000333
Fred Drake456035f1997-12-03 04:06:57 +0000334\begin{opcodedesc}{DELETE_NAME}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000335Implements \code{del name}, where \var{namei} is the index into
Fred Drakedff21a61998-04-03 05:42:10 +0000336\member{co_names} attribute of the code object.
Fred Drake456035f1997-12-03 04:06:57 +0000337\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000338
Fred Drake456035f1997-12-03 04:06:57 +0000339\begin{opcodedesc}{UNPACK_TUPLE}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000340Unpacks TOS into \var{count} individual values, which are put onto
341the stack right-to-left.
Fred Drake456035f1997-12-03 04:06:57 +0000342\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000343
Fred Drake456035f1997-12-03 04:06:57 +0000344\begin{opcodedesc}{UNPACK_LIST}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000345Unpacks TOS into \var{count} individual values.
Fred Drake456035f1997-12-03 04:06:57 +0000346\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000347
Fred Drake456035f1997-12-03 04:06:57 +0000348%\begin{opcodedesc}{UNPACK_ARG}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000349%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000350%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000351
Fred Drake456035f1997-12-03 04:06:57 +0000352\begin{opcodedesc}{STORE_ATTR}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000353Implements \code{TOS.name = TOS1}, where \var{namei} is the index
Fred Drakedff21a61998-04-03 05:42:10 +0000354of name in \member{co_names}.
Fred Drake456035f1997-12-03 04:06:57 +0000355\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000356
Fred Drake456035f1997-12-03 04:06:57 +0000357\begin{opcodedesc}{DELETE_ATTR}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000358Implements \code{del TOS.name}, using \var{namei} as index into
Fred Drakedff21a61998-04-03 05:42:10 +0000359\member{co_names}.
Fred Drake456035f1997-12-03 04:06:57 +0000360\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000361
Fred Drake456035f1997-12-03 04:06:57 +0000362\begin{opcodedesc}{STORE_GLOBAL}{namei}
363Works as \code{STORE_NAME}, but stores the name as a global.
364\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000365
Fred Drake456035f1997-12-03 04:06:57 +0000366\begin{opcodedesc}{DELETE_GLOBAL}{namei}
367Works as \code{DELETE_NAME}, but deletes a global name.
368\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000369
Fred Drake456035f1997-12-03 04:06:57 +0000370%\begin{opcodedesc}{UNPACK_VARARG}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000371%This opcode is obsolete.
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}{LOAD_CONST}{consti}
Fred Drakedff21a61998-04-03 05:42:10 +0000375Pushes \samp{co_consts[\var{consti}]} onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000376\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000377
Fred Drake456035f1997-12-03 04:06:57 +0000378\begin{opcodedesc}{LOAD_NAME}{namei}
Fred Drakedff21a61998-04-03 05:42:10 +0000379Pushes the value associated with \samp{co_names[\var{namei}]} onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000380\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000381
Fred Drake456035f1997-12-03 04:06:57 +0000382\begin{opcodedesc}{BUILD_TUPLE}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000383Creates a tuple consuming \var{count} items from the stack, and pushes
384the resulting tuple onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000385\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000386
Fred Drake456035f1997-12-03 04:06:57 +0000387\begin{opcodedesc}{BUILD_LIST}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000388Works as \code{BUILD_TUPLE}, but creates a list.
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}{BUILD_MAP}{zero}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000392Pushes an empty dictionary object onto the stack. The argument is ignored
393and set to zero by the compiler.
Fred Drake456035f1997-12-03 04:06:57 +0000394\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000395
Fred Drake456035f1997-12-03 04:06:57 +0000396\begin{opcodedesc}{LOAD_ATTR}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000397Replaces TOS with \code{getattr(TOS,co_names[\var{namei}]}.
Fred Drake456035f1997-12-03 04:06:57 +0000398\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000399
Fred Drake456035f1997-12-03 04:06:57 +0000400\begin{opcodedesc}{COMPARE_OP}{opname}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000401Performs a boolean operation. The operation name can be found
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000402in \code{cmp_op[\var{opname}]}.
Fred Drake456035f1997-12-03 04:06:57 +0000403\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000404
Fred Drake456035f1997-12-03 04:06:57 +0000405\begin{opcodedesc}{IMPORT_NAME}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000406Imports the module \code{co_names[\var{namei}]}. The module object is
407pushed onto the stack. The current name space is not affected: for a
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000408proper import statement, a subsequent \code{STORE_FAST} instruction
409modifies the name space.
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_FROM}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000413Imports the attribute \code{co_names[\var{namei}]}. The module to import
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000414from is found in TOS and left there.
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}{JUMP_FORWARD}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000418Increments byte code counter by \var{delta}.
Fred Drake456035f1997-12-03 04:06:57 +0000419\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000420
Fred Drake456035f1997-12-03 04:06:57 +0000421\begin{opcodedesc}{JUMP_IF_TRUE}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000422If TOS is true, increment the byte code counter by \var{delta}. TOS is
423left on the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000424\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000425
Fred Drake456035f1997-12-03 04:06:57 +0000426\begin{opcodedesc}{JUMP_IF_FALSE}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000427If TOS is false, increment the byte code counter by \var{delta}. TOS
428is not changed.
Fred Drake456035f1997-12-03 04:06:57 +0000429\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000430
Fred Drake456035f1997-12-03 04:06:57 +0000431\begin{opcodedesc}{JUMP_ABSOLUTE}{target}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000432Set byte code counter to \var{target}.
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}{FOR_LOOP}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000436Iterate over a sequence. TOS is the current index, TOS1 the sequence.
437First, the next element is computed. If the sequence is exhausted,
438increment byte code counter by \var{delta}. Otherwise, push the
439sequence, the incremented counter, and the current item onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000440\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000441
Fred Drake456035f1997-12-03 04:06:57 +0000442%\begin{opcodedesc}{LOAD_LOCAL}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000443%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000444%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000445
Fred Drake456035f1997-12-03 04:06:57 +0000446\begin{opcodedesc}{LOAD_GLOBAL}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000447Loads the global named \code{co_names[\var{namei}]} 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}{SET_FUNC_ARGS}{argc}
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}{SETUP_LOOP}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000455Pushes a block for a loop onto the block stack. The block spans
456from the current instruction with a size of \var{delta} bytes.
Fred Drake456035f1997-12-03 04:06:57 +0000457\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000458
Fred Drake456035f1997-12-03 04:06:57 +0000459\begin{opcodedesc}{SETUP_EXCEPT}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000460Pushes a try block from a try-except clause onto the block stack.
461\var{delta} points to the first except block.
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}{SETUP_FINALLY}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000465Pushes a try block from a try-except clause onto the block stack.
466\var{delta} points to the finally block.
Fred Drake456035f1997-12-03 04:06:57 +0000467\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000468
Fred Drake456035f1997-12-03 04:06:57 +0000469\begin{opcodedesc}{LOAD_FAST}{var_num}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000470Pushes a reference to the local \code{co_varnames[\var{var_num}]} onto
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000471the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000472\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000473
Fred Drake456035f1997-12-03 04:06:57 +0000474\begin{opcodedesc}{STORE_FAST}{var_num}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000475Stores TOS into the local \code{co_varnames[\var{var_num}]}.
Fred Drake456035f1997-12-03 04:06:57 +0000476\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000477
Fred Drake456035f1997-12-03 04:06:57 +0000478\begin{opcodedesc}{DELETE_FAST}{var_num}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000479Deletes local \code{co_varnames[\var{var_num}]}.
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}{SET_LINE_NO}{lineno}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000483Sets the current line number to \var{lineno}.
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}{RAISE_VARARGS}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000487Raises an exception. \var{argc} indicates the number of parameters
488to the raise statement, ranging from 1 to 3. The handler will find
489the traceback as TOS2, the parameter as TOS1, and the exception
490as TOS.
Fred Drake456035f1997-12-03 04:06:57 +0000491\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000492
Fred Drake456035f1997-12-03 04:06:57 +0000493\begin{opcodedesc}{CALL_FUNCTION}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000494Calls a function. The low byte of \var{argc} indicates the number of
495positional parameters, the high byte the number of keyword parameters.
496On the stack, the opcode finds the keyword parameters first. For each
497keyword argument, the value is on top of the key. Below the keyword
498parameters, the positional parameters are on the stack, with the
499right-most parameter on top. Below the parameters, the function object
500to call is on the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000501\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000502
Fred Drake456035f1997-12-03 04:06:57 +0000503\begin{opcodedesc}{MAKE_FUNCTION}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000504Pushes a new function object on the stack. TOS is the code associated
505with the function. The function object is defined to have \var{argc}
506default parameters, which are found below TOS.
Fred Drake456035f1997-12-03 04:06:57 +0000507\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000508
Fred Drake456035f1997-12-03 04:06:57 +0000509\begin{opcodedesc}{BUILD_SLICE}{argc}
Guido van Rossumda623981998-02-12 03:53:02 +0000510Pushes a slice object on the stack. \var{argc} must be 2 or 3. If it
511is 2, \code{slice(TOS1, TOS)} is pushed; if it is 3,
512\code{slice(TOS2, TOS1, TOS)} is pushed.
Fred Drake19479911998-02-13 06:58:54 +0000513See the \code{slice()}\bifuncindex{slice} built-in function.
Fred Drake456035f1997-12-03 04:06:57 +0000514\end{opcodedesc}