Coverage Report

Created: 2026-10-17 06:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Python/ceval_macros.h
Line
Count
Source
1
// Macros and other things needed by ceval.c, and bytecodes.c
2
3
/* Computed GOTOs, or
4
       the-optimization-commonly-but-improperly-known-as-"threaded code"
5
   using gcc's labels-as-values extension
6
   (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
7
8
   The traditional bytecode evaluation loop uses a "switch" statement, which
9
   decent compilers will optimize as a single indirect branch instruction
10
   combined with a lookup table of jump addresses. However, since the
11
   indirect jump instruction is shared by all opcodes, the CPU will have a
12
   hard time making the right prediction for where to jump next (actually,
13
   it will be always wrong except in the uncommon case of a sequence of
14
   several identical opcodes).
15
16
   "Threaded code" in contrast, uses an explicit jump table and an explicit
17
   indirect jump instruction at the end of each opcode. Since the jump
18
   instruction is at a different address for each opcode, the CPU will make a
19
   separate prediction for each of these instructions, which is equivalent to
20
   predicting the second opcode of each opcode pair. These predictions have
21
   a much better chance to turn out valid, especially in small bytecode loops.
22
23
   A mispredicted branch on a modern CPU flushes the whole pipeline and
24
   can cost several CPU cycles (depending on the pipeline depth),
25
   and potentially many more instructions (depending on the pipeline width).
26
   A correctly predicted branch, however, is nearly free.
27
28
   At the time of this writing, the "threaded code" version is up to 15-20%
29
   faster than the normal "switch" version, depending on the compiler and the
30
   CPU architecture.
31
32
   NOTE: care must be taken that the compiler doesn't try to "optimize" the
33
   indirect jumps by sharing them between all opcodes. Such optimizations
34
   can be disabled on gcc by using the -fno-gcse flag (or possibly
35
   -fno-crossjumping).
36
*/
37
38
/* Use macros rather than inline functions, to make it as clear as possible
39
 * to the C compiler that the tracing check is a simple test then branch.
40
 * We want to be sure that the compiler knows this before it generates
41
 * the CFG.
42
 */
43
44
#ifdef WITH_DTRACE
45
#define OR_DTRACE_LINE | (PyDTrace_LINE_ENABLED() ? 255 : 0)
46
#else
47
#define OR_DTRACE_LINE
48
#endif
49
50
#ifdef HAVE_COMPUTED_GOTOS
51
    #ifndef USE_COMPUTED_GOTOS
52
    #define USE_COMPUTED_GOTOS 1
53
    #endif
54
#else
55
    #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
56
    #error "Computed gotos are not supported on this compiler."
57
    #endif
58
    #undef USE_COMPUTED_GOTOS
59
    #define USE_COMPUTED_GOTOS 0
60
#endif
61
62
#ifdef Py_STATS
63
#define INSTRUCTION_STATS(op) \
64
    do { \
65
        PyStats *s = _PyStats_GET(); \
66
        OPCODE_EXE_INC(op); \
67
        if (s) s->opcode_stats[lastopcode].pair_count[op]++; \
68
        lastopcode = op; \
69
    } while (0)
70
#else
71
46.6G
#define INSTRUCTION_STATS(op) ((void)0)
72
#endif
73
74
#ifdef Py_STATS
75
#   define TAIL_CALL_PARAMS _PyInterpreterFrame *frame, _PyStackRef *stack_pointer, PyThreadState *tstate, _Py_CODEUNIT *next_instr, const void *instruction_funcptr_table, int oparg, int lastopcode
76
#   define TAIL_CALL_ARGS frame, stack_pointer, tstate, next_instr, instruction_funcptr_table, oparg, lastopcode
77
#else
78
#   define TAIL_CALL_PARAMS _PyInterpreterFrame *frame, _PyStackRef *stack_pointer, PyThreadState *tstate, _Py_CODEUNIT *next_instr, const void *instruction_funcptr_table, int oparg
79
#   define TAIL_CALL_ARGS frame, stack_pointer, tstate, next_instr, instruction_funcptr_table, oparg
80
#endif
81
82
#if _Py_TAIL_CALL_INTERP
83
#   if defined(__clang__) || defined(__GNUC__)
84
#       if !_Py__has_attribute(preserve_none) || !_Py__has_attribute(musttail)
85
#           error "This compiler does not have support for efficient tail calling."
86
#       endif
87
#   elif defined(_MSC_VER) && (_MSC_VER < 1950)
88
#       error "You need at least VS 2026 / PlatformToolset v145 for tail calling."
89
#   endif
90
#   if defined(_MSC_VER) && !defined(__clang__)
91
#      define Py_MUSTTAIL [[msvc::musttail]]
92
#      define Py_PRESERVE_NONE_CC __preserve_none
93
#   else
94
#       define Py_MUSTTAIL __attribute__((musttail))
95
#       define Py_PRESERVE_NONE_CC __attribute__((preserve_none))
96
#   endif
97
    typedef PyObject *(Py_PRESERVE_NONE_CC *py_tail_call_funcptr)(TAIL_CALL_PARAMS);
