1 #ifndef Py_CPYTHON_CODE_H 2 # error "this header file must not be included directly" 3 #endif 4 5 /* Each instruction in a code object is a fixed-width value, 6 * currently 2 bytes: 1-byte opcode + 1-byte oparg. The EXTENDED_ARG 7 * opcode allows for larger values but the current limit is 3 uses 8 * of EXTENDED_ARG (see Python/wordcode_helpers.h), for a maximum 9 * 32-bit value. This aligns with the note in Python/compile.c 10 * (compiler_addop_i_line) indicating that the max oparg value is 11 * 2**32 - 1, rather than INT_MAX. 12 */ 13 14 typedef uint16_t _Py_CODEUNIT; 15 16 #ifdef WORDS_BIGENDIAN 17 # define _Py_OPCODE(word) ((word) >> 8) 18 # define _Py_OPARG(word) ((word) & 255) 19 # define _Py_MAKECODEUNIT(opcode, oparg) (((opcode)<<8)|(oparg)) 20 #else 21 # define _Py_OPCODE(word) ((word) & 255) 22 # define _Py_OPARG(word) ((word) >> 8) 23 # define _Py_MAKECODEUNIT(opcode, oparg) ((opcode)|((oparg)<<8)) 24 #endif 25 26 27 /* Bytecode object */ 28 struct PyCodeObject { 29 PyObject_HEAD 30 31 /* Note only the following fields are used in hash and/or comparisons 32 * 33 * - co_name 34 * - co_argcount 35 * - co_posonlyargcount 36 * - co_kwonlyargcount 37 * - co_nlocals 38 * - co_stacksize 39 * - co_flags 40 * - co_firstlineno 41 * - co_code 42 * - co_consts 43 * - co_names 44 * - co_varnames 45 * - co_freevars 46 * - co_cellvars 47 * 48 * This is done to preserve the name and line number for tracebacks 49 * and debuggers; otherwise, constant de-duplication would collapse 50 * identical functions/lambdas defined on different lines. 51 */ 52 53 /* These fields are set with provided values on new code objects. */ 54 55 // The hottest fields (in the eval loop) are grouped here at the top. 56 PyObject *co_consts; /* list (constants used) */ 57 PyObject *co_names; /* list of strings (names used) */ 58 _Py_CODEUNIT *co_firstinstr; /* Pointer to first instruction, used for quickening. 59 Unlike the other "hot" fields, this one is 60 actually derived from co_code. */ 61 PyObject *co_exceptiontable; /* Byte string encoding exception handling table */ 62 int co_flags; /* CO_..., see below */ 63 int co_warmup; /* Warmup counter for quickening */ 64 65 // The rest are not so impactful on performance. 66 int co_argcount; /* #arguments, except *args */ 67 int co_posonlyargcount; /* #positional only arguments */ 68 int co_kwonlyargcount; /* #keyword only arguments */ 69 int co_stacksize; /* #entries needed for evaluation stack */ 70 int co_firstlineno; /* first source line number */ 71 PyObject *co_code; /* instruction opcodes */ 72 PyObject *co_localsplusnames; /* tuple mapping offsets to names */ 73 PyObject *co_localspluskinds; /* Bytes mapping to local kinds (one byte per variable) */ 74 PyObject *co_filename; /* unicode (where it was loaded from) */ 75 PyObject *co_name; /* unicode (name, for reference) */ 76 PyObject *co_qualname; /* unicode (qualname, for reference) */ 77 PyObject *co_linetable; /* bytes (encoding addr<->lineno mapping) See 78 Objects/lnotab_notes.txt for details. */ 79 PyObject *co_endlinetable; /* bytes object that holds end lineno for 80 instructions separated across different 81 lines */ 82 PyObject *co_columntable; /* bytes object that holds start/end column 83 offset each instruction */ 84 85 /* These fields are set with computed values on new code objects. */ 86 87 // redundant values (derived from co_localsplusnames and co_localspluskinds) 88 int co_nlocalsplus; /* number of local + cell + free variables */ 89 int co_nlocals; /* number of local variables */ 90 int co_nplaincellvars; /* number of non-arg cell variables */ 91 int co_ncellvars; /* total number of cell variables */ 92 int co_nfreevars; /* number of free variables */ 93 // lazily-computed values 94 PyObject *co_varnames; /* tuple of strings (local variable names) */ 95 PyObject *co_cellvars; /* tuple of strings (cell variable names) */ 96 PyObject *co_freevars; /* tuple of strings (free variable names) */ 97 98 /* The remaining fields are zeroed out on new code objects. */ 99 100 PyObject *co_weakreflist; /* to support weakrefs to code objects */ 101 /* Scratch space for extra data relating to the code object. 102 Type is a void* to keep the format private in codeobject.c to force 103 people to go through the proper APIs. */ 104 void *co_extra; 105 /* Quickened instructions and cache, or NULL 106 This should be treated as opaque by all code except the specializer and 107 interpreter. */ 108 union _cache_or_instruction *co_quickened; 109 110 }; 111 112 /* Masks for co_flags above */ 113 #define CO_OPTIMIZED 0x0001 114 #define CO_NEWLOCALS 0x0002 115 #define CO_VARARGS 0x0004 116 #define CO_VARKEYWORDS 0x0008 117 #define CO_NESTED 0x0010 118 #define CO_GENERATOR 0x0020 119 120 /* The CO_COROUTINE flag is set for coroutine functions (defined with 121 ``async def`` keywords) */ 122 #define CO_COROUTINE 0x0080 123 #define CO_ITERABLE_COROUTINE 0x0100 124 #define CO_ASYNC_GENERATOR 0x0200 125 126 /* bpo-39562: These constant values are changed in Python 3.9 127 to prevent collision with compiler flags. CO_FUTURE_ and PyCF_ 128 constants must be kept unique. PyCF_ constants can use bits from 129 0x0100 to 0x10000. CO_FUTURE_ constants use bits starting at 0x20000. */ 130 #define CO_FUTURE_DIVISION 0x20000 131 #define CO_FUTURE_ABSOLUTE_IMPORT 0x40000 /* do absolute imports by default */ 132 #define CO_FUTURE_WITH_STATEMENT 0x80000 133 #define CO_FUTURE_PRINT_FUNCTION 0x100000 134 #define CO_FUTURE_UNICODE_LITERALS 0x200000 135 136 #define CO_FUTURE_BARRY_AS_BDFL 0x400000 137 #define CO_FUTURE_GENERATOR_STOP 0x800000 138 #define CO_FUTURE_ANNOTATIONS 0x1000000 139 140 /* This should be defined if a future statement modifies the syntax. 141 For example, when a keyword is added. 142 */ 143 #define PY_PARSER_REQUIRES_FUTURE_KEYWORD 144 145 #define CO_MAXBLOCKS 20 /* Max static block nesting within a function */ 146 147 PyAPI_DATA(PyTypeObject) PyCode_Type; 148 149 #define PyCode_Check(op) Py_IS_TYPE(op, &PyCode_Type) 150 #define PyCode_GetNumFree(op) ((op)->co_nfreevars) 151 152 /* Public interface */ 153 PyAPI_FUNC(PyCodeObject *) PyCode_New( 154 int, int, int, int, int, PyObject *, PyObject *, 155 PyObject *, PyObject *, PyObject *, PyObject *, 156 PyObject *, PyObject *, PyObject *, int, PyObject *, 157 PyObject *, PyObject *, PyObject *); 158 159 PyAPI_FUNC(PyCodeObject *) PyCode_NewWithPosOnlyArgs( 160 int, int, int, int, int, int, PyObject *, PyObject *, 161 PyObject *, PyObject *, PyObject *, PyObject *, 162 PyObject *, PyObject *, PyObject *, int, PyObject *, 163 PyObject *, PyObject *, PyObject *); 164 /* same as struct above */ 165 166 /* Creates a new empty code object with the specified source location. */ 167 PyAPI_FUNC(PyCodeObject *) 168 PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno); 169 170 /* Return the line number associated with the specified bytecode index 171 in this code object. If you just need the line number of a frame, 172 use PyFrame_GetLineNumber() instead. */ 173 PyAPI_FUNC(int) PyCode_Addr2Line(PyCodeObject *, int); 174 175 PyAPI_FUNC(int) PyCode_Addr2Location(PyCodeObject *, int, int *, int *, int *, int *); 176 177 /* Return the ending source code line number from a bytecode index. */ 178 PyAPI_FUNC(int) _PyCode_Addr2EndLine(PyCodeObject *, int); 179 /* Return the starting source code column offset from a bytecode index. */ 180 PyAPI_FUNC(int) _PyCode_Addr2Offset(PyCodeObject *, int); 181 /* Return the ending source code column offset from a bytecode index. */ 182 PyAPI_FUNC(int) _PyCode_Addr2EndOffset(PyCodeObject *, int); 183 184 /* for internal use only */ 185 struct _opaque { 186 int computed_line; 187 const char *lo_next; 188 const char *limit; 189 }; 190 191 typedef struct _line_offsets { 192 int ar_start; 193 int ar_end; 194 int ar_line; 195 struct _opaque opaque; 196 } PyCodeAddressRange; 197 198 /* Update *bounds to describe the first and one-past-the-last instructions in the 199 same line as lasti. Return the number of that line. 200 */ 201 PyAPI_FUNC(int) _PyCode_CheckLineNumber(int lasti, PyCodeAddressRange *bounds); 202 203 /* Create a comparable key used to compare constants taking in account the 204 * object type. It is used to make sure types are not coerced (e.g., float and 205 * complex) _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms 206 * 207 * Return (type(obj), obj, ...): a tuple with variable size (at least 2 items) 208 * depending on the type and the value. The type is the first item to not 209 * compare bytes and str which can raise a BytesWarning exception. */ 210 PyAPI_FUNC(PyObject*) _PyCode_ConstantKey(PyObject *obj); 211 212 PyAPI_FUNC(PyObject*) PyCode_Optimize(PyObject *code, PyObject* consts, 213 PyObject *names, PyObject *lnotab); 214 215 216 PyAPI_FUNC(int) _PyCode_GetExtra(PyObject *code, Py_ssize_t index, 217 void **extra); 218 PyAPI_FUNC(int) _PyCode_SetExtra(PyObject *code, Py_ssize_t index, 219 void *extra); 220 221 /** API for initializing the line number tables. */ 222 int _PyCode_InitAddressRange(PyCodeObject* co, PyCodeAddressRange *bounds); 223 int _PyCode_InitEndAddressRange(PyCodeObject* co, PyCodeAddressRange* bounds); 224 225 /** Out of process API for initializing the line number table. */ 226 void PyLineTable_InitAddressRange(const char *linetable, Py_ssize_t length, int firstlineno, PyCodeAddressRange *range); 227 228 /** API for traversing the line number table. */ 229 int PyLineTable_NextAddressRange(PyCodeAddressRange *range); 230 int PyLineTable_PreviousAddressRange(PyCodeAddressRange *range); 231 232