aboutsummaryrefslogtreecommitdiff
path: root/gcc/config/cil32/gimple-to-cil.c
blob: d6c707b9776dbad5e5d00c63c4bdb1f7614efde3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
/* GIMPLE to CIL conversion pass.

   Copyright (C) 2006-2009 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING.  If not, write to the Free
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.

Authors:
   Andrea Ornstein
   Erven Rohou
   Gabriele Svelto

Contact information at STMicroelectronics:
Andrea C. Ornstein      <andrea.ornstein@st.com>
Contact information at INRIA:
Erven Rohou             <erven.rohou@inria.fr>
*/

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "flags.h"
#include "timevar.h"
#include "errors.h"
#include "ggc.h"
#include "tree.h"
#include "tree-flow.h"
#include "tree-pass.h"
#include "pointer-set.h"
#include "c-common.h"

#include "cil-builtins.h"
#include "cil-refs.h"
#include "cil-stmt.h"
#include "cil-types.h"
#include "cil-stack.h"
#include "emit-cil.h"

/******************************************************************************
 * Globals                                                                    *
 ******************************************************************************/

/* Return variable for pre-C99 functions which contain VOID return statements
   even though they are declared to return a non-VOID value.  */
static tree res_var;

/******************************************************************************
 * Local functions prototypes                                                 *
 ******************************************************************************/

static void gen_integer_cst (cil_stmt_iterator *, tree);
static void gen_real_cst (cil_stmt_iterator *, tree);
static void gen_vector_cst (cil_stmt_iterator *, tree);
static void gen_addr_expr (cil_stmt_iterator *, tree);
static void gen_array_ref_addr_expr (cil_stmt_iterator *, tree);
static void gen_initblk (cil_stmt_iterator *, tree, tree, tree);
static void gen_cpblk (cil_stmt_iterator *, tree, tree, tree);
static void gen_scalar_ld_st_ind (cil_stmt_iterator *, tree, bool, bool);
static inline void gen_scalar_stind (cil_stmt_iterator *, tree, bool);
static inline void gen_scalar_ldind (cil_stmt_iterator *, tree, bool);
static void gen_ldind (cil_stmt_iterator *, tree, bool);
static void gen_stind (cil_stmt_iterator *, tree, bool);
static bool mostly_zeros_p (tree);
static bool all_zeros_p (tree);
static void write_cst_image (char *, char *, tree, unsigned HOST_WIDE_INT);
static void expand_init_to_cil_seq1 (tree, tree, cil_seq, bool, cil_seq,
				     char *, char *);
static size_t cil_seq_num_instr (cil_seq);
static void gen_bit_field_modify_expr (cil_stmt_iterator *, tree, tree);
static void gen_target_mem_ref_modify_expr (cil_stmt_iterator *, tree, tree);
static void gen_bitfield_ref_modify_expr (cil_stmt_iterator *, tree, tree);
static void gen_vector_bitfield_ref_modify_expr (cil_stmt_iterator *,
						 tree, tree);
static void gen_modify_expr (cil_stmt_iterator *, tree, tree);
static void gen_goto_expr (cil_stmt_iterator *, tree);
static void gen_cond_expr (cil_stmt_iterator *, tree);
static void gen_switch_expr (cil_stmt_iterator *, tree);
static void gen_builtin_va_start (cil_stmt_iterator *, tree);
static void gen_builtin_va_end (cil_stmt_iterator *, tree);
static void gen_builtin_va_copy (cil_stmt_iterator *, tree);
static bool gen_call_builtin (cil_stmt_iterator *, tree, tree);
static void gen_call_expr (cil_stmt_iterator *, tree);
static tree gen_expr_copy (cil_stmt_iterator *, tree);
static void gen_bit_and_expr (cil_stmt_iterator *, tree);
static void gen_compare_expr (cil_stmt_iterator *, tree);
static void gen_minmax_expr (cil_stmt_iterator *, tree);
static void gen_abs_expr (cil_stmt_iterator *, tree);
static void gen_var_decl (cil_stmt_iterator *, tree);
static void gen_bit_field_comp_ref (cil_stmt_iterator *, tree);
static void gen_comp_ref (cil_stmt_iterator *, tree);
static void gen_vector_bitfield_ref (cil_stmt_iterator *, tree);
static void gen_bit_field_ref (cil_stmt_iterator *, tree);
static void gen_truth_expr (cil_stmt_iterator *, tree);
static void gen_target_mem_ref (cil_stmt_iterator *, tree);
static void gen_vector_ctor (cil_stmt_iterator *, tree);
static void gen_vector_view_convert_expr (cil_stmt_iterator *, tree, tree);
static void gen_complex_part_expr (cil_stmt_iterator *, tree);
static void gen_complex (cil_stmt_iterator *, tree, tree, tree);
static enum cil_opcode conv_opcode_from_type (tree);
static void gen_integral_conv (cil_stmt_iterator *, tree, tree);
static void gen_conv (cil_stmt_iterator *, bool, tree, tree);
static void gen_rotate (cil_stmt_iterator *, tree);
static void gimple_to_cil_node (cil_stmt_iterator *, tree);
static void process_labels (void);
static void process_initializers (void);

/******************************************************************************
 * GIMPLE/generic to CIL conversion functions                                 *
 ******************************************************************************/

/* Load the value of the integer constant CST on the stack.  The constant will
   be 32-bits or 64-bits wide depending on the type of CST.  The generated
   statements will be appended to the current function's CIL code using the CSI
   iterator.  */

static void
gen_integer_cst (cil_stmt_iterator *csi, tree cst)
{
  unsigned HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (TREE_TYPE (cst)), 1);
  enum cil_opcode opcode;
  cil_stmt stmt;

  opcode = (size <= 32) ? CIL_LDC_I4 : CIL_LDC_I8;
  stmt = cil_build_stmt_arg (opcode, cst);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
}

/* Load the value of the real constant CST on the stack.  The constant will
   be 32-bits or 64-bits wide depending on the type of CST.  The generated
   statements will be appended to the current function CIL code using the CSI
   iterator.  */

static void
gen_real_cst (cil_stmt_iterator *csi, tree cst)
{
  unsigned HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (TREE_TYPE (cst)), 1);
  enum cil_opcode opcode;
  cil_stmt stmt;

  opcode = (size == 32) ? CIL_LDC_R4 : CIL_LDC_R8;
  stmt = cil_build_stmt_arg (opcode, cst);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
}

/* Load the value of a vector constant CST on the stack.  The generated
   statements will be appended to the current function CIL code using the CSI
   iterator.  */

static void
gen_vector_cst (cil_stmt_iterator *csi, tree cst)
{
  unsigned HOST_WIDE_INT num_elt = 0;
  tree vector_type = TREE_TYPE (cst);
  tree elt_type = TREE_TYPE (vector_type);
  tree zero, elt;
  cil_type_t cil_type = scalar_to_cil (elt_type);
  cil_stmt stmt;

  if (cil_float_p (cil_type))
    zero = build_real_from_int_cst (elt_type, integer_zero_node);
  else if (cil_int_or_smaller_p (cil_type))
    zero = integer_zero_node;
  else /* 64-bit integers */
    zero = build_int_cst (intDI_type_node, 0);

  for (elt = TREE_VECTOR_CST_ELTS (cst); elt; elt = TREE_CHAIN (elt))
    {
      tree elt_val = TREE_VALUE (elt);
      gimple_to_cil_node (csi, elt_val);
      ++num_elt;
    }

  /* Fill in the missing initializers, if any */
  for ( ; num_elt < TYPE_VECTOR_SUBPARTS (vector_type); ++num_elt)
    gimple_to_cil_node (csi, zero);

  stmt = cil_build_stmt_arg (CIL_VEC_CTOR, vector_type);
  csi_insert_after (csi,  stmt, CSI_CONTINUE_LINKING);

  if (cfun)
    cfun->machine->has_vec = true;
}

/* Generates a sequence which computes the address of the object described by
   OBJ and pushes it on top of the stack. The generated statements are
   appended to the current function's CIL code using the CSI iterator.  */

static void
gen_addr_expr (cil_stmt_iterator *csi, tree node)
{
  cil_stmt stmt;

  switch (TREE_CODE (node))
    {
    case STRING_CST:
      node = mark_referenced_string (node);
      stmt = cil_build_stmt_arg (CIL_LDSFLDA, node);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      break;

    case VAR_DECL:
    case RESULT_DECL:
      /* Function local static variables are promoted to global variables.  */
      if (!DECL_FILE_SCOPE_P (node) && !TREE_STATIC (node))
	stmt = cil_build_stmt_arg (CIL_LDLOCA, node);
      else
	stmt = cil_build_stmt_arg (CIL_LDSFLDA, node);

      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

      if (TREE_CODE (TREE_TYPE (node)) == ARRAY_TYPE)
	{
	  stmt = cil_build_stmt (CIL_CONV_I);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}

      break;

    case PARM_DECL:
      stmt = cil_build_stmt_arg (CIL_LDARGA, node);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

      if (TREE_CODE (TREE_TYPE (node)) == ARRAY_TYPE)
	{
	  stmt = cil_build_stmt (CIL_CONV_I);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}
      else if (TREE_CODE (TREE_TYPE (node)) == VECTOR_TYPE)
	cfun->machine->has_vec = true;

      break;

    case FUNCTION_DECL:
      stmt = cil_build_stmt_arg (CIL_LDFTN, node);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      break;

    case LABEL_DECL:
      {
	/* We cannot emit the address of the label in CIL, so we map each
	  label to an ID and emit the ID.  The GOTO will then be implemented
	  with a switch based on that ID.  The ID is simply the position in
	  the list of all address taken labels.  */

	tree id = get_addr_taken_label_id (node);
	gen_integer_cst (csi, id);
      }
      break;

    case INDIRECT_REF:
    case MISALIGNED_INDIRECT_REF:
      gimple_to_cil_node (csi, GENERIC_TREE_OPERAND (node, 0));
      break;

    case ARRAY_REF:
      gen_array_ref_addr_expr (csi, node);
      break;

    case COMPONENT_REF:
      {
	tree obj = GENERIC_TREE_OPERAND (node, 0);
	tree fld = GENERIC_TREE_OPERAND (node, 1);
	tree obj_type = TYPE_MAIN_VARIANT (TREE_TYPE (obj));

	gcc_assert (!DECL_BIT_FIELD (fld));

	gen_addr_expr (csi, obj);
	stmt = cil_build_stmt_arg (CIL_LDFLDA, fld);
	mark_referenced_type (obj_type);
	/* Some statements might have been added */
	csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      }
      break;

    case VIEW_CONVERT_EXPR:
      gen_addr_expr (csi, GENERIC_TREE_OPERAND (node, 0));
      csi_insert_after (csi, cil_build_stmt (CIL_CONV_I), CSI_CONTINUE_LINKING);
      break;

    case REALPART_EXPR:
    case IMAGPART_EXPR:
      {
	tree obj = GENERIC_TREE_OPERAND (node, 0);
	tree type = TREE_TYPE (obj);

	gen_addr_expr (csi, obj);

	if (TREE_CODE (node) == REALPART_EXPR)
	  {
	    stmt = cil_build_stmt_arg (CIL_LDFLDA,
				       cil_get_builtin_complex_real_fld (type));
	  }
	else
	  {
	    stmt = cil_build_stmt_arg (CIL_LDFLDA,
				       cil_get_builtin_complex_imag_fld (type));
	  }

	csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      }
      break;

    case COMPOUND_LITERAL_EXPR:
      /* HACK: We should find a way to avoid front-end nodes */
      gen_addr_expr (csi, COMPOUND_LITERAL_EXPR_DECL (node));
      break;

    default:
      {
	/* A generic expression, we first evaluate it, then store it inside a
	   temporary and finally get the address of said temporary.  */
	tree tmp_var;

	tmp_var = create_tmp_var (TREE_TYPE (node), "addr_expr");
	gimple_to_cil_node (csi, node);
	stmt = cil_build_stmt_arg (CIL_STLOC, tmp_var);
	csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	stmt = cil_build_stmt_arg (CIL_LDLOCA, tmp_var);
	csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	stmt = cil_build_stmt (CIL_CONV_I);
	csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	break;
      }
    }
}

/* Generates the address of an ARRAY_REF expression.  The generated statements
   are appended to the current funcion's CIL code using the CSI iterator.  */

static void
gen_array_ref_addr_expr (cil_stmt_iterator *csi, tree node)
{
  HOST_WIDE_INT bitsize = 0;
  HOST_WIDE_INT bitpos = 0;
  tree offset = NULL_TREE;
  enum machine_mode mode;
  int unsignedp = 0;
  int volatilep = 0;
  cil_stmt stmt;
  tree ref;

  ref = get_inner_reference (node, &bitsize, &bitpos, &offset, &mode,
			     &unsignedp, &volatilep, false);
  gen_addr_expr (csi, ref);

  if (TREE_CODE (TREE_TYPE (ref)) != ARRAY_TYPE)
    csi_insert_after (csi, cil_build_stmt (CIL_CONV_I), CSI_CONTINUE_LINKING);

  if (bitpos != 0)
    {
      gen_integer_cst (csi,
		       build_int_cst (intSI_type_node, bitpos / BITS_PER_UNIT));
      stmt = cil_build_stmt (CIL_ADD);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }

  if (offset != NULL_TREE)
    {
      gimple_to_cil_node (csi, offset);
      stmt = cil_build_stmt (CIL_ADD);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }
}

/* Generates a CIL INITBLK instruction with the destination address specified
   by PTR, the value specified by VALUE and the size specified by SIZE.  */

static void
gen_initblk (cil_stmt_iterator *csi, tree ptr, tree value, tree size)
{
  cil_stmt stmt;

  gimple_to_cil_node (csi, ptr);
  gimple_to_cil_node (csi, value);
  gimple_to_cil_node (csi, size);
  stmt = cil_build_stmt (CIL_INITBLK);
  cil_set_prefix_unaligned (stmt, 1);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
}

/* Generates a CIL CPBLK instruction with the destination address specified
   by DST, the source address specified by SRC and the size specified by SIZE.
 */

static void
gen_cpblk (cil_stmt_iterator *csi, tree dst, tree src, tree size)
{
  cil_stmt stmt;

  gimple_to_cil_node (csi, dst);
  gimple_to_cil_node (csi, src);
  gimple_to_cil_node (csi, size);
  stmt = cil_build_stmt (CIL_CPBLK);
  cil_set_prefix_unaligned (stmt, 1);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
}

/* Generates a load/store indirect statement for the scalar type specified by
   TYPE. If STORE is true then a store is generated, otherwise a load.
   The statement is made volatile if VOLAT is true. The generated statements
   are appended to the current function's CIL code using the CSI iterator.  */