98
99
#   define DISPATCH_TABLE_VAR instruction_funcptr_table
100
#   define DISPATCH_TABLE instruction_funcptr_handler_table
101
#   define TRACING_DISPATCH_TABLE instruction_funcptr_tracing_table
102
#   define TARGET(op) Py_NO_INLINE PyObject *Py_PRESERVE_NONE_CC _TAIL_CALL_##op(TAIL_CALL_PARAMS)
103
104
#   define DISPATCH_GOTO() \
105
        do { \
106
            Py_MUSTTAIL return (((py_tail_call_funcptr *)instruction_funcptr_table)[opcode])(TAIL_CALL_ARGS); \
107
        } while (0)
108
#   define DISPATCH_GOTO_NON_TRACING() \
109
        do { \
110
            Py_MUSTTAIL return (((py_tail_call_funcptr *)DISPATCH_TABLE)[opcode])(TAIL_CALL_ARGS); \
111
        } while (0)
112
#   define JUMP_TO_LABEL(name) \
113
        do { \
114
            Py_MUSTTAIL return (_TAIL_CALL_##name)(TAIL_CALL_ARGS); \
115
        } while (0)
116
#   ifdef Py_STATS
117
#       define JUMP_TO_PREDICTED(name) \
118
            do { \
119
                Py_MUSTTAIL return (_TAIL_CALL_##name)(frame, stack_pointer, tstate, this_instr, instruction_funcptr_table, oparg, lastopcode); \
120
            } while (0)
121
#   else
122
#       define JUMP_TO_PREDICTED(name) \
123
            do { \
124
                Py_MUSTTAIL return (_TAIL_CALL_##name)(frame, stack_pointer, tstate, this_instr, instruction_funcptr_table, oparg); \
125
            } while (0)
126
#   endif
127
#    define LABEL(name) TARGET(name)
128
#elif USE_COMPUTED_GOTOS
129
#  define DISPATCH_TABLE_VAR opcode_targets
130
6.42M
#  define DISPATCH_TABLE opcode_targets_table
131
#  define TRACING_DISPATCH_TABLE opcode_tracing_targets_table
132
46.6G
#  define TARGET(op) TARGET_##op:
133
46.8G
#  define DISPATCH_GOTO() goto *opcode_targets[opcode]
134
6.42M
#  define DISPATCH_GOTO_NON_TRACING() goto *DISPATCH_TABLE[opcode];
135
153M
#  define JUMP_TO_LABEL(name) goto name;
136
239M
#  define JUMP_TO_PREDICTED(name) goto PREDICTED_##name;
137
477M
#  define LABEL(name) name:
138
#else
139
#  define TARGET(op) case op: TARGET_##op:
140
#  define DISPATCH_GOTO() dispatch_code = opcode | tracing_mode ; goto dispatch_opcode
141
#  define DISPATCH_GOTO_NON_TRACING() dispatch_code = opcode; goto dispatch_opcode
142
#  define JUMP_TO_LABEL(name) goto name;
143
#  define JUMP_TO_PREDICTED(name) goto PREDICTED_##name;
144
#  define LABEL(name) name:
145
#endif
146
147
#if (_Py_TAIL_CALL_INTERP || USE_COMPUTED_GOTOS) && _Py_TIER2
148
#  define IS_JIT_TRACING() (DISPATCH_TABLE_VAR == TRACING_DISPATCH_TABLE)
149
#  define ENTER_TRACING() \
150
    DISPATCH_TABLE_VAR = TRACING_DISPATCH_TABLE;
151
#  define LEAVE_TRACING() \
152
    DISPATCH_TABLE_VAR = DISPATCH_TABLE;
153
#else
154
#  define IS_JIT_TRACING() (tracing_mode != 0)
155
#  define ENTER_TRACING() tracing_mode = 255
156
#  define LEAVE_TRACING() tracing_mode = 0
157
#endif
158
159
#if _Py_TIER2
160
#define STOP_TRACING() \
161
    do { \
162
        if (IS_JIT_TRACING()) { \
163
            LEAVE_TRACING(); \
164
            _PyJit_FinalizeTracing(tstate, 0); \
165
        } \
166
    } while (0);
167
#else
168
133M
#define STOP_TRACING() ((void)(0));
169
#endif
170
171
/* PRE_DISPATCH_GOTO() does lltrace if enabled. Normally a no-op */
172
#ifdef Py_DEBUG
173
#define PRE_DISPATCH_GOTO() if (frame->lltrace >= 5) { \
174
    lltrace_instruction(frame, stack_pointer, next_instr, opcode, oparg); }
175
#else
176
46.8G
#define PRE_DISPATCH_GOTO() ((void)0)
177
#endif
178
179
#ifdef Py_DEBUG
180
#define LLTRACE_RESUME_FRAME() \
181
do { \
182
    _PyFrame_SetStackPointer(frame, stack_pointer); \
183
    int lltrace = maybe_lltrace_resume_frame(frame, GLOBALS()); \
184
    stack_pointer = _PyFrame_GetStackPointer(frame); \
185
    frame->lltrace = lltrace; \
186
} while (0)
187
#else
188
2.69G
#define LLTRACE_RESUME_FRAME() ((void)0)
189
#endif
190
191
#ifdef Py_GIL_DISABLED
192
#define QSBR_QUIESCENT_STATE(tstate) _Py_qsbr_quiescent_state(((_PyThreadStateImpl *)tstate)->qsbr)
193
#else
194
#define QSBR_QUIESCENT_STATE(tstate)
195
#endif
196
197
198
/* Do interpreter dispatch accounting for tracing and instrumentation */
199
#define DISPATCH() \
200
46.5G
    { \
201
46.5G
        _PyFrame_StackAssertInvalid(frame); \
202
46.5G
        NEXTOPARG(); \
203
46.5G
        PRE_DISPATCH_GOTO(); \
204
46.5G
        DISPATCH_GOTO(); \
205
46.5G
    }
206
207
#define DISPATCH_NON_TRACING() \
208
    { \
209
        _PyFrame_StackAssertInvalid(frame); \
210
        NEXTOPARG(); \
211
        PRE_DISPATCH_GOTO(); \
212
        DISPATCH_GOTO_NON_TRACING(); \
213
    }
214
215
#define DISPATCH_SAME_OPARG() \
216
6.42M
    { \
217
6.42M
        opcode = next_instr->op.code; \
218
6.42M
        PRE_DISPATCH_GOTO(); \
219
6.42M
        DISPATCH_GOTO_NON_TRACING(); \
220
6.42M
    }
221
222
#define DISPATCH_INLINED(NEW_FRAME)                              \
223
2.20M
    do {                                                         \
224
2.20M
        assert(!IS_PEP523_HOOKED(tstate));                       \
225
2.20M
        _PyFrame_SetStackPointer(frame, stack_pointer);          \
226
2.20M
        _PyFrame_StackPointerValidate(frame);                    \
227
2.20M
        assert((NEW_FRAME)->previous == frame);                  \
228
2.20M
        frame = tstate->current_frame = (NEW_FRAME);             \
229
2.20M
        CALL_STAT_INC(inlined_py_calls);                         \
230
2.20M
        JUMP_TO_LABEL(start_frame);                              \
231
0
    } while (0)
232
233
/* Tuple access macros */
234
235
#ifndef Py_DEBUG
236
2.07G
#define GETITEM(v, i) PyTuple_GET_ITEM((v), (i))
237
#else
238
static inline PyObject *
239
GETITEM(PyObject *v, Py_ssize_t i) {
240
    assert(PyTuple_Check(v));
241
    assert(i >= 0);
242
    assert(i < PyTuple_GET_SIZE(v));
243
    return PyTuple_GET_ITEM(v, i);
244
}
245
#endif
246
247
/* Code access macros */
248
249
/* The integer overflow is checked by an assertion below. */
250
69.4M
#define INSTR_OFFSET() ((int)(next_instr - _PyFrame_GetBytecode(frame)))
251
46.5G
#define NEXTOPARG()  do { \
252
46.5G
        _Py_CODEUNIT word  = {.cache = FT_ATOMIC_LOAD_UINT16_RELAXED(*(uint16_t*)next_instr)}; \
253
46.5G
        opcode = word.op.code; \
254
46.5G
        oparg = word.op.arg; \
255
46.5G
    } while (0)
256
257
/* JUMPBY makes the generator identify the instruction as a jump. SKIP_OVER is
258
 * for advancing to the next instruction, taking into account cache entries
259
 * and skipped instructions.
260
 */
261
7.20G
#define JUMPBY(x)       (next_instr += (x))
262
#define SKIP_OVER(x)    (next_instr += (x))
263
264
#define STACK_LEVEL()     ((int)(stack_pointer - _PyFrame_Stackbase(frame)))
265
#define STACK_SIZE()      (_PyFrame_GetCode(frame)->co_stacksize)
266
267
#define WITHIN_STACK_BOUNDS() \
268
   (frame->owner == FRAME_OWNED_BY_INTERPRETER || (STACK_LEVEL() >= 0 && STACK_LEVEL() <= STACK_SIZE()))
269
270
#if defined(Py_DEBUG) && !defined(_Py_JIT)
271
// This allows temporary stack "overflows", provided it's all in the cache at any point of time.
272
#define ASSERT_WITHIN_STACK_BOUNDS_IGNORING_CACHE(F, L) \
273
   assert(frame->owner == FRAME_OWNED_BY_INTERPRETER || (STACK_LEVEL() >= 0 && (STACK_LEVEL()) <= STACK_SIZE()))
274
#else
275
#define ASSERT_WITHIN_STACK_BOUNDS_IGNORING_CACHE ASSERT_WITHIN_STACK_BOUNDS
276
#endif
277
278
/* Data access macros */
279
#define FRAME_CO_CONSTS (_PyFrame_GetCode(frame)->co_consts)
280
#define FRAME_CO_NAMES  (_PyFrame_GetCode(frame)->co_names)
281
282
/* Local variable macros */
283
284
1.20M
#define LOCALS_ARRAY    (frame->localsplus)
285
20.5G
#define GETLOCAL(i)     (frame->localsplus[i])
286
287
288
#ifdef Py_STATS
289
#define UPDATE_MISS_STATS(INSTNAME)                              \
290
    do {                                                         \
291
        STAT_INC(opcode, miss);                                  \
292
        STAT_INC((INSTNAME), miss);                              \
293
        /* The counter is always the first cache entry: */       \
294
        if (ADAPTIVE_COUNTER_TRIGGERS(next_instr->cache)) {      \
295
            STAT_INC((INSTNAME), deopt);                         \
296
        }                                                        \
297
    } while (0)
298
#else
299
239M
#define UPDATE_MISS_STATS(INSTNAME) ((void)0)
300
#endif
301
302
303
// Try to lock an object in the free threading build, if it's not already
304
// locked. Use with a DEOPT_IF() to deopt if the object is already locked.
305
// These are no-ops in the default GIL build. The general pattern is:
306
//
307
// DEOPT_IF(!LOCK_OBJECT(op));
308
// if (/* condition fails */) {
309
//     UNLOCK_OBJECT(op);
310
//     DEOPT_IF(true);
311
//  }
312
//  ...
313
//  UNLOCK_OBJECT(op);
314
//
315
// NOTE: The object must be unlocked on every exit code path and you should
316
// avoid any potentially escaping calls (like PyStackRef_CLOSE) while the
317
// object is locked.
318
#ifdef Py_GIL_DISABLED
319
#  define LOCK_OBJECT(op) PyMutex_LockFast(&(_PyObject_CAST(op))->ob_mutex)
320
#  define UNLOCK_OBJECT(op) PyMutex_Unlock(&(_PyObject_CAST(op))->ob_mutex)
321
#else
322
694M
#  define LOCK_OBJECT(op) (1)
323
694M
#  define UNLOCK_OBJECT(op) ((void)0)
324
#endif
325
326
2.10G
#define GLOBALS() frame->f_globals
327
736M
#define BUILTINS() frame->f_builtins
328
3.13M
#define LOCALS() frame->f_locals
329
#define CONSTS() _PyFrame_GetCode(frame)->co_consts
330
#define NAMES() _PyFrame_GetCode(frame)->co_names
331
332
#if defined(WITH_DTRACE) && !defined(Py_BUILD_CORE_MODULE)
333
static void dtrace_function_entry(_PyInterpreterFrame *);
334
static void dtrace_function_return(_PyInterpreterFrame *);
335
336
#define DTRACE_FUNCTION_ENTRY()  \
337
    if (PyDTrace_FUNCTION_ENTRY_ENABLED()) { \
338
        dtrace_function_entry(frame); \
339
    }
340
341
#define DTRACE_FUNCTION_RETURN() \
342
    if (PyDTrace_FUNCTION_RETURN_ENABLED()) { \
343
        dtrace_function_return(frame); \
344
    }
345
#else
346
1.37G
#define DTRACE_FUNCTION_ENTRY() ((void)0)
347
1.33G
#define DTRACE_FUNCTION_RETURN() ((void)0)
348
#endif
349
350
/* This takes a uint16_t instead of a _Py_BackoffCounter,
351
 * because it is used directly on the cache entry in generated code,
352
 * which is always an integral type. */
353
// Force re-specialization when tracing a side exit to get good side exits.
354
#define ADAPTIVE_COUNTER_TRIGGERS(COUNTER) \
355
1.65G
    backoff_counter_triggers(forge_backoff_counter((COUNTER)))
356
357
#ifdef Py_GIL_DISABLED
358
/* Counters are unreachable when thread-local bytecode is disabled,
359
 * so there is no need to update them. */
360
#define ADVANCE_ADAPTIVE_COUNTER(COUNTER) \
361
    do { \
362
        _Py_BackoffCounter cnt = (COUNTER); \
363
        if (!backoff_counter_is_unreachable(cnt)) { \
364
            (COUNTER) = advance_backoff_counter(cnt); \
365
        } \
366
    } while (0);
367
368
#define PAUSE_ADAPTIVE_COUNTER(COUNTER) \
369
    do { \
370
        _Py_BackoffCounter cnt = (COUNTER); \
371
        if (!backoff_counter_is_unreachable(cnt)) { \
372
            (COUNTER) = pause_backoff_counter(cnt); \
373
        } \
374
    } while (0);
375
#else
376
#define ADVANCE_ADAPTIVE_COUNTER(COUNTER) \
377
1.65G
    do { \
378
1.65G
        (COUNTER) = advance_backoff_counter((COUNTER)); \
379
1.65G
    } while (0);
380
381
#define PAUSE_ADAPTIVE_COUNTER(COUNTER) \
382
0
    do { \
383
0
        (COUNTER) = pause_backoff_counter((COUNTER)); \
384
0
    } while (0);
385
#endif
386
387
#ifdef ENABLE_SPECIALIZATION
388
/* Multiple threads may execute these concurrently if thread-local bytecode is
389
 * disabled and they all execute the main copy of the bytecode. Specialization
390
 * is disabled in that case so the value is unused, but the RMW cycle should be
391
 * free of data races.
392
 */
393
#define RECORD_BRANCH_TAKEN(bitset, flag) \
394
3.60G
    FT_ATOMIC_STORE_UINT16_RELAXED(       \
395
3.60G
        bitset, (FT_ATOMIC_LOAD_UINT16_RELAXED(bitset) << 1) | (flag))
396
#else
397
#define RECORD_BRANCH_TAKEN(bitset, flag)
398
#endif
399
400
#define UNBOUNDLOCAL_ERROR_MSG \
401
0
    "cannot access local variable '%s' where it is not associated with a value"
402
#define UNBOUNDFREE_ERROR_MSG \
403
0
    "cannot access free variable '%s' where it is not associated with a value" \
404
0
    " in enclosing scope"
405
5
#define NAME_ERROR_MSG "name '%.200s' is not defined"
406
407
// If a trace function sets a new f_lineno and
408
// *then* raises, we use the destination when searching
409
// for an exception handler, displaying the traceback, and so on
410
0
#define INSTRUMENTED_JUMP(src, dest, event) \
411
0
do { \
412
0
    _Py_CODEUNIT *_dest = (dest); \
413
0
    if (tstate->tracing) {\
414
0
        next_instr = _dest; \
415
0
    } else { \
416
0
        _PyFrame_SetStackPointer(frame, stack_pointer); \
417
0
        next_instr = _Py_call_instrumentation_jump(this_instr, tstate, event, frame, src, _dest); \
418
0
        stack_pointer = _PyFrame_GetStackPointer(frame); \
419
0
        if (next_instr == NULL) { \
420
0
            next_instr = _dest + 1; \
421
0
            JUMP_TO_LABEL(error); \
422
0
        } \
423
0
    } \
424
0
} while (0);
425
426
427
263M
static inline int _Py_EnterRecursivePy(PyThreadState *tstate) {
428
263M
    return (tstate->py_recursion_remaining-- <= 0) &&
429
200
        _Py_CheckRecursiveCallPy(tstate);
430
263M
}
431
432
1.37G
static inline void _Py_LeaveRecursiveCallPy(PyThreadState *tstate)  {
433
1.37G
    tstate->py_recursion_remaining++;
434
1.37G
}
435
436
/* Implementation of "macros" that modify the instruction pointer,
437
 * stack pointer, or frame pointer.
438
 * These need to treated differently by tier 1 and 2.
439
 * The Tier 1 version is here; Tier 2 is inlined in ceval.c. */
440
441
2.45G
#define LOAD_IP(OFFSET) do { \
442
2.45G
        next_instr = frame->instr_ptr + (OFFSET); \
443
2.45G
    } while (0)
