blob: 7ce864d6e434a665ae3c7403d51a409b4c98bd4a [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
Guido van Rossum04110fb2007-08-24 16:32:05 +0000477
478.. opcode:: WITH_CLEANUP ()
479
480 Cleans up the stack when a :keyword:`with` statement block exits. TOS is the
481 context manager's :meth:`__exit__` bound method. Below that are 1--3 values
482 indicating how/why the finally clause was entered:
483
484 * SECOND = None
485 * (SECOND, THIRD) = (WHY_{RETURN,CONTINUE}), retval
486 * SECOND = WHY_\*; no retval below it
487 * (SECOND, THIRD, FOURTH) = exc_info()
488
489 In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
490 ``TOS(None, None, None)``.
491
492 In addition, if the stack represents an exception, *and* the function call
493 returns a 'true' value, this information is "zapped", to prevent ``END_FINALLY``
494 from re-raising the exception. (But non-local gotos should still be resumed.)
495
496
Georg Brandl116aa622007-08-15 14:28:22 +0000497All of the following opcodes expect arguments. An argument is two bytes, with
498the more significant byte last.
499
Georg Brandl116aa622007-08-15 14:28:22 +0000500.. opcode:: STORE_NAME (namei)
501
502 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
503 :attr:`co_names` of the code object. The compiler tries to use ``STORE_LOCAL``
504 or ``STORE_GLOBAL`` if possible.
505
506
507.. opcode:: DELETE_NAME (namei)
508
509 Implements ``del name``, where *namei* is the index into :attr:`co_names`
510 attribute of the code object.
511
512
513.. opcode:: UNPACK_SEQUENCE (count)
514
515 Unpacks TOS into *count* individual values, which are put onto the stack
516 right-to-left.
517
518.. % \begin{opcodedesc}{UNPACK_LIST}{count}
519.. % This opcode is obsolete.
520.. % \end{opcodedesc}
521.. % \begin{opcodedesc}{UNPACK_ARG}{count}
522.. % This opcode is obsolete.
523.. % \end{opcodedesc}
524
525
526.. opcode:: DUP_TOPX (count)
527
528 Duplicate *count* items, keeping them in the same order. Due to implementation
529 limits, *count* should be between 1 and 5 inclusive.
530
531
532.. opcode:: STORE_ATTR (namei)
533
534 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
535 :attr:`co_names`.
536
537
538.. opcode:: DELETE_ATTR (namei)
539
540 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
541
542
543.. opcode:: STORE_GLOBAL (namei)
544
545 Works as ``STORE_NAME``, but stores the name as a global.
546
547
548.. opcode:: DELETE_GLOBAL (namei)
549
550 Works as ``DELETE_NAME``, but deletes a global name.
551
552.. % \begin{opcodedesc}{UNPACK_VARARG}{argc}
553.. % This opcode is obsolete.
554.. % \end{opcodedesc}
555
556
557.. opcode:: LOAD_CONST (consti)
558
559 Pushes ``co_consts[consti]`` onto the stack.
560
561
562.. opcode:: LOAD_NAME (namei)
563
564 Pushes the value associated with ``co_names[namei]`` onto the stack.
565
566
567.. opcode:: BUILD_TUPLE (count)
568
569 Creates a tuple consuming *count* items from the stack, and pushes the resulting
570 tuple onto the stack.
571
572
573.. opcode:: BUILD_LIST (count)
574
575 Works as ``BUILD_TUPLE``, but creates a list.
576
577
578.. opcode:: BUILD_SET (count)
579
580 Works as ``BUILD_TUPLE``, but creates a set.
581
582
583.. opcode:: BUILD_MAP (zero)
584
585 Pushes a new empty dictionary object onto the stack. The argument is ignored
586 and set to zero by the compiler.
587
588
589.. opcode:: LOAD_ATTR (namei)
590
591 Replaces TOS with ``getattr(TOS, co_names[namei])``.
592
593
594.. opcode:: COMPARE_OP (opname)
595
596 Performs a Boolean operation. The operation name can be found in
597 ``cmp_op[opname]``.
598
599
600.. opcode:: IMPORT_NAME (namei)
601
602 Imports the module ``co_names[namei]``. The module object is pushed onto the
603 stack. The current namespace is not affected: for a proper import statement, a
604 subsequent ``STORE_FAST`` instruction modifies the namespace.
605
606
607.. opcode:: IMPORT_FROM (namei)
608
609 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
610 resulting object is pushed onto the stack, to be subsequently stored by a
611 ``STORE_FAST`` instruction.
612
613
614.. opcode:: JUMP_FORWARD (delta)
615
616 Increments byte code counter by *delta*.
617
618
619.. opcode:: JUMP_IF_TRUE (delta)
620
621 If TOS is true, increment the byte code counter by *delta*. TOS is left on the
622 stack.
623
624
625.. opcode:: JUMP_IF_FALSE (delta)
626
627 If TOS is false, increment the byte code counter by *delta*. TOS is not
628 changed.
629
630
631.. opcode:: JUMP_ABSOLUTE (target)
632
633 Set byte code counter to *target*.
634
635
636.. opcode:: FOR_ITER (delta)
637
638 ``TOS`` is an iterator. Call its :meth:`__next__` method. If this yields a new
639 value, push it on the stack (leaving the iterator below it). If the iterator
640 indicates it is exhausted ``TOS`` is popped, and the byte code counter is
641 incremented by *delta*.
642
643.. % \begin{opcodedesc}{FOR_LOOP}{delta}
644.. % This opcode is obsolete.
645.. % \end{opcodedesc}
646.. % \begin{opcodedesc}{LOAD_LOCAL}{namei}
647.. % This opcode is obsolete.
648.. % \end{opcodedesc}
649
650
651.. opcode:: LOAD_GLOBAL (namei)
652
653 Loads the global named ``co_names[namei]`` onto the stack.
654
655.. % \begin{opcodedesc}{SET_FUNC_ARGS}{argc}
656.. % This opcode is obsolete.
657.. % \end{opcodedesc}
658
659
660.. opcode:: SETUP_LOOP (delta)
661
662 Pushes a block for a loop onto the block stack. The block spans from the
663 current instruction with a size of *delta* bytes.
664
665
666.. opcode:: SETUP_EXCEPT (delta)
667
668 Pushes a try block from a try-except clause onto the block stack. *delta* points
669 to the first except block.
670
671
672.. opcode:: SETUP_FINALLY (delta)
673
674 Pushes a try block from a try-except clause onto the block stack. *delta* points
675 to the finally block.
676
677
678.. opcode:: LOAD_FAST (var_num)
679
680 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
681
682
683.. opcode:: STORE_FAST (var_num)
684
685 Stores TOS into the local ``co_varnames[var_num]``.
686
687
688.. opcode:: DELETE_FAST (var_num)
689
690 Deletes local ``co_varnames[var_num]``.
691
692
693.. opcode:: LOAD_CLOSURE (i)
694
695 Pushes a reference to the cell contained in slot *i* of the cell and free
696 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
697 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
698 len(co_cellvars)]``.
699
700
701.. opcode:: LOAD_DEREF (i)
702
703 Loads the cell contained in slot *i* of the cell and free variable storage.
704 Pushes a reference to the object the cell contains on the stack.
705
706
707.. opcode:: STORE_DEREF (i)
708
709 Stores TOS into the cell contained in slot *i* of the cell and free variable
710 storage.
711
712
713.. opcode:: SET_LINENO (lineno)
714
715 This opcode is obsolete.
716
717
718.. opcode:: RAISE_VARARGS (argc)
719
720 Raises an exception. *argc* indicates the number of parameters to the raise
721 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
722 the parameter as TOS1, and the exception as TOS.
723
724
725.. opcode:: CALL_FUNCTION (argc)
726
727 Calls a function. The low byte of *argc* indicates the number of positional
728 parameters, the high byte the number of keyword parameters. On the stack, the
729 opcode finds the keyword parameters first. For each keyword argument, the value
730 is on top of the key. Below the keyword parameters, the positional parameters
731 are on the stack, with the right-most parameter on top. Below the parameters,
732 the function object to call is on the stack.
733
734
735.. opcode:: MAKE_FUNCTION (argc)
736
737 Pushes a new function object on the stack. TOS is the code associated with the
738 function. The function object is defined to have *argc* default parameters,
739 which are found below TOS.
740
741
742.. opcode:: MAKE_CLOSURE (argc)
743
Guido van Rossum04110fb2007-08-24 16:32:05 +0000744 Creates a new function object, sets its *__closure__* slot, and pushes it on
745 the stack. TOS is the code associated with the function, TOS1 the tuple
746 containing cells for the closure's free variables. The function also has
747 *argc* default parameters, which are found below the cells.
Georg Brandl116aa622007-08-15 14:28:22 +0000748
749
750.. opcode:: BUILD_SLICE (argc)
751
752 .. index:: builtin: slice
753
754 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
755 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
756 pushed. See the ``slice()`` built-in function for more information.
757
758
759.. opcode:: EXTENDED_ARG (ext)
760
761 Prefixes any opcode which has an argument too big to fit into the default two
762 bytes. *ext* holds two additional bytes which, taken together with the
763 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
764 most-significant bytes.
765
766
767.. opcode:: CALL_FUNCTION_VAR (argc)
768
769 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
770 on the stack contains the variable argument list, followed by keyword and
771 positional arguments.
772
773
774.. opcode:: CALL_FUNCTION_KW (argc)
775
776 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
777 on the stack contains the keyword arguments dictionary, followed by explicit
778 keyword and positional arguments.
779
780
781.. opcode:: CALL_FUNCTION_VAR_KW (argc)
782
783 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
784 element on the stack contains the keyword arguments dictionary, followed by the
785 variable-arguments tuple, followed by explicit keyword and positional arguments.
786
787
788.. opcode:: HAVE_ARGUMENT ()
789
790 This is not really an opcode. It identifies the dividing line between opcodes
791 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
792 HAVE_ARGUMENT``.
793