Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 1 | :mod:`dis` --- Disassembler for Python bytecode |
| 2 | =============================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 | |
| 4 | .. module:: dis |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 5 | :synopsis: Disassembler for Python bytecode. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 6 | |
| 7 | |
Brett Cannon | 8315fd1 | 2010-07-02 22:03:00 +0000 | [diff] [blame] | 8 | The :mod:`dis` module supports the analysis of CPython :term:`bytecode` by |
| 9 | disassembling it. The CPython bytecode which this module takes as an |
Georg Brandl | 71515ca | 2009-05-17 12:29:12 +0000 | [diff] [blame] | 10 | input is defined in the file :file:`Include/opcode.h` and used by the compiler |
| 11 | and the interpreter. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | |
Éric Araujo | 6e6cb8e | 2010-11-16 19:13:50 +0000 | [diff] [blame] | 13 | .. seealso:: |
| 14 | |
| 15 | Latest version of the :source:`dis module Python source code <Lib/dis.py>` |
| 16 | |
Georg Brandl | 19b7a87 | 2010-07-03 10:21:50 +0000 | [diff] [blame] | 17 | .. impl-detail:: |
| 18 | |
| 19 | Bytecode is an implementation detail of the CPython interpreter! No |
| 20 | guarantees are made that bytecode will not be added, removed, or changed |
| 21 | between versions of Python. Use of this module should not be considered to |
| 22 | work across Python VMs or Python releases. |
| 23 | |
Brett Cannon | 8315fd1 | 2010-07-02 22:03:00 +0000 | [diff] [blame] | 24 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 25 | Example: Given the function :func:`myfunc`:: |
| 26 | |
| 27 | def myfunc(alist): |
| 28 | return len(alist) |
| 29 | |
| 30 | the following command can be used to get the disassembly of :func:`myfunc`:: |
| 31 | |
| 32 | >>> dis.dis(myfunc) |
| 33 | 2 0 LOAD_GLOBAL 0 (len) |
| 34 | 3 LOAD_FAST 0 (alist) |
| 35 | 6 CALL_FUNCTION 1 |
| 36 | 9 RETURN_VALUE |
| 37 | |
| 38 | (The "2" is a line number). |
| 39 | |
| 40 | The :mod:`dis` module defines the following functions and constants: |
| 41 | |
| 42 | |
Nick Coghlan | e8814fb | 2010-09-10 14:08:04 +0000 | [diff] [blame] | 43 | .. function:: code_info(x) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 44 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 45 | Return a formatted multi-line string with detailed code object information |
| 46 | for the supplied function, method, source code string or code object. |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 47 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 48 | Note that the exact contents of code info strings are highly implementation |
| 49 | dependent and they may change arbitrarily across Python VMs or Python |
| 50 | releases. |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 51 | |
| 52 | .. versionadded:: 3.2 |
| 53 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 54 | |
Nick Coghlan | e8814fb | 2010-09-10 14:08:04 +0000 | [diff] [blame] | 55 | .. function:: show_code(x) |
| 56 | |
| 57 | Print detailed code object information for the supplied function, method, |
| 58 | source code string or code object to stdout. |
| 59 | |
| 60 | This is a convenient shorthand for ``print(code_info(x))``, intended for |
| 61 | interactive exploration at the interpreter prompt. |
| 62 | |
| 63 | .. versionadded:: 3.2 |
| 64 | |
Georg Brandl | c2a4f4f | 2009-04-10 09:03:43 +0000 | [diff] [blame] | 65 | .. function:: dis(x=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 67 | Disassemble the *x* object. *x* can denote either a module, a class, a |
| 68 | method, a function, a code object, a string of source code or a byte sequence |
| 69 | of raw bytecode. For a module, it disassembles all functions. For a class, |
| 70 | it disassembles all methods. For a code object or sequence of raw bytecode, |
| 71 | it prints one line per bytecode instruction. Strings are first compiled to |
| 72 | code objects with the :func:`compile` built-in function before being |
| 73 | disassembled. If no object is provided, this function disassembles the last |
| 74 | traceback. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 75 | |
| 76 | |
Georg Brandl | c2a4f4f | 2009-04-10 09:03:43 +0000 | [diff] [blame] | 77 | .. function:: distb(tb=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 78 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 79 | Disassemble the top-of-stack function of a traceback, using the last |
| 80 | traceback if none was passed. The instruction causing the exception is |
| 81 | indicated. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 82 | |
| 83 | |
Georg Brandl | c2a4f4f | 2009-04-10 09:03:43 +0000 | [diff] [blame] | 84 | .. function:: disassemble(code, lasti=-1) |
| 85 | disco(code, lasti=-1) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 86 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 87 | Disassemble a code object, indicating the last instruction if *lasti* was |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 88 | provided. The output is divided in the following columns: |
| 89 | |
| 90 | #. the line number, for the first instruction of each line |
| 91 | #. the current instruction, indicated as ``-->``, |
| 92 | #. a labelled instruction, indicated with ``>>``, |
| 93 | #. the address of the instruction, |
| 94 | #. the operation code name, |
| 95 | #. operation parameters, and |
| 96 | #. interpretation of the parameters in parentheses. |
| 97 | |
| 98 | The parameter interpretation recognizes local and global variable names, |
| 99 | constant values, branch targets, and compare operators. |
| 100 | |
| 101 | |
Benjamin Peterson | 75edad0 | 2009-01-01 15:05:06 +0000 | [diff] [blame] | 102 | .. function:: findlinestarts(code) |
| 103 | |
| 104 | This generator function uses the ``co_firstlineno`` and ``co_lnotab`` |
| 105 | attributes of the code object *code* to find the offsets which are starts of |
| 106 | lines in the source code. They are generated as ``(offset, lineno)`` pairs. |
| 107 | |
| 108 | |
| 109 | .. function:: findlabels(code) |
| 110 | |
| 111 | Detect all offsets in the code object *code* which are jump targets, and |
| 112 | return a list of these offsets. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 113 | |
| 114 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 115 | .. data:: opname |
| 116 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 117 | Sequence of operation names, indexable using the bytecode. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 118 | |
| 119 | |
| 120 | .. data:: opmap |
| 121 | |
Georg Brandl | 2379877 | 2010-10-17 11:29:07 +0000 | [diff] [blame] | 122 | Dictionary mapping operation names to bytecodes. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 123 | |
| 124 | |
| 125 | .. data:: cmp_op |
| 126 | |
| 127 | Sequence of all compare operation names. |
| 128 | |
| 129 | |
| 130 | .. data:: hasconst |
| 131 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 132 | Sequence of bytecodes that have a constant parameter. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 133 | |
| 134 | |
| 135 | .. data:: hasfree |
| 136 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 137 | Sequence of bytecodes that access a free variable. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 138 | |
| 139 | |
| 140 | .. data:: hasname |
| 141 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 142 | Sequence of bytecodes that access an attribute by name. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 143 | |
| 144 | |
| 145 | .. data:: hasjrel |
| 146 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 147 | Sequence of bytecodes that have a relative jump target. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 148 | |
| 149 | |
| 150 | .. data:: hasjabs |
| 151 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 152 | Sequence of bytecodes that have an absolute jump target. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 153 | |
| 154 | |
| 155 | .. data:: haslocal |
| 156 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 157 | Sequence of bytecodes that access a local variable. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 158 | |
| 159 | |
| 160 | .. data:: hascompare |
| 161 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 162 | Sequence of bytecodes of Boolean operations. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 163 | |
| 164 | |
| 165 | .. _bytecodes: |
| 166 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 167 | Python Bytecode Instructions |
| 168 | ---------------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 169 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 170 | The Python compiler currently generates the following bytecode instructions. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 171 | |
| 172 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 173 | **General instructions** |
| 174 | |
| 175 | .. opcode:: STOP_CODE |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 176 | |
| 177 | Indicates end-of-code to the compiler, not used by the interpreter. |
| 178 | |
| 179 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 180 | .. opcode:: NOP |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 181 | |
| 182 | Do nothing code. Used as a placeholder by the bytecode optimizer. |
| 183 | |
| 184 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 185 | .. opcode:: POP_TOP |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 186 | |
| 187 | Removes the top-of-stack (TOS) item. |
| 188 | |
| 189 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 190 | .. opcode:: ROT_TWO |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 191 | |
| 192 | Swaps the two top-most stack items. |
| 193 | |
| 194 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 195 | .. opcode:: ROT_THREE |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 196 | |
| 197 | Lifts second and third stack item one position up, moves top down to position |
| 198 | three. |
| 199 | |
| 200 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 201 | .. opcode:: DUP_TOP |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 202 | |
| 203 | Duplicates the reference on top of the stack. |
| 204 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 205 | |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 206 | .. opcode:: DUP_TOP_TWO |
| 207 | |
| 208 | Duplicates the two references on top of the stack, leaving them in the |
| 209 | same order. |
| 210 | |
| 211 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 212 | **Unary operations** |
| 213 | |
| 214 | Unary operations take the top of the stack, apply the operation, and push the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 215 | result back on the stack. |
| 216 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 217 | .. opcode:: UNARY_POSITIVE |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 218 | |
| 219 | Implements ``TOS = +TOS``. |
| 220 | |
| 221 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 222 | .. opcode:: UNARY_NEGATIVE |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 223 | |
| 224 | Implements ``TOS = -TOS``. |
| 225 | |
| 226 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 227 | .. opcode:: UNARY_NOT |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 228 | |
| 229 | Implements ``TOS = not TOS``. |
| 230 | |
| 231 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 232 | .. opcode:: UNARY_INVERT |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 233 | |
| 234 | Implements ``TOS = ~TOS``. |
| 235 | |
| 236 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 237 | .. opcode:: GET_ITER |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 238 | |
| 239 | Implements ``TOS = iter(TOS)``. |
| 240 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 241 | |
| 242 | **Binary operations** |
| 243 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 244 | Binary operations remove the top of the stack (TOS) and the second top-most |
| 245 | stack item (TOS1) from the stack. They perform the operation, and put the |
| 246 | result back on the stack. |
| 247 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 248 | .. opcode:: BINARY_POWER |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 249 | |
| 250 | Implements ``TOS = TOS1 ** TOS``. |
| 251 | |
| 252 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 253 | .. opcode:: BINARY_MULTIPLY |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 254 | |
| 255 | Implements ``TOS = TOS1 * TOS``. |
| 256 | |
| 257 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 258 | .. opcode:: BINARY_FLOOR_DIVIDE |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 259 | |
| 260 | Implements ``TOS = TOS1 // TOS``. |
| 261 | |
| 262 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 263 | .. opcode:: BINARY_TRUE_DIVIDE |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 264 | |
Ezio Melotti | 7de0a6e | 2010-01-05 08:37:27 +0000 | [diff] [blame] | 265 | Implements ``TOS = TOS1 / TOS``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 266 | |
| 267 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 268 | .. opcode:: BINARY_MODULO |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 269 | |
| 270 | Implements ``TOS = TOS1 % TOS``. |
| 271 | |
| 272 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 273 | .. opcode:: BINARY_ADD |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 274 | |
| 275 | Implements ``TOS = TOS1 + TOS``. |
| 276 | |
| 277 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 278 | .. opcode:: BINARY_SUBTRACT |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 279 | |
| 280 | Implements ``TOS = TOS1 - TOS``. |
| 281 | |
| 282 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 283 | .. opcode:: BINARY_SUBSCR |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 284 | |
| 285 | Implements ``TOS = TOS1[TOS]``. |
| 286 | |
| 287 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 288 | .. opcode:: BINARY_LSHIFT |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 289 | |
| 290 | Implements ``TOS = TOS1 << TOS``. |
| 291 | |
| 292 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 293 | .. opcode:: BINARY_RSHIFT |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 294 | |
| 295 | Implements ``TOS = TOS1 >> TOS``. |
| 296 | |
| 297 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 298 | .. opcode:: BINARY_AND |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 299 | |
| 300 | Implements ``TOS = TOS1 & TOS``. |
| 301 | |
| 302 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 303 | .. opcode:: BINARY_XOR |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 304 | |
| 305 | Implements ``TOS = TOS1 ^ TOS``. |
| 306 | |
| 307 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 308 | .. opcode:: BINARY_OR |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 309 | |
| 310 | Implements ``TOS = TOS1 | TOS``. |
| 311 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 312 | |
| 313 | **In-place operations** |
| 314 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 315 | In-place operations are like binary operations, in that they remove TOS and |
| 316 | TOS1, and push the result back on the stack, but the operation is done in-place |
| 317 | when TOS1 supports it, and the resulting TOS may be (but does not have to be) |
| 318 | the original TOS1. |
| 319 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 320 | .. opcode:: INPLACE_POWER |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 321 | |
| 322 | Implements in-place ``TOS = TOS1 ** TOS``. |
| 323 | |
| 324 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 325 | .. opcode:: INPLACE_MULTIPLY |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 326 | |
| 327 | Implements in-place ``TOS = TOS1 * TOS``. |
| 328 | |
| 329 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 330 | .. opcode:: INPLACE_FLOOR_DIVIDE |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 331 | |
| 332 | Implements in-place ``TOS = TOS1 // TOS``. |
| 333 | |
| 334 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 335 | .. opcode:: INPLACE_TRUE_DIVIDE |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 336 | |
Ezio Melotti | 7de0a6e | 2010-01-05 08:37:27 +0000 | [diff] [blame] | 337 | Implements in-place ``TOS = TOS1 / TOS``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 338 | |
| 339 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 340 | .. opcode:: INPLACE_MODULO |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 341 | |
| 342 | Implements in-place ``TOS = TOS1 % TOS``. |
| 343 | |
| 344 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 345 | .. opcode:: INPLACE_ADD |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 346 | |
| 347 | Implements in-place ``TOS = TOS1 + TOS``. |
| 348 | |
| 349 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 350 | .. opcode:: INPLACE_SUBTRACT |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 351 | |
| 352 | Implements in-place ``TOS = TOS1 - TOS``. |
| 353 | |
| 354 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 355 | .. opcode:: INPLACE_LSHIFT |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 356 | |
| 357 | Implements in-place ``TOS = TOS1 << TOS``. |
| 358 | |
| 359 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 360 | .. opcode:: INPLACE_RSHIFT |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 361 | |
| 362 | Implements in-place ``TOS = TOS1 >> TOS``. |
| 363 | |
| 364 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 365 | .. opcode:: INPLACE_AND |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 366 | |
| 367 | Implements in-place ``TOS = TOS1 & TOS``. |
| 368 | |
| 369 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 370 | .. opcode:: INPLACE_XOR |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 371 | |
| 372 | Implements in-place ``TOS = TOS1 ^ TOS``. |
| 373 | |
| 374 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 375 | .. opcode:: INPLACE_OR |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 376 | |
| 377 | Implements in-place ``TOS = TOS1 | TOS``. |
| 378 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 379 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 380 | .. opcode:: STORE_SUBSCR |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 381 | |
| 382 | Implements ``TOS1[TOS] = TOS2``. |
| 383 | |
| 384 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 385 | .. opcode:: DELETE_SUBSCR |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 386 | |
| 387 | Implements ``del TOS1[TOS]``. |
| 388 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 389 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 390 | **Miscellaneous opcodes** |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 391 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 392 | .. opcode:: PRINT_EXPR |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 393 | |
| 394 | Implements the expression statement for the interactive mode. TOS is removed |
| 395 | from the stack and printed. In non-interactive mode, an expression statement is |
| 396 | terminated with ``POP_STACK``. |
| 397 | |
| 398 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 399 | .. opcode:: BREAK_LOOP |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 400 | |
| 401 | Terminates a loop due to a :keyword:`break` statement. |
| 402 | |
| 403 | |
| 404 | .. opcode:: CONTINUE_LOOP (target) |
| 405 | |
| 406 | Continues a loop due to a :keyword:`continue` statement. *target* is the |
| 407 | address to jump to (which should be a ``FOR_ITER`` instruction). |
| 408 | |
| 409 | |
Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 410 | .. opcode:: SET_ADD (i) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 411 | |
Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 412 | Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 413 | |
| 414 | |
Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 415 | .. opcode:: LIST_APPEND (i) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 416 | |
Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 417 | Calls ``list.append(TOS[-i], TOS)``. Used to implement list comprehensions. |
| 418 | |
| 419 | |
| 420 | .. opcode:: MAP_ADD (i) |
| 421 | |
| 422 | Calls ``dict.setitem(TOS1[-i], TOS, TOS1)``. Used to implement dict |
| 423 | comprehensions. |
| 424 | |
Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 425 | For all of the SET_ADD, LIST_APPEND and MAP_ADD instructions, while the |
| 426 | added value or key/value pair is popped off, the container object remains on |
| 427 | the stack so that it is available for further iterations of the loop. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 428 | |
| 429 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 430 | .. opcode:: RETURN_VALUE |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 431 | |
| 432 | Returns with TOS to the caller of the function. |
| 433 | |
| 434 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 435 | .. opcode:: YIELD_VALUE |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 436 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 437 | Pops ``TOS`` and yields it from a :term:`generator`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 438 | |
| 439 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 440 | .. opcode:: IMPORT_STAR |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 441 | |
| 442 | Loads all symbols not starting with ``'_'`` directly from the module TOS to the |
| 443 | local namespace. The module is popped after loading all names. This opcode |
| 444 | implements ``from module import *``. |
| 445 | |
| 446 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 447 | .. opcode:: POP_BLOCK |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 448 | |
| 449 | Removes one block from the block stack. Per frame, there is a stack of blocks, |
| 450 | denoting nested loops, try statements, and such. |
| 451 | |
| 452 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 453 | .. opcode:: POP_EXCEPT |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 454 | |
| 455 | Removes one block from the block stack. The popped block must be an exception |
| 456 | handler block, as implicitly created when entering an except handler. |
| 457 | In addition to popping extraneous values from the frame stack, the |
| 458 | last three popped values are used to restore the exception state. |
| 459 | |
| 460 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 461 | .. opcode:: END_FINALLY |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 462 | |
| 463 | Terminates a :keyword:`finally` clause. The interpreter recalls whether the |
| 464 | exception has to be re-raised, or whether the function returns, and continues |
| 465 | with the outer-next block. |
| 466 | |
| 467 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 468 | .. opcode:: LOAD_BUILD_CLASS |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 469 | |
Georg Brandl | 5ac2230 | 2008-07-20 21:39:03 +0000 | [diff] [blame] | 470 | Pushes :func:`builtins.__build_class__` onto the stack. It is later called |
Benjamin Peterson | aac8fd3 | 2008-07-20 22:02:26 +0000 | [diff] [blame] | 471 | by ``CALL_FUNCTION`` to construct a class. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 472 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 473 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 474 | .. opcode:: SETUP_WITH (delta) |
| 475 | |
| 476 | This opcode performs several operations before a with block starts. First, |
| 477 | it loads :meth:`~object.__exit__` from the context manager and pushes it onto |
| 478 | the stack for later use by :opcode:`WITH_CLEANUP`. Then, |
| 479 | :meth:`~object.__enter__` is called, and a finally block pointing to *delta* |
| 480 | is pushed. Finally, the result of calling the enter method is pushed onto |
| 481 | the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or |
| 482 | store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or |
| 483 | :opcode:`UNPACK_SEQUENCE`). |
| 484 | |
| 485 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 486 | .. opcode:: WITH_CLEANUP |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 487 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 488 | Cleans up the stack when a :keyword:`with` statement block exits. TOS is |
| 489 | the context manager's :meth:`__exit__` bound method. Below TOS are 1--3 |
| 490 | values indicating how/why the finally clause was entered: |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 491 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 492 | * SECOND = ``None`` |
| 493 | * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval |
| 494 | * SECOND = ``WHY_*``; no retval below it |
| 495 | * (SECOND, THIRD, FOURTH) = exc_info() |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 496 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 497 | In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise |
| 498 | ``TOS(None, None, None)``. In addition, TOS is removed from the stack. |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 499 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 500 | If the stack represents an exception, *and* the function call returns |
| 501 | a 'true' value, this information is "zapped" and replaced with a single |
| 502 | ``WHY_SILENCED`` to prevent ``END_FINALLY`` from re-raising the exception. |
| 503 | (But non-local gotos will still be resumed.) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 504 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 505 | .. XXX explain the WHY stuff! |
| 506 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 507 | |
Georg Brandl | 5ac2230 | 2008-07-20 21:39:03 +0000 | [diff] [blame] | 508 | .. opcode:: STORE_LOCALS |
| 509 | |
| 510 | Pops TOS from the stack and stores it as the current frame's ``f_locals``. |
| 511 | This is used in class construction. |
| 512 | |
| 513 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 514 | All of the following opcodes expect arguments. An argument is two bytes, with |
| 515 | the more significant byte last. |
| 516 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 517 | .. opcode:: STORE_NAME (namei) |
| 518 | |
| 519 | Implements ``name = TOS``. *namei* is the index of *name* in the attribute |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 520 | :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST`` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 521 | or ``STORE_GLOBAL`` if possible. |
| 522 | |
| 523 | |
| 524 | .. opcode:: DELETE_NAME (namei) |
| 525 | |
| 526 | Implements ``del name``, where *namei* is the index into :attr:`co_names` |
| 527 | attribute of the code object. |
| 528 | |
| 529 | |
| 530 | .. opcode:: UNPACK_SEQUENCE (count) |
| 531 | |
| 532 | Unpacks TOS into *count* individual values, which are put onto the stack |
| 533 | right-to-left. |
| 534 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 535 | |
Georg Brandl | 5ac2230 | 2008-07-20 21:39:03 +0000 | [diff] [blame] | 536 | .. opcode:: UNPACK_EX (counts) |
| 537 | |
| 538 | Implements assignment with a starred target: Unpacks an iterable in TOS into |
| 539 | individual values, where the total number of values can be smaller than the |
| 540 | number of items in the iterable: one the new values will be a list of all |
| 541 | leftover items. |
| 542 | |
| 543 | The low byte of *counts* is the number of values before the list value, the |
| 544 | high byte of *counts* the number of values after it. The resulting values |
| 545 | are put onto the stack right-to-left. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 546 | |
Georg Brandl | 5ac2230 | 2008-07-20 21:39:03 +0000 | [diff] [blame] | 547 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 548 | .. opcode:: STORE_ATTR (namei) |
| 549 | |
| 550 | Implements ``TOS.name = TOS1``, where *namei* is the index of name in |
| 551 | :attr:`co_names`. |
| 552 | |
| 553 | |
| 554 | .. opcode:: DELETE_ATTR (namei) |
| 555 | |
| 556 | Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`. |
| 557 | |
| 558 | |
| 559 | .. opcode:: STORE_GLOBAL (namei) |
| 560 | |
| 561 | Works as ``STORE_NAME``, but stores the name as a global. |
| 562 | |
| 563 | |
| 564 | .. opcode:: DELETE_GLOBAL (namei) |
| 565 | |
| 566 | Works as ``DELETE_NAME``, but deletes a global name. |
| 567 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 568 | |
| 569 | .. opcode:: LOAD_CONST (consti) |
| 570 | |
| 571 | Pushes ``co_consts[consti]`` onto the stack. |
| 572 | |
| 573 | |
| 574 | .. opcode:: LOAD_NAME (namei) |
| 575 | |
| 576 | Pushes the value associated with ``co_names[namei]`` onto the stack. |
| 577 | |
| 578 | |
| 579 | .. opcode:: BUILD_TUPLE (count) |
| 580 | |
| 581 | Creates a tuple consuming *count* items from the stack, and pushes the resulting |
| 582 | tuple onto the stack. |
| 583 | |
| 584 | |
| 585 | .. opcode:: BUILD_LIST (count) |
| 586 | |
| 587 | Works as ``BUILD_TUPLE``, but creates a list. |
| 588 | |
| 589 | |
| 590 | .. opcode:: BUILD_SET (count) |
| 591 | |
| 592 | Works as ``BUILD_TUPLE``, but creates a set. |
| 593 | |
| 594 | |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 595 | .. opcode:: BUILD_MAP (count) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 596 | |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 597 | Pushes a new dictionary object onto the stack. The dictionary is pre-sized |
| 598 | to hold *count* entries. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 599 | |
| 600 | |
| 601 | .. opcode:: LOAD_ATTR (namei) |
| 602 | |
| 603 | Replaces TOS with ``getattr(TOS, co_names[namei])``. |
| 604 | |
| 605 | |
| 606 | .. opcode:: COMPARE_OP (opname) |
| 607 | |
| 608 | Performs a Boolean operation. The operation name can be found in |
| 609 | ``cmp_op[opname]``. |
| 610 | |
| 611 | |
| 612 | .. opcode:: IMPORT_NAME (namei) |
| 613 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 614 | Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide |
| 615 | the *fromlist* and *level* arguments of :func:`__import__`. The module |
| 616 | object is pushed onto the stack. The current namespace is not affected: |
| 617 | for a proper import statement, a subsequent ``STORE_FAST`` instruction |
| 618 | modifies the namespace. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 619 | |
| 620 | |
| 621 | .. opcode:: IMPORT_FROM (namei) |
| 622 | |
| 623 | Loads the attribute ``co_names[namei]`` from the module found in TOS. The |
| 624 | resulting object is pushed onto the stack, to be subsequently stored by a |
| 625 | ``STORE_FAST`` instruction. |
| 626 | |
| 627 | |
| 628 | .. opcode:: JUMP_FORWARD (delta) |
| 629 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 630 | Increments bytecode counter by *delta*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 631 | |
| 632 | |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 633 | .. opcode:: POP_JUMP_IF_TRUE (target) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 634 | |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 635 | If TOS is true, sets the bytecode counter to *target*. TOS is popped. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 636 | |
| 637 | |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 638 | .. opcode:: POP_JUMP_IF_FALSE (target) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 639 | |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 640 | If TOS is false, sets the bytecode counter to *target*. TOS is popped. |
| 641 | |
| 642 | |
| 643 | .. opcode:: JUMP_IF_TRUE_OR_POP (target) |
| 644 | |
| 645 | If TOS is true, sets the bytecode counter to *target* and leaves TOS |
| 646 | on the stack. Otherwise (TOS is false), TOS is popped. |
| 647 | |
| 648 | |
| 649 | .. opcode:: JUMP_IF_FALSE_OR_POP (target) |
| 650 | |
| 651 | If TOS is false, sets the bytecode counter to *target* and leaves |
| 652 | TOS on the stack. Otherwise (TOS is true), TOS is popped. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 653 | |
| 654 | |
| 655 | .. opcode:: JUMP_ABSOLUTE (target) |
| 656 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 657 | Set bytecode counter to *target*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 658 | |
| 659 | |
| 660 | .. opcode:: FOR_ITER (delta) |
| 661 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 662 | ``TOS`` is an :term:`iterator`. Call its :meth:`__next__` method. If this |
| 663 | yields a new value, push it on the stack (leaving the iterator below it). If |
| 664 | the iterator indicates it is exhausted ``TOS`` is popped, and the byte code |
| 665 | counter is incremented by *delta*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 666 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 667 | |
| 668 | .. opcode:: LOAD_GLOBAL (namei) |
| 669 | |
| 670 | Loads the global named ``co_names[namei]`` onto the stack. |
| 671 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 672 | |
| 673 | .. opcode:: SETUP_LOOP (delta) |
| 674 | |
| 675 | Pushes a block for a loop onto the block stack. The block spans from the |
| 676 | current instruction with a size of *delta* bytes. |
| 677 | |
| 678 | |
| 679 | .. opcode:: SETUP_EXCEPT (delta) |
| 680 | |
| 681 | Pushes a try block from a try-except clause onto the block stack. *delta* points |
| 682 | to the first except block. |
| 683 | |
| 684 | |
| 685 | .. opcode:: SETUP_FINALLY (delta) |
| 686 | |
| 687 | Pushes a try block from a try-except clause onto the block stack. *delta* points |
| 688 | to the finally block. |
| 689 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 690 | .. opcode:: STORE_MAP |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 691 | |
| 692 | Store a key and value pair in a dictionary. Pops the key and value while leaving |
| 693 | the dictionary on the stack. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 694 | |
| 695 | .. opcode:: LOAD_FAST (var_num) |
| 696 | |
| 697 | Pushes a reference to the local ``co_varnames[var_num]`` onto the stack. |
| 698 | |
| 699 | |
| 700 | .. opcode:: STORE_FAST (var_num) |
| 701 | |
| 702 | Stores TOS into the local ``co_varnames[var_num]``. |
| 703 | |
| 704 | |
| 705 | .. opcode:: DELETE_FAST (var_num) |
| 706 | |
| 707 | Deletes local ``co_varnames[var_num]``. |
| 708 | |
| 709 | |
| 710 | .. opcode:: LOAD_CLOSURE (i) |
| 711 | |
| 712 | Pushes a reference to the cell contained in slot *i* of the cell and free |
| 713 | variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is |
| 714 | less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i - |
| 715 | len(co_cellvars)]``. |
| 716 | |
| 717 | |
| 718 | .. opcode:: LOAD_DEREF (i) |
| 719 | |
| 720 | Loads the cell contained in slot *i* of the cell and free variable storage. |
| 721 | Pushes a reference to the object the cell contains on the stack. |
| 722 | |
| 723 | |
| 724 | .. opcode:: STORE_DEREF (i) |
| 725 | |
| 726 | Stores TOS into the cell contained in slot *i* of the cell and free variable |
| 727 | storage. |
| 728 | |
| 729 | |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 730 | .. opcode:: DELETE_DEREF (i) |
| 731 | |
| 732 | Empties the cell contained in slot *i* of the cell and free variable storage. |
| 733 | Used by the :keyword:`del` statement. |
| 734 | |
| 735 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 736 | .. opcode:: RAISE_VARARGS (argc) |
| 737 | |
| 738 | Raises an exception. *argc* indicates the number of parameters to the raise |
| 739 | statement, ranging from 0 to 3. The handler will find the traceback as TOS2, |
| 740 | the parameter as TOS1, and the exception as TOS. |
| 741 | |
| 742 | |
| 743 | .. opcode:: CALL_FUNCTION (argc) |
| 744 | |
| 745 | Calls a function. The low byte of *argc* indicates the number of positional |
| 746 | parameters, the high byte the number of keyword parameters. On the stack, the |
| 747 | opcode finds the keyword parameters first. For each keyword argument, the value |
| 748 | is on top of the key. Below the keyword parameters, the positional parameters |
| 749 | are on the stack, with the right-most parameter on top. Below the parameters, |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 750 | the function object to call is on the stack. Pops all function arguments, and |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 751 | the function itself off the stack, and pushes the return value. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 752 | |
| 753 | |
| 754 | .. opcode:: MAKE_FUNCTION (argc) |
| 755 | |
| 756 | Pushes a new function object on the stack. TOS is the code associated with the |
| 757 | function. The function object is defined to have *argc* default parameters, |
| 758 | which are found below TOS. |
| 759 | |
| 760 | |
| 761 | .. opcode:: MAKE_CLOSURE (argc) |
| 762 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 763 | Creates a new function object, sets its *__closure__* slot, and pushes it on |
| 764 | the stack. TOS is the code associated with the function, TOS1 the tuple |
| 765 | containing cells for the closure's free variables. The function also has |
| 766 | *argc* default parameters, which are found below the cells. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 767 | |
| 768 | |
| 769 | .. opcode:: BUILD_SLICE (argc) |
| 770 | |
| 771 | .. index:: builtin: slice |
| 772 | |
| 773 | Pushes a slice object on the stack. *argc* must be 2 or 3. If it is 2, |
| 774 | ``slice(TOS1, TOS)`` is pushed; if it is 3, ``slice(TOS2, TOS1, TOS)`` is |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 775 | pushed. See the :func:`slice` built-in function for more information. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 776 | |
| 777 | |
| 778 | .. opcode:: EXTENDED_ARG (ext) |
| 779 | |
| 780 | Prefixes any opcode which has an argument too big to fit into the default two |
| 781 | bytes. *ext* holds two additional bytes which, taken together with the |
| 782 | subsequent opcode's argument, comprise a four-byte argument, *ext* being the two |
| 783 | most-significant bytes. |
| 784 | |
| 785 | |
| 786 | .. opcode:: CALL_FUNCTION_VAR (argc) |
| 787 | |
| 788 | Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element |
| 789 | on the stack contains the variable argument list, followed by keyword and |
| 790 | positional arguments. |
| 791 | |
| 792 | |
| 793 | .. opcode:: CALL_FUNCTION_KW (argc) |
| 794 | |
| 795 | Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element |
| 796 | on the stack contains the keyword arguments dictionary, followed by explicit |
| 797 | keyword and positional arguments. |
| 798 | |
| 799 | |
| 800 | .. opcode:: CALL_FUNCTION_VAR_KW (argc) |
| 801 | |
| 802 | Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top |
| 803 | element on the stack contains the keyword arguments dictionary, followed by the |
| 804 | variable-arguments tuple, followed by explicit keyword and positional arguments. |
| 805 | |
| 806 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 807 | .. opcode:: HAVE_ARGUMENT |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 808 | |
| 809 | This is not really an opcode. It identifies the dividing line between opcodes |
| 810 | which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>= |
| 811 | HAVE_ARGUMENT``. |
| 812 | |