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