• 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 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 
10 typedef uint16_t _Py_CODEUNIT;
11 
12 #ifdef WORDS_BIGENDIAN
13 #  define _Py_OPCODE(word) ((word) >> 8)
14 #  define _Py_OPARG(word) ((word) & 255)
15 #else
16 #  define _Py_OPCODE(word) ((word) & 255)
17 #  define _Py_OPARG(word) ((word) >> 8)
18 #endif
19 
20 /* Bytecode object */
21 typedef struct {
22     PyObject_HEAD
23     int co_argcount;            /* #arguments, except *args */
24     int co_kwonlyargcount;      /* #keyword only arguments */
25     int co_nlocals;             /* #local variables */
26     int co_stacksize;           /* #entries needed for evaluation stack */
27     int co_flags;               /* CO_..., see below */
28     int co_firstlineno;         /* first source line number */
29     PyObject *co_code;          /* instruction opcodes */
30     PyObject *co_consts;        /* list (constants used) */
31     PyObject *co_names;         /* list of strings (names used) */
32     PyObject *co_varnames;      /* tuple of strings (local variable names) */
33     PyObject *co_freevars;      /* tuple of strings (free variable names) */
34     PyObject *co_cellvars;      /* tuple of strings (cell variable names) */
35     /* The rest aren't used in either hash or comparisons, except for co_name,
36        used in both. This is done to preserve the name and line number
37        for tracebacks and debuggers; otherwise, constant de-duplication
38        would collapse identical functions/lambdas defined on different lines.
39     */
40     Py_ssize_t *co_cell2arg;    /* Maps cell vars which are arguments. */
41     PyObject *co_filename;      /* unicode (where it was loaded from) */
42     PyObject *co_name;          /* unicode (name, for reference) */
43     PyObject *co_lnotab;        /* string (encoding addr<->lineno mapping) See
44                                    Objects/lnotab_notes.txt for details. */
45     void *co_zombieframe;       /* for optimization only (see frameobject.c) */
46     PyObject *co_weakreflist;   /* to support weakrefs to code objects */
47     /* Scratch space for extra data relating to the code object.
48        Type is a void* to keep the format private in codeobject.c to force
49        people to go through the proper APIs. */
50     void *co_extra;
51 } PyCodeObject;
52 
53 /* Masks for co_flags above */
54 #define CO_OPTIMIZED    0x0001
55 #define CO_NEWLOCALS    0x0002
56 #define CO_VARARGS      0x0004
57 #define CO_VARKEYWORDS  0x0008
58 #define CO_NESTED       0x0010
59 #define CO_GENERATOR    0x0020
60 /* The CO_NOFREE flag is set if there are no free or cell variables.
61    This information is redundant, but it allows a single flag test
62    to determine whether there is any extra work to be done when the
63    call frame it setup.
64 */
65 #define CO_NOFREE       0x0040
66 
67 /* The CO_COROUTINE flag is set for coroutine functions (defined with
68    ``async def`` keywords) */
69 #define CO_COROUTINE            0x0080
70 #define CO_ITERABLE_COROUTINE   0x0100
71 #define CO_ASYNC_GENERATOR      0x0200
72 
73 /* These are no longer used. */
74 #if 0
75 #define CO_GENERATOR_ALLOWED    0x1000
76 #endif
77 #define CO_FUTURE_DIVISION      0x2000
78 #define CO_FUTURE_ABSOLUTE_IMPORT 0x4000 /* do absolute imports by default */
79 #define CO_FUTURE_WITH_STATEMENT  0x8000
80 #define CO_FUTURE_PRINT_FUNCTION  0x10000
81 #define CO_FUTURE_UNICODE_LITERALS 0x20000
82 
83 #define CO_FUTURE_BARRY_AS_BDFL  0x40000
84 #define CO_FUTURE_GENERATOR_STOP  0x80000
85 #define CO_FUTURE_ANNOTATIONS    0x100000
86 
87 /* This value is found in the co_cell2arg array when the associated cell
88    variable does not correspond to an argument. */
89 #define CO_CELL_NOT_AN_ARG (-1)
90 
91 /* This should be defined if a future statement modifies the syntax.
92    For example, when a keyword is added.
93 */
94 #define PY_PARSER_REQUIRES_FUTURE_KEYWORD
95 
96 #define CO_MAXBLOCKS 20 /* Max static block nesting within a function */
97 
98 PyAPI_DATA(PyTypeObject) PyCode_Type;
99 
100 #define PyCode_Check(op) (Py_TYPE(op) == &PyCode_Type)
101 #define PyCode_GetNumFree(op) (PyTuple_GET_SIZE((op)->co_freevars))
102 
103 /* Public interface */
104 PyAPI_FUNC(PyCodeObject *) PyCode_New(
105         int, int, int, int, int, PyObject *, PyObject *,
106         PyObject *, PyObject *, PyObject *, PyObject *,
107         PyObject *, PyObject *, int, PyObject *);
108         /* same as struct above */
109 
110 /* Creates a new empty code object with the specified source location. */
111 PyAPI_FUNC(PyCodeObject *)
112 PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno);
113 
114 /* Return the line number associated with the specified bytecode index
115    in this code object.  If you just need the line number of a frame,
116    use PyFrame_GetLineNumber() instead. */
117 PyAPI_FUNC(int) PyCode_Addr2Line(PyCodeObject *, int);
118 
119 /* for internal use only */
120 typedef struct _addr_pair {
121         int ap_lower;
122         int ap_upper;
123 } PyAddrPair;
124 
125 #ifndef Py_LIMITED_API
126 /* Update *bounds to describe the first and one-past-the-last instructions in the
127    same line as lasti.  Return the number of that line.
128 */
129 PyAPI_FUNC(int) _PyCode_CheckLineNumber(PyCodeObject* co,
130                                         int lasti, PyAddrPair *bounds);
131 
132 /* Create a comparable key used to compare constants taking in account the
133  * object type. It is used to make sure types are not coerced (e.g., float and
134  * complex) _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms
135  *
136  * Return (type(obj), obj, ...): a tuple with variable size (at least 2 items)
137  * depending on the type and the value. The type is the first item to not
138  * compare bytes and str which can raise a BytesWarning exception. */
139 PyAPI_FUNC(PyObject*) _PyCode_ConstantKey(PyObject *obj);
140 #endif
141 
142 PyAPI_FUNC(PyObject*) PyCode_Optimize(PyObject *code, PyObject* consts,
143                                       PyObject *names, PyObject *lnotab);
144 
145 
146 #ifndef Py_LIMITED_API
147 PyAPI_FUNC(int) _PyCode_GetExtra(PyObject *code, Py_ssize_t index,
148                                  void **extra);
149 PyAPI_FUNC(int) _PyCode_SetExtra(PyObject *code, Py_ssize_t index,
150                                  void *extra);
151 #endif
152 
153 #ifdef __cplusplus
154 }
155 #endif
156 #endif /* !Py_CODE_H */
157 #endif /* Py_LIMITED_API */
158