444
445
/* There's no STORE_IP(), it's inlined by the code generator. */
446
447
#define LOAD_SP() \
448
stack_pointer = _PyFrame_GetStackPointer(frame)
449
450
#define SAVE_SP() \
451
_PyFrame_SetStackPointer(frame, stack_pointer)
452
453
/* Tier-switching macros. */
454
455
#define TIER1_TO_TIER2(EXECUTOR)                        \
456
do {                                                   \
457
    OPT_STAT_INC(traces_executed);                     \
458
    next_instr = _Py_jit_entry((EXECUTOR), frame, stack_pointer, tstate); \
459
    frame = tstate->current_frame;                     \
460
    stack_pointer = _PyFrame_GetStackPointer(frame);   \
461
    int keep_tracing_bit = (uintptr_t)next_instr & 1;   \
462
    next_instr = (_Py_CODEUNIT *)(((uintptr_t)next_instr) & (~1)); \
463
    if (next_instr == NULL) {                          \
464
        /* gh-140104: The exception handler expects frame->instr_ptr
465
            to after this_instr, not this_instr! */ \
466
        next_instr = frame->instr_ptr + 1;                 \
467
        JUMP_TO_LABEL(error);                          \
468
    }                                                  \
469
    if (keep_tracing_bit) { \
470
        assert(uop_buffer_length(&((_PyThreadStateImpl *)tstate)->jit_tracer_state->code_buffer)); \
471
        ENTER_TRACING(); \
472
        DISPATCH_NON_TRACING(); \
473
    } \
474
    DISPATCH();                                        \
475
} while (0)
476
477
#define TIER2_TO_TIER2(EXECUTOR) \
478
do {                                                   \
479
    OPT_STAT_INC(traces_executed);                     \
480
    current_executor = (EXECUTOR);                     \
481
    goto tier2_start;                                  \
482
} while (0)
483
484
#define GOTO_TIER_ONE_SETUP \
485
    tstate->current_executor = NULL;                              \
