pycore_compile.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #ifndef Py_INTERNAL_COMPILE_H
  2. #define Py_INTERNAL_COMPILE_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #ifndef Py_BUILD_CORE
  7. # error "this header requires Py_BUILD_CORE define"
  8. #endif
  9. struct _arena; // Type defined in pycore_pyarena.h
  10. struct _mod; // Type defined in pycore_ast.h
  11. // Export the symbol for test_peg_generator (built as a library)
  12. PyAPI_FUNC(PyCodeObject*) _PyAST_Compile(
  13. struct _mod *mod,
  14. PyObject *filename,
  15. PyCompilerFlags *flags,
  16. int optimize,
  17. struct _arena *arena);
  18. static const _PyCompilerSrcLocation NO_LOCATION = {-1, -1, -1, -1};
  19. typedef struct {
  20. int optimize;
  21. int ff_features;
  22. int recursion_depth; /* current recursion depth */
  23. int recursion_limit; /* recursion limit */
  24. } _PyASTOptimizeState;
  25. extern int _PyAST_Optimize(
  26. struct _mod *,
  27. struct _arena *arena,
  28. _PyASTOptimizeState *state);
  29. typedef struct {
  30. int h_offset;
  31. int h_startdepth;
  32. int h_preserve_lasti;
  33. } _PyCompile_ExceptHandlerInfo;
  34. typedef struct {
  35. int i_opcode;
  36. int i_oparg;
  37. _PyCompilerSrcLocation i_loc;
  38. _PyCompile_ExceptHandlerInfo i_except_handler_info;
  39. } _PyCompile_Instruction;
  40. typedef struct {
  41. _PyCompile_Instruction *s_instrs;
  42. int s_allocated;
  43. int s_used;
  44. int *s_labelmap; /* label id --> instr offset */
  45. int s_labelmap_size;
  46. int s_next_free_label; /* next free label id */
  47. } _PyCompile_InstructionSequence;
  48. typedef struct {
  49. PyObject *u_name;
  50. PyObject *u_qualname; /* dot-separated qualified name (lazy) */
  51. /* The following fields are dicts that map objects to
  52. the index of them in co_XXX. The index is used as
  53. the argument for opcodes that refer to those collections.
  54. */
  55. PyObject *u_consts; /* all constants */
  56. PyObject *u_names; /* all names */
  57. PyObject *u_varnames; /* local variables */
  58. PyObject *u_cellvars; /* cell variables */
  59. PyObject *u_freevars; /* free variables */
  60. PyObject *u_fasthidden; /* dict; keys are names that are fast-locals only
  61. temporarily within an inlined comprehension. When
  62. value is True, treat as fast-local. */
  63. Py_ssize_t u_argcount; /* number of arguments for block */
  64. Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */
  65. Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */
  66. int u_firstlineno; /* the first lineno of the block */
  67. } _PyCompile_CodeUnitMetadata;
  68. /* Utility for a number of growing arrays used in the compiler */
  69. int _PyCompile_EnsureArrayLargeEnough(
  70. int idx,
  71. void **array,
  72. int *alloc,
  73. int default_alloc,
  74. size_t item_size);
  75. int _PyCompile_ConstCacheMergeOne(PyObject *const_cache, PyObject **obj);
  76. int _PyCompile_InstrSize(int opcode, int oparg);
  77. /* Access compiler internals for unit testing */
  78. PyAPI_FUNC(PyObject*) _PyCompile_CodeGen(
  79. PyObject *ast,
  80. PyObject *filename,
  81. PyCompilerFlags *flags,
  82. int optimize,
  83. int compile_mode);
  84. PyAPI_FUNC(PyObject*) _PyCompile_OptimizeCfg(
  85. PyObject *instructions,
  86. PyObject *consts,
  87. int nlocals);
  88. PyAPI_FUNC(PyCodeObject*)
  89. _PyCompile_Assemble(_PyCompile_CodeUnitMetadata *umd, PyObject *filename,
  90. PyObject *instructions);
  91. #ifdef __cplusplus
  92. }
  93. #endif
  94. #endif /* !Py_INTERNAL_COMPILE_H */