static void
gen_scalar_ld_st_ind (cil_stmt_iterator *csi, tree type, bool store, bool volat)
{
  unsigned HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 1);
  enum cil_opcode opcode;
  cil_stmt stmt;

  if (INTEGRAL_TYPE_P (type))
    {
      if (TYPE_UNSIGNED (type))
	{
	  switch (size)
	    {
	    case 8:  opcode = store ? CIL_STIND_I1 : CIL_LDIND_U1; break;
	    case 16: opcode = store ? CIL_STIND_I2 : CIL_LDIND_U2; break;
	    case 32: opcode = store ? CIL_STIND_I4 : CIL_LDIND_U4; break;
	    case 64: opcode = store ? CIL_STIND_I8 : CIL_LDIND_U8; break;
	    default:
	      internal_error ("Unsupported integer size "
			      HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
	    }
	}
      else
	{
	  switch (size)
	    {
	    case 8:  opcode = store ? CIL_STIND_I1 : CIL_LDIND_I1; break;
	    case 16: opcode = store ? CIL_STIND_I2 : CIL_LDIND_I2; break;
	    case 32: opcode = store ? CIL_STIND_I4 : CIL_LDIND_I4; break;
	    case 64: opcode = store ? CIL_STIND_I8 : CIL_LDIND_I8; break;
	    default:
	      internal_error ("Unsupported integer size "
			      HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
	    }
	}
    }
  else if (POINTER_TYPE_P (type))
    opcode = store ? CIL_STIND_I : CIL_LDIND_I;
  else if (SCALAR_FLOAT_TYPE_P (type))
    {
      switch (size)
	{
	case 32: opcode = store ? CIL_STIND_R4 : CIL_LDIND_R4; break;
	case 64: opcode = store ? CIL_STIND_R8 : CIL_LDIND_R8; break;
	default:
	  internal_error ("Unsupported floating point size "
			  HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
	}
    }
  else
    gcc_unreachable ();

  stmt = cil_build_stmt (opcode);
  cil_set_prefix_volatile (stmt, volat);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
}

/* Generates a load indirect statement for the scalar type specified by TYPE.
   The statement is made volatile if VOLAT is true. The generated statements
   are appended to the current function's CIL code using the CSI iterator.  */

static inline void
gen_scalar_ldind (cil_stmt_iterator *csi, tree type, bool volat)
{
  gen_scalar_ld_st_ind (csi, type, false, volat);
}

/* Generates a store indirect statement for the scalar type specified by TYPE.
   The statement is made volatile if VOLAT is true. The generated statements
   are appended to the current function's CIL code using the CSI iterator.  */

static inline void
gen_scalar_stind (cil_stmt_iterator *csi, tree type, bool volat)
{
  gen_scalar_ld_st_ind (csi, type, true, volat);
}

/* Generate a load indirect statement for the type specified by TYPE. The
   load is made volatile if VOLAT is true. The generated statements are
   appended to the current function's CIL code using the CSI iterator.  */

static void
gen_ldind (cil_stmt_iterator *csi, tree type, bool volat)
{
  cil_stmt stmt;

  if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
    {
      stmt = cil_build_stmt_arg (CIL_LDOBJ, type);
      cil_set_prefix_volatile (stmt, volat);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }
  else if (TREE_CODE (type) == VECTOR_TYPE)
    {
      stmt = cil_build_stmt_arg (CIL_LDVEC, type);
      cil_set_prefix_volatile (stmt, volat);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      cfun->machine->has_vec = true;
    }
  else
    gen_scalar_ldind (csi, type, true);
}

/* Generate a misaligned indirect load statement for the type specified by
   TYPE. The load is made volatile if VOLAT is true. The generated statements
   are appended to the current function's CIL code using the CSI iterator.  */

static void
gen_misaligned_ldvec (cil_stmt_iterator *csi, tree type, bool volat)
{
  cil_stmt stmt;

  gcc_assert (TREE_CODE (type) == VECTOR_TYPE);

  if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
    {
      gcc_assert (0);  /* FIXME: is this reachable? */
      stmt = cil_build_stmt_arg (CIL_LDOBJ, type);
      cil_set_prefix_volatile (stmt, volat);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }
  else if (TREE_CODE (type) == VECTOR_TYPE)
    {
      stmt = cil_build_stmt_arg (CIL_LDVEC, type);
      cil_set_prefix_volatile (stmt, volat);
      cil_set_prefix_unaligned (stmt, 1);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

      if (cfun)
	cfun->machine->has_vec = true;
    }
  else
    gen_scalar_ldind (csi, type, true);
}

/* Generate a store indirect statement for the type specified by TYPE. The
   store is made volatile if VOLAT is true. The generated statements are
   appended to the current function's CIL code using the CSI iterator.  */

static void
gen_stind (cil_stmt_iterator *csi, tree type, bool volat)
{
  cil_stmt stmt;

  if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
    {
      stmt = cil_build_stmt_arg (CIL_STOBJ, type);
      cil_set_prefix_volatile (stmt, volat);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }
  else if (TREE_CODE (type) == VECTOR_TYPE)
    {
      stmt = cil_build_stmt_arg (CIL_STVEC, type);
      cil_set_prefix_volatile (stmt, volat);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

      if (cfun)
	cfun->machine->has_vec = true;
    }
  else
    gen_scalar_stind (csi, type, true);
}

/* Generate a misaligned indirect store statement for the type specified by
   TYPE. The store is made volatile if VOLAT is true. The generated statements
   are appended to the current function's CIL code using the CSI iterator.  */

static void
gen_misaligned_stvec (cil_stmt_iterator *csi, tree type, bool volat)
{
  cil_stmt stmt;

  gcc_assert (TREE_CODE (type) == VECTOR_TYPE);

  if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
    {
      gcc_assert (0);  /* FIXME: is this reachable? */
      stmt = cil_build_stmt_arg (CIL_STOBJ, type);
      cil_set_prefix_volatile (stmt, volat);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }
  else if (TREE_CODE (type) == VECTOR_TYPE)
    {
      stmt = cil_build_stmt_arg (CIL_STVEC, type);
      cil_set_prefix_volatile (stmt, volat);
      cil_set_prefix_unaligned (stmt, 1);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      cfun->machine->has_vec = true;
    }
  else
    gen_scalar_stind (csi, type, true);
}

/* Return TRUE if EXP contains mostly (3/4)  zeros.  */

static bool
mostly_zeros_p (tree exp)
{
  HOST_WIDE_INT nz_elts, count, elts;
  bool must_clear;

  gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);

  categorize_ctor_elements (exp, &nz_elts, &count, &must_clear);

  if (must_clear)
    return TRUE;

  elts = count_type_elements (TREE_TYPE (exp), false);

  return (nz_elts < elts / 4);
}

/* Return TRUE if EXP contains all zeros. */

static bool
all_zeros_p (tree exp)
{
  HOST_WIDE_INT nz_elts, count;
  bool must_clear;

  gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);

  categorize_ctor_elements (exp, &nz_elts, &count, &must_clear);

  return (nz_elts == 0);
}

/* Write the value of size SIZE bits held by the tree CST into the
   little-endian image LE_IMAGE and big-endian image BE_IMAGE.  */

static void
write_cst_image (char *le_image, char *be_image, tree value,
		 unsigned HOST_WIDE_INT size)
{
  char b0 = TREE_INT_CST_LOW (value);
  char b1 = TREE_INT_CST_LOW (value) >> 8;
  char b2 = TREE_INT_CST_LOW (value) >> 16;
  char b3 = TREE_INT_CST_LOW (value) >> 24;

  switch (size)
    {
    case 8:
      le_image[0] |= b0;
      be_image[0] |= b0;
      break;

    case 16:
      le_image[0] |= b0;
      le_image[1] |= b1;
      be_image[0] |= b1;
      be_image[1] |= b0;
      break;

    case 32:
      le_image[0] |= b0;
      le_image[1] |= b1;
      le_image[2] |= b2;
      le_image[3] |= b3;
      be_image[0] |= b3;
      be_image[1] |= b2;
      be_image[2] |= b1;
      be_image[3] |= b0;
      break;

    default:
      gcc_unreachable ();
    }
}

/* Expand the initializer INIT for the declaration DECL into two CIL sequences
   held by SEQ1 and SEQ2, also write the constant values encountered in
   little-endian format in LE_IMAGE and in big-endian format in BE_IMAGE.  */

static void
expand_init_to_cil_seq1 (tree decl, tree init, cil_seq seq1, bool cleared,
			 cil_seq seq2, char *le_image, char *be_image)
{
  tree decl_size = TYPE_SIZE_UNIT (TREE_TYPE (decl));
  unsigned HOST_WIDE_INT size = tree_low_cst (decl_size, 1);
  bool need_to_clear = FALSE;
  cil_stmt_iterator csi;

  if (TREE_CODE (init) == CONST_DECL)
    {
      init = DECL_INITIAL (init);
      gcc_assert (init && init != error_mark_node);
    }

  if (!cleared && TREE_CODE (init) == CONSTRUCTOR && all_zeros_p (init))
    {
      csi = csi_last (seq1);
      gen_initblk (&csi, build_fold_addr_expr (decl), integer_zero_node,
		   decl_size);
      return;
    }

  switch (TREE_CODE (init))
    {
    case STRING_CST:
      {
	if (TREE_STRING_LENGTH (init) < size)
	  size = TREE_STRING_LENGTH (init);

	csi = csi_last (seq1);
	gen_cpblk (&csi, build_fold_addr_expr (decl),
		   build_fold_addr_expr (init), decl_size);
	memcpy(le_image, TREE_STRING_POINTER (init), size);
	memcpy(be_image, TREE_STRING_POINTER (init), size);
      }
    break;

    case CONSTRUCTOR:
      switch (TREE_CODE (TREE_TYPE (init)))
	{
	case RECORD_TYPE:
	case UNION_TYPE:
	case QUAL_UNION_TYPE:
	  {
	    unsigned HOST_WIDE_INT idx;
	    tree init_type = TREE_TYPE (init);
	    tree field, value;

	    /* If size is zero or the target is already cleared, do nothing */
	    if (size == 0 || cleared)
	      {
		need_to_clear = FALSE;
		cleared = TRUE;
	      }
	    /* We either clear the aggregate or indicate the value is dead.  */
	    else if ((TREE_CODE (init_type) == UNION_TYPE
		      || TREE_CODE (init_type) == QUAL_UNION_TYPE)
		     && !CONSTRUCTOR_ELTS (init))
	      {
		/* If the constructor is empty, clear the union.  */
		need_to_clear = TRUE;
	      }
	    /* If the constructor has fewer fields than the structure or
	       if we are initializing the structure to mostly zeros, clear
	       the whole structure first. */
	    else if (size > 0
		     && (((int) VEC_length (constructor_elt,
					    CONSTRUCTOR_ELTS (init))
			  != fields_length (init_type))
			 || mostly_zeros_p (init)))
	      {
		need_to_clear = TRUE;
	      }

	    if (need_to_clear && size > 0)
	      {
		csi = csi_last (seq1);
		gen_initblk (&csi, build_fold_addr_expr (decl),
			     integer_zero_node, decl_size);
		cleared = TRUE;
	      }

	    /* Store each element of the constructor into the
	       corresponding field of TARGET.  */
	    FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
				      field, value)
	      {
		tree ltarget;

		/* Just ignore missing fields.  We cleared the whole
		   structure, above, if any fields are missing.  */
		if (field == 0)
		  continue;

		if (cleared && initializer_zerop (value))
		  continue;

		ltarget = build3 (COMPONENT_REF, TREE_TYPE (field), decl,
				  field, NULL);

		if (le_image != NULL && !DECL_BIT_FIELD (field))
		  {
		    unsigned HOST_WIDE_INT offset = tree_low_cst (DECL_FIELD_OFFSET (field), 1);
		    unsigned HOST_WIDE_INT bit_offset = tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1);
		    gcc_assert (bit_offset % BITS_PER_UNIT == 0);
		    offset += (bit_offset / BITS_PER_UNIT);

		    expand_init_to_cil_seq1 (ltarget, value, seq1, cleared,
					     seq2, le_image + offset,
					     be_image + offset);
		  }
		else if (le_image != NULL && DECL_BIT_FIELD (field)
			 && (TARGET_LITTLE_ENDIAN || TARGET_BIG_ENDIAN))
		  {
		    unsigned HOST_WIDE_INT offset = 0;
		    HOST_WIDE_INT bit_size = 0;
		    HOST_WIDE_INT bit_pos = 0;
		    HOST_WIDE_INT cont_off;
		    HOST_WIDE_INT cont_size = 8;
		    enum machine_mode mode;
		    int unsignedp = 0;
		    int volatilep = 0;
		    tree cont_type;
		    tree shift_cst;
		    tree tmp;

		    get_inner_reference (ltarget, &bit_size, &bit_pos,
					 &tmp, &mode, &unsignedp,
					 &volatilep, false);

		    /* Calculate the container size.  */
		    while ((bit_pos % cont_size + bit_size) > cont_size)
		      cont_size *= 2;

		    if (cont_size > 32)
		      {
			expand_init_to_cil_seq1 (ltarget, value,
						 seq1, cleared,
						 seq2, NULL, NULL);
		      }
		    else
		      {
			csi = csi_last (seq1);
			gen_modify_expr (&csi, ltarget, value);

			cont_type = get_integer_type (cont_size, true);
			cont_off = bit_pos % cont_size;

			/* Calculate the container offset.  */
			if ((bit_pos - cont_off) / BITS_PER_UNIT != 0)
			  {
			    tmp = build_int_cst (intSI_type_node,
						 (bit_pos - cont_off)
						 / BITS_PER_UNIT);
			    offset = tree_low_cst (tmp, 1);
			  }

			shift_cst = build_int_cst (intSI_type_node, cont_off);
			tmp = fold_binary_to_constant (LSHIFT_EXPR, cont_type,
						       fold_convert (cont_type,
								     value),
						       shift_cst);
			write_cst_image (le_image, be_image, tmp, cont_size);
		      }
		  }
		else
		  {
		    expand_init_to_cil_seq1 (ltarget, value, seq1, cleared,
					     seq2, NULL, NULL);
		  }
	      }
	  }
	  break;

	case ARRAY_TYPE:
	  {
	    tree value, index;
	    unsigned HOST_WIDE_INT i;
	    tree domain;
	    tree elttype = TREE_TYPE (TREE_TYPE (init));
	    int const_bounds_p;
	    HOST_WIDE_INT minelt = 0;
	    HOST_WIDE_INT maxelt = 0;

	    domain = TYPE_DOMAIN (TREE_TYPE (init));
	    const_bounds_p = (TYPE_MIN_VALUE (domain)
			      && TYPE_MAX_VALUE (domain)
			      && host_integerp (TYPE_MIN_VALUE (domain), 0)
			      && host_integerp (TYPE_MAX_VALUE (domain), 0));

	    /* If we have constant bounds for the range
	       of the type, get them.  */
	    if (const_bounds_p)
	      {
		minelt = tree_low_cst (TYPE_MIN_VALUE (domain), 0);
		maxelt = tree_low_cst (TYPE_MAX_VALUE (domain), 0);
	      }

	    /* If the constructor has fewer elements than the array, clear
	       the whole array first. */
	    if (cleared)
	      need_to_clear = FALSE;
	    else
	      {
		unsigned HOST_WIDE_INT idx;
		tree index, value;
		HOST_WIDE_INT count = 0;
		HOST_WIDE_INT zero_count = 0;
		need_to_clear = !const_bounds_p;

		/* This loop is a more accurate version of the loop in
		   mostly_zeros_p (it handles RANGE_EXPR in an index).  It
		   is also needed to check for missing elements.  */
		FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
					  index, value)
		  {
		    HOST_WIDE_INT this_node_count;

		    if (need_to_clear)
		      break;

		    if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
		      {
			tree lo_index = TREE_OPERAND (index, 0);
			tree hi_index = TREE_OPERAND (index, 1);

			if (!host_integerp (lo_index, 1)
			    || !host_integerp (hi_index, 1))
			  {
			    need_to_clear = TRUE;
			    break;
			  }

			this_node_count = tree_low_cst (hi_index, 1)
					  - tree_low_cst (lo_index, 1) + 1;
		      }
		    else
		      this_node_count = 1;

		    count += this_node_count;
		    if (TREE_CODE (value) == CONSTRUCTOR
			&& mostly_zeros_p (value))
		      {
			zero_count += this_node_count;
		      }
		  }

		/* Clear the entire array first if there are any missing
		   elements, or if the incidence of zero elements is >= 75%.  */
		if (!need_to_clear
		    && (count < maxelt - minelt + 1
			|| 4 * zero_count >= 3 * count))
		  {
		    need_to_clear = TRUE;
		  }
	      }

	    if (need_to_clear && size > 0)
	      {
		csi = csi_last (seq1);
		gen_initblk (&csi, build_fold_addr_expr (decl),
			     integer_zero_node, decl_size);
		cleared = TRUE;
	      }

	    /* Store each element of the constructor into the
	       corresponding element of TARGET, determined by counting the
	       elements.  */
	    FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), i, index, value)
	      {
		tree t;
		tree elsize;

		if (initializer_zerop (value))
		  continue;

		gcc_assert (index == NULL_TREE
			    || TREE_CODE (index) != RANGE_EXPR);

		if (minelt)
		  index = fold_convert (ssizetype,
					fold_build2 (MINUS_EXPR,
						     TREE_TYPE (index),
						     index,
						     TYPE_MIN_VALUE (domain)));

		t = build4 (ARRAY_REF, elttype, decl, index, NULL, NULL);

		elsize = array_ref_element_size (t);

		if (le_image != NULL
		    && TREE_CODE (index)  == INTEGER_CST
		    && TREE_CODE (elsize) == INTEGER_CST)
		  {
		    unsigned HOST_WIDE_INT offset;

		    offset = tree_low_cst (index, 1) * tree_low_cst (elsize, 1);
		    expand_init_to_cil_seq1 (t, value, seq1, cleared, seq2,
					     le_image + offset,
					     be_image + offset);
		  }
		else
		  {
		    expand_init_to_cil_seq1 (t, value, seq1, cleared, seq2,
					     NULL, NULL);
		  }
	      }
	  }
	  break;

	case VECTOR_TYPE:
	  csi = csi_last (seq1);
	  gen_modify_expr (&csi, decl, init);
	  csi = csi_last (seq2);
	  gen_modify_expr (&csi, decl, init);
	  break;

	default:
	  gcc_unreachable ();
	  break;
	}
      break;

    case INTEGER_CST:
      {
	unsigned HOST_WIDE_INT type_size = tree_low_cst (decl_size, 1);
	tree t = fold_build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, init);

	csi = csi_last (seq1);
	gimple_to_cil_node (&csi, t);

	if (le_image != NULL && type_size <= 4)
	  write_cst_image (le_image, be_image, init, type_size * BITS_PER_UNIT);
	else
	  {
	    csi = csi_last (seq2);
	    gimple_to_cil_node (&csi, t);
	  }
      }
      break;

    case REAL_CST:
      /* Missing optimization, fall through for now */
    default:
      csi = csi_last (seq1);
      gen_modify_expr (&csi, decl, init);
      csi = csi_last (seq2);
      gen_modify_expr (&csi, decl, init);
    }
}

static size_t
cil_seq_num_instr (cil_seq seq)
{
  cil_stmt_iterator csi;
  size_t i = 0;

  for (csi = csi_start (seq); !csi_end_p (csi); csi_next (&csi))
    i++;

  return i;
}

