Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Python/bytecodes.c
12 views
1
// This file contains instruction definitions.
2
// It is read by Tools/cases_generator/generate_cases.py
3
// to generate Python/generated_cases.c.h.
4
// Note that there is some dummy C code at the top and bottom of the file
5
// to fool text editors like VS Code into believing this is valid C code.
6
// The actual instruction definitions start at // BEGIN BYTECODES //.
7
// See Tools/cases_generator/README.md for more information.
8
9
#include "Python.h"
10
#include "pycore_abstract.h" // _PyIndex_Check()
11
#include "pycore_ceval.h" // _PyEval_SignalAsyncExc()
12
#include "pycore_code.h"
13
#include "pycore_function.h"
14
#include "pycore_intrinsics.h"
15
#include "pycore_long.h" // _PyLong_GetZero()
16
#include "pycore_instruments.h"
17
#include "pycore_object.h" // _PyObject_GC_TRACK()
18
#include "pycore_moduleobject.h" // PyModuleObject
19
#include "pycore_opcode.h" // EXTRA_CASES
20
#include "pycore_opcode_utils.h" // MAKE_FUNCTION_*
21
#include "pycore_pyerrors.h" // _PyErr_GetRaisedException()
22
#include "pycore_pystate.h" // _PyInterpreterState_GET()
23
#include "pycore_range.h" // _PyRangeIterObject
24
#include "pycore_sliceobject.h" // _PyBuildSlice_ConsumeRefs
25
#include "pycore_sysmodule.h" // _PySys_Audit()
26
#include "pycore_tuple.h" // _PyTuple_ITEMS()
27
#include "pycore_typeobject.h" // _PySuper_Lookup()
28
#include "pycore_emscripten_signal.h" // _Py_CHECK_EMSCRIPTEN_SIGNALS
29
30
#include "pycore_dict.h"
31
#include "dictobject.h"
32
#include "pycore_frame.h"
33
#include "opcode.h"
34
#include "optimizer.h"
35
#include "pydtrace.h"
36
#include "setobject.h"
37
#include "structmember.h" // struct PyMemberDef, T_OFFSET_EX
38
39
#define USE_COMPUTED_GOTOS 0
40
#include "ceval_macros.h"
41
42
/* Flow control macros */
43
#define DEOPT_IF(cond, instname) ((void)0)
44
#define ERROR_IF(cond, labelname) ((void)0)
45
#define GO_TO_INSTRUCTION(instname) ((void)0)
46
47
#define inst(name, ...) case name:
48
#define op(name, ...) /* NAME is ignored */
49
#define macro(name) static int MACRO_##name
50
#define super(name) static int SUPER_##name
51
#define family(name, ...) static int family_##name
52
#define pseudo(name) static int pseudo_##name
53
54
// Dummy variables for stack effects.
55
static PyObject *value, *value1, *value2, *left, *right, *res, *sum, *prod, *sub;
56
static PyObject *container, *start, *stop, *v, *lhs, *rhs, *res2;
57
static PyObject *list, *tuple, *dict, *owner, *set, *str, *tup, *map, *keys;
58
static PyObject *exit_func, *lasti, *val, *retval, *obj, *iter;
59
static PyObject *aiter, *awaitable, *iterable, *w, *exc_value, *bc, *locals;
60
static PyObject *orig, *excs, *update, *b, *fromlist, *level, *from;
61
static PyObject **pieces, **values;
62
static size_t jump;
63
// Dummy variables for cache effects
64
static uint16_t invert, counter, index, hint;
65
static uint32_t type_version;
66
67
static PyObject *
68
dummy_func(
69
PyThreadState *tstate,
70
_PyInterpreterFrame *frame,
71
unsigned char opcode,
72
unsigned int oparg,
73
_PyCFrame cframe,
74
_Py_CODEUNIT *next_instr,
75
PyObject **stack_pointer,
76
PyObject *kwnames,
77
int throwflag,
78
binaryfunc binary_ops[],
79
PyObject *args[]
80
)
81
{
82
// Dummy labels.
83
pop_1_error:
84
// Dummy locals.
85
PyObject *dummy;
86
PyObject *attr;
87
PyObject *attrs;
88
PyObject *bottom;
89
PyObject *callable;
90
PyObject *callargs;
91
PyObject *codeobj;
92
PyObject *cond;
93
PyObject *descr;
94
_PyInterpreterFrame entry_frame;
95
PyObject *exc;
96
PyObject *exit;
97
PyObject *fget;
98
PyObject *fmt_spec;
99
PyObject *func;
100
uint32_t func_version;
101
PyObject *getattribute;
102
PyObject *kwargs;
103
PyObject *kwdefaults;
104
PyObject *len_o;
105
PyObject *match;
106
PyObject *match_type;
107
PyObject *method;
108
PyObject *mgr;
109
Py_ssize_t min_args;
110
PyObject *names;
111
PyObject *new_exc;
112
PyObject *next;
113
PyObject *none;
114
PyObject *null;
115
PyObject *prev_exc;
116
PyObject *receiver;
117
PyObject *rest;
118
int result;
119
PyObject *self;
120
PyObject *seq;
121
PyObject *slice;
122
PyObject *step;
123
PyObject *subject;
124
PyObject *top;
125
PyObject *type;
126
PyObject *typevars;
127
int values_or_none;
128
129
switch (opcode) {
130
131
// BEGIN BYTECODES //
132
inst(NOP, (--)) {
133
}
134
135
inst(RESUME, (--)) {
136
assert(tstate->cframe == &cframe);
137
assert(frame == cframe.current_frame);
138
/* Possibly combine this with eval breaker */
139
if (_PyFrame_GetCode(frame)->_co_instrumentation_version != tstate->interp->monitoring_version) {
140
int err = _Py_Instrument(_PyFrame_GetCode(frame), tstate->interp);
141
ERROR_IF(err, error);
142
next_instr--;
143
}
144
else if (_Py_atomic_load_relaxed_int32(&tstate->interp->ceval.eval_breaker) && oparg < 2) {
145
goto handle_eval_breaker;
146
}
147
}
148
149
inst(INSTRUMENTED_RESUME, (--)) {
150
/* Possible performance enhancement:
151
* We need to check the eval breaker anyway, can we
152
* combine the instrument verison check and the eval breaker test?
153
*/
154
if (_PyFrame_GetCode(frame)->_co_instrumentation_version != tstate->interp->monitoring_version) {
155
if (_Py_Instrument(_PyFrame_GetCode(frame), tstate->interp)) {
156
goto error;
157
}
158
next_instr--;
159
}
160
else {
161
_PyFrame_SetStackPointer(frame, stack_pointer);
162
int err = _Py_call_instrumentation(
163
tstate, oparg > 0, frame, next_instr-1);
164
stack_pointer = _PyFrame_GetStackPointer(frame);
165
ERROR_IF(err, error);
166
if (frame->prev_instr != next_instr-1) {
167
/* Instrumentation has jumped */
168
next_instr = frame->prev_instr;
169
DISPATCH();
170
}
171
if (_Py_atomic_load_relaxed_int32(&tstate->interp->ceval.eval_breaker) && oparg < 2) {
172
goto handle_eval_breaker;
173
}
174
}
175
}
176
177
pseudo(LOAD_CLOSURE) = {
178
LOAD_FAST,
179
};
180
181
inst(LOAD_FAST_CHECK, (-- value)) {
182
value = GETLOCAL(oparg);
183
ERROR_IF(value == NULL, unbound_local_error);
184
Py_INCREF(value);
185
}
186
187
inst(LOAD_FAST, (-- value)) {
188
value = GETLOCAL(oparg);
189
assert(value != NULL);
190
Py_INCREF(value);
191
}
192
193
inst(LOAD_FAST_AND_CLEAR, (-- value)) {
194
value = GETLOCAL(oparg);
195
// do not use SETLOCAL here, it decrefs the old value
196
GETLOCAL(oparg) = NULL;
197
}
198
199
inst(LOAD_FAST_LOAD_FAST, ( -- value1, value2)) {
200
uint32_t oparg1 = oparg >> 4;
201
uint32_t oparg2 = oparg & 15;
202
value1 = GETLOCAL(oparg1);
203
value2 = GETLOCAL(oparg2);
204
Py_INCREF(value1);
205
Py_INCREF(value2);
206
}
207
208
inst(LOAD_CONST, (-- value)) {
209
value = GETITEM(FRAME_CO_CONSTS, oparg);
210
Py_INCREF(value);
211
}
212
213
inst(STORE_FAST, (value --)) {
214
SETLOCAL(oparg, value);
215
}
216
217
pseudo(STORE_FAST_MAYBE_NULL) = {
218
STORE_FAST,
219
};
220
221
inst(STORE_FAST_LOAD_FAST, (value1 -- value2)) {
222
uint32_t oparg1 = oparg >> 4;
223
uint32_t oparg2 = oparg & 15;
224
SETLOCAL(oparg1, value1);
225
value2 = GETLOCAL(oparg2);
226
Py_INCREF(value2);
227
}
228
229
inst(STORE_FAST_STORE_FAST, (value2, value1 --)) {
230
uint32_t oparg1 = oparg >> 4;
231
uint32_t oparg2 = oparg & 15;
232
SETLOCAL(oparg1, value1);
233
SETLOCAL(oparg2, value2);
234
}
235
236
inst(POP_TOP, (value --)) {
237
DECREF_INPUTS();
238
}
239
240
inst(PUSH_NULL, (-- res)) {
241
res = NULL;
242
}
243
244
macro(END_FOR) = POP_TOP + POP_TOP;
245
246
inst(INSTRUMENTED_END_FOR, (receiver, value --)) {
247
/* Need to create a fake StopIteration error here,
248
* to conform to PEP 380 */
249
if (PyGen_Check(receiver)) {
250
PyErr_SetObject(PyExc_StopIteration, value);
251
if (monitor_stop_iteration(tstate, frame, next_instr-1)) {
252
goto error;
253
}
254
PyErr_SetRaisedException(NULL);
255
}
256
DECREF_INPUTS();
257
}
258
259
inst(END_SEND, (receiver, value -- value)) {
260
Py_DECREF(receiver);
261
}
262
263
inst(INSTRUMENTED_END_SEND, (receiver, value -- value)) {
264
if (PyGen_Check(receiver) || PyCoro_CheckExact(receiver)) {
265
PyErr_SetObject(PyExc_StopIteration, value);
266
if (monitor_stop_iteration(tstate, frame, next_instr-1)) {
267
goto error;
268
}
269
PyErr_SetRaisedException(NULL);
270
}
271
Py_DECREF(receiver);
272
}
273
274
inst(UNARY_NEGATIVE, (value -- res)) {
275
res = PyNumber_Negative(value);
276
DECREF_INPUTS();
277
ERROR_IF(res == NULL, error);
278
}
279
280
inst(UNARY_NOT, (value -- res)) {
281
assert(PyBool_Check(value));
282
res = Py_IsFalse(value) ? Py_True : Py_False;
283
}
284
285
family(to_bool, INLINE_CACHE_ENTRIES_TO_BOOL) = {
286
TO_BOOL,
287
TO_BOOL_ALWAYS_TRUE,
288
TO_BOOL_BOOL,
289
TO_BOOL_INT,
290
TO_BOOL_LIST,
291
TO_BOOL_NONE,
292
TO_BOOL_STR,
293
};
294
295
inst(TO_BOOL, (unused/1, unused/2, value -- res)) {
296
#if ENABLE_SPECIALIZATION
297
_PyToBoolCache *cache = (_PyToBoolCache *)next_instr;
298
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
299
next_instr--;
300
_Py_Specialize_ToBool(value, next_instr);
301
DISPATCH_SAME_OPARG();
302
}
303
STAT_INC(TO_BOOL, deferred);
304
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
305
#endif /* ENABLE_SPECIALIZATION */
306
int err = PyObject_IsTrue(value);
307
DECREF_INPUTS();
308
ERROR_IF(err < 0, error);
309
res = err ? Py_True : Py_False;
310
}
311
312
inst(TO_BOOL_BOOL, (unused/1, unused/2, value -- value)) {
313
DEOPT_IF(!PyBool_Check(value), TO_BOOL);
314
STAT_INC(TO_BOOL, hit);
315
}
316
317
inst(TO_BOOL_INT, (unused/1, unused/2, value -- res)) {
318
DEOPT_IF(!PyLong_CheckExact(value), TO_BOOL);
319
STAT_INC(TO_BOOL, hit);
320
if (_PyLong_IsZero((PyLongObject *)value)) {
321
assert(_Py_IsImmortal(value));
322
res = Py_False;
323
}
324
else {
325
DECREF_INPUTS();
326
res = Py_True;
327
}
328
}
329
330
inst(TO_BOOL_LIST, (unused/1, unused/2, value -- res)) {
331
DEOPT_IF(!PyList_CheckExact(value), TO_BOOL);
332
STAT_INC(TO_BOOL, hit);
333
res = Py_SIZE(value) ? Py_True : Py_False;
334
DECREF_INPUTS();
335
}
336
337
inst(TO_BOOL_NONE, (unused/1, unused/2, value -- res)) {
338
// This one is a bit weird, because we expect *some* failures:
339
DEOPT_IF(!Py_IsNone(value), TO_BOOL);
340
STAT_INC(TO_BOOL, hit);
341
res = Py_False;
342
}
343
344
inst(TO_BOOL_STR, (unused/1, unused/2, value -- res)) {
345
DEOPT_IF(!PyUnicode_CheckExact(value), TO_BOOL);
346
STAT_INC(TO_BOOL, hit);
347
if (value == &_Py_STR(empty)) {
348
assert(_Py_IsImmortal(value));
349
res = Py_False;
350
}
351
else {
352
assert(Py_SIZE(value));
353
DECREF_INPUTS();
354
res = Py_True;
355
}
356
}
357
358
inst(TO_BOOL_ALWAYS_TRUE, (unused/1, version/2, value -- res)) {
359
// This one is a bit weird, because we expect *some* failures:
360
assert(version);
361
DEOPT_IF(Py_TYPE(value)->tp_version_tag != version, TO_BOOL);
362
STAT_INC(TO_BOOL, hit);
363
DECREF_INPUTS();
364
res = Py_True;
365
}
366
367
inst(UNARY_INVERT, (value -- res)) {
368
res = PyNumber_Invert(value);
369
DECREF_INPUTS();
370
ERROR_IF(res == NULL, error);
371
}
372
373
family(binary_op, INLINE_CACHE_ENTRIES_BINARY_OP) = {
374
BINARY_OP,
375
BINARY_OP_MULTIPLY_INT,
376
BINARY_OP_ADD_INT,
377
BINARY_OP_SUBTRACT_INT,
378
BINARY_OP_MULTIPLY_FLOAT,
379
BINARY_OP_ADD_FLOAT,
380
BINARY_OP_SUBTRACT_FLOAT,
381
BINARY_OP_ADD_UNICODE,
382
// BINARY_OP_INPLACE_ADD_UNICODE, // See comments at that opcode.
383
};
384
385
op(_GUARD_BOTH_INT, (left, right -- left, right)) {
386
DEOPT_IF(!PyLong_CheckExact(left), BINARY_OP);
387
DEOPT_IF(!PyLong_CheckExact(right), BINARY_OP);
388
}
389
390
op(_BINARY_OP_MULTIPLY_INT, (unused/1, left, right -- res)) {
391
STAT_INC(BINARY_OP, hit);
392
res = _PyLong_Multiply((PyLongObject *)left, (PyLongObject *)right);
393
_Py_DECREF_SPECIALIZED(right, (destructor)PyObject_Free);
394
_Py_DECREF_SPECIALIZED(left, (destructor)PyObject_Free);
395
ERROR_IF(res == NULL, error);
396
}
397
398
op(_BINARY_OP_ADD_INT, (unused/1, left, right -- res)) {
399
STAT_INC(BINARY_OP, hit);
400
res = _PyLong_Add((PyLongObject *)left, (PyLongObject *)right);
401
_Py_DECREF_SPECIALIZED(right, (destructor)PyObject_Free);
402
_Py_DECREF_SPECIALIZED(left, (destructor)PyObject_Free);
403
ERROR_IF(res == NULL, error);
404
}
405
406
op(_BINARY_OP_SUBTRACT_INT, (unused/1, left, right -- res)) {
407
STAT_INC(BINARY_OP, hit);
408
res = _PyLong_Subtract((PyLongObject *)left, (PyLongObject *)right);
409
_Py_DECREF_SPECIALIZED(right, (destructor)PyObject_Free);
410
_Py_DECREF_SPECIALIZED(left, (destructor)PyObject_Free);
411
ERROR_IF(res == NULL, error);
412
}
413
414
macro(BINARY_OP_MULTIPLY_INT) =
415
_GUARD_BOTH_INT + _BINARY_OP_MULTIPLY_INT;
416
macro(BINARY_OP_ADD_INT) =
417
_GUARD_BOTH_INT + _BINARY_OP_ADD_INT;
418
macro(BINARY_OP_SUBTRACT_INT) =
419
_GUARD_BOTH_INT + _BINARY_OP_SUBTRACT_INT;
420
421
op(_GUARD_BOTH_FLOAT, (left, right -- left, right)) {
422
DEOPT_IF(!PyFloat_CheckExact(left), BINARY_OP);
423
DEOPT_IF(!PyFloat_CheckExact(right), BINARY_OP);
424
}
425
426
op(_BINARY_OP_MULTIPLY_FLOAT, (unused/1, left, right -- res)) {
427
STAT_INC(BINARY_OP, hit);
428
double dres =
429
((PyFloatObject *)left)->ob_fval *
430
((PyFloatObject *)right)->ob_fval;
431
DECREF_INPUTS_AND_REUSE_FLOAT(left, right, dres, res);
432
}
433
434
op(_BINARY_OP_ADD_FLOAT, (unused/1, left, right -- res)) {
435
STAT_INC(BINARY_OP, hit);
436
double dres =
437
((PyFloatObject *)left)->ob_fval +
438
((PyFloatObject *)right)->ob_fval;
439
DECREF_INPUTS_AND_REUSE_FLOAT(left, right, dres, res);
440
}
441
442
op(_BINARY_OP_SUBTRACT_FLOAT, (unused/1, left, right -- res)) {
443
STAT_INC(BINARY_OP, hit);
444
double dres =
445
((PyFloatObject *)left)->ob_fval -
446
((PyFloatObject *)right)->ob_fval;
447
DECREF_INPUTS_AND_REUSE_FLOAT(left, right, dres, res);
448
}
449
450
macro(BINARY_OP_MULTIPLY_FLOAT) =
451
_GUARD_BOTH_FLOAT + _BINARY_OP_MULTIPLY_FLOAT;
452
macro(BINARY_OP_ADD_FLOAT) =
453
_GUARD_BOTH_FLOAT + _BINARY_OP_ADD_FLOAT;
454
macro(BINARY_OP_SUBTRACT_FLOAT) =
455
_GUARD_BOTH_FLOAT + _BINARY_OP_SUBTRACT_FLOAT;
456
457
op(_GUARD_BOTH_UNICODE, (left, right -- left, right)) {
458
DEOPT_IF(!PyUnicode_CheckExact(left), BINARY_OP);
459
DEOPT_IF(!PyUnicode_CheckExact(right), BINARY_OP);
460
}
461
462
op(_BINARY_OP_ADD_UNICODE, (unused/1, left, right -- res)) {
463
STAT_INC(BINARY_OP, hit);
464
res = PyUnicode_Concat(left, right);
465
_Py_DECREF_SPECIALIZED(left, _PyUnicode_ExactDealloc);
466
_Py_DECREF_SPECIALIZED(right, _PyUnicode_ExactDealloc);
467
ERROR_IF(res == NULL, error);
468
}
469
470
macro(BINARY_OP_ADD_UNICODE) =
471
_GUARD_BOTH_UNICODE + _BINARY_OP_ADD_UNICODE;
472
473
// This is a subtle one. It's a super-instruction for
474
// BINARY_OP_ADD_UNICODE followed by STORE_FAST
475
// where the store goes into the left argument.
476
// So the inputs are the same as for all BINARY_OP
477
// specializations, but there is no output.
478
// At the end we just skip over the STORE_FAST.
479
op(_BINARY_OP_INPLACE_ADD_UNICODE, (left, right --)) {
480
_Py_CODEUNIT true_next = next_instr[INLINE_CACHE_ENTRIES_BINARY_OP];
481
assert(true_next.op.code == STORE_FAST);
482
PyObject **target_local = &GETLOCAL(true_next.op.arg);
483
DEOPT_IF(*target_local != left, BINARY_OP);
484
STAT_INC(BINARY_OP, hit);
485
/* Handle `left = left + right` or `left += right` for str.
486
*
487
* When possible, extend `left` in place rather than
488
* allocating a new PyUnicodeObject. This attempts to avoid
489
* quadratic behavior when one neglects to use str.join().
490
*
491
* If `left` has only two references remaining (one from
492
* the stack, one in the locals), DECREFing `left` leaves
493
* only the locals reference, so PyUnicode_Append knows
494
* that the string is safe to mutate.
495
*/
496
assert(Py_REFCNT(left) >= 2);
497
_Py_DECREF_NO_DEALLOC(left);
498
PyUnicode_Append(target_local, right);
499
_Py_DECREF_SPECIALIZED(right, _PyUnicode_ExactDealloc);
500
ERROR_IF(*target_local == NULL, error);
501
// The STORE_FAST is already done.
502
SKIP_OVER(INLINE_CACHE_ENTRIES_BINARY_OP + 1);
503
}
504
505
macro(BINARY_OP_INPLACE_ADD_UNICODE) =
506
_GUARD_BOTH_UNICODE + _BINARY_OP_INPLACE_ADD_UNICODE;
507
508
family(binary_subscr, INLINE_CACHE_ENTRIES_BINARY_SUBSCR) = {
509
BINARY_SUBSCR,
510
BINARY_SUBSCR_DICT,
511
BINARY_SUBSCR_GETITEM,
512
BINARY_SUBSCR_LIST_INT,
513
BINARY_SUBSCR_TUPLE_INT,
514
};
515
516
inst(BINARY_SUBSCR, (unused/1, container, sub -- res)) {
517
#if ENABLE_SPECIALIZATION
518
_PyBinarySubscrCache *cache = (_PyBinarySubscrCache *)next_instr;
519
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
520
next_instr--;
521
_Py_Specialize_BinarySubscr(container, sub, next_instr);
522
DISPATCH_SAME_OPARG();
523
}
524
STAT_INC(BINARY_SUBSCR, deferred);
525
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
526
#endif /* ENABLE_SPECIALIZATION */
527
res = PyObject_GetItem(container, sub);
528
DECREF_INPUTS();
529
ERROR_IF(res == NULL, error);
530
}
531
532
inst(BINARY_SLICE, (container, start, stop -- res)) {
533
PyObject *slice = _PyBuildSlice_ConsumeRefs(start, stop);
534
// Can't use ERROR_IF() here, because we haven't
535
// DECREF'ed container yet, and we still own slice.
536
if (slice == NULL) {
537
res = NULL;
538
}
539
else {
540
res = PyObject_GetItem(container, slice);
541
Py_DECREF(slice);
542
}
543
Py_DECREF(container);
544
ERROR_IF(res == NULL, error);
545
}
546
547
inst(STORE_SLICE, (v, container, start, stop -- )) {
548
PyObject *slice = _PyBuildSlice_ConsumeRefs(start, stop);
549
int err;
550
if (slice == NULL) {
551
err = 1;
552
}
553
else {
554
err = PyObject_SetItem(container, slice, v);
555
Py_DECREF(slice);
556
}
557
Py_DECREF(v);
558
Py_DECREF(container);
559
ERROR_IF(err, error);
560
}
561
562
inst(BINARY_SUBSCR_LIST_INT, (unused/1, list, sub -- res)) {
563
DEOPT_IF(!PyLong_CheckExact(sub), BINARY_SUBSCR);
564
DEOPT_IF(!PyList_CheckExact(list), BINARY_SUBSCR);
565
566
// Deopt unless 0 <= sub < PyList_Size(list)
567
DEOPT_IF(!_PyLong_IsNonNegativeCompact((PyLongObject *)sub), BINARY_SUBSCR);
568
Py_ssize_t index = ((PyLongObject*)sub)->long_value.ob_digit[0];
569
DEOPT_IF(index >= PyList_GET_SIZE(list), BINARY_SUBSCR);
570
STAT_INC(BINARY_SUBSCR, hit);
571
res = PyList_GET_ITEM(list, index);
572
assert(res != NULL);
573
Py_INCREF(res);
574
_Py_DECREF_SPECIALIZED(sub, (destructor)PyObject_Free);
575
Py_DECREF(list);
576
}
577
578
inst(BINARY_SUBSCR_TUPLE_INT, (unused/1, tuple, sub -- res)) {
579
DEOPT_IF(!PyLong_CheckExact(sub), BINARY_SUBSCR);
580
DEOPT_IF(!PyTuple_CheckExact(tuple), BINARY_SUBSCR);
581
582
// Deopt unless 0 <= sub < PyTuple_Size(list)
583
DEOPT_IF(!_PyLong_IsNonNegativeCompact((PyLongObject *)sub), BINARY_SUBSCR);
584
Py_ssize_t index = ((PyLongObject*)sub)->long_value.ob_digit[0];
585
DEOPT_IF(index >= PyTuple_GET_SIZE(tuple), BINARY_SUBSCR);
586
STAT_INC(BINARY_SUBSCR, hit);
587
res = PyTuple_GET_ITEM(tuple, index);
588
assert(res != NULL);
589
Py_INCREF(res);
590
_Py_DECREF_SPECIALIZED(sub, (destructor)PyObject_Free);
591
Py_DECREF(tuple);
592
}
593
594
inst(BINARY_SUBSCR_DICT, (unused/1, dict, sub -- res)) {
595
DEOPT_IF(!PyDict_CheckExact(dict), BINARY_SUBSCR);
596
STAT_INC(BINARY_SUBSCR, hit);
597
res = PyDict_GetItemWithError(dict, sub);
598
if (res == NULL) {
599
if (!_PyErr_Occurred(tstate)) {
600
_PyErr_SetKeyError(sub);
601
}
602
DECREF_INPUTS();
603
ERROR_IF(true, error);
604
}
605
Py_INCREF(res); // Do this before DECREF'ing dict, sub
606
DECREF_INPUTS();
607
}
608
609
inst(BINARY_SUBSCR_GETITEM, (unused/1, container, sub -- unused)) {
610
DEOPT_IF(tstate->interp->eval_frame, BINARY_SUBSCR);
611
PyTypeObject *tp = Py_TYPE(container);
612
DEOPT_IF(!PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE), BINARY_SUBSCR);
613
PyHeapTypeObject *ht = (PyHeapTypeObject *)tp;
614
PyObject *cached = ht->_spec_cache.getitem;
615
DEOPT_IF(cached == NULL, BINARY_SUBSCR);
616
assert(PyFunction_Check(cached));
617
PyFunctionObject *getitem = (PyFunctionObject *)cached;
618
uint32_t cached_version = ht->_spec_cache.getitem_version;
619
DEOPT_IF(getitem->func_version != cached_version, BINARY_SUBSCR);
620
PyCodeObject *code = (PyCodeObject *)getitem->func_code;
621
assert(code->co_argcount == 2);
622
DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize), BINARY_SUBSCR);
623
STAT_INC(BINARY_SUBSCR, hit);
624
Py_INCREF(getitem);
625
_PyInterpreterFrame *new_frame = _PyFrame_PushUnchecked(tstate, getitem, 2);
626
STACK_SHRINK(2);
627
new_frame->localsplus[0] = container;
628
new_frame->localsplus[1] = sub;
629
SKIP_OVER(INLINE_CACHE_ENTRIES_BINARY_SUBSCR);
630
frame->return_offset = 0;
631
DISPATCH_INLINED(new_frame);
632
}
633
634
inst(LIST_APPEND, (list, unused[oparg-1], v -- list, unused[oparg-1])) {
635
ERROR_IF(_PyList_AppendTakeRef((PyListObject *)list, v) < 0, error);
636
}
637
638
inst(SET_ADD, (set, unused[oparg-1], v -- set, unused[oparg-1])) {
639
int err = PySet_Add(set, v);
640
DECREF_INPUTS();
641
ERROR_IF(err, error);
642
}
643
644
family(store_subscr, INLINE_CACHE_ENTRIES_STORE_SUBSCR) = {
645
STORE_SUBSCR,
646
STORE_SUBSCR_DICT,
647
STORE_SUBSCR_LIST_INT,
648
};
649
650
inst(STORE_SUBSCR, (counter/1, v, container, sub -- )) {
651
#if ENABLE_SPECIALIZATION
652
if (ADAPTIVE_COUNTER_IS_ZERO(counter)) {
653
next_instr--;
654
_Py_Specialize_StoreSubscr(container, sub, next_instr);
655
DISPATCH_SAME_OPARG();
656
}
657
STAT_INC(STORE_SUBSCR, deferred);
658
_PyStoreSubscrCache *cache = (_PyStoreSubscrCache *)next_instr;
659
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
660
#else
661
(void)counter; // Unused.
662
#endif /* ENABLE_SPECIALIZATION */
663
/* container[sub] = v */
664
int err = PyObject_SetItem(container, sub, v);
665
DECREF_INPUTS();
666
ERROR_IF(err, error);
667
}
668
669
inst(STORE_SUBSCR_LIST_INT, (unused/1, value, list, sub -- )) {
670
DEOPT_IF(!PyLong_CheckExact(sub), STORE_SUBSCR);
671
DEOPT_IF(!PyList_CheckExact(list), STORE_SUBSCR);
672
673
// Ensure nonnegative, zero-or-one-digit ints.
674
DEOPT_IF(!_PyLong_IsNonNegativeCompact((PyLongObject *)sub), STORE_SUBSCR);
675
Py_ssize_t index = ((PyLongObject*)sub)->long_value.ob_digit[0];
676
// Ensure index < len(list)
677
DEOPT_IF(index >= PyList_GET_SIZE(list), STORE_SUBSCR);
678
STAT_INC(STORE_SUBSCR, hit);
679
680
PyObject *old_value = PyList_GET_ITEM(list, index);
681
PyList_SET_ITEM(list, index, value);
682
assert(old_value != NULL);
683
Py_DECREF(old_value);
684
_Py_DECREF_SPECIALIZED(sub, (destructor)PyObject_Free);
685
Py_DECREF(list);
686
}
687
688
inst(STORE_SUBSCR_DICT, (unused/1, value, dict, sub -- )) {
689
DEOPT_IF(!PyDict_CheckExact(dict), STORE_SUBSCR);
690
STAT_INC(STORE_SUBSCR, hit);
691
int err = _PyDict_SetItem_Take2((PyDictObject *)dict, sub, value);
692
Py_DECREF(dict);
693
ERROR_IF(err, error);
694
}
695
696
inst(DELETE_SUBSCR, (container, sub --)) {
697
/* del container[sub] */
698
int err = PyObject_DelItem(container, sub);
699
DECREF_INPUTS();
700
ERROR_IF(err, error);
701
}
702
703
inst(CALL_INTRINSIC_1, (value -- res)) {
704
assert(oparg <= MAX_INTRINSIC_1);
705
res = _PyIntrinsics_UnaryFunctions[oparg](tstate, value);
706
DECREF_INPUTS();
707
ERROR_IF(res == NULL, error);
708
}
709
710
inst(CALL_INTRINSIC_2, (value2, value1 -- res)) {
711
assert(oparg <= MAX_INTRINSIC_2);
712
res = _PyIntrinsics_BinaryFunctions[oparg](tstate, value2, value1);
713
DECREF_INPUTS();
714
ERROR_IF(res == NULL, error);
715
}
716
717
inst(RAISE_VARARGS, (args[oparg] -- )) {
718
PyObject *cause = NULL, *exc = NULL;
719
switch (oparg) {
720
case 2:
721
cause = args[1];
722
/* fall through */
723
case 1:
724
exc = args[0];
725
/* fall through */
726
case 0:
727
ERROR_IF(do_raise(tstate, exc, cause), exception_unwind);
728
break;
729
default:
730
_PyErr_SetString(tstate, PyExc_SystemError,
731
"bad RAISE_VARARGS oparg");
732
break;
733
}
734
ERROR_IF(true, error);
735
}
736
737
inst(INTERPRETER_EXIT, (retval --)) {
738
assert(frame == &entry_frame);
739
assert(_PyFrame_IsIncomplete(frame));
740
/* Restore previous cframe and return. */
741
tstate->cframe = cframe.previous;
742
assert(tstate->cframe->current_frame == frame->previous);
743
assert(!_PyErr_Occurred(tstate));
744
_Py_LeaveRecursiveCallTstate(tstate);
745
return retval;
746
}
747
748
inst(RETURN_VALUE, (retval --)) {
749
STACK_SHRINK(1);
750
assert(EMPTY());
751
_PyFrame_SetStackPointer(frame, stack_pointer);
752
_Py_LeaveRecursiveCallPy(tstate);
753
assert(frame != &entry_frame);
754
// GH-99729: We need to unlink the frame *before* clearing it:
755
_PyInterpreterFrame *dying = frame;
756
frame = cframe.current_frame = dying->previous;
757
_PyEvalFrameClearAndPop(tstate, dying);
758
frame->prev_instr += frame->return_offset;
759
_PyFrame_StackPush(frame, retval);
760
goto resume_frame;
761
}
762
763
inst(INSTRUMENTED_RETURN_VALUE, (retval --)) {
764
int err = _Py_call_instrumentation_arg(
765
tstate, PY_MONITORING_EVENT_PY_RETURN,
766
frame, next_instr-1, retval);
767
if (err) goto error;
768
STACK_SHRINK(1);
769
assert(EMPTY());
770
_PyFrame_SetStackPointer(frame, stack_pointer);
771
_Py_LeaveRecursiveCallPy(tstate);
772
assert(frame != &entry_frame);
773
// GH-99729: We need to unlink the frame *before* clearing it:
774
_PyInterpreterFrame *dying = frame;
775
frame = cframe.current_frame = dying->previous;
776
_PyEvalFrameClearAndPop(tstate, dying);
777
frame->prev_instr += frame->return_offset;
778
_PyFrame_StackPush(frame, retval);
779
goto resume_frame;
780
}
781
782
inst(RETURN_CONST, (--)) {
783
PyObject *retval = GETITEM(FRAME_CO_CONSTS, oparg);
784
Py_INCREF(retval);
785
assert(EMPTY());
786
_PyFrame_SetStackPointer(frame, stack_pointer);
787
_Py_LeaveRecursiveCallPy(tstate);
788
assert(frame != &entry_frame);
789
// GH-99729: We need to unlink the frame *before* clearing it:
790
_PyInterpreterFrame *dying = frame;
791
frame = cframe.current_frame = dying->previous;
792
_PyEvalFrameClearAndPop(tstate, dying);
793
frame->prev_instr += frame->return_offset;
794
_PyFrame_StackPush(frame, retval);
795
goto resume_frame;
796
}
797
798
inst(INSTRUMENTED_RETURN_CONST, (--)) {
799
PyObject *retval = GETITEM(FRAME_CO_CONSTS, oparg);
800
int err = _Py_call_instrumentation_arg(
801
tstate, PY_MONITORING_EVENT_PY_RETURN,
802
frame, next_instr-1, retval);
803
if (err) goto error;
804
Py_INCREF(retval);
805
assert(EMPTY());
806
_PyFrame_SetStackPointer(frame, stack_pointer);
807
_Py_LeaveRecursiveCallPy(tstate);
808
assert(frame != &entry_frame);
809
// GH-99729: We need to unlink the frame *before* clearing it:
810
_PyInterpreterFrame *dying = frame;
811
frame = cframe.current_frame = dying->previous;
812
_PyEvalFrameClearAndPop(tstate, dying);
813
frame->prev_instr += frame->return_offset;
814
_PyFrame_StackPush(frame, retval);
815
goto resume_frame;
816
}
817
818
inst(GET_AITER, (obj -- iter)) {
819
unaryfunc getter = NULL;
820
PyTypeObject *type = Py_TYPE(obj);
821
822
if (type->tp_as_async != NULL) {
823
getter = type->tp_as_async->am_aiter;
824
}
825
826
if (getter == NULL) {
827
_PyErr_Format(tstate, PyExc_TypeError,
828
"'async for' requires an object with "
829
"__aiter__ method, got %.100s",
830
type->tp_name);
831
DECREF_INPUTS();
832
ERROR_IF(true, error);
833
}
834
835
iter = (*getter)(obj);
836
DECREF_INPUTS();
837
ERROR_IF(iter == NULL, error);
838
839
if (Py_TYPE(iter)->tp_as_async == NULL ||
840
Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
841
842
_PyErr_Format(tstate, PyExc_TypeError,
843
"'async for' received an object from __aiter__ "
844
"that does not implement __anext__: %.100s",
845
Py_TYPE(iter)->tp_name);
846
Py_DECREF(iter);
847
ERROR_IF(true, error);
848
}
849
}
850
851
inst(GET_ANEXT, (aiter -- aiter, awaitable)) {
852
unaryfunc getter = NULL;
853
PyObject *next_iter = NULL;
854
PyTypeObject *type = Py_TYPE(aiter);
855
856
if (PyAsyncGen_CheckExact(aiter)) {
857
awaitable = type->tp_as_async->am_anext(aiter);
858
if (awaitable == NULL) {
859
goto error;
860
}
861
} else {
862
if (type->tp_as_async != NULL){
863
getter = type->tp_as_async->am_anext;
864
}
865
866
if (getter != NULL) {
867
next_iter = (*getter)(aiter);
868
if (next_iter == NULL) {
869
goto error;
870
}
871
}
872
else {
873
_PyErr_Format(tstate, PyExc_TypeError,
874
"'async for' requires an iterator with "
875
"__anext__ method, got %.100s",
876
type->tp_name);
877
goto error;
878
}
879
880
awaitable = _PyCoro_GetAwaitableIter(next_iter);
881
if (awaitable == NULL) {
882
_PyErr_FormatFromCause(
883
PyExc_TypeError,
884
"'async for' received an invalid object "
885
"from __anext__: %.100s",
886
Py_TYPE(next_iter)->tp_name);
887
888
Py_DECREF(next_iter);
889
goto error;
890
} else {
891
Py_DECREF(next_iter);
892
}
893
}
894
}
895
896
inst(GET_AWAITABLE, (iterable -- iter)) {
897
iter = _PyCoro_GetAwaitableIter(iterable);
898
899
if (iter == NULL) {
900
format_awaitable_error(tstate, Py_TYPE(iterable), oparg);
901
}
902
903
DECREF_INPUTS();
904
905
if (iter != NULL && PyCoro_CheckExact(iter)) {
906
PyObject *yf = _PyGen_yf((PyGenObject*)iter);
907
if (yf != NULL) {
908
/* `iter` is a coroutine object that is being
909
awaited, `yf` is a pointer to the current awaitable
910
being awaited on. */
911
Py_DECREF(yf);
912
Py_CLEAR(iter);
913
_PyErr_SetString(tstate, PyExc_RuntimeError,
914
"coroutine is being awaited already");
915
/* The code below jumps to `error` if `iter` is NULL. */
916
}
917
}
918
919
ERROR_IF(iter == NULL, error);
920
}
921
922
family(send, INLINE_CACHE_ENTRIES_SEND) = {
923
SEND,
924
SEND_GEN,
925
};
926
927
inst(SEND, (unused/1, receiver, v -- receiver, retval)) {
928
#if ENABLE_SPECIALIZATION
929
_PySendCache *cache = (_PySendCache *)next_instr;
930
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
931
next_instr--;
932
_Py_Specialize_Send(receiver, next_instr);
933
DISPATCH_SAME_OPARG();
934
}
935
STAT_INC(SEND, deferred);
936
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
937
#endif /* ENABLE_SPECIALIZATION */
938
assert(frame != &entry_frame);
939
if ((tstate->interp->eval_frame == NULL) &&
940
(Py_TYPE(receiver) == &PyGen_Type || Py_TYPE(receiver) == &PyCoro_Type) &&
941
((PyGenObject *)receiver)->gi_frame_state < FRAME_EXECUTING)
942
{
943
PyGenObject *gen = (PyGenObject *)receiver;
944
_PyInterpreterFrame *gen_frame = (_PyInterpreterFrame *)gen->gi_iframe;
945
frame->return_offset = oparg;
946
STACK_SHRINK(1);
947
_PyFrame_StackPush(gen_frame, v);
948
gen->gi_frame_state = FRAME_EXECUTING;
949
gen->gi_exc_state.previous_item = tstate->exc_info;
950
tstate->exc_info = &gen->gi_exc_state;
951
SKIP_OVER(INLINE_CACHE_ENTRIES_SEND);
952
DISPATCH_INLINED(gen_frame);
953
}
954
if (Py_IsNone(v) && PyIter_Check(receiver)) {
955
retval = Py_TYPE(receiver)->tp_iternext(receiver);
956
}
957
else {
958
retval = PyObject_CallMethodOneArg(receiver, &_Py_ID(send), v);
959
}
960
if (retval == NULL) {
961
if (_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)
962
) {
963
monitor_raise(tstate, frame, next_instr-1);
964
}
965
if (_PyGen_FetchStopIterationValue(&retval) == 0) {
966
assert(retval != NULL);
967
JUMPBY(oparg);
968
}
969
else {
970
goto error;
971
}
972
}
973
Py_DECREF(v);
974
}
975
976
inst(SEND_GEN, (unused/1, receiver, v -- receiver, unused)) {
977
DEOPT_IF(tstate->interp->eval_frame, SEND);
978
PyGenObject *gen = (PyGenObject *)receiver;
979
DEOPT_IF(Py_TYPE(gen) != &PyGen_Type &&
980
Py_TYPE(gen) != &PyCoro_Type, SEND);
981
DEOPT_IF(gen->gi_frame_state >= FRAME_EXECUTING, SEND);
982
STAT_INC(SEND, hit);
983
_PyInterpreterFrame *gen_frame = (_PyInterpreterFrame *)gen->gi_iframe;
984
frame->return_offset = oparg;
985
STACK_SHRINK(1);
986
_PyFrame_StackPush(gen_frame, v);
987
gen->gi_frame_state = FRAME_EXECUTING;
988
gen->gi_exc_state.previous_item = tstate->exc_info;
989
tstate->exc_info = &gen->gi_exc_state;
990
SKIP_OVER(INLINE_CACHE_ENTRIES_SEND);
991
DISPATCH_INLINED(gen_frame);
992
}
993
994
inst(INSTRUMENTED_YIELD_VALUE, (retval -- unused)) {
995
assert(frame != &entry_frame);
996
assert(oparg >= 0); /* make the generator identify this as HAS_ARG */
997
PyGenObject *gen = _PyFrame_GetGenerator(frame);
998
gen->gi_frame_state = FRAME_SUSPENDED;
999
_PyFrame_SetStackPointer(frame, stack_pointer - 1);
1000
int err = _Py_call_instrumentation_arg(
1001
tstate, PY_MONITORING_EVENT_PY_YIELD,
1002
frame, next_instr-1, retval);
1003
if (err) goto error;
1004
tstate->exc_info = gen->gi_exc_state.previous_item;
1005
gen->gi_exc_state.previous_item = NULL;
1006
_Py_LeaveRecursiveCallPy(tstate);
1007
_PyInterpreterFrame *gen_frame = frame;
1008
frame = cframe.current_frame = frame->previous;
1009
gen_frame->previous = NULL;
1010
_PyFrame_StackPush(frame, retval);
1011
goto resume_frame;
1012
}
1013
1014
inst(YIELD_VALUE, (retval -- unused)) {
1015
// NOTE: It's important that YIELD_VALUE never raises an exception!
1016
// The compiler treats any exception raised here as a failed close()
1017
// or throw() call.
1018
assert(oparg >= 0); /* make the generator identify this as HAS_ARG */
1019
assert(frame != &entry_frame);
1020
PyGenObject *gen = _PyFrame_GetGenerator(frame);
1021
gen->gi_frame_state = FRAME_SUSPENDED;
1022
_PyFrame_SetStackPointer(frame, stack_pointer - 1);
1023
tstate->exc_info = gen->gi_exc_state.previous_item;
1024
gen->gi_exc_state.previous_item = NULL;
1025
_Py_LeaveRecursiveCallPy(tstate);
1026
_PyInterpreterFrame *gen_frame = frame;
1027
frame = cframe.current_frame = frame->previous;
1028
gen_frame->previous = NULL;
1029
_PyFrame_StackPush(frame, retval);
1030
goto resume_frame;
1031
}
1032
1033
inst(POP_EXCEPT, (exc_value -- )) {
1034
_PyErr_StackItem *exc_info = tstate->exc_info;
1035
Py_XSETREF(exc_info->exc_value, exc_value);
1036
}
1037
1038
inst(RERAISE, (values[oparg], exc -- values[oparg])) {
1039
assert(oparg >= 0 && oparg <= 2);
1040
if (oparg) {
1041
PyObject *lasti = values[0];
1042
if (PyLong_Check(lasti)) {
1043
frame->prev_instr = _PyCode_CODE(_PyFrame_GetCode(frame)) + PyLong_AsLong(lasti);
1044
assert(!_PyErr_Occurred(tstate));
1045
}
1046
else {
1047
assert(PyLong_Check(lasti));
1048
_PyErr_SetString(tstate, PyExc_SystemError, "lasti is not an int");
1049
goto error;
1050
}
1051
}
1052
assert(exc && PyExceptionInstance_Check(exc));
1053
Py_INCREF(exc);
1054
_PyErr_SetRaisedException(tstate, exc);
1055
goto exception_unwind;
1056
}
1057
1058
inst(END_ASYNC_FOR, (awaitable, exc -- )) {
1059
assert(exc && PyExceptionInstance_Check(exc));
1060
if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
1061
DECREF_INPUTS();
1062
}
1063
else {
1064
Py_INCREF(exc);
1065
_PyErr_SetRaisedException(tstate, exc);
1066
goto exception_unwind;
1067
}
1068
}
1069
1070
inst(CLEANUP_THROW, (sub_iter, last_sent_val, exc_value -- none, value)) {
1071
assert(throwflag);
1072
assert(exc_value && PyExceptionInstance_Check(exc_value));
1073
if (PyErr_GivenExceptionMatches(exc_value, PyExc_StopIteration)) {
1074
value = Py_NewRef(((PyStopIterationObject *)exc_value)->value);
1075
DECREF_INPUTS();
1076
none = Py_None;
1077
}
1078
else {
1079
_PyErr_SetRaisedException(tstate, Py_NewRef(exc_value));
1080
goto exception_unwind;
1081
}
1082
}
1083
1084
inst(LOAD_ASSERTION_ERROR, ( -- value)) {
1085
value = Py_NewRef(PyExc_AssertionError);
1086
}
1087
1088
inst(LOAD_BUILD_CLASS, ( -- bc)) {
1089
if (PyDict_CheckExact(BUILTINS())) {
1090
bc = _PyDict_GetItemWithError(BUILTINS(),
1091
&_Py_ID(__build_class__));
1092
if (bc == NULL) {
1093
if (!_PyErr_Occurred(tstate)) {
1094
_PyErr_SetString(tstate, PyExc_NameError,
1095
"__build_class__ not found");
1096
}
1097
ERROR_IF(true, error);
1098
}
1099
Py_INCREF(bc);
1100
}
1101
else {
1102
bc = PyObject_GetItem(BUILTINS(), &_Py_ID(__build_class__));
1103
if (bc == NULL) {
1104
if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
1105
_PyErr_SetString(tstate, PyExc_NameError,
1106
"__build_class__ not found");
1107
ERROR_IF(true, error);
1108
}
1109
}
1110
}
1111
1112
1113
inst(STORE_NAME, (v -- )) {
1114
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
1115
PyObject *ns = LOCALS();
1116
int err;
1117
if (ns == NULL) {
1118
_PyErr_Format(tstate, PyExc_SystemError,
1119
"no locals found when storing %R", name);
1120
DECREF_INPUTS();
1121
ERROR_IF(true, error);
1122
}
1123
if (PyDict_CheckExact(ns))
1124
err = PyDict_SetItem(ns, name, v);
1125
else
1126
err = PyObject_SetItem(ns, name, v);
1127
DECREF_INPUTS();
1128
ERROR_IF(err, error);
1129
}
1130
1131
inst(DELETE_NAME, (--)) {
1132
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
1133
PyObject *ns = LOCALS();
1134
int err;
1135
if (ns == NULL) {
1136
_PyErr_Format(tstate, PyExc_SystemError,
1137
"no locals when deleting %R", name);
1138
goto error;
1139
}
1140
err = PyObject_DelItem(ns, name);
1141
// Can't use ERROR_IF here.
1142
if (err != 0) {
1143
format_exc_check_arg(tstate, PyExc_NameError,
1144
NAME_ERROR_MSG,
1145
name);
1146
goto error;
1147
}
1148
}
1149
1150
family(unpack_sequence, INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE) = {
1151
UNPACK_SEQUENCE,
1152
UNPACK_SEQUENCE_TWO_TUPLE,
1153
UNPACK_SEQUENCE_TUPLE,
1154
UNPACK_SEQUENCE_LIST,
1155
};
1156
1157
inst(UNPACK_SEQUENCE, (unused/1, seq -- unused[oparg])) {
1158
#if ENABLE_SPECIALIZATION
1159
_PyUnpackSequenceCache *cache = (_PyUnpackSequenceCache *)next_instr;
1160
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
1161
next_instr--;
1162
_Py_Specialize_UnpackSequence(seq, next_instr, oparg);
1163
DISPATCH_SAME_OPARG();
1164
}
1165
STAT_INC(UNPACK_SEQUENCE, deferred);
1166
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
1167
#endif /* ENABLE_SPECIALIZATION */
1168
PyObject **top = stack_pointer + oparg - 1;
1169
int res = unpack_iterable(tstate, seq, oparg, -1, top);
1170
DECREF_INPUTS();
1171
ERROR_IF(res == 0, error);
1172
}
1173
1174
inst(UNPACK_SEQUENCE_TWO_TUPLE, (unused/1, seq -- values[oparg])) {
1175
DEOPT_IF(!PyTuple_CheckExact(seq), UNPACK_SEQUENCE);
1176
DEOPT_IF(PyTuple_GET_SIZE(seq) != 2, UNPACK_SEQUENCE);
1177
assert(oparg == 2);
1178
STAT_INC(UNPACK_SEQUENCE, hit);
1179
values[0] = Py_NewRef(PyTuple_GET_ITEM(seq, 1));
1180
values[1] = Py_NewRef(PyTuple_GET_ITEM(seq, 0));
1181
DECREF_INPUTS();
1182
}
1183
1184
inst(UNPACK_SEQUENCE_TUPLE, (unused/1, seq -- values[oparg])) {
1185
DEOPT_IF(!PyTuple_CheckExact(seq), UNPACK_SEQUENCE);
1186
DEOPT_IF(PyTuple_GET_SIZE(seq) != oparg, UNPACK_SEQUENCE);
1187
STAT_INC(UNPACK_SEQUENCE, hit);
1188
PyObject **items = _PyTuple_ITEMS(seq);
1189
for (int i = oparg; --i >= 0; ) {
1190
*values++ = Py_NewRef(items[i]);
1191
}
1192
DECREF_INPUTS();
1193
}
1194
1195
inst(UNPACK_SEQUENCE_LIST, (unused/1, seq -- values[oparg])) {
1196
DEOPT_IF(!PyList_CheckExact(seq), UNPACK_SEQUENCE);
1197
DEOPT_IF(PyList_GET_SIZE(seq) != oparg, UNPACK_SEQUENCE);
1198
STAT_INC(UNPACK_SEQUENCE, hit);
1199
PyObject **items = _PyList_ITEMS(seq);
1200
for (int i = oparg; --i >= 0; ) {
1201
*values++ = Py_NewRef(items[i]);
1202
}
1203
DECREF_INPUTS();
1204
}
1205
1206
inst(UNPACK_EX, (seq -- unused[oparg & 0xFF], unused, unused[oparg >> 8])) {
1207
int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1208
PyObject **top = stack_pointer + totalargs - 1;
1209
int res = unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8, top);
1210
DECREF_INPUTS();
1211
ERROR_IF(res == 0, error);
1212
}
1213
1214
family(store_attr, INLINE_CACHE_ENTRIES_STORE_ATTR) = {
1215
STORE_ATTR,
1216
STORE_ATTR_INSTANCE_VALUE,
1217
STORE_ATTR_SLOT,
1218
STORE_ATTR_WITH_HINT,
1219
};
1220
1221
inst(STORE_ATTR, (counter/1, unused/3, v, owner --)) {
1222
#if ENABLE_SPECIALIZATION
1223
if (ADAPTIVE_COUNTER_IS_ZERO(counter)) {
1224
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
1225
next_instr--;
1226
_Py_Specialize_StoreAttr(owner, next_instr, name);
1227
DISPATCH_SAME_OPARG();
1228
}
1229
STAT_INC(STORE_ATTR, deferred);
1230
_PyAttrCache *cache = (_PyAttrCache *)next_instr;
1231
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
1232
#else
1233
(void)counter; // Unused.
1234
#endif /* ENABLE_SPECIALIZATION */
1235
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
1236
int err = PyObject_SetAttr(owner, name, v);
1237
DECREF_INPUTS();
1238
ERROR_IF(err, error);
1239
}
1240
1241
inst(DELETE_ATTR, (owner --)) {
1242
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
1243
int err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
1244
DECREF_INPUTS();
1245
ERROR_IF(err, error);
1246
}
1247
1248
inst(STORE_GLOBAL, (v --)) {
1249
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
1250
int err = PyDict_SetItem(GLOBALS(), name, v);
1251
DECREF_INPUTS();
1252
ERROR_IF(err, error);
1253
}
1254
1255
inst(DELETE_GLOBAL, (--)) {
1256
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
1257
int err;
1258
err = PyDict_DelItem(GLOBALS(), name);
1259
// Can't use ERROR_IF here.
1260
if (err != 0) {
1261
if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
1262
format_exc_check_arg(tstate, PyExc_NameError,
1263
NAME_ERROR_MSG, name);
1264
}
1265
goto error;
1266
}
1267
}
1268
1269
op(_LOAD_LOCALS, ( -- locals)) {
1270
locals = LOCALS();
1271
if (locals == NULL) {
1272
_PyErr_SetString(tstate, PyExc_SystemError,
1273
"no locals found");
1274
ERROR_IF(true, error);
1275
}
1276
Py_INCREF(locals);
1277
}
1278
1279
macro(LOAD_LOCALS) = _LOAD_LOCALS;
1280
1281
op(_LOAD_FROM_DICT_OR_GLOBALS, (mod_or_class_dict -- v)) {
1282
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
1283
if (PyDict_CheckExact(mod_or_class_dict)) {
1284
v = PyDict_GetItemWithError(mod_or_class_dict, name);
1285
if (v != NULL) {
1286
Py_INCREF(v);
1287
}
1288
else if (_PyErr_Occurred(tstate)) {
1289
Py_DECREF(mod_or_class_dict);
1290
goto error;
1291
}
1292
}
1293
else {
1294
v = PyObject_GetItem(mod_or_class_dict, name);
1295
if (v == NULL) {
1296
if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
1297
Py_DECREF(mod_or_class_dict);
1298
goto error;
1299
}
1300
_PyErr_Clear(tstate);
1301
}
1302
}
1303
Py_DECREF(mod_or_class_dict);
1304
if (v == NULL) {
1305
v = PyDict_GetItemWithError(GLOBALS(), name);
1306
if (v != NULL) {
1307
Py_INCREF(v);
1308
}
1309
else if (_PyErr_Occurred(tstate)) {
1310
goto error;
1311
}
1312
else {
1313
if (PyDict_CheckExact(BUILTINS())) {
1314
v = PyDict_GetItemWithError(BUILTINS(), name);
1315
if (v == NULL) {
1316
if (!_PyErr_Occurred(tstate)) {
1317
format_exc_check_arg(
1318
tstate, PyExc_NameError,
1319
NAME_ERROR_MSG, name);
1320
}
1321
goto error;
1322
}
1323
Py_INCREF(v);
1324
}
1325
else {
1326
v = PyObject_GetItem(BUILTINS(), name);
1327
if (v == NULL) {
1328
if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
1329
format_exc_check_arg(
1330
tstate, PyExc_NameError,
1331
NAME_ERROR_MSG, name);
1332
}
1333
goto error;
1334
}
1335
}
1336
}
1337
}
1338
}
1339
1340
macro(LOAD_NAME) = _LOAD_LOCALS + _LOAD_FROM_DICT_OR_GLOBALS;
1341
1342
macro(LOAD_FROM_DICT_OR_GLOBALS) = _LOAD_FROM_DICT_OR_GLOBALS;
1343
1344
family(load_global, INLINE_CACHE_ENTRIES_LOAD_GLOBAL) = {
1345
LOAD_GLOBAL,
1346
LOAD_GLOBAL_MODULE,
1347
LOAD_GLOBAL_BUILTIN,
1348
};
1349
1350
inst(LOAD_GLOBAL, (unused/1, unused/1, unused/1, unused/1 -- null if (oparg & 1), v)) {
1351
#if ENABLE_SPECIALIZATION
1352
_PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)next_instr;
1353
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
1354
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg>>1);
1355
next_instr--;
1356
_Py_Specialize_LoadGlobal(GLOBALS(), BUILTINS(), next_instr, name);
1357
DISPATCH_SAME_OPARG();
1358
}
1359
STAT_INC(LOAD_GLOBAL, deferred);
1360
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
1361
#endif /* ENABLE_SPECIALIZATION */
1362
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg>>1);
1363
if (PyDict_CheckExact(GLOBALS())
1364
&& PyDict_CheckExact(BUILTINS()))
1365
{
1366
v = _PyDict_LoadGlobal((PyDictObject *)GLOBALS(),
1367
(PyDictObject *)BUILTINS(),
1368
name);
1369
if (v == NULL) {
1370
if (!_PyErr_Occurred(tstate)) {
1371
/* _PyDict_LoadGlobal() returns NULL without raising
1372
* an exception if the key doesn't exist */
1373
format_exc_check_arg(tstate, PyExc_NameError,
1374
NAME_ERROR_MSG, name);
1375
}
1376
ERROR_IF(true, error);
1377
}
1378
Py_INCREF(v);
1379
}
1380
else {
1381
/* Slow-path if globals or builtins is not a dict */
1382
1383
/* namespace 1: globals */
1384
v = PyObject_GetItem(GLOBALS(), name);
1385
if (v == NULL) {
1386
ERROR_IF(!_PyErr_ExceptionMatches(tstate, PyExc_KeyError), error);
1387
_PyErr_Clear(tstate);
1388
1389
/* namespace 2: builtins */
1390
v = PyObject_GetItem(BUILTINS(), name);
1391
if (v == NULL) {
1392
if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
1393
format_exc_check_arg(
1394
tstate, PyExc_NameError,
1395
NAME_ERROR_MSG, name);
1396
}
1397
ERROR_IF(true, error);
1398
}
1399
}
1400
}
1401
null = NULL;
1402
}
1403
1404
inst(LOAD_GLOBAL_MODULE, (unused/1, index/1, version/1, unused/1 -- null if (oparg & 1), res)) {
1405
DEOPT_IF(!PyDict_CheckExact(GLOBALS()), LOAD_GLOBAL);
1406
PyDictObject *dict = (PyDictObject *)GLOBALS();
1407
DEOPT_IF(dict->ma_keys->dk_version != version, LOAD_GLOBAL);
1408
assert(DK_IS_UNICODE(dict->ma_keys));
1409
PyDictUnicodeEntry *entries = DK_UNICODE_ENTRIES(dict->ma_keys);
1410
res = entries[index].me_value;
1411
DEOPT_IF(res == NULL, LOAD_GLOBAL);
1412
Py_INCREF(res);
1413
STAT_INC(LOAD_GLOBAL, hit);
1414
null = NULL;
1415
}
1416
1417
inst(LOAD_GLOBAL_BUILTIN, (unused/1, index/1, mod_version/1, bltn_version/1 -- null if (oparg & 1), res)) {
1418
DEOPT_IF(!PyDict_CheckExact(GLOBALS()), LOAD_GLOBAL);
1419
DEOPT_IF(!PyDict_CheckExact(BUILTINS()), LOAD_GLOBAL);
1420
PyDictObject *mdict = (PyDictObject *)GLOBALS();
1421
PyDictObject *bdict = (PyDictObject *)BUILTINS();
1422
assert(opcode == LOAD_GLOBAL_BUILTIN);
1423
DEOPT_IF(mdict->ma_keys->dk_version != mod_version, LOAD_GLOBAL);
1424
DEOPT_IF(bdict->ma_keys->dk_version != bltn_version, LOAD_GLOBAL);
1425
assert(DK_IS_UNICODE(bdict->ma_keys));
1426
PyDictUnicodeEntry *entries = DK_UNICODE_ENTRIES(bdict->ma_keys);
1427
res = entries[index].me_value;
1428
DEOPT_IF(res == NULL, LOAD_GLOBAL);
1429
Py_INCREF(res);
1430
STAT_INC(LOAD_GLOBAL, hit);
1431
null = NULL;
1432
}
1433
1434
inst(DELETE_FAST, (--)) {
1435
PyObject *v = GETLOCAL(oparg);
1436
ERROR_IF(v == NULL, unbound_local_error);
1437
SETLOCAL(oparg, NULL);
1438
}
1439
1440
inst(MAKE_CELL, (--)) {
1441
// "initial" is probably NULL but not if it's an arg (or set
1442
// via PyFrame_LocalsToFast() before MAKE_CELL has run).
1443
PyObject *initial = GETLOCAL(oparg);
1444
PyObject *cell = PyCell_New(initial);
1445
if (cell == NULL) {
1446
goto resume_with_error;
1447
}
1448
SETLOCAL(oparg, cell);
1449
}
1450
1451
inst(DELETE_DEREF, (--)) {
1452
PyObject *cell = GETLOCAL(oparg);
1453
PyObject *oldobj = PyCell_GET(cell);
1454
// Can't use ERROR_IF here.
1455
// Fortunately we don't need its superpower.
1456
if (oldobj == NULL) {
1457
format_exc_unbound(tstate, _PyFrame_GetCode(frame), oparg);
1458
goto error;
1459
}
1460
PyCell_SET(cell, NULL);
1461
Py_DECREF(oldobj);
1462
}
1463
1464
inst(LOAD_FROM_DICT_OR_DEREF, (class_dict -- value)) {
1465
PyObject *name;
1466
assert(class_dict);
1467
assert(oparg >= 0 && oparg < _PyFrame_GetCode(frame)->co_nlocalsplus);
1468
name = PyTuple_GET_ITEM(_PyFrame_GetCode(frame)->co_localsplusnames, oparg);
1469
if (PyDict_CheckExact(class_dict)) {
1470
value = PyDict_GetItemWithError(class_dict, name);
1471
if (value != NULL) {
1472
Py_INCREF(value);
1473
}
1474
else if (_PyErr_Occurred(tstate)) {
1475
Py_DECREF(class_dict);
1476
goto error;
1477
}
1478
}
1479
else {
1480
value = PyObject_GetItem(class_dict, name);
1481
if (value == NULL) {
1482
if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
1483
Py_DECREF(class_dict);
1484
goto error;
1485
}
1486
_PyErr_Clear(tstate);
1487
}
1488
}
1489
Py_DECREF(class_dict);
1490
if (!value) {
1491
PyObject *cell = GETLOCAL(oparg);
1492
value = PyCell_GET(cell);
1493
if (value == NULL) {
1494
format_exc_unbound(tstate, _PyFrame_GetCode(frame), oparg);
1495
goto error;
1496
}
1497
Py_INCREF(value);
1498
}
1499
}
1500
1501
inst(LOAD_DEREF, ( -- value)) {
1502
PyObject *cell = GETLOCAL(oparg);
1503
value = PyCell_GET(cell);
1504
if (value == NULL) {
1505
format_exc_unbound(tstate, _PyFrame_GetCode(frame), oparg);
1506
ERROR_IF(true, error);
1507
}
1508
Py_INCREF(value);
1509
}
1510
1511
inst(STORE_DEREF, (v --)) {
1512
PyObject *cell = GETLOCAL(oparg);
1513
PyObject *oldobj = PyCell_GET(cell);
1514
PyCell_SET(cell, v);
1515
Py_XDECREF(oldobj);
1516
}
1517
1518
inst(COPY_FREE_VARS, (--)) {
1519
/* Copy closure variables to free variables */
1520
PyCodeObject *co = _PyFrame_GetCode(frame);
1521
assert(PyFunction_Check(frame->f_funcobj));
1522
PyObject *closure = ((PyFunctionObject *)frame->f_funcobj)->func_closure;
1523
assert(oparg == co->co_nfreevars);
1524
int offset = co->co_nlocalsplus - oparg;
1525
for (int i = 0; i < oparg; ++i) {
1526
PyObject *o = PyTuple_GET_ITEM(closure, i);
1527
frame->localsplus[offset + i] = Py_NewRef(o);
1528
}
1529
}
1530
1531
inst(BUILD_STRING, (pieces[oparg] -- str)) {
1532
str = _PyUnicode_JoinArray(&_Py_STR(empty), pieces, oparg);
1533
DECREF_INPUTS();
1534
ERROR_IF(str == NULL, error);
1535
}
1536
1537
inst(BUILD_TUPLE, (values[oparg] -- tup)) {
1538
tup = _PyTuple_FromArraySteal(values, oparg);
1539
ERROR_IF(tup == NULL, error);
1540
}
1541
1542
inst(BUILD_LIST, (values[oparg] -- list)) {
1543
list = _PyList_FromArraySteal(values, oparg);
1544
ERROR_IF(list == NULL, error);
1545
}
1546
1547
inst(LIST_EXTEND, (list, unused[oparg-1], iterable -- list, unused[oparg-1])) {
1548
PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
1549
if (none_val == NULL) {
1550
if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
1551
(Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
1552
{
1553
_PyErr_Clear(tstate);
1554
_PyErr_Format(tstate, PyExc_TypeError,
1555
"Value after * must be an iterable, not %.200s",
1556
Py_TYPE(iterable)->tp_name);
1557
}
1558
DECREF_INPUTS();
1559
ERROR_IF(true, error);
1560
}
1561
assert(Py_IsNone(none_val));
1562
DECREF_INPUTS();
1563
}
1564
1565
inst(SET_UPDATE, (set, unused[oparg-1], iterable -- set, unused[oparg-1])) {
1566
int err = _PySet_Update(set, iterable);
1567
DECREF_INPUTS();
1568
ERROR_IF(err < 0, error);
1569
}
1570
1571
inst(BUILD_SET, (values[oparg] -- set)) {
1572
set = PySet_New(NULL);
1573
if (set == NULL)
1574
goto error;
1575
int err = 0;
1576
for (int i = 0; i < oparg; i++) {
1577
PyObject *item = values[i];
1578
if (err == 0)
1579
err = PySet_Add(set, item);
1580
Py_DECREF(item);
1581
}
1582
if (err != 0) {
1583
Py_DECREF(set);
1584
ERROR_IF(true, error);
1585
}
1586
}
1587
1588
inst(BUILD_MAP, (values[oparg*2] -- map)) {
1589
map = _PyDict_FromItems(
1590
values, 2,
1591
values+1, 2,
1592
oparg);
1593
if (map == NULL)
1594
goto error;
1595
1596
DECREF_INPUTS();
1597
ERROR_IF(map == NULL, error);
1598
}
1599
1600
inst(SETUP_ANNOTATIONS, (--)) {
1601
int err;
1602
PyObject *ann_dict;
1603
if (LOCALS() == NULL) {
1604
_PyErr_Format(tstate, PyExc_SystemError,
1605
"no locals found when setting up annotations");
1606
ERROR_IF(true, error);
1607
}
1608
/* check if __annotations__ in locals()... */
1609
if (PyDict_CheckExact(LOCALS())) {
1610
ann_dict = _PyDict_GetItemWithError(LOCALS(),
1611
&_Py_ID(__annotations__));
1612
if (ann_dict == NULL) {
1613
ERROR_IF(_PyErr_Occurred(tstate), error);
1614
/* ...if not, create a new one */
1615
ann_dict = PyDict_New();
1616
ERROR_IF(ann_dict == NULL, error);
1617
err = PyDict_SetItem(LOCALS(), &_Py_ID(__annotations__),
1618
ann_dict);
1619
Py_DECREF(ann_dict);
1620
ERROR_IF(err, error);
1621
}
1622
}
1623
else {
1624
/* do the same if locals() is not a dict */
1625
ann_dict = PyObject_GetItem(LOCALS(), &_Py_ID(__annotations__));
1626
if (ann_dict == NULL) {
1627
ERROR_IF(!_PyErr_ExceptionMatches(tstate, PyExc_KeyError), error);
1628
_PyErr_Clear(tstate);
1629
ann_dict = PyDict_New();
1630
ERROR_IF(ann_dict == NULL, error);
1631
err = PyObject_SetItem(LOCALS(), &_Py_ID(__annotations__),
1632
ann_dict);
1633
Py_DECREF(ann_dict);
1634
ERROR_IF(err, error);
1635
}
1636
else {
1637
Py_DECREF(ann_dict);
1638
}
1639
}
1640
}
1641
1642
inst(BUILD_CONST_KEY_MAP, (values[oparg], keys -- map)) {
1643
if (!PyTuple_CheckExact(keys) ||
1644
PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
1645
_PyErr_SetString(tstate, PyExc_SystemError,
1646
"bad BUILD_CONST_KEY_MAP keys argument");
1647
goto error; // Pop the keys and values.
1648
}
1649
map = _PyDict_FromItems(
1650
&PyTuple_GET_ITEM(keys, 0), 1,
1651
values, 1, oparg);
1652
DECREF_INPUTS();
1653
ERROR_IF(map == NULL, error);
1654
}
1655
1656
inst(DICT_UPDATE, (update --)) {
1657
PyObject *dict = PEEK(oparg + 1); // update is still on the stack
1658
if (PyDict_Update(dict, update) < 0) {
1659
if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
1660
_PyErr_Format(tstate, PyExc_TypeError,
1661
"'%.200s' object is not a mapping",
1662
Py_TYPE(update)->tp_name);
1663
}
1664
DECREF_INPUTS();
1665
ERROR_IF(true, error);
1666
}
1667
DECREF_INPUTS();
1668
}
1669
1670
inst(DICT_MERGE, (update --)) {
1671
PyObject *dict = PEEK(oparg + 1); // update is still on the stack
1672
1673
if (_PyDict_MergeEx(dict, update, 2) < 0) {
1674
format_kwargs_error(tstate, PEEK(3 + oparg), update);
1675
DECREF_INPUTS();
1676
ERROR_IF(true, error);
1677
}
1678
DECREF_INPUTS();
1679
}
1680
1681
inst(MAP_ADD, (key, value --)) {
1682
PyObject *dict = PEEK(oparg + 2); // key, value are still on the stack
1683
assert(PyDict_CheckExact(dict));
1684
/* dict[key] = value */
1685
// Do not DECREF INPUTS because the function steals the references
1686
ERROR_IF(_PyDict_SetItem_Take2((PyDictObject *)dict, key, value) != 0, error);
1687
}
1688
1689
inst(INSTRUMENTED_LOAD_SUPER_ATTR, (unused/9, unused, unused, unused -- unused if (oparg & 1), unused)) {
1690
_PySuperAttrCache *cache = (_PySuperAttrCache *)next_instr;
1691
// cancel out the decrement that will happen in LOAD_SUPER_ATTR; we
1692
// don't want to specialize instrumented instructions
1693
INCREMENT_ADAPTIVE_COUNTER(cache->counter);
1694
GO_TO_INSTRUCTION(LOAD_SUPER_ATTR);
1695
}
1696
1697
family(load_super_attr, INLINE_CACHE_ENTRIES_LOAD_SUPER_ATTR) = {
1698
LOAD_SUPER_ATTR,
1699
LOAD_SUPER_ATTR_ATTR,
1700
LOAD_SUPER_ATTR_METHOD,
1701
};
1702
1703
inst(LOAD_SUPER_ATTR, (unused/1, global_super, class, self -- res2 if (oparg & 1), res)) {
1704
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg >> 2);
1705
int load_method = oparg & 1;
1706
#if ENABLE_SPECIALIZATION
1707
_PySuperAttrCache *cache = (_PySuperAttrCache *)next_instr;
1708
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
1709
next_instr--;
1710
_Py_Specialize_LoadSuperAttr(global_super, class, next_instr, load_method);
1711
DISPATCH_SAME_OPARG();
1712
}
1713
STAT_INC(LOAD_SUPER_ATTR, deferred);
1714
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
1715
#endif /* ENABLE_SPECIALIZATION */
1716
1717
if (opcode == INSTRUMENTED_LOAD_SUPER_ATTR) {
1718
PyObject *arg = oparg & 2 ? class : &_PyInstrumentation_MISSING;
1719
int err = _Py_call_instrumentation_2args(
1720
tstate, PY_MONITORING_EVENT_CALL,
1721
frame, next_instr-1, global_super, arg);
1722
ERROR_IF(err, error);
1723
}
1724
1725
// we make no attempt to optimize here; specializations should
1726
// handle any case whose performance we care about
1727
PyObject *stack[] = {class, self};
1728
PyObject *super = PyObject_Vectorcall(global_super, stack, oparg & 2, NULL);
1729
if (opcode == INSTRUMENTED_LOAD_SUPER_ATTR) {
1730
PyObject *arg = oparg & 2 ? class : &_PyInstrumentation_MISSING;
1731
if (super == NULL) {
1732
_Py_call_instrumentation_exc2(
1733
tstate, PY_MONITORING_EVENT_C_RAISE,
1734
frame, next_instr-1, global_super, arg);
1735
}
1736
else {
1737
int err = _Py_call_instrumentation_2args(
1738
tstate, PY_MONITORING_EVENT_C_RETURN,
1739
frame, next_instr-1, global_super, arg);
1740
if (err < 0) {
1741
Py_CLEAR(super);
1742
}
1743
}
1744
}
1745
DECREF_INPUTS();
1746
ERROR_IF(super == NULL, error);
1747
res = PyObject_GetAttr(super, name);
1748
Py_DECREF(super);
1749
ERROR_IF(res == NULL, error);
1750
}
1751
1752
pseudo(LOAD_SUPER_METHOD) = {
1753
LOAD_SUPER_ATTR,
1754
};
1755
1756
pseudo(LOAD_ZERO_SUPER_METHOD) = {
1757
LOAD_SUPER_ATTR,
1758
};
1759
1760
pseudo(LOAD_ZERO_SUPER_ATTR) = {
1761
LOAD_SUPER_ATTR,
1762
};
1763
1764
inst(LOAD_SUPER_ATTR_ATTR, (unused/1, global_super, class, self -- res2 if (oparg & 1), res)) {
1765
assert(!(oparg & 1));
1766
DEOPT_IF(global_super != (PyObject *)&PySuper_Type, LOAD_SUPER_ATTR);
1767
DEOPT_IF(!PyType_Check(class), LOAD_SUPER_ATTR);
1768
STAT_INC(LOAD_SUPER_ATTR, hit);
1769
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg >> 2);
1770
res = _PySuper_Lookup((PyTypeObject *)class, self, name, NULL);
1771
DECREF_INPUTS();
1772
ERROR_IF(res == NULL, error);
1773
}
1774
1775
inst(LOAD_SUPER_ATTR_METHOD, (unused/1, global_super, class, self -- res2, res)) {
1776
assert(oparg & 1);
1777
DEOPT_IF(global_super != (PyObject *)&PySuper_Type, LOAD_SUPER_ATTR);
1778
DEOPT_IF(!PyType_Check(class), LOAD_SUPER_ATTR);
1779
STAT_INC(LOAD_SUPER_ATTR, hit);
1780
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg >> 2);
1781
PyTypeObject *cls = (PyTypeObject *)class;
1782
int method_found = 0;
1783
res2 = _PySuper_Lookup(cls, self, name,
1784
cls->tp_getattro == PyObject_GenericGetAttr ? &method_found : NULL);
1785
Py_DECREF(global_super);
1786
Py_DECREF(class);
1787
if (res2 == NULL) {
1788
Py_DECREF(self);
1789
ERROR_IF(true, error);
1790
}
1791
if (method_found) {
1792
res = self; // transfer ownership
1793
} else {
1794
Py_DECREF(self);
1795
res = res2;
1796
res2 = NULL;
1797
}
1798
}
1799
1800
family(load_attr, INLINE_CACHE_ENTRIES_LOAD_ATTR) = {
1801
LOAD_ATTR,
1802
LOAD_ATTR_INSTANCE_VALUE,
1803
LOAD_ATTR_MODULE,
1804
LOAD_ATTR_WITH_HINT,
1805
LOAD_ATTR_SLOT,
1806
LOAD_ATTR_CLASS,
1807
LOAD_ATTR_PROPERTY,
1808
LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN,
1809
LOAD_ATTR_METHOD_WITH_VALUES,
1810
LOAD_ATTR_METHOD_NO_DICT,
1811
LOAD_ATTR_METHOD_LAZY_DICT,
1812
};
1813
1814
inst(LOAD_ATTR, (unused/9, owner -- res2 if (oparg & 1), res)) {
1815
#if ENABLE_SPECIALIZATION
1816
_PyAttrCache *cache = (_PyAttrCache *)next_instr;
1817
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
1818
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg>>1);
1819
next_instr--;
1820
_Py_Specialize_LoadAttr(owner, next_instr, name);
1821
DISPATCH_SAME_OPARG();
1822
}
1823
STAT_INC(LOAD_ATTR, deferred);
1824
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
1825
#endif /* ENABLE_SPECIALIZATION */
1826
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg >> 1);
1827
if (oparg & 1) {
1828
/* Designed to work in tandem with CALL, pushes two values. */
1829
PyObject* meth = NULL;
1830
if (_PyObject_GetMethod(owner, name, &meth)) {
1831
/* We can bypass temporary bound method object.
1832
meth is unbound method and obj is self.
1833
1834
meth | self | arg1 | ... | argN
1835
*/
1836
assert(meth != NULL); // No errors on this branch
1837
res2 = meth;
1838
res = owner; // Transfer ownership
1839
}
1840
else {
1841
/* meth is not an unbound method (but a regular attr, or
1842
something was returned by a descriptor protocol). Set
1843
the second element of the stack to NULL, to signal
1844
CALL that it's not a method call.
1845
1846
NULL | meth | arg1 | ... | argN
1847
*/
1848
DECREF_INPUTS();
1849
ERROR_IF(meth == NULL, error);
1850
res2 = NULL;
1851
res = meth;
1852
}
1853
}
1854
else {
1855
/* Classic, pushes one value. */
1856
res = PyObject_GetAttr(owner, name);
1857
DECREF_INPUTS();
1858
ERROR_IF(res == NULL, error);
1859
}
1860
}
1861
1862
pseudo(LOAD_METHOD) = {
1863
LOAD_ATTR,
1864
};
1865
1866
inst(LOAD_ATTR_INSTANCE_VALUE, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
1867
PyTypeObject *tp = Py_TYPE(owner);
1868
assert(type_version != 0);
1869
DEOPT_IF(tp->tp_version_tag != type_version, LOAD_ATTR);
1870
assert(tp->tp_dictoffset < 0);
1871
assert(tp->tp_flags & Py_TPFLAGS_MANAGED_DICT);
1872
PyDictOrValues dorv = *_PyObject_DictOrValuesPointer(owner);
1873
DEOPT_IF(!_PyDictOrValues_IsValues(dorv), LOAD_ATTR);
1874
res = _PyDictOrValues_GetValues(dorv)->values[index];
1875
DEOPT_IF(res == NULL, LOAD_ATTR);
1876
STAT_INC(LOAD_ATTR, hit);
1877
Py_INCREF(res);
1878
res2 = NULL;
1879
DECREF_INPUTS();
1880
}
1881
1882
inst(LOAD_ATTR_MODULE, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
1883
DEOPT_IF(!PyModule_CheckExact(owner), LOAD_ATTR);
1884
PyDictObject *dict = (PyDictObject *)((PyModuleObject *)owner)->md_dict;
1885
assert(dict != NULL);
1886
DEOPT_IF(dict->ma_keys->dk_version != type_version, LOAD_ATTR);
1887
assert(dict->ma_keys->dk_kind == DICT_KEYS_UNICODE);
1888
assert(index < dict->ma_keys->dk_nentries);
1889
PyDictUnicodeEntry *ep = DK_UNICODE_ENTRIES(dict->ma_keys) + index;
1890
res = ep->me_value;
1891
DEOPT_IF(res == NULL, LOAD_ATTR);
1892
STAT_INC(LOAD_ATTR, hit);
1893
Py_INCREF(res);
1894
res2 = NULL;
1895
DECREF_INPUTS();
1896
}
1897
1898
inst(LOAD_ATTR_WITH_HINT, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
1899
PyTypeObject *tp = Py_TYPE(owner);
1900
assert(type_version != 0);
1901
DEOPT_IF(tp->tp_version_tag != type_version, LOAD_ATTR);
1902
assert(tp->tp_flags & Py_TPFLAGS_MANAGED_DICT);
1903
PyDictOrValues dorv = *_PyObject_DictOrValuesPointer(owner);
1904
DEOPT_IF(_PyDictOrValues_IsValues(dorv), LOAD_ATTR);
1905
PyDictObject *dict = (PyDictObject *)_PyDictOrValues_GetDict(dorv);
1906
DEOPT_IF(dict == NULL, LOAD_ATTR);
1907
assert(PyDict_CheckExact((PyObject *)dict));
1908
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg>>1);
1909
uint16_t hint = index;
1910
DEOPT_IF(hint >= (size_t)dict->ma_keys->dk_nentries, LOAD_ATTR);
1911
if (DK_IS_UNICODE(dict->ma_keys)) {
1912
PyDictUnicodeEntry *ep = DK_UNICODE_ENTRIES(dict->ma_keys) + hint;
1913
DEOPT_IF(ep->me_key != name, LOAD_ATTR);
1914
res = ep->me_value;
1915
}
1916
else {
1917
PyDictKeyEntry *ep = DK_ENTRIES(dict->ma_keys) + hint;
1918
DEOPT_IF(ep->me_key != name, LOAD_ATTR);
1919
res = ep->me_value;
1920
}
1921
DEOPT_IF(res == NULL, LOAD_ATTR);
1922
STAT_INC(LOAD_ATTR, hit);
1923
Py_INCREF(res);
1924
res2 = NULL;
1925
DECREF_INPUTS();
1926
}
1927
1928
inst(LOAD_ATTR_SLOT, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
1929
PyTypeObject *tp = Py_TYPE(owner);
1930
assert(type_version != 0);
1931
DEOPT_IF(tp->tp_version_tag != type_version, LOAD_ATTR);
1932
char *addr = (char *)owner + index;
1933
res = *(PyObject **)addr;
1934
DEOPT_IF(res == NULL, LOAD_ATTR);
1935
STAT_INC(LOAD_ATTR, hit);
1936
Py_INCREF(res);
1937
res2 = NULL;
1938
DECREF_INPUTS();
1939
}
1940
1941
inst(LOAD_ATTR_CLASS, (unused/1, type_version/2, unused/2, descr/4, cls -- res2 if (oparg & 1), res)) {
1942
1943
DEOPT_IF(!PyType_Check(cls), LOAD_ATTR);
1944
DEOPT_IF(((PyTypeObject *)cls)->tp_version_tag != type_version,
1945
LOAD_ATTR);
1946
assert(type_version != 0);
1947
1948
STAT_INC(LOAD_ATTR, hit);
1949
res2 = NULL;
1950
res = descr;
1951
assert(res != NULL);
1952
Py_INCREF(res);
1953
DECREF_INPUTS();
1954
}
1955
1956
inst(LOAD_ATTR_PROPERTY, (unused/1, type_version/2, func_version/2, fget/4, owner -- unused if (oparg & 1), unused)) {
1957
DEOPT_IF(tstate->interp->eval_frame, LOAD_ATTR);
1958
1959
PyTypeObject *cls = Py_TYPE(owner);
1960
DEOPT_IF(cls->tp_version_tag != type_version, LOAD_ATTR);
1961
assert(type_version != 0);
1962
assert(Py_IS_TYPE(fget, &PyFunction_Type));
1963
PyFunctionObject *f = (PyFunctionObject *)fget;
1964
assert(func_version != 0);
1965
DEOPT_IF(f->func_version != func_version, LOAD_ATTR);
1966
PyCodeObject *code = (PyCodeObject *)f->func_code;
1967
assert(code->co_argcount == 1);
1968
DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize), LOAD_ATTR);
1969
STAT_INC(LOAD_ATTR, hit);
1970
Py_INCREF(fget);
1971
_PyInterpreterFrame *new_frame = _PyFrame_PushUnchecked(tstate, f, 1);
1972
// Manipulate stack directly because we exit with DISPATCH_INLINED().
1973
SET_TOP(NULL);
1974
int shrink_stack = !(oparg & 1);
1975
STACK_SHRINK(shrink_stack);
1976
new_frame->localsplus[0] = owner;
1977
SKIP_OVER(INLINE_CACHE_ENTRIES_LOAD_ATTR);
1978
frame->return_offset = 0;
1979
DISPATCH_INLINED(new_frame);
1980
}
1981
1982
inst(LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN, (unused/1, type_version/2, func_version/2, getattribute/4, owner -- unused if (oparg & 1), unused)) {
1983
DEOPT_IF(tstate->interp->eval_frame, LOAD_ATTR);
1984
PyTypeObject *cls = Py_TYPE(owner);
1985
DEOPT_IF(cls->tp_version_tag != type_version, LOAD_ATTR);
1986
assert(type_version != 0);
1987
assert(Py_IS_TYPE(getattribute, &PyFunction_Type));
1988
PyFunctionObject *f = (PyFunctionObject *)getattribute;
1989
assert(func_version != 0);
1990
DEOPT_IF(f->func_version != func_version, LOAD_ATTR);
1991
PyCodeObject *code = (PyCodeObject *)f->func_code;
1992
assert(code->co_argcount == 2);
1993
DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize), LOAD_ATTR);
1994
STAT_INC(LOAD_ATTR, hit);
1995
1996
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg >> 1);
1997
Py_INCREF(f);
1998
_PyInterpreterFrame *new_frame = _PyFrame_PushUnchecked(tstate, f, 2);
1999
// Manipulate stack directly because we exit with DISPATCH_INLINED().
2000
SET_TOP(NULL);
2001
int shrink_stack = !(oparg & 1);
2002
STACK_SHRINK(shrink_stack);
2003
new_frame->localsplus[0] = owner;
2004
new_frame->localsplus[1] = Py_NewRef(name);
2005
SKIP_OVER(INLINE_CACHE_ENTRIES_LOAD_ATTR);
2006
frame->return_offset = 0;
2007
DISPATCH_INLINED(new_frame);
2008
}
2009
2010
inst(STORE_ATTR_INSTANCE_VALUE, (unused/1, type_version/2, index/1, value, owner --)) {
2011
PyTypeObject *tp = Py_TYPE(owner);
2012
assert(type_version != 0);
2013
DEOPT_IF(tp->tp_version_tag != type_version, STORE_ATTR);
2014
assert(tp->tp_flags & Py_TPFLAGS_MANAGED_DICT);
2015
PyDictOrValues dorv = *_PyObject_DictOrValuesPointer(owner);
2016
DEOPT_IF(!_PyDictOrValues_IsValues(dorv), STORE_ATTR);
2017
STAT_INC(STORE_ATTR, hit);
2018
PyDictValues *values = _PyDictOrValues_GetValues(dorv);
2019
PyObject *old_value = values->values[index];
2020
values->values[index] = value;
2021
if (old_value == NULL) {
2022
_PyDictValues_AddToInsertionOrder(values, index);
2023
}
2024
else {
2025
Py_DECREF(old_value);
2026
}
2027
Py_DECREF(owner);
2028
}
2029
2030
inst(STORE_ATTR_WITH_HINT, (unused/1, type_version/2, hint/1, value, owner --)) {
2031
PyTypeObject *tp = Py_TYPE(owner);
2032
assert(type_version != 0);
2033
DEOPT_IF(tp->tp_version_tag != type_version, STORE_ATTR);
2034
assert(tp->tp_flags & Py_TPFLAGS_MANAGED_DICT);
2035
PyDictOrValues dorv = *_PyObject_DictOrValuesPointer(owner);
2036
DEOPT_IF(_PyDictOrValues_IsValues(dorv), STORE_ATTR);
2037
PyDictObject *dict = (PyDictObject *)_PyDictOrValues_GetDict(dorv);
2038
DEOPT_IF(dict == NULL, STORE_ATTR);
2039
assert(PyDict_CheckExact((PyObject *)dict));
2040
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
2041
DEOPT_IF(hint >= (size_t)dict->ma_keys->dk_nentries, STORE_ATTR);
2042
PyObject *old_value;
2043
uint64_t new_version;
2044
if (DK_IS_UNICODE(dict->ma_keys)) {
2045
PyDictUnicodeEntry *ep = DK_UNICODE_ENTRIES(dict->ma_keys) + hint;
2046
DEOPT_IF(ep->me_key != name, STORE_ATTR);
2047
old_value = ep->me_value;
2048
DEOPT_IF(old_value == NULL, STORE_ATTR);
2049
new_version = _PyDict_NotifyEvent(tstate->interp, PyDict_EVENT_MODIFIED, dict, name, value);
2050
ep->me_value = value;
2051
}
2052
else {
2053
PyDictKeyEntry *ep = DK_ENTRIES(dict->ma_keys) + hint;
2054
DEOPT_IF(ep->me_key != name, STORE_ATTR);
2055
old_value = ep->me_value;
2056
DEOPT_IF(old_value == NULL, STORE_ATTR);
2057
new_version = _PyDict_NotifyEvent(tstate->interp, PyDict_EVENT_MODIFIED, dict, name, value);
2058
ep->me_value = value;
2059
}
2060
Py_DECREF(old_value);
2061
STAT_INC(STORE_ATTR, hit);
2062
/* Ensure dict is GC tracked if it needs to be */
2063
if (!_PyObject_GC_IS_TRACKED(dict) && _PyObject_GC_MAY_BE_TRACKED(value)) {
2064
_PyObject_GC_TRACK(dict);
2065
}
2066
/* PEP 509 */
2067
dict->ma_version_tag = new_version;
2068
Py_DECREF(owner);
2069
}
2070
2071
inst(STORE_ATTR_SLOT, (unused/1, type_version/2, index/1, value, owner --)) {
2072
PyTypeObject *tp = Py_TYPE(owner);
2073
assert(type_version != 0);
2074
DEOPT_IF(tp->tp_version_tag != type_version, STORE_ATTR);
2075
char *addr = (char *)owner + index;
2076
STAT_INC(STORE_ATTR, hit);
2077
PyObject *old_value = *(PyObject **)addr;
2078
*(PyObject **)addr = value;
2079
Py_XDECREF(old_value);
2080
Py_DECREF(owner);
2081
}
2082
2083
family(compare_op, INLINE_CACHE_ENTRIES_COMPARE_OP) = {
2084
COMPARE_OP,
2085
COMPARE_OP_FLOAT,
2086
COMPARE_OP_INT,
2087
COMPARE_OP_STR,
2088
};
2089
2090
inst(COMPARE_OP, (unused/1, left, right -- res)) {
2091
#if ENABLE_SPECIALIZATION
2092
_PyCompareOpCache *cache = (_PyCompareOpCache *)next_instr;
2093
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
2094
next_instr--;
2095
_Py_Specialize_CompareOp(left, right, next_instr, oparg);
2096
DISPATCH_SAME_OPARG();
2097
}
2098
STAT_INC(COMPARE_OP, deferred);
2099
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
2100
#endif /* ENABLE_SPECIALIZATION */
2101
assert((oparg >> 5) <= Py_GE);
2102
res = PyObject_RichCompare(left, right, oparg >> 5);
2103
DECREF_INPUTS();
2104
ERROR_IF(res == NULL, error);
2105
if (oparg & 16) {
2106
int res_bool = PyObject_IsTrue(res);
2107
Py_DECREF(res);
2108
ERROR_IF(res_bool < 0, error);
2109
res = res_bool ? Py_True : Py_False;
2110
}
2111
}
2112
2113
inst(COMPARE_OP_FLOAT, (unused/1, left, right -- res)) {
2114
DEOPT_IF(!PyFloat_CheckExact(left), COMPARE_OP);
2115
DEOPT_IF(!PyFloat_CheckExact(right), COMPARE_OP);
2116
STAT_INC(COMPARE_OP, hit);
2117
double dleft = PyFloat_AS_DOUBLE(left);
2118
double dright = PyFloat_AS_DOUBLE(right);
2119
// 1 if NaN, 2 if <, 4 if >, 8 if ==; this matches low four bits of the oparg
2120
int sign_ish = COMPARISON_BIT(dleft, dright);
2121
_Py_DECREF_SPECIALIZED(left, _PyFloat_ExactDealloc);
2122
_Py_DECREF_SPECIALIZED(right, _PyFloat_ExactDealloc);
2123
res = (sign_ish & oparg) ? Py_True : Py_False;
2124
// It's always a bool, so we don't care about oparg & 16.
2125
}
2126
2127
// Similar to COMPARE_OP_FLOAT
2128
inst(COMPARE_OP_INT, (unused/1, left, right -- res)) {
2129
DEOPT_IF(!PyLong_CheckExact(left), COMPARE_OP);
2130
DEOPT_IF(!PyLong_CheckExact(right), COMPARE_OP);
2131
DEOPT_IF(!_PyLong_IsCompact((PyLongObject *)left), COMPARE_OP);
2132
DEOPT_IF(!_PyLong_IsCompact((PyLongObject *)right), COMPARE_OP);
2133
STAT_INC(COMPARE_OP, hit);
2134
assert(_PyLong_DigitCount((PyLongObject *)left) <= 1 &&
2135
_PyLong_DigitCount((PyLongObject *)right) <= 1);
2136
Py_ssize_t ileft = _PyLong_CompactValue((PyLongObject *)left);
2137
Py_ssize_t iright = _PyLong_CompactValue((PyLongObject *)right);
2138
// 2 if <, 4 if >, 8 if ==; this matches the low 4 bits of the oparg
2139
int sign_ish = COMPARISON_BIT(ileft, iright);
2140
_Py_DECREF_SPECIALIZED(left, (destructor)PyObject_Free);
2141
_Py_DECREF_SPECIALIZED(right, (destructor)PyObject_Free);
2142
res = (sign_ish & oparg) ? Py_True : Py_False;
2143
// It's always a bool, so we don't care about oparg & 16.
2144
}
2145
2146
// Similar to COMPARE_OP_FLOAT, but for ==, != only
2147
inst(COMPARE_OP_STR, (unused/1, left, right -- res)) {
2148
DEOPT_IF(!PyUnicode_CheckExact(left), COMPARE_OP);
2149
DEOPT_IF(!PyUnicode_CheckExact(right), COMPARE_OP);
2150
STAT_INC(COMPARE_OP, hit);
2151
int eq = _PyUnicode_Equal(left, right);
2152
assert((oparg >> 5) == Py_EQ || (oparg >> 5) == Py_NE);
2153
_Py_DECREF_SPECIALIZED(left, _PyUnicode_ExactDealloc);
2154
_Py_DECREF_SPECIALIZED(right, _PyUnicode_ExactDealloc);
2155
assert(eq == 0 || eq == 1);
2156
assert((oparg & 0xf) == COMPARISON_NOT_EQUALS || (oparg & 0xf) == COMPARISON_EQUALS);
2157
assert(COMPARISON_NOT_EQUALS + 1 == COMPARISON_EQUALS);
2158
res = ((COMPARISON_NOT_EQUALS + eq) & oparg) ? Py_True : Py_False;
2159
// It's always a bool, so we don't care about oparg & 16.
2160
}
2161
2162
inst(IS_OP, (left, right -- b)) {
2163
int res = Py_Is(left, right) ^ oparg;
2164
DECREF_INPUTS();
2165
b = res ? Py_True : Py_False;
2166
}
2167
2168
inst(CONTAINS_OP, (left, right -- b)) {
2169
int res = PySequence_Contains(right, left);
2170
DECREF_INPUTS();
2171
ERROR_IF(res < 0, error);
2172
b = (res ^ oparg) ? Py_True : Py_False;
2173
}
2174
2175
inst(CHECK_EG_MATCH, (exc_value, match_type -- rest, match)) {
2176
if (check_except_star_type_valid(tstate, match_type) < 0) {
2177
DECREF_INPUTS();
2178
ERROR_IF(true, error);
2179
}
2180
2181
match = NULL;
2182
rest = NULL;
2183
int res = exception_group_match(exc_value, match_type,
2184
&match, &rest);
2185
DECREF_INPUTS();
2186
ERROR_IF(res < 0, error);
2187
2188
assert((match == NULL) == (rest == NULL));
2189
ERROR_IF(match == NULL, error);
2190
2191
if (!Py_IsNone(match)) {
2192
PyErr_SetHandledException(match);
2193
}
2194
}
2195
2196
inst(CHECK_EXC_MATCH, (left, right -- left, b)) {
2197
assert(PyExceptionInstance_Check(left));
2198
if (check_except_type_valid(tstate, right) < 0) {
2199
DECREF_INPUTS();
2200
ERROR_IF(true, error);
2201
}
2202
2203
int res = PyErr_GivenExceptionMatches(left, right);
2204
DECREF_INPUTS();
2205
b = res ? Py_True : Py_False;
2206
}
2207
2208
inst(IMPORT_NAME, (level, fromlist -- res)) {
2209
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
2210
res = import_name(tstate, frame, name, fromlist, level);
2211
DECREF_INPUTS();
2212
ERROR_IF(res == NULL, error);
2213
}
2214
2215
inst(IMPORT_FROM, (from -- from, res)) {
2216
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
2217
res = import_from(tstate, from, name);
2218
ERROR_IF(res == NULL, error);
2219
}
2220
2221
inst(JUMP_FORWARD, (--)) {
2222
JUMPBY(oparg);
2223
}
2224
2225
inst(JUMP_BACKWARD, (--)) {
2226
_Py_CODEUNIT *here = next_instr - 1;
2227
assert(oparg <= INSTR_OFFSET());
2228
JUMPBY(1-oparg);
2229
#if ENABLE_SPECIALIZATION
2230
here[1].cache += (1 << OPTIMIZER_BITS_IN_COUNTER);
2231
if (here[1].cache > tstate->interp->optimizer_backedge_threshold) {
2232
OBJECT_STAT_INC(optimization_attempts);
2233
frame = _PyOptimizer_BackEdge(frame, here, next_instr, stack_pointer);
2234
if (frame == NULL) {
2235
frame = cframe.current_frame;
2236
goto error;
2237
}
2238
assert(frame == cframe.current_frame);
2239
here[1].cache &= ((1 << OPTIMIZER_BITS_IN_COUNTER) -1);
2240
goto resume_frame;
2241
}
2242
#endif /* ENABLE_SPECIALIZATION */
2243
CHECK_EVAL_BREAKER();
2244
}
2245
2246
pseudo(JUMP) = {
2247
JUMP_FORWARD,
2248
JUMP_BACKWARD,
2249
};
2250
2251
pseudo(JUMP_NO_INTERRUPT) = {
2252
JUMP_FORWARD,
2253
JUMP_BACKWARD_NO_INTERRUPT,
2254
};
2255
2256
inst(ENTER_EXECUTOR, (--)) {
2257
PyCodeObject *code = _PyFrame_GetCode(frame);
2258
_PyExecutorObject *executor = (_PyExecutorObject *)code->co_executors->executors[oparg&255];
2259
Py_INCREF(executor);
2260
frame = executor->execute(executor, frame, stack_pointer);
2261
if (frame == NULL) {
2262
frame = cframe.current_frame;
2263
goto resume_with_error;
2264
}
2265
goto resume_frame;
2266
}
2267
2268
inst(POP_JUMP_IF_FALSE, (cond -- )) {
2269
assert(PyBool_Check(cond));
2270
JUMPBY(oparg * Py_IsFalse(cond));
2271
}
2272
2273
inst(POP_JUMP_IF_TRUE, (cond -- )) {
2274
assert(PyBool_Check(cond));
2275
JUMPBY(oparg * Py_IsTrue(cond));
2276
}
2277
2278
inst(POP_JUMP_IF_NOT_NONE, (value -- )) {
2279
if (!Py_IsNone(value)) {
2280
DECREF_INPUTS();
2281
JUMPBY(oparg);
2282
}
2283
}
2284
2285
inst(POP_JUMP_IF_NONE, (value -- )) {
2286
if (Py_IsNone(value)) {
2287
JUMPBY(oparg);
2288
}
2289
else {
2290
DECREF_INPUTS();
2291
}
2292
}
2293
2294
inst(JUMP_BACKWARD_NO_INTERRUPT, (--)) {
2295
/* This bytecode is used in the `yield from` or `await` loop.
2296
* If there is an interrupt, we want it handled in the innermost
2297
* generator or coroutine, so we deliberately do not check it here.
2298
* (see bpo-30039).
2299
*/
2300
JUMPBY(-oparg);
2301
}
2302
2303
inst(GET_LEN, (obj -- obj, len_o)) {
2304
// PUSH(len(TOS))
2305
Py_ssize_t len_i = PyObject_Length(obj);
2306
ERROR_IF(len_i < 0, error);
2307
len_o = PyLong_FromSsize_t(len_i);
2308
ERROR_IF(len_o == NULL, error);
2309
}
2310
2311
inst(MATCH_CLASS, (subject, type, names -- attrs)) {
2312
// Pop TOS and TOS1. Set TOS to a tuple of attributes on success, or
2313
// None on failure.
2314
assert(PyTuple_CheckExact(names));
2315
attrs = match_class(tstate, subject, type, oparg, names);
2316
DECREF_INPUTS();
2317
if (attrs) {
2318
assert(PyTuple_CheckExact(attrs)); // Success!
2319
}
2320
else {
2321
ERROR_IF(_PyErr_Occurred(tstate), error); // Error!
2322
attrs = Py_None; // Failure!
2323
}
2324
}
2325
2326
inst(MATCH_MAPPING, (subject -- subject, res)) {
2327
int match = Py_TYPE(subject)->tp_flags & Py_TPFLAGS_MAPPING;
2328
res = match ? Py_True : Py_False;
2329
}
2330
2331
inst(MATCH_SEQUENCE, (subject -- subject, res)) {
2332
int match = Py_TYPE(subject)->tp_flags & Py_TPFLAGS_SEQUENCE;
2333
res = match ? Py_True : Py_False;
2334
}
2335
2336
inst(MATCH_KEYS, (subject, keys -- subject, keys, values_or_none)) {
2337
// On successful match, PUSH(values). Otherwise, PUSH(None).
2338
values_or_none = match_keys(tstate, subject, keys);
2339
ERROR_IF(values_or_none == NULL, error);
2340
}
2341
2342
inst(GET_ITER, (iterable -- iter)) {
2343
/* before: [obj]; after [getiter(obj)] */
2344
iter = PyObject_GetIter(iterable);
2345
DECREF_INPUTS();
2346
ERROR_IF(iter == NULL, error);
2347
}
2348
2349
inst(GET_YIELD_FROM_ITER, (iterable -- iter)) {
2350
/* before: [obj]; after [getiter(obj)] */
2351
if (PyCoro_CheckExact(iterable)) {
2352
/* `iterable` is a coroutine */
2353
if (!(_PyFrame_GetCode(frame)->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2354
/* and it is used in a 'yield from' expression of a
2355
regular generator. */
2356
_PyErr_SetString(tstate, PyExc_TypeError,
2357
"cannot 'yield from' a coroutine object "
2358
"in a non-coroutine generator");
2359
goto error;
2360
}
2361
iter = iterable;
2362
}
2363
else if (PyGen_CheckExact(iterable)) {
2364
iter = iterable;
2365
}
2366
else {
2367
/* `iterable` is not a generator. */
2368
iter = PyObject_GetIter(iterable);
2369
if (iter == NULL) {
2370
goto error;
2371
}
2372
DECREF_INPUTS();
2373
}
2374
}
2375
2376
// Most members of this family are "secretly" super-instructions.
2377
// When the loop is exhausted, they jump, and the jump target is
2378
// always END_FOR, which pops two values off the stack.
2379
// This is optimized by skipping that instruction and combining
2380
// its effect (popping 'iter' instead of pushing 'next'.)
2381
2382
family(for_iter, INLINE_CACHE_ENTRIES_FOR_ITER) = {
2383
FOR_ITER,
2384
FOR_ITER_LIST,
2385
FOR_ITER_TUPLE,
2386
FOR_ITER_RANGE,
2387
FOR_ITER_GEN,
2388
};
2389
2390
inst(FOR_ITER, (unused/1, iter -- iter, next)) {
2391
#if ENABLE_SPECIALIZATION
2392
_PyForIterCache *cache = (_PyForIterCache *)next_instr;
2393
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
2394
next_instr--;
2395
_Py_Specialize_ForIter(iter, next_instr, oparg);
2396
DISPATCH_SAME_OPARG();
2397
}
2398
STAT_INC(FOR_ITER, deferred);
2399
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
2400
#endif /* ENABLE_SPECIALIZATION */
2401
/* before: [iter]; after: [iter, iter()] *or* [] (and jump over END_FOR.) */
2402
next = (*Py_TYPE(iter)->tp_iternext)(iter);
2403
if (next == NULL) {
2404
if (_PyErr_Occurred(tstate)) {
2405
if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
2406
goto error;
2407
}
2408
monitor_raise(tstate, frame, next_instr-1);
2409
_PyErr_Clear(tstate);
2410
}
2411
/* iterator ended normally */
2412
assert(next_instr[INLINE_CACHE_ENTRIES_FOR_ITER + oparg].op.code == END_FOR ||
2413
next_instr[INLINE_CACHE_ENTRIES_FOR_ITER + oparg].op.code == INSTRUMENTED_END_FOR);
2414
Py_DECREF(iter);
2415
STACK_SHRINK(1);
2416
SKIP_OVER(INLINE_CACHE_ENTRIES_FOR_ITER);
2417
/* Jump forward oparg, then skip following END_FOR instruction */
2418
JUMPBY(oparg + 1);
2419
DISPATCH();
2420
}
2421
// Common case: no jump, leave it to the code generator
2422
}
2423
2424
inst(INSTRUMENTED_FOR_ITER, ( -- )) {
2425
_Py_CODEUNIT *here = next_instr-1;
2426
_Py_CODEUNIT *target;
2427
PyObject *iter = TOP();
2428
PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
2429
if (next != NULL) {
2430
PUSH(next);
2431
target = next_instr + INLINE_CACHE_ENTRIES_FOR_ITER;
2432
}
2433
else {
2434
if (_PyErr_Occurred(tstate)) {
2435
if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
2436
goto error;
2437
}
2438
monitor_raise(tstate, frame, here);
2439
_PyErr_Clear(tstate);
2440
}
2441
/* iterator ended normally */
2442
assert(next_instr[INLINE_CACHE_ENTRIES_FOR_ITER + oparg].op.code == END_FOR ||
2443
next_instr[INLINE_CACHE_ENTRIES_FOR_ITER + oparg].op.code == INSTRUMENTED_END_FOR);
2444
STACK_SHRINK(1);
2445
Py_DECREF(iter);
2446
/* Skip END_FOR */
2447
target = next_instr + INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1;
2448
}
2449
INSTRUMENTED_JUMP(here, target, PY_MONITORING_EVENT_BRANCH);
2450
}
2451
2452
inst(FOR_ITER_LIST, (unused/1, iter -- iter, next)) {
2453
DEOPT_IF(Py_TYPE(iter) != &PyListIter_Type, FOR_ITER);
2454
_PyListIterObject *it = (_PyListIterObject *)iter;
2455
STAT_INC(FOR_ITER, hit);
2456
PyListObject *seq = it->it_seq;
2457
if (seq) {
2458
if (it->it_index < PyList_GET_SIZE(seq)) {
2459
next = Py_NewRef(PyList_GET_ITEM(seq, it->it_index++));
2460
goto end_for_iter_list; // End of this instruction
2461
}
2462
it->it_seq = NULL;
2463
Py_DECREF(seq);
2464
}
2465
Py_DECREF(iter);
2466
STACK_SHRINK(1);
2467
SKIP_OVER(INLINE_CACHE_ENTRIES_FOR_ITER);
2468
/* Jump forward oparg, then skip following END_FOR instruction */
2469
JUMPBY(oparg + 1);
2470
DISPATCH();
2471
end_for_iter_list:
2472
// Common case: no jump, leave it to the code generator
2473
}
2474
2475
inst(FOR_ITER_TUPLE, (unused/1, iter -- iter, next)) {
2476
_PyTupleIterObject *it = (_PyTupleIterObject *)iter;
2477
DEOPT_IF(Py_TYPE(it) != &PyTupleIter_Type, FOR_ITER);
2478
STAT_INC(FOR_ITER, hit);
2479
PyTupleObject *seq = it->it_seq;
2480
if (seq) {
2481
if (it->it_index < PyTuple_GET_SIZE(seq)) {
2482
next = Py_NewRef(PyTuple_GET_ITEM(seq, it->it_index++));
2483
goto end_for_iter_tuple; // End of this instruction
2484
}
2485
it->it_seq = NULL;
2486
Py_DECREF(seq);
2487
}
2488
Py_DECREF(iter);
2489
STACK_SHRINK(1);
2490
SKIP_OVER(INLINE_CACHE_ENTRIES_FOR_ITER);
2491
/* Jump forward oparg, then skip following END_FOR instruction */
2492
JUMPBY(oparg + 1);
2493
DISPATCH();
2494
end_for_iter_tuple:
2495
// Common case: no jump, leave it to the code generator
2496
}
2497
2498
inst(FOR_ITER_RANGE, (unused/1, iter -- iter, next)) {
2499
_PyRangeIterObject *r = (_PyRangeIterObject *)iter;
2500
DEOPT_IF(Py_TYPE(r) != &PyRangeIter_Type, FOR_ITER);
2501
STAT_INC(FOR_ITER, hit);
2502
if (r->len <= 0) {
2503
STACK_SHRINK(1);
2504
Py_DECREF(r);
2505
SKIP_OVER(INLINE_CACHE_ENTRIES_FOR_ITER);
2506
// Jump over END_FOR instruction.
2507
JUMPBY(oparg + 1);
2508
DISPATCH();
2509
}
2510
long value = r->start;
2511
r->start = value + r->step;
2512
r->len--;
2513
next = PyLong_FromLong(value);
2514
if (next == NULL) {
2515
goto error;
2516
}
2517
}
2518
2519
inst(FOR_ITER_GEN, (unused/1, iter -- iter, unused)) {
2520
DEOPT_IF(tstate->interp->eval_frame, FOR_ITER);
2521
PyGenObject *gen = (PyGenObject *)iter;
2522
DEOPT_IF(Py_TYPE(gen) != &PyGen_Type, FOR_ITER);
2523
DEOPT_IF(gen->gi_frame_state >= FRAME_EXECUTING, FOR_ITER);
2524
STAT_INC(FOR_ITER, hit);
2525
_PyInterpreterFrame *gen_frame = (_PyInterpreterFrame *)gen->gi_iframe;
2526
frame->return_offset = oparg;
2527
_PyFrame_StackPush(gen_frame, Py_None);
2528
gen->gi_frame_state = FRAME_EXECUTING;
2529
gen->gi_exc_state.previous_item = tstate->exc_info;
2530
tstate->exc_info = &gen->gi_exc_state;
2531
SKIP_OVER(INLINE_CACHE_ENTRIES_FOR_ITER);
2532
assert(next_instr[oparg].op.code == END_FOR ||
2533
next_instr[oparg].op.code == INSTRUMENTED_END_FOR);
2534
DISPATCH_INLINED(gen_frame);
2535
}
2536
2537
inst(BEFORE_ASYNC_WITH, (mgr -- exit, res)) {
2538
PyObject *enter = _PyObject_LookupSpecial(mgr, &_Py_ID(__aenter__));
2539
if (enter == NULL) {
2540
if (!_PyErr_Occurred(tstate)) {
2541
_PyErr_Format(tstate, PyExc_TypeError,
2542
"'%.200s' object does not support the "
2543
"asynchronous context manager protocol",
2544
Py_TYPE(mgr)->tp_name);
2545
}
2546
goto error;
2547
}
2548
exit = _PyObject_LookupSpecial(mgr, &_Py_ID(__aexit__));
2549
if (exit == NULL) {
2550
if (!_PyErr_Occurred(tstate)) {
2551
_PyErr_Format(tstate, PyExc_TypeError,
2552
"'%.200s' object does not support the "
2553
"asynchronous context manager protocol "
2554
"(missed __aexit__ method)",
2555
Py_TYPE(mgr)->tp_name);
2556
}
2557
Py_DECREF(enter);
2558
goto error;
2559
}
2560
DECREF_INPUTS();
2561
res = _PyObject_CallNoArgs(enter);
2562
Py_DECREF(enter);
2563
if (res == NULL) {
2564
Py_DECREF(exit);
2565
ERROR_IF(true, error);
2566
}
2567
}
2568
2569
inst(BEFORE_WITH, (mgr -- exit, res)) {
2570
/* pop the context manager, push its __exit__ and the
2571
* value returned from calling its __enter__
2572
*/
2573
PyObject *enter = _PyObject_LookupSpecial(mgr, &_Py_ID(__enter__));
2574
if (enter == NULL) {
2575
if (!_PyErr_Occurred(tstate)) {
2576
_PyErr_Format(tstate, PyExc_TypeError,
2577
"'%.200s' object does not support the "
2578
"context manager protocol",
2579
Py_TYPE(mgr)->tp_name);
2580
}
2581
goto error;
2582
}
2583
exit = _PyObject_LookupSpecial(mgr, &_Py_ID(__exit__));
2584
if (exit == NULL) {
2585
if (!_PyErr_Occurred(tstate)) {
2586
_PyErr_Format(tstate, PyExc_TypeError,
2587
"'%.200s' object does not support the "
2588
"context manager protocol "
2589
"(missed __exit__ method)",
2590
Py_TYPE(mgr)->tp_name);
2591
}
2592
Py_DECREF(enter);
2593
goto error;
2594
}
2595
DECREF_INPUTS();
2596
res = _PyObject_CallNoArgs(enter);
2597
Py_DECREF(enter);
2598
if (res == NULL) {
2599
Py_DECREF(exit);
2600
ERROR_IF(true, error);
2601
}
2602
}
2603
2604
inst(WITH_EXCEPT_START, (exit_func, lasti, unused, val -- exit_func, lasti, unused, val, res)) {
2605
/* At the top of the stack are 4 values:
2606
- val: TOP = exc_info()
2607
- unused: SECOND = previous exception
2608
- lasti: THIRD = lasti of exception in exc_info()
2609
- exit_func: FOURTH = the context.__exit__ bound method
2610
We call FOURTH(type(TOP), TOP, GetTraceback(TOP)).
2611
Then we push the __exit__ return value.
2612
*/
2613
PyObject *exc, *tb;
2614
2615
assert(val && PyExceptionInstance_Check(val));
2616
exc = PyExceptionInstance_Class(val);
2617
tb = PyException_GetTraceback(val);
2618
Py_XDECREF(tb);
2619
assert(PyLong_Check(lasti));
2620
(void)lasti; // Shut up compiler warning if asserts are off
2621
PyObject *stack[4] = {NULL, exc, val, tb};
2622
res = PyObject_Vectorcall(exit_func, stack + 1,
2623
3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
2624
ERROR_IF(res == NULL, error);
2625
}
2626
2627
pseudo(SETUP_FINALLY) = {
2628
NOP,
2629
};
2630
2631
pseudo(SETUP_CLEANUP) = {
2632
NOP,
2633
};
2634
2635
pseudo(SETUP_WITH) = {
2636
NOP,
2637
};
2638
2639
pseudo(POP_BLOCK) = {
2640
NOP,
2641
};
2642
2643
inst(PUSH_EXC_INFO, (new_exc -- prev_exc, new_exc)) {
2644
_PyErr_StackItem *exc_info = tstate->exc_info;
2645
if (exc_info->exc_value != NULL) {
2646
prev_exc = exc_info->exc_value;
2647
}
2648
else {
2649
prev_exc = Py_None;
2650
}
2651
assert(PyExceptionInstance_Check(new_exc));
2652
exc_info->exc_value = Py_NewRef(new_exc);
2653
}
2654
2655
inst(LOAD_ATTR_METHOD_WITH_VALUES, (unused/1, type_version/2, keys_version/2, descr/4, self -- res2 if (oparg & 1), res)) {
2656
/* Cached method object */
2657
PyTypeObject *self_cls = Py_TYPE(self);
2658
assert(type_version != 0);
2659
DEOPT_IF(self_cls->tp_version_tag != type_version, LOAD_ATTR);
2660
assert(self_cls->tp_flags & Py_TPFLAGS_MANAGED_DICT);
2661
PyDictOrValues dorv = *_PyObject_DictOrValuesPointer(self);
2662
DEOPT_IF(!_PyDictOrValues_IsValues(dorv), LOAD_ATTR);
2663
PyHeapTypeObject *self_heap_type = (PyHeapTypeObject *)self_cls;
2664
DEOPT_IF(self_heap_type->ht_cached_keys->dk_version !=
2665
keys_version, LOAD_ATTR);
2666
STAT_INC(LOAD_ATTR, hit);
2667
assert(descr != NULL);
2668
res2 = Py_NewRef(descr);
2669
assert(_PyType_HasFeature(Py_TYPE(res2), Py_TPFLAGS_METHOD_DESCRIPTOR));
2670
res = self;
2671
assert(oparg & 1);
2672
}
2673
2674
inst(LOAD_ATTR_METHOD_NO_DICT, (unused/1, type_version/2, unused/2, descr/4, self -- res2 if (oparg & 1), res)) {
2675
PyTypeObject *self_cls = Py_TYPE(self);
2676
DEOPT_IF(self_cls->tp_version_tag != type_version, LOAD_ATTR);
2677
assert(self_cls->tp_dictoffset == 0);
2678
STAT_INC(LOAD_ATTR, hit);
2679
assert(descr != NULL);
2680
assert(_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR));
2681
res2 = Py_NewRef(descr);
2682
res = self;
2683
assert(oparg & 1);
2684
}
2685
2686
inst(LOAD_ATTR_METHOD_LAZY_DICT, (unused/1, type_version/2, unused/2, descr/4, self -- res2 if (oparg & 1), res)) {
2687
PyTypeObject *self_cls = Py_TYPE(self);
2688
DEOPT_IF(self_cls->tp_version_tag != type_version, LOAD_ATTR);
2689
Py_ssize_t dictoffset = self_cls->tp_dictoffset;
2690
assert(dictoffset > 0);
2691
PyObject *dict = *(PyObject **)((char *)self + dictoffset);
2692
/* This object has a __dict__, just not yet created */
2693
DEOPT_IF(dict != NULL, LOAD_ATTR);
2694
STAT_INC(LOAD_ATTR, hit);
2695
assert(descr != NULL);
2696
assert(_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR));
2697
res2 = Py_NewRef(descr);
2698
res = self;
2699
assert(oparg & 1);
2700
}
2701
2702
inst(KW_NAMES, (--)) {
2703
assert(kwnames == NULL);
2704
assert(oparg < PyTuple_GET_SIZE(FRAME_CO_CONSTS));
2705
kwnames = GETITEM(FRAME_CO_CONSTS, oparg);
2706
}
2707
2708
inst(INSTRUMENTED_CALL, ( -- )) {
2709
int is_meth = PEEK(oparg+2) != NULL;
2710
int total_args = oparg + is_meth;
2711
PyObject *function = PEEK(total_args + 1);
2712
PyObject *arg = total_args == 0 ?
2713
&_PyInstrumentation_MISSING : PEEK(total_args);
2714
int err = _Py_call_instrumentation_2args(
2715
tstate, PY_MONITORING_EVENT_CALL,
2716
frame, next_instr-1, function, arg);
2717
ERROR_IF(err, error);
2718
_PyCallCache *cache = (_PyCallCache *)next_instr;
2719
INCREMENT_ADAPTIVE_COUNTER(cache->counter);
2720
GO_TO_INSTRUCTION(CALL);
2721
}
2722
2723
// Cache layout: counter/1, func_version/2
2724
// Neither CALL_INTRINSIC_1/2 nor CALL_FUNCTION_EX are members!
2725
family(call, INLINE_CACHE_ENTRIES_CALL) = {
2726
CALL,
2727
CALL_BOUND_METHOD_EXACT_ARGS,
2728
CALL_PY_EXACT_ARGS,
2729
CALL_PY_WITH_DEFAULTS,
2730
CALL_NO_KW_TYPE_1,
2731
CALL_NO_KW_STR_1,
2732
CALL_NO_KW_TUPLE_1,
2733
CALL_BUILTIN_CLASS,
2734
CALL_NO_KW_BUILTIN_O,
2735
CALL_NO_KW_BUILTIN_FAST,
2736
CALL_BUILTIN_FAST_WITH_KEYWORDS,
2737
CALL_NO_KW_LEN,
2738
CALL_NO_KW_ISINSTANCE,
2739
CALL_NO_KW_LIST_APPEND,
2740
CALL_NO_KW_METHOD_DESCRIPTOR_O,
2741
CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS,
2742
CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS,
2743
CALL_NO_KW_METHOD_DESCRIPTOR_FAST,
2744
CALL_NO_KW_ALLOC_AND_ENTER_INIT,
2745
};
2746
2747
// On entry, the stack is either
2748
// [NULL, callable, arg1, arg2, ...]
2749
// or
2750
// [method, self, arg1, arg2, ...]
2751
// (Some args may be keywords, see KW_NAMES, which sets 'kwnames'.)
2752
// On exit, the stack is [result].
2753
// When calling Python, inline the call using DISPATCH_INLINED().
2754
inst(CALL, (unused/1, unused/2, method, callable, args[oparg] -- res)) {
2755
int is_meth = method != NULL;
2756
int total_args = oparg;
2757
if (is_meth) {
2758
callable = method;
2759
args--;
2760
total_args++;
2761
}
2762
#if ENABLE_SPECIALIZATION
2763
_PyCallCache *cache = (_PyCallCache *)next_instr;
2764
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
2765
next_instr--;
2766
_Py_Specialize_Call(callable, next_instr, total_args, kwnames);
2767
DISPATCH_SAME_OPARG();
2768
}
2769
STAT_INC(CALL, deferred);
2770
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
2771
#endif /* ENABLE_SPECIALIZATION */
2772
if (!is_meth && Py_TYPE(callable) == &PyMethod_Type) {
2773
is_meth = 1; // For consistenct; it's dead, though
2774
args--;
2775
total_args++;
2776
PyObject *self = ((PyMethodObject *)callable)->im_self;
2777
args[0] = Py_NewRef(self);
2778
method = ((PyMethodObject *)callable)->im_func;
2779
args[-1] = Py_NewRef(method);
2780
Py_DECREF(callable);
2781
callable = method;
2782
}
2783
int positional_args = total_args - KWNAMES_LEN();
2784
// Check if the call can be inlined or not
2785
if (Py_TYPE(callable) == &PyFunction_Type &&
2786
tstate->interp->eval_frame == NULL &&
2787
((PyFunctionObject *)callable)->vectorcall == _PyFunction_Vectorcall)
2788
{
2789
int code_flags = ((PyCodeObject*)PyFunction_GET_CODE(callable))->co_flags;
2790
PyObject *locals = code_flags & CO_OPTIMIZED ? NULL : Py_NewRef(PyFunction_GET_GLOBALS(callable));
2791
_PyInterpreterFrame *new_frame = _PyEvalFramePushAndInit(
2792
tstate, (PyFunctionObject *)callable, locals,
2793
args, positional_args, kwnames
2794
);
2795
kwnames = NULL;
2796
// Manipulate stack directly since we leave using DISPATCH_INLINED().
2797
STACK_SHRINK(oparg + 2);
2798
// The frame has stolen all the arguments from the stack,
2799
// so there is no need to clean them up.
2800
if (new_frame == NULL) {
2801
goto error;
2802
}
2803
SKIP_OVER(INLINE_CACHE_ENTRIES_CALL);
2804
frame->return_offset = 0;
2805
DISPATCH_INLINED(new_frame);
2806
}
2807
/* Callable is not a normal Python function */
2808
res = PyObject_Vectorcall(
2809
callable, args,
2810
positional_args | PY_VECTORCALL_ARGUMENTS_OFFSET,
2811
kwnames);
2812
if (opcode == INSTRUMENTED_CALL) {
2813
PyObject *arg = total_args == 0 ?
2814
&_PyInstrumentation_MISSING : PEEK(total_args);
2815
if (res == NULL) {
2816
_Py_call_instrumentation_exc2(
2817
tstate, PY_MONITORING_EVENT_C_RAISE,
2818
frame, next_instr-1, callable, arg);
2819
}
2820
else {
2821
int err = _Py_call_instrumentation_2args(
2822
tstate, PY_MONITORING_EVENT_C_RETURN,
2823
frame, next_instr-1, callable, arg);
2824
if (err < 0) {
2825
Py_CLEAR(res);
2826
}
2827
}
2828
}
2829
kwnames = NULL;
2830
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
2831
Py_DECREF(callable);
2832
for (int i = 0; i < total_args; i++) {
2833
Py_DECREF(args[i]);
2834
}
2835
ERROR_IF(res == NULL, error);
2836
CHECK_EVAL_BREAKER();
2837
}
2838
2839
// Start out with [NULL, bound_method, arg1, arg2, ...]
2840
// Transform to [callable, self, arg1, arg2, ...]
2841
// Then fall through to CALL_PY_EXACT_ARGS
2842
inst(CALL_BOUND_METHOD_EXACT_ARGS, (unused/1, unused/2, method, callable, unused[oparg] -- unused)) {
2843
DEOPT_IF(method != NULL, CALL);
2844
DEOPT_IF(Py_TYPE(callable) != &PyMethod_Type, CALL);
2845
STAT_INC(CALL, hit);
2846
PyObject *self = ((PyMethodObject *)callable)->im_self;
2847
PEEK(oparg + 1) = Py_NewRef(self); // callable
2848
PyObject *meth = ((PyMethodObject *)callable)->im_func;
2849
PEEK(oparg + 2) = Py_NewRef(meth); // method
2850
Py_DECREF(callable);
2851
GO_TO_INSTRUCTION(CALL_PY_EXACT_ARGS);
2852
}
2853
2854
inst(CALL_PY_EXACT_ARGS, (unused/1, func_version/2, method, callable, args[oparg] -- unused)) {
2855
assert(kwnames == NULL);
2856
DEOPT_IF(tstate->interp->eval_frame, CALL);
2857
int is_meth = method != NULL;
2858
int argcount = oparg;
2859
if (is_meth) {
2860
callable = method;
2861
args--;
2862
argcount++;
2863
}
2864
DEOPT_IF(!PyFunction_Check(callable), CALL);
2865
PyFunctionObject *func = (PyFunctionObject *)callable;
2866
DEOPT_IF(func->func_version != func_version, CALL);
2867
PyCodeObject *code = (PyCodeObject *)func->func_code;
2868
DEOPT_IF(code->co_argcount != argcount, CALL);
2869
DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize), CALL);
2870
STAT_INC(CALL, hit);
2871
_PyInterpreterFrame *new_frame = _PyFrame_PushUnchecked(tstate, func, argcount);
2872
for (int i = 0; i < argcount; i++) {
2873
new_frame->localsplus[i] = args[i];
2874
}
2875
// Manipulate stack directly since we leave using DISPATCH_INLINED().
2876
STACK_SHRINK(oparg + 2);
2877
SKIP_OVER(INLINE_CACHE_ENTRIES_CALL);
2878
frame->return_offset = 0;
2879
DISPATCH_INLINED(new_frame);
2880
}
2881
2882
inst(CALL_PY_WITH_DEFAULTS, (unused/1, func_version/2, method, callable, args[oparg] -- unused)) {
2883
assert(kwnames == NULL);
2884
DEOPT_IF(tstate->interp->eval_frame, CALL);
2885
int is_meth = method != NULL;
2886
int argcount = oparg;
2887
if (is_meth) {
2888
callable = method;
2889
args--;
2890
argcount++;
2891
}
2892
DEOPT_IF(!PyFunction_Check(callable), CALL);
2893
PyFunctionObject *func = (PyFunctionObject *)callable;
2894
DEOPT_IF(func->func_version != func_version, CALL);
2895
PyCodeObject *code = (PyCodeObject *)func->func_code;
2896
assert(func->func_defaults);
2897
assert(PyTuple_CheckExact(func->func_defaults));
2898
int defcount = (int)PyTuple_GET_SIZE(func->func_defaults);
2899
assert(defcount <= code->co_argcount);
2900
int min_args = code->co_argcount - defcount;
2901
DEOPT_IF(argcount > code->co_argcount, CALL);
2902
DEOPT_IF(argcount < min_args, CALL);
2903
DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize), CALL);
2904
STAT_INC(CALL, hit);
2905
_PyInterpreterFrame *new_frame = _PyFrame_PushUnchecked(tstate, func, code->co_argcount);
2906
for (int i = 0; i < argcount; i++) {
2907
new_frame->localsplus[i] = args[i];
2908
}
2909
for (int i = argcount; i < code->co_argcount; i++) {
2910
PyObject *def = PyTuple_GET_ITEM(func->func_defaults, i - min_args);
2911
new_frame->localsplus[i] = Py_NewRef(def);
2912
}
2913
// Manipulate stack and cache directly since we leave using DISPATCH_INLINED().
2914
STACK_SHRINK(oparg + 2);
2915
SKIP_OVER(INLINE_CACHE_ENTRIES_CALL);
2916
frame->return_offset = 0;
2917
DISPATCH_INLINED(new_frame);
2918
}
2919
2920
inst(CALL_NO_KW_TYPE_1, (unused/1, unused/2, null, callable, args[oparg] -- res)) {
2921
assert(kwnames == NULL);
2922
assert(oparg == 1);
2923
DEOPT_IF(null != NULL, CALL);
2924
PyObject *obj = args[0];
2925
DEOPT_IF(callable != (PyObject *)&PyType_Type, CALL);
2926
STAT_INC(CALL, hit);
2927
res = Py_NewRef(Py_TYPE(obj));
2928
Py_DECREF(obj);
2929
Py_DECREF(&PyType_Type); // I.e., callable
2930
}
2931
2932
inst(CALL_NO_KW_STR_1, (unused/1, unused/2, null, callable, args[oparg] -- res)) {
2933
assert(kwnames == NULL);
2934
assert(oparg == 1);
2935
DEOPT_IF(null != NULL, CALL);
2936
DEOPT_IF(callable != (PyObject *)&PyUnicode_Type, CALL);
2937
STAT_INC(CALL, hit);
2938
PyObject *arg = args[0];
2939
res = PyObject_Str(arg);
2940
Py_DECREF(arg);
2941
Py_DECREF(&PyUnicode_Type); // I.e., callable
2942
ERROR_IF(res == NULL, error);
2943
CHECK_EVAL_BREAKER();
2944
}
2945
2946
inst(CALL_NO_KW_TUPLE_1, (unused/1, unused/2, null, callable, args[oparg] -- res)) {
2947
assert(kwnames == NULL);
2948
assert(oparg == 1);
2949
DEOPT_IF(null != NULL, CALL);
2950
DEOPT_IF(callable != (PyObject *)&PyTuple_Type, CALL);
2951
STAT_INC(CALL, hit);
2952
PyObject *arg = args[0];
2953
res = PySequence_Tuple(arg);
2954
Py_DECREF(arg);
2955
Py_DECREF(&PyTuple_Type); // I.e., tuple
2956
ERROR_IF(res == NULL, error);
2957
CHECK_EVAL_BREAKER();
2958
}
2959
2960
inst(CALL_NO_KW_ALLOC_AND_ENTER_INIT, (unused/1, unused/2, null, callable, args[oparg] -- unused)) {
2961
/* This instruction does the following:
2962
* 1. Creates the object (by calling ``object.__new__``)
2963
* 2. Pushes a shim frame to the frame stack (to cleanup after ``__init__``)
2964
* 3. Pushes the frame for ``__init__`` to the frame stack
2965
* */
2966
assert(kwnames == NULL);
2967
_PyCallCache *cache = (_PyCallCache *)next_instr;
2968
DEOPT_IF(null != NULL, CALL);
2969
DEOPT_IF(!PyType_Check(callable), CALL);
2970
PyTypeObject *tp = (PyTypeObject *)callable;
2971
DEOPT_IF(tp->tp_version_tag != read_u32(cache->func_version), CALL);
2972
PyHeapTypeObject *cls = (PyHeapTypeObject *)callable;
2973
PyFunctionObject *init = (PyFunctionObject *)cls->_spec_cache.init;
2974
PyCodeObject *code = (PyCodeObject *)init->func_code;
2975
DEOPT_IF(code->co_argcount != oparg+1, CALL);
2976
DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize + _Py_InitCleanup.co_framesize), CALL);
2977
STAT_INC(CALL, hit);
2978
PyObject *self = _PyType_NewManagedObject(tp);
2979
if (self == NULL) {
2980
goto error;
2981
}
2982
Py_DECREF(tp);
2983
if (_Py_EnterRecursivePy(tstate)) {
2984
goto exit_unwind;
2985
}
2986
_PyInterpreterFrame *shim = _PyFrame_PushTrampolineUnchecked(
2987
tstate, (PyCodeObject *)&_Py_InitCleanup, 1, 0);
2988
assert(_PyCode_CODE((PyCodeObject *)shim->f_executable)[1].op.code == EXIT_INIT_CHECK);
2989
/* Push self onto stack of shim */
2990
Py_INCREF(self);
2991
shim->localsplus[0] = self;
2992
Py_INCREF(init);
2993
_PyInterpreterFrame *init_frame = _PyFrame_PushUnchecked(tstate, init, oparg+1);
2994
/* Copy self followed by args to __init__ frame */
2995
init_frame->localsplus[0] = self;
2996
for (int i = 0; i < oparg; i++) {
2997
init_frame->localsplus[i+1] = args[i];
2998
}
2999
SKIP_OVER(INLINE_CACHE_ENTRIES_CALL);
3000
frame->prev_instr = next_instr - 1;
3001
frame->return_offset = 0;
3002
STACK_SHRINK(oparg+2);
3003
_PyFrame_SetStackPointer(frame, stack_pointer);
3004
/* Link frames */
3005
init_frame->previous = shim;
3006
shim->previous = frame;
3007
frame = cframe.current_frame = init_frame;
3008
CALL_STAT_INC(inlined_py_calls);
3009
goto start_frame;
3010
}
3011
3012
inst(EXIT_INIT_CHECK, (should_be_none -- )) {
3013
assert(STACK_LEVEL() == 2);
3014
if (should_be_none != Py_None) {
3015
PyErr_Format(PyExc_TypeError,
3016
"__init__() should return None, not '%.200s'",
3017
Py_TYPE(should_be_none)->tp_name);
3018
goto error;
3019
}
3020
}
3021
3022
inst(CALL_BUILTIN_CLASS, (unused/1, unused/2, method, callable, args[oparg] -- res)) {
3023
int is_meth = method != NULL;
3024
int total_args = oparg;
3025
if (is_meth) {
3026
callable = method;
3027
args--;
3028
total_args++;
3029
}
3030
int kwnames_len = KWNAMES_LEN();
3031
DEOPT_IF(!PyType_Check(callable), CALL);
3032
PyTypeObject *tp = (PyTypeObject *)callable;
3033
DEOPT_IF(tp->tp_vectorcall == NULL, CALL);
3034
STAT_INC(CALL, hit);
3035
res = tp->tp_vectorcall((PyObject *)tp, args,
3036
total_args - kwnames_len, kwnames);
3037
kwnames = NULL;
3038
/* Free the arguments. */
3039
for (int i = 0; i < total_args; i++) {
3040
Py_DECREF(args[i]);
3041
}
3042
Py_DECREF(tp);
3043
ERROR_IF(res == NULL, error);
3044
CHECK_EVAL_BREAKER();
3045
}
3046
3047
inst(CALL_NO_KW_BUILTIN_O, (unused/1, unused/2, method, callable, args[oparg] -- res)) {
3048
/* Builtin METH_O functions */
3049
assert(kwnames == NULL);
3050
int is_meth = method != NULL;
3051
int total_args = oparg;
3052
if (is_meth) {
3053
callable = method;
3054
args--;
3055
total_args++;
3056
}
3057
DEOPT_IF(total_args != 1, CALL);
3058
DEOPT_IF(!PyCFunction_CheckExact(callable), CALL);
3059
DEOPT_IF(PyCFunction_GET_FLAGS(callable) != METH_O, CALL);
3060
STAT_INC(CALL, hit);
3061
PyCFunction cfunc = PyCFunction_GET_FUNCTION(callable);
3062
// This is slower but CPython promises to check all non-vectorcall
3063
// function calls.
3064
if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object")) {
3065
goto error;
3066
}
3067
PyObject *arg = args[0];
3068
res = _PyCFunction_TrampolineCall(cfunc, PyCFunction_GET_SELF(callable), arg);
3069
_Py_LeaveRecursiveCallTstate(tstate);
3070
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
3071
3072
Py_DECREF(arg);
3073
Py_DECREF(callable);
3074
ERROR_IF(res == NULL, error);
3075
CHECK_EVAL_BREAKER();
3076
}
3077
3078
inst(CALL_NO_KW_BUILTIN_FAST, (unused/1, unused/2, method, callable, args[oparg] -- res)) {
3079
/* Builtin METH_FASTCALL functions, without keywords */
3080
assert(kwnames == NULL);
3081
int is_meth = method != NULL;
3082
int total_args = oparg;
3083
if (is_meth) {
3084
callable = method;
3085
args--;
3086
total_args++;
3087
}
3088
DEOPT_IF(!PyCFunction_CheckExact(callable), CALL);
3089
DEOPT_IF(PyCFunction_GET_FLAGS(callable) != METH_FASTCALL, CALL);
3090
STAT_INC(CALL, hit);
3091
PyCFunction cfunc = PyCFunction_GET_FUNCTION(callable);
3092
/* res = func(self, args, nargs) */
3093
res = ((_PyCFunctionFast)(void(*)(void))cfunc)(
3094
PyCFunction_GET_SELF(callable),
3095
args,
3096
total_args);
3097
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
3098
3099
/* Free the arguments. */
3100
for (int i = 0; i < total_args; i++) {
3101
Py_DECREF(args[i]);
3102
}
3103
Py_DECREF(callable);
3104
ERROR_IF(res == NULL, error);
3105
/* Not deopting because this doesn't mean our optimization was
3106
wrong. `res` can be NULL for valid reasons. Eg. getattr(x,
3107
'invalid'). In those cases an exception is set, so we must
3108
handle it.
3109
*/
3110
CHECK_EVAL_BREAKER();
3111
}
3112
3113
inst(CALL_BUILTIN_FAST_WITH_KEYWORDS, (unused/1, unused/2, method, callable, args[oparg] -- res)) {
3114
/* Builtin METH_FASTCALL | METH_KEYWORDS functions */
3115
int is_meth = method != NULL;
3116
int total_args = oparg;
3117
if (is_meth) {
3118
callable = method;
3119
args--;
3120
total_args++;
3121
}
3122
DEOPT_IF(!PyCFunction_CheckExact(callable), CALL);
3123
DEOPT_IF(PyCFunction_GET_FLAGS(callable) !=
3124
(METH_FASTCALL | METH_KEYWORDS), CALL);
3125
STAT_INC(CALL, hit);
3126
/* res = func(self, args, nargs, kwnames) */
3127
_PyCFunctionFastWithKeywords cfunc =
3128
(_PyCFunctionFastWithKeywords)(void(*)(void))
3129
PyCFunction_GET_FUNCTION(callable);
3130
res = cfunc(
3131
PyCFunction_GET_SELF(callable),
3132
args,
3133
total_args - KWNAMES_LEN(),
3134
kwnames
3135
);
3136
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
3137
kwnames = NULL;
3138
3139
/* Free the arguments. */
3140
for (int i = 0; i < total_args; i++) {
3141
Py_DECREF(args[i]);
3142
}
3143
Py_DECREF(callable);
3144
ERROR_IF(res == NULL, error);
3145
CHECK_EVAL_BREAKER();
3146
}
3147
3148
inst(CALL_NO_KW_LEN, (unused/1, unused/2, method, callable, args[oparg] -- res)) {
3149
assert(kwnames == NULL);
3150
/* len(o) */
3151
int is_meth = method != NULL;
3152
int total_args = oparg;
3153
if (is_meth) {
3154
callable = method;
3155
args--;
3156
total_args++;
3157
}
3158
DEOPT_IF(total_args != 1, CALL);
3159
PyInterpreterState *interp = _PyInterpreterState_GET();
3160
DEOPT_IF(callable != interp->callable_cache.len, CALL);
3161
STAT_INC(CALL, hit);
3162
PyObject *arg = args[0];
3163
Py_ssize_t len_i = PyObject_Length(arg);
3164
if (len_i < 0) {
3165
goto error;
3166
}
3167
res = PyLong_FromSsize_t(len_i);
3168
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
3169
3170
Py_DECREF(callable);
3171
Py_DECREF(arg);
3172
ERROR_IF(res == NULL, error);
3173
}
3174
3175
inst(CALL_NO_KW_ISINSTANCE, (unused/1, unused/2, method, callable, args[oparg] -- res)) {
3176
assert(kwnames == NULL);
3177
/* isinstance(o, o2) */
3178
int is_meth = method != NULL;
3179
int total_args = oparg;
3180
if (is_meth) {
3181
callable = method;
3182
args--;
3183
total_args++;
3184
}
3185
DEOPT_IF(total_args != 2, CALL);
3186
PyInterpreterState *interp = _PyInterpreterState_GET();
3187
DEOPT_IF(callable != interp->callable_cache.isinstance, CALL);
3188
STAT_INC(CALL, hit);
3189
PyObject *cls = args[1];
3190
PyObject *inst = args[0];
3191
int retval = PyObject_IsInstance(inst, cls);
3192
if (retval < 0) {
3193
goto error;
3194
}
3195
res = PyBool_FromLong(retval);
3196
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
3197
3198
Py_DECREF(inst);
3199
Py_DECREF(cls);
3200
Py_DECREF(callable);
3201
ERROR_IF(res == NULL, error);
3202
}
3203
3204
// This is secretly a super-instruction
3205
inst(CALL_NO_KW_LIST_APPEND, (unused/1, unused/2, method, self, args[oparg] -- unused)) {
3206
assert(kwnames == NULL);
3207
assert(oparg == 1);
3208
assert(method != NULL);
3209
PyInterpreterState *interp = _PyInterpreterState_GET();
3210
DEOPT_IF(method != interp->callable_cache.list_append, CALL);
3211
DEOPT_IF(!PyList_Check(self), CALL);
3212
STAT_INC(CALL, hit);
3213
if (_PyList_AppendTakeRef((PyListObject *)self, args[0]) < 0) {
3214
goto pop_1_error; // Since arg is DECREF'ed already
3215
}
3216
Py_DECREF(self);
3217
Py_DECREF(method);
3218
STACK_SHRINK(3);
3219
// CALL + POP_TOP
3220
SKIP_OVER(INLINE_CACHE_ENTRIES_CALL + 1);
3221
assert(next_instr[-1].op.code == POP_TOP);
3222
DISPATCH();
3223
}
3224
3225
inst(CALL_NO_KW_METHOD_DESCRIPTOR_O, (unused/1, unused/2, method, unused, args[oparg] -- res)) {
3226
assert(kwnames == NULL);
3227
int is_meth = method != NULL;
3228
int total_args = oparg;
3229
if (is_meth) {
3230
args--;
3231
total_args++;
3232
}
3233
PyMethodDescrObject *callable =
3234
(PyMethodDescrObject *)PEEK(total_args + 1);
3235
DEOPT_IF(total_args != 2, CALL);
3236
DEOPT_IF(!Py_IS_TYPE(callable, &PyMethodDescr_Type), CALL);
3237
PyMethodDef *meth = callable->d_method;
3238
DEOPT_IF(meth->ml_flags != METH_O, CALL);
3239
PyObject *arg = args[1];
3240
PyObject *self = args[0];
3241
DEOPT_IF(!Py_IS_TYPE(self, callable->d_common.d_type), CALL);
3242
STAT_INC(CALL, hit);
3243
PyCFunction cfunc = meth->ml_meth;
3244
// This is slower but CPython promises to check all non-vectorcall
3245
// function calls.
3246
if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object")) {
3247
goto error;
3248
}
3249
res = _PyCFunction_TrampolineCall(cfunc, self, arg);
3250
_Py_LeaveRecursiveCallTstate(tstate);
3251
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
3252
Py_DECREF(self);
3253
Py_DECREF(arg);
3254
Py_DECREF(callable);
3255
ERROR_IF(res == NULL, error);
3256
CHECK_EVAL_BREAKER();
3257
}
3258
3259
inst(CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS, (unused/1, unused/2, method, unused, args[oparg] -- res)) {
3260
int is_meth = method != NULL;
3261
int total_args = oparg;
3262
if (is_meth) {
3263
args--;
3264
total_args++;
3265
}
3266
PyMethodDescrObject *callable =
3267
(PyMethodDescrObject *)PEEK(total_args + 1);
3268
DEOPT_IF(!Py_IS_TYPE(callable, &PyMethodDescr_Type), CALL);
3269
PyMethodDef *meth = callable->d_method;
3270
DEOPT_IF(meth->ml_flags != (METH_FASTCALL|METH_KEYWORDS), CALL);
3271
PyTypeObject *d_type = callable->d_common.d_type;
3272
PyObject *self = args[0];
3273
DEOPT_IF(!Py_IS_TYPE(self, d_type), CALL);
3274
STAT_INC(CALL, hit);
3275
int nargs = total_args - 1;
3276
_PyCFunctionFastWithKeywords cfunc =
3277
(_PyCFunctionFastWithKeywords)(void(*)(void))meth->ml_meth;
3278
res = cfunc(self, args + 1, nargs - KWNAMES_LEN(), kwnames);
3279
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
3280
kwnames = NULL;
3281
3282
/* Free the arguments. */
3283
for (int i = 0; i < total_args; i++) {
3284
Py_DECREF(args[i]);
3285
}
3286
Py_DECREF(callable);
3287
ERROR_IF(res == NULL, error);
3288
CHECK_EVAL_BREAKER();
3289
}
3290
3291
inst(CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS, (unused/1, unused/2, method, unused, args[oparg] -- res)) {
3292
assert(kwnames == NULL);
3293
assert(oparg == 0 || oparg == 1);
3294
int is_meth = method != NULL;
3295
int total_args = oparg;
3296
if (is_meth) {
3297
args--;
3298
total_args++;
3299
}
3300
DEOPT_IF(total_args != 1, CALL);
3301
PyMethodDescrObject *callable = (PyMethodDescrObject *)SECOND();
3302
DEOPT_IF(!Py_IS_TYPE(callable, &PyMethodDescr_Type), CALL);
3303
PyMethodDef *meth = callable->d_method;
3304
PyObject *self = args[0];
3305
DEOPT_IF(!Py_IS_TYPE(self, callable->d_common.d_type), CALL);
3306
DEOPT_IF(meth->ml_flags != METH_NOARGS, CALL);
3307
STAT_INC(CALL, hit);
3308
PyCFunction cfunc = meth->ml_meth;
3309
// This is slower but CPython promises to check all non-vectorcall
3310
// function calls.
3311
if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object")) {
3312
goto error;
3313
}
3314
res = _PyCFunction_TrampolineCall(cfunc, self, NULL);
3315
_Py_LeaveRecursiveCallTstate(tstate);
3316
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
3317
Py_DECREF(self);
3318
Py_DECREF(callable);
3319
ERROR_IF(res == NULL, error);
3320
CHECK_EVAL_BREAKER();
3321
}
3322
3323
inst(CALL_NO_KW_METHOD_DESCRIPTOR_FAST, (unused/1, unused/2, method, unused, args[oparg] -- res)) {
3324
assert(kwnames == NULL);
3325
int is_meth = method != NULL;
3326
int total_args = oparg;
3327
if (is_meth) {
3328
args--;
3329
total_args++;
3330
}
3331
PyMethodDescrObject *callable =
3332
(PyMethodDescrObject *)PEEK(total_args + 1);
3333
/* Builtin METH_FASTCALL methods, without keywords */
3334
DEOPT_IF(!Py_IS_TYPE(callable, &PyMethodDescr_Type), CALL);
3335
PyMethodDef *meth = callable->d_method;
3336
DEOPT_IF(meth->ml_flags != METH_FASTCALL, CALL);
3337
PyObject *self = args[0];
3338
DEOPT_IF(!Py_IS_TYPE(self, callable->d_common.d_type), CALL);
3339
STAT_INC(CALL, hit);
3340
_PyCFunctionFast cfunc =
3341
(_PyCFunctionFast)(void(*)(void))meth->ml_meth;
3342
int nargs = total_args - 1;
3343
res = cfunc(self, args + 1, nargs);
3344
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
3345
/* Clear the stack of the arguments. */
3346
for (int i = 0; i < total_args; i++) {
3347
Py_DECREF(args[i]);
3348
}
3349
Py_DECREF(callable);
3350
ERROR_IF(res == NULL, error);
3351
CHECK_EVAL_BREAKER();
3352
}
3353
3354
inst(INSTRUMENTED_CALL_FUNCTION_EX, ( -- )) {
3355
GO_TO_INSTRUCTION(CALL_FUNCTION_EX);
3356
}
3357
3358
inst(CALL_FUNCTION_EX, (unused, func, callargs, kwargs if (oparg & 1) -- result)) {
3359
// DICT_MERGE is called before this opcode if there are kwargs.
3360
// It converts all dict subtypes in kwargs into regular dicts.
3361
assert(kwargs == NULL || PyDict_CheckExact(kwargs));
3362
if (!PyTuple_CheckExact(callargs)) {
3363
if (check_args_iterable(tstate, func, callargs) < 0) {
3364
goto error;
3365
}
3366
PyObject *tuple = PySequence_Tuple(callargs);
3367
if (tuple == NULL) {
3368
goto error;
3369
}
3370
Py_SETREF(callargs, tuple);
3371
}
3372
assert(PyTuple_CheckExact(callargs));
3373
EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_FUNCTION_EX, func);
3374
if (opcode == INSTRUMENTED_CALL_FUNCTION_EX &&
3375
!PyFunction_Check(func) && !PyMethod_Check(func)
3376
) {
3377
PyObject *arg = PyTuple_GET_SIZE(callargs) > 0 ?
3378
PyTuple_GET_ITEM(callargs, 0) : Py_None;
3379
int err = _Py_call_instrumentation_2args(
3380
tstate, PY_MONITORING_EVENT_CALL,
3381
frame, next_instr-1, func, arg);
3382
if (err) goto error;
3383
result = PyObject_Call(func, callargs, kwargs);
3384
if (result == NULL) {
3385
_Py_call_instrumentation_exc2(
3386
tstate, PY_MONITORING_EVENT_C_RAISE,
3387
frame, next_instr-1, func, arg);
3388
}
3389
else {
3390
int err = _Py_call_instrumentation_2args(
3391
tstate, PY_MONITORING_EVENT_C_RETURN,
3392
frame, next_instr-1, func, arg);
3393
if (err < 0) {
3394
Py_CLEAR(result);
3395
}
3396
}
3397
}
3398
else {
3399
if (Py_TYPE(func) == &PyFunction_Type &&
3400
tstate->interp->eval_frame == NULL &&
3401
((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall) {
3402
assert(PyTuple_CheckExact(callargs));
3403
Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
3404
int code_flags = ((PyCodeObject *)PyFunction_GET_CODE(func))->co_flags;
3405
PyObject *locals = code_flags & CO_OPTIMIZED ? NULL : Py_NewRef(PyFunction_GET_GLOBALS(func));
3406
3407
_PyInterpreterFrame *new_frame = _PyEvalFramePushAndInit_Ex(tstate,
3408
(PyFunctionObject *)func, locals,
3409
nargs, callargs, kwargs);
3410
// Need to manually shrink the stack since we exit with DISPATCH_INLINED.
3411
STACK_SHRINK(oparg + 3);
3412
if (new_frame == NULL) {
3413
goto error;
3414
}
3415
frame->return_offset = 0;
3416
DISPATCH_INLINED(new_frame);
3417
}
3418
result = PyObject_Call(func, callargs, kwargs);
3419
}
3420
DECREF_INPUTS();
3421
assert(PEEK(3 + (oparg & 1)) == NULL);
3422
ERROR_IF(result == NULL, error);
3423
CHECK_EVAL_BREAKER();
3424
}
3425
3426
inst(MAKE_FUNCTION, (codeobj -- func)) {
3427
3428
PyFunctionObject *func_obj = (PyFunctionObject *)
3429
PyFunction_New(codeobj, GLOBALS());
3430
3431
Py_DECREF(codeobj);
3432
if (func_obj == NULL) {
3433
goto error;
3434
}
3435
3436
func_obj->func_version = ((PyCodeObject *)codeobj)->co_version;
3437
func = (PyObject *)func_obj;
3438
}
3439
3440
inst(SET_FUNCTION_ATTRIBUTE, (attr, func -- func)) {
3441
assert(PyFunction_Check(func));
3442
PyFunctionObject *func_obj = (PyFunctionObject *)func;
3443
switch(oparg) {
3444
case MAKE_FUNCTION_CLOSURE:
3445
assert(func_obj->func_closure == NULL);
3446
func_obj->func_closure = attr;
3447
break;
3448
case MAKE_FUNCTION_ANNOTATIONS:
3449
assert(func_obj->func_annotations == NULL);
3450
func_obj->func_annotations = attr;
3451
break;
3452
case MAKE_FUNCTION_KWDEFAULTS:
3453
assert(PyDict_CheckExact(attr));
3454
assert(func_obj->func_kwdefaults == NULL);
3455
func_obj->func_kwdefaults = attr;
3456
break;
3457
case MAKE_FUNCTION_DEFAULTS:
3458
assert(PyTuple_CheckExact(attr));
3459
assert(func_obj->func_defaults == NULL);
3460
func_obj->func_defaults = attr;
3461
break;
3462
default:
3463
Py_UNREACHABLE();
3464
}
3465
}
3466
3467
inst(RETURN_GENERATOR, (--)) {
3468
assert(PyFunction_Check(frame->f_funcobj));
3469
PyFunctionObject *func = (PyFunctionObject *)frame->f_funcobj;
3470
PyGenObject *gen = (PyGenObject *)_Py_MakeCoro(func);
3471
if (gen == NULL) {
3472
goto error;
3473
}
3474
assert(EMPTY());
3475
_PyFrame_SetStackPointer(frame, stack_pointer);
3476
_PyInterpreterFrame *gen_frame = (_PyInterpreterFrame *)gen->gi_iframe;
3477
_PyFrame_Copy(frame, gen_frame);
3478
assert(frame->frame_obj == NULL);
3479
gen->gi_frame_state = FRAME_CREATED;
3480
gen_frame->owner = FRAME_OWNED_BY_GENERATOR;
3481
_Py_LeaveRecursiveCallPy(tstate);
3482
assert(frame != &entry_frame);
3483
_PyInterpreterFrame *prev = frame->previous;
3484
_PyThreadState_PopFrame(tstate, frame);
3485
frame = cframe.current_frame = prev;
3486
_PyFrame_StackPush(frame, (PyObject *)gen);
3487
goto resume_frame;
3488
}
3489
3490
inst(BUILD_SLICE, (start, stop, step if (oparg == 3) -- slice)) {
3491
slice = PySlice_New(start, stop, step);
3492
DECREF_INPUTS();
3493
ERROR_IF(slice == NULL, error);
3494
}
3495
3496
inst(CONVERT_VALUE, (value -- result)) {
3497
convertion_func_ptr conv_fn;
3498
assert(oparg >= FVC_STR && oparg <= FVC_ASCII);
3499
conv_fn = CONVERSION_FUNCTIONS[oparg];
3500
result = conv_fn(value);
3501
Py_DECREF(value);
3502
ERROR_IF(result == NULL, error);
3503
}
3504
3505
inst(FORMAT_SIMPLE, (value -- res)) {
3506
/* If value is a unicode object, then we know the result
3507
* of format(value) is value itself. */
3508
if (!PyUnicode_CheckExact(value)) {
3509
res = PyObject_Format(value, NULL);
3510
Py_DECREF(value);
3511
ERROR_IF(res == NULL, error);
3512
}
3513
else {
3514
res = value;
3515
}
3516
}
3517
3518
inst(FORMAT_WITH_SPEC, (value, fmt_spec -- res)) {
3519
res = PyObject_Format(value, fmt_spec);
3520
Py_DECREF(value);
3521
Py_DECREF(fmt_spec);
3522
ERROR_IF(res == NULL, error);
3523
}
3524
3525
inst(COPY, (bottom, unused[oparg-1] -- bottom, unused[oparg-1], top)) {
3526
assert(oparg > 0);
3527
top = Py_NewRef(bottom);
3528
}
3529
3530
inst(BINARY_OP, (unused/1, lhs, rhs -- res)) {
3531
#if ENABLE_SPECIALIZATION
3532
_PyBinaryOpCache *cache = (_PyBinaryOpCache *)next_instr;
3533
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
3534
next_instr--;
3535
_Py_Specialize_BinaryOp(lhs, rhs, next_instr, oparg, &GETLOCAL(0));
3536
DISPATCH_SAME_OPARG();
3537
}
3538
STAT_INC(BINARY_OP, deferred);
3539
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
3540
#endif /* ENABLE_SPECIALIZATION */
3541
assert(0 <= oparg);
3542
assert((unsigned)oparg < Py_ARRAY_LENGTH(binary_ops));
3543
assert(binary_ops[oparg]);
3544
res = binary_ops[oparg](lhs, rhs);
3545
DECREF_INPUTS();
3546
ERROR_IF(res == NULL, error);
3547
}
3548
3549
inst(SWAP, (bottom, unused[oparg-2], top --
3550
top, unused[oparg-2], bottom)) {
3551
assert(oparg >= 2);
3552
}
3553
3554
inst(INSTRUMENTED_INSTRUCTION, ( -- )) {
3555
int next_opcode = _Py_call_instrumentation_instruction(
3556
tstate, frame, next_instr-1);
3557
ERROR_IF(next_opcode < 0, error);
3558
next_instr--;
3559
if (_PyOpcode_Caches[next_opcode]) {
3560
_PyBinaryOpCache *cache = (_PyBinaryOpCache *)(next_instr+1);
3561
INCREMENT_ADAPTIVE_COUNTER(cache->counter);
3562
}
3563
assert(next_opcode > 0 && next_opcode < 256);
3564
opcode = next_opcode;
3565
DISPATCH_GOTO();
3566
}
3567
3568
inst(INSTRUMENTED_JUMP_FORWARD, ( -- )) {
3569
INSTRUMENTED_JUMP(next_instr-1, next_instr+oparg, PY_MONITORING_EVENT_JUMP);
3570
}
3571
3572
inst(INSTRUMENTED_JUMP_BACKWARD, ( -- )) {
3573
INSTRUMENTED_JUMP(next_instr-1, next_instr+1-oparg, PY_MONITORING_EVENT_JUMP);
3574
CHECK_EVAL_BREAKER();
3575
}
3576
3577
inst(INSTRUMENTED_POP_JUMP_IF_TRUE, ( -- )) {
3578
PyObject *cond = POP();
3579
assert(PyBool_Check(cond));
3580
_Py_CODEUNIT *here = next_instr - 1;
3581
int offset = Py_IsTrue(cond) * oparg;
3582
INSTRUMENTED_JUMP(here, next_instr + offset, PY_MONITORING_EVENT_BRANCH);
3583
}
3584
3585
inst(INSTRUMENTED_POP_JUMP_IF_FALSE, ( -- )) {
3586
PyObject *cond = POP();
3587
assert(PyBool_Check(cond));
3588
_Py_CODEUNIT *here = next_instr - 1;
3589
int offset = Py_IsFalse(cond) * oparg;
3590
INSTRUMENTED_JUMP(here, next_instr + offset, PY_MONITORING_EVENT_BRANCH);
3591
}
3592
3593
inst(INSTRUMENTED_POP_JUMP_IF_NONE, ( -- )) {
3594
PyObject *value = POP();
3595
_Py_CODEUNIT *here = next_instr-1;
3596
int offset;
3597
if (Py_IsNone(value)) {
3598
offset = oparg;
3599
}
3600
else {
3601
Py_DECREF(value);
3602
offset = 0;
3603
}
3604
INSTRUMENTED_JUMP(here, next_instr + offset, PY_MONITORING_EVENT_BRANCH);
3605
}
3606
3607
inst(INSTRUMENTED_POP_JUMP_IF_NOT_NONE, ( -- )) {
3608
PyObject *value = POP();
3609
_Py_CODEUNIT *here = next_instr-1;
3610
int offset;
3611
if (Py_IsNone(value)) {
3612
offset = 0;
3613
}
3614
else {
3615
Py_DECREF(value);
3616
offset = oparg;
3617
}
3618
INSTRUMENTED_JUMP(here, next_instr + offset, PY_MONITORING_EVENT_BRANCH);
3619
}
3620
3621
inst(EXTENDED_ARG, ( -- )) {
3622
assert(oparg);
3623
opcode = next_instr->op.code;
3624
oparg = oparg << 8 | next_instr->op.arg;
3625
PRE_DISPATCH_GOTO();
3626
DISPATCH_GOTO();
3627
}
3628
3629
inst(CACHE, (--)) {
3630
assert(0 && "Executing a cache.");
3631
Py_UNREACHABLE();
3632
}
3633
3634
inst(RESERVED, (--)) {
3635
assert(0 && "Executing RESERVED instruction.");
3636
Py_UNREACHABLE();
3637
}
3638
3639
3640
// END BYTECODES //
3641
3642
}
3643
dispatch_opcode:
3644
error:
3645
exception_unwind:
3646
exit_unwind:
3647
handle_eval_breaker:
3648
resume_frame:
3649
resume_with_error:
3650
start_frame:
3651
unbound_local_error:
3652
;
3653
}
3654
3655
// Future families go below this point //
3656
3657