code.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #ifndef Py_CPYTHON_CODE_H
  2. # error "this header file must not be included directly"
  3. #endif
  4. typedef uint16_t _Py_CODEUNIT;
  5. #ifdef WORDS_BIGENDIAN
  6. # define _Py_OPCODE(word) ((word) >> 8)
  7. # define _Py_OPARG(word) ((word) & 255)
  8. #else
  9. # define _Py_OPCODE(word) ((word) & 255)
  10. # define _Py_OPARG(word) ((word) >> 8)
  11. #endif
  12. typedef struct _PyOpcache _PyOpcache;
  13. /* Bytecode object */
  14. struct PyCodeObject {
  15. PyObject_HEAD
  16. int co_argcount; /* #arguments, except *args */
  17. int co_posonlyargcount; /* #positional only arguments */
  18. int co_kwonlyargcount; /* #keyword only arguments */
  19. int co_nlocals; /* #local variables */
  20. int co_stacksize; /* #entries needed for evaluation stack */
  21. int co_flags; /* CO_..., see below */
  22. int co_firstlineno; /* first source line number */
  23. PyObject *co_code; /* instruction opcodes */
  24. PyObject *co_consts; /* list (constants used) */
  25. PyObject *co_names; /* list of strings (names used) */
  26. PyObject *co_varnames; /* tuple of strings (local variable names) */
  27. PyObject *co_freevars; /* tuple of strings (free variable names) */
  28. PyObject *co_cellvars; /* tuple of strings (cell variable names) */
  29. /* The rest aren't used in either hash or comparisons, except for co_name,
  30. used in both. This is done to preserve the name and line number
  31. for tracebacks and debuggers; otherwise, constant de-duplication
  32. would collapse identical functions/lambdas defined on different lines.
  33. */
  34. Py_ssize_t *co_cell2arg; /* Maps cell vars which are arguments. */
  35. PyObject *co_filename; /* unicode (where it was loaded from) */
  36. PyObject *co_name; /* unicode (name, for reference) */
  37. PyObject *co_lnotab; /* string (encoding addr<->lineno mapping) See
  38. Objects/lnotab_notes.txt for details. */
  39. void *co_zombieframe; /* for optimization only (see frameobject.c) */
  40. PyObject *co_weakreflist; /* to support weakrefs to code objects */
  41. /* Scratch space for extra data relating to the code object.
  42. Type is a void* to keep the format private in codeobject.c to force
  43. people to go through the proper APIs. */
  44. void *co_extra;
  45. /* Per opcodes just-in-time cache
  46. *
  47. * To reduce cache size, we use indirect mapping from opcode index to
  48. * cache object:
  49. * cache = co_opcache[co_opcache_map[next_instr - first_instr] - 1]
  50. */
  51. // co_opcache_map is indexed by (next_instr - first_instr).
  52. // * 0 means there is no cache for this opcode.
  53. // * n > 0 means there is cache in co_opcache[n-1].
  54. unsigned char *co_opcache_map;
  55. _PyOpcache *co_opcache;
  56. int co_opcache_flag; // used to determine when create a cache.
  57. unsigned char co_opcache_size; // length of co_opcache.
  58. };
  59. /* Masks for co_flags above */
  60. #define CO_OPTIMIZED 0x0001
  61. #define CO_NEWLOCALS 0x0002
  62. #define CO_VARARGS 0x0004
  63. #define CO_VARKEYWORDS 0x0008
  64. #define CO_NESTED 0x0010
  65. #define CO_GENERATOR 0x0020
  66. /* The CO_NOFREE flag is set if there are no free or cell variables.
  67. This information is redundant, but it allows a single flag test
  68. to determine whether there is any extra work to be done when the
  69. call frame it setup.
  70. */
  71. #define CO_NOFREE 0x0040
  72. /* The CO_COROUTINE flag is set for coroutine functions (defined with
  73. ``async def`` keywords) */
  74. #define CO_COROUTINE 0x0080
  75. #define CO_ITERABLE_COROUTINE 0x0100
  76. #define CO_ASYNC_GENERATOR 0x0200
  77. /* bpo-39562: These constant values are changed in Python 3.9
  78. to prevent collision with compiler flags. CO_FUTURE_ and PyCF_
  79. constants must be kept unique. PyCF_ constants can use bits from
  80. 0x0100 to 0x10000. CO_FUTURE_ constants use bits starting at 0x20000. */
  81. #define CO_FUTURE_DIVISION 0x20000
  82. #define CO_FUTURE_ABSOLUTE_IMPORT 0x40000 /* do absolute imports by default */
  83. #define CO_FUTURE_WITH_STATEMENT 0x80000
  84. #define CO_FUTURE_PRINT_FUNCTION 0x100000
  85. #define CO_FUTURE_UNICODE_LITERALS 0x200000
  86. #define CO_FUTURE_BARRY_AS_BDFL 0x400000
  87. #define CO_FUTURE_GENERATOR_STOP 0x800000
  88. #define CO_FUTURE_ANNOTATIONS 0x1000000
  89. /* This value is found in the co_cell2arg array when the associated cell
  90. variable does not correspond to an argument. */
  91. #define CO_CELL_NOT_AN_ARG (-1)
  92. /* This should be defined if a future statement modifies the syntax.
  93. For example, when a keyword is added.
  94. */
  95. #define PY_PARSER_REQUIRES_FUTURE_KEYWORD
  96. #define CO_MAXBLOCKS 20 /* Max static block nesting within a function */
  97. PyAPI_DATA(PyTypeObject) PyCode_Type;
  98. #define PyCode_Check(op) Py_IS_TYPE(op, &PyCode_Type)
  99. #define PyCode_GetNumFree(op) (PyTuple_GET_SIZE((op)->co_freevars))
  100. /* Public interface */
  101. PyAPI_FUNC(PyCodeObject *) PyCode_New(
  102. int, int, int, int, int, PyObject *, PyObject *,
  103. PyObject *, PyObject *, PyObject *, PyObject *,
  104. PyObject *, PyObject *, int, PyObject *);
  105. PyAPI_FUNC(PyCodeObject *) PyCode_NewWithPosOnlyArgs(
  106. int, int, int, int, int, int, PyObject *, PyObject *,
  107. PyObject *, PyObject *, PyObject *, PyObject *,
  108. PyObject *, PyObject *, int, PyObject *);
  109. /* same as struct above */
  110. /* Creates a new empty code object with the specified source location. */
  111. PyAPI_FUNC(PyCodeObject *)
  112. PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno);
  113. /* Return the line number associated with the specified bytecode index
  114. in this code object. If you just need the line number of a frame,
  115. use PyFrame_GetLineNumber() instead. */
  116. PyAPI_FUNC(int) PyCode_Addr2Line(PyCodeObject *, int);
  117. /* for internal use only */
  118. typedef struct _addr_pair {
  119. int ap_lower;
  120. int ap_upper;
  121. } PyAddrPair;
  122. /* Update *bounds to describe the first and one-past-the-last instructions in the
  123. same line as lasti. Return the number of that line.
  124. */
  125. PyAPI_FUNC(int) _PyCode_CheckLineNumber(PyCodeObject* co,
  126. int lasti, PyAddrPair *bounds);
  127. /* Create a comparable key used to compare constants taking in account the
  128. * object type. It is used to make sure types are not coerced (e.g., float and
  129. * complex) _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms
  130. *
  131. * Return (type(obj), obj, ...): a tuple with variable size (at least 2 items)
  132. * depending on the type and the value. The type is the first item to not
  133. * compare bytes and str which can raise a BytesWarning exception. */
  134. PyAPI_FUNC(PyObject*) _PyCode_ConstantKey(PyObject *obj);
  135. PyAPI_FUNC(PyObject*) PyCode_Optimize(PyObject *code, PyObject* consts,
  136. PyObject *names, PyObject *lnotab);
  137. PyAPI_FUNC(int) _PyCode_GetExtra(PyObject *code, Py_ssize_t index,
  138. void **extra);
  139. PyAPI_FUNC(int) _PyCode_SetExtra(PyObject *code, Py_ssize_t index,
  140. void *extra);