486
    OPT_HIST(trace_uop_execution_counter, trace_run_length_hist); \
487
    _PyFrame_SetStackPointer(frame, stack_pointer);
488
489
#define GOTO_TIER_ONE(TARGET) \
490
    do \
491
    { \
492
        GOTO_TIER_ONE_SETUP \
493
        return (_Py_CODEUNIT *)(TARGET); \
494
    } while (0)
495
496
#define GOTO_TIER_ONE_CONTINUE_TRACING(TARGET) \
497
    do \
498
    { \
499
        GOTO_TIER_ONE_SETUP \
500
        return (_Py_CODEUNIT *)(((uintptr_t)(TARGET))| 1); \
501
    } while (0)
502
503
#define CURRENT_OPARG()    (next_uop[-1].oparg)
504
#define CURRENT_OPERAND0_64() (next_uop[-1].operand0)
505
#define CURRENT_OPERAND1_64() (next_uop[-1].operand1)
506
#define CURRENT_OPERAND0_32() (next_uop[-1].operand0)
507
#define CURRENT_OPERAND1_32() (next_uop[-1].operand1)
508
#define CURRENT_OPERAND0_16() (next_uop[-1].operand0)
509
#define CURRENT_OPERAND1_16() (next_uop[-1].operand1)
510
#define CURRENT_TARGET()   (next_uop[-1].target)
511
512
#define JUMP_TO_JUMP_TARGET() goto jump_to_jump_target
513
#define JUMP_TO_ERROR() goto jump_to_error_target
514
515
/* Stackref macros */
516
517
/* How much scratch space to give stackref to PyObject* conversion. */
518
1.03G
#define MAX_STACKREF_SCRATCH 10
519
520
#define STACKREFS_TO_PYOBJECTS(ARGS, ARG_COUNT, NAME) \
521
    /* +1 because vectorcall might use -1 to write self */ \
