code.h 6.8 KB

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