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
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
|
/**
* \file
* transform CIL into different opcodes for more
* efficient interpretation
*
* Written by Bernie Solomon (bernard@ugsolutions.com)
* Copyright (c) 2004.
*/
#include "config.h"
#include <string.h>
#include <mono/metadata/appdomain.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/exception.h>
#include <mono/metadata/mono-endian.h>
#include <mono/metadata/marshal.h>
#include <mono/metadata/profiler-private.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/seq-points-data.h>
#include <mono/metadata/mono-basic-block.h>
#include <mono/mini/mini.h>
#include <mono/mini/mini-runtime.h>
#include "mintops.h"
#include "interp-internals.h"
#include "interp.h"
#define DEBUG 0
typedef struct
{
MonoClass *klass;
unsigned char type;
unsigned char flags;
} StackInfo;
typedef struct {
guint8 *ip;
GSList *preds;
GSList *seq_points;
SeqPoint *last_seq_point;
// This will hold a list of last sequence points of incoming basic blocks
SeqPoint **pred_seq_points;
guint num_pred_seq_points;
} InterpBasicBlock;
typedef enum {
RELOC_SHORT_BRANCH,
RELOC_LONG_BRANCH,
RELOC_SWITCH
} RelocType;
typedef struct {
RelocType type;
/* In the interpreter IR */
int offset;
/* In the IL code */
int target;
} Reloc;
typedef struct
{
MonoMethod *method;
MonoMethodHeader *header;
InterpMethod *rtm;
const unsigned char *il_code;
const unsigned char *ip;
const unsigned char *last_ip;
const unsigned char *in_start;
int code_size;
int *in_offsets;
StackInfo **stack_state;
int *stack_height;
int *vt_stack_size;
unsigned char *is_bb_start;
unsigned short *new_code;
unsigned short *new_code_end;
unsigned short *new_ip;
unsigned short *last_new_ip;
unsigned int max_code_size;
StackInfo *stack;
StackInfo *sp;
unsigned int max_stack_height;
unsigned int stack_capacity;
unsigned int vt_sp;
unsigned int max_vt_sp;
unsigned int total_locals_size;
int n_data_items;
int max_data_items;
void **data_items;
GHashTable *data_hash;
int *clause_indexes;
gboolean gen_sdb_seq_points;
GPtrArray *seq_points;
InterpBasicBlock **offset_to_bb;
InterpBasicBlock *entry_bb;
MonoMemPool *mempool;
GList *basic_blocks;
GPtrArray *relocs;
gboolean verbose_level;
} TransformData;
#define STACK_TYPE_I4 0
#define STACK_TYPE_I8 1
#define STACK_TYPE_R4 2
#define STACK_TYPE_R8 3
#define STACK_TYPE_O 4
#define STACK_TYPE_VT 5
#define STACK_TYPE_MP 6
#define STACK_TYPE_F 7
static const char *stack_type_string [] = { "I4", "I8", "R4", "R8", "O ", "VT", "MP", "F " };
#if SIZEOF_VOID_P == 8
#define STACK_TYPE_I STACK_TYPE_I8
#else
#define STACK_TYPE_I STACK_TYPE_I4
#endif
static int stack_type [] = {
STACK_TYPE_I4, /*I1*/
STACK_TYPE_I4, /*U1*/
STACK_TYPE_I4, /*I2*/
STACK_TYPE_I4, /*U2*/
STACK_TYPE_I4, /*I4*/
STACK_TYPE_I8, /*I8*/
STACK_TYPE_R4, /*R4*/
STACK_TYPE_R8, /*R8*/
STACK_TYPE_O, /*O*/
STACK_TYPE_MP, /*P*/
STACK_TYPE_VT
};
#if SIZEOF_VOID_P == 8
#define MINT_NEG_P MINT_NEG_I8
#define MINT_NOT_P MINT_NOT_I8
#define MINT_NEG_FP MINT_NEG_R8
#define MINT_ADD_P MINT_ADD_I8
#define MINT_SUB_P MINT_SUB_I8
#define MINT_MUL_P MINT_MUL_I8
#define MINT_DIV_P MINT_DIV_I8
#define MINT_DIV_UN_P MINT_DIV_UN_I8
#define MINT_REM_P MINT_REM_I8
#define MINT_REM_UN_P MINT_REM_UN_I8
#define MINT_AND_P MINT_AND_I8
#define MINT_OR_P MINT_OR_I8
#define MINT_XOR_P MINT_XOR_I8
#define MINT_SHL_P MINT_SHL_I8
#define MINT_SHR_P MINT_SHR_I8
#define MINT_SHR_UN_P MINT_SHR_UN_I8
#define MINT_CEQ_P MINT_CEQ_I8
#define MINT_CNE_P MINT_CNE_I8
#define MINT_CLT_P MINT_CLT_I8
#define MINT_CLT_UN_P MINT_CLT_UN_I8
#define MINT_CGT_P MINT_CGT_I8
#define MINT_CGT_UN_P MINT_CGT_UN_I8
#define MINT_CLE_P MINT_CLE_I8
#define MINT_CLE_UN_P MINT_CLE_UN_I8
#define MINT_CGE_P MINT_CGE_I8
#define MINT_CGE_UN_P MINT_CGE_UN_I8
#define MINT_ADD_FP MINT_ADD_R8
#define MINT_SUB_FP MINT_SUB_R8
#define MINT_MUL_FP MINT_MUL_R8
#define MINT_DIV_FP MINT_DIV_R8
#define MINT_REM_FP MINT_REM_R8
#define MINT_CNE_FP MINT_CNE_R8
#define MINT_CEQ_FP MINT_CEQ_R8
#define MINT_CGT_FP MINT_CGT_R8
#define MINT_CGE_FP MINT_CGE_R8
#define MINT_CLT_FP MINT_CLT_R8
#define MINT_CLE_FP MINT_CLE_R8
#else
#define MINT_NEG_P MINT_NEG_I4
#define MINT_NOT_P MINT_NOT_I4
#define MINT_NEG_FP MINT_NEG_R4
#define MINT_ADD_P MINT_ADD_I4
#define MINT_SUB_P MINT_SUB_I4
#define MINT_MUL_P MINT_MUL_I4
#define MINT_DIV_P MINT_DIV_I4
#define MINT_DIV_UN_P MINT_DIV_UN_I4
#define MINT_REM_P MINT_REM_I4
#define MINT_REM_UN_P MINT_REM_UN_I4
#define MINT_AND_P MINT_AND_I4
#define MINT_OR_P MINT_OR_I4
#define MINT_XOR_P MINT_XOR_I4
#define MINT_SHL_P MINT_SHL_I4
#define MINT_SHR_P MINT_SHR_I4
#define MINT_SHR_UN_P MINT_SHR_UN_I4
#define MINT_CEQ_P MINT_CEQ_I4
#define MINT_CNE_P MINT_CNE_I4
#define MINT_CLT_P MINT_CLT_I4
#define MINT_CLT_UN_P MINT_CLT_UN_I4
#define MINT_CGT_P MINT_CGT_I4
#define MINT_CGT_UN_P MINT_CGT_UN_I4
#define MINT_CLE_P MINT_CLE_I4
#define MINT_CLE_UN_P MINT_CLE_UN_I4
#define MINT_CGE_P MINT_CGE_I4
#define MINT_CGE_UN_P MINT_CGE_UN_I4
#define MINT_ADD_FP MINT_ADD_R4
#define MINT_SUB_FP MINT_SUB_R4
#define MINT_MUL_FP MINT_MUL_R4
#define MINT_DIV_FP MINT_DIV_R4
#define MINT_REM_FP MINT_REM_R4
#define MINT_CNE_FP MINT_CNE_R4
#define MINT_CEQ_FP MINT_CEQ_R4
#define MINT_CGT_FP MINT_CGT_R4
#define MINT_CGE_FP MINT_CGE_R4
#define MINT_CLT_FP MINT_CLT_R4
#define MINT_CLE_FP MINT_CLE_R4
#endif
typedef struct {
const gchar *op_name;
guint16 insn [3];
} MagicIntrinsic;
// static const MagicIntrinsic int_binop[] = {
static const MagicIntrinsic int_unnop[] = {
{ "op_UnaryPlus", {MINT_NOP, MINT_NOP, MINT_NOP}},
{ "op_UnaryNegation", {MINT_NEG_P, MINT_NEG_P, MINT_NEG_FP}},
{ "op_OnesComplement", {MINT_NOT_P, MINT_NOT_P, MINT_NIY}}
};
static const MagicIntrinsic int_binop[] = {
{ "op_Addition", {MINT_ADD_P, MINT_ADD_P, MINT_ADD_FP}},
{ "op_Subtraction", {MINT_SUB_P, MINT_SUB_P, MINT_SUB_FP}},
{ "op_Multiply", {MINT_MUL_P, MINT_MUL_P, MINT_MUL_FP}},
{ "op_Division", {MINT_DIV_P, MINT_DIV_UN_P, MINT_DIV_FP}},
{ "op_Modulus", {MINT_REM_P, MINT_REM_UN_P, MINT_REM_FP}},
{ "op_BitwiseAnd", {MINT_AND_P, MINT_AND_P, MINT_NIY}},
{ "op_BitwiseOr", {MINT_OR_P, MINT_OR_P, MINT_NIY}},
{ "op_ExclusiveOr", {MINT_XOR_P, MINT_XOR_P, MINT_NIY}},
{ "op_LeftShift", {MINT_SHL_P, MINT_SHL_P, MINT_NIY}},
{ "op_RightShift", {MINT_SHR_P, MINT_SHR_UN_P, MINT_NIY}},
};
static const MagicIntrinsic int_cmpop[] = {
{ "op_Inequality", {MINT_CNE_P, MINT_CNE_P, MINT_CNE_FP}},
{ "op_Equality", {MINT_CEQ_P, MINT_CEQ_P, MINT_CEQ_FP}},
{ "op_GreaterThan", {MINT_CGT_P, MINT_CGT_UN_P, MINT_CGT_FP}},
{ "op_GreaterThanOrEqual", {MINT_CGE_P, MINT_CGE_UN_P, MINT_CGE_FP}},
{ "op_LessThan", {MINT_CLT_P, MINT_CLT_UN_P, MINT_CLT_FP}},
{ "op_LessThanOrEqual", {MINT_CLE_P, MINT_CLE_UN_P, MINT_CLE_FP}}
};
static void
grow_code (TransformData *td)
{
unsigned int old_ip_offset = td->new_ip - td->new_code;
unsigned int old_last_ip_offset = td->last_new_ip - td->new_code;
g_assert (old_ip_offset <= td->max_code_size);
td->new_code = g_realloc (td->new_code, (td->max_code_size *= 2) * sizeof (td->new_code [0]));
td->new_code_end = td->new_code + td->max_code_size;
td->new_ip = td->new_code + old_ip_offset;
td->last_new_ip = td->new_code + old_last_ip_offset;
}
#define ENSURE_CODE(td, n) \
do { \
if ((td)->new_ip + (n) > (td)->new_code_end) \
grow_code (td); \
} while (0)
#define ADD_CODE(td, n) \
do { \
if ((td)->new_ip == (td)->new_code_end) \
grow_code (td); \
*(td)->new_ip++ = (n); \
} while (0)
#define CHECK_STACK(td, n) \
do { \
int stack_size = (td)->sp - (td)->stack; \
if (stack_size < (n)) \
g_warning ("%s.%s: not enough values (%d < %d) on stack at %04x", \
m_class_get_name ((td)->method->klass), (td)->method->name, \
stack_size, n, (td)->ip - (td)->il_code); \
} while (0)
#define ENSURE_I4(td, sp_off) \
do { \
if ((td)->sp [-sp_off].type == STACK_TYPE_I8) \
ADD_CODE(td, sp_off == 1 ? MINT_CONV_I4_I8 : MINT_CONV_I4_I8_SP); \
} while (0)
#define CHECK_TYPELOAD(klass) \
do { \
if (!(klass) || mono_class_has_failure (klass)) { \
mono_error_set_for_class_failure (error, klass); \
goto exit; \
} \
} while (0)
static void
handle_branch (TransformData *td, int short_op, int long_op, int offset)
{
int shorten_branch = 0;
int target = td->ip + offset - td->il_code;
if (target < 0 || target >= td->code_size)
g_assert_not_reached ();
/* Add exception checkpoint for backward branches */
if (offset < 0)
ADD_CODE(td, MINT_CHECKPOINT);
if (offset > 0 && td->stack_height [target] < 0) {
td->stack_height [target] = td->sp - td->stack;
if (td->stack_height [target] > 0)
td->stack_state [target] = g_memdup (td->stack, td->stack_height [target] * sizeof (td->stack [0]));
td->vt_stack_size [target] = td->vt_sp;
}
if (offset < 0) {
offset = td->in_offsets [target] - (td->new_ip - td->new_code);
if (offset >= -32768) {
shorten_branch = 1;
}
} else {
if (td->header->code_size <= 25000) /* FIX to be precise somehow? */
shorten_branch = 1;
Reloc *reloc = mono_mempool_alloc0 (td->mempool, sizeof (Reloc));
if (shorten_branch) {
offset = 0xffff;
reloc->type = RELOC_SHORT_BRANCH;
} else {
offset = 0xdeadbeef;
reloc->type = RELOC_LONG_BRANCH;
}
reloc->offset = td->new_ip - td->new_code;
reloc->target = target;
g_ptr_array_add (td->relocs, reloc);
}
if (shorten_branch) {
ADD_CODE(td, short_op);
ADD_CODE(td, offset);
} else {
ADD_CODE(td, long_op);
ADD_CODE(td, * (unsigned short *)(&offset));
ADD_CODE(td, * ((unsigned short *)&offset + 1));
}
}
static void
one_arg_branch(TransformData *td, int mint_op, int offset)
{
int type = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
int long_op = mint_op + type - STACK_TYPE_I4;
int short_op = long_op + MINT_BRFALSE_I4_S - MINT_BRFALSE_I4;
CHECK_STACK(td, 1);
--td->sp;
handle_branch (td, short_op, long_op, offset);
}
static void
two_arg_branch(TransformData *td, int mint_op, int offset)
{
int type1 = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
int type2 = td->sp [-2].type == STACK_TYPE_O || td->sp [-2].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-2].type;
int long_op = mint_op + type1 - STACK_TYPE_I4;
int short_op = long_op + MINT_BEQ_I4_S - MINT_BEQ_I4;
CHECK_STACK(td, 2);
if (type1 == STACK_TYPE_I4 && type2 == STACK_TYPE_I8) {
ADD_CODE(td, MINT_CONV_I8_I4);
td->in_offsets [td->ip - td->il_code]++;
} else if (type1 == STACK_TYPE_I8 && type2 == STACK_TYPE_I4) {
ADD_CODE(td, MINT_CONV_I8_I4_SP);
td->in_offsets [td->ip - td->il_code]++;
} else if (type1 == STACK_TYPE_R4 && type2 == STACK_TYPE_R8) {
ADD_CODE (td, MINT_CONV_R8_R4);
td->in_offsets [td->ip - td->il_code]++;
} else if (type1 == STACK_TYPE_R8 && type2 == STACK_TYPE_R4) {
ADD_CODE (td, MINT_CONV_R8_R4_SP);
td->in_offsets [td->ip - td->il_code]++;
} else if (type1 != type2) {
g_warning("%s.%s: branch type mismatch %d %d",
m_class_get_name (td->method->klass), td->method->name,
td->sp [-1].type, td->sp [-2].type);
}
td->sp -= 2;
handle_branch (td, short_op, long_op, offset);
}
static void
unary_arith_op(TransformData *td, int mint_op)
{
int op = mint_op + td->sp [-1].type - STACK_TYPE_I4;
CHECK_STACK(td, 1);
ADD_CODE(td, op);
}
static void
binary_arith_op(TransformData *td, int mint_op)
{
int type1 = td->sp [-2].type;
int type2 = td->sp [-1].type;
int op;
#if SIZEOF_VOID_P == 8
if ((type1 == STACK_TYPE_MP || type1 == STACK_TYPE_I8) && type2 == STACK_TYPE_I4) {
ADD_CODE(td, MINT_CONV_I8_I4);
type2 = STACK_TYPE_I8;
}
if (type1 == STACK_TYPE_I4 && (type2 == STACK_TYPE_MP || type2 == STACK_TYPE_I8)) {
ADD_CODE(td, MINT_CONV_I8_I4_SP);
type1 = STACK_TYPE_I8;
td->sp [-2].type = STACK_TYPE_I8;
}
#endif
if (type1 == STACK_TYPE_R8 && type2 == STACK_TYPE_R4) {
ADD_CODE (td, MINT_CONV_R8_R4);
type2 = STACK_TYPE_R8;
}
if (type1 == STACK_TYPE_R4 && type2 == STACK_TYPE_R8) {
ADD_CODE (td, MINT_CONV_R8_R4_SP);
type1 = STACK_TYPE_R8;
td->sp [-2].type = STACK_TYPE_R8;
}
if (type1 == STACK_TYPE_MP)
type1 = STACK_TYPE_I;
if (type2 == STACK_TYPE_MP)
type2 = STACK_TYPE_I;
if (type1 != type2) {
g_warning("%s.%s: %04x arith type mismatch %s %d %d",
m_class_get_name (td->method->klass), td->method->name,
td->ip - td->il_code, mono_interp_opname[mint_op], type1, type2);
}
op = mint_op + type1 - STACK_TYPE_I4;
CHECK_STACK(td, 2);
ADD_CODE(td, op);
--td->sp;
}
static void
shift_op(TransformData *td, int mint_op)
{
int op = mint_op + td->sp [-2].type - STACK_TYPE_I4;
CHECK_STACK(td, 2);
if (td->sp [-1].type != STACK_TYPE_I4) {
g_warning("%s.%s: shift type mismatch %d",
m_class_get_name (td->method->klass), td->method->name,
td->sp [-2].type);
}
ADD_CODE(td, op);
--td->sp;
}
static int
can_store (int stack_type, int var_type)
{
if (stack_type == STACK_TYPE_O || stack_type == STACK_TYPE_MP)
stack_type = STACK_TYPE_I;
if (var_type == STACK_TYPE_O || var_type == STACK_TYPE_MP)
var_type = STACK_TYPE_I;
return stack_type == var_type;
}
#define SET_SIMPLE_TYPE(s, ty) \
do { \
(s)->type = (ty); \
(s)->flags = 0; \
(s)->klass = NULL; \
} while (0)
#define SET_TYPE(s, ty, k) \
do { \
(s)->type = (ty); \
(s)->flags = 0; \
(s)->klass = k; \
} while (0)
#define REALLOC_STACK(td, sppos) \
do { \
(td)->stack_capacity *= 2; \
(td)->stack = realloc ((td)->stack, (td)->stack_capacity * sizeof (td->stack [0])); \
(td)->sp = (td)->stack + sppos; \
} while (0);
#define PUSH_SIMPLE_TYPE(td, ty) \
do { \
int sp_height; \
(td)->sp++; \
sp_height = (td)->sp - (td)->stack; \
if (sp_height > (td)->max_stack_height) \
(td)->max_stack_height = sp_height; \
if (sp_height > (td)->stack_capacity) \
REALLOC_STACK(td, sp_height); \
SET_SIMPLE_TYPE((td)->sp - 1, ty); \
} while (0)
#define PUSH_TYPE(td, ty, k) \
do { \
int sp_height; \
(td)->sp++; \
sp_height = (td)->sp - (td)->stack; \
if (sp_height > (td)->max_stack_height) \
(td)->max_stack_height = sp_height; \
if (sp_height > (td)->stack_capacity) \
REALLOC_STACK(td, sp_height); \
SET_TYPE((td)->sp - 1, ty, k); \
} while (0)
#define PUSH_VT(td, size) \
do { \
(td)->vt_sp += ALIGN_TO ((size), MINT_VT_ALIGNMENT); \
if ((td)->vt_sp > (td)->max_vt_sp) \
(td)->max_vt_sp = (td)->vt_sp; \
} while (0)
#define POP_VT(td, size) \
do { \
(td)->vt_sp -= ALIGN_TO ((size), MINT_VT_ALIGNMENT); \
} while (0)
#if NO_UNALIGNED_ACCESS
#define WRITE32(td, v) \
do { \
ENSURE_CODE(td, 2); \
* (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
* ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
(td)->new_ip += 2; \
} while (0)
#define WRITE64(td, v) \
do { \
ENSURE_CODE(td, 4); \
* (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
* ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
* ((guint16 *)((td)->new_ip) + 2) = * ((guint16 *)(v) + 2); \
* ((guint16 *)((td)->new_ip) + 3) = * ((guint16 *)(v) + 3); \
(td)->new_ip += 4; \
} while (0)
#else
#define WRITE32(td, v) \
do { \
ENSURE_CODE(td, 2); \
* (guint32 *)((td)->new_ip) = * (guint32 *)(v); \
(td)->new_ip += 2; \
} while (0)
#define WRITE64(td, v) \
do { \
ENSURE_CODE(td, 4); \
* (guint64 *)((td)->new_ip) = * (guint64 *)(v); \
(td)->new_ip += 4; \
} while (0)
#endif
static void
load_arg(TransformData *td, int n)
{
int mt;
MonoClass *klass = NULL;
MonoType *type;
gboolean hasthis = mono_method_signature (td->method)->hasthis;
if (hasthis && n == 0)
type = m_class_get_byval_arg (td->method->klass);
else
type = mono_method_signature (td->method)->params [hasthis ? n - 1 : n];
mt = mint_type (type);
if (mt == MINT_TYPE_VT) {
gint32 size;
klass = mono_class_from_mono_type (type);
if (mono_method_signature (td->method)->pinvoke)
size = mono_class_native_size (klass, NULL);
else
size = mono_class_value_size (klass, NULL);
if (hasthis && n == 0) {
mt = MINT_TYPE_P;
ADD_CODE (td, MINT_LDARG_P);
ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
klass = NULL;
} else {
PUSH_VT (td, size);
ADD_CODE (td, MINT_LDARG_VT);
ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
WRITE32 (td, &size);
}
} else {
if (hasthis && n == 0) {
mt = MINT_TYPE_P;
ADD_CODE (td, MINT_LDARG_P);
ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
klass = NULL;
} else {
ADD_CODE(td, MINT_LDARG_I1 + (mt - MINT_TYPE_I1));
ADD_CODE(td, td->rtm->arg_offsets [n]); /* FIX for large offset */
if (mt == MINT_TYPE_O)
klass = mono_class_from_mono_type (type);
}
}
PUSH_TYPE(td, stack_type[mt], klass);
}
static void
store_arg(TransformData *td, int n)
{
int mt;
CHECK_STACK (td, 1);
MonoType *type;
gboolean hasthis = mono_method_signature (td->method)->hasthis;
if (hasthis && n == 0)
type = m_class_get_byval_arg (td->method->klass);
else
type = mono_method_signature (td->method)->params [n - !!hasthis];
mt = mint_type (type);
if (mt == MINT_TYPE_VT) {
gint32 size;
MonoClass *klass = mono_class_from_mono_type (type);
if (mono_method_signature (td->method)->pinvoke)
size = mono_class_native_size (klass, NULL);
else
size = mono_class_value_size (klass, NULL);
ADD_CODE(td, MINT_STARG_VT);
ADD_CODE(td, td->rtm->arg_offsets [n]);
WRITE32(td, &size);
if (td->sp [-1].type == STACK_TYPE_VT)
POP_VT(td, size);
} else {
ADD_CODE(td, MINT_STARG_I1 + (mt - MINT_TYPE_I1));
ADD_CODE(td, td->rtm->arg_offsets [n]);
}
--td->sp;
}
static void
store_inarg(TransformData *td, int n)
{
MonoType *type;
gboolean hasthis = mono_method_signature (td->method)->hasthis;
if (hasthis && n == 0)
type = m_class_get_byval_arg (td->method->klass);
else
type = mono_method_signature (td->method)->params [n - !!hasthis];
int mt = mint_type (type);
if (hasthis && n == 0) {
ADD_CODE (td, MINT_STINARG_P);
ADD_CODE (td, n);
return;
}
if (mt == MINT_TYPE_VT) {
MonoClass *klass = mono_class_from_mono_type (type);
gint32 size;
if (mono_method_signature (td->method)->pinvoke)
size = mono_class_native_size (klass, NULL);
else
size = mono_class_value_size (klass, NULL);
ADD_CODE(td, MINT_STINARG_VT);
ADD_CODE(td, n);
WRITE32(td, &size);
} else {
ADD_CODE(td, MINT_STINARG_I1 + (mt - MINT_TYPE_I1));
ADD_CODE(td, n);
}
}
static void
load_local(TransformData *td, int n)
{
MonoType *type = td->header->locals [n];
int mt = mint_type (type);
int offset = td->rtm->local_offsets [n];
MonoClass *klass = NULL;
if (mt == MINT_TYPE_VT) {
klass = mono_class_from_mono_type (type);
gint32 size = mono_class_value_size (klass, NULL);
PUSH_VT(td, size);
ADD_CODE(td, MINT_LDLOC_VT);
ADD_CODE(td, offset); /*FIX for large offset */
WRITE32(td, &size);
} else {
g_assert (mt < MINT_TYPE_VT);
if (!td->gen_sdb_seq_points &&
mt == MINT_TYPE_I4 && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
td->last_new_ip [0] == MINT_STLOC_I4 && td->last_new_ip [1] == offset) {
td->last_new_ip [0] = MINT_STLOC_NP_I4;
} else if (!td->gen_sdb_seq_points &&
mt == MINT_TYPE_O && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
td->last_new_ip [0] == MINT_STLOC_O && td->last_new_ip [1] == offset) {
td->last_new_ip [0] = MINT_STLOC_NP_O;
} else {
ADD_CODE(td, MINT_LDLOC_I1 + (mt - MINT_TYPE_I1));
ADD_CODE(td, offset); /*FIX for large offset */
}
if (mt == MINT_TYPE_O)
klass = mono_class_from_mono_type (type);
}
PUSH_TYPE(td, stack_type[mt], klass);
}
static void
store_local_general (TransformData *td, int offset, MonoType *type)
{
int mt = mint_type (type);
CHECK_STACK (td, 1);
#if SIZEOF_VOID_P == 8
if (td->sp [-1].type == STACK_TYPE_I4 && stack_type [mt] == STACK_TYPE_I8) {
ADD_CODE(td, MINT_CONV_I8_I4);
td->sp [-1].type = STACK_TYPE_I8;
}
#endif
if (!can_store(td->sp [-1].type, stack_type [mt])) {
g_warning("%s.%s: Store local stack type mismatch %d %d",
m_class_get_name (td->method->klass), td->method->name,
stack_type [mt], td->sp [-1].type);
}
if (mt == MINT_TYPE_VT) {
MonoClass *klass = mono_class_from_mono_type (type);
gint32 size = mono_class_value_size (klass, NULL);
ADD_CODE(td, MINT_STLOC_VT);
ADD_CODE(td, offset); /*FIX for large offset */
WRITE32(td, &size);
if (td->sp [-1].type == STACK_TYPE_VT)
POP_VT(td, size);
} else {
g_assert (mt < MINT_TYPE_VT);
ADD_CODE(td, MINT_STLOC_I1 + (mt - MINT_TYPE_I1));
ADD_CODE(td, offset); /*FIX for large offset */
}
--td->sp;
}
static void
store_local (TransformData *td, int n)
{
MonoType *type = td->header->locals [n];
int offset = td->rtm->local_offsets [n];
store_local_general (td, offset, type);
}
#define SIMPLE_OP(td, op) \
do { \
ADD_CODE(td, op); \
++td->ip; \
} while (0)
static guint16
get_data_item_index (TransformData *td, void *ptr)
{
gpointer p = g_hash_table_lookup (td->data_hash, ptr);
guint index;
if (p != NULL)
return GPOINTER_TO_UINT (p) - 1;
if (td->max_data_items == td->n_data_items) {
td->max_data_items = td->n_data_items == 0 ? 16 : 2 * td->max_data_items;
td->data_items = g_realloc (td->data_items, td->max_data_items * sizeof(td->data_items [0]));
}
index = td->n_data_items;
td->data_items [index] = ptr;
++td->n_data_items;
g_hash_table_insert (td->data_hash, ptr, GUINT_TO_POINTER (index + 1));
return index;
}
static gboolean
jit_call_supported (MonoMethod *method, MonoMethodSignature *sig)
{
GSList *l;
if (sig->param_count > 6)
return FALSE;
if (sig->pinvoke)
return FALSE;
if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
return FALSE;
if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
return FALSE;
if (method->is_inflated)
return FALSE;
if (method->string_ctor)
return FALSE;
if (mono_aot_only && m_class_get_image (method->klass)->aot_module) {
ERROR_DECL (error);
gpointer addr = mono_jit_compile_method_jit_only (method, error);
if (addr && mono_error_ok (error))
return TRUE;
}
for (l = mono_interp_jit_classes; l; l = l->next) {
char *class_name = l->data;
// FIXME: Namespaces
if (!strcmp (m_class_get_name (method->klass), class_name))
return TRUE;
}
//return TRUE;
return FALSE;
}
static int mono_class_get_magic_index (MonoClass *k)
{
if (mono_class_is_magic_int (k))
return !strcmp ("nint", m_class_get_name (k)) ? 0 : 1;
if (mono_class_is_magic_float (k))
return 2;
return -1;
}
static void
interp_generate_mae_throw (TransformData *td, MonoMethod *method, MonoMethod *target_method)
{
MonoJitICallInfo *info = mono_find_jit_icall_by_name ("mono_throw_method_access");
/* Inject code throwing MethodAccessException */
ADD_CODE (td, MINT_MONO_LDPTR);
ADD_CODE (td, get_data_item_index (td, method));
PUSH_SIMPLE_TYPE (td, STACK_TYPE_I);
ADD_CODE (td, MINT_MONO_LDPTR);
ADD_CODE (td, get_data_item_index (td, target_method));
PUSH_SIMPLE_TYPE (td, STACK_TYPE_I);
ADD_CODE (td, MINT_ICALL_PP_V);
ADD_CODE (td, get_data_item_index (td, (gpointer)info->func));
}
/*
* These are additional locals that can be allocated as we transform the code.
* They are allocated past the method locals so they are accessed in the same
* way, with an offset relative to the frame->locals.
*/
static int
create_interp_local (TransformData *td, MonoType *type)
{
int align, size;
int offset = td->total_locals_size;
size = mono_type_size (type, &align);
offset = ALIGN_TO (offset, align);
td->total_locals_size = offset + size;
return offset;
}
static void
dump_mint_code (TransformData *td)
{
const guint16 *p = td->new_code;
while (p < td->new_ip) {
char *ins = mono_interp_dis_mintop (td->new_code, p);
g_print ("%s\n", ins);
g_free (ins);
p = mono_interp_dis_mintop_len (p);
}
}
static MonoMethodHeader*
interp_method_get_header (MonoMethod* method, MonoError *error)
{
/* An explanation: mono_method_get_header_internal returns an error if
* called on a method with no body (e.g. an abstract method, or an
* icall). We don't want that.
*/
if (mono_method_has_no_body (method))
return NULL;
else
return mono_method_get_header_internal (method, error);
}
/* stores top of stack as local and pushes address of it on stack */
static void
emit_store_value_as_local (TransformData *td, MonoType *src)
{
int size = mini_magic_type_size (NULL, src);
int local_offset = create_interp_local (td, mini_native_type_replace_type (src));
store_local_general (td, local_offset, src);
size = ALIGN_TO (size, MINT_VT_ALIGNMENT);
ADD_CODE (td, MINT_LDLOC_VT);
ADD_CODE (td, local_offset);
WRITE32 (td, &size);
PUSH_VT (td, size);
PUSH_TYPE (td, STACK_TYPE_VT, NULL);
}
/* Return TRUE if call transformation is finished */
static gboolean
interp_handle_intrinsics (TransformData *td, MonoMethod *target_method, MonoMethodSignature *csignature, gboolean readonly, int *op)
{
const char *tm = target_method->name;
int i;
int type_index = mono_class_get_magic_index (target_method->klass);
gboolean in_corlib = m_class_get_image (target_method->klass) == mono_defaults.corlib;
const char *klass_name_space = m_class_get_name_space (target_method->klass);
const char *klass_name = m_class_get_name (target_method->klass);
if (target_method->klass == mono_defaults.string_class) {
if (tm [0] == 'g') {
if (strcmp (tm, "get_Chars") == 0)
*op = MINT_GETCHR;
else if (strcmp (tm, "get_Length") == 0)
*op = MINT_STRLEN;
}
} else if (type_index >= 0) {
MonoClass *magic_class = target_method->klass;
const int mt = mint_type (m_class_get_byval_arg (magic_class));
if (!strcmp (".ctor", tm)) {
MonoType *arg = csignature->params [0];
/* depending on SIZEOF_VOID_P and the type of the value passed to the .ctor we either have to CONV it, or do nothing */
int arg_size = mini_magic_type_size (NULL, arg);
if (arg_size > SIZEOF_VOID_P) { // 8 -> 4
switch (type_index) {
case 0: case 1:
ADD_CODE (td, MINT_CONV_I4_I8);
break;
case 2:
ADD_CODE (td, MINT_CONV_R4_R8);
break;
}
}
if (arg_size < SIZEOF_VOID_P) { // 4 -> 8
switch (type_index) {
case 0:
ADD_CODE (td, MINT_CONV_I8_I4);
break;
case 1:
ADD_CODE (td, MINT_CONV_I8_U4);
break;
case 2:
ADD_CODE (td, MINT_CONV_R8_R4);
break;
}
}
switch (type_index) {
case 0: case 1:
#if SIZEOF_VOID_P == 4
ADD_CODE (td, MINT_STIND_I4);
#else
ADD_CODE (td, MINT_STIND_I8);
#endif
break;
case 2:
#if SIZEOF_VOID_P == 4
ADD_CODE (td, MINT_STIND_R4);
#else
ADD_CODE (td, MINT_STIND_R8);
#endif
break;
}
td->sp -= 2;
td->ip += 5;
return TRUE;
} else if (!strcmp ("op_Implicit", tm ) || !strcmp ("op_Explicit", tm)) {
MonoType *src = csignature->params [0];
MonoType *dst = csignature->ret;
int src_size = mini_magic_type_size (NULL, src);
int dst_size = mini_magic_type_size (NULL, dst);
gboolean store_value_as_local = FALSE;
switch (type_index) {
case 0: case 1:
if (!mini_magic_is_int_type (src) || !mini_magic_is_int_type (dst)) {
if (mini_magic_is_int_type (src))
store_value_as_local = TRUE;
else
return FALSE;
}
break;
case 2:
if (!mini_magic_is_float_type (src) || !mini_magic_is_float_type (dst)) {
if (mini_magic_is_float_type (src))
store_value_as_local = TRUE;
else
return FALSE;
}
break;
}
if (store_value_as_local) {
emit_store_value_as_local (td, src);
/* emit call to managed conversion method */
return FALSE;
}
#if SIZEOF_VOID_P == 4
if (src_size > dst_size) { // 8 -> 4
switch (type_index) {
case 0: case 1:
ADD_CODE (td, MINT_CONV_I4_I8);
break;
case 2:
ADD_CODE (td, MINT_CONV_R4_R8);
break;
}
}
#endif
#if SIZEOF_VOID_P == 8
if (src_size < dst_size) { // 4 -> 8
switch (type_index) {
case 0:
ADD_CODE (td, MINT_CONV_I8_I4);
break;
case 1:
ADD_CODE (td, MINT_CONV_I8_U4);
break;
case 2:
ADD_CODE (td, MINT_CONV_R8_R4);
break;
}
}
#endif
SET_TYPE (td->sp - 1, stack_type [mt], magic_class);
td->ip += 5;
return TRUE;
} else if (!strcmp ("op_Increment", tm)) {
g_assert (type_index != 2); // no nfloat
#if SIZEOF_VOID_P == 8
ADD_CODE (td, MINT_ADD1_I8);
#else
ADD_CODE (td, MINT_ADD1_I4);
#endif
SET_TYPE (td->sp - 1, stack_type [mt], magic_class);
td->ip += 5;
return TRUE;
} else if (!strcmp ("op_Decrement", tm)) {
g_assert (type_index != 2); // no nfloat
#if SIZEOF_VOID_P == 8
ADD_CODE (td, MINT_SUB1_I8);
#else
ADD_CODE (td, MINT_SUB1_I4);
#endif
SET_TYPE (td->sp - 1, stack_type [mt], magic_class);
td->ip += 5;
return TRUE;
} else if (!strcmp ("CompareTo", tm) || !strcmp ("Equals", tm)) {
MonoType *arg = csignature->params [0];
/* on 'System.n*::{CompareTo,Equals} (System.n*)' variant we need to push managed
* pointer instead of value */
if (arg->type == MONO_TYPE_VALUETYPE)
emit_store_value_as_local (td, arg);
/* emit call to managed conversion method */
return FALSE;
} else if (!strcmp (".cctor", tm)) {
/* white list */
return FALSE;
} else if (!strcmp ("Parse", tm)) {
/* white list */
return FALSE;
} else if (!strcmp ("ToString", tm)) {
/* white list */
return FALSE;
} else if (!strcmp ("GetHashCode", tm)) {
/* white list */
return FALSE;
} else if (!strcmp ("IsNaN", tm) || !strcmp ("IsInfinity", tm) || !strcmp ("IsNegativeInfinity", tm) || !strcmp ("IsPositiveInfinity", tm)) {
g_assert (type_index == 2); // nfloat only
/* white list */
return FALSE;
}
for (i = 0; i < sizeof (int_unnop) / sizeof (MagicIntrinsic); ++i) {
if (!strcmp (int_unnop [i].op_name, tm)) {
ADD_CODE (td, int_unnop [i].insn [type_index]);
SET_TYPE (td->sp - 1, stack_type [mt], magic_class);
td->ip += 5;
return TRUE;
}
}
for (i = 0; i < sizeof (int_binop) / sizeof (MagicIntrinsic); ++i) {
if (!strcmp (int_binop [i].op_name, tm)) {
ADD_CODE (td, int_binop [i].insn [type_index]);
td->sp -= 1;
SET_TYPE (td->sp - 1, stack_type [mt], magic_class);
td->ip += 5;
return TRUE;
}
}
for (i = 0; i < sizeof (int_cmpop) / sizeof (MagicIntrinsic); ++i) {
if (!strcmp (int_cmpop [i].op_name, tm)) {
MonoClass *k = mono_defaults.boolean_class;
ADD_CODE (td, int_cmpop [i].insn [type_index]);
td->sp -= 1;
SET_TYPE (td->sp - 1, stack_type [mint_type (m_class_get_byval_arg (k))], k);
td->ip += 5;
return TRUE;
}
}
g_error ("TODO: interp_transform_call %s:%s", m_class_get_name (target_method->klass), tm);
} else if (mono_class_is_subclass_of (target_method->klass, mono_defaults.array_class, FALSE)) {
if (!strcmp (tm, "get_Rank")) {
*op = MINT_ARRAY_RANK;
} else if (!strcmp (tm, "get_Length")) {
*op = MINT_LDLEN;
} else if (!strcmp (tm, "Address")) {
*op = readonly ? MINT_LDELEMA : MINT_LDELEMA_TC;
} else if (!strcmp (tm, "UnsafeMov") || !strcmp (tm, "UnsafeLoad") || !strcmp (tm, "Set") || !strcmp (tm, "Get")) {
*op = MINT_CALLRUN;
} else if (!strcmp (tm, "UnsafeStore")) {
g_error ("TODO ArrayClass::UnsafeStore");
}
} else if (in_corlib &&
!strcmp (klass_name_space, "System.Diagnostics") &&
!strcmp (klass_name, "Debugger")) {
if (!strcmp (tm, "Break") && csignature->param_count == 0) {
if (mini_should_insert_breakpoint (td->method))
*op = MINT_BREAK;
}
} else if (in_corlib && !strcmp (klass_name_space, "System") && !strcmp (klass_name, "ByReference`1")) {
*op = MINT_INTRINS_BYREFERENCE_GET_VALUE;
} else if (in_corlib && !strcmp (klass_name_space, "System") && (!strcmp (klass_name, "Span`1") || !strcmp (klass_name, "ReadOnlySpan`1"))) {
if (!strcmp (tm, "get_Item")) {
MonoGenericClass *gclass = mono_class_get_generic_class (target_method->klass);
MonoClass *param_class = mono_class_from_mono_type (gclass->context.class_inst->type_argv [0]);
if (!mini_is_gsharedvt_variable_klass (param_class)) {
MonoClassField *length_field = mono_class_get_field_from_name_full (target_method->klass, "_length", NULL);
g_assert (length_field);
int offset_length = length_field->offset - sizeof (MonoObject);
MonoClassField *ptr_field = mono_class_get_field_from_name_full (target_method->klass, "_pointer", NULL);
g_assert (ptr_field);
int offset_pointer = ptr_field->offset - sizeof (MonoObject);
int size = mono_class_array_element_size (param_class);
ADD_CODE (td, MINT_GETITEM_SPAN);
ADD_CODE (td, size);
ADD_CODE (td, offset_length);
ADD_CODE (td, offset_pointer);
SET_SIMPLE_TYPE (td->sp - 1, STACK_TYPE_MP);
td->sp -= 1;
td->ip += 5;
return TRUE;
}
} else if (!strcmp (tm, "get_Length")) {
MonoClassField *length_field = mono_class_get_field_from_name_full (target_method->klass, "_length", NULL);
g_assert (length_field);
int offset_length = length_field->offset - sizeof (MonoObject);
ADD_CODE (td, MINT_LDLEN_SPAN);
ADD_CODE (td, offset_length);
SET_SIMPLE_TYPE (td->sp - 1, STACK_TYPE_I4);
td->ip += 5;
return TRUE;
}
}
return FALSE;
}
static MonoMethod*
interp_transform_internal_calls (MonoMethod *method, MonoMethod *target_method, MonoMethodSignature *csignature, gboolean is_virtual)
{
if (method->wrapper_type == MONO_WRAPPER_NONE && target_method != NULL) {
if (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
target_method = mono_marshal_get_native_wrapper (target_method, FALSE, FALSE);
if (!is_virtual && target_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
target_method = mono_marshal_get_synchronized_wrapper (target_method);
if (target_method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL && !is_virtual && !mono_class_is_marshalbyref (target_method->klass) && m_class_get_rank (target_method->klass) == 0)
target_method = mono_marshal_get_native_wrapper (target_method, TRUE, FALSE);
}
return target_method;
}
static void
interp_transform_call (TransformData *td, MonoMethod *method, MonoMethod *target_method, MonoDomain *domain, MonoGenericContext *generic_context, unsigned char *is_bb_start, int body_start_offset, MonoClass *constrained_class, gboolean readonly, MonoError *error, gboolean check_visibility)
{
MonoImage *image = m_class_get_image (method->klass);
MonoMethodSignature *csignature;
int is_virtual = *td->ip == CEE_CALLVIRT;
int calli = *td->ip == CEE_CALLI || *td->ip == CEE_MONO_CALLI_EXTRA_ARG;
int i;
guint32 vt_stack_used = 0;
guint32 vt_res_size = 0;
int op = -1;
int native = 0;
int is_void = 0;
int need_null_check = is_virtual;
guint32 token = read32 (td->ip + 1);
if (target_method == NULL) {
if (calli) {
CHECK_STACK(td, 1);
if (method->wrapper_type != MONO_WRAPPER_NONE)
csignature = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
else {
csignature = mono_metadata_parse_signature_checked (image, token, error);
return_if_nok (error);
}
if (generic_context) {
csignature = mono_inflate_generic_signature (csignature, generic_context, error);
return_if_nok (error);
}
/*
* The compiled interp entry wrapper is passed to runtime_invoke instead of
* the InterpMethod pointer. FIXME
*/
native = csignature->pinvoke || method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE;
--td->sp;
target_method = NULL;
} else {
if (method->wrapper_type == MONO_WRAPPER_NONE) {
target_method = mono_get_method_checked (image, token, NULL, generic_context, error);
return_if_nok (error);
} else
target_method = (MonoMethod *)mono_method_get_wrapper_data (method, token);
csignature = mono_method_signature (target_method);
if (generic_context) {
csignature = mono_inflate_generic_signature (csignature, generic_context, error);
return_if_nok (error);
target_method = mono_class_inflate_generic_method_checked (target_method, generic_context, error);
return_if_nok (error);
}
}
} else {
csignature = mono_method_signature (target_method);
}
if (check_visibility && target_method && !mono_method_can_access_method (method, target_method))
interp_generate_mae_throw (td, method, target_method);
if (target_method && target_method->string_ctor) {
/* Create the real signature */
MonoMethodSignature *ctor_sig = mono_metadata_signature_dup_mempool (td->mempool, csignature);
ctor_sig->ret = m_class_get_byval_arg (mono_defaults.string_class);
csignature = ctor_sig;
}
/* Intrinsics */
if (target_method && interp_handle_intrinsics (td, target_method, csignature, readonly, &op))
return;
if (constrained_class) {
if (m_class_is_enumtype (constrained_class) && !strcmp (target_method->name, "GetHashCode")) {
/* Use the corresponding method from the base type to avoid boxing */
MonoType *base_type = mono_class_enum_basetype (constrained_class);
g_assert (base_type);
constrained_class = mono_class_from_mono_type (base_type);
target_method = mono_class_get_method_from_name (constrained_class, target_method->name, 0);
g_assert (target_method);
}
}
if (constrained_class) {
mono_class_setup_vtable (constrained_class);
if (mono_class_has_failure (constrained_class)) {
mono_error_set_for_class_failure (error, constrained_class);
return;
}
#if DEBUG_INTERP
g_print ("CONSTRAINED.CALLVIRT: %s::%s. %s (%p) ->\n", target_method->klass->name, target_method->name, mono_signature_full_name (target_method->signature), target_method);
#endif
target_method = mono_get_method_constrained_with_method (image, target_method, constrained_class, generic_context, error);
#if DEBUG_INTERP
g_print (" : %s::%s. %s (%p)\n", target_method->klass->name, target_method->name, mono_signature_full_name (target_method->signature), target_method);
#endif
/* Intrinsics: Try again, it could be that `mono_get_method_constrained_with_method` resolves to a method that we can substitute */
if (target_method && interp_handle_intrinsics (td, target_method, csignature, readonly, &op))
return;
return_if_nok (error);
mono_class_setup_vtable (target_method->klass);
if (m_class_is_valuetype (constrained_class) && (target_method->klass == mono_defaults.object_class || target_method->klass == m_class_get_parent (mono_defaults.enum_class) || target_method->klass == mono_defaults.enum_class)) {
if (target_method->klass == mono_defaults.enum_class && (td->sp - csignature->param_count - 1)->type == STACK_TYPE_MP) {
/* managed pointer on the stack, we need to deref that puppy */
ADD_CODE (td, MINT_LDIND_I);
ADD_CODE (td, csignature->param_count);
}
ADD_CODE (td, MINT_BOX);
ADD_CODE (td, get_data_item_index (td, constrained_class));
ADD_CODE (td, csignature->param_count | ((td->sp - 1 - csignature->param_count)->type != STACK_TYPE_MP ? 0 : BOX_NOT_CLEAR_VT_SP));
} else if (!m_class_is_valuetype (constrained_class)) {
/* managed pointer on the stack, we need to deref that puppy */
ADD_CODE (td, MINT_LDIND_I);
ADD_CODE (td, csignature->param_count);
} else {
if (m_class_is_valuetype (target_method->klass)) {
/* Own method */
} else {
/* Interface method */
int ioffset, slot;
mono_class_setup_vtable (constrained_class);
ioffset = mono_class_interface_offset (constrained_class, target_method->klass);
if (ioffset == -1)
g_error ("type load error: constrained_class");
slot = mono_method_get_vtable_slot (target_method);
if (slot == -1)
g_error ("type load error: target_method->klass");
target_method = m_class_get_vtable (constrained_class) [ioffset + slot];
if (target_method->klass == mono_defaults.enum_class) {
if ((td->sp - csignature->param_count - 1)->type == STACK_TYPE_MP) {
/* managed pointer on the stack, we need to deref that puppy */
ADD_CODE (td, MINT_LDIND_I);
ADD_CODE (td, csignature->param_count);
}
ADD_CODE (td, MINT_BOX);
ADD_CODE (td, get_data_item_index (td, constrained_class));
ADD_CODE (td, csignature->param_count | ((td->sp - 1 - csignature->param_count)->type != STACK_TYPE_MP ? 0 : BOX_NOT_CLEAR_VT_SP));
}
}
is_virtual = FALSE;
}
}
if (target_method)
mono_class_init (target_method->klass);
if (!is_virtual && target_method && (target_method->flags & METHOD_ATTRIBUTE_ABSTRACT))
/* MS.NET seems to silently convert this to a callvirt */
is_virtual = TRUE;
if (is_virtual && target_method && (!(target_method->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
(MONO_METHOD_IS_FINAL (target_method) &&
target_method->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)) &&
!(mono_class_is_marshalbyref (target_method->klass))) {
/* Not really virtual, just needs a null check */
is_virtual = FALSE;
need_null_check = TRUE;
}
CHECK_STACK (td, csignature->param_count + csignature->hasthis);
if (!calli && op == -1 && (!is_virtual || (target_method->flags & METHOD_ATTRIBUTE_VIRTUAL) == 0) &&
(target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
(target_method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) == 0 &&
!(target_method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING)) {
MonoVTable *vt = mono_class_vtable_checked (domain, target_method->klass, error);
return_if_nok (error);
int called_inited = vt->initialized;
if (method == target_method && *(td->ip + 5) == CEE_RET && !(csignature->hasthis && m_class_is_valuetype (target_method->klass))) {
int offset;
if (td->verbose_level)
g_print ("Optimize tail call of %s.%s\n", m_class_get_name (target_method->klass), target_method->name);
for (i = csignature->param_count - 1 + !!csignature->hasthis; i >= 0; --i)
store_arg (td, i);
ADD_CODE(td, MINT_BR_S);
offset = body_start_offset - ((td->new_ip - 1) - td->new_code);
ADD_CODE(td, offset);
if (!is_bb_start [td->ip + 5 - td->il_code])
++td->ip; /* gobble the CEE_RET if it isn't branched to */
td->ip += 5;
return;
} else {
MonoMethodHeader *mheader = interp_method_get_header (target_method, error);
return_if_nok (error);
/* mheader might not exist if this is a delegate invoc, etc */
gboolean has_vt_arg = FALSE;
for (i = 0; i < csignature->param_count; i++)
has_vt_arg |= !mini_type_is_reference (csignature->params [i]);
gboolean empty_callee = mheader && *mheader->code == CEE_RET;
mono_metadata_free_mh (mheader);
if (empty_callee && called_inited && !has_vt_arg) {
if (td->verbose_level)
g_print ("Inline (empty) call of %s.%s\n", m_class_get_name (target_method->klass), target_method->name);
for (i = 0; i < csignature->param_count; i++) {
ADD_CODE (td, MINT_POP); /*FIX: vt */
ADD_CODE (td, 0);
}
if (csignature->hasthis) {
if (is_virtual || need_null_check)
ADD_CODE (td, MINT_CKNULL);
ADD_CODE (td, MINT_POP);
ADD_CODE (td, 0);
}
td->sp -= csignature->param_count + csignature->hasthis;
td->ip += 5;
return;
}
}
}
target_method = interp_transform_internal_calls (method, target_method, csignature, is_virtual);
if (csignature->call_convention == MONO_CALL_VARARG) {
csignature = mono_method_get_signature_checked (target_method, image, token, generic_context, error);
int vararg_stack = 0;
/*
* For vararg calls, ArgIterator expects the signature and the varargs to be
* stored in a linear memory. We allocate the necessary vt_stack space for
* this. All varargs will be pushed to the vt_stack at call site.
*/
vararg_stack += sizeof (gpointer);
for (i = csignature->sentinelpos; i < csignature->param_count; ++i) {
int align, arg_size;
arg_size = mono_type_stack_size (csignature->params [i], &align);
vararg_stack += arg_size;
}
vt_stack_used += ALIGN_TO (vararg_stack, MINT_VT_ALIGNMENT);
PUSH_VT (td, vararg_stack);
}
g_assert (csignature->call_convention != MONO_CALL_FASTCALL);
td->sp -= csignature->param_count + !!csignature->hasthis;
for (i = 0; i < csignature->param_count; ++i) {
if (td->sp [i + !!csignature->hasthis].type == STACK_TYPE_VT) {
gint32 size;
MonoClass *klass = mono_class_from_mono_type (csignature->params [i]);
if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
size = mono_class_native_size (klass, NULL);
else
size = mono_class_value_size (klass, NULL);
size = ALIGN_TO (size, MINT_VT_ALIGNMENT);
vt_stack_used += size;
}
}
if (need_null_check) {
ADD_CODE (td, MINT_CKNULL_N);
ADD_CODE (td, csignature->param_count + 1);
}
/* need to handle typedbyref ... */
if (csignature->ret->type != MONO_TYPE_VOID) {
int mt = mint_type(csignature->ret);
MonoClass *klass = mono_class_from_mono_type (csignature->ret);
if (mt == MINT_TYPE_VT) {
if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
vt_res_size = mono_class_native_size (klass, NULL);
else
vt_res_size = mono_class_value_size (klass, NULL);
if (mono_class_has_failure (klass)) {
mono_error_set_for_class_failure (error, klass);
return;
}
PUSH_VT(td, vt_res_size);
}
PUSH_TYPE(td, stack_type[mt], klass);
} else
is_void = TRUE;
/* We need to convert delegate invoke to a indirect call on the interp_invoke_impl field */
if (target_method && m_class_get_parent (target_method->klass) == mono_defaults.multicastdelegate_class) {
const char *name = target_method->name;
if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
calli = TRUE;
ADD_CODE (td, MINT_LD_DELEGATE_INVOKE_IMPL);
ADD_CODE (td, csignature->param_count + 1);
}
}
if (op >= 0) {
ADD_CODE (td, op);
#if SIZEOF_VOID_P == 8
if (op == MINT_LDLEN)
ADD_CODE (td, MINT_CONV_I4_I8);
#endif
if (op == MINT_LDELEMA || op == MINT_LDELEMA_TC) {
ADD_CODE (td, get_data_item_index (td, target_method->klass));
ADD_CODE (td, 1 + m_class_get_rank (target_method->klass));
}
if (op == MINT_CALLRUN) {
ADD_CODE (td, get_data_item_index (td, target_method));
ADD_CODE (td, get_data_item_index (td, mono_method_signature (target_method)));
}
} else if (!calli && !is_virtual && jit_call_supported (target_method, csignature)) {
ADD_CODE(td, MINT_JIT_CALL);
ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_imethod (domain, target_method, error)));
mono_error_assert_ok (error);
} else {
if (calli)
ADD_CODE(td, native ? MINT_CALLI_NAT : MINT_CALLI);
else if (is_virtual)
ADD_CODE(td, is_void ? MINT_VCALLVIRT : MINT_CALLVIRT);
else
ADD_CODE(td, is_void ? MINT_VCALL : MINT_CALL);
if (calli) {
ADD_CODE(td, get_data_item_index (td, (void *)csignature));
} else {
ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_imethod (domain, target_method, error)));
return_if_nok (error);
if (csignature->call_convention == MONO_CALL_VARARG)
ADD_CODE(td, get_data_item_index (td, (void *)csignature));
}
}
td->ip += 5;
if (vt_stack_used != 0 || vt_res_size != 0) {
ADD_CODE(td, MINT_VTRESULT);
ADD_CODE(td, vt_res_size);
WRITE32(td, &vt_stack_used);
td->vt_sp -= vt_stack_used;
}
}
static MonoClassField *
interp_field_from_token (MonoMethod *method, guint32 token, MonoClass **klass, MonoGenericContext *generic_context, MonoError *error)
{
MonoClassField *field = NULL;
if (method->wrapper_type != MONO_WRAPPER_NONE) {
field = (MonoClassField *) mono_method_get_wrapper_data (method, token);
*klass = field->parent;
} else {
field = mono_field_from_token_checked (m_class_get_image (method->klass), token, klass, generic_context, error);
return_val_if_nok (error, NULL);
}
if (!method->skip_visibility && !mono_method_can_access_field (method, field)) {
char *method_fname = mono_method_full_name (method, TRUE);
char *field_fname = mono_field_full_name (field);
mono_error_set_generic_error (error, "System", "FieldAccessException", "Field `%s' is inaccessible from method `%s'\n", field_fname, method_fname);
g_free (method_fname);
g_free (field_fname);
return NULL;
}
return field;
}
static InterpBasicBlock*
get_bb (TransformData *td, InterpBasicBlock *cbb, unsigned char *ip)
{
int offset = ip - td->il_code;
InterpBasicBlock *bb = td->offset_to_bb [offset];
if (!bb) {
bb = mono_mempool_alloc0 (td->mempool, sizeof (InterpBasicBlock));
bb->ip = ip;
td->offset_to_bb [offset] = bb;
td->basic_blocks = g_list_append_mempool (td->mempool, td->basic_blocks, bb);
}
if (cbb)
bb->preds = g_slist_prepend_mempool (td->mempool, bb->preds, cbb);
return bb;
}
/*
* get_basic_blocks:
*
* Compute the set of IL level basic blocks.
*/
static void
get_basic_blocks (TransformData *td)
{
guint8 *start = (guint8*)td->il_code;
guint8 *end = (guint8*)td->il_code + td->code_size;
guint8 *ip = start;
unsigned char *target;
int i;
guint cli_addr;
const MonoOpcode *opcode;
InterpBasicBlock *cbb;
td->offset_to_bb = mono_mempool_alloc0 (td->mempool, sizeof (InterpBasicBlock*) * (end - start + 1));
td->entry_bb = cbb = get_bb (td, NULL, start);
while (ip < end) {
cli_addr = ip - start;
td->offset_to_bb [cli_addr] = cbb;
i = mono_opcode_value ((const guint8 **)&ip, end);
opcode = &mono_opcodes [i];
switch (opcode->argument) {
case MonoInlineNone:
ip++;
break;
case MonoInlineString:
case MonoInlineType:
case MonoInlineField:
case MonoInlineMethod:
case MonoInlineTok:
case MonoInlineSig:
case MonoShortInlineR:
case MonoInlineI:
ip += 5;
break;
case MonoInlineVar:
ip += 3;
break;
case MonoShortInlineVar:
case MonoShortInlineI:
ip += 2;
break;
case MonoShortInlineBrTarget:
target = start + cli_addr + 2 + (signed char)ip [1];
get_bb (td, cbb, target);
ip += 2;
cbb = get_bb (td, cbb, ip);
break;
case MonoInlineBrTarget:
target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
get_bb (td, cbb, target);
ip += 5;
cbb = get_bb (td, cbb, ip);
break;
case MonoInlineSwitch: {
guint32 n = read32 (ip + 1);
guint32 j;
ip += 5;
cli_addr += 5 + 4 * n;
target = start + cli_addr;
get_bb (td, cbb, target);
for (j = 0; j < n; ++j) {
target = start + cli_addr + (gint32)read32 (ip);
get_bb (td, cbb, target);
ip += 4;
}
cbb = get_bb (td, cbb, ip);
break;
}
case MonoInlineR:
case MonoInlineI8:
ip += 9;
break;
default:
g_assert_not_reached ();
}
if (i == CEE_THROW)
cbb = get_bb (td, NULL, ip);
}
}
static void
interp_save_debug_info (InterpMethod *rtm, MonoMethodHeader *header, TransformData *td, GArray *line_numbers)
{
MonoDebugMethodJitInfo *dinfo;
int i;
if (!mono_debug_enabled ())
return;
/*
* We save the debug info in the same way the JIT does it, treating the interpreter IR as the native code.
*/
dinfo = g_new0 (MonoDebugMethodJitInfo, 1);
dinfo->num_params = rtm->param_count;
dinfo->params = g_new0 (MonoDebugVarInfo, dinfo->num_params);
dinfo->num_locals = header->num_locals;
dinfo->locals = g_new0 (MonoDebugVarInfo, header->num_locals);
dinfo->code_start = (guint8*)rtm->code;
dinfo->code_size = td->new_ip - td->new_code;
dinfo->epilogue_begin = 0;
dinfo->has_var_info = TRUE;
dinfo->num_line_numbers = line_numbers->len;
dinfo->line_numbers = g_new0 (MonoDebugLineNumberEntry, dinfo->num_line_numbers);
for (i = 0; i < dinfo->num_params; i++) {
MonoDebugVarInfo *var = &dinfo->params [i];
var->type = rtm->param_types [i];
}
for (i = 0; i < dinfo->num_locals; i++) {
MonoDebugVarInfo *var = &dinfo->locals [i];
var->type = mono_metadata_type_dup (NULL, header->locals [i]);
}
for (i = 0; i < dinfo->num_line_numbers; i++)
dinfo->line_numbers [i] = g_array_index (line_numbers, MonoDebugLineNumberEntry, i);
mono_debug_add_method (rtm->method, dinfo, rtm->domain);
mono_debug_free_method_jit_info (dinfo);
}
/* Same as the code in seq-points.c */
static void
insert_pred_seq_point (SeqPoint *last_sp, SeqPoint *sp, GSList **next)
{
GSList *l;
int src_index = last_sp->next_offset;
int dst_index = sp->next_offset;
/* bb->in_bb might contain duplicates */
for (l = next [src_index]; l; l = l->next)
if (GPOINTER_TO_UINT (l->data) == dst_index)
break;
if (!l)
next [src_index] = g_slist_append (next [src_index], GUINT_TO_POINTER (dst_index));
}
static void
recursively_make_pred_seq_points (TransformData *td, InterpBasicBlock *bb)
{
const gpointer MONO_SEQ_SEEN_LOOP = GINT_TO_POINTER(-1);
GSList *l;
GArray *predecessors = g_array_new (FALSE, TRUE, sizeof (gpointer));
GHashTable *seen = g_hash_table_new_full (g_direct_hash, NULL, NULL, NULL);
// Insert/remove sentinel into the memoize table to detect loops containing bb
bb->pred_seq_points = MONO_SEQ_SEEN_LOOP;
for (l = bb->preds; l; l = l->next) {
InterpBasicBlock *in_bb = l->data;
// This bb has the last seq point, append it and continue
if (in_bb->last_seq_point != NULL) {
predecessors = g_array_append_val (predecessors, in_bb->last_seq_point);
continue;
}
// We've looped or handled this before, exit early.
// No last sequence points to find.
if (in_bb->pred_seq_points == MONO_SEQ_SEEN_LOOP)
continue;
// Take sequence points from incoming basic blocks
if (in_bb == td->entry_bb)
continue;
if (in_bb->pred_seq_points == NULL)
recursively_make_pred_seq_points (td, in_bb);
// Union sequence points with incoming bb's
for (int i=0; i < in_bb->num_pred_seq_points; i++) {
if (!g_hash_table_lookup (seen, in_bb->pred_seq_points [i])) {
g_array_append_val (predecessors, in_bb->pred_seq_points [i]);
g_hash_table_insert (seen, in_bb->pred_seq_points [i], (gpointer)&MONO_SEQ_SEEN_LOOP);
}
}
// predecessors = g_array_append_vals (predecessors, in_bb->pred_seq_points, in_bb->num_pred_seq_points);
}
g_hash_table_destroy (seen);
if (predecessors->len != 0) {
bb->pred_seq_points = mono_mempool_alloc0 (td->mempool, sizeof (SeqPoint *) * predecessors->len);
bb->num_pred_seq_points = predecessors->len;
for (int newer = 0; newer < bb->num_pred_seq_points; newer++) {
bb->pred_seq_points [newer] = g_array_index (predecessors, gpointer, newer);
}
}
g_array_free (predecessors, TRUE);
}
static void
collect_pred_seq_points (TransformData *td, InterpBasicBlock *bb, SeqPoint *seqp, GSList **next)
{
// Doesn't have a last sequence point, must find from incoming basic blocks
if (bb->pred_seq_points == NULL && bb != td->entry_bb)
recursively_make_pred_seq_points (td, bb);
for (int i = 0; i < bb->num_pred_seq_points; i++)
insert_pred_seq_point (bb->pred_seq_points [i], seqp, next);
return;
}
static void
save_seq_points (TransformData *td, MonoJitInfo *jinfo)
{
InterpMethod *rtm = td->rtm;
GByteArray *array;
int i, seq_info_size;
MonoSeqPointInfo *info;
MonoDomain *domain = mono_domain_get ();
GSList **next = NULL;
GList *bblist;
if (!td->gen_sdb_seq_points)
return;
/*
* For each sequence point, compute the list of sequence points immediately
* following it, this is needed to implement 'step over' in the debugger agent.
* Similar to the code in mono_save_seq_point_info ().
*/
for (i = 0; i < td->seq_points->len; ++i) {
SeqPoint *sp = g_ptr_array_index (td->seq_points, i);
/* Store the seq point index here temporarily */
sp->next_offset = i;
}
next = mono_mempool_alloc0 (td->mempool, sizeof (GList*) * td->seq_points->len);
for (bblist = td->basic_blocks; bblist; bblist = bblist->next) {
InterpBasicBlock *bb = bblist->data;
GSList *bb_seq_points = g_slist_reverse (bb->seq_points);
SeqPoint *last = NULL;
for (GSList *l = bb_seq_points; l; l = l->next) {
SeqPoint *sp = l->data;
if (sp->il_offset == METHOD_ENTRY_IL_OFFSET || sp->il_offset == METHOD_EXIT_IL_OFFSET)
/* Used to implement method entry/exit events */
continue;
if (last != NULL) {
/* Link with the previous seq point in the same bb */
next [last->next_offset] = g_slist_append_mempool (td->mempool, next [last->next_offset], GINT_TO_POINTER (sp->next_offset));
} else {
/* Link with the last bb in the previous bblocks */
collect_pred_seq_points (td, bb, sp, next);
}
last = sp;
}
}
/* Serialize the seq points into a byte array */
array = g_byte_array_new ();
SeqPoint zero_seq_point = {0};
SeqPoint* last_seq_point = &zero_seq_point;
for (i = 0; i < td->seq_points->len; ++i) {
SeqPoint *sp = (SeqPoint*)g_ptr_array_index (td->seq_points, i);
sp->next_offset = 0;
if (mono_seq_point_info_add_seq_point (array, sp, last_seq_point, next [i], TRUE))
last_seq_point = sp;
}
if (td->verbose_level) {
g_print ("\nSEQ POINT MAP FOR %s: \n", td->method->name);
for (i = 0; i < td->seq_points->len; ++i) {
SeqPoint *sp = (SeqPoint*)g_ptr_array_index (td->seq_points, i);
GSList *l;
if (!next [i])
continue;
g_print ("\tIL0x%x[0x%0x] ->", sp->il_offset, sp->native_offset);
for (l = next [i]; l; l = l->next) {
int next_index = GPOINTER_TO_UINT (l->data);
g_print (" IL0x%x", ((SeqPoint*)g_ptr_array_index (td->seq_points, next_index))->il_offset);
}
g_print ("\n");
}
}
info = mono_seq_point_info_new (array->len, TRUE, array->data, TRUE, &seq_info_size);
mono_atomic_fetch_add_i32 (&mono_jit_stats.allocated_seq_points_size, seq_info_size);
g_byte_array_free (array, TRUE);
mono_domain_lock (domain);
g_hash_table_insert (domain_jit_info (domain)->seq_points, rtm->method, info);
mono_domain_unlock (domain);
jinfo->seq_points = info;
}
static void
emit_seq_point (TransformData *td, int il_offset, InterpBasicBlock *cbb, gboolean nonempty_stack)
{
SeqPoint *seqp;
seqp = mono_mempool_alloc0 (td->mempool, sizeof (SeqPoint));
seqp->il_offset = il_offset;
seqp->native_offset = (guint8*)td->new_ip - (guint8*)td->new_code;
if (nonempty_stack)
seqp->flags |= MONO_SEQ_POINT_FLAG_NONEMPTY_STACK;
ADD_CODE (td, MINT_SDB_SEQ_POINT);
g_ptr_array_add (td->seq_points, seqp);
cbb->seq_points = g_slist_prepend_mempool (td->mempool, cbb->seq_points, seqp);
cbb->last_seq_point = seqp;
}
#define BARRIER_IF_VOLATILE(td) \
do { \
if (volatile_) { \
ADD_CODE (td, MINT_MONO_MEMORY_BARRIER); \
volatile_ = FALSE; \
} \
} while (0)
static void
generate (MonoMethod *method, MonoMethodHeader *header, InterpMethod *rtm, unsigned char *is_bb_start, MonoGenericContext *generic_context, MonoError *error)
{
MonoMethodSignature *signature = mono_method_signature (method);
MonoImage *image = m_class_get_image (method->klass);
MonoDomain *domain = rtm->domain;
MonoClass *constrained_class = NULL;
MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
int offset, mt, i, i32;
gboolean readonly = FALSE;
gboolean volatile_ = FALSE;
MonoClass *klass;
MonoClassField *field;
const unsigned char *end;
int new_in_start_offset;
int body_start_offset;
int target;
guint32 token;
TransformData transform_data;
TransformData *td;
GArray *line_numbers;
MonoDebugMethodInfo *minfo;
MonoBitSet *seq_point_locs = NULL;
MonoBitSet *seq_point_set_locs = NULL;
gboolean sym_seq_points = FALSE;
InterpBasicBlock *bb_exit = NULL;
static gboolean verbose_method_inited;
static char* verbose_method_name;
if (!verbose_method_inited) {
verbose_method_name = getenv ("MONO_VERBOSE_METHOD");
verbose_method_inited = TRUE;
}
memset (&transform_data, 0, sizeof(transform_data));
td = &transform_data;
td->method = method;
td->rtm = rtm;
td->is_bb_start = is_bb_start;
td->il_code = header->code;
td->code_size = header->code_size;
td->header = header;
td->max_code_size = td->code_size;
td->new_code = (unsigned short *)g_malloc(td->max_code_size * sizeof(gushort));
td->new_code_end = td->new_code + td->max_code_size;
td->mempool = mono_mempool_new ();
td->in_offsets = g_malloc0((header->code_size + 1) * sizeof(int));
td->stack_state = g_malloc0(header->code_size * sizeof(StackInfo *));
td->stack_height = g_malloc(header->code_size * sizeof(int));
td->vt_stack_size = g_malloc(header->code_size * sizeof(int));
td->n_data_items = 0;
td->max_data_items = 0;
td->data_items = NULL;
td->data_hash = g_hash_table_new (NULL, NULL);
td->clause_indexes = g_malloc (header->code_size * sizeof (int));
td->gen_sdb_seq_points = mini_debug_options.gen_sdb_seq_points;
td->seq_points = g_ptr_array_new ();
td->relocs = g_ptr_array_new ();
td->verbose_level = mono_interp_traceopt;
td->total_locals_size = rtm->locals_size;
rtm->data_items = td->data_items;
for (i = 0; i < header->code_size; i++) {
td->stack_height [i] = -1;
td->clause_indexes [i] = -1;
}
if (verbose_method_name) {
const char *name = verbose_method_name;
if ((strchr (name, '.') > name) || strchr (name, ':')) {
MonoMethodDesc *desc;
desc = mono_method_desc_new (name, TRUE);
if (mono_method_desc_full_match (desc, method)) {
td->verbose_level = 4;
}
mono_method_desc_free (desc);
} else {
if (strcmp (method->name, name) == 0)
td->verbose_level = 4;
}
}
if (td->gen_sdb_seq_points) {
get_basic_blocks (td);
minfo = mono_debug_lookup_method (method);
if (minfo) {
MonoSymSeqPoint *sps;
int i, n_il_offsets;
mono_debug_get_seq_points (minfo, NULL, NULL, NULL, &sps, &n_il_offsets);
// FIXME: Free
seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (td->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
seq_point_set_locs = mono_bitset_mem_new (mono_mempool_alloc0 (td->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
sym_seq_points = TRUE;
for (i = 0; i < n_il_offsets; ++i) {
if (sps [i].il_offset < header->code_size)
mono_bitset_set_fast (seq_point_locs, sps [i].il_offset);
}
g_free (sps);
MonoDebugMethodAsyncInfo* asyncMethod = mono_debug_lookup_method_async_debug_info (method);
if (asyncMethod) {
for (i = 0; asyncMethod != NULL && i < asyncMethod->num_awaits; i++) {
mono_bitset_set_fast (seq_point_locs, asyncMethod->resume_offsets [i]);
mono_bitset_set_fast (seq_point_locs, asyncMethod->yield_offsets [i]);
}
mono_debug_free_method_async_debug_info (asyncMethod);
}
} else if (!method->wrapper_type && !method->dynamic && mono_debug_image_has_debug_info (m_class_get_image (method->klass))) {
/* Methods without line number info like auto-generated property accessors */
seq_point_locs = mono_bitset_new (header->code_size, 0);
seq_point_set_locs = mono_bitset_new (header->code_size, 0);
sym_seq_points = TRUE;
}
}
td->new_ip = td->new_code;
td->last_new_ip = NULL;
td->stack = g_malloc0 ((header->max_stack + 1) * sizeof (td->stack [0]));
td->stack_capacity = header->max_stack + 1;
td->sp = td->stack;
td->max_stack_height = 0;
line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
for (i = 0; i < header->num_clauses; i++) {
MonoExceptionClause *c = header->clauses + i;
td->stack_height [c->handler_offset] = 0;
td->vt_stack_size [c->handler_offset] = 0;
td->is_bb_start [c->handler_offset] = 1;
td->stack_height [c->handler_offset] = 1;
td->stack_state [c->handler_offset] = g_malloc0(sizeof(StackInfo));
td->stack_state [c->handler_offset][0].type = STACK_TYPE_O;
td->stack_state [c->handler_offset][0].klass = NULL; /*FIX*/
if (c->flags & MONO_EXCEPTION_CLAUSE_FILTER) {
td->stack_height [c->data.filter_offset] = 0;
td->vt_stack_size [c->data.filter_offset] = 0;
td->is_bb_start [c->data.filter_offset] = 1;
td->stack_height [c->data.filter_offset] = 1;
td->stack_state [c->data.filter_offset] = g_malloc0(sizeof(StackInfo));
td->stack_state [c->data.filter_offset][0].type = STACK_TYPE_O;
td->stack_state [c->data.filter_offset][0].klass = NULL; /*FIX*/
}
for (int j = c->handler_offset; j < c->handler_offset + c->handler_len; ++j) {
if (td->clause_indexes [j] == -1)
td->clause_indexes [j] = i;
}
}
td->ip = header->code;
end = td->ip + header->code_size;
if (td->verbose_level) {
char *tmp = mono_disasm_code (NULL, method, td->ip, end);
char *name = mono_method_full_name (method, TRUE);
g_print ("Method %s, original code:\n", name);
g_print ("%s\n", tmp);
g_free (tmp);
g_free (name);
}
if (signature->hasthis)
store_inarg (td, 0);
for (i = 0; i < signature->param_count; i++)
store_inarg (td, i + !!signature->hasthis);
body_start_offset = td->new_ip - td->new_code;
for (i = 0; i < header->num_locals; i++) {
int mt = mint_type(header->locals [i]);
if (mt == MINT_TYPE_VT || mt == MINT_TYPE_O || mt == MINT_TYPE_P) {
ADD_CODE(td, MINT_INITLOCALS);
break;
}
}
if (rtm->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_ENTER)
ADD_CODE (td, MINT_PROF_ENTER);
if (sym_seq_points) {
InterpBasicBlock *cbb = td->offset_to_bb [0];
g_assert (cbb);
emit_seq_point (td, METHOD_ENTRY_IL_OFFSET, cbb, FALSE);
}
original_bb = bb = mono_basic_block_split (method, error, header);
goto_if_nok (error, exit);
g_assert (bb);
int in_offset;
while (td->ip < end) {
g_assert (td->sp >= td->stack);
g_assert (td->vt_sp < 0x10000000);
in_offset = td->ip - header->code;
td->in_offsets [in_offset] = td->new_ip - td->new_code;
new_in_start_offset = td->new_ip - td->new_code;
td->in_start = td->ip;
MonoDebugLineNumberEntry lne;
lne.native_offset = (guint8*)td->new_ip - (guint8*)td->new_code;
lne.il_offset = in_offset;
g_array_append_val (line_numbers, lne);
if (td->stack_height [in_offset] >= 0) {
g_assert (is_bb_start [in_offset]);
if (td->stack_height [in_offset] > 0)
memcpy (td->stack, td->stack_state [in_offset], td->stack_height [in_offset] * sizeof(td->stack [0]));
td->sp = td->stack + td->stack_height [in_offset];
td->vt_sp = td->vt_stack_size [in_offset];
}
if (in_offset == bb->end)
bb = bb->next;
if (bb->dead) {
int op_size = mono_opcode_size (td->ip, end);
g_assert (op_size > 0); /* The BB formation pass must catch all bad ops */
if (td->verbose_level > 1)
g_print ("SKIPPING DEAD OP at %x\n", in_offset);
td->ip += op_size;
continue;
}
if (td->verbose_level > 1) {
g_print ("IL_%04lx %s %-10s -> IL_%04lx, sp %ld, %s %-12s vt_sp %u (max %u)\n",
td->ip - td->il_code,
td->is_bb_start [td->ip - td->il_code] == 3 ? "<>" :
td->is_bb_start [td->ip - td->il_code] == 2 ? "< " :
td->is_bb_start [td->ip - td->il_code] == 1 ? " >" : " ",
mono_opcode_name (*td->ip), td->new_ip - td->new_code, td->sp - td->stack,
td->sp > td->stack ? stack_type_string [td->sp [-1].type] : " ",
(td->sp > td->stack && (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_VT)) ? (td->sp [-1].klass == NULL ? "?" : m_class_get_name (td->sp [-1].klass)) : "",
td->vt_sp, td->max_vt_sp);
}
if (sym_seq_points && mono_bitset_test_fast (seq_point_locs, td->ip - header->code)) {
InterpBasicBlock *cbb = td->offset_to_bb [td->ip - header->code];
g_assert (cbb);
/*
* Make methods interruptable at the beginning, and at the targets of
* backward branches.
*/
if (in_offset == 0 || g_slist_length (cbb->preds) > 1)
ADD_CODE (td, MINT_SDB_INTR_LOC);
emit_seq_point (td, in_offset, cbb, FALSE);
mono_bitset_set_fast (seq_point_set_locs, td->ip - header->code);
}
if (sym_seq_points)
bb_exit = td->offset_to_bb [td->ip - header->code];
if (is_bb_start [in_offset]) {
int index = td->clause_indexes [in_offset];
if (index != -1) {
MonoExceptionClause *clause = &header->clauses [index];
if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY &&
in_offset == clause->handler_offset)
ADD_CODE (td, MINT_START_ABORT_PROT);
}
}
switch (*td->ip) {
case CEE_NOP:
/* lose it */
++td->ip;
break;
case CEE_BREAK:
SIMPLE_OP(td, MINT_BREAK);
break;
case CEE_LDARG_0:
case CEE_LDARG_1:
case CEE_LDARG_2:
case CEE_LDARG_3:
load_arg (td, *td->ip - CEE_LDARG_0);
++td->ip;
break;
case CEE_LDLOC_0:
case CEE_LDLOC_1:
case CEE_LDLOC_2:
case CEE_LDLOC_3:
load_local (td, *td->ip - CEE_LDLOC_0);
++td->ip;
break;
case CEE_STLOC_0:
case CEE_STLOC_1:
case CEE_STLOC_2:
case CEE_STLOC_3:
store_local (td, *td->ip - CEE_STLOC_0);
++td->ip;
break;
case CEE_LDARG_S:
load_arg (td, ((guint8 *)td->ip)[1]);
td->ip += 2;
break;
case CEE_LDARGA_S: {
/* NOTE: n includes this */
int n = ((guint8 *) td->ip) [1];
ADD_CODE (td, MINT_LDARGA);
ADD_CODE (td, td->rtm->arg_offsets [n]);
PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
td->ip += 2;
break;
}
case CEE_STARG_S:
store_arg (td, ((guint8 *)td->ip)[1]);
td->ip += 2;
break;
case CEE_LDLOC_S:
load_local (td, ((guint8 *)td->ip)[1]);
td->ip += 2;
break;
case CEE_LDLOCA_S:
ADD_CODE(td, MINT_LDLOCA_S);
ADD_CODE(td, td->rtm->local_offsets [((guint8 *)td->ip)[1]]);
PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
td->ip += 2;
break;
case CEE_STLOC_S:
store_local (td, ((guint8 *)td->ip)[1]);
td->ip += 2;
break;
case CEE_LDNULL:
SIMPLE_OP(td, MINT_LDNULL);
PUSH_TYPE(td, STACK_TYPE_O, NULL);
break;
case CEE_LDC_I4_M1:
SIMPLE_OP(td, MINT_LDC_I4_M1);
PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
break;
case CEE_LDC_I4_0:
if (!td->is_bb_start[td->ip + 1 - td->il_code] && td->ip [1] == 0xfe && td->ip [2] == CEE_CEQ &&
td->sp > td->stack && td->sp [-1].type == STACK_TYPE_I4) {
SIMPLE_OP(td, MINT_CEQ0_I4);
td->ip += 2;
} else {
SIMPLE_OP(td, MINT_LDC_I4_0);
PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
}
break;
case CEE_LDC_I4_1:
if (!td->is_bb_start[td->ip + 1 - td->il_code] &&
(td->ip [1] == CEE_ADD || td->ip [1] == CEE_SUB) && td->sp [-1].type == STACK_TYPE_I4) {
ADD_CODE(td, td->ip [1] == CEE_ADD ? MINT_ADD1_I4 : MINT_SUB1_I4);
td->ip += 2;
} else {
SIMPLE_OP(td, MINT_LDC_I4_1);
PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
}
break;
case CEE_LDC_I4_2:
case CEE_LDC_I4_3:
case CEE_LDC_I4_4:
case CEE_LDC_I4_5:
case CEE_LDC_I4_6:
case CEE_LDC_I4_7:
case CEE_LDC_I4_8:
SIMPLE_OP(td, (*td->ip - CEE_LDC_I4_0) + MINT_LDC_I4_0);
PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
break;
case CEE_LDC_I4_S:
ADD_CODE(td, MINT_LDC_I4_S);
ADD_CODE(td, ((gint8 *) td->ip) [1]);
td->ip += 2;
PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
break;
case CEE_LDC_I4:
i32 = read32 (td->ip + 1);
ADD_CODE(td, MINT_LDC_I4);
WRITE32(td, &i32);
td->ip += 5;
PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
break;
case CEE_LDC_I8: {
gint64 val = read64 (td->ip + 1);
ADD_CODE(td, MINT_LDC_I8);
WRITE64(td, &val);
td->ip += 9;
PUSH_SIMPLE_TYPE(td, STACK_TYPE_I8);
break;
}
case CEE_LDC_R4: {
float val;
readr4 (td->ip + 1, &val);
ADD_CODE(td, MINT_LDC_R4);
WRITE32(td, &val);
td->ip += 5;
PUSH_SIMPLE_TYPE(td, STACK_TYPE_R4);
break;
}
case CEE_LDC_R8: {
double val;
readr8 (td->ip + 1, &val);
ADD_CODE(td, MINT_LDC_R8);
WRITE64(td, &val);
td->ip += 9;
PUSH_SIMPLE_TYPE(td, STACK_TYPE_R8);
break;
}
case CEE_DUP: {
int type = td->sp [-1].type;
MonoClass *klass = td->sp [-1].klass;
if (td->sp [-1].type == STACK_TYPE_VT) {
gint32 size = mono_class_value_size (klass, NULL);
PUSH_VT(td, size);
ADD_CODE(td, MINT_DUP_VT);
WRITE32(td, &size);
td->ip ++;
} else
SIMPLE_OP(td, MINT_DUP);
PUSH_TYPE(td, type, klass);
break;
}
case CEE_POP:
CHECK_STACK(td, 1);
SIMPLE_OP(td, MINT_POP);
ADD_CODE (td, 0);
if (td->sp [-1].type == STACK_TYPE_VT) {
int size = mono_class_value_size (td->sp [-1].klass, NULL);
size = ALIGN_TO (size, MINT_VT_ALIGNMENT);
ADD_CODE(td, MINT_VTRESULT);
ADD_CODE(td, 0);
WRITE32(td, &size);
td->vt_sp -= size;
}
--td->sp;
break;
case CEE_JMP: {
MonoMethod *m;
if (td->sp > td->stack)
g_warning ("CEE_JMP: stack must be empty");
token = read32 (td->ip + 1);
m = mono_get_method_checked (image, token, NULL, generic_context, error);
goto_if_nok (error, exit);
ADD_CODE (td, MINT_JMP);
ADD_CODE (td, get_data_item_index (td, mono_interp_get_imethod (domain, m, error)));
goto_if_nok (error, exit);
td->ip += 5;
break;
}
case CEE_CALLVIRT: /* Fall through */
case CEE_CALLI: /* Fall through */
case CEE_CALL: {
gboolean need_seq_point = FALSE;
if (sym_seq_points && !mono_bitset_test_fast (seq_point_locs, td->ip + 5 - header->code))
need_seq_point = TRUE;
interp_transform_call (td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, constrained_class, readonly, error, TRUE);
goto_if_nok (error, exit);
if (need_seq_point) {
InterpBasicBlock *cbb = td->offset_to_bb [td->ip - header->code];
g_assert (cbb);
emit_seq_point (td, td->ip - header->code, cbb, TRUE);
}
constrained_class = NULL;
readonly = FALSE;
break;
}
case CEE_RET: {
int vt_size = 0;
MonoType *ult = mini_type_get_underlying_type (signature->ret);
if (ult->type != MONO_TYPE_VOID) {
CHECK_STACK (td, 1);
--td->sp;
if (mint_type (ult) == MINT_TYPE_VT) {
MonoClass *klass = mono_class_from_mono_type (ult);
vt_size = mono_class_value_size (klass, NULL);
}
}
if (td->sp > td->stack)
g_warning ("%s: CEE_RET: more values on stack: %d", mono_method_full_name (td->method, TRUE), td->sp - td->stack);
if (td->vt_sp != ALIGN_TO (vt_size, MINT_VT_ALIGNMENT))
g_error ("%s: CEE_RET: value type stack: %d vs. %d", mono_method_full_name (td->method, TRUE), td->vt_sp, vt_size);
if (sym_seq_points) {
InterpBasicBlock *cbb = td->offset_to_bb [td->ip - header->code];
g_assert (cbb);
emit_seq_point (td, METHOD_EXIT_IL_OFFSET, bb_exit, FALSE);
}
if (vt_size == 0)
SIMPLE_OP(td, ult->type == MONO_TYPE_VOID ? MINT_RET_VOID : MINT_RET);
else {
ADD_CODE(td, MINT_RET_VT);
WRITE32(td, &vt_size);
++td->ip;
}
break;
}
case CEE_BR:
handle_branch (td, MINT_BR_S, MINT_BR, 5 + read32 (td->ip + 1));
td->ip += 5;
break;
case CEE_BR_S:
handle_branch (td, MINT_BR_S, MINT_BR, 2 + (gint8)td->ip [1]);
td->ip += 2;
break;
case CEE_BRFALSE:
one_arg_branch (td, MINT_BRFALSE_I4, 5 + read32 (td->ip + 1));
td->ip += 5;
break;
case CEE_BRFALSE_S:
one_arg_branch (td, MINT_BRFALSE_I4, 2 + (gint8)td->ip [1]);
td->ip += 2;
break;
case CEE_BRTRUE:
one_arg_branch (td, MINT_BRTRUE_I4, 5 + read32 (td->ip + 1));
td->ip += 5;
break;
case CEE_BRTRUE_S:
one_arg_branch (td, MINT_BRTRUE_I4, 2 + (gint8)td->ip [1]);
td->ip += 2;
break;
case CEE_BEQ:
two_arg_branch (td, MINT_BEQ_I4, 5 + read32 (td->ip + 1));
td->ip += 5;
break;
case CEE_BEQ_S:
two_arg_branch (td, MINT_BEQ_I4, 2 + (gint8) td->ip [1]);
td->ip += 2;
break;
case CEE_BGE:
two_arg_branch (td, MINT_BGE_I4, 5 + read32 (td->ip + 1));
td->ip += 5;
break;
case CEE_BGE_S:
two_arg_branch (td, MINT_BGE_I4, 2 + (gint8) td->ip [1]);
td->ip += 2;
break;
case CEE_BGT:
two_arg_branch (td, MINT_BGT_I4, 5 + read32 (td->ip + 1));
td->ip += 5;
break;
case CEE_BGT_S:
two_arg_branch (td, MINT_BGT_I4, 2 + (gint8) td->ip [1]);
td->ip += 2;
break;
case CEE_BLT:
two_arg_branch (td, MINT_BLT_I4, 5 + read32 (td->ip + 1));
td->ip += 5;
break;
case CEE_BLT_S:
two_arg_branch (td, MINT_BLT_I4, 2 + (gint8) td->ip [1]);
td->ip += 2;
break;
case CEE_BLE:
two_arg_branch (td, MINT_BLE_I4, 5 + read32 (td->ip + 1));
td->ip += 5;
break;
case CEE_BLE_S:
two_arg_branch (td, MINT_BLE_I4, 2 + (gint8) td->ip [1]);
td->ip += 2;
break;
case CEE_BNE_UN:
two_arg_branch (td, MINT_BNE_UN_I4, 5 + read32 (td->ip + 1));
td->ip += 5;
break;
case CEE_BNE_UN_S:
two_arg_branch (td, MINT_BNE_UN_I4, 2 + (gint8) td->ip [1]);
td->ip += 2;
break;
case CEE_BGE_UN:
two_arg_branch (td, MINT_BGE_UN_I4, 5 + read32 (td->ip + 1));
td->ip += 5;
break;
case CEE_BGE_UN_S:
two_arg_branch (td, MINT_BGE_UN_I4, 2 + (gint8) td->ip [1]);
td->ip += 2;
break;
case CEE_BGT_UN:
two_arg_branch (td, MINT_BGT_UN_I4, 5 + read32 (td->ip + 1));
td->ip += 5;
break;
case CEE_BGT_UN_S:
two_arg_branch (td, MINT_BGT_UN_I4, 2 + (gint8) td->ip [1]);
td->ip += 2;
break;
case CEE_BLE_UN:
two_arg_branch (td, MINT_BLE_UN_I4, 5 + read32 (td->ip + 1));
td->ip += 5;
break;
case CEE_BLE_UN_S:
two_arg_branch (td, MINT_BLE_UN_I4, 2 + (gint8) td->ip [1]);
td->ip += 2;
break;
case CEE_BLT_UN:
two_arg_branch (td, MINT_BLT_UN_I4, 5 + read32 (td->ip + 1));
td->ip += 5;
break;
case CEE_BLT_UN_S:
two_arg_branch (td, MINT_BLT_UN_I4, 2 + (gint8) td->ip [1]);
td->ip += 2;
break;
case CEE_SWITCH: {
guint32 n;
const unsigned char *next_ip;
++td->ip;
n = read32 (td->ip);
ADD_CODE (td, MINT_SWITCH);
WRITE32 (td, &n);
td->ip += 4;
next_ip = td->ip + n * 4;
--td->sp;
int stack_height = td->sp - td->stack;
for (i = 0; i < n; i++) {
offset = read32 (td->ip);
target = next_ip - td->il_code + offset;
if (offset < 0) {
#if DEBUG_INTERP
if (stack_height > 0 && stack_height != td->stack_height [target])
g_warning ("SWITCH with back branch and non-empty stack");
#endif
target = td->in_offsets [target] - (td->new_ip - td->new_code);
} else {
td->stack_height [target] = stack_height;
td->vt_stack_size [target] = td->vt_sp;
if (stack_height > 0)
td->stack_state [target] = g_memdup (td->stack, stack_height * sizeof (td->stack [0]));
Reloc *reloc = mono_mempool_alloc0 (td->mempool, sizeof (Reloc));
reloc->type = RELOC_SWITCH;
reloc->offset = td->new_ip - td->new_code;
reloc->target = target;
g_ptr_array_add (td->relocs, reloc);
target = 0xffff;
}
WRITE32 (td, &target);
td->ip += 4;
}
break;
}
case CEE_LDIND_I1:
CHECK_STACK (td, 1);
ADD_CODE (td, MINT_CKNULL);
SIMPLE_OP (td, MINT_LDIND_I1);
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
BARRIER_IF_VOLATILE (td);
break;
case CEE_LDIND_U1:
CHECK_STACK (td, 1);
ADD_CODE (td, MINT_CKNULL);
SIMPLE_OP (td, MINT_LDIND_U1);
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
BARRIER_IF_VOLATILE (td);
break;
case CEE_LDIND_I2:
CHECK_STACK (td, 1);
ADD_CODE (td, MINT_CKNULL);
SIMPLE_OP (td, MINT_LDIND_I2);
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
BARRIER_IF_VOLATILE (td);
break;
case CEE_LDIND_U2:
CHECK_STACK (td, 1);
ADD_CODE (td, MINT_CKNULL);
SIMPLE_OP (td, MINT_LDIND_U2);
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
BARRIER_IF_VOLATILE (td);
break;
case CEE_LDIND_I4:
CHECK_STACK (td, 1);
ADD_CODE (td, MINT_CKNULL);
SIMPLE_OP (td, MINT_LDIND_I4);
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
BARRIER_IF_VOLATILE (td);
break;
case CEE_LDIND_U4:
CHECK_STACK (td, 1);
ADD_CODE (td, MINT_CKNULL);
SIMPLE_OP (td, MINT_LDIND_U4);
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
BARRIER_IF_VOLATILE (td);
break;
case CEE_LDIND_I8:
CHECK_STACK (td, 1);
ADD_CODE (td, MINT_CKNULL);
SIMPLE_OP (td, MINT_LDIND_I8);
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
BARRIER_IF_VOLATILE (td);
break;
case CEE_LDIND_I:
CHECK_STACK (td, 1);
ADD_CODE (td, MINT_CKNULL);
SIMPLE_OP (td, MINT_LDIND_I);
ADD_CODE (td, 0);
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
BARRIER_IF_VOLATILE (td);
break;
case CEE_LDIND_R4:
CHECK_STACK (td, 1);
ADD_CODE (td, MINT_CKNULL);
SIMPLE_OP (td, MINT_LDIND_R4);
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R4);
BARRIER_IF_VOLATILE (td);
break;
case CEE_LDIND_R8:
CHECK_STACK (td, 1);
ADD_CODE (td, MINT_CKNULL);
SIMPLE_OP (td, MINT_LDIND_R8);
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
BARRIER_IF_VOLATILE (td);
break;
case CEE_LDIND_REF:
CHECK_STACK (td, 1);
ADD_CODE (td, MINT_CKNULL);
SIMPLE_OP (td, MINT_LDIND_REF);
BARRIER_IF_VOLATILE (td);
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_O);
break;
case CEE_STIND_REF:
CHECK_STACK (td, 2);
BARRIER_IF_VOLATILE (td);
SIMPLE_OP (td, MINT_STIND_REF);
td->sp -= 2;
break;
case CEE_STIND_I1:
CHECK_STACK (td, 2);
BARRIER_IF_VOLATILE (td);
SIMPLE_OP (td, MINT_STIND_I1);
td->sp -= 2;
break;
case CEE_STIND_I2:
CHECK_STACK (td, 2);
BARRIER_IF_VOLATILE (td);
SIMPLE_OP (td, MINT_STIND_I2);
td->sp -= 2;
break;
case CEE_STIND_I4:
CHECK_STACK (td, 2);
BARRIER_IF_VOLATILE (td);
SIMPLE_OP (td, MINT_STIND_I4);
td->sp -= 2;
break;
case CEE_STIND_I:
CHECK_STACK (td, 2);
BARRIER_IF_VOLATILE (td);
SIMPLE_OP (td, MINT_STIND_I);
td->sp -= 2;
break;
case CEE_STIND_I8:
CHECK_STACK (td, 2);
BARRIER_IF_VOLATILE (td);
SIMPLE_OP (td, MINT_STIND_I8);
td->sp -= 2;
break;
case CEE_STIND_R4:
CHECK_STACK (td, 2);
BARRIER_IF_VOLATILE (td);
SIMPLE_OP (td, MINT_STIND_R4);
td->sp -= 2;
break;
case CEE_STIND_R8:
CHECK_STACK (td, 2);
BARRIER_IF_VOLATILE (td);
SIMPLE_OP (td, MINT_STIND_R8);
td->sp -= 2;
break;
case CEE_ADD:
binary_arith_op(td, MINT_ADD_I4);
++td->ip;
break;
case CEE_SUB:
binary_arith_op(td, MINT_SUB_I4);
++td->ip;
break;
case CEE_MUL:
binary_arith_op(td, MINT_MUL_I4);
++td->ip;
break;
case CEE_DIV:
binary_arith_op(td, MINT_DIV_I4);
++td->ip;
break;
case CEE_DIV_UN:
binary_arith_op(td, MINT_DIV_UN_I4);
++td->ip;
break;
case CEE_REM:
binary_arith_op (td, MINT_REM_I4);
++td->ip;
break;
case CEE_REM_UN:
binary_arith_op (td, MINT_REM_UN_I4);
++td->ip;
break;
case CEE_AND:
binary_arith_op (td, MINT_AND_I4);
++td->ip;
break;
case CEE_OR:
binary_arith_op (td, MINT_OR_I4);
++td->ip;
break;
case CEE_XOR:
binary_arith_op (td, MINT_XOR_I4);
++td->ip;
break;
case CEE_SHL:
shift_op (td, MINT_SHL_I4);
++td->ip;
break;
case CEE_SHR:
shift_op (td, MINT_SHR_I4);
++td->ip;
break;
case CEE_SHR_UN:
shift_op (td, MINT_SHR_UN_I4);
++td->ip;
break;
case CEE_NEG:
unary_arith_op (td, MINT_NEG_I4);
++td->ip;
break;
case CEE_NOT:
unary_arith_op (td, MINT_NOT_I4);
++td->ip;
break;
case CEE_CONV_U1:
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_R4:
ADD_CODE (td, MINT_CONV_U1_R4);
break;
case STACK_TYPE_R8:
ADD_CODE(td, MINT_CONV_U1_R8);
break;
case STACK_TYPE_I4:
ADD_CODE(td, MINT_CONV_U1_I4);
break;
case STACK_TYPE_I8:
ADD_CODE(td, MINT_CONV_U1_I8);
break;
default:
g_assert_not_reached ();
}
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
case CEE_CONV_I1:
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_R4:
ADD_CODE(td, MINT_CONV_I1_R4);
break;
case STACK_TYPE_R8:
ADD_CODE(td, MINT_CONV_I1_R8);
break;
case STACK_TYPE_I4:
ADD_CODE(td, MINT_CONV_I1_I4);
break;
case STACK_TYPE_I8:
ADD_CODE(td, MINT_CONV_I1_I8);
break;
default:
g_assert_not_reached ();
}
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
case CEE_CONV_U2:
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_R4:
ADD_CODE(td, MINT_CONV_U2_R4);
break;
case STACK_TYPE_R8:
ADD_CODE(td, MINT_CONV_U2_R8);
break;
case STACK_TYPE_I4:
ADD_CODE(td, MINT_CONV_U2_I4);
break;
case STACK_TYPE_I8:
ADD_CODE(td, MINT_CONV_U2_I8);
break;
default:
g_assert_not_reached ();
}
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
case CEE_CONV_I2:
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_R4:
ADD_CODE(td, MINT_CONV_I2_R4);
break;
case STACK_TYPE_R8:
ADD_CODE(td, MINT_CONV_I2_R8);
break;
case STACK_TYPE_I4:
ADD_CODE(td, MINT_CONV_I2_I4);
break;
case STACK_TYPE_I8:
ADD_CODE(td, MINT_CONV_I2_I8);
break;
default:
g_assert_not_reached ();
}
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
case CEE_CONV_U:
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_R8:
#if SIZEOF_VOID_P == 4
ADD_CODE(td, MINT_CONV_U4_R8);
#else
ADD_CODE(td, MINT_CONV_U8_R8);
#endif
break;
case STACK_TYPE_I4:
#if SIZEOF_VOID_P == 8
ADD_CODE(td, MINT_CONV_U8_I4);
#endif
break;
case STACK_TYPE_I8:
#if SIZEOF_VOID_P == 4
ADD_CODE(td, MINT_CONV_U4_I8);
#endif
break;
case STACK_TYPE_MP:
case STACK_TYPE_O:
break;
default:
g_assert_not_reached ();
}
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
break;
case CEE_CONV_I:
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_R8:
#if SIZEOF_VOID_P == 8
ADD_CODE(td, MINT_CONV_I8_R8);
#else
ADD_CODE(td, MINT_CONV_I4_R8);
#endif
break;
case STACK_TYPE_I4:
#if SIZEOF_VOID_P == 8
ADD_CODE(td, MINT_CONV_I8_I4);
#endif
break;
case STACK_TYPE_O:
break;
case STACK_TYPE_MP:
break;
case STACK_TYPE_I8:
#if SIZEOF_VOID_P == 4
ADD_CODE(td, MINT_CONV_I4_I8);
#endif
break;
default:
g_assert_not_reached ();
}
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
break;
case CEE_CONV_U4:
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_R4:
ADD_CODE(td, MINT_CONV_U4_R4);
break;
case STACK_TYPE_R8:
ADD_CODE(td, MINT_CONV_U4_R8);
break;
case STACK_TYPE_I4:
break;
case STACK_TYPE_I8:
ADD_CODE(td, MINT_CONV_U4_I8);
break;
case STACK_TYPE_MP:
#if SIZEOF_VOID_P == 8
ADD_CODE(td, MINT_CONV_U4_I8);
#endif
break;
default:
g_assert_not_reached ();
}
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
case CEE_CONV_I4:
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_R4:
ADD_CODE (td, MINT_CONV_I4_R4);
break;
case STACK_TYPE_R8:
ADD_CODE(td, MINT_CONV_I4_R8);
break;
case STACK_TYPE_I4:
break;
case STACK_TYPE_I8:
ADD_CODE(td, MINT_CONV_I4_I8);
break;
case STACK_TYPE_MP:
#if SIZEOF_VOID_P == 8
ADD_CODE(td, MINT_CONV_I4_I8);
#endif
break;
default:
g_assert_not_reached ();
}
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
case CEE_CONV_I8:
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_R4:
ADD_CODE(td, MINT_CONV_I8_R4);
break;
case STACK_TYPE_R8:
ADD_CODE(td, MINT_CONV_I8_R8);
break;
case STACK_TYPE_I4:
ADD_CODE(td, MINT_CONV_I8_I4);
break;
case STACK_TYPE_I8:
break;
case STACK_TYPE_MP:
#if SIZEOF_VOID_P == 4
ADD_CODE(td, MINT_CONV_I8_I4);
#endif
break;
default:
g_assert_not_reached ();
}
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
break;
case CEE_CONV_R4:
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_R8:
ADD_CODE(td, MINT_CONV_R4_R8);
break;
case STACK_TYPE_I8:
ADD_CODE(td, MINT_CONV_R4_I8);
break;
case STACK_TYPE_I4:
ADD_CODE(td, MINT_CONV_R4_I4);
break;
case STACK_TYPE_R4:
/* no-op */
break;
default:
g_assert_not_reached ();
}
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R4);
break;
case CEE_CONV_R8:
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_I4:
ADD_CODE(td, MINT_CONV_R8_I4);
break;
case STACK_TYPE_I8:
ADD_CODE(td, MINT_CONV_R8_I8);
break;
case STACK_TYPE_R4:
ADD_CODE (td, MINT_CONV_R8_R4);
break;
case STACK_TYPE_R8:
break;
default:
g_assert_not_reached ();
}
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
break;
case CEE_CONV_U8:
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_I4:
ADD_CODE(td, MINT_CONV_U8_I4);
break;
case STACK_TYPE_I8:
break;
case STACK_TYPE_R4:
ADD_CODE (td, MINT_CONV_U8_R4);
break;
case STACK_TYPE_R8:
ADD_CODE(td, MINT_CONV_U8_R8);
break;
case STACK_TYPE_MP:
#if SIZEOF_VOID_P == 4
ADD_CODE(td, MINT_CONV_U8_I4);
#endif
break;
default:
g_assert_not_reached ();
}
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
break;
case CEE_CPOBJ: {
CHECK_STACK (td, 2);
token = read32 (td->ip + 1);
klass = mono_class_get_and_inflate_typespec_checked (image, token, generic_context, error);
goto_if_nok (error, exit);
if (m_class_is_valuetype (klass)) {
ADD_CODE (td, MINT_CPOBJ);
ADD_CODE (td, get_data_item_index(td, klass));
} else {
ADD_CODE (td, MINT_LDIND_REF);
ADD_CODE (td, MINT_STIND_REF);
}
td->ip += 5;
td->sp -= 2;
break;
}
case CEE_LDOBJ: {
int size;
CHECK_STACK (td, 1);
token = read32 (td->ip + 1);
if (method->wrapper_type != MONO_WRAPPER_NONE)
klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
else {
klass = mono_class_get_and_inflate_typespec_checked (image, token, generic_context, error);
goto_if_nok (error, exit);
}
MonoClass *tos_klass = td->sp [-1].klass;
if (tos_klass && td->sp [-1].type == STACK_TYPE_VT) {
int tos_size = mono_class_value_size (tos_klass, NULL);
POP_VT (td, tos_size);
}
ADD_CODE(td, MINT_LDOBJ);
ADD_CODE(td, get_data_item_index(td, klass));
int mt = mint_type (m_class_get_byval_arg (klass));
if (mt == MINT_TYPE_VT) {
size = mono_class_value_size (klass, NULL);
PUSH_VT (td, size);
}
td->ip += 5;
SET_TYPE (td->sp - 1, stack_type [mt], klass);
BARRIER_IF_VOLATILE (td);
break;
}
case CEE_LDSTR: {
token = mono_metadata_token_index (read32 (td->ip + 1));
td->ip += 5;
if (method->wrapper_type == MONO_WRAPPER_NONE) {
MonoString *s = mono_ldstr_checked (domain, image, token, error);
goto_if_nok (error, exit);
/* GC won't scan code stream, but reference is held by metadata
* machinery so we are good here */
ADD_CODE (td, MINT_LDSTR);
ADD_CODE (td, get_data_item_index (td, s));
} else {
/* defer allocation to execution-time */
ADD_CODE (td, MINT_LDSTR_TOKEN);
ADD_CODE (td, get_data_item_index (td, GUINT_TO_POINTER (token)));
}
PUSH_TYPE(td, STACK_TYPE_O, mono_defaults.string_class);
break;
}
case CEE_NEWOBJ: {
MonoMethod *m;
MonoMethodSignature *csignature;
guint32 vt_stack_used = 0;
guint32 vt_res_size = 0;
td->ip++;
token = read32 (td->ip);
td->ip += 4;
if (method->wrapper_type != MONO_WRAPPER_NONE)
m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
else {
m = mono_get_method_checked (image, token, NULL, generic_context, error);
goto_if_nok (error, exit);
}
csignature = mono_method_signature (m);
klass = m->klass;
if (!mono_class_init (klass)) {
mono_error_set_for_class_failure (error, klass);
goto_if_nok (error, exit);
}
if (mono_class_get_flags (klass) & TYPE_ATTRIBUTE_ABSTRACT) {
char* full_name = mono_type_get_full_name (klass);
mono_error_set_member_access (error, "Cannot create an abstract class: %s", full_name);
g_free (full_name);
goto_if_nok (error, exit);
}
td->sp -= csignature->param_count;
if (mono_class_is_magic_int (klass) || mono_class_is_magic_float (klass)) {
#if SIZEOF_VOID_P == 8
if (mono_class_is_magic_int (klass) && td->sp [0].type == STACK_TYPE_I4)
ADD_CODE (td, MINT_CONV_I8_I4);
else if (mono_class_is_magic_float (klass) && td->sp [0].type == STACK_TYPE_R4)
ADD_CODE (td, MINT_CONV_R8_R4);
#endif
ADD_CODE (td, MINT_NEWOBJ_MAGIC);
ADD_CODE (td, get_data_item_index (td, mono_interp_get_imethod (domain, m, error)));
goto_if_nok (error, exit);
PUSH_TYPE (td, stack_type [mint_type (m_class_get_byval_arg (klass))], klass);
} else {
if (m_class_get_parent (klass) == mono_defaults.array_class) {
ADD_CODE(td, MINT_NEWOBJ_ARRAY);
ADD_CODE(td, get_data_item_index (td, mono_interp_get_imethod (domain, m, error)));
ADD_CODE(td, csignature->param_count);
} else if (m_class_get_image (klass) == mono_defaults.corlib &&
!strcmp (m_class_get_name (m->klass), "ByReference`1") &&
!strcmp (m->name, ".ctor")) {
/* public ByReference(ref T value) */
g_assert (csignature->hasthis && csignature->param_count == 1);
ADD_CODE(td, MINT_INTRINS_BYREFERENCE_CTOR);
ADD_CODE(td, get_data_item_index (td, mono_interp_get_imethod (domain, m, error)));
} else if (klass != mono_defaults.string_class &&
!mono_class_is_marshalbyref (klass) &&
!mono_class_has_finalizer (klass) &&
!m_class_has_weak_fields (klass)) {
if (!m_class_is_valuetype (klass))
ADD_CODE(td, MINT_NEWOBJ_FAST);
else if (mint_type (m_class_get_byval_arg (klass)) == MINT_TYPE_VT)
ADD_CODE(td, MINT_NEWOBJ_VTST_FAST);
else
ADD_CODE(td, MINT_NEWOBJ_VT_FAST);
ADD_CODE(td, get_data_item_index (td, mono_interp_get_imethod (domain, m, error)));
ADD_CODE(td, csignature->param_count);
if (!m_class_is_valuetype (klass)) {
MonoVTable *vtable = mono_class_vtable_checked (domain, klass, error);
goto_if_nok (error, exit);
ADD_CODE(td, get_data_item_index (td, vtable));
}
} else {
ADD_CODE(td, MINT_NEWOBJ);
ADD_CODE(td, get_data_item_index (td, mono_interp_get_imethod (domain, m, error)));
}
goto_if_nok (error, exit);
if (mint_type (m_class_get_byval_arg (klass)) == MINT_TYPE_VT) {
vt_res_size = mono_class_value_size (klass, NULL);
PUSH_VT (td, vt_res_size);
}
for (i = 0; i < csignature->param_count; ++i) {
int mt = mint_type(csignature->params [i]);
if (mt == MINT_TYPE_VT) {
MonoClass *k = mono_class_from_mono_type (csignature->params [i]);
gint32 size = mono_class_value_size (k, NULL);
size = ALIGN_TO (size, MINT_VT_ALIGNMENT);
vt_stack_used += size;
}
}
if (vt_stack_used != 0 || vt_res_size != 0) {
ADD_CODE(td, MINT_VTRESULT);
ADD_CODE(td, vt_res_size);
WRITE32(td, &vt_stack_used);
td->vt_sp -= vt_stack_used;
}
PUSH_TYPE (td, stack_type [mint_type (m_class_get_byval_arg (klass))], klass);
}
break;
}
case CEE_CASTCLASS:
CHECK_STACK (td, 1);
token = read32 (td->ip + 1);
klass = mini_get_class (method, token, generic_context);
CHECK_TYPELOAD (klass);
ADD_CODE(td, MINT_CASTCLASS);
ADD_CODE(td, get_data_item_index (td, klass));
td->sp [-1].klass = klass;
td->ip += 5;
break;
case CEE_ISINST:
CHECK_STACK (td, 1);
token = read32 (td->ip + 1);
klass = mini_get_class (method, token, generic_context);
CHECK_TYPELOAD (klass);
ADD_CODE(td, MINT_ISINST);
ADD_CODE(td, get_data_item_index (td, klass));
td->ip += 5;
break;
case CEE_CONV_R_UN:
switch (td->sp [-1].type) {
case STACK_TYPE_R8:
break;
case STACK_TYPE_I8:
ADD_CODE(td, MINT_CONV_R_UN_I8);
break;
case STACK_TYPE_I4:
ADD_CODE(td, MINT_CONV_R_UN_I4);
break;
default:
g_assert_not_reached ();
}
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
++td->ip;
break;
case CEE_UNBOX:
CHECK_STACK (td, 1);
token = read32 (td->ip + 1);
if (method->wrapper_type != MONO_WRAPPER_NONE)
klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
else {
klass = mono_class_get_and_inflate_typespec_checked (image, token, generic_context, error);
goto_if_nok (error, exit);
}
if (mono_class_is_nullable (klass)) {
MonoMethod *target_method;
if (m_class_is_enumtype (mono_class_get_nullable_param (klass)))
target_method = mono_class_get_method_from_name (klass, "UnboxExact", 1);
else
target_method = mono_class_get_method_from_name (klass, "Unbox", 1);
/* td->ip is incremented by interp_transform_call */
interp_transform_call (td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE, error, FALSE);
goto_if_nok (error, exit);
/*
* CEE_UNBOX needs to push address of vtype while Nullable.Unbox returns the value type
* We create a local variable in the frame so that we can fetch its address.
*/
int local_offset = create_interp_local (td, m_class_get_byval_arg (klass));
store_local_general (td, local_offset, m_class_get_byval_arg (klass));
ADD_CODE (td, MINT_LDLOCA_S);
ADD_CODE (td, local_offset);
PUSH_SIMPLE_TYPE (td, STACK_TYPE_MP);
} else {
ADD_CODE (td, MINT_UNBOX);
ADD_CODE (td, get_data_item_index (td, klass));
SET_SIMPLE_TYPE (td->sp - 1, STACK_TYPE_MP);
td->ip += 5;
}
break;
case CEE_UNBOX_ANY:
CHECK_STACK (td, 1);
token = read32 (td->ip + 1);
klass = mini_get_class (method, token, generic_context);
CHECK_TYPELOAD (klass);
if (mini_type_is_reference (m_class_get_byval_arg (klass))) {
int mt = mint_type (m_class_get_byval_arg (klass));
ADD_CODE (td, MINT_CASTCLASS);
ADD_CODE (td, get_data_item_index (td, klass));
SET_TYPE (td->sp - 1, stack_type [mt], klass);
td->ip += 5;
} else if (mono_class_is_nullable (klass)) {
MonoMethod *target_method;
if (m_class_is_enumtype (mono_class_get_nullable_param (klass)))
target_method = mono_class_get_method_from_name (klass, "UnboxExact", 1);
else
target_method = mono_class_get_method_from_name (klass, "Unbox", 1);
/* td->ip is incremented by interp_transform_call */
interp_transform_call (td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE, error, FALSE);
goto_if_nok (error, exit);
} else {
int mt = mint_type (m_class_get_byval_arg (klass));
ADD_CODE (td, MINT_UNBOX);
ADD_CODE (td, get_data_item_index (td, klass));
ADD_CODE (td, MINT_LDOBJ);
ADD_CODE (td, get_data_item_index(td, klass));
SET_TYPE (td->sp - 1, stack_type [mt], klass);
if (mt == MINT_TYPE_VT) {
int size = mono_class_value_size (klass, NULL);
PUSH_VT (td, size);
}
td->ip += 5;
}
break;
case CEE_THROW:
CHECK_STACK (td, 1);
SIMPLE_OP (td, MINT_THROW);
td->sp = td->stack;
break;
case CEE_LDFLDA: {
CHECK_STACK (td, 1);
token = read32 (td->ip + 1);
field = interp_field_from_token (method, token, &klass, generic_context, error);
goto_if_nok (error, exit);
MonoType *ftype = mono_field_get_type (field);
gboolean is_static = !!(ftype->attrs & FIELD_ATTRIBUTE_STATIC);
mono_class_init (klass);
#ifndef DISABLE_REMOTING
if (m_class_get_marshalbyref (klass) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class) {
g_assert (!is_static);
int offset = m_class_is_valuetype (klass) ? field->offset - sizeof (MonoObject) : field->offset;
ADD_CODE (td, MINT_MONO_LDPTR);
ADD_CODE (td, get_data_item_index (td, klass));
PUSH_SIMPLE_TYPE (td, STACK_TYPE_I);
ADD_CODE (td, MINT_MONO_LDPTR);
ADD_CODE (td, get_data_item_index (td, field));
PUSH_SIMPLE_TYPE (td, STACK_TYPE_I);
ADD_CODE (td, MINT_LDC_I4);
WRITE32 (td, &offset);
PUSH_SIMPLE_TYPE (td, STACK_TYPE_I4);
#if SIZEOF_VOID_P == 8
ADD_CODE(td, MINT_CONV_I8_I4);
#endif
MonoMethod *wrapper = mono_marshal_get_ldflda_wrapper (field->type);
/* td->ip is incremented by interp_transform_call */
interp_transform_call (td, method, wrapper, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE, error, FALSE);
goto_if_nok (error, exit);
} else
#endif
{
if (is_static) {
ADD_CODE (td, MINT_POP);
ADD_CODE (td, 0);
ADD_CODE (td, MINT_LDSFLDA);
ADD_CODE (td, get_data_item_index (td, field));
} else {
if ((td->sp - 1)->type == STACK_TYPE_O) {
ADD_CODE (td, MINT_LDFLDA);
} else {
g_assert ((td->sp -1)->type == STACK_TYPE_MP);
ADD_CODE (td, MINT_LDFLDA_UNSAFE);
}
ADD_CODE (td, m_class_is_valuetype (klass) ? field->offset - sizeof (MonoObject) : field->offset);
}
td->ip += 5;
}
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
break;
}
case CEE_LDFLD: {
CHECK_STACK (td, 1);
token = read32 (td->ip + 1);
field = interp_field_from_token (method, token, &klass, generic_context, error);
goto_if_nok (error, exit);
MonoType *ftype = mono_field_get_type (field);
gboolean is_static = !!(ftype->attrs & FIELD_ATTRIBUTE_STATIC);
mono_class_init (klass);
MonoClass *field_klass = mono_class_from_mono_type (ftype);
mt = mint_type (m_class_get_byval_arg (field_klass));
#ifndef DISABLE_REMOTING
if (m_class_get_marshalbyref (klass)) {
g_assert (!is_static);
ADD_CODE(td, mt == MINT_TYPE_VT ? MINT_LDRMFLD_VT : MINT_LDRMFLD);
ADD_CODE(td, get_data_item_index (td, field));
} else
#endif
{
if (is_static) {
ADD_CODE (td, MINT_POP);
ADD_CODE (td, 0);
ADD_CODE (td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
ADD_CODE (td, get_data_item_index (td, field));
} else {
ADD_CODE (td, MINT_LDFLD_I1 + mt - MINT_TYPE_I1);
ADD_CODE (td, m_class_is_valuetype (klass) ? field->offset - sizeof(MonoObject) : field->offset);
if (mt == MINT_TYPE_VT)
ADD_CODE (td, get_data_item_index (td, field));
}
}
if (mt == MINT_TYPE_VT) {
int size = mono_class_value_size (field_klass, NULL);
PUSH_VT (td, size);
}
if (td->sp [-1].type == STACK_TYPE_VT) {
int size = mono_class_value_size (klass, NULL);
size = ALIGN_TO (size, MINT_VT_ALIGNMENT);
int field_vt_size = 0;
if (mt == MINT_TYPE_VT) {
/*
* Pop the loaded field from the vtstack (it will still be present
* at the same vtstack address) and we will load it in place of the
* containing valuetype with the second MINT_VTRESULT.
*/
field_vt_size = mono_class_value_size (field_klass, NULL);
field_vt_size = ALIGN_TO (field_vt_size, MINT_VT_ALIGNMENT);
ADD_CODE (td, MINT_VTRESULT);
ADD_CODE (td, 0);
WRITE32 (td, &field_vt_size);
}
td->vt_sp -= size;
ADD_CODE (td, MINT_VTRESULT);
ADD_CODE (td, field_vt_size);
WRITE32 (td, &size);
}
td->ip += 5;
SET_TYPE (td->sp - 1, stack_type [mt], field_klass);
BARRIER_IF_VOLATILE (td);
break;
}
case CEE_STFLD: {
CHECK_STACK (td, 2);
token = read32 (td->ip + 1);
field = interp_field_from_token (method, token, &klass, generic_context, error);
goto_if_nok (error, exit);
MonoType *ftype = mono_field_get_type (field);
gboolean is_static = !!(ftype->attrs & FIELD_ATTRIBUTE_STATIC);
mono_class_init (klass);
mt = mint_type (ftype);
BARRIER_IF_VOLATILE (td);
#ifndef DISABLE_REMOTING
if (m_class_get_marshalbyref (klass)) {
g_assert (!is_static);
ADD_CODE(td, mt == MINT_TYPE_VT ? MINT_STRMFLD_VT : MINT_STRMFLD);
ADD_CODE(td, get_data_item_index (td, field));
} else
#endif
{
if (is_static) {
ADD_CODE (td, MINT_POP);
ADD_CODE (td, 1);
ADD_CODE (td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
ADD_CODE (td, get_data_item_index (td, field));
/* the vtable of the field might not be initialized at this point */
MonoClass *fld_klass = mono_class_from_mono_type (field->type);
mono_class_vtable_checked (domain, fld_klass, error);
goto_if_nok (error, exit);
} else {
ADD_CODE (td, MINT_STFLD_I1 + mt - MINT_TYPE_I1);
ADD_CODE (td, m_class_is_valuetype (klass) ? field->offset - sizeof(MonoObject) : field->offset);
if (mt == MINT_TYPE_VT) {
ADD_CODE (td, get_data_item_index (td, field));
/* the vtable of the field might not be initialized at this point */
MonoClass *fld_klass = mono_class_from_mono_type (field->type);
mono_class_vtable_checked (domain, fld_klass, error);
goto_if_nok (error, exit);
}
}
}
if (mt == MINT_TYPE_VT) {
MonoClass *klass = mono_class_from_mono_type (ftype);
int size = mono_class_value_size (klass, NULL);
POP_VT (td, size);
}
td->ip += 5;
td->sp -= 2;
break;
}
case CEE_LDSFLDA: {
token = read32 (td->ip + 1);
field = interp_field_from_token (method, token, &klass, generic_context, error);
goto_if_nok (error, exit);
mono_field_get_type (field);
ADD_CODE(td, MINT_LDSFLDA);
ADD_CODE(td, get_data_item_index (td, field));
td->ip += 5;
PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
break;
}
case CEE_LDSFLD: {
token = read32 (td->ip + 1);
field = interp_field_from_token (method, token, &klass, generic_context, error);
goto_if_nok (error, exit);
MonoType *ftype = mono_field_get_type (field);
mt = mint_type (ftype);
ADD_CODE(td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
ADD_CODE(td, get_data_item_index (td, field));
klass = NULL;
if (mt == MINT_TYPE_VT) {
klass = mono_class_from_mono_type (ftype);
int size = mono_class_value_size (klass, NULL);
PUSH_VT(td, size);
WRITE32(td, &size);
} else {
if (mt == MINT_TYPE_O)
klass = mono_class_from_mono_type (ftype);
}
td->ip += 5;
PUSH_TYPE(td, stack_type [mt], klass);
break;
}
case CEE_STSFLD:
CHECK_STACK (td, 1);
token = read32 (td->ip + 1);
field = interp_field_from_token (method, token, &klass, generic_context, error);
goto_if_nok (error, exit);
MonoType *ftype = mono_field_get_type (field);
mt = mint_type (ftype);
ADD_CODE(td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
ADD_CODE(td, get_data_item_index (td, field));
/* the vtable of the field might not be initialized at this point */
MonoClass *fld_klass = mono_class_from_mono_type (field->type);
mono_class_vtable_checked (domain, fld_klass, error);
goto_if_nok (error, exit);
if (mt == MINT_TYPE_VT) {
MonoClass *klass = mono_class_from_mono_type (ftype);
int size = mono_class_value_size (klass, NULL);
POP_VT (td, size);
}
td->ip += 5;
--td->sp;
break;
case CEE_STOBJ: {
int size;
token = read32 (td->ip + 1);
if (method->wrapper_type != MONO_WRAPPER_NONE)
klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
else
klass = mini_get_class (method, token, generic_context);
CHECK_TYPELOAD (klass);
BARRIER_IF_VOLATILE (td);
ADD_CODE(td, td->sp [-1].type == STACK_TYPE_VT ? MINT_STOBJ_VT : MINT_STOBJ);
ADD_CODE(td, get_data_item_index (td, klass));
if (td->sp [-1].type == STACK_TYPE_VT) {
size = mono_class_value_size (klass, NULL);
POP_VT (td, size);
}
td->ip += 5;
td->sp -= 2;
break;
}
case CEE_CONV_OVF_I_UN:
case CEE_CONV_OVF_U_UN:
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_R8:
#if SIZEOF_VOID_P == 8
ADD_CODE(td, MINT_CONV_OVF_I8_UN_R8);
#else
ADD_CODE(td, MINT_CONV_OVF_I4_UN_R8);
#endif
break;
case STACK_TYPE_I8:
#if SIZEOF_VOID_P == 4
ADD_CODE (td, MINT_CONV_OVF_I4_UN_I8);
#endif
break;
case STACK_TYPE_I4:
#if SIZEOF_VOID_P == 8
ADD_CODE(td, MINT_CONV_I8_U4);
#elif SIZEOF_VOID_P == 4
if (*td->ip == CEE_CONV_OVF_I_UN)
ADD_CODE(td, MINT_CONV_OVF_I4_U4);
#endif
break;
default:
g_assert_not_reached ();
break;
}
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
++td->ip;
break;
case CEE_CONV_OVF_I8_UN:
case CEE_CONV_OVF_U8_UN:
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_R8:
ADD_CODE(td, MINT_CONV_OVF_I8_UN_R8);
break;
case STACK_TYPE_I8:
if (*td->ip == CEE_CONV_OVF_I8_UN)
ADD_CODE (td, MINT_CONV_OVF_I8_U8);
break;
case STACK_TYPE_I4:
ADD_CODE(td, MINT_CONV_I8_U4);
break;
default:
g_assert_not_reached ();
break;
}
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
++td->ip;
break;
case CEE_BOX: {
int size;
CHECK_STACK (td, 1);
token = read32 (td->ip + 1);
if (method->wrapper_type != MONO_WRAPPER_NONE)
klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
else
klass = mini_get_class (method, token, generic_context);
CHECK_TYPELOAD (klass);
if (mono_class_is_nullable (klass)) {
MonoMethod *target_method = mono_class_get_method_from_name (klass, "Box", 1);
/* td->ip is incremented by interp_transform_call */
interp_transform_call (td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE, error, FALSE);
goto_if_nok (error, exit);
} else if (!m_class_is_valuetype (klass)) {
/* already boxed, do nothing. */
td->ip += 5;
} else {
if (mint_type (m_class_get_byval_arg (klass)) == MINT_TYPE_VT && !m_class_is_enumtype (klass)) {
size = mono_class_value_size (klass, NULL);
size = ALIGN_TO (size, MINT_VT_ALIGNMENT);
td->vt_sp -= size;
} else if (td->sp [-1].type == STACK_TYPE_R8 && m_class_get_byval_arg (klass)->type == MONO_TYPE_R4) {
ADD_CODE (td, MINT_CONV_R4_R8);
}
ADD_CODE(td, MINT_BOX);
ADD_CODE(td, get_data_item_index (td, klass));
ADD_CODE (td, 0);
SET_TYPE(td->sp - 1, STACK_TYPE_O, klass);
td->ip += 5;
}
break;
}
case CEE_NEWARR: {
CHECK_STACK (td, 1);
token = read32 (td->ip + 1);
if (method->wrapper_type != MONO_WRAPPER_NONE)
klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
else
klass = mini_get_class (method, token, generic_context);
CHECK_TYPELOAD (klass);
unsigned char lentype = (td->sp - 1)->type;
if (lentype == STACK_TYPE_I8) {
/* mimic mini behaviour */
ADD_CODE (td, MINT_CONV_OVF_U4_I8);
} else {
g_assert (lentype == STACK_TYPE_I4);
ADD_CODE (td, MINT_CONV_OVF_U4_I4);
}
SET_SIMPLE_TYPE (td->sp - 1, STACK_TYPE_I4);
ADD_CODE (td, MINT_NEWARR);
ADD_CODE (td, get_data_item_index (td, klass));
SET_TYPE (td->sp - 1, STACK_TYPE_O, klass);
td->ip += 5;
break;
}
case CEE_LDLEN:
CHECK_STACK (td, 1);
SIMPLE_OP (td, MINT_LDLEN);
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
break;
case CEE_LDELEMA:
CHECK_STACK (td, 2);
ENSURE_I4 (td, 1);
token = read32 (td->ip + 1);
if (method->wrapper_type != MONO_WRAPPER_NONE)
klass = (MonoClass *) mono_method_get_wrapper_data (method, token);
else
klass = mini_get_class (method, token, generic_context);
CHECK_TYPELOAD (klass);
if (!m_class_is_valuetype (klass) && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
/*
* Check the class for failures before the type check, which can
* throw other exceptions.
*/
mono_class_setup_vtable (klass);
CHECK_TYPELOAD (klass);
ADD_CODE (td, MINT_LDELEMA_TC);
} else {
ADD_CODE (td, MINT_LDELEMA);
}
ADD_CODE (td, get_data_item_index (td, klass));
/* according to spec, ldelema bytecode is only used for 1-dim arrays */
ADD_CODE (td, 2);
readonly = FALSE;
td->ip += 5;
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
break;
case CEE_LDELEM_I1:
CHECK_STACK (td, 2);
ENSURE_I4 (td, 1);
SIMPLE_OP (td, MINT_LDELEM_I1);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
case CEE_LDELEM_U1:
CHECK_STACK (td, 2);
ENSURE_I4 (td, 1);
SIMPLE_OP (td, MINT_LDELEM_U1);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
case CEE_LDELEM_I2:
CHECK_STACK (td, 2);
ENSURE_I4 (td, 1);
SIMPLE_OP (td, MINT_LDELEM_I2);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
case CEE_LDELEM_U2:
CHECK_STACK (td, 2);
ENSURE_I4 (td, 1);
SIMPLE_OP (td, MINT_LDELEM_U2);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
case CEE_LDELEM_I4:
CHECK_STACK (td, 2);
ENSURE_I4 (td, 1);
SIMPLE_OP (td, MINT_LDELEM_I4);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
case CEE_LDELEM_U4:
CHECK_STACK (td, 2);
ENSURE_I4 (td, 1);
SIMPLE_OP (td, MINT_LDELEM_U4);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
case CEE_LDELEM_I8:
CHECK_STACK (td, 2);
ENSURE_I4 (td, 1);
SIMPLE_OP (td, MINT_LDELEM_I8);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
break;
case CEE_LDELEM_I:
CHECK_STACK (td, 2);
ENSURE_I4 (td, 1);
SIMPLE_OP (td, MINT_LDELEM_I);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I);
break;
case CEE_LDELEM_R4:
CHECK_STACK (td, 2);
ENSURE_I4 (td, 1);
SIMPLE_OP (td, MINT_LDELEM_R4);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R4);
break;
case CEE_LDELEM_R8:
CHECK_STACK (td, 2);
ENSURE_I4 (td, 1);
SIMPLE_OP (td, MINT_LDELEM_R8);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
break;
case CEE_LDELEM_REF:
CHECK_STACK (td, 2);
ENSURE_I4 (td, 1);
SIMPLE_OP (td, MINT_LDELEM_REF);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_O);
break;
case CEE_LDELEM:
CHECK_STACK (td, 2);
token = read32 (td->ip + 1);
klass = mini_get_class (method, token, generic_context);
CHECK_TYPELOAD (klass);
switch (mint_type (m_class_get_byval_arg (klass))) {
case MINT_TYPE_I1:
ENSURE_I4 (td, 1);
SIMPLE_OP (td, MINT_LDELEM_I1);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
case MINT_TYPE_U1:
ENSURE_I4 (td, 1);
SIMPLE_OP (td, MINT_LDELEM_U1);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
case MINT_TYPE_U2:
ENSURE_I4 (td, 1);
SIMPLE_OP (td, MINT_LDELEM_U2);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
case MINT_TYPE_I2:
ENSURE_I4 (td, 1);
SIMPLE_OP (td, MINT_LDELEM_I2);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
case MINT_TYPE_I4:
ENSURE_I4 (td, 1);
SIMPLE_OP (td, MINT_LDELEM_I4);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
case MINT_TYPE_I8:
ENSURE_I4 (td, 1);
SIMPLE_OP (td, MINT_LDELEM_I8);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
break;
case MINT_TYPE_R4:
ENSURE_I4 (td, 1);
SIMPLE_OP (td, MINT_LDELEM_R4);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R4);
break;
case MINT_TYPE_R8:
ENSURE_I4 (td, 1);
SIMPLE_OP (td, MINT_LDELEM_R8);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_R8);
break;
case MINT_TYPE_O:
ENSURE_I4 (td, 1);
SIMPLE_OP (td, MINT_LDELEM_REF);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_O);
break;
case MINT_TYPE_VT: {
int size = mono_class_value_size (klass, NULL);
ENSURE_I4 (td, 1);
SIMPLE_OP (td, MINT_LDELEM_VT);
ADD_CODE (td, get_data_item_index (td, klass));
WRITE32 (td, &size);
--td->sp;
SET_TYPE (td->sp - 1, STACK_TYPE_VT, klass);
PUSH_VT (td, size);
break;
}
default: {
GString *res = g_string_new ("");
mono_type_get_desc (res, m_class_get_byval_arg (klass), TRUE);
g_print ("LDELEM: %s -> %d (%s)\n", m_class_get_name (klass), mint_type (m_class_get_byval_arg (klass)), res->str);
g_string_free (res, TRUE);
g_assert (0);
break;
}
}
td->ip += 4;
break;
case CEE_STELEM_I:
CHECK_STACK (td, 3);
ENSURE_I4 (td, 2);
SIMPLE_OP (td, MINT_STELEM_I);
td->sp -= 3;
break;
case CEE_STELEM_I1:
CHECK_STACK (td, 3);
ENSURE_I4 (td, 2);
SIMPLE_OP (td, MINT_STELEM_I1);
td->sp -= 3;
break;
case CEE_STELEM_I2:
CHECK_STACK (td, 3);
ENSURE_I4 (td, 2);
SIMPLE_OP (td, MINT_STELEM_I2);
td->sp -= 3;
break;
case CEE_STELEM_I4:
CHECK_STACK (td, 3);
ENSURE_I4 (td, 2);
SIMPLE_OP (td, MINT_STELEM_I4);
td->sp -= 3;
break;
case CEE_STELEM_I8:
CHECK_STACK (td, 3);
ENSURE_I4 (td, 2);
SIMPLE_OP (td, MINT_STELEM_I8);
td->sp -= 3;
break;
case CEE_STELEM_R4:
CHECK_STACK (td, 3);
ENSURE_I4 (td, 2);
SIMPLE_OP (td, MINT_STELEM_R4);
td->sp -= 3;
break;
case CEE_STELEM_R8:
CHECK_STACK (td, 3);
ENSURE_I4 (td, 2);
SIMPLE_OP (td, MINT_STELEM_R8);
td->sp -= 3;
break;
case CEE_STELEM_REF:
CHECK_STACK (td, 3);
ENSURE_I4 (td, 2);
SIMPLE_OP (td, MINT_STELEM_REF);
td->sp -= 3;
break;
case CEE_STELEM:
CHECK_STACK (td, 3);
ENSURE_I4 (td, 2);
token = read32 (td->ip + 1);
klass = mini_get_class (method, token, generic_context);
CHECK_TYPELOAD (klass);
switch (mint_type (m_class_get_byval_arg (klass))) {
case MINT_TYPE_I1:
SIMPLE_OP (td, MINT_STELEM_I1);
break;
case MINT_TYPE_U1:
SIMPLE_OP (td, MINT_STELEM_U1);
break;
case MINT_TYPE_I2:
SIMPLE_OP (td, MINT_STELEM_I2);
break;
case MINT_TYPE_U2:
SIMPLE_OP (td, MINT_STELEM_U2);
break;
case MINT_TYPE_I4:
SIMPLE_OP (td, MINT_STELEM_I4);
break;
case MINT_TYPE_I8:
SIMPLE_OP (td, MINT_STELEM_I8);
break;
case MINT_TYPE_R4:
SIMPLE_OP (td, MINT_STELEM_R4);
break;
case MINT_TYPE_R8:
SIMPLE_OP (td, MINT_STELEM_R8);
break;
case MINT_TYPE_O:
SIMPLE_OP (td, MINT_STELEM_REF);
break;
case MINT_TYPE_VT: {
int size = mono_class_value_size (klass, NULL);
SIMPLE_OP (td, MINT_STELEM_VT);
ADD_CODE (td, get_data_item_index (td, klass));
WRITE32 (td, &size);
POP_VT (td, size);
break;
}
default: {
GString *res = g_string_new ("");
mono_type_get_desc (res, m_class_get_byval_arg (klass), TRUE);
g_print ("STELEM: %s -> %d (%s)\n", m_class_get_name (klass), mint_type (m_class_get_byval_arg (klass)), res->str);
g_string_free (res, TRUE);
g_assert (0);
break;
}
}
td->ip += 4;
td->sp -= 3;
break;
#if 0
case CEE_CONV_OVF_U1:
case CEE_CONV_OVF_I8:
#if SIZEOF_VOID_P == 8
case CEE_CONV_OVF_U:
#endif
#endif
case CEE_CKFINITE:
CHECK_STACK (td, 1);
SIMPLE_OP (td, MINT_CKFINITE);
break;
case CEE_MKREFANY:
CHECK_STACK (td, 1);
token = read32 (td->ip + 1);
klass = mini_get_class (method, token, generic_context);
CHECK_TYPELOAD (klass);
ADD_CODE (td, MINT_MKREFANY);
ADD_CODE (td, get_data_item_index (td, klass));
td->ip += 5;
PUSH_VT (td, sizeof (MonoTypedRef));
SET_TYPE(td->sp - 1, STACK_TYPE_VT, mono_defaults.typed_reference_class);
break;
case CEE_REFANYVAL: {
CHECK_STACK (td, 1);
token = read32 (td->ip + 1);
klass = mini_get_class (method, token, generic_context);
CHECK_TYPELOAD (klass);
ADD_CODE (td, MINT_REFANYVAL);
ADD_CODE (td, get_data_item_index (td, klass));
POP_VT (td, sizeof (MonoTypedRef));
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
td->ip += 5;
break;
}
case CEE_CONV_OVF_I1:
case CEE_CONV_OVF_I1_UN: {
gboolean is_un = *td->ip == CEE_CONV_OVF_I1_UN;
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_R8:
ADD_CODE(td, is_un ? MINT_CONV_OVF_I1_UN_R8 : MINT_CONV_OVF_I1_R8);
break;
case STACK_TYPE_I4:
ADD_CODE(td, is_un ? MINT_CONV_OVF_I1_U4 : MINT_CONV_OVF_I1_I4);
break;
case STACK_TYPE_I8:
ADD_CODE(td, is_un ? MINT_CONV_OVF_I1_U8 : MINT_CONV_OVF_I1_I8);
break;
default:
g_assert_not_reached ();
}
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
}
case CEE_CONV_OVF_U1:
case CEE_CONV_OVF_U1_UN:
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_R8:
ADD_CODE(td, MINT_CONV_OVF_U1_R8);
break;
case STACK_TYPE_I4:
ADD_CODE(td, MINT_CONV_OVF_U1_I4);
break;
case STACK_TYPE_I8:
ADD_CODE(td, MINT_CONV_OVF_U1_I8);
break;
default:
g_assert_not_reached ();
}
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
case CEE_CONV_OVF_I2:
case CEE_CONV_OVF_I2_UN: {
gboolean is_un = *td->ip == CEE_CONV_OVF_I2_UN;
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_R8:
ADD_CODE(td, is_un ? MINT_CONV_OVF_I2_UN_R8 : MINT_CONV_OVF_I2_R8);
break;
case STACK_TYPE_I4:
ADD_CODE(td, is_un ? MINT_CONV_OVF_I2_U4 : MINT_CONV_OVF_I2_I4);
break;
case STACK_TYPE_I8:
ADD_CODE(td, is_un ? MINT_CONV_OVF_I2_U8 : MINT_CONV_OVF_I2_I8);
break;
default:
g_assert_not_reached ();
}
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
}
case CEE_CONV_OVF_U2_UN:
case CEE_CONV_OVF_U2:
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_R8:
ADD_CODE(td, MINT_CONV_OVF_U2_R8);
break;
case STACK_TYPE_I4:
ADD_CODE(td, MINT_CONV_OVF_U2_I4);
break;
case STACK_TYPE_I8:
ADD_CODE(td, MINT_CONV_OVF_U2_I8);
break;
default:
g_assert_not_reached ();
}
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
#if SIZEOF_VOID_P == 4
case CEE_CONV_OVF_I:
#endif
case CEE_CONV_OVF_I4:
case CEE_CONV_OVF_I4_UN:
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_R4:
ADD_CODE(td, MINT_CONV_OVF_I4_R4);
break;
case STACK_TYPE_R8:
ADD_CODE(td, MINT_CONV_OVF_I4_R8);
break;
case STACK_TYPE_I4:
if (*td->ip == CEE_CONV_OVF_I4_UN)
ADD_CODE(td, MINT_CONV_OVF_I4_U4);
break;
case STACK_TYPE_I8:
if (*td->ip == CEE_CONV_OVF_I4_UN)
ADD_CODE (td, MINT_CONV_OVF_I4_U8);
else
ADD_CODE (td, MINT_CONV_OVF_I4_I8);
break;
default:
g_assert_not_reached ();
}
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
#if SIZEOF_VOID_P == 4
case CEE_CONV_OVF_U:
#endif
case CEE_CONV_OVF_U4:
case CEE_CONV_OVF_U4_UN:
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_R4:
ADD_CODE(td, MINT_CONV_OVF_U4_R4);
break;
case STACK_TYPE_R8:
ADD_CODE(td, MINT_CONV_OVF_U4_R8);
break;
case STACK_TYPE_I4:
if (*td->ip != CEE_CONV_OVF_U4_UN)
ADD_CODE(td, MINT_CONV_OVF_U4_I4);
break;
case STACK_TYPE_I8:
ADD_CODE(td, MINT_CONV_OVF_U4_I8);
break;
default:
g_assert_not_reached ();
}
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
break;
#if SIZEOF_VOID_P == 8
case CEE_CONV_OVF_I:
#endif
case CEE_CONV_OVF_I8:
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_R4:
ADD_CODE(td, MINT_CONV_OVF_I8_R4);
break;
case STACK_TYPE_R8:
ADD_CODE(td, MINT_CONV_OVF_I8_R8);
break;
case STACK_TYPE_I4:
ADD_CODE(td, MINT_CONV_I8_I4);
break;
case STACK_TYPE_I8:
break;
default:
g_assert_not_reached ();
}
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
break;
#if SIZEOF_VOID_P == 8
case CEE_CONV_OVF_U:
#endif
case CEE_CONV_OVF_U8:
CHECK_STACK (td, 1);
switch (td->sp [-1].type) {
case STACK_TYPE_R4:
ADD_CODE(td, MINT_CONV_OVF_U8_R4);
break;
case STACK_TYPE_R8:
ADD_CODE(td, MINT_CONV_OVF_U8_R8);
break;
case STACK_TYPE_I4:
ADD_CODE(td, MINT_CONV_OVF_U8_I4);
break;
case STACK_TYPE_I8:
ADD_CODE (td, MINT_CONV_OVF_U8_I8);
break;
default:
g_assert_not_reached ();
}
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I8);
break;
case CEE_LDTOKEN: {
int size;
gpointer handle;
token = read32 (td->ip + 1);
if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
handle = mono_method_get_wrapper_data (method, token);
klass = (MonoClass *) mono_method_get_wrapper_data (method, token + 1);
if (klass == mono_defaults.typehandle_class)
handle = m_class_get_byval_arg ((MonoClass *) handle);
if (generic_context) {
handle = mono_class_inflate_generic_type_checked (handle, generic_context, error);
goto_if_nok (error, exit);
}
} else {
handle = mono_ldtoken_checked (image, token, &klass, generic_context, error);
goto_if_nok (error, exit);
}
mono_class_init (klass);
mt = mint_type (m_class_get_byval_arg (klass));
g_assert (mt == MINT_TYPE_VT);
size = mono_class_value_size (klass, NULL);
g_assert (size == sizeof(gpointer));
PUSH_VT (td, sizeof(gpointer));
ADD_CODE (td, MINT_LDTOKEN);
ADD_CODE (td, get_data_item_index (td, handle));
SET_TYPE (td->sp, stack_type [mt], klass);
td->sp++;
td->ip += 5;
break;
}
case CEE_ADD_OVF:
binary_arith_op(td, MINT_ADD_OVF_I4);
++td->ip;
break;
case CEE_ADD_OVF_UN:
binary_arith_op(td, MINT_ADD_OVF_UN_I4);
++td->ip;
break;
case CEE_MUL_OVF:
binary_arith_op(td, MINT_MUL_OVF_I4);
++td->ip;
break;
case CEE_MUL_OVF_UN:
binary_arith_op(td, MINT_MUL_OVF_UN_I4);
++td->ip;
break;
case CEE_SUB_OVF:
binary_arith_op(td, MINT_SUB_OVF_I4);
++td->ip;
break;
case CEE_SUB_OVF_UN:
binary_arith_op(td, MINT_SUB_OVF_UN_I4);
++td->ip;
break;
case CEE_ENDFINALLY: {
g_assert (td->clause_indexes [in_offset] != -1);
MonoExceptionClause *clause = &header->clauses [td->clause_indexes [in_offset]];
if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
ADD_CODE (td, MINT_END_ABORT_PROT);
td->sp = td->stack;
SIMPLE_OP (td, MINT_ENDFINALLY);
ADD_CODE (td, td->clause_indexes [in_offset]);
break;
}
case CEE_LEAVE:
case CEE_LEAVE_S: {
int offset;
if (*td->ip == CEE_LEAVE)
offset = 5 + read32 (td->ip + 1);
else
offset = 2 + (gint8)td->ip [1];
td->sp = td->stack;
if (td->clause_indexes [in_offset] != -1) {
/* LEAVE instructions in catch clauses need to check for abort exceptions */
handle_branch (td, MINT_LEAVE_S_CHECK, MINT_LEAVE_CHECK, offset);
} else {
handle_branch (td, MINT_LEAVE_S, MINT_LEAVE, offset);
}
if (*td->ip == CEE_LEAVE)
td->ip += 5;
else
td->ip += 2;
break;
}
case MONO_CUSTOM_PREFIX:
++td->ip;
switch (*td->ip) {
case CEE_MONO_LD_DELEGATE_METHOD_PTR:
--td->sp;
td->ip += 1;
ADD_CODE (td, MINT_LD_DELEGATE_METHOD_PTR);
PUSH_SIMPLE_TYPE (td, STACK_TYPE_I);
break;
case CEE_MONO_CALLI_EXTRA_ARG:
/* Same as CEE_CALLI, except that we drop the extra arg required for llvm specific behaviour */
ADD_CODE (td, MINT_POP);
ADD_CODE (td, 1);
--td->sp;
interp_transform_call (td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE, error, FALSE);
goto_if_nok (error, exit);
break;
case CEE_MONO_JIT_ICALL_ADDR: {
guint32 token;
gpointer func;
MonoJitICallInfo *info;
token = read32 (td->ip + 1);
td->ip += 5;
func = mono_method_get_wrapper_data (method, token);
info = mono_find_jit_icall_by_addr (func);
ADD_CODE (td, MINT_LDFTN);
ADD_CODE (td, get_data_item_index (td, func));
PUSH_SIMPLE_TYPE (td, STACK_TYPE_I);
break;
}
case CEE_MONO_ICALL: {
guint32 token;
gpointer func;
MonoJitICallInfo *info;
token = read32 (td->ip + 1);
td->ip += 5;
func = mono_method_get_wrapper_data (method, token);
info = mono_find_jit_icall_by_addr (func);
g_assert (info);
CHECK_STACK (td, info->sig->param_count);
switch (info->sig->param_count) {
case 0:
if (MONO_TYPE_IS_VOID (info->sig->ret))
ADD_CODE (td,MINT_ICALL_V_V);
else
ADD_CODE (td, MINT_ICALL_V_P);
break;
case 1:
if (MONO_TYPE_IS_VOID (info->sig->ret))
ADD_CODE (td,MINT_ICALL_P_V);
else
ADD_CODE (td,MINT_ICALL_P_P);
break;
case 2:
if (MONO_TYPE_IS_VOID (info->sig->ret)) {
if (info->sig->params [1]->type == MONO_TYPE_I4)
ADD_CODE (td,MINT_ICALL_PI_V);
else
ADD_CODE (td,MINT_ICALL_PP_V);
} else {
if (info->sig->params [1]->type == MONO_TYPE_I4)
ADD_CODE (td,MINT_ICALL_PI_P);
else
ADD_CODE (td,MINT_ICALL_PP_P);
}
break;
case 3:
if (MONO_TYPE_IS_VOID (info->sig->ret)) {
if (info->sig->params [2]->type == MONO_TYPE_I4)
ADD_CODE (td,MINT_ICALL_PPI_V);
else
ADD_CODE (td,MINT_ICALL_PPP_V);
} else {
g_assert (info->sig->params [1]->type == MONO_TYPE_I4 && info->sig->params [2]->type == MONO_TYPE_I4);
ADD_CODE (td, MINT_ICALL_PII_P);
}
break;
case 4:
g_assert (MONO_TYPE_IS_VOID (info->sig->ret));
g_assert (info->sig->params [2]->type == MONO_TYPE_I4 && info->sig->params [3]->type == MONO_TYPE_I4);
ADD_CODE (td, MINT_ICALL_PPII_V);
break;
default:
g_assert_not_reached ();
}
ADD_CODE(td, get_data_item_index (td, func));
td->sp -= info->sig->param_count;
if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
int mt = mint_type (info->sig->ret);
td->sp ++;
SET_SIMPLE_TYPE(td->sp - 1, stack_type [mt]);
}
break;
}
case CEE_MONO_VTADDR: {
int size;
CHECK_STACK (td, 1);
if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
size = mono_class_native_size(td->sp [-1].klass, NULL);
else
size = mono_class_value_size(td->sp [-1].klass, NULL);
size = ALIGN_TO (size, MINT_VT_ALIGNMENT);
ADD_CODE(td, MINT_VTRESULT);
ADD_CODE(td, 0);
WRITE32(td, &size);
td->vt_sp -= size;
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
break;
}
case CEE_MONO_LDPTR:
case CEE_MONO_CLASSCONST:
token = read32 (td->ip + 1);
td->ip += 5;
ADD_CODE(td, MINT_MONO_LDPTR);
ADD_CODE(td, get_data_item_index (td, mono_method_get_wrapper_data (method, token)));
td->sp [0].type = STACK_TYPE_I;
++td->sp;
break;
case CEE_MONO_OBJADDR:
CHECK_STACK (td, 1);
++td->ip;
td->sp[-1].type = STACK_TYPE_MP;
/* do nothing? */
break;
case CEE_MONO_NEWOBJ:
token = read32 (td->ip + 1);
td->ip += 5;
ADD_CODE(td, MINT_MONO_NEWOBJ);
ADD_CODE(td, get_data_item_index (td, mono_method_get_wrapper_data (method, token)));
td->sp [0].type = STACK_TYPE_O;
++td->sp;
break;
case CEE_MONO_RETOBJ:
CHECK_STACK (td, 1);
token = read32 (td->ip + 1);
td->ip += 5;
ADD_CODE(td, MINT_MONO_RETOBJ);
td->sp--;
klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
/*stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);*/
if (td->sp > td->stack)
g_warning ("CEE_MONO_RETOBJ: more values on stack: %d", td->sp-td->stack);
break;
case CEE_MONO_LDNATIVEOBJ:
token = read32 (td->ip + 1);
td->ip += 5;
klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
g_assert(m_class_is_valuetype (klass));
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
break;
case CEE_MONO_TLS: {
gint32 key = read32 (td->ip + 1);
td->ip += 5;
g_assert (key < TLS_KEY_NUM);
ADD_CODE (td, MINT_MONO_TLS);
WRITE32 (td, &key);
PUSH_SIMPLE_TYPE (td, STACK_TYPE_MP);
break;
}
case CEE_MONO_ATOMIC_STORE_I4:
CHECK_STACK (td, 2);
SIMPLE_OP (td, MINT_MONO_ATOMIC_STORE_I4);
td->sp -= 2;
td->ip++;
break;
case CEE_MONO_SAVE_LMF:
case CEE_MONO_RESTORE_LMF:
case CEE_MONO_NOT_TAKEN:
++td->ip;
break;
case CEE_MONO_LDPTR_INT_REQ_FLAG:
ADD_CODE (td, MINT_MONO_LDPTR);
ADD_CODE (td, get_data_item_index (td, mono_thread_interruption_request_flag ()));
PUSH_TYPE (td, STACK_TYPE_MP, NULL);
++td->ip;
break;
case CEE_MONO_MEMORY_BARRIER:
ADD_CODE (td, MINT_MONO_MEMORY_BARRIER);
++td->ip;
break;
case CEE_MONO_JIT_ATTACH:
ADD_CODE (td, MINT_MONO_JIT_ATTACH);
++td->ip;
break;
case CEE_MONO_JIT_DETACH:
ADD_CODE (td, MINT_MONO_JIT_DETACH);
++td->ip;
break;
case CEE_MONO_LDDOMAIN:
ADD_CODE (td, MINT_MONO_LDDOMAIN);
td->sp [0].type = STACK_TYPE_I;
++td->sp;
++td->ip;
break;
default:
g_error ("transform.c: Unimplemented opcode: 0xF0 %02x at 0x%x\n", *td->ip, td->ip-header->code);
}
break;
#if 0
case CEE_PREFIX7:
case CEE_PREFIX6:
case CEE_PREFIX5:
case CEE_PREFIX4:
case CEE_PREFIX3:
case CEE_PREFIX2:
case CEE_PREFIXREF: ves_abort(); break;
#endif
/*
* Note: Exceptions thrown when executing a prefixed opcode need
* to take into account the number of prefix bytes (usually the
* throw point is just (ip - n_prefix_bytes).
*/
case CEE_PREFIX1:
++td->ip;
switch (*td->ip) {
case CEE_ARGLIST:
ADD_CODE (td, MINT_ARGLIST);
PUSH_VT (td, SIZEOF_VOID_P);
PUSH_SIMPLE_TYPE (td, STACK_TYPE_VT);
++td->ip;
break;
case CEE_CEQ:
CHECK_STACK(td, 2);
if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP) {
ADD_CODE(td, MINT_CEQ_I4 + STACK_TYPE_I - STACK_TYPE_I4);
} else {
if (td->sp [-1].type == STACK_TYPE_R4 && td->sp [-2].type == STACK_TYPE_R8)
ADD_CODE (td, MINT_CONV_R8_R4);
if (td->sp [-1].type == STACK_TYPE_R8 && td->sp [-2].type == STACK_TYPE_R4)
ADD_CODE (td, MINT_CONV_R8_R4_SP);
ADD_CODE(td, MINT_CEQ_I4 + td->sp [-1].type - STACK_TYPE_I4);
}
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
++td->ip;
break;
case CEE_CGT:
CHECK_STACK(td, 2);
if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
ADD_CODE(td, MINT_CGT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
else
ADD_CODE(td, MINT_CGT_I4 + td->sp [-1].type - STACK_TYPE_I4);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
++td->ip;
break;
case CEE_CGT_UN:
CHECK_STACK(td, 2);
if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
ADD_CODE(td, MINT_CGT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
else
ADD_CODE(td, MINT_CGT_UN_I4 + td->sp [-1].type - STACK_TYPE_I4);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
++td->ip;
break;
case CEE_CLT:
CHECK_STACK(td, 2);
if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
ADD_CODE(td, MINT_CLT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
else
ADD_CODE(td, MINT_CLT_I4 + td->sp [-1].type - STACK_TYPE_I4);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
++td->ip;
break;
case CEE_CLT_UN:
CHECK_STACK(td, 2);
if (td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP)
ADD_CODE(td, MINT_CLT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
else
ADD_CODE(td, MINT_CLT_UN_I4 + td->sp [-1].type - STACK_TYPE_I4);
--td->sp;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4);
++td->ip;
break;
case CEE_LDVIRTFTN: /* fallthrough */
case CEE_LDFTN: {
MonoMethod *m;
if (*td->ip == CEE_LDVIRTFTN) {
CHECK_STACK (td, 1);
--td->sp;
}
token = read32 (td->ip + 1);
if (method->wrapper_type != MONO_WRAPPER_NONE)
m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
else {
m = mono_get_method_checked (image, token, NULL, generic_context, error);
goto_if_nok (error, exit);
}
if (!mono_method_can_access_method (method, m))
interp_generate_mae_throw (td, method, m);
if (method->wrapper_type == MONO_WRAPPER_NONE && m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
m = mono_marshal_get_synchronized_wrapper (m);
ADD_CODE(td, *td->ip == CEE_LDFTN ? MINT_LDFTN : MINT_LDVIRTFTN);
ADD_CODE(td, get_data_item_index (td, mono_interp_get_imethod (domain, m, error)));
goto_if_nok (error, exit);
td->ip += 5;
PUSH_SIMPLE_TYPE (td, STACK_TYPE_F);
break;
}
case CEE_LDARG:
load_arg (td, read16 (td->ip + 1));
td->ip += 3;
break;
case CEE_LDARGA: {
int n = read16 (td->ip + 1);
ADD_CODE (td, MINT_LDARGA);
ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offsets */
PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
td->ip += 3;
break;
}
case CEE_STARG:
store_arg (td, read16 (td->ip + 1));
td->ip += 3;
break;
case CEE_LDLOC:
load_local (td, read16 (td->ip + 1));
td->ip += 3;
break;
case CEE_LDLOCA:
ADD_CODE(td, MINT_LDLOCA_S);
ADD_CODE(td, td->rtm->local_offsets [read16 (td->ip + 1)]);
PUSH_SIMPLE_TYPE(td, STACK_TYPE_MP);
td->ip += 3;
break;
case CEE_STLOC:
store_local (td, read16 (td->ip + 1));
td->ip += 3;
break;
case CEE_LOCALLOC:
CHECK_STACK (td, 1);
#if SIZEOF_VOID_P == 8
if (td->sp [-1].type == STACK_TYPE_I8)
ADD_CODE(td, MINT_CONV_I4_I8);
#endif
ADD_CODE(td, MINT_LOCALLOC);
if (td->sp != td->stack + 1)
g_warning("CEE_LOCALLOC: stack not empty");
++td->ip;
SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_MP);
break;
#if 0
case CEE_UNUSED57: ves_abort(); break;
#endif
case CEE_ENDFILTER:
ADD_CODE (td, MINT_ENDFILTER);
++td->ip;
break;
case CEE_UNALIGNED_:
td->ip += 2;
break;
case CEE_VOLATILE_:
++td->ip;
volatile_ = TRUE;
break;
case CEE_TAIL_:
++td->ip;
/* FIX: should do something? */;
// TODO: This should raise a method_tail_call profiler event.
break;
case CEE_INITOBJ:
CHECK_STACK(td, 1);
token = read32 (td->ip + 1);
klass = mini_get_class (method, token, generic_context);
CHECK_TYPELOAD (klass);
if (m_class_is_valuetype (klass)) {
ADD_CODE (td, MINT_INITOBJ);
i32 = mono_class_value_size (klass, NULL);
WRITE32 (td, &i32);
} else {
ADD_CODE (td, MINT_LDNULL);
ADD_CODE (td, MINT_STIND_REF);
}
td->ip += 5;
--td->sp;
break;
case CEE_CPBLK:
CHECK_STACK(td, 3);
/* FIX? convert length to I8? */
if (volatile_)
ADD_CODE (td, MINT_MONO_MEMORY_BARRIER);
ADD_CODE(td, MINT_CPBLK);
BARRIER_IF_VOLATILE (td);
td->sp -= 3;
++td->ip;
break;
case CEE_READONLY_:
readonly = TRUE;
td->ip += 1;
break;
case CEE_CONSTRAINED_:
token = read32 (td->ip + 1);
constrained_class = mini_get_class (method, token, generic_context);
CHECK_TYPELOAD (constrained_class);
td->ip += 5;
break;
case CEE_INITBLK:
CHECK_STACK(td, 3);
BARRIER_IF_VOLATILE (td);
ADD_CODE(td, MINT_INITBLK);
td->sp -= 3;
td->ip += 1;
break;
case CEE_NO_:
/* FIXME: implement */
td->ip += 2;
break;
case CEE_RETHROW: {
int clause_index = td->clause_indexes [in_offset];
g_assert (clause_index != -1);
SIMPLE_OP (td, MINT_RETHROW);
ADD_CODE (td, rtm->exvar_offsets [clause_index]);
td->sp = td->stack;
break;
}
case CEE_SIZEOF: {
gint32 size;
token = read32 (td->ip + 1);
td->ip += 5;
if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (m_class_get_image (method->klass)) && !generic_context) {
int align;
MonoType *type = mono_type_create_from_typespec_checked (image, token, error);
goto_if_nok (error, exit);
size = mono_type_size (type, &align);
} else {
int align;
MonoClass *szclass = mini_get_class (method, token, generic_context);
CHECK_TYPELOAD (szclass);
#if 0
if (!szclass->valuetype)
THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
#endif
size = mono_type_size (m_class_get_byval_arg (szclass), &align);
}
ADD_CODE(td, MINT_LDC_I4);
WRITE32(td, &size);
PUSH_SIMPLE_TYPE(td, STACK_TYPE_I4);
break;
}
case CEE_REFANYTYPE:
ADD_CODE (td, MINT_REFANYTYPE);
td->ip += 1;
POP_VT (td, sizeof (MonoTypedRef));
PUSH_VT (td, sizeof (gpointer));
SET_TYPE(td->sp - 1, STACK_TYPE_VT, NULL);
break;
default:
g_error ("transform.c: Unimplemented opcode: 0xFE %02x (%s) at 0x%x\n", *td->ip, mono_opcode_name (256 + *td->ip), td->ip-header->code);
}
break;
default:
g_error ("transform.c: Unimplemented opcode: %02x at 0x%x\n", *td->ip, td->ip-header->code);
}
if (td->new_ip - td->new_code != new_in_start_offset)
td->last_new_ip = td->new_code + new_in_start_offset;
else if (td->is_bb_start [td->in_start - td->il_code])
td->is_bb_start [td->ip - td->il_code] = 1;
td->last_ip = td->in_start;
}
in_offset = td->ip - header->code;
g_assert (td->ip == end);
td->in_offsets [in_offset] = td->new_ip - td->new_code;
/* Handle relocations */
for (int i = 0; i < td->relocs->len; ++i) {
Reloc *reloc = g_ptr_array_index (td->relocs, i);
int offset = td->in_offsets [reloc->target] - reloc->offset;
switch (reloc->type) {
case RELOC_SHORT_BRANCH:
g_assert (td->new_code [reloc->offset + 1] == 0xffff);
td->new_code [reloc->offset + 1] = offset;
break;
case RELOC_LONG_BRANCH: {
guint16 *v = (guint16 *) &offset;
g_assert (td->new_code [reloc->offset + 1] == 0xbeef);
g_assert (td->new_code [reloc->offset + 2] == 0xdead);
td->new_code [reloc->offset + 1] = *(guint16 *) v;
td->new_code [reloc->offset + 2] = *(guint16 *) (v + 1);
break;
}
case RELOC_SWITCH: {
guint16 *v = (guint16*)&offset;
td->new_code [reloc->offset] = *(guint16*)v;
td->new_code [reloc->offset + 1] = *(guint16*)(v + 1);
break;
}
default:
g_assert_not_reached ();
break;
}
}
if (td->verbose_level) {
g_print ("Runtime method: %s %p, VT stack size: %d\n", mono_method_full_name (method, TRUE), rtm, td->max_vt_sp);
g_print ("Calculated stack size: %d, stated size: %d\n", td->max_stack_height, header->max_stack);
dump_mint_code (td);
}
/* Check if we use excessive stack space */
if (td->max_stack_height > header->max_stack * 3)
g_warning ("Excessive stack space usage for method %s, %d/%d", method->name, td->max_stack_height, header->max_stack);
int code_len = td->new_ip - td->new_code;
rtm->clauses = mono_domain_alloc0 (domain, header->num_clauses * sizeof (MonoExceptionClause));
memcpy (rtm->clauses, header->clauses, header->num_clauses * sizeof(MonoExceptionClause));
rtm->code = mono_domain_alloc0 (domain, (td->new_ip - td->new_code) * sizeof (gushort));
memcpy (rtm->code, td->new_code, (td->new_ip - td->new_code) * sizeof(gushort));
g_free (td->new_code);
rtm->new_body_start = rtm->code + body_start_offset;
rtm->init_locals = header->init_locals;
rtm->num_clauses = header->num_clauses;
for (i = 0; i < header->num_clauses; i++) {
MonoExceptionClause *c = rtm->clauses + i;
int end_off = c->try_offset + c->try_len;
c->try_offset = td->in_offsets [c->try_offset];
c->try_len = td->in_offsets [end_off] - c->try_offset;
end_off = c->handler_offset + c->handler_len;
c->handler_offset = td->in_offsets [c->handler_offset];
c->handler_len = td->in_offsets [end_off] - c->handler_offset;
if (c->flags & MONO_EXCEPTION_CLAUSE_FILTER)
c->data.filter_offset = td->in_offsets [c->data.filter_offset];
}
rtm->stack_size = (sizeof (stackval)) * (td->max_stack_height + 2); /* + 1 for returns of called functions + 1 for 0-ing in trace*/
rtm->stack_size = ALIGN_TO (rtm->stack_size, MINT_VT_ALIGNMENT);
rtm->vt_stack_size = td->max_vt_sp;
rtm->total_locals_size = td->total_locals_size;
rtm->alloca_size = rtm->total_locals_size + rtm->args_size + rtm->vt_stack_size + rtm->stack_size;
rtm->data_items = mono_domain_alloc0 (domain, td->n_data_items * sizeof (td->data_items [0]));
memcpy (rtm->data_items, td->data_items, td->n_data_items * sizeof (td->data_items [0]));
/* Save debug info */
interp_save_debug_info (rtm, header, td, line_numbers);
/* Create a MonoJitInfo for the interpreted method by creating the interpreter IR as the native code. */
int jinfo_len = mono_jit_info_size (0, header->num_clauses, 0);
MonoJitInfo *jinfo = (MonoJitInfo *)mono_domain_alloc0 (domain, jinfo_len);
jinfo->is_interp = 1;
rtm->jinfo = jinfo;
mono_jit_info_init (jinfo, method, (guint8*)rtm->code, code_len, 0, header->num_clauses, 0);
for (i = 0; i < jinfo->num_clauses; ++i) {
MonoJitExceptionInfo *ei = &jinfo->clauses [i];
MonoExceptionClause *c = rtm->clauses + i;
ei->flags = c->flags;
ei->try_start = (guint8*)(rtm->code + c->try_offset);
ei->try_end = (guint8*)(rtm->code + c->try_offset + c->try_len);
ei->handler_start = (guint8*)(rtm->code + c->handler_offset);
ei->exvar_offset = rtm->exvar_offsets [i];
if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
ei->data.filter = (guint8*)(rtm->code + c->data.filter_offset);
} else if (ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
ei->data.handler_end = (guint8*)(rtm->code + c->handler_offset + c->handler_len);
} else {
ei->data.catch_class = c->data.catch_class;
}
}
save_seq_points (td, jinfo);
exit:
mono_basic_block_free (original_bb);
g_free (td->in_offsets);
for (i = 0; i < header->code_size; ++i)
g_free (td->stack_state [i]);
g_free (td->stack_state);
g_free (td->stack_height);
g_free (td->vt_stack_size);
g_free (td->data_items);
g_free (td->stack);
g_hash_table_destroy (td->data_hash);
g_free (td->clause_indexes);
g_ptr_array_free (td->seq_points, TRUE);
g_array_free (line_numbers, TRUE);
g_ptr_array_free (td->relocs, TRUE);
mono_mempool_destroy (td->mempool);
}
static mono_mutex_t calc_section;
void
mono_interp_transform_init (void)
{
mono_os_mutex_init_recursive(&calc_section);
}
void
mono_interp_transform_method (InterpMethod *imethod, ThreadContext *context, MonoError *error)
{
int i, align, size, offset;
MonoMethod *method = imethod->method;
MonoImage *image = m_class_get_image (method->klass);
MonoMethodHeader *header = NULL;
MonoMethodSignature *signature = mono_method_signature (method);
register const unsigned char *ip, *end;
const MonoOpcode *opcode;
MonoMethod *m;
MonoClass *klass;
unsigned char *is_bb_start;
int in;
MonoVTable *method_class_vt;
int backwards;
MonoGenericContext *generic_context = NULL;
MonoDomain *domain = imethod->domain;
InterpMethod tmp_imethod;
InterpMethod *real_imethod;
error_init (error);
if (mono_class_is_open_constructed_type (m_class_get_byval_arg (method->klass))) {
mono_error_set_invalid_operation (error, "%s", "Could not execute the method because the containing type is not fully instantiated.");
return;
}
// g_printerr ("TRANSFORM(0x%016lx): begin %s::%s\n", mono_thread_current (), method->klass->name, method->name);
method_class_vt = mono_class_vtable_checked (domain, imethod->method->klass, error);
return_if_nok (error);
if (!method_class_vt->initialized) {
mono_runtime_class_init_full (method_class_vt, error);
return_if_nok (error);
}
MONO_PROFILER_RAISE (jit_begin, (method));
if (mono_method_signature (method)->is_inflated)
generic_context = mono_method_get_context (method);
else {
MonoGenericContainer *generic_container = mono_method_get_generic_container (method);
if (generic_container)
generic_context = &generic_container->context;
}
if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
MonoMethod *nm = NULL;
mono_os_mutex_lock (&calc_section);
if (imethod->transformed) {
mono_os_mutex_unlock (&calc_section);
MONO_PROFILER_RAISE (jit_done, (method, imethod->jinfo));
return;
}
/* assumes all internal calls with an array this are built in... */
if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL && (! mono_method_signature (method)->hasthis || m_class_get_rank (method->klass) == 0)) {
nm = mono_marshal_get_native_wrapper (method, TRUE, FALSE);
signature = mono_method_signature (nm);
} else {
const char *name = method->name;
if (m_class_get_parent (method->klass) == mono_defaults.multicastdelegate_class) {
if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("ves_icall_mono_delegate_ctor_interp");
g_assert (mi);
char *wrapper_name = g_strdup_printf ("__icall_wrapper_%s", mi->name);
nm = mono_marshal_get_icall_wrapper (mi->sig, wrapper_name, mi->func, TRUE);
g_free (wrapper_name);
} else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
/*
* Usually handled during transformation of the caller, but
* when the caller is handled by another execution engine
* (for example fullAOT) we need to handle it here. That's
* known to be wrong in cases where the reference to
* `MonoDelegate` would be needed (FIXME).
*/
nm = mono_marshal_get_delegate_invoke (method, NULL);
} else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
nm = mono_marshal_get_delegate_begin_invoke (method);
} else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
nm = mono_marshal_get_delegate_end_invoke (method);
}
}
if (nm == NULL)
g_assert_not_reached ();
}
if (nm == NULL) {
imethod->stack_size = sizeof (stackval); /* for tracing */
imethod->alloca_size = imethod->stack_size;
imethod->transformed = TRUE;
mono_os_mutex_unlock(&calc_section);
MONO_PROFILER_RAISE (jit_done, (method, NULL));
return;
}
method = nm;
header = interp_method_get_header (nm, error);
mono_os_mutex_unlock (&calc_section);
return_if_nok (error);
}
if (!header) {
header = mono_method_get_header_checked (method, error);
return_if_nok (error);
}
g_assert ((signature->param_count + signature->hasthis) < 1000);
g_assert (header->max_stack < 10000);
/* intern the strings in the method. */
ip = header->code;
end = ip + header->code_size;
is_bb_start = g_malloc0(header->code_size);
is_bb_start [0] = 1;
while (ip < end) {
in = *ip;
if (in == 0xfe) {
ip++;
in = *ip + 256;
}
else if (in == 0xf0) {
ip++;
in = *ip + MONO_CEE_MONO_ICALL;
}
opcode = &mono_opcodes [in];
switch (opcode->argument) {
case MonoInlineNone:
++ip;
break;
case MonoInlineString:
if (method->wrapper_type == MONO_WRAPPER_NONE) {
mono_ldstr_checked (domain, image, mono_metadata_token_index (read32 (ip + 1)), error);
if (!is_ok (error)) {
g_free (is_bb_start);
mono_metadata_free_mh (header);
return;
}
}
ip += 5;
break;
case MonoInlineType:
if (method->wrapper_type == MONO_WRAPPER_NONE) {
klass = mini_get_class (method, read32 (ip + 1), generic_context);
mono_class_init (klass);
/* quick fix to not do this for the fake ptr classes - probably should not be getting the vtable at all here */
#if 0
g_error ("FIXME: interface method lookup: %s (in method %s)", klass->name, method->name);
if (!(klass->flags & TYPE_ATTRIBUTE_INTERFACE) && klass->interface_offsets != NULL)
mono_class_vtable (domain, klass);
#endif
}
ip += 5;
break;
case MonoInlineMethod:
if (method->wrapper_type == MONO_WRAPPER_NONE && *ip != CEE_CALLI) {
m = mono_get_method_checked (image, read32 (ip + 1), NULL, generic_context, error);
if (!is_ok (error)) {
g_free (is_bb_start);
mono_metadata_free_mh (header);
return;
}
mono_class_init (m->klass);
if (!mono_class_is_interface (m->klass)) {
mono_class_vtable_checked (domain, m->klass, error);
if (!is_ok (error)) {
g_free (is_bb_start);
mono_metadata_free_mh (header);
return;
}
}
}
ip += 5;
break;
case MonoInlineField:
case MonoInlineSig:
case MonoInlineI:
case MonoInlineTok:
case MonoShortInlineR:
ip += 5;
break;
case MonoInlineBrTarget:
offset = read32 (ip + 1);
ip += 5;
backwards = offset < 0;
offset += ip - header->code;
g_assert (offset >= 0 && offset < header->code_size);
is_bb_start [offset] |= backwards ? 2 : 1;
break;
case MonoShortInlineBrTarget:
offset = ((gint8 *)ip) [1];
ip += 2;
backwards = offset < 0;
offset += ip - header->code;
g_assert (offset >= 0 && offset < header->code_size);
is_bb_start [offset] |= backwards ? 2 : 1;
break;
case MonoInlineVar:
ip += 3;
break;
case MonoShortInlineVar:
case MonoShortInlineI:
ip += 2;
break;
case MonoInlineSwitch: {
guint32 n;
const unsigned char *next_ip;
++ip;
n = read32 (ip);
ip += 4;
next_ip = ip + 4 * n;
for (i = 0; i < n; i++) {
offset = read32 (ip);
backwards = offset < 0;
offset += next_ip - header->code;
g_assert (offset >= 0 && offset < header->code_size);
is_bb_start [offset] |= backwards ? 2 : 1;
ip += 4;
}
break;
}
case MonoInlineR:
case MonoInlineI8:
ip += 9;
break;
default:
g_assert_not_reached ();
}
}
// g_printerr ("TRANSFORM(0x%016lx): end %s::%s\n", mono_thread_current (), method->klass->name, method->name);
/* Make modifications to a copy of imethod, copy them back inside the lock */
real_imethod = imethod;
memcpy (&tmp_imethod, imethod, sizeof (InterpMethod));
imethod = &tmp_imethod;
imethod->local_offsets = g_malloc (header->num_locals * sizeof(guint32));
offset = 0;
for (i = 0; i < header->num_locals; ++i) {
size = mono_type_size (header->locals [i], &align);
offset += align - 1;
offset &= ~(align - 1);
imethod->local_offsets [i] = offset;
offset += size;
}
offset = (offset + 7) & ~7;
imethod->exvar_offsets = g_malloc (header->num_clauses * sizeof (guint32));
for (i = 0; i < header->num_clauses; i++) {
imethod->exvar_offsets [i] = offset;
offset += sizeof (MonoObject*);
}
offset = (offset + 7) & ~7;
imethod->locals_size = offset;
g_assert (imethod->locals_size < 65536);
offset = 0;
imethod->arg_offsets = g_malloc ((!!signature->hasthis + signature->param_count) * sizeof(guint32));
if (signature->hasthis) {
g_assert (!signature->pinvoke);
size = align = SIZEOF_VOID_P;
offset += align - 1;
offset &= ~(align - 1);
imethod->arg_offsets [0] = offset;
offset += size;
}
for (i = 0; i < signature->param_count; ++i) {
if (signature->pinvoke) {
guint32 dummy;
size = mono_type_native_stack_size (signature->params [i], &dummy);
align = 8;
}
else
size = mono_type_stack_size (signature->params [i], &align);
offset += align - 1;
offset &= ~(align - 1);
imethod->arg_offsets [i + !!signature->hasthis] = offset;
offset += size;
}
offset = (offset + 7) & ~7;
imethod->args_size = offset;
g_assert (imethod->args_size < 10000);
generate (method, header, imethod, is_bb_start, generic_context, error);
mono_metadata_free_mh (header);
g_free (is_bb_start);
return_if_nok (error);
// FIXME: Add a different callback ?
MONO_PROFILER_RAISE (jit_done, (method, imethod->jinfo));
/* Copy changes back */
imethod = real_imethod;
mono_os_mutex_lock (&calc_section);
if (!imethod->transformed) {
InterpMethod *hash = imethod->next_jit_code_hash;
memcpy (imethod, &tmp_imethod, sizeof (InterpMethod));
imethod->next_jit_code_hash = hash;
mono_memory_barrier ();
imethod->transformed = TRUE;
mono_atomic_fetch_add_i32 (&mono_jit_stats.methods_with_interp, 1);
}
mono_os_mutex_unlock (&calc_section);
}
|