code.h 5.9 KB

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