blob: 2a142633020e4db9357264e2e3f68b06b9ae327b [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
Georg Brandl116aa622007-08-15 14:28:22 +0000329
330.. opcode:: STORE_SUBSCR ()
331
332 Implements ``TOS1[TOS] = TOS2``.
333
334
335.. opcode:: DELETE_SUBSCR ()
336
337 Implements ``del TOS1[TOS]``.
338
339Miscellaneous opcodes.
340
341
342.. opcode:: PRINT_EXPR ()
343
344 Implements the expression statement for the interactive mode. TOS is removed
345 from the stack and printed. In non-interactive mode, an expression statement is
346 terminated with ``POP_STACK``.
347
348
349.. opcode:: BREAK_LOOP ()
350
351 Terminates a loop due to a :keyword:`break` statement.
352
353
354.. opcode:: CONTINUE_LOOP (target)
355
356 Continues a loop due to a :keyword:`continue` statement. *target* is the
357 address to jump to (which should be a ``FOR_ITER`` instruction).
358
359
360.. opcode:: SET_ADD ()
361
362 Calls ``set.add(TOS1, TOS)``. Used to implement set comprehensions.
363
364
365.. opcode:: LIST_APPEND ()
366
367 Calls ``list.append(TOS1, TOS)``. Used to implement list comprehensions.
368
369
370.. opcode:: LOAD_LOCALS ()
371
372 Pushes a reference to the locals of the current scope on the stack. This is used
373 in the code for a class definition: After the class body is evaluated, the
374 locals are passed to the class definition.
375
376
377.. opcode:: RETURN_VALUE ()
378
379 Returns with TOS to the caller of the function.
380
381
382.. opcode:: YIELD_VALUE ()
383
384 Pops ``TOS`` and yields it from a generator.
385
386
387.. opcode:: IMPORT_STAR ()
388
389 Loads all symbols not starting with ``'_'`` directly from the module TOS to the
390 local namespace. The module is popped after loading all names. This opcode
391 implements ``from module import *``.
392
393
394.. opcode:: POP_BLOCK ()
395
396 Removes one block from the block stack. Per frame, there is a stack of blocks,
397 denoting nested loops, try statements, and such.
398
399
400.. opcode:: END_FINALLY ()
401
402 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
403 exception has to be re-raised, or whether the function returns, and continues
404 with the outer-next block.
405
406
407.. opcode:: BUILD_CLASS ()
408
409 Creates a new class object. TOS is the methods dictionary, TOS1 the tuple of
410 the names of the base classes, and TOS2 the class name.
411
Guido van Rossum04110fb2007-08-24 16:32:05 +0000412
413.. opcode:: WITH_CLEANUP ()
414
415 Cleans up the stack when a :keyword:`with` statement block exits. TOS is the
416 context manager's :meth:`__exit__` bound method. Below that are 1--3 values
417 indicating how/why the finally clause was entered:
418
419 * SECOND = None
420 * (SECOND, THIRD) = (WHY_{RETURN,CONTINUE}), retval
421 * SECOND = WHY_\*; no retval below it
422 * (SECOND, THIRD, FOURTH) = exc_info()
423
424 In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
425 ``TOS(None, None, None)``.
426
427 In addition, if the stack represents an exception, *and* the function call
428 returns a 'true' value, this information is "zapped", to prevent ``END_FINALLY``
429 from re-raising the exception. (But non-local gotos should still be resumed.)
430
431
Georg Brandl116aa622007-08-15 14:28:22 +0000432All of the following opcodes expect arguments. An argument is two bytes, with
433the more significant byte last.
434
Georg Brandl116aa622007-08-15 14:28:22 +0000435.. opcode:: STORE_NAME (namei)
436
437 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
438 :attr:`co_names` of the code object. The compiler tries to use ``STORE_LOCAL``
439 or ``STORE_GLOBAL`` if possible.
440
441
442.. opcode:: DELETE_NAME (namei)
443
444 Implements ``del name``, where *namei* is the index into :attr:`co_names`
445 attribute of the code object.
446
447
448.. opcode:: UNPACK_SEQUENCE (count)
449
450 Unpacks TOS into *count* individual values, which are put onto the stack
451 right-to-left.
452
453.. % \begin{opcodedesc}{UNPACK_LIST}{count}
454.. % This opcode is obsolete.
455.. % \end{opcodedesc}
456.. % \begin{opcodedesc}{UNPACK_ARG}{count}
457.. % This opcode is obsolete.
458.. % \end{opcodedesc}
459
460
461.. opcode:: DUP_TOPX (count)
462
463 Duplicate *count* items, keeping them in the same order. Due to implementation
464 limits, *count* should be between 1 and 5 inclusive.
465
466
467.. opcode:: STORE_ATTR (namei)
468
469 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
470 :attr:`co_names`.
471
472
473.. opcode:: DELETE_ATTR (namei)
474
475 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
476
477
478.. opcode:: STORE_GLOBAL (namei)
479
480 Works as ``STORE_NAME``, but stores the name as a global.
481
482
483.. opcode:: DELETE_GLOBAL (namei)
484
485 Works as ``DELETE_NAME``, but deletes a global name.
486
487.. % \begin{opcodedesc}{UNPACK_VARARG}{argc}
488.. % This opcode is obsolete.
489.. % \end{opcodedesc}
490
491
492.. opcode:: LOAD_CONST (consti)
493
494 Pushes ``co_consts[consti]`` onto the stack.
495
496
497.. opcode:: LOAD_NAME (namei)
498
499 Pushes the value associated with ``co_names[namei]`` onto the stack.
500
501
502.. opcode:: BUILD_TUPLE (count)
503
504 Creates a tuple consuming *count* items from the stack, and pushes the resulting
505 tuple onto the stack.
506
507
508.. opcode:: BUILD_LIST (count)
509
510 Works as ``BUILD_TUPLE``, but creates a list.
511
512
513.. opcode:: BUILD_SET (count)
514
515 Works as ``BUILD_TUPLE``, but creates a set.
516
517
518.. opcode:: BUILD_MAP (zero)
519
520 Pushes a new empty dictionary object onto the stack. The argument is ignored
521 and set to zero by the compiler.
522
523
524.. opcode:: LOAD_ATTR (namei)
525
526 Replaces TOS with ``getattr(TOS, co_names[namei])``.
527
528
529.. opcode:: COMPARE_OP (opname)
530
531 Performs a Boolean operation. The operation name can be found in
532 ``cmp_op[opname]``.
533
534
535.. opcode:: IMPORT_NAME (namei)
536
537 Imports the module ``co_names[namei]``. The module object is pushed onto the
538 stack. The current namespace is not affected: for a proper import statement, a
539 subsequent ``STORE_FAST`` instruction modifies the namespace.
540
541
542.. opcode:: IMPORT_FROM (namei)
543
544 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
545 resulting object is pushed onto the stack, to be subsequently stored by a
546 ``STORE_FAST`` instruction.
547
548
549.. opcode:: JUMP_FORWARD (delta)
550
551 Increments byte code counter by *delta*.
552
553
554.. opcode:: JUMP_IF_TRUE (delta)
555
556 If TOS is true, increment the byte code counter by *delta*. TOS is left on the
557 stack.
558
559
560.. opcode:: JUMP_IF_FALSE (delta)
561
562 If TOS is false, increment the byte code counter by *delta*. TOS is not
563 changed.
564
565
566.. opcode:: JUMP_ABSOLUTE (target)
567
568 Set byte code counter to *target*.
569
570
571.. opcode:: FOR_ITER (delta)
572
573 ``TOS`` is an iterator. Call its :meth:`__next__` method. If this yields a new
574 value, push it on the stack (leaving the iterator below it). If the iterator
575 indicates it is exhausted ``TOS`` is popped, and the byte code counter is
576 incremented by *delta*.
577
578.. % \begin{opcodedesc}{FOR_LOOP}{delta}
579.. % This opcode is obsolete.
580.. % \end{opcodedesc}
581.. % \begin{opcodedesc}{LOAD_LOCAL}{namei}
582.. % This opcode is obsolete.
583.. % \end{opcodedesc}
584
585
586.. opcode:: LOAD_GLOBAL (namei)
587
588 Loads the global named ``co_names[namei]`` onto the stack.
589
590.. % \begin{opcodedesc}{SET_FUNC_ARGS}{argc}
591.. % This opcode is obsolete.
592.. % \end{opcodedesc}
593
594
595.. opcode:: SETUP_LOOP (delta)
596
597 Pushes a block for a loop onto the block stack. The block spans from the
598 current instruction with a size of *delta* bytes.
599
600
601.. opcode:: SETUP_EXCEPT (delta)
602
603 Pushes a try block from a try-except clause onto the block stack. *delta* points
604 to the first except block.
605
606
607.. opcode:: SETUP_FINALLY (delta)
608
609 Pushes a try block from a try-except clause onto the block stack. *delta* points
610 to the finally block.
611
612
613.. opcode:: LOAD_FAST (var_num)
614
615 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
616
617
618.. opcode:: STORE_FAST (var_num)
619
620 Stores TOS into the local ``co_varnames[var_num]``.
621
622
623.. opcode:: DELETE_FAST (var_num)
624
625 Deletes local ``co_varnames[var_num]``.
626
627
628.. opcode:: LOAD_CLOSURE (i)
629
630 Pushes a reference to the cell contained in slot *i* of the cell and free
631 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
632 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
633 len(co_cellvars)]``.
634
635
636.. opcode:: LOAD_DEREF (i)
637
638 Loads the cell contained in slot *i* of the cell and free variable storage.
639 Pushes a reference to the object the cell contains on the stack.
640
641
642.. opcode:: STORE_DEREF (i)
643
644 Stores TOS into the cell contained in slot *i* of the cell and free variable
645 storage.
646
647
648.. opcode:: SET_LINENO (lineno)
649
650 This opcode is obsolete.
651
652
653.. opcode:: RAISE_VARARGS (argc)
654
655 Raises an exception. *argc* indicates the number of parameters to the raise
656 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
657 the parameter as TOS1, and the exception as TOS.
658
659
660.. opcode:: CALL_FUNCTION (argc)
661
662 Calls a function. The low byte of *argc* indicates the number of positional
663 parameters, the high byte the number of keyword parameters. On the stack, the
664 opcode finds the keyword parameters first. For each keyword argument, the value
665 is on top of the key. Below the keyword parameters, the positional parameters
666 are on the stack, with the right-most parameter on top. Below the parameters,
667 the function object to call is on the stack.
668
669
670.. opcode:: MAKE_FUNCTION (argc)
671
672 Pushes a new function object on the stack. TOS is the code associated with the
673 function. The function object is defined to have *argc* default parameters,
674 which are found below TOS.
675
676
677.. opcode:: MAKE_CLOSURE (argc)
678
Guido van Rossum04110fb2007-08-24 16:32:05 +0000679 Creates a new function object, sets its *__closure__* slot, and pushes it on
680 the stack. TOS is the code associated with the function, TOS1 the tuple
681 containing cells for the closure's free variables. The function also has
682 *argc* default parameters, which are found below the cells.
Georg Brandl116aa622007-08-15 14:28:22 +0000683
684
685.. opcode:: BUILD_SLICE (argc)
686
687 .. index:: builtin: slice
688
689 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
690 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
691 pushed. See the ``slice()`` built-in function for more information.
692
693
694.. opcode:: EXTENDED_ARG (ext)
695
696 Prefixes any opcode which has an argument too big to fit into the default two
697 bytes. *ext* holds two additional bytes which, taken together with the
698 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
699 most-significant bytes.
700
701
702.. opcode:: CALL_FUNCTION_VAR (argc)
703
704 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
705 on the stack contains the variable argument list, followed by keyword and
706 positional arguments.
707
708
709.. opcode:: CALL_FUNCTION_KW (argc)
710
711 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
712 on the stack contains the keyword arguments dictionary, followed by explicit
713 keyword and positional arguments.
714
715
716.. opcode:: CALL_FUNCTION_VAR_KW (argc)
717
718 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
719 element on the stack contains the keyword arguments dictionary, followed by the
720 variable-arguments tuple, followed by explicit keyword and positional arguments.
721
722
723.. opcode:: HAVE_ARGUMENT ()
724
725 This is not really an opcode. It identifies the dividing line between opcodes
726 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
727 HAVE_ARGUMENT``.
728