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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX Utils.cpp XX
XX XX
XX Has miscellaneous utility functions XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#include "opcode.h"
/*****************************************************************************/
// Define the string platform name based on compilation #ifdefs. This is the
// same code for all platforms, hence it is here instead of in the targetXXX.cpp
// files.
#ifdef _TARGET_UNIX_
// Should we distinguish Mac? Can we?
// Should we distinguish flavors of Unix? Can we?
const char* Target::g_tgtPlatformName = "Unix";
#else // !_TARGET_UNIX_
const char* Target::g_tgtPlatformName = "Windows";
#endif // !_TARGET_UNIX_
/*****************************************************************************/
#define DECLARE_DATA
// clang-format off
extern
const signed char opcodeSizes[] =
{
#define InlineNone_size 0
#define ShortInlineVar_size 1
#define InlineVar_size 2
#define ShortInlineI_size 1
#define InlineI_size 4
#define InlineI8_size 8
#define ShortInlineR_size 4
#define InlineR_size 8
#define ShortInlineBrTarget_size 1
#define InlineBrTarget_size 4
#define InlineMethod_size 4
#define InlineField_size 4
#define InlineType_size 4
#define InlineString_size 4
#define InlineSig_size 4
#define InlineRVA_size 4
#define InlineTok_size 4
#define InlineSwitch_size 0 // for now
#define InlinePhi_size 0 // for now
#define InlineVarTok_size 0 // remove
#define OPDEF(name,string,pop,push,oprType,opcType,l,s1,s2,ctrl) oprType ## _size ,
#include "opcode.def"
#undef OPDEF
#undef InlineNone_size
#undef ShortInlineVar_size
#undef InlineVar_size
#undef ShortInlineI_size
#undef InlineI_size
#undef InlineI8_size
#undef ShortInlineR_size
#undef InlineR_size
#undef ShortInlineBrTarget_size
#undef InlineBrTarget_size
#undef InlineMethod_size
#undef InlineField_size
#undef InlineType_size
#undef InlineString_size
#undef InlineSig_size
#undef InlineRVA_size
#undef InlineTok_size
#undef InlineSwitch_size
#undef InlinePhi_size
};
// clang-format on
const BYTE varTypeClassification[] = {
#define DEF_TP(tn, nm, jitType, verType, sz, sze, asze, st, al, tf, howUsed) tf,
#include "typelist.h"
#undef DEF_TP
};
/*****************************************************************************/
/*****************************************************************************/
#ifdef DEBUG
extern const char* const opcodeNames[] = {
#define OPDEF(name, string, pop, push, oprType, opcType, l, s1, s2, ctrl) string,
#include "opcode.def"
#undef OPDEF
};
extern const BYTE opcodeArgKinds[] = {
#define OPDEF(name, string, pop, push, oprType, opcType, l, s1, s2, ctrl) (BYTE) oprType,
#include "opcode.def"
#undef OPDEF
};
#endif
/*****************************************************************************/
const char* varTypeName(var_types vt)
{
static const char* const varTypeNames[] = {
#define DEF_TP(tn, nm, jitType, verType, sz, sze, asze, st, al, tf, howUsed) nm,
#include "typelist.h"
#undef DEF_TP
};
assert((unsigned)vt < _countof(varTypeNames));
return varTypeNames[vt];
}
#if defined(DEBUG) || defined(LATE_DISASM)
/*****************************************************************************
*
* Return the name of the given register.
*/
const char* getRegName(regNumber reg, bool isFloat)
{
// Special-case REG_NA; it's not in the regNames array, but we might want to print it.
if (reg == REG_NA)
{
return "NA";
}
#if defined(_TARGET_ARM64_)
static const char* const regNames[] = {
#define REGDEF(name, rnum, mask, xname, wname) xname,
#include "register.h"
};
assert(reg < ArrLen(regNames));
return regNames[reg];
#else
static const char* const regNames[] = {
#define REGDEF(name, rnum, mask, sname) sname,
#include "register.h"
};
assert(reg < ArrLen(regNames));
return regNames[reg];
#endif
}
const char* getRegName(unsigned reg,
bool isFloat) // this is for gcencode.cpp and disasm.cpp that dont use the regNumber type
{
return getRegName((regNumber)reg, isFloat);
}
#endif // defined(DEBUG) || defined(LATE_DISASM)
#if defined(DEBUG)
const char* getRegNameFloat(regNumber reg, var_types type)
{
#ifdef _TARGET_ARM_
assert(genIsValidFloatReg(reg));
if (type == TYP_FLOAT)
return getRegName(reg);
else
{
const char* regName;
switch (reg)
{
default:
assert(!"Bad double register");
regName = "d??";
break;
case REG_F0:
regName = "d0";
break;
case REG_F2:
regName = "d2";
break;
case REG_F4:
regName = "d4";
break;
case REG_F6:
regName = "d6";
break;
case REG_F8:
regName = "d8";
break;
case REG_F10:
regName = "d10";
break;
case REG_F12:
regName = "d12";
break;
case REG_F14:
regName = "d14";
break;
case REG_F16:
regName = "d16";
break;
case REG_F18:
regName = "d18";
break;
case REG_F20:
regName = "d20";
break;
case REG_F22:
regName = "d22";
break;
case REG_F24:
regName = "d24";
break;
case REG_F26:
regName = "d26";
break;
case REG_F28:
regName = "d28";
break;
case REG_F30:
regName = "d30";
break;
}
return regName;
}
#elif defined(_TARGET_ARM64_)
static const char* regNamesFloat[] = {
#define REGDEF(name, rnum, mask, xname, wname) xname,
#include "register.h"
};
assert((unsigned)reg < ArrLen(regNamesFloat));
return regNamesFloat[reg];
#else
static const char* regNamesFloat[] = {
#define REGDEF(name, rnum, mask, sname) "x" sname,
#include "register.h"
};
#ifdef FEATURE_SIMD
static const char* regNamesYMM[] = {
#define REGDEF(name, rnum, mask, sname) "y" sname,
#include "register.h"
};
#endif // FEATURE_SIMD
assert((unsigned)reg < ArrLen(regNamesFloat));
#ifdef FEATURE_SIMD
if (type == TYP_SIMD32)
{
return regNamesYMM[reg];
}
#endif // FEATURE_SIMD
return regNamesFloat[reg];
#endif
}
/*****************************************************************************
*
* Displays a register set.
* TODO-ARM64-Cleanup: don't allow ip0, ip1 as part of a range.
*/
void dspRegMask(regMaskTP regMask, size_t minSiz)
{
const char* sep = "";
printf("[");
bool inRegRange = false;
regNumber regPrev = REG_NA;
regNumber regHead = REG_NA; // When we start a range, remember the first register of the range, so we don't use
// range notation if the range contains just a single register.
for (regNumber regNum = REG_INT_FIRST; regNum <= REG_INT_LAST; regNum = REG_NEXT(regNum))
{
regMaskTP regBit = genRegMask(regNum);
if ((regMask & regBit) != 0)
{
// We have a register to display. It gets displayed now if:
// 1. This is the first register to display of a new range of registers (possibly because
// no register has ever been displayed).
// 2. This is the last register of an acceptable range (either the last integer register,
// or the last of a range that is displayed with range notation).
if (!inRegRange)
{
// It's the first register of a potential range.
const char* nam = getRegName(regNum);
printf("%s%s", sep, nam);
minSiz -= strlen(sep) + strlen(nam);
// By default, we're not starting a potential register range.
sep = " ";
// What kind of separator should we use for this range (if it is indeed going to be a range)?
CLANG_FORMAT_COMMENT_ANCHOR;
#if defined(_TARGET_AMD64_)
// For AMD64, create ranges for int registers R8 through R15, but not the "old" registers.
if (regNum >= REG_R8)
{
regHead = regNum;
inRegRange = true;
sep = "-";
}
#elif defined(_TARGET_ARM64_)
// R17 and R28 can't be the start of a range, since the range would include TEB or FP
if ((regNum < REG_R17) || ((REG_R19 <= regNum) && (regNum < REG_R28)))
{
regHead = regNum;
inRegRange = true;
sep = "-";
}
#elif defined(_TARGET_ARM_)
if (regNum < REG_R12)
{
regHead = regNum;
inRegRange = true;
sep = "-";
}
#elif defined(_TARGET_X86_)
// No register ranges
#else // _TARGET_*
#error Unsupported or unset target architecture
#endif // _TARGET_*
}
#if defined(_TARGET_ARM64_)
// We've already printed a register. Is this the end of a range?
else if ((regNum == REG_INT_LAST) || (regNum == REG_R17) // last register before TEB
|| (regNum == REG_R28)) // last register before FP
#else // _TARGET_ARM64_
// We've already printed a register. Is this the end of a range?
else if (regNum == REG_INT_LAST)
#endif // _TARGET_ARM64_
{
const char* nam = getRegName(regNum);
printf("%s%s", sep, nam);
minSiz -= strlen(sep) + strlen(nam);
inRegRange = false; // No longer in the middle of a register range
regHead = REG_NA;
sep = " ";
}
}
else // ((regMask & regBit) == 0)
{
if (inRegRange)
{
assert(regHead != REG_NA);
if (regPrev != regHead)
{
// Close out the previous range, if it included more than one register.
const char* nam = getRegName(regPrev);
printf("%s%s", sep, nam);
minSiz -= strlen(sep) + strlen(nam);
}
sep = " ";
inRegRange = false;
regHead = REG_NA;
}
}
if (regBit > regMask)
{
break;
}
regPrev = regNum;
}
if (strlen(sep) > 0)
{
// We've already printed something.
sep = " ";
}
inRegRange = false;
regPrev = REG_NA;
regHead = REG_NA;
for (regNumber regNum = REG_FP_FIRST; regNum <= REG_FP_LAST; regNum = REG_NEXT(regNum))
{
regMaskTP regBit = genRegMask(regNum);
if (regMask & regBit)
{
if (!inRegRange || (regNum == REG_FP_LAST))
{
const char* nam = getRegName(regNum);
printf("%s%s", sep, nam);
minSiz -= strlen(sep) + strlen(nam);
sep = "-";
regHead = regNum;
}
inRegRange = true;
}
else
{
if (inRegRange)
{
if (regPrev != regHead)
{
const char* nam = getRegName(regPrev);
printf("%s%s", sep, nam);
minSiz -= (strlen(sep) + strlen(nam));
}
sep = " ";
}
inRegRange = false;
}
if (regBit > regMask)
{
break;
}
regPrev = regNum;
}
printf("]");
while ((int)minSiz > 0)
{
printf(" ");
minSiz--;
}
}
//------------------------------------------------------------------------
// dumpILBytes: Helper for dumpSingleInstr() to dump hex bytes of an IL stream,
// aligning up to a minimum alignment width.
//
// Arguments:
// codeAddr - Pointer to IL byte stream to display.
// codeSize - Number of bytes of IL byte stream to display.
// alignSize - Pad out to this many characters, if fewer than this were written.
//
void dumpILBytes(const BYTE* const codeAddr,
unsigned codeSize,
unsigned alignSize) // number of characters to write, for alignment
{
for (IL_OFFSET offs = 0; offs < codeSize; ++offs)
{
printf(" %02x", *(codeAddr + offs));
}
unsigned charsWritten = 3 * codeSize;
for (unsigned i = charsWritten; i < alignSize; i++)
{
printf(" ");
}
}
//------------------------------------------------------------------------
// dumpSingleInstr: Display a single IL instruction.
//
// Arguments:
// codeAddr - Base pointer to a stream of IL instructions.
// offs - Offset from codeAddr of the IL instruction to display.
// prefix - Optional string to prefix the IL instruction with (if nullptr, no prefix is output).
//
// Return Value:
// Size of the displayed IL instruction in the instruction stream, in bytes. (Add this to 'offs' to
// get to the next instruction.)
//
unsigned dumpSingleInstr(const BYTE* const codeAddr, IL_OFFSET offs, const char* prefix)
{
const BYTE* opcodePtr = codeAddr + offs;
const BYTE* startOpcodePtr = opcodePtr;
const unsigned ALIGN_WIDTH = 3 * 6; // assume 3 characters * (1 byte opcode + 4 bytes data + 1 prefix byte) for
// most things
if (prefix != nullptr)
{
printf("%s", prefix);
}
OPCODE opcode = (OPCODE)getU1LittleEndian(opcodePtr);
opcodePtr += sizeof(__int8);
DECODE_OPCODE:
if (opcode >= CEE_COUNT)
{
printf("\nIllegal opcode: %02X\n", (int)opcode);
return (IL_OFFSET)(opcodePtr - startOpcodePtr);
}
/* Get the size of additional parameters */
size_t sz = opcodeSizes[opcode];
unsigned argKind = opcodeArgKinds[opcode];
/* See what kind of an opcode we have, then */
switch (opcode)
{
case CEE_PREFIX1:
opcode = OPCODE(getU1LittleEndian(opcodePtr) + 256);
opcodePtr += sizeof(__int8);
goto DECODE_OPCODE;
default:
{
__int64 iOp;
double dOp;
int jOp;
DWORD jOp2;
switch (argKind)
{
case InlineNone:
dumpILBytes(startOpcodePtr, (unsigned)(opcodePtr - startOpcodePtr), ALIGN_WIDTH);
printf(" %-12s", opcodeNames[opcode]);
break;
case ShortInlineVar:
iOp = getU1LittleEndian(opcodePtr);
goto INT_OP;
case ShortInlineI:
iOp = getI1LittleEndian(opcodePtr);
goto INT_OP;
case InlineVar:
iOp = getU2LittleEndian(opcodePtr);
goto INT_OP;
case InlineTok:
case InlineMethod:
case InlineField:
case InlineType:
case InlineString:
case InlineSig:
case InlineI:
iOp = getI4LittleEndian(opcodePtr);
goto INT_OP;
case InlineI8:
iOp = getU4LittleEndian(opcodePtr);
iOp |= (__int64)getU4LittleEndian(opcodePtr + 4) << 32;
goto INT_OP;
INT_OP:
dumpILBytes(startOpcodePtr, (unsigned)((opcodePtr - startOpcodePtr) + sz), ALIGN_WIDTH);
printf(" %-12s 0x%X", opcodeNames[opcode], iOp);
break;
case ShortInlineR:
dOp = getR4LittleEndian(opcodePtr);
goto FLT_OP;
case InlineR:
dOp = getR8LittleEndian(opcodePtr);
goto FLT_OP;
FLT_OP:
dumpILBytes(startOpcodePtr, (unsigned)((opcodePtr - startOpcodePtr) + sz), ALIGN_WIDTH);
printf(" %-12s %f", opcodeNames[opcode], dOp);
break;
case ShortInlineBrTarget:
jOp = getI1LittleEndian(opcodePtr);
goto JMP_OP;
case InlineBrTarget:
jOp = getI4LittleEndian(opcodePtr);
goto JMP_OP;
JMP_OP:
dumpILBytes(startOpcodePtr, (unsigned)((opcodePtr - startOpcodePtr) + sz), ALIGN_WIDTH);
printf(" %-12s %d (IL_%04x)", opcodeNames[opcode], jOp, (int)(opcodePtr + sz - codeAddr) + jOp);
break;
case InlineSwitch:
jOp2 = getU4LittleEndian(opcodePtr);
opcodePtr += 4;
opcodePtr += jOp2 * 4; // Jump over the table
dumpILBytes(startOpcodePtr, (unsigned)(opcodePtr - startOpcodePtr), ALIGN_WIDTH);
printf(" %-12s", opcodeNames[opcode]);
break;
case InlinePhi:
jOp2 = getU1LittleEndian(opcodePtr);
opcodePtr += 1;
opcodePtr += jOp2 * 2; // Jump over the table
dumpILBytes(startOpcodePtr, (unsigned)(opcodePtr - startOpcodePtr), ALIGN_WIDTH);
printf(" %-12s", opcodeNames[opcode]);
break;
default:
assert(!"Bad argKind");
}
opcodePtr += sz;
break;
}
}
printf("\n");
return (IL_OFFSET)(opcodePtr - startOpcodePtr);
}
//------------------------------------------------------------------------
// dumpILRange: Display a range of IL instructions from an IL instruction stream.
//
// Arguments:
// codeAddr - Pointer to IL byte stream to display.
// codeSize - Number of bytes of IL byte stream to display.
//
void dumpILRange(const BYTE* const codeAddr, unsigned codeSize) // in bytes
{
for (IL_OFFSET offs = 0; offs < codeSize;)
{
char prefix[100];
sprintf_s(prefix, _countof(prefix), "IL_%04x ", offs);
unsigned codeBytesDumped = dumpSingleInstr(codeAddr, offs, prefix);
offs += codeBytesDumped;
}
}
/*****************************************************************************
*
* Display a variable set.
*/
const char* genES2str(BitVecTraits* traits, EXPSET_TP set)
{
const int bufSize = 17;
static char num1[bufSize];
static char num2[bufSize];
static char* nump = num1;
char* temp = nump;
nump = (nump == num1) ? num2 : num1;
sprintf_s(temp, bufSize, "%s", BitVecOps::ToString(traits, set));
return temp;
}
const char* refCntWtd2str(unsigned refCntWtd)
{
const int bufSize = 17;
static char num1[bufSize];
static char num2[bufSize];
static char* nump = num1;
char* temp = nump;
nump = (nump == num1) ? num2 : num1;
if (refCntWtd == BB_MAX_WEIGHT)
{
sprintf_s(temp, bufSize, "MAX ");
}
else
{
unsigned valueInt = refCntWtd / BB_UNITY_WEIGHT;
unsigned valueFrac = refCntWtd % BB_UNITY_WEIGHT;
if (valueFrac == 0)
{
sprintf_s(temp, bufSize, "%u ", valueInt);
}
else
{
sprintf_s(temp, bufSize, "%u.%02u", valueInt, (valueFrac * 100 / BB_UNITY_WEIGHT));
}
}
return temp;
}
#endif // DEBUG
#if defined(DEBUG) || defined(INLINE_DATA)
//------------------------------------------------------------------------
// Contains: check if the range includes a particular method
//
// Arguments:
// info -- jit interface pointer
// method -- method handle for the method of interest
bool ConfigMethodRange::Contains(ICorJitInfo* info, CORINFO_METHOD_HANDLE method)
{
_ASSERT(m_inited == 1);
// No ranges specified means all methods included.
if (m_lastRange == 0)
{
return true;
}
// Check the hash. Note we can't use the cached hash here since
// we may not be asking about the method currently being jitted.
const unsigned hash = info->getMethodHash(method);
for (unsigned i = 0; i < m_lastRange; i++)
{
if ((m_ranges[i].m_low <= hash) && (hash <= m_ranges[i].m_high))
{
return true;
}
}
return false;
}
//------------------------------------------------------------------------
// InitRanges: parse the range string and set up the range info
//
// Arguments:
// rangeStr -- string to parse (may be nullptr)
// capacity -- number ranges to allocate in the range array
//
// Notes:
// Does some internal error checking; clients can use Error()
// to determine if the range string couldn't be fully parsed
// because of bad characters or too many entries, or had values
// that were too large to represent.
void ConfigMethodRange::InitRanges(const wchar_t* rangeStr, unsigned capacity)
{
// Make sure that the memory was zero initialized
assert(m_inited == 0 || m_inited == 1);
assert(m_entries == 0);
assert(m_ranges == nullptr);
assert(m_lastRange == 0);
// Flag any crazy-looking requests
assert(capacity < 100000);
if (rangeStr == nullptr)
{
m_inited = 1;
return;
}
// Allocate some persistent memory
ICorJitHost* jitHost = g_jitHost;
m_ranges = (Range*)jitHost->allocateMemory(capacity * sizeof(Range));
m_entries = capacity;
const wchar_t* p = rangeStr;
unsigned lastRange = 0;
bool setHighPart = false;
while ((*p != 0) && (lastRange < m_entries))
{
while (*p == L' ')
{
p++;
}
int i = 0;
while (L'0' <= *p && *p <= L'9')
{
int j = 10 * i + ((*p++) - L'0');
// Check for overflow
if ((m_badChar != 0) && (j <= i))
{
m_badChar = (p - rangeStr) + 1;
}
i = j;
}
// Was this the high part of a low-high pair?
if (setHighPart)
{
// Yep, set it and move to the next range
m_ranges[lastRange].m_high = i;
// Sanity check that range is proper
if ((m_badChar != 0) && (m_ranges[lastRange].m_high < m_ranges[lastRange].m_low))
{
m_badChar = (p - rangeStr) + 1;
}
lastRange++;
setHighPart = false;
continue;
}
// Must have been looking for the low part of a range
m_ranges[lastRange].m_low = i;
while (*p == L' ')
{
p++;
}
// Was that the low part of a low-high pair?
if (*p == L'-')
{
// Yep, skip the dash and set high part next time around.
p++;
setHighPart = true;
continue;
}
// Else we have a point range, so set high = low
m_ranges[lastRange].m_high = i;
lastRange++;
}
// If we didn't parse the full range string, note index of the the
// first bad char.
if ((m_badChar != 0) && (*p != 0))
{
m_badChar = (p - rangeStr) + 1;
}
// Finish off any remaining open range
if (setHighPart)
{
m_ranges[lastRange].m_high = UINT_MAX;
lastRange++;
}
assert(lastRange <= m_entries);
m_lastRange = lastRange;
m_inited = 1;
}
#endif // defined(DEBUG) || defined(INLINE_DATA)
#if CALL_ARG_STATS || COUNT_BASIC_BLOCKS || COUNT_LOOPS || EMITTER_STATS || MEASURE_NODE_SIZE || MEASURE_MEM_ALLOC
/*****************************************************************************
* Histogram class.
*/
Histogram::Histogram(const unsigned* const sizeTable) : m_sizeTable(sizeTable)
{
unsigned sizeCount = 0;
do
{
sizeCount++;
} while ((sizeTable[sizeCount] != 0) && (sizeCount < 1000));
assert(sizeCount < HISTOGRAM_MAX_SIZE_COUNT - 1);
m_sizeCount = sizeCount;
memset(m_counts, 0, (m_sizeCount + 1) * sizeof(*m_counts));
}
void Histogram::dump(FILE* output)
{
unsigned t = 0;
for (unsigned i = 0; i < m_sizeCount; i++)
{
t += m_counts[i];
}
for (unsigned c = 0, i = 0; i <= m_sizeCount; i++)
{
if (i == m_sizeCount)
{
if (m_counts[i] == 0)
{
break;
}
fprintf(output, " > %7u", m_sizeTable[i - 1]);
}
else
{
if (i == 0)
{
fprintf(output, " <= ");
}
else
{
fprintf(output, "%7u .. ", m_sizeTable[i - 1] + 1);
}
fprintf(output, "%7u", m_sizeTable[i]);
}
c += m_counts[i];
fprintf(output, " ===> %7u count (%3u%% of total)\n", m_counts[i], (int)(100.0 * c / t));
}
}
void Histogram::record(unsigned size)
{
unsigned i;
for (i = 0; i < m_sizeCount; i++)
{
if (m_sizeTable[i] >= size)
{
break;
}
}
m_counts[i]++;
}
#endif // CALL_ARG_STATS || COUNT_BASIC_BLOCKS || COUNT_LOOPS || EMITTER_STATS || MEASURE_NODE_SIZE
/*****************************************************************************
* Fixed bit vector class
*/
// bitChunkSize() - Returns number of bits in a bitVect chunk
inline UINT FixedBitVect::bitChunkSize()
{
return sizeof(UINT) * 8;
}
// bitNumToBit() - Returns a bit mask of the given bit number
inline UINT FixedBitVect::bitNumToBit(UINT bitNum)
{
assert(bitNum < bitChunkSize());
assert(bitChunkSize() <= sizeof(int) * 8);
return 1 << bitNum;
}
// bitVectInit() - Initializes a bit vector of a given size
FixedBitVect* FixedBitVect::bitVectInit(UINT size, Compiler* comp)
{
UINT bitVectMemSize, numberOfChunks;
FixedBitVect* bv;
assert(size != 0);
numberOfChunks = (size - 1) / bitChunkSize() + 1;
bitVectMemSize = numberOfChunks * (bitChunkSize() / 8); // size in bytes
assert(bitVectMemSize * bitChunkSize() >= size);
bv = (FixedBitVect*)comp->getAllocator(CMK_FixedBitVect).allocate<char>(sizeof(FixedBitVect) + bitVectMemSize);
memset(bv->bitVect, 0, bitVectMemSize);
bv->bitVectSize = size;
return bv;
}
// bitVectSet() - Sets the given bit
void FixedBitVect::bitVectSet(UINT bitNum)
{
UINT index;
assert(bitNum <= bitVectSize);
index = bitNum / bitChunkSize();
bitNum -= index * bitChunkSize();
bitVect[index] |= bitNumToBit(bitNum);
}
// bitVectTest() - Tests the given bit
bool FixedBitVect::bitVectTest(UINT bitNum)
{
UINT index;
assert(bitNum <= bitVectSize);
index = bitNum / bitChunkSize();
bitNum -= index * bitChunkSize();
return (bitVect[index] & bitNumToBit(bitNum)) != 0;
}
// bitVectOr() - Or in the given bit vector
void FixedBitVect::bitVectOr(FixedBitVect* bv)
{
UINT bitChunkCnt = (bitVectSize - 1) / bitChunkSize() + 1;
assert(bitVectSize == bv->bitVectSize);
// Or each chunks
for (UINT i = 0; i < bitChunkCnt; i++)
{
bitVect[i] |= bv->bitVect[i];
}
}
// bitVectAnd() - And with passed in bit vector
void FixedBitVect::bitVectAnd(FixedBitVect& bv)
{
UINT bitChunkCnt = (bitVectSize - 1) / bitChunkSize() + 1;
assert(bitVectSize == bv.bitVectSize);
// And each chunks
for (UINT i = 0; i < bitChunkCnt; i++)
{
bitVect[i] &= bv.bitVect[i];
}
}
// bitVectGetFirst() - Find the first bit on and return bit num,
// Return -1 if no bits found.
UINT FixedBitVect::bitVectGetFirst()
{
return bitVectGetNext((UINT)-1);
}
// bitVectGetNext() - Find the next bit on given previous position and return bit num.
// Return -1 if no bits found.
UINT FixedBitVect::bitVectGetNext(UINT bitNumPrev)
{
UINT bitNum = (UINT)-1;
UINT index;
UINT bitMask;
UINT bitChunkCnt = (bitVectSize - 1) / bitChunkSize() + 1;
UINT i;
if (bitNumPrev == (UINT)-1)
{
index = 0;
bitMask = (UINT)-1;
}
else
{
UINT bit;
index = bitNumPrev / bitChunkSize();
bitNumPrev -= index * bitChunkSize();
bit = bitNumToBit(bitNumPrev);
bitMask = ~(bit | (bit - 1));
}
// Find first bit
for (i = index; i < bitChunkCnt; i++)
{
UINT bitChunk = bitVect[i] & bitMask;
if (bitChunk != 0)
{
BitScanForward((ULONG*)&bitNum, bitChunk);
break;
}
bitMask = 0xFFFFFFFF;
}
// Empty bit vector?
if (bitNum == (UINT)-1)
{
return (UINT)-1;
}
bitNum += i * bitChunkSize();
assert(bitNum <= bitVectSize);
return bitNum;
}
// bitVectGetNextAndClear() - Find the first bit on, clear it and return it.
// Return -1 if no bits found.
UINT FixedBitVect::bitVectGetNextAndClear()
{
UINT bitNum = (UINT)-1;
UINT bitChunkCnt = (bitVectSize - 1) / bitChunkSize() + 1;
UINT i;
// Find first bit
for (i = 0; i < bitChunkCnt; i++)
{
if (bitVect[i] != 0)
{
BitScanForward((ULONG*)&bitNum, bitVect[i]);
break;
}
}
// Empty bit vector?
if (bitNum == (UINT)-1)
{
return (UINT)-1;
}
// Clear the bit in the right chunk
bitVect[i] &= ~bitNumToBit(bitNum);
bitNum += i * bitChunkSize();
assert(bitNum <= bitVectSize);
return bitNum;
}
int SimpleSprintf_s(__in_ecount(cbBufSize - (pWriteStart - pBufStart)) char* pWriteStart,
__in_ecount(cbBufSize) char* pBufStart,
size_t cbBufSize,
__in_z const char* fmt,
...)
{
assert(fmt);
assert(pBufStart);
assert(pWriteStart);
assert((size_t)pBufStart <= (size_t)pWriteStart);
int ret;
// compute the space left in the buffer.
if ((pBufStart + cbBufSize) < pWriteStart)
{
NO_WAY("pWriteStart is past end of buffer");
}
size_t cbSpaceLeft = (size_t)((pBufStart + cbBufSize) - pWriteStart);
va_list args;
va_start(args, fmt);
ret = vsprintf_s(pWriteStart, cbSpaceLeft, const_cast<char*>(fmt), args);
va_end(args);
if (ret < 0)
{
NO_WAY("vsprintf_s failed.");
}
return ret;
}
#ifdef DEBUG
void hexDump(FILE* dmpf, const char* name, BYTE* addr, size_t size)
{
if (!size)
{
return;
}
assert(addr);
fprintf(dmpf, "Hex dump of %s:\n", name);
for (unsigned i = 0; i < size; i++)
{
if ((i % 16) == 0)
{
fprintf(dmpf, "\n %04X: ", i);
}
fprintf(dmpf, "%02X ", *addr++);
}
fprintf(dmpf, "\n\n");
}
#endif // DEBUG
void HelperCallProperties::init()
{
for (CorInfoHelpFunc helper = CORINFO_HELP_UNDEF; // initialize helper
(helper < CORINFO_HELP_COUNT); // test helper for loop exit
helper = CorInfoHelpFunc(int(helper) + 1)) // update helper to next
{
// Generally you want initialize these to their most typical/safest result
//
bool isPure = false; // true if the result only depends upon input args and not any global state
bool noThrow = false; // true if the helper will never throw
bool nonNullReturn = false; // true if the result will never be null or zero
bool isAllocator = false; // true if the result is usually a newly created heap item, or may throw OutOfMemory
bool mutatesHeap = false; // true if any previous heap objects [are|can be] modified
bool mayRunCctor = false; // true if the helper call may cause a static constructor to be run.
bool mayFinalize = false; // true if the helper call allocates an object that may need to run a finalizer
switch (helper)
{
// Arithmetic helpers that cannot throw
case CORINFO_HELP_LLSH:
case CORINFO_HELP_LRSH:
case CORINFO_HELP_LRSZ:
case CORINFO_HELP_LMUL:
case CORINFO_HELP_LNG2DBL:
case CORINFO_HELP_ULNG2DBL:
case CORINFO_HELP_DBL2INT:
case CORINFO_HELP_DBL2LNG:
case CORINFO_HELP_DBL2UINT:
case CORINFO_HELP_DBL2ULNG:
case CORINFO_HELP_FLTREM:
case CORINFO_HELP_DBLREM:
case CORINFO_HELP_FLTROUND:
case CORINFO_HELP_DBLROUND:
isPure = true;
noThrow = true;
break;
// Arithmetic helpers that *can* throw.
// This (or these) are not pure, in that they have "VM side effects"...but they don't mutate the heap.
case CORINFO_HELP_ENDCATCH:
noThrow = true;
break;
// Arithmetic helpers that may throw
case CORINFO_HELP_LMOD: // Mods throw div-by zero, and signed mods have problems with the smallest integer
// mod -1,
case CORINFO_HELP_MOD: // which is not representable as a positive integer.
case CORINFO_HELP_UMOD:
case CORINFO_HELP_ULMOD:
case CORINFO_HELP_UDIV: // Divs throw divide-by-zero.
case CORINFO_HELP_DIV:
case CORINFO_HELP_LDIV:
case CORINFO_HELP_ULDIV:
case CORINFO_HELP_LMUL_OVF:
case CORINFO_HELP_ULMUL_OVF:
case CORINFO_HELP_DBL2INT_OVF:
case CORINFO_HELP_DBL2LNG_OVF:
case CORINFO_HELP_DBL2UINT_OVF:
case CORINFO_HELP_DBL2ULNG_OVF:
isPure = true;
break;
// Heap Allocation helpers, these all never return null
case CORINFO_HELP_NEWSFAST:
case CORINFO_HELP_NEWSFAST_ALIGN8:
isAllocator = true;
nonNullReturn = true;
noThrow = true; // only can throw OutOfMemory
break;
case CORINFO_HELP_NEW_CROSSCONTEXT:
case CORINFO_HELP_NEWFAST:
case CORINFO_HELP_READYTORUN_NEW:
mayFinalize = true; // These may run a finalizer
isAllocator = true;
nonNullReturn = true;
noThrow = true; // only can throw OutOfMemory
break;
// These allocation helpers do some checks on the size (and lower bound) inputs,
// and can throw exceptions other than OOM.
case CORINFO_HELP_NEWARR_1_VC:
case CORINFO_HELP_NEWARR_1_ALIGN8:
isAllocator = true;
nonNullReturn = true;
break;
// These allocation helpers do some checks on the size (and lower bound) inputs,
// and can throw exceptions other than OOM.
case CORINFO_HELP_NEW_MDARR:
case CORINFO_HELP_NEWARR_1_DIRECT:
case CORINFO_HELP_NEWARR_1_OBJ:
case CORINFO_HELP_NEWARR_1_R2R_DIRECT:
case CORINFO_HELP_READYTORUN_NEWARR_1:
mayFinalize = true; // These may run a finalizer
isAllocator = true;
nonNullReturn = true;
break;
// Heap Allocation helpers that are also pure
case CORINFO_HELP_STRCNS:
isPure = true;
isAllocator = true;
nonNullReturn = true;
noThrow = true; // only can throw OutOfMemory
break;
case CORINFO_HELP_BOX:
nonNullReturn = true;
isAllocator = true;
noThrow = true; // only can throw OutOfMemory
break;
case CORINFO_HELP_BOX_NULLABLE:
// Box Nullable is not a 'pure' function
// It has a Byref argument that it reads the contents of.
//
// So two calls to Box Nullable that pass the same address (with the same Value Number)
// will produce different results when the contents of the memory pointed to by the Byref changes
//
isAllocator = true;
noThrow = true; // only can throw OutOfMemory
break;
case CORINFO_HELP_RUNTIMEHANDLE_METHOD:
case CORINFO_HELP_RUNTIMEHANDLE_CLASS:
case CORINFO_HELP_RUNTIMEHANDLE_METHOD_LOG:
case CORINFO_HELP_RUNTIMEHANDLE_CLASS_LOG:
// logging helpers are not technically pure but can be optimized away
isPure = true;
noThrow = true;
nonNullReturn = true;
break;
// type casting helpers
case CORINFO_HELP_ISINSTANCEOFINTERFACE:
case CORINFO_HELP_ISINSTANCEOFARRAY:
case CORINFO_HELP_ISINSTANCEOFCLASS:
case CORINFO_HELP_ISINSTANCEOFANY:
case CORINFO_HELP_READYTORUN_ISINSTANCEOF:
case CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE:
case CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPEHANDLE:
isPure = true;
noThrow = true; // These return null for a failing cast
break;
case CORINFO_HELP_ARE_TYPES_EQUIVALENT:
isPure = true;
noThrow = true;
break;
// type casting helpers that throw
case CORINFO_HELP_CHKCASTINTERFACE:
case CORINFO_HELP_CHKCASTARRAY:
case CORINFO_HELP_CHKCASTCLASS:
case CORINFO_HELP_CHKCASTANY:
case CORINFO_HELP_CHKCASTCLASS_SPECIAL:
case CORINFO_HELP_READYTORUN_CHKCAST:
// These throw for a failing cast
// But if given a null input arg will return null
isPure = true;
break;
// helpers returning addresses, these can also throw
case CORINFO_HELP_UNBOX:
case CORINFO_HELP_GETREFANY:
case CORINFO_HELP_LDELEMA_REF:
isPure = true;
break;
// helpers that return internal handle
case CORINFO_HELP_GETCLASSFROMMETHODPARAM:
case CORINFO_HELP_GETSYNCFROMCLASSHANDLE:
isPure = true;
noThrow = true;
break;
// Helpers that load the base address for static variables.
// We divide these between those that may and may not invoke
// static class constructors.
case CORINFO_HELP_GETSHARED_GCSTATIC_BASE:
case CORINFO_HELP_GETSHARED_NONGCSTATIC_BASE:
case CORINFO_HELP_GETSHARED_GCSTATIC_BASE_DYNAMICCLASS:
case CORINFO_HELP_GETSHARED_NONGCSTATIC_BASE_DYNAMICCLASS:
case CORINFO_HELP_GETGENERICS_GCTHREADSTATIC_BASE:
case CORINFO_HELP_GETGENERICS_NONGCTHREADSTATIC_BASE:
case CORINFO_HELP_GETSHARED_GCTHREADSTATIC_BASE:
case CORINFO_HELP_GETSHARED_NONGCTHREADSTATIC_BASE:
case CORINFO_HELP_CLASSINIT_SHARED_DYNAMICCLASS:
case CORINFO_HELP_GETSHARED_GCTHREADSTATIC_BASE_DYNAMICCLASS:
case CORINFO_HELP_GETSHARED_NONGCTHREADSTATIC_BASE_DYNAMICCLASS:
case CORINFO_HELP_GETSTATICFIELDADDR_CONTEXT:
case CORINFO_HELP_GETSTATICFIELDADDR_TLS:
case CORINFO_HELP_GETGENERICS_GCSTATIC_BASE:
case CORINFO_HELP_GETGENERICS_NONGCSTATIC_BASE:
case CORINFO_HELP_READYTORUN_STATIC_BASE:
case CORINFO_HELP_READYTORUN_GENERIC_STATIC_BASE:
// These may invoke static class constructors
// These can throw InvalidProgram exception if the class can not be constructed
//
isPure = true;
nonNullReturn = true;
mayRunCctor = true;
break;
case CORINFO_HELP_GETSHARED_GCSTATIC_BASE_NOCTOR:
case CORINFO_HELP_GETSHARED_NONGCSTATIC_BASE_NOCTOR:
case CORINFO_HELP_GETSHARED_GCTHREADSTATIC_BASE_NOCTOR:
case CORINFO_HELP_GETSHARED_NONGCTHREADSTATIC_BASE_NOCTOR:
// These do not invoke static class constructors
//
isPure = true;
noThrow = true;
nonNullReturn = true;
break;
// GC Write barrier support
// TODO-ARM64-Bug?: Can these throw or not?
case CORINFO_HELP_ASSIGN_REF:
case CORINFO_HELP_CHECKED_ASSIGN_REF:
case CORINFO_HELP_ASSIGN_REF_ENSURE_NONHEAP:
case CORINFO_HELP_ASSIGN_BYREF:
case CORINFO_HELP_ASSIGN_STRUCT:
mutatesHeap = true;
break;
// Accessing fields (write)
case CORINFO_HELP_SETFIELD32:
case CORINFO_HELP_SETFIELD64:
case CORINFO_HELP_SETFIELDOBJ:
case CORINFO_HELP_SETFIELDSTRUCT:
case CORINFO_HELP_SETFIELDFLOAT:
case CORINFO_HELP_SETFIELDDOUBLE:
case CORINFO_HELP_ARRADDR_ST:
mutatesHeap = true;
break;
// These helper calls always throw an exception
case CORINFO_HELP_OVERFLOW:
case CORINFO_HELP_VERIFICATION:
case CORINFO_HELP_RNGCHKFAIL:
case CORINFO_HELP_THROWDIVZERO:
case CORINFO_HELP_THROWNULLREF:
case CORINFO_HELP_THROW:
case CORINFO_HELP_RETHROW:
case CORINFO_HELP_THROW_ARGUMENTEXCEPTION:
case CORINFO_HELP_THROW_ARGUMENTOUTOFRANGEEXCEPTION:
case CORINFO_HELP_THROW_NOT_IMPLEMENTED:
case CORINFO_HELP_THROW_PLATFORM_NOT_SUPPORTED:
case CORINFO_HELP_THROW_TYPE_NOT_SUPPORTED:
break;
// These helper calls may throw an exception
case CORINFO_HELP_METHOD_ACCESS_CHECK:
case CORINFO_HELP_FIELD_ACCESS_CHECK:
case CORINFO_HELP_CLASS_ACCESS_CHECK:
case CORINFO_HELP_DELEGATE_SECURITY_CHECK:
case CORINFO_HELP_MON_EXIT_STATIC:
break;
// This is a debugging aid; it simply returns a constant address.
case CORINFO_HELP_LOOP_CLONE_CHOICE_ADDR:
isPure = true;
noThrow = true;
break;
case CORINFO_HELP_DBG_IS_JUST_MY_CODE:
case CORINFO_HELP_BBT_FCN_ENTER:
case CORINFO_HELP_POLL_GC:
case CORINFO_HELP_MON_ENTER:
case CORINFO_HELP_MON_EXIT:
case CORINFO_HELP_MON_ENTER_STATIC:
case CORINFO_HELP_JIT_REVERSE_PINVOKE_ENTER:
case CORINFO_HELP_JIT_REVERSE_PINVOKE_EXIT:
case CORINFO_HELP_SECURITY_PROLOG:
case CORINFO_HELP_SECURITY_PROLOG_FRAMED:
case CORINFO_HELP_VERIFICATION_RUNTIME_CHECK:
case CORINFO_HELP_GETFIELDADDR:
case CORINFO_HELP_INIT_PINVOKE_FRAME:
case CORINFO_HELP_JIT_PINVOKE_BEGIN:
case CORINFO_HELP_JIT_PINVOKE_END:
case CORINFO_HELP_GETCURRENTMANAGEDTHREADID:
noThrow = true;
break;
// Not sure how to handle optimization involving the rest of these helpers
default:
// The most pessimistic results are returned for these helpers
mutatesHeap = true;
break;
}
m_isPure[helper] = isPure;
m_noThrow[helper] = noThrow;
m_nonNullReturn[helper] = nonNullReturn;
m_isAllocator[helper] = isAllocator;
m_mutatesHeap[helper] = mutatesHeap;
m_mayRunCctor[helper] = mayRunCctor;
m_mayFinalize[helper] = mayFinalize;
}
}
//=============================================================================
// AssemblyNamesList2
//=============================================================================
// The string should be of the form
// MyAssembly
// MyAssembly;mscorlib;System
// MyAssembly;mscorlib System
AssemblyNamesList2::AssemblyNamesList2(const wchar_t* list, HostAllocator alloc) : m_alloc(alloc)
{
WCHAR prevChar = '?'; // dummy
LPWSTR nameStart = nullptr; // start of the name currently being processed. nullptr if no current name
AssemblyName** ppPrevLink = &m_pNames;
for (LPWSTR listWalk = const_cast<LPWSTR>(list); prevChar != '\0'; prevChar = *listWalk, listWalk++)
{
WCHAR curChar = *listWalk;
if (iswspace(curChar) || curChar == W(';') || curChar == W('\0'))
{
//
// Found white-space
//
if (nameStart)
{
// Found the end of the current name; add a new assembly name to the list.
AssemblyName* newName = new (m_alloc) AssemblyName();
// Null out the current character so we can do zero-terminated string work; we'll restore it later.
*listWalk = W('\0');
// How much space do we need?
int convertedNameLenBytes =
WszWideCharToMultiByte(CP_UTF8, 0, nameStart, -1, nullptr, 0, nullptr, nullptr);
newName->m_assemblyName = new (m_alloc) char[convertedNameLenBytes]; // convertedNameLenBytes includes
// the trailing null character
if (WszWideCharToMultiByte(CP_UTF8, 0, nameStart, -1, newName->m_assemblyName, convertedNameLenBytes,
nullptr, nullptr) != 0)
{
*ppPrevLink = newName;
ppPrevLink = &newName->m_next;
}
else
{
// Failed to convert the string. Ignore this string (and leak the memory).
}
nameStart = nullptr;
// Restore the current character.
*listWalk = curChar;
}
}
else if (!nameStart)
{
//
// Found the start of a new name
//
nameStart = listWalk;
}
}
assert(nameStart == nullptr); // cannot be in the middle of a name
*ppPrevLink = nullptr; // Terminate the last element of the list.
}
AssemblyNamesList2::~AssemblyNamesList2()
{
for (AssemblyName* pName = m_pNames; pName != nullptr; /**/)
{
AssemblyName* cur = pName;
pName = pName->m_next;
m_alloc.deallocate(cur->m_assemblyName);
m_alloc.deallocate(cur);
}
}
bool AssemblyNamesList2::IsInList(const char* assemblyName)
{
for (AssemblyName* pName = m_pNames; pName != nullptr; pName = pName->m_next)
{
if (_stricmp(pName->m_assemblyName, assemblyName) == 0)
{
return true;
}
}
return false;
}
#ifdef FEATURE_JIT_METHOD_PERF
CycleCount::CycleCount() : cps(CycleTimer::CyclesPerSecond())
{
}
bool CycleCount::GetCycles(unsigned __int64* time)
{
return CycleTimer::GetThreadCyclesS(time);
}
bool CycleCount::Start()
{
return GetCycles(&beginCycles);
}
double CycleCount::ElapsedTime()
{
unsigned __int64 nowCycles;
(void)GetCycles(&nowCycles);
return ((double)(nowCycles - beginCycles) / cps) * 1000.0;
}
bool PerfCounter::Start()
{
bool result = QueryPerformanceFrequency(&beg) != 0;
if (!result)
{
return result;
}
freq = (double)beg.QuadPart / 1000.0;
(void)QueryPerformanceCounter(&beg);
return result;
}
// Return elapsed time from Start() in millis.
double PerfCounter::ElapsedTime()
{
LARGE_INTEGER li;
(void)QueryPerformanceCounter(&li);
return (double)(li.QuadPart - beg.QuadPart) / freq;
}
#endif
#ifdef DEBUG
/*****************************************************************************
* Return the number of digits in a number of the given base (default base 10).
* Used when outputting strings.
*/
unsigned CountDigits(unsigned num, unsigned base /* = 10 */)
{
assert(2 <= base && base <= 16); // sanity check
unsigned count = 1;
while (num >= base)
{
num /= base;
++count;
}
return count;
}
#endif // DEBUG
double FloatingPointUtils::convertUInt64ToDouble(unsigned __int64 uIntVal)
{
__int64 s64 = uIntVal;
double d;
if (s64 < 0)
{
#if defined(_TARGET_XARCH_)
// RyuJIT codegen and clang (or gcc) may produce different results for casting uint64 to
// double, and the clang result is more accurate. For example,
// 1) (double)0x84595161401484A0UL --> 43e08b2a2c280290 (RyuJIT codegen or VC++)
// 2) (double)0x84595161401484A0UL --> 43e08b2a2c280291 (clang or gcc)
// If the folding optimization below is implemented by simple casting of (double)uint64_val
// and it is compiled by clang, casting result can be inconsistent, depending on whether
// the folding optimization is triggered or the codegen generates instructions for casting. //
// The current solution is to force the same math as the codegen does, so that casting
// result is always consistent.
// d = (double)(int64_t)uint64 + 0x1p64
uint64_t adjHex = 0x43F0000000000000UL;
d = (double)s64 + *(double*)&adjHex;
#else
d = (double)uIntVal;
#endif
}
else
{
d = (double)uIntVal;
}
return d;
}
float FloatingPointUtils::convertUInt64ToFloat(unsigned __int64 u64)
{
double d = convertUInt64ToDouble(u64);
return (float)d;
}
unsigned __int64 FloatingPointUtils::convertDoubleToUInt64(double d)
{
unsigned __int64 u64;
if (d >= 0.0)
{
// Work around a C++ issue where it doesn't properly convert large positive doubles
const double two63 = 2147483648.0 * 4294967296.0;
if (d < two63)
{
u64 = UINT64(d);
}
else
{
// subtract 0x8000000000000000, do the convert then add it back again
u64 = INT64(d - two63) + I64(0x8000000000000000);
}
return u64;
}
#ifdef _TARGET_XARCH_
// While the Ecma spec does not specifically call this out,
// the case of conversion from negative double to unsigned integer is
// effectively an overflow and therefore the result is unspecified.
// With MSVC for x86/x64, such a conversion results in the bit-equivalent
// unsigned value of the conversion to integer. Other compilers convert
// negative doubles to zero when the target is unsigned.
// To make the behavior consistent across OS's on TARGET_XARCH,
// this double cast is needed to conform MSVC behavior.
u64 = UINT64(INT64(d));
#else
u64 = UINT64(d);
#endif // _TARGET_XARCH_
return u64;
}
// Rounds a double-precision floating-point value to the nearest integer,
// and rounds midpoint values to the nearest even number.
double FloatingPointUtils::round(double x)
{
// ************************************************************************************
// IMPORTANT: Do not change this implementation without also updating Math.Round(double),
// MathF.Round(float), and FloatingPointUtils::round(float)
// ************************************************************************************
// If the number has no fractional part do nothing
// This shortcut is necessary to workaround precision loss in borderline cases on some platforms
if (x == (double)((INT64)x))
{
return x;
}
// We had a number that was equally close to 2 integers.
// We need to return the even one.
double flrTempVal = floor(x + 0.5);
if ((x == (floor(x) + 0.5)) && (fmod(flrTempVal, 2.0) != 0))
{
flrTempVal -= 1.0;
}
return _copysign(flrTempVal, x);
}
// Windows x86 and Windows ARM/ARM64 may not define _copysignf() but they do define _copysign().
// We will redirect the macro to this other functions if the macro is not defined for the platform.
// This has the side effect of a possible implicit upcasting for arguments passed in and an explicit
// downcasting for the _copysign() call.
#if (defined(_TARGET_X86_) || defined(_TARGET_ARM_) || defined(_TARGET_ARM64_)) && !defined(FEATURE_PAL)
#if !defined(_copysignf)
#define _copysignf (float)_copysign
#endif
#endif
// Rounds a single-precision floating-point value to the nearest integer,
// and rounds midpoint values to the nearest even number.
float FloatingPointUtils::round(float x)
{
// ************************************************************************************
// IMPORTANT: Do not change this implementation without also updating MathF.Round(float),
// Math.Round(double), and FloatingPointUtils::round(double)
// ************************************************************************************
// If the number has no fractional part do nothing
// This shortcut is necessary to workaround precision loss in borderline cases on some platforms
if (x == (float)((INT32)x))
{
return x;
}
// We had a number that was equally close to 2 integers.
// We need to return the even one.
float flrTempVal = floorf(x + 0.5f);
if ((x == (floorf(x) + 0.5f)) && (fmodf(flrTempVal, 2.0f) != 0))
{
flrTempVal -= 1.0f;
}
return _copysignf(flrTempVal, x);
}
namespace MagicDivide
{
template <int TableBase = 0, int TableSize, typename Magic>
static const Magic* TryGetMagic(const Magic (&table)[TableSize], typename Magic::DivisorType index)
{
if ((index < TableBase) || (TableBase + TableSize <= index))
{
return nullptr;
}
const Magic* p = &table[index - TableBase];
if (p->magic == 0)
{
return nullptr;
}
return p;
};
template <typename T>
struct UnsignedMagic
{
typedef T DivisorType;
T magic;
bool add;
int shift;
};
template <typename T>
const UnsignedMagic<T>* TryGetUnsignedMagic(T divisor)
{
return nullptr;
}
template <>
const UnsignedMagic<uint32_t>* TryGetUnsignedMagic(uint32_t divisor)
{
static const UnsignedMagic<uint32_t> table[]{
{0xaaaaaaab, false, 1}, // 3
{},
{0xcccccccd, false, 2}, // 5
{0xaaaaaaab, false, 2}, // 6
{0x24924925, true, 3}, // 7
{},
{0x38e38e39, false, 1}, // 9
{0xcccccccd, false, 3}, // 10
{0xba2e8ba3, false, 3}, // 11
{0xaaaaaaab, false, 3}, // 12
};
return TryGetMagic<3>(table, divisor);
}
template <>
const UnsignedMagic<uint64_t>* TryGetUnsignedMagic(uint64_t divisor)
{
static const UnsignedMagic<uint64_t> table[]{
{0xaaaaaaaaaaaaaaab, false, 1}, // 3
{},
{0xcccccccccccccccd, false, 2}, // 5
{0xaaaaaaaaaaaaaaab, false, 2}, // 6
{0x2492492492492493, true, 3}, // 7
{},
{0xe38e38e38e38e38f, false, 3}, // 9
{0xcccccccccccccccd, false, 3}, // 10
{0x2e8ba2e8ba2e8ba3, false, 1}, // 11
{0xaaaaaaaaaaaaaaab, false, 3}, // 12
};
return TryGetMagic<3>(table, divisor);
}
//------------------------------------------------------------------------
// GetUnsignedMagic: Generates a magic number and shift amount for the magic
// number unsigned division optimization.
//
// Arguments:
// d - The divisor
// add - Pointer to a flag indicating the kind of code to generate
// shift - Pointer to the shift value to be returned
//
// Returns:
// The magic number.
//
// Notes:
// This code is adapted from _The_PowerPC_Compiler_Writer's_Guide_, pages 57-58.
// The paper is based on "Division by invariant integers using multiplication"
// by Torbjorn Granlund and Peter L. Montgomery in PLDI 94
template <typename T>
T GetUnsignedMagic(T d, bool* add /*out*/, int* shift /*out*/)
{
assert((d >= 3) && !isPow2(d));
const UnsignedMagic<T>* magic = TryGetUnsignedMagic(d);
if (magic != nullptr)
{
*shift = magic->shift;
*add = magic->add;
return magic->magic;
}
typedef typename jitstd::make_signed<T>::type ST;
const unsigned bits = sizeof(T) * 8;
const unsigned bitsMinus1 = bits - 1;
const T twoNMinus1 = T(1) << bitsMinus1;
*add = false;
const T nc = -ST(1) - -ST(d) % ST(d);
unsigned p = bitsMinus1;
T q1 = twoNMinus1 / nc;
T r1 = twoNMinus1 - (q1 * nc);
T q2 = (twoNMinus1 - 1) / d;
T r2 = (twoNMinus1 - 1) - (q2 * d);
T delta;
do
{
p++;
if (r1 >= (nc - r1))
{
q1 = 2 * q1 + 1;
r1 = 2 * r1 - nc;
}
else
{
q1 = 2 * q1;
r1 = 2 * r1;
}
if ((r2 + 1) >= (d - r2))
{
if (q2 >= (twoNMinus1 - 1))
{
*add = true;
}
q2 = 2 * q2 + 1;
r2 = 2 * r2 + 1 - d;
}
else
{
if (q2 >= twoNMinus1)
{
*add = true;
}
q2 = 2 * q2;
r2 = 2 * r2 + 1;
}
delta = d - 1 - r2;
} while ((p < (bits * 2)) && ((q1 < delta) || ((q1 == delta) && (r1 == 0))));
*shift = p - bits; // resulting shift
return q2 + 1; // resulting magic number
}
uint32_t GetUnsigned32Magic(uint32_t d, bool* add /*out*/, int* shift /*out*/)
{
return GetUnsignedMagic<uint32_t>(d, add, shift);
}
#ifdef _TARGET_64BIT_
uint64_t GetUnsigned64Magic(uint64_t d, bool* add /*out*/, int* shift /*out*/)
{
return GetUnsignedMagic<uint64_t>(d, add, shift);
}
#endif
template <typename T>
struct SignedMagic
{
typedef T DivisorType;
T magic;
int shift;
};
template <typename T>
const SignedMagic<T>* TryGetSignedMagic(T divisor)
{
return nullptr;
}
template <>
const SignedMagic<int32_t>* TryGetSignedMagic(int32_t divisor)
{
static const SignedMagic<int32_t> table[]{
{0x55555556, 0}, // 3
{},
{0x66666667, 1}, // 5
{0x2aaaaaab, 0}, // 6
{0x92492493, 2}, // 7
{},
{0x38e38e39, 1}, // 9
{0x66666667, 2}, // 10
{0x2e8ba2e9, 1}, // 11
{0x2aaaaaab, 1}, // 12
};
return TryGetMagic<3>(table, divisor);
}
template <>
const SignedMagic<int64_t>* TryGetSignedMagic(int64_t divisor)
{
static const SignedMagic<int64_t> table[]{
{0x5555555555555556, 0}, // 3
{},
{0x6666666666666667, 1}, // 5
{0x2aaaaaaaaaaaaaab, 0}, // 6
{0x4924924924924925, 1}, // 7
{},
{0x1c71c71c71c71c72, 0}, // 9
{0x6666666666666667, 2}, // 10
{0x2e8ba2e8ba2e8ba3, 1}, // 11
{0x2aaaaaaaaaaaaaab, 1}, // 12
};
return TryGetMagic<3>(table, divisor);
}
//------------------------------------------------------------------------
// GetSignedMagic: Generates a magic number and shift amount for
// the magic number division optimization.
//
// Arguments:
// denom - The denominator
// shift - Pointer to the shift value to be returned
//
// Returns:
// The magic number.
//
// Notes:
// This code is previously from UTC where it notes it was taken from
// _The_PowerPC_Compiler_Writer's_Guide_, pages 57-58. The paper is based on
// is "Division by invariant integers using multiplication" by Torbjorn Granlund
// and Peter L. Montgomery in PLDI 94
template <typename T>
T GetSignedMagic(T denom, int* shift /*out*/)
{
const SignedMagic<T>* magic = TryGetSignedMagic(denom);
if (magic != nullptr)
{
*shift = magic->shift;
return magic->magic;
}
const int bits = sizeof(T) * 8;
const int bits_minus_1 = bits - 1;
typedef typename jitstd::make_unsigned<T>::type UT;
const UT two_nminus1 = UT(1) << bits_minus_1;
int p;
UT absDenom;
UT absNc;
UT delta;
UT q1;
UT r1;
UT r2;
UT q2;
UT t;
T result_magic;
int iters = 0;
absDenom = abs(denom);
t = two_nminus1 + ((unsigned int)denom >> 31);
absNc = t - 1 - (t % absDenom); // absolute value of nc
p = bits_minus_1; // initialize p
q1 = two_nminus1 / absNc; // initialize q1 = 2^p / abs(nc)
r1 = two_nminus1 - (q1 * absNc); // initialize r1 = rem(2^p, abs(nc))
q2 = two_nminus1 / absDenom; // initialize q1 = 2^p / abs(denom)
r2 = two_nminus1 - (q2 * absDenom); // initialize r1 = rem(2^p, abs(denom))
do
{
iters++;
p++;
q1 *= 2; // update q1 = 2^p / abs(nc)
r1 *= 2; // update r1 = rem(2^p / abs(nc))
if (r1 >= absNc)
{ // must be unsigned comparison
q1++;
r1 -= absNc;
}
q2 *= 2; // update q2 = 2^p / abs(denom)
r2 *= 2; // update r2 = rem(2^p / abs(denom))
if (r2 >= absDenom)
{ // must be unsigned comparison
q2++;
r2 -= absDenom;
}
delta = absDenom - r2;
} while (q1 < delta || (q1 == delta && r1 == 0));
result_magic = q2 + 1; // resulting magic number
if (denom < 0)
{
result_magic = -result_magic;
}
*shift = p - bits; // resulting shift
return result_magic;
}
int32_t GetSigned32Magic(int32_t d, int* shift /*out*/)
{
return GetSignedMagic<int32_t>(d, shift);
}
#ifdef _TARGET_64BIT_
int64_t GetSigned64Magic(int64_t d, int* shift /*out*/)
{
return GetSignedMagic<int64_t>(d, shift);
}
#endif
}
|