FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpython/Python/specialize.c at doc-link-https · 180909/cpython · GitHub
180909
/
cpython
Public
forked from
python/cpython
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
5
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpython
/
Python
/
specialize.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
2223 lines (2132 loc) · 75.7 KB
Breadcrumbs
cpython
/
Python
/
specialize.c
Copy path
File metadata and controls
2223 lines (2132 loc) · 75.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include
"Python.h"
#include
"pycore_code.h"
#include
"pycore_dict.h"
#include
"pycore_function.h"
// _PyFunction_GetVersionForCurrentState()
#include
"pycore_global_strings.h"
// _Py_ID()
#include
"pycore_long.h"
#include
"pycore_moduleobject.h"
#include
"pycore_object.h"
#include
"pycore_opcode.h"
// _PyOpcode_Caches
#include
"structmember.h"
// struct PyMemberDef, T_OFFSET_EX
#include
"pycore_descrobject.h"
#include
<stdlib.h>
// rand()
/* For guidance on adding or extending families of instructions see
* ./adaptive.md
*/
/* Map from opcode to adaptive opcode.
Values of zero are ignored. */
uint8_t
_PyOpcode_Adaptive
[
256
]
=
{
[
LOAD_ATTR
]
=
LOAD_ATTR_ADAPTIVE
,
[
LOAD_GLOBAL
]
=
LOAD_GLOBAL_ADAPTIVE
,
[
BINARY_SUBSCR
]
=
BINARY_SUBSCR_ADAPTIVE
,
[
STORE_SUBSCR
]
=
STORE_SUBSCR_ADAPTIVE
,
[
CALL
]
=
CALL_ADAPTIVE
,
[
STORE_ATTR
]
=
STORE_ATTR_ADAPTIVE
,
[
BINARY_OP
]
=
BINARY_OP_ADAPTIVE
,
[
COMPARE_OP
]
=
COMPARE_OP_ADAPTIVE
,
[
UNPACK_SEQUENCE
]
=
UNPACK_SEQUENCE_ADAPTIVE
,
[
FOR_ITER
]
=
FOR_ITER_ADAPTIVE
,
};
Py_ssize_t
_Py_QuickenedCount
=
0
;
#ifdef
Py_STATS
PyStats
_py_stats_struct
=
{
0
};
PyStats
*
_py_stats
=
&
_py_stats_struct
;
#define
ADD_STAT_TO_DICT
(
res
,
field
) \
do { \
PyObject *val = PyLong_FromUnsignedLongLong(stats->field); \
if (val == NULL) { \
Py_DECREF(res); \
return NULL; \
} \
if (PyDict_SetItemString(res, #field, val) == -1) { \
Py_DECREF(res); \
Py_DECREF(val); \
return NULL; \
} \
Py_DECREF(val); \
} while(0);
static
PyObject
*
stats_to_dict
(
SpecializationStats
*
stats
)
{
PyObject
*
res
=
PyDict_New
();
if
(
res
==
NULL
) {
return
NULL
;
}
ADD_STAT_TO_DICT
(
res
,
success
);
ADD_STAT_TO_DICT
(
res
,
failure
);
ADD_STAT_TO_DICT
(
res
,
hit
);
ADD_STAT_TO_DICT
(
res
,
deferred
);
ADD_STAT_TO_DICT
(
res
,
miss
);
ADD_STAT_TO_DICT
(
res
,
deopt
);
PyObject
*
failure_kinds
=
PyTuple_New
(
SPECIALIZATION_FAILURE_KINDS
);
if
(
failure_kinds
==
NULL
) {
Py_DECREF
(
res
);
return
NULL
;
}
for
(
int
i
=
0
;
i
<
SPECIALIZATION_FAILURE_KINDS
;
i
++
) {
PyObject
*
stat
=
PyLong_FromUnsignedLongLong
(
stats
->
failure_kinds
[
i
]);
if
(
stat
==
NULL
) {
Py_DECREF
(
res
);
Py_DECREF
(
failure_kinds
);
return
NULL
;
}
PyTuple_SET_ITEM
(
failure_kinds
,
i
,
stat
);
}
if
(
PyDict_SetItemString
(
res
,
"failure_kinds"
,
failure_kinds
)) {
Py_DECREF
(
res
);
Py_DECREF
(
failure_kinds
);
return
NULL
;
}
Py_DECREF
(
failure_kinds
);
return
res
;
}
#undef
ADD_STAT_TO_DICT
static
int
add_stat_dict
(
PyObject
*
res
,
int
opcode
,
const
char
*
name
) {
SpecializationStats
*
stats
=
&
_py_stats_struct
.
opcode_stats
[
opcode
].
specialization
;
PyObject
*
d
=
stats_to_dict
(
stats
);
if
(
d
==
NULL
) {
return
-1
;
}
int
err
=
PyDict_SetItemString
(
res
,
name
,
d
);
Py_DECREF
(
d
);
return
err
;
}
#ifdef
Py_STATS
PyObject
*
_Py_GetSpecializationStats
(
void
) {
PyObject
*
stats
=
PyDict_New
();
if
(
stats
==
NULL
) {
return
NULL
;
}
int
err
=
0
;
err
+=
add_stat_dict
(
stats
,
LOAD_ATTR
,
"load_attr"
);
err
+=
add_stat_dict
(
stats
,
LOAD_GLOBAL
,
"load_global"
);
err
+=
add_stat_dict
(
stats
,
BINARY_SUBSCR
,
"binary_subscr"
);
err
+=
add_stat_dict
(
stats
,
STORE_SUBSCR
,
"store_subscr"
);
err
+=
add_stat_dict
(
stats
,
STORE_ATTR
,
"store_attr"
);
err
+=
add_stat_dict
(
stats
,
CALL
,
"call"
);
err
+=
add_stat_dict
(
stats
,
BINARY_OP
,
"binary_op"
);
err
+=
add_stat_dict
(
stats
,
COMPARE_OP
,
"compare_op"
);
err
+=
add_stat_dict
(
stats
,
UNPACK_SEQUENCE
,
"unpack_sequence"
);
err
+=
add_stat_dict
(
stats
,
FOR_ITER
,
"for_iter"
);
if
(
err
<
0
) {
Py_DECREF
(
stats
);
return
NULL
;
}
return
stats
;
}
#endif
#define
PRINT_STAT
(
i
,
field
) \
if (stats[i].field) { \
fprintf(out, " opcode[%d]." #field " : %" PRIu64 "\n", i, stats[i].field); \
}
static
void
print_spec_stats
(
FILE
*
out
,
OpcodeStats
*
stats
)
{
/* Mark some opcodes as specializable for stats,
* even though we don't specialize them yet. */
fprintf
(
out
,
"opcode[%d].specializable : 1\n"
,
BINARY_SLICE
);
fprintf
(
out
,
"opcode[%d].specializable : 1\n"
,
STORE_SLICE
);
for
(
int
i
=
0
;
i
<
256
;
i
++
) {
if
(
_PyOpcode_Adaptive
[
i
]) {
fprintf
(
out
,
"opcode[%d].specializable : 1\n"
,
i
);
}
PRINT_STAT
(
i
,
specialization
.
success
);
PRINT_STAT
(
i
,
specialization
.
failure
);
PRINT_STAT
(
i
,
specialization
.
hit
);
PRINT_STAT
(
i
,
specialization
.
deferred
);
PRINT_STAT
(
i
,
specialization
.
miss
);
PRINT_STAT
(
i
,
specialization
.
deopt
);
PRINT_STAT
(
i
,
execution_count
);
for
(
int
j
=
0
;
j
<
SPECIALIZATION_FAILURE_KINDS
;
j
++
) {
uint64_t
val
=
stats
[
i
].
specialization
.
failure_kinds
[
j
];
if
(
val
) {
fprintf
(
out
,
" opcode[%d].specialization.failure_kinds[%d] : %"
PRIu64
"\n"
,
i
,
j
,
val
);
}
}
for
(
int
j
=
0
;
j
<
256
;
j
++
) {
if
(
stats
[
i
].
pair_count
[
j
]) {
fprintf
(
out
,
"opcode[%d].pair_count[%d] : %"
PRIu64
"\n"
,
i
,
j
,
stats
[
i
].
pair_count
[
j
]);
}
}
}
}
#undef
PRINT_STAT
static
void
print_call_stats
(
FILE
*
out
,
CallStats
*
stats
)
{
fprintf
(
out
,
"Calls to PyEval_EvalDefault: %"
PRIu64
"\n"
,
stats
->
pyeval_calls
);
fprintf
(
out
,
"Calls to Python functions inlined: %"
PRIu64
"\n"
,
stats
->
inlined_py_calls
);
fprintf
(
out
,
"Frames pushed: %"
PRIu64
"\n"
,
stats
->
frames_pushed
);
fprintf
(
out
,
"Frame objects created: %"
PRIu64
"\n"
,
stats
->
frame_objects_created
);
for
(
int
i
=
0
;
i
<
EVAL_CALL_KINDS
;
i
++
) {
fprintf
(
out
,
"Calls via PyEval_EvalFrame[%d] : %"
PRIu64
"\n"
,
i
,
stats
->
eval_calls
[
i
]);
}
}
static
void
print_object_stats
(
FILE
*
out
,
ObjectStats
*
stats
)
{
fprintf
(
out
,
"Object allocations from freelist: %"
PRIu64
"\n"
,
stats
->
from_freelist
);
fprintf
(
out
,
"Object frees to freelist: %"
PRIu64
"\n"
,
stats
->
to_freelist
);
fprintf
(
out
,
"Object allocations: %"
PRIu64
"\n"
,
stats
->
allocations
);
fprintf
(
out
,
"Object allocations to 512 bytes: %"
PRIu64
"\n"
,
stats
->
allocations512
);
fprintf
(
out
,
"Object allocations to 4 kbytes: %"
PRIu64
"\n"
,
stats
->
allocations4k
);
fprintf
(
out
,
"Object allocations over 4 kbytes: %"
PRIu64
"\n"
,
stats
->
allocations_big
);
fprintf
(
out
,
"Object frees: %"
PRIu64
"\n"
,
stats
->
frees
);
fprintf
(
out
,
"Object new values: %"
PRIu64
"\n"
,
stats
->
new_values
);
fprintf
(
out
,
"Object interpreter increfs: %"
PRIu64
"\n"
,
stats
->
interpreter_increfs
);
fprintf
(
out
,
"Object interpreter decrefs: %"
PRIu64
"\n"
,
stats
->
interpreter_decrefs
);
fprintf
(
out
,
"Object increfs: %"
PRIu64
"\n"
,
stats
->
increfs
);
fprintf
(
out
,
"Object decrefs: %"
PRIu64
"\n"
,
stats
->
decrefs
);
fprintf
(
out
,
"Object materialize dict (on request): %"
PRIu64
"\n"
,
stats
->
dict_materialized_on_request
);
fprintf
(
out
,
"Object materialize dict (new key): %"
PRIu64
"\n"
,
stats
->
dict_materialized_new_key
);
fprintf
(
out
,
"Object materialize dict (too big): %"
PRIu64
"\n"
,
stats
->
dict_materialized_too_big
);
fprintf
(
out
,
"Object materialize dict (str subclass): %"
PRIu64
"\n"
,
stats
->
dict_materialized_str_subclass
);
}
static
void
print_stats
(
FILE
*
out
,
PyStats
*
stats
) {
print_spec_stats
(
out
,
stats
->
opcode_stats
);
print_call_stats
(
out
,
&
stats
->
call_stats
);
print_object_stats
(
out
,
&
stats
->
object_stats
);
}
void
_Py_StatsClear
(
void
)
{
_py_stats_struct
=
(
PyStats
) {
0
};
}
void
_Py_PrintSpecializationStats
(
int
to_file
)
{
if
(
_py_stats
==
NULL
) {
return
;
}
FILE
*
out
=
stderr
;
if
(
to_file
) {
/* Write to a file instead of stderr. */
# ifdef
MS_WINDOWS
const
char
*
dirname
=
"c:\\temp\\py_stats\\"
;
# else
const
char
*
dirname
=
"/tmp/py_stats/"
;
# endif
/* Use random 160 bit number as file name,
* to avoid both accidental collisions and
* symlink attacks. */
unsigned
char
rand
[
20
];
char
hex_name
[
41
];
_PyOS_URandomNonblock
(
rand
,
20
);
for
(
int
i
=
0
;
i
<
20
;
i
++
) {
hex_name
[
2
*
i
]
=
"0123456789abcdef"
[
rand
[
i
]
&
15
];
hex_name
[
2
*
i
+
1
]
=
"0123456789abcdef"
[(
rand
[
i
]>>
4
)
&
15
];
}
hex_name
[
40
]
=
'\0'
;
char
buf
[
64
];
assert
(
strlen
(
dirname
)
+
40
+
strlen
(
".txt"
)
<
64
);
sprintf
(
buf
,
"%s%s.txt"
,
dirname
,
hex_name
);
FILE
*
fout
=
fopen
(
buf
,
"w"
);
if
(
fout
) {
out
=
fout
;
}
}
else
{
fprintf
(
out
,
"Specialization stats:\n"
);
}
print_stats
(
out
,
_py_stats
);
if
(
out
!=
stderr
) {
fclose
(
out
);
}
}
#ifdef
Py_STATS
#define
SPECIALIZATION_FAIL
(
opcode
,
kind
) \
do { \
if (_py_stats) { \
_py_stats->opcode_stats[opcode].specialization.failure_kinds[kind]++; \
} \
} while (0)
#endif
#endif
#ifndef
SPECIALIZATION_FAIL
#define
SPECIALIZATION_FAIL
(
opcode
,
kind
) ((void)0)
#endif
// Insert adaptive instructions and superinstructions. This cannot fail.
void
_PyCode_Quicken
(
PyCodeObject
*
code
)
{
_Py_QuickenedCount
++
;
int
previous_opcode
=
-1
;
_Py_CODEUNIT
*
instructions
=
_PyCode_CODE
(
code
);
for
(
int
i
=
0
;
i
<
Py_SIZE
(
code
);
i
++
) {
int
opcode
=
_Py_OPCODE
(
instructions
[
i
]);
uint8_t
adaptive_opcode
=
_PyOpcode_Adaptive
[
opcode
];
if
(
adaptive_opcode
) {
_Py_SET_OPCODE
(
instructions
[
i
],
adaptive_opcode
);
// Make sure the adaptive counter is zero:
assert
(
instructions
[
i
+
1
]
==
0
);
previous_opcode
=
-1
;
i
+=
_PyOpcode_Caches
[
opcode
];
}
else
{
assert
(!
_PyOpcode_Caches
[
opcode
]);
switch
(
opcode
) {
case
EXTENDED_ARG
:
_Py_SET_OPCODE
(
instructions
[
i
],
EXTENDED_ARG_QUICK
);
break
;
case
JUMP_BACKWARD
:
_Py_SET_OPCODE
(
instructions
[
i
],
JUMP_BACKWARD_QUICK
);
break
;
case
RESUME
:
_Py_SET_OPCODE
(
instructions
[
i
],
RESUME_QUICK
);
break
;
case
LOAD_FAST
:
switch
(
previous_opcode
) {
case
LOAD_FAST
:
_Py_SET_OPCODE
(
instructions
[
i
-
1
],
LOAD_FAST__LOAD_FAST
);
break
;
case
STORE_FAST
:
_Py_SET_OPCODE
(
instructions
[
i
-
1
],
STORE_FAST__LOAD_FAST
);
break
;
case
LOAD_CONST
:
_Py_SET_OPCODE
(
instructions
[
i
-
1
],
LOAD_CONST__LOAD_FAST
);
break
;
}
break
;
case
STORE_FAST
:
if
(
previous_opcode
==
STORE_FAST
) {
_Py_SET_OPCODE
(
instructions
[
i
-
1
],
STORE_FAST__STORE_FAST
);
}
break
;
case
LOAD_CONST
:
if
(
previous_opcode
==
LOAD_FAST
) {
_Py_SET_OPCODE
(
instructions
[
i
-
1
],
LOAD_FAST__LOAD_CONST
);
}
break
;
}
previous_opcode
=
opcode
;
}
}
}
static
inline
int
miss_counter_start
(
void
) {
/* Starting value for the counter.
* This value needs to be not too low, otherwise
* it would cause excessive de-optimization.
* Neither should it be too high, or that would delay
* de-optimization excessively when it is needed.
* A value around 50 seems to work, and we choose a
* prime number to avoid artifacts.
*/
return
53
;
}
#define
SIMPLE_FUNCTION
0
/* Common */
#define
SPEC_FAIL_OTHER
0
#define
SPEC_FAIL_NO_DICT
1
#define
SPEC_FAIL_OVERRIDDEN
2
#define
SPEC_FAIL_OUT_OF_VERSIONS
3
#define
SPEC_FAIL_OUT_OF_RANGE
4
#define
SPEC_FAIL_EXPECTED_ERROR
5
#define
SPEC_FAIL_WRONG_NUMBER_ARGUMENTS
6
#define
SPEC_FAIL_NOT_PY_FUNCTION
7
#define
SPEC_FAIL_LOAD_GLOBAL_NON_STRING_OR_SPLIT
18
/* Attributes */
#define
SPEC_FAIL_ATTR_OVERRIDING_DESCRIPTOR
8
#define
SPEC_FAIL_ATTR_NON_OVERRIDING_DESCRIPTOR
9
#define
SPEC_FAIL_ATTR_NOT_DESCRIPTOR
10
#define
SPEC_FAIL_ATTR_METHOD
11
#define
SPEC_FAIL_ATTR_MUTABLE_CLASS
12
#define
SPEC_FAIL_ATTR_PROPERTY
13
#define
SPEC_FAIL_ATTR_NON_OBJECT_SLOT
14
#define
SPEC_FAIL_ATTR_READ_ONLY
15
#define
SPEC_FAIL_ATTR_AUDITED_SLOT
16
#define
SPEC_FAIL_ATTR_NOT_MANAGED_DICT
17
#define
SPEC_FAIL_ATTR_NON_STRING_OR_SPLIT
18
#define
SPEC_FAIL_ATTR_MODULE_ATTR_NOT_FOUND
19
#define
SPEC_FAIL_ATTR_SHADOWED
21
#define
SPEC_FAIL_ATTR_BUILTIN_CLASS_METHOD
22
#define
SPEC_FAIL_ATTR_CLASS_METHOD_OBJ
23
#define
SPEC_FAIL_ATTR_OBJECT_SLOT
24
#define
SPEC_FAIL_ATTR_HAS_MANAGED_DICT
25
#define
SPEC_FAIL_ATTR_INSTANCE_ATTRIBUTE
26
#define
SPEC_FAIL_ATTR_METACLASS_ATTRIBUTE
27
#define
SPEC_FAIL_ATTR_PROPERTY_NOT_PY_FUNCTION
28
/* Binary subscr and store subscr */
#define
SPEC_FAIL_SUBSCR_ARRAY_INT
8
#define
SPEC_FAIL_SUBSCR_ARRAY_SLICE
9
#define
SPEC_FAIL_SUBSCR_LIST_SLICE
10
#define
SPEC_FAIL_SUBSCR_TUPLE_SLICE
11
#define
SPEC_FAIL_SUBSCR_STRING_INT
12
#define
SPEC_FAIL_SUBSCR_STRING_SLICE
13
#define
SPEC_FAIL_SUBSCR_BUFFER_INT
15
#define
SPEC_FAIL_SUBSCR_BUFFER_SLICE
16
#define
SPEC_FAIL_SUBSCR_SEQUENCE_INT
17
/* Store subscr */
#define
SPEC_FAIL_SUBSCR_BYTEARRAY_INT
18
#define
SPEC_FAIL_SUBSCR_BYTEARRAY_SLICE
19
#define
SPEC_FAIL_SUBSCR_PY_SIMPLE
20
#define
SPEC_FAIL_SUBSCR_PY_OTHER
21
#define
SPEC_FAIL_SUBSCR_DICT_SUBCLASS_NO_OVERRIDE
22
#define
SPEC_FAIL_SUBSCR_NOT_HEAP_TYPE
23
/* Binary op */
#define
SPEC_FAIL_BINARY_OP_ADD_DIFFERENT_TYPES
8
#define
SPEC_FAIL_BINARY_OP_ADD_OTHER
9
#define
SPEC_FAIL_BINARY_OP_AND_DIFFERENT_TYPES
10
#define
SPEC_FAIL_BINARY_OP_AND_INT
11
#define
SPEC_FAIL_BINARY_OP_AND_OTHER
12
#define
SPEC_FAIL_BINARY_OP_FLOOR_DIVIDE
13
#define
SPEC_FAIL_BINARY_OP_LSHIFT
14
#define
SPEC_FAIL_BINARY_OP_MATRIX_MULTIPLY
15
#define
SPEC_FAIL_BINARY_OP_MULTIPLY_DIFFERENT_TYPES
16
#define
SPEC_FAIL_BINARY_OP_MULTIPLY_OTHER
17
#define
SPEC_FAIL_BINARY_OP_OR
18
#define
SPEC_FAIL_BINARY_OP_POWER
19
#define
SPEC_FAIL_BINARY_OP_REMAINDER
20
#define
SPEC_FAIL_BINARY_OP_RSHIFT
21
#define
SPEC_FAIL_BINARY_OP_SUBTRACT_DIFFERENT_TYPES
22
#define
SPEC_FAIL_BINARY_OP_SUBTRACT_OTHER
23
#define
SPEC_FAIL_BINARY_OP_TRUE_DIVIDE_DIFFERENT_TYPES
24
#define
SPEC_FAIL_BINARY_OP_TRUE_DIVIDE_FLOAT
25
#define
SPEC_FAIL_BINARY_OP_TRUE_DIVIDE_OTHER
26
#define
SPEC_FAIL_BINARY_OP_XOR
27
/* Calls */
#define
SPEC_FAIL_CALL_COMPLEX_PARAMETERS
9
#define
SPEC_FAIL_CALL_CO_NOT_OPTIMIZED
10
/* SPEC_FAIL_METHOD defined as 11 above */
#define
SPEC_FAIL_CALL_INSTANCE_METHOD
11
#define
SPEC_FAIL_CALL_CMETHOD
12
#define
SPEC_FAIL_CALL_PYCFUNCTION
13
#define
SPEC_FAIL_CALL_PYCFUNCTION_WITH_KEYWORDS
14
#define
SPEC_FAIL_CALL_PYCFUNCTION_FAST_WITH_KEYWORDS
15
#define
SPEC_FAIL_CALL_PYCFUNCTION_NOARGS
16
#define
SPEC_FAIL_CALL_BAD_CALL_FLAGS
17
#define
SPEC_FAIL_CALL_CLASS
18
#define
SPEC_FAIL_CALL_PYTHON_CLASS
19
#define
SPEC_FAIL_CALL_METHOD_DESCRIPTOR
20
#define
SPEC_FAIL_CALL_BOUND_METHOD
21
#define
SPEC_FAIL_CALL_STR
22
#define
SPEC_FAIL_CALL_CLASS_NO_VECTORCALL
23
#define
SPEC_FAIL_CALL_CLASS_MUTABLE
24
#define
SPEC_FAIL_CALL_KWNAMES
25
#define
SPEC_FAIL_CALL_METHOD_WRAPPER
26
#define
SPEC_FAIL_CALL_OPERATOR_WRAPPER
27
#define
SPEC_FAIL_CALL_PYFUNCTION
28
#define
SPEC_FAIL_CALL_PEP_523
29
/* COMPARE_OP */
#define
SPEC_FAIL_COMPARE_OP_DIFFERENT_TYPES
12
#define
SPEC_FAIL_COMPARE_OP_STRING
13
#define
SPEC_FAIL_COMPARE_OP_NOT_FOLLOWED_BY_COND_JUMP
14
#define
SPEC_FAIL_COMPARE_OP_BIG_INT
15
#define
SPEC_FAIL_COMPARE_OP_BYTES
16
#define
SPEC_FAIL_COMPARE_OP_TUPLE
17
#define
SPEC_FAIL_COMPARE_OP_LIST
18
#define
SPEC_FAIL_COMPARE_OP_SET
19
#define
SPEC_FAIL_COMPARE_OP_BOOL
20
#define
SPEC_FAIL_COMPARE_OP_BASEOBJECT
21
#define
SPEC_FAIL_COMPARE_OP_FLOAT_LONG
22
#define
SPEC_FAIL_COMPARE_OP_LONG_FLOAT
23
#define
SPEC_FAIL_COMPARE_OP_EXTENDED_ARG
24
/* FOR_ITER */
#define
SPEC_FAIL_FOR_ITER_GENERATOR
10
#define
SPEC_FAIL_FOR_ITER_COROUTINE
11
#define
SPEC_FAIL_FOR_ITER_ASYNC_GENERATOR
12
#define
SPEC_FAIL_FOR_ITER_LIST
13
#define
SPEC_FAIL_FOR_ITER_TUPLE
14
#define
SPEC_FAIL_FOR_ITER_SET
15
#define
SPEC_FAIL_FOR_ITER_STRING
16
#define
SPEC_FAIL_FOR_ITER_BYTES
17
#define
SPEC_FAIL_FOR_ITER_RANGE
18
#define
SPEC_FAIL_FOR_ITER_ITERTOOLS
19
#define
SPEC_FAIL_FOR_ITER_DICT_KEYS
20
#define
SPEC_FAIL_FOR_ITER_DICT_ITEMS
21
#define
SPEC_FAIL_FOR_ITER_DICT_VALUES
22
#define
SPEC_FAIL_FOR_ITER_ENUMERATE
23
#define
SPEC_FAIL_FOR_ITER_MAP
24
#define
SPEC_FAIL_FOR_ITER_ZIP
25
#define
SPEC_FAIL_FOR_ITER_SEQ_ITER
26
#define
SPEC_FAIL_FOR_ITER_REVERSED_LIST
27
#define
SPEC_FAIL_FOR_ITER_CALLABLE
28
#define
SPEC_FAIL_FOR_ITER_ASCII_STRING
29
// UNPACK_SEQUENCE
#define
SPEC_FAIL_UNPACK_SEQUENCE_ITERATOR
8
#define
SPEC_FAIL_UNPACK_SEQUENCE_SEQUENCE
9
static
int
function_kind
(
PyCodeObject
*
code
);
static
bool
function_check_args
(
PyObject
*
o
,
int
expected_argcount
,
int
opcode
);
static
uint32_t
function_get_version
(
PyObject
*
o
,
int
opcode
);
static
int
specialize_module_load_attr
(
PyObject
*
owner
,
_Py_CODEUNIT
*
instr
,
PyObject
*
name
,
int
opcode
,
int
opcode_module
)
{
_PyAttrCache
*
cache
=
(
_PyAttrCache
*
)(
instr
+
1
);
PyModuleObject
*
m
=
(
PyModuleObject
*
)
owner
;
assert
((
owner
->
ob_type
->
tp_flags
&
Py_TPFLAGS_MANAGED_DICT
)
==
0
);
PyDictObject
*
dict
=
(
PyDictObject
*
)
m
->
md_dict
;
if
(
dict
==
NULL
) {
SPECIALIZATION_FAIL
(
opcode
,
SPEC_FAIL_NO_DICT
);
return
-1
;
}
if
(
dict
->
ma_keys
->
dk_kind
!=
DICT_KEYS_UNICODE
) {
SPECIALIZATION_FAIL
(
opcode
,
SPEC_FAIL_ATTR_NON_STRING_OR_SPLIT
);
return
-1
;
}
Py_ssize_t
index
=
_PyDict_LookupIndex
(
dict
,
&
_Py_ID
(
__getattr__
));
assert
(
index
!=
DKIX_ERROR
);
if
(
index
!=
DKIX_EMPTY
) {
SPECIALIZATION_FAIL
(
opcode
,
SPEC_FAIL_ATTR_MODULE_ATTR_NOT_FOUND
);
return
-1
;
}
index
=
_PyDict_LookupIndex
(
dict
,
name
);
assert
(
index
!=
DKIX_ERROR
);
if
(
index
!=
(
uint16_t
)
index
) {
SPECIALIZATION_FAIL
(
opcode
,
SPEC_FAIL_OUT_OF_RANGE
);
return
-1
;
}
uint32_t
keys_version
=
_PyDictKeys_GetVersionForCurrentState
(
dict
->
ma_keys
);
if
(
keys_version
==
0
) {
SPECIALIZATION_FAIL
(
opcode
,
SPEC_FAIL_OUT_OF_VERSIONS
);
return
-1
;
}
write_u32
(
cache
->
version
,
keys_version
);
cache
->
index
=
(
uint16_t
)
index
;
_Py_SET_OPCODE
(
*
instr
,
opcode_module
);
return
0
;
}
/* Attribute specialization */
typedef
enum
{
OVERRIDING
,
/* Is an overriding descriptor, and will remain so. */
METHOD
,
/* Attribute has Py_TPFLAGS_METHOD_DESCRIPTOR set */
PROPERTY
,
/* Is a property */
OBJECT_SLOT
,
/* Is an object slot descriptor */
OTHER_SLOT
,
/* Is a slot descriptor of another type */
NON_OVERRIDING
,
/* Is another non-overriding descriptor, and is an instance of an immutable class*/
BUILTIN_CLASSMETHOD
,
/* Builtin methods with METH_CLASS */
PYTHON_CLASSMETHOD
,
/* Python classmethod(func) object */
NON_DESCRIPTOR
,
/* Is not a descriptor, and is an instance of an immutable class */
MUTABLE
,
/* Instance of a mutable class; might, or might not, be a descriptor */
ABSENT
,
/* Attribute is not present on the class */
DUNDER_CLASS
,
/* __class__ attribute */
GETSET_OVERRIDDEN
,
/* __getattribute__ or __setattr__ has been overridden */
GETATTRIBUTE_IS_PYTHON_FUNCTION
/* Descriptor requires calling a Python __getattribute__ */
}
DescriptorClassification
;
static
DescriptorClassification
analyze_descriptor
(
PyTypeObject
*
type
,
PyObject
*
name
,
PyObject
*
*
descr
,
int
store
)
{
bool
has_getattr
=
false;
if
(
store
) {
if
(
type
->
tp_setattro
!=
PyObject_GenericSetAttr
) {
*
descr
=
NULL
;
return
GETSET_OVERRIDDEN
;
}
}
else
{
getattrofunc
getattro_slot
=
type
->
tp_getattro
;
if
(
getattro_slot
==
PyObject_GenericGetAttr
) {
/* Normal attribute lookup; */
has_getattr
=
false;
}
else
if
(
getattro_slot
==
_Py_slot_tp_getattr_hook
||
getattro_slot
==
_Py_slot_tp_getattro
) {
/* One or both of __getattribute__ or __getattr__ may have been
overridden See typeobject.c for why these functions are special. */
PyObject
*
getattribute
=
_PyType_Lookup
(
type
,
&
_Py_ID
(
__getattribute__
));
PyInterpreterState
*
interp
=
_PyInterpreterState_GET
();
bool
has_custom_getattribute
=
getattribute
!=
NULL
&&
getattribute
!=
interp
->
callable_cache
.
object__getattribute__
;
has_getattr
=
_PyType_Lookup
(
type
,
&
_Py_ID
(
__getattr__
))
!=
NULL
;
if
(
has_custom_getattribute
) {
if
(
getattro_slot
==
_Py_slot_tp_getattro
&&
!
has_getattr
&&
Py_IS_TYPE
(
getattribute
,
&
PyFunction_Type
)) {
*
descr
=
getattribute
;
return
GETATTRIBUTE_IS_PYTHON_FUNCTION
;
}
/* Potentially both __getattr__ and __getattribute__ are set.
Too complicated */
*
descr
=
NULL
;
return
GETSET_OVERRIDDEN
;
}
/* Potentially has __getattr__ but no custom __getattribute__.
Fall through to usual descriptor analysis.
Usual attribute lookup should only be allowed at runtime
if we can guarantee that there is no way an exception can be
raised. This means some specializations, e.g. specializing
for property() isn't safe.
*/
}
else
{
*
descr
=
NULL
;
return
GETSET_OVERRIDDEN
;
}
}
PyObject
*
descriptor
=
_PyType_Lookup
(
type
,
name
);
*
descr
=
descriptor
;
if
(
descriptor
==
NULL
) {
return
ABSENT
;
}
PyTypeObject
*
desc_cls
=
Py_TYPE
(
descriptor
);
if
(!(
desc_cls
->
tp_flags
&
Py_TPFLAGS_IMMUTABLETYPE
)) {
return
MUTABLE
;
}
if
(
desc_cls
->
tp_descr_set
) {
if
(
desc_cls
==
&
PyMemberDescr_Type
) {
PyMemberDescrObject
*
member
=
(
PyMemberDescrObject
*
)
descriptor
;
struct
PyMemberDef
*
dmem
=
member
->
d_member
;
if
(
dmem
->
type
==
T_OBJECT_EX
) {
return
OBJECT_SLOT
;
}
return
OTHER_SLOT
;
}
if
(
desc_cls
==
&
PyProperty_Type
) {
/* We can't detect at runtime whether an attribute exists
with property. So that means we may have to call
__getattr__. */
return
has_getattr
?
GETSET_OVERRIDDEN
:
PROPERTY
;
}
if
(
PyUnicode_CompareWithASCIIString
(
name
,
"__class__"
)
==
0
) {
if
(
descriptor
==
_PyType_Lookup
(
&
PyBaseObject_Type
,
name
)) {
return
DUNDER_CLASS
;
}
}
if
(
store
) {
return
OVERRIDING
;
}
}
if
(
desc_cls
->
tp_descr_get
) {
if
(
desc_cls
->
tp_flags
&
Py_TPFLAGS_METHOD_DESCRIPTOR
) {
return
METHOD
;
}
if
(
Py_IS_TYPE
(
descriptor
,
&
PyClassMethodDescr_Type
)) {
return
BUILTIN_CLASSMETHOD
;
}
if
(
Py_IS_TYPE
(
descriptor
,
&
PyClassMethod_Type
)) {
return
PYTHON_CLASSMETHOD
;
}
return
NON_OVERRIDING
;
}
return
NON_DESCRIPTOR
;
}
static
int
specialize_dict_access
(
PyObject
*
owner
,
_Py_CODEUNIT
*
instr
,
PyTypeObject
*
type
,
DescriptorClassification
kind
,
PyObject
*
name
,
int
base_op
,
int
values_op
,
int
hint_op
)
{
assert
(
kind
==
NON_OVERRIDING
||
kind
==
NON_DESCRIPTOR
||
kind
==
ABSENT
||
kind
==
BUILTIN_CLASSMETHOD
||
kind
==
PYTHON_CLASSMETHOD
);
// No descriptor, or non overriding.
if
((
type
->
tp_flags
&
Py_TPFLAGS_MANAGED_DICT
)
==
0
) {
SPECIALIZATION_FAIL
(
base_op
,
SPEC_FAIL_ATTR_NOT_MANAGED_DICT
);
return
0
;
}
_PyAttrCache
*
cache
=
(
_PyAttrCache
*
)(
instr
+
1
);
PyDictOrValues
dorv
=
*
_PyObject_DictOrValuesPointer
(
owner
);
if
(
_PyDictOrValues_IsValues
(
dorv
)) {
// Virtual dictionary
PyDictKeysObject
*
keys
=
((
PyHeapTypeObject
*
)
type
)
->
ht_cached_keys
;
assert
(
PyUnicode_CheckExact
(
name
));
Py_ssize_t
index
=
_PyDictKeys_StringLookup
(
keys
,
name
);
assert
(
index
!=
DKIX_ERROR
);
if
(
index
!=
(
uint16_t
)
index
) {
SPECIALIZATION_FAIL
(
base_op
,
SPEC_FAIL_OUT_OF_RANGE
);
return
0
;
}
write_u32
(
cache
->
version
,
type
->
tp_version_tag
);
cache
->
index
=
(
uint16_t
)
index
;
_Py_SET_OPCODE
(
*
instr
,
values_op
);
}
else
{
PyDictObject
*
dict
=
(
PyDictObject
*
)
_PyDictOrValues_GetDict
(
dorv
);
if
(
dict
==
NULL
||
!
PyDict_CheckExact
(
dict
)) {
SPECIALIZATION_FAIL
(
base_op
,
SPEC_FAIL_NO_DICT
);
return
0
;
}
// We found an instance with a __dict__.
Py_ssize_t
index
=
_PyDict_LookupIndex
(
dict
,
name
);
if
(
index
!=
(
uint16_t
)
index
) {
SPECIALIZATION_FAIL
(
base_op
,
SPEC_FAIL_OUT_OF_RANGE
);
return
0
;
}
cache
->
index
=
(
uint16_t
)
index
;
write_u32
(
cache
->
version
,
type
->
tp_version_tag
);
_Py_SET_OPCODE
(
*
instr
,
hint_op
);
}
return
1
;
}
static
int
specialize_attr_loadmethod
(
PyObject
*
owner
,
_Py_CODEUNIT
*
instr
,
PyObject
*
name
,
PyObject
*
descr
,
DescriptorClassification
kind
);
static
int
specialize_class_load_attr
(
PyObject
*
owner
,
_Py_CODEUNIT
*
instr
,
PyObject
*
name
);
int
_Py_Specialize_LoadAttr
(
PyObject
*
owner
,
_Py_CODEUNIT
*
instr
,
PyObject
*
name
)
{
assert
(
_PyOpcode_Caches
[
LOAD_ATTR
]
==
INLINE_CACHE_ENTRIES_LOAD_ATTR
);
_PyAttrCache
*
cache
=
(
_PyAttrCache
*
)(
instr
+
1
);
if
(
PyModule_CheckExact
(
owner
)) {
int
err
=
specialize_module_load_attr
(
owner
,
instr
,
name
,
LOAD_ATTR
,
LOAD_ATTR_MODULE
);
if
(
err
) {
goto
fail
;
}
goto
success
;
}
if
(
PyType_Check
(
owner
)) {
int
err
=
specialize_class_load_attr
(
owner
,
instr
,
name
);
if
(
err
) {
goto
fail
;
}
goto
success
;
}
PyTypeObject
*
type
=
Py_TYPE
(
owner
);
if
(
type
->
tp_dict
==
NULL
) {
if
(
PyType_Ready
(
type
)
<
0
) {
return
-1
;
}
}
PyObject
*
descr
=
NULL
;
DescriptorClassification
kind
=
analyze_descriptor
(
type
,
name
,
&
descr
,
0
);
assert
(
descr
!=
NULL
||
kind
==
ABSENT
||
kind
==
GETSET_OVERRIDDEN
);
switch
(
kind
) {
case
OVERRIDING
:
SPECIALIZATION_FAIL
(
LOAD_ATTR
,
SPEC_FAIL_ATTR_OVERRIDING_DESCRIPTOR
);
goto
fail
;
case
METHOD
:
{
int
oparg
=
_Py_OPARG
(
*
instr
);
if
(
oparg
&
1
) {
if
(
specialize_attr_loadmethod
(
owner
,
instr
,
name
,
descr
,
kind
)) {
goto
success
;
}
}
SPECIALIZATION_FAIL
(
LOAD_ATTR
,
SPEC_FAIL_ATTR_METHOD
);
goto
fail
;
}
case
PROPERTY
:
{
_PyLoadMethodCache
*
lm_cache
=
(
_PyLoadMethodCache
*
)(
instr
+
1
);
assert
(
Py_TYPE
(
descr
)
==
&
PyProperty_Type
);
PyObject
*
fget
=
((
_PyPropertyObject
*
)
descr
)
->
prop_get
;
if
(
fget
==
NULL
) {
SPECIALIZATION_FAIL
(
LOAD_ATTR
,
SPEC_FAIL_EXPECTED_ERROR
);
goto
fail
;
}
if
(!
Py_IS_TYPE
(
fget
,
&
PyFunction_Type
)) {
SPECIALIZATION_FAIL
(
LOAD_ATTR
,
SPEC_FAIL_ATTR_PROPERTY_NOT_PY_FUNCTION
);
goto
fail
;
}
if
(!
function_check_args
(
fget
,
1
,
LOAD_ATTR
)) {
goto
fail
;
}
uint32_t
version
=
function_get_version
(
fget
,
LOAD_ATTR
);
if
(
version
==
0
) {
goto
fail
;
}
write_u32
(
lm_cache
->
keys_version
,
version
);
assert
(
type
->
tp_version_tag
!=
0
);
write_u32
(
lm_cache
->
type_version
,
type
->
tp_version_tag
);
/* borrowed */
write_obj
(
lm_cache
->
descr
,
fget
);
_Py_SET_OPCODE
(
*
instr
,
LOAD_ATTR_PROPERTY
);
goto
success
;
}
case
OBJECT_SLOT
:
{
PyMemberDescrObject
*
member
=
(
PyMemberDescrObject
*
)
descr
;
struct
PyMemberDef
*
dmem
=
member
->
d_member
;
Py_ssize_t
offset
=
dmem
->
offset
;
if
(
dmem
->
flags
&
PY_AUDIT_READ
) {
SPECIALIZATION_FAIL
(
LOAD_ATTR
,
SPEC_FAIL_ATTR_AUDITED_SLOT
);
goto
fail
;
}
if
(
offset
!=
(
uint16_t
)
offset
) {
SPECIALIZATION_FAIL
(
LOAD_ATTR
,
SPEC_FAIL_OUT_OF_RANGE
);
goto
fail
;
}
assert
(
dmem
->
type
==
T_OBJECT_EX
);
assert
(
offset
>
0
);
cache
->
index
=
(
uint16_t
)
offset
;
write_u32
(
cache
->
version
,
type
->
tp_version_tag
);
_Py_SET_OPCODE
(
*
instr
,
LOAD_ATTR_SLOT
);
goto
success
;
}
case
DUNDER_CLASS
:
{
Py_ssize_t
offset
=
offsetof(
PyObject
,
ob_type
);
assert
(
offset
==
(
uint16_t
)
offset
);
cache
->
index
=
(
uint16_t
)
offset
;
write_u32
(
cache
->
version
,
type
->
tp_version_tag
);
_Py_SET_OPCODE
(
*
instr
,
LOAD_ATTR_SLOT
);
goto
success
;
}
case
OTHER_SLOT
:
SPECIALIZATION_FAIL
(
LOAD_ATTR
,
SPEC_FAIL_ATTR_NON_OBJECT_SLOT
);
goto
fail
;
case
MUTABLE
:
SPECIALIZATION_FAIL
(
LOAD_ATTR
,
SPEC_FAIL_ATTR_MUTABLE_CLASS
);
goto
fail
;
case
GETSET_OVERRIDDEN
:
SPECIALIZATION_FAIL
(
LOAD_ATTR
,
SPEC_FAIL_OVERRIDDEN
);
goto
fail
;
case
GETATTRIBUTE_IS_PYTHON_FUNCTION
:
{
assert
(
type
->
tp_getattro
==
_Py_slot_tp_getattro
);
assert
(
Py_IS_TYPE
(
descr
,
&
PyFunction_Type
));
_PyLoadMethodCache
*
lm_cache
=
(
_PyLoadMethodCache
*
)(
instr
+
1
);
if
(!
function_check_args
(
descr
,
2
,
LOAD_ATTR
)) {
goto
fail
;
}
uint32_t
version
=
function_get_version
(
descr
,
LOAD_ATTR
);
if
(
version
==
0
) {
goto
fail
;
}
write_u32
(
lm_cache
->
keys_version
,
version
);
/* borrowed */
write_obj
(
lm_cache
->
descr
,
descr
);
write_u32
(
lm_cache
->
type_version
,
type
->
tp_version_tag
);
_Py_SET_OPCODE
(
*
instr
,
LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN
);
goto
success
;
}
case
BUILTIN_CLASSMETHOD
:
case
PYTHON_CLASSMETHOD
:
case
NON_OVERRIDING
:
case
NON_DESCRIPTOR
:
case
ABSENT
:
break
;
}
int
err
=
specialize_dict_access
(
owner
,
instr
,
type
,
kind
,
name
,
LOAD_ATTR
,
LOAD_ATTR_INSTANCE_VALUE
,
LOAD_ATTR_WITH_HINT
);
if
(
err
<
0
) {
return
-1
;
}
if
(
err
) {
goto
success
;
}
fail
:
STAT_INC
(
LOAD_ATTR
,
failure
);
assert
(!
PyErr_Occurred
());
cache
->
counter
=
adaptive_counter_backoff
(
cache
->
counter
);
return
0
;
success
:
STAT_INC
(
LOAD_ATTR
,
success
);
assert
(!
PyErr_Occurred
());
cache
->
counter
=
miss_counter_start
();
return
0
;
}
int
_Py_Specialize_StoreAttr
(
PyObject
*
owner
,
_Py_CODEUNIT
*
instr
,
PyObject
*
name
)
{
assert
(
_PyOpcode_Caches
[
STORE_ATTR
]
==
INLINE_CACHE_ENTRIES_STORE_ATTR
);
_PyAttrCache
*
cache
=
(
_PyAttrCache
*
)(
instr
+
1
);
PyTypeObject
*
type
=
Py_TYPE
(
owner
);
if
(
PyModule_CheckExact
(
owner
)) {
SPECIALIZATION_FAIL
(
STORE_ATTR
,
SPEC_FAIL_OVERRIDDEN
);
goto
fail
;
}
PyObject
*
descr
;
DescriptorClassification
kind
=
analyze_descriptor
(
type
,
name
,
&
descr
,
1
);
switch
(
kind
) {
case
OVERRIDING
:
SPECIALIZATION_FAIL
(
STORE_ATTR
,
SPEC_FAIL_ATTR_OVERRIDING_DESCRIPTOR
);
goto
fail
;
case
METHOD
:
SPECIALIZATION_FAIL
(
STORE_ATTR
,
SPEC_FAIL_ATTR_METHOD
);
goto
fail
;
case
PROPERTY
:
SPECIALIZATION_FAIL
(
STORE_ATTR
,
SPEC_FAIL_ATTR_PROPERTY
);
goto
fail
;
case
OBJECT_SLOT
:
{
PyMemberDescrObject
*
member
=
(
PyMemberDescrObject
*
)
descr
;
struct
PyMemberDef
*
dmem
=
member
->
d_member
;
Py_ssize_t
offset
=
dmem
->
offset
;
if
(
dmem
->
flags
&
READONLY
) {
SPECIALIZATION_FAIL
(
STORE_ATTR
,
SPEC_FAIL_ATTR_READ_ONLY
);
goto
fail
;
}
if
(
offset
!=
(
uint16_t
)
offset
) {
SPECIALIZATION_FAIL
(
STORE_ATTR
,
SPEC_FAIL_OUT_OF_RANGE
);
goto
fail
;
}
assert
(
dmem
->
type
==
T_OBJECT_EX
);
assert
(
offset
>
0
);
cache
->
index
=
(
uint16_t
)
offset
;
write_u32
(
cache
->
version
,
type
->
tp_version_tag
);
_Py_SET_OPCODE
(
*
instr
,
STORE_ATTR_SLOT
);
goto
success
;
}
case
DUNDER_CLASS
:
case
OTHER_SLOT
:
SPECIALIZATION_FAIL
(
STORE_ATTR
,
SPEC_FAIL_ATTR_NON_OBJECT_SLOT
);
goto
fail
;
case
MUTABLE
:
SPECIALIZATION_FAIL
(
STORE_ATTR
,
SPEC_FAIL_ATTR_MUTABLE_CLASS
);
goto
fail
;
case
GETATTRIBUTE_IS_PYTHON_FUNCTION
:
case
GETSET_OVERRIDDEN
:
SPECIALIZATION_FAIL
(
STORE_ATTR
,
SPEC_FAIL_OVERRIDDEN
);
goto
fail
;
case
BUILTIN_CLASSMETHOD
:
case
PYTHON_CLASSMETHOD
:
case
NON_OVERRIDING
:
case
NON_DESCRIPTOR
:
case
ABSENT
:
break
;
}
int
err
=
specialize_dict_access
(
owner
,
instr
,
type
,
kind
,
name
,
STORE_ATTR
,
STORE_ATTR_INSTANCE_VALUE
,
STORE_ATTR_WITH_HINT
);
if
(
err
<
0
) {
return
-1
;
}
if
(
err
) {
goto
success
;
}
fail
:
STAT_INC
(
STORE_ATTR
,
failure
);
assert
(!
PyErr_Occurred
());
cache
->
counter
=
adaptive_counter_backoff
(
cache
->
counter
);
return
0
;
success
:
STAT_INC
(
STORE_ATTR
,
success
);
assert
(!
PyErr_Occurred
());
cache
->
counter
=
miss_counter_start
();
return
0
;
}
#ifdef
Py_STATS
static
int
load_attr_fail_kind
(
DescriptorClassification
kind
)
{
switch
(
kind
) {
case
OVERRIDING
:
return
SPEC_FAIL_ATTR_OVERRIDING_DESCRIPTOR
;
case
METHOD
:
return
SPEC_FAIL_ATTR_METHOD
;
case
PROPERTY
:
return
SPEC_FAIL_ATTR_PROPERTY
;
case
OBJECT_SLOT
:
return
SPEC_FAIL_ATTR_OBJECT_SLOT
;
case
OTHER_SLOT
:
return
SPEC_FAIL_ATTR_NON_OBJECT_SLOT
;
case
DUNDER_CLASS
:
return
SPEC_FAIL_OTHER
;
case
MUTABLE
:
return
SPEC_FAIL_ATTR_MUTABLE_CLASS
;
case
GETSET_OVERRIDDEN
:
case
GETATTRIBUTE_IS_PYTHON_FUNCTION
:
return
SPEC_FAIL_OVERRIDDEN
;
case
BUILTIN_CLASSMETHOD
:
return
SPEC_FAIL_ATTR_BUILTIN_CLASS_METHOD
;
case
PYTHON_CLASSMETHOD
:
return
SPEC_FAIL_ATTR_CLASS_METHOD_OBJ
;
case
NON_OVERRIDING
:
return
SPEC_FAIL_ATTR_NON_OVERRIDING_DESCRIPTOR
;
case
NON_DESCRIPTOR
:
return
SPEC_FAIL_ATTR_NOT_DESCRIPTOR
;
case
ABSENT
:
return
SPEC_FAIL_ATTR_INSTANCE_ATTRIBUTE
;
}
Py_UNREACHABLE
();
}
#endif
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL