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
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
|
/*
* aot-compiler.c: mono Ahead of Time compiler
*
* Author:
* Dietmar Maurer (dietmar@ximian.com)
* Zoltan Varga (vargaz@gmail.com)
*
* (C) 2002 Ximian, Inc.
*/
/* Remaining AOT-only work:
* - optimize the trampolines, generate more code in the arch files.
* - make things more consistent with how elf works, for example, use ELF
* relocations.
* Remaining generics sharing work:
* - optimize the size of the data which is encoded.
* - optimize the runtime loading of data:
* - the trampoline code calls mono_jit_info_table_find () to find the rgctx,
* which loads the debugging+exception handling info for the method. This is a
* huge waste of time and code, since the rgctx structure is currently empty.
* - every shared method has a MonoGenericJitInfo structure which is only really
* used for handling catch clauses with open types, not a very common use case.
*/
#include "config.h"
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include <fcntl.h>
#include <ctype.h>
#include <string.h>
#ifndef PLATFORM_WIN32
#include <sys/time.h>
#else
#include <winsock2.h>
#include <windows.h>
#endif
#include <errno.h>
#include <sys/stat.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/class.h>
#include <mono/metadata/object.h>
#include <mono/metadata/tokentype.h>
#include <mono/metadata/appdomain.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/metadata-internals.h>
#include <mono/metadata/marshal.h>
#include <mono/metadata/gc-internal.h>
#include <mono/metadata/monitor.h>
#include <mono/metadata/mempool-internals.h>
#include <mono/metadata/mono-endian.h>
#include <mono/metadata/threads-types.h>
#include <mono/utils/mono-logger.h>
#include <mono/utils/mono-compiler.h>
#include <mono/utils/mono-time.h>
#include <mono/utils/mono-mmap.h>
#include "mini.h"
#include "image-writer.h"
#include "dwarfwriter.h"
#if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
#define TV_DECLARE(name) gint64 name
#define TV_GETTIME(tv) tv = mono_100ns_ticks ()
#define TV_ELAPSED(start,end) (((end) - (start)) / 10)
#ifdef PLATFORM_WIN32
#define SHARED_EXT ".dll"
#elif defined(__ppc__) && defined(__MACH__)
#define SHARED_EXT ".dylib"
#else
#define SHARED_EXT ".so"
#endif
#define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
#define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
#define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
typedef struct MonoAotOptions {
char *outfile;
gboolean save_temps;
gboolean write_symbols;
gboolean metadata_only;
gboolean bind_to_runtime_version;
gboolean full_aot;
gboolean no_dlsym;
gboolean static_link;
gboolean asm_only;
gboolean asm_writer;
gboolean nodebug;
gboolean soft_debug;
int nthreads;
int ntrampolines;
int nrgctx_trampolines;
gboolean print_skipped_methods;
char *tool_prefix;
} MonoAotOptions;
typedef struct MonoAotStats {
int ccount, mcount, lmfcount, abscount, gcount, ocount, genericcount;
int code_size, info_size, ex_info_size, unwind_info_size, got_size, class_info_size, got_info_size, got_info_offsets_size;
int methods_without_got_slots, direct_calls, all_calls;
int got_slots;
int got_slot_types [MONO_PATCH_INFO_NONE];
int jit_time, gen_time, link_time;
} MonoAotStats;
typedef struct MonoAotCompile {
MonoImage *image;
GPtrArray *methods;
GHashTable *method_indexes;
GHashTable *method_depth;
MonoCompile **cfgs;
int cfgs_size;
GHashTable *patch_to_plt_offset;
GHashTable *plt_offset_to_patch;
GHashTable *patch_to_got_offset;
GHashTable **patch_to_got_offset_by_type;
GPtrArray *got_patches;
GHashTable *image_hash;
GHashTable *method_to_cfg;
GHashTable *token_info_hash;
GPtrArray *extra_methods;
GPtrArray *image_table;
GPtrArray *globals;
GList *method_order;
guint32 *plt_got_info_offsets;
guint32 got_offset, plt_offset, plt_got_offset_base;
/* Number of GOT entries reserved for trampolines */
guint32 num_trampoline_got_entries;
guint32 num_trampolines [MONO_AOT_TRAMP_NUM];
guint32 trampoline_got_offset_base [MONO_AOT_TRAMP_NUM];
guint32 trampoline_size [MONO_AOT_TRAMP_NUM];
MonoAotOptions aot_opts;
guint32 nmethods;
guint32 opts;
MonoMemPool *mempool;
MonoAotStats stats;
int method_index;
char *static_linking_symbol;
CRITICAL_SECTION mutex;
gboolean use_bin_writer;
MonoImageWriter *w;
MonoDwarfWriter *dwarf;
FILE *fp;
char *tmpfname;
GSList *cie_program;
GHashTable *unwind_info_offsets;
GPtrArray *unwind_ops;
guint32 unwind_info_offset;
char *got_symbol;
char *plt_symbol;
GHashTable *method_label_hash;
const char *temp_prefix;
guint32 label_generator;
} MonoAotCompile;
#define mono_acfg_lock(acfg) EnterCriticalSection (&((acfg)->mutex))
#define mono_acfg_unlock(acfg) LeaveCriticalSection (&((acfg)->mutex))
#ifdef HAVE_ARRAY_ELEM_INIT
#define MSGSTRFIELD(line) MSGSTRFIELD1(line)
#define MSGSTRFIELD1(line) str##line
static const struct msgstr_t {
#define PATCH_INFO(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
#include "patch-info.h"
#undef PATCH_INFO
} opstr = {
#define PATCH_INFO(a,b) b,
#include "patch-info.h"
#undef PATCH_INFO
};
static const gint16 opidx [] = {
#define PATCH_INFO(a,b) [MONO_PATCH_INFO_ ## a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
#include "patch-info.h"
#undef PATCH_INFO
};
static G_GNUC_UNUSED const char*
get_patch_name (int info)
{
return (const char*)&opstr + opidx [info];
}
#else
#define PATCH_INFO(a,b) b,
static const char* const
patch_types [MONO_PATCH_INFO_NUM + 1] = {
#include "patch-info.h"
NULL
};
static G_GNUC_UNUSED const char*
get_patch_name (int info)
{
return patch_types [info];
}
#endif
/* Wrappers around the image writer functions */
static inline void
emit_section_change (MonoAotCompile *acfg, const char *section_name, int subsection_index)
{
img_writer_emit_section_change (acfg->w, section_name, subsection_index);
}
static inline void
emit_push_section (MonoAotCompile *acfg, const char *section_name, int subsection)
{
img_writer_emit_push_section (acfg->w, section_name, subsection);
}
static inline void
emit_pop_section (MonoAotCompile *acfg)
{
img_writer_emit_pop_section (acfg->w);
}
static inline void
emit_local_symbol (MonoAotCompile *acfg, const char *name, const char *end_label, gboolean func)
{
img_writer_emit_local_symbol (acfg->w, name, end_label, func);
}
static inline void
emit_label (MonoAotCompile *acfg, const char *name)
{
img_writer_emit_label (acfg->w, name);
}
static inline void
emit_bytes (MonoAotCompile *acfg, const guint8* buf, int size)
{
img_writer_emit_bytes (acfg->w, buf, size);
}
static inline void
emit_string (MonoAotCompile *acfg, const char *value)
{
img_writer_emit_string (acfg->w, value);
}
static inline void
emit_line (MonoAotCompile *acfg)
{
img_writer_emit_line (acfg->w);
}
static inline void
emit_alignment (MonoAotCompile *acfg, int size)
{
img_writer_emit_alignment (acfg->w, size);
}
static inline void
emit_pointer_unaligned (MonoAotCompile *acfg, const char *target)
{
img_writer_emit_pointer_unaligned (acfg->w, target);
}
static inline void
emit_pointer (MonoAotCompile *acfg, const char *target)
{
img_writer_emit_pointer (acfg->w, target);
}
static inline void
emit_int16 (MonoAotCompile *acfg, int value)
{
img_writer_emit_int16 (acfg->w, value);
}
static inline void
emit_int32 (MonoAotCompile *acfg, int value)
{
img_writer_emit_int32 (acfg->w, value);
}
static inline void
emit_symbol_diff (MonoAotCompile *acfg, const char *end, const char* start, int offset)
{
img_writer_emit_symbol_diff (acfg->w, end, start, offset);
}
static inline void
emit_zero_bytes (MonoAotCompile *acfg, int num)
{
img_writer_emit_zero_bytes (acfg->w, num);
}
static inline void
emit_byte (MonoAotCompile *acfg, guint8 val)
{
img_writer_emit_byte (acfg->w, val);
}
static G_GNUC_UNUSED void
emit_global_inner (MonoAotCompile *acfg, const char *name, gboolean func)
{
img_writer_emit_global (acfg->w, name, func);
}
static void
emit_global (MonoAotCompile *acfg, const char *name, gboolean func)
{
if (acfg->aot_opts.no_dlsym) {
g_ptr_array_add (acfg->globals, g_strdup (name));
img_writer_emit_local_symbol (acfg->w, name, NULL, func);
} else {
img_writer_emit_global (acfg->w, name, func);
}
}
static void
emit_symbol_size (MonoAotCompile *acfg, const char *name, const char *end_label)
{
img_writer_emit_symbol_size (acfg->w, name, end_label);
}
static void
emit_string_symbol (MonoAotCompile *acfg, const char *name, const char *value)
{
img_writer_emit_section_change (acfg->w, ".text", 1);
emit_global (acfg, name, FALSE);
img_writer_emit_label (acfg->w, name);
img_writer_emit_string (acfg->w, value);
}
static G_GNUC_UNUSED void
emit_uleb128 (MonoAotCompile *acfg, guint32 value)
{
do {
guint8 b = value & 0x7f;
value >>= 7;
if (value != 0) /* more bytes to come */
b |= 0x80;
emit_byte (acfg, b);
} while (value);
}
static G_GNUC_UNUSED void
emit_sleb128 (MonoAotCompile *acfg, gint64 value)
{
gboolean more = 1;
gboolean negative = (value < 0);
guint32 size = 64;
guint8 byte;
while (more) {
byte = value & 0x7f;
value >>= 7;
/* the following is unnecessary if the
* implementation of >>= uses an arithmetic rather
* than logical shift for a signed left operand
*/
if (negative)
/* sign extend */
value |= - ((gint64)1 <<(size - 7));
/* sign bit of byte is second high order bit (0x40) */
if ((value == 0 && !(byte & 0x40)) ||
(value == -1 && (byte & 0x40)))
more = 0;
else
byte |= 0x80;
emit_byte (acfg, byte);
}
}
static G_GNUC_UNUSED void
encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
{
guint8 *p = buf;
do {
guint8 b = value & 0x7f;
value >>= 7;
if (value != 0) /* more bytes to come */
b |= 0x80;
*p ++ = b;
} while (value);
*endbuf = p;
}
static G_GNUC_UNUSED void
encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
{
gboolean more = 1;
gboolean negative = (value < 0);
guint32 size = 32;
guint8 byte;
guint8 *p = buf;
while (more) {
byte = value & 0x7f;
value >>= 7;
/* the following is unnecessary if the
* implementation of >>= uses an arithmetic rather
* than logical shift for a signed left operand
*/
if (negative)
/* sign extend */
value |= - (1 <<(size - 7));
/* sign bit of byte is second high order bit (0x40) */
if ((value == 0 && !(byte & 0x40)) ||
(value == -1 && (byte & 0x40)))
more = 0;
else
byte |= 0x80;
*p ++= byte;
}
*endbuf = p;
}
/* ARCHITECTURE SPECIFIC CODE */
#if defined(TARGET_X86) || defined(TARGET_AMD64) || defined(TARGET_ARM) || defined(TARGET_POWERPC)
#define EMIT_DWARF_INFO 1
#endif
#if defined(TARGET_ARM)
#define AOT_FUNC_ALIGNMENT 4
#else
#define AOT_FUNC_ALIGNMENT 16
#endif
#if defined(TARGET_POWERPC64) && !defined(__mono_ilp32__)
#define PPC_LD_OP "ld"
#define PPC_LDX_OP "ldx"
#else
#define PPC_LD_OP "lwz"
#define PPC_LDX_OP "lwzx"
#endif
/*
* arch_emit_direct_call:
*
* Emit a direct call to the symbol TARGET. CALL_SIZE is set to the size of the
* calling code.
*/
static void
arch_emit_direct_call (MonoAotCompile *acfg, const char *target, int *call_size)
{
#if defined(TARGET_X86) || defined(TARGET_AMD64)
/* Need to make sure this is exactly 5 bytes long */
emit_byte (acfg, '\xe8');
emit_symbol_diff (acfg, target, ".", -4);
*call_size = 5;
#elif defined(TARGET_ARM)
if (acfg->use_bin_writer) {
guint8 buf [4];
guint8 *code;
code = buf;
ARM_BL (code, 0);
img_writer_emit_reloc (acfg->w, R_ARM_CALL, target, -8);
emit_bytes (acfg, buf, 4);
} else {
img_writer_emit_unset_mode (acfg->w);
fprintf (acfg->fp, "bl %s\n", target);
}
*call_size = 4;
#elif defined(TARGET_POWERPC)
if (acfg->use_bin_writer) {
g_assert_not_reached ();
} else {
img_writer_emit_unset_mode (acfg->w);
fprintf (acfg->fp, "bl %s\n", target);
*call_size = 4;
}
#else
g_assert_not_reached ();
#endif
}
/*
* PPC32 design:
* - we use an approach similar to the x86 abi: reserve a register (r30) to hold
* the GOT pointer.
* - The full-aot trampolines need access to the GOT of mscorlib, so we store
* in in the 2. slot of every GOT, and require every method to place the GOT
* address in r30, even when it doesn't access the GOT otherwise. This way,
* the trampolines can compute the mscorlib GOT address by loading 4(r30).
*/
/*
* PPC64 design:
* PPC64 uses function descriptors which greatly complicate all code, since
* these are used very inconsistently in the runtime. Some functions like
* mono_compile_method () return ftn descriptors, while others like the
* trampoline creation functions do not.
* We assume that all GOT slots contain function descriptors, and create
* descriptors in aot-runtime.c when needed.
* The ppc64 abi uses r2 to hold the address of the TOC/GOT, which is loaded
* from function descriptors, we could do the same, but it would require
* rewriting all the ppc/aot code to handle function descriptors properly.
* So instead, we use the same approach as on PPC32.
* This is a horrible mess, but fixing it would probably lead to an even bigger
* one.
*/
#ifdef MONO_ARCH_AOT_SUPPORTED
/*
* arch_emit_got_offset:
*
* The memory pointed to by CODE should hold native code for computing the GOT
* address. Emit this code while patching it with the offset between code and
* the GOT. CODE_SIZE is set to the number of bytes emitted.
*/
static void
arch_emit_got_offset (MonoAotCompile *acfg, guint8 *code, int *code_size)
{
#if defined(TARGET_POWERPC64)
g_assert (!acfg->use_bin_writer);
img_writer_emit_unset_mode (acfg->w);
/*
* The ppc32 code doesn't seem to work on ppc64, the assembler complains about
* unsupported relocations. So we store the got address into the .Lgot_addr
* symbol which is in the text segment, compute its address, and load it.
*/
fprintf (acfg->fp, ".L%d:\n", acfg->label_generator);
fprintf (acfg->fp, "lis 0, (.Lgot_addr + 4 - .L%d)@h\n", acfg->label_generator);
fprintf (acfg->fp, "ori 0, 0, (.Lgot_addr + 4 - .L%d)@l\n", acfg->label_generator);
fprintf (acfg->fp, "add 30, 30, 0\n");
fprintf (acfg->fp, "%s 30, 0(30)\n", PPC_LD_OP);
acfg->label_generator ++;
*code_size = 16;
#elif defined(TARGET_POWERPC)
g_assert (!acfg->use_bin_writer);
img_writer_emit_unset_mode (acfg->w);
fprintf (acfg->fp, ".L%d:\n", acfg->label_generator);
fprintf (acfg->fp, "lis 0, (%s + 4 - .L%d)@h\n", acfg->got_symbol, acfg->label_generator);
fprintf (acfg->fp, "ori 0, 0, (%s + 4 - .L%d)@l\n", acfg->got_symbol, acfg->label_generator);
acfg->label_generator ++;
*code_size = 8;
#else
guint32 offset = mono_arch_get_patch_offset (code);
emit_bytes (acfg, code, offset);
emit_symbol_diff (acfg, acfg->got_symbol, ".", offset);
*code_size = offset + 4;
#endif
}
/*
* arch_emit_got_access:
*
* The memory pointed to by CODE should hold native code for loading a GOT
* slot. Emit this code while patching it so it accesses the GOT slot GOT_SLOT.
* CODE_SIZE is set to the number of bytes emitted.
*/
static void
arch_emit_got_access (MonoAotCompile *acfg, guint8 *code, int got_slot, int *code_size)
{
/* Emit beginning of instruction */
emit_bytes (acfg, code, mono_arch_get_patch_offset (code));
/* Emit the offset */
#ifdef TARGET_AMD64
emit_symbol_diff (acfg, acfg->got_symbol, ".", (unsigned int) ((got_slot * sizeof (gpointer)) - 4));
*code_size = mono_arch_get_patch_offset (code) + 4;
#elif defined(TARGET_X86)
emit_int32 (acfg, (unsigned int) ((got_slot * sizeof (gpointer))));
*code_size = mono_arch_get_patch_offset (code) + 4;
#elif defined(TARGET_ARM)
emit_symbol_diff (acfg, acfg->got_symbol, ".", (unsigned int) ((got_slot * sizeof (gpointer))) - 12);
*code_size = mono_arch_get_patch_offset (code) + 4;
#elif defined(TARGET_POWERPC)
{
guint8 buf [32];
guint8 *code;
code = buf;
ppc_load32 (code, ppc_r0, got_slot * sizeof (gpointer));
g_assert (code - buf == 8);
emit_bytes (acfg, buf, code - buf);
*code_size = code - buf;
}
#else
g_assert_not_reached ();
#endif
}
#endif
/*
* arch_emit_plt_entry:
*
* Emit code for the PLT entry with index INDEX.
*/
static void
arch_emit_plt_entry (MonoAotCompile *acfg, int index)
{
#if defined(TARGET_X86)
if (index == 0) {
/* It is filled up during loading by the AOT loader. */
emit_zero_bytes (acfg, 16);
} else {
/* Need to make sure this is 9 bytes long */
emit_byte (acfg, '\xe9');
emit_symbol_diff (acfg, acfg->plt_symbol, ".", -4);
emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
}
#elif defined(TARGET_AMD64)
/*
* We can't emit jumps because they are 32 bits only so they can't be patched.
* So we make indirect calls through GOT entries which are patched by the AOT
* loader to point to .Lpd entries.
* An x86_64 plt entry is 10 bytes long, init_plt () depends on this.
*/
/* jmpq *<offset>(%rip) */
emit_byte (acfg, '\xff');
emit_byte (acfg, '\x25');
emit_symbol_diff (acfg, acfg->got_symbol, ".", ((acfg->plt_got_offset_base + index) * sizeof (gpointer)) -4);
/* Used by mono_aot_get_plt_info_offset */
emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
#elif defined(TARGET_ARM)
guint8 buf [256];
guint8 *code;
/* FIXME:
* - optimize OP_AOTCONST implementation
* - optimize the PLT entries
* - optimize SWITCH AOT implementation
* - implement IMT support
*/
code = buf;
if (acfg->use_bin_writer && FALSE) {
/* FIXME: mono_arch_patch_plt_entry () needs to decode this */
/* We only emit 1 relocation since we implement it ourselves anyway */
img_writer_emit_reloc (acfg->w, R_ARM_ALU_PC_G0_NC, acfg->got_symbol, ((acfg->plt_got_offset_base + index) * sizeof (gpointer)) - 8);
/* FIXME: A 2 instruction encoding is sufficient in most cases */
ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_PC, 0, 0);
ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_IP, 0, 0);
ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, 0);
emit_bytes (acfg, buf, code - buf);
/* Used by mono_aot_get_plt_info_offset */
emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
} else {
ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_IP);
emit_bytes (acfg, buf, code - buf);
emit_symbol_diff (acfg, acfg->got_symbol, ".", ((acfg->plt_got_offset_base + index) * sizeof (gpointer)) - 4);
/* Used by mono_aot_get_plt_info_offset */
emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
}
/*
* The plt_got_info_offset is computed automatically by
* mono_aot_get_plt_info_offset (), so no need to save it here.
*/
#elif defined(TARGET_POWERPC)
guint32 offset = (acfg->plt_got_offset_base + index) * sizeof (gpointer);
/* The GOT address is guaranteed to be in r30 by OP_LOAD_GOTADDR */
g_assert (!acfg->use_bin_writer);
img_writer_emit_unset_mode (acfg->w);
fprintf (acfg->fp, "lis 11, %d@h\n", offset);
fprintf (acfg->fp, "ori 11, 11, %d@l\n", offset);
fprintf (acfg->fp, "add 11, 11, 30\n");
fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
#ifdef PPC_USES_FUNCTION_DESCRIPTOR
fprintf (acfg->fp, "%s 2, %d(11)\n", PPC_LD_OP, (int)sizeof (gpointer));
fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
#endif
fprintf (acfg->fp, "mtctr 11\n");
fprintf (acfg->fp, "bctr\n");
emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
#else
g_assert_not_reached ();
#endif
}
/*
* arch_emit_specific_trampoline:
*
* Emit code for a specific trampoline. OFFSET is the offset of the first of
* two GOT slots which contain the generic trampoline address and the trampoline
* argument. TRAMP_SIZE is set to the size of the emitted trampoline.
*/
static void
arch_emit_specific_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
{
/*
* The trampolines created here are variations of the specific
* trampolines created in mono_arch_create_specific_trampoline (). The
* differences are:
* - the generic trampoline address is taken from a got slot.
* - the offset of the got slot where the trampoline argument is stored
* is embedded in the instruction stream, and the generic trampoline
* can load the argument by loading the offset, adding it to the
* address of the trampoline to get the address of the got slot, and
* loading the argument from there.
* - all the trampolines should be of the same length.
*/
#if defined(TARGET_AMD64)
/* This should be exactly 16 bytes long */
*tramp_size = 16;
/* call *<offset>(%rip) */
emit_byte (acfg, '\x41');
emit_byte (acfg, '\xff');
emit_byte (acfg, '\x15');
emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
/* This should be relative to the start of the trampoline */
emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4 + 19);
emit_zero_bytes (acfg, 5);
#elif defined(TARGET_ARM)
guint8 buf [128];
guint8 *code;
/* This should be exactly 20 bytes long */
*tramp_size = 20;
code = buf;
ARM_PUSH (code, 0x5fff);
ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 4);
/* Load the value from the GOT */
ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_PC, ARMREG_R1);
/* Branch to it */
ARM_BLX_REG (code, ARMREG_R1);
g_assert (code - buf == 16);
/* Emit it */
emit_bytes (acfg, buf, code - buf);
/*
* Only one offset is needed, since the second one would be equal to the
* first one.
*/
emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4 + 4);
//emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4 + 8);
#elif defined(TARGET_POWERPC)
guint8 buf [128];
guint8 *code;
*tramp_size = 4;
code = buf;
g_assert (!acfg->use_bin_writer);
/*
* PPC has no ip relative addressing, so we need to compute the address
* of the mscorlib got. That is slow and complex, so instead, we store it
* in the second got slot of every aot image. The caller already computed
* the address of its got and placed it into r30.
*/
img_writer_emit_unset_mode (acfg->w);
/* Load mscorlib got address */
fprintf (acfg->fp, "%s 0, %d(30)\n", PPC_LD_OP, (int)sizeof (gpointer));
/* Load generic trampoline address */
fprintf (acfg->fp, "lis 11, %d@h\n", (int)(offset * sizeof (gpointer)));
fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)(offset * sizeof (gpointer)));
fprintf (acfg->fp, "%s 11, 11, 0\n", PPC_LDX_OP);
#ifdef PPC_USES_FUNCTION_DESCRIPTOR
fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
#endif
fprintf (acfg->fp, "mtctr 11\n");
/* Load trampoline argument */
/* On ppc, we pass it normally to the generic trampoline */
fprintf (acfg->fp, "lis 11, %d@h\n", (int)((offset + 1) * sizeof (gpointer)));
fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)((offset + 1) * sizeof (gpointer)));
fprintf (acfg->fp, "%s 0, 11, 0\n", PPC_LDX_OP);
/* Branch to generic trampoline */
fprintf (acfg->fp, "bctr\n");
#ifdef PPC_USES_FUNCTION_DESCRIPTOR
*tramp_size = 10 * 4;
#else
*tramp_size = 9 * 4;
#endif
#else
g_assert_not_reached ();
#endif
}
/*
* arch_emit_unbox_trampoline:
*
* Emit code for the unbox trampoline for METHOD used in the full-aot case.
* CALL_TARGET is the symbol pointing to the native code of METHOD.
*/
static void
arch_emit_unbox_trampoline (MonoAotCompile *acfg, MonoMethod *method, MonoGenericSharingContext *gsctx, const char *call_target)
{
#if defined(TARGET_AMD64)
guint8 buf [32];
guint8 *code;
int this_reg;
this_reg = mono_arch_get_this_arg_reg (mono_method_signature (method), gsctx, NULL);
code = buf;
amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
emit_bytes (acfg, buf, code - buf);
/* jump <method> */
emit_byte (acfg, '\xe9');
emit_symbol_diff (acfg, call_target, ".", -4);
#elif defined(TARGET_ARM)
guint8 buf [128];
guint8 *code;
int this_pos = 0;
code = buf;
if (MONO_TYPE_ISSTRUCT (mono_method_signature (method)->ret))
this_pos = 1;
ARM_ADD_REG_IMM8 (code, this_pos, this_pos, sizeof (MonoObject));
emit_bytes (acfg, buf, code - buf);
/* jump to method */
if (acfg->use_bin_writer) {
guint8 buf [4];
guint8 *code;
code = buf;
ARM_B (code, 0);
img_writer_emit_reloc (acfg->w, R_ARM_JUMP24, call_target, -8);
emit_bytes (acfg, buf, 4);
} else {
fprintf (acfg->fp, "\n\tb %s\n", call_target);
}
#elif defined(TARGET_POWERPC)
int this_pos = 3;
if (MONO_TYPE_ISSTRUCT (mono_method_signature (method)->ret))
this_pos = 4;
g_assert (!acfg->use_bin_writer);
fprintf (acfg->fp, "\n\taddi %d, %d, %d\n", this_pos, this_pos, (int)sizeof (MonoObject));
fprintf (acfg->fp, "\n\tb %s\n", call_target);
#else
g_assert_not_reached ();
#endif
}
/*
* arch_emit_static_rgctx_trampoline:
*
* Emit code for a static rgctx trampoline. OFFSET is the offset of the first of
* two GOT slots which contain the rgctx argument, and the method to jump to.
* TRAMP_SIZE is set to the size of the emitted trampoline.
* These kinds of trampolines cannot be enumerated statically, since there could
* be one trampoline per method instantiation, so we emit the same code for all
* trampolines, and parameterize them using two GOT slots.
*/
static void
arch_emit_static_rgctx_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
{
#if defined(TARGET_AMD64)
/* This should be exactly 13 bytes long */
*tramp_size = 13;
/* mov <OFFSET>(%rip), %r10 */
emit_byte (acfg, '\x4d');
emit_byte (acfg, '\x8b');
emit_byte (acfg, '\x15');
emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
/* jmp *<offset>(%rip) */
emit_byte (acfg, '\xff');
emit_byte (acfg, '\x25');
emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4);
#elif defined(TARGET_ARM)
guint8 buf [128];
guint8 *code;
/* This should be exactly 24 bytes long */
*tramp_size = 24;
code = buf;
/* Load rgctx value */
ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 8);
ARM_LDR_REG_REG (code, MONO_ARCH_RGCTX_REG, ARMREG_PC, ARMREG_IP);
/* Load branch addr + branch */
ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_IP);
g_assert (code - buf == 16);
/* Emit it */
emit_bytes (acfg, buf, code - buf);
emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4 + 8);
emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4 + 4);
#elif defined(TARGET_POWERPC)
guint8 buf [128];
guint8 *code;
*tramp_size = 4;
code = buf;
g_assert (!acfg->use_bin_writer);
/*
* PPC has no ip relative addressing, so we need to compute the address
* of the mscorlib got. That is slow and complex, so instead, we store it
* in the second got slot of every aot image. The caller already computed
* the address of its got and placed it into r30.
*/
img_writer_emit_unset_mode (acfg->w);
/* Load mscorlib got address */
fprintf (acfg->fp, "%s 0, %d(30)\n", PPC_LD_OP, (int)sizeof (gpointer));
/* Load rgctx */
fprintf (acfg->fp, "lis 11, %d@h\n", (int)(offset * sizeof (gpointer)));
fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)(offset * sizeof (gpointer)));
fprintf (acfg->fp, "%s %d, 11, 0\n", PPC_LDX_OP, MONO_ARCH_RGCTX_REG);
/* Load target address */
fprintf (acfg->fp, "lis 11, %d@h\n", (int)((offset + 1) * sizeof (gpointer)));
fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)((offset + 1) * sizeof (gpointer)));
fprintf (acfg->fp, "%s 11, 11, 0\n", PPC_LDX_OP);
#ifdef PPC_USES_FUNCTION_DESCRIPTOR
fprintf (acfg->fp, "%s 2, %d(11)\n", PPC_LD_OP, (int)sizeof (gpointer));
fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
#endif
fprintf (acfg->fp, "mtctr 11\n");
/* Branch to the target address */
fprintf (acfg->fp, "bctr\n");
#ifdef PPC_USES_FUNCTION_DESCRIPTOR
*tramp_size = 11 * 4;
#else
*tramp_size = 9 * 4;
#endif
#else
g_assert_not_reached ();
#endif
}
/*
* arch_emit_imt_thunk:
*
* Emit an IMT thunk usable in full-aot mode. The thunk uses 1 got slot which
* points to an array of pointer pairs. The pairs of the form [key, ptr], where
* key is the IMT key, and ptr holds the address of a memory location holding
* the address to branch to if the IMT arg matches the key. The array is
* terminated by a pair whose key is NULL, and whose ptr is the address of the
* fail_tramp.
* TRAMP_SIZE is set to the size of the emitted trampoline.
*/
static void
arch_emit_imt_thunk (MonoAotCompile *acfg, int offset, int *tramp_size)
{
#if defined(TARGET_AMD64)
guint8 *buf, *code;
guint8 *labels [3];
code = buf = g_malloc (256);
/* FIXME: Optimize this, i.e. use binary search etc. */
/* Maybe move the body into a separate function (slower, but much smaller) */
/* R10 is a free register */
labels [0] = code;
amd64_alu_membase_imm (code, X86_CMP, AMD64_R10, 0, 0);
labels [1] = code;
amd64_branch8 (code, X86_CC_Z, FALSE, 0);
/* Check key */
amd64_alu_membase_reg (code, X86_CMP, AMD64_R10, 0, MONO_ARCH_IMT_REG);
labels [2] = code;
amd64_branch8 (code, X86_CC_Z, FALSE, 0);
/* Loop footer */
amd64_alu_reg_imm (code, X86_ADD, AMD64_R10, 2 * sizeof (gpointer));
amd64_jump_code (code, labels [0]);
/* Match */
mono_amd64_patch (labels [2], code);
amd64_mov_reg_membase (code, AMD64_R10, AMD64_R10, sizeof (gpointer), 8);
amd64_jump_membase (code, AMD64_R10, 0);
/* No match */
/* FIXME: */
mono_amd64_patch (labels [1], code);
x86_breakpoint (code);
/* mov <OFFSET>(%rip), %r10 */
emit_byte (acfg, '\x4d');
emit_byte (acfg, '\x8b');
emit_byte (acfg, '\x15');
emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
emit_bytes (acfg, buf, code - buf);
*tramp_size = code - buf + 7;
#elif defined(TARGET_ARM)
guint8 buf [128];
guint8 *code, *code2, *labels [16];
code = buf;
/* The IMT method is in v5 */
/* Need at least two free registers, plus a slot for storing the pc */
ARM_PUSH (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_R2));
labels [0] = code;
/* Load the parameter from the GOT */
ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
labels [1] = code;
ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R0, 0);
ARM_CMP_REG_REG (code, ARMREG_R1, ARMREG_V5);
labels [2] = code;
ARM_B_COND (code, ARMCOND_EQ, 0);
/* End-of-loop check */
ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
labels [3] = code;
ARM_B_COND (code, ARMCOND_EQ, 0);
/* Loop footer */
ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (gpointer) * 2);
labels [4] = code;
ARM_B (code, 0);
arm_patch (labels [4], labels [1]);
/* Match */
arm_patch (labels [2], code);
ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 4);
ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 0);
/* Save it to the third stack slot */
ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, 8);
/* Restore the registers and branch */
ARM_POP (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_PC));
/* No match */
arm_patch (labels [3], code);
ARM_DBRK (code);
/* Fixup offset */
code2 = labels [0];
ARM_LDR_IMM (code2, ARMREG_R0, ARMREG_PC, (code - (labels [0] + 8)));
emit_bytes (acfg, buf, code - buf);
emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) + (code - (labels [0] + 8)) - 4);
*tramp_size = code - buf + 4;
#elif defined(TARGET_POWERPC)
guint8 buf [128];
guint8 *code, *labels [16];
code = buf;
/* Load the mscorlib got address */
ppc_ldptr (code, ppc_r11, sizeof (gpointer), ppc_r30);
/* Load the parameter from the GOT */
ppc_load (code, ppc_r0, offset * sizeof (gpointer));
ppc_ldptr_indexed (code, ppc_r11, ppc_r11, ppc_r0);
/* Load and check key */
labels [1] = code;
ppc_ldptr (code, ppc_r0, 0, ppc_r11);
ppc_cmp (code, 0, sizeof (gpointer) == 8 ? 1 : 0, ppc_r0, MONO_ARCH_IMT_REG);
labels [2] = code;
ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
/* End-of-loop check */
ppc_cmpi (code, 0, sizeof (gpointer) == 8 ? 1 : 0, ppc_r0, 0);
labels [3] = code;
ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
/* Loop footer */
ppc_addi (code, ppc_r11, ppc_r11, 2 * sizeof (gpointer));
labels [4] = code;
ppc_b (code, 0);
mono_ppc_patch (labels [4], labels [1]);
/* Match */
mono_ppc_patch (labels [2], code);
ppc_ldptr (code, ppc_r11, sizeof (gpointer), ppc_r11);
/* r11 now contains the value of the vtable slot */
/* this is not a function descriptor on ppc64 */
ppc_ldptr (code, ppc_r11, 0, ppc_r11);
ppc_mtctr (code, ppc_r11);
ppc_bcctr (code, PPC_BR_ALWAYS, 0);
/* Fail */
mono_ppc_patch (labels [3], code);
/* FIXME: */
ppc_break (code);
*tramp_size = code - buf;
emit_bytes (acfg, buf, code - buf);
#else
g_assert_not_reached ();
#endif
}
/*
* arch_get_cie_program:
*
* Get the unwind bytecode for the DWARF CIE.
*/
static GSList*
arch_get_cie_program (void)
{
#ifdef TARGET_AMD64
GSList *l = NULL;
mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, AMD64_RSP, 8);
mono_add_unwind_op_offset (l, (guint8*)NULL, (guint8*)NULL, AMD64_RIP, -8);
return l;
#elif defined(TARGET_POWERPC)
GSList *l = NULL;
mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, ppc_r1, 0);
return l;
#else
return NULL;
#endif
}
/* END OF ARCH SPECIFIC CODE */
static guint32
mono_get_field_token (MonoClassField *field)
{
MonoClass *klass = field->parent;
int i;
for (i = 0; i < klass->field.count; ++i) {
if (field == &klass->fields [i])
return MONO_TOKEN_FIELD_DEF | (klass->field.first + 1 + i);
}
g_assert_not_reached ();
return 0;
}
static inline void
encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
{
guint8 *p = buf;
//printf ("ENCODE: %d 0x%x.\n", value, value);
/*
* Same encoding as the one used in the metadata, extended to handle values
* greater than 0x1fffffff.
*/
if ((value >= 0) && (value <= 127))
*p++ = value;
else if ((value >= 0) && (value <= 16383)) {
p [0] = 0x80 | (value >> 8);
p [1] = value & 0xff;
p += 2;
} else if ((value >= 0) && (value <= 0x1fffffff)) {
p [0] = (value >> 24) | 0xc0;
p [1] = (value >> 16) & 0xff;
p [2] = (value >> 8) & 0xff;
p [3] = value & 0xff;
p += 4;
}
else {
p [0] = 0xff;
p [1] = (value >> 24) & 0xff;
p [2] = (value >> 16) & 0xff;
p [3] = (value >> 8) & 0xff;
p [4] = value & 0xff;
p += 5;
}
if (endbuf)
*endbuf = p;
}
static guint32
get_image_index (MonoAotCompile *cfg, MonoImage *image)
{
guint32 index;
index = GPOINTER_TO_UINT (g_hash_table_lookup (cfg->image_hash, image));
if (index)
return index - 1;
else {
index = g_hash_table_size (cfg->image_hash);
g_hash_table_insert (cfg->image_hash, image, GUINT_TO_POINTER (index + 1));
g_ptr_array_add (cfg->image_table, image);
return index;
}
}
static guint32
find_typespec_for_class (MonoAotCompile *acfg, MonoClass *klass)
{
int i;
MonoClass *k = NULL;
/* FIXME: Search referenced images as well */
for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPESPEC].rows; ++i) {
k = mono_class_get_full (acfg->image, MONO_TOKEN_TYPE_SPEC | (i + 1), NULL);
if (k == klass)
break;
}
if (i < acfg->image->tables [MONO_TABLE_TYPESPEC].rows)
return MONO_TOKEN_TYPE_SPEC | (i + 1);
else
return 0;
}
static void
encode_method_ref (MonoAotCompile *acfg, MonoMethod *method, guint8 *buf, guint8 **endbuf);
/*
* encode_klass_ref:
*
* Encode a reference to KLASS. We use our home-grown encoding instead of the
* standard metadata encoding.
*/
static void
encode_klass_ref (MonoAotCompile *acfg, MonoClass *klass, guint8 *buf, guint8 **endbuf)
{
guint8 *p = buf;
if (klass->generic_class) {
guint32 token;
g_assert (klass->type_token);
/* Find a typespec for a class if possible */
token = find_typespec_for_class (acfg, klass);
if (token) {
encode_value (token, p, &p);
encode_value (get_image_index (acfg, acfg->image), p, &p);
} else {
MonoClass *gclass = klass->generic_class->container_class;
MonoGenericInst *inst = klass->generic_class->context.class_inst;
int i;
/* Encode it ourselves */
/* Marker */
encode_value (MONO_TOKEN_TYPE_SPEC, p, &p);
encode_value (MONO_TYPE_GENERICINST, p, &p);
encode_klass_ref (acfg, gclass, p, &p);
encode_value (inst->type_argc, p, &p);
for (i = 0; i < inst->type_argc; ++i)
encode_klass_ref (acfg, mono_class_from_mono_type (inst->type_argv [i]), p, &p);
}
} else if (klass->type_token) {
g_assert (mono_metadata_token_code (klass->type_token) == MONO_TOKEN_TYPE_DEF);
encode_value (klass->type_token - MONO_TOKEN_TYPE_DEF, p, &p);
encode_value (get_image_index (acfg, klass->image), p, &p);
} else if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR)) {
MonoGenericContainer *container = mono_type_get_generic_param_owner (&klass->byval_arg);
g_assert (container);
/* Marker */
encode_value (MONO_TOKEN_TYPE_SPEC, p, &p);
encode_value (klass->byval_arg.type, p, &p);
encode_value (mono_type_get_generic_param_num (&klass->byval_arg), p, &p);
encode_value (container->is_method, p, &p);
if (container->is_method)
encode_method_ref (acfg, container->owner.method, p, &p);
else
encode_klass_ref (acfg, container->owner.klass, p, &p);
} else {
/* Array class */
g_assert (klass->rank > 0);
encode_value (MONO_TOKEN_TYPE_DEF, p, &p);
encode_value (get_image_index (acfg, klass->image), p, &p);
encode_value (klass->rank, p, &p);
encode_klass_ref (acfg, klass->element_class, p, &p);
}
*endbuf = p;
}
static void
encode_field_info (MonoAotCompile *cfg, MonoClassField *field, guint8 *buf, guint8 **endbuf)
{
guint32 token = mono_get_field_token (field);
guint8 *p = buf;
encode_klass_ref (cfg, field->parent, p, &p);
g_assert (mono_metadata_token_code (token) == MONO_TOKEN_FIELD_DEF);
encode_value (token - MONO_TOKEN_FIELD_DEF, p, &p);
*endbuf = p;
}
static void
encode_generic_context (MonoAotCompile *acfg, MonoGenericContext *context, guint8 *buf, guint8 **endbuf)
{
guint8 *p = buf;
int i;
MonoGenericInst *inst;
/* Encode the context */
inst = context->class_inst;
encode_value (inst ? 1 : 0, p, &p);
if (inst) {
encode_value (inst->type_argc, p, &p);
for (i = 0; i < inst->type_argc; ++i)
encode_klass_ref (acfg, mono_class_from_mono_type (inst->type_argv [i]), p, &p);
}
inst = context->method_inst;
encode_value (inst ? 1 : 0, p, &p);
if (inst) {
encode_value (inst->type_argc, p, &p);
for (i = 0; i < inst->type_argc; ++i)
encode_klass_ref (acfg, mono_class_from_mono_type (inst->type_argv [i]), p, &p);
}
*endbuf = p;
}
#define MAX_IMAGE_INDEX 250
static void
encode_method_ref (MonoAotCompile *acfg, MonoMethod *method, guint8 *buf, guint8 **endbuf)
{
guint32 image_index = get_image_index (acfg, method->klass->image);
guint32 token = method->token;
MonoJumpInfoToken *ji;
guint8 *p = buf;
char *name;
/*
* The encoding for most methods is as follows:
* - image index encoded as a leb128
* - token index encoded as a leb128
* Values of image index >= MONO_AOT_METHODREF_MIN are used to mark additional
* types of method encodings.
*/
g_assert (image_index < MONO_AOT_METHODREF_MIN);
/* Mark methods which can't use aot trampolines because they need the further
* processing in mono_magic_trampoline () which requires a MonoMethod*.
*/
if ((method->is_generic && (method->flags & METHOD_ATTRIBUTE_VIRTUAL)) ||
(method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED))
encode_value ((MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE << 24), p, &p);
/*
* Some wrapper methods are shared using their signature, encode their
* stringified signature instead.
* FIXME: Optimize disk usage
*/
name = NULL;
if (method->wrapper_type) {
if (method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
if (strcmp (method->name, "runtime_invoke_dynamic")) {
name = mono_aot_wrapper_name (method);
} else if (mono_marshal_method_from_wrapper (method) != method) {
/* Direct wrapper, encode it normally */
} else {
name = g_strdup_printf ("(wrapper runtime-invoke):%s (%s)", method->name, tmpsig);
}
g_free (tmpsig);
} else if (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE) {
char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
name = g_strdup_printf ("(wrapper delegate-invoke):%s (%s)", method->name, tmpsig);
g_free (tmpsig);
} else if (method->wrapper_type == MONO_WRAPPER_DELEGATE_BEGIN_INVOKE) {
char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
name = g_strdup_printf ("(wrapper delegate-begin-invoke):%s (%s)", method->name, tmpsig);
g_free (tmpsig);
} else if (method->wrapper_type == MONO_WRAPPER_DELEGATE_END_INVOKE) {
char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
name = g_strdup_printf ("(wrapper delegate-end-invoke):%s (%s)", method->name, tmpsig);
g_free (tmpsig);
}
}
if (name) {
encode_value ((MONO_AOT_METHODREF_WRAPPER_NAME << 24), p, &p);
strcpy ((char*)p, name);
p += strlen (name) + 1;
g_free (name);
} else if (method->wrapper_type) {
encode_value ((MONO_AOT_METHODREF_WRAPPER << 24), p, &p);
encode_value (method->wrapper_type, p, &p);
switch (method->wrapper_type) {
case MONO_WRAPPER_REMOTING_INVOKE:
case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
case MONO_WRAPPER_XDOMAIN_INVOKE: {
MonoMethod *m;
m = mono_marshal_method_from_wrapper (method);
g_assert (m);
encode_method_ref (acfg, m, p, &p);
break;
}
case MONO_WRAPPER_PROXY_ISINST:
case MONO_WRAPPER_LDFLD:
case MONO_WRAPPER_LDFLDA:
case MONO_WRAPPER_STFLD:
case MONO_WRAPPER_ISINST: {
MonoClass *proxy_class = (MonoClass*)mono_marshal_method_from_wrapper (method);
encode_klass_ref (acfg, proxy_class, p, &p);
break;
}
case MONO_WRAPPER_LDFLD_REMOTE:
case MONO_WRAPPER_STFLD_REMOTE:
break;
case MONO_WRAPPER_ALLOC: {
int alloc_type = mono_gc_get_managed_allocator_type (method);
g_assert (alloc_type != -1);
encode_value (alloc_type, p, &p);
break;
}
case MONO_WRAPPER_STELEMREF:
break;
case MONO_WRAPPER_UNKNOWN:
if (strcmp (method->name, "FastMonitorEnter") == 0)
encode_value (MONO_AOT_WRAPPER_MONO_ENTER, p, &p);
else if (strcmp (method->name, "FastMonitorExit") == 0)
encode_value (MONO_AOT_WRAPPER_MONO_EXIT, p, &p);
else
g_assert_not_reached ();
break;
case MONO_WRAPPER_SYNCHRONIZED:
case MONO_WRAPPER_MANAGED_TO_NATIVE:
case MONO_WRAPPER_RUNTIME_INVOKE: {
MonoMethod *m;
m = mono_marshal_method_from_wrapper (method);
g_assert (m);
g_assert (m != method);
encode_method_ref (acfg, m, p, &p);
break;
}
default:
g_assert_not_reached ();
}
} else if (mono_method_signature (method)->is_inflated) {
/*
* This is a generic method, find the original token which referenced it and
* encode that.
* Obtain the token from information recorded by the JIT.
*/
ji = g_hash_table_lookup (acfg->token_info_hash, method);
if (ji) {
image_index = get_image_index (acfg, ji->image);
g_assert (image_index < MAX_IMAGE_INDEX);
token = ji->token;
encode_value ((MONO_AOT_METHODREF_METHODSPEC << 24), p, &p);
encode_value (image_index, p, &p);
encode_value (token, p, &p);
} else {
MonoMethod *declaring;
MonoGenericContext *context = mono_method_get_context (method);
g_assert (method->is_inflated);
declaring = ((MonoMethodInflated*)method)->declaring;
/*
* This might be a non-generic method of a generic instance, which
* doesn't have a token since the reference is generated by the JIT
* like Nullable:Box/Unbox, or by generic sharing.
*/
encode_value ((MONO_AOT_METHODREF_GINST << 24), p, &p);
/* Encode the klass */
encode_klass_ref (acfg, method->klass, p, &p);
/* Encode the method */
image_index = get_image_index (acfg, method->klass->image);
g_assert (image_index < MAX_IMAGE_INDEX);
g_assert (declaring->token);
token = declaring->token;
g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
encode_value (image_index, p, &p);
encode_value (token, p, &p);
encode_generic_context (acfg, context, p, &p);
}
} else if (token == 0) {
/* This might be a method of a constructed type like int[,].Set */
/* Obtain the token from information recorded by the JIT */
ji = g_hash_table_lookup (acfg->token_info_hash, method);
if (ji) {
image_index = get_image_index (acfg, ji->image);
g_assert (image_index < MAX_IMAGE_INDEX);
token = ji->token;
encode_value ((MONO_AOT_METHODREF_METHODSPEC << 24), p, &p);
encode_value (image_index, p, &p);
encode_value (token, p, &p);
} else {
/* Array methods */
g_assert (method->klass->rank);
/* Encode directly */
encode_value ((MONO_AOT_METHODREF_ARRAY << 24), p, &p);
encode_klass_ref (acfg, method->klass, p, &p);
if (!strcmp (method->name, ".ctor") && mono_method_signature (method)->param_count == method->klass->rank)
encode_value (0, p, &p);
else if (!strcmp (method->name, ".ctor") && mono_method_signature (method)->param_count == method->klass->rank * 2)
encode_value (1, p, &p);
else if (!strcmp (method->name, "Get"))
encode_value (2, p, &p);
else if (!strcmp (method->name, "Address"))
encode_value (3, p, &p);
else if (!strcmp (method->name, "Set"))
encode_value (4, p, &p);
else
g_assert_not_reached ();
}
} else {
g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
encode_value ((image_index << 24) | mono_metadata_token_index (token), p, &p);
}
*endbuf = p;
}
static gint
compare_patches (gconstpointer a, gconstpointer b)
{
int i, j;
i = (*(MonoJumpInfo**)a)->ip.i;
j = (*(MonoJumpInfo**)b)->ip.i;
if (i < j)
return -1;
else
if (i > j)
return 1;
else
return 0;
}
/*
* is_plt_patch:
*
* Return whenever PATCH_INFO refers to a direct call, and thus requires a
* PLT entry.
*/
static inline gboolean
is_plt_patch (MonoJumpInfo *patch_info)
{
switch (patch_info->type) {
case MONO_PATCH_INFO_METHOD:
case MONO_PATCH_INFO_INTERNAL_METHOD:
case MONO_PATCH_INFO_JIT_ICALL_ADDR:
case MONO_PATCH_INFO_ICALL_ADDR:
case MONO_PATCH_INFO_CLASS_INIT:
case MONO_PATCH_INFO_RGCTX_FETCH:
case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
case MONO_PATCH_INFO_MONITOR_ENTER:
case MONO_PATCH_INFO_MONITOR_EXIT:
return TRUE;
default:
return FALSE;
}
}
static int
get_plt_offset (MonoAotCompile *acfg, MonoJumpInfo *patch_info)
{
int res = -1;
if (is_plt_patch (patch_info)) {
int idx = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->patch_to_plt_offset, patch_info));
if (patch_info->type == MONO_PATCH_INFO_METHOD && (patch_info->data.method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)) {
/*
* Allocate a separate PLT slot for each such patch, since some plt
* entries will refer to the method itself, and some will refer to
* wrapper.
*/
idx = 0;
}
if (idx) {
res = idx;
} else {
MonoJumpInfo *new_ji = mono_patch_info_dup_mp (acfg->mempool, patch_info);
res = acfg->plt_offset;
g_hash_table_insert (acfg->plt_offset_to_patch, GUINT_TO_POINTER (res), new_ji);
g_hash_table_insert (acfg->patch_to_plt_offset, new_ji, GUINT_TO_POINTER (res));
acfg->plt_offset ++;
}
}
return res;
}
/**
* get_got_offset:
*
* Returns the offset of the GOT slot where the runtime object resulting from resolving
* JI could be found if it exists, otherwise allocates a new one.
*/
static guint32
get_got_offset (MonoAotCompile *acfg, MonoJumpInfo *ji)
{
guint32 got_offset;
got_offset = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->patch_to_got_offset_by_type [ji->type], ji));
if (got_offset)
return got_offset - 1;
got_offset = acfg->got_offset;
acfg->got_offset ++;
acfg->stats.got_slots ++;
acfg->stats.got_slot_types [ji->type] ++;
g_hash_table_insert (acfg->patch_to_got_offset, ji, GUINT_TO_POINTER (got_offset + 1));
g_hash_table_insert (acfg->patch_to_got_offset_by_type [ji->type], ji, GUINT_TO_POINTER (got_offset + 1));
g_ptr_array_add (acfg->got_patches, ji);
return got_offset;
}
/* Add a method to the list of methods which need to be emitted */
static void
add_method_with_index (MonoAotCompile *acfg, MonoMethod *method, int index, gboolean extra)
{
g_assert (method);
if (!g_hash_table_lookup (acfg->method_indexes, method)) {
g_ptr_array_add (acfg->methods, method);
g_hash_table_insert (acfg->method_indexes, method, GUINT_TO_POINTER (index + 1));
acfg->nmethods = acfg->methods->len + 1;
}
if (method->wrapper_type || extra)
g_ptr_array_add (acfg->extra_methods, method);
}
static guint32
get_method_index (MonoAotCompile *acfg, MonoMethod *method)
{
int index = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_indexes, method));
g_assert (index);
return index - 1;
}
static int
add_method_full (MonoAotCompile *acfg, MonoMethod *method, gboolean extra, int depth)
{
int index;
index = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_indexes, method));
if (index)
return index - 1;
index = acfg->method_index;
add_method_with_index (acfg, method, index, extra);
/* FIXME: Fix quadratic behavior */
acfg->method_order = g_list_append (acfg->method_order, GUINT_TO_POINTER (index));
g_hash_table_insert (acfg->method_depth, method, GUINT_TO_POINTER (depth));
acfg->method_index ++;
return index;
}
static int
add_method (MonoAotCompile *acfg, MonoMethod *method)
{
return add_method_full (acfg, method, FALSE, 0);
}
static void
add_extra_method (MonoAotCompile *acfg, MonoMethod *method)
{
add_method_full (acfg, method, TRUE, 0);
}
static void
add_extra_method_with_depth (MonoAotCompile *acfg, MonoMethod *method, int depth)
{
add_method_full (acfg, method, TRUE, depth);
}
static void
add_jit_icall_wrapper (gpointer key, gpointer value, gpointer user_data)
{
MonoAotCompile *acfg = user_data;
MonoJitICallInfo *callinfo = value;
MonoMethod *wrapper;
char *name;
if (!callinfo->sig)
return;
name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func, check_for_pending_exc);
g_free (name);
add_method (acfg, wrapper);
}
static MonoMethod*
get_runtime_invoke_sig (MonoMethodSignature *sig)
{
MonoMethodBuilder *mb;
MonoMethod *m;
mb = mono_mb_new (mono_defaults.object_class, "FOO", MONO_WRAPPER_NONE);
m = mono_mb_create_method (mb, sig, 16);
return mono_marshal_get_runtime_invoke (m, FALSE);
}
static gboolean
can_marshal_struct (MonoClass *klass)
{
MonoClassField *field;
gboolean can_marshal = TRUE;
gpointer iter = NULL;
if ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_AUTO_LAYOUT)
return FALSE;
/* Only allow a few field types to avoid asserts in the marshalling code */
while ((field = mono_class_get_fields (klass, &iter))) {
if ((field->type->attrs & FIELD_ATTRIBUTE_STATIC))
continue;
switch (field->type->type) {
case MONO_TYPE_I4:
case MONO_TYPE_U4:
case MONO_TYPE_I1:
case MONO_TYPE_U1:
case MONO_TYPE_BOOLEAN:
case MONO_TYPE_I2:
case MONO_TYPE_U2:
case MONO_TYPE_CHAR:
case MONO_TYPE_I8:
case MONO_TYPE_U8:
case MONO_TYPE_I:
case MONO_TYPE_U:
case MONO_TYPE_PTR:
case MONO_TYPE_R4:
case MONO_TYPE_R8:
case MONO_TYPE_STRING:
break;
case MONO_TYPE_VALUETYPE:
if (!can_marshal_struct (mono_class_from_mono_type (field->type)))
can_marshal = FALSE;
break;
default:
can_marshal = FALSE;
break;
}
}
/* Special cases */
/* Its hard to compute whenever these can be marshalled or not */
if (!strcmp (klass->name_space, "System.Net.NetworkInformation.MacOsStructs"))
return TRUE;
return can_marshal;
}
static void
add_wrappers (MonoAotCompile *acfg)
{
MonoMethod *method, *m;
int i, j;
MonoMethodSignature *sig, *csig;
guint32 token;
/*
* FIXME: Instead of AOTing all the wrappers, it might be better to redesign them
* so there is only one wrapper of a given type, or inlining their contents into their
* callers.
*/
/*
* FIXME: This depends on the fact that different wrappers have different
* names.
*/
for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
MonoMethod *method;
guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
gboolean skip = FALSE;
method = mono_get_method (acfg->image, token, NULL);
if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
(method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
(method->flags & METHOD_ATTRIBUTE_ABSTRACT))
skip = TRUE;
if (method->is_generic || method->klass->generic_container)
skip = TRUE;
/* Skip methods which can not be handled by get_runtime_invoke () */
sig = mono_method_signature (method);
if ((sig->ret->type == MONO_TYPE_PTR) ||
(sig->ret->type == MONO_TYPE_TYPEDBYREF))
skip = TRUE;
for (j = 0; j < sig->param_count; j++) {
if (sig->params [j]->type == MONO_TYPE_TYPEDBYREF)
skip = TRUE;
}
#ifdef MONO_ARCH_DYN_CALL_SUPPORTED
if (!method->klass->contextbound) {
MonoDynCallInfo *info = mono_arch_dyn_call_prepare (sig);
if (info) {
/* Supported by the dynamic runtime-invoke wrapper */
skip = TRUE;
g_free (info);
}
}
#endif
if (!skip) {
//printf ("%s\n", mono_method_full_name (method, TRUE));
add_method (acfg, mono_marshal_get_runtime_invoke (method, FALSE));
}
}
if (strcmp (acfg->image->assembly->aname.name, "mscorlib") == 0) {
#ifdef MONO_ARCH_HAVE_TLS_GET
MonoMethodDesc *desc;
MonoMethod *orig_method;
int nallocators;
#endif
/* Runtime invoke wrappers */
/* void runtime-invoke () [.cctor] */
csig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
csig->ret = &mono_defaults.void_class->byval_arg;
add_method (acfg, get_runtime_invoke_sig (csig));
/* void runtime-invoke () [Finalize] */
csig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
csig->hasthis = 1;
csig->ret = &mono_defaults.void_class->byval_arg;
add_method (acfg, get_runtime_invoke_sig (csig));
/* void runtime-invoke (string) [exception ctor] */
csig = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
csig->hasthis = 1;
csig->ret = &mono_defaults.void_class->byval_arg;
csig->params [0] = &mono_defaults.string_class->byval_arg;
add_method (acfg, get_runtime_invoke_sig (csig));
/* void runtime-invoke (string, string) [exception ctor] */
csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
csig->hasthis = 1;
csig->ret = &mono_defaults.void_class->byval_arg;
csig->params [0] = &mono_defaults.string_class->byval_arg;
csig->params [1] = &mono_defaults.string_class->byval_arg;
add_method (acfg, get_runtime_invoke_sig (csig));
/* string runtime-invoke () [Exception.ToString ()] */
csig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
csig->hasthis = 1;
csig->ret = &mono_defaults.string_class->byval_arg;
add_method (acfg, get_runtime_invoke_sig (csig));
/* void runtime-invoke (string, Exception) [exception ctor] */
csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
csig->hasthis = 1;
csig->ret = &mono_defaults.void_class->byval_arg;
csig->params [0] = &mono_defaults.string_class->byval_arg;
csig->params [1] = &mono_defaults.exception_class->byval_arg;
add_method (acfg, get_runtime_invoke_sig (csig));
/* Assembly runtime-invoke (string, bool) [DoAssemblyResolve] */
csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
csig->hasthis = 1;
csig->ret = &(mono_class_from_name (
mono_defaults.corlib, "System.Reflection", "Assembly"))->byval_arg;
csig->params [0] = &mono_defaults.string_class->byval_arg;
csig->params [1] = &mono_defaults.boolean_class->byval_arg;
add_method (acfg, get_runtime_invoke_sig (csig));
/* runtime-invoke used by finalizers */
add_method (acfg, mono_marshal_get_runtime_invoke (mono_class_get_method_from_name_flags (mono_defaults.object_class, "Finalize", 0, 0), TRUE));
/* This is used by mono_runtime_capture_context () */
method = mono_get_context_capture_method ();
if (method)
add_method (acfg, mono_marshal_get_runtime_invoke (method, FALSE));
#ifdef MONO_ARCH_DYN_CALL_SUPPORTED
add_method (acfg, mono_marshal_get_runtime_invoke_dynamic ());
#endif
/* JIT icall wrappers */
/* FIXME: locking */
g_hash_table_foreach (mono_get_jit_icall_info (), add_jit_icall_wrapper, acfg);
/* stelemref */
add_method (acfg, mono_marshal_get_stelemref ());
#ifdef MONO_ARCH_HAVE_TLS_GET
/* Managed Allocators */
nallocators = mono_gc_get_managed_allocator_types ();
for (i = 0; i < nallocators; ++i) {
m = mono_gc_get_managed_allocator_by_type (i);
if (m)
add_method (acfg, m);
}
/* Monitor Enter/Exit */
desc = mono_method_desc_new ("Monitor:Enter", FALSE);
orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
g_assert (orig_method);
mono_method_desc_free (desc);
method = mono_monitor_get_fast_path (orig_method);
if (method)
add_method (acfg, method);
desc = mono_method_desc_new ("Monitor:Exit", FALSE);
orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
g_assert (orig_method);
mono_method_desc_free (desc);
method = mono_monitor_get_fast_path (orig_method);
if (method)
add_method (acfg, method);
#endif
}
/*
* remoting-invoke-with-check wrappers are very frequent, so avoid emitting them,
* we use the original method instead at runtime.
* Since full-aot doesn't support remoting, this is not a problem.
*/
#if 0
/* remoting-invoke wrappers */
for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
MonoMethodSignature *sig;
token = MONO_TOKEN_METHOD_DEF | (i + 1);
method = mono_get_method (acfg->image, token, NULL);
sig = mono_method_signature (method);
if (sig->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class)) {
m = mono_marshal_get_remoting_invoke_with_check (method);
add_method (acfg, m);
}
}
#endif
/* delegate-invoke wrappers */
for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
MonoClass *klass;
token = MONO_TOKEN_TYPE_DEF | (i + 1);
klass = mono_class_get (acfg->image, token);
if (klass->delegate && klass != mono_defaults.delegate_class && klass != mono_defaults.multicastdelegate_class && !klass->generic_container) {
method = mono_get_delegate_invoke (klass);
m = mono_marshal_get_delegate_invoke (method, NULL);
add_method (acfg, m);
method = mono_class_get_method_from_name_flags (klass, "BeginInvoke", -1, 0);
add_method (acfg, mono_marshal_get_delegate_begin_invoke (method));
method = mono_class_get_method_from_name_flags (klass, "EndInvoke", -1, 0);
add_method (acfg, mono_marshal_get_delegate_end_invoke (method));
}
}
/* Synchronized wrappers */
for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
token = MONO_TOKEN_METHOD_DEF | (i + 1);
method = mono_get_method (acfg->image, token, NULL);
if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED && !method->is_generic)
add_method (acfg, mono_marshal_get_synchronized_wrapper (method));
}
/* pinvoke wrappers */
for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
MonoMethod *method;
guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
method = mono_get_method (acfg->image, token, NULL);
if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
add_method (acfg, mono_marshal_get_native_wrapper (method, TRUE, TRUE));
}
}
/* StructureToPtr/PtrToStructure wrappers */
for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
MonoClass *klass;
token = MONO_TOKEN_TYPE_DEF | (i + 1);
klass = mono_class_get (acfg->image, token);
if (klass->valuetype && !klass->generic_container && can_marshal_struct (klass)) {
add_method (acfg, mono_marshal_get_struct_to_ptr (klass));
add_method (acfg, mono_marshal_get_ptr_to_struct (klass));
}
}
}
static gboolean
has_type_vars (MonoClass *klass)
{
if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR))
return TRUE;
if (klass->rank)
return has_type_vars (klass->element_class);
if (klass->generic_class) {
MonoGenericContext *context = &klass->generic_class->context;
if (context->class_inst) {
int i;
for (i = 0; i < context->class_inst->type_argc; ++i)
if (has_type_vars (mono_class_from_mono_type (context->class_inst->type_argv [i])))
return TRUE;
}
}
return FALSE;
}
static gboolean
method_has_type_vars (MonoMethod *method)
{
if (has_type_vars (method->klass))
return TRUE;
if (method->is_inflated) {
MonoGenericContext *context = mono_method_get_context (method);
if (context->method_inst) {
int i;
for (i = 0; i < context->method_inst->type_argc; ++i)
if (has_type_vars (mono_class_from_mono_type (context->method_inst->type_argv [i])))
return TRUE;
}
}
return FALSE;
}
/*
* add_generic_class:
*
* Add all methods of a generic class.
*/
static void
add_generic_class (MonoAotCompile *acfg, MonoClass *klass)
{
MonoMethod *method;
gpointer iter;
mono_class_init (klass);
if (klass->generic_class && klass->generic_class->context.class_inst->is_open)
return;
if (has_type_vars (klass))
return;
if (!klass->generic_class && !klass->rank)
return;
iter = NULL;
while ((method = mono_class_get_methods (klass, &iter))) {
if (mono_method_is_generic_sharable_impl (method, FALSE))
/* Already added */
continue;
if (method->is_generic)
/* FIXME: */
continue;
/*
* FIXME: Instances which are referenced by these methods are not added,
* for example Array.Resize<int> for List<int>.Add ().
*/
add_extra_method (acfg, method);
}
if (klass->delegate) {
method = mono_get_delegate_invoke (klass);
method = mono_marshal_get_delegate_invoke (method, NULL);
add_method (acfg, method);
}
/*
* For ICollection<T>, add instances of the helper methods
* in Array, since a T[] could be cast to ICollection<T>.
*/
if (klass->image == mono_defaults.corlib && !strcmp (klass->name_space, "System.Collections.Generic") &&
(!strcmp(klass->name, "ICollection`1") || !strcmp (klass->name, "IEnumerable`1") || !strcmp (klass->name, "IList`1") || !strcmp (klass->name, "IEnumerator`1"))) {
MonoClass *tclass = mono_class_from_mono_type (klass->generic_class->context.class_inst->type_argv [0]);
MonoClass *array_class = mono_bounded_array_class_get (tclass, 1, FALSE);
gpointer iter;
char *name_prefix;
if (!strcmp (klass->name, "IEnumerator`1"))
name_prefix = g_strdup_printf ("%s.%s", klass->name_space, "IEnumerable`1");
else
name_prefix = g_strdup_printf ("%s.%s", klass->name_space, klass->name);
/* Add the T[]/InternalEnumerator class */
if (!strcmp (klass->name, "IEnumerable`1") || !strcmp (klass->name, "IEnumerator`1")) {
MonoClass *nclass;
iter = NULL;
while ((nclass = mono_class_get_nested_types (array_class->parent, &iter))) {
if (!strcmp (nclass->name, "InternalEnumerator`1"))
break;
}
g_assert (nclass);
nclass = mono_class_inflate_generic_class (nclass, mono_generic_class_get_context (klass->generic_class));
add_generic_class (acfg, nclass);
}
iter = NULL;
while ((method = mono_class_get_methods (array_class, &iter))) {
if (strstr (method->name, name_prefix)) {
MonoMethod *m = mono_aot_get_array_helper_from_wrapper (method);
add_extra_method (acfg, m);
}
}
g_free (name_prefix);
}
/* Add an instance of GenericComparer<T> which is created dynamically by Comparer<T> */
if (klass->image == mono_defaults.corlib && !strcmp (klass->name_space, "System.Collections.Generic") && !strcmp (klass->name, "Comparer`1")) {
MonoClass *tclass = mono_class_from_mono_type (klass->generic_class->context.class_inst->type_argv [0]);
MonoClass *icomparable, *gcomparer;
MonoGenericContext ctx;
MonoType *args [16];
memset (&ctx, 0, sizeof (ctx));
icomparable = mono_class_from_name (mono_defaults.corlib, "System", "IComparable`1");
g_assert (icomparable);
args [0] = &tclass->byval_arg;
ctx.class_inst = mono_metadata_get_generic_inst (1, args);
if (mono_class_is_assignable_from (mono_class_inflate_generic_class (icomparable, &ctx), tclass)) {
gcomparer = mono_class_from_name (mono_defaults.corlib, "System.Collections.Generic", "GenericComparer`1");
g_assert (gcomparer);
add_generic_class (acfg, mono_class_inflate_generic_class (gcomparer, &ctx));
}
}
}
static void
add_instances_of (MonoAotCompile *acfg, MonoClass *klass, MonoType **insts, int ninsts)
{
int i;
MonoGenericContext ctx;
MonoType *args [16];
memset (&ctx, 0, sizeof (ctx));
for (i = 0; i < ninsts; ++i) {
args [0] = insts [i];
ctx.class_inst = mono_metadata_get_generic_inst (1, args);
add_generic_class (acfg, mono_class_inflate_generic_class (klass, &ctx));
}
}
/*
* add_generic_instances:
*
* Add instances referenced by the METHODSPEC/TYPESPEC table.
*/
static void
add_generic_instances (MonoAotCompile *acfg)
{
int i;
guint32 token;
MonoMethod *method;
MonoMethodHeader *header;
MonoMethodSignature *sig;
MonoGenericContext *context;
for (i = 0; i < acfg->image->tables [MONO_TABLE_METHODSPEC].rows; ++i) {
token = MONO_TOKEN_METHOD_SPEC | (i + 1);
method = mono_get_method (acfg->image, token, NULL);
context = mono_method_get_context (method);
if (context && ((context->class_inst && context->class_inst->is_open) ||
(context->method_inst && context->method_inst->is_open)))
continue;
if (method->klass->image != acfg->image)
continue;
if (mono_method_is_generic_sharable_impl (method, FALSE))
/* Already added */
continue;
add_extra_method (acfg, method);
}
for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPESPEC].rows; ++i) {
MonoClass *klass;
token = MONO_TOKEN_TYPE_SPEC | (i + 1);
klass = mono_class_get (acfg->image, token);
if (!klass || klass->rank)
continue;
add_generic_class (acfg, klass);
}
/* Add types of args/locals */
for (i = 0; i < acfg->methods->len; ++i) {
int j;
method = g_ptr_array_index (acfg->methods, i);
sig = mono_method_signature (method);
if (sig) {
for (j = 0; j < sig->param_count; ++j)
if (sig->params [j]->type == MONO_TYPE_GENERICINST)
add_generic_class (acfg, mono_class_from_mono_type (sig->params [j]));
}
header = mono_method_get_header (method);
if (header) {
for (j = 0; j < header->num_locals; ++j)
if (header->locals [j]->type == MONO_TYPE_GENERICINST)
add_generic_class (acfg, mono_class_from_mono_type (header->locals [j]));
}
}
if (acfg->image == mono_defaults.corlib) {
MonoClass *klass;
MonoType *insts [256];
int ninsts = 0;
insts [ninsts ++] = &mono_defaults.byte_class->byval_arg;
insts [ninsts ++] = &mono_defaults.sbyte_class->byval_arg;
insts [ninsts ++] = &mono_defaults.int16_class->byval_arg;
insts [ninsts ++] = &mono_defaults.uint16_class->byval_arg;
insts [ninsts ++] = &mono_defaults.int32_class->byval_arg;
insts [ninsts ++] = &mono_defaults.uint32_class->byval_arg;
insts [ninsts ++] = &mono_defaults.int64_class->byval_arg;
insts [ninsts ++] = &mono_defaults.uint64_class->byval_arg;
insts [ninsts ++] = &mono_defaults.single_class->byval_arg;
insts [ninsts ++] = &mono_defaults.double_class->byval_arg;
insts [ninsts ++] = &mono_defaults.char_class->byval_arg;
insts [ninsts ++] = &mono_defaults.boolean_class->byval_arg;
/* Add GenericComparer<T> instances for primitive types for Enum.ToString () */
klass = mono_class_from_name (acfg->image, "System.Collections.Generic", "GenericComparer`1");
if (klass)
add_instances_of (acfg, klass, insts, ninsts);
klass = mono_class_from_name (acfg->image, "System.Collections.Generic", "GenericEqualityComparer`1");
if (klass)
add_instances_of (acfg, klass, insts, ninsts);
/* Add instances of the array generic interfaces for primitive types */
/* This will add instances of the InternalArray_ helper methods in Array too */
klass = mono_class_from_name (acfg->image, "System.Collections.Generic", "ICollection`1");
if (klass)
add_instances_of (acfg, klass, insts, ninsts);
klass = mono_class_from_name (acfg->image, "System.Collections.Generic", "IList`1");
if (klass)
add_instances_of (acfg, klass, insts, ninsts);
klass = mono_class_from_name (acfg->image, "System.Collections.Generic", "IEnumerable`1");
if (klass)
add_instances_of (acfg, klass, insts, ninsts);
}
/*
* Add a managed-to-native wrapper of Array.GetGenericValueImpl<object>, which is
* used for all instances of GetGenericValueImpl by the AOT runtime.
*/
{
MonoGenericContext ctx;
MonoType *args [16];
MonoMethod *get_method;
MonoClass *array_klass = mono_array_class_get (mono_defaults.object_class, 1)->parent;
get_method = mono_class_get_method_from_name (array_klass, "GetGenericValueImpl", 2);
if (get_method) {
memset (&ctx, 0, sizeof (ctx));
args [0] = &mono_defaults.object_class->byval_arg;
ctx.method_inst = mono_metadata_get_generic_inst (1, args);
add_extra_method (acfg, mono_marshal_get_native_wrapper (mono_class_inflate_generic_method (get_method, &ctx), TRUE, TRUE));
}
}
}
/*
* is_direct_callable:
*
* Return whenever the method identified by JI is directly callable without
* going through the PLT.
*/
static gboolean
is_direct_callable (MonoAotCompile *acfg, MonoMethod *method, MonoJumpInfo *patch_info)
{
if ((patch_info->type == MONO_PATCH_INFO_METHOD) && (patch_info->data.method->klass->image == acfg->image)) {
MonoCompile *callee_cfg = g_hash_table_lookup (acfg->method_to_cfg, patch_info->data.method);
if (callee_cfg) {
gboolean direct_callable = TRUE;
if (direct_callable && !(!callee_cfg->has_got_slots && (callee_cfg->method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
direct_callable = FALSE;
if ((callee_cfg->method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) && (!method || method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED))
// FIXME: Maybe call the wrapper directly ?
direct_callable = FALSE;
if (direct_callable)
return TRUE;
}
}
return FALSE;
}
/*
* emit_and_reloc_code:
*
* Emit the native code in CODE, handling relocations along the way. If GOT_ONLY
* is true, calls are made through the GOT too. This is used for emitting trampolines
* in full-aot mode, since calls made from trampolines couldn't go through the PLT,
* since trampolines are needed to make PTL work.
*/
static void
emit_and_reloc_code (MonoAotCompile *acfg, MonoMethod *method, guint8 *code, guint32 code_len, MonoJumpInfo *relocs, gboolean got_only)
{
int i, pindex, start_index, method_index;
GPtrArray *patches;
MonoJumpInfo *patch_info;
MonoMethodHeader *header;
gboolean skip, direct_call;
guint32 got_slot;
char direct_call_target [128];
if (method) {
header = mono_method_get_header (method);
method_index = get_method_index (acfg, method);
}
/* Collect and sort relocations */
patches = g_ptr_array_new ();
for (patch_info = relocs; patch_info; patch_info = patch_info->next)
g_ptr_array_add (patches, patch_info);
g_ptr_array_sort (patches, compare_patches);
start_index = 0;
for (i = 0; i < code_len; i++) {
patch_info = NULL;
for (pindex = start_index; pindex < patches->len; ++pindex) {
patch_info = g_ptr_array_index (patches, pindex);
if (patch_info->ip.i >= i)
break;
}
#ifdef MONO_ARCH_AOT_SUPPORTED
skip = FALSE;
if (patch_info && (patch_info->ip.i == i) && (pindex < patches->len)) {
start_index = pindex;
switch (patch_info->type) {
case MONO_PATCH_INFO_NONE:
break;
case MONO_PATCH_INFO_GOT_OFFSET: {
int code_size;
arch_emit_got_offset (acfg, code + i, &code_size);
i += code_size - 1;
skip = TRUE;
patch_info->type = MONO_PATCH_INFO_NONE;
break;
}
default: {
/*
* If this patch is a call, try emitting a direct call instead of
* through a PLT entry. This is possible if the called method is in
* the same assembly and requires no initialization.
*/
direct_call = FALSE;
if ((patch_info->type == MONO_PATCH_INFO_METHOD) && (patch_info->data.method->klass->image == acfg->image)) {
if (!got_only && is_direct_callable (acfg, method, patch_info)) {
MonoCompile *callee_cfg = g_hash_table_lookup (acfg->method_to_cfg, patch_info->data.method);
//printf ("DIRECT: %s %s\n", method ? mono_method_full_name (method, TRUE) : "", mono_method_full_name (callee_cfg->method, TRUE));
direct_call = TRUE;
sprintf (direct_call_target, "%sm_%x", acfg->temp_prefix, get_method_index (acfg, callee_cfg->orig_method));
patch_info->type = MONO_PATCH_INFO_NONE;
acfg->stats.direct_calls ++;
}
acfg->stats.all_calls ++;
}
if (!got_only && !direct_call) {
int plt_offset = get_plt_offset (acfg, patch_info);
if (plt_offset != -1) {
/* This patch has a PLT entry, so we must emit a call to the PLT entry */
direct_call = TRUE;
sprintf (direct_call_target, "%sp_%d", acfg->temp_prefix, plt_offset);
/* Nullify the patch */
patch_info->type = MONO_PATCH_INFO_NONE;
}
}
if (direct_call) {
int call_size;
arch_emit_direct_call (acfg, direct_call_target, &call_size);
i += call_size - 1;
} else {
int code_size;
got_slot = get_got_offset (acfg, patch_info);
arch_emit_got_access (acfg, code + i, got_slot, &code_size);
i += code_size - 1;
}
skip = TRUE;
}
}
}
#endif /* MONO_ARCH_AOT_SUPPORTED */
if (!skip) {
/* Find next patch */
patch_info = NULL;
for (pindex = start_index; pindex < patches->len; ++pindex) {
patch_info = g_ptr_array_index (patches, pindex);
if (patch_info->ip.i >= i)
break;
}
/* Try to emit multiple bytes at once */
if (pindex < patches->len && patch_info->ip.i > i) {
emit_bytes (acfg, code + i, patch_info->ip.i - i);
i = patch_info->ip.i - 1;
} else {
emit_bytes (acfg, code + i, 1);
}
}
}
}
/*
* sanitize_symbol:
*
* Modify SYMBOL so it only includes characters permissible in symbols.
*/
static void
sanitize_symbol (char *symbol)
{
int i, len = strlen (symbol);
for (i = 0; i < len; ++i)
if (!isalnum (symbol [i]) && (symbol [i] != '_'))
symbol [i] = '_';
}
static char*
get_debug_sym (MonoMethod *method, const char *prefix, GHashTable *cache)
{
char *name1, *name2, *cached;
int i, j, len, count;
name1 = mono_method_full_name (method, TRUE);
len = strlen (name1);
name2 = malloc (strlen (prefix) + len + 16);
memcpy (name2, prefix, strlen (prefix));
j = strlen (prefix);
for (i = 0; i < len; ++i) {
if (isalnum (name1 [i])) {
name2 [j ++] = name1 [i];
} else if (name1 [i] == ' ' && name1 [i + 1] == '(' && name1 [i + 2] == ')') {
i += 2;
} else if (name1 [i] == ',' && name1 [i + 1] == ' ') {
name2 [j ++] = '_';
i++;
} else if (name1 [i] == '(' || name1 [i] == ')' || name1 [i] == '>') {
} else
name2 [j ++] = '_';
}
name2 [j] = '\0';
g_free (name1);
count = 0;
while (g_hash_table_lookup (cache, name2)) {
sprintf (name2 + j, "_%d", count);
count ++;
}
cached = g_strdup (name2);
g_hash_table_insert (cache, cached, cached);
return name2;
}
static void
emit_method_code (MonoAotCompile *acfg, MonoCompile *cfg)
{
MonoMethod *method;
int method_index;
guint8 *code;
char *debug_sym = NULL;
char symbol [128];
int func_alignment = AOT_FUNC_ALIGNMENT;
MonoMethodHeader *header;
method = cfg->orig_method;
code = cfg->native_code;
header = mono_method_get_header (method);
method_index = get_method_index (acfg, method);
/* Emit unbox trampoline */
if (acfg->aot_opts.full_aot && cfg->orig_method->klass->valuetype && (method->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
char call_target [256];
if (!method->wrapper_type && !method->is_inflated) {
g_assert (method->token);
sprintf (symbol, "ut_%d", mono_metadata_token_index (method->token) - 1);
} else {
sprintf (symbol, "ut_e_%d", get_method_index (acfg, method));
}
emit_section_change (acfg, ".text", 0);
emit_global (acfg, symbol, TRUE);
emit_label (acfg, symbol);
sprintf (call_target, "%sm_%x", acfg->temp_prefix, method_index);
arch_emit_unbox_trampoline (acfg, cfg->orig_method, cfg->generic_sharing_context, call_target);
}
/* Make the labels local */
sprintf (symbol, "%sm_%x", acfg->temp_prefix, method_index);
emit_alignment (acfg, func_alignment);
emit_label (acfg, symbol);
if (acfg->aot_opts.write_symbols) {
/*
* Write a C style symbol for every method, this has two uses:
* - it works on platforms where the dwarf debugging info is not
* yet supported.
* - it allows the setting of breakpoints of aot-ed methods.
*/
debug_sym = get_debug_sym (method, "", acfg->method_label_hash);
sprintf (symbol, "%sme_%x", acfg->temp_prefix, method_index);
emit_local_symbol (acfg, debug_sym, symbol, TRUE);
emit_label (acfg, debug_sym);
}
if (cfg->verbose_level > 0)
g_print ("Method %s emitted as %s\n", mono_method_full_name (method, TRUE), symbol);
acfg->stats.code_size += cfg->code_len;
acfg->cfgs [method_index]->got_offset = acfg->got_offset;
emit_and_reloc_code (acfg, method, code, cfg->code_len, cfg->patch_info, FALSE);
emit_line (acfg);
if (acfg->aot_opts.write_symbols) {
emit_symbol_size (acfg, debug_sym, ".");
g_free (debug_sym);
}
sprintf (symbol, "%sme_%x", acfg->temp_prefix, method_index);
emit_label (acfg, symbol);
}
/**
* encode_patch:
*
* Encode PATCH_INFO into its disk representation.
*/
static void
encode_patch (MonoAotCompile *acfg, MonoJumpInfo *patch_info, guint8 *buf, guint8 **endbuf)
{
guint8 *p = buf;
switch (patch_info->type) {
case MONO_PATCH_INFO_NONE:
break;
case MONO_PATCH_INFO_IMAGE:
encode_value (get_image_index (acfg, patch_info->data.image), p, &p);
break;
case MONO_PATCH_INFO_MSCORLIB_GOT_ADDR:
break;
case MONO_PATCH_INFO_METHOD_REL:
encode_value ((gint)patch_info->data.offset, p, &p);
break;
case MONO_PATCH_INFO_SWITCH: {
gpointer *table = (gpointer *)patch_info->data.table->table;
int k;
encode_value (patch_info->data.table->table_size, p, &p);
for (k = 0; k < patch_info->data.table->table_size; k++)
encode_value ((int)(gssize)table [k], p, &p);
break;
}
case MONO_PATCH_INFO_METHODCONST:
case MONO_PATCH_INFO_METHOD:
case MONO_PATCH_INFO_METHOD_JUMP:
case MONO_PATCH_INFO_ICALL_ADDR:
case MONO_PATCH_INFO_METHOD_RGCTX:
encode_method_ref (acfg, patch_info->data.method, p, &p);
break;
case MONO_PATCH_INFO_INTERNAL_METHOD:
case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
guint32 len = strlen (patch_info->data.name);
encode_value (len, p, &p);
memcpy (p, patch_info->data.name, len);
p += len;
*p++ = '\0';
break;
}
case MONO_PATCH_INFO_LDSTR: {
guint32 image_index = get_image_index (acfg, patch_info->data.token->image);
guint32 token = patch_info->data.token->token;
g_assert (mono_metadata_token_code (token) == MONO_TOKEN_STRING);
encode_value (image_index, p, &p);
encode_value (patch_info->data.token->token - MONO_TOKEN_STRING, p, &p);
break;
}
case MONO_PATCH_INFO_RVA:
case MONO_PATCH_INFO_DECLSEC:
case MONO_PATCH_INFO_LDTOKEN:
case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
encode_value (get_image_index (acfg, patch_info->data.token->image), p, &p);
encode_value (patch_info->data.token->token, p, &p);
encode_value (patch_info->data.token->has_context, p, &p);
if (patch_info->data.token->has_context)
encode_generic_context (acfg, &patch_info->data.token->context, p, &p);
break;
case MONO_PATCH_INFO_EXC_NAME: {
MonoClass *ex_class;
ex_class =
mono_class_from_name (mono_defaults.exception_class->image,
"System", patch_info->data.target);
g_assert (ex_class);
encode_klass_ref (acfg, ex_class, p, &p);
break;
}
case MONO_PATCH_INFO_R4:
encode_value (*((guint32 *)patch_info->data.target), p, &p);
break;
case MONO_PATCH_INFO_R8:
encode_value (*((guint32 *)patch_info->data.target), p, &p);
encode_value (*(((guint32 *)patch_info->data.target) + 1), p, &p);
break;
case MONO_PATCH_INFO_VTABLE:
case MONO_PATCH_INFO_CLASS:
case MONO_PATCH_INFO_IID:
case MONO_PATCH_INFO_ADJUSTED_IID:
encode_klass_ref (acfg, patch_info->data.klass, p, &p);
break;
case MONO_PATCH_INFO_CLASS_INIT:
case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
encode_klass_ref (acfg, patch_info->data.klass, p, &p);
break;
case MONO_PATCH_INFO_FIELD:
case MONO_PATCH_INFO_SFLDA:
encode_field_info (acfg, patch_info->data.field, p, &p);
break;
case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
break;
case MONO_PATCH_INFO_RGCTX_FETCH: {
MonoJumpInfoRgctxEntry *entry = patch_info->data.rgctx_entry;
encode_method_ref (acfg, entry->method, p, &p);
encode_value (entry->in_mrgctx, p, &p);
encode_value (entry->info_type, p, &p);
encode_value (entry->data->type, p, &p);
encode_patch (acfg, entry->data, p, &p);
break;
}
case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
case MONO_PATCH_INFO_MONITOR_ENTER:
case MONO_PATCH_INFO_MONITOR_EXIT:
case MONO_PATCH_INFO_SEQ_POINT_INFO:
break;
default:
g_warning ("unable to handle jump info %d", patch_info->type);
g_assert_not_reached ();
}
*endbuf = p;
}
static void
encode_patch_list (MonoAotCompile *acfg, GPtrArray *patches, int n_patches, int first_got_offset, guint8 *buf, guint8 **endbuf)
{
guint8 *p = buf;
guint32 pindex, offset;
MonoJumpInfo *patch_info;
encode_value (n_patches, p, &p);
for (pindex = 0; pindex < patches->len; ++pindex) {
patch_info = g_ptr_array_index (patches, pindex);
if (patch_info->type == MONO_PATCH_INFO_NONE)
/* Nothing to do */
continue;
offset = get_got_offset (acfg, patch_info);
encode_value (offset, p, &p);
}
*endbuf = p;
}
static void
emit_method_info (MonoAotCompile *acfg, MonoCompile *cfg)
{
MonoMethod *method;
GList *l;
int pindex, buf_size, n_patches;
guint8 *code;
char symbol [128];
GPtrArray *patches;
MonoJumpInfo *patch_info;
MonoMethodHeader *header;
guint32 method_index;
guint8 *p, *buf;
guint32 first_got_offset;
method = cfg->orig_method;
code = cfg->native_code;
header = mono_method_get_header (method);
method_index = get_method_index (acfg, method);
/* Make the labels local */
sprintf (symbol, "%sm_%x_p", acfg->temp_prefix, method_index);
/* Sort relocations */
patches = g_ptr_array_new ();
for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next)
g_ptr_array_add (patches, patch_info);
g_ptr_array_sort (patches, compare_patches);
first_got_offset = acfg->cfgs [method_index]->got_offset;
/**********************/
/* Encode method info */
/**********************/
buf_size = (patches->len < 1000) ? 40960 : 40960 + (patches->len * 64);
p = buf = g_malloc (buf_size);
if (mono_class_get_cctor (method->klass))
encode_klass_ref (acfg, method->klass, p, &p);
else
/* Not needed when loading the method */
encode_value (0, p, &p);
/* String table */
if (cfg->opt & MONO_OPT_SHARED) {
encode_value (g_list_length (cfg->ldstr_list), p, &p);
for (l = cfg->ldstr_list; l; l = l->next) {
encode_value ((long)l->data, p, &p);
}
}
else
/* Used only in shared mode */
g_assert (!cfg->ldstr_list);
n_patches = 0;
for (pindex = 0; pindex < patches->len; ++pindex) {
patch_info = g_ptr_array_index (patches, pindex);
if ((patch_info->type == MONO_PATCH_INFO_GOT_OFFSET) ||
(patch_info->type == MONO_PATCH_INFO_NONE)) {
patch_info->type = MONO_PATCH_INFO_NONE;
/* Nothing to do */
continue;
}
if ((patch_info->type == MONO_PATCH_INFO_IMAGE) && (patch_info->data.image == acfg->image)) {
/* Stored in a GOT slot initialized at module load time */
patch_info->type = MONO_PATCH_INFO_NONE;
continue;
}
if (is_plt_patch (patch_info)) {
/* Calls are made through the PLT */
patch_info->type = MONO_PATCH_INFO_NONE;
continue;
}
n_patches ++;
}
if (n_patches)
g_assert (cfg->has_got_slots);
encode_patch_list (acfg, patches, n_patches, first_got_offset, p, &p);
acfg->stats.info_size += p - buf;
/* Emit method info */
emit_label (acfg, symbol);
g_assert (p - buf < buf_size);
emit_bytes (acfg, buf, p - buf);
g_free (buf);
}
static guint32
get_unwind_info_offset (MonoAotCompile *acfg, guint8 *encoded, guint32 encoded_len)
{
guint32 cache_index;
guint32 offset;
/* Reuse the unwind module to canonize and store unwind info entries */
cache_index = mono_cache_unwind_info (encoded, encoded_len);
/* Use +/- 1 to distinguish 0s from missing entries */
offset = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->unwind_info_offsets, GUINT_TO_POINTER (cache_index + 1)));
if (offset)
return offset - 1;
else {
guint8 buf [16];
guint8 *p;
/*
* It would be easier to use assembler symbols, but the caller needs an
* offset now.
*/
offset = acfg->unwind_info_offset;
g_hash_table_insert (acfg->unwind_info_offsets, GUINT_TO_POINTER (cache_index + 1), GUINT_TO_POINTER (offset + 1));
g_ptr_array_add (acfg->unwind_ops, GUINT_TO_POINTER (cache_index));
p = buf;
encode_value (encoded_len, p, &p);
acfg->unwind_info_offset += encoded_len + (p - buf);
return offset;
}
}
static void
emit_exception_debug_info (MonoAotCompile *acfg, MonoCompile *cfg)
{
MonoMethod *method;
int i, k, buf_size, method_index;
guint32 debug_info_size;
guint8 *code;
char symbol [128];
MonoMethodHeader *header;
guint8 *p, *buf, *debug_info;
MonoJitInfo *jinfo = cfg->jit_info;
guint32 flags;
gboolean use_unwind_ops = FALSE;
GPtrArray *seq_points;
method = cfg->orig_method;
code = cfg->native_code;
header = mono_method_get_header (method);
method_index = get_method_index (acfg, method);
/* Make the labels local */
sprintf (symbol, "%se_%x_p", acfg->temp_prefix, method_index);
if (!acfg->aot_opts.nodebug) {
mono_debug_serialize_debug_info (cfg, &debug_info, &debug_info_size);
} else {
debug_info = NULL;
debug_info_size = 0;
}
buf_size = header->num_clauses * 256 + debug_info_size + 1024 + (cfg->seq_points ? (cfg->seq_points->len * 16) : 0);
p = buf = g_malloc (buf_size);
#ifdef MONO_ARCH_HAVE_XP_UNWIND
use_unwind_ops = cfg->unwind_ops != NULL;
#endif
seq_points = cfg->seq_points;
flags = (jinfo->has_generic_jit_info ? 1 : 0) | (use_unwind_ops ? 2 : 0) | (header->num_clauses ? 4 : 0) | (seq_points ? 8 : 0);
encode_value (jinfo->code_size, p, &p);
encode_value (flags, p, &p);
if (use_unwind_ops) {
guint32 encoded_len;
guint8 *encoded;
/*
* This is a duplicate of the data in the .debug_frame section, but that
* section cannot be accessed using the dl interface.
*/
encoded = mono_unwind_ops_encode (cfg->unwind_ops, &encoded_len);
encode_value (get_unwind_info_offset (acfg, encoded, encoded_len), p, &p);
g_free (encoded);
} else {
encode_value (jinfo->used_regs, p, &p);
}
/* Exception table */
if (jinfo->num_clauses)
encode_value (jinfo->num_clauses, p, &p);
for (k = 0; k < jinfo->num_clauses; ++k) {
MonoJitExceptionInfo *ei = &jinfo->clauses [k];
encode_value (ei->flags, p, &p);
encode_value (ei->exvar_offset, p, &p);
if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
encode_value ((gint)((guint8*)ei->data.filter - code), p, &p);
else {
if (ei->data.catch_class) {
encode_value (1, p, &p);
encode_klass_ref (acfg, ei->data.catch_class, p, &p);
} else {
encode_value (0, p, &p);
}
}
encode_value ((gint)((guint8*)ei->try_start - code), p, &p);
encode_value ((gint)((guint8*)ei->try_end - code), p, &p);
encode_value ((gint)((guint8*)ei->handler_start - code), p, &p);
}
if (jinfo->has_generic_jit_info) {
MonoGenericJitInfo *gi = mono_jit_info_get_generic_jit_info (jinfo);
encode_value (gi->has_this ? 1 : 0, p, &p);
encode_value (gi->this_reg, p, &p);
encode_value (gi->this_offset, p, &p);
/*
* Need to encode jinfo->method too, since it is not equal to 'method'
* when using generic sharing.
*/
encode_method_ref (acfg, jinfo->method, p, &p);
}
if (seq_points) {
int il_offset, native_offset, last_il_offset, last_native_offset;
encode_value (seq_points->len, p, &p);
last_il_offset = last_native_offset = 0;
for (i = 0; i < seq_points->len; i += 2) {
il_offset = GPOINTER_TO_INT (g_ptr_array_index (seq_points, i));
native_offset = GPOINTER_TO_INT (g_ptr_array_index (seq_points, i + 1));
encode_value (il_offset - last_il_offset, p, &p);
encode_value (native_offset - last_native_offset, p, &p);
last_il_offset = il_offset;
last_native_offset = native_offset;
}
}
g_assert (debug_info_size < buf_size);
encode_value (debug_info_size, p, &p);
if (debug_info_size) {
memcpy (p, debug_info, debug_info_size);
p += debug_info_size;
g_free (debug_info);
}
acfg->stats.ex_info_size += p - buf;
/* Emit info */
emit_label (acfg, symbol);
g_assert (p - buf < buf_size);
emit_bytes (acfg, buf, p - buf);
g_free (buf);
}
static void
emit_klass_info (MonoAotCompile *acfg, guint32 token)
{
MonoClass *klass = mono_class_get (acfg->image, token);
guint8 *p, *buf;
int i, buf_size;
char symbol [128];
gboolean no_special_static, cant_encode;
gpointer iter = NULL;
buf_size = 10240 + (klass->vtable_size * 16);
p = buf = g_malloc (buf_size);
g_assert (klass);
mono_class_init (klass);
mono_class_get_nested_types (klass, &iter);
g_assert (klass->nested_classes_inited);
mono_class_setup_vtable (klass);
/*
* Emit all the information which is required for creating vtables so
* the runtime does not need to create the MonoMethod structures which
* take up a lot of space.
*/
no_special_static = !mono_class_has_special_static_fields (klass);
/* Check whenever we have enough info to encode the vtable */
cant_encode = FALSE;
for (i = 0; i < klass->vtable_size; ++i) {
MonoMethod *cm = klass->vtable [i];
if (cm && mono_method_signature (cm)->is_inflated && !g_hash_table_lookup (acfg->token_info_hash, cm))
cant_encode = TRUE;
}
if (klass->generic_container || cant_encode) {
encode_value (-1, p, &p);
} else {
encode_value (klass->vtable_size, p, &p);
encode_value ((no_special_static << 7) | (klass->has_static_refs << 6) | (klass->has_references << 5) | ((klass->blittable << 4) | ((klass->ext && klass->ext->nested_classes) ? 1 : 0) << 3) | (klass->has_cctor << 2) | (klass->has_finalize << 1) | klass->ghcimpl, p, &p);
if (klass->has_cctor)
encode_method_ref (acfg, mono_class_get_cctor (klass), p, &p);
if (klass->has_finalize)
encode_method_ref (acfg, mono_class_get_finalizer (klass), p, &p);
encode_value (klass->instance_size, p, &p);
encode_value (mono_class_data_size (klass), p, &p);
encode_value (klass->packing_size, p, &p);
encode_value (klass->min_align, p, &p);
for (i = 0; i < klass->vtable_size; ++i) {
MonoMethod *cm = klass->vtable [i];
if (cm)
encode_method_ref (acfg, cm, p, &p);
else
encode_value (0, p, &p);
}
}
acfg->stats.class_info_size += p - buf;
/* Emit the info */
sprintf (symbol, "%sK_I_%x", acfg->temp_prefix, token - MONO_TOKEN_TYPE_DEF - 1);
emit_label (acfg, symbol);
g_assert (p - buf < buf_size);
emit_bytes (acfg, buf, p - buf);
g_free (buf);
}
/*
* Calls made from AOTed code are routed through a table of jumps similar to the
* ELF PLT (Program Linkage Table). The differences are the following:
* - the ELF PLT entries make an indirect jump though the GOT so they expect the
* GOT pointer to be in EBX. We want to avoid this, so our table contains direct
* jumps. This means the jumps need to be patched when the address of the callee is
* known. Initially the PLT entries jump to code which transfers control to the
* AOT runtime through the first PLT entry.
*/
static void
emit_plt (MonoAotCompile *acfg)
{
char symbol [128];
int i;
GHashTable *cache;
cache = g_hash_table_new (g_str_hash, g_str_equal);
emit_line (acfg);
sprintf (symbol, "plt");
emit_section_change (acfg, ".text", 0);
emit_global (acfg, symbol, TRUE);
#ifdef TARGET_X86
/* This section will be made read-write by the AOT loader */
emit_alignment (acfg, mono_pagesize ());
#else
emit_alignment (acfg, 16);
#endif
emit_label (acfg, symbol);
emit_label (acfg, acfg->plt_symbol);
for (i = 0; i < acfg->plt_offset; ++i) {
char label [128];
char *debug_sym = NULL;
sprintf (label, "%sp_%d", acfg->temp_prefix, i);
emit_label (acfg, label);
if (acfg->aot_opts.write_symbols) {
MonoJumpInfo *ji = g_hash_table_lookup (acfg->plt_offset_to_patch, GUINT_TO_POINTER (i));
if (ji) {
switch (ji->type) {
case MONO_PATCH_INFO_METHOD:
debug_sym = get_debug_sym (ji->data.method, "plt_", cache);
break;
case MONO_PATCH_INFO_INTERNAL_METHOD:
debug_sym = g_strdup_printf ("plt__jit_icall_%s", ji->data.name);
break;
case MONO_PATCH_INFO_CLASS_INIT:
debug_sym = g_strdup_printf ("plt__class_init_%s", mono_type_get_name (&ji->data.klass->byval_arg));
sanitize_symbol (debug_sym);
break;
case MONO_PATCH_INFO_RGCTX_FETCH:
debug_sym = g_strdup_printf ("plt__rgctx_fetch_%d", acfg->label_generator ++);
break;
case MONO_PATCH_INFO_ICALL_ADDR: {
char *s = get_debug_sym (ji->data.method, "", cache);
debug_sym = g_strdup_printf ("plt__icall_native_%s", s);
g_free (s);
break;
}
case MONO_PATCH_INFO_JIT_ICALL_ADDR:
debug_sym = g_strdup_printf ("plt__jit_icall_native_%s", ji->data.name);
break;
case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
debug_sym = g_strdup_printf ("plt__generic_class_init");
break;
default:
break;
}
if (debug_sym) {
emit_local_symbol (acfg, debug_sym, NULL, TRUE);
emit_label (acfg, debug_sym);
}
}
}
/*
* The first plt entry is used to transfer code to the AOT loader.
*/
arch_emit_plt_entry (acfg, i);
if (debug_sym) {
emit_symbol_size (acfg, debug_sym, ".");
g_free (debug_sym);
}
}
emit_symbol_size (acfg, acfg->plt_symbol, ".");
sprintf (symbol, "plt_end");
emit_global (acfg, symbol, TRUE);
emit_label (acfg, symbol);
g_hash_table_destroy (cache);
}
static G_GNUC_UNUSED void
emit_trampoline (MonoAotCompile *acfg, const char *name, guint8 *code,
guint32 code_size, int got_offset, MonoJumpInfo *ji, GSList *unwind_ops)
{
char start_symbol [256];
char symbol [256];
guint32 buf_size;
MonoJumpInfo *patch_info;
guint8 *buf, *p;
GPtrArray *patches;
/* Emit code */
sprintf (start_symbol, "%s", name);
emit_section_change (acfg, ".text", 0);
emit_global (acfg, start_symbol, TRUE);
emit_alignment (acfg, 16);
emit_label (acfg, start_symbol);
sprintf (symbol, "%snamed_%s", acfg->temp_prefix, name);
emit_label (acfg, symbol);
/*
* The code should access everything through the GOT, so we pass
* TRUE here.
*/
emit_and_reloc_code (acfg, NULL, code, code_size, ji, TRUE);
emit_symbol_size (acfg, start_symbol, ".");
/* Emit info */
/* Sort relocations */
patches = g_ptr_array_new ();
for (patch_info = ji; patch_info; patch_info = patch_info->next)
if (patch_info->type != MONO_PATCH_INFO_NONE)
g_ptr_array_add (patches, patch_info);
g_ptr_array_sort (patches, compare_patches);
buf_size = patches->len * 128 + 128;
buf = g_malloc (buf_size);
p = buf;
encode_patch_list (acfg, patches, patches->len, got_offset, p, &p);
g_assert (p - buf < buf_size);
sprintf (symbol, "%s_p", name);
emit_section_change (acfg, ".text", 0);
emit_global (acfg, symbol, FALSE);
emit_label (acfg, symbol);
emit_bytes (acfg, buf, p - buf);
/* Emit debug info */
if (unwind_ops) {
char symbol2 [256];
sprintf (symbol, "%s", name);
sprintf (symbol2, "%snamed_%s", acfg->temp_prefix, name);
if (acfg->dwarf)
mono_dwarf_writer_emit_trampoline (acfg->dwarf, symbol, symbol2, NULL, NULL, code_size, unwind_ops);
}
}
static void
emit_trampolines (MonoAotCompile *acfg)
{
char symbol [256];
int i, tramp_got_offset;
MonoAotTrampoline ntype;
#ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
int tramp_type;
guint32 code_size;
MonoJumpInfo *ji;
guint8 *code;
GSList *unwind_ops;
#endif
if (!acfg->aot_opts.full_aot)
return;
g_assert (acfg->image->assembly);
/* Currently, we only emit most trampolines into the mscorlib AOT image. */
if (strcmp (acfg->image->assembly->aname.name, "mscorlib") == 0) {
#ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
/*
* Emit the generic trampolines.
*
* We could save some code by treating the generic trampolines as a wrapper
* method, but that approach has its own complexities, so we choose the simpler
* method.
*/
for (tramp_type = 0; tramp_type < MONO_TRAMPOLINE_NUM; ++tramp_type) {
code = mono_arch_create_trampoline_code_full (tramp_type, &code_size, &ji, &unwind_ops, TRUE);
/* Emit trampoline code */
sprintf (symbol, "generic_trampoline_%d", tramp_type);
emit_trampoline (acfg, symbol, code, code_size, acfg->got_offset, ji, unwind_ops);
}
code = mono_arch_get_nullified_class_init_trampoline (&code_size);
emit_trampoline (acfg, "nullified_class_init_trampoline", code, code_size, acfg->got_offset, NULL, NULL);
#if defined(TARGET_AMD64) && defined(MONO_ARCH_MONITOR_OBJECT_REG)
code = mono_arch_create_monitor_enter_trampoline_full (&code_size, &ji, TRUE);
emit_trampoline (acfg, "monitor_enter_trampoline", code, code_size, acfg->got_offset, ji, NULL);
code = mono_arch_create_monitor_exit_trampoline_full (&code_size, &ji, TRUE);
emit_trampoline (acfg, "monitor_exit_trampoline", code, code_size, acfg->got_offset, ji, NULL);
#endif
code = mono_arch_create_generic_class_init_trampoline_full (&code_size, &ji, TRUE);
emit_trampoline (acfg, "generic_class_init_trampoline", code, code_size, acfg->got_offset, ji, NULL);
/* Emit the exception related code pieces */
code = mono_arch_get_restore_context_full (&code_size, &ji, TRUE);
emit_trampoline (acfg, "restore_context", code, code_size, acfg->got_offset, ji, NULL);
code = mono_arch_get_call_filter_full (&code_size, &ji, TRUE);
emit_trampoline (acfg, "call_filter", code, code_size, acfg->got_offset, ji, NULL);
code = mono_arch_get_throw_exception_full (&code_size, &ji, TRUE);
emit_trampoline (acfg, "throw_exception", code, code_size, acfg->got_offset, ji, NULL);
code = mono_arch_get_rethrow_exception_full (&code_size, &ji, TRUE);
emit_trampoline (acfg, "rethrow_exception", code, code_size, acfg->got_offset, ji, NULL);
code = mono_arch_get_throw_exception_by_name_full (&code_size, &ji, TRUE);
emit_trampoline (acfg, "throw_exception_by_name", code, code_size, acfg->got_offset, ji, NULL);
code = mono_arch_get_throw_corlib_exception_full (&code_size, &ji, TRUE);
emit_trampoline (acfg, "throw_corlib_exception", code, code_size, acfg->got_offset, ji, NULL);
#if defined(TARGET_AMD64)
code = mono_arch_get_throw_pending_exception_full (&code_size, &ji, TRUE);
emit_trampoline (acfg, "throw_pending_exception", code, code_size, acfg->got_offset, ji, NULL);
#endif
for (i = 0; i < 128; ++i) {
int offset;
offset = MONO_RGCTX_SLOT_MAKE_RGCTX (i);
code = mono_arch_create_rgctx_lazy_fetch_trampoline_full (offset, &code_size, &ji, TRUE);
sprintf (symbol, "rgctx_fetch_trampoline_%u", offset);
emit_trampoline (acfg, symbol, code, code_size, acfg->got_offset, ji, NULL);
offset = MONO_RGCTX_SLOT_MAKE_MRGCTX (i);
code = mono_arch_create_rgctx_lazy_fetch_trampoline_full (offset, &code_size, &ji, TRUE);
sprintf (symbol, "rgctx_fetch_trampoline_%u", offset);
emit_trampoline (acfg, symbol, code, code_size, acfg->got_offset, ji, NULL);
}
{
GSList *l;
/* delegate_invoke_impl trampolines */
l = mono_arch_get_delegate_invoke_impls ();
while (l) {
MonoAotTrampInfo *info = l->data;
emit_trampoline (acfg, info->name, info->code, info->code_size, acfg->got_offset, NULL, NULL);
l = l->next;
}
}
#endif /* #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES */
/* Emit trampolines which are numerous */
/*
* These include the following:
* - specific trampolines
* - static rgctx invoke trampolines
* - imt thunks
* These trampolines have the same code, they are parameterized by GOT
* slots.
* They are defined in this file, in the arch_... routines instead of
* in tramp-<ARCH>.c, since it is easier to do it this way.
*/
/*
* When running in aot-only mode, we can't create specific trampolines at
* runtime, so we create a few, and save them in the AOT file.
* Normal trampolines embed their argument as a literal inside the
* trampoline code, we can't do that here, so instead we embed an offset
* which needs to be added to the trampoline address to get the address of
* the GOT slot which contains the argument value.
* The generated trampolines jump to the generic trampolines using another
* GOT slot, which will be setup by the AOT loader to point to the
* generic trampoline code of the given type.
*/
/*
* FIXME: Maybe we should use more specific trampolines (i.e. one class init for
* each class).
*/
emit_section_change (acfg, ".text", 0);
tramp_got_offset = acfg->got_offset;
for (ntype = 0; ntype < MONO_AOT_TRAMP_NUM; ++ntype) {
switch (ntype) {
case MONO_AOT_TRAMP_SPECIFIC:
sprintf (symbol, "specific_trampolines");
break;
case MONO_AOT_TRAMP_STATIC_RGCTX:
sprintf (symbol, "static_rgctx_trampolines");
break;
case MONO_AOT_TRAMP_IMT_THUNK:
sprintf (symbol, "imt_thunks");
break;
default:
g_assert_not_reached ();
}
emit_global (acfg, symbol, TRUE);
emit_alignment (acfg, 16);
emit_label (acfg, symbol);
acfg->trampoline_got_offset_base [ntype] = tramp_got_offset;
for (i = 0; i < acfg->num_trampolines [ntype]; ++i) {
int tramp_size = 0;
switch (ntype) {
case MONO_AOT_TRAMP_SPECIFIC:
arch_emit_specific_trampoline (acfg, tramp_got_offset, &tramp_size);
tramp_got_offset += 2;
break;
case MONO_AOT_TRAMP_STATIC_RGCTX:
arch_emit_static_rgctx_trampoline (acfg, tramp_got_offset, &tramp_size);
tramp_got_offset += 2;
break;
case MONO_AOT_TRAMP_IMT_THUNK:
arch_emit_imt_thunk (acfg, tramp_got_offset, &tramp_size);
tramp_got_offset += 1;
break;
default:
g_assert_not_reached ();
}
if (!acfg->trampoline_size [ntype]) {
g_assert (tramp_size);
acfg->trampoline_size [ntype] = tramp_size;
}
}
}
/* Reserve some entries at the end of the GOT for our use */
acfg->num_trampoline_got_entries = tramp_got_offset - acfg->got_offset;
}
acfg->got_offset += acfg->num_trampoline_got_entries;
}
static gboolean
str_begins_with (const char *str1, const char *str2)
{
int len = strlen (str2);
return strncmp (str1, str2, len) == 0;
}
static void
mono_aot_parse_options (const char *aot_options, MonoAotOptions *opts)
{
gchar **args, **ptr;
args = g_strsplit (aot_options ? aot_options : "", ",", -1);
for (ptr = args; ptr && *ptr; ptr ++) {
const char *arg = *ptr;
if (str_begins_with (arg, "outfile=")) {
opts->outfile = g_strdup (arg + strlen ("outfile="));
} else if (str_begins_with (arg, "save-temps")) {
opts->save_temps = TRUE;
} else if (str_begins_with (arg, "keep-temps")) {
opts->save_temps = TRUE;
} else if (str_begins_with (arg, "write-symbols")) {
opts->write_symbols = TRUE;
} else if (str_begins_with (arg, "metadata-only")) {
opts->metadata_only = TRUE;
} else if (str_begins_with (arg, "bind-to-runtime-version")) {
opts->bind_to_runtime_version = TRUE;
} else if (str_begins_with (arg, "full")) {
opts->full_aot = TRUE;
} else if (str_begins_with (arg, "threads=")) {
opts->nthreads = atoi (arg + strlen ("threads="));
} else if (str_begins_with (arg, "static")) {
opts->static_link = TRUE;
opts->no_dlsym = TRUE;
} else if (str_begins_with (arg, "asmonly")) {
opts->asm_only = TRUE;
} else if (str_begins_with (arg, "asmwriter")) {
opts->asm_writer = TRUE;
} else if (str_begins_with (arg, "nodebug")) {
opts->nodebug = TRUE;
} else if (str_begins_with (arg, "ntrampolines=")) {
opts->ntrampolines = atoi (arg + strlen ("ntrampolines="));
} else if (str_begins_with (arg, "nrgctx-trampolines=")) {
opts->nrgctx_trampolines = atoi (arg + strlen ("nrgctx-trampolines="));
} else if (str_begins_with (arg, "tool-prefix=")) {
opts->tool_prefix = g_strdup (arg + strlen ("tool-prefix="));
} else if (str_begins_with (arg, "soft-debug")) {
opts->soft_debug = TRUE;
} else {
fprintf (stderr, "AOT : Unknown argument '%s'.\n", arg);
exit (1);
}
}
g_strfreev (args);
}
static void
add_token_info_hash (gpointer key, gpointer value, gpointer user_data)
{
MonoMethod *method = (MonoMethod*)key;
MonoJumpInfoToken *ji = (MonoJumpInfoToken*)value;
MonoJumpInfoToken *new_ji = g_new0 (MonoJumpInfoToken, 1);
MonoAotCompile *acfg = user_data;
new_ji->image = ji->image;
new_ji->token = ji->token;
g_hash_table_insert (acfg->token_info_hash, method, new_ji);
}
static gboolean
can_encode_class (MonoAotCompile *acfg, MonoClass *klass)
{
if (klass->type_token)
return TRUE;
if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR))
return TRUE;
if (klass->rank)
return can_encode_class (acfg, klass->element_class);
return FALSE;
}
static gboolean
can_encode_patch (MonoAotCompile *acfg, MonoJumpInfo *patch_info)
{
switch (patch_info->type) {
case MONO_PATCH_INFO_METHOD:
case MONO_PATCH_INFO_METHODCONST: {
MonoMethod *method = patch_info->data.method;
if (method->wrapper_type) {
switch (method->wrapper_type) {
case MONO_WRAPPER_NONE:
case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
case MONO_WRAPPER_XDOMAIN_INVOKE:
case MONO_WRAPPER_STFLD:
case MONO_WRAPPER_LDFLD:
case MONO_WRAPPER_LDFLDA:
case MONO_WRAPPER_LDFLD_REMOTE:
case MONO_WRAPPER_STFLD_REMOTE:
case MONO_WRAPPER_STELEMREF:
case MONO_WRAPPER_ISINST:
case MONO_WRAPPER_PROXY_ISINST:
case MONO_WRAPPER_ALLOC:
case MONO_WRAPPER_REMOTING_INVOKE:
case MONO_WRAPPER_UNKNOWN:
break;
default:
//printf ("Skip (wrapper call): %d -> %s\n", patch_info->type, mono_method_full_name (patch_info->data.method, TRUE));
return FALSE;
}
} else {
if (!method->token) {
/* The method is part of a constructed type like Int[,].Set (). */
if (!g_hash_table_lookup (acfg->token_info_hash, method)) {
if (method->klass->rank)
return TRUE;
return FALSE;
}
}
}
break;
}
case MONO_PATCH_INFO_VTABLE:
case MONO_PATCH_INFO_CLASS_INIT:
case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
case MONO_PATCH_INFO_CLASS:
case MONO_PATCH_INFO_IID:
case MONO_PATCH_INFO_ADJUSTED_IID:
if (!can_encode_class (acfg, patch_info->data.klass)) {
//printf ("Skip: %s\n", mono_type_full_name (&patch_info->data.klass->byval_arg));
return FALSE;
}
break;
case MONO_PATCH_INFO_RGCTX_FETCH: {
MonoJumpInfoRgctxEntry *entry = patch_info->data.rgctx_entry;
if (!can_encode_patch (acfg, entry->data))
return FALSE;
break;
}
default:
break;
}
return TRUE;
}
static void
add_generic_class (MonoAotCompile *acfg, MonoClass *klass);
/*
* compile_method:
*
* AOT compile a given method.
* This function might be called by multiple threads, so it must be thread-safe.
*/
static void
compile_method (MonoAotCompile *acfg, MonoMethod *method)
{
MonoCompile *cfg;
MonoJumpInfo *patch_info;
gboolean skip;
int index, depth;
MonoMethod *wrapped;
if (acfg->aot_opts.metadata_only)
return;
mono_acfg_lock (acfg);
index = get_method_index (acfg, method);
mono_acfg_unlock (acfg);
/* fixme: maybe we can also precompile wrapper methods */
if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
(method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
(method->flags & METHOD_ATTRIBUTE_ABSTRACT)) {
//printf ("Skip (impossible): %s\n", mono_method_full_name (method, TRUE));
return;
}
if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
return;
wrapped = mono_marshal_method_from_wrapper (method);
if (wrapped && (wrapped->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && wrapped->is_generic)
// FIXME: The wrapper should be generic too, but it is not
return;
InterlockedIncrement (&acfg->stats.mcount);
#if 0
if (method->is_generic || method->klass->generic_container) {
InterlockedIncrement (&acfg->stats.genericcount);
return;
}
#endif
//acfg->aot_opts.print_skipped_methods = TRUE;
/*
* Since these methods are the only ones which are compiled with
* AOT support, and they are not used by runtime startup/shutdown code,
* the runtime will not see AOT methods during AOT compilation,so it
* does not need to support them by creating a fake GOT etc.
*/
cfg = mini_method_compile (method, acfg->opts, mono_get_root_domain (), FALSE, TRUE, 0);
if (cfg->exception_type == MONO_EXCEPTION_GENERIC_SHARING_FAILED) {
//printf ("F: %s\n", mono_method_full_name (method, TRUE));
InterlockedIncrement (&acfg->stats.genericcount);
return;
}
if (cfg->exception_type != MONO_EXCEPTION_NONE) {
/* Let the exception happen at runtime */
return;
}
if (cfg->disable_aot) {
if (acfg->aot_opts.print_skipped_methods)
printf ("Skip (disabled): %s\n", mono_method_full_name (method, TRUE));
InterlockedIncrement (&acfg->stats.ocount);
mono_destroy_compile (cfg);
return;
}
/* Nullify patches which need no aot processing */
for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
switch (patch_info->type) {
case MONO_PATCH_INFO_LABEL:
case MONO_PATCH_INFO_BB:
patch_info->type = MONO_PATCH_INFO_NONE;
break;
default:
break;
}
}
/* Collect method->token associations from the cfg */
mono_acfg_lock (acfg);
g_hash_table_foreach (cfg->token_info_hash, add_token_info_hash, acfg);
mono_acfg_unlock (acfg);
/*
* Check for absolute addresses.
*/
skip = FALSE;
for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
switch (patch_info->type) {
case MONO_PATCH_INFO_ABS:
/* unable to handle this */
skip = TRUE;
break;
default:
break;
}
}
if (skip) {
if (acfg->aot_opts.print_skipped_methods)
printf ("Skip (abs call): %s\n", mono_method_full_name (method, TRUE));
InterlockedIncrement (&acfg->stats.abscount);
mono_destroy_compile (cfg);
return;
}
/* Lock for the rest of the code */
mono_acfg_lock (acfg);
/*
* Check for methods/klasses we can't encode.
*/
skip = FALSE;
for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
if (!can_encode_patch (acfg, patch_info))
skip = TRUE;
}
if (skip) {
if (acfg->aot_opts.print_skipped_methods)
printf ("Skip (patches): %s\n", mono_method_full_name (method, TRUE));
acfg->stats.ocount++;
mono_destroy_compile (cfg);
mono_acfg_unlock (acfg);
return;
}
/* Adds generic instances referenced by this method */
/*
* The depth is used to avoid infinite loops when generic virtual recursion is
* encountered.
*/
depth = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_depth, method));
if (depth < 32) {
for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
switch (patch_info->type) {
case MONO_PATCH_INFO_METHOD: {
MonoMethod *m = patch_info->data.method;
if (m->is_inflated) {
if (!(mono_class_generic_sharing_enabled (m->klass) &&
mono_method_is_generic_sharable_impl (m, FALSE)) &&
!method_has_type_vars (m)) {
if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
if (acfg->aot_opts.full_aot)
add_extra_method_with_depth (acfg, mono_marshal_get_native_wrapper (m, TRUE, TRUE), depth + 1);
} else {
add_extra_method_with_depth (acfg, m, depth + 1);
}
}
add_generic_class (acfg, m->klass);
}
break;
}
case MONO_PATCH_INFO_VTABLE: {
MonoClass *klass = patch_info->data.klass;
if (klass->generic_class && !mono_generic_context_is_sharable (&klass->generic_class->context, FALSE))
add_generic_class (acfg, klass);
break;
}
default:
break;
}
}
}
/* Determine whenever the method has GOT slots */
for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
switch (patch_info->type) {
case MONO_PATCH_INFO_GOT_OFFSET:
case MONO_PATCH_INFO_NONE:
break;
case MONO_PATCH_INFO_IMAGE:
/* The assembly is stored in GOT slot 0 */
if (patch_info->data.image != acfg->image)
cfg->has_got_slots = TRUE;
break;
default:
if (!is_plt_patch (patch_info))
cfg->has_got_slots = TRUE;
break;
}
}
if (!cfg->has_got_slots)
InterlockedIncrement (&acfg->stats.methods_without_got_slots);
/* Make a copy of the patch info which is in the mempool */
{
MonoJumpInfo *patches = NULL, *patches_end = NULL;
for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
MonoJumpInfo *new_patch_info = mono_patch_info_dup_mp (acfg->mempool, patch_info);
if (!patches)
patches = new_patch_info;
else
patches_end->next = new_patch_info;
patches_end = new_patch_info;
}
cfg->patch_info = patches;
}
/* Make a copy of the unwind info */
{
GSList *l, *unwind_ops;
MonoUnwindOp *op;
unwind_ops = NULL;
for (l = cfg->unwind_ops; l; l = l->next) {
op = mono_mempool_alloc (acfg->mempool, sizeof (MonoUnwindOp));
memcpy (op, l->data, sizeof (MonoUnwindOp));
unwind_ops = g_slist_prepend_mempool (acfg->mempool, unwind_ops, op);
}
cfg->unwind_ops = g_slist_reverse (unwind_ops);
}
/* Make a copy of the argument/local info */
{
MonoInst **args, **locals;
MonoMethodSignature *sig;
MonoMethodHeader *header;
int i;
sig = mono_method_signature (method);
args = mono_mempool_alloc (acfg->mempool, sizeof (MonoInst*) * (sig->param_count + sig->hasthis));
for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
args [i] = mono_mempool_alloc (acfg->mempool, sizeof (MonoInst));
memcpy (args [i], cfg->args [i], sizeof (MonoInst));
}
cfg->args = args;
header = mono_method_get_header (method);
locals = mono_mempool_alloc (acfg->mempool, sizeof (MonoInst*) * header->num_locals);
for (i = 0; i < header->num_locals; ++i) {
locals [i] = mono_mempool_alloc (acfg->mempool, sizeof (MonoInst));
memcpy (locals [i], cfg->locals [i], sizeof (MonoInst));
}
cfg->locals = locals;
}
/* Free some fields used by cfg to conserve memory */
mono_mempool_destroy (cfg->mempool);
cfg->mempool = NULL;
g_free (cfg->varinfo);
cfg->varinfo = NULL;
g_free (cfg->vars);
cfg->vars = NULL;
if (cfg->rs) {
mono_regstate_free (cfg->rs);
cfg->rs = NULL;
}
//printf ("Compile: %s\n", mono_method_full_name (method, TRUE));
while (index >= acfg->cfgs_size) {
MonoCompile **new_cfgs;
int new_size;
new_size = acfg->cfgs_size * 2;
new_cfgs = g_new0 (MonoCompile*, new_size);
memcpy (new_cfgs, acfg->cfgs, sizeof (MonoCompile*) * acfg->cfgs_size);
g_free (acfg->cfgs);
acfg->cfgs = new_cfgs;
acfg->cfgs_size = new_size;
}
acfg->cfgs [index] = cfg;
g_hash_table_insert (acfg->method_to_cfg, cfg->orig_method, cfg);
/*
if (cfg->orig_method->wrapper_type)
g_ptr_array_add (acfg->extra_methods, cfg->orig_method);
*/
mono_acfg_unlock (acfg);
InterlockedIncrement (&acfg->stats.ccount);
}
static void
compile_thread_main (gpointer *user_data)
{
MonoDomain *domain = user_data [0];
MonoAotCompile *acfg = user_data [1];
GPtrArray *methods = user_data [2];
int i;
mono_thread_attach (domain);
for (i = 0; i < methods->len; ++i)
compile_method (acfg, g_ptr_array_index (methods, i));
}
static void
load_profile_files (MonoAotCompile *acfg)
{
FILE *infile;
char *tmp;
int file_index, res, method_index, i;
char ver [256];
guint32 token;
GList *unordered;
file_index = 0;
while (TRUE) {
tmp = g_strdup_printf ("%s/.mono/aot-profile-data/%s-%s-%d", g_get_home_dir (), acfg->image->assembly_name, acfg->image->guid, file_index);
if (!g_file_test (tmp, G_FILE_TEST_IS_REGULAR)) {
g_free (tmp);
break;
}
infile = fopen (tmp, "r");
g_assert (infile);
printf ("Using profile data file '%s'\n", tmp);
g_free (tmp);
file_index ++;
res = fscanf (infile, "%32s\n", ver);
if ((res != 1) || strcmp (ver, "#VER:1") != 0) {
printf ("Profile file has wrong version or invalid.\n");
fclose (infile);
continue;
}
while (TRUE) {
res = fscanf (infile, "%d\n", &token);
if (res < 1)
break;
method_index = mono_metadata_token_index (token) - 1;
if (!g_list_find (acfg->method_order, GUINT_TO_POINTER (method_index)))
acfg->method_order = g_list_append (acfg->method_order, GUINT_TO_POINTER (method_index));
}
fclose (infile);
}
/* Add missing methods */
unordered = NULL;
for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
if (!g_list_find (acfg->method_order, GUINT_TO_POINTER (i)))
unordered = g_list_prepend (unordered, GUINT_TO_POINTER (i));
}
unordered = g_list_reverse (unordered);
if (acfg->method_order)
g_list_last (acfg->method_order)->next = unordered;
else
acfg->method_order = unordered;
}
static void
emit_code (MonoAotCompile *acfg)
{
int i;
char symbol [256];
GList *l;
#if defined(TARGET_POWERPC64)
sprintf (symbol, ".Lgot_addr");
emit_section_change (acfg, ".text", 0);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
emit_pointer (acfg, acfg->got_symbol);
#endif
sprintf (symbol, "methods");
emit_section_change (acfg, ".text", 0);
emit_global (acfg, symbol, TRUE);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
/*
* Emit some padding so the local symbol for the first method doesn't have the
* same address as 'methods'.
*/
emit_zero_bytes (acfg, 16);
for (l = acfg->method_order; l != NULL; l = l->next) {
i = GPOINTER_TO_UINT (l->data);
if (acfg->cfgs [i])
emit_method_code (acfg, acfg->cfgs [i]);
}
sprintf (symbol, "methods_end");
emit_section_change (acfg, ".text", 0);
emit_global (acfg, symbol, FALSE);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
sprintf (symbol, "method_offsets");
emit_section_change (acfg, ".text", 1);
emit_global (acfg, symbol, FALSE);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
for (i = 0; i < acfg->nmethods; ++i) {
if (acfg->cfgs [i]) {
sprintf (symbol, "%sm_%x", acfg->temp_prefix, i);
emit_symbol_diff (acfg, symbol, "methods", 0);
} else {
emit_int32 (acfg, 0xffffffff);
}
}
emit_line (acfg);
}
static void
emit_info (MonoAotCompile *acfg)
{
int i;
char symbol [256];
GList *l;
/* Emit method info */
sprintf (symbol, "method_info");
emit_section_change (acfg, ".text", 1);
emit_global (acfg, symbol, FALSE);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
/* To reduce size of generated assembly code */
sprintf (symbol, "mi");
emit_label (acfg, symbol);
for (l = acfg->method_order; l != NULL; l = l->next) {
i = GPOINTER_TO_UINT (l->data);
if (acfg->cfgs [i])
emit_method_info (acfg, acfg->cfgs [i]);
}
sprintf (symbol, "method_info_offsets");
emit_section_change (acfg, ".text", 1);
emit_global (acfg, symbol, FALSE);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
for (i = 0; i < acfg->nmethods; ++i) {
if (acfg->cfgs [i]) {
sprintf (symbol, "%sm_%x_p", acfg->temp_prefix, i);
emit_symbol_diff (acfg, symbol, "mi", 0);
} else {
emit_int32 (acfg, 0);
}
}
emit_line (acfg);
}
#endif /* #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */
/*
* mono_aot_str_hash:
*
* Hash function for strings which we use to hash strings for things which are
* saved in the AOT image, since g_str_hash () can change.
*/
guint
mono_aot_str_hash (gconstpointer v1)
{
/* Same as g_str_hash () in glib */
char *p = (char *) v1;
guint hash = *p;
while (*p++) {
if (*p)
hash = (hash << 5) - hash + *p;
}
return hash;
}
#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
#define mix(a,b,c) { \
a -= c; a ^= rot(c, 4); c += b; \
b -= a; b ^= rot(a, 6); a += c; \
c -= b; c ^= rot(b, 8); b += a; \
a -= c; a ^= rot(c,16); c += b; \
b -= a; b ^= rot(a,19); a += c; \
c -= b; c ^= rot(b, 4); b += a; \
}
#define final(a,b,c) { \
c ^= b; c -= rot(b,14); \
a ^= c; a -= rot(c,11); \
b ^= a; b -= rot(a,25); \
c ^= b; c -= rot(b,16); \
a ^= c; a -= rot(c,4); \
b ^= a; b -= rot(a,14); \
c ^= b; c -= rot(b,24); \
}
static guint
mono_aot_type_hash (MonoType *t1)
{
guint hash = t1->type;
hash |= t1->byref << 6; /* do not collide with t1->type values */
switch (t1->type) {
case MONO_TYPE_VALUETYPE:
case MONO_TYPE_CLASS:
case MONO_TYPE_SZARRAY:
/* check if the distribution is good enough */
return ((hash << 5) - hash) ^ mono_aot_str_hash (t1->data.klass->name);
case MONO_TYPE_PTR:
return ((hash << 5) - hash) ^ mono_aot_type_hash (t1->data.type);
case MONO_TYPE_ARRAY:
return ((hash << 5) - hash) ^ mono_aot_type_hash (&t1->data.array->eklass->byval_arg);
case MONO_TYPE_GENERICINST:
return ((hash << 5) - hash) ^ 0;
}
return hash;
}
/*
* mono_aot_method_hash:
*
* Return a hash code for methods which only depends on metadata.
*/
guint32
mono_aot_method_hash (MonoMethod *method)
{
MonoMethodSignature *sig;
MonoClass *klass;
int i;
int hashes_count;
guint32 *hashes_start, *hashes;
guint32 a, b, c;
/* Similar to the hash in mono_method_get_imt_slot () */
sig = mono_method_signature (method);
hashes_count = sig->param_count + 5;
hashes_start = malloc (hashes_count * sizeof (guint32));
hashes = hashes_start;
/* Some wrappers are assigned to random classes */
if (!method->wrapper_type || method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
klass = method->klass;
else
klass = mono_defaults.object_class;
if (!method->wrapper_type) {
char *full_name = mono_type_full_name (&klass->byval_arg);
hashes [0] = mono_aot_str_hash (full_name);
hashes [1] = 0;
g_free (full_name);
} else {
hashes [0] = mono_aot_str_hash (klass->name);
hashes [1] = mono_aot_str_hash (klass->name_space);
}
if (method->wrapper_type == MONO_WRAPPER_STFLD || method->wrapper_type == MONO_WRAPPER_LDFLD || method->wrapper_type == MONO_WRAPPER_LDFLDA)
/* The method name includes a stringified pointer */
hashes [2] = 0;
else
hashes [2] = mono_aot_str_hash (method->name);
hashes [3] = method->wrapper_type;
hashes [4] = mono_aot_type_hash (sig->ret);
for (i = 0; i < sig->param_count; i++) {
hashes [5 + i] = mono_aot_type_hash (sig->params [i]);
}
/* Setup internal state */
a = b = c = 0xdeadbeef + (((guint32)hashes_count)<<2);
/* Handle most of the hashes */
while (hashes_count > 3) {
a += hashes [0];
b += hashes [1];
c += hashes [2];
mix (a,b,c);
hashes_count -= 3;
hashes += 3;
}
/* Handle the last 3 hashes (all the case statements fall through) */
switch (hashes_count) {
case 3 : c += hashes [2];
case 2 : b += hashes [1];
case 1 : a += hashes [0];
final (a,b,c);
case 0: /* nothing left to add */
break;
}
free (hashes_start);
return c;
}
#undef rot
#undef mix
#undef final
/*
* mono_aot_wrapper_name:
*
* Return a string which uniqely identifies the given wrapper method.
*/
char*
mono_aot_wrapper_name (MonoMethod *method)
{
char *name, *tmpsig, *klass_desc;
tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
switch (method->wrapper_type) {
case MONO_WRAPPER_RUNTIME_INVOKE:
if (!strcmp (method->name, "runtime_invoke_dynamic"))
name = g_strdup_printf ("(wrapper runtime-invoke-dynamic)");
else
name = g_strdup_printf ("%s (%s)", method->name, tmpsig);
break;
case MONO_WRAPPER_DELEGATE_INVOKE:
case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE:
case MONO_WRAPPER_DELEGATE_END_INVOKE:
/* This is a hack to work around the fact that these wrappers get assigned to some random class */
name = g_strdup_printf ("%s (%s)", method->name, tmpsig);
break;
default:
klass_desc = mono_type_full_name (&method->klass->byval_arg);
name = g_strdup_printf ("%s:%s (%s)", klass_desc, method->name, tmpsig);
g_free (klass_desc);
break;
}
g_free (tmpsig);
return name;
}
/*
* mono_aot_get_array_helper_from_wrapper;
*
* Get the helper method in Array called by an array wrapper method.
*/
MonoMethod*
mono_aot_get_array_helper_from_wrapper (MonoMethod *method)
{
MonoMethod *m;
const char *prefix;
MonoGenericContext ctx;
MonoType *args [16];
char *mname, *iname, *s, *s2, *helper_name = NULL;
prefix = "System.Collections.Generic";
s = g_strdup_printf ("%s", method->name + strlen (prefix) + 1);
s2 = strstr (s, "`1.");
g_assert (s2);
s2 [0] = '\0';
iname = s;
mname = s2 + 3;
//printf ("X: %s %s\n", iname, mname);
if (!strcmp (iname, "IList"))
helper_name = g_strdup_printf ("InternalArray__%s", mname);
else
helper_name = g_strdup_printf ("InternalArray__%s_%s", iname, mname);
m = mono_class_get_method_from_name (mono_defaults.array_class, helper_name, mono_method_signature (method)->param_count);
g_assert (m);
g_free (helper_name);
g_free (s);
if (m->is_generic) {
memset (&ctx, 0, sizeof (ctx));
args [0] = &method->klass->element_class->byval_arg;
ctx.method_inst = mono_metadata_get_generic_inst (1, args);
m = mono_class_inflate_generic_method (m, &ctx);
}
return m;
}
/*
* mono_aot_tramp_info_create:
*
* Create a MonoAotTrampInfo structure from the arguments.
*/
MonoAotTrampInfo*
mono_aot_tramp_info_create (const char *name, guint8 *code, guint32 code_size)
{
MonoAotTrampInfo *info = g_new0 (MonoAotTrampInfo, 1);
info->name = (char*)name;
info->code = code;
info->code_size = code_size;
return info;
}
#if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
typedef struct HashEntry {
guint32 key, value, index;
struct HashEntry *next;
} HashEntry;
/*
* emit_extra_methods:
*
* Emit methods which are not in the METHOD table, like wrappers.
*/
static void
emit_extra_methods (MonoAotCompile *acfg)
{
int i, table_size, buf_size;
char symbol [256];
guint8 *p, *buf;
guint32 *info_offsets;
guint32 hash;
GPtrArray *table;
HashEntry *entry, *new_entry;
int nmethods, max_chain_length;
int *chain_lengths;
info_offsets = g_new0 (guint32, acfg->extra_methods->len);
buf_size = acfg->extra_methods->len * 256 + 256;
p = buf = g_malloc (buf_size);
/* Encode method info */
nmethods = 0;
/* So offsets are > 0 */
*p = 0;
p++;
for (i = 0; i < acfg->extra_methods->len; ++i) {
MonoMethod *method = g_ptr_array_index (acfg->extra_methods, i);
MonoCompile *cfg = g_hash_table_lookup (acfg->method_to_cfg, method);
char *name;
if (!cfg)
continue;
nmethods ++;
info_offsets [i] = p - buf;
name = NULL;
if (method->wrapper_type) {
/*
* We encode some wrappers using their name, since encoding them
* directly would be difficult. This also avoids creating the wrapper
* methods at runtime, since they are not needed anyway.
*/
switch (method->wrapper_type) {
case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
case MONO_WRAPPER_SYNCHRONIZED:
/* encode_method_ref () can handle these */
break;
case MONO_WRAPPER_RUNTIME_INVOKE:
if (mono_marshal_method_from_wrapper (method) != method && !strstr (method->name, "virtual"))
/* Direct wrapper, encode normally */
break;
/* Fall through */
default:
name = mono_aot_wrapper_name (method);
break;
}
}
if (name) {
encode_value (1, p, &p);
encode_value (method->wrapper_type, p, &p);
strcpy ((char*)p, name);
p += strlen (name ) + 1;
g_free (name);
} else {
encode_value (0, p, &p);
encode_method_ref (acfg, method, p, &p);
}
g_assert ((p - buf) < buf_size);
}
g_assert ((p - buf) < buf_size);
/* Emit method info */
sprintf (symbol, "extra_method_info");
emit_section_change (acfg, ".text", 1);
emit_global (acfg, symbol, FALSE);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
emit_bytes (acfg, buf, p - buf);
emit_line (acfg);
/*
* Construct a chained hash table for mapping indexes in extra_method_info to
* method indexes.
*/
table_size = g_spaced_primes_closest ((int)(nmethods * 1.5));
table = g_ptr_array_sized_new (table_size);
for (i = 0; i < table_size; ++i)
g_ptr_array_add (table, NULL);
chain_lengths = g_new0 (int, table_size);
max_chain_length = 0;
for (i = 0; i < acfg->extra_methods->len; ++i) {
MonoMethod *method = g_ptr_array_index (acfg->extra_methods, i);
MonoCompile *cfg = g_hash_table_lookup (acfg->method_to_cfg, method);
guint32 key, value;
if (!cfg)
continue;
key = info_offsets [i];
value = get_method_index (acfg, method);
hash = mono_aot_method_hash (method) % table_size;
chain_lengths [hash] ++;
max_chain_length = MAX (max_chain_length, chain_lengths [hash]);
/* FIXME: Allocate from the mempool */
new_entry = g_new0 (HashEntry, 1);
new_entry->key = key;
new_entry->value = value;
entry = g_ptr_array_index (table, hash);
if (entry == NULL) {
new_entry->index = hash;
g_ptr_array_index (table, hash) = new_entry;
} else {
while (entry->next)
entry = entry->next;
entry->next = new_entry;
new_entry->index = table->len;
g_ptr_array_add (table, new_entry);
}
}
//printf ("MAX: %d\n", max_chain_length);
/* Emit the table */
sprintf (symbol, "extra_method_table");
emit_section_change (acfg, ".text", 0);
emit_global (acfg, symbol, FALSE);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
emit_int32 (acfg, table_size);
for (i = 0; i < table->len; ++i) {
HashEntry *entry = g_ptr_array_index (table, i);
if (entry == NULL) {
emit_int32 (acfg, 0);
emit_int32 (acfg, 0);
emit_int32 (acfg, 0);
} else {
g_assert (entry->key > 0);
emit_int32 (acfg, entry->key);
emit_int32 (acfg, entry->value);
if (entry->next)
emit_int32 (acfg, entry->next->index);
else
emit_int32 (acfg, 0);
}
}
/*
* Emit a table reverse mapping method indexes to their index in extra_method_info.
* This is used by mono_aot_find_jit_info ().
*/
sprintf (symbol, "extra_method_info_offsets");
emit_section_change (acfg, ".text", 0);
emit_global (acfg, symbol, FALSE);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
emit_int32 (acfg, acfg->extra_methods->len);
for (i = 0; i < acfg->extra_methods->len; ++i) {
MonoMethod *method = g_ptr_array_index (acfg->extra_methods, i);
emit_int32 (acfg, get_method_index (acfg, method));
emit_int32 (acfg, info_offsets [i]);
}
}
static void
emit_method_order (MonoAotCompile *acfg)
{
int i, index, len;
char symbol [256];
GList *l;
sprintf (symbol, "method_order");
emit_section_change (acfg, ".text", 1);
emit_global (acfg, symbol, FALSE);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
/* First emit an index table */
index = 0;
len = 0;
for (l = acfg->method_order; l != NULL; l = l->next) {
i = GPOINTER_TO_UINT (l->data);
if (acfg->cfgs [i]) {
if ((index % 1024) == 0) {
emit_int32 (acfg, i);
}
index ++;
}
len ++;
}
emit_int32 (acfg, 0xffffff);
/* Then emit the whole method order */
for (l = acfg->method_order; l != NULL; l = l->next) {
i = GPOINTER_TO_UINT (l->data);
if (acfg->cfgs [i]) {
emit_int32 (acfg, i);
}
}
emit_line (acfg);
sprintf (symbol, "method_order_end");
emit_section_change (acfg, ".text", 1);
emit_global (acfg, symbol, FALSE);
emit_label (acfg, symbol);
}
static void
emit_exception_info (MonoAotCompile *acfg)
{
int i;
char symbol [256];
sprintf (symbol, "ex_info");
emit_section_change (acfg, ".text", 1);
emit_global (acfg, symbol, FALSE);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
/* To reduce size of generated assembly */
sprintf (symbol, "ex");
emit_label (acfg, symbol);
for (i = 0; i < acfg->nmethods; ++i) {
if (acfg->cfgs [i])
emit_exception_debug_info (acfg, acfg->cfgs [i]);
}
sprintf (symbol, "ex_info_offsets");
emit_section_change (acfg, ".text", 1);
emit_global (acfg, symbol, FALSE);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
for (i = 0; i < acfg->nmethods; ++i) {
if (acfg->cfgs [i]) {
sprintf (symbol, "%se_%x_p", acfg->temp_prefix, i);
emit_symbol_diff (acfg, symbol, "ex", 0);
} else {
emit_int32 (acfg, 0);
}
}
emit_line (acfg);
}
static void
emit_unwind_info (MonoAotCompile *acfg)
{
int i;
char symbol [128];
/*
* The unwind info contains a lot of duplicates so we emit each unique
* entry once, and only store the offset from the start of the table in the
* exception info.
*/
sprintf (symbol, "unwind_info");
emit_section_change (acfg, ".text", 1);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
emit_global (acfg, symbol, FALSE);
for (i = 0; i < acfg->unwind_ops->len; ++i) {
guint32 index = GPOINTER_TO_UINT (g_ptr_array_index (acfg->unwind_ops, i));
guint8 *unwind_info;
guint32 unwind_info_len;
guint8 buf [16];
guint8 *p;
unwind_info = mono_get_cached_unwind_info (index, &unwind_info_len);
p = buf;
encode_value (unwind_info_len, p, &p);
emit_bytes (acfg, buf, p - buf);
emit_bytes (acfg, unwind_info, unwind_info_len);
acfg->stats.unwind_info_size += (p - buf) + unwind_info_len;
}
}
static void
emit_class_info (MonoAotCompile *acfg)
{
int i;
char symbol [256];
sprintf (symbol, "class_info");
emit_section_change (acfg, ".text", 1);
emit_global (acfg, symbol, FALSE);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i)
emit_klass_info (acfg, MONO_TOKEN_TYPE_DEF | (i + 1));
sprintf (symbol, "class_info_offsets");
emit_section_change (acfg, ".text", 1);
emit_global (acfg, symbol, FALSE);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
sprintf (symbol, "%sK_I_%x", acfg->temp_prefix, i);
emit_symbol_diff (acfg, symbol, "class_info", 0);
}
emit_line (acfg);
}
typedef struct ClassNameTableEntry {
guint32 token, index;
struct ClassNameTableEntry *next;
} ClassNameTableEntry;
static void
emit_class_name_table (MonoAotCompile *acfg)
{
int i, table_size;
guint32 token, hash;
MonoClass *klass;
GPtrArray *table;
char *full_name;
char symbol [256];
ClassNameTableEntry *entry, *new_entry;
/*
* Construct a chained hash table for mapping class names to typedef tokens.
*/
table_size = g_spaced_primes_closest ((int)(acfg->image->tables [MONO_TABLE_TYPEDEF].rows * 1.5));
table = g_ptr_array_sized_new (table_size);
for (i = 0; i < table_size; ++i)
g_ptr_array_add (table, NULL);
for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
token = MONO_TOKEN_TYPE_DEF | (i + 1);
klass = mono_class_get (acfg->image, token);
full_name = mono_type_get_name_full (mono_class_get_type (klass), MONO_TYPE_NAME_FORMAT_FULL_NAME);
hash = mono_aot_str_hash (full_name) % table_size;
g_free (full_name);
/* FIXME: Allocate from the mempool */
new_entry = g_new0 (ClassNameTableEntry, 1);
new_entry->token = token;
entry = g_ptr_array_index (table, hash);
if (entry == NULL) {
new_entry->index = hash;
g_ptr_array_index (table, hash) = new_entry;
} else {
while (entry->next)
entry = entry->next;
entry->next = new_entry;
new_entry->index = table->len;
g_ptr_array_add (table, new_entry);
}
}
/* Emit the table */
sprintf (symbol, "class_name_table");
emit_section_change (acfg, ".text", 0);
emit_global (acfg, symbol, FALSE);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
/* FIXME: Optimize memory usage */
g_assert (table_size < 65000);
emit_int16 (acfg, table_size);
g_assert (table->len < 65000);
for (i = 0; i < table->len; ++i) {
ClassNameTableEntry *entry = g_ptr_array_index (table, i);
if (entry == NULL) {
emit_int16 (acfg, 0);
emit_int16 (acfg, 0);
} else {
emit_int16 (acfg, mono_metadata_token_index (entry->token));
if (entry->next)
emit_int16 (acfg, entry->next->index);
else
emit_int16 (acfg, 0);
}
}
}
static void
emit_image_table (MonoAotCompile *acfg)
{
int i;
char symbol [256];
/*
* The image table is small but referenced in a lot of places.
* So we emit it at once, and reference its elements by an index.
*/
sprintf (symbol, "mono_image_table");
emit_section_change (acfg, ".text", 1);
emit_global (acfg, symbol, FALSE);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
emit_int32 (acfg, acfg->image_table->len);
for (i = 0; i < acfg->image_table->len; i++) {
MonoImage *image = (MonoImage*)g_ptr_array_index (acfg->image_table, i);
MonoAssemblyName *aname = &image->assembly->aname;
/* FIXME: Support multi-module assemblies */
g_assert (image->assembly->image == image);
emit_string (acfg, image->assembly_name);
emit_string (acfg, image->guid);
emit_string (acfg, aname->culture ? aname->culture : "");
emit_string (acfg, (const char*)aname->public_key_token);
emit_alignment (acfg, 8);
emit_int32 (acfg, aname->flags);
emit_int32 (acfg, aname->major);
emit_int32 (acfg, aname->minor);
emit_int32 (acfg, aname->build);
emit_int32 (acfg, aname->revision);
}
}
static void
emit_got_info (MonoAotCompile *acfg)
{
char symbol [256];
int i, first_plt_got_patch, buf_size;
guint8 *p, *buf;
guint32 *got_info_offsets;
/* Add the patches needed by the PLT to the GOT */
acfg->plt_got_offset_base = acfg->got_offset;
first_plt_got_patch = acfg->got_patches->len;
for (i = 1; i < acfg->plt_offset; ++i) {
MonoJumpInfo *patch_info = g_hash_table_lookup (acfg->plt_offset_to_patch, GUINT_TO_POINTER (i));
g_ptr_array_add (acfg->got_patches, patch_info);
}
acfg->got_offset += acfg->plt_offset;
/**
* FIXME:
* - optimize offsets table.
* - reduce number of exported symbols.
* - emit info for a klass only once.
* - determine when a method uses a GOT slot which is guaranteed to be already
* initialized.
* - clean up and document the code.
* - use String.Empty in class libs.
*/
/* Encode info required to decode shared GOT entries */
buf_size = acfg->got_patches->len * 64;
p = buf = mono_mempool_alloc (acfg->mempool, buf_size);
got_info_offsets = mono_mempool_alloc (acfg->mempool, acfg->got_patches->len * sizeof (guint32));
acfg->plt_got_info_offsets = mono_mempool_alloc (acfg->mempool, acfg->plt_offset * sizeof (guint32));
/* Unused */
if (acfg->plt_offset)
acfg->plt_got_info_offsets [0] = 0;
for (i = 0; i < acfg->got_patches->len; ++i) {
MonoJumpInfo *ji = g_ptr_array_index (acfg->got_patches, i);
got_info_offsets [i] = p - buf;
if (i >= first_plt_got_patch)
acfg->plt_got_info_offsets [i - first_plt_got_patch + 1] = got_info_offsets [i];
encode_value (ji->type, p, &p);
encode_patch (acfg, ji, p, &p);
}
g_assert (p - buf <= buf_size);
acfg->stats.got_info_size = p - buf;
/* Emit got_info table */
sprintf (symbol, "got_info");
emit_section_change (acfg, ".text", 1);
emit_global (acfg, symbol, FALSE);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
emit_bytes (acfg, buf, p - buf);
/* Emit got_info_offsets table */
sprintf (symbol, "got_info_offsets");
emit_section_change (acfg, ".text", 1);
emit_global (acfg, symbol, FALSE);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
/* No need to emit offsets for the got plt entries, the plt embeds them directly */
for (i = 0; i < first_plt_got_patch; ++i)
emit_int32 (acfg, got_info_offsets [i]);
acfg->stats.got_info_offsets_size = acfg->got_patches->len * 4;
}
static void
emit_got (MonoAotCompile *acfg)
{
char symbol [256];
/* Don't make GOT global so accesses to it don't need relocations */
sprintf (symbol, "%s", acfg->got_symbol);
emit_section_change (acfg, ".bss", 0);
emit_alignment (acfg, 8);
emit_local_symbol (acfg, symbol, "got_end", FALSE);
emit_label (acfg, symbol);
if (acfg->got_offset > 0)
emit_zero_bytes (acfg, (int)(acfg->got_offset * sizeof (gpointer)));
sprintf (symbol, "got_end");
emit_label (acfg, symbol);
sprintf (symbol, "mono_aot_got_addr");
emit_section_change (acfg, ".data", 0);
emit_global (acfg, symbol, FALSE);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
emit_pointer (acfg, acfg->got_symbol);
}
typedef struct GlobalsTableEntry {
guint32 value, index;
struct GlobalsTableEntry *next;
} GlobalsTableEntry;
static void
emit_globals_table (MonoAotCompile *acfg)
{
int i, table_size;
guint32 hash;
GPtrArray *table;
char symbol [256];
GlobalsTableEntry *entry, *new_entry;
/*
* Construct a chained hash table for mapping global names to their index in
* the globals table.
*/
table_size = g_spaced_primes_closest ((int)(acfg->globals->len * 1.5));
table = g_ptr_array_sized_new (table_size);
for (i = 0; i < table_size; ++i)
g_ptr_array_add (table, NULL);
for (i = 0; i < acfg->globals->len; ++i) {
char *name = g_ptr_array_index (acfg->globals, i);
hash = mono_aot_str_hash (name) % table_size;
/* FIXME: Allocate from the mempool */
new_entry = g_new0 (GlobalsTableEntry, 1);
new_entry->value = i;
entry = g_ptr_array_index (table, hash);
if (entry == NULL) {
new_entry->index = hash;
g_ptr_array_index (table, hash) = new_entry;
} else {
while (entry->next)
entry = entry->next;
entry->next = new_entry;
new_entry->index = table->len;
g_ptr_array_add (table, new_entry);
}
}
/* Emit the table */
sprintf (symbol, ".Lglobals_hash");
emit_section_change (acfg, ".text", 0);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
/* FIXME: Optimize memory usage */
g_assert (table_size < 65000);
emit_int16 (acfg, table_size);
for (i = 0; i < table->len; ++i) {
GlobalsTableEntry *entry = g_ptr_array_index (table, i);
if (entry == NULL) {
emit_int16 (acfg, 0);
emit_int16 (acfg, 0);
} else {
emit_int16 (acfg, entry->value + 1);
if (entry->next)
emit_int16 (acfg, entry->next->index);
else
emit_int16 (acfg, 0);
}
}
/* Emit the names */
for (i = 0; i < acfg->globals->len; ++i) {
char *name = g_ptr_array_index (acfg->globals, i);
sprintf (symbol, "name_%d", i);
emit_section_change (acfg, ".text", 1);
emit_label (acfg, symbol);
emit_string (acfg, name);
}
/* Emit the globals table */
sprintf (symbol, ".Lglobals");
emit_section_change (acfg, ".data", 0);
/* This is not a global, since it is accessed by the init function */
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
sprintf (symbol, "%sglobals_hash", acfg->temp_prefix);
emit_pointer (acfg, symbol);
for (i = 0; i < acfg->globals->len; ++i) {
char *name = g_ptr_array_index (acfg->globals, i);
sprintf (symbol, "name_%d", i);
emit_pointer (acfg, symbol);
sprintf (symbol, "%s", name);
emit_pointer (acfg, symbol);
}
/* Null terminate the table */
emit_int32 (acfg, 0);
emit_int32 (acfg, 0);
}
static void
emit_globals (MonoAotCompile *acfg)
{
char *opts_str;
char *build_info;
emit_string_symbol (acfg, "mono_assembly_guid" , acfg->image->guid);
emit_string_symbol (acfg, "mono_aot_version", MONO_AOT_FILE_VERSION);
opts_str = g_strdup_printf ("%d", acfg->opts);
emit_string_symbol (acfg, "mono_aot_opt_flags", opts_str);
g_free (opts_str);
emit_string_symbol (acfg, "mono_aot_full_aot", acfg->aot_opts.full_aot ? "TRUE" : "FALSE");
if (acfg->aot_opts.bind_to_runtime_version) {
build_info = mono_get_runtime_build_info ();
emit_string_symbol (acfg, "mono_runtime_version", build_info);
g_free (build_info);
} else {
emit_string_symbol (acfg, "mono_runtime_version", "");
}
/*
* When static linking, we emit a global which will point to the symbol table.
*/
if (acfg->aot_opts.static_link) {
char symbol [256];
char *p;
/* Emit a string holding the assembly name */
emit_string_symbol (acfg, "mono_aot_assembly_name", acfg->image->assembly->aname.name);
emit_globals_table (acfg);
/*
* Emit a global symbol which can be passed by an embedding app to
* mono_aot_register_module ().
*/
#if defined(__MACH__)
sprintf (symbol, "_mono_aot_module_%s_info", acfg->image->assembly->aname.name);
#else
sprintf (symbol, "mono_aot_module_%s_info", acfg->image->assembly->aname.name);
#endif
/* Get rid of characters which cannot occur in symbols */
p = symbol;
for (p = symbol; *p; ++p) {
if (!(isalnum (*p) || *p == '_'))
*p = '_';
}
acfg->static_linking_symbol = g_strdup (symbol);
emit_global_inner (acfg, symbol, FALSE);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
sprintf (symbol, "%sglobals", acfg->temp_prefix);
emit_pointer (acfg, symbol);
}
}
static void
emit_mem_end (MonoAotCompile *acfg)
{
char symbol [128];
sprintf (symbol, "mem_end");
emit_section_change (acfg, ".text", 1);
emit_global (acfg, symbol, FALSE);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
}
/*
* Emit a structure containing all the information not stored elsewhere.
*/
static void
emit_file_info (MonoAotCompile *acfg)
{
char symbol [128];
int i;
sprintf (symbol, "mono_aot_file_info");
emit_section_change (acfg, ".data", 0);
emit_alignment (acfg, 8);
emit_label (acfg, symbol);
emit_global (acfg, symbol, FALSE);
/* The data emitted here must match MonoAotFileInfo in aot-runtime.c. */
emit_int32 (acfg, acfg->plt_got_offset_base);
emit_int32 (acfg, (int)(acfg->got_offset * sizeof (gpointer)));
emit_int32 (acfg, acfg->plt_offset);
emit_int32 (acfg, acfg->nmethods);
for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
emit_int32 (acfg, acfg->num_trampolines [i]);
for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
emit_int32 (acfg, acfg->trampoline_got_offset_base [i]);
for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
emit_int32 (acfg, acfg->trampoline_size [i]);
}
static void
emit_dwarf_info (MonoAotCompile *acfg)
{
#ifdef EMIT_DWARF_INFO
int i;
char symbol [128], symbol2 [128];
/* DIEs for methods */
for (i = 0; i < acfg->nmethods; ++i) {
MonoCompile *cfg = acfg->cfgs [i];
if (!cfg)
continue;
sprintf (symbol, "%sm_%x", acfg->temp_prefix, i);
sprintf (symbol2, "%sme_%x", acfg->temp_prefix, i);
mono_dwarf_writer_emit_method (acfg->dwarf, cfg, cfg->method, symbol, symbol2, cfg->jit_info->code_start, cfg->jit_info->code_size, cfg->args, cfg->locals, cfg->unwind_ops, mono_debug_find_method (cfg->jit_info->method, mono_domain_get ()));
}
#endif
}
static void
collect_methods (MonoAotCompile *acfg)
{
int i;
MonoImage *image = acfg->image;
/* Collect methods */
for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
MonoMethod *method;
guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
method = mono_get_method (acfg->image, token, NULL);
if (!method) {
printf ("Failed to load method 0x%x from '%s'.\n", token, image->name);
exit (1);
}
/* Load all methods eagerly to skip the slower lazy loading code */
mono_class_setup_methods (method->klass);
if (acfg->aot_opts.full_aot && method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
/* Compile the wrapper instead */
/* We do this here instead of add_wrappers () because it is easy to do it here */
MonoMethod *wrapper = mono_marshal_get_native_wrapper (method, check_for_pending_exc, TRUE);
method = wrapper;
}
/* FIXME: Some mscorlib methods don't have debug info */
/*
if (acfg->aot_opts.soft_debug && !method->wrapper_type) {
if (!((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
(method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
(method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))) {
if (!mono_debug_lookup_method (method)) {
fprintf (stderr, "Method %s has no debug info, probably the .mdb file for the assembly is missing.\n", mono_method_full_name (method, TRUE));
exit (1);
}
}
}
*/
/* Since we add the normal methods first, their index will be equal to their zero based token index */
add_method_with_index (acfg, method, i, FALSE);
acfg->method_index ++;
}
add_generic_instances (acfg);
if (acfg->aot_opts.full_aot)
add_wrappers (acfg);
}
static void
compile_methods (MonoAotCompile *acfg)
{
int i, methods_len;
if (acfg->aot_opts.nthreads > 0) {
GPtrArray *frag;
int len, j;
GPtrArray *threads;
HANDLE handle;
gpointer *user_data;
MonoMethod **methods;
methods_len = acfg->methods->len;
len = acfg->methods->len / acfg->aot_opts.nthreads;
g_assert (len > 0);
/*
* Partition the list of methods into fragments, and hand it to threads to
* process.
*/
threads = g_ptr_array_new ();
/* Make a copy since acfg->methods is modified by compile_method () */
methods = g_new0 (MonoMethod*, methods_len);
//memcpy (methods, g_ptr_array_index (acfg->methods, 0), sizeof (MonoMethod*) * methods_len);
for (i = 0; i < methods_len; ++i)
methods [i] = g_ptr_array_index (acfg->methods, i);
i = 0;
while (i < methods_len) {
frag = g_ptr_array_new ();
for (j = 0; j < len; ++j) {
if (i < methods_len) {
g_ptr_array_add (frag, methods [i]);
i ++;
}
}
user_data = g_new0 (gpointer, 3);
user_data [0] = mono_domain_get ();
user_data [1] = acfg;
user_data [2] = frag;
handle = mono_create_thread (NULL, 0, (gpointer)compile_thread_main, user_data, 0, NULL);
g_ptr_array_add (threads, handle);
}
g_free (methods);
for (i = 0; i < threads->len; ++i) {
WaitForSingleObjectEx (g_ptr_array_index (threads, i), INFINITE, FALSE);
}
} else {
methods_len = 0;
}
/* Compile methods added by compile_method () or all methods if nthreads == 0 */
for (i = methods_len; i < acfg->methods->len; ++i) {
/* This can new methods to acfg->methods */
compile_method (acfg, g_ptr_array_index (acfg->methods, i));
}
}
static int
compile_asm (MonoAotCompile *acfg)
{
char *command, *objfile;
char *outfile_name, *tmp_outfile_name;
const char *tool_prefix = acfg->aot_opts.tool_prefix ? acfg->aot_opts.tool_prefix : "";
#if defined(TARGET_AMD64)
#define AS_OPTIONS "--64"
#elif defined(TARGET_POWERPC64)
#define AS_OPTIONS "-a64 -mppc64"
#define LD_OPTIONS "-m elf64ppc"
#elif defined(sparc) && SIZEOF_VOID_P == 8
#define AS_OPTIONS "-xarch=v9"
#else
#define AS_OPTIONS ""
#endif
#ifndef LD_OPTIONS
#define LD_OPTIONS ""
#endif
if (acfg->aot_opts.asm_only) {
printf ("Output file: '%s'.\n", acfg->tmpfname);
if (acfg->aot_opts.static_link)
printf ("Linking symbol: '%s'.\n", acfg->static_linking_symbol);
return 0;
}
if (acfg->aot_opts.static_link) {
if (acfg->aot_opts.outfile)
objfile = g_strdup_printf ("%s", acfg->aot_opts.outfile);
else
objfile = g_strdup_printf ("%s.o", acfg->image->name);
} else {
objfile = g_strdup_printf ("%s.o", acfg->tmpfname);
}
command = g_strdup_printf ("%sas %s %s -o %s", tool_prefix, AS_OPTIONS, acfg->tmpfname, objfile);
printf ("Executing the native assembler: %s\n", command);
if (system (command) != 0) {
g_free (command);
g_free (objfile);
return 1;
}
g_free (command);
if (acfg->aot_opts.static_link) {
printf ("Output file: '%s'.\n", objfile);
printf ("Linking symbol: '%s'.\n", acfg->static_linking_symbol);
g_free (objfile);
return 0;
}
if (acfg->aot_opts.outfile)
outfile_name = g_strdup_printf ("%s", acfg->aot_opts.outfile);
else
outfile_name = g_strdup_printf ("%s%s", acfg->image->name, SHARED_EXT);
tmp_outfile_name = g_strdup_printf ("%s.tmp", outfile_name);
#if defined(sparc)
command = g_strdup_printf ("ld -shared -G -o %s %s.o", tmp_outfile_name, acfg->tmpfname);
#elif defined(__ppc__) && defined(__MACH__)
command = g_strdup_printf ("gcc -dynamiclib -o %s %s.o", tmp_outfile_name, acfg->tmpfname);
#elif defined(PLATFORM_WIN32)
command = g_strdup_printf ("gcc -shared --dll -mno-cygwin -o %s %s.o", tmp_outfile_name, acfg->tmpfname);
#else
command = g_strdup_printf ("%sld %s -shared -o %s %s.o", tool_prefix, LD_OPTIONS, tmp_outfile_name, acfg->tmpfname);
#endif
printf ("Executing the native linker: %s\n", command);
if (system (command) != 0) {
g_free (tmp_outfile_name);
g_free (outfile_name);
g_free (command);
g_free (objfile);
return 1;
}
g_free (command);
unlink (objfile);
/*com = g_strdup_printf ("strip --strip-unneeded %s%s", acfg->image->name, SHARED_EXT);
printf ("Stripping the binary: %s\n", com);
system (com);
g_free (com);*/
#if defined(TARGET_ARM) && !defined(__MACH__)
/*
* gas generates 'mapping symbols' each time code and data is mixed, which
* happens a lot in emit_and_reloc_code (), so we need to get rid of them.
*/
command = g_strdup_printf ("%sstrip --strip-symbol=\\$a --strip-symbol=\\$d %s", tool_prefix, tmp_outfile_name);
printf ("Stripping the binary: %s\n", command);
if (system (command) != 0) {
g_free (tmp_outfile_name);
g_free (outfile_name);
g_free (command);
g_free (objfile);
return 1;
}
#endif
rename (tmp_outfile_name, outfile_name);
g_free (tmp_outfile_name);
g_free (outfile_name);
g_free (objfile);
if (acfg->aot_opts.save_temps)
printf ("Retained input file.\n");
else
unlink (acfg->tmpfname);
return 0;
}
static MonoAotCompile*
acfg_create (MonoAssembly *ass, guint32 opts)
{
MonoImage *image = ass->image;
MonoAotCompile *acfg;
int i;
acfg = g_new0 (MonoAotCompile, 1);
acfg->methods = g_ptr_array_new ();
acfg->method_indexes = g_hash_table_new (NULL, NULL);
acfg->method_depth = g_hash_table_new (NULL, NULL);
acfg->plt_offset_to_patch = g_hash_table_new (NULL, NULL);
acfg->patch_to_plt_offset = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
acfg->patch_to_got_offset = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
acfg->patch_to_got_offset_by_type = g_new0 (GHashTable*, MONO_PATCH_INFO_NUM);
for (i = 0; i < MONO_PATCH_INFO_NUM; ++i)
acfg->patch_to_got_offset_by_type [i] = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
acfg->got_patches = g_ptr_array_new ();
acfg->method_to_cfg = g_hash_table_new (NULL, NULL);
acfg->token_info_hash = g_hash_table_new_full (NULL, NULL, NULL, g_free);
acfg->image_hash = g_hash_table_new (NULL, NULL);
acfg->image_table = g_ptr_array_new ();
acfg->globals = g_ptr_array_new ();
acfg->image = image;
acfg->opts = opts;
acfg->mempool = mono_mempool_new ();
acfg->extra_methods = g_ptr_array_new ();
acfg->unwind_info_offsets = g_hash_table_new (NULL, NULL);
acfg->unwind_ops = g_ptr_array_new ();
acfg->method_label_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
InitializeCriticalSection (&acfg->mutex);
return acfg;
}
static void
acfg_free (MonoAotCompile *acfg)
{
int i;
img_writer_destroy (acfg->w);
for (i = 0; i < acfg->nmethods; ++i)
if (acfg->cfgs [i])
g_free (acfg->cfgs [i]);
g_free (acfg->cfgs);
g_free (acfg->static_linking_symbol);
g_free (acfg->got_symbol);
g_free (acfg->plt_symbol);
g_ptr_array_free (acfg->methods, TRUE);
g_ptr_array_free (acfg->got_patches, TRUE);
g_ptr_array_free (acfg->image_table, TRUE);
g_ptr_array_free (acfg->globals, TRUE);
g_ptr_array_free (acfg->unwind_ops, TRUE);
g_hash_table_destroy (acfg->method_indexes);
g_hash_table_destroy (acfg->method_depth);
g_hash_table_destroy (acfg->plt_offset_to_patch);
g_hash_table_destroy (acfg->patch_to_plt_offset);
g_hash_table_destroy (acfg->patch_to_got_offset);
g_hash_table_destroy (acfg->method_to_cfg);
g_hash_table_destroy (acfg->token_info_hash);
g_hash_table_destroy (acfg->image_hash);
g_hash_table_destroy (acfg->unwind_info_offsets);
g_hash_table_destroy (acfg->method_label_hash);
for (i = 0; i < MONO_PATCH_INFO_NUM; ++i)
g_hash_table_destroy (acfg->patch_to_got_offset_by_type [i]);
g_free (acfg->patch_to_got_offset_by_type);
mono_mempool_destroy (acfg->mempool);
g_free (acfg);
}
int
mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
{
MonoImage *image = ass->image;
int res;
MonoAotCompile *acfg;
char *outfile_name, *tmp_outfile_name, *p;
TV_DECLARE (atv);
TV_DECLARE (btv);
printf ("Mono Ahead of Time compiler - compiling assembly %s\n", image->name);
acfg = acfg_create (ass, opts);
memset (&acfg->aot_opts, 0, sizeof (acfg->aot_opts));
acfg->aot_opts.write_symbols = TRUE;
acfg->aot_opts.ntrampolines = 1024;
acfg->aot_opts.nrgctx_trampolines = 1024;
mono_aot_parse_options (aot_options, &acfg->aot_opts);
//acfg->aot_opts.print_skipped_methods = TRUE;
#ifndef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
if (acfg->aot_opts.full_aot) {
printf ("--aot=full is not supported on this platform.\n");
return 1;
}
#endif
if (acfg->aot_opts.static_link)
acfg->aot_opts.asm_writer = TRUE;
if (acfg->aot_opts.soft_debug) {
MonoDebugOptions *opt = mini_get_debug_options ();
opt->mdb_optimizations = TRUE;
opt->gen_seq_points = TRUE;
if (mono_debug_format == MONO_DEBUG_FORMAT_NONE) {
fprintf (stderr, "The soft-debug AOT option requires the --debug option.\n");
return 1;
}
}
load_profile_files (acfg);
acfg->num_trampolines [MONO_AOT_TRAMP_SPECIFIC] = acfg->aot_opts.full_aot ? acfg->aot_opts.ntrampolines : 0;
#ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
acfg->num_trampolines [MONO_AOT_TRAMP_STATIC_RGCTX] = acfg->aot_opts.full_aot ? acfg->aot_opts.nrgctx_trampolines : 0;
#endif
acfg->num_trampolines [MONO_AOT_TRAMP_IMT_THUNK] = acfg->aot_opts.full_aot ? 128 : 0;
acfg->got_symbol = g_strdup_printf ("mono_aot_%s_got", acfg->image->assembly->aname.name);
acfg->plt_symbol = g_strdup_printf ("mono_aot_%s_plt", acfg->image->assembly->aname.name);
/* Get rid of characters which cannot occur in symbols */
for (p = acfg->got_symbol; *p; ++p) {
if (!(isalnum (*p) || *p == '_'))
*p = '_';
}
for (p = acfg->plt_symbol; *p; ++p) {
if (!(isalnum (*p) || *p == '_'))
*p = '_';
}
acfg->method_index = 1;
collect_methods (acfg);
acfg->cfgs_size = acfg->methods->len + 32;
acfg->cfgs = g_new0 (MonoCompile*, acfg->cfgs_size);
/* PLT offset 0 is reserved for the PLT trampoline */
acfg->plt_offset = 1;
/* GOT offset 0 is reserved for the address of the current assembly */
{
MonoJumpInfo *ji;
ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
ji->type = MONO_PATCH_INFO_IMAGE;
ji->data.image = acfg->image;
get_got_offset (acfg, ji);
/* Slot 1 is reserved for the mscorlib got addr */
ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
ji->type = MONO_PATCH_INFO_MSCORLIB_GOT_ADDR;
get_got_offset (acfg, ji);
}
TV_GETTIME (atv);
compile_methods (acfg);
TV_GETTIME (btv);
acfg->stats.jit_time = TV_ELAPSED (atv, btv);
TV_GETTIME (atv);
if (!acfg->aot_opts.asm_only && !acfg->aot_opts.asm_writer && bin_writer_supported ()) {
if (acfg->aot_opts.outfile)
outfile_name = g_strdup_printf ("%s", acfg->aot_opts.outfile);
else
outfile_name = g_strdup_printf ("%s%s", acfg->image->name, SHARED_EXT);
/*
* Can't use g_file_open_tmp () as it will be deleted at exit, and
* it might be in another file system so the rename () won't work.
*/
tmp_outfile_name = g_strdup_printf ("%s.tmp", outfile_name);
acfg->fp = fopen (tmp_outfile_name, "w");
if (!acfg->fp) {
printf ("Unable to create temporary file '%s': %s\n", tmp_outfile_name, strerror (errno));
return 1;
}
acfg->w = img_writer_create (acfg->fp, TRUE);
acfg->use_bin_writer = TRUE;
} else {
if (acfg->aot_opts.asm_only) {
if (acfg->aot_opts.outfile)
acfg->tmpfname = g_strdup_printf ("%s", acfg->aot_opts.outfile);
else
acfg->tmpfname = g_strdup_printf ("%s.s", acfg->image->name);
acfg->fp = fopen (acfg->tmpfname, "w+");
} else {
int i = g_file_open_tmp ("mono_aot_XXXXXX", &acfg->tmpfname, NULL);
acfg->fp = fdopen (i, "w+");
}
g_assert (acfg->fp);
acfg->w = img_writer_create (acfg->fp, FALSE);
tmp_outfile_name = NULL;
outfile_name = NULL;
}
acfg->temp_prefix = img_writer_get_temp_label_prefix (acfg->w);
if (!acfg->aot_opts.nodebug)
acfg->dwarf = mono_dwarf_writer_create (acfg->w, NULL, 0, FALSE);
img_writer_emit_start (acfg->w);
if (acfg->dwarf)
mono_dwarf_writer_emit_base_info (acfg->dwarf, arch_get_cie_program ());
emit_code (acfg);
emit_info (acfg);
emit_extra_methods (acfg);
emit_method_order (acfg);
emit_trampolines (acfg);
emit_class_name_table (acfg);
emit_got_info (acfg);
emit_exception_info (acfg);
emit_unwind_info (acfg);
emit_class_info (acfg);
emit_plt (acfg);
emit_image_table (acfg);
emit_got (acfg);
emit_file_info (acfg);
emit_globals (acfg);
if (acfg->dwarf) {
emit_dwarf_info (acfg);
mono_dwarf_writer_close (acfg->dwarf);
}
emit_mem_end (acfg);
TV_GETTIME (btv);
acfg->stats.gen_time = TV_ELAPSED (atv, btv);
printf ("Code: %d Info: %d Ex Info: %d Unwind Info: %d Class Info: %d PLT: %d GOT Info: %d GOT Info Offsets: %d GOT: %d Offsets: %d\n", acfg->stats.code_size, acfg->stats.info_size, acfg->stats.ex_info_size, acfg->stats.unwind_info_size, acfg->stats.class_info_size, acfg->plt_offset, acfg->stats.got_info_size, acfg->stats.got_info_offsets_size, (int)(acfg->got_offset * sizeof (gpointer)), (int)(acfg->nmethods * 3 * sizeof (gpointer)));
TV_GETTIME (atv);
res = img_writer_emit_writeout (acfg->w);
if (res != 0) {
acfg_free (acfg);
return res;
}
if (acfg->use_bin_writer) {
int err = rename (tmp_outfile_name, outfile_name);
if (err) {
printf ("Unable to rename '%s' to '%s': %s\n", tmp_outfile_name, outfile_name, strerror (errno));
return 1;
}
} else {
res = compile_asm (acfg);
if (res != 0) {
acfg_free (acfg);
return res;
}
}
TV_GETTIME (btv);
acfg->stats.link_time = TV_ELAPSED (atv, btv);
printf ("Compiled %d out of %d methods (%d%%)\n", acfg->stats.ccount, acfg->stats.mcount, acfg->stats.mcount ? (acfg->stats.ccount * 100) / acfg->stats.mcount : 100);
if (acfg->stats.genericcount)
printf ("%d methods are generic (%d%%)\n", acfg->stats.genericcount, acfg->stats.mcount ? (acfg->stats.genericcount * 100) / acfg->stats.mcount : 100);
if (acfg->stats.abscount)
printf ("%d methods contain absolute addresses (%d%%)\n", acfg->stats.abscount, acfg->stats.mcount ? (acfg->stats.abscount * 100) / acfg->stats.mcount : 100);
if (acfg->stats.lmfcount)
printf ("%d methods contain lmf pointers (%d%%)\n", acfg->stats.lmfcount, acfg->stats.mcount ? (acfg->stats.lmfcount * 100) / acfg->stats.mcount : 100);
if (acfg->stats.ocount)
printf ("%d methods have other problems (%d%%)\n", acfg->stats.ocount, acfg->stats.mcount ? (acfg->stats.ocount * 100) / acfg->stats.mcount : 100);
printf ("Methods without GOT slots: %d (%d%%)\n", acfg->stats.methods_without_got_slots, acfg->stats.mcount ? (acfg->stats.methods_without_got_slots * 100) / acfg->stats.mcount : 100);
printf ("Direct calls: %d (%d%%)\n", acfg->stats.direct_calls, acfg->stats.all_calls ? (acfg->stats.direct_calls * 100) / acfg->stats.all_calls : 100);
/*
printf ("GOT slot distribution:\n");
for (i = 0; i < MONO_PATCH_INFO_NONE; ++i)
if (acfg->stats.got_slot_types [i])
printf ("\t%s: %d\n", get_patch_name (i), acfg->stats.got_slot_types [i]);
*/
printf ("JIT time: %d ms, Generation time: %d ms, Assembly+Link time: %d ms.\n", acfg->stats.jit_time / 1000, acfg->stats.gen_time / 1000, acfg->stats.link_time / 1000);
acfg_free (acfg);
return 0;
}
/*
* Support for emitting debug info for JITted code.
*
* This works as follows:
* - the runtime writes out an xdb.s file containing DWARF debug info.
* - the user calls a gdb macro
* - the macro compiles and loads this shared library using add-symbol-file.
*
* This is based on the xdebug functionality in the Kaffe Java VM.
*
* We emit assembly code instead of using the ELF writer, so we can emit debug info
* incrementally as each method is JITted, and the debugger doesn't have to call
* into the runtime to emit the shared library, which would cause all kinds of
* complications, like threading issues, and the fact that the ELF writer's
* emit_writeout () function cannot be called more than once.
* GDB 7.0 and later has a JIT interface.
*/
#define USE_GDB_JIT_INTERFACE
/* The recommended gdb macro is: */
/*
define xdb
shell rm -f xdb.so && as --64 -o xdb.o xdb.s && ld -shared -o xdb.so xdb.o
add-symbol-file xdb.so 0
end
*/
/*
* GDB JIT interface definitions.
*
* http://sources.redhat.com/gdb/onlinedocs/gdb_30.html
*/
typedef enum
{
JIT_NOACTION = 0,
JIT_REGISTER_FN,
JIT_UNREGISTER_FN
} jit_actions_t;
struct jit_code_entry
{
struct jit_code_entry *next_entry;
struct jit_code_entry *prev_entry;
const char *symfile_addr;
guint64 symfile_size;
};
struct jit_descriptor
{
guint32 version;
/* This type should be jit_actions_t, but we use guint32
to be explicit about the bitwidth. */
guint32 action_flag;
struct jit_code_entry *relevant_entry;
struct jit_code_entry *first_entry;
};
#ifdef _MSC_VER
#define MONO_NOINLINE __declspec (noinline)
#else
#define MONO_NOINLINE __attribute__((noinline))
#endif
/* GDB puts a breakpoint in this function. */
void MONO_NOINLINE __jit_debug_register_code(void);
void MONO_NOINLINE __jit_debug_register_code(void) { };
/* Make sure to specify the version statically, because the
debugger may check the version before we can set it. */
struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
static MonoImageWriter *xdebug_w;
static MonoDwarfWriter *xdebug_writer;
static FILE *xdebug_fp, *il_file;
static gboolean use_gdb_interface, save_symfiles;
static int il_file_line_index;
static GHashTable *xdebug_syms;
void
mono_xdebug_init (char *options)
{
MonoImageWriter *w;
char **args, **ptr;
args = g_strsplit (options, ",", -1);
for (ptr = args; ptr && *ptr; ptr ++) {
char *arg = *ptr;
if (!strcmp (arg, "gdb"))
use_gdb_interface = TRUE;
if (!strcmp (arg, "save-symfiles"))
save_symfiles = TRUE;
}
/* This file will contain the IL code for methods which don't have debug info */
il_file = fopen ("xdb.il", "w");
if (use_gdb_interface)
return;
unlink ("xdb.s");
xdebug_fp = fopen ("xdb.s", "w");
w = img_writer_create (xdebug_fp, FALSE);
img_writer_emit_start (w);
xdebug_writer = mono_dwarf_writer_create (w, il_file, 0, TRUE);
/* Emit something so the file has a text segment */
img_writer_emit_section_change (w, ".text", 0);
img_writer_emit_string (w, "");
mono_dwarf_writer_emit_base_info (xdebug_writer, arch_get_cie_program ());
}
static void
xdebug_begin_emit (MonoImageWriter **out_w, MonoDwarfWriter **out_dw)
{
MonoImageWriter *w;
MonoDwarfWriter *dw;
w = img_writer_create (NULL, TRUE);
img_writer_emit_start (w);
/* This file will contain the IL code for methods which don't have debug info */
if (!il_file)
il_file = fopen ("xdb.il", "w");
dw = mono_dwarf_writer_create (w, il_file, il_file_line_index, FALSE);
mono_dwarf_writer_emit_base_info (dw, arch_get_cie_program ());
*out_w = w;
*out_dw = dw;
}
static void
xdebug_end_emit (MonoImageWriter *w, MonoDwarfWriter *dw, MonoMethod *method)
{
guint8 *img;
guint32 img_size;
struct jit_code_entry *entry;
il_file_line_index = mono_dwarf_writer_get_il_file_line_index (dw);
mono_dwarf_writer_close (dw);
img_writer_emit_writeout (w);
img = img_writer_get_output (w, &img_size);
img_writer_destroy (w);
if (FALSE) {
/* Save the symbol files to help debugging */
FILE *fp;
char *file_name;
static int file_counter;
file_counter ++;
file_name = g_strdup_printf ("xdb-%d.o", file_counter);
//printf ("%s -> %s\n", mono_method_full_name (method, TRUE), file_name);
fp = fopen (file_name, "w");
fwrite (img, img_size, 1, fp);
fclose (fp);
g_free (file_name);
}
/* Register the image with GDB */
entry = g_malloc (sizeof (struct jit_code_entry));
entry->symfile_addr = (const char*)img;
entry->symfile_size = img_size;
entry->next_entry = __jit_debug_descriptor.first_entry;
if (__jit_debug_descriptor.first_entry)
__jit_debug_descriptor.first_entry->prev_entry = entry;
__jit_debug_descriptor.first_entry = entry;
__jit_debug_descriptor.relevant_entry = entry;
__jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
__jit_debug_register_code ();
}
/*
* mono_xdebug_flush:
*
* This could be called from inside gdb to flush the debugging information not yet
* registered with gdb.
*/
static void
mono_xdebug_flush (void)
{
if (xdebug_w)
xdebug_end_emit (xdebug_w, xdebug_writer, NULL);
xdebug_begin_emit (&xdebug_w, &xdebug_writer);
}
static int xdebug_method_count;
/*
* mono_save_xdebug_info:
*
* Emit debugging info for METHOD into an assembly file which can be assembled
* and loaded into gdb to provide debugging info for JITted code.
* LOCKING: Acquires the loader lock.
*/
void
mono_save_xdebug_info (MonoCompile *cfg)
{
if (use_gdb_interface) {
mono_loader_lock ();
if (!xdebug_syms)
xdebug_syms = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
/*
* gdb is not designed to handle 1000s of symbol files (one per method). So we
* group them into groups of 100.
*/
if ((xdebug_method_count % 100) == 0)
mono_xdebug_flush ();
xdebug_method_count ++;
mono_dwarf_writer_emit_method (xdebug_writer, cfg, cfg->jit_info->method, NULL, NULL, cfg->jit_info->code_start, cfg->jit_info->code_size, cfg->args, cfg->locals, cfg->unwind_ops, mono_debug_find_method (cfg->jit_info->method, mono_domain_get ()));
#if 0
/*
* Emit a symbol for the code by emitting it at the beginning of the text
* segment, and setting the text segment to have an absolute address.
* This symbol can be used to set breakpoints in gdb.
* FIXME: This doesn't work when multiple methods are emitted into the same file.
*/
sym = get_debug_sym (cfg->jit_info->method, "", xdebug_syms);
img_writer_emit_section_change (w, ".text", 0);
if (!xdebug_text_addr) {
xdebug_text_addr = cfg->jit_info->code_start;
img_writer_set_section_addr (w, (gssize)xdebug_text_addr);
}
img_writer_emit_global_with_size (w, sym, cfg->jit_info->code_size, TRUE);
img_writer_emit_label (w, sym);
img_writer_emit_bytes (w, cfg->jit_info->code_start, cfg->jit_info->code_size);
g_free (sym);
#endif
mono_loader_unlock ();
} else {
if (!xdebug_writer)
return;
mono_loader_lock ();
mono_dwarf_writer_emit_method (xdebug_writer, cfg, cfg->jit_info->method, NULL, NULL, cfg->jit_info->code_start, cfg->jit_info->code_size, cfg->args, cfg->locals, cfg->unwind_ops, mono_debug_find_method (cfg->jit_info->method, mono_domain_get ()));
fflush (xdebug_fp);
mono_loader_unlock ();
}
}
/*
* mono_save_trampoline_xdebug_info:
*
* Same as mono_save_xdebug_info, but for trampolines.
* LOCKING: Acquires the loader lock.
*/
void
mono_save_trampoline_xdebug_info (const char *tramp_name, guint8 *code, guint32 code_size, GSList *unwind_info)
{
if (use_gdb_interface) {
MonoImageWriter *w;
MonoDwarfWriter *dw;
mono_loader_lock ();
xdebug_begin_emit (&w, &dw);
mono_dwarf_writer_emit_trampoline (dw, tramp_name, NULL, NULL, code, code_size, unwind_info);
xdebug_end_emit (w, dw, NULL);
mono_loader_unlock ();
} else {
if (!xdebug_writer)
return;
mono_loader_lock ();
mono_dwarf_writer_emit_trampoline (xdebug_writer, tramp_name, NULL, NULL, code, code_size, unwind_info);
fflush (xdebug_fp);
mono_loader_unlock ();
}
}
#else
/* AOT disabled */
int
mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
{
return 0;
}
void
mono_xdebug_init (char *options)
{
}
void
mono_save_xdebug_info (MonoCompile *cfg)
{
}
void
mono_save_trampoline_xdebug_info (const char *tramp_name, guint8 *code, guint32 code_size, GSList *unwind_info)
{
}
#endif
|