522
1.03G
    PyObject *NAME##_temp[MAX_STACKREF_SCRATCH+1]; \
523
1.03G
    PyObject **NAME = _PyObjectArray_FromStackRefArray(ARGS, ARG_COUNT, NAME##_temp);
524
525
#define STACKREFS_TO_PYOBJECTS_CLEANUP(NAME) \
526
    /* +1 because we +1 previously */ \
527
1.03G
    _PyObjectArray_Free(NAME - 1, NAME##_temp);
528
529
1.03G
#define CONVERSION_FAILED(NAME) ((NAME) == NULL)
530
531
#if defined(Py_DEBUG) && !defined(_Py_JIT)
532
#define SET_CURRENT_CACHED_VALUES(N) current_cached_values = (N)
533
#define CHECK_CURRENT_CACHED_VALUES(N) assert(current_cached_values == (N))
534
#else
535
#define SET_CURRENT_CACHED_VALUES(N) ((void)0)
536
#define CHECK_CURRENT_CACHED_VALUES(N) ((void)0)
537
#endif
538
539
1.14G
#define IS_PEP523_HOOKED(tstate) (tstate->interp->eval_frame != NULL)
540
541
static inline int
542
1.28G
check_periodics(PyThreadState *tstate) {
543
1.28G
    _Py_CHECK_EMSCRIPTEN_SIGNALS_PERIODICALLY();
544
1.28G
    QSBR_QUIESCENT_STATE(tstate);
545
1.28G
    if (_Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker) & _PY_EVAL_EVENTS_MASK) {
546
70.7k
        return _Py_HandlePending(tstate);
547
70.7k
    }
548
1.28G
    return 0;
549
1.28G
}
550
551
static inline int
552
1.49G
check_periodics_at_end(PyThreadState *tstate, _PyInterpreterFrame *frame) {
553
1.49G
    _Py_CHECK_EMSCRIPTEN_SIGNALS_PERIODICALLY();
554
1.49G
    QSBR_QUIESCENT_STATE(tstate);
555
1.49G
    if (_Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker) & _PY_EVAL_EVENTS_MASK) {
556
        // Do not handle pending interrupts if the previous instruction was LOAD_SPECIAL
557
        // This may also not handle interrupts if a cache looks like LOAD_SPECIAL,
558
        // but this is benign as we won't skip periodic checks indefinitely.
559
54.6k
        if (frame->instr_ptr[-1].op.code == LOAD_SPECIAL) {
560
0
            return 0;
561
0
        }
562
54.6k
        return _Py_HandlePending(tstate);
563
54.6k
    }
564
1.49G
    return 0;
565
1.49G
}
566
567
// Mark the generator as executing. Returns true if the state was changed,
568
// false if it was already executing or finished.
569
static inline bool
570
gen_try_set_executing(PyGenObject *gen)
571
54.3M
{
572
#ifdef Py_GIL_DISABLED
573
    if (!_PyObject_IsUniquelyReferenced((PyObject *)gen)) {
574
        int8_t frame_state = _Py_atomic_load_int8_relaxed(&gen->gi_frame_state);
575
        while (frame_state < FRAME_SUSPENDED_YIELD_FROM_LOCKED) {
576
            if (_Py_atomic_compare_exchange_int8(&gen->gi_frame_state,
577
                                                 &frame_state,
578
                                                 FRAME_EXECUTING)) {
579
                return true;
580
            }
581
        }
582
        // NB: We return false for FRAME_SUSPENDED_YIELD_FROM_LOCKED as well.
583
        // That case is rare enough that we can just handle it in the deopt.
584
        return false;
585
    }
586
#endif
587
    // Use faster non-atomic modifications in the GIL-enabled build and when
588
    // the object is uniquely referenced in the free-threaded build.
589
54.3M
    if (gen->gi_frame_state < FRAME_EXECUTING) {
590
54.3M
        assert(gen->gi_frame_state != FRAME_SUSPENDED_YIELD_FROM_LOCKED);
591
54.3M
        gen->gi_frame_state = FRAME_EXECUTING;
592
54.3M
        return true;
593
54.3M
    }
594
0
    return false;
595
54.3M
}
596
597
// Macro for inplace float binary ops (tier 2 only).
598
// Mutates the uniquely-referenced TARGET operand in place.
599
// TARGET must be either left or right.
600
#define FLOAT_INPLACE_OP(left, right, TARGET, OP)                        \
601
    do {                                                                 \
602
        PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);            \