static bool
cil_choose_init_method (size_t n1, size_t n2, size_t size, bool same)
{
  size_t c1, c2;

  if (same)
    {
      c1 = n1 * 2;
      c2 = (n2 + 5) * 2 + size;
    }
  else
    {
      c1 = n1 * 2;
      c2 = (n2 + 8) * 2 + size * 2;
    }

  if (size < 100)
    {
      if (c1 < c2 / 2)
	return false;
      else
	return true;
    }
  else
    {
      if (c1 < c2 / 10)
	return false;
      else
	return true;
    }
}

cil_seq
expand_init_to_cil_seq (tree decl, tree init)
{
  unsigned HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (decl)), 1);
  char *le_image = (char *) alloca (size);
  char *be_image = (char *) alloca (size);
  cil_stmt_iterator csi;
  cil_seq seq, seq1, seq2;
  cil_stmt stmt;
  unsigned int num_seq1, num_seq2;
  bool same;

  seq = cil_seq_alloc ();
  csi = csi_start (seq);

  if (size == 0)
    return seq;

  seq1 = cil_seq_alloc ();
  seq2 = cil_seq_alloc ();
  memset (le_image, 0, size);
  memset (be_image, 0, size);

  expand_init_to_cil_seq1 (decl, init, seq1, FALSE, seq2, le_image, be_image);

  num_seq1 = cil_seq_num_instr (seq1);
  num_seq2 = cil_seq_num_instr (seq2);
  same = (memcmp (le_image, be_image, size) == 0)
	 || TARGET_BIG_ENDIAN || TARGET_LITTLE_ENDIAN;

  /* Decide what to do */
  if (cil_choose_init_method (num_seq1, num_seq2, size, same))
    {
      tree sconst_le, sconst_be;

      gimple_to_cil_node (&csi, build_fold_addr_expr (decl));

      if (!same)
	{
	  sconst_le = build_string_literal (size, le_image);
	  gimple_to_cil_node (&csi, sconst_le);

	  sconst_be = build_string_literal (size, be_image);
	  gimple_to_cil_node (&csi, sconst_be);

	  stmt = cil_build_call (cil32_builtins[CIL32_BUILT_IN_ENDIAN_SELECT]);
	  csi_insert_after (&csi, stmt, CSI_CONTINUE_LINKING);
	}
      else if (TARGET_BIG_ENDIAN)
	{
	  sconst_be = build_string_literal (size, be_image);
	  gimple_to_cil_node (&csi, sconst_be);
	}
      else /* TARGET_LITTLE_ENDIAN */
	{
	  sconst_le = build_string_literal (size, le_image);
	  gimple_to_cil_node (&csi, sconst_le);
	}

      gimple_to_cil_node (&csi, size_int (size));
      stmt = cil_build_stmt (CIL_CPBLK);
      cil_set_prefix_unaligned (stmt, 1);
      csi_insert_after (&csi, stmt, CSI_CONTINUE_LINKING);
      csi_insert_seq_after (&csi, seq2, CSI_CONTINUE_LINKING);
    }
  else
    csi_insert_seq_after (&csi, seq1, CSI_CONTINUE_LINKING);

  cil_lower_init (seq);

  return seq;
}

/* Generates a GIMPLE_MODIFY_STMT, MODIFY_EXPR or INIT_EXPR with a bit field as
   its left hand side operand.  The left hand side operand is pointed by LHS
   abd the right hand side one by RHS.  The generated statements are appended
   to the current function's CIL code using the CSI iterator.  */

static void
gen_bit_field_modify_expr (cil_stmt_iterator *csi, tree lhs, tree rhs)
{
  HOST_WIDE_INT bit_size = 0;
  HOST_WIDE_INT bit_pos = 0;
  HOST_WIDE_INT cont_off;
  HOST_WIDE_INT cont_size = 8;
  tree offset = NULL_TREE;
  enum machine_mode mode;
  int unsignedp = 0;
  int volatilep = 0;
  cil_stmt stmt;
  tree cont_type;
  tree mask_cst, shift_cst;
  tree ref;
  tree folded_rhs, tmp;

  /* TODO: Add support for packed bit-fields crossing 64-bit boundaries.
     TODO: Add support for big-endian targets.  */

  /* Get the object base address and emit it.  */
  ref = get_inner_reference (lhs, &bit_size, &bit_pos, &offset, &mode,
			     &unsignedp, &volatilep, false);
  gen_addr_expr (csi, ref);
  csi_insert_after (csi, cil_build_stmt (CIL_CONV_I), CSI_CONTINUE_LINKING);

  /* Calculate the container size.  */
  while ((bit_pos % cont_size + bit_size) > cont_size)
    cont_size *= 2;

  cont_type = get_integer_type (cont_size, true);
  cont_off = bit_pos % cont_size;

  /* Calculate the container address if needed.  */
  if ((bit_pos - cont_off) / BITS_PER_UNIT != 0)
    {
      gen_integer_cst (csi,
		       build_int_cst (intSI_type_node,
				      (bit_pos - cont_off) / BITS_PER_UNIT));
      stmt = cil_build_stmt (CIL_ADD);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }

  if (offset != NULL_TREE)
    {
      gimple_to_cil_node (csi, offset);
      stmt = cil_build_stmt (CIL_ADD);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }

  /* Duplicate the container address, we will need it later.  */
  stmt = cil_build_stmt (CIL_DUP);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

  /* Load the container.  */
  gen_scalar_ldind (csi, cont_type, volatilep);

  /* Compute the mask to be applied to the container.  */
  shift_cst = build_int_cst (intSI_type_node, cont_off);
  mask_cst = size_binop (LSHIFT_EXPR, build_int_cst (cont_type, 1),
			 build_int_cst (intSI_type_node, bit_size));
  mask_cst = size_binop (MINUS_EXPR, mask_cst, build_int_cst (cont_type, 1));
  mask_cst = size_binop (LSHIFT_EXPR, mask_cst, shift_cst);

  /* Apply the mask to the container.  */
  gen_integer_cst (csi, size_binop (BIT_XOR_EXPR, mask_cst,
				    build_int_cst (cont_type, -1)));
  stmt = cil_build_stmt (CIL_AND);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

  /* Put the new value on the stack. If the rhs is a constant fold the
     shift & mask operations, if it is not copy it and convert it in the
     container type */
  folded_rhs = fold_binary_to_constant (LSHIFT_EXPR, cont_type,
					fold_convert (cont_type, rhs),
					shift_cst);

  if (folded_rhs != NULL_TREE)
    {
      folded_rhs = fold_binary_to_constant (BIT_AND_EXPR, cont_type,
					    fold_convert (cont_type,
							  folded_rhs),
					    mask_cst);
    }

  if (folded_rhs != NULL_TREE)
    {
      if (!integer_zerop (folded_rhs))
	gimple_to_cil_node (csi, folded_rhs);
    }
  else
    {
      tmp = rhs;

      /* Strip redundant conversions.  */
      while (TREE_CODE (tmp) == NOP_EXPR && INTEGRAL_TYPE_P (TREE_TYPE (tmp)))
	tmp = GENERIC_TREE_OPERAND (tmp, 0);

      gimple_to_cil_node (csi, tmp);

      if (TYPE_PRECISION (TREE_TYPE (tmp)) > 32 && cont_size <= 32)
	{
	  stmt = cil_build_stmt (CIL_CONV_U4);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}
      else if (TYPE_PRECISION (TREE_TYPE (tmp)) <= 32 && cont_size > 32)
	{
	  stmt = cil_build_stmt (CIL_CONV_U8);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}

      if (!integer_zerop (shift_cst))
	{
	  gen_integer_cst (csi, shift_cst);
	  stmt = cil_build_stmt (CIL_SHL);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}

      if (cont_off + bit_size != cont_size)
	{
	  gen_integer_cst (csi, mask_cst);
	  stmt = cil_build_stmt (CIL_AND);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}
    }

  if (folded_rhs == NULL_TREE || !integer_zerop (folded_rhs))
    {
      /* Insert the new value inside the container.  */
      stmt = cil_build_stmt (CIL_OR);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }

  /* Store the container in memory.  */
  gen_scalar_stind (csi, cont_type, volatilep);
}

/* Generates a MODIFY_EXPR using a TARGET_MEM_REF node as its LHS operand.  */

static void
gen_target_mem_ref_modify_expr (cil_stmt_iterator *csi, tree lhs, tree rhs)
{
  tree type = TREE_TYPE (lhs);
  tree ptr_type = build_pointer_type (type);

  gimple_to_cil_node (csi, tree_mem_ref_addr (ptr_type, lhs));
  gimple_to_cil_node (csi, rhs);
  gen_stind (csi, type, TREE_THIS_VOLATILE (lhs));
}

/* Generates a MODIFY_EXPR using a BIT_FIELD_REF scalar access as its LHS
   operand.  */

static void
gen_bitfield_ref_modify_expr (cil_stmt_iterator *csi, tree lhs, tree rhs)
{
  HOST_WIDE_INT bit_size = 0;
  HOST_WIDE_INT bit_pos = 0;
  tree offset = NULL_TREE;
  enum machine_mode mode;
  int unsignedp = 0;
  int volatilep = 0;
  cil_stmt stmt;
  tree ref;

  /* Get the object base address and emit it.  */
  ref = get_inner_reference (lhs, &bit_size, &bit_pos, &offset, &mode,
			     &unsignedp, &volatilep, false);
  gen_addr_expr (csi, ref);
  csi_insert_after (csi, cil_build_stmt (CIL_CONV_I), CSI_CONTINUE_LINKING);

  /* Calculate the container address if needed.  */
  if (bit_pos / BITS_PER_UNIT != 0)
    {
      gen_integer_cst (csi,
		       build_int_cst (intSI_type_node,
				      bit_pos / BITS_PER_UNIT));
      stmt = cil_build_stmt (CIL_ADD);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }

  if (offset != NULL_TREE)
    {
      gimple_to_cil_node (csi, offset);
      stmt = cil_build_stmt (CIL_ADD);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }

  /* Put the new value on the stack and store the field.  */
  gimple_to_cil_node (csi, rhs);
  gen_scalar_stind (csi, TREE_TYPE (rhs), volatilep);
}

/* Generates a MODIFY_EXPR using a BIT_FIELD_REF scalar-element vector access
   as its LHS operand.  */

static void
gen_vector_bitfield_ref_modify_expr (cil_stmt_iterator *csi, tree lhs, tree rhs)
{
  cil_stmt stmt;
  tree cst;

  gen_addr_expr (csi, GENERIC_TREE_OPERAND (lhs, 0));
  csi_insert_after (csi, cil_build_stmt (CIL_CONV_I), CSI_CONTINUE_LINKING);
  cst = size_binop (TRUNC_DIV_EXPR, GENERIC_TREE_OPERAND (lhs, 2),
		    bitsize_unit_node);

  if (!integer_zerop (cst))
    {
      gen_integer_cst (csi, fold_convert (intSI_type_node, cst));
      stmt = cil_build_stmt (CIL_ADD);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }

  gimple_to_cil_node (csi, rhs);
  gen_stind (csi, TREE_TYPE (rhs), TREE_THIS_VOLATILE (lhs));
}

/* Converts a GIMPLE_MODIFY_STMT, MODIFY_EXPR or INIT_EXPR into the CIL form
   eventually expanding the arguments if they cannot be converted directly. The
   left hand side operand is pointed by LHS and the right hand side one by RHS.
   The generated statements are appended to the current function's CIL code
   using the CSI iterator.  */

static void
gen_modify_expr (cil_stmt_iterator *csi, tree lhs, tree rhs)
{
  cil_stmt stmt;

  switch (TREE_CODE (lhs))
    {
    case VAR_DECL:
    case RESULT_DECL:
      mark_referenced_type (TREE_TYPE (lhs));

      if (!DECL_FILE_SCOPE_P (lhs) && !TREE_STATIC (lhs))
	{
	  if (TREE_THIS_VOLATILE (lhs))
	    {
	      /* put the address of the loc on the stack */
	      stmt = cil_build_stmt_arg (CIL_LDLOCA, lhs);
	      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	      /* put the value on the stack */
	      gimple_to_cil_node (csi, rhs);
	      /* and emit a volatile stind or stobj */
	      gen_stind (csi, TREE_TYPE (lhs), true);
	    }
	  else
	    {
	      /* put the value on the stack */
	      gimple_to_cil_node (csi, rhs);
	      stmt = cil_build_stmt_arg (CIL_STLOC, lhs);
	      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	    }
	}
      else
	{
	  gimple_to_cil_node (csi, rhs);
	  stmt = cil_build_stmt_arg (CIL_STSFLD, lhs);
	  cil_set_prefix_volatile (stmt, TREE_THIS_VOLATILE (lhs));
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}

      break;

    case PARM_DECL:
      gimple_to_cil_node (csi, rhs);
      stmt = cil_build_stmt_arg (CIL_STARG, lhs);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      break;

    case ARRAY_REF:
    case INDIRECT_REF:
      gen_addr_expr (csi, lhs);
      gimple_to_cil_node (csi, rhs);
      gen_stind (csi, TREE_TYPE (rhs), TREE_THIS_VOLATILE (lhs));
      break;

    case MISALIGNED_INDIRECT_REF:
      gen_addr_expr (csi, lhs);
      gimple_to_cil_node (csi, rhs);
      gen_misaligned_stvec (csi, TREE_TYPE (rhs), TREE_THIS_VOLATILE (lhs));
      break;

    case COMPONENT_REF:
      {
	tree obj = GENERIC_TREE_OPERAND (lhs, 0);
	tree fld = GENERIC_TREE_OPERAND (lhs, 1);

	mark_referenced_type (TYPE_MAIN_VARIANT (TREE_TYPE (obj)));

	if (DECL_BIT_FIELD (fld))
	  gen_bit_field_modify_expr (csi, lhs, rhs);
	else
	  {
	    /* put the value on the stack */
	    gen_addr_expr (csi, obj);
	    gimple_to_cil_node (csi, rhs);
	    stmt = cil_build_stmt_arg (CIL_STFLD, fld);

	    if (contains_packed_reference (lhs))
	      cil_set_prefix_unaligned (stmt, 1);

	    cil_set_prefix_volatile (stmt, TREE_THIS_VOLATILE (lhs));
	    csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  }
      }
      break;

    case TARGET_MEM_REF:
      gen_target_mem_ref_modify_expr (csi, lhs, rhs);
      break;

    case REALPART_EXPR:
    case IMAGPART_EXPR:
      gen_addr_expr (csi, lhs);
      gimple_to_cil_node (csi, rhs);
      gen_scalar_stind (csi, TREE_TYPE (lhs), TREE_THIS_VOLATILE (lhs));
      break;

    case BIT_FIELD_REF:
      if (TREE_CODE (TREE_TYPE (GENERIC_TREE_OPERAND (lhs, 0))) == VECTOR_TYPE)
	gen_vector_bitfield_ref_modify_expr (csi, lhs, rhs);
      else
	gen_bitfield_ref_modify_expr (csi, lhs, rhs);

      break;

    default:
      gcc_unreachable ();
    }
}

/* Generates a GOTO_EXPR including the emulation needed for computed GOTOs.  */

static void
gen_goto_expr (cil_stmt_iterator *csi, tree node)
{
  tree label_decl = GOTO_DESTINATION (node);
  cil_stmt stmt;

  if (computed_goto_p (node))
    {
      /* This is a goto to the address of a label. Labels have
	 been numbered, and we emit a switch based on that ID.  */
      gimple_to_cil_node (csi, label_decl);
      stmt = cil_build_switch (get_label_addrs());
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }
  else
    {
      basic_block dest_bb = label_to_block (label_decl);

      if (csi_bb (*csi)->next_bb != dest_bb)
	{
	  stmt = cil_build_stmt_arg (CIL_BR, label_decl);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}
    }
}

/* Generates a conditional expression.  */

