blob: 5f2847305cb5b6940c4fa068bd84ebfa8233bacb [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`dis` --- Disassembler for Python byte code
3================================================
4
5.. module:: dis
6 :synopsis: Disassembler for Python byte code.
7
8
9The :mod:`dis` module supports the analysis of Python byte code by disassembling
10it. Since there is no Python assembler, this module defines the Python assembly
11language. The Python byte code which this module takes as an input is defined
12in the file :file:`Include/opcode.h` and used by the compiler and the
13interpreter.
14
15Example: Given the function :func:`myfunc`::
16
17 def myfunc(alist):
18 return len(alist)
19
20the following command can be used to get the disassembly of :func:`myfunc`::
21
22 >>> dis.dis(myfunc)
23 2 0 LOAD_GLOBAL 0 (len)
24 3 LOAD_FAST 0 (alist)
25 6 CALL_FUNCTION 1
26 9 RETURN_VALUE
27
28(The "2" is a line number).
29
30The :mod:`dis` module defines the following functions and constants:
31
32
33.. function:: dis([bytesource])
34
35 Disassemble the *bytesource* object. *bytesource* can denote either a module, a
36 class, a method, a function, or a code object. For a module, it disassembles
37 all functions. For a class, it disassembles all methods. For a single code
38 sequence, it prints one line per byte code instruction. If no object is
39 provided, it disassembles the last traceback.
40
41
42.. function:: distb([tb])
43
44 Disassembles the top-of-stack function of a traceback, using the last traceback
45 if none was passed. The instruction causing the exception is indicated.
46
47
48.. function:: disassemble(code[, lasti])
49
50 Disassembles a code object, indicating the last instruction if *lasti* was
51 provided. The output is divided in the following columns:
52
53 #. the line number, for the first instruction of each line
54 #. the current instruction, indicated as ``-->``,
55 #. a labelled instruction, indicated with ``>>``,
56 #. the address of the instruction,
57 #. the operation code name,
58 #. operation parameters, and
59 #. interpretation of the parameters in parentheses.
60
61 The parameter interpretation recognizes local and global variable names,
62 constant values, branch targets, and compare operators.
63
64
65.. function:: disco(code[, lasti])
66
67 A synonym for disassemble. It is more convenient to type, and kept for
68 compatibility with earlier Python releases.
69
70
71.. data:: opname
72
73 Sequence of operation names, indexable using the byte code.
74
75
76.. data:: opmap
77
78 Dictionary mapping byte codes to operation names.
79
80
81.. data:: cmp_op
82
83 Sequence of all compare operation names.
84
85
86.. data:: hasconst
87
88 Sequence of byte codes that have a constant parameter.
89
90
91.. data:: hasfree
92
93 Sequence of byte codes that access a free variable.
94
95
96.. data:: hasname
97
98 Sequence of byte codes that access an attribute by name.
99
100
101.. data:: hasjrel
102
103 Sequence of byte codes that have a relative jump target.
104
105
106.. data:: hasjabs
107
108 Sequence of byte codes that have an absolute jump target.
109
110
111.. data:: haslocal
112
113 Sequence of byte codes that access a local variable.
114
115
116.. data:: hascompare
117
118 Sequence of byte codes of Boolean operations.
119
120
121.. _bytecodes:
122
123Python Byte Code Instructions
124-----------------------------
125
126The Python compiler currently generates the following byte code instructions.
127
128
129.. opcode:: STOP_CODE ()
130
131 Indicates end-of-code to the compiler, not used by the interpreter.
132
133
134.. opcode:: NOP ()
135
136 Do nothing code. Used as a placeholder by the bytecode optimizer.
137
138
139.. opcode:: POP_TOP ()
140
141 Removes the top-of-stack (TOS) item.
142
143
144.. opcode:: ROT_TWO ()
145
146 Swaps the two top-most stack items.
147
148
149.. opcode:: ROT_THREE ()
150
151 Lifts second and third stack item one position up, moves top down to position
152 three.
153
154
155.. opcode:: ROT_FOUR ()
156
157 Lifts second, third and forth stack item one position up, moves top down to
158 position four.
159
160
161.. opcode:: DUP_TOP ()
162
163 Duplicates the reference on top of the stack.
164
165Unary Operations take the top of the stack, apply the operation, and push the
166result back on the stack.
167
168
169.. opcode:: UNARY_POSITIVE ()
170
171 Implements ``TOS = +TOS``.
172
173
174.. opcode:: UNARY_NEGATIVE ()
175
176 Implements ``TOS = -TOS``.
177
178
179.. opcode:: UNARY_NOT ()
180
181 Implements ``TOS = not TOS``.
182
183
184.. opcode:: UNARY_INVERT ()
185
186 Implements ``TOS = ~TOS``.
187
188
189.. opcode:: GET_ITER ()
190
191 Implements ``TOS = iter(TOS)``.
192
193Binary operations remove the top of the stack (TOS) and the second top-most
194stack item (TOS1) from the stack. They perform the operation, and put the
195result back on the stack.
196
197
198.. opcode:: BINARY_POWER ()
199
200 Implements ``TOS = TOS1 ** TOS``.
201
202
203.. opcode:: BINARY_MULTIPLY ()
204
205 Implements ``TOS = TOS1 * TOS``.
206
207
208.. opcode:: BINARY_FLOOR_DIVIDE ()
209
210 Implements ``TOS = TOS1 // TOS``.
211
212
213.. opcode:: BINARY_TRUE_DIVIDE ()
214
215 Implements ``TOS = TOS1 / TOS`` when ``from __future__ import division`` is in
216 effect.
217
218
219.. opcode:: BINARY_MODULO ()
220
221 Implements ``TOS = TOS1 % TOS``.
222
223
224.. opcode:: BINARY_ADD ()
225
226 Implements ``TOS = TOS1 + TOS``.
227
228
229.. opcode:: BINARY_SUBTRACT ()
230
231 Implements ``TOS = TOS1 - TOS``.
232
233
234.. opcode:: BINARY_SUBSCR ()
235
236 Implements ``TOS = TOS1[TOS]``.
237
238
239.. opcode:: BINARY_LSHIFT ()
240
241 Implements ``TOS = TOS1 << TOS``.
242
243
244.. opcode:: BINARY_RSHIFT ()
245
246 Implements ``TOS = TOS1 >> TOS``.
247
248
249.. opcode:: BINARY_AND ()
250
251 Implements ``TOS = TOS1 & TOS``.
252
253
254.. opcode:: BINARY_XOR ()
255
256 Implements ``TOS = TOS1 ^ TOS``.
257
258
259.. opcode:: BINARY_OR ()
260
261 Implements ``TOS = TOS1 | TOS``.
262
263In-place operations are like binary operations, in that they remove TOS and
264TOS1, and push the result back on the stack, but the operation is done in-place
265when TOS1 supports it, and the resulting TOS may be (but does not have to be)
266the original TOS1.
267
268
269.. opcode:: INPLACE_POWER ()
270
271 Implements in-place ``TOS = TOS1 ** TOS``.
272
273
274.. opcode:: INPLACE_MULTIPLY ()
275
276 Implements in-place ``TOS = TOS1 * TOS``.
277
278
279.. opcode:: INPLACE_FLOOR_DIVIDE ()
280
281 Implements in-place ``TOS = TOS1 // TOS``.
282
283
284.. opcode:: INPLACE_TRUE_DIVIDE ()
285
286 Implements in-place ``TOS = TOS1 / TOS`` when ``from __future__ import
287 division`` is in effect.
288
289
290.. opcode:: INPLACE_MODULO ()
291
292 Implements in-place ``TOS = TOS1 % TOS``.
293
294
295.. opcode:: INPLACE_ADD ()
296
297 Implements in-place ``TOS = TOS1 + TOS``.
298
299
300.. opcode:: INPLACE_SUBTRACT ()
301
302 Implements in-place ``TOS = TOS1 - TOS``.
303
304
305.. opcode:: INPLACE_LSHIFT ()
306
307 Implements in-place ``TOS = TOS1 << TOS``.
308
309
310.. opcode:: INPLACE_RSHIFT ()
311
312 Implements in-place ``TOS = TOS1 >> TOS``.
313
314
315.. opcode:: INPLACE_AND ()
316
317 Implements in-place ``TOS = TOS1 & TOS``.
318
319
320.. opcode:: INPLACE_XOR ()
321
322 Implements in-place ``TOS = TOS1 ^ TOS``.
323
324
325.. opcode:: INPLACE_OR ()
326
327 Implements in-place ``TOS = TOS1 | TOS``.
328
329The slice opcodes take up to three parameters.
330
331
332.. opcode:: SLICE+0 ()
333
334 Implements ``TOS = TOS[:]``.
335
336
337.. opcode:: SLICE+1 ()
338
339 Implements ``TOS = TOS1[TOS:]``.
340
341
342.. opcode:: SLICE+2 ()
343
344 Implements ``TOS = TOS1[:TOS]``.
345
346
347.. opcode:: SLICE+3 ()
348
349 Implements ``TOS = TOS2[TOS1:TOS]``.
350
351Slice assignment needs even an additional parameter. As any statement, they put
352nothing on the stack.
353
354
355.. opcode:: STORE_SLICE+0 ()
356
357 Implements ``TOS[:] = TOS1``.
358
359
360.. opcode:: STORE_SLICE+1 ()
361
362 Implements ``TOS1[TOS:] = TOS2``.
363
364
365.. opcode:: STORE_SLICE+2 ()
366
367 Implements ``TOS1[:TOS] = TOS2``.
368
369
370.. opcode:: STORE_SLICE+3 ()
371
372 Implements ``TOS2[TOS1:TOS] = TOS3``.
373
374
375.. opcode:: DELETE_SLICE+0 ()
376
377 Implements ``del TOS[:]``.
378
379
380.. opcode:: DELETE_SLICE+1 ()
381
382 Implements ``del TOS1[TOS:]``.
383
384
385.. opcode:: DELETE_SLICE+2 ()
386
387 Implements ``del TOS1[:TOS]``.
388
389
390.. opcode:: DELETE_SLICE+3 ()
391
392 Implements ``del TOS2[TOS1:TOS]``.
393
394
395.. opcode:: STORE_SUBSCR ()
396
397 Implements ``TOS1[TOS] = TOS2``.
398
399
400.. opcode:: DELETE_SUBSCR ()
401
402 Implements ``del TOS1[TOS]``.
403
404Miscellaneous opcodes.
405
406
407.. opcode:: PRINT_EXPR ()
408
409 Implements the expression statement for the interactive mode. TOS is removed
410 from the stack and printed. In non-interactive mode, an expression statement is
411 terminated with ``POP_STACK``.
412
413
414.. opcode:: BREAK_LOOP ()
415
416 Terminates a loop due to a :keyword:`break` statement.
417
418
419.. opcode:: CONTINUE_LOOP (target)
420
421 Continues a loop due to a :keyword:`continue` statement. *target* is the
422 address to jump to (which should be a ``FOR_ITER`` instruction).
423
424
425.. opcode:: SET_ADD ()
426
427 Calls ``set.add(TOS1, TOS)``. Used to implement set comprehensions.
428
429
430.. opcode:: LIST_APPEND ()
431
432 Calls ``list.append(TOS1, TOS)``. Used to implement list comprehensions.
433
434
435.. opcode:: LOAD_LOCALS ()
436
437 Pushes a reference to the locals of the current scope on the stack. This is used
438 in the code for a class definition: After the class body is evaluated, the
439 locals are passed to the class definition.
440
441
442.. opcode:: RETURN_VALUE ()
443
444 Returns with TOS to the caller of the function.
445
446
447.. opcode:: YIELD_VALUE ()
448
449 Pops ``TOS`` and yields it from a generator.
450
451
452.. opcode:: IMPORT_STAR ()
453
454 Loads all symbols not starting with ``'_'`` directly from the module TOS to the
455 local namespace. The module is popped after loading all names. This opcode
456 implements ``from module import *``.
457
458
459.. opcode:: POP_BLOCK ()
460
461 Removes one block from the block stack. Per frame, there is a stack of blocks,
462 denoting nested loops, try statements, and such.
463
464
465.. opcode:: END_FINALLY ()
466
467 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
468 exception has to be re-raised, or whether the function returns, and continues
469 with the outer-next block.
470
471
472.. opcode:: BUILD_CLASS ()
473
474 Creates a new class object. TOS is the methods dictionary, TOS1 the tuple of
475 the names of the base classes, and TOS2 the class name.
476
477All of the following opcodes expect arguments. An argument is two bytes, with
478the more significant byte last.
479
480
481.. opcode:: STORE_NAME (namei)
482
483 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
484 :attr:`co_names` of the code object. The compiler tries to use ``STORE_LOCAL``
485 or ``STORE_GLOBAL`` if possible.
486
487
488.. opcode:: DELETE_NAME (namei)
489
490 Implements ``del name``, where *namei* is the index into :attr:`co_names`
491 attribute of the code object.
492
493
494.. opcode:: UNPACK_SEQUENCE (count)
495
496 Unpacks TOS into *count* individual values, which are put onto the stack
497 right-to-left.
498
499.. % \begin{opcodedesc}{UNPACK_LIST}{count}
500.. % This opcode is obsolete.
501.. % \end{opcodedesc}
502.. % \begin{opcodedesc}{UNPACK_ARG}{count}
503.. % This opcode is obsolete.
504.. % \end{opcodedesc}
505
506
507.. opcode:: DUP_TOPX (count)
508
509 Duplicate *count* items, keeping them in the same order. Due to implementation
510 limits, *count* should be between 1 and 5 inclusive.
511
512
513.. opcode:: STORE_ATTR (namei)
514
515 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
516 :attr:`co_names`.
517
518
519.. opcode:: DELETE_ATTR (namei)
520
521 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
522
523
524.. opcode:: STORE_GLOBAL (namei)
525
526 Works as ``STORE_NAME``, but stores the name as a global.
527
528
529.. opcode:: DELETE_GLOBAL (namei)
530
531 Works as ``DELETE_NAME``, but deletes a global name.
532
533.. % \begin{opcodedesc}{UNPACK_VARARG}{argc}
534.. % This opcode is obsolete.
535.. % \end{opcodedesc}
536
537
538.. opcode:: LOAD_CONST (consti)
539
540 Pushes ``co_consts[consti]`` onto the stack.
541
542
543.. opcode:: LOAD_NAME (namei)
544
545 Pushes the value associated with ``co_names[namei]`` onto the stack.
546
547
548.. opcode:: BUILD_TUPLE (count)
549
550 Creates a tuple consuming *count* items from the stack, and pushes the resulting
551 tuple onto the stack.
552
553
554.. opcode:: BUILD_LIST (count)
555
556 Works as ``BUILD_TUPLE``, but creates a list.
557
558
559.. opcode:: BUILD_SET (count)
560
561 Works as ``BUILD_TUPLE``, but creates a set.
562
563
564.. opcode:: BUILD_MAP (zero)
565
566 Pushes a new empty dictionary object onto the stack. The argument is ignored
567 and set to zero by the compiler.
568
569
570.. opcode:: LOAD_ATTR (namei)
571
572 Replaces TOS with ``getattr(TOS, co_names[namei])``.
573
574
575.. opcode:: COMPARE_OP (opname)
576
577 Performs a Boolean operation. The operation name can be found in
578 ``cmp_op[opname]``.
579
580
581.. opcode:: IMPORT_NAME (namei)
582
583 Imports the module ``co_names[namei]``. The module object is pushed onto the
584 stack. The current namespace is not affected: for a proper import statement, a
585 subsequent ``STORE_FAST`` instruction modifies the namespace.
586
587
588.. opcode:: IMPORT_FROM (namei)
589
590 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
591 resulting object is pushed onto the stack, to be subsequently stored by a
592 ``STORE_FAST`` instruction.
593
594
595.. opcode:: JUMP_FORWARD (delta)
596
597 Increments byte code counter by *delta*.
598
599
600.. opcode:: JUMP_IF_TRUE (delta)
601
602 If TOS is true, increment the byte code counter by *delta*. TOS is left on the
603 stack.
604
605
606.. opcode:: JUMP_IF_FALSE (delta)
607
608 If TOS is false, increment the byte code counter by *delta*. TOS is not
609 changed.
610
611
612.. opcode:: JUMP_ABSOLUTE (target)
613
614 Set byte code counter to *target*.
615
616
617.. opcode:: FOR_ITER (delta)
618
619 ``TOS`` is an iterator. Call its :meth:`__next__` method. If this yields a new
620 value, push it on the stack (leaving the iterator below it). If the iterator
621 indicates it is exhausted ``TOS`` is popped, and the byte code counter is
622 incremented by *delta*.
623
624.. % \begin{opcodedesc}{FOR_LOOP}{delta}
625.. % This opcode is obsolete.
626.. % \end{opcodedesc}
627.. % \begin{opcodedesc}{LOAD_LOCAL}{namei}
628.. % This opcode is obsolete.
629.. % \end{opcodedesc}
630
631
632.. opcode:: LOAD_GLOBAL (namei)
633
634 Loads the global named ``co_names[namei]`` onto the stack.
635
636.. % \begin{opcodedesc}{SET_FUNC_ARGS}{argc}
637.. % This opcode is obsolete.
638.. % \end{opcodedesc}
639
640
641.. opcode:: SETUP_LOOP (delta)
642
643 Pushes a block for a loop onto the block stack. The block spans from the
644 current instruction with a size of *delta* bytes.
645
646
647.. opcode:: SETUP_EXCEPT (delta)
648
649 Pushes a try block from a try-except clause onto the block stack. *delta* points
650 to the first except block.
651
652
653.. opcode:: SETUP_FINALLY (delta)
654
655 Pushes a try block from a try-except clause onto the block stack. *delta* points
656 to the finally block.
657
658
659.. opcode:: LOAD_FAST (var_num)
660
661 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
662
663
664.. opcode:: STORE_FAST (var_num)
665
666 Stores TOS into the local ``co_varnames[var_num]``.
667
668
669.. opcode:: DELETE_FAST (var_num)
670
671 Deletes local ``co_varnames[var_num]``.
672
673
674.. opcode:: LOAD_CLOSURE (i)
675
676 Pushes a reference to the cell contained in slot *i* of the cell and free
677 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
678 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
679 len(co_cellvars)]``.
680
681
682.. opcode:: LOAD_DEREF (i)
683
684 Loads the cell contained in slot *i* of the cell and free variable storage.
685 Pushes a reference to the object the cell contains on the stack.
686
687
688.. opcode:: STORE_DEREF (i)
689
690 Stores TOS into the cell contained in slot *i* of the cell and free variable
691 storage.
692
693
694.. opcode:: SET_LINENO (lineno)
695
696 This opcode is obsolete.
697
698
699.. opcode:: RAISE_VARARGS (argc)
700
701 Raises an exception. *argc* indicates the number of parameters to the raise
702 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
703 the parameter as TOS1, and the exception as TOS.
704
705
706.. opcode:: CALL_FUNCTION (argc)
707
708 Calls a function. The low byte of *argc* indicates the number of positional
709 parameters, the high byte the number of keyword parameters. On the stack, the
710 opcode finds the keyword parameters first. For each keyword argument, the value
711 is on top of the key. Below the keyword parameters, the positional parameters
712 are on the stack, with the right-most parameter on top. Below the parameters,
713 the function object to call is on the stack.
714
715
716.. opcode:: MAKE_FUNCTION (argc)
717
718 Pushes a new function object on the stack. TOS is the code associated with the
719 function. The function object is defined to have *argc* default parameters,
720 which are found below TOS.
721
722
723.. opcode:: MAKE_CLOSURE (argc)
724
725 Creates a new function object, sets its *__closure__* slot, and pushes it on the
726 stack. TOS is the code associated with the function. If the code object has N
727 free variables, the next N items on the stack are the cells for these variables.
728 The function also has *argc* default parameters, where are found before the
729 cells.
730
731
732.. opcode:: BUILD_SLICE (argc)
733
734 .. index:: builtin: slice
735
736 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
737 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
738 pushed. See the ``slice()`` built-in function for more information.
739
740
741.. opcode:: EXTENDED_ARG (ext)
742
743 Prefixes any opcode which has an argument too big to fit into the default two
744 bytes. *ext* holds two additional bytes which, taken together with the
745 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
746 most-significant bytes.
747
748
749.. opcode:: CALL_FUNCTION_VAR (argc)
750
751 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
752 on the stack contains the variable argument list, followed by keyword and
753 positional arguments.
754
755
756.. opcode:: CALL_FUNCTION_KW (argc)
757
758 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
759 on the stack contains the keyword arguments dictionary, followed by explicit
760 keyword and positional arguments.
761
762
763.. opcode:: CALL_FUNCTION_VAR_KW (argc)
764
765 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
766 element on the stack contains the keyword arguments dictionary, followed by the
767 variable-arguments tuple, followed by explicit keyword and positional arguments.
768
769
770.. opcode:: HAVE_ARGUMENT ()
771
772 This is not really an opcode. It identifies the dividing line between opcodes
773 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
774 HAVE_ARGUMENT``.
775