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