aboutsummaryrefslogtreecommitdiffstats
path: root/linearize.c
blob: d9aed61b361f9355f56018a061be1c8490e5039f (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
/*
 * Linearize - walk the statement tree (but _not_ the expressions)
 * to generate a linear version of it and the basic blocks. 
 *
 * NOTE! We're not interested in the actual sub-expressions yet,
 * even though they can generate conditional branches and
 * subroutine calls. That's all "local" behaviour.
 *
 * Copyright (C) 2004 Linus Torvalds
 * Copyright (C) 2004 Christopher Li
 */

#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

#include "parse.h"
#include "expression.h"
#include "linearize.h"
#include "optimize.h"
#include "flow.h"
#include "target.h"

static pseudo_t linearize_statement(struct entrypoint *ep, struct statement *stmt);
static pseudo_t linearize_expression(struct entrypoint *ep, struct expression *expr);

static pseudo_t add_cast(struct entrypoint *ep, struct symbol *to, struct symbol *from, int op, pseudo_t src);
static pseudo_t add_binary_op(struct entrypoint *ep, struct symbol *ctype, int op, pseudo_t left, pseudo_t right);
static pseudo_t linearize_one_symbol(struct entrypoint *ep, struct symbol *sym);

static pseudo_t cast_pseudo(struct entrypoint *ep, pseudo_t src, struct symbol *from, struct symbol *to);

struct pseudo void_pseudo = {};

static struct position current_pos;

ALLOCATOR(pseudo_user, "pseudo_user");

static struct instruction *alloc_instruction(int opcode, int size)
{
	struct instruction * insn = __alloc_instruction(0);
	insn->opcode = opcode;
	insn->size = size;
	insn->pos = current_pos;
	return insn;
}

static inline int type_size(struct symbol *type)
{
	return type ? type->bit_size > 0 ? type->bit_size : 0 : 0;
}

static struct instruction *alloc_typed_instruction(int opcode, struct symbol *type)
{
	struct instruction *insn = alloc_instruction(opcode, type_size(type));
	insn->type = type;
	return insn;
}

static struct entrypoint *alloc_entrypoint(void)
{
	return __alloc_entrypoint(0);
}

static struct basic_block *alloc_basic_block(struct entrypoint *ep, struct position pos)
{
	static int nr;
	struct basic_block *bb = __alloc_basic_block(0);
	bb->pos = pos;
	bb->ep = ep;
	bb->nr = nr++;
	return bb;
}

static struct multijmp *alloc_multijmp(struct basic_block *target, long long begin, long long end)
{
	struct multijmp *multijmp = __alloc_multijmp(0);
	multijmp->target = target;
	multijmp->begin = begin;
	multijmp->end = end;
	return multijmp;
}

const char *show_label(struct basic_block *bb)
{
	static int n;
	static char buffer[4][16];
	char *buf = buffer[3 & ++n];

	if (!bb)
		return ".L???";
	snprintf(buf, 64, ".L%u", bb->nr);
	return buf;
}

const char *show_pseudo(pseudo_t pseudo)
{
	static int n;
	static char buffer[4][64];
	char *buf;
	int i;

	if (!pseudo)
		return "no pseudo";
	if (pseudo == VOID)
		return "VOID";
	buf = buffer[3 & ++n];
	switch(pseudo->type) {
	case PSEUDO_SYM: {
		struct symbol *sym = pseudo->sym;
		struct expression *expr;

		if (!sym) {
			snprintf(buf, 64, "<bad symbol>");
			break;
		}
		if (sym->bb_target) {
			snprintf(buf, 64, "%s", show_label(sym->bb_target));
			break;
		}
		if (sym->ident) {
			snprintf(buf, 64, "%s", show_ident(sym->ident));
			break;
		}
		expr = sym->initializer;
		snprintf(buf, 64, "<anon symbol:%p>", verbose ? sym : NULL);
		if (expr) {
			switch (expr->type) {
			case EXPR_VALUE:
				snprintf(buf, 64, "<symbol value: %lld>", expr->value);
				break;
			case EXPR_STRING:
				return show_string(expr->string);
			default:
				break;
			}
		}
		break;
	}
	case PSEUDO_REG:
		i = snprintf(buf, 64, "%%r%d", pseudo->nr);
		if (pseudo->ident)
			sprintf(buf+i, "(%s)", show_ident(pseudo->ident));
		break;
	case PSEUDO_VAL: {
		long long value = pseudo->value;
		if (value > 1000 || value < -1000)
			snprintf(buf, 64, "$%#llx", value);
		else
			snprintf(buf, 64, "$%lld", value);
		break;
	}
	case PSEUDO_ARG:
		snprintf(buf, 64, "%%arg%d", pseudo->nr);
		break;
	case PSEUDO_PHI:
		i = snprintf(buf, 64, "%%phi%d", pseudo->nr);
		if (pseudo->ident)
			sprintf(buf+i, "(%s)", show_ident(pseudo->ident));
		break;
	case PSEUDO_UNDEF:
		return "UNDEF";
	default:
		snprintf(buf, 64, "<bad pseudo type %d>", pseudo->type);
	}
	return buf;
}

static const char *opcodes[] = {
	[OP_BADOP] = "bad_op",

	/* Fn entrypoint */
	[OP_ENTRY] = "<entry-point>",

	/* Terminator */
	[OP_RET] = "ret",
	[OP_BR] = "br",
	[OP_CBR] = "cbr",
	[OP_SWITCH] = "switch",
	[OP_UNREACH] = "unreachable",
	[OP_COMPUTEDGOTO] = "jmp *",
	
	/* Binary */
	[OP_ADD] = "add",
	[OP_SUB] = "sub",
	[OP_MUL] = "mul",
	[OP_DIVU] = "divu",
	[OP_DIVS] = "divs",
	[OP_MODU] = "modu",
	[OP_MODS] = "mods",
	[OP_SHL] = "shl",
	[OP_LSR] = "lsr",
	[OP_ASR] = "asr",
	
	/* Floating-point Binary */
	[OP_FADD] = "fadd",
	[OP_FSUB] = "fsub",
	[OP_FMUL] = "fmul",
	[OP_FDIV] = "fdiv",

	/* Logical */
	[OP_AND] = "and",
	[OP_OR] = "or",
	[OP_XOR] = "xor",

	/* Binary comparison */
	[OP_SET_EQ] = "seteq",
	[OP_SET_NE] = "setne",
	[OP_SET_LE] = "setle",
	[OP_SET_GE] = "setge",
	[OP_SET_LT] = "setlt",
	[OP_SET_GT] = "setgt",
	[OP_SET_B] = "setb",
	[OP_SET_A] = "seta",
	[OP_SET_BE] = "setbe",
	[OP_SET_AE] = "setae",

	/* floating-point comparison */
	[OP_FCMP_ORD] = "fcmpord",
	[OP_FCMP_OEQ] = "fcmpoeq",
	[OP_FCMP_ONE] = "fcmpone",
	[OP_FCMP_OLE] = "fcmpole",
	[OP_FCMP_OGE] = "fcmpoge",
	[OP_FCMP_OLT] = "fcmpolt",
	[OP_FCMP_OGT] = "fcmpogt",
	[OP_FCMP_UEQ] = "fcmpueq",
	[OP_FCMP_UNE] = "fcmpune",
	[OP_FCMP_ULE] = "fcmpule",
	[OP_FCMP_UGE] = "fcmpuge",
	[OP_FCMP_ULT] = "fcmpult",
	[OP_FCMP_UGT] = "fcmpugt",
	[OP_FCMP_UNO] = "fcmpuno",

	/* Uni */
	[OP_NOT] = "not",
	[OP_NEG] = "neg",
	[OP_FNEG] = "fneg",

	/* Special three-input */
	[OP_SEL] = "select",
	[OP_FMADD] = "fmadd",
	
	/* Memory */
	[OP_LOAD] = "load",
	[OP_STORE] = "store",
	[OP_LABEL] = "label",
	[OP_SETVAL] = "set",
	[OP_SETFVAL] = "setfval",
	[OP_SYMADDR] = "symaddr",

	/* Other */
	[OP_PHI] = "phi",
	[OP_PHISOURCE] = "phisrc",
	[OP_SEXT] = "sext",
	[OP_ZEXT] = "zext",
	[OP_TRUNC] = "trunc",
	[OP_FCVTU] = "fcvtu",
	[OP_FCVTS] = "fcvts",
	[OP_UCVTF] = "ucvtf",
	[OP_SCVTF] = "scvtf",
	[OP_FCVTF] = "fcvtf",
	[OP_UTPTR] = "utptr",
	[OP_PTRTU] = "ptrtu",
	[OP_PTRCAST] = "ptrcast",
	[OP_INLINED_CALL] = "# call",
	[OP_CALL] = "call",
	[OP_SLICE] = "slice",
	[OP_NOP] = "nop",
	[OP_DEATHNOTE] = "dead",
	[OP_ASM] = "asm",

	/* Sparse tagging (line numbers, context, whatever) */
	[OP_CONTEXT] = "context",
	[OP_RANGE] = "range-check",

	[OP_COPY] = "copy",
};

static char *show_asm_constraints(char *buf, const char *sep, struct asm_constraint_list *list)
{
	struct asm_constraint *entry;

	FOR_EACH_PTR(list, entry) {
		buf += sprintf(buf, "%s\"%s\"", sep, entry->constraint);
		if (entry->pseudo)
			buf += sprintf(buf, " (%s)", show_pseudo(entry->pseudo));
		if (entry->ident)
			buf += sprintf(buf, " [%s]", show_ident(entry->ident));
		sep = ", ";		
	} END_FOR_EACH_PTR(entry);
	return buf;
}

static char *show_asm(char *buf, struct instruction *insn)
{
	struct asm_rules *rules = insn->asm_rules;

	buf += sprintf(buf, "\"%s\"", insn->string);
	buf = show_asm_constraints(buf, "\n\t\tout: ", rules->outputs);
	buf = show_asm_constraints(buf, "\n\t\tin: ", rules->inputs);
	buf = show_asm_constraints(buf, "\n\t\tclobber: ", rules->clobbers);
	return buf;
}

const char *show_instruction(struct instruction *insn)
{
	int opcode = insn->opcode;
	static char buffer[4096];
	char *buf;

	buf = buffer;
	if (!insn->bb)
		buf += sprintf(buf, "# ");

	if (opcode < ARRAY_SIZE(opcodes)) {
		const char *op = opcodes[opcode];
		if (!op)
			buf += sprintf(buf, "opcode:%d", opcode);
		else
			buf += sprintf(buf, "%s", op);
		if (insn->size)
			buf += sprintf(buf, ".%d", insn->size);
		memset(buf, ' ', 20);
		buf++;
	}

	if (buf < buffer + 12)
		buf = buffer + 12;
	switch (opcode) {
	case OP_RET:
		if (insn->src && insn->src != VOID)
			buf += sprintf(buf, "%s", show_pseudo(insn->src));
		break;

	case OP_CBR:
		buf += sprintf(buf, "%s, %s, %s", show_pseudo(insn->cond), show_label(insn->bb_true), show_label(insn->bb_false));
		break;

	case OP_BR:
		buf += sprintf(buf, "%s", show_label(insn->bb_true));
		break;

	case OP_LABEL:
		buf += sprintf(buf, "%s <- ", show_pseudo(insn->target));
		buf += sprintf(buf, "%s", show_label(insn->bb_true));
		break;

	case OP_SETVAL: {
		struct expression *expr = insn->val;
		buf += sprintf(buf, "%s <- ", show_pseudo(insn->target));

		if (!expr) {
			buf += sprintf(buf, "%s", "<none>");
			break;
		}
			
		switch (expr->type) {
		case EXPR_VALUE:
			buf += sprintf(buf, "%lld", expr->value);
			break;
		case EXPR_FVALUE:
			buf += sprintf(buf, "%Le", expr->fvalue);
			break;
		case EXPR_STRING:
			buf += sprintf(buf, "%.40s", show_string(expr->string));
			break;
		case EXPR_SYMBOL:
			buf += sprintf(buf, "%s", show_ident(expr->symbol->ident));
			break;
		case EXPR_LABEL:
			buf += sprintf(buf, "%s", show_label(expr->symbol->bb_target));
			break;
		default:
			buf += sprintf(buf, "SETVAL EXPR TYPE %d", expr->type);
		}
		break;
	}
	case OP_SETFVAL:
		buf += sprintf(buf, "%s <- ", show_pseudo(insn->target));
		buf += sprintf(buf, "%Le", insn->fvalue);
		break;

	case OP_SWITCH: {
		struct multijmp *jmp;
		buf += sprintf(buf, "%s", show_pseudo(insn->cond));
		FOR_EACH_PTR(insn->multijmp_list, jmp) {
			if (jmp->begin == jmp->end)
				buf += sprintf(buf, ", %lld -> %s", jmp->begin, show_label(jmp->target));
			else if (jmp->begin < jmp->end)
				buf += sprintf(buf, ", %lld ... %lld -> %s", jmp->begin, jmp->end, show_label(jmp->target));
			else
				buf += sprintf(buf, ", default -> %s", show_label(jmp->target));
		} END_FOR_EACH_PTR(jmp);
		break;
	}
	case OP_COMPUTEDGOTO: {
		struct multijmp *jmp;
		buf += sprintf(buf, "%s", show_pseudo(insn->src));
		FOR_EACH_PTR(insn->multijmp_list, jmp) {
			buf += sprintf(buf, ", %s", show_label(jmp->target));
		} END_FOR_EACH_PTR(jmp);
		break;
	}
	case OP_UNREACH:
		break;

	case OP_PHISOURCE: {
		buf += sprintf(buf, "%s <- %s    ", show_pseudo(insn->target), show_pseudo(insn->phi_src));
		break;
	}

	case OP_PHI: {
		pseudo_t phi;
		const char *s = " <-";
		buf += sprintf(buf, "%s", show_pseudo(insn->target));
		FOR_EACH_PTR(insn->phi_list, phi) {
			if (phi == VOID && !verbose)
				continue;
			buf += sprintf(buf, "%s %s", s, show_pseudo(phi));
			s = ",";
		} END_FOR_EACH_PTR(phi);
		break;
	}	
	case OP_LOAD:
		buf += sprintf(buf, "%s <- %lld[%s]", show_pseudo(insn->target), insn->offset, show_pseudo(insn->src));
		break;
	case OP_STORE:
		buf += sprintf(buf, "%s -> %lld[%s]", show_pseudo(insn->target), insn->offset, show_pseudo(insn->src));
		break;
	case OP_INLINED_CALL:
	case OP_CALL: {
		struct pseudo *arg;
		if (insn->target && insn->target != VOID)
			buf += sprintf(buf, "%s <- ", show_pseudo(insn->target));
		buf += sprintf(buf, "%s", show_pseudo(insn->func));
		FOR_EACH_PTR(insn->arguments, arg) {
			buf += sprintf(buf, ", %s", show_pseudo(arg));
		} END_FOR_EACH_PTR(arg);
		break;
	}
	case OP_SEXT: case OP_ZEXT:
	case OP_TRUNC:
	case OP_FCVTU: case OP_FCVTS:
	case OP_UCVTF: case OP_SCVTF:
	case OP_FCVTF:
	case OP_UTPTR:
	case OP_PTRTU:
	case OP_PTRCAST:
		buf += sprintf(buf, "%s <- (%d) %s",
			show_pseudo(insn->target),
			type_size(insn->orig_type),
			show_pseudo(insn->src));
		break;
	case OP_BINARY ... OP_BINARY_END:
	case OP_FPCMP ... OP_FPCMP_END:
	case OP_BINCMP ... OP_BINCMP_END:
		buf += sprintf(buf, "%s <- %s, %s", show_pseudo(insn->target), show_pseudo(insn->src1), show_pseudo(insn->src2));
		break;

	case OP_SEL:
	case OP_FMADD:
		buf += sprintf(buf, "%s <- %s, %s, %s", show_pseudo(insn->target),
			show_pseudo(insn->src1), show_pseudo(insn->src2), show_pseudo(insn->src3));
		break;

	case OP_SLICE:
		buf += sprintf(buf, "%s <- (%d) %s, %d", show_pseudo(insn->target), type_size(insn->orig_type), show_pseudo(insn->src), insn->from);
		break;

	case OP_NOT: case OP_NEG:
	case OP_FNEG:
	case OP_SYMADDR:
		buf += sprintf(buf, "%s <- %s", show_pseudo(insn->target), show_pseudo(insn->src1));
		break;

	case OP_CONTEXT:
		buf += sprintf(buf, "%s%d", insn->check ? "check: " : "", insn->increment);
		break;
	case OP_RANGE:
		buf += sprintf(buf, "%s between %s..%s", show_pseudo(insn->src1), show_pseudo(insn->src2), show_pseudo(insn->src3));
		break;
	case OP_NOP:
		buf += sprintf(buf, "%s <- %s", show_pseudo(insn->target), show_pseudo(insn->src1));
		break;
	case OP_DEATHNOTE:
		buf += sprintf(buf, "%s", show_pseudo(insn->target));
		break;
	case OP_ASM:
		buf = show_asm(buf, insn);
		break;
	case OP_COPY:
		buf += sprintf(buf, "%s <- %s", show_pseudo(insn->target), show_pseudo(insn->src));
		break;
	default:
		break;
	}

	if (buf >= buffer + sizeof(buffer))
		die("instruction buffer overflowed %td\n", buf - buffer);
	do { --buf; } while (*buf == ' ');
	*++buf = 0;
	return buffer;
}

void show_bb(struct basic_block *bb)
{
	struct instruction *insn;

	printf("%s:\n", show_label(bb));
	if (verbose) {
		pseudo_t needs, defines;
		printf("%s:%d\n", stream_name(bb->pos.stream), bb->pos.line);

		FOR_EACH_PTR(bb->needs, needs) {
			struct instruction *def = needs->def;
			if (def->opcode != OP_PHI) {
				printf("  **uses %s (from %s)**\n", show_pseudo(needs), show_label(def->bb));
			} else {
				pseudo_t phi;
				const char *sep = " ";
				printf("  **uses %s (from", show_pseudo(needs));
				FOR_EACH_PTR(def->phi_list, phi) {
					if (phi == VOID)
						continue;
					printf("%s(%s:%s)", sep, show_pseudo(phi), show_label(phi->def->bb));
					sep = ", ";
				} END_FOR_EACH_PTR(phi);		
				printf(")**\n");
			}
		} END_FOR_EACH_PTR(needs);

		FOR_EACH_PTR(bb->defines, defines) {
			printf("  **defines %s **\n", show_pseudo(defines));
		} END_FOR_EACH_PTR(defines);

		if (bb->parents) {
			struct basic_block *from;
			FOR_EACH_PTR(bb->parents, from) {
				printf("  **from %s (%s:%d:%d)**\n", show_label(from),
					stream_name(from->pos.stream), from->pos.line, from->pos.pos);
			} END_FOR_EACH_PTR(from);
		}

		if (bb->children) {
			struct basic_block *to;
			FOR_EACH_PTR(bb->children, to) {
				printf("  **to %s (%s:%d:%d)**\n", show_label(to),
					stream_name(to->pos.stream), to->pos.line, to->pos.pos);
			} END_FOR_EACH_PTR(to);
		}
	}

	FOR_EACH_PTR(bb->insns, insn) {
		if (!insn->bb && verbose < 2)
			continue;
		printf("\t%s\n", show_instruction(insn));
	} END_FOR_EACH_PTR(insn);
	if (!bb_terminated(bb))
		printf("\tEND\n");
}

///
// show BB of non-removed instruction
void show_insn_bb(struct instruction *insn)
{
	if (!insn || !insn->bb)
		return;
	show_bb(insn->bb);
}

static void show_symbol_usage(pseudo_t pseudo)
{
	struct pseudo_user *pu;

	if (pseudo) {
		FOR_EACH_PTR(pseudo->users, pu) {
			printf("\t%s\n", show_instruction(pu->insn));
		} END_FOR_EACH_PTR(pu);
	}
}

void show_entry(struct entrypoint *ep)
{
	struct symbol *sym;
	struct basic_block *bb;

	printf("%s:\n", show_ident(ep->name->ident));

	if (verbose) {
		printf("ep %p: %s\n", ep, show_ident(ep->name->ident));

		FOR_EACH_PTR(ep->syms, sym) {
			if (!sym->pseudo)
				continue;
			if (!sym->pseudo->users)
				continue;
			printf("   sym: %p %s\n", sym, show_ident(sym->ident));
			if (sym->ctype.modifiers & (MOD_EXTERN | MOD_STATIC | MOD_ADDRESSABLE))
				printf("\texternal visibility\n");
			show_symbol_usage(sym->pseudo);
		} END_FOR_EACH_PTR(sym);

		printf("\n");
	}

	FOR_EACH_PTR(ep->bbs, bb) {
		if (!bb)
			continue;
		if (!bb->parents && !bb->children && !bb->insns && verbose < 2)
			continue;
		show_bb(bb);
		printf("\n");
	} END_FOR_EACH_PTR(bb);

	printf("\n");
}

///
// show the function containing the instruction but only if not already removed.
void show_insn_entry(struct instruction *insn)
{
	if (!insn || !insn->bb || !insn->bb->ep)
		return;
	show_entry(insn->bb->ep);
}

static void bind_label(struct symbol *label, struct basic_block *bb, struct position pos)
{
	if (label->bb_target)
		warning(pos, "label '%s' already bound", show_ident(label->ident));
	label->bb_target = bb;
}

static struct basic_block * get_bound_block(struct entrypoint *ep, struct symbol *label)
{
	struct basic_block *bb = label->bb_target;

	if (!bb) {
		bb = alloc_basic_block(ep, label->pos);
		label->bb_target = bb;
	}
	return bb;
}

static void finish_block(struct entrypoint *ep)
{
	struct basic_block *src = ep->active;
	if (bb_reachable(src))
		ep->active = NULL;
}

static void add_goto(struct entrypoint *ep, struct basic_block *dst)
{
	struct basic_block *src = ep->active;
	if (bb_reachable(src)) {
		struct instruction *br = alloc_instruction(OP_BR, 0);
		br->bb_true = dst;
		add_bb(&dst->parents, src);
		add_bb(&src->children, dst);
		br->bb = src;
		add_instruction(&src->insns, br);
		ep->active = NULL;
	}
}

static void add_one_insn(struct entrypoint *ep, struct instruction *insn)
{
	struct basic_block *bb = ep->active;    

	if (bb_reachable(bb)) {
		insn->bb = bb;
		add_instruction(&bb->insns, insn);
	}
}

static void add_unreachable(struct entrypoint *ep)
{
	struct instruction *insn = alloc_instruction(OP_UNREACH, 0);
	add_one_insn(ep, insn);
	ep->active = NULL;
}

static void set_activeblock(struct entrypoint *ep, struct basic_block *bb)
{
	if (!bb_terminated(ep->active))
		add_goto(ep, bb);

	ep->active = bb;
	if (bb_reachable(bb))
		add_bb(&ep->bbs, bb);
}

void insert_select(struct basic_block *bb, struct instruction *br, struct instruction *phi_node, pseudo_t if_true, pseudo_t if_false)
{
	pseudo_t target;
	struct instruction *select;

	select = alloc_typed_instruction(OP_SEL, phi_node->type);

	assert(br->cond);
	use_pseudo(select, br->cond, &select->src1);

	target = phi_node->target;
	assert(target->def == phi_node);
	select->target = target;
	target->def = select;

	use_pseudo(select, if_true, &select->src2);
	use_pseudo(select, if_false, &select->src3);

	insert_last_instruction(bb, select);
}

static inline int bb_empty(struct basic_block *bb)
{
	return !bb->insns;
}

/* Add a label to the currently active block, return new active block */
static struct basic_block * add_label(struct entrypoint *ep, struct symbol *label)
{
	struct basic_block *bb = label->bb_target;

	if (bb) {
		set_activeblock(ep, bb);
		return bb;
	}
	bb = ep->active;
	if (!bb_reachable(bb) || !bb_empty(bb)) {
		bb = alloc_basic_block(ep, label->pos);
		set_activeblock(ep, bb);
	}
	label->bb_target = bb;
	return bb;
}

static void add_branch(struct entrypoint *ep, pseudo_t cond, struct basic_block *bb_true, struct basic_block *bb_false)
{
	struct basic_block *bb = ep->active;
	struct instruction *br;

	if (bb_reachable(bb)) {
		br = alloc_instruction(OP_CBR, 0);
		use_pseudo(br, cond, &br->cond);
		br->bb_true = bb_true;
		br->bb_false = bb_false;
		add_bb(&bb_true->parents, bb);
		add_bb(&bb_false->parents, bb);
		add_bb(&bb->children, bb_true);
		add_bb(&bb->children, bb_false);
		add_one_insn(ep, br);
	}
}

pseudo_t alloc_pseudo(struct instruction *def)
{
	static int nr = 0;
	struct pseudo * pseudo = __alloc_pseudo(0);
	pseudo->type = PSEUDO_REG;
	pseudo->nr = ++nr;
	pseudo->def = def;
	return pseudo;
}

static pseudo_t symbol_pseudo(struct entrypoint *ep, struct symbol *sym)
{
	pseudo_t pseudo;

	if (!sym)
		return VOID;

	pseudo = sym->pseudo;
	if (!pseudo) {
		pseudo = __alloc_pseudo(0);
		pseudo->nr = -1;
		pseudo->type = PSEUDO_SYM;
		pseudo->sym = sym;
		pseudo->ident = sym->ident;
		sym->pseudo = pseudo;
		add_pseudo(&ep->accesses, pseudo);
	}
	/* Symbol pseudos have neither nr nor def */
	return pseudo;
}

pseudo_t value_pseudo(long long val)
{
#define MAX_VAL_HASH 64
	static struct pseudo_list *prev[MAX_VAL_HASH];
	int hash = val & (MAX_VAL_HASH-1);
	struct pseudo_list **list = prev + hash;
	pseudo_t pseudo;

	FOR_EACH_PTR(*list, pseudo) {
		if (pseudo->value == val)
			return pseudo;
	} END_FOR_EACH_PTR(pseudo);

	pseudo = __alloc_pseudo(0);
	pseudo->type = PSEUDO_VAL;
	pseudo->value = val;
	add_pseudo(list, pseudo);

	/* Value pseudos have neither nr, usage nor def */
	return pseudo;
}

pseudo_t undef_pseudo(void)
{
	pseudo_t pseudo = __alloc_pseudo(0);
	pseudo->type = PSEUDO_UNDEF;
	return pseudo;
}

static pseudo_t argument_pseudo(struct entrypoint *ep, int nr)
{
	pseudo_t pseudo = __alloc_pseudo(0);
	struct instruction *entry = ep->entry;

	pseudo->type = PSEUDO_ARG;
	pseudo->nr = nr;
	pseudo->def = entry;
	add_pseudo(&entry->arg_list, pseudo);

	/* Argument pseudos have neither usage nor def */
	return pseudo;
}

struct instruction *alloc_phisrc(pseudo_t pseudo, struct symbol *type)
{
	struct instruction *insn = alloc_typed_instruction(OP_PHISOURCE, type);
	pseudo_t phi = __alloc_pseudo(0);
	static int nr = 0;

	phi->type = PSEUDO_PHI;
	phi->nr = ++nr;
	phi->def = insn;

	use_pseudo(insn, pseudo, &insn->phi_src);
	insn->target = phi;
	return insn;
}

pseudo_t alloc_phi(struct basic_block *source, pseudo_t pseudo, struct symbol *type)
{
	struct instruction *insn;

	if (!source)
		return VOID;

	insn = alloc_phisrc(pseudo, type);
	insn->bb = source;
	add_instruction(&source->insns, insn);
	return insn->target;
}

struct instruction *alloc_phi_node(struct basic_block *bb, struct symbol *type, struct ident *ident)
{
	struct instruction *phi_node = alloc_typed_instruction(OP_PHI, type);
	pseudo_t phi;

	phi = alloc_pseudo(phi_node);
	phi->ident = ident;
	phi->def = phi_node;
	phi_node->target = phi;
	phi_node->bb = bb;
	return phi_node;
}

void add_phi_node(struct basic_block *bb, struct instruction *phi_node)
{
	struct instruction *insn;

	FOR_EACH_PTR(bb->insns, insn) {
		enum opcode op = insn->opcode;
		if (op == OP_PHI)
			continue;
		INSERT_CURRENT(phi_node, insn);
		return;
	} END_FOR_EACH_PTR(insn);

	// FIXME
	add_instruction(&bb->insns, phi_node);
}

struct instruction *insert_phi_node(struct basic_block *bb, struct symbol *var)
{
	struct instruction *phi_node = alloc_phi_node(bb, var, var->ident);
	add_phi_node(bb, phi_node);
	return phi_node;
}

/*
 * We carry the "access_data" structure around for any accesses,
 * which simplifies things a lot. It contains all the access
 * information in one place.
 */
struct access_data {
	struct symbol *type;		// ctype
	struct symbol *btype;		// base type of bitfields
	pseudo_t address;		// pseudo containing address ..
	long long offset;		// byte offset
};

static int linearize_simple_address(struct entrypoint *ep,
	struct expression *addr,
	struct access_data *ad)
{
	if (addr->type == EXPR_SYMBOL) {
		linearize_one_symbol(ep, addr->symbol);
		ad->address = symbol_pseudo(ep, addr->symbol);
		return 1;
	}
	if (addr->type == EXPR_BINOP) {
		if (addr->right->type == EXPR_VALUE) {
			if (addr->op == '+') {
				ad->offset += get_expression_value(addr->right);
				return linearize_simple_address(ep, addr->left, ad);
			}
		}
	}
	ad->address = linearize_expression(ep, addr);
	return 1;
}

static struct symbol *bitfield_base_type(struct symbol *sym)
{
	struct symbol *base = sym;

	if (sym) {
		if (sym->type == SYM_NODE)
			base = base->ctype.base_type;
		if (base->type == SYM_BITFIELD) {
			base = base->ctype.base_type;
			if (sym->packed) {
				int size = bits_to_bytes(sym->bit_offset + sym->bit_size);
				sym = __alloc_symbol(0);
				*sym = *base;
				sym->bit_size = bytes_to_bits(size);
				return sym;
			}
			return base;
		}
	}
	return sym;
}

static int linearize_address_gen(struct entrypoint *ep,
	struct expression *expr,
	struct access_data *ad)
{
	struct symbol *ctype = expr->ctype;

	if (!ctype)
		return 0;
	ad->type = ctype;
	if (expr->type == EXPR_PREOP && expr->op == '*')
		return linearize_simple_address(ep, expr->unop, ad);

	warning(expr->pos, "generating address of non-lvalue (%d)", expr->type);
	return 0;
}

static pseudo_t add_load(struct entrypoint *ep, struct access_data *ad)
{
	struct instruction *insn;
	pseudo_t new;

	if (!ep->active)
		return VOID;

	insn = alloc_typed_instruction(OP_LOAD, ad->btype);
	new = alloc_pseudo(insn);

	insn->target = new;
	insn->offset = ad->offset;
	insn->is_volatile = ad->type && (ad->type->ctype.modifiers & MOD_VOLATILE);
	use_pseudo(insn, ad->address, &insn->src);
	add_one_insn(ep, insn);
	return new;
}

static void add_store(struct entrypoint *ep, struct access_data *ad, pseudo_t value)
{
	struct basic_block *bb = ep->active;
	struct instruction *store;

	if (!bb)
		return;

	store = alloc_typed_instruction(OP_STORE, ad->btype);
	store->offset = ad->offset;
	store->is_volatile = ad->type && (ad->type->ctype.modifiers & MOD_VOLATILE);
	use_pseudo(store, value, &store->target);
	use_pseudo(store, ad->address, &store->src);
	add_one_insn(ep, store);
}

static pseudo_t linearize_bitfield_insert(struct entrypoint *ep,
	pseudo_t ori, pseudo_t val, struct symbol *ctype, struct symbol *btype)
{
	unsigned int shift = ctype->bit_offset;
	unsigned int size = ctype->bit_size;
	unsigned long long mask = ((1ULL << size) - 1);
	unsigned long long smask= bits_mask(btype->bit_size);

	val = add_cast(ep, btype, ctype, OP_ZEXT, val);
	if (shift) {
		val = add_binary_op(ep, btype, OP_SHL, val, value_pseudo(shift));
		mask <<= shift;
	}
	ori = add_binary_op(ep, btype, OP_AND, ori, value_pseudo(~mask & smask));
	val = add_binary_op(ep, btype, OP_OR, ori, val);

	return val;
}

static pseudo_t linearize_store_gen(struct entrypoint *ep,
		pseudo_t value,
		struct access_data *ad)
{
	struct symbol *ctype = ad->type;
	struct symbol *btype;
	pseudo_t store = value;

	if (!ep->active)
		return VOID;

	btype = ad->btype = bitfield_base_type(ctype);
	if (type_size(btype) != type_size(ctype)) {
		pseudo_t orig = add_load(ep, ad);
		store = linearize_bitfield_insert(ep, orig, value, ctype, btype);
	}
	add_store(ep, ad, store);
	return value;
}

static void taint_undefined_behaviour(struct instruction *insn)
{
	pseudo_t src2;

	switch (insn->opcode) {
	case OP_LSR:
	case OP_ASR:
	case OP_SHL:
		src2 = insn->src2;
		if (src2->type != PSEUDO_VAL)
			break;
		if ((unsigned long long)src2->value >= insn->size)
			insn->tainted = 1;
		break;
	}
}

static pseudo_t add_binary_op(struct entrypoint *ep, struct symbol *ctype, int op, pseudo_t left, pseudo_t right)
{
	struct instruction *insn = alloc_typed_instruction(op, ctype);
	pseudo_t target = alloc_pseudo(insn);
	insn->target = target;
	use_pseudo(insn, left, &insn->src1);
	use_pseudo(insn, right, &insn->src2);
	add_one_insn(ep, insn);
	return target;
}

static pseudo_t add_cmp_op(struct entrypoint *ep, struct symbol *ctype, int op, struct symbol *itype, pseudo_t left, pseudo_t right)
{
	pseudo_t target = add_binary_op(ep, ctype, op, left, right);
	target->def->itype = itype;
	return target;
}

static pseudo_t add_setval(struct entrypoint *ep, struct symbol *ctype, struct expression *val)
{
	struct instruction *insn = alloc_typed_instruction(OP_SETVAL, ctype);
	pseudo_t target = alloc_pseudo(insn);
	insn->target = target;
	insn->val = val;
	add_one_insn(ep, insn);
	return target;
}

static pseudo_t add_setfval(struct entrypoint *ep, struct symbol *ctype, long double fval)
{
	struct instruction *insn = alloc_typed_instruction(OP_SETFVAL, ctype);
	pseudo_t target = alloc_pseudo(insn);
	insn->target = target;
	insn->fvalue = fval;
	add_one_insn(ep, insn);
	return target;
}

static pseudo_t add_symbol_address(struct entrypoint *ep, struct expression *expr)
{
	struct instruction *insn = alloc_typed_instruction(OP_SYMADDR, expr->ctype);
	pseudo_t target = alloc_pseudo(insn);

	insn->target = target;
	use_pseudo(insn, symbol_pseudo(ep, expr->symbol), &insn->src);
	add_one_insn(ep, insn);
	return target;
}

static pseudo_t linearize_bitfield_extract(struct entrypoint *ep,
		pseudo_t val, struct symbol *ctype, struct symbol *btype)
{
	unsigned int off = ctype->bit_offset;

	if (off) {
		pseudo_t shift = value_pseudo(off);
		val = add_binary_op(ep, btype, OP_LSR, val, shift);
	}
	val = cast_pseudo(ep, val, btype, ctype);
	return val;
}

static pseudo_t linearize_load_gen(struct entrypoint *ep, struct access_data *ad)
{
	struct symbol *ctype = ad->type;
	struct symbol *btype;
	pseudo_t new;

	if (!ep->active)
		return VOID;

	btype = ad->btype = bitfield_base_type(ctype);
	new = add_load(ep, ad);
	if (ctype->bit_size != type_size(btype))
		new = linearize_bitfield_extract(ep, new, ctype, btype);
	return new;
}

static pseudo_t linearize_access(struct entrypoint *ep, struct expression *expr)
{
	struct access_data ad = { NULL, };
	pseudo_t value;

	if (!linearize_address_gen(ep, expr, &ad))
		return VOID;
	value = linearize_load_gen(ep, &ad);
	return value;
}

static pseudo_t linearize_inc_dec(struct entrypoint *ep, struct expression *expr, int postop)
{
	struct access_data ad = { NULL, };
	pseudo_t old, new, one;
	int op = expr->op == SPECIAL_INCREMENT ? OP_ADD : OP_SUB;

	if (!linearize_address_gen(ep, expr->unop, &ad))
		return VOID;

	old = linearize_load_gen(ep, &ad);
	op = opcode_float(op, expr->ctype);
	if (is_float_type(expr->ctype))
		one = add_setfval(ep, expr->ctype, expr->op_value);
	else
		one = value_pseudo(expr->op_value);
	if (ad.btype != ad.type)
		old = cast_pseudo(ep, old, ad.type, ad.btype);
	new = add_binary_op(ep, ad.btype, op, old, one);
	if (ad.btype != ad.type)
		new = cast_pseudo(ep, new, ad.btype, ad.type);
	linearize_store_gen(ep, new, &ad);
	return postop ? old : new;
}

static pseudo_t add_unop(struct entrypoint *ep, struct symbol *ctype, int op, pseudo_t src)
{
	struct instruction *insn = alloc_typed_instruction(op, ctype);
	pseudo_t new = alloc_pseudo(insn);

	insn->target = new;
	use_pseudo(insn, src, &insn->src1);
	add_one_insn(ep, insn);
	return new;
}

static pseudo_t add_cast(struct entrypoint *ep, struct symbol *to,
			 struct symbol *from, int op, pseudo_t src)
{
	pseudo_t new = add_unop(ep, to, op, src);
	new->def->orig_type = from;
	return new;
}

static pseudo_t linearize_slice(struct entrypoint *ep, struct expression *expr)
{
	pseudo_t pre = linearize_expression(ep, expr->base);
	struct instruction *insn = alloc_typed_instruction(OP_SLICE, expr->ctype);
	pseudo_t new = alloc_pseudo(insn);

	insn->target = new;
	insn->from = expr->r_bitpos;
	insn->orig_type = expr->base->ctype;
	use_pseudo(insn, pre, &insn->src);
	add_one_insn(ep, insn);
	return new;
}

static pseudo_t linearize_regular_preop(struct entrypoint *ep, struct expression *expr)
{
	pseudo_t pre = linearize_expression(ep, expr->unop);
	struct symbol *ctype = expr->ctype;
	switch (expr->op) {
	case '+':
		return pre;
	case '!': {
		pseudo_t zero = value_pseudo(0);
		return add_cmp_op(ep, ctype, OP_SET_EQ, expr->unop->ctype, pre, zero);
	}
	case '~':
		return add_unop(ep, ctype, OP_NOT, pre);
	case '-':
		return add_unop(ep, ctype, opcode_float(OP_NEG, ctype), pre);
	}
	return VOID;
}

static pseudo_t linearize_preop(struct entrypoint *ep, struct expression *expr)
{
	/*
	 * '*' is an lvalue access, and is fundamentally different
	 * from an arithmetic operation. Maybe it should have an
	 * expression type of its own..
	 */
	if (expr->op == '*')
		return linearize_access(ep, expr);
	if (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT)
		return linearize_inc_dec(ep, expr, 0);
	return linearize_regular_preop(ep, expr);
}

static pseudo_t linearize_postop(struct entrypoint *ep, struct expression *expr)
{
	return linearize_inc_dec(ep, expr, 1);
}	

/*
 * Casts to pointers are "less safe" than other casts, since
 * they imply type-unsafe accesses. "void *" is a special
 * case, since you can't access through it anyway without another
 * cast.
 */
enum mtype {
	MTYPE_UINT,
	MTYPE_SINT,
	MTYPE_PTR,
	MTYPE_VPTR,	// TODO: must be removed ?
	MTYPE_FLOAT,
	MTYPE_BAD,
};

static enum mtype get_mtype(struct symbol *s)
{
	int sign = (s->ctype.modifiers & MOD_SIGNED) ? 1 : 0;

retry:	switch (s->type) {
	case SYM_NODE:
		s = s->ctype.base_type;
		goto retry;
	case SYM_PTR:
		if (s->ctype.base_type == &void_ctype)
			return MTYPE_VPTR;
		return MTYPE_PTR;
	case SYM_BITFIELD:
	case SYM_RESTRICT:
	case SYM_FOULED:
	case SYM_ENUM:
		s = s->ctype.base_type;
		/* fall-through */
	case_int:
		return sign ? MTYPE_SINT : MTYPE_UINT;
	case SYM_BASETYPE:
		if (s->ctype.base_type == &fp_type)
			return MTYPE_FLOAT;
		if (s->ctype.base_type == &int_type)
			goto case_int;
		/* fall-through */
	default:
		return MTYPE_BAD;
	}
}

static int get_cast_opcode(struct symbol *dst, struct symbol *src)
{
	enum mtype stype = get_mtype(src);
	enum mtype dtype = get_mtype(dst);

	switch (dtype) {
	case MTYPE_FLOAT:
		switch (stype) {
		case MTYPE_FLOAT:
			if (dst->bit_size == src->bit_size)
				return OP_NOP;
			return OP_FCVTF;
		case MTYPE_UINT:
			return OP_UCVTF;
		case MTYPE_SINT:
			return OP_SCVTF;
		default:
			return OP_BADOP;
		}
	case MTYPE_PTR:
		switch (stype) {
		case MTYPE_UINT:
		case MTYPE_SINT:
			return OP_UTPTR;
		case MTYPE_PTR:
		case MTYPE_VPTR:
			return OP_PTRCAST;
		default:
			return OP_BADOP;
		}
	case MTYPE_VPTR:
		switch (stype) {
		case MTYPE_PTR:
		case MTYPE_VPTR:
		case MTYPE_UINT:
			stype = MTYPE_UINT;
			/* fall through */
		case MTYPE_SINT:
			break;
		default:
			return OP_BADOP;
		}
		/* fall through */
	case MTYPE_UINT:
	case MTYPE_SINT:
		switch (stype) {
		case MTYPE_FLOAT:
			return dtype == MTYPE_UINT ? OP_FCVTU : OP_FCVTS;
		case MTYPE_PTR:
			return OP_PTRTU;
		case MTYPE_VPTR:
		case MTYPE_UINT:
		case MTYPE_SINT:
			if (dst->bit_size ==src->bit_size)
				return OP_NOP;
			if (dst->bit_size  < src->bit_size)
				return OP_TRUNC;
			return stype == MTYPE_SINT ? OP_SEXT : OP_ZEXT;
		default:
			return OP_BADOP;
		}
		/* fall through */
	default:
		if (src->type == SYM_NODE)
			src = src->ctype.base_type;
		if (dst->type == SYM_NODE)
			dst = dst->ctype.base_type;
		if (src == dst)
			return OP_NOP;
		return OP_BADOP;
	}
}

static pseudo_t cast_pseudo(struct entrypoint *ep, pseudo_t src, struct symbol *from, struct symbol *to)
{
	const struct position pos = current_pos;
	pseudo_t result;
	struct instruction *insn;
	int opcode;

	if (src == VOID)
		return VOID;
	if (!from || !to)
		return VOID;
	if (from->bit_size < 0 || to->bit_size < 0)
		return VOID;
	opcode = get_cast_opcode(to, from);
	switch (opcode) {
	case OP_NOP:
		return src;
	case OP_UTPTR:
		if (from->bit_size == to->bit_size)
			break;
		if (src == value_pseudo(0))
			break;
		if (Wint_to_pointer_cast)
			warning(pos, "non size-preserving integer to pointer cast");
		src = cast_pseudo(ep, src, from, size_t_ctype);
		from = size_t_ctype;
		break;
	case OP_PTRTU:
		if (from->bit_size == to->bit_size)
			break;
		if (Wpointer_to_int_cast)
			warning(pos, "non size-preserving pointer to integer cast");
		src = cast_pseudo(ep, src, from, size_t_ctype);
		return cast_pseudo(ep, src, size_t_ctype, to);
	case OP_BADOP:
		return VOID;
	default:
		break;
	}
	insn = alloc_typed_instruction(opcode, to);
	result = alloc_pseudo(insn);
	insn->target = result;
	insn->orig_type = from;
	use_pseudo(insn, src, &insn->src);
	add_one_insn(ep, insn);
	return result;
}

static int map_opcode(int opcode, struct symbol *ctype)
{
	if (ctype && is_float_type(ctype))
		return opcode_table[opcode].to_float;
	if (ctype && (ctype->ctype.modifiers & MOD_SIGNED)) {
		switch(opcode) {
		case OP_DIVU: case OP_MODU: case OP_LSR:
			opcode++;
		}
	}
	return opcode;
}

static inline pseudo_t add_convert_to_bool(struct entrypoint *ep, pseudo_t src, struct symbol *type)
{
	pseudo_t zero;
	int op;

	if (!type || src == VOID)
		return VOID;
	if (is_bool_type(type))
		return src;
	if (src->type == PSEUDO_VAL && (src->value == 0 || src->value == 1))
		return src;
	if (is_float_type(type)) {
		zero = add_setfval(ep, type, 0.0);
		op = map_opcode(OP_SET_NE, type);
	} else {
		zero = value_pseudo(0);
		op = OP_SET_NE;
	}
	return add_cmp_op(ep, &bool_ctype, op, type, src, zero);
}

static pseudo_t linearize_expression_to_bool(struct entrypoint *ep, struct expression *expr)
{
	pseudo_t dst;
	dst = linearize_expression(ep, expr);
	dst = add_convert_to_bool(ep, dst, expr->ctype);
	return dst;
}

static pseudo_t linearize_assignment(struct entrypoint *ep, struct expression *expr)
{
	struct access_data ad = { NULL, };
	struct expression *target = expr->left;
	struct expression *src = expr->right;
	struct symbol *ctype;
	pseudo_t value;

	value = linearize_expression(ep, src);
	if (!target || !linearize_address_gen(ep, target, &ad))
		return value;
	if (expr->op != '=') {
		pseudo_t oldvalue = linearize_load_gen(ep, &ad);
		pseudo_t dst;
		static const int op_trans[] = {
			[SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = OP_ADD,
			[SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = OP_SUB,
			[SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = OP_MUL,
			[SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = OP_DIVU,
			[SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = OP_MODU,
			[SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = OP_SHL,
			[SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = OP_LSR,
			[SPECIAL_AND_ASSIGN - SPECIAL_BASE] = OP_AND,
			[SPECIAL_OR_ASSIGN  - SPECIAL_BASE] = OP_OR,
			[SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = OP_XOR
		};
		int opcode;

		if (!src)
			return VOID;

		ctype = src->ctype;
		oldvalue = cast_pseudo(ep, oldvalue, target->ctype, ctype);
		opcode = map_opcode(op_trans[expr->op - SPECIAL_BASE], ctype);
		dst = add_binary_op(ep, ctype, opcode, oldvalue, value);
		taint_undefined_behaviour(dst->def);
		value = cast_pseudo(ep, dst, ctype, expr->ctype);
	}
	value = linearize_store_gen(ep, value, &ad);
	return value;
}

static pseudo_t linearize_call_expression(struct entrypoint *ep, struct expression *expr)
{
	struct expression *arg, *fn;
	struct instruction *insn;
	pseudo_t retval, call;
	struct ctype *ctype = NULL;
	struct symbol *fntype;
	struct context *context;

	if (!expr->ctype)
		return VOID;

	fn = expr->fn;
	fntype = fn->ctype;

	// handle builtins
	if (fntype->op && fntype->op->linearize) {
		retval = fntype->op->linearize(ep, expr);
		if (retval)
			return retval;
	}

	ctype = &fntype->ctype;

	insn = alloc_typed_instruction(OP_CALL, expr->ctype);
	add_symbol(&insn->fntypes, fntype);
	FOR_EACH_PTR(expr->args, arg) {
		pseudo_t new = linearize_expression(ep, arg);
		use_pseudo(insn, new, add_pseudo(&insn->arguments, new));
		add_symbol(&insn->fntypes, arg->ctype);
	} END_FOR_EACH_PTR(arg);

	if (fn->type == EXPR_PREOP && fn->op == '*' && is_func_type(fn->ctype))
		fn = fn->unop;

	if (fn->type == EXPR_SYMBOL) {
		call = symbol_pseudo(ep, fn->symbol);
	} else {
		call = linearize_expression(ep, fn);
	}
	use_pseudo(insn, call, &insn->func);
	retval = VOID;
	if (expr->ctype != &void_ctype)
		retval = alloc_pseudo(insn);
	insn->target = retval;
	add_one_insn(ep, insn);

	if (ctype) {
		FOR_EACH_PTR(ctype->contexts, context) {
			int in = context->in;
			int out = context->out;
			int check = 0;
			int context_diff;
			if (in < 0) {
				check = 1;
				in = 0;
			}
			if (out < 0) {
				check = 0;
				out = 0;
			}
			context_diff = out - in;
			if (check || context_diff) {
				insn = alloc_instruction(OP_CONTEXT, 0);
				insn->increment = context_diff;
				insn->check = check;
				insn->context_expr = context->context;
				add_one_insn(ep, insn);
			}
		} END_FOR_EACH_PTR(context);

		if (ctype->modifiers & MOD_NORETURN)
			add_unreachable(ep);
	}

	return retval;
}

static pseudo_t linearize_binop_bool(struct entrypoint *ep, struct expression *expr)
{
	pseudo_t src1, src2, dst;
	int op = (expr->op == SPECIAL_LOGICAL_OR) ? OP_OR : OP_AND;

	src1 = linearize_expression_to_bool(ep, expr->left);
	src2 = linearize_expression_to_bool(ep, expr->right);
	dst = add_binary_op(ep, &bool_ctype, op, src1, src2);
	if (expr->ctype != &bool_ctype)
		dst = cast_pseudo(ep, dst, &bool_ctype, expr->ctype);
	return dst;
}

static pseudo_t linearize_binop(struct entrypoint *ep, struct expression *expr)
{
	pseudo_t src1, src2, dst;
	static const int opcode[] = {
		['+'] = OP_ADD, ['-'] = OP_SUB,
		['*'] = OP_MUL, ['/'] = OP_DIVU,
		['%'] = OP_MODU, ['&'] = OP_AND,
		['|'] = OP_OR,  ['^'] = OP_XOR,
		[SPECIAL_LEFTSHIFT] = OP_SHL,
		[SPECIAL_RIGHTSHIFT] = OP_LSR,
	};
	int op;

	src1 = linearize_expression(ep, expr->left);
	src2 = linearize_expression(ep, expr->right);
	op = map_opcode(opcode[expr->op], expr->ctype);
	dst = add_binary_op(ep, expr->ctype, op, src1, src2);
	taint_undefined_behaviour(dst->def);
	return dst;
}

static pseudo_t linearize_logical_branch(struct entrypoint *ep, struct expression *expr, struct basic_block *bb_true, struct basic_block *bb_false);

static pseudo_t linearize_cond_branch(struct entrypoint *ep, struct expression *expr, struct basic_block *bb_true, struct basic_block *bb_false);

static pseudo_t linearize_select(struct entrypoint *ep, struct expression *expr)
{
	pseudo_t cond, valt, valf, res;
	struct instruction *insn;

	valt = linearize_expression(ep, expr->cond_true);
	valf = linearize_expression(ep, expr->cond_false);
	cond = linearize_expression(ep, expr->conditional);

	insn = alloc_typed_instruction(OP_SEL, expr->ctype);
	if (!expr->cond_true)
		valt = cond;
	use_pseudo(insn, cond, &insn->src1);
	use_pseudo(insn, valt, &insn->src2);
	use_pseudo(insn, valf, &insn->src3);

	res = alloc_pseudo(insn);
	insn->target = res;
	add_one_insn(ep, insn);
	return res;
}

static pseudo_t add_join_conditional(struct entrypoint *ep, struct expression *expr,
				     pseudo_t phi1, pseudo_t phi2)
{
	pseudo_t target;
	struct instruction *phi_node;

	if (phi1 == VOID)
		return (phi2 == VOID) ? phi2 : phi2->def->src;
	if (phi2 == VOID)
		return (phi1 == VOID) ? phi1 : phi1->def->src;

	phi_node = alloc_typed_instruction(OP_PHI, expr->ctype);
	link_phi(phi_node, phi1);
	link_phi(phi_node, phi2);
	phi_node->target = target = alloc_pseudo(phi_node);
	add_one_insn(ep, phi_node);
	return target;
}	

static pseudo_t linearize_short_conditional(struct entrypoint *ep, struct expression *expr,
					    struct expression *cond,
					    struct expression *expr_false)
{
	pseudo_t src1, src2;
	struct basic_block *bb_false;
	struct basic_block *merge;
	pseudo_t phi1, phi2;

	if (!expr_false || !ep->active)
		return VOID;

	bb_false = alloc_basic_block(ep, expr_false->pos);
	merge = alloc_basic_block(ep, expr->pos);

	src1 = linearize_expression(ep, cond);
	phi1 = alloc_phi(ep->active, src1, expr->ctype);
	add_branch(ep, src1, merge, bb_false);

	set_activeblock(ep, bb_false);
	src2 = linearize_expression(ep, expr_false);
	phi2 = alloc_phi(ep->active, src2, expr->ctype);
	set_activeblock(ep, merge);

	return add_join_conditional(ep, expr, phi1, phi2);
}

static pseudo_t linearize_conditional(struct entrypoint *ep, struct expression *expr,
				      struct expression *cond,
				      struct expression *expr_true,
				      struct expression *expr_false)
{
	pseudo_t src1, src2;
	pseudo_t phi1, phi2;
	struct basic_block *bb_true, *bb_false, *merge;

	if (!cond || !expr_true || !expr_false || !ep->active)
		return VOID;
	bb_true = alloc_basic_block(ep, expr_true->pos);
	bb_false = alloc_basic_block(ep, expr_false->pos);
	merge = alloc_basic_block(ep, expr->pos);

	linearize_cond_branch(ep, cond, bb_true, bb_false);

	set_activeblock(ep, bb_true);
	src1 = linearize_expression(ep, expr_true);
	phi1 = alloc_phi(ep->active, src1, expr->ctype);
	add_goto(ep, merge); 

	set_activeblock(ep, bb_false);
	src2 = linearize_expression(ep, expr_false);
	phi2 = alloc_phi(ep->active, src2, expr->ctype);
	set_activeblock(ep, merge);

	return add_join_conditional(ep, expr, phi1, phi2);
}

static void insert_phis(struct basic_block *bb, pseudo_t src, struct symbol *ctype,
	struct instruction *node)
{
	struct basic_block *parent;

	FOR_EACH_PTR(bb->parents, parent) {
		struct instruction *phisrc = alloc_phisrc(src, ctype);
		insert_last_instruction(parent, phisrc);
		link_phi(node, phisrc->target);
	} END_FOR_EACH_PTR(parent);
}

static pseudo_t linearize_logical(struct entrypoint *ep, struct expression *expr)
{
	struct symbol *ctype = expr->ctype;
	struct basic_block *other, *merge;
	struct instruction *node;
	pseudo_t src1, src2, phi2;

	if (!ep->active || !expr->left || !expr->right)
		return VOID;

	other = alloc_basic_block(ep, expr->right->pos);
	merge = alloc_basic_block(ep, expr->pos);
	node = alloc_phi_node(merge, ctype, NULL);

	// LHS and its shortcut
	if (expr->op == SPECIAL_LOGICAL_OR) {
		linearize_cond_branch(ep, expr->left, merge, other);
		src1 = value_pseudo(1);
	} else {
		linearize_cond_branch(ep, expr->left, other, merge);
		src1 = value_pseudo(0);
	}
	insert_phis(merge, src1, ctype, node);

	// RHS
	set_activeblock(ep, other);
	src2 = linearize_expression_to_bool(ep, expr->right);
	src2 = cast_pseudo(ep, src2, &bool_ctype, ctype);
	phi2 = alloc_phi(ep->active, src2, ctype);
	link_phi(node, phi2);

	// join
	set_activeblock(ep, merge);
	add_instruction(&merge->insns, node);
	return node->target;
}

static pseudo_t linearize_compare(struct entrypoint *ep, struct expression *expr)
{
	static const int cmpop[] = {
		['>'] = OP_SET_GT, ['<'] = OP_SET_LT,
		[SPECIAL_EQUAL] = OP_SET_EQ,
		[SPECIAL_NOTEQUAL] = OP_SET_NE,
		[SPECIAL_GTE] = OP_SET_GE,
		[SPECIAL_LTE] = OP_SET_LE,
		[SPECIAL_UNSIGNED_LT] = OP_SET_B,
		[SPECIAL_UNSIGNED_GT] = OP_SET_A,
		[SPECIAL_UNSIGNED_LTE] = OP_SET_BE,
		[SPECIAL_UNSIGNED_GTE] = OP_SET_AE,
	};
	struct symbol *itype = expr->right->ctype;
	int op = opcode_float(cmpop[expr->op], itype);
	pseudo_t src1 = linearize_expression(ep, expr->left);
	pseudo_t src2 = linearize_expression(ep, expr->right);
	pseudo_t dst = add_cmp_op(ep, expr->ctype, op, itype, src1, src2);
	return dst;
}


static pseudo_t linearize_cond_branch(struct entrypoint *ep, struct expression *expr, struct basic_block *bb_true, struct basic_block *bb_false)
{
	pseudo_t cond;

	if (!expr || !valid_type(expr->ctype) || !bb_reachable(ep->active))
		return VOID;

	switch (expr->type) {

	case EXPR_STRING:
	case EXPR_VALUE:
		add_goto(ep, expr->value ? bb_true : bb_false);
		break;
	case EXPR_FVALUE:
		add_goto(ep, expr->fvalue ? bb_true : bb_false);
		break;
	case EXPR_LOGICAL:
		linearize_logical_branch(ep, expr, bb_true, bb_false);
		break;
	case EXPR_COMPARE:
		cond = linearize_compare(ep, expr);
		add_branch(ep, cond, bb_true, bb_false);
		break;
	case EXPR_PREOP:
		if (expr->op == '!')
			return linearize_cond_branch(ep, expr->unop, bb_false, bb_true);
		/* fall through */
	default:
		cond = linearize_expression_to_bool(ep, expr);
		add_branch(ep, cond, bb_true, bb_false);
		break;
	}
	return VOID;
}


static pseudo_t linearize_logical_branch(struct entrypoint *ep, struct expression *expr, struct basic_block *bb_true, struct basic_block *bb_false)
{
	struct basic_block *next = alloc_basic_block(ep, expr->pos);

	if (expr->op == SPECIAL_LOGICAL_OR)
		linearize_cond_branch(ep, expr->left, bb_true, next);
	else
		linearize_cond_branch(ep, expr->left, next, bb_false);
	set_activeblock(ep, next);
	linearize_cond_branch(ep, expr->right, bb_true, bb_false);
	return VOID;
}

static pseudo_t linearize_cast(struct entrypoint *ep, struct expression *expr)
{
	pseudo_t src;
	struct expression *orig = expr->cast_expression;

	if (!orig)
		return VOID;

	src = linearize_expression(ep, orig);
	return cast_pseudo(ep, src, orig->ctype, expr->ctype);
}

static pseudo_t linearize_initializer(struct entrypoint *ep, struct expression *initializer, struct access_data *ad)
{
	switch (initializer->type) {
	case EXPR_INITIALIZER: {
		struct expression *expr;
		FOR_EACH_PTR(initializer->expr_list, expr) {
			linearize_initializer(ep, expr, ad);
		} END_FOR_EACH_PTR(expr);
		break;
	}
	case EXPR_POS:
		ad->offset = initializer->init_offset;
		linearize_initializer(ep, initializer->init_expr, ad);
		break;
	default: {
		pseudo_t value = linearize_expression(ep, initializer);
		ad->type = initializer->ctype;
		linearize_store_gen(ep, value, ad);
		return value;
	}
	}

	return VOID;
}

static void linearize_argument(struct entrypoint *ep, struct symbol *arg, int nr)
{
	struct access_data ad = { NULL, };

	ad.type = arg;
	ad.address = symbol_pseudo(ep, arg);
	linearize_store_gen(ep, argument_pseudo(ep, nr), &ad);
}

static pseudo_t linearize_expression(struct entrypoint *ep, struct expression *expr)
{
	if (!expr || !valid_type(expr->ctype))
		return VOID;

	current_pos = expr->pos;
	switch (expr->type) {
	case EXPR_SYMBOL:
		linearize_one_symbol(ep, expr->symbol);
		return add_symbol_address(ep, expr);

	case EXPR_VALUE:
		return value_pseudo(expr->value);

	case EXPR_STRING:
	case EXPR_LABEL:
		return add_setval(ep, expr->ctype, expr);

	case EXPR_FVALUE:
		return add_setfval(ep, expr->ctype, expr->fvalue);

	case EXPR_STATEMENT:
		return linearize_statement(ep, expr->statement);

	case EXPR_CALL:
		return linearize_call_expression(ep, expr);

	case EXPR_BINOP:
		if (expr->op == SPECIAL_LOGICAL_AND || expr->op == SPECIAL_LOGICAL_OR)
			return linearize_binop_bool(ep, expr);
		return linearize_binop(ep, expr);

	case EXPR_LOGICAL:
		return linearize_logical(ep, expr);

	case EXPR_COMPARE:
		return  linearize_compare(ep, expr);

	case EXPR_SELECT:
		return	linearize_select(ep, expr);

	case EXPR_CONDITIONAL:
		if (!expr->cond_true)
			return linearize_short_conditional(ep, expr, expr->conditional, expr->cond_false);

		return  linearize_conditional(ep, expr, expr->conditional,
					      expr->cond_true, expr->cond_false);

	case EXPR_COMMA:
		linearize_expression(ep, expr->left);
		return linearize_expression(ep, expr->right);

	case EXPR_ASSIGNMENT:
		return linearize_assignment(ep, expr);

	case EXPR_PREOP:
		return linearize_preop(ep, expr);

	case EXPR_POSTOP:
		return linearize_postop(ep, expr);

	case EXPR_CAST:
	case EXPR_FORCE_CAST:
	case EXPR_IMPLIED_CAST:
		return linearize_cast(ep, expr);
	
	case EXPR_SLICE:
		return linearize_slice(ep, expr);

	case EXPR_INITIALIZER:
	case EXPR_POS:
		warning(expr->pos, "unexpected initializer expression (%d %d)", expr->type, expr->op);
		return VOID;
	default: 
		warning(expr->pos, "unknown expression (%d %d)", expr->type, expr->op);
		return VOID;
	}
	return VOID;
}

static pseudo_t linearize_one_symbol(struct entrypoint *ep, struct symbol *sym)
{
	struct access_data ad = { NULL, };
	pseudo_t value;

	if (!sym || !sym->initializer || sym->initialized)
		return VOID;

	/* We need to output these puppies some day too.. */
	if (sym->ctype.modifiers & (MOD_STATIC | MOD_TOPLEVEL))
		return VOID;

	sym->initialized = 1;
	ad.address = symbol_pseudo(ep, sym);

	if (sym->initializer && !is_scalar_type(sym)) {
		// default zero initialization [6.7.9.21]
		// FIXME: this init the whole aggregate while
		// only the existing fields need to be initialized.
		// FIXME: this init the whole aggregate even if
		// all fields arelater  explicitely initialized.
		ad.type = sym;
		ad.address = symbol_pseudo(ep, sym);
		linearize_store_gen(ep, value_pseudo(0), &ad);
	}

	value = linearize_initializer(ep, sym->initializer, &ad);
	return value;
}

static pseudo_t linearize_compound_statement(struct entrypoint *ep, struct statement *stmt)
{
	pseudo_t pseudo;
	struct statement *s;

	pseudo = VOID;
	FOR_EACH_PTR(stmt->stmts, s) {
		pseudo = linearize_statement(ep, s);
	} END_FOR_EACH_PTR(s);

	return pseudo;
}

static void add_return(struct entrypoint *ep, struct basic_block *bb, struct symbol *ctype, pseudo_t src)
{
	struct instruction *phi_node = first_instruction(bb->insns);
	pseudo_t phi;
	if (!phi_node) {
		phi_node = alloc_typed_instruction(OP_PHI, ctype);
		phi_node->target = alloc_pseudo(phi_node);
		phi_node->bb = bb;
		add_instruction(&bb->insns, phi_node);
	}
	phi = alloc_phi(ep->active, src, ctype);
	phi->ident = &return_ident;
	link_phi(phi_node, phi);
}

static pseudo_t linearize_fn_statement(struct entrypoint *ep, struct statement *stmt)
{
	struct instruction *phi_node;
	struct basic_block *bb;
	pseudo_t pseudo;

	pseudo = linearize_compound_statement(ep, stmt);
	if (!is_void_type(stmt->ret)) {			// non-void function
		struct basic_block *active = ep->active;
		if (active && !bb_terminated(active)) {	// missing return
			struct basic_block *bb_ret;
			bb_ret = get_bound_block(ep, stmt->ret);
			add_return(ep, bb_ret, stmt->ret, undef_pseudo());
		}
	}
	bb = add_label(ep, stmt->ret);
	phi_node = first_instruction(bb->insns);
	if (phi_node)
		pseudo = phi_node->target;
	return pseudo;
}

static pseudo_t linearize_inlined_call(struct entrypoint *ep, struct statement *stmt)
{
	struct instruction *insn = alloc_instruction(OP_INLINED_CALL, 0);
	struct statement *args = stmt->args;
	struct basic_block *bb;
	pseudo_t pseudo;

	if (args) {
		struct symbol *sym;

		concat_symbol_list(args->declaration, &ep->syms);
		FOR_EACH_PTR(args->declaration, sym) {
			pseudo_t value = linearize_one_symbol(ep, sym);
			add_pseudo(&insn->arguments, value);
		} END_FOR_EACH_PTR(sym);
	}

	pseudo = linearize_fn_statement(ep, stmt);
	insn->target = pseudo;

	insn->func = symbol_pseudo(ep, stmt->inline_fn);
	bb = ep->active;
	if (!bb->insns)
		bb->pos = stmt->pos;
	add_one_insn(ep, insn);
	return pseudo;
}

static pseudo_t linearize_context(struct entrypoint *ep, struct statement *stmt)
{
	struct instruction *insn = alloc_instruction(OP_CONTEXT, 0);
	struct expression *expr = stmt->expression;

	insn->increment = get_expression_value(expr);
	insn->context_expr = stmt->context;
	add_one_insn(ep, insn);
	return VOID;
}

static pseudo_t linearize_range(struct entrypoint *ep, struct statement *stmt)
{
	struct instruction *insn = alloc_instruction(OP_RANGE, 0);

	use_pseudo(insn, linearize_expression(ep, stmt->range_expression), &insn->src1);
	use_pseudo(insn, linearize_expression(ep, stmt->range_low), &insn->src2);
	use_pseudo(insn, linearize_expression(ep, stmt->range_high), &insn->src3);
	add_one_insn(ep, insn);
	return VOID;
}

ALLOCATOR(asm_rules, "asm rules");
ALLOCATOR(asm_constraint, "asm constraints");

static void add_asm_rule(struct instruction *insn, struct asm_constraint_list **list, struct asm_operand *op, pseudo_t pseudo)
{
	struct asm_constraint *rule = __alloc_asm_constraint(0);
	rule->is_memory = op->is_memory;
	rule->ident = op->name;
	rule->constraint = op->constraint ? op->constraint->string->data : "";
	use_pseudo(insn, pseudo, &rule->pseudo);
	add_ptr_list(list, rule);
}

static void add_asm_input(struct entrypoint *ep, struct instruction *insn, struct asm_operand *op)
{
	pseudo_t pseudo = linearize_expression(ep, op->expr);

	add_asm_rule(insn, &insn->asm_rules->inputs, op, pseudo);
}

static void add_asm_output_address(struct entrypoint *ep, struct instruction *insn, struct asm_operand *op)
{
	pseudo_t pseudo;

	if (!op->is_memory)
		return;

	pseudo = linearize_expression(ep, op->expr);
	add_asm_rule(insn, &insn->asm_rules->outputs, op, pseudo);
	insn->output_memory = 1;
}

static void add_asm_output(struct entrypoint *ep, struct instruction *insn, struct asm_operand *op)
{
	struct access_data ad = { NULL, };
	pseudo_t pseudo;

	if (op->is_memory)
		return;

	if (!linearize_address_gen(ep, op->expr, &ad))
		return;
	pseudo = alloc_pseudo(insn);
	linearize_store_gen(ep, pseudo, &ad);

	add_asm_rule(insn, &insn->asm_rules->outputs, op, pseudo);
}

static pseudo_t linearize_asm_statement(struct entrypoint *ep, struct statement *stmt)
{
	struct instruction *insn;
	struct expression *expr, *clob;
	struct asm_rules *rules;
	struct asm_operand *op;

	insn = alloc_instruction(OP_ASM, 0);
	expr = stmt->asm_string;
	if (!expr || expr->type != EXPR_STRING) {
		warning(stmt->pos, "expected string in inline asm");
		return VOID;
	}
	insn->string = expr->string->data;

	rules = __alloc_asm_rules(0);
	insn->asm_rules = rules;

	/* Gather the inputs.. */
	FOR_EACH_PTR(stmt->asm_inputs, op) {
		add_asm_input(ep, insn, op);
	} END_FOR_EACH_PTR(op);

	/* ... and the addresses for memory outputs */
	FOR_EACH_PTR(stmt->asm_outputs, op) {
		add_asm_output_address(ep, insn, op);
	} END_FOR_EACH_PTR(op);

	add_one_insn(ep, insn);

	/* Assign the outputs */
	FOR_EACH_PTR(stmt->asm_outputs, op) {
		add_asm_output(ep, insn, op);
	} END_FOR_EACH_PTR(op);

	/* and finally, look if it clobbers memory */
	FOR_EACH_PTR(stmt->asm_clobbers, clob) {
		if (!strcmp(clob->string->data, "memory"))
			insn->clobber_memory = 1;
	} END_FOR_EACH_PTR(clob);

	return VOID;
}

static int multijmp_cmp(const void *_a, const void *_b)
{
	const struct multijmp *a = _a;
	const struct multijmp *b = _b;

	// "default" case?
	if (a->begin > a->end) {
		if (b->begin > b->end)
			return 0;
		return 1;
	}
	if (b->begin > b->end)
		return -1;
	if (a->begin == b->begin) {
		if (a->end == b->end)
			return 0;
		return (a->end < b->end) ? -1 : 1;
	}
	return a->begin < b->begin ? -1 : 1;
}

static void sort_switch_cases(struct instruction *insn)
{
	sort_list((struct ptr_list **)&insn->multijmp_list, multijmp_cmp);
}

static pseudo_t linearize_declaration(struct entrypoint *ep, struct statement *stmt)
{
	struct symbol *sym;

	concat_symbol_list(stmt->declaration, &ep->syms);

	FOR_EACH_PTR(stmt->declaration, sym) {
		linearize_one_symbol(ep, sym);
	} END_FOR_EACH_PTR(sym);
	return VOID;
}

static pseudo_t linearize_return(struct entrypoint *ep, struct statement *stmt)
{
	struct expression *expr = stmt->expression;
	struct symbol *ret = stmt->ret_target;
	struct basic_block *bb_return = get_bound_block(ep, ret);
	struct basic_block *active;
	pseudo_t src = linearize_expression(ep, expr);
	active = ep->active;
	if (active && !is_void_type(ret)) {
		add_return(ep, bb_return, ret, src);
	}
	add_goto(ep, bb_return);
	return VOID;
}

static pseudo_t linearize_switch(struct entrypoint *ep, struct statement *stmt)
{
	struct symbol *sym;
	struct instruction *switch_ins;
	struct basic_block *switch_end = alloc_basic_block(ep, stmt->pos);
	struct basic_block *active, *default_case;
	struct expression *expr = stmt->switch_expression;
	struct multijmp *jmp;
	pseudo_t pseudo;

	if (!expr || !expr->ctype)
		return VOID;
	pseudo = linearize_expression(ep, expr);
	active = ep->active;
	if (!active) {
		active = alloc_basic_block(ep, stmt->pos);
		set_activeblock(ep, active);
	}

	switch_ins = alloc_typed_instruction(OP_SWITCH, expr->ctype);
	use_pseudo(switch_ins, pseudo, &switch_ins->cond);
	add_one_insn(ep, switch_ins);
	finish_block(ep);

	default_case = NULL;
	FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
		struct statement *case_stmt = sym->stmt;
		struct basic_block *bb_case = get_bound_block(ep, sym);

		if (!case_stmt->case_expression) {
			default_case = bb_case;
			continue;
		} else if (case_stmt->case_expression->type != EXPR_VALUE) {
			continue;
		} else {
			struct expression *case_to = case_stmt->case_to;
			long long begin, end;

			begin = end = case_stmt->case_expression->value;
			if (case_to && case_to->type == EXPR_VALUE)
				end = case_to->value;
			if (begin > end)
				jmp = alloc_multijmp(bb_case, end, begin);
			else
				jmp = alloc_multijmp(bb_case, begin, end);

		}
		add_multijmp(&switch_ins->multijmp_list, jmp);
		add_bb(&bb_case->parents, active);
		add_bb(&active->children, bb_case);
	} END_FOR_EACH_PTR(sym);

	bind_label(stmt->switch_break, switch_end, stmt->pos);

	/* And linearize the actual statement */
	linearize_statement(ep, stmt->switch_statement);
	set_activeblock(ep, switch_end);

	if (!default_case)
		default_case = switch_end;

	jmp = alloc_multijmp(default_case, 1, 0);
	add_multijmp(&switch_ins->multijmp_list, jmp);
	add_bb(&default_case->parents, active);
	add_bb(&active->children, default_case);
	sort_switch_cases(switch_ins);

	return VOID;
}

static pseudo_t linearize_iterator(struct entrypoint *ep, struct statement *stmt)
{
	struct statement  *pre_statement = stmt->iterator_pre_statement;
	struct expression *pre_condition = stmt->iterator_pre_condition;
	struct statement  *statement = stmt->iterator_statement;
	struct statement  *post_statement = stmt->iterator_post_statement;
	struct expression *post_condition = stmt->iterator_post_condition;
	struct basic_block *loop_top, *loop_body, *loop_continue, *loop_end;
	struct symbol *sym;

	FOR_EACH_PTR(stmt->iterator_syms, sym) {
		linearize_one_symbol(ep, sym);
	} END_FOR_EACH_PTR(sym);
	concat_symbol_list(stmt->iterator_syms, &ep->syms);
	linearize_statement(ep, pre_statement);

	loop_body = loop_top = alloc_basic_block(ep, stmt->pos);
	loop_continue = alloc_basic_block(ep, stmt->pos);
	loop_end = alloc_basic_block(ep, stmt->pos);

	/* An empty post-condition means that it's the same as the pre-condition */
	if (!post_condition) {
		loop_top = alloc_basic_block(ep, stmt->pos);
		set_activeblock(ep, loop_top);
	}

	if (pre_condition)
			linearize_cond_branch(ep, pre_condition, loop_body, loop_end);

	bind_label(stmt->iterator_continue, loop_continue, stmt->pos);
	bind_label(stmt->iterator_break, loop_end, stmt->pos);

	set_activeblock(ep, loop_body);
	linearize_statement(ep, statement);
	add_goto(ep, loop_continue);

	set_activeblock(ep, loop_continue);
	linearize_statement(ep, post_statement);
	if (!post_condition)
		add_goto(ep, loop_top);
	else
		linearize_cond_branch(ep, post_condition, loop_top, loop_end);
	set_activeblock(ep, loop_end);

	return VOID;
}

static pseudo_t linearize_statement(struct entrypoint *ep, struct statement *stmt)
{
	struct basic_block *bb;

	if (!stmt)
		return VOID;

	bb = ep->active;
	if (bb && !bb->insns)
		bb->pos = stmt->pos;
	current_pos = stmt->pos;

	switch (stmt->type) {
	case STMT_NONE:
		break;

	case STMT_DECLARATION:
		return linearize_declaration(ep, stmt);

	case STMT_CONTEXT:
		return linearize_context(ep, stmt);

	case STMT_RANGE:
		return linearize_range(ep, stmt);

	case STMT_EXPRESSION:
		return linearize_expression(ep, stmt->expression);

	case STMT_ASM:
		return linearize_asm_statement(ep, stmt);

	case STMT_RETURN:
		return linearize_return(ep, stmt);

	case STMT_CASE: {
		add_label(ep, stmt->case_label);
		linearize_statement(ep, stmt->case_statement);
		break;
	}

	case STMT_LABEL: {
		struct symbol *label = stmt->label_identifier;

		if (label->used) {
			add_label(ep, label);
		}
		return linearize_statement(ep, stmt->label_statement);
	}

	case STMT_GOTO: {
		struct symbol *sym;
		struct expression *expr;
		struct instruction *goto_ins;
		struct basic_block *active;
		pseudo_t pseudo;

		active = ep->active;
		if (!bb_reachable(active))
			break;

		if (stmt->goto_label) {
			add_goto(ep, get_bound_block(ep, stmt->goto_label));
			break;
		}

		expr = stmt->goto_expression;
		if (!expr)
			break;

		/* This can happen as part of simplification */
		if (expr->type == EXPR_LABEL) {
			add_goto(ep, get_bound_block(ep, expr->label_symbol));
			break;
		}

		pseudo = linearize_expression(ep, expr);
		goto_ins = alloc_instruction(OP_COMPUTEDGOTO, 0);
		use_pseudo(goto_ins, pseudo, &goto_ins->src);
		add_one_insn(ep, goto_ins);

		FOR_EACH_PTR(stmt->target_list, sym) {
			struct basic_block *bb_computed = get_bound_block(ep, sym);
			struct multijmp *jmp = alloc_multijmp(bb_computed, 1, 0);
			add_multijmp(&goto_ins->multijmp_list, jmp);
			add_bb(&bb_computed->parents, ep->active);
			add_bb(&active->children, bb_computed);
		} END_FOR_EACH_PTR(sym);

		finish_block(ep);
		break;
	}

	case STMT_COMPOUND:
		if (stmt->inline_fn)
			return linearize_inlined_call(ep, stmt);
		return linearize_compound_statement(ep, stmt);

	/*
	 * This could take 'likely/unlikely' into account, and
	 * switch the arms around appropriately..
	 */
	case STMT_IF: {
		struct basic_block *bb_true, *bb_false, *endif;
 		struct expression *cond = stmt->if_conditional;

		bb_true = alloc_basic_block(ep, stmt->pos);
		bb_false = endif = alloc_basic_block(ep, stmt->pos);

		// If the condition is invalid, the following
		// statement(s) are not evaluated.
		if (!cond || !valid_type(cond->ctype))
			return VOID;
 		linearize_cond_branch(ep, cond, bb_true, bb_false);

		set_activeblock(ep, bb_true);
 		linearize_statement(ep, stmt->if_true);
 
 		if (stmt->if_false) {
			endif = alloc_basic_block(ep, stmt->pos);
			add_goto(ep, endif);
			set_activeblock(ep, bb_false);
 			linearize_statement(ep, stmt->if_false);
		}
		set_activeblock(ep, endif);
		break;
	}

	case STMT_SWITCH:
		return linearize_switch(ep, stmt);

	case STMT_ITERATOR:
		return linearize_iterator(ep, stmt);

	default:
		break;
	}
	return VOID;
}

static void check_tainted_insn(struct instruction *insn)
{
	unsigned long long uval;
	long long sval;
	pseudo_t src2;

	switch (insn->opcode) {
	case OP_DIVU: case OP_DIVS:
	case OP_MODU: case OP_MODS:
		if (insn->src2 == value_pseudo(0))
			warning(insn->pos, "divide by zero");
		break;
	case OP_SHL: case OP_LSR: case OP_ASR:
		src2 = insn->src2;
		if (src2->type != PSEUDO_VAL)
			break;
		uval = src2->value;
		if (uval < insn->size)
			break;
		sval = sign_extend(uval, insn->size);
		if (Wshift_count_negative && sval < 0)
			warning(insn->pos, "shift count is negative (%lld)", sval);
		else if (Wshift_count_overflow)
			warning(insn->pos, "shift too big (%llu) for type %s", uval, show_typename(insn->type));
	}
}

///
// issue warnings after all possible DCE
static void late_warnings(struct entrypoint *ep)
{
	struct basic_block *bb;
	FOR_EACH_PTR(ep->bbs, bb) {
		struct instruction *insn;
		FOR_EACH_PTR(bb->insns, insn) {
			if (!insn->bb)
				continue;
			if (insn->tainted)
				check_tainted_insn(insn);
			switch (insn->opcode) {
			case OP_LOAD:
				// Check for illegal offsets.
				check_access(insn);
				break;
			}
		} END_FOR_EACH_PTR(insn);
	} END_FOR_EACH_PTR(bb);
}

static struct entrypoint *linearize_fn(struct symbol *sym, struct symbol *base_type)
{
	struct statement *stmt = base_type->stmt;
	struct entrypoint *ep;
	struct basic_block *bb;
	struct symbol *ret_type;
	struct symbol *arg;
	struct instruction *entry;
	struct instruction *ret;
	pseudo_t result;
	int i;

	if (!stmt || sym->bogus_linear)
		return NULL;

	ep = alloc_entrypoint();
	ep->name = sym;
	sym->ep = ep;
	bb = alloc_basic_block(ep, sym->pos);
	set_activeblock(ep, bb);

	if (stmt->type == STMT_ASM) {	// top-level asm
		linearize_asm_statement(ep, stmt);
		return ep;
	}

	entry = alloc_instruction(OP_ENTRY, 0);
	add_one_insn(ep, entry);
	ep->entry = entry;

	concat_symbol_list(base_type->arguments, &ep->syms);

	/* FIXME!! We should do something else about varargs.. */
	i = 0;
	FOR_EACH_PTR(base_type->arguments, arg) {
		linearize_argument(ep, arg, ++i);
	} END_FOR_EACH_PTR(arg);

	result = linearize_fn_statement(ep, stmt);
	ret_type = base_type->ctype.base_type;
	ret = alloc_typed_instruction(OP_RET, ret_type);
	if (type_size(ret_type) > 0)
		use_pseudo(ret, result, &ret->src);
	add_one_insn(ep, ret);

	optimize(ep);
	late_warnings(ep);
	return ep;
}

struct entrypoint *linearize_symbol(struct symbol *sym)
{
	struct symbol *base_type;

	if (!sym)
		return NULL;
	current_pos = sym->pos;
	base_type = sym->ctype.base_type;
	if (!base_type)
		return NULL;
	if (base_type->type == SYM_FN)
		return linearize_fn(sym, base_type);
	return NULL;
}

/*
 * Builtin functions
 */

static pseudo_t linearize_fma(struct entrypoint *ep, struct expression *expr)
{
	struct instruction *insn = alloc_typed_instruction(OP_FMADD, expr->ctype);
	struct expression *arg;

	PREPARE_PTR_LIST(expr->args, arg);
		use_pseudo(insn, linearize_expression(ep, arg), &insn->src1);
		NEXT_PTR_LIST(arg)
		use_pseudo(insn, linearize_expression(ep, arg), &insn->src2);
		NEXT_PTR_LIST(arg)
		use_pseudo(insn, linearize_expression(ep, arg), &insn->src3);
	FINISH_PTR_LIST(arg);

	add_one_insn(ep, insn);
	return insn->target = alloc_pseudo(insn);
}

static pseudo_t linearize_isdigit(struct entrypoint *ep, struct expression *expr)
{
	struct instruction *insn;
	pseudo_t src;

	insn = alloc_typed_instruction(OP_SUB, &int_ctype);
	src = linearize_expression(ep, first_expression(expr->args));
	use_pseudo(insn, src, &insn->src1);
	insn->src2 = value_pseudo('0');
	src = insn->target = alloc_pseudo(insn);
	add_one_insn(ep, insn);

	insn = alloc_typed_instruction(OP_SET_BE, &int_ctype);
	use_pseudo(insn, src, &insn->src1);
	insn->src2 = value_pseudo(9);
	insn->target = alloc_pseudo(insn);
	insn->itype = &int_ctype;
	add_one_insn(ep, insn);

	return insn->target;
}

static pseudo_t linearize_unreachable(struct entrypoint *ep, struct expression *exp)
{
	add_unreachable(ep);
	return VOID;
}

static struct sym_init {
	const char *name;
	pseudo_t (*linearize)(struct entrypoint *, struct expression*);
	struct symbol_op op;
} builtins_table[] = {
	// must be declared in builtin.c:declare_builtins[]
	{ "__builtin_fma", linearize_fma },
	{ "__builtin_fmaf", linearize_fma },
	{ "__builtin_fmal", linearize_fma },
	{ "__builtin_isdigit", linearize_isdigit },
	{ "__builtin_unreachable", linearize_unreachable },
	{ }
};

void init_linearized_builtins(int stream)
{
	struct sym_init *ptr;

	for (ptr = builtins_table; ptr->name; ptr++) {
		struct symbol *sym;
		sym = create_symbol(stream, ptr->name, SYM_NODE, NS_SYMBOL);
		if (!sym->op)
			sym->op = &ptr->op;
		sym->op->type |= KW_BUILTIN;
		sym->op->linearize = ptr->linearize;
	}
}