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
|
// gogo-tree.cc -- convert Go frontend Gogo IR to gcc trees.
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "go-system.h"
#include <gmp.h>
#ifndef ENABLE_BUILD_WITH_CXX
extern "C"
{
#endif
#include "toplev.h"
#include "tree.h"
#include "gimple.h"
#include "tree-iterator.h"
#include "cgraph.h"
#include "langhooks.h"
#include "convert.h"
#include "output.h"
#include "diagnostic.h"
#ifndef ENABLE_BUILD_WITH_CXX
}
#endif
#include "go-c.h"
#include "types.h"
#include "expressions.h"
#include "statements.h"
#include "gogo.h"
// Whether we have seen any errors.
bool
saw_errors()
{
return errorcount != 0 || sorrycount != 0;
}
// A helper function.
static inline tree
get_identifier_from_string(const std::string& str)
{
return get_identifier_with_length(str.data(), str.length());
}
// Builtin functions.
static std::map<std::string, tree> builtin_functions;
// Define a builtin function. BCODE is the builtin function code
// defined by builtins.def. NAME is the name of the builtin function.
// LIBNAME is the name of the corresponding library function, and is
// NULL if there isn't one. FNTYPE is the type of the function.
// CONST_P is true if the function has the const attribute.
static void
define_builtin(built_in_function bcode, const char* name, const char* libname,
tree fntype, bool const_p)
{
tree decl = add_builtin_function(name, fntype, bcode, BUILT_IN_NORMAL,
libname, NULL_TREE);
if (const_p)
TREE_READONLY(decl) = 1;
built_in_decls[bcode] = decl;
implicit_built_in_decls[bcode] = decl;
builtin_functions[name] = decl;
if (libname != NULL)
{
decl = add_builtin_function(libname, fntype, bcode, BUILT_IN_NORMAL,
NULL, NULL_TREE);
if (const_p)
TREE_READONLY(decl) = 1;
builtin_functions[libname] = decl;
}
}
// Create trees for implicit builtin functions.
void
Gogo::define_builtin_function_trees()
{
/* We need to define the fetch_and_add functions, since we use them
for ++ and --. */
tree t = go_type_for_size(BITS_PER_UNIT, 1);
tree p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
define_builtin(BUILT_IN_ADD_AND_FETCH_1, "__sync_fetch_and_add_1", NULL,
build_function_type_list(t, p, t, NULL_TREE), false);
t = go_type_for_size(BITS_PER_UNIT * 2, 1);
p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
define_builtin (BUILT_IN_ADD_AND_FETCH_2, "__sync_fetch_and_add_2", NULL,
build_function_type_list(t, p, t, NULL_TREE), false);
t = go_type_for_size(BITS_PER_UNIT * 4, 1);
p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
define_builtin(BUILT_IN_ADD_AND_FETCH_4, "__sync_fetch_and_add_4", NULL,
build_function_type_list(t, p, t, NULL_TREE), false);
t = go_type_for_size(BITS_PER_UNIT * 8, 1);
p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
define_builtin(BUILT_IN_ADD_AND_FETCH_8, "__sync_fetch_and_add_8", NULL,
build_function_type_list(t, p, t, NULL_TREE), false);
// We use __builtin_expect for magic import functions.
define_builtin(BUILT_IN_EXPECT, "__builtin_expect", NULL,
build_function_type_list(long_integer_type_node,
long_integer_type_node,
long_integer_type_node,
NULL_TREE),
true);
// We use __builtin_memmove for the predeclared copy function.
define_builtin(BUILT_IN_MEMMOVE, "__builtin_memmove", "memmove",
build_function_type_list(ptr_type_node,
ptr_type_node,
const_ptr_type_node,
size_type_node,
NULL_TREE),
false);
// We provide sqrt for the math library.
define_builtin(BUILT_IN_SQRT, "__builtin_sqrt", "sqrt",
build_function_type_list(double_type_node,
double_type_node,
NULL_TREE),
true);
define_builtin(BUILT_IN_SQRTL, "__builtin_sqrtl", "sqrtl",
build_function_type_list(long_double_type_node,
long_double_type_node,
NULL_TREE),
true);
// We use __builtin_return_address in the thunk we build for
// functions which call recover.
define_builtin(BUILT_IN_RETURN_ADDRESS, "__builtin_return_address", NULL,
build_function_type_list(ptr_type_node,
unsigned_type_node,
NULL_TREE),
false);
// The compiler uses __builtin_trap for some exception handling
// cases.
define_builtin(BUILT_IN_TRAP, "__builtin_trap", NULL,
build_function_type(void_type_node, void_list_node),
false);
}
// Get the name to use for the import control function. If there is a
// global function or variable, then we know that that name must be
// unique in the link, and we use it as the basis for our name.
const std::string&
Gogo::get_init_fn_name()
{
if (this->init_fn_name_.empty())
{
gcc_assert(this->package_ != NULL);
if (this->is_main_package())
{
// Use a name which the runtime knows.
this->init_fn_name_ = "__go_init_main";
}
else
{
std::string s = this->unique_prefix();
s.append(1, '.');
s.append(this->package_name());
s.append("..import");
this->init_fn_name_ = s;
}
}
return this->init_fn_name_;
}
// Add statements to INIT_STMT_LIST which run the initialization
// functions for imported packages. This is only used for the "main"
// package.
void
Gogo::init_imports(tree* init_stmt_list)
{
gcc_assert(this->is_main_package());
if (this->imported_init_fns_.empty())
return;
tree fntype = build_function_type(void_type_node, void_list_node);
// We must call them in increasing priority order.
std::vector<Import_init> v;
for (std::set<Import_init>::const_iterator p =
this->imported_init_fns_.begin();
p != this->imported_init_fns_.end();
++p)
v.push_back(*p);
std::sort(v.begin(), v.end());
for (std::vector<Import_init>::const_iterator p = v.begin();
p != v.end();
++p)
{
std::string user_name = p->package_name() + ".init";
tree decl = build_decl(UNKNOWN_LOCATION, FUNCTION_DECL,
get_identifier_from_string(user_name),
fntype);
const std::string& init_name(p->init_name());
SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(init_name));
TREE_PUBLIC(decl) = 1;
DECL_EXTERNAL(decl) = 1;
append_to_statement_list(build_call_expr(decl, 0), init_stmt_list);
}
}
// Register global variables with the garbage collector. We need to
// register all variables which can hold a pointer value. They become
// roots during the mark phase. We build a struct that is easy to
// hook into a list of roots.
// struct __go_gc_root_list
// {
// struct __go_gc_root_list* __next;
// struct __go_gc_root
// {
// void* __decl;
// size_t __size;
// } __roots[];
// };
// The last entry in the roots array has a NULL decl field.
void
Gogo::register_gc_vars(const std::vector<Named_object*>& var_gc,
tree* init_stmt_list)
{
if (var_gc.empty())
return;
size_t count = var_gc.size();
tree root_type = Gogo::builtin_struct(NULL, "__go_gc_root", NULL_TREE, 2,
"__next",
ptr_type_node,
"__size",
sizetype);
tree index_type = build_index_type(size_int(count));
tree array_type = build_array_type(root_type, index_type);
tree root_list_type = make_node(RECORD_TYPE);
root_list_type = Gogo::builtin_struct(NULL, "__go_gc_root_list",
root_list_type, 2,
"__next",
build_pointer_type(root_list_type),
"__roots",
array_type);
// Build an initialier for the __roots array.
VEC(constructor_elt,gc)* roots_init = VEC_alloc(constructor_elt, gc,
count + 1);
size_t i = 0;
for (std::vector<Named_object*>::const_iterator p = var_gc.begin();
p != var_gc.end();
++p, ++i)
{
VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
tree field = TYPE_FIELDS(root_type);
elt->index = field;
tree decl = (*p)->get_tree(this, NULL);
gcc_assert(TREE_CODE(decl) == VAR_DECL);
elt->value = build_fold_addr_expr(decl);
elt = VEC_quick_push(constructor_elt, init, NULL);
field = DECL_CHAIN(field);
elt->index = field;
elt->value = DECL_SIZE_UNIT(decl);
elt = VEC_quick_push(constructor_elt, roots_init, NULL);
elt->index = size_int(i);
elt->value = build_constructor(root_type, init);
}
// The list ends with a NULL entry.
VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
tree field = TYPE_FIELDS(root_type);
elt->index = field;
elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
elt = VEC_quick_push(constructor_elt, init, NULL);
field = DECL_CHAIN(field);
elt->index = field;
elt->value = size_zero_node;
elt = VEC_quick_push(constructor_elt, roots_init, NULL);
elt->index = size_int(i);
elt->value = build_constructor(root_type, init);
// Build a constructor for the struct.
VEC(constructor_elt,gc*) root_list_init = VEC_alloc(constructor_elt, gc, 2);
elt = VEC_quick_push(constructor_elt, root_list_init, NULL);
field = TYPE_FIELDS(root_list_type);
elt->index = field;
elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
elt = VEC_quick_push(constructor_elt, root_list_init, NULL);
field = DECL_CHAIN(field);
elt->index = field;
elt->value = build_constructor(array_type, roots_init);
// Build a decl to register.
tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
create_tmp_var_name("gc"), root_list_type);
DECL_EXTERNAL(decl) = 0;
TREE_PUBLIC(decl) = 0;
TREE_STATIC(decl) = 1;
DECL_ARTIFICIAL(decl) = 1;
DECL_INITIAL(decl) = build_constructor(root_list_type, root_list_init);
rest_of_decl_compilation(decl, 1, 0);
static tree register_gc_fndecl;
tree call = Gogo::call_builtin(®ister_gc_fndecl, BUILTINS_LOCATION,
"__go_register_gc_roots",
1,
void_type_node,
build_pointer_type(root_list_type),
build_fold_addr_expr(decl));
if (call != error_mark_node)
append_to_statement_list(call, init_stmt_list);
}
// Build the decl for the initialization function.
tree
Gogo::initialization_function_decl()
{
// The tedious details of building your own function. There doesn't
// seem to be a helper function for this.
std::string name = this->package_name() + ".init";
tree fndecl = build_decl(BUILTINS_LOCATION, FUNCTION_DECL,
get_identifier_from_string(name),
build_function_type(void_type_node,
void_list_node));
const std::string& asm_name(this->get_init_fn_name());
SET_DECL_ASSEMBLER_NAME(fndecl, get_identifier_from_string(asm_name));
tree resdecl = build_decl(BUILTINS_LOCATION, RESULT_DECL, NULL_TREE,
void_type_node);
DECL_ARTIFICIAL(resdecl) = 1;
DECL_CONTEXT(resdecl) = fndecl;
DECL_RESULT(fndecl) = resdecl;
TREE_STATIC(fndecl) = 1;
TREE_USED(fndecl) = 1;
DECL_ARTIFICIAL(fndecl) = 1;
TREE_PUBLIC(fndecl) = 1;
DECL_INITIAL(fndecl) = make_node(BLOCK);
TREE_USED(DECL_INITIAL(fndecl)) = 1;
return fndecl;
}
// Create the magic initialization function. INIT_STMT_LIST is the
// code that it needs to run.
void
Gogo::write_initialization_function(tree fndecl, tree init_stmt_list)
{
// Make sure that we thought we needed an initialization function,
// as otherwise we will not have reported it in the export data.
gcc_assert(this->is_main_package() || this->need_init_fn_);
if (fndecl == NULL_TREE)
fndecl = this->initialization_function_decl();
DECL_SAVED_TREE(fndecl) = init_stmt_list;
current_function_decl = fndecl;
if (DECL_STRUCT_FUNCTION(fndecl) == NULL)
push_struct_function(fndecl);
else
push_cfun(DECL_STRUCT_FUNCTION(fndecl));
cfun->function_end_locus = BUILTINS_LOCATION;
gimplify_function_tree(fndecl);
cgraph_add_new_function(fndecl, false);
cgraph_mark_needed_node(cgraph_node(fndecl));
current_function_decl = NULL_TREE;
pop_cfun();
}
// Search for references to VAR in any statements or called functions.
class Find_var : public Traverse
{
public:
// A hash table we use to avoid looping. The index is the name of a
// named object. We only look through objects defined in this
// package.
typedef Unordered_set(std::string) Seen_objects;
Find_var(Named_object* var, Seen_objects* seen_objects)
: Traverse(traverse_expressions),
var_(var), seen_objects_(seen_objects), found_(false)
{ }
// Whether the variable was found.
bool
found() const
{ return this->found_; }
int
expression(Expression**);
private:
// The variable we are looking for.
Named_object* var_;
// Names of objects we have already seen.
Seen_objects* seen_objects_;
// True if the variable was found.
bool found_;
};
// See if EXPR refers to VAR, looking through function calls and
// variable initializations.
int
Find_var::expression(Expression** pexpr)
{
Expression* e = *pexpr;
Var_expression* ve = e->var_expression();
if (ve != NULL)
{
Named_object* v = ve->named_object();
if (v == this->var_)
{
this->found_ = true;
return TRAVERSE_EXIT;
}
if (v->is_variable() && v->package() == NULL)
{
Expression* init = v->var_value()->init();
if (init != NULL)
{
std::pair<Seen_objects::iterator, bool> ins =
this->seen_objects_->insert(v->name());
if (ins.second)
{
// This is the first time we have seen this name.
if (Expression::traverse(&init, this) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
}
}
}
}
// We traverse the code of any function we see. Note that this
// means that we will traverse the code of a function whose address
// is taken even if it is not called.
Func_expression* fe = e->func_expression();
if (fe != NULL)
{
const Named_object* f = fe->named_object();
if (f->is_function() && f->package() == NULL)
{
std::pair<Seen_objects::iterator, bool> ins =
this->seen_objects_->insert(f->name());
if (ins.second)
{
// This is the first time we have seen this name.
if (f->func_value()->block()->traverse(this) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
}
}
}
return TRAVERSE_CONTINUE;
}
// Return true if EXPR refers to VAR.
static bool
expression_requires(Expression* expr, Block* preinit, Named_object* var)
{
Find_var::Seen_objects seen_objects;
Find_var find_var(var, &seen_objects);
if (expr != NULL)
Expression::traverse(&expr, &find_var);
if (preinit != NULL)
preinit->traverse(&find_var);
return find_var.found();
}
// Sort variable initializations. If the initialization expression
// for variable A refers directly or indirectly to the initialization
// expression for variable B, then we must initialize B before A.
class Var_init
{
public:
Var_init()
: var_(NULL), init_(NULL_TREE), waiting_(0)
{ }
Var_init(Named_object* var, tree init)
: var_(var), init_(init), waiting_(0)
{ }
// Return the variable.
Named_object*
var() const
{ return this->var_; }
// Return the initialization expression.
tree
init() const
{ return this->init_; }
// Return the number of variables waiting for this one to be
// initialized.
size_t
waiting() const
{ return this->waiting_; }
// Increment the number waiting.
void
increment_waiting()
{ ++this->waiting_; }
private:
// The variable being initialized.
Named_object* var_;
// The initialization expression to run.
tree init_;
// The number of variables which are waiting for this one.
size_t waiting_;
};
typedef std::list<Var_init> Var_inits;
// Sort the variable initializations. The rule we follow is that we
// emit them in the order they appear in the array, except that if the
// initialization expression for a variable V1 depends upon another
// variable V2 then we initialize V1 after V2.
static void
sort_var_inits(Var_inits* var_inits)
{
Var_inits ready;
while (!var_inits->empty())
{
Var_inits::iterator p1 = var_inits->begin();
Named_object* var = p1->var();
Expression* init = var->var_value()->init();
Block* preinit = var->var_value()->preinit();
// Start walking through the list to see which variables VAR
// needs to wait for. We can skip P1->WAITING variables--that
// is the number we've already checked.
Var_inits::iterator p2 = p1;
++p2;
for (size_t i = p1->waiting(); i > 0; --i)
++p2;
for (; p2 != var_inits->end(); ++p2)
{
if (expression_requires(init, preinit, p2->var()))
{
// Check for cycles.
if (expression_requires(p2->var()->var_value()->init(),
p2->var()->var_value()->preinit(),
var))
{
error_at(var->location(),
("initialization expressions for %qs and "
"%qs depend upon each other"),
var->message_name().c_str(),
p2->var()->message_name().c_str());
inform(p2->var()->location(), "%qs defined here",
p2->var()->message_name().c_str());
p2 = var_inits->end();
}
else
{
// We can't emit P1 until P2 is emitted. Move P1.
// Note that the WAITING loop always executes at
// least once, which is what we want.
p2->increment_waiting();
Var_inits::iterator p3 = p2;
for (size_t i = p2->waiting(); i > 0; --i)
++p3;
var_inits->splice(p3, *var_inits, p1);
}
break;
}
}
if (p2 == var_inits->end())
{
// VAR does not depends upon any other initialization expressions.
// Check for a loop of VAR on itself. We only do this if
// INIT is not NULL; when INIT is NULL, it means that
// PREINIT sets VAR, which we will interpret as a loop.
if (init != NULL && expression_requires(init, preinit, var))
error_at(var->location(),
"initialization expression for %qs depends upon itself",
var->message_name().c_str());
ready.splice(ready.end(), *var_inits, p1);
}
}
// Now READY is the list in the desired initialization order.
var_inits->swap(ready);
}
// Write out the global definitions.
void
Gogo::write_globals()
{
this->convert_named_types();
this->build_interface_method_tables();
Bindings* bindings = this->current_bindings();
size_t count = bindings->size_definitions();
tree* vec = new tree[count];
tree init_fndecl = NULL_TREE;
tree init_stmt_list = NULL_TREE;
if (this->is_main_package())
this->init_imports(&init_stmt_list);
// A list of variable initializations.
Var_inits var_inits;
// A list of variables which need to be registered with the garbage
// collector.
std::vector<Named_object*> var_gc;
var_gc.reserve(count);
tree var_init_stmt_list = NULL_TREE;
size_t i = 0;
for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
p != bindings->end_definitions();
++p, ++i)
{
Named_object* no = *p;
gcc_assert(!no->is_type_declaration() && !no->is_function_declaration());
// There is nothing to do for a package.
if (no->is_package())
{
--i;
--count;
continue;
}
// There is nothing to do for an object which was imported from
// a different package into the global scope.
if (no->package() != NULL)
{
--i;
--count;
continue;
}
// There is nothing useful we can output for constants which
// have ideal or non-integeral type.
if (no->is_const())
{
Type* type = no->const_value()->type();
if (type == NULL)
type = no->const_value()->expr()->type();
if (type->is_abstract() || type->integer_type() == NULL)
{
--i;
--count;
continue;
}
}
vec[i] = no->get_tree(this, NULL);
if (vec[i] == error_mark_node)
{
gcc_assert(saw_errors());
--i;
--count;
continue;
}
// If a variable is initialized to a non-constant value, do the
// initialization in an initialization function.
if (TREE_CODE(vec[i]) == VAR_DECL)
{
gcc_assert(no->is_variable());
// Check for a sink variable, which may be used to run
// an initializer purely for its side effects.
bool is_sink = no->name()[0] == '_' && no->name()[1] == '.';
tree var_init_tree = NULL_TREE;
if (!no->var_value()->has_pre_init())
{
tree init = no->var_value()->get_init_tree(this, NULL);
if (init == error_mark_node)
gcc_assert(saw_errors());
else if (init == NULL_TREE)
;
else if (TREE_CONSTANT(init))
DECL_INITIAL(vec[i]) = init;
else if (is_sink)
var_init_tree = init;
else
var_init_tree = fold_build2_loc(no->location(), MODIFY_EXPR,
void_type_node, vec[i], init);
}
else
{
// We are going to create temporary variables which
// means that we need an fndecl.
if (init_fndecl == NULL_TREE)
init_fndecl = this->initialization_function_decl();
current_function_decl = init_fndecl;
if (DECL_STRUCT_FUNCTION(init_fndecl) == NULL)
push_struct_function(init_fndecl);
else
push_cfun(DECL_STRUCT_FUNCTION(init_fndecl));
tree var_decl = is_sink ? NULL_TREE : vec[i];
var_init_tree = no->var_value()->get_init_block(this, NULL,
var_decl);
current_function_decl = NULL_TREE;
pop_cfun();
}
if (var_init_tree != NULL_TREE && var_init_tree != error_mark_node)
{
if (no->var_value()->init() == NULL
&& !no->var_value()->has_pre_init())
append_to_statement_list(var_init_tree, &var_init_stmt_list);
else
var_inits.push_back(Var_init(no, var_init_tree));
}
if (!is_sink && no->var_value()->type()->has_pointer())
var_gc.push_back(no);
}
}
// Register global variables with the garbage collector.
this->register_gc_vars(var_gc, &init_stmt_list);
// Simple variable initializations, after all variables are
// registered.
append_to_statement_list(var_init_stmt_list, &init_stmt_list);
// Complex variable initializations, first sorting them into a
// workable order.
if (!var_inits.empty())
{
sort_var_inits(&var_inits);
for (Var_inits::const_iterator p = var_inits.begin();
p != var_inits.end();
++p)
append_to_statement_list(p->init(), &init_stmt_list);
}
// After all the variables are initialized, call the "init"
// functions if there are any.
for (std::vector<Named_object*>::const_iterator p =
this->init_functions_.begin();
p != this->init_functions_.end();
++p)
{
tree decl = (*p)->get_tree(this, NULL);
tree call = build_call_expr(decl, 0);
append_to_statement_list(call, &init_stmt_list);
}
// Set up a magic function to do all the initialization actions.
// This will be called if this package is imported.
if (init_stmt_list != NULL_TREE
|| this->need_init_fn_
|| this->is_main_package())
this->write_initialization_function(init_fndecl, init_stmt_list);
// Pass everything back to the middle-end.
wrapup_global_declarations(vec, count);
cgraph_finalize_compilation_unit();
check_global_declarations(vec, count);
emit_debug_global_declarations(vec, count);
delete[] vec;
}
// Get a tree for the identifier for a named object.
tree
Named_object::get_id(Gogo* gogo)
{
std::string decl_name;
if (this->is_function_declaration()
&& !this->func_declaration_value()->asm_name().empty())
decl_name = this->func_declaration_value()->asm_name();
else if ((this->is_variable() && !this->var_value()->is_global())
|| (this->is_type()
&& this->type_value()->location() == BUILTINS_LOCATION))
{
// We don't need the package name for local variables or builtin
// types.
decl_name = Gogo::unpack_hidden_name(this->name_);
}
else
{
std::string package_name;
if (this->package_ == NULL)
package_name = gogo->package_name();
else
package_name = this->package_->name();
decl_name = package_name + '.' + Gogo::unpack_hidden_name(this->name_);
Function_type* fntype;
if (this->is_function())
fntype = this->func_value()->type();
else if (this->is_function_declaration())
fntype = this->func_declaration_value()->type();
else
fntype = NULL;
if (fntype != NULL && fntype->is_method())
{
decl_name.push_back('.');
decl_name.append(fntype->receiver()->type()->mangled_name(gogo));
}
}
if (this->is_type())
{
const Named_object* in_function = this->type_value()->in_function();
if (in_function != NULL)
decl_name += '$' + in_function->name();
}
return get_identifier_from_string(decl_name);
}
// Get a tree for a named object.
tree
Named_object::get_tree(Gogo* gogo, Named_object* function)
{
if (this->tree_ != NULL_TREE)
{
// If this is a variable whose address is taken, we must rebuild
// the INDIRECT_REF each time to avoid invalid sharing.
tree ret = this->tree_;
if (((this->classification_ == NAMED_OBJECT_VAR
&& this->var_value()->is_in_heap())
|| (this->classification_ == NAMED_OBJECT_RESULT_VAR
&& this->result_var_value()->is_in_heap()))
&& ret != error_mark_node)
{
gcc_assert(TREE_CODE(ret) == INDIRECT_REF);
ret = build_fold_indirect_ref(TREE_OPERAND(ret, 0));
TREE_THIS_NOTRAP(ret) = 1;
}
return ret;
}
tree name;
if (this->classification_ == NAMED_OBJECT_TYPE)
name = NULL_TREE;
else
name = this->get_id(gogo);
tree decl;
switch (this->classification_)
{
case NAMED_OBJECT_CONST:
{
Named_constant* named_constant = this->u_.const_value;
Translate_context subcontext(gogo, function, NULL, NULL_TREE);
tree expr_tree = named_constant->expr()->get_tree(&subcontext);
if (expr_tree == error_mark_node)
decl = error_mark_node;
else
{
Type* type = named_constant->type();
if (type != NULL && !type->is_abstract())
{
if (!type->is_undefined())
expr_tree = fold_convert(type->get_tree(gogo), expr_tree);
else
{
// Make sure we report the error.
type->base();
expr_tree = error_mark_node;
}
}
if (expr_tree == error_mark_node)
decl = error_mark_node;
else if (INTEGRAL_TYPE_P(TREE_TYPE(expr_tree)))
{
decl = build_decl(named_constant->location(), CONST_DECL,
name, TREE_TYPE(expr_tree));
DECL_INITIAL(decl) = expr_tree;
TREE_CONSTANT(decl) = 1;
TREE_READONLY(decl) = 1;
}
else
{
// A CONST_DECL is only for an enum constant, so we
// shouldn't use for non-integral types. Instead we
// just return the constant itself, rather than a
// decl.
decl = expr_tree;
}
}
}
break;
case NAMED_OBJECT_TYPE:
{
Named_type* named_type = this->u_.type_value;
tree type_tree = named_type->get_tree(gogo);
if (type_tree == error_mark_node)
decl = error_mark_node;
else
{
decl = TYPE_NAME(type_tree);
gcc_assert(decl != NULL_TREE);
// We need to produce a type descriptor for every named
// type, and for a pointer to every named type, since
// other files or packages might refer to them. We need
// to do this even for hidden types, because they might
// still be returned by some function. Simply calling the
// type_descriptor method is enough to create the type
// descriptor, even though we don't do anything with it.
if (this->package_ == NULL)
{
named_type->type_descriptor_pointer(gogo);
Type* pn = Type::make_pointer_type(named_type);
pn->type_descriptor_pointer(gogo);
}
}
}
break;
case NAMED_OBJECT_TYPE_DECLARATION:
error("reference to undefined type %qs",
this->message_name().c_str());
return error_mark_node;
case NAMED_OBJECT_VAR:
{
Variable* var = this->u_.var_value;
Type* type = var->type();
if (type->is_error_type()
|| (type->is_undefined()
&& (!var->is_global() || this->package() == NULL)))
{
// Force the error for an undefined type, just in case.
type->base();
decl = error_mark_node;
}
else
{
tree var_type = type->get_tree(gogo);
bool is_parameter = var->is_parameter();
if (var->is_receiver() && type->points_to() == NULL)
is_parameter = false;
if (var->is_in_heap())
{
is_parameter = false;
var_type = build_pointer_type(var_type);
}
decl = build_decl(var->location(),
is_parameter ? PARM_DECL : VAR_DECL,
name, var_type);
if (!var->is_global())
{
tree fnid = function->get_id(gogo);
tree fndecl = function->func_value()->get_or_make_decl(gogo,
function,
fnid);
DECL_CONTEXT(decl) = fndecl;
}
if (is_parameter)
DECL_ARG_TYPE(decl) = TREE_TYPE(decl);
if (var->is_global())
{
const Package* package = this->package();
if (package == NULL)
TREE_STATIC(decl) = 1;
else
DECL_EXTERNAL(decl) = 1;
if (!Gogo::is_hidden_name(this->name_))
{
TREE_PUBLIC(decl) = 1;
std::string asm_name = (package == NULL
? gogo->unique_prefix()
: package->unique_prefix());
asm_name.append(1, '.');
asm_name.append(IDENTIFIER_POINTER(name),
IDENTIFIER_LENGTH(name));
tree asm_id = get_identifier_from_string(asm_name);
SET_DECL_ASSEMBLER_NAME(decl, asm_id);
}
}
// FIXME: We should only set this for variables which are
// actually used somewhere.
TREE_USED(decl) = 1;
}
}
break;
case NAMED_OBJECT_RESULT_VAR:
{
Result_variable* result = this->u_.result_var_value;
Type* type = result->type();
if (type->is_error_type() || type->is_undefined())
{
// Force the error.
type->base();
decl = error_mark_node;
}
else
{
gcc_assert(result->function() == function->func_value());
source_location loc = function->location();
tree result_type = type->get_tree(gogo);
tree init;
if (!result->is_in_heap())
init = type->get_init_tree(gogo, false);
else
{
tree space = gogo->allocate_memory(type,
TYPE_SIZE_UNIT(result_type),
loc);
result_type = build_pointer_type(result_type);
tree subinit = type->get_init_tree(gogo, true);
if (subinit == NULL_TREE)
init = fold_convert_loc(loc, result_type, space);
else
{
space = save_expr(space);
space = fold_convert_loc(loc, result_type, space);
tree spaceref = build_fold_indirect_ref_loc(loc, space);
TREE_THIS_NOTRAP(spaceref) = 1;
tree set = fold_build2_loc(loc, MODIFY_EXPR, void_type_node,
spaceref, subinit);
init = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(space),
set, space);
}
}
decl = build_decl(loc, VAR_DECL, name, result_type);
tree fnid = function->get_id(gogo);
tree fndecl = function->func_value()->get_or_make_decl(gogo,
function,
fnid);
DECL_CONTEXT(decl) = fndecl;
DECL_INITIAL(decl) = init;
TREE_USED(decl) = 1;
}
}
break;
case NAMED_OBJECT_SINK:
gcc_unreachable();
case NAMED_OBJECT_FUNC:
{
Function* func = this->u_.func_value;
decl = func->get_or_make_decl(gogo, this, name);
if (decl != error_mark_node)
{
if (func->block() != NULL)
{
if (DECL_STRUCT_FUNCTION(decl) == NULL)
push_struct_function(decl);
else
push_cfun(DECL_STRUCT_FUNCTION(decl));
cfun->function_end_locus = func->block()->end_location();
current_function_decl = decl;
func->build_tree(gogo, this);
gimplify_function_tree(decl);
cgraph_finalize_function(decl, true);
current_function_decl = NULL_TREE;
pop_cfun();
}
}
}
break;
default:
gcc_unreachable();
}
if (TREE_TYPE(decl) == error_mark_node)
decl = error_mark_node;
tree ret = decl;
// If this is a local variable whose address is taken, then we
// actually store it in the heap. For uses of the variable we need
// to return a reference to that heap location.
if (((this->classification_ == NAMED_OBJECT_VAR
&& this->var_value()->is_in_heap())
|| (this->classification_ == NAMED_OBJECT_RESULT_VAR
&& this->result_var_value()->is_in_heap()))
&& ret != error_mark_node)
{
gcc_assert(POINTER_TYPE_P(TREE_TYPE(ret)));
ret = build_fold_indirect_ref(ret);
TREE_THIS_NOTRAP(ret) = 1;
}
this->tree_ = ret;
if (ret != error_mark_node)
go_preserve_from_gc(ret);
return ret;
}
// Get the initial value of a variable as a tree. This does not
// consider whether the variable is in the heap--it returns the
// initial value as though it were always stored in the stack.
tree
Variable::get_init_tree(Gogo* gogo, Named_object* function)
{
gcc_assert(this->preinit_ == NULL);
if (this->init_ == NULL)
{
gcc_assert(!this->is_parameter_);
return this->type_->get_init_tree(gogo, this->is_global_);
}
else
{
Translate_context context(gogo, function, NULL, NULL_TREE);
tree rhs_tree = this->init_->get_tree(&context);
return Expression::convert_for_assignment(&context, this->type(),
this->init_->type(),
rhs_tree, this->location());
}
}
// Get the initial value of a variable when a block is required.
// VAR_DECL is the decl to set; it may be NULL for a sink variable.
tree
Variable::get_init_block(Gogo* gogo, Named_object* function, tree var_decl)
{
gcc_assert(this->preinit_ != NULL);
// We want to add the variable assignment to the end of the preinit
// block. The preinit block may have a TRY_FINALLY_EXPR and a
// TRY_CATCH_EXPR; if it does, we want to add to the end of the
// regular statements.
Translate_context context(gogo, function, NULL, NULL_TREE);
tree block_tree = this->preinit_->get_tree(&context);
if (block_tree == error_mark_node)
return error_mark_node;
gcc_assert(TREE_CODE(block_tree) == BIND_EXPR);
tree statements = BIND_EXPR_BODY(block_tree);
while (statements != NULL_TREE
&& (TREE_CODE(statements) == TRY_FINALLY_EXPR
|| TREE_CODE(statements) == TRY_CATCH_EXPR))
statements = TREE_OPERAND(statements, 0);
// It's possible to have pre-init statements without an initializer
// if the pre-init statements set the variable.
if (this->init_ != NULL)
{
tree rhs_tree = this->init_->get_tree(&context);
if (rhs_tree == error_mark_node)
return error_mark_node;
if (var_decl == NULL_TREE)
append_to_statement_list(rhs_tree, &statements);
else
{
tree val = Expression::convert_for_assignment(&context, this->type(),
this->init_->type(),
rhs_tree,
this->location());
if (val == error_mark_node)
return error_mark_node;
tree set = fold_build2_loc(this->location(), MODIFY_EXPR,
void_type_node, var_decl, val);
append_to_statement_list(set, &statements);
}
}
return block_tree;
}
// Get a tree for a function decl.
tree
Function::get_or_make_decl(Gogo* gogo, Named_object* no, tree id)
{
if (this->fndecl_ == NULL_TREE)
{
tree functype = this->type_->get_tree(gogo);
if (functype == error_mark_node)
this->fndecl_ = error_mark_node;
else
{
// The type of a function comes back as a pointer, but we
// want the real function type for a function declaration.
gcc_assert(POINTER_TYPE_P(functype));
functype = TREE_TYPE(functype);
tree decl = build_decl(this->location(), FUNCTION_DECL, id, functype);
this->fndecl_ = decl;
if (no->package() != NULL)
;
else if (this->enclosing_ != NULL || Gogo::is_thunk(no))
;
else if (Gogo::unpack_hidden_name(no->name()) == "init"
&& !this->type_->is_method())
;
else if (Gogo::unpack_hidden_name(no->name()) == "main"
&& gogo->is_main_package())
TREE_PUBLIC(decl) = 1;
// Methods have to be public even if they are hidden because
// they can be pulled into type descriptors when using
// anonymous fields.
else if (!Gogo::is_hidden_name(no->name())
|| this->type_->is_method())
{
TREE_PUBLIC(decl) = 1;
std::string asm_name = gogo->unique_prefix();
asm_name.append(1, '.');
asm_name.append(IDENTIFIER_POINTER(id), IDENTIFIER_LENGTH(id));
SET_DECL_ASSEMBLER_NAME(decl,
get_identifier_from_string(asm_name));
}
// Why do we have to do this in the frontend?
tree restype = TREE_TYPE(functype);
tree resdecl = build_decl(this->location(), RESULT_DECL, NULL_TREE,
restype);
DECL_ARTIFICIAL(resdecl) = 1;
DECL_IGNORED_P(resdecl) = 1;
DECL_CONTEXT(resdecl) = decl;
DECL_RESULT(decl) = resdecl;
if (this->enclosing_ != NULL)
DECL_STATIC_CHAIN(decl) = 1;
// If a function calls the predeclared recover function, we
// can't inline it, because recover behaves differently in a
// function passed directly to defer.
if (this->calls_recover_ && !this->is_recover_thunk_)
DECL_UNINLINABLE(decl) = 1;
// If this is a thunk created to call a function which calls
// the predeclared recover function, we need to disable
// stack splitting for the thunk.
if (this->is_recover_thunk_)
{
tree attr = get_identifier("__no_split_stack__");
DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE);
}
go_preserve_from_gc(decl);
if (this->closure_var_ != NULL)
{
push_struct_function(decl);
tree closure_decl = this->closure_var_->get_tree(gogo, no);
if (closure_decl == error_mark_node)
this->fndecl_ = error_mark_node;
else
{
DECL_ARTIFICIAL(closure_decl) = 1;
DECL_IGNORED_P(closure_decl) = 1;
TREE_USED(closure_decl) = 1;
DECL_ARG_TYPE(closure_decl) = TREE_TYPE(closure_decl);
TREE_READONLY(closure_decl) = 1;
DECL_STRUCT_FUNCTION(decl)->static_chain_decl = closure_decl;
}
pop_cfun();
}
}
}
return this->fndecl_;
}
// Get a tree for a function declaration.
tree
Function_declaration::get_or_make_decl(Gogo* gogo, Named_object* no, tree id)
{
if (this->fndecl_ == NULL_TREE)
{
// Let Go code use an asm declaration to pick up a builtin
// function.
if (!this->asm_name_.empty())
{
std::map<std::string, tree>::const_iterator p =
builtin_functions.find(this->asm_name_);
if (p != builtin_functions.end())
{
this->fndecl_ = p->second;
return this->fndecl_;
}
}
tree functype = this->fntype_->get_tree(gogo);
tree decl;
if (functype == error_mark_node)
decl = error_mark_node;
else
{
// The type of a function comes back as a pointer, but we
// want the real function type for a function declaration.
gcc_assert(POINTER_TYPE_P(functype));
functype = TREE_TYPE(functype);
decl = build_decl(this->location(), FUNCTION_DECL, id, functype);
TREE_PUBLIC(decl) = 1;
DECL_EXTERNAL(decl) = 1;
if (this->asm_name_.empty())
{
std::string asm_name = (no->package() == NULL
? gogo->unique_prefix()
: no->package()->unique_prefix());
asm_name.append(1, '.');
asm_name.append(IDENTIFIER_POINTER(id), IDENTIFIER_LENGTH(id));
SET_DECL_ASSEMBLER_NAME(decl,
get_identifier_from_string(asm_name));
}
}
this->fndecl_ = decl;
go_preserve_from_gc(decl);
}
return this->fndecl_;
}
// We always pass the receiver to a method as a pointer. If the
// receiver is actually declared as a non-pointer type, then we copy
// the value into a local variable, so that it has the right type. In
// this function we create the real PARM_DECL to use, and set
// DEC_INITIAL of the var_decl to be the value passed in.
tree
Function::make_receiver_parm_decl(Gogo* gogo, Named_object* no, tree var_decl)
{
if (var_decl == error_mark_node)
return error_mark_node;
// If the function takes the address of a receiver which is passed
// by value, then we will have an INDIRECT_REF here. We need to get
// the real variable.
bool is_in_heap = no->var_value()->is_in_heap();
tree val_type;
if (TREE_CODE(var_decl) != INDIRECT_REF)
{
gcc_assert(!is_in_heap);
val_type = TREE_TYPE(var_decl);
}
else
{
gcc_assert(is_in_heap);
var_decl = TREE_OPERAND(var_decl, 0);
if (var_decl == error_mark_node)
return error_mark_node;
gcc_assert(POINTER_TYPE_P(TREE_TYPE(var_decl)));
val_type = TREE_TYPE(TREE_TYPE(var_decl));
}
gcc_assert(TREE_CODE(var_decl) == VAR_DECL);
source_location loc = DECL_SOURCE_LOCATION(var_decl);
std::string name = IDENTIFIER_POINTER(DECL_NAME(var_decl));
name += ".pointer";
tree id = get_identifier_from_string(name);
tree parm_decl = build_decl(loc, PARM_DECL, id, build_pointer_type(val_type));
DECL_CONTEXT(parm_decl) = current_function_decl;
DECL_ARG_TYPE(parm_decl) = TREE_TYPE(parm_decl);
gcc_assert(DECL_INITIAL(var_decl) == NULL_TREE);
// The receiver might be passed as a null pointer.
tree check = fold_build2_loc(loc, NE_EXPR, boolean_type_node, parm_decl,
fold_convert_loc(loc, TREE_TYPE(parm_decl),
null_pointer_node));
tree ind = build_fold_indirect_ref_loc(loc, parm_decl);
TREE_THIS_NOTRAP(ind) = 1;
tree zero_init = no->var_value()->type()->get_init_tree(gogo, false);
tree init = fold_build3_loc(loc, COND_EXPR, TREE_TYPE(ind),
check, ind, zero_init);
if (is_in_heap)
{
tree size = TYPE_SIZE_UNIT(val_type);
tree space = gogo->allocate_memory(no->var_value()->type(), size,
no->location());
space = save_expr(space);
space = fold_convert(build_pointer_type(val_type), space);
tree spaceref = build_fold_indirect_ref_loc(no->location(), space);
TREE_THIS_NOTRAP(spaceref) = 1;
tree check = fold_build2_loc(loc, NE_EXPR, boolean_type_node,
parm_decl,
fold_convert_loc(loc, TREE_TYPE(parm_decl),
null_pointer_node));
tree parmref = build_fold_indirect_ref_loc(no->location(), parm_decl);
TREE_THIS_NOTRAP(parmref) = 1;
tree set = fold_build2_loc(loc, MODIFY_EXPR, void_type_node,
spaceref, parmref);
init = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(space),
build3(COND_EXPR, void_type_node,
check, set, NULL_TREE),
space);
}
DECL_INITIAL(var_decl) = init;
return parm_decl;
}
// If we take the address of a parameter, then we need to copy it into
// the heap. We will access it as a local variable via an
// indirection.
tree
Function::copy_parm_to_heap(Gogo* gogo, Named_object* no, tree ref)
{
if (ref == error_mark_node)
return error_mark_node;
gcc_assert(TREE_CODE(ref) == INDIRECT_REF);
tree var_decl = TREE_OPERAND(ref, 0);
if (var_decl == error_mark_node)
return error_mark_node;
gcc_assert(TREE_CODE(var_decl) == VAR_DECL);
source_location loc = DECL_SOURCE_LOCATION(var_decl);
std::string name = IDENTIFIER_POINTER(DECL_NAME(var_decl));
name += ".param";
tree id = get_identifier_from_string(name);
tree type = TREE_TYPE(var_decl);
gcc_assert(POINTER_TYPE_P(type));
type = TREE_TYPE(type);
tree parm_decl = build_decl(loc, PARM_DECL, id, type);
DECL_CONTEXT(parm_decl) = current_function_decl;
DECL_ARG_TYPE(parm_decl) = type;
tree size = TYPE_SIZE_UNIT(type);
tree space = gogo->allocate_memory(no->var_value()->type(), size, loc);
space = save_expr(space);
space = fold_convert(TREE_TYPE(var_decl), space);
tree spaceref = build_fold_indirect_ref_loc(loc, space);
TREE_THIS_NOTRAP(spaceref) = 1;
tree init = build2(COMPOUND_EXPR, TREE_TYPE(space),
build2(MODIFY_EXPR, void_type_node, spaceref, parm_decl),
space);
DECL_INITIAL(var_decl) = init;
return parm_decl;
}
// Get a tree for function code.
void
Function::build_tree(Gogo* gogo, Named_object* named_function)
{
tree fndecl = this->fndecl_;
gcc_assert(fndecl != NULL_TREE);
tree params = NULL_TREE;
tree* pp = ¶ms;
tree declare_vars = NULL_TREE;
for (Bindings::const_definitions_iterator p =
this->block_->bindings()->begin_definitions();
p != this->block_->bindings()->end_definitions();
++p)
{
if ((*p)->is_variable() && (*p)->var_value()->is_parameter())
{
*pp = (*p)->get_tree(gogo, named_function);
// We always pass the receiver to a method as a pointer. If
// the receiver is declared as a non-pointer type, then we
// copy the value into a local variable.
if ((*p)->var_value()->is_receiver()
&& (*p)->var_value()->type()->points_to() == NULL)
{
tree parm_decl = this->make_receiver_parm_decl(gogo, *p, *pp);
tree var = *pp;
if (TREE_CODE(var) == INDIRECT_REF)
var = TREE_OPERAND(var, 0);
if (var != error_mark_node)
{
gcc_assert(TREE_CODE(var) == VAR_DECL);
DECL_CHAIN(var) = declare_vars;
declare_vars = var;
}
*pp = parm_decl;
}
else if ((*p)->var_value()->is_in_heap())
{
// If we take the address of a parameter, then we need
// to copy it into the heap.
tree parm_decl = this->copy_parm_to_heap(gogo, *p, *pp);
if (*pp != error_mark_node)
{
gcc_assert(TREE_CODE(*pp) == INDIRECT_REF);
tree var_decl = TREE_OPERAND(*pp, 0);
if (var_decl != error_mark_node)
{
gcc_assert(TREE_CODE(var_decl) == VAR_DECL);
DECL_CHAIN(var_decl) = declare_vars;
declare_vars = var_decl;
}
}
*pp = parm_decl;
}
if (*pp != error_mark_node)
{
gcc_assert(TREE_CODE(*pp) == PARM_DECL);
pp = &DECL_CHAIN(*pp);
}
}
else if ((*p)->is_result_variable())
{
tree var_decl = (*p)->get_tree(gogo, named_function);
if (var_decl != error_mark_node
&& (*p)->result_var_value()->is_in_heap())
{
gcc_assert(TREE_CODE(var_decl) == INDIRECT_REF);
var_decl = TREE_OPERAND(var_decl, 0);
}
if (var_decl != error_mark_node)
{
gcc_assert(TREE_CODE(var_decl) == VAR_DECL);
DECL_CHAIN(var_decl) = declare_vars;
declare_vars = var_decl;
}
}
}
*pp = NULL_TREE;
DECL_ARGUMENTS(fndecl) = params;
if (this->block_ != NULL)
{
gcc_assert(DECL_INITIAL(fndecl) == NULL_TREE);
// Declare variables if necessary.
tree bind = NULL_TREE;
if (declare_vars != NULL_TREE)
{
tree block = make_node(BLOCK);
BLOCK_SUPERCONTEXT(block) = fndecl;
DECL_INITIAL(fndecl) = block;
BLOCK_VARS(block) = declare_vars;
TREE_USED(block) = 1;
bind = build3(BIND_EXPR, void_type_node, BLOCK_VARS(block),
NULL_TREE, block);
TREE_SIDE_EFFECTS(bind) = 1;
}
// Build the trees for all the statements in the function.
Translate_context context(gogo, named_function, NULL, NULL_TREE);
tree code = this->block_->get_tree(&context);
tree init = NULL_TREE;
tree except = NULL_TREE;
tree fini = NULL_TREE;
// Initialize variables if necessary.
for (tree v = declare_vars; v != NULL_TREE; v = DECL_CHAIN(v))
{
tree dv = build1(DECL_EXPR, void_type_node, v);
SET_EXPR_LOCATION(dv, DECL_SOURCE_LOCATION(v));
append_to_statement_list(dv, &init);
}
// If we have a defer stack, initialize it at the start of a
// function.
if (this->defer_stack_ != NULL_TREE)
{
tree defer_init = build1(DECL_EXPR, void_type_node,
this->defer_stack_);
SET_EXPR_LOCATION(defer_init, this->block_->start_location());
append_to_statement_list(defer_init, &init);
// Clean up the defer stack when we leave the function.
this->build_defer_wrapper(gogo, named_function, &except, &fini);
}
if (code != NULL_TREE && code != error_mark_node)
{
if (init != NULL_TREE)
code = build2(COMPOUND_EXPR, void_type_node, init, code);
if (except != NULL_TREE)
code = build2(TRY_CATCH_EXPR, void_type_node, code,
build2(CATCH_EXPR, void_type_node, NULL, except));
if (fini != NULL_TREE)
code = build2(TRY_FINALLY_EXPR, void_type_node, code, fini);
}
// Stick the code into the block we built for the receiver, if
// we built on.
if (bind != NULL_TREE && code != NULL_TREE && code != error_mark_node)
{
BIND_EXPR_BODY(bind) = code;
code = bind;
}
DECL_SAVED_TREE(fndecl) = code;
}
}
// Build the wrappers around function code needed if the function has
// any defer statements. This sets *EXCEPT to an exception handler
// and *FINI to a finally handler.
void
Function::build_defer_wrapper(Gogo* gogo, Named_object* named_function,
tree *except, tree *fini)
{
source_location end_loc = this->block_->end_location();
// Add an exception handler. This is used if a panic occurs. Its
// purpose is to stop the stack unwinding if a deferred function
// calls recover. There are more details in
// libgo/runtime/go-unwind.c.
tree stmt_list = NULL_TREE;
static tree check_fndecl;
tree call = Gogo::call_builtin(&check_fndecl,
end_loc,
"__go_check_defer",
1,
void_type_node,
ptr_type_node,
this->defer_stack(end_loc));
if (call != error_mark_node)
append_to_statement_list(call, &stmt_list);
tree retval = this->return_value(gogo, named_function, end_loc, &stmt_list);
tree set;
if (retval == NULL_TREE)
set = NULL_TREE;
else
set = fold_build2_loc(end_loc, MODIFY_EXPR, void_type_node,
DECL_RESULT(this->fndecl_), retval);
tree ret_stmt = fold_build1_loc(end_loc, RETURN_EXPR, void_type_node, set);
append_to_statement_list(ret_stmt, &stmt_list);
gcc_assert(*except == NULL_TREE);
*except = stmt_list;
// Add some finally code to run the defer functions. This is used
// both in the normal case, when no panic occurs, and also if a
// panic occurs to run any further defer functions. Of course, it
// is possible for a defer function to call panic which should be
// caught by another defer function. To handle that we use a loop.
// finish:
// try { __go_undefer(); } catch { __go_check_defer(); goto finish; }
// if (return values are named) return named_vals;
stmt_list = NULL;
tree label = create_artificial_label(end_loc);
tree define_label = fold_build1_loc(end_loc, LABEL_EXPR, void_type_node,
label);
append_to_statement_list(define_label, &stmt_list);
static tree undefer_fndecl;
tree undefer = Gogo::call_builtin(&undefer_fndecl,
end_loc,
"__go_undefer",
1,
void_type_node,
ptr_type_node,
this->defer_stack(end_loc));
if (undefer_fndecl != NULL_TREE)
TREE_NOTHROW(undefer_fndecl) = 0;
tree defer = Gogo::call_builtin(&check_fndecl,
end_loc,
"__go_check_defer",
1,
void_type_node,
ptr_type_node,
this->defer_stack(end_loc));
tree jump = fold_build1_loc(end_loc, GOTO_EXPR, void_type_node, label);
tree catch_body = build2(COMPOUND_EXPR, void_type_node, defer, jump);
catch_body = build2(CATCH_EXPR, void_type_node, NULL, catch_body);
tree try_catch = build2(TRY_CATCH_EXPR, void_type_node, undefer, catch_body);
append_to_statement_list(try_catch, &stmt_list);
if (this->type_->results() != NULL
&& !this->type_->results()->empty()
&& !this->type_->results()->front().name().empty())
{
// If the result variables are named, we need to return them
// again, because they might have been changed by a defer
// function.
retval = this->return_value(gogo, named_function, end_loc,
&stmt_list);
set = fold_build2_loc(end_loc, MODIFY_EXPR, void_type_node,
DECL_RESULT(this->fndecl_), retval);
ret_stmt = fold_build1_loc(end_loc, RETURN_EXPR, void_type_node, set);
append_to_statement_list(ret_stmt, &stmt_list);
}
gcc_assert(*fini == NULL_TREE);
*fini = stmt_list;
}
// Return the value to assign to DECL_RESULT(this->fndecl_). This may
// also add statements to STMT_LIST, which need to be executed before
// the assignment. This is used for a return statement with no
// explicit values.
tree
Function::return_value(Gogo* gogo, Named_object* named_function,
source_location location, tree* stmt_list) const
{
const Typed_identifier_list* results = this->type_->results();
if (results == NULL || results->empty())
return NULL_TREE;
// In the case of an exception handler created for functions with
// defer statements, the result variables may be unnamed.
bool is_named = !results->front().name().empty();
if (is_named)
{
gcc_assert(this->named_results_ != NULL);
if (this->named_results_->size() != results->size())
{
gcc_assert(saw_errors());
return error_mark_node;
}
}
tree retval;
if (results->size() == 1)
{
if (is_named)
return this->named_results_->front()->get_tree(gogo, named_function);
else
return results->front().type()->get_init_tree(gogo, false);
}
else
{
tree rettype = TREE_TYPE(DECL_RESULT(this->fndecl_));
retval = create_tmp_var(rettype, "RESULT");
tree field = TYPE_FIELDS(rettype);
int index = 0;
for (Typed_identifier_list::const_iterator pr = results->begin();
pr != results->end();
++pr, ++index, field = DECL_CHAIN(field))
{
gcc_assert(field != NULL);
tree val;
if (is_named)
val = (*this->named_results_)[index]->get_tree(gogo,
named_function);
else
val = pr->type()->get_init_tree(gogo, false);
tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
build3(COMPONENT_REF, TREE_TYPE(field),
retval, field, NULL_TREE),
val);
append_to_statement_list(set, stmt_list);
}
return retval;
}
}
// Get the tree for the variable holding the defer stack for this
// function. At least at present, the value of this variable is not
// used. However, a pointer to this variable is used as a marker for
// the functions on the defer stack associated with this function.
// Doing things this way permits inlining a function which uses defer.
tree
Function::defer_stack(source_location location)
{
if (this->defer_stack_ == NULL_TREE)
{
tree var = create_tmp_var(ptr_type_node, "DEFER");
DECL_INITIAL(var) = null_pointer_node;
DECL_SOURCE_LOCATION(var) = location;
TREE_ADDRESSABLE(var) = 1;
this->defer_stack_ = var;
}
return fold_convert_loc(location, ptr_type_node,
build_fold_addr_expr_loc(location,
this->defer_stack_));
}
// Get a tree for the statements in a block.
tree
Block::get_tree(Translate_context* context)
{
Gogo* gogo = context->gogo();
tree block = make_node(BLOCK);
// Put the new block into the block tree.
if (context->block() == NULL)
{
tree fndecl;
if (context->function() != NULL)
fndecl = context->function()->func_value()->get_decl();
else
fndecl = current_function_decl;
gcc_assert(fndecl != NULL_TREE);
// We may have already created a block for the receiver.
if (DECL_INITIAL(fndecl) == NULL_TREE)
{
BLOCK_SUPERCONTEXT(block) = fndecl;
DECL_INITIAL(fndecl) = block;
}
else
{
tree superblock_tree = DECL_INITIAL(fndecl);
BLOCK_SUPERCONTEXT(block) = superblock_tree;
gcc_assert(BLOCK_CHAIN(block) == NULL_TREE);
BLOCK_CHAIN(block) = block;
}
}
else
{
tree superblock_tree = context->block_tree();
BLOCK_SUPERCONTEXT(block) = superblock_tree;
tree* pp;
for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
*pp != NULL_TREE;
pp = &BLOCK_CHAIN(*pp))
;
*pp = block;
}
// Expand local variables in the block.
tree* pp = &BLOCK_VARS(block);
for (Bindings::const_definitions_iterator pv =
this->bindings_->begin_definitions();
pv != this->bindings_->end_definitions();
++pv)
{
if ((!(*pv)->is_variable() || !(*pv)->var_value()->is_parameter())
&& !(*pv)->is_result_variable()
&& !(*pv)->is_const())
{
tree var = (*pv)->get_tree(gogo, context->function());
if (var != error_mark_node && TREE_TYPE(var) != error_mark_node)
{
if ((*pv)->is_variable() && (*pv)->var_value()->is_in_heap())
{
gcc_assert(TREE_CODE(var) == INDIRECT_REF);
var = TREE_OPERAND(var, 0);
gcc_assert(TREE_CODE(var) == VAR_DECL);
}
*pp = var;
pp = &DECL_CHAIN(*pp);
}
}
}
*pp = NULL_TREE;
Translate_context subcontext(context->gogo(), context->function(),
this, block);
tree statements = NULL_TREE;
// Expand the statements.
for (std::vector<Statement*>::const_iterator p = this->statements_.begin();
p != this->statements_.end();
++p)
{
tree statement = (*p)->get_tree(&subcontext);
if (statement != error_mark_node)
append_to_statement_list(statement, &statements);
}
TREE_USED(block) = 1;
tree bind = build3(BIND_EXPR, void_type_node, BLOCK_VARS(block), statements,
block);
TREE_SIDE_EFFECTS(bind) = 1;
return bind;
}
// Get the LABEL_DECL for a label.
tree
Label::get_decl()
{
if (this->decl_ == NULL)
{
tree id = get_identifier_from_string(this->name_);
this->decl_ = build_decl(this->location_, LABEL_DECL, id, void_type_node);
DECL_CONTEXT(this->decl_) = current_function_decl;
}
return this->decl_;
}
// Return an expression for the address of this label.
tree
Label::get_addr(source_location location)
{
tree decl = this->get_decl();
TREE_USED(decl) = 1;
TREE_ADDRESSABLE(decl) = 1;
return fold_convert_loc(location, ptr_type_node,
build_fold_addr_expr_loc(location, decl));
}
// Get the LABEL_DECL for an unnamed label.
tree
Unnamed_label::get_decl()
{
if (this->decl_ == NULL)
this->decl_ = create_artificial_label(this->location_);
return this->decl_;
}
// Get the LABEL_EXPR for an unnamed label.
tree
Unnamed_label::get_definition()
{
tree t = build1(LABEL_EXPR, void_type_node, this->get_decl());
SET_EXPR_LOCATION(t, this->location_);
return t;
}
// Return a goto to this label.
tree
Unnamed_label::get_goto(source_location location)
{
tree t = build1(GOTO_EXPR, void_type_node, this->get_decl());
SET_EXPR_LOCATION(t, location);
return t;
}
// Return the integer type to use for a size.
GO_EXTERN_C
tree
go_type_for_size(unsigned int bits, int unsignedp)
{
const char* name;
switch (bits)
{
case 8:
name = unsignedp ? "uint8" : "int8";
break;
case 16:
name = unsignedp ? "uint16" : "int16";
break;
case 32:
name = unsignedp ? "uint32" : "int32";
break;
case 64:
name = unsignedp ? "uint64" : "int64";
break;
default:
if (bits == POINTER_SIZE && unsignedp)
name = "uintptr";
else
return NULL_TREE;
}
Type* type = Type::lookup_integer_type(name);
return type->get_tree(go_get_gogo());
}
// Return the type to use for a mode.
GO_EXTERN_C
tree
go_type_for_mode(enum machine_mode mode, int unsignedp)
{
// FIXME: This static_cast should be in machmode.h.
enum mode_class mc = static_cast<enum mode_class>(GET_MODE_CLASS(mode));
if (mc == MODE_INT)
return go_type_for_size(GET_MODE_BITSIZE(mode), unsignedp);
else if (mc == MODE_FLOAT)
{
Type* type;
switch (GET_MODE_BITSIZE (mode))
{
case 32:
type = Type::lookup_float_type("float32");
break;
case 64:
type = Type::lookup_float_type("float64");
break;
default:
// We have to check for long double in order to support
// i386 excess precision.
if (mode == TYPE_MODE(long_double_type_node))
return long_double_type_node;
return NULL_TREE;
}
return type->float_type()->type_tree();
}
else if (mc == MODE_COMPLEX_FLOAT)
{
Type *type;
switch (GET_MODE_BITSIZE (mode))
{
case 64:
type = Type::lookup_complex_type("complex64");
break;
case 128:
type = Type::lookup_complex_type("complex128");
break;
default:
// We have to check for long double in order to support
// i386 excess precision.
if (mode == TYPE_MODE(complex_long_double_type_node))
return complex_long_double_type_node;
return NULL_TREE;
}
return type->complex_type()->type_tree();
}
else
return NULL_TREE;
}
// Return a tree which allocates SIZE bytes which will holds value of
// type TYPE.
tree
Gogo::allocate_memory(Type* type, tree size, source_location location)
{
// If the package imports unsafe, then it may play games with
// pointers that look like integers.
if (this->imported_unsafe_ || type->has_pointer())
{
static tree new_fndecl;
return Gogo::call_builtin(&new_fndecl,
location,
"__go_new",
1,
ptr_type_node,
sizetype,
size);
}
else
{
static tree new_nopointers_fndecl;
return Gogo::call_builtin(&new_nopointers_fndecl,
location,
"__go_new_nopointers",
1,
ptr_type_node,
sizetype,
size);
}
}
// Build a builtin struct with a list of fields. The name is
// STRUCT_NAME. STRUCT_TYPE is NULL_TREE or an empty RECORD_TYPE
// node; this exists so that the struct can have fields which point to
// itself. If PTYPE is not NULL, store the result in *PTYPE. There
// are NFIELDS fields. Each field is a name (a const char*) followed
// by a type (a tree).
tree
Gogo::builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
int nfields, ...)
{
if (ptype != NULL && *ptype != NULL_TREE)
return *ptype;
va_list ap;
va_start(ap, nfields);
tree fields = NULL_TREE;
for (int i = 0; i < nfields; ++i)
{
const char* field_name = va_arg(ap, const char*);
tree type = va_arg(ap, tree);
if (type == error_mark_node)
{
if (ptype != NULL)
*ptype = error_mark_node;
return error_mark_node;
}
tree field = build_decl(BUILTINS_LOCATION, FIELD_DECL,
get_identifier(field_name), type);
DECL_CHAIN(field) = fields;
fields = field;
}
va_end(ap);
if (struct_type == NULL_TREE)
struct_type = make_node(RECORD_TYPE);
finish_builtin_struct(struct_type, struct_name, fields, NULL_TREE);
if (ptype != NULL)
{
go_preserve_from_gc(struct_type);
*ptype = struct_type;
}
return struct_type;
}
// Return a type to use for pointer to const char for a string.
tree
Gogo::const_char_pointer_type_tree()
{
static tree type;
if (type == NULL_TREE)
{
tree const_char_type = build_qualified_type(unsigned_char_type_node,
TYPE_QUAL_CONST);
type = build_pointer_type(const_char_type);
go_preserve_from_gc(type);
}
return type;
}
// Return a tree for a string constant.
tree
Gogo::string_constant_tree(const std::string& val)
{
tree index_type = build_index_type(size_int(val.length()));
tree const_char_type = build_qualified_type(unsigned_char_type_node,
TYPE_QUAL_CONST);
tree string_type = build_array_type(const_char_type, index_type);
string_type = build_variant_type_copy(string_type);
TYPE_STRING_FLAG(string_type) = 1;
tree string_val = build_string(val.length(), val.data());
TREE_TYPE(string_val) = string_type;
return string_val;
}
// Return a tree for a Go string constant.
tree
Gogo::go_string_constant_tree(const std::string& val)
{
tree string_type = Type::make_string_type()->get_tree(this);
VEC(constructor_elt, gc)* init = VEC_alloc(constructor_elt, gc, 2);
constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
tree field = TYPE_FIELDS(string_type);
gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__data") == 0);
elt->index = field;
tree str = Gogo::string_constant_tree(val);
elt->value = fold_convert(TREE_TYPE(field),
build_fold_addr_expr(str));
elt = VEC_quick_push(constructor_elt, init, NULL);
field = DECL_CHAIN(field);
gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__length") == 0);
elt->index = field;
elt->value = build_int_cst_type(TREE_TYPE(field), val.length());
tree constructor = build_constructor(string_type, init);
TREE_READONLY(constructor) = 1;
TREE_CONSTANT(constructor) = 1;
return constructor;
}
// Return a tree for a pointer to a Go string constant. This is only
// used for type descriptors, so we return a pointer to a constant
// decl.
tree
Gogo::ptr_go_string_constant_tree(const std::string& val)
{
tree pval = this->go_string_constant_tree(val);
tree decl = build_decl(UNKNOWN_LOCATION, VAR_DECL,
create_tmp_var_name("SP"), TREE_TYPE(pval));
DECL_EXTERNAL(decl) = 0;
TREE_PUBLIC(decl) = 0;
TREE_USED(decl) = 1;
TREE_READONLY(decl) = 1;
TREE_CONSTANT(decl) = 1;
TREE_STATIC(decl) = 1;
DECL_ARTIFICIAL(decl) = 1;
DECL_INITIAL(decl) = pval;
rest_of_decl_compilation(decl, 1, 0);
return build_fold_addr_expr(decl);
}
// Build the type of the struct that holds a slice for the given
// element type.
tree
Gogo::slice_type_tree(tree element_type_tree)
{
// We use int for the count and capacity fields in a slice header.
// This matches 6g. The language definition guarantees that we
// can't allocate space of a size which does not fit in int
// anyhow. FIXME: integer_type_node is the the C type "int" but is
// not necessarily the Go type "int". They will differ when the C
// type "int" has fewer than 32 bits.
return Gogo::builtin_struct(NULL, "__go_slice", NULL_TREE, 3,
"__values",
build_pointer_type(element_type_tree),
"__count",
integer_type_node,
"__capacity",
integer_type_node);
}
// Given the tree for a slice type, return the tree for the type of
// the elements of the slice.
tree
Gogo::slice_element_type_tree(tree slice_type_tree)
{
gcc_assert(TREE_CODE(slice_type_tree) == RECORD_TYPE
&& POINTER_TYPE_P(TREE_TYPE(TYPE_FIELDS(slice_type_tree))));
return TREE_TYPE(TREE_TYPE(TYPE_FIELDS(slice_type_tree)));
}
// Build a constructor for a slice. SLICE_TYPE_TREE is the type of
// the slice. VALUES is the value pointer and COUNT is the number of
// entries. If CAPACITY is not NULL, it is the capacity; otherwise
// the capacity and the count are the same.
tree
Gogo::slice_constructor(tree slice_type_tree, tree values, tree count,
tree capacity)
{
gcc_assert(TREE_CODE(slice_type_tree) == RECORD_TYPE);
VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
tree field = TYPE_FIELDS(slice_type_tree);
gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
elt->index = field;
gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(field))
== TYPE_MAIN_VARIANT(TREE_TYPE(values)));
elt->value = values;
count = fold_convert(sizetype, count);
if (capacity == NULL_TREE)
{
count = save_expr(count);
capacity = count;
}
field = DECL_CHAIN(field);
gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
elt = VEC_quick_push(constructor_elt, init, NULL);
elt->index = field;
elt->value = fold_convert(TREE_TYPE(field), count);
field = DECL_CHAIN(field);
gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
elt = VEC_quick_push(constructor_elt, init, NULL);
elt->index = field;
elt->value = fold_convert(TREE_TYPE(field), capacity);
return build_constructor(slice_type_tree, init);
}
// Build a constructor for an empty slice.
tree
Gogo::empty_slice_constructor(tree slice_type_tree)
{
tree element_field = TYPE_FIELDS(slice_type_tree);
tree ret = Gogo::slice_constructor(slice_type_tree,
fold_convert(TREE_TYPE(element_field),
null_pointer_node),
size_zero_node,
size_zero_node);
TREE_CONSTANT(ret) = 1;
return ret;
}
// Build a map descriptor for a map of type MAPTYPE.
tree
Gogo::map_descriptor(Map_type* maptype)
{
if (this->map_descriptors_ == NULL)
this->map_descriptors_ = new Map_descriptors(10);
std::pair<const Map_type*, tree> val(maptype, NULL);
std::pair<Map_descriptors::iterator, bool> ins =
this->map_descriptors_->insert(val);
Map_descriptors::iterator p = ins.first;
if (!ins.second)
{
if (p->second == error_mark_node)
return error_mark_node;
gcc_assert(p->second != NULL_TREE && DECL_P(p->second));
return build_fold_addr_expr(p->second);
}
Type* keytype = maptype->key_type();
Type* valtype = maptype->val_type();
std::string mangled_name = ("__go_map_" + maptype->mangled_name(this));
tree id = get_identifier_from_string(mangled_name);
// Get the type of the map descriptor. This is __go_map_descriptor
// in libgo/map.h.
tree struct_type = this->map_descriptor_type();
// The map entry type is a struct with three fields. This struct is
// specific to MAPTYPE. Build it.
tree map_entry_type = make_node(RECORD_TYPE);
map_entry_type = Gogo::builtin_struct(NULL, "__map", map_entry_type, 3,
"__next",
build_pointer_type(map_entry_type),
"__key",
keytype->get_tree(this),
"__val",
valtype->get_tree(this));
if (map_entry_type == error_mark_node)
{
p->second = error_mark_node;
return error_mark_node;
}
tree map_entry_key_field = DECL_CHAIN(TYPE_FIELDS(map_entry_type));
gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(map_entry_key_field)),
"__key") == 0);
tree map_entry_val_field = DECL_CHAIN(map_entry_key_field);
gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(map_entry_val_field)),
"__val") == 0);
// Initialize the entries.
tree map_descriptor_field = TYPE_FIELDS(struct_type);
gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(map_descriptor_field)),
"__map_descriptor") == 0);
tree entry_size_field = DECL_CHAIN(map_descriptor_field);
gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(entry_size_field)),
"__entry_size") == 0);
tree key_offset_field = DECL_CHAIN(entry_size_field);
gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(key_offset_field)),
"__key_offset") == 0);
tree val_offset_field = DECL_CHAIN(key_offset_field);
gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(val_offset_field)),
"__val_offset") == 0);
VEC(constructor_elt, gc)* descriptor = VEC_alloc(constructor_elt, gc, 6);
constructor_elt* elt = VEC_quick_push(constructor_elt, descriptor, NULL);
elt->index = map_descriptor_field;
elt->value = maptype->type_descriptor_pointer(this);
elt = VEC_quick_push(constructor_elt, descriptor, NULL);
elt->index = entry_size_field;
elt->value = TYPE_SIZE_UNIT(map_entry_type);
elt = VEC_quick_push(constructor_elt, descriptor, NULL);
elt->index = key_offset_field;
elt->value = byte_position(map_entry_key_field);
elt = VEC_quick_push(constructor_elt, descriptor, NULL);
elt->index = val_offset_field;
elt->value = byte_position(map_entry_val_field);
tree constructor = build_constructor(struct_type, descriptor);
tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL, id, struct_type);
TREE_STATIC(decl) = 1;
TREE_USED(decl) = 1;
TREE_READONLY(decl) = 1;
TREE_CONSTANT(decl) = 1;
DECL_INITIAL(decl) = constructor;
make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
resolve_unique_section(decl, 1, 0);
rest_of_decl_compilation(decl, 1, 0);
go_preserve_from_gc(decl);
p->second = decl;
return build_fold_addr_expr(decl);
}
// Return a tree for the type of a map descriptor. This is struct
// __go_map_descriptor in libgo/runtime/map.h. This is the same for
// all map types.
tree
Gogo::map_descriptor_type()
{
static tree struct_type;
tree dtype = Type::make_type_descriptor_type()->get_tree(this);
dtype = build_qualified_type(dtype, TYPE_QUAL_CONST);
return Gogo::builtin_struct(&struct_type, "__go_map_descriptor", NULL_TREE,
4,
"__map_descriptor",
build_pointer_type(dtype),
"__entry_size",
sizetype,
"__key_offset",
sizetype,
"__val_offset",
sizetype);
}
// Return the name to use for a type descriptor decl for TYPE. This
// is used when TYPE does not have a name.
std::string
Gogo::unnamed_type_descriptor_decl_name(const Type* type)
{
return "__go_td_" + type->mangled_name(this);
}
// Return the name to use for a type descriptor decl for a type named
// NAME, defined in the function IN_FUNCTION. IN_FUNCTION will
// normally be NULL.
std::string
Gogo::type_descriptor_decl_name(const Named_object* no,
const Named_object* in_function)
{
std::string ret = "__go_tdn_";
if (no->type_value()->is_builtin())
gcc_assert(in_function == NULL);
else
{
const std::string& unique_prefix(no->package() == NULL
? this->unique_prefix()
: no->package()->unique_prefix());
const std::string& package_name(no->package() == NULL
? this->package_name()
: no->package()->name());
ret.append(unique_prefix);
ret.append(1, '.');
ret.append(package_name);
ret.append(1, '.');
if (in_function != NULL)
{
ret.append(Gogo::unpack_hidden_name(in_function->name()));
ret.append(1, '.');
}
}
ret.append(no->name());
return ret;
}
// Where a type descriptor decl should be defined.
Gogo::Type_descriptor_location
Gogo::type_descriptor_location(const Type* type)
{
const Named_type* name = type->named_type();
if (name != NULL)
{
if (name->named_object()->package() != NULL)
{
// This is a named type defined in a different package. The
// descriptor should be defined in that package.
return TYPE_DESCRIPTOR_UNDEFINED;
}
else if (name->is_builtin())
{
// We create the descriptor for a builtin type whenever we
// need it.
return TYPE_DESCRIPTOR_COMMON;
}
else
{
// This is a named type defined in this package. The
// descriptor should be defined here.
return TYPE_DESCRIPTOR_DEFINED;
}
}
else
{
if (type->points_to() != NULL
&& type->points_to()->named_type() != NULL
&& type->points_to()->named_type()->named_object()->package() != NULL)
{
// This is an unnamed pointer to a named type defined in a
// different package. The descriptor should be defined in
// that package.
return TYPE_DESCRIPTOR_UNDEFINED;
}
else
{
// This is an unnamed type. The descriptor could be defined
// in any package where it is needed, and the linker will
// pick one descriptor to keep.
return TYPE_DESCRIPTOR_COMMON;
}
}
}
// Build a type descriptor decl for TYPE. INITIALIZER is a struct
// composite literal which initializers the type descriptor.
void
Gogo::build_type_descriptor_decl(const Type* type, Expression* initializer,
tree* pdecl)
{
const Named_type* name = type->named_type();
// We can have multiple instances of unnamed types, but we only want
// to emit the type descriptor once. We use a hash table to handle
// this. This is not necessary for named types, as they are unique,
// and we store the type descriptor decl in the type itself.
tree* phash = NULL;
if (name == NULL)
{
if (this->type_descriptor_decls_ == NULL)
this->type_descriptor_decls_ = new Type_descriptor_decls(10);
std::pair<Type_descriptor_decls::iterator, bool> ins =
this->type_descriptor_decls_->insert(std::make_pair(type, NULL_TREE));
if (!ins.second)
{
// We've already built a type descriptor for this type.
*pdecl = ins.first->second;
return;
}
phash = &ins.first->second;
}
std::string decl_name;
if (name == NULL)
decl_name = this->unnamed_type_descriptor_decl_name(type);
else
decl_name = this->type_descriptor_decl_name(name->named_object(),
name->in_function());
tree id = get_identifier_from_string(decl_name);
tree descriptor_type_tree = initializer->type()->get_tree(this);
if (descriptor_type_tree == error_mark_node)
{
*pdecl = error_mark_node;
return;
}
tree decl = build_decl(name == NULL ? BUILTINS_LOCATION : name->location(),
VAR_DECL, id,
build_qualified_type(descriptor_type_tree,
TYPE_QUAL_CONST));
TREE_READONLY(decl) = 1;
TREE_CONSTANT(decl) = 1;
DECL_ARTIFICIAL(decl) = 1;
go_preserve_from_gc(decl);
if (phash != NULL)
*phash = decl;
// We store the new DECL now because we may need to refer to it when
// expanding INITIALIZER.
*pdecl = decl;
// If appropriate, just refer to the exported type identifier.
Gogo::Type_descriptor_location type_descriptor_location =
this->type_descriptor_location(type);
if (type_descriptor_location == TYPE_DESCRIPTOR_UNDEFINED)
{
TREE_PUBLIC(decl) = 1;
DECL_EXTERNAL(decl) = 1;
return;
}
TREE_STATIC(decl) = 1;
TREE_USED(decl) = 1;
Translate_context context(this, NULL, NULL, NULL);
context.set_is_const();
tree constructor = initializer->get_tree(&context);
if (constructor == error_mark_node)
gcc_assert(saw_errors());
DECL_INITIAL(decl) = constructor;
if (type_descriptor_location == TYPE_DESCRIPTOR_DEFINED)
TREE_PUBLIC(decl) = 1;
else
{
gcc_assert(type_descriptor_location == TYPE_DESCRIPTOR_COMMON);
make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
resolve_unique_section(decl, 1, 0);
}
rest_of_decl_compilation(decl, 1, 0);
}
// Build an interface method table for a type: a list of function
// pointers, one for each interface method. This is used for
// interfaces.
tree
Gogo::interface_method_table_for_type(const Interface_type* interface,
Named_type* type,
bool is_pointer)
{
const Typed_identifier_list* interface_methods = interface->methods();
gcc_assert(!interface_methods->empty());
std::string mangled_name = ((is_pointer ? "__go_pimt__" : "__go_imt_")
+ interface->mangled_name(this)
+ "__"
+ type->mangled_name(this));
tree id = get_identifier_from_string(mangled_name);
// See whether this interface has any hidden methods.
bool has_hidden_methods = false;
for (Typed_identifier_list::const_iterator p = interface_methods->begin();
p != interface_methods->end();
++p)
{
if (Gogo::is_hidden_name(p->name()))
{
has_hidden_methods = true;
break;
}
}
// We already know that the named type is convertible to the
// interface. If the interface has hidden methods, and the named
// type is defined in a different package, then the interface
// conversion table will be defined by that other package.
if (has_hidden_methods && type->named_object()->package() != NULL)
{
tree array_type = build_array_type(const_ptr_type_node, NULL);
tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL, id, array_type);
TREE_READONLY(decl) = 1;
TREE_CONSTANT(decl) = 1;
TREE_PUBLIC(decl) = 1;
DECL_EXTERNAL(decl) = 1;
go_preserve_from_gc(decl);
return decl;
}
size_t count = interface_methods->size();
VEC(constructor_elt, gc)* pointers = VEC_alloc(constructor_elt, gc,
count + 1);
// The first element is the type descriptor.
constructor_elt* elt = VEC_quick_push(constructor_elt, pointers, NULL);
elt->index = size_zero_node;
Type* td_type;
if (!is_pointer)
td_type = type;
else
td_type = Type::make_pointer_type(type);
elt->value = fold_convert(const_ptr_type_node,
td_type->type_descriptor_pointer(this));
size_t i = 1;
for (Typed_identifier_list::const_iterator p = interface_methods->begin();
p != interface_methods->end();
++p, ++i)
{
bool is_ambiguous;
Method* m = type->method_function(p->name(), &is_ambiguous);
gcc_assert(m != NULL);
Named_object* no = m->named_object();
tree fnid = no->get_id(this);
tree fndecl;
if (no->is_function())
fndecl = no->func_value()->get_or_make_decl(this, no, fnid);
else if (no->is_function_declaration())
fndecl = no->func_declaration_value()->get_or_make_decl(this, no,
fnid);
else
gcc_unreachable();
fndecl = build_fold_addr_expr(fndecl);
elt = VEC_quick_push(constructor_elt, pointers, NULL);
elt->index = size_int(i);
elt->value = fold_convert(const_ptr_type_node, fndecl);
}
gcc_assert(i == count + 1);
tree array_type = build_array_type(const_ptr_type_node,
build_index_type(size_int(count)));
tree constructor = build_constructor(array_type, pointers);
tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL, id, array_type);
TREE_STATIC(decl) = 1;
TREE_USED(decl) = 1;
TREE_READONLY(decl) = 1;
TREE_CONSTANT(decl) = 1;
DECL_INITIAL(decl) = constructor;
// If the interface type has hidden methods, then this is the only
// definition of the table. Otherwise it is a comdat table which
// may be defined in multiple packages.
if (has_hidden_methods)
TREE_PUBLIC(decl) = 1;
else
{
make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
resolve_unique_section(decl, 1, 0);
}
rest_of_decl_compilation(decl, 1, 0);
go_preserve_from_gc(decl);
return decl;
}
// Mark a function as a builtin library function.
void
Gogo::mark_fndecl_as_builtin_library(tree fndecl)
{
DECL_EXTERNAL(fndecl) = 1;
TREE_PUBLIC(fndecl) = 1;
DECL_ARTIFICIAL(fndecl) = 1;
TREE_NOTHROW(fndecl) = 1;
DECL_VISIBILITY(fndecl) = VISIBILITY_DEFAULT;
DECL_VISIBILITY_SPECIFIED(fndecl) = 1;
}
// Build a call to a builtin function.
tree
Gogo::call_builtin(tree* pdecl, source_location location, const char* name,
int nargs, tree rettype, ...)
{
if (rettype == error_mark_node)
return error_mark_node;
tree* types = new tree[nargs];
tree* args = new tree[nargs];
va_list ap;
va_start(ap, rettype);
for (int i = 0; i < nargs; ++i)
{
types[i] = va_arg(ap, tree);
args[i] = va_arg(ap, tree);
if (types[i] == error_mark_node || args[i] == error_mark_node)
{
delete[] types;
delete[] args;
return error_mark_node;
}
}
va_end(ap);
if (*pdecl == NULL_TREE)
{
tree fnid = get_identifier(name);
tree argtypes = NULL_TREE;
tree* pp = &argtypes;
for (int i = 0; i < nargs; ++i)
{
*pp = tree_cons(NULL_TREE, types[i], NULL_TREE);
pp = &TREE_CHAIN(*pp);
}
*pp = void_list_node;
tree fntype = build_function_type(rettype, argtypes);
*pdecl = build_decl(BUILTINS_LOCATION, FUNCTION_DECL, fnid, fntype);
Gogo::mark_fndecl_as_builtin_library(*pdecl);
go_preserve_from_gc(*pdecl);
}
tree fnptr = build_fold_addr_expr(*pdecl);
if (CAN_HAVE_LOCATION_P(fnptr))
SET_EXPR_LOCATION(fnptr, location);
tree ret = build_call_array(rettype, fnptr, nargs, args);
SET_EXPR_LOCATION(ret, location);
delete[] types;
delete[] args;
return ret;
}
// Build a call to the runtime error function.
tree
Gogo::runtime_error(int code, source_location location)
{
static tree runtime_error_fndecl;
tree ret = Gogo::call_builtin(&runtime_error_fndecl,
location,
"__go_runtime_error",
1,
void_type_node,
integer_type_node,
build_int_cst(integer_type_node, code));
if (ret == error_mark_node)
return error_mark_node;
// The runtime error function panics and does not return.
TREE_NOTHROW(runtime_error_fndecl) = 0;
TREE_THIS_VOLATILE(runtime_error_fndecl) = 1;
return ret;
}
// Send VAL on CHANNEL. If BLOCKING is true, the resulting tree has a
// void type. If BLOCKING is false, the resulting tree has a boolean
// type, and it will evaluate as true if the value was sent. If
// FOR_SELECT is true, this is being done because it was chosen in a
// select statement.
tree
Gogo::send_on_channel(tree channel, tree val, bool blocking, bool for_select,
source_location location)
{
if (channel == error_mark_node || val == error_mark_node)
return error_mark_node;
if (int_size_in_bytes(TREE_TYPE(val)) <= 8
&& !AGGREGATE_TYPE_P(TREE_TYPE(val))
&& !FLOAT_TYPE_P(TREE_TYPE(val)))
{
val = convert_to_integer(uint64_type_node, val);
if (blocking)
{
static tree send_small_fndecl;
tree ret = Gogo::call_builtin(&send_small_fndecl,
location,
"__go_send_small",
3,
void_type_node,
ptr_type_node,
channel,
uint64_type_node,
val,
boolean_type_node,
(for_select
? boolean_true_node
: boolean_false_node));
if (ret == error_mark_node)
return error_mark_node;
// This can panic if there are too many operations on a
// closed channel.
TREE_NOTHROW(send_small_fndecl) = 0;
return ret;
}
else
{
gcc_assert(!for_select);
static tree send_nonblocking_small_fndecl;
tree ret = Gogo::call_builtin(&send_nonblocking_small_fndecl,
location,
"__go_send_nonblocking_small",
2,
boolean_type_node,
ptr_type_node,
channel,
uint64_type_node,
val);
if (ret == error_mark_node)
return error_mark_node;
// This can panic if there are too many operations on a
// closed channel.
TREE_NOTHROW(send_nonblocking_small_fndecl) = 0;
return ret;
}
}
else
{
tree make_tmp;
if (TREE_ADDRESSABLE(TREE_TYPE(val)) || TREE_CODE(val) == VAR_DECL)
{
make_tmp = NULL_TREE;
val = build_fold_addr_expr(val);
if (DECL_P(val))
TREE_ADDRESSABLE(val) = 1;
}
else
{
tree tmp = create_tmp_var(TREE_TYPE(val), get_name(val));
DECL_IGNORED_P(tmp) = 0;
DECL_INITIAL(tmp) = val;
TREE_ADDRESSABLE(tmp) = 1;
make_tmp = build1(DECL_EXPR, void_type_node, tmp);
SET_EXPR_LOCATION(make_tmp, location);
val = build_fold_addr_expr(tmp);
}
val = fold_convert(ptr_type_node, val);
tree call;
if (blocking)
{
static tree send_big_fndecl;
call = Gogo::call_builtin(&send_big_fndecl,
location,
"__go_send_big",
3,
void_type_node,
ptr_type_node,
channel,
ptr_type_node,
val,
boolean_type_node,
(for_select
? boolean_true_node
: boolean_false_node));
if (call == error_mark_node)
return error_mark_node;
// This can panic if there are too many operations on a
// closed channel.
TREE_NOTHROW(send_big_fndecl) = 0;
}
else
{
gcc_assert(!for_select);
static tree send_nonblocking_big_fndecl;
call = Gogo::call_builtin(&send_nonblocking_big_fndecl,
location,
"__go_send_nonblocking_big",
2,
boolean_type_node,
ptr_type_node,
channel,
ptr_type_node,
val);
if (call == error_mark_node)
return error_mark_node;
// This can panic if there are too many operations on a
// closed channel.
TREE_NOTHROW(send_nonblocking_big_fndecl) = 0;
}
if (make_tmp == NULL_TREE)
return call;
else
{
tree ret = build2(COMPOUND_EXPR, TREE_TYPE(call), make_tmp, call);
SET_EXPR_LOCATION(ret, location);
return ret;
}
}
}
// Return a tree for receiving a value of type TYPE_TREE on CHANNEL.
// This does a blocking receive and returns the value read from the
// channel. If FOR_SELECT is true, this is being done because it was
// chosen in a select statement.
tree
Gogo::receive_from_channel(tree type_tree, tree channel, bool for_select,
source_location location)
{
if (type_tree == error_mark_node || channel == error_mark_node)
return error_mark_node;
if (int_size_in_bytes(type_tree) <= 8
&& !AGGREGATE_TYPE_P(type_tree)
&& !FLOAT_TYPE_P(type_tree))
{
static tree receive_small_fndecl;
tree call = Gogo::call_builtin(&receive_small_fndecl,
location,
"__go_receive_small",
2,
uint64_type_node,
ptr_type_node,
channel,
boolean_type_node,
(for_select
? boolean_true_node
: boolean_false_node));
if (call == error_mark_node)
return error_mark_node;
// This can panic if there are too many operations on a closed
// channel.
TREE_NOTHROW(receive_small_fndecl) = 0;
int bitsize = GET_MODE_BITSIZE(TYPE_MODE(type_tree));
tree int_type_tree = go_type_for_size(bitsize, 1);
return fold_convert_loc(location, type_tree,
fold_convert_loc(location, int_type_tree,
call));
}
else
{
tree tmp = create_tmp_var(type_tree, get_name(type_tree));
DECL_IGNORED_P(tmp) = 0;
TREE_ADDRESSABLE(tmp) = 1;
tree make_tmp = build1(DECL_EXPR, void_type_node, tmp);
SET_EXPR_LOCATION(make_tmp, location);
tree tmpaddr = build_fold_addr_expr(tmp);
tmpaddr = fold_convert(ptr_type_node, tmpaddr);
static tree receive_big_fndecl;
tree call = Gogo::call_builtin(&receive_big_fndecl,
location,
"__go_receive_big",
3,
void_type_node,
ptr_type_node,
channel,
ptr_type_node,
tmpaddr,
boolean_type_node,
(for_select
? boolean_true_node
: boolean_false_node));
if (call == error_mark_node)
return error_mark_node;
// This can panic if there are too many operations on a closed
// channel.
TREE_NOTHROW(receive_big_fndecl) = 0;
return build2(COMPOUND_EXPR, type_tree, make_tmp,
build2(COMPOUND_EXPR, type_tree, call, tmp));
}
}
// Return the type of a function trampoline. This is like
// get_trampoline_type in tree-nested.c.
tree
Gogo::trampoline_type_tree()
{
static tree type_tree;
if (type_tree == NULL_TREE)
{
unsigned int size;
unsigned int align;
go_trampoline_info(&size, &align);
tree t = build_index_type(build_int_cst(integer_type_node, size - 1));
t = build_array_type(char_type_node, t);
type_tree = Gogo::builtin_struct(NULL, "__go_trampoline", NULL_TREE, 1,
"__data", t);
t = TYPE_FIELDS(type_tree);
DECL_ALIGN(t) = align;
DECL_USER_ALIGN(t) = 1;
go_preserve_from_gc(type_tree);
}
return type_tree;
}
// Make a trampoline which calls FNADDR passing CLOSURE.
tree
Gogo::make_trampoline(tree fnaddr, tree closure, source_location location)
{
tree trampoline_type = Gogo::trampoline_type_tree();
tree trampoline_size = TYPE_SIZE_UNIT(trampoline_type);
closure = save_expr(closure);
// We allocate the trampoline using a special function which will
// mark it as executable.
static tree trampoline_fndecl;
tree x = Gogo::call_builtin(&trampoline_fndecl,
location,
"__go_allocate_trampoline",
2,
ptr_type_node,
size_type_node,
trampoline_size,
ptr_type_node,
fold_convert_loc(location, ptr_type_node,
closure));
if (x == error_mark_node)
return error_mark_node;
x = save_expr(x);
// Initialize the trampoline.
tree ini = build_call_expr(implicit_built_in_decls[BUILT_IN_INIT_TRAMPOLINE],
3, x, fnaddr, closure);
// On some targets the trampoline address needs to be adjusted. For
// example, when compiling in Thumb mode on the ARM, the address
// needs to have the low bit set.
x = build_call_expr(implicit_built_in_decls[BUILT_IN_ADJUST_TRAMPOLINE],
1, x);
x = fold_convert(TREE_TYPE(fnaddr), x);
return build2(COMPOUND_EXPR, TREE_TYPE(x), ini, x);
}
|