blob: 1d5b223ff014432f049cd9b65bfcc54b81246686 [file] [log] [blame]
Georg Brandl9afde1c2007-11-01 20:32:30 +00001:mod:`dis` --- Disassembler for Python bytecode
2===============================================
Georg Brandl116aa622007-08-15 14:28:22 +00003
4.. module:: dis
Georg Brandl9afde1c2007-11-01 20:32:30 +00005 :synopsis: Disassembler for Python bytecode.
Georg Brandl116aa622007-08-15 14:28:22 +00006
7
Brett Cannon8315fd12010-07-02 22:03:00 +00008The :mod:`dis` module supports the analysis of CPython :term:`bytecode` by
9disassembling it. The CPython bytecode which this module takes as an
Georg Brandl71515ca2009-05-17 12:29:12 +000010input is defined in the file :file:`Include/opcode.h` and used by the compiler
11and the interpreter.
Georg Brandl116aa622007-08-15 14:28:22 +000012
Georg Brandl19b7a872010-07-03 10:21:50 +000013.. impl-detail::
14
15 Bytecode is an implementation detail of the CPython interpreter! No
16 guarantees are made that bytecode will not be added, removed, or changed
17 between versions of Python. Use of this module should not be considered to
18 work across Python VMs or Python releases.
19
Brett Cannon8315fd12010-07-02 22:03:00 +000020
Georg Brandl116aa622007-08-15 14:28:22 +000021Example: Given the function :func:`myfunc`::
22
23 def myfunc(alist):
24 return len(alist)
25
26the following command can be used to get the disassembly of :func:`myfunc`::
27
28 >>> dis.dis(myfunc)
29 2 0 LOAD_GLOBAL 0 (len)
30 3 LOAD_FAST 0 (alist)
31 6 CALL_FUNCTION 1
32 9 RETURN_VALUE
33
34(The "2" is a line number).
35
36The :mod:`dis` module defines the following functions and constants:
37
38
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000039.. function:: dis(x=None)
Georg Brandl116aa622007-08-15 14:28:22 +000040
Georg Brandl4833e5b2010-07-03 10:41:33 +000041 Disassemble the *x* object. *x* can denote either a module, a
Nick Coghlan5c8b54e2010-07-03 07:36:51 +000042 class, a method, a function, a code object, a string of source code or a
Georg Brandl4833e5b2010-07-03 10:41:33 +000043 byte sequence of raw bytecode. For a module, it disassembles all
Nick Coghlan5c8b54e2010-07-03 07:36:51 +000044 functions. For a class, it disassembles all methods. For a code object
45 or sequence of raw bytecode, it prints one line per bytecode instruction.
46 Strings are first compiled to code objects with the :func:`compile`
47 built-in function before being disassembled. If no object is provided,
48 this function disassembles the last traceback.
Georg Brandl116aa622007-08-15 14:28:22 +000049
50
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000051.. function:: distb(tb=None)
Georg Brandl116aa622007-08-15 14:28:22 +000052
Georg Brandl4833e5b2010-07-03 10:41:33 +000053 Disassemble the top-of-stack function of a traceback, using the last
54 traceback if none was passed. The instruction causing the exception is
55 indicated.
Georg Brandl116aa622007-08-15 14:28:22 +000056
57
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000058.. function:: disassemble(code, lasti=-1)
59 disco(code, lasti=-1)
Georg Brandl116aa622007-08-15 14:28:22 +000060
Georg Brandl4833e5b2010-07-03 10:41:33 +000061 Disassemble a code object, indicating the last instruction if *lasti* was
Georg Brandl116aa622007-08-15 14:28:22 +000062 provided. The output is divided in the following columns:
63
64 #. the line number, for the first instruction of each line
65 #. the current instruction, indicated as ``-->``,
66 #. a labelled instruction, indicated with ``>>``,
67 #. the address of the instruction,
68 #. the operation code name,
69 #. operation parameters, and
70 #. interpretation of the parameters in parentheses.
71
72 The parameter interpretation recognizes local and global variable names,
73 constant values, branch targets, and compare operators.
74
75
Benjamin Peterson75edad02009-01-01 15:05:06 +000076.. function:: findlinestarts(code)
77
78 This generator function uses the ``co_firstlineno`` and ``co_lnotab``
79 attributes of the code object *code* to find the offsets which are starts of
80 lines in the source code. They are generated as ``(offset, lineno)`` pairs.
81
82
83.. function:: findlabels(code)
84
85 Detect all offsets in the code object *code* which are jump targets, and
86 return a list of these offsets.
Georg Brandl48310cd2009-01-03 21:18:54 +000087
88
Georg Brandl116aa622007-08-15 14:28:22 +000089.. data:: opname
90
Georg Brandl9afde1c2007-11-01 20:32:30 +000091 Sequence of operation names, indexable using the bytecode.
Georg Brandl116aa622007-08-15 14:28:22 +000092
93
94.. data:: opmap
95
Georg Brandl9afde1c2007-11-01 20:32:30 +000096 Dictionary mapping bytecodes to operation names.
Georg Brandl116aa622007-08-15 14:28:22 +000097
98
99.. data:: cmp_op
100
101 Sequence of all compare operation names.
102
103
104.. data:: hasconst
105
Georg Brandl9afde1c2007-11-01 20:32:30 +0000106 Sequence of bytecodes that have a constant parameter.
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108
109.. data:: hasfree
110
Georg Brandl9afde1c2007-11-01 20:32:30 +0000111 Sequence of bytecodes that access a free variable.
Georg Brandl116aa622007-08-15 14:28:22 +0000112
113
114.. data:: hasname
115
Georg Brandl9afde1c2007-11-01 20:32:30 +0000116 Sequence of bytecodes that access an attribute by name.
Georg Brandl116aa622007-08-15 14:28:22 +0000117
118
119.. data:: hasjrel
120
Georg Brandl9afde1c2007-11-01 20:32:30 +0000121 Sequence of bytecodes that have a relative jump target.
Georg Brandl116aa622007-08-15 14:28:22 +0000122
123
124.. data:: hasjabs
125
Georg Brandl9afde1c2007-11-01 20:32:30 +0000126 Sequence of bytecodes that have an absolute jump target.
Georg Brandl116aa622007-08-15 14:28:22 +0000127
128
129.. data:: haslocal
130
Georg Brandl9afde1c2007-11-01 20:32:30 +0000131 Sequence of bytecodes that access a local variable.
Georg Brandl116aa622007-08-15 14:28:22 +0000132
133
134.. data:: hascompare
135
Georg Brandl9afde1c2007-11-01 20:32:30 +0000136 Sequence of bytecodes of Boolean operations.
Georg Brandl116aa622007-08-15 14:28:22 +0000137
138
139.. _bytecodes:
140
Georg Brandl9afde1c2007-11-01 20:32:30 +0000141Python Bytecode Instructions
142----------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000143
Georg Brandl9afde1c2007-11-01 20:32:30 +0000144The Python compiler currently generates the following bytecode instructions.
Georg Brandl116aa622007-08-15 14:28:22 +0000145
146
Georg Brandl4833e5b2010-07-03 10:41:33 +0000147**General instructions**
148
149.. opcode:: STOP_CODE
Georg Brandl116aa622007-08-15 14:28:22 +0000150
151 Indicates end-of-code to the compiler, not used by the interpreter.
152
153
Georg Brandl4833e5b2010-07-03 10:41:33 +0000154.. opcode:: NOP
Georg Brandl116aa622007-08-15 14:28:22 +0000155
156 Do nothing code. Used as a placeholder by the bytecode optimizer.
157
158
Georg Brandl4833e5b2010-07-03 10:41:33 +0000159.. opcode:: POP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000160
161 Removes the top-of-stack (TOS) item.
162
163
Georg Brandl4833e5b2010-07-03 10:41:33 +0000164.. opcode:: ROT_TWO
Georg Brandl116aa622007-08-15 14:28:22 +0000165
166 Swaps the two top-most stack items.
167
168
Georg Brandl4833e5b2010-07-03 10:41:33 +0000169.. opcode:: ROT_THREE
Georg Brandl116aa622007-08-15 14:28:22 +0000170
171 Lifts second and third stack item one position up, moves top down to position
172 three.
173
174
Georg Brandl4833e5b2010-07-03 10:41:33 +0000175.. opcode:: ROT_FOUR
Georg Brandl116aa622007-08-15 14:28:22 +0000176
177 Lifts second, third and forth stack item one position up, moves top down to
178 position four.
179
180
Georg Brandl4833e5b2010-07-03 10:41:33 +0000181.. opcode:: DUP_TOP
Georg Brandl116aa622007-08-15 14:28:22 +0000182
183 Duplicates the reference on top of the stack.
184
Georg Brandl4833e5b2010-07-03 10:41:33 +0000185
186**Unary operations**
187
188Unary operations take the top of the stack, apply the operation, and push the
Georg Brandl116aa622007-08-15 14:28:22 +0000189result back on the stack.
190
Georg Brandl4833e5b2010-07-03 10:41:33 +0000191.. opcode:: UNARY_POSITIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000192
193 Implements ``TOS = +TOS``.
194
195
Georg Brandl4833e5b2010-07-03 10:41:33 +0000196.. opcode:: UNARY_NEGATIVE
Georg Brandl116aa622007-08-15 14:28:22 +0000197
198 Implements ``TOS = -TOS``.
199
200
Georg Brandl4833e5b2010-07-03 10:41:33 +0000201.. opcode:: UNARY_NOT
Georg Brandl116aa622007-08-15 14:28:22 +0000202
203 Implements ``TOS = not TOS``.
204
205
Georg Brandl4833e5b2010-07-03 10:41:33 +0000206.. opcode:: UNARY_INVERT
Georg Brandl116aa622007-08-15 14:28:22 +0000207
208 Implements ``TOS = ~TOS``.
209
210
Georg Brandl4833e5b2010-07-03 10:41:33 +0000211.. opcode:: GET_ITER
Georg Brandl116aa622007-08-15 14:28:22 +0000212
213 Implements ``TOS = iter(TOS)``.
214
Georg Brandl4833e5b2010-07-03 10:41:33 +0000215
216**Binary operations**
217
Georg Brandl116aa622007-08-15 14:28:22 +0000218Binary operations remove the top of the stack (TOS) and the second top-most
219stack item (TOS1) from the stack. They perform the operation, and put the
220result back on the stack.
221
Georg Brandl4833e5b2010-07-03 10:41:33 +0000222.. opcode:: BINARY_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000223
224 Implements ``TOS = TOS1 ** TOS``.
225
226
Georg Brandl4833e5b2010-07-03 10:41:33 +0000227.. opcode:: BINARY_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000228
229 Implements ``TOS = TOS1 * TOS``.
230
231
Georg Brandl4833e5b2010-07-03 10:41:33 +0000232.. opcode:: BINARY_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000233
234 Implements ``TOS = TOS1 // TOS``.
235
236
Georg Brandl4833e5b2010-07-03 10:41:33 +0000237.. opcode:: BINARY_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000238
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000239 Implements ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000240
241
Georg Brandl4833e5b2010-07-03 10:41:33 +0000242.. opcode:: BINARY_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000243
244 Implements ``TOS = TOS1 % TOS``.
245
246
Georg Brandl4833e5b2010-07-03 10:41:33 +0000247.. opcode:: BINARY_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000248
249 Implements ``TOS = TOS1 + TOS``.
250
251
Georg Brandl4833e5b2010-07-03 10:41:33 +0000252.. opcode:: BINARY_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000253
254 Implements ``TOS = TOS1 - TOS``.
255
256
Georg Brandl4833e5b2010-07-03 10:41:33 +0000257.. opcode:: BINARY_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000258
259 Implements ``TOS = TOS1[TOS]``.
260
261
Georg Brandl4833e5b2010-07-03 10:41:33 +0000262.. opcode:: BINARY_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000263
264 Implements ``TOS = TOS1 << TOS``.
265
266
Georg Brandl4833e5b2010-07-03 10:41:33 +0000267.. opcode:: BINARY_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000268
269 Implements ``TOS = TOS1 >> TOS``.
270
271
Georg Brandl4833e5b2010-07-03 10:41:33 +0000272.. opcode:: BINARY_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000273
274 Implements ``TOS = TOS1 & TOS``.
275
276
Georg Brandl4833e5b2010-07-03 10:41:33 +0000277.. opcode:: BINARY_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000278
279 Implements ``TOS = TOS1 ^ TOS``.
280
281
Georg Brandl4833e5b2010-07-03 10:41:33 +0000282.. opcode:: BINARY_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000283
284 Implements ``TOS = TOS1 | TOS``.
285
Georg Brandl4833e5b2010-07-03 10:41:33 +0000286
287**In-place operations**
288
Georg Brandl116aa622007-08-15 14:28:22 +0000289In-place operations are like binary operations, in that they remove TOS and
290TOS1, and push the result back on the stack, but the operation is done in-place
291when TOS1 supports it, and the resulting TOS may be (but does not have to be)
292the original TOS1.
293
Georg Brandl4833e5b2010-07-03 10:41:33 +0000294.. opcode:: INPLACE_POWER
Georg Brandl116aa622007-08-15 14:28:22 +0000295
296 Implements in-place ``TOS = TOS1 ** TOS``.
297
298
Georg Brandl4833e5b2010-07-03 10:41:33 +0000299.. opcode:: INPLACE_MULTIPLY
Georg Brandl116aa622007-08-15 14:28:22 +0000300
301 Implements in-place ``TOS = TOS1 * TOS``.
302
303
Georg Brandl4833e5b2010-07-03 10:41:33 +0000304.. opcode:: INPLACE_FLOOR_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000305
306 Implements in-place ``TOS = TOS1 // TOS``.
307
308
Georg Brandl4833e5b2010-07-03 10:41:33 +0000309.. opcode:: INPLACE_TRUE_DIVIDE
Georg Brandl116aa622007-08-15 14:28:22 +0000310
Ezio Melotti7de0a6e2010-01-05 08:37:27 +0000311 Implements in-place ``TOS = TOS1 / TOS``.
Georg Brandl116aa622007-08-15 14:28:22 +0000312
313
Georg Brandl4833e5b2010-07-03 10:41:33 +0000314.. opcode:: INPLACE_MODULO
Georg Brandl116aa622007-08-15 14:28:22 +0000315
316 Implements in-place ``TOS = TOS1 % TOS``.
317
318
Georg Brandl4833e5b2010-07-03 10:41:33 +0000319.. opcode:: INPLACE_ADD
Georg Brandl116aa622007-08-15 14:28:22 +0000320
321 Implements in-place ``TOS = TOS1 + TOS``.
322
323
Georg Brandl4833e5b2010-07-03 10:41:33 +0000324.. opcode:: INPLACE_SUBTRACT
Georg Brandl116aa622007-08-15 14:28:22 +0000325
326 Implements in-place ``TOS = TOS1 - TOS``.
327
328
Georg Brandl4833e5b2010-07-03 10:41:33 +0000329.. opcode:: INPLACE_LSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000330
331 Implements in-place ``TOS = TOS1 << TOS``.
332
333
Georg Brandl4833e5b2010-07-03 10:41:33 +0000334.. opcode:: INPLACE_RSHIFT
Georg Brandl116aa622007-08-15 14:28:22 +0000335
336 Implements in-place ``TOS = TOS1 >> TOS``.
337
338
Georg Brandl4833e5b2010-07-03 10:41:33 +0000339.. opcode:: INPLACE_AND
Georg Brandl116aa622007-08-15 14:28:22 +0000340
341 Implements in-place ``TOS = TOS1 & TOS``.
342
343
Georg Brandl4833e5b2010-07-03 10:41:33 +0000344.. opcode:: INPLACE_XOR
Georg Brandl116aa622007-08-15 14:28:22 +0000345
346 Implements in-place ``TOS = TOS1 ^ TOS``.
347
348
Georg Brandl4833e5b2010-07-03 10:41:33 +0000349.. opcode:: INPLACE_OR
Georg Brandl116aa622007-08-15 14:28:22 +0000350
351 Implements in-place ``TOS = TOS1 | TOS``.
352
Georg Brandl116aa622007-08-15 14:28:22 +0000353
Georg Brandl4833e5b2010-07-03 10:41:33 +0000354.. opcode:: STORE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000355
356 Implements ``TOS1[TOS] = TOS2``.
357
358
Georg Brandl4833e5b2010-07-03 10:41:33 +0000359.. opcode:: DELETE_SUBSCR
Georg Brandl116aa622007-08-15 14:28:22 +0000360
361 Implements ``del TOS1[TOS]``.
362
Georg Brandl116aa622007-08-15 14:28:22 +0000363
Georg Brandl4833e5b2010-07-03 10:41:33 +0000364**Miscellaneous opcodes**
Georg Brandl116aa622007-08-15 14:28:22 +0000365
Georg Brandl4833e5b2010-07-03 10:41:33 +0000366.. opcode:: PRINT_EXPR
Georg Brandl116aa622007-08-15 14:28:22 +0000367
368 Implements the expression statement for the interactive mode. TOS is removed
369 from the stack and printed. In non-interactive mode, an expression statement is
370 terminated with ``POP_STACK``.
371
372
Georg Brandl4833e5b2010-07-03 10:41:33 +0000373.. opcode:: BREAK_LOOP
Georg Brandl116aa622007-08-15 14:28:22 +0000374
375 Terminates a loop due to a :keyword:`break` statement.
376
377
378.. opcode:: CONTINUE_LOOP (target)
379
380 Continues a loop due to a :keyword:`continue` statement. *target* is the
381 address to jump to (which should be a ``FOR_ITER`` instruction).
382
383
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000384.. opcode:: SET_ADD (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000385
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000386 Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions.
Georg Brandl116aa622007-08-15 14:28:22 +0000387
388
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000389.. opcode:: LIST_APPEND (i)
Georg Brandl116aa622007-08-15 14:28:22 +0000390
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000391 Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions.
392
393
394.. opcode:: MAP_ADD (i)
395
396 Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict
397 comprehensions.
398
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000399For all of the SET_ADD, LIST_APPEND and MAP_ADD instructions, while the
400added value or key/value pair is popped off, the container object remains on
401the stack so that it is available for further iterations of the loop.
Georg Brandl116aa622007-08-15 14:28:22 +0000402
403
Georg Brandl4833e5b2010-07-03 10:41:33 +0000404.. opcode:: RETURN_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000405
406 Returns with TOS to the caller of the function.
407
408
Georg Brandl4833e5b2010-07-03 10:41:33 +0000409.. opcode:: YIELD_VALUE
Georg Brandl116aa622007-08-15 14:28:22 +0000410
Georg Brandl9afde1c2007-11-01 20:32:30 +0000411 Pops ``TOS`` and yields it from a :term:`generator`.
Georg Brandl116aa622007-08-15 14:28:22 +0000412
413
Georg Brandl4833e5b2010-07-03 10:41:33 +0000414.. opcode:: IMPORT_STAR
Georg Brandl116aa622007-08-15 14:28:22 +0000415
416 Loads all symbols not starting with ``'_'`` directly from the module TOS to the
417 local namespace. The module is popped after loading all names. This opcode
418 implements ``from module import *``.
419
420
Georg Brandl4833e5b2010-07-03 10:41:33 +0000421.. opcode:: POP_BLOCK
Georg Brandl116aa622007-08-15 14:28:22 +0000422
423 Removes one block from the block stack. Per frame, there is a stack of blocks,
424 denoting nested loops, try statements, and such.
425
426
Georg Brandl4833e5b2010-07-03 10:41:33 +0000427.. opcode:: POP_EXCEPT
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000428
429 Removes one block from the block stack. The popped block must be an exception
430 handler block, as implicitly created when entering an except handler.
431 In addition to popping extraneous values from the frame stack, the
432 last three popped values are used to restore the exception state.
433
434
Georg Brandl4833e5b2010-07-03 10:41:33 +0000435.. opcode:: END_FINALLY
Georg Brandl116aa622007-08-15 14:28:22 +0000436
437 Terminates a :keyword:`finally` clause. The interpreter recalls whether the
438 exception has to be re-raised, or whether the function returns, and continues
439 with the outer-next block.
440
441
Georg Brandl4833e5b2010-07-03 10:41:33 +0000442.. opcode:: LOAD_BUILD_CLASS
Georg Brandl116aa622007-08-15 14:28:22 +0000443
Georg Brandl5ac22302008-07-20 21:39:03 +0000444 Pushes :func:`builtins.__build_class__` onto the stack. It is later called
Benjamin Petersonaac8fd32008-07-20 22:02:26 +0000445 by ``CALL_FUNCTION`` to construct a class.
Georg Brandl116aa622007-08-15 14:28:22 +0000446
Guido van Rossum04110fb2007-08-24 16:32:05 +0000447
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000448.. opcode:: SETUP_WITH (delta)
449
450 This opcode performs several operations before a with block starts. First,
451 it loads :meth:`~object.__exit__` from the context manager and pushes it onto
452 the stack for later use by :opcode:`WITH_CLEANUP`. Then,
453 :meth:`~object.__enter__` is called, and a finally block pointing to *delta*
454 is pushed. Finally, the result of calling the enter method is pushed onto
455 the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
456 store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
457 :opcode:`UNPACK_SEQUENCE`).
458
459
Georg Brandl4833e5b2010-07-03 10:41:33 +0000460.. opcode:: WITH_CLEANUP
Guido van Rossum04110fb2007-08-24 16:32:05 +0000461
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000462 Cleans up the stack when a :keyword:`with` statement block exits. TOS is
463 the context manager's :meth:`__exit__` bound method. Below TOS are 1--3
464 values indicating how/why the finally clause was entered:
Guido van Rossum04110fb2007-08-24 16:32:05 +0000465
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000466 * SECOND = ``None``
467 * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval
468 * SECOND = ``WHY_*``; no retval below it
469 * (SECOND, THIRD, FOURTH) = exc_info()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000470
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000471 In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
472 ``TOS(None, None, None)``. In addition, TOS is removed from the stack.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000473
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000474 If the stack represents an exception, *and* the function call returns
475 a 'true' value, this information is "zapped" and replaced with a single
476 ``WHY_SILENCED`` to prevent ``END_FINALLY`` from re-raising the exception.
477 (But non-local gotos will still be resumed.)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000478
Georg Brandl9afde1c2007-11-01 20:32:30 +0000479 .. XXX explain the WHY stuff!
480
Guido van Rossum04110fb2007-08-24 16:32:05 +0000481
Georg Brandl5ac22302008-07-20 21:39:03 +0000482.. opcode:: STORE_LOCALS
483
484 Pops TOS from the stack and stores it as the current frame's ``f_locals``.
485 This is used in class construction.
486
487
Georg Brandl116aa622007-08-15 14:28:22 +0000488All of the following opcodes expect arguments. An argument is two bytes, with
489the more significant byte last.
490
Georg Brandl116aa622007-08-15 14:28:22 +0000491.. opcode:: STORE_NAME (namei)
492
493 Implements ``name = TOS``. *namei* is the index of *name* in the attribute
Christian Heimes8640e742008-02-23 16:23:06 +0000494 :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
Georg Brandl116aa622007-08-15 14:28:22 +0000495 or ``STORE_GLOBAL`` if possible.
496
497
498.. opcode:: DELETE_NAME (namei)
499
500 Implements ``del name``, where *namei* is the index into :attr:`co_names`
501 attribute of the code object.
502
503
504.. opcode:: UNPACK_SEQUENCE (count)
505
506 Unpacks TOS into *count* individual values, which are put onto the stack
507 right-to-left.
508
Georg Brandl116aa622007-08-15 14:28:22 +0000509
Georg Brandl5ac22302008-07-20 21:39:03 +0000510.. opcode:: UNPACK_EX (counts)
511
512 Implements assignment with a starred target: Unpacks an iterable in TOS into
513 individual values, where the total number of values can be smaller than the
514 number of items in the iterable: one the new values will be a list of all
515 leftover items.
516
517 The low byte of *counts* is the number of values before the list value, the
518 high byte of *counts* the number of values after it. The resulting values
519 are put onto the stack right-to-left.
Georg Brandl48310cd2009-01-03 21:18:54 +0000520
Georg Brandl5ac22302008-07-20 21:39:03 +0000521
Georg Brandl116aa622007-08-15 14:28:22 +0000522.. opcode:: DUP_TOPX (count)
523
524 Duplicate *count* items, keeping them in the same order. Due to implementation
525 limits, *count* should be between 1 and 5 inclusive.
526
527
528.. opcode:: STORE_ATTR (namei)
529
530 Implements ``TOS.name = TOS1``, where *namei* is the index of name in
531 :attr:`co_names`.
532
533
534.. opcode:: DELETE_ATTR (namei)
535
536 Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
537
538
539.. opcode:: STORE_GLOBAL (namei)
540
541 Works as ``STORE_NAME``, but stores the name as a global.
542
543
544.. opcode:: DELETE_GLOBAL (namei)
545
546 Works as ``DELETE_NAME``, but deletes a global name.
547
Georg Brandl116aa622007-08-15 14:28:22 +0000548
549.. opcode:: LOAD_CONST (consti)
550
551 Pushes ``co_consts[consti]`` onto the stack.
552
553
554.. opcode:: LOAD_NAME (namei)
555
556 Pushes the value associated with ``co_names[namei]`` onto the stack.
557
558
559.. opcode:: BUILD_TUPLE (count)
560
561 Creates a tuple consuming *count* items from the stack, and pushes the resulting
562 tuple onto the stack.
563
564
565.. opcode:: BUILD_LIST (count)
566
567 Works as ``BUILD_TUPLE``, but creates a list.
568
569
570.. opcode:: BUILD_SET (count)
571
572 Works as ``BUILD_TUPLE``, but creates a set.
573
574
Christian Heimesa62da1d2008-01-12 19:39:10 +0000575.. opcode:: BUILD_MAP (count)
Georg Brandl116aa622007-08-15 14:28:22 +0000576
Christian Heimesa62da1d2008-01-12 19:39:10 +0000577 Pushes a new dictionary object onto the stack. The dictionary is pre-sized
578 to hold *count* entries.
Georg Brandl116aa622007-08-15 14:28:22 +0000579
580
581.. opcode:: LOAD_ATTR (namei)
582
583 Replaces TOS with ``getattr(TOS, co_names[namei])``.
584
585
586.. opcode:: COMPARE_OP (opname)
587
588 Performs a Boolean operation. The operation name can be found in
589 ``cmp_op[opname]``.
590
591
592.. opcode:: IMPORT_NAME (namei)
593
Christian Heimesa342c012008-04-20 21:01:16 +0000594 Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
595 the *fromlist* and *level* arguments of :func:`__import__`. The module
596 object is pushed onto the stack. The current namespace is not affected:
597 for a proper import statement, a subsequent ``STORE_FAST`` instruction
598 modifies the namespace.
Georg Brandl116aa622007-08-15 14:28:22 +0000599
600
601.. opcode:: IMPORT_FROM (namei)
602
603 Loads the attribute ``co_names[namei]`` from the module found in TOS. The
604 resulting object is pushed onto the stack, to be subsequently stored by a
605 ``STORE_FAST`` instruction.
606
607
608.. opcode:: JUMP_FORWARD (delta)
609
Georg Brandl9afde1c2007-11-01 20:32:30 +0000610 Increments bytecode counter by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000611
612
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000613.. opcode:: POP_JUMP_IF_TRUE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000614
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000615 If TOS is true, sets the bytecode counter to *target*. TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000616
617
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000618.. opcode:: POP_JUMP_IF_FALSE (target)
Georg Brandl116aa622007-08-15 14:28:22 +0000619
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000620 If TOS is false, sets the bytecode counter to *target*. TOS is popped.
621
622
623.. opcode:: JUMP_IF_TRUE_OR_POP (target)
624
625 If TOS is true, sets the bytecode counter to *target* and leaves TOS
626 on the stack. Otherwise (TOS is false), TOS is popped.
627
628
629.. opcode:: JUMP_IF_FALSE_OR_POP (target)
630
631 If TOS is false, sets the bytecode counter to *target* and leaves
632 TOS on the stack. Otherwise (TOS is true), TOS is popped.
Georg Brandl116aa622007-08-15 14:28:22 +0000633
634
635.. opcode:: JUMP_ABSOLUTE (target)
636
Georg Brandl9afde1c2007-11-01 20:32:30 +0000637 Set bytecode counter to *target*.
Georg Brandl116aa622007-08-15 14:28:22 +0000638
639
640.. opcode:: FOR_ITER (delta)
641
Georg Brandl9afde1c2007-11-01 20:32:30 +0000642 ``TOS`` is an :term:`iterator`. Call its :meth:`__next__` method. If this
643 yields a new value, push it on the stack (leaving the iterator below it). If
644 the iterator indicates it is exhausted ``TOS`` is popped, and the byte code
645 counter is incremented by *delta*.
Georg Brandl116aa622007-08-15 14:28:22 +0000646
Georg Brandl116aa622007-08-15 14:28:22 +0000647
648.. opcode:: LOAD_GLOBAL (namei)
649
650 Loads the global named ``co_names[namei]`` onto the stack.
651
Georg Brandl116aa622007-08-15 14:28:22 +0000652
653.. opcode:: SETUP_LOOP (delta)
654
655 Pushes a block for a loop onto the block stack. The block spans from the
656 current instruction with a size of *delta* bytes.
657
658
659.. opcode:: SETUP_EXCEPT (delta)
660
661 Pushes a try block from a try-except clause onto the block stack. *delta* points
662 to the first except block.
663
664
665.. opcode:: SETUP_FINALLY (delta)
666
667 Pushes a try block from a try-except clause onto the block stack. *delta* points
668 to the finally block.
669
Georg Brandl4833e5b2010-07-03 10:41:33 +0000670.. opcode:: STORE_MAP
Christian Heimesa62da1d2008-01-12 19:39:10 +0000671
672 Store a key and value pair in a dictionary. Pops the key and value while leaving
673 the dictionary on the stack.
Georg Brandl116aa622007-08-15 14:28:22 +0000674
675.. opcode:: LOAD_FAST (var_num)
676
677 Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
678
679
680.. opcode:: STORE_FAST (var_num)
681
682 Stores TOS into the local ``co_varnames[var_num]``.
683
684
685.. opcode:: DELETE_FAST (var_num)
686
687 Deletes local ``co_varnames[var_num]``.
688
689
690.. opcode:: LOAD_CLOSURE (i)
691
692 Pushes a reference to the cell contained in slot *i* of the cell and free
693 variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
694 less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
695 len(co_cellvars)]``.
696
697
698.. opcode:: LOAD_DEREF (i)
699
700 Loads the cell contained in slot *i* of the cell and free variable storage.
701 Pushes a reference to the object the cell contains on the stack.
702
703
704.. opcode:: STORE_DEREF (i)
705
706 Stores TOS into the cell contained in slot *i* of the cell and free variable
707 storage.
708
709
710.. opcode:: SET_LINENO (lineno)
711
712 This opcode is obsolete.
713
714
715.. opcode:: RAISE_VARARGS (argc)
716
717 Raises an exception. *argc* indicates the number of parameters to the raise
718 statement, ranging from 0 to 3. The handler will find the traceback as TOS2,
719 the parameter as TOS1, and the exception as TOS.
720
721
722.. opcode:: CALL_FUNCTION (argc)
723
724 Calls a function. The low byte of *argc* indicates the number of positional
725 parameters, the high byte the number of keyword parameters. On the stack, the
726 opcode finds the keyword parameters first. For each keyword argument, the value
727 is on top of the key. Below the keyword parameters, the positional parameters
728 are on the stack, with the right-most parameter on top. Below the parameters,
Georg Brandl48310cd2009-01-03 21:18:54 +0000729 the function object to call is on the stack. Pops all function arguments, and
Benjamin Peterson206e3072008-10-19 14:07:49 +0000730 the function itself off the stack, and pushes the return value.
Georg Brandl116aa622007-08-15 14:28:22 +0000731
732
733.. opcode:: MAKE_FUNCTION (argc)
734
735 Pushes a new function object on the stack. TOS is the code associated with the
736 function. The function object is defined to have *argc* default parameters,
737 which are found below TOS.
738
739
740.. opcode:: MAKE_CLOSURE (argc)
741
Guido van Rossum04110fb2007-08-24 16:32:05 +0000742 Creates a new function object, sets its *__closure__* slot, and pushes it on
743 the stack. TOS is the code associated with the function, TOS1 the tuple
744 containing cells for the closure's free variables. The function also has
745 *argc* default parameters, which are found below the cells.
Georg Brandl116aa622007-08-15 14:28:22 +0000746
747
748.. opcode:: BUILD_SLICE (argc)
749
750 .. index:: builtin: slice
751
752 Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2,
753 ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000754 pushed. See the :func:`slice` built-in function for more information.
Georg Brandl116aa622007-08-15 14:28:22 +0000755
756
757.. opcode:: EXTENDED_ARG (ext)
758
759 Prefixes any opcode which has an argument too big to fit into the default two
760 bytes. *ext* holds two additional bytes which, taken together with the
761 subsequent opcode's argument, comprise a four-byte argument, *ext* being the two
762 most-significant bytes.
763
764
765.. opcode:: CALL_FUNCTION_VAR (argc)
766
767 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
768 on the stack contains the variable argument list, followed by keyword and
769 positional arguments.
770
771
772.. opcode:: CALL_FUNCTION_KW (argc)
773
774 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
775 on the stack contains the keyword arguments dictionary, followed by explicit
776 keyword and positional arguments.
777
778
779.. opcode:: CALL_FUNCTION_VAR_KW (argc)
780
781 Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
782 element on the stack contains the keyword arguments dictionary, followed by the
783 variable-arguments tuple, followed by explicit keyword and positional arguments.
784
785
Georg Brandl4833e5b2010-07-03 10:41:33 +0000786.. opcode:: HAVE_ARGUMENT
Georg Brandl116aa622007-08-15 14:28:22 +0000787
788 This is not really an opcode. It identifies the dividing line between opcodes
789 which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
790 HAVE_ARGUMENT``.
791