blob: e11a1f663d1a14666c2ad142b71aaea8d9d86117 [file] [log] [blame]
Fred Drake14f85211997-12-17 13:52:04 +00001\section{Standard Module \sectcode{dis}}
Guido van Rossumb62b6d11997-11-18 15:10:53 +00002\stmodindex{dis}
3
4\label{module-dis}
5
6The \code{dis} module supports the analysis of Python byte code by
7disassembling it. Since there is no Python assembler, this module
8defines the Python assembly language. The Python byte code which
9this module takes as an input is defined in the file
Fred Drake456035f1997-12-03 04:06:57 +000010\file{Include/opcode.h} and used by the compiler and the interpreter.
Guido van Rossumb62b6d11997-11-18 15:10:53 +000011
12Example: Given the function myfunc
13
Fred Drake19479911998-02-13 06:58:54 +000014\begin{verbatim}
Guido van Rossumb62b6d11997-11-18 15:10:53 +000015def myfunc(alist):
16 return len(alist)
Fred Drake19479911998-02-13 06:58:54 +000017\end{verbatim}
Guido van Rossumb62b6d11997-11-18 15:10:53 +000018
Fred Drake456035f1997-12-03 04:06:57 +000019the following command can be used to get the disassembly of \code{myfunc()}:
Guido van Rossumb62b6d11997-11-18 15:10:53 +000020
21\begin{verbatim}
22>>> dis.dis(myfunc)
23 0 SET_LINENO 1
24
25 3 SET_LINENO 2
26 6 LOAD_GLOBAL 0 (len)
27 9 LOAD_FAST 0 (alist)
28 12 CALL_FUNCTION 1
29 15 RETURN_VALUE
30 16 LOAD_CONST 0 (None)
31 19 RETURN_VALUE
32\end{verbatim}
33
34The \code{dis} module defines the following functions:
35
Fred Drake19479911998-02-13 06:58:54 +000036\setindexsubitem{(in module dis)}
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:
55\begin{itemize}
56\item the current instruction, indicated as \code{-->},
57\item a labelled instruction, indicated with \code{>>},
58\item the address of the instruction,
59\item the operation code name,
60\item operation parameters, and
61\item interpretation of the parameters in parentheses.
62\end{itemize}
63The parameter interpretation recognizes local and global
64variable names, constant values, branch targets, and compare
65operators.
66\end{funcdesc}
67
Fred Drakecce10901998-03-17 06:33:25 +000068\begin{funcdesc}{disco}{code\optional{, lasti}}
Guido van Rossumb62b6d11997-11-18 15:10:53 +000069A synonym for disassemble. It is more convenient to type, and kept
70for compatibility with earlier Python releases.
71\end{funcdesc}
72
73\begin{datadesc}{opname}
74Sequence of a operation names, indexable using the byte code.
75\end{datadesc}
76
77\begin{datadesc}{cmp_op}
78Sequence of all compare operation names.
79\end{datadesc}
80
81\begin{datadesc}{hasconst}
82Sequence of byte codes that have a constant parameter.
83\end{datadesc}
84
85\begin{datadesc}{hasname}
86Sequence of byte codes that access a attribute by name.
87\end{datadesc}
88
89\begin{datadesc}{hasjrel}
90Sequence of byte codes that have a relative jump target.
91\end{datadesc}
92
93\begin{datadesc}{hasjabs}
94Sequence of byte codes that have an absolute jump target.
95\end{datadesc}
96
97\begin{datadesc}{haslocal}
98Sequence of byte codes that access a a local variable.
99\end{datadesc}
100
101\begin{datadesc}{hascompare}
102Sequence of byte codes of boolean operations.
103\end{datadesc}
104
105\subsection{Python Byte Code Instructions}
Fred Drake83efb541998-02-19 20:07:39 +0000106\label{bytecodes}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000107
108The Python compiler currently generates the following byte code
109instructions.
110
Fred Drake19479911998-02-13 06:58:54 +0000111\setindexsubitem{(byte code insns)}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000112
Fred Drake456035f1997-12-03 04:06:57 +0000113\begin{opcodedesc}{STOP_CODE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000114Indicates end-of-code to the compiler, not used by the interpreter.
Fred Drake456035f1997-12-03 04:06:57 +0000115\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000116
Fred Drake456035f1997-12-03 04:06:57 +0000117\begin{opcodedesc}{POP_TOP}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000118Removes the top-of-stack (TOS) item.
Fred Drake456035f1997-12-03 04:06:57 +0000119\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000120
Fred Drake456035f1997-12-03 04:06:57 +0000121\begin{opcodedesc}{ROT_TWO}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000122Swaps the two top-most stack items.
Fred Drake456035f1997-12-03 04:06:57 +0000123\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000124
Fred Drake456035f1997-12-03 04:06:57 +0000125\begin{opcodedesc}{ROT_THREE}{}
126Lifts second and third stack item one position up, moves top down
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000127to position three.
Fred Drake456035f1997-12-03 04:06:57 +0000128\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000129
Fred Drake456035f1997-12-03 04:06:57 +0000130\begin{opcodedesc}{DUP_TOP}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000131Duplicates the reference on top of the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000132\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000133
134Unary Operations take the top of the stack, apply the operation, and
135push the result back on the stack.
136
Fred Drake456035f1997-12-03 04:06:57 +0000137\begin{opcodedesc}{UNARY_POSITIVE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000138Implements \code{TOS = +TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000139\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000140
Fred Drake456035f1997-12-03 04:06:57 +0000141\begin{opcodedesc}{UNARY_NEG}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000142Implements \code{TOS = -TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000143\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000144
Fred Drake456035f1997-12-03 04:06:57 +0000145\begin{opcodedesc}{UNARY_NOT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000146Implements \code{TOS = not TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000147\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000148
Fred Drake456035f1997-12-03 04:06:57 +0000149\begin{opcodedesc}{UNARY_CONVERT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000150Implements \code{TOS = `TOS`}.
Fred Drake456035f1997-12-03 04:06:57 +0000151\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000152
Fred Drake456035f1997-12-03 04:06:57 +0000153\begin{opcodedesc}{UNARY_INVERT}{}
Guido van Rossume903aab1997-12-18 16:28:56 +0000154Implements \code{TOS = \~TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000155\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000156
157Binary operations remove the top of the stack (TOS) and the second top-most
158stack item (TOS1) from the stack. They perform the operation, and put the
159result back on the stack.
160
Fred Drake456035f1997-12-03 04:06:57 +0000161\begin{opcodedesc}{BINARY_POWER}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000162Implements \code{TOS = TOS1 ** TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000163\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000164
Fred Drake456035f1997-12-03 04:06:57 +0000165\begin{opcodedesc}{BINARY_MULTIPLY}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000166Implements \code{TOS = TOS1 * TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000167\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000168
Fred Drake456035f1997-12-03 04:06:57 +0000169\begin{opcodedesc}{BINARY_DIVIDE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000170Implements \code{TOS = TOS1 / TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000171\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000172
Fred Drake456035f1997-12-03 04:06:57 +0000173\begin{opcodedesc}{BINARY_MODULO}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000174Implements \code{TOS = TOS1 \% TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000175\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000176
Fred Drake456035f1997-12-03 04:06:57 +0000177\begin{opcodedesc}{BINARY_ADD}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000178Implements \code{TOS = TOS1 + TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000179\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000180
Fred Drake456035f1997-12-03 04:06:57 +0000181\begin{opcodedesc}{BINARY_SUBTRACT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000182Implements \code{TOS = TOS1 - TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000183\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000184
Fred Drake456035f1997-12-03 04:06:57 +0000185\begin{opcodedesc}{BINARY_SUBSCR}{}
Fred Drake1cf87491997-12-04 04:57:56 +0000186Implements \code{TOS = TOS1[TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000187\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000188
Fred Drake456035f1997-12-03 04:06:57 +0000189\begin{opcodedesc}{BINARY_LSHIFT}{}
Fred Drake1cf87491997-12-04 04:57:56 +0000190Implements \code{TOS = TOS1 << TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000191\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000192
Fred Drake456035f1997-12-03 04:06:57 +0000193\begin{opcodedesc}{BINARY_RSHIFT}{}
Fred Drake1cf87491997-12-04 04:57:56 +0000194Implements \code{TOS = TOS1 >> TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000195\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000196
Fred Drake456035f1997-12-03 04:06:57 +0000197\begin{opcodedesc}{BINARY_AND}{}
Fred Drake1cf87491997-12-04 04:57:56 +0000198Implements \code{TOS = TOS1 and TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000199\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000200
Fred Drake456035f1997-12-03 04:06:57 +0000201\begin{opcodedesc}{BINARY_XOR}{}
Fred Draked7feffd1997-12-29 20:02:55 +0000202Implements \code{TOS = TOS1 \^\ TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000203\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000204
Fred Drake456035f1997-12-03 04:06:57 +0000205\begin{opcodedesc}{BINARY_OR}{}
Fred Drake1cf87491997-12-04 04:57:56 +0000206Implements \code{TOS = TOS1 or TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000207\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000208
209The slice opcodes take up to three parameters.
210
Fred Drake456035f1997-12-03 04:06:57 +0000211\begin{opcodedesc}{SLICE+0}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000212Implements \code{TOS = TOS[:]}.
Fred Drake456035f1997-12-03 04:06:57 +0000213\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000214
Fred Drake456035f1997-12-03 04:06:57 +0000215\begin{opcodedesc}{SLICE+1}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000216Implements \code{TOS = TOS1[TOS:]}.
Fred Drake456035f1997-12-03 04:06:57 +0000217\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000218
Fred Drake456035f1997-12-03 04:06:57 +0000219\begin{opcodedesc}{SLICE+2}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000220Implements \code{TOS = TOS1[:TOS1]}.
Fred Drake456035f1997-12-03 04:06:57 +0000221\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000222
Fred Drake456035f1997-12-03 04:06:57 +0000223\begin{opcodedesc}{SLICE+3}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000224Implements \code{TOS = TOS2[TOS1:TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000225\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000226
227Slice assignment needs even an additional parameter. As any statement,
228they put nothing on the stack.
229
Fred Drake456035f1997-12-03 04:06:57 +0000230\begin{opcodedesc}{STORE_SLICE+0}{}
Fred Drake7381e281997-12-04 04:51:12 +0000231Implements \code{TOS[:] = TOS1}.
Fred Drake456035f1997-12-03 04:06:57 +0000232\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000233
Fred Drake456035f1997-12-03 04:06:57 +0000234\begin{opcodedesc}{STORE_SLICE+1}{}
Fred Drake7381e281997-12-04 04:51:12 +0000235Implements \code{TOS1[TOS:] = TOS2}.
Fred Drake456035f1997-12-03 04:06:57 +0000236\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000237
Fred Drake456035f1997-12-03 04:06:57 +0000238\begin{opcodedesc}{STORE_SLICE+2}{}
Fred Drake7381e281997-12-04 04:51:12 +0000239Implements \code{TOS1[:TOS] = TOS2}.
Fred Drake456035f1997-12-03 04:06:57 +0000240\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000241
Fred Drake456035f1997-12-03 04:06:57 +0000242\begin{opcodedesc}{STORE_SLICE+3}{}
Fred Drake7381e281997-12-04 04:51:12 +0000243Implements \code{TOS2[TOS1:TOS] = TOS3}.
Fred Drake456035f1997-12-03 04:06:57 +0000244\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000245
Fred Drake456035f1997-12-03 04:06:57 +0000246\begin{opcodedesc}{DELETE_SLICE+0}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000247Implements \code{del TOS[:]}.
Fred Drake456035f1997-12-03 04:06:57 +0000248\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000249
Fred Drake456035f1997-12-03 04:06:57 +0000250\begin{opcodedesc}{DELETE_SLICE+1}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000251Implements \code{del TOS1[TOS:]}.
Fred Drake456035f1997-12-03 04:06:57 +0000252\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000253
Fred Drake456035f1997-12-03 04:06:57 +0000254\begin{opcodedesc}{DELETE_SLICE+2}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000255Implements \code{del TOS1[:TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000256\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000257
Fred Drake456035f1997-12-03 04:06:57 +0000258\begin{opcodedesc}{DELETE_SLICE+3}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000259Implements \code{del TOS2[TOS1:TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000260\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000261
Fred Drake456035f1997-12-03 04:06:57 +0000262\begin{opcodedesc}{STORE_SUBSCR}{}
Fred Drake7381e281997-12-04 04:51:12 +0000263Implements \code{TOS1[TOS] = TOS2}.
Fred Drake456035f1997-12-03 04:06:57 +0000264\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000265
Fred Drake456035f1997-12-03 04:06:57 +0000266\begin{opcodedesc}{DELETE_SUBSCR}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000267Implements \code{del TOS1[TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000268\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000269
Fred Drake456035f1997-12-03 04:06:57 +0000270\begin{opcodedesc}{PRINT_EXPR}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000271Implements the expression statement for the interactive mode. TOS is
272removed from the stack and printed. In non-interactive mode, an
Fred Drake456035f1997-12-03 04:06:57 +0000273expression statement is terminated with \code{POP_STACK}.
274\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000275
Fred Drake456035f1997-12-03 04:06:57 +0000276\begin{opcodedesc}{PRINT_ITEM}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000277Prints TOS. There is one such instruction for
278each item in the print statement.
Fred Drake456035f1997-12-03 04:06:57 +0000279\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000280
Fred Drake456035f1997-12-03 04:06:57 +0000281\begin{opcodedesc}{PRINT_NEWLINE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000282Prints a new line on \code{sys.stdout}. This is generated as the
283last operation of a print statement, unless the statement ends
284with a comma.
Fred Drake456035f1997-12-03 04:06:57 +0000285\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000286
Fred Drake456035f1997-12-03 04:06:57 +0000287\begin{opcodedesc}{BREAK_LOOP}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000288Terminates a loop due to a break statement.
Fred Drake456035f1997-12-03 04:06:57 +0000289\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000290
Fred Drake456035f1997-12-03 04:06:57 +0000291\begin{opcodedesc}{LOAD_LOCALS}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000292Pushes a reference to the locals of the current scope on the stack.
293This is used in the code for a class definition: After the class body
294is evaluated, the locals are passed to the class definition.
Fred Drake456035f1997-12-03 04:06:57 +0000295\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000296
Fred Drake456035f1997-12-03 04:06:57 +0000297\begin{opcodedesc}{RETURN_VALUE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000298Returns with TOS to the caller of the function.
Fred Drake456035f1997-12-03 04:06:57 +0000299\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000300
Fred Drake456035f1997-12-03 04:06:57 +0000301\begin{opcodedesc}{EXEC_STMT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000302Implements \code{exec TOS2,TOS1,TOS}. The compiler fills
303missing optional parameters with None.
Fred Drake456035f1997-12-03 04:06:57 +0000304\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000305
Fred Drake456035f1997-12-03 04:06:57 +0000306\begin{opcodedesc}{POP_BLOCK}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000307Removes one block from the block stack. Per frame, there is a
308stack of blocks, denoting nested loops, try statements, and such.
Fred Drake456035f1997-12-03 04:06:57 +0000309\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000310
Fred Drake456035f1997-12-03 04:06:57 +0000311\begin{opcodedesc}{END_FINALLY}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000312Terminates a finally-block. The interpreter recalls whether the
313exception has to be re-raised, or whether the function returns,
314and continues with the outer-next block.
Fred Drake456035f1997-12-03 04:06:57 +0000315\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000316
Fred Drake456035f1997-12-03 04:06:57 +0000317\begin{opcodedesc}{BUILD_CLASS}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000318Creates a new class object. TOS is the methods dictionary, TOS1
319the tuple of the names of the base classes, and TOS2 the class name.
Fred Drake456035f1997-12-03 04:06:57 +0000320\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000321
322All of the following opcodes expect arguments. An argument is two
323bytes, with the more significant byte last.
324
Fred Drake456035f1997-12-03 04:06:57 +0000325\begin{opcodedesc}{STORE_NAME}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000326Implements \code{name = TOS}. \var{namei} is the index of \var{name}
327in the attribute \code{co_names} of the code object.
Fred Drake456035f1997-12-03 04:06:57 +0000328The compiler tries to use \code{STORE_LOCAL} or \code{STORE_GLOBAL}
329if possible.
330\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000331
Fred Drake456035f1997-12-03 04:06:57 +0000332\begin{opcodedesc}{DELETE_NAME}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000333Implements \code{del name}, where \var{namei} is the index into
334\code{co_names} attribute of the code object.
Fred Drake456035f1997-12-03 04:06:57 +0000335\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000336
Fred Drake456035f1997-12-03 04:06:57 +0000337\begin{opcodedesc}{UNPACK_TUPLE}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000338Unpacks TOS into \var{count} individual values, which are put onto
339the stack right-to-left.
Fred Drake456035f1997-12-03 04:06:57 +0000340\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000341
Fred Drake456035f1997-12-03 04:06:57 +0000342\begin{opcodedesc}{UNPACK_LIST}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000343Unpacks TOS into \var{count} individual values.
Fred Drake456035f1997-12-03 04:06:57 +0000344\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000345
Fred Drake456035f1997-12-03 04:06:57 +0000346%\begin{opcodedesc}{UNPACK_ARG}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000347%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000348%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000349
Fred Drake456035f1997-12-03 04:06:57 +0000350\begin{opcodedesc}{STORE_ATTR}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000351Implements \code{TOS.name = TOS1}, where \var{namei} is the index
352of name in \code{co_names}.
Fred Drake456035f1997-12-03 04:06:57 +0000353\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000354
Fred Drake456035f1997-12-03 04:06:57 +0000355\begin{opcodedesc}{DELETE_ATTR}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000356Implements \code{del TOS.name}, using \var{namei} as index into
357\code{co_names}.
Fred Drake456035f1997-12-03 04:06:57 +0000358\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000359
Fred Drake456035f1997-12-03 04:06:57 +0000360\begin{opcodedesc}{STORE_GLOBAL}{namei}
361Works as \code{STORE_NAME}, but stores the name as a global.
362\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000363
Fred Drake456035f1997-12-03 04:06:57 +0000364\begin{opcodedesc}{DELETE_GLOBAL}{namei}
365Works as \code{DELETE_NAME}, but deletes a global name.
366\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000367
Fred Drake456035f1997-12-03 04:06:57 +0000368%\begin{opcodedesc}{UNPACK_VARARG}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000369%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000370%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000371
Fred Drake456035f1997-12-03 04:06:57 +0000372\begin{opcodedesc}{LOAD_CONST}{consti}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000373Pushes \code{co_consts[\var{consti}]} onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000374\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000375
Fred Drake456035f1997-12-03 04:06:57 +0000376\begin{opcodedesc}{LOAD_NAME}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000377Pushes the value associated with \code{co_names[\var{namei}]} onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000378\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000379
Fred Drake456035f1997-12-03 04:06:57 +0000380\begin{opcodedesc}{BUILD_TUPLE}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000381Creates a tuple consuming \var{count} items from the stack, and pushes
382the resulting tuple 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}{BUILD_LIST}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000386Works as \code{BUILD_TUPLE}, but creates a list.
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_MAP}{zero}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000390Pushes an empty dictionary object onto the stack. The argument is ignored
391and set to zero by the compiler.
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}{LOAD_ATTR}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000395Replaces TOS with \code{getattr(TOS,co_names[\var{namei}]}.
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}{COMPARE_OP}{opname}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000399Performs a boolean operation. The operation name can be found
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000400in \code{cmp_op[\var{opname}]}.
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}{IMPORT_NAME}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000404Imports the module \code{co_names[\var{namei}]}. The module object is
405pushed onto the stack. The current name space is not affected: for a
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000406proper import statement, a subsequent \code{STORE_FAST} instruction
407modifies the name space.
Fred Drake456035f1997-12-03 04:06:57 +0000408\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000409
Fred Drake456035f1997-12-03 04:06:57 +0000410\begin{opcodedesc}{IMPORT_FROM}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000411Imports the attribute \code{co_names[\var{namei}]}. The module to import
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000412from is found in TOS and left there.
Fred Drake456035f1997-12-03 04:06:57 +0000413\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000414
Fred Drake456035f1997-12-03 04:06:57 +0000415\begin{opcodedesc}{JUMP_FORWARD}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000416Increments byte code counter by \var{delta}.
Fred Drake456035f1997-12-03 04:06:57 +0000417\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000418
Fred Drake456035f1997-12-03 04:06:57 +0000419\begin{opcodedesc}{JUMP_IF_TRUE}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000420If TOS is true, increment the byte code counter by \var{delta}. TOS is
421left on the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000422\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000423
Fred Drake456035f1997-12-03 04:06:57 +0000424\begin{opcodedesc}{JUMP_IF_FALSE}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000425If TOS is false, increment the byte code counter by \var{delta}. TOS
426is not changed.
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_ABSOLUTE}{target}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000430Set byte code counter to \var{target}.
Fred Drake456035f1997-12-03 04:06:57 +0000431\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000432
Fred Drake456035f1997-12-03 04:06:57 +0000433\begin{opcodedesc}{FOR_LOOP}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000434Iterate over a sequence. TOS is the current index, TOS1 the sequence.
435First, the next element is computed. If the sequence is exhausted,
436increment byte code counter by \var{delta}. Otherwise, push the
437sequence, the incremented counter, and the current item onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000438\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000439
Fred Drake456035f1997-12-03 04:06:57 +0000440%\begin{opcodedesc}{LOAD_LOCAL}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000441%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000442%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000443
Fred Drake456035f1997-12-03 04:06:57 +0000444\begin{opcodedesc}{LOAD_GLOBAL}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000445Loads the global named \code{co_names[\var{namei}]} onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000446\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000447
Fred Drake456035f1997-12-03 04:06:57 +0000448%\begin{opcodedesc}{SET_FUNC_ARGS}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000449%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000450%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000451
Fred Drake456035f1997-12-03 04:06:57 +0000452\begin{opcodedesc}{SETUP_LOOP}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000453Pushes a block for a loop onto the block stack. The block spans
454from the current instruction with a size of \var{delta} bytes.
Fred Drake456035f1997-12-03 04:06:57 +0000455\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000456
Fred Drake456035f1997-12-03 04:06:57 +0000457\begin{opcodedesc}{SETUP_EXCEPT}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000458Pushes a try block from a try-except clause onto the block stack.
459\var{delta} points to the first except block.
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_FINALLY}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000463Pushes a try block from a try-except clause onto the block stack.
464\var{delta} points to the finally block.
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}{LOAD_FAST}{var_num}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000468Pushes a reference to the local \code{co_varnames[\var{var_num}]} onto
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000469the stack.
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}{STORE_FAST}{var_num}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000473Stores TOS into the local \code{co_varnames[\var{var_num}]}.
Fred Drake456035f1997-12-03 04:06:57 +0000474\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000475
Fred Drake456035f1997-12-03 04:06:57 +0000476\begin{opcodedesc}{DELETE_FAST}{var_num}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000477Deletes local \code{co_varnames[\var{var_num}]}.
Fred Drake456035f1997-12-03 04:06:57 +0000478\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000479
Fred Drake456035f1997-12-03 04:06:57 +0000480\begin{opcodedesc}{SET_LINE_NO}{lineno}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000481Sets the current line number to \var{lineno}.
Fred Drake456035f1997-12-03 04:06:57 +0000482\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000483
Fred Drake456035f1997-12-03 04:06:57 +0000484\begin{opcodedesc}{RAISE_VARARGS}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000485Raises an exception. \var{argc} indicates the number of parameters
486to the raise statement, ranging from 1 to 3. The handler will find
487the traceback as TOS2, the parameter as TOS1, and the exception
488as TOS.
Fred Drake456035f1997-12-03 04:06:57 +0000489\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000490
Fred Drake456035f1997-12-03 04:06:57 +0000491\begin{opcodedesc}{CALL_FUNCTION}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000492Calls a function. The low byte of \var{argc} indicates the number of
493positional parameters, the high byte the number of keyword parameters.
494On the stack, the opcode finds the keyword parameters first. For each
495keyword argument, the value is on top of the key. Below the keyword
496parameters, the positional parameters are on the stack, with the
497right-most parameter on top. Below the parameters, the function object
498to call is on the stack.
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}{MAKE_FUNCTION}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000502Pushes a new function object on the stack. TOS is the code associated
503with the function. The function object is defined to have \var{argc}
504default parameters, which are found below TOS.
Fred Drake456035f1997-12-03 04:06:57 +0000505\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000506
Fred Drake456035f1997-12-03 04:06:57 +0000507\begin{opcodedesc}{BUILD_SLICE}{argc}
Guido van Rossumda623981998-02-12 03:53:02 +0000508Pushes a slice object on the stack. \var{argc} must be 2 or 3. If it
509is 2, \code{slice(TOS1, TOS)} is pushed; if it is 3,
510\code{slice(TOS2, TOS1, TOS)} is pushed.
Fred Drake19479911998-02-13 06:58:54 +0000511See the \code{slice()}\bifuncindex{slice} built-in function.
Fred Drake456035f1997-12-03 04:06:57 +0000512\end{opcodedesc}