| #ifndef Py_INTERNAL_CODE_H |
| #define Py_INTERNAL_CODE_H |
| #ifdef __cplusplus |
| extern "C" { |
| #endif |
|
|
| #ifndef Py_BUILD_CORE |
| # error "this header requires Py_BUILD_CORE define" |
| #endif |
|
|
| #include "pycore_lock.h" |
| #include "pycore_backoff.h" |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| typedef union { |
| uint16_t cache; |
| struct { |
| uint8_t code; |
| uint8_t arg; |
| } op; |
| _Py_BackoffCounter counter; |
| } _Py_CODEUNIT; |
|
|
| #define _PyCode_CODE(CO) _Py_RVALUE((_Py_CODEUNIT *)(CO)->co_code_adaptive) |
| #define _PyCode_NBYTES(CO) (Py_SIZE(CO) * (Py_ssize_t)sizeof(_Py_CODEUNIT)) |
|
|
|
|
| |
| #define _Py_OPCODE(word) ((word).op.code) |
| #define _Py_OPARG(word) ((word).op.arg) |
|
|
| static inline _Py_CODEUNIT |
| _py_make_codeunit(uint8_t opcode, uint8_t oparg) |
| { |
| |
| _Py_CODEUNIT word; |
| word.op.code = opcode; |
| word.op.arg = oparg; |
| return word; |
| } |
|
|
| static inline void |
| _py_set_opcode(_Py_CODEUNIT *word, uint8_t opcode) |
| { |
| word->op.code = opcode; |
| } |
|
|
| #define _Py_MAKE_CODEUNIT(opcode, oparg) _py_make_codeunit((opcode), (oparg)) |
| #define _Py_SET_OPCODE(word, opcode) _py_set_opcode(&(word), (opcode)) |
|
|
|
|
| |
| |
| #define _PyCode_HAS_EXECUTORS(CODE) \ |
| (CODE->co_executors != NULL) |
| #define _PyCode_HAS_INSTRUMENTATION(CODE) \ |
| (CODE->_co_instrumentation_version > 0) |
|
|
| struct _py_code_state { |
| PyMutex mutex; |
| |
| struct _Py_hashtable_t *constants; |
| }; |
|
|
| extern PyStatus _PyCode_Init(PyInterpreterState *interp); |
| extern void _PyCode_Fini(PyInterpreterState *interp); |
|
|
| #define CODE_MAX_WATCHERS 8 |
|
|
| |
| |
| |
|
|
|
|
| |
| |
| |
|
|
| #define CACHE_ENTRIES(cache) (sizeof(cache)/sizeof(_Py_CODEUNIT)) |
|
|
| typedef struct { |
| _Py_BackoffCounter counter; |
| uint16_t module_keys_version; |
| uint16_t builtin_keys_version; |
| uint16_t index; |
| } _PyLoadGlobalCache; |
|
|
| #define INLINE_CACHE_ENTRIES_LOAD_GLOBAL CACHE_ENTRIES(_PyLoadGlobalCache) |
|
|
| typedef struct { |
| _Py_BackoffCounter counter; |
| } _PyBinaryOpCache; |
|
|
| #define INLINE_CACHE_ENTRIES_BINARY_OP CACHE_ENTRIES(_PyBinaryOpCache) |
|
|
| typedef struct { |
| _Py_BackoffCounter counter; |
| } _PyUnpackSequenceCache; |
|
|
| #define INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE \ |
| CACHE_ENTRIES(_PyUnpackSequenceCache) |
|
|
| typedef struct { |
| _Py_BackoffCounter counter; |
| } _PyCompareOpCache; |
|
|
| #define INLINE_CACHE_ENTRIES_COMPARE_OP CACHE_ENTRIES(_PyCompareOpCache) |
|
|
| typedef struct { |
| _Py_BackoffCounter counter; |
| } _PyBinarySubscrCache; |
|
|
| #define INLINE_CACHE_ENTRIES_BINARY_SUBSCR CACHE_ENTRIES(_PyBinarySubscrCache) |
|
|
| typedef struct { |
| _Py_BackoffCounter counter; |
| } _PySuperAttrCache; |
|
|
| #define INLINE_CACHE_ENTRIES_LOAD_SUPER_ATTR CACHE_ENTRIES(_PySuperAttrCache) |
|
|
| typedef struct { |
| _Py_BackoffCounter counter; |
| uint16_t version[2]; |
| uint16_t index; |
| } _PyAttrCache; |
|
|
| typedef struct { |
| _Py_BackoffCounter counter; |
| uint16_t type_version[2]; |
| union { |
| uint16_t keys_version[2]; |
| uint16_t dict_offset; |
| }; |
| uint16_t descr[4]; |
| } _PyLoadMethodCache; |
|
|
|
|
| |
| #define INLINE_CACHE_ENTRIES_LOAD_ATTR CACHE_ENTRIES(_PyLoadMethodCache) |
|
|
| #define INLINE_CACHE_ENTRIES_STORE_ATTR CACHE_ENTRIES(_PyAttrCache) |
|
|
| typedef struct { |
| _Py_BackoffCounter counter; |
| uint16_t func_version[2]; |
| } _PyCallCache; |
|
|
| #define INLINE_CACHE_ENTRIES_CALL CACHE_ENTRIES(_PyCallCache) |
|
|
| typedef struct { |
| _Py_BackoffCounter counter; |
| } _PyStoreSubscrCache; |
|
|
| #define INLINE_CACHE_ENTRIES_STORE_SUBSCR CACHE_ENTRIES(_PyStoreSubscrCache) |
|
|
| typedef struct { |
| _Py_BackoffCounter counter; |
| } _PyForIterCache; |
|
|
| #define INLINE_CACHE_ENTRIES_FOR_ITER CACHE_ENTRIES(_PyForIterCache) |
|
|
| typedef struct { |
| _Py_BackoffCounter counter; |
| } _PySendCache; |
|
|
| #define INLINE_CACHE_ENTRIES_SEND CACHE_ENTRIES(_PySendCache) |
|
|
| typedef struct { |
| _Py_BackoffCounter counter; |
| uint16_t version[2]; |
| } _PyToBoolCache; |
|
|
| #define INLINE_CACHE_ENTRIES_TO_BOOL CACHE_ENTRIES(_PyToBoolCache) |
|
|
| typedef struct { |
| _Py_BackoffCounter counter; |
| } _PyContainsOpCache; |
|
|
| #define INLINE_CACHE_ENTRIES_CONTAINS_OP CACHE_ENTRIES(_PyContainsOpCache) |
|
|
| |
| struct callable_cache { |
| PyObject *isinstance; |
| PyObject *len; |
| PyObject *list_append; |
| PyObject *object__getattribute__; |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| #define CO_FAST_HIDDEN 0x10 |
| #define CO_FAST_LOCAL 0x20 |
| #define CO_FAST_CELL 0x40 |
| #define CO_FAST_FREE 0x80 |
|
|
| typedef unsigned char _PyLocals_Kind; |
|
|
| static inline _PyLocals_Kind |
| _PyLocals_GetKind(PyObject *kinds, int i) |
| { |
| assert(PyBytes_Check(kinds)); |
| assert(0 <= i && i < PyBytes_GET_SIZE(kinds)); |
| char *ptr = PyBytes_AS_STRING(kinds); |
| return (_PyLocals_Kind)(ptr[i]); |
| } |
|
|
| static inline void |
| _PyLocals_SetKind(PyObject *kinds, int i, _PyLocals_Kind kind) |
| { |
| assert(PyBytes_Check(kinds)); |
| assert(0 <= i && i < PyBytes_GET_SIZE(kinds)); |
| char *ptr = PyBytes_AS_STRING(kinds); |
| ptr[i] = (char) kind; |
| } |
|
|
|
|
| struct _PyCodeConstructor { |
| |
| PyObject *filename; |
| PyObject *name; |
| PyObject *qualname; |
| int flags; |
|
|
| |
| PyObject *code; |
| int firstlineno; |
| PyObject *linetable; |
|
|
| |
| PyObject *consts; |
| PyObject *names; |
|
|
| |
| PyObject *localsplusnames; |
| PyObject *localspluskinds; |
|
|
| |
| int argcount; |
| int posonlyargcount; |
| |
| int kwonlyargcount; |
|
|
| |
| int stacksize; |
|
|
| |
| PyObject *exceptiontable; |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| extern int _PyCode_Validate(struct _PyCodeConstructor *); |
| extern PyCodeObject* _PyCode_New(struct _PyCodeConstructor *); |
|
|
|
|
| |
|
|
| |
| extern PyObject* _PyCode_GetVarnames(PyCodeObject *); |
| extern PyObject* _PyCode_GetCellvars(PyCodeObject *); |
| extern PyObject* _PyCode_GetFreevars(PyCodeObject *); |
| extern PyObject* _PyCode_GetCode(PyCodeObject *); |
|
|
| |
| extern int _PyCode_InitAddressRange(PyCodeObject* co, PyCodeAddressRange *bounds); |
|
|
| |
| extern void _PyLineTable_InitAddressRange( |
| const char *linetable, |
| Py_ssize_t length, |
| int firstlineno, |
| PyCodeAddressRange *range); |
|
|
| |
| extern int _PyLineTable_NextAddressRange(PyCodeAddressRange *range); |
| extern int _PyLineTable_PreviousAddressRange(PyCodeAddressRange *range); |
|
|
| |
| extern void _PyCode_Clear_Executors(PyCodeObject *code); |
|
|
| #ifdef Py_GIL_DISABLED |
| |
| #define ENABLE_SPECIALIZATION 0 |
| #else |
| #define ENABLE_SPECIALIZATION 1 |
| #endif |
|
|
| |
|
|
| extern void _Py_Specialize_LoadSuperAttr(PyObject *global_super, PyObject *cls, |
| _Py_CODEUNIT *instr, int load_method); |
| extern void _Py_Specialize_LoadAttr(PyObject *owner, _Py_CODEUNIT *instr, |
| PyObject *name); |
| extern void _Py_Specialize_StoreAttr(PyObject *owner, _Py_CODEUNIT *instr, |
| PyObject *name); |
| extern void _Py_Specialize_LoadGlobal(PyObject *globals, PyObject *builtins, |
| _Py_CODEUNIT *instr, PyObject *name); |
| extern void _Py_Specialize_BinarySubscr(PyObject *sub, PyObject *container, |
| _Py_CODEUNIT *instr); |
| extern void _Py_Specialize_StoreSubscr(PyObject *container, PyObject *sub, |
| _Py_CODEUNIT *instr); |
| extern void _Py_Specialize_Call(PyObject *callable, _Py_CODEUNIT *instr, |
| int nargs); |
| extern void _Py_Specialize_BinaryOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr, |
| int oparg, PyObject **locals); |
| extern void _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs, |
| _Py_CODEUNIT *instr, int oparg); |
| extern void _Py_Specialize_UnpackSequence(PyObject *seq, _Py_CODEUNIT *instr, |
| int oparg); |
| extern void _Py_Specialize_ForIter(PyObject *iter, _Py_CODEUNIT *instr, int oparg); |
| extern void _Py_Specialize_Send(PyObject *receiver, _Py_CODEUNIT *instr); |
| extern void _Py_Specialize_ToBool(PyObject *value, _Py_CODEUNIT *instr); |
| extern void _Py_Specialize_ContainsOp(PyObject *value, _Py_CODEUNIT *instr); |
|
|
| #ifdef Py_STATS |
|
|
| #include "pycore_bitutils.h" |
|
|
| #define STAT_INC(opname, name) do { if (_Py_stats) _Py_stats->opcode_stats[opname].specialization.name++; } while (0) |
| #define STAT_DEC(opname, name) do { if (_Py_stats) _Py_stats->opcode_stats[opname].specialization.name--; } while (0) |
| #define OPCODE_EXE_INC(opname) do { if (_Py_stats) _Py_stats->opcode_stats[opname].execution_count++; } while (0) |
| #define CALL_STAT_INC(name) do { if (_Py_stats) _Py_stats->call_stats.name++; } while (0) |
| #define OBJECT_STAT_INC(name) do { if (_Py_stats) _Py_stats->object_stats.name++; } while (0) |
| #define OBJECT_STAT_INC_COND(name, cond) \ |
| do { if (_Py_stats && cond) _Py_stats->object_stats.name++; } while (0) |
| #define EVAL_CALL_STAT_INC(name) do { if (_Py_stats) _Py_stats->call_stats.eval_calls[name]++; } while (0) |
| #define EVAL_CALL_STAT_INC_IF_FUNCTION(name, callable) \ |
| do { if (_Py_stats && PyFunction_Check(callable)) _Py_stats->call_stats.eval_calls[name]++; } while (0) |
| #define GC_STAT_ADD(gen, name, n) do { if (_Py_stats) _Py_stats->gc_stats[(gen)].name += (n); } while (0) |
| #define OPT_STAT_INC(name) do { if (_Py_stats) _Py_stats->optimization_stats.name++; } while (0) |
| #define UOP_STAT_INC(opname, name) do { if (_Py_stats) { assert(opname < 512); _Py_stats->optimization_stats.opcode[opname].name++; } } while (0) |
| #define UOP_PAIR_INC(uopcode, lastuop) \ |
| do { \ |
| if (lastuop && _Py_stats) { \ |
| _Py_stats->optimization_stats.opcode[lastuop].pair_count[uopcode]++; \ |
| } \ |
| lastuop = uopcode; \ |
| } while (0) |
| #define OPT_UNSUPPORTED_OPCODE(opname) do { if (_Py_stats) _Py_stats->optimization_stats.unsupported_opcode[opname]++; } while (0) |
| #define OPT_ERROR_IN_OPCODE(opname) do { if (_Py_stats) _Py_stats->optimization_stats.error_in_opcode[opname]++; } while (0) |
| #define OPT_HIST(length, name) \ |
| do { \ |
| if (_Py_stats) { \ |
| int bucket = _Py_bit_length(length >= 1 ? length - 1 : 0); \ |
| bucket = (bucket >= _Py_UOP_HIST_SIZE) ? _Py_UOP_HIST_SIZE - 1 : bucket; \ |
| _Py_stats->optimization_stats.name[bucket]++; \ |
| } \ |
| } while (0) |
| #define RARE_EVENT_STAT_INC(name) do { if (_Py_stats) _Py_stats->rare_event_stats.name++; } while (0) |
|
|
| |
| PyAPI_FUNC(PyObject*) _Py_GetSpecializationStats(void); |
|
|
| #else |
| #define STAT_INC(opname, name) ((void)0) |
| #define STAT_DEC(opname, name) ((void)0) |
| #define OPCODE_EXE_INC(opname) ((void)0) |
| #define CALL_STAT_INC(name) ((void)0) |
| #define OBJECT_STAT_INC(name) ((void)0) |
| #define OBJECT_STAT_INC_COND(name, cond) ((void)0) |
| #define EVAL_CALL_STAT_INC(name) ((void)0) |
| #define EVAL_CALL_STAT_INC_IF_FUNCTION(name, callable) ((void)0) |
| #define GC_STAT_ADD(gen, name, n) ((void)0) |
| #define OPT_STAT_INC(name) ((void)0) |
| #define UOP_STAT_INC(opname, name) ((void)0) |
| #define UOP_PAIR_INC(uopcode, lastuop) ((void)0) |
| #define OPT_UNSUPPORTED_OPCODE(opname) ((void)0) |
| #define OPT_ERROR_IN_OPCODE(opname) ((void)0) |
| #define OPT_HIST(length, name) ((void)0) |
| #define RARE_EVENT_STAT_INC(name) ((void)0) |
| #endif |
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
|
|
| static inline void |
| write_u32(uint16_t *p, uint32_t val) |
| { |
| memcpy(p, &val, sizeof(val)); |
| } |
|
|
| static inline void |
| write_u64(uint16_t *p, uint64_t val) |
| { |
| memcpy(p, &val, sizeof(val)); |
| } |
|
|
| static inline void |
| write_obj(uint16_t *p, PyObject *val) |
| { |
| memcpy(p, &val, sizeof(val)); |
| } |
|
|
| static inline uint16_t |
| read_u16(uint16_t *p) |
| { |
| return *p; |
| } |
|
|
| static inline uint32_t |
| read_u32(uint16_t *p) |
| { |
| uint32_t val; |
| memcpy(&val, p, sizeof(val)); |
| return val; |
| } |
|
|
| static inline uint64_t |
| read_u64(uint16_t *p) |
| { |
| uint64_t val; |
| memcpy(&val, p, sizeof(val)); |
| return val; |
| } |
|
|
| static inline PyObject * |
| read_obj(uint16_t *p) |
| { |
| PyObject *val; |
| memcpy(&val, p, sizeof(val)); |
| return val; |
| } |
|
|
| |
| |
| static inline unsigned char * |
| parse_varint(unsigned char *p, int *result) { |
| int val = p[0] & 63; |
| while (p[0] & 64) { |
| p++; |
| val = (val << 6) | (p[0] & 63); |
| } |
| *result = val; |
| return p+1; |
| } |
|
|
| static inline int |
| write_varint(uint8_t *ptr, unsigned int val) |
| { |
| int written = 1; |
| while (val >= 64) { |
| *ptr++ = 64 | (val & 63); |
| val >>= 6; |
| written++; |
| } |
| *ptr = (uint8_t)val; |
| return written; |
| } |
|
|
| static inline int |
| write_signed_varint(uint8_t *ptr, int val) |
| { |
| unsigned int uval; |
| if (val < 0) { |
| |
| uval = ((0 - (unsigned int)val) << 1) | 1; |
| } |
| else { |
| uval = (unsigned int)val << 1; |
| } |
| return write_varint(ptr, uval); |
| } |
|
|
| static inline int |
| write_location_entry_start(uint8_t *ptr, int code, int length) |
| { |
| assert((code & 15) == code); |
| *ptr = 128 | (uint8_t)(code << 3) | (uint8_t)(length - 1); |
| return 1; |
| } |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include "pycore_backoff.h" |
|
|
| |
| |
| |
| |
| #define ADAPTIVE_WARMUP_VALUE 1 |
| #define ADAPTIVE_WARMUP_BACKOFF 1 |
|
|
| |
| |
| |
| |
| |
| |
| #define ADAPTIVE_COOLDOWN_VALUE 52 |
| #define ADAPTIVE_COOLDOWN_BACKOFF 0 |
|
|
| |
| #if COLD_EXIT_INITIAL_VALUE <= ADAPTIVE_COOLDOWN_VALUE |
| # error "Cold exit value should be larger than adaptive cooldown value" |
| #endif |
|
|
| static inline _Py_BackoffCounter |
| adaptive_counter_bits(uint16_t value, uint16_t backoff) { |
| return make_backoff_counter(value, backoff); |
| } |
|
|
| static inline _Py_BackoffCounter |
| adaptive_counter_warmup(void) { |
| return adaptive_counter_bits(ADAPTIVE_WARMUP_VALUE, |
| ADAPTIVE_WARMUP_BACKOFF); |
| } |
|
|
| static inline _Py_BackoffCounter |
| adaptive_counter_cooldown(void) { |
| return adaptive_counter_bits(ADAPTIVE_COOLDOWN_VALUE, |
| ADAPTIVE_COOLDOWN_BACKOFF); |
| } |
|
|
| static inline _Py_BackoffCounter |
| adaptive_counter_backoff(_Py_BackoffCounter counter) { |
| return restart_backoff_counter(counter); |
| } |
|
|
|
|
| |
|
|
| |
| #define COMPARISON_BIT(x, y) (1 << (2 * ((x) >= (y)) + ((x) <= (y)))) |
|
|
| |
| |
| |
| |
| |
|
|
| |
| #define COMPARISON_UNORDERED 1 |
|
|
| #define COMPARISON_LESS_THAN 2 |
| #define COMPARISON_GREATER_THAN 4 |
| #define COMPARISON_EQUALS 8 |
|
|
| #define COMPARISON_NOT_EQUALS (COMPARISON_UNORDERED | COMPARISON_LESS_THAN | COMPARISON_GREATER_THAN) |
|
|
| extern int _Py_Instrument(PyCodeObject *co, PyInterpreterState *interp); |
|
|
| extern int _Py_GetBaseOpcode(PyCodeObject *code, int offset); |
|
|
| extern int _PyInstruction_GetLength(PyCodeObject *code, int offset); |
|
|
| #ifdef __cplusplus |
| } |
| #endif |
| #endif |
|
|