static void
gen_cond_expr (cil_stmt_iterator *csi, tree node)
{
  edge true_edge, false_edge;
  tree label_then, label_else, cond, lhs, rhs, type;
  basic_block dest_bb;
  cil_stmt stmt;
  enum cil_opcode opcode;
  bool uns;

  extract_true_false_edges_from_block (csi_bb (*csi), &true_edge, &false_edge);
  label_then = tree_block_label (true_edge->dest);
  label_else = tree_block_label (false_edge->dest);

  cond = COND_EXPR_COND (node);

  if (DECL_P (cond))
    {
      gimple_to_cil_node (csi, cond);
      gimple_to_cil_node (csi,
			  fold_convert (TREE_TYPE (cond), integer_zero_node));
      stmt = cil_build_stmt_arg (CIL_BNE_UN, label_then);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }
  else
    {
      lhs = GENERIC_TREE_OPERAND (cond, 0);
      rhs = GENERIC_TREE_OPERAND (cond, 1);
      type = TREE_TYPE (lhs);

      switch (TREE_CODE (cond))
	{
	case EQ_EXPR:
	case NE_EXPR:
	  if (tree_int_cst_equal (lhs, integer_zero_node)
	      && (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type)))
	    {
	      opcode = TREE_CODE (cond) == EQ_EXPR ? CIL_BRFALSE : CIL_BRTRUE;
	      gimple_to_cil_node (csi, rhs);
	      stmt = cil_build_stmt_arg (opcode, label_then);
	      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	    }
	  else if (tree_int_cst_equal (rhs, integer_zero_node)
		   && (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type)))
	    {
	      opcode = TREE_CODE (cond) == EQ_EXPR ? CIL_BRFALSE : CIL_BRTRUE;
	      gimple_to_cil_node (csi, lhs);
	      stmt = cil_build_stmt_arg (opcode, label_then);
	      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	    }
	  else
	    {
	      opcode = TREE_CODE (cond) == EQ_EXPR ? CIL_BEQ : CIL_BNE_UN;
	      gimple_to_cil_node (csi, lhs);
	      gimple_to_cil_node (csi, rhs);
	      stmt = cil_build_stmt_arg (opcode, label_then);
	      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	    }

	  break;

	case LE_EXPR:
	case LT_EXPR:
	case GE_EXPR:
	case GT_EXPR:
	case UNLT_EXPR:
	case UNLE_EXPR:
	case UNGT_EXPR:
	case UNGE_EXPR:
	  uns = TYPE_UNSIGNED (type);
	  gimple_to_cil_node (csi, lhs);
	  gimple_to_cil_node (csi, rhs);

	  switch (TREE_CODE (cond))
	    {
	    case LE_EXPR:   opcode = uns ? CIL_BLE_UN : CIL_BLE; break;
	    case LT_EXPR:   opcode = uns ? CIL_BLT_UN : CIL_BLT; break;
	    case GE_EXPR:   opcode = uns ? CIL_BGE_UN : CIL_BGE; break;
	    case GT_EXPR:   opcode = uns ? CIL_BGT_UN : CIL_BGT; break;
	    case UNLT_EXPR: opcode = CIL_BLT_UN;                 break;
	    case UNLE_EXPR: opcode = CIL_BLE_UN;                 break;
	    case UNGT_EXPR: opcode = CIL_BGT_UN;                 break;
	    case UNGE_EXPR: opcode = CIL_BGE_UN;                 break;
	    default:
	      gcc_unreachable ();
	    }

	  stmt = cil_build_stmt_arg (opcode, label_then);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  break;

	case UNORDERED_EXPR:
	case ORDERED_EXPR:
	  gimple_to_cil_node (csi, lhs);
	  stmt = cil_build_stmt (CIL_DUP);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  stmt = cil_build_stmt (CIL_CEQ);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

	  gimple_to_cil_node (csi, rhs);
	  stmt = cil_build_stmt (CIL_DUP);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  stmt = cil_build_stmt (CIL_CEQ);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

	  stmt = cil_build_stmt (CIL_AND);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  opcode = (TREE_CODE (cond) == ORDERED_EXPR) ? CIL_BRTRUE : CIL_BRFALSE;
	  stmt = cil_build_stmt_arg (opcode, label_then);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  break;

	case UNEQ_EXPR:
	  lhs = gen_expr_copy (csi, lhs);
	  rhs = gen_expr_copy (csi, rhs);

	  /* Emit the equivalent of an UNORDERED_EXPR ...  */
	  gimple_to_cil_node (csi, lhs);
	  stmt = cil_build_stmt (CIL_DUP);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  stmt = cil_build_stmt (CIL_CEQ);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

	  gimple_to_cil_node (csi, rhs);
	  stmt = cil_build_stmt (CIL_DUP);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  stmt = cil_build_stmt (CIL_CEQ);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  stmt = cil_build_stmt (CIL_AND);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

	  gen_integer_cst (csi, integer_one_node);
	  stmt = cil_build_stmt (CIL_XOR);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

	  /* ... plus an equal comparison.  */
	  gimple_to_cil_node (csi, lhs);
	  gimple_to_cil_node (csi, rhs);
	  stmt = cil_build_stmt (CIL_CEQ);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  stmt = cil_build_stmt (CIL_OR);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  stmt = cil_build_stmt_arg (CIL_BRTRUE, label_then);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  break;

	case LTGT_EXPR:
	  lhs = gen_expr_copy (csi, lhs);
	  rhs = gen_expr_copy (csi, rhs);

	  gimple_to_cil_node (csi, lhs);
	  gimple_to_cil_node (csi, rhs);
	  stmt = cil_build_stmt (CIL_CGT);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  gimple_to_cil_node (csi, lhs);
	  gimple_to_cil_node (csi, rhs);
	  stmt = cil_build_stmt (CIL_CLT);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  stmt = cil_build_stmt (CIL_OR);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  stmt = cil_build_stmt_arg (CIL_BRTRUE, label_then);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  break;

	default:
	  gimple_to_cil_node (csi, cond);
	  stmt = cil_build_stmt_arg (CIL_BRTRUE, label_then);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}
    }

  /* TODO: Emit JIT compilation hints
  if (TARGET_EMIT_JIT_COMPILATION_HINTS)
    branch_probability_add (file, node); */

  dest_bb = label_to_block (label_else);

  /* Emit else block only if it is not a fallthrough */
  if (csi_bb (*csi)->next_bb != dest_bb)
    {
      stmt = cil_build_stmt_arg (CIL_BR, label_else);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }
}

/* Emit a switch expression.  */

static void
gen_switch_expr (cil_stmt_iterator *csi, tree node)
{
  tree labels = SWITCH_LABELS (node);
  tree min = TREE_VEC_ELT (labels, 0);
  unsigned int length = TREE_VEC_LENGTH (labels);
  tree default_label = CASE_LABEL (TREE_VEC_ELT (labels, length - 1));
  basic_block dest_bb;
  cil_stmt stmt;

  /* Generate the switch condition.  */
  gimple_to_cil_node (csi, SWITCH_COND (node));

  /* 'Normalize' the condition.  */
  if (!tree_int_cst_equal (CASE_LOW (min), integer_zero_node))
    {
      gen_integer_cst (csi, CASE_LOW (min));
      stmt = cil_build_stmt (CIL_SUB);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }

  /* Generate the switch and the default label fall thru.  */
  stmt = cil_build_switch (labels);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

  dest_bb = label_to_block (default_label);

  if (csi_bb (*csi)->next_bb != dest_bb)
    {
      stmt = cil_build_stmt_arg (CIL_BR, default_label);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }
}


/* Generate the CIL code associated with the __builtin_va_start() call
   specified by NODE. The generated CIL statements will be appended to CSI.  */

static void
gen_builtin_va_start (cil_stmt_iterator *csi, tree node)
{
  tree argiter = create_tmp_var (cil32_arg_iterator_type, "arg_iterator");
  cil_stmt stmt;

  stmt = cil_build_stmt_arg (CIL_LDLOCA, argiter);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
  stmt = cil_build_stmt (CIL_DUP);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
  stmt = cil_build_call (cil32_builtins[CIL32_BUILT_IN_VA_INIT]);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
  stmt = cil_build_call (cil32_builtins[CIL32_BUILT_IN_VA_START]);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

  /* FIXME: The extra indirection step may be optimized out in the common case
     or removed later using a peephole optimization.  */
  gimple_to_cil_node (csi, CALL_EXPR_ARG (node, 0));
  stmt = cil_build_stmt_arg (CIL_LDLOCA, argiter);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
  stmt = cil_build_stmt (CIL_STIND_I);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
}

/* Generate the CIL code associated with the __builtin_va_end() call
   specified by NODE. The generated CIL statements will be appended to CSI.  */

static void
gen_builtin_va_end (cil_stmt_iterator *csi, tree node)
{
  cil_stmt stmt;

  /* FIXME: The extra indirection step may be optimized out in the common case
     or removed later using a peephole optimization.  */
  gimple_to_cil_node (csi, CALL_EXPR_ARG (node, 0));
  stmt = cil_build_stmt (CIL_LDIND_I);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
  stmt = cil_build_call (cil32_builtins[CIL32_BUILT_IN_VA_END]);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
}

/* Generate the CIL code associated with the __builtin_va_copy() call
   specified by NODE. The generated CIL statements will be appended to CSI.  */

static void
gen_builtin_va_copy (cil_stmt_iterator *csi, tree node)
{
  tree argiter = create_tmp_var (cil32_arg_iterator_type, "arg_iterator");
  cil_stmt stmt;

  stmt = cil_build_stmt_arg (CIL_LDLOCA, argiter);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
  stmt = cil_build_stmt (CIL_DUP);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
  stmt = cil_build_call (cil32_builtins[CIL32_BUILT_IN_VA_INIT]);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

  /* FIXME: The extra indirection step may be optimized out in the common case
     or removed later using a peephole optimization.  */
  /* Load the source argument iterator.  */
  gimple_to_cil_node (csi, CALL_EXPR_ARG (node, 0));
  stmt = cil_build_stmt_arg (CIL_LDLOCA, argiter);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
  stmt = cil_build_stmt (CIL_STIND_I);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

  /* Load the destination argument iterator.*/
  gimple_to_cil_node (csi, CALL_EXPR_ARG (node, 1));

  /* Make the copy.  */
  stmt = cil_build_call (cil32_builtins[CIL32_BUILT_IN_VA_COPY]);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
}

/* Inspired from 'expand_builtin_object_size' in builtins.c. We return -1
   for types 0 and 1, and 0 for types 2 and 3.  */

static void
gen_builtin_object_size (cil_stmt_iterator *csi, tree node)
{
  tree arg2 = CALL_EXPR_ARG (node, 1);
  int obj_type;

  STRIP_NOPS (arg2);
  gcc_assert (TREE_CODE (arg2) == INTEGER_CST);
  obj_type = tree_low_cst (arg2, 0);

  switch (obj_type)
    {
    case 0:
    case 1:
      gen_integer_cst (csi, integer_zero_node);
      break;

    case 2:
    case 3:
      gen_integer_cst (csi, integer_minus_one_node);
      break;

    default:
      gcc_unreachable ();
    }
}

/* Try to handle a builtin call. In some cases this function will expand the
   builtin and return true, this indicates that the call has been effectively
   removed and no other action is required, otherwise false will be
   returned. The current CALL_EXPR is passed in NODE and the function
   declaration in FDECL. If the builtin is expanded the generated CIL
   statements will be appended to CSI.  */

static bool
gen_call_builtin (cil_stmt_iterator *csi, tree node, tree fdecl)
{
  cil_stmt stmt;

  if (DECL_BUILT_IN_CLASS (fdecl) != BUILT_IN_MD)
    {
      switch (DECL_FUNCTION_CODE (fdecl))
	{
	case BUILT_IN_VA_START:
	  gen_builtin_va_start (csi, node);
	  return true;

	case BUILT_IN_VA_END:
	  gen_builtin_va_end (csi, node);
	  return true;

	case BUILT_IN_VA_COPY:
	  gen_builtin_va_copy (csi, node);
	  return true;

	case BUILT_IN_CLZ:
	case BUILT_IN_CLZL:
	  gimple_to_cil_node (csi, CALL_EXPR_ARG (node, 0));
	  stmt = cil_build_call (cil32_builtins[CIL32_CLZSI2]);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  return true;

	case BUILT_IN_CLZLL:
	  gimple_to_cil_node (csi, CALL_EXPR_ARG (node, 0));
	  stmt = cil_build_call (cil32_builtins[CIL32_CLZDI2]);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  return true;

	case BUILT_IN_CTZ:
	case BUILT_IN_CTZL:
	  gimple_to_cil_node (csi, CALL_EXPR_ARG (node, 0));
	  stmt = cil_build_call (cil32_builtins[CIL32_CTZSI2]);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  return true;

	case BUILT_IN_CTZLL:
	  gimple_to_cil_node (csi, CALL_EXPR_ARG (node, 0));
	  stmt = cil_build_call (cil32_builtins[CIL32_CTZDI2]);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  return true;

	case BUILT_IN_FFS:
	case BUILT_IN_FFSL:
	  gimple_to_cil_node (csi, CALL_EXPR_ARG (node, 0));
	  stmt = cil_build_call (cil32_builtins[CIL32_FFSSI2]);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  return true;

	case BUILT_IN_FFSLL:
	  gimple_to_cil_node (csi, CALL_EXPR_ARG (node, 0));
	  stmt = cil_build_call (cil32_builtins[CIL32_FFSDI2]);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  return true;

	case BUILT_IN_PARITY:
	case BUILT_IN_PARITYL:
	  gimple_to_cil_node (csi, CALL_EXPR_ARG (node, 0));
	  stmt = cil_build_call (cil32_builtins[CIL32_PARITYSI2]);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  return true;

	case BUILT_IN_PARITYLL:
	  gimple_to_cil_node (csi, CALL_EXPR_ARG (node, 0));
	  stmt = cil_build_call (cil32_builtins[CIL32_PARITYDI2]);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  return true;

	case BUILT_IN_POPCOUNT:
	case BUILT_IN_POPCOUNTL:
	  gimple_to_cil_node (csi, CALL_EXPR_ARG (node, 0));
	  stmt = cil_build_call (cil32_builtins[CIL32_POPCOUNTSI2]);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  return true;

	case BUILT_IN_POPCOUNTLL:
	  gimple_to_cil_node (csi, CALL_EXPR_ARG (node, 0));
	  stmt = cil_build_call (cil32_builtins[CIL32_POPCOUNTDI2]);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  return true;

	case BUILT_IN_OBJECT_SIZE:
	  gen_builtin_object_size (csi, node);
	  return true;

	case BUILT_IN_INIT_TRAMPOLINE:
	case BUILT_IN_ADJUST_TRAMPOLINE:
	case BUILT_IN_NONLOCAL_GOTO:
	  internal_error ("Builtins to support Trampolines not implemented\n");

	case BUILT_IN_PROFILE_FUNC_ENTER:
	case BUILT_IN_PROFILE_FUNC_EXIT:
	  internal_error ("Builtins to support Profiling not implemented\n");

	case BUILT_IN_SETJMP_SETUP:
	case BUILT_IN_SETJMP_DISPATCHER:
	case BUILT_IN_SETJMP_RECEIVER:
	  internal_error ("Builtins to support Setjump not implemented\n");

	case BUILT_IN_MEMSET:
	  {
	    tree ptr   = CALL_EXPR_ARG (node, 0);
	    tree value = CALL_EXPR_ARG (node, 1);
	    tree size  = CALL_EXPR_ARG (node, 2);

	    gimple_to_cil_node (csi, ptr);
	    stmt = cil_build_stmt (CIL_DUP);
	    csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	    gimple_to_cil_node (csi, value);
	    gimple_to_cil_node (csi, size);
	    stmt = cil_build_stmt (CIL_INITBLK);
	    cil_set_prefix_unaligned (stmt, 1);
	    csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	    return true;
	  }

	case BUILT_IN_MEMCPY:
	  {
	    tree ptr_dst = CALL_EXPR_ARG (node, 0);
	    tree ptr_src = CALL_EXPR_ARG (node, 1);
	    tree size    = CALL_EXPR_ARG (node, 2);

	    gimple_to_cil_node (csi, ptr_dst);
	    stmt = cil_build_stmt (CIL_DUP);
	    csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	    gimple_to_cil_node (csi, ptr_src);
	    gimple_to_cil_node (csi, size);
	    stmt = cil_build_stmt (CIL_CPBLK);
	    cil_set_prefix_unaligned (stmt, 1);
	    csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	    return true;
	  }

	case BUILT_IN_ALLOCA:
	  {
	    tree size = CALL_EXPR_ARG (node, 0);

	    gimple_to_cil_node (csi, size);
	    stmt = cil_build_stmt (CIL_LOCALLOC);
	    csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	    return true;
	  }

	case BUILT_IN_STACK_SAVE:
	  /* FIXME: This built-in is only used for the implementation
	     of variable-length arrays.  It is not needed in CIL.  */
	  gen_integer_cst (csi, integer_zero_node);
	  stmt = cil_build_stmt (CIL_CONV_I);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  return true;

	case BUILT_IN_STACK_RESTORE:
	  /* FIXME: This built-in is only used for the implementation
	     of variable-length arrays.  It is not needed in CIL.  */
	  return true;

	case BUILT_IN_EXPECT:
	  /* TODO: __builtin_expect(exp,val) evalutes exp and tells the
	     compiler that it most likely gives val.  We just evaluate exp
	     but we could flag it for JIT hints emission.  */
	  gimple_to_cil_node (csi, CALL_EXPR_ARG (node, 0));
	  return true;

	case BUILT_IN_PREFETCH:
	  if (TREE_SIDE_EFFECTS (CALL_EXPR_ARG (node, 0)))
	    gimple_to_cil_node (csi, CALL_EXPR_ARG (node, 0));

	  return true;

	case BUILT_IN_FRAME_ADDRESS:
	case BUILT_IN_RETURN_ADDRESS:
	  {
	    /* Supported (sort of) only for non-zero parameter, when it is ok
	       to return NULL.  */
	    tree arg = CALL_EXPR_ARG (node, 0);
	    int  int_arg = tree_low_cst (arg, 0);

	    if (int_arg == 0)
	      internal_error ("__builtin_{return,frame}_address not implemented\n");
	    else
	      gen_integer_cst (csi, integer_zero_node);
	  }
	  return true;

	case BUILT_IN_BZERO:
	  {
	    tree ptr   = CALL_EXPR_ARG (node, 0);
	    tree size  = CALL_EXPR_ARG (node, 1);

	    gen_initblk (csi, ptr, integer_zero_node, size);
	    return true;
	  }

	case BUILT_IN_BCOPY:
	  {
	    tree ptr_src = CALL_EXPR_ARG (node, 0);
	    tree ptr_dst = CALL_EXPR_ARG (node, 1);
	    tree size    = CALL_EXPR_ARG (node, 2);

	    gen_cpblk (csi, ptr_dst, ptr_src, size);
	    return true;
	  }

	default:
	  if (DECL_ASSEMBLER_NAME_SET_P (node))
	    {
	       /* Go ahead as a normal function call */
	    }
	}
    }
  else
    {
      switch (DECL_FUNCTION_CODE (fdecl))
	{
	case CIL32_BUILT_IN_VA_ARG:
	  gimple_to_cil_node (csi, CALL_EXPR_ARG (node, 0));
	  stmt = cil_build_call (fdecl);
	  cil_call_set_dummy_arg (stmt, CALL_EXPR_ARG (node, 1));
	  /* We 'patch' the generated call statement so as to make it behave as
	     if it had been passed a single argument.  */
	  stmt->arg.fcall->nargs = 1;
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  return true;

	case CIL32_BUILT_IN_CPBLK:
	  {
	    tree ptr_dst = CALL_EXPR_ARG (node, 0);
	    tree ptr_src = CALL_EXPR_ARG (node, 1);
	    tree size    = CALL_EXPR_ARG (node, 2);

	    gen_cpblk (csi, ptr_dst, ptr_src, size);
	    return true;
	  }

	case CIL32_BUILT_IN_INITBLK:
	  {
	    tree ptr   = CALL_EXPR_ARG (node, 0);
	    tree value = CALL_EXPR_ARG (node, 1);
	    tree size  = CALL_EXPR_ARG (node, 2);

	    gen_initblk (csi, ptr, value, size);
	    return true;
	  }

	default:
	  ;
	}
    }

  return false;
}