603
        PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);          \
604
        assert(PyFloat_CheckExact(left_o));                              \
605
        assert(PyFloat_CheckExact(right_o));                             \
606
        assert(_PyObject_IsUniquelyReferenced(                           \
607
            PyStackRef_AsPyObjectBorrow(TARGET)));                       \
608
        STAT_INC(BINARY_OP, hit);                                        \
609
        double _dres =                                                   \
610
            ((PyFloatObject *)left_o)->ob_fval                           \
611
            OP ((PyFloatObject *)right_o)->ob_fval;                      \
612
        ((PyFloatObject *)PyStackRef_AsPyObjectBorrow(TARGET))           \
613
            ->ob_fval = _dres;                                           \
614
    } while (0)
615
616
// Inplace float true division. Sets _divop_err to 1 on zero division.
617
// Caller must check _divop_err and call ERROR_NO_POP() if set.
618
#define FLOAT_INPLACE_DIVOP(left, right, TARGET)                         \
619
    int _divop_err = 0;                                                  \
620
    do {                                                                 \
621
        PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);            \
622
        PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);          \
623
        assert(PyFloat_CheckExact(left_o));                              \
624
        assert(PyFloat_CheckExact(right_o));                             \
625
        assert(_PyObject_IsUniquelyReferenced(                           \
626
            PyStackRef_AsPyObjectBorrow(TARGET)));                       \
