blob: f484103171f686722ffa5e61a114ad6be4037165 [file] [log] [blame]
Fred Drakedff21a61998-04-03 05:42:10 +00001\section{Standard Module \module{dis}}
Guido van Rossumb62b6d11997-11-18 15:10:53 +00002\stmodindex{dis}
Guido van Rossumb62b6d11997-11-18 15:10:53 +00003\label{module-dis}
4
Fred Drakedff21a61998-04-03 05:42:10 +00005The \module{dis} module supports the analysis of Python byte code by
Guido van Rossumb62b6d11997-11-18 15:10:53 +00006disassembling it. Since there is no Python assembler, this module
7defines the Python assembly language. The Python byte code which
8this module takes as an input is defined in the file
Fred Drake456035f1997-12-03 04:06:57 +00009\file{Include/opcode.h} and used by the compiler and the interpreter.
Guido van Rossumb62b6d11997-11-18 15:10:53 +000010
Fred Drakedff21a61998-04-03 05:42:10 +000011Example: Given the function \function{myfunc}:
Guido van Rossumb62b6d11997-11-18 15:10:53 +000012
Fred Drake19479911998-02-13 06:58:54 +000013\begin{verbatim}
Guido van Rossumb62b6d11997-11-18 15:10:53 +000014def myfunc(alist):
Fred Drakedff21a61998-04-03 05:42:10 +000015 return len(alist)
Fred Drake19479911998-02-13 06:58:54 +000016\end{verbatim}
Guido van Rossumb62b6d11997-11-18 15:10:53 +000017
Fred Drakedff21a61998-04-03 05:42:10 +000018the following command can be used to get the disassembly of
19\function{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
Fred Drakedff21a61998-04-03 05:42:10 +000034The \module{dis} module defines the following functions:
Guido van Rossumb62b6d11997-11-18 15:10:53 +000035
Guido van Rossumb62b6d11997-11-18 15:10:53 +000036\begin{funcdesc}{dis}{\optional{bytesource}}
37Disassemble the \var{bytesource} object. \var{bytesource} can denote
38either a class, a method, a function, or a code object. For a class,
39it disassembles all methods. For a single code sequence, it prints
40one line per byte code instruction. If no object is provided, it
41disassembles the last traceback.
42\end{funcdesc}
43
44\begin{funcdesc}{distb}{\optional{tb}}
45Disassembles the top-of-stack function of a traceback, using the last
46traceback if none was passed. The instruction causing the exception
47is indicated.
48\end{funcdesc}
49
Fred Drakecce10901998-03-17 06:33:25 +000050\begin{funcdesc}{disassemble}{code\optional{, lasti}}
Guido van Rossumb62b6d11997-11-18 15:10:53 +000051Disassembles a code object, indicating the last instruction if \var{lasti}
52was provided. The output is divided in the following columns:
53\begin{itemize}
Fred Drakedff21a61998-04-03 05:42:10 +000054\item the current instruction, indicated as \samp{-->},
55\item a labelled instruction, indicated with \samp{>>},
Guido van Rossumb62b6d11997-11-18 15:10:53 +000056\item the address of the instruction,
57\item the operation code name,
58\item operation parameters, and
59\item interpretation of the parameters in parentheses.
60\end{itemize}
61The parameter interpretation recognizes local and global
62variable names, constant values, branch targets, and compare
63operators.
64\end{funcdesc}
65
Fred Drakecce10901998-03-17 06:33:25 +000066\begin{funcdesc}{disco}{code\optional{, lasti}}
Guido van Rossumb62b6d11997-11-18 15:10:53 +000067A synonym for disassemble. It is more convenient to type, and kept
68for compatibility with earlier Python releases.
69\end{funcdesc}
70
71\begin{datadesc}{opname}
72Sequence of a operation names, indexable using the byte code.
73\end{datadesc}
74
75\begin{datadesc}{cmp_op}
76Sequence of all compare operation names.
77\end{datadesc}
78
79\begin{datadesc}{hasconst}
80Sequence of byte codes that have a constant parameter.
81\end{datadesc}
82
83\begin{datadesc}{hasname}
84Sequence of byte codes that access a attribute by name.
85\end{datadesc}
86
87\begin{datadesc}{hasjrel}
88Sequence of byte codes that have a relative jump target.
89\end{datadesc}
90
91\begin{datadesc}{hasjabs}
92Sequence of byte codes that have an absolute jump target.
93\end{datadesc}
94
95\begin{datadesc}{haslocal}
96Sequence of byte codes that access a a local variable.
97\end{datadesc}
98
99\begin{datadesc}{hascompare}
100Sequence of byte codes of boolean operations.
101\end{datadesc}
102
103\subsection{Python Byte Code Instructions}
Fred Drake83efb541998-02-19 20:07:39 +0000104\label{bytecodes}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000105
106The Python compiler currently generates the following byte code
107instructions.
108
Fred Drake19479911998-02-13 06:58:54 +0000109\setindexsubitem{(byte code insns)}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000110
Fred Drake456035f1997-12-03 04:06:57 +0000111\begin{opcodedesc}{STOP_CODE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000112Indicates end-of-code to the compiler, not used by the interpreter.
Fred Drake456035f1997-12-03 04:06:57 +0000113\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000114
Fred Drake456035f1997-12-03 04:06:57 +0000115\begin{opcodedesc}{POP_TOP}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000116Removes the top-of-stack (TOS) item.
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}{ROT_TWO}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000120Swaps the two top-most stack items.
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_THREE}{}
124Lifts second and third stack item one position up, moves top down
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000125to position three.
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}{DUP_TOP}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000129Duplicates the reference on top of the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000130\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000131
132Unary Operations take the top of the stack, apply the operation, and
133push the result back on the stack.
134
Fred Drake456035f1997-12-03 04:06:57 +0000135\begin{opcodedesc}{UNARY_POSITIVE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000136Implements \code{TOS = +TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000137\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000138
Fred Drake456035f1997-12-03 04:06:57 +0000139\begin{opcodedesc}{UNARY_NEG}{}
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_NOT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000144Implements \code{TOS = not 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_CONVERT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000148Implements \code{TOS = `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_INVERT}{}
Guido van Rossume903aab1997-12-18 16:28:56 +0000152Implements \code{TOS = \~TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000153\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000154
155Binary operations remove the top of the stack (TOS) and the second top-most
156stack item (TOS1) from the stack. They perform the operation, and put the
157result back on the stack.
158
Fred Drake456035f1997-12-03 04:06:57 +0000159\begin{opcodedesc}{BINARY_POWER}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000160Implements \code{TOS = TOS1 ** TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000161\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000162
Fred Drake456035f1997-12-03 04:06:57 +0000163\begin{opcodedesc}{BINARY_MULTIPLY}{}
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_DIVIDE}{}
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_MODULO}{}
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_ADD}{}
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_SUBTRACT}{}
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_SUBSCR}{}
Fred Drake1cf87491997-12-04 04:57:56 +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_LSHIFT}{}
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_RSHIFT}{}
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_AND}{}
Fred Drake1cf87491997-12-04 04:57:56 +0000196Implements \code{TOS = TOS1 and 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_XOR}{}
Fred Draked7feffd1997-12-29 20:02:55 +0000200Implements \code{TOS = TOS1 \^\ 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_OR}{}
Fred Drake1cf87491997-12-04 04:57:56 +0000204Implements \code{TOS = TOS1 or TOS}.
Fred Drake456035f1997-12-03 04:06:57 +0000205\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000206
207The slice opcodes take up to three parameters.
208
Fred Drake456035f1997-12-03 04:06:57 +0000209\begin{opcodedesc}{SLICE+0}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000210Implements \code{TOS = TOS[:]}.
Fred Drake456035f1997-12-03 04:06:57 +0000211\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000212
Fred Drake456035f1997-12-03 04:06:57 +0000213\begin{opcodedesc}{SLICE+1}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000214Implements \code{TOS = TOS1[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+2}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000218Implements \code{TOS = TOS1[:TOS1]}.
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+3}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000222Implements \code{TOS = TOS2[TOS1:TOS]}.
Fred Drake456035f1997-12-03 04:06:57 +0000223\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000224
225Slice assignment needs even an additional parameter. As any statement,
226they put nothing on the stack.
227
Fred Drake456035f1997-12-03 04:06:57 +0000228\begin{opcodedesc}{STORE_SLICE+0}{}
Fred Drake7381e281997-12-04 04:51:12 +0000229Implements \code{TOS[:] = TOS1}.
Fred Drake456035f1997-12-03 04:06:57 +0000230\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000231
Fred Drake456035f1997-12-03 04:06:57 +0000232\begin{opcodedesc}{STORE_SLICE+1}{}
Fred Drake7381e281997-12-04 04:51:12 +0000233Implements \code{TOS1[TOS:] = TOS2}.
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+2}{}
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+3}{}
Fred Drake7381e281997-12-04 04:51:12 +0000241Implements \code{TOS2[TOS1:TOS] = TOS3}.
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}{DELETE_SLICE+0}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000245Implements \code{del TOS[:]}.
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+1}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000249Implements \code{del TOS1[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+2}{}
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+3}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000257Implements \code{del TOS2[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}{STORE_SUBSCR}{}
Fred Drake7381e281997-12-04 04:51:12 +0000261Implements \code{TOS1[TOS] = TOS2}.
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}{DELETE_SUBSCR}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000265Implements \code{del TOS1[TOS]}.
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}{PRINT_EXPR}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000269Implements the expression statement for the interactive mode. TOS is
270removed from the stack and printed. In non-interactive mode, an
Fred Drake456035f1997-12-03 04:06:57 +0000271expression statement is terminated with \code{POP_STACK}.
272\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000273
Fred Drake456035f1997-12-03 04:06:57 +0000274\begin{opcodedesc}{PRINT_ITEM}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000275Prints TOS. There is one such instruction for
276each item in the print statement.
Fred Drake456035f1997-12-03 04:06:57 +0000277\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000278
Fred Drake456035f1997-12-03 04:06:57 +0000279\begin{opcodedesc}{PRINT_NEWLINE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000280Prints a new line on \code{sys.stdout}. This is generated as the
281last operation of a print statement, unless the statement ends
282with a comma.
Fred Drake456035f1997-12-03 04:06:57 +0000283\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000284
Fred Drake456035f1997-12-03 04:06:57 +0000285\begin{opcodedesc}{BREAK_LOOP}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000286Terminates a loop due to a break statement.
Fred Drake456035f1997-12-03 04:06:57 +0000287\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000288
Fred Drake456035f1997-12-03 04:06:57 +0000289\begin{opcodedesc}{LOAD_LOCALS}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000290Pushes a reference to the locals of the current scope on the stack.
291This is used in the code for a class definition: After the class body
292is evaluated, the locals are passed to the class definition.
Fred Drake456035f1997-12-03 04:06:57 +0000293\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000294
Fred Drake456035f1997-12-03 04:06:57 +0000295\begin{opcodedesc}{RETURN_VALUE}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000296Returns with TOS to the caller of the function.
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}{EXEC_STMT}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000300Implements \code{exec TOS2,TOS1,TOS}. The compiler fills
301missing optional parameters with None.
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}{POP_BLOCK}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000305Removes one block from the block stack. Per frame, there is a
306stack of blocks, denoting nested loops, try statements, and such.
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}{END_FINALLY}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000310Terminates a finally-block. The interpreter recalls whether the
311exception has to be re-raised, or whether the function returns,
312and continues with the outer-next block.
Fred Drake456035f1997-12-03 04:06:57 +0000313\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000314
Fred Drake456035f1997-12-03 04:06:57 +0000315\begin{opcodedesc}{BUILD_CLASS}{}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000316Creates a new class object. TOS is the methods dictionary, TOS1
317the tuple of the names of the base classes, and TOS2 the class name.
Fred Drake456035f1997-12-03 04:06:57 +0000318\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000319
320All of the following opcodes expect arguments. An argument is two
321bytes, with the more significant byte last.
322
Fred Drake456035f1997-12-03 04:06:57 +0000323\begin{opcodedesc}{STORE_NAME}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000324Implements \code{name = TOS}. \var{namei} is the index of \var{name}
Fred Drakedff21a61998-04-03 05:42:10 +0000325in the attribute \member{co_names} of the code object.
Fred Drake456035f1997-12-03 04:06:57 +0000326The compiler tries to use \code{STORE_LOCAL} or \code{STORE_GLOBAL}
327if possible.
328\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000329
Fred Drake456035f1997-12-03 04:06:57 +0000330\begin{opcodedesc}{DELETE_NAME}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000331Implements \code{del name}, where \var{namei} is the index into
Fred Drakedff21a61998-04-03 05:42:10 +0000332\member{co_names} attribute of the code object.
Fred Drake456035f1997-12-03 04:06:57 +0000333\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000334
Fred Drake456035f1997-12-03 04:06:57 +0000335\begin{opcodedesc}{UNPACK_TUPLE}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000336Unpacks TOS into \var{count} individual values, which are put onto
337the stack right-to-left.
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_LIST}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000341Unpacks TOS into \var{count} individual values.
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_ARG}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000345%This opcode is obsolete.
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}{STORE_ATTR}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000349Implements \code{TOS.name = TOS1}, where \var{namei} is the index
Fred Drakedff21a61998-04-03 05:42:10 +0000350of name in \member{co_names}.
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}{DELETE_ATTR}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000354Implements \code{del TOS.name}, using \var{namei} as index into
Fred Drakedff21a61998-04-03 05:42:10 +0000355\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}{STORE_GLOBAL}{namei}
359Works as \code{STORE_NAME}, but stores the name as a global.
360\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000361
Fred Drake456035f1997-12-03 04:06:57 +0000362\begin{opcodedesc}{DELETE_GLOBAL}{namei}
363Works as \code{DELETE_NAME}, but deletes a global name.
364\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000365
Fred Drake456035f1997-12-03 04:06:57 +0000366%\begin{opcodedesc}{UNPACK_VARARG}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000367%This opcode is obsolete.
Fred Drake456035f1997-12-03 04:06:57 +0000368%\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000369
Fred Drake456035f1997-12-03 04:06:57 +0000370\begin{opcodedesc}{LOAD_CONST}{consti}
Fred Drakedff21a61998-04-03 05:42:10 +0000371Pushes \samp{co_consts[\var{consti}]} onto the stack.
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_NAME}{namei}
Fred Drakedff21a61998-04-03 05:42:10 +0000375Pushes the value associated with \samp{co_names[\var{namei}]} 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}{BUILD_TUPLE}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000379Creates a tuple consuming \var{count} items from the stack, and pushes
380the resulting tuple 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_LIST}{count}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000384Works as \code{BUILD_TUPLE}, but creates a list.
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_MAP}{zero}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000388Pushes an empty dictionary object onto the stack. The argument is ignored
389and set to zero by the compiler.
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}{LOAD_ATTR}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000393Replaces TOS with \code{getattr(TOS,co_names[\var{namei}]}.
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}{COMPARE_OP}{opname}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000397Performs a boolean operation. The operation name can be found
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000398in \code{cmp_op[\var{opname}]}.
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}{IMPORT_NAME}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000402Imports the module \code{co_names[\var{namei}]}. The module object is
403pushed onto the stack. The current name space is not affected: for a
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000404proper import statement, a subsequent \code{STORE_FAST} instruction
405modifies the name space.
Fred Drake456035f1997-12-03 04:06:57 +0000406\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000407
Fred Drake456035f1997-12-03 04:06:57 +0000408\begin{opcodedesc}{IMPORT_FROM}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000409Imports the attribute \code{co_names[\var{namei}]}. The module to import
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000410from is found in TOS and left there.
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}{JUMP_FORWARD}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000414Increments byte code counter by \var{delta}.
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_IF_TRUE}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000418If TOS is true, increment the byte code counter by \var{delta}. TOS is
419left on the stack.
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_FALSE}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000423If TOS is false, increment the byte code counter by \var{delta}. TOS
424is not changed.
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_ABSOLUTE}{target}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000428Set byte code counter to \var{target}.
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}{FOR_LOOP}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000432Iterate over a sequence. TOS is the current index, TOS1 the sequence.
433First, the next element is computed. If the sequence is exhausted,
434increment byte code counter by \var{delta}. Otherwise, push the
435sequence, the incremented counter, and the current item onto the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000436\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000437
Fred Drake456035f1997-12-03 04:06:57 +0000438%\begin{opcodedesc}{LOAD_LOCAL}{namei}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000439%This opcode is obsolete.
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_GLOBAL}{namei}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000443Loads the global named \code{co_names[\var{namei}]} onto the stack.
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}{SET_FUNC_ARGS}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000447%This opcode is obsolete.
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}{SETUP_LOOP}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000451Pushes a block for a loop onto the block stack. The block spans
452from the current instruction with a size of \var{delta} bytes.
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_EXCEPT}{delta}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000456Pushes a try block from a try-except clause onto the block stack.
457\var{delta} points to the first except block.
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_FINALLY}{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 finally 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}{LOAD_FAST}{var_num}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000466Pushes a reference to the local \code{co_varnames[\var{var_num}]} onto
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000467the stack.
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}{STORE_FAST}{var_num}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000471Stores TOS into the local \code{co_varnames[\var{var_num}]}.
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}{DELETE_FAST}{var_num}
Fred Drakedd1f6cc1998-02-12 03:32:18 +0000475Deletes 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}{SET_LINE_NO}{lineno}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000479Sets the current line number to \var{lineno}.
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}{RAISE_VARARGS}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000483Raises an exception. \var{argc} indicates the number of parameters
484to the raise statement, ranging from 1 to 3. The handler will find
485the traceback as TOS2, the parameter as TOS1, and the exception
486as TOS.
Fred Drake456035f1997-12-03 04:06:57 +0000487\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000488
Fred Drake456035f1997-12-03 04:06:57 +0000489\begin{opcodedesc}{CALL_FUNCTION}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000490Calls a function. The low byte of \var{argc} indicates the number of
491positional parameters, the high byte the number of keyword parameters.
492On the stack, the opcode finds the keyword parameters first. For each
493keyword argument, the value is on top of the key. Below the keyword
494parameters, the positional parameters are on the stack, with the
495right-most parameter on top. Below the parameters, the function object
496to call is on the stack.
Fred Drake456035f1997-12-03 04:06:57 +0000497\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000498
Fred Drake456035f1997-12-03 04:06:57 +0000499\begin{opcodedesc}{MAKE_FUNCTION}{argc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000500Pushes a new function object on the stack. TOS is the code associated
501with the function. The function object is defined to have \var{argc}
502default parameters, which are found below TOS.
Fred Drake456035f1997-12-03 04:06:57 +0000503\end{opcodedesc}
Guido van Rossumb62b6d11997-11-18 15:10:53 +0000504
Fred Drake456035f1997-12-03 04:06:57 +0000505\begin{opcodedesc}{BUILD_SLICE}{argc}
Guido van Rossumda623981998-02-12 03:53:02 +0000506Pushes a slice object on the stack. \var{argc} must be 2 or 3. If it
507is 2, \code{slice(TOS1, TOS)} is pushed; if it is 3,
508\code{slice(TOS2, TOS1, TOS)} is pushed.
Fred Drake19479911998-02-13 06:58:54 +0000509See the \code{slice()}\bifuncindex{slice} built-in function.
Fred Drake456035f1997-12-03 04:06:57 +0000510\end{opcodedesc}