/* Generates a function call from a CALL_EXPR gimple node NODE.  The generated
   statements are appended to the current function's CIL code using the CSI
   iterator.  */

static void
gen_call_expr (cil_stmt_iterator *csi, tree node)
{
  tree fdecl;
  tree ftype;
  tree arg_types;
  VEC(tree, heap) *arglist;
  tree static_chain;
  bool direct = true;
  bool varargs = false;
  bool missing = false;
  size_t nargs_base;
  size_t nargs;
  size_t i;
  cil_stmt stmt;

  gcc_assert (TREE_CODE (node) == CALL_EXPR);
  fdecl = get_callee_fndecl (node);
  nargs = call_expr_nargs (node);

  if (fdecl != NULL_TREE)
    ftype = TREE_TYPE (fdecl);
  else
    {
      ftype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (node)));
      direct = false;
    }

  /* Built-in functions must be handled in a special way */
  if (direct && DECL_BUILT_IN (fdecl))
    {
      if (gen_call_builtin (csi, node, fdecl))
	return;
    }

  arg_types = TYPE_ARG_TYPES (ftype);

  if (arg_types == NULL)
    {
      if (direct)
	{
	  warning (OPT_Wcil_missing_prototypes,
		   "Missing function %s prototype, guessing it, you should fix "
		   "the code",
		   IDENTIFIER_POINTER (DECL_NAME (fdecl)));
	}
      else
	{
	  warning (OPT_Wcil_missing_prototypes,
		   "Missing indirect function prototype, guessing it, you "
		   "should fix the code");
	}

      /* Guess types using the type of the arguments.  */
      nargs_base = 0;
      missing = true;
    }
  else
    {
      tree last_arg_type = tree_last (arg_types);
      nargs_base = list_length (arg_types);

      if (TREE_VALUE (last_arg_type) != void_type_node)
	varargs = true;
      else
	nargs_base--;
    }

  arglist = VEC_alloc (tree, heap, nargs - nargs_base);

  /* If static chain present, it will be the first argument.  */
  static_chain = CALL_EXPR_STATIC_CHAIN (node);

  if (static_chain)
    gimple_to_cil_node (csi, static_chain);

  for (i = 0; i < nargs_base; i++)
    gimple_to_cil_node (csi, CALL_EXPR_ARG (node, i));

  /* Vararg parameters, this will be added only if they are present.  */
  for (; i < nargs; i++)
    {
      tree arg = CALL_EXPR_ARG (node, i);
      tree arg_type = TREE_TYPE (arg);

      gimple_to_cil_node (csi, arg);
      VEC_quick_insert (tree, arglist, i - nargs_base, arg_type);

      if (TREE_CODE (arg_type) == POINTER_TYPE
	  || (TREE_CODE (arg_type) == ARRAY_TYPE
	      && (!TYPE_DOMAIN (arg_type) || ARRAY_TYPE_VARLENGTH (arg_type))))
	{
	  stmt = cil_build_stmt (CIL_CONV_I);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}
    }

  /* TODO: We could do return slot optimizations, or insertion of the tail
     call prefix here? */
  if (!direct)
    {
      /* Generate the function pointer, in case of an indirect call.  */
      gimple_to_cil_node (csi, CALL_EXPR_FN (node));

      if (varargs)
	stmt = cil_build_calli_va (ftype, arglist);
      else if (missing)
	stmt = cil_build_calli_mp (ftype, arglist);
      else
	stmt = cil_build_calli (ftype);
    }
  else
    {
      if (varargs)
	stmt = cil_build_call_va (fdecl, arglist);
      else if (missing)
	stmt = cil_build_call_mp (fdecl, arglist);
      else
	stmt = cil_build_call (fdecl);
    }

  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

  if (static_chain)
    cil_call_set_static_chain (stmt, TREE_TYPE (static_chain));

  if (arglist != NULL)
    VEC_free (tree, heap, arglist);
}

/* Generates a copy inside a temporary variable of the expression NODE if it is
   necessary or beneficial. Returns the new variable holding the copy or the
   original expression if it wasn't copied.  */

static tree
gen_expr_copy (cil_stmt_iterator *csi, tree node)
{
  enum tree_code code = TREE_CODE (node);
  cil_stmt stmt;
  tree tmp;

  if (!TREE_SIDE_EFFECTS (node)
      && ((code == INTEGER_CST) || (code == REAL_CST)
	  || (code == VAR_DECL) || (code == PARM_DECL)))
    {
      return node;
    }

  tmp = create_tmp_var (TREE_TYPE (node), "gimple2cil");
  gimple_to_cil_node (csi, node);
  stmt = cil_build_stmt_arg (CIL_STLOC, tmp);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

  return tmp;
}

/* Generates a BIT_AND_EXPR potentially folding it into a conversion in order
   to minimize code size.  */

static void
gen_bit_and_expr (cil_stmt_iterator *csi, tree node)
{
  enum cil_opcode opcode;
  cil_stmt stmt;
  tree op0 = GENERIC_TREE_OPERAND (node, 0);
  tree op1 = GENERIC_TREE_OPERAND (node, 1);

  if (TREE_CODE (op0) == INTEGER_CST
      && (TREE_INT_CST_LOW (op0) == 255U
	  || TREE_INT_CST_LOW (op0) == 65535U
	  || TREE_INT_CST_LOW (op0) == 4294967295U)
      && TREE_INT_CST_HIGH (op0) == 0)
    {
      gimple_to_cil_node (csi, op1);

      switch (TREE_INT_CST_LOW (op0))
	{
	case 255U:        opcode = CIL_CONV_U1; break;
	case 65535U:      opcode = CIL_CONV_U2; break;
	case 4294967295U: opcode = CIL_CONV_U4; break;
	default:
	  gcc_unreachable ();
	}

      csi_insert_after (csi, cil_build_stmt (opcode), CSI_CONTINUE_LINKING);

      if (TYPE_PRECISION (TREE_TYPE (node)) > 32)
	{
	  stmt = cil_build_stmt (CIL_CONV_U8);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}
    }
  else if (TREE_CODE (op1) == INTEGER_CST
	   && (TREE_INT_CST_LOW (op1) == 255U
	       || TREE_INT_CST_LOW (op1) == 65535U
	       || TREE_INT_CST_LOW (op1) == 4294967295U)
	   && TREE_INT_CST_HIGH (op1) == 0)
    {
      gimple_to_cil_node (csi, op0);

      switch (TREE_INT_CST_LOW (op1))
	{
	case 255U:        opcode = CIL_CONV_U1; break;
	case 65535U:      opcode = CIL_CONV_U2; break;
	case 4294967295U: opcode = CIL_CONV_U4; break;
	default:
	  gcc_unreachable ();
	}

    csi_insert_after (csi, cil_build_stmt (opcode), CSI_CONTINUE_LINKING);

    if (TYPE_PRECISION (TREE_TYPE (node)) > 32)
      {
	stmt = cil_build_stmt (CIL_CONV_U8);
	csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      }
    }
  else
    {
      gimple_to_cil_node (csi, op0);
      gimple_to_cil_node (csi, op1);
      stmt = cil_build_stmt (CIL_AND);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }

  /* No need for conversions even in case of values with precision
     smaller than the one used on the evaluation stack, since for these
     operations the output is always less or equal than both operands.  */
}

/* Generates a LT_EXPR, LE_EXPR, GT_EXPR, GE_EXPR, EQ_EXPR, NE_EXPR,
   UNORDERED_EXPR, ORDERED_EXPR, UNLT_EXPR, UNLE_EXPR, UNGT_EXPR, UNGE_EXPR,
   UNEQ_EXPR or LTGT_EXPR expression used outside of a COND_EXPR.  */

static void
gen_compare_expr (cil_stmt_iterator *csi, tree node)
{
  enum tree_code code = TREE_CODE (node);
  tree op0 = GENERIC_TREE_OPERAND (node, 0);
  tree op1 = GENERIC_TREE_OPERAND (node, 1);
  cil_stmt stmt;
  enum cil_opcode opcode;

  switch (code)
    {
    case LT_EXPR:
    case GT_EXPR:
    case EQ_EXPR:
    case NE_EXPR:
    case UNLT_EXPR:
    case UNGT_EXPR:
      gimple_to_cil_node (csi, op0);
      gimple_to_cil_node (csi, op1);

      if (TREE_CODE (node) == NE_EXPR)
	{
	  stmt = cil_build_stmt (CIL_CEQ);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  gen_integer_cst (csi, integer_one_node);
	  stmt = cil_build_stmt (CIL_XOR);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}
      else
	{
	  switch (TREE_CODE (node))
	    {
	    case LT_EXPR:
	      opcode = TYPE_UNSIGNED (TREE_TYPE (op0)) ? CIL_CLT_UN : CIL_CLT;
	      break;

	    case GT_EXPR:
	      opcode = TYPE_UNSIGNED (TREE_TYPE (op0)) ? CIL_CGT_UN : CIL_CGT;
	      break;

	    case EQ_EXPR:
	      opcode = CIL_CEQ;
	      break;

	    case UNLT_EXPR:
	      opcode = CIL_CLT_UN;
	      break;

	    case UNGT_EXPR:
	      opcode = CIL_CGT_UN;
	      break;

	    default:
	      gcc_unreachable ();
	  }

	  stmt = cil_build_stmt (opcode);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}

      break;

    case LE_EXPR:
    case GE_EXPR:
      gimple_to_cil_node (csi, op0);
      gimple_to_cil_node (csi, op1);

      if (TREE_CODE (node) == LE_EXPR)
	opcode = TYPE_UNSIGNED (TREE_TYPE (op0)) ? CIL_CGT_UN : CIL_CGT;
      else
	opcode = TYPE_UNSIGNED (TREE_TYPE (op0)) ? CIL_CLT_UN : CIL_CLT;

      stmt = cil_build_stmt (opcode);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      gen_integer_cst (csi, integer_one_node);
      stmt = cil_build_stmt (CIL_XOR);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      break;

    case UNORDERED_EXPR:
    case ORDERED_EXPR:
      gimple_to_cil_node (csi, op0);
      stmt = cil_build_stmt (CIL_DUP);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      stmt = cil_build_stmt (CIL_CEQ);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

      gimple_to_cil_node (csi, op1);
      stmt = cil_build_stmt (CIL_DUP);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      stmt = cil_build_stmt (CIL_CEQ);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      stmt = cil_build_stmt (CIL_AND);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

      if (TREE_CODE (node) == UNORDERED_EXPR)
	{
	  gen_integer_cst (csi, integer_one_node);
	  stmt = cil_build_stmt (CIL_XOR);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}

      break;

    case UNEQ_EXPR:
    case UNLE_EXPR:
    case UNGE_EXPR:
      op0 = gen_expr_copy (csi, op0);
      op1 = gen_expr_copy (csi, op1);

      /* Emit the equivalent of an ORDERED_EXPR ...  */
      gimple_to_cil_node (csi, op0);
      stmt = cil_build_stmt (CIL_DUP);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      stmt = cil_build_stmt (CIL_CEQ);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

      gimple_to_cil_node (csi, op1);
      stmt = cil_build_stmt (CIL_DUP);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      stmt = cil_build_stmt (CIL_CEQ);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      stmt = cil_build_stmt (CIL_AND);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

      /* ... plus the relevant comparison.  */
      if (code == UNEQ_EXPR)
	{
	  /* !ORDERED_EXPR || EQ_EXPR */
	  gen_integer_cst (csi, integer_one_node);
	  stmt = cil_build_stmt (CIL_XOR);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  gimple_to_cil_node (csi, op0);
	  gimple_to_cil_node (csi, op1);
	  stmt = cil_build_stmt (CIL_CEQ);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  stmt = cil_build_stmt (CIL_OR);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}
      else
	{
	  /* !(ORDERED_EXPR && GT_EXPR) or !(ORDERED_EXPR && LT_EXPR) */
	  gimple_to_cil_node (csi, op0);
	  gimple_to_cil_node (csi, op1);
	  stmt = cil_build_stmt (code == UNLE_EXPR ? CIL_CGT : CIL_CLT);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  stmt = cil_build_stmt (CIL_AND);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  gen_integer_cst (csi, integer_one_node);
	  stmt = cil_build_stmt (CIL_XOR);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}

      break;

    case LTGT_EXPR:
      op0 = gen_expr_copy (csi, op0);
      op1 = gen_expr_copy (csi, op1);

      gimple_to_cil_node (csi, op0);
      gimple_to_cil_node (csi, op1);
      stmt = cil_build_stmt (CIL_CGT);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      gimple_to_cil_node (csi, op0);
      gimple_to_cil_node (csi, op1);
      stmt = cil_build_stmt (CIL_CLT);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      stmt = cil_build_stmt (CIL_OR);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      break;

    default:
      gcc_unreachable ();
  }

  if (tree_low_cst (TYPE_SIZE (TREE_TYPE (node)), 1) > 32)
    {
      stmt = cil_build_stmt (CIL_CONV_I8);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }
}

/* Generates CIL code for a (MIN|MAX)_EXPR held in the tree NODE.  The node is
   either expanded or replaced with a builtin call, depending on the options
   and the optimization level.  The generated statements are appended to the
   current function's CIL code using the CSI iterator.  */

static void
gen_minmax_expr (cil_stmt_iterator *csi, tree node)
{
  cil_stmt stmt;
  tree type = TREE_TYPE (node);
  bool max = (TREE_CODE (node) == MAX_EXPR);
  unsigned HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 1);
  bool unsignedp;
  enum cil32_builtin builtin = 0;

  gimple_to_cil_node (csi, GENERIC_TREE_OPERAND (node, 0));

  if (POINTER_TYPE_P (type))
    {
      stmt = cil_build_stmt (CIL_CONV_I);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }

  gimple_to_cil_node (csi, GENERIC_TREE_OPERAND (node, 1));

  if (POINTER_TYPE_P (type))
    {
      stmt = cil_build_stmt (CIL_CONV_I);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }

  if (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
    {
      unsignedp = TYPE_UNSIGNED (type) || POINTER_TYPE_P (type);

      if (size <= 32)
	{
	  if (max)
	    builtin = unsignedp ? CIL32_UMAXSI3 : CIL32_MAXSI3;
	  else
	    builtin = unsignedp ? CIL32_UMINSI3 : CIL32_MINSI3;
	}
      else
	{
	  gcc_assert (size <= 64);

	  if (max)
	    builtin = unsignedp ? CIL32_UMAXDI3 : CIL32_MAXDI3;
	  else
	    builtin = unsignedp ? CIL32_UMINDI3 : CIL32_MINDI3;
	}
    }
  else if (SCALAR_FLOAT_TYPE_P (type))
    {
      if (size == 32)
	builtin = max ? CIL32_MAXSF3 : CIL32_MINSF3;
      else
	{
	  gcc_assert (size == 64);
	  builtin = max ? CIL32_MAXDF3 : CIL32_MINDF3;
	}
    }
  else
    gcc_unreachable ();

  stmt = cil_build_call (cil32_builtins[builtin]);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
}

