// Macros and other things needed by ceval.c, and bytecodes.c /* Computed GOTOs, or the-optimization-commonly-but-improperly-known-as-"threaded code" using gcc's labels-as-values extension (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html). The traditional bytecode evaluation loop uses a "switch" statement, which decent compilers will optimize as a single indirect branch instruction combined with a lookup table of jump addresses. However, since the indirect jump instruction is shared by all opcodes, the CPU will have a hard time making the right prediction for where to jump next (actually, it will be always wrong except in the uncommon case of a sequence of several identical opcodes). "Threaded code" in contrast, uses an explicit jump table and an explicit indirect jump instruction at the end of each opcode. Since the jump instruction is at a different address for each opcode, the CPU will make a separate prediction for each of these instructions, which is equivalent to predicting the second opcode of each opcode pair. These predictions have a much better chance to turn out valid, especially in small bytecode loops. A mispredicted branch on a modern CPU flushes the whole pipeline and can cost several CPU cycles (depending on the pipeline depth), and potentially many more instructions (depending on the pipeline width). A correctly predicted branch, however, is nearly free. At the time of this writing, the "threaded code" version is up to 15-20% faster than the normal "switch" version, depending on the compiler and the CPU architecture. NOTE: care must be taken that the compiler doesn't try to "optimize" the indirect jumps by sharing them between all opcodes. Such optimizations can be disabled on gcc by using the -fno-gcse flag (or possibly -fno-crossjumping). */ /* Use macros rather than inline functions, to make it as clear as possible * to the C compiler that the tracing check is a simple test then branch. * We want to be sure that the compiler knows this before it generates * the CFG. */ #ifdef WITH_DTRACE #define OR_DTRACE_LINE … #else #define OR_DTRACE_LINE #endif #ifdef HAVE_COMPUTED_GOTOS #ifndef USE_COMPUTED_GOTOS #define USE_COMPUTED_GOTOS … #endif #else #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS #error "Computed gotos are not supported on this compiler." #endif #undef USE_COMPUTED_GOTOS #define USE_COMPUTED_GOTOS … #endif #ifdef Py_STATS #define INSTRUCTION_STATS … #else #define INSTRUCTION_STATS(op) … #endif #if USE_COMPUTED_GOTOS #define TARGET(op) … #define DISPATCH_GOTO() … #else #define TARGET … #define DISPATCH_GOTO … #endif /* PRE_DISPATCH_GOTO() does lltrace if enabled. Normally a no-op */ #ifdef LLTRACE #define PRE_DISPATCH_GOTO … #else #define PRE_DISPATCH_GOTO() … #endif #if LLTRACE #define LLTRACE_RESUME_FRAME … #else #define LLTRACE_RESUME_FRAME() … #endif #ifdef Py_GIL_DISABLED #define QSBR_QUIESCENT_STATE … #else #define QSBR_QUIESCENT_STATE(tstate) … #endif /* Do interpreter dispatch accounting for tracing and instrumentation */ #define DISPATCH() … #define DISPATCH_SAME_OPARG() … #define DISPATCH_INLINED(NEW_FRAME) … // Use this instead of 'goto error' so Tier 2 can go to a different label #define GOTO_ERROR(LABEL) … /* Tuple access macros */ #ifndef Py_DEBUG #define GETITEM(v, i) … #else static inline PyObject * GETITEM(PyObject *v, Py_ssize_t i) { assert(PyTuple_Check(v)); assert(i >= 0); assert(i < PyTuple_GET_SIZE(v)); return PyTuple_GET_ITEM(v, i); } #endif /* Code access macros */ /* The integer overflow is checked by an assertion below. */ #define INSTR_OFFSET() … #define NEXTOPARG() … /* JUMPBY makes the generator identify the instruction as a jump. SKIP_OVER is * for advancing to the next instruction, taking into account cache entries * and skipped instructions. */ #define JUMPBY(x) … #define SKIP_OVER(x) … /* OpCode prediction macros Some opcodes tend to come in pairs thus making it possible to predict the second code when the first is run. For example, COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE. Verifying the prediction costs a single high-speed test of a register variable against a constant. If the pairing was good, then the processor's own internal branch predication has a high likelihood of success, resulting in a nearly zero-overhead transition to the next opcode. A successful prediction saves a trip through the eval-loop including its unpredictable switch-case branch. Combined with the processor's internal branch prediction, a successful PREDICT has the effect of making the two opcodes run as if they were a single new opcode with the bodies combined. If collecting opcode statistics, your choices are to either keep the predictions turned-on and interpret the results as if some opcodes had been combined or turn-off predictions so that the opcode frequency counter updates for both opcodes. Opcode prediction is disabled with threaded code, since the latter allows the CPU to record separate branch prediction information for each opcode. */ #define PREDICT_ID(op) … #define PREDICTED(op) … /* Stack manipulation macros */ /* The stack can grow at most MAXINT deep, as co_nlocals and co_stacksize are ints. */ #define STACK_LEVEL() … #define STACK_SIZE() … #define EMPTY() … #define TOP() … #define SECOND() … #define THIRD() … #define FOURTH() … #define PEEK(n) … #define POKE(n, v) … #define SET_TOP(v) … #define SET_SECOND(v) … #define BASIC_STACKADJ(n) … #define BASIC_PUSH(v) … #define BASIC_POP() … #ifdef Py_DEBUG #define PUSH … #define POP … #define STACK_GROW … #define STACK_SHRINK … #else #define PUSH(v) … #define POP() … #define STACK_GROW(n) … #define STACK_SHRINK(n) … #endif #define WITHIN_STACK_BOUNDS() … /* Data access macros */ #define FRAME_CO_CONSTS … #define FRAME_CO_NAMES … /* Local variable macros */ #define LOCALS_ARRAY … #define GETLOCAL(i) … /* The SETLOCAL() macro must not DECREF the local variable in-place and then store the new value; it must copy the old value to a temporary value, then store the new value, and then DECREF the temporary value. This is because it is possible that during the DECREF the frame is accessed by other code (e.g. a __del__ method or gc.collect()) and the variable would be pointing to already-freed memory. */ #define SETLOCAL(i, value) … #define GO_TO_INSTRUCTION(op) … #ifdef Py_STATS #define UPDATE_MISS_STATS … #else #define UPDATE_MISS_STATS(INSTNAME) … #endif #define DEOPT_IF(COND, INSTNAME) … // Try to lock an object in the free threading build, if it's not already // locked. Use with a DEOPT_IF() to deopt if the object is already locked. // These are no-ops in the default GIL build. The general pattern is: // // DEOPT_IF(!LOCK_OBJECT(op)); // if (/* condition fails */) { // UNLOCK_OBJECT(op); // DEOPT_IF(true); // } // ... // UNLOCK_OBJECT(op); // // NOTE: The object must be unlocked on every exit code path and you should // avoid any potentially escaping calls (like PyStackRef_CLOSE) while the // object is locked. #ifdef Py_GIL_DISABLED #define LOCK_OBJECT … #define UNLOCK_OBJECT … #else #define LOCK_OBJECT(op) … #define UNLOCK_OBJECT(op) … #endif #define GLOBALS() … #define BUILTINS() … #define LOCALS() … #define CONSTS() … #define NAMES() … #define DTRACE_FUNCTION_ENTRY() … /* This takes a uint16_t instead of a _Py_BackoffCounter, * because it is used directly on the cache entry in generated code, * which is always an integral type. */ #define ADAPTIVE_COUNTER_TRIGGERS(COUNTER) … #define ADVANCE_ADAPTIVE_COUNTER(COUNTER) … #define PAUSE_ADAPTIVE_COUNTER(COUNTER) … #ifdef ENABLE_SPECIALIZATION_FT /* Multiple threads may execute these concurrently if thread-local bytecode is * disabled and they all execute the main copy of the bytecode. Specialization * is disabled in that case so the value is unused, but the RMW cycle should be * free of data races. */ #define RECORD_BRANCH_TAKEN(bitset, flag) … #else #define RECORD_BRANCH_TAKEN … #endif #define UNBOUNDLOCAL_ERROR_MSG … #define UNBOUNDFREE_ERROR_MSG … #define NAME_ERROR_MSG … // If a trace function sets a new f_lineno and // *then* raises, we use the destination when searching // for an exception handler, displaying the traceback, and so on #define INSTRUMENTED_JUMP(src, dest, event) … static inline int _Py_EnterRecursivePy(PyThreadState *tstate) { … } static inline void _Py_LeaveRecursiveCallPy(PyThreadState *tstate) { … } /* Implementation of "macros" that modify the instruction pointer, * stack pointer, or frame pointer. * These need to treated differently by tier 1 and 2. * The Tier 1 version is here; Tier 2 is inlined in ceval.c. */ #define LOAD_IP(OFFSET) … /* There's no STORE_IP(), it's inlined by the code generator. */ #define LOAD_SP() … #define SAVE_SP() … /* Tier-switching macros. */ #ifdef _Py_JIT #define GOTO_TIER_TWO … #else #define GOTO_TIER_TWO(EXECUTOR) … #endif #define GOTO_TIER_ONE(TARGET) … #define CURRENT_OPARG() … #define CURRENT_OPERAND0() … #define CURRENT_OPERAND1() … #define JUMP_TO_JUMP_TARGET() … #define JUMP_TO_ERROR() … #define GOTO_UNWIND() … #define EXIT_TO_TIER1() … #define EXIT_TO_TIER1_DYNAMIC() … /* Stackref macros */ /* How much scratch space to give stackref to PyObject* conversion. */ #define MAX_STACKREF_SCRATCH … #ifdef Py_GIL_DISABLED #define STACKREFS_TO_PYOBJECTS … #else #define STACKREFS_TO_PYOBJECTS(ARGS, ARG_COUNT, NAME) … #endif #ifdef Py_GIL_DISABLED #define STACKREFS_TO_PYOBJECTS_CLEANUP … #else #define STACKREFS_TO_PYOBJECTS_CLEANUP(NAME) … #endif #ifdef Py_GIL_DISABLED #define CONVERSION_FAILED … #else #define CONVERSION_FAILED(NAME) … #endif