627
        STAT_INC(BINARY_OP, hit);                                        \
628
        double _divisor = ((PyFloatObject *)right_o)->ob_fval;           \
629
        if (_divisor == 0.0) {                                           \
630
            PyErr_SetString(PyExc_ZeroDivisionError,                     \
631
                            "float division by zero");                   \
632
            _divop_err = 1;                                              \
633
            break;                                                       \
634
        }                                                                \
635
        double _dres = ((PyFloatObject *)left_o)->ob_fval / _divisor;    \
636
        ((PyFloatObject *)PyStackRef_AsPyObjectBorrow(TARGET))           \
637
            ->ob_fval = _dres;                                           \
638
    } while (0)
639
640
// Inplace compact int operation. TARGET is expected to be uniquely
641
// referenced at the optimizer level, but at runtime it may be a
642
// cached small int singleton. We check _Py_IsImmortal on TARGET
643
// to decide whether inplace mutation is safe.
644
//
645
// After the macro, _int_inplace_res holds the result (may be NULL
646
// on allocation failure). On success, TARGET was mutated in place
647
// and _int_inplace_res is a DUP'd reference to it. On fallback
648
// (small int target, small int result, or overflow), _int_inplace_res
649
// is from FUNC (_PyCompactLong_Add etc.).
650
// FUNC is the fallback function (_PyCompactLong_Add etc.)
651
#define INT_INPLACE_OP(left, right, TARGET, OP, FUNC)                    \
652
    _PyStackRef _int_inplace_res = PyStackRef_NULL;                      \
