blob: 959c80fb94563c69929ba89d61809aa335a54728 [file] [log] [blame]
Guido van Rossumb62b6d11997-11-18 15:10:53 +00001\section{Standard module \sectcode{dis}} % If implemented in Python
2\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
10\code{Include/opcode.h} and used by the compiler and the interpreter.
11
12Example: Given the function myfunc
13
14\bcode\begin{verbatim}
15def myfunc(alist):
16 return len(alist)
17\end{verbatim}\ecode
18
19the following command can be used to get the disassembly of myfunc:
20
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
36\renewcommand{\indexsubitem}{(in module dis)}
37
38% ---- 3.2. ----
39% For each function, use a ``funcdesc'' block. This has exactly two
40% parameters (each parameters is contained in a set of curly braces):
41% the first parameter is the function name (this automatically
42% generates an index entry); the second parameter is the function's
43% argument list. If there are no arguments, use an empty pair of
44% curly braces. If there is more than one argument, separate the
45% arguments with backslash-comma. Optional parts of the parameter
46% list are contained in \optional{...} (this generates a set of square
47% brackets around its parameter). Arguments are automatically set in
48% italics in the parameter list. Each argument should be mentioned at
49% least once in the description; each usage (even inside \code{...})
50% should be enclosed in \var{...}.
51
52\begin{funcdesc}{dis}{\optional{bytesource}}
53Disassemble the \var{bytesource} object. \var{bytesource} can denote
54either a class, a method, a function, or a code object. For a class,
55it disassembles all methods. For a single code sequence, it prints
56one line per byte code instruction. If no object is provided, it
57disassembles the last traceback.
58\end{funcdesc}
59
60\begin{funcdesc}{distb}{\optional{tb}}
61Disassembles the top-of-stack function of a traceback, using the last
62traceback if none was passed. The instruction causing the exception
63is indicated.
64\end{funcdesc}
65
66\begin{funcdesc}{disassemble}{code\optional{\, lasti}}
67Disassembles a code object, indicating the last instruction if \var{lasti}
68was provided. The output is divided in the following columns:
69\begin{itemize}
70\item the current instruction, indicated as \code{-->},
71\item a labelled instruction, indicated with \code{>>},
72\item the address of the instruction,
73\item the operation code name,
74\item operation parameters, and
75\item interpretation of the parameters in parentheses.
76\end{itemize}
77The parameter interpretation recognizes local and global
78variable names, constant values, branch targets, and compare
79operators.
80\end{funcdesc}
81
82\begin{funcdesc}{disco}{code\optional{\, lasti}}
83A synonym for disassemble. It is more convenient to type, and kept
84for compatibility with earlier Python releases.
85\end{funcdesc}
86
87\begin{datadesc}{opname}
88Sequence of a operation names, indexable using the byte code.
89\end{datadesc}
90
91\begin{datadesc}{cmp_op}
92Sequence of all compare operation names.
93\end{datadesc}
94
95\begin{datadesc}{hasconst}
96Sequence of byte codes that have a constant parameter.
97\end{datadesc}
98
99\begin{datadesc}{hasname}
100Sequence of byte codes that access a attribute by name.
101\end{datadesc}
102
103\begin{datadesc}{hasjrel}
104Sequence of byte codes that have a relative jump target.
105\end{datadesc}
106
107\begin{datadesc}{hasjabs}
108Sequence of byte codes that have an absolute jump target.
109\end{datadesc}
110
111\begin{datadesc}{haslocal}
112Sequence of byte codes that access a a local variable.
113\end{datadesc}
114
115\begin{datadesc}{hascompare}
116Sequence of byte codes of boolean operations.
117\end{datadesc}
118
119\subsection{Python Byte Code Instructions}
120
121The Python compiler currently generates the following byte code
122instructions.
123
124\renewcommand{\indexsubitem}{(byte code insns)}
125
126\begin{funcdesc}{STOP_CODE}{}
127Indicates end-of-code to the compiler, not used by the interpreter.
128\end{funcdesc}
129
130\begin{funcdesc}{POP_TOP}{}
131Removes the top-of-stack (TOS) item.
132\end{funcdesc}
133
134\begin{funcdesc}{ROT_TWO}{}
135Swaps the two top-most stack items.
136\end{funcdesc}
137
138\begin{funcdesc}{ROT_THREE}{}
139Lifts second and third stack item on position up, moves top down
140to position three.
141\end{funcdesc}
142
143\begin{funcdesc}{DUP_TOP}{}
144Duplicates the reference on top of the stack.
145\end{funcdesc}
146
147Unary Operations take the top of the stack, apply the operation, and
148push the result back on the stack.
149
150\begin{funcdesc}{UNARY_POSITIVE}{}
151Implements \code{TOS = +TOS}.
152\end{funcdesc}
153
154\begin{funcdesc}{UNARY_NEG}{}
155Implements \code{TOS = -TOS}.
156\end{funcdesc}
157
158\begin{funcdesc}{UNARY_NOT}{}
159Implements \code{TOS = not TOS}.
160\end{funcdesc}
161
162\begin{funcdesc}{UNARY_CONVERT}{}
163Implements \code{TOS = `TOS`}.
164\end{funcdesc}
165
166\begin{funcdesc}{UNARY_INVERT}{}
167Implements \code{TOS = ~TOS}.
168\end{funcdesc}
169
170Binary operations remove the top of the stack (TOS) and the second top-most
171stack item (TOS1) from the stack. They perform the operation, and put the
172result back on the stack.
173
174\begin{funcdesc}{BINARY_POWER}{}
175Implements \code{TOS = TOS1 ** TOS}.
176\end{funcdesc}
177
178\begin{funcdesc}{BINARY_MULTIPLY}{}
179Implements \code{TOS = TOS1 * TOS}.
180\end{funcdesc}
181
182\begin{funcdesc}{BINARY_DIVIDE}{}
183Implements \code{TOS = TOS1 / TOS}.
184\end{funcdesc}
185
186\begin{funcdesc}{BINARY_MODULO}{}
187Implements \code{TOS = TOS1 \% TOS}.
188\end{funcdesc}
189
190\begin{funcdesc}{BINARY_ADD}{}
191Implements \code{TOS = TOS1 + TOS}.
192\end{funcdesc}
193
194\begin{funcdesc}{BINARY_SUBTRACT}{}
195Implements \code{TOS = TOS1 - TOS}.
196\end{funcdesc}
197
198\begin{funcdesc}{BINARY_SUBSCR}{}
199Implements \code{TOS = TOS1[TOS] }.
200\end{funcdesc}
201
202\begin{funcdesc}{BINARY_LSHIFT}{}
203Implements \code{TOS = TOS1 << TOS }.
204\end{funcdesc}
205
206\begin{funcdesc}{BINARY_RSHIFT}{}
207Implements \code{TOS = TOS1 << TOS }.
208\end{funcdesc}
209
210\begin{funcdesc}{BINARY_AND}{}
211Implements \code{TOS = TOS1 and TOS }.
212\end{funcdesc}
213
214\begin{funcdesc}{BINARY_XOR}{}
215Implements \code{TOS = TOS1 \^{ }TOS }.
216\end{funcdesc}
217
218\begin{funcdesc}{BINARY_OR}{}
219Implements \code{TOS = TOS1 or TOS }.
220\end{funcdesc}
221
222The slice opcodes take up to three parameters.
223
224\begin{funcdesc}{SLICE+0}{}
225Implements \code{TOS = TOS[:]}.
226\end{funcdesc}
227
228\begin{funcdesc}{SLICE+1}{}
229Implements \code{TOS = TOS1[TOS:]}.
230\end{funcdesc}
231
232\begin{funcdesc}{SLICE+2}{}
233Implements \code{TOS = TOS1[:TOS1]}.
234\end{funcdesc}
235
236\begin{funcdesc}{SLICE+3}{}
237Implements \code{TOS = TOS2[TOS1:TOS]}.
238\end{funcdesc}
239
240Slice assignment needs even an additional parameter. As any statement,
241they put nothing on the stack.
242
243\begin{funcdesc}{STORE_SLICE+0}{}
244Implements \code{TOS[:]=TOS1}.
245\end{funcdesc}
246
247\begin{funcdesc}{STORE_SLICE+1}{}
248Implements \code{TOS1[TOS:]=TOS2}.
249\end{funcdesc}
250
251\begin{funcdesc}{STORE_SLICE+2}{}
252Implements \code{TOS1[:TOS]=TOS2}.
253\end{funcdesc}
254
255\begin{funcdesc}{STORE_SLICE+3}{}
256Implements \code{TOS2[TOS1:TOS]=TOS3}.
257\end{funcdesc}
258
259\begin{funcdesc}{DELETE_SLICE+0}{}
260Implements \code{del TOS[:]}.
261\end{funcdesc}
262
263\begin{funcdesc}{DELETE_SLICE+1}{}
264Implements \code{del TOS1[TOS:]}.
265\end{funcdesc}
266
267\begin{funcdesc}{DELETE_SLICE+2}{}
268Implements \code{del TOS1[:TOS]}.
269\end{funcdesc}
270
271\begin{funcdesc}{DELETE_SLICE+3}{}
272Implements \code{del TOS2[TOS1:TOS]}.
273\end{funcdesc}
274
275\begin{funcdesc}{STORE_SUBSCR}{}
276Implements \code{TOS1[TOS]=TOS2}.
277\end{funcdesc}
278
279\begin{funcdesc}{DELETE_SUBSCR}{}
280Implements \code{del TOS1[TOS]}.
281\end{funcdesc}
282
283\begin{funcdesc}{PRINT_EXPR}{}
284Implements the expression statement for the interactive mode. TOS is
285removed from the stack and printed. In non-interactive mode, an
286expression statement is terminated with POP_STACK.
287\end{funcdesc}
288
289\begin{funcdesc}{PRINT_ITEM}{}
290Prints TOS. There is one such instruction for
291each item in the print statement.
292\end{funcdesc}
293
294\begin{funcdesc}{PRINT_NEWLINE}{}
295Prints a new line on \code{sys.stdout}. This is generated as the
296last operation of a print statement, unless the statement ends
297with a comma.
298\end{funcdesc}
299
300\begin{funcdesc}{BREAK_LOOP}{}
301Terminates a loop due to a break statement.
302\end{funcdesc}
303
304\begin{funcdesc}{LOAD_LOCALS}{}
305Pushes a reference to the locals of the current scope on the stack.
306This is used in the code for a class definition: After the class body
307is evaluated, the locals are passed to the class definition.
308\end{funcdesc}
309
310\begin{funcdesc}{RETURN_VALUE}{}
311Returns with TOS to the caller of the function.
312\end{funcdesc}
313
314\begin{funcdesc}{EXEC_STMT}{}
315Implements \code{exec TOS2,TOS1,TOS}. The compiler fills
316missing optional parameters with None.
317\end{funcdesc}
318
319\begin{funcdesc}{POP_BLOCK}{}
320Removes one block from the block stack. Per frame, there is a
321stack of blocks, denoting nested loops, try statements, and such.
322\end{funcdesc}
323
324\begin{funcdesc}{END_FINALLY}{}
325Terminates a finally-block. The interpreter recalls whether the
326exception has to be re-raised, or whether the function returns,
327and continues with the outer-next block.
328\end{funcdesc}
329
330\begin{funcdesc}{BUILD_CLASS}{}
331Creates a new class object. TOS is the methods dictionary, TOS1
332the tuple of the names of the base classes, and TOS2 the class name.
333\end{funcdesc}
334
335All of the following opcodes expect arguments. An argument is two
336bytes, with the more significant byte last.
337
338\begin{funcdesc}{STORE_NAME}{namei}
339Implements \code{name = TOS}. \var{namei} is the index of \var{name}
340in the attribute \code{co_names} of the code object.
341The compiler tries to use STORE_LOCAL or STORE_GLOBAL if possible.
342\end{funcdesc}
343
344\begin{funcdesc}{DELETE_NAME}{namei}
345Implements \code{del name}, where \var{namei} is the index into
346\code{co_names} attribute of the code object.
347\end{funcdesc}
348
349\begin{funcdesc}{UNPACK_TUPLE}{count}
350Unpacks TOS into \var{count} individual values, which are put onto
351the stack right-to-left.
352\end{funcdesc}
353
354\begin{funcdesc}{UNPACK_LIST}{count}
355Unpacks TOS into \var{count} individual values.
356\end{funcdesc}
357
358%\begin{funcdesc}{UNPACK_ARG}{count}
359%This opcode is obsolete.
360%\end{funcdesc}
361
362\begin{funcdesc}{STORE_ATTR}{namei}
363Implements \code{TOS.name = TOS1}, where \var{namei} is the index
364of name in \code{co_names}.
365\end{funcdesc}
366
367\begin{funcdesc}{DELETE_ATTR}{namei}
368Implements \code{del TOS.name}, using \var{namei} as index into
369\code{co_names}.
370\end{funcdesc}
371
372\begin{funcdesc}{STORE_GLOBAL}{namei}
373Works as STORE_NAME, but stores the name as a global.
374\end{funcdesc}
375
376\begin{funcdesc}{DELETE_GLOBAL}{namei}
377Works as DELETE_NAME, but deletes a global name.
378\end{funcdesc}
379
380%\begin{funcdesc}{UNPACK_VARARG}{argc}
381%This opcode is obsolete.
382%\end{funcdesc}
383
384\begin{funcdesc}{LOAD_CONST}{consti}
385Pushes \code{co_consts[consti]} onto the stack.
386\end{funcdesc}
387
388\begin{funcdesc}{LOAD_NAME}{namei}
389Pushes the value associated with \code{co_names[namei]} onto the stack.
390\end{funcdesc}
391
392\begin{funcdesc}{BUILD_TUPLE}{count}
393Creates a tuple consuming \var{count} items from the stack, and pushes
394the resulting tuple onto the stack.
395\end{funcdesc}
396
397\begin{funcdesc}{BUILD_LIST}{count}
398Works as \code{BUILD_TUPLE}, but creates a list.
399\end{funcdesc}
400
401\begin{funcdesc}{BUILD_MAP}{zero}
402Pushes an empty dictionary object onto the stack. The argument is ignored
403and set to zero by the compiler.
404\end{funcdesc}
405
406\begin{funcdesc}{LOAD_ATTR}{namei}
407Replaces TOS with \code{getattr(TOS,co_names[namei]}.
408\end{funcdesc}
409
410\begin{funcdesc}{COMPARE_OP}{opname}
411Performs a boolean operation. The operation name can be found
412in \code{cmp_op[opname]}.
413\end{funcdesc}
414
415\begin{funcdesc}{IMPORT_NAME}{namei}
416Imports the module \code{co_names[namei]}. The module object is
417pushed onto the stack. The current name space is not affect: for a
418proper import statement, a subsequent \code{STORE_FAST} instruction
419modifies the name space.
420\end{funcdesc}
421
422\begin{funcdesc}{IMPORT_FROM}{namei}
423Imports the attribute \code{co_names[namei]}. The module to import
424from is found in TOS and left there.
425\end{funcdesc}
426
427\begin{funcdesc}{JUMP_FORWARD}{delta}
428Increments byte code counter by \var{delta}.
429\end{funcdesc}
430
431\begin{funcdesc}{JUMP_IF_TRUE}{delta}
432If TOS is true, increment the byte code counter by \var{delta}. TOS is
433left on the stack.
434\end{funcdesc}
435
436\begin{funcdesc}{JUMP_IF_FALSE}{delta}
437If TOS is false, increment the byte code counter by \var{delta}. TOS
438is not changed.
439\end{funcdesc}
440
441\begin{funcdesc}{JUMP_ABSOLUTE}{target}
442Set byte code counter to \var{target}.
443\end{funcdesc}
444
445\begin{funcdesc}{FOR_LOOP}{delta}
446Iterate over a sequence. TOS is the current index, TOS1 the sequence.
447First, the next element is computed. If the sequence is exhausted,
448increment byte code counter by \var{delta}. Otherwise, push the
449sequence, the incremented counter, and the current item onto the stack.
450\end{funcdesc}
451
452%\begin{funcdesc}{LOAD_LOCAL}{namei}
453%This opcode is obsolete.
454%\end{funcdesc}
455
456\begin{funcdesc}{LOAD_GLOBAL}{namei}
457Loads the global named \code{co_names[namei]} onto the stack.
458\end{funcdesc}
459
460%\begin{funcdesc}{SET_FUNC_ARGS}{argc}
461%This opcode is obsolete.
462%\end{funcdesc}
463
464\begin{funcdesc}{SETUP_LOOP}{delta}
465Pushes a block for a loop onto the block stack. The block spans
466from the current instruction with a size of \var{delta} bytes.
467\end{funcdesc}
468
469\begin{funcdesc}{SETUP_EXCEPT}{delta}
470Pushes a try block from a try-except clause onto the block stack.
471\var{delta} points to the first except block.
472\end{funcdesc}
473
474\begin{funcdesc}{SETUP_FINALLY}{delta}
475Pushes a try block from a try-except clause onto the block stack.
476\var{delta} points to the finally block.
477\end{funcdesc}
478
479\begin{funcdesc}{LOAD_FAST}{var_num}
480Pushes a reference to the local \code{co_varnames[var_num]} onto
481the stack.
482\end{funcdesc}
483
484\begin{funcdesc}{STORE_FAST}{var_num}
485Stores TOS into the local \code{co_varnames[var_num]}.
486\end{funcdesc}
487
488\begin{funcdesc}{DELETE_FAST}{var_num}
489Deletes local \code{co_varnames[var_num]}.
490\end{funcdesc}
491
492\begin{funcdesc}{SET_LINE_NO}{lineno}
493Sets the current line number to \var{lineno}.
494\end{funcdesc}
495
496\begin{funcdesc}{RAISE_VARARGS}{argc}
497Raises an exception. \var{argc} indicates the number of parameters
498to the raise statement, ranging from 1 to 3. The handler will find
499the traceback as TOS2, the parameter as TOS1, and the exception
500as TOS.
501\end{funcdesc}
502
503\begin{funcdesc}{CALL_FUNCTION}{argc}
504Calls a function. The low byte of \var{argc} indicates the number of
505positional parameters, the high byte the number of keyword parameters.
506On the stack, the opcode finds the keyword parameters first. For each
507keyword argument, the value is on top of the key. Below the keyword
508parameters, the positional parameters are on the stack, with the
509right-most parameter on top. Below the parameters, the function object
510to call is on the stack.
511\end{funcdesc}
512
513\begin{funcdesc}{MAKE_FUNCTION}{argc}
514Pushes a new function object on the stack. TOS is the code associated
515with the function. The function object is defined to have \var{argc}
516default parameters, which are found below TOS.
517\end{funcdesc}
518
519\begin{funcdesc}{BUILD_SLICE}{argc}
520Pushes a slice object on the stack. If \var{argc} is three, creates
521\code{TOS3[TOS2:TOS1:TOS]}. Otherwise, expects three arguments.
522\end{funcdesc}
523
524