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