code.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /* Definitions for bytecode */
  2. #ifndef Py_LIMITED_API
  3. #ifndef Py_CODE_H
  4. #define Py_CODE_H
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. /* Count of all local monitoring events */
  9. #define _PY_MONITORING_LOCAL_EVENTS 10
  10. /* Count of all "real" monitoring events (not derived from other events) */
  11. #define _PY_MONITORING_UNGROUPED_EVENTS 15
  12. /* Count of all monitoring events */
  13. #define _PY_MONITORING_EVENTS 17
  14. /* Tables of which tools are active for each monitored event. */
  15. /* For 3.12 ABI compatibility this is over sized */
  16. typedef struct _Py_LocalMonitors {
  17. /* Only _PY_MONITORING_LOCAL_EVENTS of these are used */
  18. uint8_t tools[_PY_MONITORING_UNGROUPED_EVENTS];
  19. } _Py_LocalMonitors;
  20. typedef struct _Py_GlobalMonitors {
  21. uint8_t tools[_PY_MONITORING_UNGROUPED_EVENTS];
  22. } _Py_GlobalMonitors;
  23. /* Each instruction in a code object is a fixed-width value,
  24. * currently 2 bytes: 1-byte opcode + 1-byte oparg. The EXTENDED_ARG
  25. * opcode allows for larger values but the current limit is 3 uses
  26. * of EXTENDED_ARG (see Python/compile.c), for a maximum
  27. * 32-bit value. This aligns with the note in Python/compile.c
  28. * (compiler_addop_i_line) indicating that the max oparg value is
  29. * 2**32 - 1, rather than INT_MAX.
  30. */
  31. typedef union {
  32. uint16_t cache;
  33. struct {
  34. uint8_t code;
  35. uint8_t arg;
  36. } op;
  37. } _Py_CODEUNIT;
  38. /* These macros only remain defined for compatibility. */
  39. #define _Py_OPCODE(word) ((word).op.code)
  40. #define _Py_OPARG(word) ((word).op.arg)
  41. static inline _Py_CODEUNIT
  42. _py_make_codeunit(uint8_t opcode, uint8_t oparg)
  43. {
  44. // No designated initialisers because of C++ compat
  45. _Py_CODEUNIT word;
  46. word.op.code = opcode;
  47. word.op.arg = oparg;
  48. return word;
  49. }
  50. static inline void
  51. _py_set_opcode(_Py_CODEUNIT *word, uint8_t opcode)
  52. {
  53. word->op.code = opcode;
  54. }
  55. #define _Py_MAKE_CODEUNIT(opcode, oparg) _py_make_codeunit((opcode), (oparg))
  56. #define _Py_SET_OPCODE(word, opcode) _py_set_opcode(&(word), (opcode))
  57. typedef struct {
  58. PyObject *_co_code;
  59. PyObject *_co_varnames;
  60. PyObject *_co_cellvars;
  61. PyObject *_co_freevars;
  62. } _PyCoCached;
  63. /* Ancillary data structure used for instrumentation.
  64. Line instrumentation creates an array of
  65. these. One entry per code unit.*/
  66. typedef struct {
  67. uint8_t original_opcode;
  68. int8_t line_delta;
  69. } _PyCoLineInstrumentationData;
  70. /* Main data structure used for instrumentation.
  71. * This is allocated when needed for instrumentation
  72. */
  73. typedef struct {
  74. /* Monitoring specific to this code object */
  75. _Py_LocalMonitors local_monitors;
  76. /* Monitoring that is active on this code object */
  77. _Py_LocalMonitors active_monitors;
  78. /* The tools that are to be notified for events for the matching code unit */
  79. uint8_t *tools;
  80. /* Information to support line events */
  81. _PyCoLineInstrumentationData *lines;
  82. /* The tools that are to be notified for line events for the matching code unit */
  83. uint8_t *line_tools;
  84. /* Information to support instruction events */
  85. /* The underlying instructions, which can themselves be instrumented */
  86. uint8_t *per_instruction_opcodes;
  87. /* The tools that are to be notified for instruction events for the matching code unit */
  88. uint8_t *per_instruction_tools;
  89. } _PyCoMonitoringData;
  90. // To avoid repeating ourselves in deepfreeze.py, all PyCodeObject members are
  91. // defined in this macro:
  92. #define _PyCode_DEF(SIZE) { \
  93. PyObject_VAR_HEAD \
  94. \
  95. /* Note only the following fields are used in hash and/or comparisons \
  96. * \
  97. * - co_name \
  98. * - co_argcount \
  99. * - co_posonlyargcount \
  100. * - co_kwonlyargcount \
  101. * - co_nlocals \
  102. * - co_stacksize \
  103. * - co_flags \
  104. * - co_firstlineno \
  105. * - co_consts \
  106. * - co_names \
  107. * - co_localsplusnames \
  108. * This is done to preserve the name and line number for tracebacks \
  109. * and debuggers; otherwise, constant de-duplication would collapse \
  110. * identical functions/lambdas defined on different lines. \
  111. */ \
  112. \
  113. /* These fields are set with provided values on new code objects. */ \
  114. \
  115. /* The hottest fields (in the eval loop) are grouped here at the top. */ \
  116. PyObject *co_consts; /* list (constants used) */ \
  117. PyObject *co_names; /* list of strings (names used) */ \
  118. PyObject *co_exceptiontable; /* Byte string encoding exception handling \
  119. table */ \
  120. int co_flags; /* CO_..., see below */ \
  121. \
  122. /* The rest are not so impactful on performance. */ \
  123. int co_argcount; /* #arguments, except *args */ \
  124. int co_posonlyargcount; /* #positional only arguments */ \
  125. int co_kwonlyargcount; /* #keyword only arguments */ \
  126. int co_stacksize; /* #entries needed for evaluation stack */ \
  127. int co_firstlineno; /* first source line number */ \
  128. \
  129. /* redundant values (derived from co_localsplusnames and \
  130. co_localspluskinds) */ \
  131. int co_nlocalsplus; /* number of local + cell + free variables */ \
  132. int co_framesize; /* Size of frame in words */ \
  133. int co_nlocals; /* number of local variables */ \
  134. int co_ncellvars; /* total number of cell variables */ \
  135. int co_nfreevars; /* number of free variables */ \
  136. uint32_t co_version; /* version number */ \
  137. \
  138. PyObject *co_localsplusnames; /* tuple mapping offsets to names */ \
  139. PyObject *co_localspluskinds; /* Bytes mapping to local kinds (one byte \
  140. per variable) */ \
  141. PyObject *co_filename; /* unicode (where it was loaded from) */ \
  142. PyObject *co_name; /* unicode (name, for reference) */ \
  143. PyObject *co_qualname; /* unicode (qualname, for reference) */ \
  144. PyObject *co_linetable; /* bytes object that holds location info */ \
  145. PyObject *co_weakreflist; /* to support weakrefs to code objects */ \
  146. _PyCoCached *_co_cached; /* cached co_* attributes */ \
  147. uint64_t _co_instrumentation_version; /* current instrumentation version */ \
  148. _PyCoMonitoringData *_co_monitoring; /* Monitoring data */ \
  149. int _co_firsttraceable; /* index of first traceable instruction */ \
  150. /* Scratch space for extra data relating to the code object. \
  151. Type is a void* to keep the format private in codeobject.c to force \
  152. people to go through the proper APIs. */ \
  153. void *co_extra; \
  154. char co_code_adaptive[(SIZE)]; \
  155. }
  156. /* Bytecode object */
  157. struct PyCodeObject _PyCode_DEF(1);
  158. /* Masks for co_flags above */
  159. #define CO_OPTIMIZED 0x0001
  160. #define CO_NEWLOCALS 0x0002
  161. #define CO_VARARGS 0x0004
  162. #define CO_VARKEYWORDS 0x0008
  163. #define CO_NESTED 0x0010
  164. #define CO_GENERATOR 0x0020
  165. /* The CO_COROUTINE flag is set for coroutine functions (defined with
  166. ``async def`` keywords) */
  167. #define CO_COROUTINE 0x0080
  168. #define CO_ITERABLE_COROUTINE 0x0100
  169. #define CO_ASYNC_GENERATOR 0x0200
  170. /* bpo-39562: These constant values are changed in Python 3.9
  171. to prevent collision with compiler flags. CO_FUTURE_ and PyCF_
  172. constants must be kept unique. PyCF_ constants can use bits from
  173. 0x0100 to 0x10000. CO_FUTURE_ constants use bits starting at 0x20000. */
  174. #define CO_FUTURE_DIVISION 0x20000
  175. #define CO_FUTURE_ABSOLUTE_IMPORT 0x40000 /* do absolute imports by default */
  176. #define CO_FUTURE_WITH_STATEMENT 0x80000
  177. #define CO_FUTURE_PRINT_FUNCTION 0x100000
  178. #define CO_FUTURE_UNICODE_LITERALS 0x200000
  179. #define CO_FUTURE_BARRY_AS_BDFL 0x400000
  180. #define CO_FUTURE_GENERATOR_STOP 0x800000
  181. #define CO_FUTURE_ANNOTATIONS 0x1000000
  182. /* This should be defined if a future statement modifies the syntax.
  183. For example, when a keyword is added.
  184. */
  185. #define PY_PARSER_REQUIRES_FUTURE_KEYWORD
  186. #define CO_MAXBLOCKS 20 /* Max static block nesting within a function */
  187. PyAPI_DATA(PyTypeObject) PyCode_Type;
  188. #define PyCode_Check(op) Py_IS_TYPE((op), &PyCode_Type)
  189. static inline Py_ssize_t PyCode_GetNumFree(PyCodeObject *op) {
  190. assert(PyCode_Check(op));
  191. return op->co_nfreevars;
  192. }
  193. static inline int PyCode_GetFirstFree(PyCodeObject *op) {
  194. assert(PyCode_Check(op));
  195. return op->co_nlocalsplus - op->co_nfreevars;
  196. }
  197. #define _PyCode_CODE(CO) _Py_RVALUE((_Py_CODEUNIT *)(CO)->co_code_adaptive)
  198. #define _PyCode_NBYTES(CO) (Py_SIZE(CO) * (Py_ssize_t)sizeof(_Py_CODEUNIT))
  199. /* Unstable public interface */
  200. PyAPI_FUNC(PyCodeObject *) PyUnstable_Code_New(
  201. int, int, int, int, int, PyObject *, PyObject *,
  202. PyObject *, PyObject *, PyObject *, PyObject *,
  203. PyObject *, PyObject *, PyObject *, int, PyObject *,
  204. PyObject *);
  205. PyAPI_FUNC(PyCodeObject *) PyUnstable_Code_NewWithPosOnlyArgs(
  206. int, int, int, int, int, int, PyObject *, PyObject *,
  207. PyObject *, PyObject *, PyObject *, PyObject *,
  208. PyObject *, PyObject *, PyObject *, int, PyObject *,
  209. PyObject *);
  210. /* same as struct above */
  211. // Old names -- remove when this API changes:
  212. _Py_DEPRECATED_EXTERNALLY(3.12) static inline PyCodeObject *
  213. PyCode_New(
  214. int a, int b, int c, int d, int e, PyObject *f, PyObject *g,
  215. PyObject *h, PyObject *i, PyObject *j, PyObject *k,
  216. PyObject *l, PyObject *m, PyObject *n, int o, PyObject *p,
  217. PyObject *q)
  218. {
  219. return PyUnstable_Code_New(
  220. a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q);
  221. }
  222. _Py_DEPRECATED_EXTERNALLY(3.12) static inline PyCodeObject *
  223. PyCode_NewWithPosOnlyArgs(
  224. int a, int poac, int b, int c, int d, int e, PyObject *f, PyObject *g,
  225. PyObject *h, PyObject *i, PyObject *j, PyObject *k,
  226. PyObject *l, PyObject *m, PyObject *n, int o, PyObject *p,
  227. PyObject *q)
  228. {
  229. return PyUnstable_Code_NewWithPosOnlyArgs(
  230. a, poac, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q);
  231. }
  232. /* Creates a new empty code object with the specified source location. */
  233. PyAPI_FUNC(PyCodeObject *)
  234. PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno);
  235. /* Return the line number associated with the specified bytecode index
  236. in this code object. If you just need the line number of a frame,
  237. use PyFrame_GetLineNumber() instead. */
  238. PyAPI_FUNC(int) PyCode_Addr2Line(PyCodeObject *, int);
  239. PyAPI_FUNC(int) PyCode_Addr2Location(PyCodeObject *, int, int *, int *, int *, int *);
  240. #define PY_FOREACH_CODE_EVENT(V) \
  241. V(CREATE) \
  242. V(DESTROY)
  243. typedef enum {
  244. #define PY_DEF_EVENT(op) PY_CODE_EVENT_##op,
  245. PY_FOREACH_CODE_EVENT(PY_DEF_EVENT)
  246. #undef PY_DEF_EVENT
  247. } PyCodeEvent;
  248. /*
  249. * A callback that is invoked for different events in a code object's lifecycle.
  250. *
  251. * The callback is invoked with a borrowed reference to co, after it is
  252. * created and before it is destroyed.
  253. *
  254. * If the callback sets an exception, it must return -1. Otherwise
  255. * it should return 0.
  256. */
  257. typedef int (*PyCode_WatchCallback)(
  258. PyCodeEvent event,
  259. PyCodeObject* co);
  260. /*
  261. * Register a per-interpreter callback that will be invoked for code object
  262. * lifecycle events.
  263. *
  264. * Returns a handle that may be passed to PyCode_ClearWatcher on success,
  265. * or -1 and sets an error if no more handles are available.
  266. */
  267. PyAPI_FUNC(int) PyCode_AddWatcher(PyCode_WatchCallback callback);
  268. /*
  269. * Clear the watcher associated with the watcher_id handle.
  270. *
  271. * Returns 0 on success or -1 if no watcher exists for the provided id.
  272. */
  273. PyAPI_FUNC(int) PyCode_ClearWatcher(int watcher_id);
  274. /* for internal use only */
  275. struct _opaque {
  276. int computed_line;
  277. const uint8_t *lo_next;
  278. const uint8_t *limit;
  279. };
  280. typedef struct _line_offsets {
  281. int ar_start;
  282. int ar_end;
  283. int ar_line;
  284. struct _opaque opaque;
  285. } PyCodeAddressRange;
  286. /* Update *bounds to describe the first and one-past-the-last instructions in the
  287. same line as lasti. Return the number of that line.
  288. */
  289. PyAPI_FUNC(int) _PyCode_CheckLineNumber(int lasti, PyCodeAddressRange *bounds);
  290. /* Create a comparable key used to compare constants taking in account the
  291. * object type. It is used to make sure types are not coerced (e.g., float and
  292. * complex) _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms
  293. *
  294. * Return (type(obj), obj, ...): a tuple with variable size (at least 2 items)
  295. * depending on the type and the value. The type is the first item to not
  296. * compare bytes and str which can raise a BytesWarning exception. */
  297. PyAPI_FUNC(PyObject*) _PyCode_ConstantKey(PyObject *obj);
  298. PyAPI_FUNC(PyObject*) PyCode_Optimize(PyObject *code, PyObject* consts,
  299. PyObject *names, PyObject *lnotab);
  300. PyAPI_FUNC(int) PyUnstable_Code_GetExtra(
  301. PyObject *code, Py_ssize_t index, void **extra);
  302. PyAPI_FUNC(int) PyUnstable_Code_SetExtra(
  303. PyObject *code, Py_ssize_t index, void *extra);
  304. // Old names -- remove when this API changes:
  305. _Py_DEPRECATED_EXTERNALLY(3.12) static inline int
  306. _PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
  307. {
  308. return PyUnstable_Code_GetExtra(code, index, extra);
  309. }
  310. _Py_DEPRECATED_EXTERNALLY(3.12) static inline int
  311. _PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
  312. {
  313. return PyUnstable_Code_SetExtra(code, index, extra);
  314. }
  315. /* Equivalent to getattr(code, 'co_code') in Python.
  316. Returns a strong reference to a bytes object. */
  317. PyAPI_FUNC(PyObject *) PyCode_GetCode(PyCodeObject *code);
  318. /* Equivalent to getattr(code, 'co_varnames') in Python. */
  319. PyAPI_FUNC(PyObject *) PyCode_GetVarnames(PyCodeObject *code);
  320. /* Equivalent to getattr(code, 'co_cellvars') in Python. */
  321. PyAPI_FUNC(PyObject *) PyCode_GetCellvars(PyCodeObject *code);
  322. /* Equivalent to getattr(code, 'co_freevars') in Python. */
  323. PyAPI_FUNC(PyObject *) PyCode_GetFreevars(PyCodeObject *code);
  324. typedef enum _PyCodeLocationInfoKind {
  325. /* short forms are 0 to 9 */
  326. PY_CODE_LOCATION_INFO_SHORT0 = 0,
  327. /* one lineforms are 10 to 12 */
  328. PY_CODE_LOCATION_INFO_ONE_LINE0 = 10,
  329. PY_CODE_LOCATION_INFO_ONE_LINE1 = 11,
  330. PY_CODE_LOCATION_INFO_ONE_LINE2 = 12,
  331. PY_CODE_LOCATION_INFO_NO_COLUMNS = 13,
  332. PY_CODE_LOCATION_INFO_LONG = 14,
  333. PY_CODE_LOCATION_INFO_NONE = 15
  334. } _PyCodeLocationInfoKind;
  335. #ifdef __cplusplus
  336. }
  337. #endif
  338. #endif // !Py_CODE_H
  339. #endif // !Py_LIMITED_API