653
    do {                                                                 \
654
        PyObject *target_o = PyStackRef_AsPyObjectBorrow(TARGET);        \
655
        if (_Py_IsImmortal(target_o)) {                                  \
656
            break;                                                       \
657
        }                                                                \
658
        assert(_PyObject_IsUniquelyReferenced(target_o));                \
659
        Py_ssize_t left_val = _PyLong_CompactValue(                      \
660
            (PyLongObject *)PyStackRef_AsPyObjectBorrow(left));          \
661
        Py_ssize_t right_val = _PyLong_CompactValue(                     \
662
            (PyLongObject *)PyStackRef_AsPyObjectBorrow(right));         \
663
        Py_ssize_t result = left_val OP right_val;                       \
664
        if (!_PY_IS_SMALL_INT(result)                                    \
665
            && ((twodigits)((stwodigits)result) + PyLong_MASK            \
666
                < (twodigits)PyLong_MASK + PyLong_BASE))                 \
667
        {                                                                \
668
            _PyLong_SetSignAndDigitCount(                                \
669
                (PyLongObject *)target_o, result < 0 ? -1 : 1, 1);       \
670
            ((PyLongObject *)target_o)->long_value.ob_digit[0] =         \
671
                (digit)(result < 0 ? -result : result);                  \
672
            _int_inplace_res = PyStackRef_DUP(TARGET);                   \
673
            break;                                                       \
674
        }                                                                \
675
    } while (0);                                                         \
676
    if (PyStackRef_IsNull(_int_inplace_res)) {                           \
677
        _int_inplace_res = FUNC(                                         \
678
            (PyLongObject *)PyStackRef_AsPyObjectBorrow(left),           \
679
            (PyLongObject *)PyStackRef_AsPyObjectBorrow(right));         \
680
    }
681
682
#define CALL_TP_ITERITEM_NO_ESCAPE(ITER, INDEX) \
683
    Py_TYPE(ITER)->_tp_iteritem((ITER), (INDEX))