code.h 5.1 KB

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