pycore_frame.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #ifndef Py_INTERNAL_FRAME_H
  2. #define Py_INTERNAL_FRAME_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include <stdbool.h>
  7. #include <stddef.h>
  8. #include "pycore_code.h" // STATS
  9. /* See Objects/frame_layout.md for an explanation of the frame stack
  10. * including explanation of the PyFrameObject and _PyInterpreterFrame
  11. * structs. */
  12. struct _frame {
  13. PyObject_HEAD
  14. PyFrameObject *f_back; /* previous frame, or NULL */
  15. struct _PyInterpreterFrame *f_frame; /* points to the frame data */
  16. PyObject *f_trace; /* Trace function */
  17. int f_lineno; /* Current line number. Only valid if non-zero */
  18. char f_trace_lines; /* Emit per-line trace events? */
  19. char f_trace_opcodes; /* Emit per-opcode trace events? */
  20. char f_fast_as_locals; /* Have the fast locals of this frame been converted to a dict? */
  21. /* The frame data, if this frame object owns the frame */
  22. PyObject *_f_frame_data[1];
  23. };
  24. extern PyFrameObject* _PyFrame_New_NoTrack(PyCodeObject *code);
  25. /* other API */
  26. typedef enum _framestate {
  27. FRAME_CREATED = -2,
  28. FRAME_SUSPENDED = -1,
  29. FRAME_EXECUTING = 0,
  30. FRAME_COMPLETED = 1,
  31. FRAME_CLEARED = 4
  32. } PyFrameState;
  33. #define FRAME_STATE_FINISHED(S) ((S) >= FRAME_COMPLETED)
  34. enum _frameowner {
  35. FRAME_OWNED_BY_THREAD = 0,
  36. FRAME_OWNED_BY_GENERATOR = 1,
  37. FRAME_OWNED_BY_FRAME_OBJECT = 2,
  38. FRAME_OWNED_BY_CSTACK = 3,
  39. };
  40. typedef struct _PyInterpreterFrame {
  41. PyCodeObject *f_code; /* Strong reference */
  42. struct _PyInterpreterFrame *previous;
  43. PyObject *f_funcobj; /* Strong reference. Only valid if not on C stack */
  44. PyObject *f_globals; /* Borrowed reference. Only valid if not on C stack */
  45. PyObject *f_builtins; /* Borrowed reference. Only valid if not on C stack */
  46. PyObject *f_locals; /* Strong reference, may be NULL. Only valid if not on C stack */
  47. PyFrameObject *frame_obj; /* Strong reference, may be NULL. Only valid if not on C stack */
  48. // NOTE: This is not necessarily the last instruction started in the given
  49. // frame. Rather, it is the code unit *prior to* the *next* instruction. For
  50. // example, it may be an inline CACHE entry, an instruction we just jumped
  51. // over, or (in the case of a newly-created frame) a totally invalid value:
  52. _Py_CODEUNIT *prev_instr;
  53. int stacktop; /* Offset of TOS from localsplus */
  54. /* The return_offset determines where a `RETURN` should go in the caller,
  55. * relative to `prev_instr`.
  56. * It is only meaningful to the callee,
  57. * so it needs to be set in any CALL (to a Python function)
  58. * or SEND (to a coroutine or generator).
  59. * If there is no callee, then it is meaningless. */
  60. uint16_t return_offset;
  61. char owner;
  62. /* Locals and stack */
  63. PyObject *localsplus[1];
  64. } _PyInterpreterFrame;
  65. #define _PyInterpreterFrame_LASTI(IF) \
  66. ((int)((IF)->prev_instr - _PyCode_CODE((IF)->f_code)))
  67. static inline PyObject **_PyFrame_Stackbase(_PyInterpreterFrame *f) {
  68. return f->localsplus + f->f_code->co_nlocalsplus;
  69. }
  70. static inline PyObject *_PyFrame_StackPeek(_PyInterpreterFrame *f) {
  71. assert(f->stacktop > f->f_code->co_nlocalsplus);
  72. assert(f->localsplus[f->stacktop-1] != NULL);
  73. return f->localsplus[f->stacktop-1];
  74. }
  75. static inline PyObject *_PyFrame_StackPop(_PyInterpreterFrame *f) {
  76. assert(f->stacktop > f->f_code->co_nlocalsplus);
  77. f->stacktop--;
  78. return f->localsplus[f->stacktop];
  79. }
  80. static inline void _PyFrame_StackPush(_PyInterpreterFrame *f, PyObject *value) {
  81. f->localsplus[f->stacktop] = value;
  82. f->stacktop++;
  83. }
  84. #define FRAME_SPECIALS_SIZE ((int)((sizeof(_PyInterpreterFrame)-1)/sizeof(PyObject *)))
  85. static inline int
  86. _PyFrame_NumSlotsForCodeObject(PyCodeObject *code)
  87. {
  88. /* This function needs to remain in sync with the calculation of
  89. * co_framesize in Tools/build/deepfreeze.py */
  90. assert(code->co_framesize >= FRAME_SPECIALS_SIZE);
  91. return code->co_framesize - FRAME_SPECIALS_SIZE;
  92. }
  93. void _PyFrame_Copy(_PyInterpreterFrame *src, _PyInterpreterFrame *dest);
  94. /* Consumes reference to func and locals.
  95. Does not initialize frame->previous, which happens
  96. when frame is linked into the frame stack.
  97. */
  98. static inline void
  99. _PyFrame_Initialize(
  100. _PyInterpreterFrame *frame, PyFunctionObject *func,
  101. PyObject *locals, PyCodeObject *code, int null_locals_from)
  102. {
  103. frame->f_funcobj = (PyObject *)func;
  104. frame->f_code = (PyCodeObject *)Py_NewRef(code);
  105. frame->f_builtins = func->func_builtins;
  106. frame->f_globals = func->func_globals;
  107. frame->f_locals = locals;
  108. frame->stacktop = code->co_nlocalsplus;
  109. frame->frame_obj = NULL;
  110. frame->prev_instr = _PyCode_CODE(code) - 1;
  111. frame->return_offset = 0;
  112. frame->owner = FRAME_OWNED_BY_THREAD;
  113. for (int i = null_locals_from; i < code->co_nlocalsplus; i++) {
  114. frame->localsplus[i] = NULL;
  115. }
  116. }
  117. /* Gets the pointer to the locals array
  118. * that precedes this frame.
  119. */
  120. static inline PyObject**
  121. _PyFrame_GetLocalsArray(_PyInterpreterFrame *frame)
  122. {
  123. return frame->localsplus;
  124. }
  125. /* Fetches the stack pointer, and sets stacktop to -1.
  126. Having stacktop <= 0 ensures that invalid
  127. values are not visible to the cycle GC.
  128. We choose -1 rather than 0 to assist debugging. */
  129. static inline PyObject**
  130. _PyFrame_GetStackPointer(_PyInterpreterFrame *frame)
  131. {
  132. PyObject **sp = frame->localsplus + frame->stacktop;
  133. frame->stacktop = -1;
  134. return sp;
  135. }
  136. static inline void
  137. _PyFrame_SetStackPointer(_PyInterpreterFrame *frame, PyObject **stack_pointer)
  138. {
  139. frame->stacktop = (int)(stack_pointer - frame->localsplus);
  140. }
  141. /* Determine whether a frame is incomplete.
  142. * A frame is incomplete if it is part way through
  143. * creating cell objects or a generator or coroutine.
  144. *
  145. * Frames on the frame stack are incomplete until the
  146. * first RESUME instruction.
  147. * Frames owned by a generator are always complete.
  148. */
  149. static inline bool
  150. _PyFrame_IsIncomplete(_PyInterpreterFrame *frame)
  151. {
  152. return frame->owner != FRAME_OWNED_BY_GENERATOR &&
  153. frame->prev_instr < _PyCode_CODE(frame->f_code) + frame->f_code->_co_firsttraceable;
  154. }
  155. static inline _PyInterpreterFrame *
  156. _PyFrame_GetFirstComplete(_PyInterpreterFrame *frame)
  157. {
  158. while (frame && _PyFrame_IsIncomplete(frame)) {
  159. frame = frame->previous;
  160. }
  161. return frame;
  162. }
  163. static inline _PyInterpreterFrame *
  164. _PyThreadState_GetFrame(PyThreadState *tstate)
  165. {
  166. return _PyFrame_GetFirstComplete(tstate->cframe->current_frame);
  167. }
  168. /* For use by _PyFrame_GetFrameObject
  169. Do not call directly. */
  170. PyFrameObject *
  171. _PyFrame_MakeAndSetFrameObject(_PyInterpreterFrame *frame);
  172. /* Gets the PyFrameObject for this frame, lazily
  173. * creating it if necessary.
  174. * Returns a borrowed reference */
  175. static inline PyFrameObject *
  176. _PyFrame_GetFrameObject(_PyInterpreterFrame *frame)
  177. {
  178. assert(!_PyFrame_IsIncomplete(frame));
  179. PyFrameObject *res = frame->frame_obj;
  180. if (res != NULL) {
  181. return res;
  182. }
  183. return _PyFrame_MakeAndSetFrameObject(frame);
  184. }
  185. /* Clears all references in the frame.
  186. * If take is non-zero, then the _PyInterpreterFrame frame
  187. * may be transferred to the frame object it references
  188. * instead of being cleared. Either way
  189. * the caller no longer owns the references
  190. * in the frame.
  191. * take should be set to 1 for heap allocated
  192. * frames like the ones in generators and coroutines.
  193. */
  194. void
  195. _PyFrame_ClearExceptCode(_PyInterpreterFrame * frame);
  196. int
  197. _PyFrame_Traverse(_PyInterpreterFrame *frame, visitproc visit, void *arg);
  198. PyObject *
  199. _PyFrame_GetLocals(_PyInterpreterFrame *frame, int include_hidden);
  200. int
  201. _PyFrame_FastToLocalsWithError(_PyInterpreterFrame *frame);
  202. void
  203. _PyFrame_LocalsToFast(_PyInterpreterFrame *frame, int clear);
  204. static inline bool
  205. _PyThreadState_HasStackSpace(PyThreadState *tstate, int size)
  206. {
  207. assert(
  208. (tstate->datastack_top == NULL && tstate->datastack_limit == NULL)
  209. ||
  210. (tstate->datastack_top != NULL && tstate->datastack_limit != NULL)
  211. );
  212. return tstate->datastack_top != NULL &&
  213. size < tstate->datastack_limit - tstate->datastack_top;
  214. }
  215. extern _PyInterpreterFrame *
  216. _PyThreadState_PushFrame(PyThreadState *tstate, size_t size);
  217. void _PyThreadState_PopFrame(PyThreadState *tstate, _PyInterpreterFrame *frame);
  218. /* Pushes a frame without checking for space.
  219. * Must be guarded by _PyThreadState_HasStackSpace()
  220. * Consumes reference to func. */
  221. static inline _PyInterpreterFrame *
  222. _PyFrame_PushUnchecked(PyThreadState *tstate, PyFunctionObject *func, int null_locals_from)
  223. {
  224. CALL_STAT_INC(frames_pushed);
  225. PyCodeObject *code = (PyCodeObject *)func->func_code;
  226. _PyInterpreterFrame *new_frame = (_PyInterpreterFrame *)tstate->datastack_top;
  227. tstate->datastack_top += code->co_framesize;
  228. assert(tstate->datastack_top < tstate->datastack_limit);
  229. _PyFrame_Initialize(new_frame, func, NULL, code, null_locals_from);
  230. return new_frame;
  231. }
  232. static inline
  233. PyGenObject *_PyFrame_GetGenerator(_PyInterpreterFrame *frame)
  234. {
  235. assert(frame->owner == FRAME_OWNED_BY_GENERATOR);
  236. size_t offset_in_gen = offsetof(PyGenObject, gi_iframe);
  237. return (PyGenObject *)(((char *)frame) - offset_in_gen);
  238. }
  239. #ifdef __cplusplus
  240. }
  241. #endif
  242. #endif /* !Py_INTERNAL_FRAME_H */