Debian Codegraph @

Include/cpython/code.h

raw
1 /* Definitions for bytecode */
2
3 #ifndef Py_LIMITED_API
4 #ifndef Py_CODE_H
5 #define Py_CODE_H
6
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10
11 /* Count of all local monitoring events */
12 #define _PY_MONITORING_LOCAL_EVENTS 10
13 /* Count of all "real" monitoring events (not derived from other events) */
14 #define _PY_MONITORING_UNGROUPED_EVENTS 15
15 /* Count of all monitoring events */
16 #define _PY_MONITORING_EVENTS 17
17
18 /* Tables of which tools are active for each monitored event. */
19 typedef struct _Py_LocalMonitors {
20 uint8_t tools[_PY_MONITORING_LOCAL_EVENTS];
21 } _Py_LocalMonitors;
22
23 typedef struct _Py_GlobalMonitors {
24 uint8_t tools[_PY_MONITORING_UNGROUPED_EVENTS];
25 } _Py_GlobalMonitors;
26
27
28 typedef struct {
29 PyObject *_co_code;
30 PyObject *_co_varnames;
31 PyObject *_co_cellvars;
32 PyObject *_co_freevars;
33 } _PyCoCached;
34
35 /* Ancillary data structure used for instrumentation.
36 Line instrumentation creates this with sufficient
37 space for one entry per code unit. The total size
38 of the data will be `bytes_per_entry * Py_SIZE(code)` */
39 typedef struct {
40 uint8_t bytes_per_entry;
41 uint8_t data[1];
42 } _PyCoLineInstrumentationData;
43
44
45 typedef struct {
46 int size;
47 int capacity;
48 struct _PyExecutorObject *executors[1];
49 } _PyExecutorArray;
50
51 /* Main data structure used for instrumentation.
52 * This is allocated when needed for instrumentation
53 */
54 typedef struct {
55 /* Monitoring specific to this code object */
56 _Py_LocalMonitors local_monitors;
57 /* Monitoring that is active on this code object */
58 _Py_LocalMonitors active_monitors;
59 /* The tools that are to be notified for events for the matching code unit */
60 uint8_t *tools;
61 /* Information to support line events */
62 _PyCoLineInstrumentationData *lines;
63 /* The tools that are to be notified for line events for the matching code unit */
64 uint8_t *line_tools;
65 /* Information to support instruction events */
66 /* The underlying instructions, which can themselves be instrumented */
67 uint8_t *per_instruction_opcodes;
68 /* The tools that are to be notified for instruction events for the matching code unit */
69 uint8_t *per_instruction_tools;
70 } _PyCoMonitoringData;
71
72 // To avoid repeating ourselves in deepfreeze.py, all PyCodeObject members are
73 // defined in this macro:
74 #define _PyCode_DEF(SIZE) { \
75 PyObject_VAR_HEAD \
76 \
77 /* Note only the following fields are used in hash and/or comparisons \
78 * \
79 * - co_name \
80 * - co_argcount \
81 * - co_posonlyargcount \
82 * - co_kwonlyargcount \
83 * - co_nlocals \
84 * - co_stacksize \
85 * - co_flags \
86 * - co_firstlineno \
87 * - co_consts \
88 * - co_names \
89 * - co_localsplusnames \
90 * This is done to preserve the name and line number for tracebacks \
91 * and debuggers; otherwise, constant de-duplication would collapse \
92 * identical functions/lambdas defined on different lines. \
93 */ \
94 \
95 /* These fields are set with provided values on new code objects. */ \
96 \
97 /* The hottest fields (in the eval loop) are grouped here at the top. */ \
98 PyObject *co_consts; /* list (constants used) */ \
99 PyObject *co_names; /* list of strings (names used) */ \
100 PyObject *co_exceptiontable; /* Byte string encoding exception handling \
101 table */ \
102 int co_flags; /* CO_..., see below */ \
103 \
104 /* The rest are not so impactful on performance. */ \
105 int co_argcount; /* #arguments, except *args */ \
106 int co_posonlyargcount; /* #positional only arguments */ \
107 int co_kwonlyargcount; /* #keyword only arguments */ \
108 int co_stacksize; /* #entries needed for evaluation stack */ \
109 int co_firstlineno; /* first source line number */ \
110 \
111 /* redundant values (derived from co_localsplusnames and \
112 co_localspluskinds) */ \
113 int co_nlocalsplus; /* number of local + cell + free variables */ \
114 int co_framesize; /* Size of frame in words */ \
115 int co_nlocals; /* number of local variables */ \
116 int co_ncellvars; /* total number of cell variables */ \
117 int co_nfreevars; /* number of free variables */ \
118 uint32_t co_version; /* version number */ \
119 \
120 PyObject *co_localsplusnames; /* tuple mapping offsets to names */ \
121 PyObject *co_localspluskinds; /* Bytes mapping to local kinds (one byte \
122 per variable) */ \
123 PyObject *co_filename; /* unicode (where it was loaded from) */ \
124 PyObject *co_name; /* unicode (name, for reference) */ \
125 PyObject *co_qualname; /* unicode (qualname, for reference) */ \
126 PyObject *co_linetable; /* bytes object that holds location info */ \
127 PyObject *co_weakreflist; /* to support weakrefs to code objects */ \
128 _PyExecutorArray *co_executors; /* executors from optimizer */ \
129 _PyCoCached *_co_cached; /* cached co_* attributes */ \
130 uintptr_t _co_instrumentation_version; /* current instrumentation version */ \
131 _PyCoMonitoringData *_co_monitoring; /* Monitoring data */ \
132 int _co_firsttraceable; /* index of first traceable instruction */ \
133 /* Scratch space for extra data relating to the code object. \
134 Type is a void* to keep the format private in codeobject.c to force \
135 people to go through the proper APIs. */ \
136 void *co_extra; \
137 char co_code_adaptive[(SIZE)]; \
138 }
139
140 /* Bytecode object */
141 struct PyCodeObject _PyCode_DEF(1);
142
143 /* Masks for co_flags above */
144 #define CO_OPTIMIZED 0x0001
145 #define CO_NEWLOCALS 0x0002
146 #define CO_VARARGS 0x0004
147 #define CO_VARKEYWORDS 0x0008
148 #define CO_NESTED 0x0010
149 #define CO_GENERATOR 0x0020
150
151 /* The CO_COROUTINE flag is set for coroutine functions (defined with
152 ``async def`` keywords) */
153 #define CO_COROUTINE 0x0080
154 #define CO_ITERABLE_COROUTINE 0x0100
155 #define CO_ASYNC_GENERATOR 0x0200
156
157 /* bpo-39562: These constant values are changed in Python 3.9
158 to prevent collision with compiler flags. CO_FUTURE_ and PyCF_
159 constants must be kept unique. PyCF_ constants can use bits from
160 0x0100 to 0x10000. CO_FUTURE_ constants use bits starting at 0x20000. */
161 #define CO_FUTURE_DIVISION 0x20000
162 #define CO_FUTURE_ABSOLUTE_IMPORT 0x40000 /* do absolute imports by default */
163 #define CO_FUTURE_WITH_STATEMENT 0x80000
164 #define CO_FUTURE_PRINT_FUNCTION 0x100000
165 #define CO_FUTURE_UNICODE_LITERALS 0x200000
166
167 #define CO_FUTURE_BARRY_AS_BDFL 0x400000
168 #define CO_FUTURE_GENERATOR_STOP 0x800000
169 #define CO_FUTURE_ANNOTATIONS 0x1000000
170
171 #define CO_NO_MONITORING_EVENTS 0x2000000
172
173 /* This should be defined if a future statement modifies the syntax.
174 For example, when a keyword is added.
175 */
176 #define PY_PARSER_REQUIRES_FUTURE_KEYWORD
177
178 #define CO_MAXBLOCKS 21 /* Max static block nesting within a function */
179
180 PyAPI_DATA(PyTypeObject) PyCode_Type;
181
182 #define PyCode_Check(op) Py_IS_TYPE((op), &PyCode_Type)
183
184 static inline Py_ssize_t PyCode_GetNumFree(PyCodeObject *op) {
185 assert(PyCode_Check(op));
186 return op->co_nfreevars;
187 }
188
189 static inline int PyUnstable_Code_GetFirstFree(PyCodeObject *op) {
190 assert(PyCode_Check(op));
191 return op->co_nlocalsplus - op->co_nfreevars;
192 }
193
194 Py_DEPRECATED(3.13) static inline int PyCode_GetFirstFree(PyCodeObject *op) {
195 return PyUnstable_Code_GetFirstFree(op);
196 }
197
198 /* Unstable public interface */
199 PyAPI_FUNC(PyCodeObject *) PyUnstable_Code_New(
200 int, int, int, int, int, PyObject *, PyObject *,
201 PyObject *, PyObject *, PyObject *, PyObject *,
202 PyObject *, PyObject *, PyObject *, int, PyObject *,
203 PyObject *);
204
205 PyAPI_FUNC(PyCodeObject *) PyUnstable_Code_NewWithPosOnlyArgs(
206 int, int, int, int, int, int, PyObject *, PyObject *,
207 PyObject *, PyObject *, PyObject *, PyObject *,
208 PyObject *, PyObject *, PyObject *, int, PyObject *,
209 PyObject *);
210 /* same as struct above */
211 // Old names -- remove when this API changes:
212 _Py_DEPRECATED_EXTERNALLY(3.12) static inline PyCodeObject *
213 PyCode_New(
214 int a, int b, int c, int d, int e, PyObject *f, PyObject *g,
215 PyObject *h, PyObject *i, PyObject *j, PyObject *k,
216 PyObject *l, PyObject *m, PyObject *n, int o, PyObject *p,
217 PyObject *q)
218 {
219 return PyUnstable_Code_New(
220 a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q);
221 }
222 _Py_DEPRECATED_EXTERNALLY(3.12) static inline PyCodeObject *
223 PyCode_NewWithPosOnlyArgs(
224 int a, int poac, int b, int c, int d, int e, PyObject *f, PyObject *g,
225 PyObject *h, PyObject *i, PyObject *j, PyObject *k,
226 PyObject *l, PyObject *m, PyObject *n, int o, PyObject *p,
227 PyObject *q)
228 {
229 return PyUnstable_Code_NewWithPosOnlyArgs(
230 a, poac, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q);
231 }
232
233 /* Creates a new empty code object with the specified source location. */
234 PyAPI_FUNC(PyCodeObject *)
235 PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno);
236
237 /* Return the line number associated with the specified bytecode index
238 in this code object. If you just need the line number of a frame,
239 use PyFrame_GetLineNumber() instead. */
240 PyAPI_FUNC(int) PyCode_Addr2Line(PyCodeObject *, int);
241
242 PyAPI_FUNC(int) PyCode_Addr2Location(PyCodeObject *, int, int *, int *, int *, int *);
243
244 #define PY_FOREACH_CODE_EVENT(V) \
245 V(CREATE) \
246 V(DESTROY)
247
248 typedef enum {
249 #define PY_DEF_EVENT(op) PY_CODE_EVENT_##op,
250 PY_FOREACH_CODE_EVENT(PY_DEF_EVENT)
251 #undef PY_DEF_EVENT
252 } PyCodeEvent;
253
254
255 /*
256 * A callback that is invoked for different events in a code object's lifecycle.
257 *
258 * The callback is invoked with a borrowed reference to co, after it is
259 * created and before it is destroyed.
260 *
261 * If the callback sets an exception, it must return -1. Otherwise
262 * it should return 0.
263 */
264 typedef int (*PyCode_WatchCallback)(
265 PyCodeEvent event,
266 PyCodeObject* co);
267
268 /*
269 * Register a per-interpreter callback that will be invoked for code object
270 * lifecycle events.
271 *
272 * Returns a handle that may be passed to PyCode_ClearWatcher on success,
273 * or -1 and sets an error if no more handles are available.
274 */
275 PyAPI_FUNC(int) PyCode_AddWatcher(PyCode_WatchCallback callback);
276
277 /*
278 * Clear the watcher associated with the watcher_id handle.
279 *
280 * Returns 0 on success or -1 if no watcher exists for the provided id.
281 */
282 PyAPI_FUNC(int) PyCode_ClearWatcher(int watcher_id);
283
284 /* for internal use only */
285 struct _opaque {
286 int computed_line;
287 const uint8_t *lo_next;
288 const uint8_t *limit;
289 };
290
291 typedef struct _line_offsets {
292 int ar_start;
293 int ar_end;
294 int ar_line;
295 struct _opaque opaque;
296 } PyCodeAddressRange;
297
298 /* Update *bounds to describe the first and one-past-the-last instructions in the
299 same line as lasti. Return the number of that line.
300 */
301 PyAPI_FUNC(int) _PyCode_CheckLineNumber(int lasti, PyCodeAddressRange *bounds);
302
303 /* Create a comparable key used to compare constants taking in account the
304 * object type. It is used to make sure types are not coerced (e.g., float and
305 * complex) _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms
306 *
307 * Return (type(obj), obj, ...): a tuple with variable size (at least 2 items)
308 * depending on the type and the value. The type is the first item to not
309 * compare bytes and str which can raise a BytesWarning exception. */
310 PyAPI_FUNC(PyObject*) _PyCode_ConstantKey(PyObject *obj);
311
312 PyAPI_FUNC(PyObject*) PyCode_Optimize(PyObject *code, PyObject* consts,
313 PyObject *names, PyObject *lnotab);
314
315 PyAPI_FUNC(int) PyUnstable_Code_GetExtra(
316 PyObject *code, Py_ssize_t index, void **extra);
317 PyAPI_FUNC(int) PyUnstable_Code_SetExtra(
318 PyObject *code, Py_ssize_t index, void *extra);
319 // Old names -- remove when this API changes:
320 _Py_DEPRECATED_EXTERNALLY(3.12) static inline int
321 _PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
322 {
323 return PyUnstable_Code_GetExtra(code, index, extra);
324 }
325 _Py_DEPRECATED_EXTERNALLY(3.12) static inline int
326 _PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
327 {
328 return PyUnstable_Code_SetExtra(code, index, extra);
329 }
330
331 /* Equivalent to getattr(code, 'co_code') in Python.
332 Returns a strong reference to a bytes object. */
333 PyAPI_FUNC(PyObject *) PyCode_GetCode(PyCodeObject *code);
334 /* Equivalent to getattr(code, 'co_varnames') in Python. */
335 PyAPI_FUNC(PyObject *) PyCode_GetVarnames(PyCodeObject *code);
336 /* Equivalent to getattr(code, 'co_cellvars') in Python. */
337 PyAPI_FUNC(PyObject *) PyCode_GetCellvars(PyCodeObject *code);
338 /* Equivalent to getattr(code, 'co_freevars') in Python. */
339 PyAPI_FUNC(PyObject *) PyCode_GetFreevars(PyCodeObject *code);
340
341 typedef enum _PyCodeLocationInfoKind {
342 /* short forms are 0 to 9 */
343 PY_CODE_LOCATION_INFO_SHORT0 = 0,
344 /* one lineforms are 10 to 12 */
345 PY_CODE_LOCATION_INFO_ONE_LINE0 = 10,
346 PY_CODE_LOCATION_INFO_ONE_LINE1 = 11,
347 PY_CODE_LOCATION_INFO_ONE_LINE2 = 12,
348
349 PY_CODE_LOCATION_INFO_NO_COLUMNS = 13,
350 PY_CODE_LOCATION_INFO_LONG = 14,
351 PY_CODE_LOCATION_INFO_NONE = 15
352 } _PyCodeLocationInfoKind;
353
354 #ifdef __cplusplus
355 }
356 #endif
357 #endif // !Py_CODE_H
358 #endif // !Py_LIMITED_API