/* Generates CIL code for an ABS_EXPR held in the tree NODE.  The node is
   either expanded or replaced with a builtin call, depending on the options
   and the optimization level.  The generated statements are appended to the
   current function's CIL code using the CSI iterator.  */

static void
gen_abs_expr (cil_stmt_iterator *csi, tree node)
{
  cil_stmt stmt;
  tree type = TREE_TYPE (node);
  unsigned HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 1);
  enum cil32_builtin builtin = 0;

  gcc_assert (!TARGET_EXPAND_ABS);

  gimple_to_cil_node (csi, GENERIC_TREE_OPERAND (node, 0));

  if (INTEGRAL_TYPE_P (type))
    {
      if (size == 32)
	builtin = CIL32_ABSSI2;
      else
	{
	  gcc_assert (size == 64);
	  builtin = CIL32_ABSDI2;
	}
    }
  else if (SCALAR_FLOAT_TYPE_P (type))
    {
      if (size == 32)
	builtin = CIL32_ABSSF2;
      else
	{
	  gcc_assert (size == 64);
	  builtin = CIL32_ABSDF2;
	}
    }
  else
    gcc_unreachable ();

  stmt = cil_build_call (cil32_builtins[builtin]);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
}

/* Generates CIL code for a VAR_DECL expression held in the tree NODE.  The
   generated statements are appended to the current function's CIL code using
   the CSI iterator.  */

static void
gen_var_decl (cil_stmt_iterator *csi, tree node)
{
  cil_stmt stmt;
  tree type = TREE_TYPE (node);

  mark_referenced_type (type);

  /* Function local static variables are promoted to global variables.  */
  if (!DECL_FILE_SCOPE_P (node) && !TREE_STATIC (node))
    {
      if (TREE_THIS_VOLATILE (node))
	{
	  /* put the address of the loc on the stack */
	  stmt = cil_build_stmt_arg (CIL_LDLOCA, node);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  /* and emit a volatile ldind or ldobj */
	  gen_ldind (csi, type, true);
	}
      else
	{
	  stmt = cil_build_stmt_arg (CIL_LDLOC, node);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}
    }
  else
    {
      stmt = cil_build_stmt_arg (CIL_LDSFLD, node);
      cil_set_prefix_volatile (stmt, TREE_THIS_VOLATILE (node));
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }
}

/* Generates CIL code for a COMPONENT_REF working on a bit field held in the
   tree NODE.  The generated statements are appended to the current function's
   CIL code using the CSI iterator.  */

static void
gen_bit_field_comp_ref (cil_stmt_iterator *csi, tree node)
{
  HOST_WIDE_INT bit_size = 0;
  HOST_WIDE_INT bit_pos = 0;
  HOST_WIDE_INT cont_off;
  HOST_WIDE_INT cont_size = 8;
  tree offset = NULL_TREE;
  enum machine_mode mode;
  int unsignedp = 0;
  int volatilep = 0;
  cil_stmt stmt;
  tree cont_type;
  tree ref;

  /* TODO: Add support for packed bit-fields crossing 64-bit boundaries.
     TODO: Add support for big-endian targets.  */

  /* Get the object base address and emit it.  */
  ref = get_inner_reference (node, &bit_size, &bit_pos, &offset, &mode,
			     &unsignedp, &volatilep, false);

  gen_addr_expr (csi, ref);
  csi_insert_after (csi, cil_build_stmt (CIL_CONV_I), CSI_CONTINUE_LINKING);

  /* Calculate the container size.  */
  while ((bit_pos % cont_size + bit_size) > cont_size)
    cont_size *= 2;

  cont_type = get_integer_type (cont_size, unsignedp);
  cont_off = bit_pos % cont_size;

  /* Calculate the container address if needed.  */
  if ((bit_pos - cont_off) / BITS_PER_UNIT != 0)
    {
      gen_integer_cst (csi,
		       build_int_cst (intSI_type_node,
				      (bit_pos - cont_off) / BITS_PER_UNIT));
      stmt = cil_build_stmt (CIL_ADD);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }

  if (offset != NULL_TREE)
    {
      gimple_to_cil_node (csi, offset);
      stmt = cil_build_stmt (CIL_ADD);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }

  /* Load the container.  */
  gen_scalar_ldind (csi, cont_type, volatilep);

  /* Shift the resulting value into the correct position, zero/sign extending
     it as appropriate. Since the value is now on the stack the container size
     is either 32 or 64.  */
  if (cont_size <= 32)
    cont_size = 32;
  else
    cont_size = 64;

  if (cont_size - (cont_off + bit_size))
    {
      gen_integer_cst (csi,
		       build_int_cst (intSI_type_node,
				      cont_size - (cont_off + bit_size)));
      stmt = cil_build_stmt (CIL_SHL);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }

  if (cont_size - bit_size)
    {
      gen_integer_cst (csi,
		       build_int_cst (intSI_type_node, cont_size - bit_size));
      stmt = cil_build_stmt (unsignedp ? CIL_SHR_UN : CIL_SHR);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }

  if (TYPE_PRECISION (TREE_TYPE (node)) <= 32)
    {
      if (cont_size > 32)
	{
	  stmt = cil_build_stmt (CIL_CONV_I4);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}
    }
  else
    {
      if (cont_size <= 32)
	{
	  stmt = cil_build_stmt (unsignedp ? CIL_CONV_U8 : CIL_CONV_I8);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}
    }
}

/* Generates CIL code for a COMPONENT_REF expression held in the tree NODE.
   The generated statements are appended to the current function's CIL code
   using the CSI iterator.  */

static void
gen_comp_ref (cil_stmt_iterator *csi, tree node)
{
  tree obj = GENERIC_TREE_OPERAND (node, 0);
  tree fld = GENERIC_TREE_OPERAND (node, 1);
  cil_stmt stmt;

  gcc_assert (TREE_CODE (fld) == FIELD_DECL);

  mark_referenced_type (TYPE_MAIN_VARIANT (TREE_TYPE (obj)));

  if (DECL_BIT_FIELD (fld))
    gen_bit_field_comp_ref (csi, node);
  else
    {
      gen_addr_expr (csi, obj);
      stmt = cil_build_stmt_arg (CIL_LDFLD, fld);

      if (contains_packed_reference (node))
	cil_set_prefix_unaligned (stmt, 1);

      cil_set_prefix_volatile (stmt, TREE_THIS_VOLATILE (node));
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }
}

/* Generates CIL code for a BIT_FIELD_REF expression used to access a vector
   element. The generated statements are appended to the current function's CIL
   code using the CSI iterator.  */

static void
gen_vector_bitfield_ref (cil_stmt_iterator *csi, tree node)
{
  cil_stmt stmt;
  tree cst;

  gen_addr_expr (csi, GENERIC_TREE_OPERAND (node, 0));
  csi_insert_after (csi, cil_build_stmt (CIL_CONV_I), CSI_CONTINUE_LINKING);
  cst = size_binop (TRUNC_DIV_EXPR, GENERIC_TREE_OPERAND (node, 2),
		    bitsize_unit_node);

  if (!integer_zerop (cst))
    {
      gen_integer_cst (csi, fold_convert (intSI_type_node, cst));
      stmt = cil_build_stmt (CIL_ADD);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }

  gen_ldind (csi, TREE_TYPE (node), TREE_THIS_VOLATILE (node));
}

/* Generates CIL code for a BIT_FIELD_REF expression held in the tree NODE.
   The generated statements are appended to the current function's CIL code
   using the CSI iterator.  Hopefully this function will go away with
   BIT_FIELD_REFs sooner than later.  */

static void
gen_bit_field_ref (cil_stmt_iterator *csi, tree node)
{
  unsigned HOST_WIDE_INT bit_size = 0;
  unsigned HOST_WIDE_INT bit_pos = 0;
  unsigned HOST_WIDE_INT cont_off;
  unsigned HOST_WIDE_INT cont_size = 8;
  cil_stmt stmt;
  enum cil_opcode opcode;
  tree cont_type;
  tree cst;
  tree offset = GENERIC_TREE_OPERAND (node, 2);

  /* TODO: Add support for big-endian targets.  */
  gen_addr_expr (csi, GENERIC_TREE_OPERAND (node, 0));
  csi_insert_after (csi, cil_build_stmt (CIL_CONV_I), CSI_CONTINUE_LINKING);

  cst = size_binop (TRUNC_DIV_EXPR, offset, bitsize_unit_node);

  if (!integer_zerop (cst))
    {
      gen_integer_cst (csi, fold_convert (intSI_type_node, cst));
      stmt = cil_build_stmt (CIL_ADD);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }

  /* Calculate the container size.  */
  cst = size_binop (TRUNC_MOD_EXPR, offset, bitsize_unit_node);
  bit_pos = tree_low_cst (cst, 1);
  bit_size = tree_low_cst (TREE_OPERAND (node, 1), 1);

  while ((bit_pos % cont_size + bit_size) > cont_size)
    cont_size *= 2;

  cont_type = get_integer_type (cont_size, BIT_FIELD_REF_UNSIGNED (node));
  cont_off = bit_pos % cont_size;

  /* Load the container.  */
  gen_scalar_ldind (csi, cont_type, TREE_THIS_VOLATILE (node));

  /* Shift the resulting value into the correct position, zero extending it.
     Since the value is now on the stack the container size is either 32
     or 64.  */
  if (bit_size != cont_size)
    {
      if (cont_size <= 32)
	cont_size = 32;
      else
	cont_size = 64;

      if (cont_size - (cont_off + bit_size))
	{
	  gen_integer_cst (csi,
			   build_int_cst (intSI_type_node,
					  cont_size - (cont_off + bit_size)));
	  stmt = cil_build_stmt (CIL_SHL);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}

      if (cont_size - bit_size)
	{
	  gen_integer_cst (csi,
			   build_int_cst (intSI_type_node,
					  cont_size - bit_size));
	  opcode = BIT_FIELD_REF_UNSIGNED (node) ? CIL_SHR_UN : CIL_SHR;
	  stmt = cil_build_stmt (opcode);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}
    }
}

/* Generates CIL code for a TRUTH_(AND|OR|XOR)_EXPR expression held in the tree
   NODE.  The generated statements are appended to the current function's CIL
   code using the CSI iterator.  */

static void
gen_truth_expr (cil_stmt_iterator *csi, tree node)
{
  tree op0 = GENERIC_TREE_OPERAND (node, 0);
  tree op1 = GENERIC_TREE_OPERAND (node, 1);
  cil_stmt stmt;

  gimple_to_cil_node (csi, op0);

  if (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE)
    {
      gen_integer_cst (csi, integer_zero_node);
      stmt = cil_build_stmt (CIL_CEQ);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      gen_integer_cst (csi, integer_one_node);
      stmt = cil_build_stmt (CIL_XOR);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }
  else
    gcc_assert (TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE);

  gimple_to_cil_node (csi, op1);

  if (TREE_CODE (TREE_TYPE (op1)) == INTEGER_TYPE)
    {
      gen_integer_cst (csi, integer_zero_node);
      stmt = cil_build_stmt (CIL_CEQ);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      gen_integer_cst (csi, integer_one_node);
      stmt = cil_build_stmt (CIL_XOR);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }
  else
    gcc_assert (TREE_CODE (TREE_TYPE (op1)) == BOOLEAN_TYPE);

  if (TREE_CODE (node) == TRUTH_AND_EXPR)
    {
      stmt = cil_build_stmt (CIL_AND);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }
  else if (TREE_CODE (node) == TRUTH_OR_EXPR)
    {
      stmt = cil_build_stmt (CIL_OR);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }
  else
    {
      stmt = cil_build_stmt (CIL_XOR);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      gen_integer_cst (csi, integer_one_node);
      stmt = cil_build_stmt (CIL_AND);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }
}

/* Generates the address of a TARGET_MEM_REF node specified by NODE and push it
   on the stack. The generated statements are appended to the current
   function's CIL code using the CSI iterator.  */

static void
gen_target_mem_ref (cil_stmt_iterator *csi, tree node)
{
  tree type = TREE_TYPE (node);
  tree ptr_type = build_pointer_type (type);

  gimple_to_cil_node (csi, tree_mem_ref_addr (ptr_type, node));
  gen_ldind (csi, type, TREE_THIS_VOLATILE (node));
}

/* Generates CIL code for a VIEW_CONVERT_EXPR working on the operand held by
   NODE to the destination type DEST_TYPE with either the type of NODE or
   DEST_TYPE or both being vector types.  The generated statements are appended
   to the current function's CIL code using the CSI iterator.  */

static void
gen_vector_view_convert_expr (cil_stmt_iterator *csi, tree dest_type, tree node)
{
  tree src_type = TREE_TYPE (node);
  tree elem_type;
  unsigned HOST_WIDE_INT dest_size = tree_low_cst (TYPE_SIZE (dest_type), 1);
  unsigned HOST_WIDE_INT src_size = tree_low_cst (TYPE_SIZE (src_type), 1);
  unsigned HOST_WIDE_INT elem_size, n_elem;
  bool unsignedp;
  enum cil32_builtin builtin = CIL32_MAX_BUILT_IN; /* Placeholder */
  cil_stmt stmt;

  gimple_to_cil_node (csi, node);

  if (TREE_CODE (src_type) == VECTOR_TYPE)
    {
      /* Convert a vector type to something.  */
      elem_type = TREE_TYPE (src_type);
      elem_size = tree_low_cst (TYPE_SIZE (elem_type), 1);
      n_elem = TYPE_VECTOR_SUBPARTS (src_type);
      unsignedp = TYPE_UNSIGNED (dest_type);

      if (INTEGRAL_TYPE_P (dest_type))
	{
	  if (dest_size == 32 && INTEGRAL_TYPE_P (elem_type))
	    {
	      if (elem_size == 8 && n_elem == 4)
		builtin = unsignedp ? CIL32_GCC_V4QI_TO_USI
				    : CIL32_GCC_V4QI_TO_SI;
	      else if (elem_size == 16 && n_elem == 2)
		builtin = unsignedp ? CIL32_GCC_V2HI_TO_USI
				    : CIL32_GCC_V2HI_TO_SI;
	    }
	  else if (dest_size == 64 && INTEGRAL_TYPE_P (elem_type))
	    {
	      if (elem_size == 8 && n_elem == 8)
		builtin = unsignedp ? CIL32_GCC_V8QI_TO_UDI
				    : CIL32_GCC_V8QI_TO_DI;
	      else if (elem_size == 16 && n_elem == 4)
		builtin = unsignedp ? CIL32_GCC_V4HI_TO_UDI
				    : CIL32_GCC_V4HI_TO_DI;
	      else if (elem_size == 32 && n_elem == 2)
		builtin = unsignedp ? CIL32_GCC_V2SI_TO_UDI
				    : CIL32_GCC_V2SI_TO_DI;
	    }
	  else if (dest_size == 64 && SCALAR_FLOAT_TYPE_P (elem_type))
	    {
	      if (elem_size == 32)
		builtin = CIL32_GCC_V2SF_TO_DI;
	    }
	}
      else if (TREE_CODE (dest_type) == VECTOR_TYPE)
	{
	  if ((INTEGRAL_TYPE_P (elem_type) && elem_size == 32)
	      && (SCALAR_FLOAT_TYPE_P (TREE_TYPE (dest_type))
		  && tree_low_cst (TYPE_SIZE (TREE_TYPE (dest_type)), 1) == 32)
	      && dest_size == src_size)
	    {
	      builtin = CIL32_GCC_V4SI_TO_V4SF;
	    }
	}
    }
  else
    {
      /* Convert something to a vector type */
      elem_type = TREE_TYPE (dest_type);
      elem_size = tree_low_cst (TYPE_SIZE (elem_type), 1);
      n_elem = TYPE_VECTOR_SUBPARTS (dest_type);

      if (INTEGRAL_TYPE_P (src_type))
	{
	  if (src_size == 32)
	    {
	      if (elem_size == 8 && n_elem == 4)
		builtin = CIL32_GCC_V4QI_CTOR2;
	      else if (elem_size == 16 && n_elem == 2)
		builtin = CIL32_GCC_V2HI_CTOR2;
	    }
	  else if (src_size == 64)
	    {
	      if (elem_size == 8 && n_elem == 8)
		builtin = CIL32_GCC_V8QI_CTOR2;
	      else if (elem_size == 16 && n_elem == 4)
		builtin = CIL32_GCC_V4HI_CTOR2;
	      else if (elem_size == 32 && n_elem == 2)
		builtin = CIL32_GCC_V2SI_CTOR2;
	    }
	}
    }

  if (builtin == CIL32_MAX_BUILT_IN)
    {
      internal_error ("Unsupported VIEW_CONVERT_EXPR\n");
    }

  stmt = cil_build_call (cil32_builtins[builtin]);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
  cfun->machine->has_vec = true;
}

/* Generates the code for a vector CONSTRUCTOR node CTOR.  The generated
   statements are appended to the current function's CIL code using the CSI
   iterator.  */

