code.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Definitions for bytecode */
  2. #ifndef Py_CODE_H
  3. #define Py_CODE_H
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. /* Bytecode object */
  8. typedef struct {
  9. PyObject_HEAD
  10. int co_argcount; /* #arguments, except *args */
  11. int co_nlocals; /* #local variables */
  12. int co_stacksize; /* #entries needed for evaluation stack */
  13. int co_flags; /* CO_..., see below */
  14. PyObject *co_code; /* instruction opcodes */
  15. PyObject *co_consts; /* list (constants used) */
  16. PyObject *co_names; /* list of strings (names used) */
  17. PyObject *co_varnames; /* tuple of strings (local variable names) */
  18. PyObject *co_freevars; /* tuple of strings (free variable names) */
  19. PyObject *co_cellvars; /* tuple of strings (cell variable names) */
  20. /* The rest doesn't count for hash/cmp */
  21. PyObject *co_filename; /* string (where it was loaded from) */
  22. PyObject *co_name; /* string (name, for reference) */
  23. int co_firstlineno; /* first source line number */
  24. PyObject *co_lnotab; /* string (encoding addr<->lineno mapping) See
  25. Objects/lnotab_notes.txt for details. */
  26. void *co_zombieframe; /* for optimization only (see frameobject.c) */
  27. PyObject *co_weakreflist; /* to support weakrefs to code objects */
  28. } PyCodeObject;
  29. /* Masks for co_flags above */
  30. #define CO_OPTIMIZED 0x0001
  31. #define CO_NEWLOCALS 0x0002
  32. #define CO_VARARGS 0x0004
  33. #define CO_VARKEYWORDS 0x0008
  34. #define CO_NESTED 0x0010
  35. #define CO_GENERATOR 0x0020
  36. /* The CO_NOFREE flag is set if there are no free or cell variables.
  37. This information is redundant, but it allows a single flag test
  38. to determine whether there is any extra work to be done when the
  39. call frame it setup.
  40. */
  41. #define CO_NOFREE 0x0040
  42. #if 0
  43. /* This is no longer used. Stopped defining in 2.5, do not re-use. */
  44. #define CO_GENERATOR_ALLOWED 0x1000
  45. #endif
  46. #define CO_FUTURE_DIVISION 0x2000
  47. #define CO_FUTURE_ABSOLUTE_IMPORT 0x4000 /* do absolute imports by default */
  48. #define CO_FUTURE_WITH_STATEMENT 0x8000
  49. #define CO_FUTURE_PRINT_FUNCTION 0x10000
  50. #define CO_FUTURE_UNICODE_LITERALS 0x20000
  51. /* This should be defined if a future statement modifies the syntax.
  52. For example, when a keyword is added.
  53. */
  54. #if 1
  55. #define PY_PARSER_REQUIRES_FUTURE_KEYWORD
  56. #endif
  57. #define CO_MAXBLOCKS 20 /* Max static block nesting within a function */
  58. PyAPI_DATA(PyTypeObject) PyCode_Type;
  59. #define PyCode_Check(op) (Py_TYPE(op) == &PyCode_Type)
  60. #define PyCode_GetNumFree(op) (PyTuple_GET_SIZE((op)->co_freevars))
  61. /* Public interface */
  62. PyAPI_FUNC(PyCodeObject *) PyCode_New(
  63. int, int, int, int, PyObject *, PyObject *, PyObject *, PyObject *,
  64. PyObject *, PyObject *, PyObject *, PyObject *, int, PyObject *);
  65. /* same as struct above */
  66. /* Creates a new empty code object with the specified source location. */
  67. PyAPI_FUNC(PyCodeObject *)
  68. PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno);
  69. /* Return the line number associated with the specified bytecode index
  70. in this code object. If you just need the line number of a frame,
  71. use PyFrame_GetLineNumber() instead. */
  72. PyAPI_FUNC(int) PyCode_Addr2Line(PyCodeObject *, int);
  73. /* for internal use only */
  74. #define _PyCode_GETCODEPTR(co, pp) \
  75. ((*Py_TYPE((co)->co_code)->tp_as_buffer->bf_getreadbuffer) \
  76. ((co)->co_code, 0, (void **)(pp)))
  77. typedef struct _addr_pair {
  78. int ap_lower;
  79. int ap_upper;
  80. } PyAddrPair;
  81. /* Update *bounds to describe the first and one-past-the-last instructions in the
  82. same line as lasti. Return the number of that line.
  83. */
  84. PyAPI_FUNC(int) _PyCode_CheckLineNumber(PyCodeObject* co,
  85. int lasti, PyAddrPair *bounds);
  86. /* Create a comparable key used to compare constants taking in account the
  87. * object type. It is used to make sure types are not coerced (e.g., float and
  88. * complex) _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms
  89. *
  90. * Return (type(obj), obj, ...): a tuple with variable size (at least 2 items)
  91. * depending on the type and the value. The type is the first item to not
  92. * compare bytes and str which can raise a BytesWarning exception. */
  93. PyAPI_FUNC(PyObject*) _PyCode_ConstantKey(PyObject *obj);
  94. PyAPI_FUNC(PyObject*) PyCode_Optimize(PyObject *code, PyObject* consts,
  95. PyObject *names, PyObject *lineno_obj);
  96. #ifdef __cplusplus
  97. }
  98. #endif
  99. #endif /* !Py_CODE_H */