static void
gen_vector_ctor (cil_stmt_iterator *csi, tree ctor)
{
  VEC (constructor_elt, gc) *elts = CONSTRUCTOR_ELTS (ctor);
  unsigned HOST_WIDE_INT i;
  tree purpose, value;
  tree vector_type = TREE_TYPE (ctor);
  cil_stmt stmt;

  FOR_EACH_CONSTRUCTOR_ELT (elts, i, purpose, value)
    gimple_to_cil_node (csi, value);

  stmt = cil_build_stmt_arg (CIL_VEC_CTOR, vector_type);
  csi_insert_after (csi,  stmt, CSI_CONTINUE_LINKING);

  if (cfun)
    cfun->machine->has_vec = true;
}

/* Emit the code needed to generate a REALPART_ or IMAGPART_EXPR expression.  */

static void
gen_complex_part_expr (cil_stmt_iterator *csi, tree node)
{
  cil_stmt stmt;
  tree op0 = GENERIC_TREE_OPERAND (node, 0);
  tree type = TREE_TYPE (op0);

  if (TREE_CODE (op0) == COMPLEX_EXPR)
    {
      /* Get the relevant part immediately */
      if (TREE_CODE (node) == REALPART_EXPR)
	gimple_to_cil_node (csi, GENERIC_TREE_OPERAND (op0, 0));
      else
	gimple_to_cil_node (csi, GENERIC_TREE_OPERAND (op0, 1));
    }
  else
    {
      if (DECL_P (op0)
	  || TREE_CODE (op0) == INDIRECT_REF
	  || TREE_CODE (op0) == ARRAY_REF
	  || TREE_CODE (op0) == COMPONENT_REF)
	{
	  /* Generate the object's address */
	  gen_addr_expr (csi, op0);
	}
      else
	gimple_to_cil_node (csi, op0);

      if (TREE_CODE (node) == REALPART_EXPR)
	{
	  stmt = cil_build_stmt_arg (CIL_LDFLD,
				     cil_get_builtin_complex_real_fld (type));
	}
      else
	{
	  stmt = cil_build_stmt_arg (CIL_LDFLD,
				     cil_get_builtin_complex_imag_fld (type));
	}

      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }
}

/* Emit the code needed for COMPLEX_CST/COMPLEX_EXPR expressions.  */

static void
gen_complex (cil_stmt_iterator *csi, tree type, tree real, tree imag)
{
  tree elem_type = TREE_TYPE (type);
  unsigned HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (elem_type), 1);
  enum cil32_builtin builtin = 0;
  cil_stmt stmt;
  bool unsignedp;

  gimple_to_cil_node (csi, real);
  gimple_to_cil_node (csi, imag);

  if (INTEGRAL_TYPE_P (elem_type))
    {
      unsignedp = TYPE_UNSIGNED (elem_type);

      switch (size)
	{
	case 8:
	  builtin = unsignedp ? CIL32_CPLX_UCHAR_CTOR : CIL32_CPLX_CHAR_CTOR;
	  break;

	case 16:
	  builtin = unsignedp ? CIL32_CPLX_USHORT_CTOR : CIL32_CPLX_SHORT_CTOR;
	  break;

	case 32:
	  builtin = unsignedp ? CIL32_CPLX_UINT_CTOR : CIL32_CPLX_INT_CTOR;
	  break;

	case 64:
	  builtin = unsignedp ? CIL32_CPLX_ULONG_CTOR : CIL32_CPLX_LONG_CTOR;
	  break;

	default:
	  gcc_unreachable ();
	}
    }
  else
    {
      gcc_assert (SCALAR_FLOAT_TYPE_P (elem_type)
		  && ((size == 32) || (size == 64)));

      if (size == 32)
	builtin = CIL32_CPLX_FLOAT_CTOR;
      else
	builtin = CIL32_CPLX_DOUBLE_CTOR;
    }

  stmt = cil_build_call (cil32_builtins[builtin]);
  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
}

/* Return the opcode associated with the conversion to the TYPE type.  */

static enum cil_opcode
conv_opcode_from_type (tree type)
{
  unsigned HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 1);
  bool unsignedp;

  if (INTEGRAL_TYPE_P (type))
    {
      unsignedp = TYPE_UNSIGNED (type);

      switch (size)
	{
	case 8:  return (unsignedp ? CIL_CONV_U1 : CIL_CONV_I1);
	case 16: return (unsignedp ? CIL_CONV_U2 : CIL_CONV_I2);
	case 32: return (unsignedp ? CIL_CONV_U4 : CIL_CONV_I4);
	case 64: return (unsignedp ? CIL_CONV_U8 : CIL_CONV_I8);
	default:
	  gcc_unreachable ();
	}
    }
  else if (POINTER_TYPE_P (type))
    return CIL_CONV_I;
  else if (SCALAR_FLOAT_TYPE_P (type))
    {
      if (size == 32)
	return CIL_CONV_R4;
      else
	{
	  gcc_assert (size == 64);
	  return CIL_CONV_R8;
	}
    }
  else
    gcc_unreachable ();
}

/* Emit a conversion from integral or pointer type SRC to integral type DST.
   If the precision of DST is bigger than that of SRC, then SRC and DST have
   to have the same signedness.   */

static void
gen_integral_conv (cil_stmt_iterator *csi, tree dst, tree src)
{
  unsigned int dst_bits, cont_size, src_bits;
  enum cil_opcode opcode;
  cil_stmt stmt;
  tree type;

  gcc_assert (INTEGRAL_TYPE_P (dst));
  gcc_assert (INTEGRAL_TYPE_P (src) || POINTER_TYPE_P (src));
  gcc_assert (TYPE_PRECISION (dst) <= 64);
  gcc_assert (TYPE_PRECISION (dst) <= TYPE_PRECISION (src)
	      || TYPE_UNSIGNED (dst) == TYPE_UNSIGNED (src));

  /* Get the precision of the output and input types and the size
     of the output type container */
  src_bits = TYPE_PRECISION (src);
  dst_bits = TYPE_PRECISION (dst);
  cont_size = GET_MODE_BITSIZE (TYPE_MODE (dst));
  gcc_assert (cont_size >= dst_bits);

  /* Dump a conv with for the container size, if not superfluous */
  if ((cont_size == dst_bits && (dst_bits != src_bits || dst_bits < 32))
      || ((dst_bits > 32) ^ (src_bits > 32)))
    {
      opcode = conv_opcode_from_type (get_integer_type (cont_size,
							TYPE_UNSIGNED (dst)));
      stmt = cil_build_stmt (opcode);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }

  /* If the container is bigger than the output type precision,
     force the output to be of the desired precision.   */
  if (cont_size > dst_bits)
    {
      type = (dst_bits <= 32) ? intSI_type_node : intDI_type_node;

      if (TYPE_UNSIGNED (dst))
	{
	  tree mask;

	  mask = size_binop (LSHIFT_EXPR, build_int_cst (type, 1),
			     build_int_cst (type, dst_bits));
	  mask = size_binop (MINUS_EXPR, mask, build_int_cst (type, 1));
	  gen_integer_cst (csi, mask);
	  stmt = cil_build_stmt (CIL_AND);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}
      else
	{
	  tree shift;

	  if (dst_bits <= 32)
	    shift = build_int_cst (intSI_type_node, 32 - dst_bits);
	  else
	    shift = build_int_cst (intSI_type_node, 64 - dst_bits);

	  /* Do a pair of shift to perform the sign extension */
	  gen_integer_cst (csi, shift);
	  stmt = cil_build_stmt (CIL_SHL);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  gen_integer_cst (csi, shift);
	  stmt = cil_build_stmt (CIL_SHR);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}
    }
}

/* Emit a conversion from type IN_TYPE to type OUT_TYPE to file FILE.
   IS_NOP says whether the conversion comes from a NOP_EXPR.   */

static void
gen_conv (cil_stmt_iterator *csi, bool is_nop, tree dst, tree src)
{
  cil_stmt stmt;

  if (is_nop && INTEGRAL_TYPE_P (dst) && INTEGRAL_TYPE_P (src))
    {
      if (TYPE_PRECISION (dst) > TYPE_PRECISION (src))
	{
	  tree tmp = TYPE_UNSIGNED (src)
		     ? unsigned_type_for (dst)
		     : signed_type_for (dst);

	  gen_integral_conv (csi, tmp, src);
	  gen_integral_conv (csi, dst, tmp);
	}
      else
	gen_integral_conv (csi, dst, src);
    }

  /* Special case for conversion to float type are not orthogonal
     in CIL opcode set.   */
  else if (SCALAR_FLOAT_TYPE_P (dst)
	   && INTEGRAL_TYPE_P (src)
	   && TYPE_UNSIGNED (src))
    {
      stmt = cil_build_stmt (CIL_CONV_R_UN);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

      if (TYPE_PRECISION (dst) <= 32)
	{
	  stmt = cil_build_stmt (CIL_CONV_R4);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}
    }

  /* Do nothing for a conversion from two REAL_TYPEs with the
     same precision or two pointers.   */
  else if (!SCALAR_FLOAT_TYPE_P (dst)
	   || !SCALAR_FLOAT_TYPE_P (src)
	   || TYPE_PRECISION (dst) != TYPE_PRECISION (src))
    {
      stmt = cil_build_stmt (conv_opcode_from_type (dst));
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
    }
}

/* Generates the equivalent CIL code for rotate expressions. Since rotations
   are not available in CIL they are emulated using shifts.  */

static void
gen_rotate (cil_stmt_iterator *csi, tree node)
{
  bool left = (TREE_CODE (node) == LROTATE_EXPR);
  tree op0, op1;
  tree t1, t2;
  tree uns_type;

  /* Rotation is replaced by shifts on unsigned values:
     generate the unsigned version of first operand type.  */
  op0 = GENERIC_TREE_OPERAND (node, 0);
  uns_type = unsigned_type_for (TREE_TYPE (op0));
  op0 = fold_convert (uns_type, op0);

  /* Convert the second operand to 32-bit.  */
  op1 = fold_convert (intSI_type_node, GENERIC_TREE_OPERAND (node, 1));

  /* Build first shift.  */
  t1 = fold_build2 (left ? LSHIFT_EXPR : RSHIFT_EXPR, uns_type, op0, op1);

  /* Build second shift.  */
  t2 = fold_build2 (left ? RSHIFT_EXPR : LSHIFT_EXPR, uns_type,
		    op0,
		    fold_build2 (MINUS_EXPR, unsigned_intSI_type_node,
				 fold_convert (unsigned_intSI_type_node,
					       TYPE_SIZE (TREE_TYPE (op0))),
				 op1));

  /* Build the rotate result.  We do not use fold_build2() as it would
     recreate the *ROTATE_EXPR.  */
  t1 = build2 (BIT_IOR_EXPR, uns_type, t1, t2);
  t1 = fold_convert (TREE_TYPE (TREE_OPERAND (node, 0)), t1);

  /* Generate the code */
  gimple_to_cil_node (csi, t1);
}

/* Converts a GIMPLE/generic node into its CIL form. The generated statements
 * are appended to the current function's CIL code using the CSI iterator.  */

static void
gimple_to_cil_node (cil_stmt_iterator *csi, tree node)
{
  tree op0, op1;
  cil_stmt stmt = NULL;
  enum cil_opcode opcode;
  bool uns;

  if (node == NULL_TREE || node == error_mark_node)
    return;

  switch (TREE_CODE (node))
    {
    case INTEGER_CST:
      gen_integer_cst (csi, node);

      if (POINTER_TYPE_P (TREE_TYPE (node)))
	{
	  stmt = cil_build_stmt (CIL_CONV_I);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	}
      break;

    case REAL_CST:
      gen_real_cst (csi, node);
      break;

    case COMPLEX_CST:
      gen_complex (csi, TREE_TYPE (node),
		   TREE_REALPART (node),
		   TREE_IMAGPART (node));
      break;

    case CONSTRUCTOR:
      if (TREE_CODE (TREE_TYPE (node)) == VECTOR_TYPE)
	gen_vector_ctor (csi, node);
      else
	internal_error ("Unsupported type in CONSTRUCTOR\n");

      break;

    case VECTOR_CST:
      gen_vector_cst (csi, node);
      break;

    case LABEL_DECL:
      gcc_unreachable ();
      break;

    case COMPOUND_LITERAL_EXPR:
      /* HACK: We should find a way to avoid front-end nodes */
      gimple_to_cil_node (csi, COMPOUND_LITERAL_EXPR_DECL (node));
      break;

    case INIT_EXPR:
    case MODIFY_EXPR:
    case GIMPLE_MODIFY_STMT:
      op0 = GENERIC_TREE_OPERAND (node, 0);
      op1 = GENERIC_TREE_OPERAND (node, 1);

      if ((TREE_CODE (op1) == CONSTRUCTOR &&
	   (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE)) ||
	  TREE_CODE (op1) == STRING_CST)
	{
	  csi_insert_seq_after (csi, expand_init_to_cil_seq (op0, op1),
				CSI_CONTINUE_LINKING);
	}
      else
	gen_modify_expr (csi, op0, op1);

      break;

    case GOTO_EXPR:
      internal_error ("GOTO_EXPRs shouldn't appear inside other trees or "
		      "before the end of a basic block\n");
      break;

    case COND_EXPR:
      {
	/* HACK: COND_EXPRs shouldn't appear here without proper vector
	   support, we should either implement proper vector support in
	   builtin-calls form or remove it alltogether.  */
	tree type = TREE_TYPE (node);
	unsigned HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 1);
	enum cil32_builtin builtin;

	if (INTEGRAL_TYPE_P (type))
	  {
	    if (size <= 32)
	      builtin = CIL32_SELECTSI4;
	    else
	      builtin = CIL32_SELECTDI4;
	  }
	else if (SCALAR_FLOAT_TYPE_P (type))
	  {
	    if (size <= 32)
	      builtin = CIL32_SELECTSF4;
	    else
	      builtin = CIL32_SELECTDF4;
	  }
	else
	  gcc_unreachable ();

	gimple_to_cil_node (csi, COND_EXPR_COND (node));
	gimple_to_cil_node (csi, COND_EXPR_THEN (node));
	gimple_to_cil_node (csi, COND_EXPR_ELSE (node));
	stmt = cil_build_call (cil32_builtins[builtin]);
	csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      }

      break;

    case SWITCH_EXPR:
      internal_error ("SWITCH_EXPRs shouldn't appear inside other trees or "
		      "before the end of a basic block\n");
      break;

    case CALL_EXPR:
      gen_call_expr (csi, node);
      break;

    case MULT_EXPR:
    case PLUS_EXPR:
    case POINTER_PLUS_EXPR:
    case MINUS_EXPR:
    case RDIV_EXPR:
    case LSHIFT_EXPR:
      op0 = GENERIC_TREE_OPERAND (node, 0);
      op1 = GENERIC_TREE_OPERAND (node, 1);

      gimple_to_cil_node (csi, op0);

      if (TREE_CODE (node) == LSHIFT_EXPR)
	gimple_to_cil_node (csi, fold_convert (intSI_type_node, op1));
      else
	gimple_to_cil_node (csi, op1);

      switch (TREE_CODE (node))
	{
	case MULT_EXPR:         opcode = CIL_MUL; break;
	case POINTER_PLUS_EXPR:
	case PLUS_EXPR:         opcode = CIL_ADD; break;
	case MINUS_EXPR:        opcode = CIL_SUB; break;
	case RDIV_EXPR:         opcode = CIL_DIV; break;
	case LSHIFT_EXPR:       opcode = CIL_SHL; break;
	default:
	  gcc_unreachable ();
	}

      stmt = cil_build_stmt (opcode);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

      /* Values with precision smaller than the one used
	 on the evaluation stack require an explicit conversion.   */
      if (INTEGRAL_TYPE_P (TREE_TYPE (node)))
	gen_integral_conv (csi, TREE_TYPE (node), TREE_TYPE (node));
      break;

    case BIT_IOR_EXPR:
    case BIT_XOR_EXPR:
      op0 = GENERIC_TREE_OPERAND (node, 0);
      op1 = GENERIC_TREE_OPERAND (node, 1);

      gimple_to_cil_node (csi, op0);
      gimple_to_cil_node (csi, op1);

      switch (TREE_CODE (node))
	{
	case BIT_IOR_EXPR: opcode = CIL_OR;  break;
	case BIT_XOR_EXPR: opcode = CIL_XOR; break;
	default:
	  gcc_unreachable ();
	}

      /* No need for conversions even in case of values with precision
	 smaller than the one used on the evaluation stack,
	 since for these operations the output is
	 always less or equal than both operands.   */

      stmt = cil_build_stmt (opcode);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      break;

    case BIT_AND_EXPR:
      gen_bit_and_expr (csi, node);
      break;

    case LT_EXPR:
    case LE_EXPR:
    case GT_EXPR:
    case GE_EXPR:
    case EQ_EXPR:
    case NE_EXPR:
    case UNORDERED_EXPR:
    case ORDERED_EXPR:
    case UNLT_EXPR:
    case UNLE_EXPR:
    case UNGT_EXPR:
    case UNGE_EXPR:
    case UNEQ_EXPR:
    case LTGT_EXPR:
      gen_compare_expr (csi, node);
      break;

    case EXACT_DIV_EXPR:
    case TRUNC_DIV_EXPR:
    case TRUNC_MOD_EXPR:
    case RSHIFT_EXPR:
      op0 = GENERIC_TREE_OPERAND (node, 0);
      op1 = GENERIC_TREE_OPERAND (node, 1);
      uns = TYPE_UNSIGNED (TREE_TYPE (node));

      gimple_to_cil_node (csi, op0);

      if (TREE_CODE (node) == RSHIFT_EXPR)
	gimple_to_cil_node (csi, fold_convert (intSI_type_node, op1));
      else
	gimple_to_cil_node (csi, op1);

      switch (TREE_CODE (node))
	{
	case EXACT_DIV_EXPR:
	case TRUNC_DIV_EXPR: opcode = uns ? CIL_DIV_UN : CIL_DIV; break;
	case TRUNC_MOD_EXPR: opcode = uns ? CIL_REM_UN : CIL_REM; break;
	case RSHIFT_EXPR:    opcode = uns ? CIL_SHR_UN : CIL_SHR; break;
	default:
	  gcc_unreachable ();
	}

      stmt = cil_build_stmt (opcode);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

      /* No need for conversions even in case of values with precision
	 smaller than the one used on the evaluation stack,
	 since for these operations the output is
	 always less or equal than both operands.   */
      break;

    case LROTATE_EXPR:
    case RROTATE_EXPR:
      gen_rotate (csi, node);
      break;

    case FLOOR_DIV_EXPR:
      {
	bool is_signed0, is_signed1;

	op0 = GENERIC_TREE_OPERAND (node, 0);
	op1 = GENERIC_TREE_OPERAND (node, 1);

	gimple_to_cil_node (csi, op0);
	gimple_to_cil_node (csi, op1);

	is_signed0 = TYPE_UNSIGNED (TREE_TYPE (op0));
	is_signed1 = TYPE_UNSIGNED (TREE_TYPE (op1));
	/* If both operands are unsigned, the result is positive and thus
	   rounding towards zero is identical to towards -infinity.  */
	if (is_signed0 && is_signed1)
	  {
	    stmt = cil_build_stmt (CIL_DIV_UN);
	    csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
	  }
	else
	  internal_error ("\n\nFLOOR_DIV_EXPR is not completely supported\n");

	/* No need for conversions even in case of values with precision
	   smaller than the one used on the evaluation stack,
	   since for these operations the output is
	   always less or equal than both operands.   */
	break;
      }

    case NEGATE_EXPR:
    case BIT_NOT_EXPR:
      gimple_to_cil_node (csi, GENERIC_TREE_OPERAND (node, 0));

      if (POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (node, 0))))
	{
	  csi_insert_after (csi, cil_build_stmt (CIL_CONV_I),
			    CSI_CONTINUE_LINKING);
	}

      opcode = (TREE_CODE (node) == NEGATE_EXPR) ? CIL_NEG : CIL_NOT;
      stmt = cil_build_stmt (opcode);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

      /* Values with precision smaller than the one used
	 on the evaluation stack require an explicit conversion.
	 Unfortunately this is true for the negation as well just
	 for the case in which the operand is the smallest negative value.
	 Example: 8-bit negation of -128 gives 0 and not 128.   */
      if (INTEGRAL_TYPE_P (TREE_TYPE (node)))
	gen_integral_conv (csi, TREE_TYPE (node), TREE_TYPE (node));
      break;

    case ARRAY_REF:
    case INDIRECT_REF:
      gen_addr_expr (csi, node);
      gen_ldind (csi, TREE_TYPE (node), TREE_THIS_VOLATILE (node));
      break;

    case MISALIGNED_INDIRECT_REF:
      gen_addr_expr (csi, node);
      gen_misaligned_ldvec (csi, TREE_TYPE (node), TREE_THIS_VOLATILE (node));
      break;

    case TARGET_MEM_REF:
      gen_target_mem_ref (csi, node);
      break;

    case CONVERT_EXPR:
    case FLOAT_EXPR:
      /* TODO: if flag_trapv is set, we could generate the .ovf version? */
    case FIX_TRUNC_EXPR:
    case NOP_EXPR:
      {
	tree type;

	op0 = GENERIC_TREE_OPERAND (node, 0);
	gimple_to_cil_node (csi, op0);

	/* Temporaries with weird types are handled correctly without need
	   for an explicit conversion as they have already been promoted.  */
	if ((TREE_CODE (node) == NOP_EXPR) && (TREE_CODE (op0) == VAR_DECL))
	  type = promote_local_var_type (op0);
	else
	  type = TREE_TYPE (op0);

	gen_conv (csi, TREE_CODE (node) == NOP_EXPR, TREE_TYPE (node), type);
      }
      break;

    case LABEL_EXPR:
      /* Skip this expression, labels are emitted later. TODO: Check that the
	 labels appear only at the beginning of a basic-block?  */
      break;

    case RETURN_EXPR:
      op0 = GENERIC_TREE_OPERAND (node, 0);

      if (op0 != NULL_TREE)
	{
	  if (TREE_CODE (op0) == MODIFY_EXPR
	      || TREE_CODE (op0) == GIMPLE_MODIFY_STMT)
	    op0 = GENERIC_TREE_OPERAND (op0, 1);

	  gimple_to_cil_node (csi, op0);
	}
      else if (!VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
	{
	  /* Pre-C99 code may contain void-returns for non-void functions.
	     In this case, return an artificially generated result variable.  */
	  tree res_type = TREE_TYPE (TREE_TYPE (current_function_decl));

	  if (TYPE_SIZE (res_type) != NULL_TREE
	      && TREE_CODE (TYPE_SIZE (res_type)) != INTEGER_CST)
	    {
	      internal_error ("Returned type cannot be a variable size array or"
			      " struct\n");
	    }

	  if (res_var == NULL_TREE)
	    res_var = create_tmp_var (res_type, "gimple2cil");

	  stmt = cil_build_stmt_arg (CIL_LDLOC, res_var);
	  csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

	  /* Flag the function so that the emission phase will emit an 'init'
	     directive in the local variables declaration.  */
	  cfun->machine->locals_init = true;
	}

      stmt = cil_build_stmt (CIL_RET);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      break;

    case ASM_EXPR:
      /* TODO: support just a simple string, no input/output/clober */
      stmt = cil_build_stmt_arg (CIL_ASM, ASM_STRING (node));
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      break;

    case MAX_EXPR:
    case MIN_EXPR:
      gen_minmax_expr (csi, node);
      break;

    case ABS_EXPR:
      gen_abs_expr (csi, node);
      break;

    case SSA_NAME:
      gcc_unreachable ();

    case VAR_DECL:
    case RESULT_DECL:
      gen_var_decl (csi, node);
      break;

    case PARM_DECL:
      {
	tree type = TREE_TYPE (node);
	mark_referenced_type (type);
	stmt = cil_build_stmt_arg (CIL_LDARG, node);
	csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);

	if (TREE_CODE (type) == VECTOR_TYPE)
	  cfun->machine->has_vec = true;
      }
      break;

    case FIELD_DECL:
    case NAMESPACE_DECL:
      internal_error ("CIL: Cannot handle FIELD_DECL or NAMESPACE_DECL");
      break;

    case TREE_LIST:
      gcc_unreachable ();
      break;

    case FUNCTION_DECL:
    case CONST_DECL:
      gcc_unreachable ();
      break;

    case ADDR_EXPR:
      gen_addr_expr (csi, GENERIC_TREE_OPERAND (node, 0));
      break;

    case COMPONENT_REF:
      gen_comp_ref (csi, node);
      break;

    case TRUTH_NOT_EXPR:
      gimple_to_cil_node (csi, GENERIC_TREE_OPERAND (node, 0));
      gen_integer_cst (csi, integer_zero_node);
      stmt = cil_build_stmt (CIL_CEQ);
      csi_insert_after (csi, stmt, CSI_CONTINUE_LINKING);
      break;

    case TRUTH_AND_EXPR:
    case TRUTH_OR_EXPR:
    case TRUTH_XOR_EXPR:
      gen_truth_expr (csi, node);
      break;

    case VIEW_CONVERT_EXPR:
      {
	/* VIEW_CONVERT_EXPRs may be redundant, check the type of the innermost
	   object and emit code only if it differs from the topmost conversion.
	 */
	tree op0 = GENERIC_TREE_OPERAND (node, 0);
	tree dest_type = TREE_TYPE (node);
	tree src_type;

	while (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
	  op0 = GENERIC_TREE_OPERAND (op0, 0);

	src_type = TREE_TYPE (op0);

	if (src_type == dest_type)
	  gimple_to_cil_node (csi, op0);
	else if ((TREE_CODE (src_type) == VECTOR_TYPE)
		 || (TREE_CODE (dest_type) == VECTOR_TYPE))
	  {
	    gen_vector_view_convert_expr (csi, dest_type, op0);
	  }
	else
	  {
	    gen_addr_expr (csi, node);
	    gen_ldind (csi, dest_type, TREE_THIS_VOLATILE (node));
	  }
      }
      break;

    case REALPART_EXPR:
    case IMAGPART_EXPR:
      gen_complex_part_expr (csi, node);
      break;

    case COMPLEX_EXPR:
      gen_complex (csi, TREE_TYPE (node),
		   GENERIC_TREE_OPERAND (node, 0),
		   GENERIC_TREE_OPERAND (node, 1));
      break;

    case BIT_FIELD_REF:
      if (TREE_CODE (TREE_TYPE (GENERIC_TREE_OPERAND (node, 0))) == VECTOR_TYPE)
	gen_vector_bitfield_ref (csi, node);
      else
	gen_bit_field_ref (csi, node);

      break;

    case ENUMERAL_TYPE:
    case ARRAY_TYPE:
    case RECORD_TYPE:
    case UNION_TYPE:
    case QUAL_UNION_TYPE:
    case VOID_TYPE:
    case INTEGER_TYPE:
    case REAL_TYPE:
    case COMPLEX_TYPE:
    case VECTOR_TYPE:
    case BOOLEAN_TYPE:
    case POINTER_TYPE:
    case REFERENCE_TYPE:
      internal_error ("gen_cil_node does not support TYPE nodes, "
		      "to dump Type name use dump_type.\n");
      break;

    default:
      internal_error ("\n\nUnsupported tree in CIL generation: '%s'\n",
		      tree_code_name[TREE_CODE (node)]);
      break;
    }
}

/* Records the addresses whose labels have been taken and generate the
   appropriate switch labels to emulate computed GOTOs.  Also ensure that all
   basic blocks are properly labeled.  */

static void
process_labels (void)
{
  basic_block bb;
  block_stmt_iterator bsi;
  tree stmt;

  /* Record all the labels whose address has been taken */
  FOR_EACH_BB (bb)
    {
      for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
	{
	  stmt = bsi_stmt (bsi);

	  /* Record the address taken labels.  */
	  if (TREE_CODE (stmt) == LABEL_EXPR)
	    {
	      tree label = LABEL_EXPR_LABEL (stmt);
	      /* Check if the label has its address taken.  */
	      if (FORCED_LABEL (label))
		record_addr_taken_label (label);
	    }
	}
    }

  /* Make sure that every bb has a label */
  FOR_EACH_BB (bb)
    {
      tree_block_label (bb);
    }
}

/* Looks for non structured types initializers specified as DECL_INIT
   expressions attached to the declarations and turn them into a list of CIL
   statements. The generated list is prepended to the instructions in the first
   basic block of the current function.  */

static void
process_initializers (void)
{
  cil_seq seq = cil_seq_alloc ();
  cil_stmt_iterator csi = csi_start (seq);
  cil_stmt_iterator bb_csi = csi_start_bb (single_succ (ENTRY_BLOCK_PTR));
  tree cell;

  for (cell = cfun->unexpanded_var_list; cell; cell = TREE_CHAIN (cell))
    {
      tree var = TREE_VALUE (cell);
      tree init = DECL_INITIAL (var);

      if (!TREE_STATIC (var) && init && init != error_mark_node)
	{
	  csi_insert_seq_after (&csi, expand_init_to_cil_seq (var, init),
				CSI_CONTINUE_LINKING);
	}
    }

  csi_insert_seq_before (&bb_csi, seq, CSI_SAME_STMT);
}

/* Converts the GIMPLE/generic code of the current function in the CIL
   intermediate representation */

static unsigned int
gimple_to_cil (void)
{
  basic_block bb;
  block_stmt_iterator bsi;
  cil_stmt stmt;
  cil_seq seq;
  cil_stmt_iterator csi, prev_csi;
  tree node = NULL_TREE;

  /* Initialization */
  refs_init ();
  res_var = NULL_TREE;

  /* Preprocessing */
  process_labels ();

  FOR_EACH_BB (bb)
    {
      seq = cil_seq_alloc ();
      cil_set_bb_seq (bb, seq);
      csi = csi_start_bb (bb);

      for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
	{
	  node = bsi_stmt (bsi);
	  prev_csi = csi;

	  switch (TREE_CODE (node))
	    {
	    case CALL_EXPR:
	      {
		tree fun_expr = CALL_EXPR_FN (node);
		tree fun_type = TREE_TYPE (TREE_TYPE (fun_expr));

		gen_call_expr (&csi, node);

		if (!VOID_TYPE_P (TREE_TYPE (fun_type)))
		  {
		    csi_insert_after (&csi, cil_build_stmt (CIL_POP),
				      CSI_CONTINUE_LINKING);
		  }
	      }
	      break;

	    case GOTO_EXPR:
	      gcc_assert (bsi_stmt (bsi_last (bb)) == node);
	      gen_goto_expr (&csi, node);
	      break;

	    case COND_EXPR:
	      gcc_assert (bsi_stmt (bsi_last (bb)) == node);
	      gen_cond_expr (&csi, node);
	      break;

	    case SWITCH_EXPR:
	      gcc_assert (bsi_stmt (bsi_last (bb)) == node);
	      gen_switch_expr (&csi, node);
	      break;

	    default:
	      if (TREE_CODE (node) != NOP_EXPR
		  || TREE_CODE (TREE_OPERAND (node, 0)) != INTEGER_CST)
		{
		  gimple_to_cil_node (&csi, node);
		}
	    }

	  for (; !csi_end_p (prev_csi); csi_next (&prev_csi))
	    cil_set_locus (csi_stmt (prev_csi), EXPR_LOCATION (node));
	}

      if ((!node || (TREE_CODE (node) != COND_EXPR)) && single_succ_p (bb))
	{
	  basic_block succ = single_succ (bb);

	  /* The last part of the test (succ != bb->next_bb) is a HACK.  It
	     avoids generating a branch to the successor in case of a
	     fallthrough. To be fixed when we have a proper layout of basic
	     blocks.   */
	  if ((succ->index != EXIT_BLOCK) && (succ != bb->next_bb))
	    {
	      tree label = tree_block_label (succ);

	      stmt = cil_build_stmt_arg (CIL_BR, label);
	      cil_set_locus (stmt,
			     node ? EXPR_LOCATION (node) : UNKNOWN_LOCATION);
	      csi_insert_after (&csi, stmt, CSI_CONTINUE_LINKING);
	    }
	}
      else if (EDGE_COUNT (bb->succs) == 0)
	{
	  bsi = bsi_last (bb);
	  node = bsi_stmt (bsi);

	  if (TREE_CODE (node) != RETURN_EXPR)
	    {
	      tree ret_type = TREE_TYPE (TREE_TYPE (current_function_decl));

	      if (!VOID_TYPE_P (ret_type) && res_var == NULL_TREE)
		res_var = create_tmp_var (ret_type, "gimple2cil");

	      if (res_var != NULL_TREE)
		{
		  stmt = cil_build_stmt_arg (CIL_LDLOC, res_var);
		  csi_insert_after (&csi, stmt, CSI_CONTINUE_LINKING);
		}

	      stmt = cil_build_stmt (CIL_RET);
	      csi_insert_after (&csi, stmt, CSI_CONTINUE_LINKING);

	      /* Flag the function so that the emission phase will emit an
		 'init' directive in the local variables declaration.  */
	      cfun->machine->locals_init = true;

	      /* FIXME: Is this really needed? */
	      make_single_succ_edge (bb, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
	    }
	}
    }

  /* Add the initializers to the entry block */
  process_initializers ();

  return 0;
}

/* Gate function of GIMPLE/generic-to-CIL conversion.  */

static bool
gimple_to_cil_gate (void)
{
  return current_function_decl != NULL;
}

/* Define the parameters of the tree-final-simp-CIL pass.  */

struct tree_opt_pass pass_gimple_to_cil =
{
  "gimple2cil",                         /* name */
  gimple_to_cil_gate,                   /* gate */
  gimple_to_cil,                        /* execute */
  NULL,                                 /* sub */
  NULL,                                 /* next */
  0,                                    /* static_pass_number */
  TV_GIMPLE_TO_CIL,                     /* tv_id */
  PROP_cfg,                             /* properties_required */
  0,                                    /* properties_provided */
  0,                                    /* properties_destroyed */
  0,
  TODO_ggc_collect,                     /* todo_flags_finish */
  0                                     /* letter */
};

/*
 * Local variables:
 * eval: (c-set-style "gnu")
 * End:
 */