summaryrefslogtreecommitdiff
path: root/tccgen.c
blob: c1fb2915ecbb1c6cc5378a180018cdddab6891a7 (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
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
/*
 *  TCC - Tiny C Compiler
 * 
 *  Copyright (c) 2001-2004 Fabrice Bellard
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#define USING_GLOBALS
#include "tcc.h"

/********************************************************/
/* global variables */

/* loc : local variable index
   ind : output code index
   rsym: return symbol
   anon_sym: anonymous symbol index
*/
//static SValue _vstack[1 + VSTACK_SIZE];
#define vstack (S->tccgen__vstack + 1)

#define unevalmask 0xffff /* unevaluated subexpression */
#define NODATA_WANTED (S->tccgen_nocode_wanted > 0) /* no static data output wanted either */
#define STATIC_DATA_WANTED (S->tccgen_nocode_wanted & 0xC0000000) /* only static data output */

/* Automagical code suppression ----> */
#define CODE_OFF() (S->tccgen_nocode_wanted |= 0x20000000)
#define CODE_ON() (S->tccgen_nocode_wanted &= ~0x20000000)

static void tcc_tcov_block_begin(TCCState* S);

/* Clear 'nocode_wanted' at label if it was used */
ST_FUNC void gsym(TCCState* S, int t) { if (t) { gsym_addr(S, t, S->tccgen_ind); CODE_ON(); }}
static int gind(TCCState* S) { int t = S->tccgen_ind; CODE_ON(); if (S->tccgen_debug_modes) tcc_tcov_block_begin(S); return t; }

/* Set 'nocode_wanted' after unconditional jumps */
static void gjmp_addr_acs(TCCState* S, int t) { gjmp_addr(S, t); CODE_OFF(); }
static int gjmp_acs(TCCState* S, int t) { t = gjmp(S, t); CODE_OFF(); return t; }

/* These are #undef'd at the end of this file */
#define gjmp_addr gjmp_addr_acs
#define gjmp gjmp_acs
/* <---- */

#if PTR_SIZE == 4
#define VT_SIZE_T (VT_INT | VT_UNSIGNED)
#define VT_PTRDIFF_T VT_INT
#elif LONG_SIZE == 4
#define VT_SIZE_T (VT_LLONG | VT_UNSIGNED)
#define VT_PTRDIFF_T VT_LLONG
#else
#define VT_SIZE_T (VT_LONG | VT_LLONG | VT_UNSIGNED)
#define VT_PTRDIFF_T (VT_LONG | VT_LLONG)
#endif

typedef struct {
    Section *sec;
    int local_offset;
    Sym *flex_array_ref;
} init_params;

#if 1
#define precedence_parser
static void init_prec(TCCState* S);
#endif

/********************************************************/
/* stab debug support */

static const struct {
  int type;
  const char *name;
} default_debug[] = {
    {   VT_INT, "int:t1=r1;-2147483648;2147483647;" },
    {   VT_BYTE, "char:t2=r2;0;127;" },
#if LONG_SIZE == 4
    {   VT_LONG | VT_INT, "long int:t3=r3;-2147483648;2147483647;" },
#else
    {   VT_LLONG | VT_LONG, "long int:t3=r3;-9223372036854775808;9223372036854775807;" },
#endif
    {   VT_INT | VT_UNSIGNED, "unsigned int:t4=r4;0;037777777777;" },
#if LONG_SIZE == 4
    {   VT_LONG | VT_INT | VT_UNSIGNED, "long unsigned int:t5=r5;0;037777777777;" },
#else
    /* use octal instead of -1 so size_t works (-gstabs+ in gcc) */
    {   VT_LLONG | VT_LONG | VT_UNSIGNED, "long unsigned int:t5=r5;0;01777777777777777777777;" },
#endif
    {   VT_QLONG, "__int128:t6=r6;0;-1;" },
    {   VT_QLONG | VT_UNSIGNED, "__int128 unsigned:t7=r7;0;-1;" },
    {   VT_LLONG, "long long int:t8=r8;-9223372036854775808;9223372036854775807;" },
    {   VT_LLONG | VT_UNSIGNED, "long long unsigned int:t9=r9;0;01777777777777777777777;" },
    {   VT_SHORT, "short int:t10=r10;-32768;32767;" },
    {   VT_SHORT | VT_UNSIGNED, "short unsigned int:t11=r11;0;65535;" },
    {   VT_BYTE | VT_DEFSIGN, "signed char:t12=r12;-128;127;" },
    {   VT_BYTE | VT_DEFSIGN | VT_UNSIGNED, "unsigned char:t13=r13;0;255;" },
    {   VT_FLOAT, "float:t14=r1;4;0;" },
    {   VT_DOUBLE, "double:t15=r1;8;0;" },
#ifdef TCC_USING_DOUBLE_FOR_LDOUBLE
    {   VT_DOUBLE | VT_LONG, "long double:t16=r1;8;0;" },
#else
    {   VT_LDOUBLE, "long double:t16=r1;16;0;" },
#endif
    {   -1, "_Float32:t17=r1;4;0;" },
    {   -1, "_Float64:t18=r1;8;0;" },
    {   -1, "_Float128:t19=r1;16;0;" },
    {   -1, "_Float32x:t20=r1;8;0;" },
    {   -1, "_Float64x:t21=r1;16;0;" },
    {   -1, "_Decimal32:t22=r1;4;0;" },
    {   -1, "_Decimal64:t23=r1;8;0;" },
    {   -1, "_Decimal128:t24=r1;16;0;" },
    /* if default char is unsigned */
    {   VT_BYTE | VT_UNSIGNED, "unsigned char:t25=r25;0;255;" },
    /* boolean type */
    {   VT_BOOL, "bool:t26=r26;0;255;" },
    {   VT_VOID, "void:t27=27" },
};

/********************************************************/
static void gen_cast(TCCState* S, CType *type);
static void gen_cast_s(TCCState* S, int t);
static inline CType *pointed_type(CType *type);
static int is_compatible_types(CType *type1, CType *type2);
static int parse_btype(TCCState* S, CType *type, AttributeDef *ad);
static CType *type_decl(TCCState* S, CType *type, AttributeDef *ad, int *v, int td);
static void parse_expr_type(TCCState* S, CType *type);
static void init_putv(TCCState* S, init_params *p, CType *type, unsigned long c);
static void decl_initializer(TCCState* S, init_params *p, CType *type, unsigned long c, int flags);
static void block(TCCState* S, int is_expr);
static void decl_initializer_alloc(TCCState* S, CType *type, AttributeDef *ad, int r, int has_init, int v, int scope);
static void decl(TCCState* S, int l);
static int decl0(TCCState* S, int l, int is_for_loop_init, Sym *);
static void expr_eq(TCCState* S);
static void vla_runtime_type_size(TCCState* S, CType *type, int *a);
static int is_compatible_unqualified_types(CType *type1, CType *type2);
static inline int64_t expr_const64(TCCState* S);
static void vpush64(TCCState* S, int ty, unsigned long long v);
static void vpush(TCCState* S, CType *type);
static int gvtst(TCCState* S, int inv, int t);
static void gen_inline_functions(TCCState *S);
static void free_inline_functions(TCCState *S);
static void skip_or_save_block(TCCState* S, TokenString **str);
static void gv_dup(TCCState* S);
static int get_temp_local_var(TCCState* S, int size,int align);
static void clear_temp_local_var_list();
static void cast_error(TCCState* S, CType *st, CType *dt);

ST_INLN int is_float(int t)
{
    int bt = t & VT_BTYPE;
    return bt == VT_LDOUBLE
        || bt == VT_DOUBLE
        || bt == VT_FLOAT
        || bt == VT_QFLOAT;
}

static inline int is_integer_btype(int bt)
{
    return bt == VT_BYTE
        || bt == VT_BOOL
        || bt == VT_SHORT
        || bt == VT_INT
        || bt == VT_LLONG;
}

static int btype_size(int bt)
{
    return bt == VT_BYTE || bt == VT_BOOL ? 1 :
        bt == VT_SHORT ? 2 :
        bt == VT_INT ? 4 :
        bt == VT_LLONG ? 8 :
        bt == VT_PTR ? PTR_SIZE : 0;
}

/* returns function return register from type */
static int R_RET(int t)
{
    if (!is_float(t))
        return REG_IRET;
#ifdef TCC_TARGET_X86_64
    if ((t & VT_BTYPE) == VT_LDOUBLE)
        return TREG_ST0;
#elif defined TCC_TARGET_RISCV64
    if ((t & VT_BTYPE) == VT_LDOUBLE)
        return REG_IRET;
#endif
    return REG_FRET;
}

/* returns 2nd function return register, if any */
static int R2_RET(int t)
{
    t &= VT_BTYPE;
#if PTR_SIZE == 4
    if (t == VT_LLONG)
        return REG_IRE2;
#elif defined TCC_TARGET_X86_64
    if (t == VT_QLONG)
        return REG_IRE2;
    if (t == VT_QFLOAT)
        return REG_FRE2;
#elif defined TCC_TARGET_RISCV64
    if (t == VT_LDOUBLE)
        return REG_IRE2;
#endif
    return VT_CONST;
}

/* returns true for two-word types */
#define USING_TWO_WORDS(t) (R2_RET(t) != VT_CONST)

/* put function return registers to stack value */
static void PUT_R_RET(SValue *sv, int t)
{
    sv->r = R_RET(t), sv->r2 = R2_RET(t);
}

/* returns function return register class for type t */
static int RC_RET(int t)
{
    return reg_classes[R_RET(t)] & ~(RC_FLOAT | RC_INT);
}

/* returns generic register class for type t */
static int RC_TYPE(int t)
{
    if (!is_float(t))
        return RC_INT;
#ifdef TCC_TARGET_X86_64
    if ((t & VT_BTYPE) == VT_LDOUBLE)
        return RC_ST0;
    if ((t & VT_BTYPE) == VT_QFLOAT)
        return RC_FRET;
#elif defined TCC_TARGET_RISCV64
    if ((t & VT_BTYPE) == VT_LDOUBLE)
        return RC_INT;
#endif
    return RC_FLOAT;
}

/* returns 2nd register class corresponding to t and rc */
static int RC2_TYPE(int t, int rc)
{
    if (!USING_TWO_WORDS(t))
        return 0;
#ifdef RC_IRE2
    if (rc == RC_IRET)
        return RC_IRE2;
#endif
#ifdef RC_FRE2
    if (rc == RC_FRET)
        return RC_FRE2;
#endif
    if (rc & RC_FLOAT)
        return RC_FLOAT;
    return RC_INT;
}

/* we use our own 'finite' function to avoid potential problems with
   non standard math libs */
/* XXX: endianness dependent */
ST_FUNC int ieee_finite(double d)
{
    int p[4];
    memcpy(p, &d, sizeof(double));
    return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
}

/* compiling intel long double natively */
#if (defined __i386__ || defined __x86_64__) \
    && (defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64)
# define TCC_IS_NATIVE_387
#endif

ST_FUNC void test_lvalue(TCCState* S)
{
    if (!(S->tccgen_vtop->r & VT_LVAL))
        expect(S, "lvalue");
}

ST_FUNC void check_vstack(TCCState* S)
{
    if (S->tccgen_vtop != vstack - 1)
        tcc_error(S, "internal compiler error: vstack leak (%d)",
                  (int)(S->tccgen_vtop - vstack + 1));
}

/* ------------------------------------------------------------------------- */
/* vstack debugging aid */

#if 0
void pv (const char *lbl, int a, int b)
{
    int i;
    for (i = a; i < a + b; ++i) {
        SValue *p = &S->tccgen_vtop[-i];
        printf("%s vtop[-%d] : type.t:%04x  r:%04x  r2:%04x  c.i:%d\n",
            lbl, i, p->type.t, p->r, p->r2, (int)p->c.i);
    }
}
#endif

/* ------------------------------------------------------------------------- */
/* start of translation unit info */
ST_FUNC void tcc_debug_start(TCCState *S)
{
    if (S->do_debug) {
        int i;
        char buf[512];

        /* file info: full path + filename */
        S->tccgen_section_sym = put_elf_sym(S, symtab_section, 0, 0,
                                  ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
                                  text_section->sh_num, NULL);
        getcwd(buf, sizeof(buf));
#ifdef _WIN32
        normalize_slashes(buf);
#endif
        pstrcat(buf, sizeof(buf), "/");
        put_stabs_r(S, buf, N_SO, 0, 0,
                    text_section->data_offset, text_section, S->tccgen_section_sym);
        put_stabs_r(S, S->tccpp_file->prev ? S->tccpp_file->prev->filename : S->tccpp_file->filename,
                    N_SO, 0, 0,
                    text_section->data_offset, text_section, S->tccgen_section_sym);
        for (i = 0; i < sizeof (default_debug) / sizeof (default_debug[0]); i++)
            put_stabs(S, default_debug[i].name, N_LSYM, 0, 0, 0);

        S->tccgen_new_file = S->tccgen_last_line_num = 0;
        S->tccgen_func_ind = -1;
        S->tccgen_debug_next_type = sizeof(default_debug) / sizeof(default_debug[0]);
        S->tccgen_debug_hash = NULL;
        S->tccgen_n_debug_hash = 0;

        /* we're currently 'including' the <command line> */
        tcc_debug_bincl(S);
    }

    /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
       symbols can be safely used */
    put_elf_sym(S, symtab_section, 0, 0,
                ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
                SHN_ABS, S->tccpp_file->filename);
}

/* put end of translation unit info */
ST_FUNC void tcc_debug_end(TCCState *S)
{
    if (!S->do_debug)
        return;
    put_stabs_r(S, NULL, N_SO, 0, 0,
        text_section->data_offset, text_section, S->tccgen_section_sym);
    tcc_free(S, S->tccgen_debug_hash);
}

static BufferedFile* put_new_file(TCCState *S)
{
    BufferedFile *f = S->tccpp_file;
    /* use upper file if from inline ":asm:" */
    if (f->filename[0] == ':')
        f = f->prev;
    if (f && S->tccgen_new_file) {
        put_stabs_r(S, f->filename, N_SOL, 0, 0, S->tccgen_ind, text_section, S->tccgen_section_sym);
        S->tccgen_new_file = S->tccgen_last_line_num = 0;
    }
    return f;
}

/* put alternative filename */
ST_FUNC void tcc_debug_putfile(TCCState *S, const char *filename)
{
    if (0 == strcmp(S->tccpp_file->filename, filename))
        return;
    pstrcpy(S->tccpp_file->filename, sizeof(S->tccpp_file->filename), filename);
    S->tccgen_new_file = 1;
}

/* begin of #include */
ST_FUNC void tcc_debug_bincl(TCCState *S)
{
    if (!S->do_debug)
        return;
    put_stabs(S, S->tccpp_file->filename, N_BINCL, 0, 0, 0);
    S->tccgen_new_file = 1;
}

/* end of #include */
ST_FUNC void tcc_debug_eincl(TCCState *S)
{
    if (!S->do_debug)
        return;
    put_stabn(S, N_EINCL, 0, 0, 0);
    S->tccgen_new_file = 1;
}

/* generate line number info */
static void tcc_debug_line(TCCState *S)
{
    BufferedFile *f;
    if (!S->do_debug
        || cur_text_section != text_section
        || !(f = put_new_file(S))
        || S->tccgen_last_line_num == f->line_num)
        return;
    if (S->tccgen_func_ind != -1) {
        put_stabn(S, N_SLINE, 0, f->line_num, S->tccgen_ind - S->tccgen_func_ind);
    } else {
        /* from tcc_assemble */
        put_stabs_r(S, NULL, N_SLINE, 0, f->line_num, S->tccgen_ind, text_section, S->tccgen_section_sym);
    }
    S->tccgen_last_line_num = f->line_num;
}

static void tcc_debug_stabs (TCCState *S, const char *str, int type, unsigned long value,
                             Section *sec, int sym_index)
{
    struct debug_sym *s;

    if (S->tccgen_debug_info) {
        S->tccgen_debug_info->sym =
            (struct debug_sym *)tcc_realloc (S, S->tccgen_debug_info->sym,
                                             sizeof(struct debug_sym) *
                                             (S->tccgen_debug_info->n_sym + 1));
        s = S->tccgen_debug_info->sym + S->tccgen_debug_info->n_sym++;
        s->type = type;
        s->value = value;
        s->str = tcc_strdup(S, str);
        s->sec = sec;
        s->sym_index = sym_index;
    }
    else if (sec)
        put_stabs_r (S, str, type, 0, 0, value, sec, sym_index);
    else
        put_stabs (S, str, type, 0, 0, value);
}

static void tcc_debug_stabn(TCCState *S, int type, int value)
{
    if (!S->do_debug)
        return;
    if (type == N_LBRAC) {
        debug_info_t *info =
            (debug_info_t *) tcc_mallocz(S, sizeof (*info));

        info->start = value;
        info->parent = S->tccgen_debug_info;
        if (S->tccgen_debug_info) {
            if (S->tccgen_debug_info->child) {
                if (S->tccgen_debug_info->child->last)
                    S->tccgen_debug_info->child->last->next = info;
                else
                    S->tccgen_debug_info->child->next = info;
                S->tccgen_debug_info->child->last = info;
            }
            else
                S->tccgen_debug_info->child = info;
        }
        else
            S->tccgen_debug_info_root = info;
        S->tccgen_debug_info = info;
    }
    else {
        S->tccgen_debug_info->end = value;
        S->tccgen_debug_info = S->tccgen_debug_info->parent;
    }
}

static void tcc_get_debug_info(TCCState *S, Sym *s, CString *result)
{
    int type;
    int n = 0;
    int debug_type = -1;
    Sym *t = s;
    CString str;

    for (;;) {
        type = t->type.t & ~(VT_STORAGE | VT_CONSTANT | VT_VOLATILE);
        if ((type & VT_BTYPE) != VT_BYTE)
            type &= ~VT_DEFSIGN;
        if (type == VT_PTR || type == (VT_PTR | VT_ARRAY))
            n++, t = t->type.ref;
        else
            break;
    }
    if ((type & VT_BTYPE) == VT_STRUCT) {
        int i;

        t = t->type.ref;
        for (i = 0; i < S->tccgen_n_debug_hash; i++) {
            if (t == S->tccgen_debug_hash[i].type) {
                debug_type = S->tccgen_debug_hash[i].debug_type;
                break;
            }
        }
        if (debug_type == -1) {
            debug_type = ++S->tccgen_debug_next_type;
            S->tccgen_debug_hash = (debug_hash_t *)
                tcc_realloc (S, S->tccgen_debug_hash,
                             (S->tccgen_n_debug_hash + 1) * sizeof(*S->tccgen_debug_hash));
            S->tccgen_debug_hash[S->tccgen_n_debug_hash].debug_type = debug_type;
            S->tccgen_debug_hash[S->tccgen_n_debug_hash++].type = t;
            cstr_new (S, &str);
            cstr_printf (S, &str, "%s:T%d=%c%d",
                         (t->v & ~SYM_STRUCT) >= SYM_FIRST_ANOM
                         ? "" : get_tok_str(S, t->v & ~SYM_STRUCT, NULL),
                         debug_type,
                         IS_UNION (t->type.t) ? 'u' : 's',
                         t->c);
            while (t->next) {
                int pos, size, align;

                t = t->next;
                cstr_printf (S, &str, "%s:",
                             (t->v & ~SYM_FIELD) >= SYM_FIRST_ANOM
                             ? "" : get_tok_str(S, t->v & ~SYM_FIELD, NULL));
                tcc_get_debug_info (S, t, &str);
                if (t->type.t & VT_BITFIELD) {
                    pos = t->c * 8 + BIT_POS(t->type.t);
                    size = BIT_SIZE(t->type.t);
                }
                else {
                    pos = t->c * 8;
                    size = type_size(&t->type, &align) * 8;
                }
                cstr_printf (S, &str, ",%d,%d;", pos, size);
            }
            cstr_printf (S, &str, ";");
            tcc_debug_stabs(S, str.data, N_LSYM, 0, NULL, 0);
            cstr_free (S, &str);
        }
    }
    else if (IS_ENUM(type)) {
        Sym *e = t = t->type.ref;

        debug_type = ++S->tccgen_debug_next_type;
        cstr_new (S, &str);
        cstr_printf (S, &str, "%s:T%d=e",
                     (t->v & ~SYM_STRUCT) >= SYM_FIRST_ANOM
                     ? "" : get_tok_str(S, t->v & ~SYM_STRUCT, NULL),
                     debug_type);
        while (t->next) {
            t = t->next;
            cstr_printf (S, &str, "%s:",
                         (t->v & ~SYM_FIELD) >= SYM_FIRST_ANOM
                         ? "" : get_tok_str(S, t->v & ~SYM_FIELD, NULL));
            cstr_printf (S, &str, e->type.t & VT_UNSIGNED ? "%u," : "%d,",
                         (int)t->enum_val);
        }
        cstr_printf (S, &str, ";");
        tcc_debug_stabs(S, str.data, N_LSYM, 0, NULL, 0);
        cstr_free (S, &str);
    }
    else if ((type & VT_BTYPE) != VT_FUNC) {
        type &= ~VT_STRUCT_MASK;
        for (debug_type = 1;
             debug_type <= sizeof(default_debug) / sizeof(default_debug[0]);
             debug_type++)
            if (default_debug[debug_type - 1].type == type)
                break;
        if (debug_type > sizeof(default_debug) / sizeof(default_debug[0]))
            return;
    }
    if (n > 0)
        cstr_printf (S, result, "%d=", ++S->tccgen_debug_next_type);
    t = s;
    for (;;) {
        type = t->type.t & ~(VT_STORAGE | VT_CONSTANT | VT_VOLATILE);
        if ((type & VT_BTYPE) != VT_BYTE)
            type &= ~VT_DEFSIGN;
        if (type == VT_PTR)
            cstr_printf (S, result, "%d=*", ++S->tccgen_debug_next_type);
        else if (type == (VT_PTR | VT_ARRAY))
            cstr_printf (S, result, "%d=ar1;0;%d;",
                         ++S->tccgen_debug_next_type, t->type.ref->c - 1);
        else if (type == VT_FUNC) {
            cstr_printf (S, result, "%d=f", ++S->tccgen_debug_next_type);
            tcc_get_debug_info (S, t->type.ref, result);
            return;
        }
        else
            break;
        t = t->type.ref;
    }
    cstr_printf (S, result, "%d", debug_type);
}

static void tcc_debug_finish (TCCState *S, debug_info_t *cur)
{
    while (cur) {
        int i;
        debug_info_t *next = cur->next;

        for (i = 0; i < cur->n_sym; i++) {
            struct debug_sym *s = &cur->sym[i];

            if (s->sec)
                put_stabs_r(S, s->str, s->type, 0, 0, s->value,
                            s->sec, s->sym_index);
            else
                put_stabs(S, s->str, s->type, 0, 0, s->value);
            tcc_free (S, s->str);
        }
        tcc_free (S, cur->sym);
        put_stabn(S, N_LBRAC, 0, 0, cur->start);
        tcc_debug_finish (S, cur->child);
        put_stabn(S, N_RBRAC, 0, 0, cur->end);
        tcc_free (S, cur);
        cur = next;
    }
}

static void tcc_add_debug_info(TCCState *S, int param, Sym *s, Sym *e)
{
    CString debug_str;
    if (!S->do_debug)
        return;
    cstr_new (S, &debug_str);
    for (; s != e; s = s->prev) {
        if (!s->v || (s->r & VT_VALMASK) != VT_LOCAL)
            continue;
        cstr_reset (&debug_str);
        cstr_printf (S, &debug_str, "%s:%s", get_tok_str(S, s->v, NULL), param ? "p" : "");
        tcc_get_debug_info(S, s, &debug_str);
        tcc_debug_stabs(S, debug_str.data, param ? N_PSYM : N_LSYM, s->c, NULL, 0);
    }
    cstr_free (S, &debug_str);
}

/* put function symbol */
static void tcc_debug_funcstart(TCCState *S, Sym *sym)
{
    CString debug_str;
    BufferedFile *f;
    if (!S->do_debug)
        return;
    S->tccgen_debug_info_root = NULL;
    S->tccgen_debug_info = NULL;
    tcc_debug_stabn(S, N_LBRAC, S->tccgen_ind - S->tccgen_func_ind);
    if (!(f = put_new_file(S)))
        return;
    cstr_new (S, &debug_str);
    cstr_printf(S, &debug_str, "%s:%c", S->tccgen_funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
    tcc_get_debug_info(S, sym->type.ref, &debug_str);
    put_stabs_r(S, debug_str.data, N_FUN, 0, f->line_num, 0, cur_text_section, sym->c);
    cstr_free (S, &debug_str);

    tcc_debug_line(S);
}

/* put function size */
static void tcc_debug_funcend(TCCState *S, int size)
{
    if (!S->do_debug)
        return;
    tcc_debug_stabn(S, N_RBRAC, size);
    tcc_debug_finish (S, S->tccgen_debug_info_root);
}


static void tcc_debug_extern_sym(TCCState *S, Sym *sym, int sh_num, int sym_bind, int sym_type)
{
    Section *s;
    CString str;

    if (!S->do_debug)
        return;
    if (sym_type == STT_FUNC || sym->v >= SYM_FIRST_ANOM)
        return;
    s = S->sections[sh_num];

    cstr_new (S, &str);
    cstr_printf (S, &str, "%s:%c",
        get_tok_str(S, sym->v, NULL),
        sym_bind == STB_GLOBAL ? 'G' : S->tccgen_local_scope ? 'V' : 'S'
        );
    tcc_get_debug_info(S, sym, &str);
    if (sym_bind == STB_GLOBAL)
        tcc_debug_stabs(S, str.data, N_GSYM, 0, NULL, 0);
    else
        tcc_debug_stabs(S, str.data,
            (sym->type.t & VT_STATIC) && data_section == s
            ? N_STSYM : N_LCSYM, 0, s, sym->c);
    cstr_free (S, &str);
}

static void tcc_debug_typedef(TCCState *S, Sym *sym)
{
    CString str;

    if (!S->do_debug)
        return;
    cstr_new (S, &str);
    cstr_printf (S, &str, "%s:t",
                 (sym->v & ~SYM_FIELD) >= SYM_FIRST_ANOM
                 ? "" : get_tok_str(S, sym->v & ~SYM_FIELD, NULL));
    tcc_get_debug_info(S, sym, &str);
    tcc_debug_stabs(S, str.data, N_LSYM, 0, NULL, 0);
    cstr_free (S, &str);
}

/* ------------------------------------------------------------------------- */
/* for section layout see lib/tcov.c */

static void tcc_tcov_block_end(TCCState* S, int line);

static void tcc_tcov_block_begin(TCCState* S)
{
    SValue sv;
    void *ptr;
    unsigned long last_offset = S->tccgen_tcov_data.offset;

    tcc_tcov_block_end (S, 0);
    if (S->test_coverage == 0 || S->tccgen_nocode_wanted)
	return;

    if (S->tccgen_tcov_data.last_file_name == 0 ||
	strcmp ((const char *)(tcov_section->data + S->tccgen_tcov_data.last_file_name),
		S->tccpp_file->true_filename) != 0) {
	char wd[1024];
	CString cstr;

	if (S->tccgen_tcov_data.last_func_name)
	    section_ptr_add(S, tcov_section, 1);
	if (S->tccgen_tcov_data.last_file_name)
	    section_ptr_add(S, tcov_section, 1);
	S->tccgen_tcov_data.last_func_name = 0;
	cstr_new (S, &cstr);
	if (S->tccpp_file->true_filename[0] == '/') {
	    S->tccgen_tcov_data.last_file_name = tcov_section->data_offset;
	    cstr_printf (S, &cstr, "%s", S->tccpp_file->true_filename);
	}
	else {
	    getcwd (wd, sizeof(wd));
	    S->tccgen_tcov_data.last_file_name = tcov_section->data_offset + strlen(wd) + 1;
	    cstr_printf (S, &cstr, "%s/%s", wd, S->tccpp_file->true_filename);
	}
	ptr = section_ptr_add(S, tcov_section, cstr.size + 1);
	strcpy((char *)ptr, cstr.data);
#ifdef _WIN32
        normalize_slashes((char *)ptr);
#endif
	cstr_free (S, &cstr);
    }
    if (S->tccgen_tcov_data.last_func_name == 0 ||
	strcmp ((const char *)(tcov_section->data + S->tccgen_tcov_data.last_func_name),
		S->tccgen_funcname) != 0) {
	size_t len;

	if (S->tccgen_tcov_data.last_func_name)
	    section_ptr_add(S, tcov_section, 1);
	S->tccgen_tcov_data.last_func_name = tcov_section->data_offset;
	len = strlen (S->tccgen_funcname);
	ptr = section_ptr_add(S, tcov_section, len + 1);
	strcpy((char *)ptr, S->tccgen_funcname);
	section_ptr_add(S, tcov_section, -tcov_section->data_offset & 7);
	ptr = section_ptr_add(S, tcov_section, 8);
	write64le (ptr, S->tccpp_file->line_num);
    }
    if (S->tccgen_ind == S->tccgen_tcov_data.ind && S->tccgen_tcov_data.line == S->tccpp_file->line_num)
        S->tccgen_tcov_data.offset = last_offset;
    else {
        Sym label = {0};
        label.type.t = VT_LLONG | VT_STATIC;

        ptr = section_ptr_add(S, tcov_section, 16);
        S->tccgen_tcov_data.line = S->tccpp_file->line_num;
        write64le (ptr, (S->tccgen_tcov_data.line << 8) | 0xff);
        put_extern_sym(S, &label, tcov_section,
		       ((unsigned char *)ptr - tcov_section->data) + 8, 0);
        sv.type = label.type;
        sv.r = VT_SYM | VT_LVAL | VT_CONST;
        sv.r2 = VT_CONST;
        sv.c.i = 0;
        sv.sym = &label;
#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 || \
    defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64 || \
    defined TCC_TARGET_RISCV64
        gen_increment_tcov (S, &sv);
#else
        vpushv(S, &sv);
        inc(S, 0, TOK_INC);
        vpop(S);
#endif
        S->tccgen_tcov_data.offset = (unsigned char *)ptr - tcov_section->data;
        S->tccgen_tcov_data.ind = S->tccgen_ind;
    }
}

static void tcc_tcov_block_end(TCCState* S, int line)
{
    if (S->test_coverage == 0)
	return;
    if (S->tccgen_tcov_data.offset) {
	void *ptr = tcov_section->data + S->tccgen_tcov_data.offset;
	unsigned long long nline = line ? line : S->tccpp_file->line_num;

	write64le (ptr, (read64le (ptr) & 0xfffffffffull) | (nline << 36));
	S->tccgen_tcov_data.offset = 0;
    }
}

static void tcc_tcov_check_line(TCCState* S, int start)
{
    if (S->test_coverage == 0)
	return;
    if (S->tccgen_tcov_data.line != S->tccpp_file->line_num) {
        if ((S->tccgen_tcov_data.line + 1) != S->tccpp_file->line_num) {
	    tcc_tcov_block_end (S, S->tccgen_tcov_data.line);
	    if (start)
                tcc_tcov_block_begin (S);
	}
	else
	    S->tccgen_tcov_data.line = S->tccpp_file->line_num;
    }
}

static void tcc_tcov_start(TCCState* S)
{
    if (S->test_coverage == 0)
	return;
    memset (&S->tccgen_tcov_data, 0, sizeof (S->tccgen_tcov_data));
    if (tcov_section == NULL) {
        tcov_section = new_section(S, ".tcov", SHT_PROGBITS,
				   SHF_ALLOC | SHF_WRITE);
	section_ptr_add(S, tcov_section, 4); // pointer to executable name
    }
}

static void tcc_tcov_end(TCCState* S)
{
    if (S->test_coverage == 0)
	return;
    if (S->tccgen_tcov_data.last_func_name)
        section_ptr_add(S, tcov_section, 1);
    if (S->tccgen_tcov_data.last_file_name)
        section_ptr_add(S, tcov_section, 1);
}

/* ------------------------------------------------------------------------- */
/* initialize vstack and types.  This must be done also for tcc -E */
ST_FUNC void tccgen_init(TCCState *S)
{
    S->tccgen_vtop = vstack - 1;
    memset(S->tccgen_vtop, 0, sizeof *S->tccgen_vtop);

    /* define some often used types */
    S->tccgen_int_type.t = VT_INT;

    S->tccgen_char_type.t = VT_BYTE;
    if (S->char_is_unsigned)
        S->tccgen_char_type.t |= VT_UNSIGNED;
    S->tccgen_char_pointer_type = S->tccgen_char_type;
    mk_pointer(S, &S->tccgen_char_pointer_type);

    S->tccgen_func_old_type.t = VT_FUNC;
    S->tccgen_func_old_type.ref = sym_push(S, SYM_FIELD, &S->tccgen_int_type, 0, 0);
    S->tccgen_func_old_type.ref->f.func_call = FUNC_CDECL;
    S->tccgen_func_old_type.ref->f.func_type = FUNC_OLD;
#ifdef precedence_parser
    init_prec(S);
#endif
    cstr_new(S, &S->tccgen_initstr);
}

ST_FUNC int tccgen_compile(TCCState *S)
{
    cur_text_section = NULL;
    S->tccgen_funcname = "";
    S->tccgen_anon_sym = SYM_FIRST_ANOM;
    S->tccgen_section_sym = 0;
    S->tccgen_const_wanted = 0;
    S->tccgen_nocode_wanted = 0x80000000;
    S->tccgen_local_scope = 0;
    S->tccgen_debug_modes = S->do_debug | S->test_coverage << 1;

    tcc_debug_start(S);
    tcc_tcov_start (S);
#ifdef TCC_TARGET_ARM
    arm_init(S);
#endif
#ifdef INC_DEBUG
    printf("%s: **** new file\n", file->filename);
#endif
    S->tccpp_parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM | PARSE_FLAG_TOK_STR;
    next(S);
    decl(S, VT_CONST);
    gen_inline_functions(S);
    check_vstack(S);
    /* end of translation unit info */
    tcc_debug_end(S);
    tcc_tcov_end (S);
    return 0;
}

ST_FUNC void tccgen_finish(TCCState *S)
{
    cstr_free(S, &S->tccgen_initstr);
    free_inline_functions(S);
    sym_pop(S, &S->tccgen_global_stack, NULL, 0);
    sym_pop(S, &S->tccgen_local_stack, NULL, 0);
    /* free preprocessor macros */
    free_defines(S, NULL);
    /* free sym_pools */
    dynarray_reset(S, &S->tccgen_sym_pools, &S->tccgen_nb_sym_pools);
    S->tccgen_sym_free_first = NULL;
}

/* ------------------------------------------------------------------------- */
ST_FUNC ElfSym *elfsym(TCCState* S, Sym *s)
{
  if (!s || !s->c)
    return NULL;
  return &((ElfSym *)symtab_section->data)[s->c];
}

/* apply storage attributes to Elf symbol */
ST_FUNC void update_storage(TCCState* S, Sym *sym)
{
    ElfSym *esym;
    int sym_bind, old_sym_bind;

    esym = elfsym(S, sym);
    if (!esym)
        return;

    if (sym->a.visibility)
        esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1))
            | sym->a.visibility;

    if (sym->type.t & (VT_STATIC | VT_INLINE))
        sym_bind = STB_LOCAL;
    else if (sym->a.weak)
        sym_bind = STB_WEAK;
    else
        sym_bind = STB_GLOBAL;
    old_sym_bind = ELFW(ST_BIND)(esym->st_info);
    if (sym_bind != old_sym_bind) {
        esym->st_info = ELFW(ST_INFO)(sym_bind, ELFW(ST_TYPE)(esym->st_info));
    }

#ifdef TCC_TARGET_PE
    if (sym->a.dllimport)
        esym->st_other |= ST_PE_IMPORT;
    if (sym->a.dllexport)
        esym->st_other |= ST_PE_EXPORT;
#endif

#if 0
    printf("storage %s: bind=%c vis=%d exp=%d imp=%d\n",
        get_tok_str(S, sym->v, NULL),
        sym_bind == STB_WEAK ? 'w' : sym_bind == STB_LOCAL ? 'l' : 'g',
        sym->a.visibility,
        sym->a.dllexport,
        sym->a.dllimport
        );
#endif
}

/* ------------------------------------------------------------------------- */
/* update sym->c so that it points to an external symbol in section
   'section' with value 'value' */

ST_FUNC void put_extern_sym2(TCCState* S, Sym *sym, int sh_num,
                            addr_t value, unsigned long size,
                            int can_add_underscore)
{
    int sym_type, sym_bind, info, other, t;
    ElfSym *esym;
    const char *name;
    char buf1[256];

    if (!sym->c) {
        name = get_tok_str(S, sym->v, NULL);
        t = sym->type.t;
        if ((t & VT_BTYPE) == VT_FUNC) {
            sym_type = STT_FUNC;
        } else if ((t & VT_BTYPE) == VT_VOID) {
            sym_type = STT_NOTYPE;
            if ((t & (VT_BTYPE|VT_ASM_FUNC)) == VT_ASM_FUNC)
                sym_type = STT_FUNC;
        } else {
            sym_type = STT_OBJECT;
        }
        if (t & (VT_STATIC | VT_INLINE))
            sym_bind = STB_LOCAL;
        else
            sym_bind = STB_GLOBAL;
        other = 0;

#ifdef TCC_TARGET_PE
        if (sym_type == STT_FUNC && sym->type.ref) {
            Sym *ref = sym->type.ref;
            if (ref->a.nodecorate) {
                can_add_underscore = 0;
            }
            if (ref->f.func_call == FUNC_STDCALL && can_add_underscore) {
                sprintf(buf1, "_%s@%d", name, ref->f.func_args * PTR_SIZE);
                name = buf1;
                other |= ST_PE_STDCALL;
                can_add_underscore = 0;
            }
        }
#endif

        if (sym->asm_label) {
            name = get_tok_str(S, sym->asm_label, NULL);
            can_add_underscore = 0;
        }

        if (S->leading_underscore && can_add_underscore) {
            buf1[0] = '_';
            pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
            name = buf1;
        }

        info = ELFW(ST_INFO)(sym_bind, sym_type);
        sym->c = put_elf_sym(S, symtab_section, value, size, info, other, sh_num, name);

        if (S->tccgen_debug_modes)
            tcc_debug_extern_sym(S, sym, sh_num, sym_bind, sym_type);

    } else {
        esym = elfsym(S, sym);
        esym->st_value = value;
        esym->st_size = size;
        esym->st_shndx = sh_num;
    }
    update_storage(S, sym);
}

ST_FUNC void put_extern_sym(TCCState* S, Sym *sym, Section *section,
                           addr_t value, unsigned long size)
{
    int sh_num = section ? section->sh_num : SHN_UNDEF;
    put_extern_sym2(S, sym, sh_num, value, size, 1);
}

/* add a new relocation entry to symbol 'sym' in section 's' */
ST_FUNC void greloca(TCCState* S, Section *s, Sym *sym, unsigned long offset, int type,
                     addr_t addend)
{
    int c = 0;

    if (S->tccgen_nocode_wanted && s == cur_text_section)
        return;

    if (sym) {
        if (0 == sym->c)
            put_extern_sym(S, sym, NULL, 0, 0);
        c = sym->c;
    }

    /* now we can add ELF relocation info */
    put_elf_reloca(symtab_section, s, offset, type, c, addend);
}

#if PTR_SIZE == 4
ST_FUNC void greloc(TCCState* S, Section *s, Sym *sym, unsigned long offset, int type)
{
    greloca(S, s, sym, offset, type, 0);
}
#endif

/* ------------------------------------------------------------------------- */
/* symbol allocator */
static Sym *__sym_malloc(TCCState* S)
{
    Sym *sym_pool, *sym, *last_sym;
    int i;

    sym_pool = tcc_malloc(S, SYM_POOL_NB * sizeof(Sym));
    dynarray_add(S, &S->tccgen_sym_pools, &S->tccgen_nb_sym_pools, sym_pool);

    last_sym = S->tccgen_sym_free_first;
    sym = sym_pool;
    for(i = 0; i < SYM_POOL_NB; i++) {
        sym->next = last_sym;
        last_sym = sym;
        sym++;
    }
    S->tccgen_sym_free_first = last_sym;
    return last_sym;
}

static inline Sym *sym_malloc(TCCState* S)
{
    Sym *sym;
#ifndef SYM_DEBUG
    sym = S->tccgen_sym_free_first;
    if (!sym)
        sym = __sym_malloc(S);
    S->tccgen_sym_free_first = sym->next;
    return sym;
#else
    sym = tcc_malloc(S, sizeof(Sym));
    return sym;
#endif
}

ST_INLN void sym_free(TCCState* S, Sym *sym)
{
#ifndef SYM_DEBUG
    sym->next = S->tccgen_sym_free_first;
    S->tccgen_sym_free_first = sym;
#else
    tcc_free(S, sym);
#endif
}

/* push, without hashing */
ST_FUNC Sym *sym_push2(TCCState* S, Sym **ps, int v, int t, int c)
{
    Sym *s;

    s = sym_malloc(S);
    memset(s, 0, sizeof *s);
    s->v = v;
    s->type.t = t;
    s->c = c;
    /* add in stack */
    s->prev = *ps;
    *ps = s;
    return s;
}

/* find a symbol and return its associated structure. 's' is the top
   of the symbol stack */
ST_FUNC Sym *sym_find2(Sym *s, int v)
{
    while (s) {
        if (s->v == v)
            return s;
        else if (s->v == -1)
            return NULL;
        s = s->prev;
    }
    return NULL;
}

/* structure lookup */
ST_INLN Sym *struct_find(TCCState* S, int v)
{
    v -= TOK_IDENT;
    if ((unsigned)v >= (unsigned)(S->tccpp_tok_ident - TOK_IDENT))
        return NULL;
    return S->tccpp_table_ident[v]->sym_struct;
}

/* find an identifier */
ST_INLN Sym *sym_find(TCCState* S, int v)
{
    v -= TOK_IDENT;
    if ((unsigned)v >= (unsigned)(S->tccpp_tok_ident - TOK_IDENT))
        return NULL;
    return S->tccpp_table_ident[v]->sym_identifier;
}

static int sym_scope(Sym *s)
{
  if (IS_ENUM_VAL (s->type.t))
    return s->type.ref->sym_scope;
  else
    return s->sym_scope;
}

/* push a given symbol on the symbol stack */
ST_FUNC Sym *sym_push(TCCState* S, int v, CType *type, int r, int c)
{
    Sym *s, **ps;
    TokenSym *ts;

    if (S->tccgen_local_stack)
        ps = &S->tccgen_local_stack;
    else
        ps = &S->tccgen_global_stack;
    s = sym_push2(S, ps, v, type->t, c);
    s->type.ref = type->ref;
    s->r = r;
    /* don't record fields or anonymous symbols */
    /* XXX: simplify */
    if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
        /* record symbol in token array */
        ts = S->tccpp_table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
        if (v & SYM_STRUCT)
            ps = &ts->sym_struct;
        else
            ps = &ts->sym_identifier;
        s->prev_tok = *ps;
        *ps = s;
        s->sym_scope = S->tccgen_local_scope;
        if (s->prev_tok && sym_scope(s->prev_tok) == s->sym_scope)
            tcc_error(S, "redeclaration of '%s'",
                get_tok_str(S, v & ~SYM_STRUCT, NULL));
    }
    return s;
}

/* push a global identifier */
ST_FUNC Sym *global_identifier_push(TCCState* S, int v, int t, int c)
{
    Sym *s, **ps;
    s = sym_push2(S, &S->tccgen_global_stack, v, t, c);
    s->r = VT_CONST | VT_SYM;
    /* don't record anonymous symbol */
    if (v < SYM_FIRST_ANOM) {
        ps = &S->tccpp_table_ident[v - TOK_IDENT]->sym_identifier;
        /* modify the top most local identifier, so that sym_identifier will
           point to 's' when popped; happens when called from inline asm */
        while (*ps != NULL && (*ps)->sym_scope)
            ps = &(*ps)->prev_tok;
        s->prev_tok = *ps;
        *ps = s;
    }
    return s;
}

/* pop symbols until top reaches 'b'.  If KEEP is non-zero don't really
   pop them yet from the list, but do remove them from the token array.  */
ST_FUNC void sym_pop(TCCState* S, Sym **ptop, Sym *b, int keep)
{
    Sym *s, *ss, **ps;
    TokenSym *ts;
    int v;

    s = *ptop;
    while(s != b) {
        ss = s->prev;
        v = s->v;
        /* remove symbol in token array */
        /* XXX: simplify */
        if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
            ts = S->tccpp_table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
            if (v & SYM_STRUCT)
                ps = &ts->sym_struct;
            else
                ps = &ts->sym_identifier;
            *ps = s->prev_tok;
        }
	if (!keep)
	    sym_free(S, s);
        s = ss;
    }
    if (!keep)
	*ptop = b;
}

/* ------------------------------------------------------------------------- */
static void vcheck_cmp(TCCState* S)
{
    /* cannot let cpu flags if other instruction are generated. Also
       avoid leaving VT_JMP anywhere except on the top of the stack
       because it would complicate the code generator.

       Don't do this when nocode_wanted.  vtop might come from
       !nocode_wanted regions (see 88_codeopt.c) and transforming
       it to a register without actually generating code is wrong
       as their value might still be used for real.  All values
       we push under nocode_wanted will eventually be popped
       again, so that the VT_CMP/VT_JMP value will be in vtop
       when code is unsuppressed again. */

    if (S->tccgen_vtop->r == VT_CMP && !S->tccgen_nocode_wanted)
        gv(S, RC_INT);
}

static void vsetc(TCCState* S, CType *type, int r, CValue *vc)
{
    if (S->tccgen_vtop >= vstack + (VSTACK_SIZE - 1))
        tcc_error(S, "memory full (vstack)");
    vcheck_cmp(S);
    S->tccgen_vtop++;
    S->tccgen_vtop->type = *type;
    S->tccgen_vtop->r = r;
    S->tccgen_vtop->r2 = VT_CONST;
    S->tccgen_vtop->c = *vc;
    S->tccgen_vtop->sym = NULL;
}

ST_FUNC void vswap(TCCState* S)
{
    SValue tmp;

    vcheck_cmp(S);
    tmp = S->tccgen_vtop[0];
    S->tccgen_vtop[0] = S->tccgen_vtop[-1];
    S->tccgen_vtop[-1] = tmp;
}

/* pop stack value */
ST_FUNC void vpop(TCCState* S)
{
    int v;
    v = S->tccgen_vtop->r & VT_VALMASK;
#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
    /* for x86, we need to pop the FP stack */
    if (v == TREG_ST0) {
        o(S, 0xd8dd); /* fstp %st(0) */
    } else
#endif
    if (v == VT_CMP) {
        /* need to put correct jump if && or || without test */
        gsym(S, S->tccgen_vtop->jtrue);
        gsym(S, S->tccgen_vtop->jfalse);
    }
    S->tccgen_vtop--;
}

/* push constant of type "type" with useless value */
static void vpush(TCCState* S, CType *type)
{
    vset(S, type, VT_CONST, 0);
}

/* push arbitrary 64bit constant */
static void vpush64(TCCState* S, int ty, unsigned long long v)
{
    CValue cval;
    CType ctype;
    ctype.t = ty;
    ctype.ref = NULL;
    cval.i = v;
    vsetc(S, &ctype, VT_CONST, &cval);
}

/* push integer constant */
ST_FUNC void vpushi(TCCState* S, int v)
{
    vpush64(S, VT_INT, v);
}

/* push a pointer sized constant */
static void vpushs(TCCState* S, addr_t v)
{
    vpush64(S, VT_SIZE_T, v);
}

/* push long long constant */
static inline void vpushll(TCCState* S, long long v)
{
    vpush64(S, VT_LLONG, v);
}

ST_FUNC void vset(TCCState* S, CType *type, int r, int v)
{
    CValue cval;
    cval.i = v;
    vsetc(S, type, r, &cval);
}

static void vseti(TCCState* S, int r, int v)
{
    CType type;
    type.t = VT_INT;
    type.ref = NULL;
    vset(S, &type, r, v);
}

ST_FUNC void vpushv(TCCState* S, SValue *v)
{
    if (S->tccgen_vtop >= vstack + (VSTACK_SIZE - 1))
        tcc_error(S, "memory full (vstack)");
    S->tccgen_vtop++;
    *S->tccgen_vtop = *v;
}

static void vdup(TCCState* S)
{
    vpushv(S, S->tccgen_vtop);
}

/* rotate n first stack elements to the bottom
   I1 ... In -> I2 ... In I1 [top is right]
*/
ST_FUNC void vrotb(TCCState* S, int n)
{
    int i;
    SValue tmp;

    vcheck_cmp(S);
    tmp = S->tccgen_vtop[-n + 1];
    for(i=-n+1;i!=0;i++)
        S->tccgen_vtop[i] = S->tccgen_vtop[i+1];
    S->tccgen_vtop[0] = tmp;
}

/* rotate the n elements before entry e towards the top
   I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
 */
ST_FUNC void vrote(TCCState* S, SValue *e, int n)
{
    int i;
    SValue tmp;

    vcheck_cmp(S);
    tmp = *e;
    for(i = 0;i < n - 1; i++)
        e[-i] = e[-i - 1];
    e[-n + 1] = tmp;
}

/* rotate n first stack elements to the top
   I1 ... In -> In I1 ... I(n-1)  [top is right]
 */
ST_FUNC void vrott(TCCState* S, int n)
{
    vrote(S, S->tccgen_vtop, n);
}

/* ------------------------------------------------------------------------- */
/* vtop->r = VT_CMP means CPU-flags have been set from comparison or test. */

/* called from generators to set the result from relational ops  */
ST_FUNC void vset_VT_CMP(TCCState* S, int op)
{
    S->tccgen_vtop->r = VT_CMP;
    S->tccgen_vtop->cmp_op = op;
    S->tccgen_vtop->jfalse = 0;
    S->tccgen_vtop->jtrue = 0;
}

/* called once before asking generators to load VT_CMP to a register */
static void vset_VT_JMP(TCCState* S)
{
    int op = S->tccgen_vtop->cmp_op;

    if (S->tccgen_vtop->jtrue || S->tccgen_vtop->jfalse) {
        /* we need to jump to 'mov $0,%R' or 'mov $1,%R' */
        int inv = op & (op < 2); /* small optimization */
        vseti(S, VT_JMP+inv, gvtst(S, inv, 0));
    } else {
        /* otherwise convert flags (rsp. 0/1) to register */
        S->tccgen_vtop->c.i = op;
        if (op < 2) /* doesn't seem to happen */
            S->tccgen_vtop->r = VT_CONST;
    }
}

/* Set CPU Flags, doesn't yet jump */
static void gvtst_set(TCCState* S, int inv, int t)
{
    int *p;

    if (S->tccgen_vtop->r != VT_CMP) {
        vpushi(S, 0);
        gen_op(S, TOK_NE);
        if (S->tccgen_vtop->r != VT_CMP) /* must be VT_CONST then */
            vset_VT_CMP(S, S->tccgen_vtop->c.i != 0);
    }

    p = inv ? &S->tccgen_vtop->jfalse : &S->tccgen_vtop->jtrue;
    *p = gjmp_append(S, *p, t);
}

/* Generate value test
 *
 * Generate a test for any value (jump, comparison and integers) */
static int gvtst(TCCState* S, int inv, int t)
{
    int op, x, u;

    gvtst_set(S, inv, t);
    t = S->tccgen_vtop->jtrue, u = S->tccgen_vtop->jfalse;
    if (inv)
        x = u, u = t, t = x;
    op = S->tccgen_vtop->cmp_op;

    /* jump to the wanted target */
    if (op > 1)
        t = gjmp_cond(S, op ^ inv, t);
    else if (op != inv)
        t = gjmp(S, t);
    /* resolve complementary jumps to here */
    gsym(S, u);

    S->tccgen_vtop--;
    return t;
}

/* generate a zero or nozero test */
static void gen_test_zero(TCCState* S, int op)
{
    if (S->tccgen_vtop->r == VT_CMP) {
        int j;
        if (op == TOK_EQ) {
            j = S->tccgen_vtop->jfalse;
            S->tccgen_vtop->jfalse = S->tccgen_vtop->jtrue;
            S->tccgen_vtop->jtrue = j;
            S->tccgen_vtop->cmp_op ^= 1;
        }
    } else {
        vpushi(S, 0);
        gen_op(S, op);
    }
}

/* ------------------------------------------------------------------------- */
/* push a symbol value of TYPE */
ST_FUNC void vpushsym(TCCState* S, CType *type, Sym *sym)
{
    CValue cval;
    cval.i = 0;
    vsetc(S, type, VT_CONST | VT_SYM, &cval);
    S->tccgen_vtop->sym = sym;
}

/* Return a static symbol pointing to a section */
ST_FUNC Sym *get_sym_ref(TCCState* S, CType *type, Section *sec, unsigned long offset, unsigned long size)
{
    int v;
    Sym *sym;

    v = S->tccgen_anon_sym++;
    sym = sym_push(S, v, type, VT_CONST | VT_SYM, 0);
    sym->type.t |= VT_STATIC;
    put_extern_sym(S, sym, sec, offset, size);
    return sym;
}

/* push a reference to a section offset by adding a dummy symbol */
static void vpush_ref(TCCState* S, CType *type, Section *sec, unsigned long offset, unsigned long size)
{
    vpushsym(S, type, get_sym_ref(S, type, sec, offset, size));  
}

/* define a new external reference to a symbol 'v' of type 'u' */
ST_FUNC Sym *external_global_sym(TCCState* S, int v, CType *type)
{
    Sym *s;

    s = sym_find(S, v);
    if (!s) {
        /* push forward reference */
        s = global_identifier_push(S, v, type->t | VT_EXTERN, 0);
        s->type.ref = type->ref;
    } else if (IS_ASM_SYM(s)) {
        s->type.t = type->t | (s->type.t & VT_EXTERN);
        s->type.ref = type->ref;
        update_storage(S, s);
    }
    return s;
}

/* create an external reference with no specific type similar to asm labels.
   This avoids type conflicts if the symbol is used from C too */
ST_FUNC Sym *external_helper_sym(TCCState* S, int v)
{
    CType ct = { VT_ASM_FUNC, NULL };
    return external_global_sym(S, v, &ct);
}

/* push a reference to an helper function (such as memmove) */
ST_FUNC void vpush_helper_func(TCCState* S, int v)
{
    vpushsym(S, &S->tccgen_func_old_type, external_helper_sym(S, v));
}

/* Merge symbol attributes.  */
static void merge_symattr(struct SymAttr *sa, struct SymAttr *sa1)
{
    if (sa1->aligned && !sa->aligned)
      sa->aligned = sa1->aligned;
    sa->packed |= sa1->packed;
    sa->weak |= sa1->weak;
    if (sa1->visibility != STV_DEFAULT) {
	int vis = sa->visibility;
	if (vis == STV_DEFAULT
	    || vis > sa1->visibility)
	  vis = sa1->visibility;
	sa->visibility = vis;
    }
    sa->dllexport |= sa1->dllexport;
    sa->nodecorate |= sa1->nodecorate;
    sa->dllimport |= sa1->dllimport;
}

/* Merge function attributes.  */
static void merge_funcattr(struct FuncAttr *fa, struct FuncAttr *fa1)
{
    if (fa1->func_call && !fa->func_call)
      fa->func_call = fa1->func_call;
    if (fa1->func_type && !fa->func_type)
      fa->func_type = fa1->func_type;
    if (fa1->func_args && !fa->func_args)
      fa->func_args = fa1->func_args;
    if (fa1->func_noreturn)
      fa->func_noreturn = 1;
    if (fa1->func_ctor)
      fa->func_ctor = 1;
    if (fa1->func_dtor)
      fa->func_dtor = 1;
}

/* Merge attributes.  */
static void merge_attr(AttributeDef *ad, AttributeDef *ad1)
{
    merge_symattr(&ad->a, &ad1->a);
    merge_funcattr(&ad->f, &ad1->f);

    if (ad1->section)
      ad->section = ad1->section;
    if (ad1->alias_target)
      ad->alias_target = ad1->alias_target;
    if (ad1->asm_label)
      ad->asm_label = ad1->asm_label;
    if (ad1->attr_mode)
      ad->attr_mode = ad1->attr_mode;
}

/* Merge some type attributes.  */
static void patch_type(TCCState* S, Sym *sym, CType *type)
{
    if (!(type->t & VT_EXTERN) || IS_ENUM_VAL(sym->type.t)) {
        if (!(sym->type.t & VT_EXTERN))
            tcc_error(S, "redefinition of '%s'", get_tok_str(S, sym->v, NULL));
        sym->type.t &= ~VT_EXTERN;
    }

    if (IS_ASM_SYM(sym)) {
        /* stay static if both are static */
        sym->type.t = type->t & (sym->type.t | ~VT_STATIC);
        sym->type.ref = type->ref;
    }

    if (!is_compatible_types(&sym->type, type)) {
        tcc_error(S, "incompatible types for redefinition of '%s'",
                  get_tok_str(S, sym->v, NULL));

    } else if ((sym->type.t & VT_BTYPE) == VT_FUNC) {
        int static_proto = sym->type.t & VT_STATIC;
        /* warn if static follows non-static function declaration */
        if ((type->t & VT_STATIC) && !static_proto
            /* XXX this test for inline shouldn't be here.  Until we
               implement gnu-inline mode again it silences a warning for
               mingw caused by our workarounds.  */
            && !((type->t | sym->type.t) & VT_INLINE))
            tcc_warning(S, "static storage ignored for redefinition of '%s'",
                get_tok_str(S, sym->v, NULL));

        /* set 'inline' if both agree or if one has static */
        if ((type->t | sym->type.t) & VT_INLINE) {
            if (!((type->t ^ sym->type.t) & VT_INLINE)
             || ((type->t | sym->type.t) & VT_STATIC))
                static_proto |= VT_INLINE;
        }

        if (0 == (type->t & VT_EXTERN)) {
            struct FuncAttr f = sym->type.ref->f;
            /* put complete type, use static from prototype */
            sym->type.t = (type->t & ~(VT_STATIC|VT_INLINE)) | static_proto;
            sym->type.ref = type->ref;
            merge_funcattr(&sym->type.ref->f, &f);
        } else {
            sym->type.t &= ~VT_INLINE | static_proto;
        }

        if (sym->type.ref->f.func_type == FUNC_OLD
             && type->ref->f.func_type != FUNC_OLD) {
            sym->type.ref = type->ref;
        }

    } else {
        if ((sym->type.t & VT_ARRAY) && type->ref->c >= 0) {
            /* set array size if it was omitted in extern declaration */
            sym->type.ref->c = type->ref->c;
        }
        if ((type->t ^ sym->type.t) & VT_STATIC)
            tcc_warning(S, "storage mismatch for redefinition of '%s'",
                get_tok_str(S, sym->v, NULL));
    }
}

/* Merge some storage attributes.  */
static void patch_storage(TCCState* S, Sym *sym, AttributeDef *ad, CType *type)
{
    if (type)
        patch_type(S, sym, type);

#ifdef TCC_TARGET_PE
    if (sym->a.dllimport != ad->a.dllimport)
        tcc_error(S, "incompatible dll linkage for redefinition of '%s'",
            get_tok_str(S, sym->v, NULL));
#endif
    merge_symattr(&sym->a, &ad->a);
    if (ad->asm_label)
        sym->asm_label = ad->asm_label;
    update_storage(S, sym);
}

/* copy sym to other stack */
static Sym *sym_copy(TCCState* S, Sym *s0, Sym **ps)
{
    Sym *s;
    s = sym_malloc(S), *s = *s0;
    s->prev = *ps, *ps = s;
    if (s->v < SYM_FIRST_ANOM) {
        ps = &S->tccpp_table_ident[s->v - TOK_IDENT]->sym_identifier;
        s->prev_tok = *ps, *ps = s;
    }
    return s;
}

/* copy s->type.ref to stack 'ps' for VT_FUNC and VT_PTR */
static void sym_copy_ref(TCCState* S, Sym *s, Sym **ps)
{
    int bt = s->type.t & VT_BTYPE;
    if (bt == VT_FUNC || bt == VT_PTR || (bt == VT_STRUCT && s->sym_scope)) {
        Sym **sp = &s->type.ref;
        for (s = *sp, *sp = NULL; s; s = s->next) {
            Sym *s2 = sym_copy(S, s, ps);
            sp = &(*sp = s2)->next;
            sym_copy_ref(S, s2, ps);
        }
    }
}

/* define a new external reference to a symbol 'v' */
static Sym *external_sym(TCCState* S, int v, CType *type, int r, AttributeDef *ad)
{
    Sym *s;

    /* look for global symbol */
    s = sym_find(S, v);
    while (s && s->sym_scope)
        s = s->prev_tok;

    if (!s) {
        /* push forward reference */
        s = global_identifier_push(S, v, type->t, 0);
        s->r |= r;
        s->a = ad->a;
        s->asm_label = ad->asm_label;
        s->type.ref = type->ref;
        /* copy type to the global stack */
        if (S->tccgen_local_stack)
            sym_copy_ref(S, s, &S->tccgen_global_stack);
    } else {
        patch_storage(S, s, ad, type);
    }
    /* push variables on local_stack if any */
    if (S->tccgen_local_stack && (s->type.t & VT_BTYPE) != VT_FUNC)
        s = sym_copy(S, s, &S->tccgen_local_stack);
    return s;
}

/* save registers up to (vtop - n) stack entry */
ST_FUNC void save_regs(TCCState* S, int n)
{
    SValue *p, *p1;
    for(p = vstack, p1 = S->tccgen_vtop - n; p <= p1; p++)
        save_reg(S, p->r);
}

/* save r to the memory stack, and mark it as being free */
ST_FUNC void save_reg(TCCState* S, int r)
{
    save_reg_upstack(S, r, 0);
}

/* save r to the memory stack, and mark it as being free,
   if seen up to (vtop - n) stack entry */
ST_FUNC void save_reg_upstack(TCCState* S, int r, int n)
{
    int l, size, align, bt;
    SValue *p, *p1, sv;

    if ((r &= VT_VALMASK) >= VT_CONST)
        return;
    if (S->tccgen_nocode_wanted)
        return;
    l = 0;
    for(p = vstack, p1 = S->tccgen_vtop - n; p <= p1; p++) {
        if ((p->r & VT_VALMASK) == r || p->r2 == r) {
            /* must save value on stack if not already done */
            if (!l) {
                bt = p->type.t & VT_BTYPE;
                if (bt == VT_VOID)
                    continue;
                if ((p->r & VT_LVAL) || bt == VT_FUNC)
                    bt = VT_PTR;
                sv.type.t = bt;
                size = type_size(&sv.type, &align);
                l = get_temp_local_var(S, size,align);
                sv.r = VT_LOCAL | VT_LVAL;
                sv.c.i = l;
                store(S, p->r & VT_VALMASK, &sv);
#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
                /* x86 specific: need to pop fp register ST0 if saved */
                if (r == TREG_ST0) {
                    o(S, 0xd8dd); /* fstp %st(0) */
                }
#endif
                /* special long long case */
                if (p->r2 < VT_CONST && USING_TWO_WORDS(bt)) {
                    sv.c.i += PTR_SIZE;
                    store(S, p->r2, &sv);
                }
            }
            /* mark that stack entry as being saved on the stack */
            if (p->r & VT_LVAL) {
                /* also clear the bounded flag because the
                   relocation address of the function was stored in
                   p->c.i */
                p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
            } else {
                p->r = VT_LVAL | VT_LOCAL;
            }
            p->sym = NULL;
            p->r2 = VT_CONST;
            p->c.i = l;
        }
    }
}

#ifdef TCC_TARGET_ARM
/* find a register of class 'rc2' with at most one reference on stack.
 * If none, call get_reg(rc) */
ST_FUNC int get_reg_ex(TCCState* S, int rc, int rc2)
{
    int r;
    SValue *p;
    
    for(r=0;r<NB_REGS;r++) {
        if (reg_classes[r] & rc2) {
            int n;
            n=0;
            for(p = vstack; p <= S->tccgen_vtop; p++) {
                if ((p->r & VT_VALMASK) == r ||
                    p->r2 == r)
                    n++;
            }
            if (n <= 1)
                return r;
        }
    }
    return get_reg(S, rc);
}
#endif

/* find a free register of class 'rc'. If none, save one register */
ST_FUNC int get_reg(TCCState* S, int rc)
{
    int r;
    SValue *p;

    /* find a free register */
    for(r=0;r<NB_REGS;r++) {
        if (reg_classes[r] & rc) {
            if (S->tccgen_nocode_wanted)
                return r;
            for(p=vstack;p<=S->tccgen_vtop;p++) {
                if ((p->r & VT_VALMASK) == r ||
                    p->r2 == r)
                    goto notfound;
            }
            return r;
        }
    notfound: ;
    }
    
    /* no register left : free the first one on the stack (VERY
       IMPORTANT to start from the bottom to ensure that we don't
       spill registers used in gen_opi()) */
    for(p=vstack;p<=S->tccgen_vtop;p++) {
        /* look at second register (if long long) */
        r = p->r2;
        if (r < VT_CONST && (reg_classes[r] & rc))
            goto save_found;
        r = p->r & VT_VALMASK;
        if (r < VT_CONST && (reg_classes[r] & rc)) {
        save_found:
            save_reg(S, r);
            return r;
        }
    }
    /* Should never comes here */
    return -1;
}

/* find a free temporary local variable (return the offset on stack) match the size and align. If none, add new temporary stack variable*/
static int get_temp_local_var(TCCState* S, int size,int align){
	int i;
	temp_local_variable_t *temp_var;
	int found_var;
	SValue *p;
	int r;
	char free;
	char found;
	found=0;
	for(i=0;i<S->tccgen_nb_temp_local_vars;i++){
		temp_var=&S->tccgen_arr_temp_local_vars[i];
		if(temp_var->size<size||align!=temp_var->align){
			continue;
		}
		/*check if temp_var is free*/
		free=1;
		for(p=vstack;p<=S->tccgen_vtop;p++) {
			r=p->r&VT_VALMASK;
			if(r==VT_LOCAL||r==VT_LLOCAL){
				if(p->c.i==temp_var->location){
					free=0;
					break;
				}
			}
		}
		if(free){
			found_var=temp_var->location;
			found=1;
			break;
		}
	}
	if(!found){
		S->tccgen_loc = (S->tccgen_loc - size) & -align;
		if(S->tccgen_nb_temp_local_vars<MAX_TEMP_LOCAL_VARIABLE_NUMBER){
			temp_var=&S->tccgen_arr_temp_local_vars[i];
			temp_var->location=S->tccgen_loc;
			temp_var->size=size;
			temp_var->align=align;
			S->tccgen_nb_temp_local_vars++;
		}
		found_var=S->tccgen_loc;
	}
	return found_var;
}

static void clear_temp_local_var_list(TCCState* S){
	S->tccgen_nb_temp_local_vars=0;
}

/* move register 's' (of type 't') to 'r', and flush previous value of r to memory
   if needed */
static void move_reg(TCCState* S, int r, int s, int t)
{
    SValue sv;

    if (r != s) {
        save_reg(S, r);
        sv.type.t = t;
        sv.type.ref = NULL;
        sv.r = s;
        sv.c.i = 0;
        load(S, r, &sv);
    }
}

/* get address of vtop (vtop MUST BE an lvalue) */
ST_FUNC void gaddrof(TCCState* S)
{
    S->tccgen_vtop->r &= ~VT_LVAL;
    /* tricky: if saved lvalue, then we can go back to lvalue */
    if ((S->tccgen_vtop->r & VT_VALMASK) == VT_LLOCAL)
        S->tccgen_vtop->r = (S->tccgen_vtop->r & ~VT_VALMASK) | VT_LOCAL | VT_LVAL;
}

#ifdef CONFIG_TCC_BCHECK
/* generate a bounded pointer addition */
static void gen_bounded_ptr_add(TCCState* S)
{
    int save = (S->tccgen_vtop[-1].r & VT_VALMASK) == VT_LOCAL;
    if (save) {
      vpushv(S, &S->tccgen_vtop[-1]);
      vrott(S, 3);
    }
    vpush_helper_func(S, TOK___bound_ptr_add);
    vrott(S, 3);
    gfunc_call(S, 2);
    S->tccgen_vtop -= save;
    vpushi(S, 0);
    /* returned pointer is in REG_IRET */
    S->tccgen_vtop->r = REG_IRET | VT_BOUNDED;
    if (S->tccgen_nocode_wanted)
        return;
    /* relocation offset of the bounding function call point */
    S->tccgen_vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(ElfW_Rel));
}

/* patch pointer addition in vtop so that pointer dereferencing is
   also tested */
static void gen_bounded_ptr_deref(TCCState* S)
{
    addr_t func;
    int size, align;
    ElfW_Rel *rel;
    Sym *sym;

    if (S->tccgen_nocode_wanted)
        return;

    size = type_size(&S->tccgen_vtop->type, &align);
    switch(size) {
    case  1: func = TOK___bound_ptr_indir1; break;
    case  2: func = TOK___bound_ptr_indir2; break;
    case  4: func = TOK___bound_ptr_indir4; break;
    case  8: func = TOK___bound_ptr_indir8; break;
    case 12: func = TOK___bound_ptr_indir12; break;
    case 16: func = TOK___bound_ptr_indir16; break;
    default:
        /* may happen with struct member access */
        return;
    }
    sym = external_helper_sym(S, func);
    if (!sym->c)
        put_extern_sym(S, sym, NULL, 0, 0);
    /* patch relocation */
    /* XXX: find a better solution ? */
    rel = (ElfW_Rel *)(cur_text_section->reloc->data + S->tccgen_vtop->c.i);
    rel->r_info = ELFW(R_INFO)(sym->c, ELFW(R_TYPE)(rel->r_info));
}

/* generate lvalue bound code */
static void gbound(TCCState* S)
{
    CType type1;

    S->tccgen_vtop->r &= ~VT_MUSTBOUND;
    /* if lvalue, then use checking code before dereferencing */
    if (S->tccgen_vtop->r & VT_LVAL) {
        /* if not VT_BOUNDED value, then make one */
        if (!(S->tccgen_vtop->r & VT_BOUNDED)) {
            /* must save type because we must set it to int to get pointer */
            type1 = S->tccgen_vtop->type;
            S->tccgen_vtop->type.t = VT_PTR;
            gaddrof(S);
            vpushi(S, 0);
            gen_bounded_ptr_add(S);
            S->tccgen_vtop->r |= VT_LVAL;
            S->tccgen_vtop->type = type1;
        }
        /* then check for dereferencing */
        gen_bounded_ptr_deref(S);
    }
}

/* we need to call __bound_ptr_add before we start to load function
   args into registers */
ST_FUNC void gbound_args(TCCState* S, int nb_args)
{
    int i, v;
    SValue *sv;

    for (i = 1; i <= nb_args; ++i)
        if (S->tccgen_vtop[1 - i].r & VT_MUSTBOUND) {
            vrotb(S, i);
            gbound(S);
            vrott(S, i);
        }

    sv = S->tccgen_vtop - nb_args;
    if (sv->r & VT_SYM) {
        v = sv->sym->v;
        if (v == TOK_setjmp
          || v == TOK__setjmp
#ifndef TCC_TARGET_PE
          || v == TOK_sigsetjmp
          || v == TOK___sigsetjmp
#endif
          ) {
            vpush_helper_func(S, TOK___bound_setjmp);
            vpushv(S, sv + 1);
            gfunc_call(S, 1);
            S->func_bound_add_epilog = 1;
        }
#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
        if (v == TOK_alloca)
            S->func_bound_add_epilog = 1;
#endif
#if TARGETOS_NetBSD
        if (v == TOK_longjmp) /* undo rename to __longjmp14 */
            sv->sym->asm_label = TOK___bound_longjmp;
#endif
    }
}

/* Add bounds for local symbols from S to E (via ->prev) */
static void add_local_bounds(TCCState* S, Sym *s, Sym *e)
{
    for (; s != e; s = s->prev) {
        if (!s->v || (s->r & VT_VALMASK) != VT_LOCAL)
          continue;
        /* Add arrays/structs/unions because we always take address */
        if ((s->type.t & VT_ARRAY)
            || (s->type.t & VT_BTYPE) == VT_STRUCT
            || s->a.addrtaken) {
            /* add local bound info */
            int align, size = type_size(&s->type, &align);
            addr_t *bounds_ptr = section_ptr_add(S, lbounds_section,
                                                 2 * sizeof(addr_t));
            bounds_ptr[0] = s->c;
            bounds_ptr[1] = size;
        }
    }
}
#endif

/* Wrapper around sym_pop, that potentially also registers local bounds.  */
static void pop_local_syms(TCCState* S, Sym *b, int keep)
{
#ifdef CONFIG_TCC_BCHECK
    if (S->do_bounds_check && !keep && (S->tccgen_local_scope || !S->tccgen_func_var))
        add_local_bounds(S, S->tccgen_local_stack, b);
#endif
    if (S->tccgen_debug_modes)
        tcc_add_debug_info (S, !S->tccgen_local_scope, S->tccgen_local_stack, b);
    sym_pop(S, &S->tccgen_local_stack, b, keep);
}

static void incr_bf_adr(TCCState* S, int o)
{
    S->tccgen_vtop->type = S->tccgen_char_pointer_type;
    gaddrof(S);
    vpushs(S, o);
    gen_op(S, '+');
    S->tccgen_vtop->type.t = VT_BYTE | VT_UNSIGNED;
    S->tccgen_vtop->r |= VT_LVAL;
}

/* single-byte load mode for packed or otherwise unaligned bitfields */
static void load_packed_bf(TCCState* S, CType *type, int bit_pos, int bit_size)
{
    int n, o, bits;
    save_reg_upstack(S, S->tccgen_vtop->r, 1);
    vpush64(S, type->t & VT_BTYPE, 0); // B X
    bits = 0, o = bit_pos >> 3, bit_pos &= 7;
    do {
        vswap(S); // X B
        incr_bf_adr(S, o);
        vdup(S); // X B B
        n = 8 - bit_pos;
        if (n > bit_size)
            n = bit_size;
        if (bit_pos)
            vpushi(S, bit_pos), gen_op(S, TOK_SHR), bit_pos = 0; // X B Y
        if (n < 8)
            vpushi(S, (1 << n) - 1), gen_op(S, '&');
        gen_cast(S, type);
        if (bits)
            vpushi(S, bits), gen_op(S, TOK_SHL);
        vrotb(S, 3); // B Y X
        gen_op(S, '|'); // B X
        bits += n, bit_size -= n, o = 1;
    } while (bit_size);
    vswap(S), vpop(S);
    if (!(type->t & VT_UNSIGNED)) {
        n = ((type->t & VT_BTYPE) == VT_LLONG ? 64 : 32) - bits;
        vpushi(S, n), gen_op(S, TOK_SHL);
        vpushi(S, n), gen_op(S, TOK_SAR);
    }
}

/* single-byte store mode for packed or otherwise unaligned bitfields */
static void store_packed_bf(TCCState* S, int bit_pos, int bit_size)
{
    int bits, n, o, m, c;
    c = (S->tccgen_vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
    vswap(S); // X B
    save_reg_upstack(S, S->tccgen_vtop->r, 1);
    bits = 0, o = bit_pos >> 3, bit_pos &= 7;
    do {
        incr_bf_adr(S, o); // X B
        vswap(S); //B X
        c ? vdup(S) : gv_dup(S); // B V X
        vrott(S, 3); // X B V
        if (bits)
            vpushi(S, bits), gen_op(S, TOK_SHR);
        if (bit_pos)
            vpushi(S, bit_pos), gen_op(S, TOK_SHL);
        n = 8 - bit_pos;
        if (n > bit_size)
            n = bit_size;
        if (n < 8) {
            m = ((1 << n) - 1) << bit_pos;
            vpushi(S, m), gen_op(S, '&'); // X B V1
            vpushv(S, S->tccgen_vtop-1); // X B V1 B
            vpushi(S, m & 0x80 ? ~m & 0x7f : ~m);
            gen_op(S, '&'); // X B V1 B1
            gen_op(S, '|'); // X B V2
        }
        vdup(S), S->tccgen_vtop[-1] = S->tccgen_vtop[-2]; // X B B V2
        vstore(S), vpop(S); // X B
        bits += n, bit_size -= n, bit_pos = 0, o = 1;
    } while (bit_size);
    vpop(S), vpop(S);
}

static int adjust_bf(SValue *sv, int bit_pos, int bit_size)
{
    int t;
    if (0 == sv->type.ref)
        return 0;
    t = sv->type.ref->auxtype;
    if (t != -1 && t != VT_STRUCT) {
        sv->type.t = (sv->type.t & ~(VT_BTYPE | VT_LONG)) | t;
        sv->r |= VT_LVAL;
    }
    return t;
}

/* store vtop a register belonging to class 'rc'. lvalues are
   converted to values. Cannot be used if cannot be converted to
   register value (such as structures). */
ST_FUNC int gv(TCCState* S, int rc)
{
    int r, r2, r_ok, r2_ok, rc2, bt;
    int bit_pos, bit_size, size, align;

    /* NOTE: get_reg can modify vstack[] */
    if (S->tccgen_vtop->type.t & VT_BITFIELD) {
        CType type;

        bit_pos = BIT_POS(S->tccgen_vtop->type.t);
        bit_size = BIT_SIZE(S->tccgen_vtop->type.t);
        /* remove bit field info to avoid loops */
        S->tccgen_vtop->type.t &= ~VT_STRUCT_MASK;

        type.ref = NULL;
        type.t = S->tccgen_vtop->type.t & VT_UNSIGNED;
        if ((S->tccgen_vtop->type.t & VT_BTYPE) == VT_BOOL)
            type.t |= VT_UNSIGNED;

        r = adjust_bf(S->tccgen_vtop, bit_pos, bit_size);

        if ((S->tccgen_vtop->type.t & VT_BTYPE) == VT_LLONG)
            type.t |= VT_LLONG;
        else
            type.t |= VT_INT;

        if (r == VT_STRUCT) {
            load_packed_bf(S, &type, bit_pos, bit_size);
        } else {
            int bits = (type.t & VT_BTYPE) == VT_LLONG ? 64 : 32;
            /* cast to int to propagate signedness in following ops */
            gen_cast(S, &type);
            /* generate shifts */
            vpushi(S, bits - (bit_pos + bit_size));
            gen_op(S, TOK_SHL);
            vpushi(S, bits - bit_size);
            /* NOTE: transformed to SHR if unsigned */
            gen_op(S, TOK_SAR);
        }
        r = gv(S, rc);
    } else {
        if (is_float(S->tccgen_vtop->type.t) && 
            (S->tccgen_vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
            /* CPUs usually cannot use float constants, so we store them
               generically in data segment */
            init_params p = { rodata_section };
            unsigned long offset;
            size = type_size(&S->tccgen_vtop->type, &align);
            if (NODATA_WANTED)
                size = 0, align = 1;
            offset = section_add(S, p.sec, size, align);
            vpush_ref(S, &S->tccgen_vtop->type, p.sec, offset, size);
	    vswap(S);
	    init_putv(S, &p, &S->tccgen_vtop->type, offset);
	    S->tccgen_vtop->r |= VT_LVAL;
        }
#ifdef CONFIG_TCC_BCHECK
        if (S->tccgen_vtop->r & VT_MUSTBOUND) 
            gbound(S);
#endif

        bt = S->tccgen_vtop->type.t & VT_BTYPE;

#ifdef TCC_TARGET_RISCV64
        /* XXX mega hack */
        if (bt == VT_LDOUBLE && rc == RC_FLOAT)
          rc = RC_INT;
#endif
        rc2 = RC2_TYPE(bt, rc);

        /* need to reload if:
           - constant
           - lvalue (need to dereference pointer)
           - already a register, but not in the right class */
        r = S->tccgen_vtop->r & VT_VALMASK;
        r_ok = !(S->tccgen_vtop->r & VT_LVAL) && (r < VT_CONST) && (reg_classes[r] & rc);
        r2_ok = !rc2 || ((S->tccgen_vtop->r2 < VT_CONST) && (reg_classes[S->tccgen_vtop->r2] & rc2));

        if (!r_ok || !r2_ok) {
            if (!r_ok)
                r = get_reg(S, rc);
            if (rc2) {
                int load_type = (bt == VT_QFLOAT) ? VT_DOUBLE : VT_PTRDIFF_T;
                int original_type = S->tccgen_vtop->type.t;

                /* two register type load :
                   expand to two words temporarily */
                if ((S->tccgen_vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
                    /* load constant */
                    unsigned long long ll = S->tccgen_vtop->c.i;
                    S->tccgen_vtop->c.i = ll; /* first word */
                    load(S, r, S->tccgen_vtop);
                    S->tccgen_vtop->r = r; /* save register value */
                    vpushi(S, ll >> 32); /* second word */
                } else if (S->tccgen_vtop->r & VT_LVAL) {
                    /* We do not want to modifier the long long pointer here.
                       So we save any other instances down the stack */
                    save_reg_upstack(S, S->tccgen_vtop->r, 1);
                    /* load from memory */
                    S->tccgen_vtop->type.t = load_type;
                    load(S, r, S->tccgen_vtop);
                    vdup(S);
                    S->tccgen_vtop[-1].r = r; /* save register value */
                    /* increment pointer to get second word */
                    S->tccgen_vtop->type.t = VT_PTRDIFF_T;
                    gaddrof(S);
                    vpushs(S, PTR_SIZE);
                    gen_op(S, '+');
                    S->tccgen_vtop->r |= VT_LVAL;
                    S->tccgen_vtop->type.t = load_type;
                } else {
                    /* move registers */
                    if (!r_ok)
                        load(S, r, S->tccgen_vtop);
                    if (r2_ok && S->tccgen_vtop->r2 < VT_CONST)
                        goto done;
                    vdup(S);
                    S->tccgen_vtop[-1].r = r; /* save register value */
                    S->tccgen_vtop->r = S->tccgen_vtop[-1].r2;
                }
                /* Allocate second register. Here we rely on the fact that
                   get_reg() tries first to free r2 of an SValue. */
                r2 = get_reg(S, rc2);
                load(S, r2, S->tccgen_vtop);
                vpop(S);
                /* write second register */
                S->tccgen_vtop->r2 = r2;
            done:
                S->tccgen_vtop->type.t = original_type;
            } else {
                if (S->tccgen_vtop->r == VT_CMP)
                    vset_VT_JMP(S);
                /* one register type load */
                load(S, r, S->tccgen_vtop);
            }
        }
        S->tccgen_vtop->r = r;
#ifdef TCC_TARGET_C67
        /* uses register pairs for doubles */
        if (bt == VT_DOUBLE)
            S->tccgen_vtop->r2 = r+1;
#endif
    }
    return r;
}

/* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
ST_FUNC void gv2(TCCState* S, int rc1, int rc2)
{
    /* generate more generic register first. But VT_JMP or VT_CMP
       values must be generated first in all cases to avoid possible
       reload errors */
    if (S->tccgen_vtop->r != VT_CMP && rc1 <= rc2) {
        vswap(S);
        gv(S, rc1);
        vswap(S);
        gv(S, rc2);
        /* test if reload is needed for first register */
        if ((S->tccgen_vtop[-1].r & VT_VALMASK) >= VT_CONST) {
            vswap(S);
            gv(S, rc1);
            vswap(S);
        }
    } else {
        gv(S, rc2);
        vswap(S);
        gv(S, rc1);
        vswap(S);
        /* test if reload is needed for first register */
        if ((S->tccgen_vtop[0].r & VT_VALMASK) >= VT_CONST) {
            gv(S, rc2);
        }
    }
}

#if PTR_SIZE == 4
/* expand 64bit on stack in two ints */
ST_FUNC void lexpand(TCCState* S)
{
    int u, v;
    u = S->tccgen_vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED);
    v = S->tccgen_vtop->r & (VT_VALMASK | VT_LVAL);
    if (v == VT_CONST) {
        vdup(S);
        S->tccgen_vtop[0].c.i >>= 32;
    } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
        vdup(S);
        S->tccgen_vtop[0].c.i += 4;
    } else {
        gv(S, RC_INT);
        vdup(S);
        S->tccgen_vtop[0].r = S->tccgen_vtop[-1].r2;
        S->tccgen_vtop[0].r2 = S->tccgen_vtop[-1].r2 = VT_CONST;
    }
    S->tccgen_vtop[0].type.t = S->tccgen_vtop[-1].type.t = VT_INT | u;
}
#endif

#if PTR_SIZE == 4
/* build a long long from two ints */
static void lbuild(TCCState* S, int t)
{
    gv2(S, RC_INT, RC_INT);
    S->tccgen_vtop[-1].r2 = S->tccgen_vtop[0].r;
    S->tccgen_vtop[-1].type.t = t;
    vpop(S);
}
#endif

/* convert stack entry to register and duplicate its value in another
   register */
static void gv_dup(TCCState* S)
{
    int t, rc, r;

    t = S->tccgen_vtop->type.t;
#if PTR_SIZE == 4
    if ((t & VT_BTYPE) == VT_LLONG) {
        if (t & VT_BITFIELD) {
            gv(S, RC_INT);
            t = S->tccgen_vtop->type.t;
        }
        lexpand(S);
        gv_dup(S);
        vswap(S);
        vrotb(S, 3);
        gv_dup(S);
        vrotb(S, 4);
        /* stack: H L L1 H1 */
        lbuild(S, t);
        vrotb(S, 3);
        vrotb(S, 3);
        vswap(S);
        lbuild(S, t);
        vswap(S);
        return;
    }
#endif
    /* duplicate value */
    rc = RC_TYPE(t);
    gv(S, rc);
    r = get_reg(S, rc);
    vdup(S);
    load(S, r, S->tccgen_vtop);
    S->tccgen_vtop->r = r;
}

#if PTR_SIZE == 4
/* generate CPU independent (unsigned) long long operations */
static void gen_opl(TCCState* S, int op)
{
    int t, a, b, op1, c, i;
    int func;
    unsigned short reg_iret = REG_IRET;
    unsigned short reg_lret = REG_IRE2;
    SValue tmp;

    switch(op) {
    case '/':
    case TOK_PDIV:
        func = TOK___divdi3;
        goto gen_func;
    case TOK_UDIV:
        func = TOK___udivdi3;
        goto gen_func;
    case '%':
        func = TOK___moddi3;
        goto gen_mod_func;
    case TOK_UMOD:
        func = TOK___umoddi3;
    gen_mod_func:
#ifdef TCC_ARM_EABI
        reg_iret = TREG_R2;
        reg_lret = TREG_R3;
#endif
    gen_func:
        /* call generic long long function */
        vpush_helper_func(S, func);
        vrott(S, 3);
        gfunc_call(S, 2);
        vpushi(S, 0);
        S->tccgen_vtop->r = reg_iret;
        S->tccgen_vtop->r2 = reg_lret;
        break;
    case '^':
    case '&':
    case '|':
    case '*':
    case '+':
    case '-':
        //pv("gen_opl A",0,2);
        t = S->tccgen_vtop->type.t;
        vswap(S);
        lexpand(S);
        vrotb(S, 3);
        lexpand(S);
        /* stack: L1 H1 L2 H2 */
        tmp = S->tccgen_vtop[0];
        S->tccgen_vtop[0] = S->tccgen_vtop[-3];
        S->tccgen_vtop[-3] = tmp;
        tmp = S->tccgen_vtop[-2];
        S->tccgen_vtop[-2] = S->tccgen_vtop[-3];
        S->tccgen_vtop[-3] = tmp;
        vswap(S);
        /* stack: H1 H2 L1 L2 */
        //pv("gen_opl B",0,4);
        if (op == '*') {
            vpushv(S, S->tccgen_vtop - 1);
            vpushv(S, S->tccgen_vtop - 1);
            gen_op(S, TOK_UMULL);
            lexpand(S);
            /* stack: H1 H2 L1 L2 ML MH */
            for(i=0;i<4;i++)
                vrotb(S, 6);
            /* stack: ML MH H1 H2 L1 L2 */
            tmp = S->tccgen_vtop[0];
            S->tccgen_vtop[0] = S->tccgen_vtop[-2];
            S->tccgen_vtop[-2] = tmp;
            /* stack: ML MH H1 L2 H2 L1 */
            gen_op(S, '*');
            vrotb(S, 3);
            vrotb(S, 3);
            gen_op(S, '*');
            /* stack: ML MH M1 M2 */
            gen_op(S, '+');
            gen_op(S, '+');
        } else if (op == '+' || op == '-') {
            /* XXX: add non carry method too (for MIPS or alpha) */
            if (op == '+')
                op1 = TOK_ADDC1;
            else
                op1 = TOK_SUBC1;
            gen_op(S, op1);
            /* stack: H1 H2 (L1 op L2) */
            vrotb(S, 3);
            vrotb(S, 3);
            gen_op(S, op1 + 1); /* TOK_xxxC2 */
        } else {
            gen_op(S, op);
            /* stack: H1 H2 (L1 op L2) */
            vrotb(S, 3);
            vrotb(S, 3);
            /* stack: (L1 op L2) H1 H2 */
            gen_op(S, op);
            /* stack: (L1 op L2) (H1 op H2) */
        }
        /* stack: L H */
        lbuild(S, t);
        break;
    case TOK_SAR:
    case TOK_SHR:
    case TOK_SHL:
        if ((S->tccgen_vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
            t = S->tccgen_vtop[-1].type.t;
            vswap(S);
            lexpand(S);
            vrotb(S, 3);
            /* stack: L H shift */
            c = (int)S->tccgen_vtop->c.i;
            /* constant: simpler */
            /* NOTE: all comments are for SHL. the other cases are
               done by swapping words */
            vpop(S);
            if (op != TOK_SHL)
                vswap(S);
            if (c >= 32) {
                /* stack: L H */
                vpop(S);
                if (c > 32) {
                    vpushi(S, c - 32);
                    gen_op(S, op);
                }
                if (op != TOK_SAR) {
                    vpushi(S, 0);
                } else {
                    gv_dup(S);
                    vpushi(S, 31);
                    gen_op(S, TOK_SAR);
                }
                vswap(S);
            } else {
                vswap(S);
                gv_dup(S);
                /* stack: H L L */
                vpushi(S, c);
                gen_op(S, op);
                vswap(S);
                vpushi(S, 32 - c);
                if (op == TOK_SHL)
                    gen_op(S, TOK_SHR);
                else
                    gen_op(S, TOK_SHL);
                vrotb(S, 3);
                /* stack: L L H */
                vpushi(S, c);
                if (op == TOK_SHL)
                    gen_op(S, TOK_SHL);
                else
                    gen_op(S, TOK_SHR);
                gen_op(S, '|');
            }
            if (op != TOK_SHL)
                vswap(S);
            lbuild(S, t);
        } else {
            /* XXX: should provide a faster fallback on x86 ? */
            switch(op) {
            case TOK_SAR:
                func = TOK___ashrdi3;
                goto gen_func;
            case TOK_SHR:
                func = TOK___lshrdi3;
                goto gen_func;
            case TOK_SHL:
                func = TOK___ashldi3;
                goto gen_func;
            }
        }
        break;
    default:
        /* compare operations */
        t = S->tccgen_vtop->type.t;
        vswap(S);
        lexpand(S);
        vrotb(S, 3);
        lexpand(S);
        /* stack: L1 H1 L2 H2 */
        tmp = S->tccgen_vtop[-1];
        S->tccgen_vtop[-1] = S->tccgen_vtop[-2];
        S->tccgen_vtop[-2] = tmp;
        /* stack: L1 L2 H1 H2 */
        save_regs(S, 4);
        /* compare high */
        op1 = op;
        /* when values are equal, we need to compare low words. since
           the jump is inverted, we invert the test too. */
        if (op1 == TOK_LT)
            op1 = TOK_LE;
        else if (op1 == TOK_GT)
            op1 = TOK_GE;
        else if (op1 == TOK_ULT)
            op1 = TOK_ULE;
        else if (op1 == TOK_UGT)
            op1 = TOK_UGE;
        a = 0;
        b = 0;
        gen_op(S, op1);
        if (op == TOK_NE) {
            b = gvtst(S, 0, 0);
        } else {
            a = gvtst(S, 1, 0);
            if (op != TOK_EQ) {
                /* generate non equal test */
                vpushi(S, 0);
                vset_VT_CMP(S, TOK_NE);
                b = gvtst(S, 0, 0);
            }
        }
        /* compare low. Always unsigned */
        op1 = op;
        if (op1 == TOK_LT)
            op1 = TOK_ULT;
        else if (op1 == TOK_LE)
            op1 = TOK_ULE;
        else if (op1 == TOK_GT)
            op1 = TOK_UGT;
        else if (op1 == TOK_GE)
            op1 = TOK_UGE;
        gen_op(S, op1);
#if 0//def TCC_TARGET_I386
        if (op == TOK_NE) { gsym(S, b); break; }
        if (op == TOK_EQ) { gsym(S, a); break; }
#endif
        gvtst_set(S, 1, a);
        gvtst_set(S, 0, b);
        break;
    }
}
#endif

static uint64_t gen_opic_sdiv(uint64_t a, uint64_t b)
{
    uint64_t x = (a >> 63 ? -a : a) / (b >> 63 ? -b : b);
    return (a ^ b) >> 63 ? -x : x;
}

static int gen_opic_lt(uint64_t a, uint64_t b)
{
    return (a ^ (uint64_t)1 << 63) < (b ^ (uint64_t)1 << 63);
}

/* handle integer constant optimizations and various machine
   independent opt */
static void gen_opic(TCCState* S, int op)
{
    SValue *v1 = S->tccgen_vtop - 1;
    SValue *v2 = S->tccgen_vtop;
    int t1 = v1->type.t & VT_BTYPE;
    int t2 = v2->type.t & VT_BTYPE;
    int c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
    int c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
    uint64_t l1 = c1 ? v1->c.i : 0;
    uint64_t l2 = c2 ? v2->c.i : 0;
    int shm = (t1 == VT_LLONG) ? 63 : 31;

    if (t1 != VT_LLONG && (PTR_SIZE != 8 || t1 != VT_PTR))
        l1 = ((uint32_t)l1 |
              (v1->type.t & VT_UNSIGNED ? 0 : -(l1 & 0x80000000)));
    if (t2 != VT_LLONG && (PTR_SIZE != 8 || t2 != VT_PTR))
        l2 = ((uint32_t)l2 |
              (v2->type.t & VT_UNSIGNED ? 0 : -(l2 & 0x80000000)));

    if (c1 && c2) {
        switch(op) {
        case '+': l1 += l2; break;
        case '-': l1 -= l2; break;
        case '&': l1 &= l2; break;
        case '^': l1 ^= l2; break;
        case '|': l1 |= l2; break;
        case '*': l1 *= l2; break;

        case TOK_PDIV:
        case '/':
        case '%':
        case TOK_UDIV:
        case TOK_UMOD:
            /* if division by zero, generate explicit division */
            if (l2 == 0) {
                if (S->tccgen_const_wanted && !(S->tccgen_nocode_wanted & unevalmask))
                    tcc_error(S, "division by zero in constant");
                goto general_case;
            }
            switch(op) {
            default: l1 = gen_opic_sdiv(l1, l2); break;
            case '%': l1 = l1 - l2 * gen_opic_sdiv(l1, l2); break;
            case TOK_UDIV: l1 = l1 / l2; break;
            case TOK_UMOD: l1 = l1 % l2; break;
            }
            break;
        case TOK_SHL: l1 <<= (l2 & shm); break;
        case TOK_SHR: l1 >>= (l2 & shm); break;
        case TOK_SAR:
            l1 = (l1 >> 63) ? ~(~l1 >> (l2 & shm)) : l1 >> (l2 & shm);
            break;
            /* tests */
        case TOK_ULT: l1 = l1 < l2; break;
        case TOK_UGE: l1 = l1 >= l2; break;
        case TOK_EQ: l1 = l1 == l2; break;
        case TOK_NE: l1 = l1 != l2; break;
        case TOK_ULE: l1 = l1 <= l2; break;
        case TOK_UGT: l1 = l1 > l2; break;
        case TOK_LT: l1 = gen_opic_lt(l1, l2); break;
        case TOK_GE: l1 = !gen_opic_lt(l1, l2); break;
        case TOK_LE: l1 = !gen_opic_lt(l2, l1); break;
        case TOK_GT: l1 = gen_opic_lt(l2, l1); break;
            /* logical */
        case TOK_LAND: l1 = l1 && l2; break;
        case TOK_LOR: l1 = l1 || l2; break;
        default:
            goto general_case;
        }
	if (t1 != VT_LLONG && (PTR_SIZE != 8 || t1 != VT_PTR))
	    l1 = ((uint32_t)l1 |
		(v1->type.t & VT_UNSIGNED ? 0 : -(l1 & 0x80000000)));
        v1->c.i = l1;
        S->tccgen_vtop--;
    } else {
        /* if commutative ops, put c2 as constant */
        if (c1 && (op == '+' || op == '&' || op == '^' || 
                   op == '|' || op == '*' || op == TOK_EQ || op == TOK_NE)) {
            vswap(S);
            c2 = c1; //c = c1, c1 = c2, c2 = c;
            l2 = l1; //l = l1, l1 = l2, l2 = l;
        }
        if (!S->tccgen_const_wanted &&
            c1 && ((l1 == 0 &&
                    (op == TOK_SHL || op == TOK_SHR || op == TOK_SAR)) ||
                   (l1 == -1 && op == TOK_SAR))) {
            /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
            S->tccgen_vtop--;
        } else if (!S->tccgen_const_wanted &&
                   c2 && ((l2 == 0 && (op == '&' || op == '*')) ||
                          (op == '|' &&
                            (l2 == -1 || (l2 == 0xFFFFFFFF && t2 != VT_LLONG))) ||
                          (l2 == 1 && (op == '%' || op == TOK_UMOD)))) {
            /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
            if (l2 == 1)
                S->tccgen_vtop->c.i = 0;
            vswap(S);
            S->tccgen_vtop--;
        } else if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
                          op == TOK_PDIV) &&
                           l2 == 1) ||
                          ((op == '+' || op == '-' || op == '|' || op == '^' ||
                            op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
                           l2 == 0) ||
                          (op == '&' &&
                            (l2 == -1 || (l2 == 0xFFFFFFFF && t2 != VT_LLONG))))) {
            /* filter out NOP operations like x*1, x-0, x&-1... */
            S->tccgen_vtop--;
        } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
            /* try to use shifts instead of muls or divs */
            if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
                int n = -1;
                while (l2) {
                    l2 >>= 1;
                    n++;
                }
                S->tccgen_vtop->c.i = n;
                if (op == '*')
                    op = TOK_SHL;
                else if (op == TOK_PDIV)
                    op = TOK_SAR;
                else
                    op = TOK_SHR;
            }
            goto general_case;
        } else if (c2 && (op == '+' || op == '-') &&
                   (((S->tccgen_vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
                    || (S->tccgen_vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
            /* symbol + constant case */
            if (op == '-')
                l2 = -l2;
	    l2 += S->tccgen_vtop[-1].c.i;
	    /* The backends can't always deal with addends to symbols
	       larger than +-1<<31.  Don't construct such.  */
	    if ((int)l2 != l2)
	        goto general_case;
            S->tccgen_vtop--;
            S->tccgen_vtop->c.i = l2;
        } else {
        general_case:
                /* call low level op generator */
                if (t1 == VT_LLONG || t2 == VT_LLONG ||
                    (PTR_SIZE == 8 && (t1 == VT_PTR || t2 == VT_PTR)))
                    gen_opl(S, op);
                else
                    gen_opi(S, op);
        }
    }
}

#if defined TCC_TARGET_X86_64 || defined TCC_TARGET_I386
# define gen_negf gen_opf
#elif defined TCC_TARGET_ARM
void gen_negf(TCCState* S, int op)
{
    /* arm will detect 0-x and replace by vneg */
    vpushi(S, 0), vswap(S), gen_op(S, '-');
}
#else
/* XXX: implement in gen_opf() for other backends too */
void gen_negf(TCCState* S, int op)
{
    /* In IEEE negate(x) isn't subtract(0,x).  Without NaNs it's
       subtract(-0, x), but with them it's really a sign flip
       operation.  We implement this with bit manipulation and have
       to do some type reinterpretation for this, which TCC can do
       only via memory.  */

    int align, size, bt;

    size = type_size(&S->tccgen_vtop->type, &align);
    bt = S->tccgen_vtop->type.t & VT_BTYPE;
    save_reg(S, gv(S, RC_TYPE(bt)));
    vdup(S);
    incr_bf_adr(S, size - 1);
    vdup(S);
    vpushi(S, 0x80); /* flip sign */
    gen_op(S, '^');
    vstore(S);
    vpop(S);
}
#endif

/* generate a floating point operation with constant propagation */
static void gen_opif(TCCState* S, int op)
{
    int c1, c2;
    SValue *v1, *v2;
#if defined _MSC_VER && defined __x86_64__
    /* avoid bad optimization with f1 -= f2 for f1:-0.0, f2:0.0 */
    volatile
#endif
    long double f1, f2;

    v1 = S->tccgen_vtop - 1;
    v2 = S->tccgen_vtop;
    if (op == TOK_NEG)
        v1 = v2;

    /* currently, we cannot do computations with forward symbols */
    c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
    c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
    if (c1 && c2) {
        if (v1->type.t == VT_FLOAT) {
            f1 = v1->c.f;
            f2 = v2->c.f;
        } else if (v1->type.t == VT_DOUBLE) {
            f1 = v1->c.d;
            f2 = v2->c.d;
        } else {
            f1 = v1->c.ld;
            f2 = v2->c.ld;
        }
        /* NOTE: we only do constant propagation if finite number (not
           NaN or infinity) (ANSI spec) */
        if (!(ieee_finite(f1) || !ieee_finite(f2)) && !S->tccgen_const_wanted)
            goto general_case;
        switch(op) {
        case '+': f1 += f2; break;
        case '-': f1 -= f2; break;
        case '*': f1 *= f2; break;
        case '/': 
            if (f2 == 0.0) {
                union { float f; unsigned u; } x1, x2, y;
		/* If not in initializer we need to potentially generate
		   FP exceptions at runtime, otherwise we want to fold.  */
                if (!S->tccgen_const_wanted)
                    goto general_case;
                /* the run-time result of 0.0/0.0 on x87, also of other compilers
                   when used to compile the f1 /= f2 below, would be -nan */
                x1.f = f1, x2.f = f2;
                if (f1 == 0.0)
                    y.u = 0x7fc00000; /* nan */
                else
                    y.u = 0x7f800000; /* infinity */
                y.u |= (x1.u ^ x2.u) & 0x80000000; /* set sign */
                f1 = y.f;
                break;
            }
            f1 /= f2;
            break;
        case TOK_NEG:
            f1 = -f1;
            goto unary_result;
            /* XXX: also handles tests ? */
        default:
            goto general_case;
        }
        S->tccgen_vtop--;
    unary_result:
        /* XXX: overflow test ? */
        if (v1->type.t == VT_FLOAT) {
            v1->c.f = f1;
        } else if (v1->type.t == VT_DOUBLE) {
            v1->c.d = f1;
        } else {
            v1->c.ld = f1;
        }
    } else {
    general_case:
        if (op == TOK_NEG) {
            gen_negf(S, op);
        } else {
            gen_opf(S, op);
        }
    }
}

/* print a type. If 'varstr' is not NULL, then the variable is also
   printed in the type */
/* XXX: union */
/* XXX: add array and function pointers */
static void type_to_str(TCCState* S, char *buf, int buf_size,
                 CType *type, const char *varstr)
{
    int bt, v, t;
    Sym *s, *sa;
    char buf1[256];
    const char *tstr;

    t = type->t;
    bt = t & VT_BTYPE;
    buf[0] = '\0';

    if (t & VT_EXTERN)
        pstrcat(buf, buf_size, "extern ");
    if (t & VT_STATIC)
        pstrcat(buf, buf_size, "static ");
    if (t & VT_TYPEDEF)
        pstrcat(buf, buf_size, "typedef ");
    if (t & VT_INLINE)
        pstrcat(buf, buf_size, "inline ");
    if (bt != VT_PTR) {
        if (t & VT_VOLATILE)
            pstrcat(buf, buf_size, "volatile ");
        if (t & VT_CONSTANT)
            pstrcat(buf, buf_size, "const ");
    }
    if (((t & VT_DEFSIGN) && bt == VT_BYTE)
        || ((t & VT_UNSIGNED)
            && (bt == VT_SHORT || bt == VT_INT || bt == VT_LLONG)
            && !IS_ENUM(t)
            ))
        pstrcat(buf, buf_size, (t & VT_UNSIGNED) ? "unsigned " : "signed ");

    buf_size -= strlen(buf);
    buf += strlen(buf);

    switch(bt) {
    case VT_VOID:
        tstr = "void";
        goto add_tstr;
    case VT_BOOL:
        tstr = "_Bool";
        goto add_tstr;
    case VT_BYTE:
        tstr = "char";
        goto add_tstr;
    case VT_SHORT:
        tstr = "short";
        goto add_tstr;
    case VT_INT:
        tstr = "int";
        goto maybe_long;
    case VT_LLONG:
        tstr = "long long";
    maybe_long:
        if (t & VT_LONG)
            tstr = "long";
        if (!IS_ENUM(t))
            goto add_tstr;
        tstr = "enum ";
        goto tstruct;
    case VT_FLOAT:
        tstr = "float";
        goto add_tstr;
    case VT_DOUBLE:
        tstr = "double";
        if (!(t & VT_LONG))
            goto add_tstr;
    case VT_LDOUBLE:
        tstr = "long double";
    add_tstr:
        pstrcat(buf, buf_size, tstr);
        break;
    case VT_STRUCT:
        tstr = "struct ";
        if (IS_UNION(t))
            tstr = "union ";
    tstruct:
        pstrcat(buf, buf_size, tstr);
        v = type->ref->v & ~SYM_STRUCT;
        if (v >= SYM_FIRST_ANOM)
            pstrcat(buf, buf_size, "<anonymous>");
        else
            pstrcat(buf, buf_size, get_tok_str(S, v, NULL));
        break;
    case VT_FUNC:
        s = type->ref;
        buf1[0]=0;
        if (varstr && '*' == *varstr) {
            pstrcat(buf1, sizeof(buf1), "(");
            pstrcat(buf1, sizeof(buf1), varstr);
            pstrcat(buf1, sizeof(buf1), ")");
        }
        pstrcat(buf1, buf_size, "(");
        sa = s->next;
        while (sa != NULL) {
            char buf2[256];
            type_to_str(S, buf2, sizeof(buf2), &sa->type, NULL);
            pstrcat(buf1, sizeof(buf1), buf2);
            sa = sa->next;
            if (sa)
                pstrcat(buf1, sizeof(buf1), ", ");
        }
        if (s->f.func_type == FUNC_ELLIPSIS)
            pstrcat(buf1, sizeof(buf1), ", ...");
        pstrcat(buf1, sizeof(buf1), ")");
        type_to_str(S, buf, buf_size, &s->type, buf1);
        goto no_var;
    case VT_PTR:
        s = type->ref;
        if (t & VT_ARRAY) {
            if (varstr && '*' == *varstr)
                snprintf(buf1, sizeof(buf1), "(%s)[%d]", varstr, s->c);
            else
                snprintf(buf1, sizeof(buf1), "%s[%d]", varstr ? varstr : "", s->c);
            type_to_str(S, buf, buf_size, &s->type, buf1);
            goto no_var;
        }
        pstrcpy(buf1, sizeof(buf1), "*");
        if (t & VT_CONSTANT)
            pstrcat(buf1, buf_size, "const ");
        if (t & VT_VOLATILE)
            pstrcat(buf1, buf_size, "volatile ");
        if (varstr)
            pstrcat(buf1, sizeof(buf1), varstr);
        type_to_str(S, buf, buf_size, &s->type, buf1);
        goto no_var;
    }
    if (varstr) {
        pstrcat(buf, buf_size, " ");
        pstrcat(buf, buf_size, varstr);
    }
 no_var: ;
}

static void type_incompatibility_error(TCCState* S, CType* st, CType* dt, const char* fmt)
{
    char buf1[256], buf2[256];
    type_to_str(S, buf1, sizeof(buf1), st, NULL);
    type_to_str(S, buf2, sizeof(buf2), dt, NULL);
    tcc_error(S, fmt, buf1, buf2);
}

static void type_incompatibility_warning(TCCState* S, CType* st, CType* dt, const char* fmt)
{
    char buf1[256], buf2[256];
    type_to_str(S, buf1, sizeof(buf1), st, NULL);
    type_to_str(S, buf2, sizeof(buf2), dt, NULL);
    tcc_warning(S, fmt, buf1, buf2);
}

static int pointed_size(CType *type)
{
    int align;
    return type_size(pointed_type(type), &align);
}

static void vla_runtime_pointed_size(TCCState* S, CType *type)
{
    int align;
    vla_runtime_type_size(S, pointed_type(type), &align);
}

static inline int is_null_pointer(SValue *p)
{
    if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
        return 0;
    return ((p->type.t & VT_BTYPE) == VT_INT && (uint32_t)p->c.i == 0) ||
        ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.i == 0) ||
        ((p->type.t & VT_BTYPE) == VT_PTR &&
         (PTR_SIZE == 4 ? (uint32_t)p->c.i == 0 : p->c.i == 0) &&
         ((pointed_type(&p->type)->t & VT_BTYPE) == VT_VOID) &&
         0 == (pointed_type(&p->type)->t & (VT_CONSTANT | VT_VOLATILE))
         );
}

/* compare function types. OLD functions match any new functions */
static int is_compatible_func(CType *type1, CType *type2)
{
    Sym *s1, *s2;

    s1 = type1->ref;
    s2 = type2->ref;
    if (s1->f.func_call != s2->f.func_call)
        return 0;
    if (s1->f.func_type != s2->f.func_type
        && s1->f.func_type != FUNC_OLD
        && s2->f.func_type != FUNC_OLD)
        return 0;
    for (;;) {
        if (!is_compatible_unqualified_types(&s1->type, &s2->type))
            return 0;
        if (s1->f.func_type == FUNC_OLD || s2->f.func_type == FUNC_OLD )
            return 1;
        s1 = s1->next;
        s2 = s2->next;
        if (!s1)
            return !s2;
        if (!s2)
            return 0;
    }
}

/* return true if type1 and type2 are the same.  If unqualified is
   true, qualifiers on the types are ignored.
 */
static int compare_types(CType *type1, CType *type2, int unqualified)
{
    int bt1, t1, t2;

    t1 = type1->t & VT_TYPE;
    t2 = type2->t & VT_TYPE;
    if (unqualified) {
        /* strip qualifiers before comparing */
        t1 &= ~(VT_CONSTANT | VT_VOLATILE);
        t2 &= ~(VT_CONSTANT | VT_VOLATILE);
    }

    /* Default Vs explicit signedness only matters for char */
    if ((t1 & VT_BTYPE) != VT_BYTE) {
        t1 &= ~VT_DEFSIGN;
        t2 &= ~VT_DEFSIGN;
    }
    /* XXX: bitfields ? */
    if (t1 != t2)
        return 0;

    if ((t1 & VT_ARRAY)
        && !(type1->ref->c < 0
          || type2->ref->c < 0
          || type1->ref->c == type2->ref->c))
            return 0;

    /* test more complicated cases */
    bt1 = t1 & VT_BTYPE;
    if (bt1 == VT_PTR) {
        type1 = pointed_type(type1);
        type2 = pointed_type(type2);
        return is_compatible_types(type1, type2);
    } else if (bt1 == VT_STRUCT) {
        return (type1->ref == type2->ref);
    } else if (bt1 == VT_FUNC) {
        return is_compatible_func(type1, type2);
    } else if (IS_ENUM(type1->t) && IS_ENUM(type2->t)) {
        /* If both are enums then they must be the same, if only one is then
           t1 and t2 must be equal, which was checked above already.  */
        return type1->ref == type2->ref;
    } else {
        return 1;
    }
}

/* Check if OP1 and OP2 can be "combined" with operation OP, the combined
   type is stored in DEST if non-null (except for pointer plus/minus) . */
static int combine_types(TCCState* S, CType *dest, SValue *op1, SValue *op2, int op)
{
    CType *type1 = &op1->type, *type2 = &op2->type, type;
    int t1 = type1->t, t2 = type2->t, bt1 = t1 & VT_BTYPE, bt2 = t2 & VT_BTYPE;
    int ret = 1;

    type.t = VT_VOID;
    type.ref = NULL;

    if (bt1 == VT_VOID || bt2 == VT_VOID) {
        ret = op == '?' ? 1 : 0;
        /* NOTE: as an extension, we accept void on only one side */
        type.t = VT_VOID;
    } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
        if (op == '+') ; /* Handled in caller */
        /* http://port70.net/~nsz/c/c99/n1256.html#6.5.15p6 */
        /* If one is a null ptr constant the result type is the other.  */
        else if (is_null_pointer (op2)) type = *type1;
        else if (is_null_pointer (op1)) type = *type2;
        else if (bt1 != bt2) {
            /* accept comparison or cond-expr between pointer and integer
               with a warning */
            if ((op == '?' || TOK_ISCOND(op))
                && (is_integer_btype(bt1) || is_integer_btype(bt2)))
              tcc_warning(S, "pointer/integer mismatch in %s",
                          op == '?' ? "conditional expression" : "comparison");
            else if (op != '-' || !is_integer_btype(bt2))
              ret = 0;
            type = *(bt1 == VT_PTR ? type1 : type2);
        } else {
            CType *pt1 = pointed_type(type1);
            CType *pt2 = pointed_type(type2);
            int pbt1 = pt1->t & VT_BTYPE;
            int pbt2 = pt2->t & VT_BTYPE;
            int newquals, copied = 0;
            if (pbt1 != VT_VOID && pbt2 != VT_VOID
                && !compare_types(pt1, pt2, 1/*unqualif*/)) {
                if (op != '?' && !TOK_ISCOND(op))
                  ret = 0;
                else
                  type_incompatibility_warning(S, type1, type2,
                    op == '?'
                     ? "pointer type mismatch in conditional expression ('%s' and '%s')"
                     : "pointer type mismatch in comparison('%s' and '%s')");
            }
            if (op == '?') {
                /* pointers to void get preferred, otherwise the
                   pointed to types minus qualifs should be compatible */
                type = *((pbt1 == VT_VOID) ? type1 : type2);
                /* combine qualifs */
                newquals = ((pt1->t | pt2->t) & (VT_CONSTANT | VT_VOLATILE));
                if ((~pointed_type(&type)->t & (VT_CONSTANT | VT_VOLATILE))
                    & newquals)
                  {
                    /* copy the pointer target symbol */
                    type.ref = sym_push(S, SYM_FIELD, &type.ref->type,
                                        0, type.ref->c);
                    copied = 1;
                    pointed_type(&type)->t |= newquals;
                  }
                /* pointers to incomplete arrays get converted to
                   pointers to completed ones if possible */
                if (pt1->t & VT_ARRAY
                    && pt2->t & VT_ARRAY
                    && pointed_type(&type)->ref->c < 0
                    && (pt1->ref->c > 0 || pt2->ref->c > 0))
                  {
                    if (!copied)
                      type.ref = sym_push(S, SYM_FIELD, &type.ref->type,
                                          0, type.ref->c);
                    pointed_type(&type)->ref =
                        sym_push(S, SYM_FIELD, &pointed_type(&type)->ref->type,
                                 0, pointed_type(&type)->ref->c);
                    pointed_type(&type)->ref->c =
                        0 < pt1->ref->c ? pt1->ref->c : pt2->ref->c;
                  }
            }
        }
        if (TOK_ISCOND(op))
          type.t = VT_SIZE_T;
    } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
        if (op != '?' || !compare_types(type1, type2, 1))
          ret = 0;
        type = *type1;
    } else if (is_float(bt1) || is_float(bt2)) {
        if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
            type.t = VT_LDOUBLE;
        } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
            type.t = VT_DOUBLE;
        } else {
            type.t = VT_FLOAT;
        }
    } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
        /* cast to biggest op */
        type.t = VT_LLONG | VT_LONG;
        if (bt1 == VT_LLONG)
          type.t &= t1;
        if (bt2 == VT_LLONG)
          type.t &= t2;
        /* convert to unsigned if it does not fit in a long long */
        if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED) ||
            (t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED))
          type.t |= VT_UNSIGNED;
    } else {
        /* integer operations */
        type.t = VT_INT | (VT_LONG & (t1 | t2));
        /* convert to unsigned if it does not fit in an integer */
        if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED) ||
            (t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED))
          type.t |= VT_UNSIGNED;
    }
    if (dest)
      *dest = type;
    return ret;
}

/* generic gen_op: handles types problems */
ST_FUNC void gen_op(TCCState* S, int op)
{
    int u, t1, t2, bt1, bt2, t;
    CType type1, combtype;

redo:
    t1 = S->tccgen_vtop[-1].type.t;
    t2 = S->tccgen_vtop[0].type.t;
    bt1 = t1 & VT_BTYPE;
    bt2 = t2 & VT_BTYPE;
        
    if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
	if (bt2 == VT_FUNC) {
	    mk_pointer(S, &S->tccgen_vtop->type);
	    gaddrof(S);
	}
	if (bt1 == VT_FUNC) {
	    vswap(S);
	    mk_pointer(S, &S->tccgen_vtop->type);
	    gaddrof(S);
	    vswap(S);
	}
	goto redo;
    } else if (!combine_types(S, &combtype, S->tccgen_vtop - 1, S->tccgen_vtop, op)) {
        tcc_error_noabort(S, "invalid operand types for binary operation");
        vpop(S);
    } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
        /* at least one operand is a pointer */
        /* relational op: must be both pointers */
        if (TOK_ISCOND(op))
            goto std_op;
        /* if both pointers, then it must be the '-' op */
        if (bt1 == VT_PTR && bt2 == VT_PTR) {
            if (op != '-')
                tcc_error(S, "cannot use pointers here");
            if (S->tccgen_vtop[-1].type.t & VT_VLA) {
                vla_runtime_pointed_size(S, &S->tccgen_vtop[-1].type);
            } else {
                vpushi(S, pointed_size(&S->tccgen_vtop[-1].type));
            }
            vrott(S, 3);
            gen_opic(S, op);
            S->tccgen_vtop->type.t = VT_PTRDIFF_T;
            vswap(S);
            gen_op(S, TOK_PDIV);
        } else {
            /* exactly one pointer : must be '+' or '-'. */
            if (op != '-' && op != '+')
                tcc_error(S, "cannot use pointers here");
            /* Put pointer as first operand */
            if (bt2 == VT_PTR) {
                vswap(S);
                t = t1, t1 = t2, t2 = t;
            }
#if PTR_SIZE == 4
            if ((S->tccgen_vtop[0].type.t & VT_BTYPE) == VT_LLONG)
                /* XXX: truncate here because gen_opl can't handle ptr + long long */
                gen_cast_s(S, VT_INT);
#endif
            type1 = S->tccgen_vtop[-1].type;
            if (S->tccgen_vtop[-1].type.t & VT_VLA)
                vla_runtime_pointed_size(S, &S->tccgen_vtop[-1].type);
            else {
                u = pointed_size(&S->tccgen_vtop[-1].type);
                if (u < 0)
                    tcc_error(S, "unknown array element size");
#if PTR_SIZE == 8
                vpushll(S, u);
#else
                /* XXX: cast to int ? (long long case) */
                vpushi(S, u);
#endif
            }
            gen_op(S, '*');
#ifdef CONFIG_TCC_BCHECK
            if (S->do_bounds_check && !S->tccgen_const_wanted) {
                /* if bounded pointers, we generate a special code to
                   test bounds */
                if (op == '-') {
                    vpushi(S, 0);
                    vswap(S);
                    gen_op(S, '-');
                }
                gen_bounded_ptr_add(S);
            } else
#endif
            {
                gen_opic(S, op);
            }
            type1.t &= ~VT_ARRAY;
            /* put again type if gen_opic() swaped operands */
            S->tccgen_vtop->type = type1;
        }
    } else {
        /* floats can only be used for a few operations */
        if (is_float(combtype.t)
            && op != '+' && op != '-' && op != '*' && op != '/'
            && !TOK_ISCOND(op))
            tcc_error(S, "invalid operands for binary operation");
        else if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL) {
            t = bt1 == VT_LLONG ? VT_LLONG : VT_INT;
            if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (t | VT_UNSIGNED))
              t |= VT_UNSIGNED;
            t |= (VT_LONG & t1);
            combtype.t = t;
        }
    std_op:
        t = t2 = combtype.t;
        /* XXX: currently, some unsigned operations are explicit, so
           we modify them here */
        if (t & VT_UNSIGNED) {
            if (op == TOK_SAR)
                op = TOK_SHR;
            else if (op == '/')
                op = TOK_UDIV;
            else if (op == '%')
                op = TOK_UMOD;
            else if (op == TOK_LT)
                op = TOK_ULT;
            else if (op == TOK_GT)
                op = TOK_UGT;
            else if (op == TOK_LE)
                op = TOK_ULE;
            else if (op == TOK_GE)
                op = TOK_UGE;
        }
        vswap(S);
        gen_cast_s(S, t);
        vswap(S);
        /* special case for shifts and long long: we keep the shift as
           an integer */
        if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
            t2 = VT_INT;
        gen_cast_s(S, t2);
        if (is_float(t))
            gen_opif(S, op);
        else
            gen_opic(S, op);
        if (TOK_ISCOND(op)) {
            /* relational op: the result is an int */
            S->tccgen_vtop->type.t = VT_INT;
        } else {
            S->tccgen_vtop->type.t = t;
        }
    }
    // Make sure that we have converted to an rvalue:
    if (S->tccgen_vtop->r & VT_LVAL)
        gv(S, is_float(S->tccgen_vtop->type.t & VT_BTYPE) ? RC_FLOAT : RC_INT);
}

#if defined TCC_TARGET_ARM64 || defined TCC_TARGET_RISCV64 || defined TCC_TARGET_ARM
#define gen_cvt_itof1 gen_cvt_itof
#else
/* generic itof for unsigned long long case */
static void gen_cvt_itof1(TCCState* S, int t)
{
    if ((S->tccgen_vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) == 
        (VT_LLONG | VT_UNSIGNED)) {

        if (t == VT_FLOAT)
            vpush_helper_func(S, TOK___floatundisf);
#if LDOUBLE_SIZE != 8
        else if (t == VT_LDOUBLE)
            vpush_helper_func(S, TOK___floatundixf);
#endif
        else
            vpush_helper_func(S, TOK___floatundidf);
        vrott(S, 2);
        gfunc_call(S, 1);
        vpushi(S, 0);
        PUT_R_RET(S->tccgen_vtop, t);
    } else {
        gen_cvt_itof(S, t);
    }
}
#endif

#if defined TCC_TARGET_ARM64 || defined TCC_TARGET_RISCV64
#define gen_cvt_ftoi1 gen_cvt_ftoi
#else
/* generic ftoi for unsigned long long case */
static void gen_cvt_ftoi1(TCCState* S, int t)
{
    int st;
    if (t == (VT_LLONG | VT_UNSIGNED)) {
        /* not handled natively */
        st = S->tccgen_vtop->type.t & VT_BTYPE;
        if (st == VT_FLOAT)
            vpush_helper_func(S, TOK___fixunssfdi);
#if LDOUBLE_SIZE != 8
        else if (st == VT_LDOUBLE)
            vpush_helper_func(S, TOK___fixunsxfdi);
#endif
        else
            vpush_helper_func(S, TOK___fixunsdfdi);
        vrott(S, 2);
        gfunc_call(S, 1);
        vpushi(S, 0);
        PUT_R_RET(S->tccgen_vtop, t);
    } else {
        gen_cvt_ftoi(S, t);
    }
}
#endif

/* special delayed cast for char/short */
static void force_charshort_cast(TCCState* S)
{
    int sbt = BFGET(S->tccgen_vtop->r, VT_MUSTCAST) == 2 ? VT_LLONG : VT_INT;
    int dbt = S->tccgen_vtop->type.t;
    S->tccgen_vtop->r &= ~VT_MUSTCAST;
    S->tccgen_vtop->type.t = sbt;
    gen_cast_s(S, dbt == VT_BOOL ? VT_BYTE|VT_UNSIGNED : dbt);
    S->tccgen_vtop->type.t = dbt;
}

static void gen_cast_s(TCCState* S, int t)
{
    CType type;
    type.t = t;
    type.ref = NULL;
    gen_cast(S, &type);
}

/* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
static void gen_cast(TCCState* S, CType *type)
{
    int sbt, dbt, sf, df, c;
    int dbt_bt, sbt_bt, ds, ss, bits, trunc;

    /* special delayed cast for char/short */
    if (S->tccgen_vtop->r & VT_MUSTCAST)
        force_charshort_cast(S);

    /* bitfields first get cast to ints */
    if (S->tccgen_vtop->type.t & VT_BITFIELD)
        gv(S, RC_INT);

    dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
    sbt = S->tccgen_vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
    if (sbt == VT_FUNC)
        sbt = VT_PTR;

again:
    if (sbt != dbt) {
        sf = is_float(sbt);
        df = is_float(dbt);
        dbt_bt = dbt & VT_BTYPE;
        sbt_bt = sbt & VT_BTYPE;
        if (dbt_bt == VT_VOID)
            goto done;
        if (sbt_bt == VT_VOID) {
error:
            cast_error(S, &S->tccgen_vtop->type, type);
        }

        c = (S->tccgen_vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
#if !defined TCC_IS_NATIVE && !defined TCC_IS_NATIVE_387
        c &= (dbt != VT_LDOUBLE) | !!S->tccgen_nocode_wanted;
#endif
        if (c) {
            /* constant case: we can do it now */
            /* XXX: in ISOC, cannot do it if error in convert */
            if (sbt == VT_FLOAT)
                S->tccgen_vtop->c.ld = S->tccgen_vtop->c.f;
            else if (sbt == VT_DOUBLE)
                S->tccgen_vtop->c.ld = S->tccgen_vtop->c.d;

            if (df) {
                if (sbt_bt == VT_LLONG) {
                    if ((sbt & VT_UNSIGNED) || !(S->tccgen_vtop->c.i >> 63))
                        S->tccgen_vtop->c.ld = S->tccgen_vtop->c.i;
                    else
                        S->tccgen_vtop->c.ld = -(long double)-S->tccgen_vtop->c.i;
                } else if(!sf) {
                    if ((sbt & VT_UNSIGNED) || !(S->tccgen_vtop->c.i >> 31))
                        S->tccgen_vtop->c.ld = (uint32_t)S->tccgen_vtop->c.i;
                    else
                        S->tccgen_vtop->c.ld = -(long double)-(uint32_t)S->tccgen_vtop->c.i;
                }

                if (dbt == VT_FLOAT)
                    S->tccgen_vtop->c.f = (float)S->tccgen_vtop->c.ld;
                else if (dbt == VT_DOUBLE)
                    S->tccgen_vtop->c.d = (double)S->tccgen_vtop->c.ld;
            } else if (sf && dbt == VT_BOOL) {
                S->tccgen_vtop->c.i = (S->tccgen_vtop->c.ld != 0);
            } else {
                if(sf)
                    S->tccgen_vtop->c.i = S->tccgen_vtop->c.ld;
                else if (sbt_bt == VT_LLONG || (PTR_SIZE == 8 && sbt == VT_PTR))
                    ;
                else if (sbt & VT_UNSIGNED)
                    S->tccgen_vtop->c.i = (uint32_t)S->tccgen_vtop->c.i;
                else
                    S->tccgen_vtop->c.i = ((uint32_t)S->tccgen_vtop->c.i | -(S->tccgen_vtop->c.i & 0x80000000));

                if (dbt_bt == VT_LLONG || (PTR_SIZE == 8 && dbt == VT_PTR))
                    ;
                else if (dbt == VT_BOOL)
                    S->tccgen_vtop->c.i = (S->tccgen_vtop->c.i != 0);
                else {
                    uint32_t m = dbt_bt == VT_BYTE ? 0xff :
                                 dbt_bt == VT_SHORT ? 0xffff :
                                  0xffffffff;
                    S->tccgen_vtop->c.i &= m;
                    if (!(dbt & VT_UNSIGNED))
                        S->tccgen_vtop->c.i |= -(S->tccgen_vtop->c.i & ((m >> 1) + 1));
                }
            }
            goto done;

        } else if (dbt == VT_BOOL
            && (S->tccgen_vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM))
                == (VT_CONST | VT_SYM)) {
            /* addresses are considered non-zero (see tcctest.c:sinit23) */
            S->tccgen_vtop->r = VT_CONST;
            S->tccgen_vtop->c.i = 1;
            goto done;
        }

        /* cannot generate code for global or static initializers */
        if (STATIC_DATA_WANTED)
            goto done;

        /* non constant case: generate code */
        if (dbt == VT_BOOL) {
            gen_test_zero(S, TOK_NE);
            goto done;
        }

        if (sf || df) {
            if (sf && df) {
                /* convert from fp to fp */
                gen_cvt_ftof(S, dbt);
            } else if (df) {
                /* convert int to fp */
                gen_cvt_itof1(S, dbt);
            } else {
                /* convert fp to int */
                sbt = dbt;
                if (dbt_bt != VT_LLONG && dbt_bt != VT_INT)
                    sbt = VT_INT;
                gen_cvt_ftoi1(S, sbt);
                goto again; /* may need char/short cast */
            }
            goto done;
        }

        ds = btype_size(dbt_bt);
        ss = btype_size(sbt_bt);
        if (ds == 0 || ss == 0)
            goto error;

        if (IS_ENUM(type->t) && type->ref->c < 0)
            tcc_error(S, "cast to incomplete type");

        /* same size and no sign conversion needed */
        if (ds == ss && ds >= 4)
            goto done;
        if (dbt_bt == VT_PTR || sbt_bt == VT_PTR) {
            tcc_warning(S, "cast between pointer and integer of different size");
            if (sbt_bt == VT_PTR) {
                /* put integer type to allow logical operations below */
                S->tccgen_vtop->type.t = (PTR_SIZE == 8 ? VT_LLONG : VT_INT);
            }
        }

        /* processor allows { int a = 0, b = *(char*)&a; }
           That means that if we cast to less width, we can just
           change the type and read it still later. */
        #define ALLOW_SUBTYPE_ACCESS 1

        if (ALLOW_SUBTYPE_ACCESS && (S->tccgen_vtop->r & VT_LVAL)) {
            /* value still in memory */
            if (ds <= ss)
                goto done;
            /* ss <= 4 here */
            if (ds <= 4 && !(dbt == (VT_SHORT | VT_UNSIGNED) && sbt == VT_BYTE)) {
                gv(S, RC_INT);
                goto done; /* no 64bit envolved */
            }
        }
        gv(S, RC_INT);

        trunc = 0;
#if PTR_SIZE == 4
        if (ds == 8) {
            /* generate high word */
            if (sbt & VT_UNSIGNED) {
                vpushi(S, 0);
                gv(S, RC_INT);
            } else {
                gv_dup(S);
                vpushi(S, 31);
                gen_op(S, TOK_SAR);
            }
            lbuild(S, dbt);
        } else if (ss == 8) {
            /* from long long: just take low order word */
            lexpand(S);
            vpop(S);
        }
        ss = 4;

#elif PTR_SIZE == 8
        if (ds == 8) {
            /* need to convert from 32bit to 64bit */
            if (sbt & VT_UNSIGNED) {
#if defined(TCC_TARGET_RISCV64)
                /* RISC-V keeps 32bit vals in registers sign-extended.
                   So here we need a zero-extension.  */
                trunc = 32;
#else
                goto done;
#endif
            } else {
                gen_cvt_sxtw(S);
                goto done;
            }
            ss = ds, ds = 4, dbt = sbt;
        } else if (ss == 8) {
            /* RISC-V keeps 32bit vals in registers sign-extended.
               So here we need a sign-extension for signed types and
               zero-extension. for unsigned types. */
#if !defined(TCC_TARGET_RISCV64)
            trunc = 32; /* zero upper 32 bits for non RISC-V targets */
#endif
        } else {
            ss = 4;
        }
#endif

        if (ds >= ss)
            goto done;
#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 || defined TCC_TARGET_ARM64
        if (ss == 4) {
            gen_cvt_csti(S, dbt);
            goto done;
        }
#endif
        bits = (ss - ds) * 8;
        /* for unsigned, gen_op will convert SAR to SHR */
        S->tccgen_vtop->type.t = (ss == 8 ? VT_LLONG : VT_INT) | (dbt & VT_UNSIGNED);
        vpushi(S, bits);
        gen_op(S, TOK_SHL);
        vpushi(S, bits - trunc);
        gen_op(S, TOK_SAR);
        vpushi(S, trunc);
        gen_op(S, TOK_SHR);
    }
done:
    S->tccgen_vtop->type = *type;
    S->tccgen_vtop->type.t &= ~ ( VT_CONSTANT | VT_VOLATILE | VT_ARRAY );
}

/* return type size as known at compile time. Put alignment at 'a' */
ST_FUNC int type_size(CType *type, int *a)
{
    Sym *s;
    int bt;

    bt = type->t & VT_BTYPE;
    if (bt == VT_STRUCT) {
        /* struct/union */
        s = type->ref;
        *a = s->r;
        return s->c;
    } else if (bt == VT_PTR) {
        if (type->t & VT_ARRAY) {
            int ts;

            s = type->ref;
            ts = type_size(&s->type, a);

            if (ts < 0 && s->c < 0)
                ts = -ts;

            return ts * s->c;
        } else {
            *a = PTR_SIZE;
            return PTR_SIZE;
        }
    } else if (IS_ENUM(type->t) && type->ref->c < 0) {
        return -1; /* incomplete enum */
    } else if (bt == VT_LDOUBLE) {
        *a = LDOUBLE_ALIGN;
        return LDOUBLE_SIZE;
    } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
#ifdef TCC_TARGET_I386
#ifdef TCC_TARGET_PE
        *a = 8;
#else
        *a = 4;
#endif
#elif defined(TCC_TARGET_ARM)
#ifdef TCC_ARM_EABI
        *a = 8; 
#else
        *a = 4;
#endif
#else
        *a = 8;
#endif
        return 8;
    } else if (bt == VT_INT || bt == VT_FLOAT) {
        *a = 4;
        return 4;
    } else if (bt == VT_SHORT) {
        *a = 2;
        return 2;
    } else if (bt == VT_QLONG || bt == VT_QFLOAT) {
        *a = 8;
        return 16;
    } else {
        /* char, void, function, _Bool */
        *a = 1;
        return 1;
    }
}

/* push type size as known at runtime time on top of value stack. Put
   alignment at 'a' */
ST_FUNC void vla_runtime_type_size(TCCState* S, CType *type, int *a)
{
    if (type->t & VT_VLA) {
        type_size(&type->ref->type, a);
        vset(S, &S->tccgen_int_type, VT_LOCAL|VT_LVAL, type->ref->c);
    } else {
        vpushi(S, type_size(type, a));
    }
}

/* return the pointed type of t */
static inline CType *pointed_type(CType *type)
{
    return &type->ref->type;
}

/* modify type so that its it is a pointer to type. */
ST_FUNC void mk_pointer(TCCState* S, CType *type)
{
    Sym *s;
    s = sym_push(S, SYM_FIELD, type, 0, -1);
    type->t = VT_PTR | (type->t & VT_STORAGE);
    type->ref = s;
}

/* return true if type1 and type2 are exactly the same (including
   qualifiers). 
*/
static int is_compatible_types(CType *type1, CType *type2)
{
    return compare_types(type1,type2,0);
}

/* return true if type1 and type2 are the same (ignoring qualifiers).
*/
static int is_compatible_unqualified_types(CType *type1, CType *type2)
{
    return compare_types(type1,type2,1);
}

static void cast_error(TCCState* S, CType *st, CType *dt)
{
    type_incompatibility_error(S, st, dt, "cannot convert '%s' to '%s'");
}

/* verify type compatibility to store vtop in 'dt' type */
static void verify_assign_cast(TCCState* S, CType *dt)
{
    CType *st, *type1, *type2;
    int dbt, sbt, qualwarn, lvl;

    st = &S->tccgen_vtop->type; /* source type */
    dbt = dt->t & VT_BTYPE;
    sbt = st->t & VT_BTYPE;
    if (dt->t & VT_CONSTANT)
        tcc_warning(S, "assignment of read-only location");
    switch(dbt) {
    case VT_VOID:
        if (sbt != dbt)
            tcc_error(S, "assignment to void expression");
        break;
    case VT_PTR:
        /* special cases for pointers */
        /* '0' can also be a pointer */
        if (is_null_pointer(S->tccgen_vtop))
            break;
        /* accept implicit pointer to integer cast with warning */
        if (is_integer_btype(sbt)) {
            tcc_warning(S, "assignment makes pointer from integer without a cast");
            break;
        }
        type1 = pointed_type(dt);
        if (sbt == VT_PTR)
            type2 = pointed_type(st);
        else if (sbt == VT_FUNC)
            type2 = st; /* a function is implicitly a function pointer */
        else
            goto error;
        if (is_compatible_types(type1, type2))
            break;
        for (qualwarn = lvl = 0;; ++lvl) {
            if (((type2->t & VT_CONSTANT) && !(type1->t & VT_CONSTANT)) ||
                ((type2->t & VT_VOLATILE) && !(type1->t & VT_VOLATILE)))
                qualwarn = 1;
            dbt = type1->t & (VT_BTYPE|VT_LONG);
            sbt = type2->t & (VT_BTYPE|VT_LONG);
            if (dbt != VT_PTR || sbt != VT_PTR)
                break;
            type1 = pointed_type(type1);
            type2 = pointed_type(type2);
        }
        if (!is_compatible_unqualified_types(type1, type2)) {
            if ((dbt == VT_VOID || sbt == VT_VOID) && lvl == 0) {
                /* void * can match anything */
            } else if (dbt == sbt
                && is_integer_btype(sbt & VT_BTYPE)
                && IS_ENUM(type1->t) + IS_ENUM(type2->t)
                    + !!((type1->t ^ type2->t) & VT_UNSIGNED) < 2) {
		/* Like GCC don't warn by default for merely changes
		   in pointer target signedness.  Do warn for different
		   base types, though, in particular for unsigned enums
		   and signed int targets.  */
            } else {
                tcc_warning(S, "assignment from incompatible pointer type");
                break;
            }
        }
        if (qualwarn)
            tcc_warning_c(warn_discarded_qualifiers)(S, "assignment discards qualifiers from pointer target type");
        break;
    case VT_BYTE:
    case VT_SHORT:
    case VT_INT:
    case VT_LLONG:
        if (sbt == VT_PTR || sbt == VT_FUNC) {
            tcc_warning(S, "assignment makes integer from pointer without a cast");
        } else if (sbt == VT_STRUCT) {
            goto case_VT_STRUCT;
        }
        /* XXX: more tests */
        break;
    case VT_STRUCT:
    case_VT_STRUCT:
        if (!is_compatible_unqualified_types(dt, st)) {
    error:
            cast_error(S, st, dt);
        }
        break;
    }
}

static void gen_assign_cast(TCCState* S, CType *dt)
{
    verify_assign_cast(S, dt);
    gen_cast(S, dt);
}

/* store vtop in lvalue pushed on stack */
ST_FUNC void vstore(TCCState* S)
{
    int sbt, dbt, ft, r, size, align, bit_size, bit_pos, delayed_cast;

    ft = S->tccgen_vtop[-1].type.t;
    sbt = S->tccgen_vtop->type.t & VT_BTYPE;
    dbt = ft & VT_BTYPE;

    verify_assign_cast(S, &S->tccgen_vtop[-1].type);

    if (sbt == VT_STRUCT) {
        /* if structure, only generate pointer */
        /* structure assignment : generate memcpy */
        /* XXX: optimize if small size */
            size = type_size(&S->tccgen_vtop->type, &align);

            /* destination */
            vswap(S);
#ifdef CONFIG_TCC_BCHECK
            if (S->tccgen_vtop->r & VT_MUSTBOUND)
                gbound(S); /* check would be wrong after gaddrof() */
#endif
            S->tccgen_vtop->type.t = VT_PTR;
            gaddrof(S);

            /* address of memcpy() */
#ifdef TCC_ARM_EABI
            if(!(align & 7))
                vpush_helper_func(S, TOK_memmove8);
            else if(!(align & 3))
                vpush_helper_func(S, TOK_memmove4);
            else
#endif
            /* Use memmove, rather than memcpy, as dest and src may be same: */
            vpush_helper_func(S, TOK_memmove);

            vswap(S);
            /* source */
            vpushv(S, S->tccgen_vtop - 2);
#ifdef CONFIG_TCC_BCHECK
            if (S->tccgen_vtop->r & VT_MUSTBOUND)
                gbound(S);
#endif
            S->tccgen_vtop->type.t = VT_PTR;
            gaddrof(S);
            /* type size */
            vpushi(S, size);
            gfunc_call(S, 3);
        /* leave source on stack */

    } else if (ft & VT_BITFIELD) {
        /* bitfield store handling */

        /* save lvalue as expression result (example: s.b = s.a = n;) */
        vdup(S), S->tccgen_vtop[-1] = S->tccgen_vtop[-2];

        bit_pos = BIT_POS(ft);
        bit_size = BIT_SIZE(ft);
        /* remove bit field info to avoid loops */
        S->tccgen_vtop[-1].type.t = ft & ~VT_STRUCT_MASK;

        if (dbt == VT_BOOL) {
            gen_cast(S, &S->tccgen_vtop[-1].type);
            S->tccgen_vtop[-1].type.t = (S->tccgen_vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
        }
        r = adjust_bf(S->tccgen_vtop - 1, bit_pos, bit_size);
        if (dbt != VT_BOOL) {
            gen_cast(S, &S->tccgen_vtop[-1].type);
            dbt = S->tccgen_vtop[-1].type.t & VT_BTYPE;
        }
        if (r == VT_STRUCT) {
            store_packed_bf(S, bit_pos, bit_size);
        } else {
            unsigned long long mask = (1ULL << bit_size) - 1;
            if (dbt != VT_BOOL) {
                /* mask source */
                if (dbt == VT_LLONG)
                    vpushll(S, mask);
                else
                    vpushi(S, (unsigned)mask);
                gen_op(S, '&');
            }
            /* shift source */
            vpushi(S, bit_pos);
            gen_op(S, TOK_SHL);
            vswap(S);
            /* duplicate destination */
            vdup(S);
            vrott(S, 3);
            /* load destination, mask and or with source */
            if (dbt == VT_LLONG)
                vpushll(S, ~(mask << bit_pos));
            else
                vpushi(S, ~((unsigned)mask << bit_pos));
            gen_op(S, '&');
            gen_op(S, '|');
            /* store result */
            vstore(S);
            /* ... and discard */
            vpop(S);
        }
    } else if (dbt == VT_VOID) {
        --S->tccgen_vtop;
    } else {
            /* optimize char/short casts */
            delayed_cast = 0;
            if ((dbt == VT_BYTE || dbt == VT_SHORT)
                && is_integer_btype(sbt)
                ) {
                if ((S->tccgen_vtop->r & VT_MUSTCAST)
                    && btype_size(dbt) > btype_size(sbt)
                    )
                    force_charshort_cast(S);
                delayed_cast = 1;
            } else {
                gen_cast(S, &S->tccgen_vtop[-1].type);
            }

#ifdef CONFIG_TCC_BCHECK
            /* bound check case */
            if (S->tccgen_vtop[-1].r & VT_MUSTBOUND) {
                vswap(S);
                gbound(S);
                vswap(S);
            }
#endif
            gv(S, RC_TYPE(dbt)); /* generate value */

            if (delayed_cast) {
                S->tccgen_vtop->r |= BFVAL(VT_MUSTCAST, (sbt == VT_LLONG) + 1);
                //tcc_warning(S, "deley cast %x -> %x", sbt, dbt);
                S->tccgen_vtop->type.t = ft & VT_TYPE;
            }

            /* if lvalue was saved on stack, must read it */
            if ((S->tccgen_vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
                SValue sv;
                r = get_reg(S, RC_INT);
                sv.type.t = VT_PTRDIFF_T;
                sv.r = VT_LOCAL | VT_LVAL;
                sv.c.i = S->tccgen_vtop[-1].c.i;
                load(S, r, &sv);
                S->tccgen_vtop[-1].r = r | VT_LVAL;
            }

            r = S->tccgen_vtop->r & VT_VALMASK;
            /* two word case handling :
               store second register at word + 4 (or +8 for x86-64)  */
            if (USING_TWO_WORDS(dbt)) {
                int load_type = (dbt == VT_QFLOAT) ? VT_DOUBLE : VT_PTRDIFF_T;
                S->tccgen_vtop[-1].type.t = load_type;
                store(S, r, S->tccgen_vtop - 1);
                vswap(S);
                /* convert to int to increment easily */
                S->tccgen_vtop->type.t = VT_PTRDIFF_T;
                gaddrof(S);
                vpushs(S, PTR_SIZE);
                gen_op(S, '+');
                S->tccgen_vtop->r |= VT_LVAL;
                vswap(S);
                S->tccgen_vtop[-1].type.t = load_type;
                /* XXX: it works because r2 is spilled last ! */
                store(S, S->tccgen_vtop->r2, S->tccgen_vtop - 1);
            } else {
                /* single word */
                store(S, r, S->tccgen_vtop - 1);
            }
        vswap(S);
        S->tccgen_vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
    }
}

/* post defines POST/PRE add. c is the token ++ or -- */
ST_FUNC void inc(TCCState* S, int post, int c)
{
    test_lvalue(S);
    vdup(S); /* save lvalue */
    if (post) {
        gv_dup(S); /* duplicate value */
        vrotb(S, 3);
        vrotb(S, 3);
    }
    /* add constant */
    vpushi(S, c - TOK_MID); 
    gen_op(S, '+');
    vstore(S); /* store value */
    if (post)
        vpop(S); /* if post op, return saved value */
}

ST_FUNC void parse_mult_str (TCCState* S, CString *astr, const char *msg)
{
    /* read the string */
    if (S->tccpp_tok != TOK_STR)
        expect(S, msg);
    cstr_new(S, astr);
    while (S->tccpp_tok == TOK_STR) {
        /* XXX: add \0 handling too ? */
        cstr_cat(S, astr, S->tccpp_tokc.str.data, -1);
        next(S);
    }
    cstr_ccat(S, astr, '\0');
}

/* If I is >= 1 and a power of two, returns log2(i)+1.
   If I is 0 returns 0.  */
ST_FUNC int exact_log2p1(int i)
{
  int ret;
  if (!i)
    return 0;
  for (ret = 1; i >= 1 << 8; ret += 8)
    i >>= 8;
  if (i >= 1 << 4)
    ret += 4, i >>= 4;
  if (i >= 1 << 2)
    ret += 2, i >>= 2;
  if (i >= 1 << 1)
    ret++;
  return ret;
}

/* Parse __attribute__((...)) GNUC extension. */
static void parse_attribute(TCCState* S, AttributeDef *ad)
{
    int t, n;
    CString astr;
    
redo:
    if (S->tccpp_tok != TOK_ATTRIBUTE1 && S->tccpp_tok != TOK_ATTRIBUTE2)
        return;
    next(S);
    skip(S, '(');
    skip(S, '(');
    while (S->tccpp_tok != ')') {
        if (S->tccpp_tok < TOK_IDENT)
            expect(S, "attribute name");
        t = S->tccpp_tok;
        next(S);
        switch(t) {
	case TOK_CLEANUP1:
	case TOK_CLEANUP2:
	{
	    Sym *s;

	    skip(S, '(');
	    s = sym_find(S, S->tccpp_tok);
	    if (!s) {
	        tcc_warning_c(warn_implicit_function_declaration)(S,
                    "implicit declaration of function '%s'", get_tok_str(S, S->tccpp_tok, &S->tccpp_tokc));
	        s = external_global_sym(S, S->tccpp_tok, &S->tccgen_func_old_type);
            } else if ((s->type.t & VT_BTYPE) != VT_FUNC)
                tcc_error(S, "'%s' is not declared as function", get_tok_str(S, S->tccpp_tok, &S->tccpp_tokc));
	    ad->cleanup_func = s;
	    next(S);
            skip(S, ')');
	    break;
	}
        case TOK_CONSTRUCTOR1:
        case TOK_CONSTRUCTOR2:
            ad->f.func_ctor = 1;
            break;
        case TOK_DESTRUCTOR1:
        case TOK_DESTRUCTOR2:
            ad->f.func_dtor = 1;
            break;
        case TOK_ALWAYS_INLINE1:
        case TOK_ALWAYS_INLINE2:
            ad->f.func_alwinl = 1;
            break;
        case TOK_SECTION1:
        case TOK_SECTION2:
            skip(S, '(');
	    parse_mult_str(S, &astr, "section name");
            ad->section = find_section(S, (char *)astr.data);
            skip(S, ')');
	    cstr_free(S, &astr);
            break;
        case TOK_ALIAS1:
        case TOK_ALIAS2:
            skip(S, '(');
	    parse_mult_str(S, &astr, "alias(\"target\")");
            ad->alias_target = /* save string as token, for later */
                tok_alloc(S, (char*)astr.data, astr.size-1)->tok;
            skip(S, ')');
	    cstr_free(S, &astr);
            break;
	case TOK_VISIBILITY1:
	case TOK_VISIBILITY2:
            skip(S, '(');
	    parse_mult_str(S, &astr,
			   "visibility(\"default|hidden|internal|protected\")");
	    if (!strcmp (astr.data, "default"))
	        ad->a.visibility = STV_DEFAULT;
	    else if (!strcmp (astr.data, "hidden"))
	        ad->a.visibility = STV_HIDDEN;
	    else if (!strcmp (astr.data, "internal"))
	        ad->a.visibility = STV_INTERNAL;
	    else if (!strcmp (astr.data, "protected"))
	        ad->a.visibility = STV_PROTECTED;
	    else
                expect(S, "visibility(\"default|hidden|internal|protected\")");
            skip(S, ')');
	    cstr_free(S, &astr);
            break;
        case TOK_ALIGNED1:
        case TOK_ALIGNED2:
            if (S->tccpp_tok == '(') {
                next(S);
                n = expr_const(S);
                if (n <= 0 || (n & (n - 1)) != 0) 
                    tcc_error(S, "alignment must be a positive power of two");
                skip(S, ')');
            } else {
                n = MAX_ALIGN;
            }
            ad->a.aligned = exact_log2p1(n);
	    if (n != 1 << (ad->a.aligned - 1))
	      tcc_error(S, "alignment of %d is larger than implemented", n);
            break;
        case TOK_PACKED1:
        case TOK_PACKED2:
            ad->a.packed = 1;
            break;
        case TOK_WEAK1:
        case TOK_WEAK2:
            ad->a.weak = 1;
            break;
        case TOK_UNUSED1:
        case TOK_UNUSED2:
            /* currently, no need to handle it because tcc does not
               track unused objects */
            break;
        case TOK_NORETURN1:
        case TOK_NORETURN2:
            ad->f.func_noreturn = 1;
            break;
        case TOK_CDECL1:
        case TOK_CDECL2:
        case TOK_CDECL3:
            ad->f.func_call = FUNC_CDECL;
            break;
        case TOK_STDCALL1:
        case TOK_STDCALL2:
        case TOK_STDCALL3:
            ad->f.func_call = FUNC_STDCALL;
            break;
#ifdef TCC_TARGET_I386
        case TOK_REGPARM1:
        case TOK_REGPARM2:
            skip(S, '(');
            n = expr_const(S);
            if (n > 3) 
                n = 3;
            else if (n < 0)
                n = 0;
            if (n > 0)
                ad->f.func_call = FUNC_FASTCALL1 + n - 1;
            skip(S, ')');
            break;
        case TOK_FASTCALL1:
        case TOK_FASTCALL2:
        case TOK_FASTCALL3:
            ad->f.func_call = FUNC_FASTCALLW;
            break;            
#endif
        case TOK_MODE:
            skip(S, '(');
            switch(S->tccpp_tok) {
                case TOK_MODE_DI:
                    ad->attr_mode = VT_LLONG + 1;
                    break;
                case TOK_MODE_QI:
                    ad->attr_mode = VT_BYTE + 1;
                    break;
                case TOK_MODE_HI:
                    ad->attr_mode = VT_SHORT + 1;
                    break;
                case TOK_MODE_SI:
                case TOK_MODE_word:
                    ad->attr_mode = VT_INT + 1;
                    break;
                default:
                    tcc_warning(S, "__mode__(%s) not supported\n", get_tok_str(S, S->tccpp_tok, NULL));
                    break;
            }
            next(S);
            skip(S, ')');
            break;
        case TOK_DLLEXPORT:
            ad->a.dllexport = 1;
            break;
        case TOK_NODECORATE:
            ad->a.nodecorate = 1;
            break;
        case TOK_DLLIMPORT:
            ad->a.dllimport = 1;
            break;
        default:
            tcc_warning_c(warn_unsupported)(S, "'%s' attribute ignored", get_tok_str(S, t, NULL));
            /* skip parameters */
            if (S->tccpp_tok == '(') {
                int parenthesis = 0;
                do {
                    if (S->tccpp_tok == '(') 
                        parenthesis++;
                    else if (S->tccpp_tok == ')') 
                        parenthesis--;
                    next(S);
                } while (parenthesis && S->tccpp_tok != -1);
            }
            break;
        }
        if (S->tccpp_tok != ',')
            break;
        next(S);
    }
    skip(S, ')');
    skip(S, ')');
    goto redo;
}

static Sym * find_field (CType *type, int v, int *cumofs)
{
    Sym *s = type->ref;
    v |= SYM_FIELD;
    while ((s = s->next) != NULL) {
	if ((s->v & SYM_FIELD) &&
	    (s->type.t & VT_BTYPE) == VT_STRUCT &&
	    (s->v & ~SYM_FIELD) >= SYM_FIRST_ANOM) {
	    Sym *ret = find_field (&s->type, v, cumofs);
	    if (ret) {
                *cumofs += s->c;
	        return ret;
            }
	}
	if (s->v == v)
	  break;
    }
    return s;
}

static void check_fields (TCCState* S, CType *type, int check)
{
    Sym *s = type->ref;

    while ((s = s->next) != NULL) {
        int v = s->v & ~SYM_FIELD;
        if (v < SYM_FIRST_ANOM) {
            TokenSym *ts = S->tccpp_table_ident[v - TOK_IDENT];
            if (check && (ts->tok & SYM_FIELD))
                tcc_error(S, "duplicate member '%s'", get_tok_str(S, v, NULL));
            ts->tok ^= SYM_FIELD;
        } else if ((s->type.t & VT_BTYPE) == VT_STRUCT)
            check_fields (S, &s->type, check);
    }
}

static void struct_layout(TCCState* S, CType *type, AttributeDef *ad)
{
    int size, align, maxalign, offset, c, bit_pos, bit_size;
    int packed, a, bt, prevbt, prev_bit_size;
    int pcc = !S->ms_bitfields;
    int pragma_pack = *S->pack_stack_ptr;
    Sym *f;

    maxalign = 1;
    offset = 0;
    c = 0;
    bit_pos = 0;
    prevbt = VT_STRUCT; /* make it never match */
    prev_bit_size = 0;

//#define BF_DEBUG

    for (f = type->ref->next; f; f = f->next) {
        if (f->type.t & VT_BITFIELD)
            bit_size = BIT_SIZE(f->type.t);
        else
            bit_size = -1;
        size = type_size(&f->type, &align);
        a = f->a.aligned ? 1 << (f->a.aligned - 1) : 0;
        packed = 0;

        if (pcc && bit_size == 0) {
            /* in pcc mode, packing does not affect zero-width bitfields */

        } else {
            /* in pcc mode, attribute packed overrides if set. */
            if (pcc && (f->a.packed || ad->a.packed))
                align = packed = 1;

            /* pragma pack overrides align if lesser and packs bitfields always */
            if (pragma_pack) {
                packed = 1;
                if (pragma_pack < align)
                    align = pragma_pack;
                /* in pcc mode pragma pack also overrides individual align */
                if (pcc && pragma_pack < a)
                    a = 0;
            }
        }
        /* some individual align was specified */
        if (a)
            align = a;

        if (type->ref->type.t == VT_UNION) {
	    if (pcc && bit_size >= 0)
	        size = (bit_size + 7) >> 3;
	    offset = 0;
	    if (size > c)
	        c = size;

	} else if (bit_size < 0) {
            if (pcc)
                c += (bit_pos + 7) >> 3;
	    c = (c + align - 1) & -align;
	    offset = c;
	    if (size > 0)
	        c += size;
	    bit_pos = 0;
	    prevbt = VT_STRUCT;
	    prev_bit_size = 0;

	} else {
	    /* A bit-field.  Layout is more complicated.  There are two
	       options: PCC (GCC) compatible and MS compatible */
            if (pcc) {
		/* In PCC layout a bit-field is placed adjacent to the
                   preceding bit-fields, except if:
                   - it has zero-width
                   - an individual alignment was given
                   - it would overflow its base type container and
                     there is no packing */
                if (bit_size == 0) {
            new_field:
		    c = (c + ((bit_pos + 7) >> 3) + align - 1) & -align;
		    bit_pos = 0;
                } else if (f->a.aligned) {
                    goto new_field;
                } else if (!packed) {
                    int a8 = align * 8;
	            int ofs = ((c * 8 + bit_pos) % a8 + bit_size + a8 - 1) / a8;
                    if (ofs > size / align)
                        goto new_field;
                }

                /* in pcc mode, long long bitfields have type int if they fit */
                if (size == 8 && bit_size <= 32)
                    f->type.t = (f->type.t & ~VT_BTYPE) | VT_INT, size = 4;

                while (bit_pos >= align * 8)
                    c += align, bit_pos -= align * 8;
                offset = c;

		/* In PCC layout named bit-fields influence the alignment
		   of the containing struct using the base types alignment,
		   except for packed fields (which here have correct align).  */
		if (f->v & SYM_FIRST_ANOM
                    // && bit_size // ??? gcc on ARM/rpi does that
                    )
		    align = 1;

	    } else {
		bt = f->type.t & VT_BTYPE;
		if ((bit_pos + bit_size > size * 8)
                    || (bit_size > 0) == (bt != prevbt)
                    ) {
		    c = (c + align - 1) & -align;
		    offset = c;
		    bit_pos = 0;
		    /* In MS bitfield mode a bit-field run always uses
		       at least as many bits as the underlying type.
		       To start a new run it's also required that this
		       or the last bit-field had non-zero width.  */
		    if (bit_size || prev_bit_size)
		        c += size;
		}
		/* In MS layout the records alignment is normally
		   influenced by the field, except for a zero-width
		   field at the start of a run (but by further zero-width
		   fields it is again).  */
		if (bit_size == 0 && prevbt != bt)
		    align = 1;
		prevbt = bt;
                prev_bit_size = bit_size;
	    }

	    f->type.t = (f->type.t & ~(0x3f << VT_STRUCT_SHIFT))
		        | (bit_pos << VT_STRUCT_SHIFT);
	    bit_pos += bit_size;
	}
	if (align > maxalign)
	    maxalign = align;

#ifdef BF_DEBUG
	printf("set field %s offset %-2d size %-2d align %-2d",
	       get_tok_str(S, f->v & ~SYM_FIELD, NULL), offset, size, align);
	if (f->type.t & VT_BITFIELD) {
	    printf(" pos %-2d bits %-2d",
                    BIT_POS(f->type.t),
                    BIT_SIZE(f->type.t)
                    );
	}
	printf("\n");
#endif

        f->c = offset;
	f->r = 0;
    }

    if (pcc)
        c += (bit_pos + 7) >> 3;

    /* store size and alignment */
    a = bt = ad->a.aligned ? 1 << (ad->a.aligned - 1) : 1;
    if (a < maxalign)
        a = maxalign;
    type->ref->r = a;
    if (pragma_pack && pragma_pack < maxalign && 0 == pcc) {
        /* can happen if individual align for some member was given.  In
           this case MSVC ignores maxalign when aligning the size */
        a = pragma_pack;
        if (a < bt)
            a = bt;
    }
    c = (c + a - 1) & -a;
    type->ref->c = c;

#ifdef BF_DEBUG
    printf("struct size %-2d align %-2d\n\n", c, a), fflush(stdout);
#endif

    /* check whether we can access bitfields by their type */
    for (f = type->ref->next; f; f = f->next) {
        int s, px, cx, c0;
        CType t;

        if (0 == (f->type.t & VT_BITFIELD))
            continue;
        f->type.ref = f;
        f->auxtype = -1;
        bit_size = BIT_SIZE(f->type.t);
        if (bit_size == 0)
            continue;
        bit_pos = BIT_POS(f->type.t);
        size = type_size(&f->type, &align);

        if (bit_pos + bit_size <= size * 8 && f->c + size <= c
#ifdef TCC_TARGET_ARM
            && !(f->c & (align - 1))
#endif
            )
            continue;

        /* try to access the field using a different type */
        c0 = -1, s = align = 1;
        t.t = VT_BYTE;
        for (;;) {
            px = f->c * 8 + bit_pos;
            cx = (px >> 3) & -align;
            px = px - (cx << 3);
            if (c0 == cx)
                break;
            s = (px + bit_size + 7) >> 3;
            if (s > 4) {
                t.t = VT_LLONG;
            } else if (s > 2) {
                t.t = VT_INT;
            } else if (s > 1) {
                t.t = VT_SHORT;
            } else {
                t.t = VT_BYTE;
            }
            s = type_size(&t, &align);
            c0 = cx;
        }

        if (px + bit_size <= s * 8 && cx + s <= c
#ifdef TCC_TARGET_ARM
            && !(cx & (align - 1))
#endif
            ) {
            /* update offset and bit position */
            f->c = cx;
            bit_pos = px;
	    f->type.t = (f->type.t & ~(0x3f << VT_STRUCT_SHIFT))
		        | (bit_pos << VT_STRUCT_SHIFT);
            if (s != size)
                f->auxtype = t.t;
#ifdef BF_DEBUG
            printf("FIX field %s offset %-2d size %-2d align %-2d "
                "pos %-2d bits %-2d\n",
                get_tok_str(S, f->v & ~SYM_FIELD, NULL),
                cx, s, align, px, bit_size);
#endif
        } else {
            /* fall back to load/store single-byte wise */
            f->auxtype = VT_STRUCT;
#ifdef BF_DEBUG
            printf("FIX field %s : load byte-wise\n",
                 get_tok_str(S, f->v & ~SYM_FIELD, NULL));
#endif
        }
    }
}

/* enum/struct/union declaration. u is VT_ENUM/VT_STRUCT/VT_UNION */
static void struct_decl(TCCState* S, CType *type, int u)
{
    int v, c, size, align, flexible;
    int bit_size, bsize, bt;
    Sym *s, *ss, **ps;
    AttributeDef ad, ad1;
    CType type1, btype;

    memset(&ad, 0, sizeof ad);
    next(S);
    parse_attribute(S, &ad);
    if (S->tccpp_tok != '{') {
        v = S->tccpp_tok;
        next(S);
        /* struct already defined ? return it */
        if (v < TOK_IDENT)
            expect(S, "struct/union/enum name");
        s = struct_find(S, v);
        if (s && (s->sym_scope == S->tccgen_local_scope || S->tccpp_tok != '{')) {
            if (u == s->type.t)
                goto do_decl;
            if (u == VT_ENUM && IS_ENUM(s->type.t))
                goto do_decl;
            tcc_error(S, "redefinition of '%s'", get_tok_str(S, v, NULL));
        }
    } else {
        v = S->tccgen_anon_sym++;
    }
    /* Record the original enum/struct/union token.  */
    type1.t = u == VT_ENUM ? u | VT_INT | VT_UNSIGNED : u;
    type1.ref = NULL;
    /* we put an undefined size for struct/union */
    s = sym_push(S, v | SYM_STRUCT, &type1, 0, -1);
    s->r = 0; /* default alignment is zero as gcc */
do_decl:
    type->t = s->type.t;
    type->ref = s;

    if (S->tccpp_tok == '{') {
        next(S);
        if (s->c != -1)
            tcc_error(S, "struct/union/enum already defined");
        s->c = -2;
        /* cannot be empty */
        /* non empty enums are not allowed */
        ps = &s->next;
        if (u == VT_ENUM) {
            long long ll = 0, pl = 0, nl = 0;
	    CType t;
            t.ref = s;
            /* enum symbols have static storage */
            t.t = VT_INT|VT_STATIC|VT_ENUM_VAL;
            for(;;) {
                v = S->tccpp_tok;
                if (v < TOK_UIDENT)
                    expect(S, "identifier");
                ss = sym_find(S, v);
                if (ss && !S->tccgen_local_stack)
                    tcc_error(S, "redefinition of enumerator '%s'",
                              get_tok_str(S, v, NULL));
                next(S);
                if (S->tccpp_tok == '=') {
                    next(S);
		    ll = expr_const64(S);
                }
                ss = sym_push(S, v, &t, VT_CONST, 0);
                ss->enum_val = ll;
                *ps = ss, ps = &ss->next;
                if (ll < nl)
                    nl = ll;
                if (ll > pl)
                    pl = ll;
                if (S->tccpp_tok != ',')
                    break;
                next(S);
                ll++;
                /* NOTE: we accept a trailing comma */
                if (S->tccpp_tok == '}')
                    break;
            }
            skip(S, '}');
            /* set integral type of the enum */
            t.t = VT_INT;
            if (nl >= 0) {
                if (pl != (unsigned)pl)
                    t.t = (LONG_SIZE==8 ? VT_LLONG|VT_LONG : VT_LLONG);
                t.t |= VT_UNSIGNED;
            } else if (pl != (int)pl || nl != (int)nl)
                t.t = (LONG_SIZE==8 ? VT_LLONG|VT_LONG : VT_LLONG);
            s->type.t = type->t = t.t | VT_ENUM;
            s->c = 0;
            /* set type for enum members */
            for (ss = s->next; ss; ss = ss->next) {
                ll = ss->enum_val;
                if (ll == (int)ll) /* default is int if it fits */
                    continue;
                if (t.t & VT_UNSIGNED) {
                    ss->type.t |= VT_UNSIGNED;
                    if (ll == (unsigned)ll)
                        continue;
                }
                ss->type.t = (ss->type.t & ~VT_BTYPE)
                    | (LONG_SIZE==8 ? VT_LLONG|VT_LONG : VT_LLONG);
            }
        } else {
            c = 0;
            flexible = 0;
            while (S->tccpp_tok != '}') {
                if (!parse_btype(S, &btype, &ad1)) {
		    skip(S, ';');
		    continue;
		}
                while (1) {
		    if (flexible)
		        tcc_error(S, "flexible array member '%s' not at the end of struct",
                              get_tok_str(S, v, NULL));
                    bit_size = -1;
                    v = 0;
                    type1 = btype;
                    if (S->tccpp_tok != ':') {
			if (S->tccpp_tok != ';')
                            type_decl(S, &type1, &ad1, &v, TYPE_DIRECT);
                        if (v == 0) {
                    	    if ((type1.t & VT_BTYPE) != VT_STRUCT)
                        	expect(S, "identifier");
                    	    else {
				int v = btype.ref->v;
				if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
				    if (S->ms_extensions == 0)
                        		expect(S, "identifier");
				}
                    	    }
                        }
                        if (type_size(&type1, &align) < 0) {
			    if ((u == VT_STRUCT) && (type1.t & VT_ARRAY) && c)
			        flexible = 1;
			    else
			        tcc_error(S, "field '%s' has incomplete type",
                                      get_tok_str(S, v, NULL));
                        }
                        if ((type1.t & VT_BTYPE) == VT_FUNC ||
			    (type1.t & VT_BTYPE) == VT_VOID ||
                            (type1.t & VT_STORAGE))
                            tcc_error(S, "invalid type for '%s'", 
                                  get_tok_str(S, v, NULL));
                    }
                    if (S->tccpp_tok == ':') {
                        next(S);
                        bit_size = expr_const(S);
                        /* XXX: handle v = 0 case for messages */
                        if (bit_size < 0)
                            tcc_error(S, "negative width in bit-field '%s'", 
                                  get_tok_str(S, v, NULL));
                        if (v && bit_size == 0)
                            tcc_error(S, "zero width for bit-field '%s'", 
                                  get_tok_str(S, v, NULL));
			parse_attribute(S, &ad1);
                    }
                    size = type_size(&type1, &align);
                    if (bit_size >= 0) {
                        bt = type1.t & VT_BTYPE;
                        if (bt != VT_INT && 
                            bt != VT_BYTE && 
                            bt != VT_SHORT &&
                            bt != VT_BOOL &&
                            bt != VT_LLONG)
                            tcc_error(S, "bitfields must have scalar type");
                        bsize = size * 8;
                        if (bit_size > bsize) {
                            tcc_error(S, "width of '%s' exceeds its type",
                                  get_tok_str(S, v, NULL));
                        } else if (bit_size == bsize
                                    && !ad.a.packed && !ad1.a.packed) {
                            /* no need for bit fields */
                            ;
                        } else if (bit_size == 64) {
                            tcc_error(S, "field width 64 not implemented");
                        } else {
                            type1.t = (type1.t & ~VT_STRUCT_MASK)
                                | VT_BITFIELD
                                | (bit_size << (VT_STRUCT_SHIFT + 6));
                        }
                    }
                    if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
                        /* Remember we've seen a real field to check
			   for placement of flexible array member. */
			c = 1;
                    }
		    /* If member is a struct or bit-field, enforce
		       placing into the struct (as anonymous).  */
                    if (v == 0 &&
			((type1.t & VT_BTYPE) == VT_STRUCT ||
			 bit_size >= 0)) {
		        v = S->tccgen_anon_sym++;
		    }
                    if (v) {
                        ss = sym_push(S, v | SYM_FIELD, &type1, 0, 0);
                        ss->a = ad1.a;
                        *ps = ss;
                        ps = &ss->next;
                    }
                    if (S->tccpp_tok == ';' || S->tccpp_tok == TOK_EOF)
                        break;
                    skip(S, ',');
                }
                skip(S, ';');
            }
            skip(S, '}');
	    parse_attribute(S, &ad);
            if (ad.cleanup_func) {
                tcc_warning(S, "attribute '__cleanup__' ignored on type");
            }
	    check_fields(S, type, 1);
	    check_fields(S, type, 0);
            struct_layout(S, type, &ad);
        }
    }
}

static void sym_to_attr(AttributeDef *ad, Sym *s)
{
    merge_symattr(&ad->a, &s->a);
    merge_funcattr(&ad->f, &s->f);
}

/* Add type qualifiers to a type. If the type is an array then the qualifiers
   are added to the element type, copied because it could be a typedef. */
static void parse_btype_qualify(TCCState* S, CType *type, int qualifiers)
{
    while (type->t & VT_ARRAY) {
        type->ref = sym_push(S, SYM_FIELD, &type->ref->type, 0, type->ref->c);
        type = &type->ref->type;
    }
    type->t |= qualifiers;
}

/* return 0 if no type declaration. otherwise, return the basic type
   and skip it. 
 */
static int parse_btype(TCCState* S, CType *type, AttributeDef *ad)
{
    int t, u, bt, st, type_found, typespec_found, g, n;
    Sym *s;
    CType type1;

    memset(ad, 0, sizeof(AttributeDef));
    type_found = 0;
    typespec_found = 0;
    t = VT_INT;
    bt = st = -1;
    type->ref = NULL;

    while(1) {
        switch(S->tccpp_tok) {
        case TOK_EXTENSION:
            /* currently, we really ignore extension */
            next(S);
            continue;

            /* basic types */
        case TOK_CHAR:
            u = VT_BYTE;
        basic_type:
            next(S);
        basic_type1:
            if (u == VT_SHORT || u == VT_LONG) {
                if (st != -1 || (bt != -1 && bt != VT_INT))
                    tmbt: tcc_error(S, "too many basic types");
                st = u;
            } else {
                if (bt != -1 || (st != -1 && u != VT_INT))
                    goto tmbt;
                bt = u;
            }
            if (u != VT_INT)
                t = (t & ~(VT_BTYPE|VT_LONG)) | u;
            typespec_found = 1;
            break;
        case TOK_VOID:
            u = VT_VOID;
            goto basic_type;
        case TOK_SHORT:
            u = VT_SHORT;
            goto basic_type;
        case TOK_INT:
            u = VT_INT;
            goto basic_type;
        case TOK_ALIGNAS:
            { int n;
              AttributeDef ad1;
              next(S);
              skip(S, '(');
              memset(&ad1, 0, sizeof(AttributeDef));
              if (parse_btype(S, &type1, &ad1)) {
                  type_decl(S, &type1, &ad1, &n, TYPE_ABSTRACT);
                  if (ad1.a.aligned)
                    n = 1 << (ad1.a.aligned - 1);
                  else
                    type_size(&type1, &n);
              } else {
                  n = expr_const(S);
                  if (n <= 0 || (n & (n - 1)) != 0)
                    tcc_error(S, "alignment must be a positive power of two");
              }
              skip(S, ')');
              ad->a.aligned = exact_log2p1(n);
            }
            continue;
        case TOK_LONG:
            if ((t & VT_BTYPE) == VT_DOUBLE) {
                t = (t & ~(VT_BTYPE|VT_LONG)) | VT_LDOUBLE;
            } else if ((t & (VT_BTYPE|VT_LONG)) == VT_LONG) {
                t = (t & ~(VT_BTYPE|VT_LONG)) | VT_LLONG;
            } else {
                u = VT_LONG;
                goto basic_type;
            }
            next(S);
            break;
#ifdef TCC_TARGET_ARM64
        case TOK_UINT128:
            /* GCC's __uint128_t appears in some Linux header files. Make it a
               synonym for long double to get the size and alignment right. */
            u = VT_LDOUBLE;
            goto basic_type;
#endif
        case TOK_BOOL:
            u = VT_BOOL;
            goto basic_type;
        case TOK_FLOAT:
            u = VT_FLOAT;
            goto basic_type;
        case TOK_DOUBLE:
            if ((t & (VT_BTYPE|VT_LONG)) == VT_LONG) {
                t = (t & ~(VT_BTYPE|VT_LONG)) | VT_LDOUBLE;
            } else {
                u = VT_DOUBLE;
                goto basic_type;
            }
            next(S);
            break;
        case TOK_ENUM:
            struct_decl(S, &type1, VT_ENUM);
        basic_type2:
            u = type1.t;
            type->ref = type1.ref;
            goto basic_type1;
        case TOK_STRUCT:
            struct_decl(S, &type1, VT_STRUCT);
            goto basic_type2;
        case TOK_UNION:
            struct_decl(S, &type1, VT_UNION);
            goto basic_type2;

            /* type modifiers */
        case TOK__Atomic:
            next(S);
            type->t = t;
            parse_btype_qualify(S, type, VT_ATOMIC);
            t = type->t;
            if (S->tccpp_tok == '(') {
                parse_expr_type(S, &type1);
                /* remove all storage modifiers except typedef */
                type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
                if (type1.ref)
                    sym_to_attr(ad, type1.ref);
                goto basic_type2;
            }
            break;
        case TOK_CONST1:
        case TOK_CONST2:
        case TOK_CONST3:
            type->t = t;
            parse_btype_qualify(S, type, VT_CONSTANT);
            t = type->t;
            next(S);
            break;
        case TOK_VOLATILE1:
        case TOK_VOLATILE2:
        case TOK_VOLATILE3:
            type->t = t;
            parse_btype_qualify(S, type, VT_VOLATILE);
            t = type->t;
            next(S);
            break;
        case TOK_SIGNED1:
        case TOK_SIGNED2:
        case TOK_SIGNED3:
            if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == (VT_DEFSIGN|VT_UNSIGNED))
                tcc_error(S, "signed and unsigned modifier");
            t |= VT_DEFSIGN;
            next(S);
            typespec_found = 1;
            break;
        case TOK_REGISTER:
        case TOK_AUTO:
        case TOK_RESTRICT1:
        case TOK_RESTRICT2:
        case TOK_RESTRICT3:
            next(S);
            break;
        case TOK_UNSIGNED:
            if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == VT_DEFSIGN)
                tcc_error(S, "signed and unsigned modifier");
            t |= VT_DEFSIGN | VT_UNSIGNED;
            next(S);
            typespec_found = 1;
            break;

            /* storage */
        case TOK_EXTERN:
            g = VT_EXTERN;
            goto storage;
        case TOK_STATIC:
            g = VT_STATIC;
            goto storage;
        case TOK_TYPEDEF:
            g = VT_TYPEDEF;
            goto storage;
       storage:
            if (t & (VT_EXTERN|VT_STATIC|VT_TYPEDEF) & ~g)
                tcc_error(S, "multiple storage classes");
            t |= g;
            next(S);
            break;
        case TOK_INLINE1:
        case TOK_INLINE2:
        case TOK_INLINE3:
            t |= VT_INLINE;
            next(S);
            break;
        case TOK_NORETURN3:
            next(S);
            ad->f.func_noreturn = 1;
            break;
            /* GNUC attribute */
        case TOK_ATTRIBUTE1:
        case TOK_ATTRIBUTE2:
            parse_attribute(S, ad);
            if (ad->attr_mode) {
                u = ad->attr_mode -1;
                t = (t & ~(VT_BTYPE|VT_LONG)) | u;
            }
            continue;
            /* GNUC typeof */
        case TOK_TYPEOF1:
        case TOK_TYPEOF2:
        case TOK_TYPEOF3:
            next(S);
            parse_expr_type(S, &type1);
            /* remove all storage modifiers except typedef */
            type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
	    if (type1.ref)
                sym_to_attr(ad, type1.ref);
            goto basic_type2;
        default:
            if (typespec_found)
                goto the_end;
            s = sym_find(S, S->tccpp_tok);
            if (!s || !(s->type.t & VT_TYPEDEF))
                goto the_end;

            n = S->tccpp_tok, next(S);
            if (S->tccpp_tok == ':' && !S->tccgen_in_generic) {
                /* ignore if it's a label */
                unget_tok(S, n);
                goto the_end;
            }

            t &= ~(VT_BTYPE|VT_LONG);
            u = t & ~(VT_CONSTANT | VT_VOLATILE), t ^= u;
            type->t = (s->type.t & ~VT_TYPEDEF) | u;
            type->ref = s->type.ref;
            if (t)
                parse_btype_qualify(S, type, t);
            t = type->t;
            /* get attributes from typedef */
            sym_to_attr(ad, s);
            typespec_found = 1;
            st = bt = -2;
            break;
        }
        type_found = 1;
    }
the_end:
    if (S->char_is_unsigned) {
        if ((t & (VT_DEFSIGN|VT_BTYPE)) == VT_BYTE)
            t |= VT_UNSIGNED;
    }
    /* VT_LONG is used just as a modifier for VT_INT / VT_LLONG */
    bt = t & (VT_BTYPE|VT_LONG);
    if (bt == VT_LONG)
        t |= LONG_SIZE == 8 ? VT_LLONG : VT_INT;
#ifdef TCC_USING_DOUBLE_FOR_LDOUBLE
    if (bt == VT_LDOUBLE)
        t = (t & ~(VT_BTYPE|VT_LONG)) | (VT_DOUBLE|VT_LONG);
#endif
    type->t = t;
    return type_found;
}

/* convert a function parameter type (array to pointer and function to
   function pointer) */
static inline void convert_parameter_type(TCCState* S, CType *pt)
{
    /* remove const and volatile qualifiers (XXX: const could be used
       to indicate a const function parameter */
    pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
    /* array must be transformed to pointer according to ANSI C */
    pt->t &= ~VT_ARRAY;
    if ((pt->t & VT_BTYPE) == VT_FUNC) {
        mk_pointer(S, pt);
    }
}

ST_FUNC void parse_asm_str(TCCState* S, CString *astr)
{
    skip(S, '(');
    parse_mult_str(S, astr, "string constant");
}

/* Parse an asm label and return the token */
static int asm_label_instr(TCCState* S)
{
    int v;
    CString astr;

    next(S);
    parse_asm_str(S, &astr);
    skip(S, ')');
#ifdef ASM_DEBUG
    printf("asm_alias: \"%s\"\n", (char *)astr.data);
#endif
    v = tok_alloc(S, astr.data, astr.size - 1)->tok;
    cstr_free(S, &astr);
    return v;
}

static int post_type(TCCState* S, CType *type, AttributeDef *ad, int storage, int td)
{
    int n, l, t1, arg_size, align, unused_align;
    Sym **plast, *s, *first;
    AttributeDef ad1;
    CType pt;

    if (S->tccpp_tok == '(') {
        /* function type, or recursive declarator (return if so) */
        next(S);
	if (td && !(td & TYPE_ABSTRACT))
	  return 0;
	if (S->tccpp_tok == ')')
	  l = 0;
	else if (parse_btype(S, &pt, &ad1))
	  l = FUNC_NEW;
	else if (td) {
	    merge_attr (ad, &ad1);
	    return 0;
	} else
	  l = FUNC_OLD;
        first = NULL;
        plast = &first;
        arg_size = 0;
        if (l) {
            for(;;) {
                /* read param name and compute offset */
                if (l != FUNC_OLD) {
                    if ((pt.t & VT_BTYPE) == VT_VOID && S->tccpp_tok == ')')
                        break;
                    type_decl(S, &pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
                    if ((pt.t & VT_BTYPE) == VT_VOID)
                        tcc_error(S, "parameter declared as void");
                } else {
                    n = S->tccpp_tok;
                    if (n < TOK_UIDENT)
                        expect(S, "identifier");
                    pt.t = VT_VOID; /* invalid type */
                    pt.ref = NULL;
                    next(S);
                }
                convert_parameter_type(S, &pt);
                arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
                s = sym_push(S, n | SYM_FIELD, &pt, 0, 0);
                *plast = s;
                plast = &s->next;
                if (S->tccpp_tok == ')')
                    break;
                skip(S, ',');
                if (l == FUNC_NEW && S->tccpp_tok == TOK_DOTS) {
                    l = FUNC_ELLIPSIS;
                    next(S);
                    break;
                }
		if (l == FUNC_NEW && !parse_btype(S, &pt, &ad1))
		    tcc_error(S, "invalid type");
            }
        } else
            /* if no parameters, then old type prototype */
            l = FUNC_OLD;
        skip(S, ')');
        /* NOTE: const is ignored in returned type as it has a special
           meaning in gcc / C++ */
        type->t &= ~VT_CONSTANT; 
        /* some ancient pre-K&R C allows a function to return an array
           and the array brackets to be put after the arguments, such 
           that "int c()[]" means something like "int[] c()" */
        if (S->tccpp_tok == '[') {
            next(S);
            skip(S, ']'); /* only handle simple "[]" */
            mk_pointer(S, type);
        }
        /* we push a anonymous symbol which will contain the function prototype */
        ad->f.func_args = arg_size;
        ad->f.func_type = l;
        s = sym_push(S, SYM_FIELD, type, 0, 0);
        s->a = ad->a;
        s->f = ad->f;
        s->next = first;
        type->t = VT_FUNC;
        type->ref = s;
    } else if (S->tccpp_tok == '[') {
	int saved_nocode_wanted = S->tccgen_nocode_wanted;
        /* array definition */
        next(S);
	while (1) {
	    /* XXX The optional type-quals and static should only be accepted
	       in parameter decls.  The '*' as well, and then even only
	       in prototypes (not function defs).  */
	    switch (S->tccpp_tok) {
	    case TOK_RESTRICT1: case TOK_RESTRICT2: case TOK_RESTRICT3:
	    case TOK_CONST1:
	    case TOK_VOLATILE1:
	    case TOK_STATIC:
	    case '*':
		next(S);
		continue;
	    default:
		break;
	    }
	    break;
	}
        n = -1;
        t1 = 0;
        if (S->tccpp_tok != ']') {
            if (!S->tccgen_local_stack || (storage & VT_STATIC))
                vpushi(S, expr_const(S));
            else {
		/* VLAs (which can only happen with local_stack && !VT_STATIC)
		   length must always be evaluated, even under nocode_wanted,
		   so that its size slot is initialized (e.g. under sizeof
		   or typeof).  */
		S->tccgen_nocode_wanted = 0;
		gexpr(S);
	    }
            if ((S->tccgen_vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
                n = S->tccgen_vtop->c.i;
                if (n < 0)
                    tcc_error(S, "invalid array size");
            } else {
                if (!is_integer_btype(S->tccgen_vtop->type.t & VT_BTYPE))
                    tcc_error(S, "size of variable length array should be an integer");
                n = 0;
                t1 = VT_VLA;
            }
        }
        skip(S, ']');
        /* parse next post type */
        post_type(S, type, ad, storage, 0);

        if ((type->t & VT_BTYPE) == VT_FUNC)
            tcc_error(S, "declaration of an array of functions");
        if ((type->t & VT_BTYPE) == VT_VOID
            || type_size(type, &unused_align) < 0)
            tcc_error(S, "declaration of an array of incomplete type elements");

        t1 |= type->t & VT_VLA;

        if (t1 & VT_VLA) {
            if (n < 0)
              tcc_error(S, "need explicit inner array size in VLAs");
            S->tccgen_loc -= type_size(&S->tccgen_int_type, &align);
            S->tccgen_loc &= -align;
            n = S->tccgen_loc;

            vla_runtime_type_size(S, type, &align);
            gen_op(S, '*');
            vset(S, &S->tccgen_int_type, VT_LOCAL|VT_LVAL, n);
            vswap(S);
            vstore(S);
        }
        if (n != -1)
            vpop(S);
	S->tccgen_nocode_wanted = saved_nocode_wanted;
                
        /* we push an anonymous symbol which will contain the array
           element type */
        s = sym_push(S, SYM_FIELD, type, 0, n);
        type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
        type->ref = s;
    }
    return 1;
}

/* Parse a type declarator (except basic type), and return the type
   in 'type'. 'td' is a bitmask indicating which kind of type decl is
   expected. 'type' should contain the basic type. 'ad' is the
   attribute definition of the basic type. It can be modified by
   type_decl().  If this (possibly abstract) declarator is a pointer chain
   it returns the innermost pointed to type (equals *type, but is a different
   pointer), otherwise returns type itself, that's used for recursive calls.  */
static CType *type_decl(TCCState* S, CType *type, AttributeDef *ad, int *v, int td)
{
    CType *post, *ret;
    int qualifiers, storage;

    /* recursive type, remove storage bits first, apply them later again */
    storage = type->t & VT_STORAGE;
    type->t &= ~VT_STORAGE;
    post = ret = type;

    while (S->tccpp_tok == '*') {
        qualifiers = 0;
    redo:
        next(S);
        switch(S->tccpp_tok) {
        case TOK__Atomic:
            qualifiers |= VT_ATOMIC;
            goto redo;
        case TOK_CONST1:
        case TOK_CONST2:
        case TOK_CONST3:
            qualifiers |= VT_CONSTANT;
            goto redo;
        case TOK_VOLATILE1:
        case TOK_VOLATILE2:
        case TOK_VOLATILE3:
            qualifiers |= VT_VOLATILE;
            goto redo;
        case TOK_RESTRICT1:
        case TOK_RESTRICT2:
        case TOK_RESTRICT3:
            goto redo;
	/* XXX: clarify attribute handling */
	case TOK_ATTRIBUTE1:
	case TOK_ATTRIBUTE2:
	    parse_attribute(S, ad);
	    break;
        }
        mk_pointer(S, type);
        type->t |= qualifiers;
	if (ret == type)
	    /* innermost pointed to type is the one for the first derivation */
	    ret = pointed_type(type);
    }

    if (S->tccpp_tok == '(') {
	/* This is possibly a parameter type list for abstract declarators
	   ('int ()'), use post_type for testing this.  */
	if (!post_type(S, type, ad, 0, td)) {
	    /* It's not, so it's a nested declarator, and the post operations
	       apply to the innermost pointed to type (if any).  */
	    /* XXX: this is not correct to modify 'ad' at this point, but
	       the syntax is not clear */
	    parse_attribute(S, ad);
	    post = type_decl(S, type, ad, v, td);
	    skip(S, ')');
	} else
	  goto abstract;
    } else if (S->tccpp_tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
	/* type identifier */
	*v = S->tccpp_tok;
	next(S);
    } else {
  abstract:
	if (!(td & TYPE_ABSTRACT))
	  expect(S, "identifier");
	*v = 0;
    }
    post_type(S, post, ad, storage, 0);
    parse_attribute(S, ad);
    type->t |= storage;
    return ret;
}

/* indirection with full error checking and bound check */
ST_FUNC void indir(TCCState* S)
{
    if ((S->tccgen_vtop->type.t & VT_BTYPE) != VT_PTR) {
        if ((S->tccgen_vtop->type.t & VT_BTYPE) == VT_FUNC)
            return;
        expect(S, "pointer");
    }
    if (S->tccgen_vtop->r & VT_LVAL)
        gv(S, RC_INT);
    S->tccgen_vtop->type = *pointed_type(&S->tccgen_vtop->type);
    /* Arrays and functions are never lvalues */
    if (!(S->tccgen_vtop->type.t & (VT_ARRAY | VT_VLA))
        && (S->tccgen_vtop->type.t & VT_BTYPE) != VT_FUNC) {
        S->tccgen_vtop->r |= VT_LVAL;
        /* if bound checking, the referenced pointer must be checked */
#ifdef CONFIG_TCC_BCHECK
        if (S->do_bounds_check)
            S->tccgen_vtop->r |= VT_MUSTBOUND;
#endif
    }
}

/* pass a parameter to a function and do type checking and casting */
static void gfunc_param_typed(TCCState* S, Sym *func, Sym *arg)
{
    int func_type;
    CType type;

    func_type = func->f.func_type;
    if (func_type == FUNC_OLD ||
        (func_type == FUNC_ELLIPSIS && arg == NULL)) {
        /* default casting : only need to convert float to double */
        if ((S->tccgen_vtop->type.t & VT_BTYPE) == VT_FLOAT) {
            gen_cast_s(S, VT_DOUBLE);
        } else if (S->tccgen_vtop->type.t & VT_BITFIELD) {
            type.t = S->tccgen_vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
	    type.ref = S->tccgen_vtop->type.ref;
            gen_cast(S, &type);
        } else if (S->tccgen_vtop->r & VT_MUSTCAST) {
            force_charshort_cast(S);
        }
    } else if (arg == NULL) {
        tcc_error(S, "too many arguments to function");
    } else {
        type = arg->type;
        type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
        gen_assign_cast(S, &type);
    }
}

/* parse an expression and return its type without any side effect. */
static void expr_type(TCCState* S, CType *type, void (*expr_fn)(TCCState* S))
{
    S->tccgen_nocode_wanted++;
    expr_fn(S);
    *type = S->tccgen_vtop->type;
    vpop(S);
    S->tccgen_nocode_wanted--;
}

/* parse an expression of the form '(type)' or '(expr)' and return its
   type */
static void parse_expr_type(TCCState* S, CType *type)
{
    int n;
    AttributeDef ad;

    skip(S, '(');
    if (parse_btype(S, type, &ad)) {
        type_decl(S, type, &ad, &n, TYPE_ABSTRACT);
    } else {
        expr_type(S, type, gexpr);
    }
    skip(S, ')');
}

static void parse_type(TCCState* S, CType *type)
{
    AttributeDef ad;
    int n;

    if (!parse_btype(S, type, &ad)) {
        expect(S, "type");
    }
    type_decl(S, type, &ad, &n, TYPE_ABSTRACT);
}

static void parse_builtin_params(TCCState* S, int nc, const char *args)
{
    char c, sep = '(';
    CType type;
    if (nc)
        S->tccgen_nocode_wanted++;
    next(S);
    if (*args == 0)
	skip(S, sep);
    while ((c = *args++)) {
	skip(S, sep);
	sep = ',';
        if (c == 't') {
            parse_type(S, &type);
	    vpush(S, &type);
	    continue;
        }
        expr_eq(S);
        type.ref = NULL;
        type.t = 0;
	switch (c) {
	    case 'e':
		continue;
	    case 'V':
                type.t = VT_CONSTANT;
	    case 'v':
                type.t |= VT_VOID;
                mk_pointer (S, &type);
                break;
	    case 'S':
                type.t = VT_CONSTANT;
	    case 's':
                type.t |= S->tccgen_char_type.t;
                mk_pointer (S, &type);
                break;
	    case 'i':
                type.t = VT_INT;
                break;
	    case 'l':
                type.t = VT_SIZE_T;
                break;
	    default:
                break;
	}
        gen_assign_cast(S, &type);
    }
    skip(S, ')');
    if (nc)
        S->tccgen_nocode_wanted--;
}

static void parse_atomic(TCCState* S, int atok)
{
    int size, align, arg;
    CType *atom, *atom_ptr, ct = {0};
    char buf[40];
    static const char *const templates[] = {
        /*
         * Each entry consists of callback and function template.
         * The template represents argument types and return type.
         *
         * ? void (return-only)
         * b bool
         * a atomic
         * A read-only atomic
         * p pointer to memory
         * v value
         * m memory model
         */

        /* keep in order of appearance in tcctok.h: */
        /* __atomic_store */            "avm.?",
        /* __atomic_load */             "Am.v",
        /* __atomic_exchange */         "avm.v",
        /* __atomic_compare_exchange */ "apvbmm.b",
        /* __atomic_fetch_add */        "avm.v",
        /* __atomic_fetch_sub */        "avm.v",
        /* __atomic_fetch_or */         "avm.v",
        /* __atomic_fetch_xor */        "avm.v",
        /* __atomic_fetch_and */        "avm.v"
    };
    const char *template = templates[(atok - TOK___atomic_store)];

    atom = atom_ptr = NULL;
    size = 0; /* pacify compiler */
    next(S);
    skip(S, '(');
    for (arg = 0;;) {
        expr_eq(S);
        switch (template[arg]) {
        case 'a':
        case 'A':
            atom_ptr = &S->tccgen_vtop->type;
            if ((atom_ptr->t & VT_BTYPE) != VT_PTR)
                expect(S, "pointer");
            atom = pointed_type(atom_ptr);
            size = type_size(atom, &align);
            if (size > 8
                || (size & (size - 1))
                || (atok > TOK___atomic_compare_exchange
                    && (0 == btype_size(atom->t & VT_BTYPE)
                        || (atom->t & VT_BTYPE) == VT_PTR)))
                expect(S, "integral or integer-sized pointer target type");
            /* GCC does not care either: */
            /* if (!(atom->t & VT_ATOMIC))
                tcc_warning(S, "pointer target declaration is missing '_Atomic'"); */
            break;

        case 'p':
            if ((S->tccgen_vtop->type.t & VT_BTYPE) != VT_PTR
             || type_size(pointed_type(&S->tccgen_vtop->type), &align) != size)
                tcc_error(S, "pointer target type mismatch in argument %d", arg + 1);
            gen_assign_cast(S, atom_ptr);
            break;
        case 'v':
            gen_assign_cast(S, atom);
            break;
        case 'm':
            gen_assign_cast(S, &S->tccgen_int_type);
            break;
        case 'b':
            ct.t = VT_BOOL;
            gen_assign_cast(S, &ct);
            break;
        }
        if ('.' == template[++arg])
            break;
        skip(S, ',');
    }
    skip(S, ')');

    ct.t = VT_VOID;
    switch (template[arg + 1]) {
    case 'b':
        ct.t = VT_BOOL;
        break;
    case 'v':
        ct = *atom;
        break;
    }

    sprintf(buf, "%s_%d", get_tok_str(S, atok, 0), size);
    vpush_helper_func(S, tok_alloc_const(S, buf));
    vrott(S, arg + 1);
    gfunc_call(S, arg);

    vpush(S, &ct);
    PUT_R_RET(S->tccgen_vtop, ct.t);
    if (ct.t == VT_BOOL) {
#ifdef PROMOTE_RET
	S->tccgen_vtop->r |= BFVAL(VT_MUSTCAST, 1);
#else
	S->tccgen_vtop->type.t = VT_INT;
#endif
    }
}

ST_FUNC void unary(TCCState* S)
{
    int n, t, align, size, r, sizeof_caller;
    CType type;
    Sym *s;
    AttributeDef ad;

    /* generate line number info */
    if (S->tccgen_debug_modes)
        tcc_debug_line(S), tcc_tcov_check_line (S, 1);

    sizeof_caller = S->tccgen_in_sizeof;
    S->tccgen_in_sizeof = 0;
    type.ref = NULL;
    /* XXX: GCC 2.95.3 does not generate a table although it should be
       better here */
 tok_next:
    switch(S->tccpp_tok) {
    case TOK_EXTENSION:
        next(S);
        goto tok_next;
    case TOK_LCHAR:
#ifdef TCC_TARGET_PE
        t = VT_SHORT|VT_UNSIGNED;
        goto push_tokc;
#endif
    case TOK_CINT:
    case TOK_CCHAR: 
	t = VT_INT;
 push_tokc:
	type.t = t;
	vsetc(S, &type, VT_CONST, &S->tccpp_tokc);
        next(S);
        break;
    case TOK_CUINT:
        t = VT_INT | VT_UNSIGNED;
        goto push_tokc;
    case TOK_CLLONG:
        t = VT_LLONG;
	goto push_tokc;
    case TOK_CULLONG:
        t = VT_LLONG | VT_UNSIGNED;
	goto push_tokc;
    case TOK_CFLOAT:
        t = VT_FLOAT;
	goto push_tokc;
    case TOK_CDOUBLE:
        t = VT_DOUBLE;
	goto push_tokc;
    case TOK_CLDOUBLE:
        t = VT_LDOUBLE;
	goto push_tokc;
    case TOK_CLONG:
        t = (LONG_SIZE == 8 ? VT_LLONG : VT_INT) | VT_LONG;
	goto push_tokc;
    case TOK_CULONG:
        t = (LONG_SIZE == 8 ? VT_LLONG : VT_INT) | VT_LONG | VT_UNSIGNED;
	goto push_tokc;
    case TOK___FUNCTION__:
        if (!gnu_ext)
            goto tok_identifier;
        /* fall thru */
    case TOK___FUNC__:
        {
            Section *sec;
            int len;
            /* special function name identifier */
            len = strlen(S->tccgen_funcname) + 1;
            /* generate char[len] type */
            type.t = S->tccgen_char_type.t;
            if (S->warn_write_strings & WARN_ON)
                type.t |= VT_CONSTANT;
            mk_pointer(S, &type);
            type.t |= VT_ARRAY;
            type.ref->c = len;
            sec = rodata_section;
            vpush_ref(S, &type, sec, sec->data_offset, len);
            if (!NODATA_WANTED)
                memcpy(section_ptr_add(S, sec, len), S->tccgen_funcname, len);
            next(S);
        }
        break;
    case TOK_LSTR:
#ifdef TCC_TARGET_PE
        t = VT_SHORT | VT_UNSIGNED;
#else
        t = VT_INT;
#endif
        goto str_init;
    case TOK_STR:
        /* string parsing */
        t = S->tccgen_char_type.t;
    str_init:
        if (S->warn_write_strings & WARN_ON)
            t |= VT_CONSTANT;
        type.t = t;
        mk_pointer(S, &type);
        type.t |= VT_ARRAY;
        memset(&ad, 0, sizeof(AttributeDef));
        ad.section = rodata_section;
        decl_initializer_alloc(S, &type, &ad, VT_CONST, 2, 0, 0);
        break;
    case '(':
        next(S);
        /* cast ? */
        if (parse_btype(S, &type, &ad)) {
            type_decl(S, &type, &ad, &n, TYPE_ABSTRACT);
            skip(S, ')');
            /* check ISOC99 compound literal */
            if (S->tccpp_tok == '{') {
                    /* data is allocated locally by default */
                if (S->tccgen_global_expr)
                    r = VT_CONST;
                else
                    r = VT_LOCAL;
                /* all except arrays are lvalues */
                if (!(type.t & VT_ARRAY))
                    r |= VT_LVAL;
                memset(&ad, 0, sizeof(AttributeDef));
                decl_initializer_alloc(S, &type, &ad, r, 1, 0, 0);
            } else {
                if (sizeof_caller) {
                    vpush(S, &type);
                    return;
                }
                unary(S);
                gen_cast(S, &type);
            }
        } else if (S->tccpp_tok == '{') {
	    int saved_nocode_wanted = S->tccgen_nocode_wanted;
            if (S->tccgen_const_wanted && !(S->tccgen_nocode_wanted & unevalmask))
                expect(S, "constant");
            if (0 == S->tccgen_local_scope)
                tcc_error(S, "statement expression outside of function");
            /* save all registers */
            save_regs(S, 0);
            /* statement expression : we do not accept break/continue
               inside as GCC does.  We do retain the nocode_wanted state,
	       as statement expressions can't ever be entered from the
	       outside, so any reactivation of code emission (from labels
	       or loop heads) can be disabled again after the end of it. */
            block(S, 1);
	    S->tccgen_nocode_wanted = saved_nocode_wanted;
            skip(S, ')');
        } else {
            gexpr(S);
            skip(S, ')');
        }
        break;
    case '*':
        next(S);
        unary(S);
        indir(S);
        break;
    case '&':
        next(S);
        unary(S);
        /* functions names must be treated as function pointers,
           except for unary '&' and sizeof. Since we consider that
           functions are not lvalues, we only have to handle it
           there and in function calls. */
        /* arrays can also be used although they are not lvalues */
        if ((S->tccgen_vtop->type.t & VT_BTYPE) != VT_FUNC &&
            !(S->tccgen_vtop->type.t & VT_ARRAY))
            test_lvalue(S);
        if (S->tccgen_vtop->sym)
          S->tccgen_vtop->sym->a.addrtaken = 1;
        mk_pointer(S, &S->tccgen_vtop->type);
        gaddrof(S);
        break;
    case '!':
        next(S);
        unary(S);
        gen_test_zero(S, TOK_EQ);
        break;
    case '~':
        next(S);
        unary(S);
        vpushi(S, -1);
        gen_op(S, '^');
        break;
    case '+':
        next(S);
        unary(S);
        if ((S->tccgen_vtop->type.t & VT_BTYPE) == VT_PTR)
            tcc_error(S, "pointer not accepted for unary plus");
        /* In order to force cast, we add zero, except for floating point
	   where we really need an noop (otherwise -0.0 will be transformed
	   into +0.0).  */
	if (!is_float(S->tccgen_vtop->type.t)) {
	    vpushi(S, 0);
	    gen_op(S, '+');
	}
        break;
    case TOK_SIZEOF:
    case TOK_ALIGNOF1:
    case TOK_ALIGNOF2:
    case TOK_ALIGNOF3:
        t = S->tccpp_tok;
        next(S);
        S->tccgen_in_sizeof++;
        expr_type(S, &type, unary); /* Perform a in_sizeof = 0; */
        s = NULL;
        if (S->tccgen_vtop[1].r & VT_SYM)
            s = S->tccgen_vtop[1].sym; /* hack: accessing previous vtop */
        size = type_size(&type, &align);
        if (s && s->a.aligned)
            align = 1 << (s->a.aligned - 1);
        if (t == TOK_SIZEOF) {
            if (!(type.t & VT_VLA)) {
                if (size < 0)
                    tcc_error(S, "sizeof applied to an incomplete type");
                vpushs(S, size);
            } else {
                vla_runtime_type_size(S, &type, &align);
            }
        } else {
            vpushs(S, align);
        }
        S->tccgen_vtop->type.t |= VT_UNSIGNED;
        break;

    case TOK_builtin_expect:
	/* __builtin_expect is a no-op for now */
	parse_builtin_params(S, 0, "ee");
	vpop(S);
        break;
    case TOK_builtin_types_compatible_p:
	parse_builtin_params(S, 0, "tt");
	S->tccgen_vtop[-1].type.t &= ~(VT_CONSTANT | VT_VOLATILE);
	S->tccgen_vtop[0].type.t &= ~(VT_CONSTANT | VT_VOLATILE);
	n = is_compatible_types(&S->tccgen_vtop[-1].type, &S->tccgen_vtop[0].type);
	S->tccgen_vtop -= 2;
	vpushi(S, n);
        break;
    case TOK_builtin_choose_expr:
	{
	    int64_t c;
	    next(S);
	    skip(S, '(');
	    c = expr_const64(S);
	    skip(S, ',');
	    if (!c) {
		S->tccgen_nocode_wanted++;
	    }
	    expr_eq(S);
	    if (!c) {
		vpop(S);
		S->tccgen_nocode_wanted--;
	    }
	    skip(S, ',');
	    if (c) {
		S->tccgen_nocode_wanted++;
	    }
	    expr_eq(S);
	    if (c) {
		vpop(S);
		S->tccgen_nocode_wanted--;
	    }
	    skip(S, ')');
	}
        break;
    case TOK_builtin_constant_p:
	parse_builtin_params(S, 1, "e");
	n = (S->tccgen_vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST &&
            !((S->tccgen_vtop->r & VT_SYM) && S->tccgen_vtop->sym->a.addrtaken);
	S->tccgen_vtop--;
	vpushi(S, n);
        break;
    case TOK_builtin_frame_address:
    case TOK_builtin_return_address:
        {
            int tok1 = S->tccpp_tok;
            int level;
            next(S);
            skip(S, '(');
            if (S->tccpp_tok != TOK_CINT) {
                tcc_error(S, "%s only takes positive integers",
                          tok1 == TOK_builtin_return_address ?
                          "__builtin_return_address" :
                          "__builtin_frame_address");
            }
            level = (uint32_t)S->tccpp_tokc.i;
            next(S);
            skip(S, ')');
            type.t = VT_VOID;
            mk_pointer(S, &type);
            vset(S, &type, VT_LOCAL, 0);       /* local frame */
            while (level--) {
#ifdef TCC_TARGET_RISCV64
                vpushi(S, 2*PTR_SIZE);
                gen_op(S, '-');
#endif
                mk_pointer(S, &S->tccgen_vtop->type);
                indir(S);                    /* -> parent frame */
            }
            if (tok1 == TOK_builtin_return_address) {
                // assume return address is just above frame pointer on stack
#ifdef TCC_TARGET_ARM
                vpushi(S, 2*PTR_SIZE);
                gen_op(S, '+');
#elif defined TCC_TARGET_RISCV64
                vpushi(S, PTR_SIZE);
                gen_op(S, '-');
#else
                vpushi(S, PTR_SIZE);
                gen_op(S, '+');
#endif
                mk_pointer(S, &S->tccgen_vtop->type);
                indir(S);
            }
        }
        break;
#ifdef TCC_TARGET_RISCV64
    case TOK_builtin_va_start:
        parse_builtin_params(S, 0, "ee");
        r = S->tccgen_vtop->r & VT_VALMASK;
        if (r == VT_LLOCAL)
            r = VT_LOCAL;
        if (r != VT_LOCAL)
            tcc_error(S, "__builtin_va_start expects a local variable");
        gen_va_start(S);
	vstore(S);
        break;
#endif
#ifdef TCC_TARGET_X86_64
#ifdef TCC_TARGET_PE
    case TOK_builtin_va_start:
	parse_builtin_params(S, 0, "ee");
        r = S->tccgen_vtop->r & VT_VALMASK;
        if (r == VT_LLOCAL)
            r = VT_LOCAL;
        if (r != VT_LOCAL)
            tcc_error(S, "__builtin_va_start expects a local variable");
        S->tccgen_vtop->r = r;
	S->tccgen_vtop->type = S->tccgen_char_pointer_type;
	S->tccgen_vtop->c.i += 8;
	vstore(S);
        break;
#else
    case TOK_builtin_va_arg_types:
	parse_builtin_params(S, 0, "t");
	vpushi(S, classify_x86_64_va_arg(&S->tccgen_vtop->type));
	vswap(S);
	vpop(S);
	break;
#endif
#endif

#ifdef TCC_TARGET_ARM64
    case TOK_builtin_va_start: {
	parse_builtin_params(S, 0, "ee");
        //xx check types
        gen_va_start(S);
        vpushi(S, 0);
        S->tccgen_vtop->type.t = VT_VOID;
        break;
    }
    case TOK_builtin_va_arg: {
	parse_builtin_params(S, 0, "et");
	type = S->tccgen_vtop->type;
	vpop(S);
        //xx check types
        gen_va_arg(S, &type);
        S->tccgen_vtop->type = type;
        break;
    }
    case TOK___arm64_clear_cache: {
	parse_builtin_params(S, 0, "ee");
        gen_clear_cache(S);
        vpushi(S, 0);
        S->tccgen_vtop->type.t = VT_VOID;
        break;
    }
#endif

    /* atomic operations */
    case TOK___atomic_store:
    case TOK___atomic_load:
    case TOK___atomic_exchange:
    case TOK___atomic_compare_exchange:
    case TOK___atomic_fetch_add:
    case TOK___atomic_fetch_sub:
    case TOK___atomic_fetch_or:
    case TOK___atomic_fetch_xor:
    case TOK___atomic_fetch_and:
        parse_atomic(S, S->tccpp_tok);
        break;

    /* pre operations */
    case TOK_INC:
    case TOK_DEC:
        t = S->tccpp_tok;
        next(S);
        unary(S);
        inc(S, 0, t);
        break;
    case '-':
        next(S);
        unary(S);
	if (is_float(S->tccgen_vtop->type.t)) {
            gen_opif(S, TOK_NEG);
	} else {
            vpushi(S, 0);
            vswap(S);
            gen_op(S, '-');
        }
        break;
    case TOK_LAND:
        if (!gnu_ext)
            goto tok_identifier;
        next(S);
        /* allow to take the address of a label */
        if (S->tccpp_tok < TOK_UIDENT)
            expect(S, "label identifier");
        s = label_find(S, S->tccpp_tok);
        if (!s) {
            s = label_push(S, &S->tccgen_global_label_stack, S->tccpp_tok, LABEL_FORWARD);
        } else {
            if (s->r == LABEL_DECLARED)
                s->r = LABEL_FORWARD;
        }
        if (!s->type.t) {
            s->type.t = VT_VOID;
            mk_pointer(S, &s->type);
            s->type.t |= VT_STATIC;
        }
        vpushsym(S, &s->type, s);
        next(S);
        break;

    case TOK_GENERIC:
    {
	CType controlling_type;
	int has_default = 0;
	int has_match = 0;
	int learn = 0;
	TokenString *str = NULL;
	int saved_const_wanted = S->tccgen_const_wanted;

	next(S);
	skip(S, '(');
	S->tccgen_const_wanted = 0;
	expr_type(S, &controlling_type, expr_eq);
	controlling_type.t &= ~(VT_CONSTANT | VT_VOLATILE | VT_ARRAY);
	if ((controlling_type.t & VT_BTYPE) == VT_FUNC)
	  mk_pointer(S, &controlling_type);
	S->tccgen_const_wanted = saved_const_wanted;
	for (;;) {
	    learn = 0;
	    skip(S, ',');
	    if (S->tccpp_tok == TOK_DEFAULT) {
		if (has_default)
		    tcc_error(S, "too many 'default'");
		has_default = 1;
		if (!has_match)
		    learn = 1;
		next(S);
	    } else {
	        AttributeDef ad_tmp;
		int itmp;
	        CType cur_type;

                S->tccgen_in_generic++;
		parse_btype(S, &cur_type, &ad_tmp);
                S->tccgen_in_generic--;

		type_decl(S, &cur_type, &ad_tmp, &itmp, TYPE_ABSTRACT);
		if (compare_types(&controlling_type, &cur_type, 0)) {
		    if (has_match) {
		      tcc_error(S, "type match twice");
		    }
		    has_match = 1;
		    learn = 1;
		}
	    }
	    skip(S, ':');
	    if (learn) {
		if (str)
		    tok_str_free(S, str);
		skip_or_save_block(S, &str);
	    } else {
		skip_or_save_block(S, NULL);
	    }
	    if (S->tccpp_tok == ')')
		break;
	}
	if (!str) {
	    char buf[60];
	    type_to_str(S, buf, sizeof buf, &controlling_type, NULL);
	    tcc_error(S, "type '%s' does not match any association", buf);
	}
	begin_macro(S, str, 1);
	next(S);
	expr_eq(S);
	if (S->tccpp_tok != TOK_EOF)
	    expect(S, ",");
	end_macro(S);
        next(S);
	break;
    }
    // special qnan , snan and infinity values
    case TOK___NAN__:
        n = 0x7fc00000;
special_math_val:
	vpushi(S, n);
	S->tccgen_vtop->type.t = VT_FLOAT;
        next(S);
        break;
    case TOK___SNAN__:
	n = 0x7f800001;
	goto special_math_val;
    case TOK___INF__:
	n = 0x7f800000;
	goto special_math_val;

    default:
    tok_identifier:
        t = S->tccpp_tok;
        next(S);
        if (t < TOK_UIDENT)
            expect(S, "identifier");
        s = sym_find(S, t);
        if (!s || IS_ASM_SYM(s)) {
            const char *name = get_tok_str(S, t, NULL);
            if (S->tccpp_tok != '(')
                tcc_error(S, "'%s' undeclared", name);
            /* for simple function calls, we tolerate undeclared
               external reference to int() function */
            tcc_warning_c(warn_implicit_function_declaration)(S,
                "implicit declaration of function '%s'", name);
            s = external_global_sym(S, t, &S->tccgen_func_old_type);
        }

        r = s->r;
        /* A symbol that has a register is a local register variable,
           which starts out as VT_LOCAL value.  */
        if ((r & VT_VALMASK) < VT_CONST)
            r = (r & ~VT_VALMASK) | VT_LOCAL;

        vset(S, &s->type, r, s->c);
        /* Point to s as backpointer (even without r&VT_SYM).
	   Will be used by at least the x86 inline asm parser for
	   regvars.  */
	S->tccgen_vtop->sym = s;

        if (r & VT_SYM) {
            S->tccgen_vtop->c.i = 0;
        } else if (r == VT_CONST && IS_ENUM_VAL(s->type.t)) {
            S->tccgen_vtop->c.i = s->enum_val;
        }
        break;
    }
    
    /* post operations */
    while (1) {
        if (S->tccpp_tok == TOK_INC || S->tccpp_tok == TOK_DEC) {
            inc(S, 1, S->tccpp_tok);
            next(S);
        } else if (S->tccpp_tok == '.' || S->tccpp_tok == TOK_ARROW || S->tccpp_tok == TOK_CDOUBLE) {
            int qualifiers, cumofs = 0;
            /* field */ 
            if (S->tccpp_tok == TOK_ARROW) 
                indir(S);
            qualifiers = S->tccgen_vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
            test_lvalue(S);
            gaddrof(S);
            /* expect pointer on structure */
            if ((S->tccgen_vtop->type.t & VT_BTYPE) != VT_STRUCT)
                expect(S, "struct or union");
            if (S->tccpp_tok == TOK_CDOUBLE)
                expect(S, "field name");
            next(S);
            if (S->tccpp_tok == TOK_CINT || S->tccpp_tok == TOK_CUINT)
                expect(S, "field name");
	    s = find_field(&S->tccgen_vtop->type, S->tccpp_tok, &cumofs);
            if (!s)
                tcc_error(S, "field not found: %s",  get_tok_str(S, S->tccpp_tok & ~SYM_FIELD, &S->tccpp_tokc));
            /* add field offset to pointer */
            S->tccgen_vtop->type = S->tccgen_char_pointer_type; /* change type to 'char *' */
            vpushi(S, cumofs + s->c);
            gen_op(S, '+');
            /* change type to field type, and set to lvalue */
            S->tccgen_vtop->type = s->type;
            S->tccgen_vtop->type.t |= qualifiers;
            /* an array is never an lvalue */
            if (!(S->tccgen_vtop->type.t & VT_ARRAY)) {
                S->tccgen_vtop->r |= VT_LVAL;
#ifdef CONFIG_TCC_BCHECK
                /* if bound checking, the referenced pointer must be checked */
                if (S->do_bounds_check)
                    S->tccgen_vtop->r |= VT_MUSTBOUND;
#endif
            }
            next(S);
        } else if (S->tccpp_tok == '[') {
            next(S);
            gexpr(S);
            gen_op(S, '+');
            indir(S);
            skip(S, ']');
        } else if (S->tccpp_tok == '(') {
            SValue ret;
            Sym *sa;
            int nb_args, ret_nregs, ret_align, regsize, variadic;

            /* function call  */
            if ((S->tccgen_vtop->type.t & VT_BTYPE) != VT_FUNC) {
                /* pointer test (no array accepted) */
                if ((S->tccgen_vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
                    S->tccgen_vtop->type = *pointed_type(&S->tccgen_vtop->type);
                    if ((S->tccgen_vtop->type.t & VT_BTYPE) != VT_FUNC)
                        goto error_func;
                } else {
                error_func:
                    expect(S, "function pointer");
                }
            } else {
                S->tccgen_vtop->r &= ~VT_LVAL; /* no lvalue */
            }
            /* get return type */
            s = S->tccgen_vtop->type.ref;
            next(S);
            sa = s->next; /* first parameter */
            nb_args = regsize = 0;
            ret.r2 = VT_CONST;
            /* compute first implicit argument if a structure is returned */
            if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
                variadic = (s->f.func_type == FUNC_ELLIPSIS);
                ret_nregs = gfunc_sret(&s->type, variadic, &ret.type,
                                       &ret_align, &regsize);
                if (ret_nregs <= 0) {
                    /* get some space for the returned structure */
                    size = type_size(&s->type, &align);
#ifdef TCC_TARGET_ARM64
                /* On arm64, a small struct is return in registers.
                   It is much easier to write it to memory if we know
                   that we are allowed to write some extra bytes, so
                   round the allocated space up to a power of 2: */
                if (size < 16)
                    while (size & (size - 1))
                        size = (size | (size - 1)) + 1;
#endif
                    S->tccgen_loc = (S->tccgen_loc - size) & -align;
                    ret.type = s->type;
                    ret.r = VT_LOCAL | VT_LVAL;
                    /* pass it as 'int' to avoid structure arg passing
                       problems */
                    vseti(S, VT_LOCAL, S->tccgen_loc);
#ifdef CONFIG_TCC_BCHECK
                    if (S->do_bounds_check)
                        --S->tccgen_loc;
#endif
                    ret.c = S->tccgen_vtop->c;
                    if (ret_nregs < 0)
                      S->tccgen_vtop--;
                    else
                      nb_args++;
                }
            } else {
                ret_nregs = 1;
                ret.type = s->type;
            }

            if (ret_nregs > 0) {
                /* return in register */
                ret.c.i = 0;
                PUT_R_RET(&ret, ret.type.t);
            }
            if (S->tccpp_tok != ')') {
                for(;;) {
                    expr_eq(S);
                    gfunc_param_typed(S, s, sa);
                    nb_args++;
                    if (sa)
                        sa = sa->next;
                    if (S->tccpp_tok == ')')
                        break;
                    skip(S, ',');
                }
            }
            if (sa)
                tcc_error(S, "too few arguments to function");
            skip(S, ')');
            gfunc_call(S, nb_args);

            if (ret_nregs < 0) {
                vsetc(S, &ret.type, ret.r, &ret.c);
#ifdef TCC_TARGET_RISCV64
                arch_transfer_ret_regs(S, 1);
#endif
            } else {
                /* return value */
                for (r = ret.r + ret_nregs + !ret_nregs; r-- > ret.r;) {
                    vsetc(S, &ret.type, r, &ret.c);
                    S->tccgen_vtop->r2 = ret.r2; /* Loop only happens when r2 is VT_CONST */
                }

                /* handle packed struct return */
                if (((s->type.t & VT_BTYPE) == VT_STRUCT) && ret_nregs) {
                    int addr, offset;

                    size = type_size(&s->type, &align);
                    /* We're writing whole regs often, make sure there's enough
                       space.  Assume register size is power of 2.  */
                    if (regsize > align)
                      align = regsize;
                    S->tccgen_loc = (S->tccgen_loc - size) & -align;
                    addr = S->tccgen_loc;
                    offset = 0;
                    for (;;) {
                        vset(S, &ret.type, VT_LOCAL | VT_LVAL, addr + offset);
                        vswap(S);
                        vstore(S);
                        S->tccgen_vtop--;
                        if (--ret_nregs == 0)
                          break;
                        offset += regsize;
                    }
                    vset(S, &s->type, VT_LOCAL | VT_LVAL, addr);
                }

                /* Promote char/short return values. This is matters only
                   for calling function that were not compiled by TCC and
                   only on some architectures.  For those where it doesn't
                   matter we expect things to be already promoted to int,
                   but not larger.  */
                t = s->type.t & VT_BTYPE;
                if (t == VT_BYTE || t == VT_SHORT || t == VT_BOOL) {
#ifdef PROMOTE_RET
                    S->tccgen_vtop->r |= BFVAL(VT_MUSTCAST, 1);
#else
                    S->tccgen_vtop->type.t = VT_INT;
#endif
                }
            }
            if (s->f.func_noreturn) {
                if (S->tccgen_debug_modes)
	            tcc_tcov_block_end (S, S->tccgen_tcov_data.line);
                CODE_OFF();
	    }
        } else {
            break;
        }
    }
}

#ifndef precedence_parser /* original top-down parser */

static void expr_prod(void)
{
    int t;

    unary(S);
    while ((t = Stccpp_tok) == '*' || t == '/' || t == '%') {
        next(S);
        unary(S);
        gen_op(S, t);
    }
}

static void expr_sum(void)
{
    int t;

    expr_prod();
    while ((t = Stccpp_tok) == '+' || t == '-') {
        next(S);
        expr_prod();
        gen_op(S, t);
    }
}

static void expr_shift(void)
{
    int t;

    expr_sum();
    while ((t = Stccpp_tok) == TOK_SHL || t == TOK_SAR) {
        next(S);
        expr_sum();
        gen_op(S, t);
    }
}

static void expr_cmp(void)
{
    int t;

    expr_shift();
    while (((t = Stccpp_tok) >= TOK_ULE && t <= TOK_GT) ||
           t == TOK_ULT || t == TOK_UGE) {
        next(S);
        expr_shift();
        gen_op(S, t);
    }
}

static void expr_cmpeq(void)
{
    int t;

    expr_cmp();
    while ((t = Stccpp_tok) == TOK_EQ || t == TOK_NE) {
        next(S);
        expr_cmp();
        gen_op(S, t);
    }
}

static void expr_and(void)
{
    expr_cmpeq();
    while (Stccpp_tok == '&') {
        next(S);
        expr_cmpeq();
        gen_op(S, '&');
    }
}

static void expr_xor(void)
{
    expr_and();
    while (Stccpp_tok == '^') {
        next(S);
        expr_and();
        gen_op(S, '^');
    }
}

static void expr_or(void)
{
    expr_xor();
    while (Stccpp_tok == '|') {
        next(S);
        expr_xor();
        gen_op(S, '|');
    }
}

static void expr_landor(S, int op);

static void expr_land(void)
{
    expr_or();
    if (Stccpp_tok == TOK_LAND)
        expr_landor(S, Stccpp_tok);
}

static void expr_lor(void)
{
    expr_land();
    if (Stccpp_tok == TOK_LOR)
        expr_landor(S, Stccpp_tok);
}

# define expr_landor_next(op) op == TOK_LAND ? expr_or() : expr_land()
#else /* defined precedence_parser */
# define expr_landor_next(op) unary(S), expr_infix(S, precedence(op) + 1)
# define expr_lor() unary(S), expr_infix(S, 1)

static int precedence(int tok)
{
    switch (tok) {
        case TOK_LOR: return 1;
        case TOK_LAND: return 2;
	case '|': return 3;
	case '^': return 4;
	case '&': return 5;
	case TOK_EQ: case TOK_NE: return 6;
 relat: case TOK_ULT: case TOK_UGE: return 7;
	case TOK_SHL: case TOK_SAR: return 8;
	case '+': case '-': return 9;
	case '*': case '/': case '%': return 10;
	default:
	    if (tok >= TOK_ULE && tok <= TOK_GT)
	        goto relat;
	    return 0;
    }
}
static void init_prec(TCCState* S)
{
    int i;
    for (i = 0; i < 256; i++)
	S->tccgen_prec[i] = precedence(i);
}
#define precedence(i) ((unsigned)i < 256 ? S->tccgen_prec[i] : 0)

static void expr_landor(TCCState* S, int op);

static void expr_infix(TCCState* S, int p)
{
    int t = S->tccpp_tok, p2;
    while ((p2 = precedence(t)) >= p) {
        if (t == TOK_LOR || t == TOK_LAND) {
            expr_landor(S, t);
        } else {
            next(S);
            unary(S);
            if (precedence(S->tccpp_tok) > p2)
              expr_infix(S, p2 + 1);
            gen_op(S, t);
        }
        t = S->tccpp_tok;
    }
}
#endif

/* Assuming vtop is a value used in a conditional context
   (i.e. compared with zero) return 0 if it's false, 1 if
   true and -1 if it can't be statically determined.  */
static int condition_3way(TCCState* S)
{
    int c = -1;
    if ((S->tccgen_vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST &&
	(!(S->tccgen_vtop->r & VT_SYM) || !S->tccgen_vtop->sym->a.weak)) {
	vdup(S);
        gen_cast_s(S, VT_BOOL);
	c = S->tccgen_vtop->c.i;
	vpop(S);
    }
    return c;
}

static void expr_landor(TCCState* S, int op)
{
    int t = 0, cc = 1, f = 0, i = op == TOK_LAND, c;
    for(;;) {
        c = f ? i : condition_3way(S);
        if (c < 0)
            save_regs(S, 1), cc = 0;
        else if (c != i)
            S->tccgen_nocode_wanted++, f = 1;
        if (S->tccpp_tok != op)
            break;
        if (c < 0)
            t = gvtst(S, i, t);
        else
            vpop(S);
        next(S);
        expr_landor_next(op);
    }
    if (cc || f) {
        vpop(S);
        vpushi(S, i ^ f);
        gsym(S, t);
        S->tccgen_nocode_wanted -= f;
    } else {
        gvtst_set(S, i, t);
    }
}

static int is_cond_bool(SValue *sv)
{
    if ((sv->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST
        && (sv->type.t & VT_BTYPE) == VT_INT)
        return (unsigned)sv->c.i < 2;
    if (sv->r == VT_CMP)
        return 1;
    return 0;
}

static void expr_cond(TCCState* S)
{
    int tt, u, r1, r2, rc, t1, t2, islv, c, g;
    SValue sv;
    CType type;
    int ncw_prev;

    expr_lor();
    if (S->tccpp_tok == '?') {
        next(S);
	c = condition_3way(S);
        g = (S->tccpp_tok == ':' && gnu_ext);
        tt = 0;
        if (!g) {
            if (c < 0) {
                save_regs(S, 1);
                tt = gvtst(S, 1, 0);
            } else {
                vpop(S);
            }
        } else if (c < 0) {
            /* needed to avoid having different registers saved in
               each branch */
            save_regs(S, 1);
            gv_dup(S);
            tt = gvtst(S, 0, 0);
        }

        ncw_prev = S->tccgen_nocode_wanted;
        if (c == 0)
          S->tccgen_nocode_wanted++;
        if (!g)
          gexpr(S);

        if ((S->tccgen_vtop->type.t & VT_BTYPE) == VT_FUNC)
          mk_pointer(S, &S->tccgen_vtop->type);
        sv = *S->tccgen_vtop; /* save value to handle it later */
        S->tccgen_vtop--; /* no vpop so that FP stack is not flushed */

        if (g) {
            u = tt;
        } else if (c < 0) {
            u = gjmp(S, 0);
            gsym(S, tt);
        } else
          u = 0;

        S->tccgen_nocode_wanted = ncw_prev;
        if (c == 1)
          S->tccgen_nocode_wanted++;
        skip(S, ':');
        expr_cond(S);

        if (c < 0 && is_cond_bool(S->tccgen_vtop) && is_cond_bool(&sv)) {
            /* optimize "if (f ? a > b : c || d) ..." for example, where normally
               "a < b" and "c || d" would be forced to "(int)0/1" first, whereas
               this code jumps directly to the if's then/else branches. */
            t1 = gvtst(S, 0, 0);
            t2 = gjmp(S, 0);
            gsym(S, u);
            vpushv(S, &sv);
            /* combine jump targets of 2nd op with VT_CMP of 1st op */
            gvtst_set(S, 0, t1);
            gvtst_set(S, 1, t2);
            S->tccgen_nocode_wanted = ncw_prev;
            //  tcc_warning(S, "two conditions expr_cond");
            return;
        }

        if ((S->tccgen_vtop->type.t & VT_BTYPE) == VT_FUNC)
          mk_pointer(S, &S->tccgen_vtop->type);

        /* cast operands to correct type according to ISOC rules */
        if (!combine_types(S, &type, &sv, S->tccgen_vtop, '?'))
          type_incompatibility_error(S, &sv.type, &S->tccgen_vtop->type,
            "type mismatch in conditional expression (have '%s' and '%s')");
        /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
           that `(expr ? a : b).mem` does not error  with "lvalue expected" */
        islv = (S->tccgen_vtop->r & VT_LVAL) && (sv.r & VT_LVAL) && VT_STRUCT == (type.t & VT_BTYPE);

        /* now we convert second operand */
        if (c != 1) {
            gen_cast(S, &type);
            if (islv) {
                mk_pointer(S, &S->tccgen_vtop->type);
                gaddrof(S);
            } else if (VT_STRUCT == (S->tccgen_vtop->type.t & VT_BTYPE))
              gaddrof(S);
        }

        rc = RC_TYPE(type.t);
        /* for long longs, we use fixed registers to avoid having
           to handle a complicated move */
        if (USING_TWO_WORDS(type.t))
          rc = RC_RET(type.t);

        tt = r2 = 0;
        if (c < 0) {
            r2 = gv(S, rc);
            tt = gjmp(S, 0);
        }
        gsym(S, u);
        S->tccgen_nocode_wanted = ncw_prev;

        /* this is horrible, but we must also convert first
           operand */
        if (c != 0) {
            *S->tccgen_vtop = sv;
            gen_cast(S, &type);
            if (islv) {
                mk_pointer(S, &S->tccgen_vtop->type);
                gaddrof(S);
            } else if (VT_STRUCT == (S->tccgen_vtop->type.t & VT_BTYPE))
              gaddrof(S);
        }

        if (c < 0) {
            r1 = gv(S, rc);
            move_reg(S, r2, r1, islv ? VT_PTR : type.t);
            S->tccgen_vtop->r = r2;
            gsym(S, tt);
        }

        if (islv)
          indir(S);
    }
}

static void expr_eq(TCCState* S)
{
    int t;
    
    expr_cond(S);
    if ((t = S->tccpp_tok) == '=' || TOK_ASSIGN(t)) {
        test_lvalue(S);
        next(S);
        if (t == '=') {
            expr_eq(S);
        } else {
            vdup(S);
            expr_eq(S);
            gen_op(S, TOK_ASSIGN_OP(t));
        }
        vstore(S);
    }
}

ST_FUNC void gexpr(TCCState* S)
{
    while (1) {
        expr_eq(S);
        if (S->tccpp_tok != ',')
            break;
        vpop(S);
        next(S);
    }
}

/* parse a constant expression and return value in vtop.  */
static void expr_const1(TCCState* S)
{
    S->tccgen_const_wanted++;
    S->tccgen_nocode_wanted += unevalmask + 1;
    expr_cond(S);
    S->tccgen_nocode_wanted -= unevalmask + 1;
    S->tccgen_const_wanted--;
}

/* parse an integer constant and return its value. */
static inline int64_t expr_const64(TCCState* S)
{
    int64_t c;
    expr_const1(S);
    if ((S->tccgen_vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
        expect(S, "constant expression");
    c = S->tccgen_vtop->c.i;
    vpop(S);
    return c;
}

/* parse an integer constant and return its value.
   Complain if it doesn't fit 32bit (signed or unsigned).  */
ST_FUNC int expr_const(TCCState* S)
{
    int c;
    int64_t wc = expr_const64(S);
    c = wc;
    if (c != wc && (unsigned)c != wc)
        tcc_error(S, "constant exceeds 32 bit");
    return c;
}

/* ------------------------------------------------------------------------- */
/* return from function */

#ifndef TCC_TARGET_ARM64
static void gfunc_return(TCCState* S, CType *func_type)
{
    if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
        CType type, ret_type;
        int ret_align, ret_nregs, regsize;
        ret_nregs = gfunc_sret(func_type, S->tccgen_func_var, &ret_type,
                               &ret_align, &regsize);
        if (ret_nregs < 0) {
#ifdef TCC_TARGET_RISCV64
            arch_transfer_ret_regs(S, 0);
#endif
        } else if (0 == ret_nregs) {
            /* if returning structure, must copy it to implicit
               first pointer arg location */
            type = *func_type;
            mk_pointer(S, &type);
            vset(S, &type, VT_LOCAL | VT_LVAL, S->tccgen_func_vc);
            indir(S);
            vswap(S);
            /* copy structure value to pointer */
            vstore(S);
        } else {
            /* returning structure packed into registers */
            int size, addr, align, rc;
            size = type_size(func_type,&align);
            if ((S->tccgen_vtop->r != (VT_LOCAL | VT_LVAL) ||
                 (S->tccgen_vtop->c.i & (ret_align-1)))
                && (align & (ret_align-1))) {
                S->tccgen_loc = (S->tccgen_loc - size) & -ret_align;
                addr = S->tccgen_loc;
                type = *func_type;
                vset(S, &type, VT_LOCAL | VT_LVAL, addr);
                vswap(S);
                vstore(S);
                vpop(S);
                vset(S, &ret_type, VT_LOCAL | VT_LVAL, addr);
            }
            S->tccgen_vtop->type = ret_type;
            rc = RC_RET(ret_type.t);
            if (ret_nregs == 1)
                gv(S, rc);
            else {
                for (;;) {
                    vdup(S);
                    gv(S, rc);
                    vpop(S);
                    if (--ret_nregs == 0)
                      break;
                    /* We assume that when a structure is returned in multiple
                       registers, their classes are consecutive values of the
                       suite s(n) = 2^n */
                    rc <<= 1;
                    S->tccgen_vtop->c.i += regsize;
                }
            }
        }
    } else {
        gv(S, RC_RET(func_type->t));
    }
    S->tccgen_vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
}
#endif

static void check_func_return(TCCState* S)
{
    if ((S->tccgen_func_vt.t & VT_BTYPE) == VT_VOID)
        return;
    if (!strcmp (S->tccgen_funcname, "main")
        && (S->tccgen_func_vt.t & VT_BTYPE) == VT_INT) {
        /* main returns 0 by default */
        vpushi(S, 0);
        gen_assign_cast(S, &S->tccgen_func_vt);
        gfunc_return(S, &S->tccgen_func_vt);
    } else {
        tcc_warning(S, "function might return no value: '%s'", S->tccgen_funcname);
    }
}

/* ------------------------------------------------------------------------- */
/* switch/case */

static int case_cmpi(const void *pa, const void *pb)
{
    int64_t a = (*(struct case_t**) pa)->v1;
    int64_t b = (*(struct case_t**) pb)->v1;
    return a < b ? -1 : a > b;
}

static int case_cmpu(const void *pa, const void *pb)
{
    uint64_t a = (uint64_t)(*(struct case_t**) pa)->v1;
    uint64_t b = (uint64_t)(*(struct case_t**) pb)->v1;
    return a < b ? -1 : a > b;
}

static void gtst_addr(TCCState* S, int t, int a)
{
    gsym_addr(S, gvtst(S, 0, t), a);
}

static void gcase(TCCState* S, struct case_t **base, int len, int *bsym)
{
    struct case_t *p;
    int e;
    int ll = (S->tccgen_vtop->type.t & VT_BTYPE) == VT_LLONG;
    while (len > 8) {
        /* binary search */
        p = base[len/2];
        vdup(S);
	if (ll)
	    vpushll(S, p->v2);
	else
	    vpushi(S, p->v2);
        gen_op(S, TOK_LE);
        e = gvtst(S, 1, 0);
        vdup(S);
	if (ll)
	    vpushll(S, p->v1);
	else
	    vpushi(S, p->v1);
        gen_op(S, TOK_GE);
        gtst_addr(S, 0, p->sym); /* v1 <= x <= v2 */
        /* x < v1 */
        gcase(S, base, len/2, bsym);
        /* x > v2 */
        gsym(S, e);
        e = len/2 + 1;
        base += e; len -= e;
    }
    /* linear scan */
    while (len--) {
        p = *base++;
        vdup(S);
	if (ll)
	    vpushll(S, p->v2);
	else
	    vpushi(S, p->v2);
        if (p->v1 == p->v2) {
            gen_op(S, TOK_EQ);
            gtst_addr(S, 0, p->sym);
        } else {
            gen_op(S, TOK_LE);
            e = gvtst(S, 1, 0);
            vdup(S);
	    if (ll)
	        vpushll(S, p->v1);
	    else
	        vpushi(S, p->v1);
            gen_op(S, TOK_GE);
            gtst_addr(S, 0, p->sym);
            gsym(S, e);
        }
    }
    *bsym = gjmp(S, *bsym);
}

/* ------------------------------------------------------------------------- */
/* __attribute__((cleanup(fn))) */

static void try_call_scope_cleanup(TCCState* S, Sym *stop)
{
    Sym *cls = S->tccgen_cur_scope->cl.s;

    for (; cls != stop; cls = cls->ncl) {
	Sym *fs = cls->next;
	Sym *vs = cls->prev_tok;

	vpushsym(S, &fs->type, fs);
	vset(S, &vs->type, vs->r, vs->c);
	S->tccgen_vtop->sym = vs;
        mk_pointer(S, &S->tccgen_vtop->type);
	gaddrof(S);
	gfunc_call(S, 1);
    }
}

static void try_call_cleanup_goto(TCCState* S, Sym *cleanupstate)
{
    Sym *oc, *cc;
    int ocd, ccd;

    if (!S->tccgen_cur_scope->cl.s)
	return;

    /* search NCA of both cleanup chains given parents and initial depth */
    ocd = cleanupstate ? cleanupstate->v & ~SYM_FIELD : 0;
    for (ccd = S->tccgen_cur_scope->cl.n, oc = cleanupstate; ocd > ccd; --ocd, oc = oc->ncl)
      ;
    for (cc = S->tccgen_cur_scope->cl.s; ccd > ocd; --ccd, cc = cc->ncl)
      ;
    for (; cc != oc; cc = cc->ncl, oc = oc->ncl, --ccd)
      ;

    try_call_scope_cleanup(S, cc);
}

/* call 'func' for each __attribute__((cleanup(func))) */
static void block_cleanup(TCCState* S, scope_t *o)
{
    int jmp = 0;
    Sym *g, **pg;
    for (pg = &S->tccgen_pending_gotos; (g = *pg) && g->c > o->cl.n;) {
        if (g->prev_tok->r & LABEL_FORWARD) {
            Sym *pcl = g->next;
            if (!jmp)
                jmp = gjmp(S, 0);
            gsym(S, pcl->jnext);
            try_call_scope_cleanup(S, o->cl.s);
            pcl->jnext = gjmp(S, 0);
            if (!o->cl.n)
                goto remove_pending;
            g->c = o->cl.n;
            pg = &g->prev;
        } else {
    remove_pending:
            *pg = g->prev;
            sym_free(S, g);
        }
    }
    gsym(S, jmp);
    try_call_scope_cleanup(S, o->cl.s);
}

/* ------------------------------------------------------------------------- */
/* VLA */

static void vla_restore(TCCState * S, int loc)
{
    if (loc)
        gen_vla_sp_restore(S, loc);
}

static void vla_leave(TCCState * S, scope_t *o)
{
    scope_t *c = S->tccgen_cur_scope, *v = NULL;
    for (; c != o && c; c = c->prev)
      if (c->vla.num)
        v = c;
    if (v)
      vla_restore(S, v->vla.locorig);
}

/* ------------------------------------------------------------------------- */
/* local scopes */

static void new_scope(TCCState* S, scope_t *o)
{
    /* copy and link previous scope */
    *o = *S->tccgen_cur_scope;
    o->prev = S->tccgen_cur_scope;
    S->tccgen_cur_scope = o;
    S->tccgen_cur_scope->vla.num = 0;

    /* record local declaration stack position */
    o->lstk = S->tccgen_local_stack;
    o->llstk = S->tccgen_local_label_stack;
    ++S->tccgen_local_scope;

    if (S->tccgen_debug_modes)
        tcc_debug_stabn(S, N_LBRAC, S->tccgen_ind - S->tccgen_func_ind);
}

static void prev_scope(TCCState* S, scope_t *o, int is_expr)
{
    vla_leave(S, o->prev);

    if (o->cl.s != o->prev->cl.s)
        block_cleanup(S, o->prev);

    /* pop locally defined labels */
    label_pop(S, &S->tccgen_local_label_stack, o->llstk, is_expr);

    /* In the is_expr case (a statement expression is finished here),
       vtop might refer to symbols on the local_stack.  Either via the
       type or via vtop->sym.  We can't pop those nor any that in turn
       might be referred to.  To make it easier we don't roll back
       any symbols in that case; some upper level call to block() will
       do that.  We do have to remove such symbols from the lookup
       tables, though.  sym_pop will do that.  */

    /* pop locally defined symbols */
    pop_local_syms(S, o->lstk, is_expr);
    S->tccgen_cur_scope = o->prev;
    --S->tccgen_local_scope;

    if (S->tccgen_debug_modes)
        tcc_debug_stabn(S, N_RBRAC, S->tccgen_ind - S->tccgen_func_ind);
}

/* leave a scope via break/continue(/goto) */
static void leave_scope(TCCState* S, scope_t *o)
{
    if (!o)
        return;
    try_call_scope_cleanup(S, o->cl.s);
    vla_leave(S, o);
}

/* ------------------------------------------------------------------------- */
/* call block from 'for do while' loops */

static void lblock(TCCState* S, int *bsym, int *csym)
{
    scope_t *lo = S->tccgen_loop_scope, *co = S->tccgen_cur_scope;
    int *b = co->bsym, *c = co->csym;
    if (csym) {
        co->csym = csym;
        S->tccgen_loop_scope = co;
    }
    co->bsym = bsym;
    block(S, 0);
    co->bsym = b;
    if (csym) {
        co->csym = c;
        S->tccgen_loop_scope = lo;
    }
}

static void block(TCCState* S, int is_expr)
{
    int a, b, c, d, e, t;
    scope_t o;
    Sym *s;

    if (is_expr) {
        /* default return value is (void) */
        vpushi(S, 0);
        S->tccgen_vtop->type.t = VT_VOID;
    }

again:
    t = S->tccpp_tok;
    /* If the token carries a value, next() might destroy it. Only with
       invalid code such as f(){"123"4;} */
    if (TOK_HAS_VALUE(t))
        goto expr;
    next(S);

    if (S->tccgen_debug_modes)
        tcc_tcov_check_line (S, 0), tcc_tcov_block_begin (S);

    if (t == TOK_IF) {
        skip(S, '(');
        gexpr(S);
        skip(S, ')');
        a = gvtst(S, 1, 0);
        block(S, 0);
        if (S->tccpp_tok == TOK_ELSE) {
            d = gjmp(S, 0);
            gsym(S, a);
            next(S);
            block(S, 0);
            gsym(S, d); /* patch else jmp */
        } else {
            gsym(S, a);
        }

    } else if (t == TOK_WHILE) {
        d = gind(S);
        skip(S, '(');
        gexpr(S);
        skip(S, ')');
        a = gvtst(S, 1, 0);
        b = 0;
        lblock(S, &a, &b);
        gjmp_addr(S, d);
        gsym_addr(S, b, d);
        gsym(S, a);

    } else if (t == '{') {
        new_scope(S, &o);

        /* handle local labels declarations */
        while (S->tccpp_tok == TOK_LABEL) {
            do {
                next(S);
                if (S->tccpp_tok < TOK_UIDENT)
                    expect(S, "label identifier");
                label_push(S, &S->tccgen_local_label_stack, S->tccpp_tok, LABEL_DECLARED);
                next(S);
            } while (S->tccpp_tok == ',');
            skip(S, ';');
        }

        while (S->tccpp_tok != '}') {
	    decl(S, VT_LOCAL);
            if (S->tccpp_tok != '}') {
                if (is_expr)
                    vpop(S);
                block(S, is_expr);
            }
        }

        prev_scope(S, &o, is_expr);
        if (S->tccgen_local_scope)
            next(S);
        else if (!S->tccgen_nocode_wanted)
            check_func_return(S);

    } else if (t == TOK_RETURN) {
        b = (S->tccgen_func_vt.t & VT_BTYPE) != VT_VOID;
        if (S->tccpp_tok != ';') {
            gexpr(S);
            if (b) {
                gen_assign_cast(S, &S->tccgen_func_vt);
            } else {
                if (S->tccgen_vtop->type.t != VT_VOID)
                    tcc_warning(S, "void function returns a value");
                S->tccgen_vtop--;
            }
        } else if (b) {
            tcc_warning(S, "'return' with no value");
            b = 0;
        }
        leave_scope(S, S->tccgen_root_scope);
        if (b)
            gfunc_return(S, &S->tccgen_func_vt);
        skip(S, ';');
        /* jump unless last stmt in top-level block */
        if (S->tccpp_tok != '}' || S->tccgen_local_scope != 1)
            S->tccgen_rsym = gjmp(S, S->tccgen_rsym);
        if (S->tccgen_debug_modes)
	    tcc_tcov_block_end (S, S->tccgen_tcov_data.line);
        CODE_OFF();

    } else if (t == TOK_BREAK) {
        /* compute jump */
        if (!S->tccgen_cur_scope->bsym)
            tcc_error(S, "cannot break");
        if (S->tccgen_cur_switch && S->tccgen_cur_scope->bsym == S->tccgen_cur_switch->bsym)
            leave_scope(S, S->tccgen_cur_switch->scope);
        else
            leave_scope(S, S->tccgen_loop_scope);
        *S->tccgen_cur_scope->bsym = gjmp(S, *S->tccgen_cur_scope->bsym);
        skip(S, ';');

    } else if (t == TOK_CONTINUE) {
        /* compute jump */
        if (!S->tccgen_cur_scope->csym)
            tcc_error(S, "cannot continue");
        leave_scope(S, S->tccgen_loop_scope);
        *S->tccgen_cur_scope->csym = gjmp(S, *S->tccgen_cur_scope->csym);
        skip(S, ';');

    } else if (t == TOK_FOR) {
        new_scope(S, &o);

        skip(S, '(');
        if (S->tccpp_tok != ';') {
            /* c99 for-loop init decl? */
            if (!decl0(S, VT_LOCAL, 1, NULL)) {
                /* no, regular for-loop init expr */
                gexpr(S);
                vpop(S);
            }
        }
        skip(S, ';');
        a = b = 0;
        c = d = gind(S);
        if (S->tccpp_tok != ';') {
            gexpr(S);
            a = gvtst(S, 1, 0);
        }
        skip(S, ';');
        if (S->tccpp_tok != ')') {
            e = gjmp(S, 0);
            d = gind(S);
            gexpr(S);
            vpop(S);
            gjmp_addr(S, c);
            gsym(S, e);
        }
        skip(S, ')');
        lblock(S, &a, &b);
        gjmp_addr(S, d);
        gsym_addr(S, b, d);
        gsym(S, a);
        prev_scope(S, &o, 0);

    } else if (t == TOK_DO) {
        a = b = 0;
        d = gind(S);
        lblock(S, &a, &b);
        gsym(S, b);
        skip(S, TOK_WHILE);
        skip(S, '(');
	gexpr(S);
        skip(S, ')');
        skip(S, ';');
	c = gvtst(S, 0, 0);
	gsym_addr(S, c, d);
        gsym(S, a);

    } else if (t == TOK_SWITCH) {
        struct switch_t *sw;

        sw = tcc_mallocz(S, sizeof *sw);
        sw->bsym = &a;
        sw->scope = S->tccgen_cur_scope;
        sw->prev = S->tccgen_cur_switch;
        S->tccgen_cur_switch = sw;

        skip(S, '(');
        gexpr(S);
        skip(S, ')');
        sw->sv = *S->tccgen_vtop--; /* save switch value */

        a = 0;
        b = gjmp(S, 0); /* jump to first case */
        lblock(S, &a, NULL);
        a = gjmp(S, a); /* add implicit break */
        /* case lookup */
        gsym(S, b);

        if (sw->sv.type.t & VT_UNSIGNED)
            qsort(sw->p, sw->n, sizeof(void*), case_cmpu);
        else
            qsort(sw->p, sw->n, sizeof(void*), case_cmpi);

        for (b = 1; b < sw->n; b++)
            if (sw->sv.type.t & VT_UNSIGNED
                ? (uint64_t)sw->p[b - 1]->v2 >= (uint64_t)sw->p[b]->v1
                : sw->p[b - 1]->v2 >= sw->p[b]->v1)
                tcc_error(S, "duplicate case value");

        vpushv(S, &sw->sv);
        gv(S, RC_INT);
        d = 0, gcase(S, sw->p, sw->n, &d);
        vpop(S);
        if (sw->def_sym)
            gsym_addr(S, d, sw->def_sym);
        else
            gsym(S, d);
        /* break label */
        gsym(S, a);

        dynarray_reset(S, &sw->p, &sw->n);
        S->tccgen_cur_switch = sw->prev;
        tcc_free(S, sw);

    } else if (t == TOK_CASE) {
        struct case_t *cr = tcc_malloc(S, sizeof(struct case_t));
        if (!S->tccgen_cur_switch)
            expect(S, "switch");
        cr->v1 = cr->v2 = expr_const64(S);
        if (gnu_ext && S->tccpp_tok == TOK_DOTS) {
            next(S);
            cr->v2 = expr_const64(S);
            if ((!(S->tccgen_cur_switch->sv.type.t & VT_UNSIGNED) && cr->v2 < cr->v1)
                || (S->tccgen_cur_switch->sv.type.t & VT_UNSIGNED && (uint64_t)cr->v2 < (uint64_t)cr->v1))
                tcc_warning(S, "empty case range");
        }
        S->tccgen_tcov_data.ind = 0;
        cr->sym = gind(S);
        dynarray_add(S, &S->tccgen_cur_switch->p, &S->tccgen_cur_switch->n, cr);
        skip(S, ':');
        is_expr = 0;
        goto block_after_label;

    } else if (t == TOK_DEFAULT) {
        if (!S->tccgen_cur_switch)
            expect(S, "switch");
        if (S->tccgen_cur_switch->def_sym)
            tcc_error(S, "too many 'default'");
        S->tccgen_tcov_data.ind = 0;
        S->tccgen_cur_switch->def_sym = gind(S);
        skip(S, ':');
        is_expr = 0;
        goto block_after_label;

    } else if (t == TOK_GOTO) {
        if (S->tccgen_cur_scope->vla.num)
          vla_restore(S, S->tccgen_cur_scope->vla.locorig);
        if (S->tccpp_tok == '*' && gnu_ext) {
            /* computed goto */
            next(S);
            gexpr(S);
            if ((S->tccgen_vtop->type.t & VT_BTYPE) != VT_PTR)
                expect(S, "pointer");
            ggoto(S);

        } else if (S->tccpp_tok >= TOK_UIDENT) {
	    s = label_find(S, S->tccpp_tok);
	    /* put forward definition if needed */
            if (!s)
              s = label_push(S, &S->tccgen_global_label_stack, S->tccpp_tok, LABEL_FORWARD);
            else if (s->r == LABEL_DECLARED)
              s->r = LABEL_FORWARD;

	    if (s->r & LABEL_FORWARD) {
		/* start new goto chain for cleanups, linked via label->next */
		if (S->tccgen_cur_scope->cl.s && !S->tccgen_nocode_wanted) {
                    sym_push2(S, &S->tccgen_pending_gotos, SYM_FIELD, 0, S->tccgen_cur_scope->cl.n);
                    S->tccgen_pending_gotos->prev_tok = s;
                    s = sym_push2(S, &s->next, SYM_FIELD, 0, 0);
                    S->tccgen_pending_gotos->next = s;
                }
		s->jnext = gjmp(S, s->jnext);
	    } else {
		try_call_cleanup_goto(S, s->cleanupstate);
		gjmp_addr(S, s->jnext);
	    }
	    next(S);

        } else {
            expect(S, "label identifier");
        }
        skip(S, ';');

    } else if (t == TOK_ASM1 || t == TOK_ASM2 || t == TOK_ASM3) {
        asm_instr(S);

    } else {
        if (S->tccpp_tok == ':' && t >= TOK_UIDENT) {
            /* label case */
	    next(S);
            s = label_find(S, t);
            if (s) {
                if (s->r == LABEL_DEFINED)
                    tcc_error(S, "duplicate label '%s'", get_tok_str(S, s->v, NULL));
                s->r = LABEL_DEFINED;
		if (s->next) {
		    Sym *pcl; /* pending cleanup goto */
		    for (pcl = s->next; pcl; pcl = pcl->prev)
		      gsym(S, pcl->jnext);
		    sym_pop(S, &s->next, NULL, 0);
		} else
		  gsym(S, s->jnext);
            } else {
                s = label_push(S, &S->tccgen_global_label_stack, t, LABEL_DEFINED);
            }
            s->jnext = gind(S);
            s->cleanupstate = S->tccgen_cur_scope->cl.s;

    block_after_label:
            vla_restore(S, S->tccgen_cur_scope->vla.loc);
            if (S->tccpp_tok != '}')
                goto again;
            /* we accept this, but it is a mistake */
            tcc_warning_c(warn_all)(S, "deprecated use of label at end of compound statement");

        } else {
            /* expression case */
            if (t != ';') {
                unget_tok(S, t);
    expr:
                if (is_expr) {
                    vpop(S);
                    gexpr(S);
                } else {
                    gexpr(S);
                    vpop(S);
                }
                skip(S, ';');
            }
        }
    }

    if (S->tccgen_debug_modes)
        tcc_tcov_check_line (S, 0), tcc_tcov_block_end (S, 0);
}

/* This skips over a stream of tokens containing balanced {} and ()
   pairs, stopping at outer ',' ';' and '}' (or matching '}' if we started
   with a '{').  If STR then allocates and stores the skipped tokens
   in *STR.  This doesn't check if () and {} are nested correctly,
   i.e. "({)}" is accepted.  */
static void skip_or_save_block(TCCState* S, TokenString **str)
{
    int braces = S->tccpp_tok == '{';
    int level = 0;
    if (str)
      *str = tok_str_alloc(S);

    while ((level > 0 || (S->tccpp_tok != '}' && S->tccpp_tok != ',' && S->tccpp_tok != ';' && S->tccpp_tok != ')'))) {
	int t;
	if (S->tccpp_tok == TOK_EOF) {
	     if (str || level > 0)
	       tcc_error(S, "unexpected end of file");
	     else
	       break;
	}
	if (str)
	  tok_str_add_tok(S, *str);
	t = S->tccpp_tok;
	next(S);
	if (t == '{' || t == '(') {
	    level++;
	} else if (t == '}' || t == ')') {
	    level--;
	    if (level == 0 && braces && t == '}')
	      break;
	}
    }
    if (str) {
	tok_str_add(S, *str, -1);
	tok_str_add(S, *str, 0);
    }
}

#define EXPR_CONST 1
#define EXPR_ANY   2

static void parse_init_elem(TCCState* S, int expr_type)
{
    int saved_global_expr;
    switch(expr_type) {
    case EXPR_CONST:
        /* compound literals must be allocated globally in this case */
        saved_global_expr = S->tccgen_global_expr;
        S->tccgen_global_expr = 1;
        expr_const1(S);
        S->tccgen_global_expr = saved_global_expr;
        /* NOTE: symbols are accepted, as well as lvalue for anon symbols
	   (compound literals).  */
        if (((S->tccgen_vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST
             && ((S->tccgen_vtop->r & (VT_SYM|VT_LVAL)) != (VT_SYM|VT_LVAL)
                 || S->tccgen_vtop->sym->v < SYM_FIRST_ANOM))
#ifdef TCC_TARGET_PE
                 || ((S->tccgen_vtop->r & VT_SYM) && S->tccgen_vtop->sym->a.dllimport)
#endif
           )
            tcc_error(S, "initializer element is not constant");
        break;
    case EXPR_ANY:
        expr_eq(S);
        break;
    }
}

#if 1
static void init_assert(TCCState* S, init_params *p, int offset)
{
    if (p->sec ? !NODATA_WANTED && offset > p->sec->data_offset
               : !S->tccgen_nocode_wanted && offset > p->local_offset)
        tcc_internal_error(S, "initializer overflow");
}
#else
#define init_assert(S, sec, offset)
#endif

/* put zeros for variable based init */
static void init_putz(TCCState* S, init_params *p, unsigned long c, int size)
{
    init_assert(S, p, c + size);
    if (p->sec) {
        /* nothing to do because globals are already set to zero */
    } else {
        vpush_helper_func(S, TOK_memset);
        vseti(S, VT_LOCAL, c);
#ifdef TCC_TARGET_ARM
        vpushs(S, size);
        vpushi(S, 0);
#else
        vpushi(S, 0);
        vpushs(S, size);
#endif
        gfunc_call(S, 3);
    }
}

#define DIF_FIRST     1
#define DIF_SIZE_ONLY 2
#define DIF_HAVE_ELEM 4
#define DIF_CLEAR     8

/* delete relocations for specified range c ... c + size. Unfortunatly
   in very special cases, relocations may occur unordered */
static void decl_design_delrels(Section *sec, int c, int size)
{
    ElfW_Rel *rel, *rel2, *rel_end;
    if (!sec || !sec->reloc)
        return;
    rel = rel2 = (ElfW_Rel*)sec->reloc->data;
    rel_end = (ElfW_Rel*)(sec->reloc->data + sec->reloc->data_offset);
    while (rel < rel_end) {
        if (rel->r_offset >= c && rel->r_offset < c + size) {
            sec->reloc->data_offset -= sizeof *rel;
        } else {
            if (rel2 != rel)
                memcpy(rel2, rel, sizeof *rel);
            ++rel2;
        }
        ++rel;
    }
}

static void decl_design_flex(TCCState* S, init_params *p, Sym *ref, int index)
{
    if (ref == p->flex_array_ref) {
        if (index >= ref->c)
            ref->c = index + 1;
    } else if (ref->c < 0)
        tcc_error(S, "flexible array has zero size in this context");
}

/* t is the array or struct type. c is the array or struct
   address. cur_field is the pointer to the current
   field, for arrays the 'c' member contains the current start
   index.  'flags' is as in decl_initializer.
   'al' contains the already initialized length of the
   current container (starting at c).  This returns the new length of that.  */
static int decl_designator(TCCState* S, init_params *p, CType *type, unsigned long c,
                           Sym **cur_field, int flags, int al)
{
    Sym *s, *f;
    int index, index_last, align, l, nb_elems, elem_size;
    unsigned long corig = c;

    elem_size = 0;
    nb_elems = 1;

    if (flags & DIF_HAVE_ELEM)
        goto no_designator;

    if (gnu_ext && S->tccpp_tok >= TOK_UIDENT) {
        l = S->tccpp_tok, next(S);
        if (S->tccpp_tok == ':')
            goto struct_field;
        unget_tok(S, l);
    }

    /* NOTE: we only support ranges for last designator */
    while (nb_elems == 1 && (S->tccpp_tok == '[' || S->tccpp_tok == '.')) {
        if (S->tccpp_tok == '[') {
            if (!(type->t & VT_ARRAY))
                expect(S, "array type");
            next(S);
            index = index_last = expr_const(S);
            if (S->tccpp_tok == TOK_DOTS && gnu_ext) {
                next(S);
                index_last = expr_const(S);
            }
            skip(S, ']');
            s = type->ref;
            decl_design_flex(S, p, s, index_last);
            if (index < 0 || index_last >= s->c || index_last < index)
	        tcc_error(S, "index exceeds array bounds or range is empty");
            if (cur_field)
		(*cur_field)->c = index_last;
            type = pointed_type(type);
            elem_size = type_size(type, &align);
            c += index * elem_size;
            nb_elems = index_last - index + 1;
        } else {
            int cumofs;
            next(S);
            l = S->tccpp_tok;
        struct_field:
            next(S);
            if ((type->t & VT_BTYPE) != VT_STRUCT)
                expect(S, "struct/union type");
            cumofs = 0;
	    f = find_field(type, l, &cumofs);
            if (!f)
                expect(S, "field");
            if (cur_field)
                *cur_field = f;
	    type = &f->type;
            c += cumofs + f->c;
        }
        cur_field = NULL;
    }
    if (!cur_field) {
        if (S->tccpp_tok == '=') {
            next(S);
        } else if (!gnu_ext) {
	    expect(S, "=");
        }
    } else {
    no_designator:
        if (type->t & VT_ARRAY) {
	    index = (*cur_field)->c;
            s = type->ref;
            decl_design_flex(S, p, s, index);
            if (index >= s->c)
                tcc_error(S, "too many initializers");
            type = pointed_type(type);
            elem_size = type_size(type, &align);
            c += index * elem_size;
        } else {
            f = *cur_field;
	    while (f && (f->v & SYM_FIRST_ANOM) && (f->type.t & VT_BITFIELD))
	        *cur_field = f = f->next;
            if (!f)
                tcc_error(S, "too many initializers");
	    type = &f->type;
            c += f->c;
        }
    }

    if (!elem_size) /* for structs */
        elem_size = type_size(type, &align);

    /* Using designators the same element can be initialized more
       than once.  In that case we need to delete possibly already
       existing relocations. */
    if (!(flags & DIF_SIZE_ONLY) && c - corig < al) {
        decl_design_delrels(p->sec, c, elem_size * nb_elems);
        flags &= ~DIF_CLEAR; /* mark stack dirty too */
    }

    decl_initializer(S, p, type, c, flags & ~DIF_FIRST);

    if (!(flags & DIF_SIZE_ONLY) && nb_elems > 1) {
        Sym aref = {0};
        CType t1;
        int i;
        if (p->sec || (type->t & VT_ARRAY)) {
            /* make init_putv/vstore believe it were a struct */
            aref.c = elem_size;
            t1.t = VT_STRUCT, t1.ref = &aref;
            type = &t1;
        }
        if (p->sec)
            vpush_ref(S, type, p->sec, c, elem_size);
        else
	    vset(S, type, VT_LOCAL|VT_LVAL, c);
        for (i = 1; i < nb_elems; i++) {
            vdup(S);
            init_putv(S, p, type, c + elem_size * i);
	}
        vpop(S);
    }

    c += nb_elems * elem_size;
    if (c - corig > al)
      al = c - corig;
    return al;
}

/* store a value or an expression directly in global data or in local array */
static void init_putv(TCCState* S, init_params *p, CType *type, unsigned long c)
{
    int bt;
    void *ptr;
    CType dtype;
    int size, align;
    Section *sec = p->sec;
    uint64_t val;

    dtype = *type;
    dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */

    size = type_size(type, &align);
    if (type->t & VT_BITFIELD)
        size = (BIT_POS(type->t) + BIT_SIZE(type->t) + 7) / 8;
    init_assert(S, p, c + size);

    if (sec) {
        /* XXX: not portable */
        /* XXX: generate error if incorrect relocation */
        gen_assign_cast(S, &dtype);
        bt = type->t & VT_BTYPE;

        if ((S->tccgen_vtop->r & VT_SYM)
            && bt != VT_PTR
            && (bt != (PTR_SIZE == 8 ? VT_LLONG : VT_INT)
                || (type->t & VT_BITFIELD))
            && !((S->tccgen_vtop->r & VT_CONST) && S->tccgen_vtop->sym->v >= SYM_FIRST_ANOM)
            )
            tcc_error(S, "initializer element is not computable at load time");

        if (NODATA_WANTED) {
            S->tccgen_vtop--;
            return;
        }

        ptr = sec->data + c;
        val = S->tccgen_vtop->c.i;

        /* XXX: make code faster ? */
	if ((S->tccgen_vtop->r & (VT_SYM|VT_CONST)) == (VT_SYM|VT_CONST) &&
	    S->tccgen_vtop->sym->v >= SYM_FIRST_ANOM &&
	    /* XXX This rejects compound literals like
	       '(void *){ptr}'.  The problem is that '&sym' is
	       represented the same way, which would be ruled out
	       by the SYM_FIRST_ANOM check above, but also '"string"'
	       in 'char *p = "string"' is represented the same
	       with the type being VT_PTR and the symbol being an
	       anonymous one.  That is, there's no difference in vtop
	       between '(void *){x}' and '&(void *){x}'.  Ignore
	       pointer typed entities here.  Hopefully no real code
	       will ever use compound literals with scalar type.  */
	    (S->tccgen_vtop->type.t & VT_BTYPE) != VT_PTR) {
	    /* These come from compound literals, memcpy stuff over.  */
	    Section *ssec;
	    ElfSym *esym;
	    ElfW_Rel *rel;
	    esym = elfsym(S, S->tccgen_vtop->sym);
	    ssec = S->sections[esym->st_shndx];
	    memmove (ptr, ssec->data + esym->st_value + (int)S->tccgen_vtop->c.i, size);
	    if (ssec->reloc) {
		/* We need to copy over all memory contents, and that
		   includes relocations.  Use the fact that relocs are
		   created it order, so look from the end of relocs
		   until we hit one before the copied region.  */
                unsigned long relofs = ssec->reloc->data_offset;
		while (relofs >= sizeof(*rel)) {
                    relofs -= sizeof(*rel);
                    rel = (ElfW_Rel*)(ssec->reloc->data + relofs);
		    if (rel->r_offset >= esym->st_value + size)
		      continue;
		    if (rel->r_offset < esym->st_value)
		      break;
		    put_elf_reloca(symtab_section, sec,
				   c + rel->r_offset - esym->st_value,
				   ELFW(R_TYPE)(rel->r_info),
				   ELFW(R_SYM)(rel->r_info),
#if PTR_SIZE == 8
				   rel->r_addend
#else
				   0
#endif
				  );
		}
	    }
	} else {
            if (type->t & VT_BITFIELD) {
                int bit_pos, bit_size, bits, n;
                unsigned char *p, v, m;
                bit_pos = BIT_POS(S->tccgen_vtop->type.t);
                bit_size = BIT_SIZE(S->tccgen_vtop->type.t);
                p = (unsigned char*)ptr + (bit_pos >> 3);
                bit_pos &= 7, bits = 0;
                while (bit_size) {
                    n = 8 - bit_pos;
                    if (n > bit_size)
                        n = bit_size;
                    v = val >> bits << bit_pos;
                    m = ((1 << n) - 1) << bit_pos;
                    *p = (*p & ~m) | (v & m);
                    bits += n, bit_size -= n, bit_pos = 0, ++p;
                }
            } else
            switch(bt) {
	    case VT_BOOL:
		*(char *)ptr = val != 0;
                break;
	    case VT_BYTE:
		*(char *)ptr = val;
		break;
	    case VT_SHORT:
                write16le(ptr, val);
		break;
	    case VT_FLOAT:
                write32le(ptr, val);
		break;
	    case VT_DOUBLE:
                write64le(ptr, val);
		break;
	    case VT_LDOUBLE:
#if defined TCC_IS_NATIVE_387
                /* Host and target platform may be different but both have x87.
                   On windows, tcc does not use VT_LDOUBLE, except when it is a
                   cross compiler.  In this case a mingw gcc as host compiler
                   comes here with 10-byte long doubles, while msvc or tcc won't.
                   tcc itself can still translate by asm.
                   In any case we avoid possibly random bytes 11 and 12.
                */
                if (sizeof (long double) >= 10)
                    memcpy(ptr, &S->tccgen_vtop->c.ld, 10);
#ifdef __TINYC__
                else if (sizeof (long double) == sizeof (double))
                    __asm__("fldl %1\nfstpt %0\n" : "=m" (*ptr) : "m" (S->tccgen_vtop->c.ld));
#endif
                else if (S->tccgen_vtop->c.ld == 0.0)
                    ;
                else
#endif
                /* For other platforms it should work natively, but may not work
                   for cross compilers */
                if (sizeof(long double) == LDOUBLE_SIZE)
                    memcpy(ptr, &S->tccgen_vtop->c.ld, LDOUBLE_SIZE);
                else if (sizeof(double) == LDOUBLE_SIZE)
                    memcpy(ptr, &S->tccgen_vtop->c.ld, LDOUBLE_SIZE);
#ifndef TCC_CROSS_TEST
                else
                    tcc_error(S, "can't cross compile long double constants");
#endif
		break;

#if PTR_SIZE == 8
            /* intptr_t may need a reloc too, see tcctest.c:relocation_test() */
	    case VT_LLONG:
	    case VT_PTR:
	        if (S->tccgen_vtop->r & VT_SYM)
	          greloca(S, sec, S->tccgen_vtop->sym, c, R_DATA_PTR, val);
	        else
	          write64le(ptr, val);
	        break;
            case VT_INT:
                write32le(ptr, val);
                break;
#else
	    case VT_LLONG:
                write64le(ptr, val);
                break;
            case VT_PTR:
            case VT_INT:
	        if (S->tccgen_vtop->r & VT_SYM)
	          greloc(S, sec, S->tccgen_vtop->sym, c, R_DATA_PTR);
	        write32le(ptr, val);
	        break;
#endif
	    default:
                //tcc_internal_error("unexpected type");
                break;
	    }
	}
        S->tccgen_vtop--;
    } else {
        vset(S, &dtype, VT_LOCAL|VT_LVAL, c);
        vswap(S);
        vstore(S);
        vpop(S);
    }
}

/* 't' contains the type and storage info. 'c' is the offset of the
   object in section 'sec'. If 'sec' is NULL, it means stack based
   allocation. 'flags & DIF_FIRST' is true if array '{' must be read (multi
   dimension implicit array init handling). 'flags & DIF_SIZE_ONLY' is true if
   size only evaluation is wanted (only for arrays). */
static void decl_initializer(TCCState* S, init_params *p, CType *type, unsigned long c, int flags)
{
    int len, n, no_oblock, i;
    int size1, align1;
    Sym *s, *f;
    Sym indexsym;
    CType *t1;

    /* generate line number info */
    if (S->tccgen_debug_modes && !p->sec)
        tcc_debug_line(S), tcc_tcov_check_line (S, 1);

    if (!(flags & DIF_HAVE_ELEM) && S->tccpp_tok != '{' &&
	/* In case of strings we have special handling for arrays, so
	   don't consume them as initializer value (which would commit them
	   to some anonymous symbol).  */
	S->tccpp_tok != TOK_LSTR && S->tccpp_tok != TOK_STR &&
	!(flags & DIF_SIZE_ONLY)) {
	parse_init_elem(S, !p->sec ? EXPR_ANY : EXPR_CONST);
        flags |= DIF_HAVE_ELEM;
    }

    if ((flags & DIF_HAVE_ELEM) &&
	!(type->t & VT_ARRAY) &&
	/* Use i_c_parameter_t, to strip toplevel qualifiers.
	   The source type might have VT_CONSTANT set, which is
	   of course assignable to non-const elements.  */
	is_compatible_unqualified_types(type, &S->tccgen_vtop->type)) {
        goto init_putv;

    } else if (type->t & VT_ARRAY) {
        no_oblock = 1;
        if (((flags & DIF_FIRST) && S->tccpp_tok != TOK_LSTR && S->tccpp_tok != TOK_STR) ||
            S->tccpp_tok == '{') {
            skip(S, '{');
            no_oblock = 0;
        }

        s = type->ref;
        n = s->c;
        t1 = pointed_type(type);
        size1 = type_size(t1, &align1);

        /* only parse strings here if correct type (otherwise: handle
           them as ((w)char *) expressions */
        if ((S->tccpp_tok == TOK_LSTR && 
#ifdef TCC_TARGET_PE
             (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
#else
             (t1->t & VT_BTYPE) == VT_INT
#endif
            ) || (S->tccpp_tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
	    len = 0;
            cstr_reset(&S->tccgen_initstr);
            if (size1 != (S->tccpp_tok == TOK_STR ? 1 : sizeof(nwchar_t)))
              tcc_error(S, "unhandled string literal merging");
            while (S->tccpp_tok == TOK_STR || S->tccpp_tok == TOK_LSTR) {
                if (S->tccgen_initstr.size)
                  S->tccgen_initstr.size -= size1;
                if (S->tccpp_tok == TOK_STR)
                  len += S->tccpp_tokc.str.size;
                else
                  len += S->tccpp_tokc.str.size / sizeof(nwchar_t);
                len--;
                cstr_cat(S, &S->tccgen_initstr, S->tccpp_tokc.str.data, S->tccpp_tokc.str.size);
                next(S);
            }
            if (S->tccpp_tok != ')' && S->tccpp_tok != '}' && S->tccpp_tok != ',' && S->tccpp_tok != ';'
                && S->tccpp_tok != TOK_EOF) {
                /* Not a lone literal but part of a bigger expression.  */
                unget_tok(S, size1 == 1 ? TOK_STR : TOK_LSTR);
                S->tccpp_tokc.str.size = S->tccgen_initstr.size;
                S->tccpp_tokc.str.data = S->tccgen_initstr.data;
                goto do_init_array;
            }

            decl_design_flex(S, p, s, len);
            if (!(flags & DIF_SIZE_ONLY)) {
                int nb = n;
                if (len < nb)
                    nb = len;
                if (len > nb)
                  tcc_warning(S, "initializer-string for array is too long");
                /* in order to go faster for common case (char
                   string in global variable, we handle it
                   specifically */
                if (p->sec && size1 == 1) {
                    init_assert(S, p, c + nb);
                    if (!NODATA_WANTED)
                      memcpy(p->sec->data + c, S->tccgen_initstr.data, nb);
                } else {
                    for(i=0;i<n;i++) {
                        if (i >= nb) {
                          /* only add trailing zero if enough storage (no
                             warning in this case since it is standard) */
                          if (flags & DIF_CLEAR)
                            break;
                          if (n - i >= 4) {
                            init_putz(S, p, c + i * size1, (n - i) * size1);
                            break;
                          }
                          S->tccpp_ch = 0;
                        } else if (size1 == 1)
                          S->tccpp_ch = ((unsigned char *)S->tccgen_initstr.data)[i];
                        else
                          S->tccpp_ch = ((nwchar_t *)S->tccgen_initstr.data)[i];
                        vpushi(S, S->tccpp_ch);
                        init_putv(S, p, t1, c + i * size1);
                    }
                }
            }
        } else {

          do_init_array:
	    indexsym.c = 0;
	    f = &indexsym;

          do_init_list:
            /* zero memory once in advance */
            if (!(flags & (DIF_CLEAR | DIF_SIZE_ONLY))) {
                init_putz(S, p, c, n*size1);
                flags |= DIF_CLEAR;
            }

	    len = 0;
	    while (S->tccpp_tok != '}' || (flags & DIF_HAVE_ELEM)) {
		len = decl_designator(S, p, type, c, &f, flags, len);
		flags &= ~DIF_HAVE_ELEM;
		if (type->t & VT_ARRAY) {
		    ++indexsym.c;
		    /* special test for multi dimensional arrays (may not
		       be strictly correct if designators are used at the
		       same time) */
		    if (no_oblock && len >= n*size1)
		        break;
		} else {
		    if (s->type.t == VT_UNION)
		        f = NULL;
		    else
		        f = f->next;
		    if (no_oblock && f == NULL)
		        break;
		}

		if (S->tccpp_tok == '}')
		    break;
		skip(S, ',');
	    }
        }
        if (!no_oblock)
            skip(S, '}');
    } else if ((type->t & VT_BTYPE) == VT_STRUCT) {
        no_oblock = 1;
        if ((flags & DIF_FIRST) || S->tccpp_tok == '{') {
            skip(S, '{');
            no_oblock = 0;
        }
        s = type->ref;
        f = s->next;
        n = s->c;
        size1 = 1;
	goto do_init_list;
    } else if (S->tccpp_tok == '{') {
        if (flags & DIF_HAVE_ELEM)
          skip(S, ';');
        next(S);
        decl_initializer(S, p, type, c, flags & ~DIF_HAVE_ELEM);
        skip(S, '}');
    } else if ((flags & DIF_SIZE_ONLY)) {
	/* If we supported only ISO C we wouldn't have to accept calling
	   this on anything than an array if DIF_SIZE_ONLY (and even then
	   only on the outermost level, so no recursion would be needed),
	   because initializing a flex array member isn't supported.
	   But GNU C supports it, so we need to recurse even into
	   subfields of structs and arrays when DIF_SIZE_ONLY is set.  */
        /* just skip expression */
        skip_or_save_block(S, NULL);
    } else {
	if (!(flags & DIF_HAVE_ELEM)) {
	    /* This should happen only when we haven't parsed
	       the init element above for fear of committing a
	       string constant to memory too early.  */
	    if (S->tccpp_tok != TOK_STR && S->tccpp_tok != TOK_LSTR)
	      expect(S, "string constant");
	    parse_init_elem(S, !p->sec ? EXPR_ANY : EXPR_CONST);
	}
    init_putv:
        if (!p->sec && (flags & DIF_CLEAR) /* container was already zero'd */
            && (S->tccgen_vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST
            && S->tccgen_vtop->c.i == 0
            && btype_size(type->t & VT_BTYPE) /* not for fp constants */
            )
            vpop(S);
        else
            init_putv(S, p, type, c);
    }
}

/* parse an initializer for type 't' if 'has_init' is non zero, and
   allocate space in local or global data space ('r' is either
   VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
   variable 'v' of scope 'scope' is declared before initializers
   are parsed. If 'v' is zero, then a reference to the new object
   is put in the value stack. If 'has_init' is 2, a special parsing
   is done to handle string constants. */
static void decl_initializer_alloc(TCCState* S, CType *type, AttributeDef *ad, int r, 
                                   int has_init, int v, int scope)
{
    int size, align, addr;
    TokenString *init_str = NULL;

    Section *sec;
    Sym *flexible_array;
    Sym *sym;
    int saved_nocode_wanted = S->tccgen_nocode_wanted;
#ifdef CONFIG_TCC_BCHECK
    int bcheck = S->do_bounds_check && !NODATA_WANTED;
#endif
    init_params p = {0};

    /* Always allocate static or global variables */
    if (v && (r & VT_VALMASK) == VT_CONST)
        S->tccgen_nocode_wanted |= 0x80000000;

    flexible_array = NULL;
    size = type_size(type, &align);

    /* exactly one flexible array may be initialized, either the
       toplevel array or the last member of the toplevel struct */

    if (size < 0) {
        /* If the base type itself was an array type of unspecified size
           (like in 'typedef int arr[]; arr x = {1};') then we will
           overwrite the unknown size by the real one for this decl.
           We need to unshare the ref symbol holding that size. */
        type->ref = sym_push(S, SYM_FIELD, &type->ref->type, 0, type->ref->c);
        p.flex_array_ref = type->ref;

    } else if (has_init && (type->t & VT_BTYPE) == VT_STRUCT) {
        Sym *field = type->ref->next;
        if (field) {
            while (field->next)
                field = field->next;
            if (field->type.t & VT_ARRAY && field->type.ref->c < 0) {
                flexible_array = field;
                p.flex_array_ref = field->type.ref;
                size = -1;
            }
        }
    }

    if (size < 0) {
        /* If unknown size, do a dry-run 1st pass */
        if (!has_init) 
            tcc_error(S, "unknown type size");
        if (has_init == 2) {
            /* only get strings */
            init_str = tok_str_alloc(S);
            while (S->tccpp_tok == TOK_STR || S->tccpp_tok == TOK_LSTR) {
                tok_str_add_tok(S, init_str);
                next(S);
            }
            tok_str_add(S, init_str, -1);
            tok_str_add(S, init_str, 0);
        } else
            skip_or_save_block(S, &init_str);
        unget_tok(S, 0);

        /* compute size */
        begin_macro(S, init_str, 1);
        next(S);
        decl_initializer(S, &p, type, 0, DIF_FIRST | DIF_SIZE_ONLY);
        /* prepare second initializer parsing */
        S->tccpp_macro_ptr = init_str->str;
        next(S);

        /* if still unknown size, error */
        size = type_size(type, &align);
        if (size < 0) 
            tcc_error(S, "unknown type size");

        /* If there's a flex member and it was used in the initializer
           adjust size.  */
        if (flexible_array && flexible_array->type.ref->c > 0)
            size += flexible_array->type.ref->c
                    * pointed_size(&flexible_array->type);
    }

    /* take into account specified alignment if bigger */
    if (ad->a.aligned) {
	int speca = 1 << (ad->a.aligned - 1);
        if (speca > align)
            align = speca;
    } else if (ad->a.packed) {
        align = 1;
    }

    if (!v && NODATA_WANTED)
        size = 0, align = 1;

    if ((r & VT_VALMASK) == VT_LOCAL) {
        sec = NULL;
#ifdef CONFIG_TCC_BCHECK
        if (bcheck && v) {
            /* add padding between stack variables for bound checking */
            S->tccgen_loc -= align;
        }
#endif
        S->tccgen_loc = (S->tccgen_loc - size) & -align;
        addr = S->tccgen_loc;
        p.local_offset = addr + size;
#ifdef CONFIG_TCC_BCHECK
        if (bcheck && v) {
            /* add padding between stack variables for bound checking */
            S->tccgen_loc -= align;
        }
#endif
        if (v) {
            /* local variable */
#ifdef CONFIG_TCC_ASM
	    if (ad->asm_label) {
		int reg = asm_parse_regvar(S, ad->asm_label);
		if (reg >= 0)
		    r = (r & ~VT_VALMASK) | reg;
	    }
#endif
            sym = sym_push(S, v, type, r, addr);
	    if (ad->cleanup_func) {
		Sym *cls = sym_push2(S, &S->tccgen_all_cleanups,
                    SYM_FIELD | ++S->tccgen_cur_scope->cl.n, 0, 0);
		cls->prev_tok = sym;
		cls->next = ad->cleanup_func;
		cls->ncl = S->tccgen_cur_scope->cl.s;
		S->tccgen_cur_scope->cl.s = cls;
	    }

            sym->a = ad->a;
        } else {
            /* push local reference */
            vset(S, type, r, addr);
        }
    } else {
	sym = NULL;
        if (v && scope == VT_CONST) {
            /* see if the symbol was already defined */
            sym = sym_find(S, v);
            if (sym) {
                if (p.flex_array_ref && (sym->type.t & type->t & VT_ARRAY)
                    && sym->type.ref->c > type->ref->c) {
                    /* flex array was already declared with explicit size
                            extern int arr[10];
                            int arr[] = { 1,2,3 }; */
                    type->ref->c = sym->type.ref->c;
                    size = type_size(type, &align);
                }
                patch_storage(S, sym, ad, type);
                /* we accept several definitions of the same global variable. */
                if (!has_init && sym->c && elfsym(S, sym)->st_shndx != SHN_UNDEF)
                    goto no_alloc;
            }
        }

        /* allocate symbol in corresponding section */
        sec = ad->section;
        if (!sec) {
            CType *tp = type;
            while ((tp->t & (VT_BTYPE|VT_ARRAY)) == (VT_PTR|VT_ARRAY))
                tp = &tp->ref->type;
            if (tp->t & VT_CONSTANT) {
		sec = rodata_section;
            } else if (has_init) {
		sec = data_section;
                /*if (tcc_state->g_debug & 4)
                    tcc_warning(S, "rw data: %s", get_tok_str(v, 0));*/
            } else if (S->nocommon)
                sec = bss_section;
        }

        if (sec) {
	    addr = section_add(S, sec, size, align);
#ifdef CONFIG_TCC_BCHECK
            /* add padding if bound check */
            if (bcheck)
                section_add(S, sec, 1, 1);
#endif
        } else {
            addr = align; /* SHN_COMMON is special, symbol value is align */
	    sec = common_section;
        }

        if (v) {
            if (!sym) {
                sym = sym_push(S, v, type, r | VT_SYM, 0);
                patch_storage(S, sym, ad, NULL);
            }
            /* update symbol definition */
	    put_extern_sym(S, sym, sec, addr, size);
        } else {
            /* push global reference */
            vpush_ref(S, type, sec, addr, size);
            sym = S->tccgen_vtop->sym;
	    S->tccgen_vtop->r |= r;
        }

#ifdef CONFIG_TCC_BCHECK
        /* handles bounds now because the symbol must be defined
           before for the relocation */
        if (bcheck) {
            addr_t *bounds_ptr;

            greloca(S, bounds_section, sym, bounds_section->data_offset, R_DATA_PTR, 0);
            /* then add global bound info */
            bounds_ptr = section_ptr_add(S, bounds_section, 2 * sizeof(addr_t));
            bounds_ptr[0] = 0; /* relocated */
            bounds_ptr[1] = size;
        }
#endif
    }

    if (type->t & VT_VLA) {
        int a;

        if (NODATA_WANTED)
            goto no_alloc;

        /* save before-VLA stack pointer if needed */
        if (S->tccgen_cur_scope->vla.num == 0) {
            if (S->tccgen_cur_scope->prev && S->tccgen_cur_scope->prev->vla.num) {
                S->tccgen_cur_scope->vla.locorig = S->tccgen_cur_scope->prev->vla.loc;
            } else {
                gen_vla_sp_save(S, S->tccgen_loc -= PTR_SIZE);
                S->tccgen_cur_scope->vla.locorig = S->tccgen_loc;
            }
        }

        vla_runtime_type_size(S, type, &a);
        gen_vla_alloc(S, type, a);
#if defined TCC_TARGET_PE && defined TCC_TARGET_X86_64
        /* on _WIN64, because of the function args scratch area, the
           result of alloca differs from RSP and is returned in RAX.  */
        gen_vla_result(S, addr), addr = (S->tccgen_loc -= PTR_SIZE);
#endif
        gen_vla_sp_save(S, addr);
        S->tccgen_cur_scope->vla.loc = addr;
        S->tccgen_cur_scope->vla.num++;
    } else if (has_init) {
        p.sec = sec;
        decl_initializer(S, &p, type, addr, DIF_FIRST);
        /* patch flexible array member size back to -1, */
        /* for possible subsequent similar declarations */
        if (flexible_array)
            flexible_array->type.ref->c = -1;
    }

 no_alloc:
    /* restore parse state if needed */
    if (init_str) {
        end_macro(S);
        next(S);
    }

    S->tccgen_nocode_wanted = saved_nocode_wanted;
}

/* parse a function defined by symbol 'sym' and generate its code in
   'cur_text_section' */
static void gen_function(TCCState* S, Sym *sym)
{
    scope_t f = { 0 };
    S->tccgen_cur_scope = S->tccgen_root_scope = &f;
    S->tccgen_nocode_wanted = 0;
    S->tccgen_ind = cur_text_section->data_offset;
    if (sym->a.aligned) {
	size_t newoff = section_add(S, cur_text_section, 0,
				    1 << (sym->a.aligned - 1));
	gen_fill_nops(S, newoff - S->tccgen_ind);
    }
    /* NOTE: we patch the symbol size later */
    put_extern_sym(S, sym, cur_text_section, S->tccgen_ind, 0);
    if (sym->type.ref->f.func_ctor)
        add_array (S, ".init_array", sym->c);
    if (sym->type.ref->f.func_dtor)
        add_array (S, ".fini_array", sym->c);

    S->tccgen_funcname = get_tok_str(S, sym->v, NULL);
    S->tccgen_func_ind = S->tccgen_ind;
    S->tccgen_func_vt = sym->type.ref->type;
    S->tccgen_func_var = sym->type.ref->f.func_type == FUNC_ELLIPSIS;

    /* put debug symbol */
    tcc_debug_funcstart(S, sym);
    /* push a dummy symbol to enable local sym storage */
    sym_push2(S, &S->tccgen_local_stack, SYM_FIELD, 0, 0);
    S->tccgen_local_scope = 1; /* for function parameters */
    gfunc_prolog(S, sym);
    S->tccgen_local_scope = 0;
    S->tccgen_rsym = 0;
    clear_temp_local_var_list(S);
    block(S, 0);
    gsym(S, S->tccgen_rsym);
    S->tccgen_nocode_wanted = 0;
    /* reset local stack */
    pop_local_syms(S, NULL, 0);
    gfunc_epilog(S);
    cur_text_section->data_offset = S->tccgen_ind;
    S->tccgen_local_scope = 0;
    label_pop(S, &S->tccgen_global_label_stack, NULL, 0);
    sym_pop(S, &S->tccgen_all_cleanups, NULL, 0);
    /* patch symbol size */
    elfsym(S, sym)->st_size = S->tccgen_ind - S->tccgen_func_ind;
    /* end of function */
    tcc_debug_funcend(S, S->tccgen_ind - S->tccgen_func_ind);
    /* It's better to crash than to generate wrong code */
    cur_text_section = NULL;
    S->tccgen_funcname = ""; /* for safety */
    S->tccgen_func_vt.t = VT_VOID; /* for safety */
    S->tccgen_func_var = 0; /* for safety */
    S->tccgen_ind = 0; /* for safety */
    S->tccgen_nocode_wanted = 0x80000000;
    check_vstack(S);
    /* do this after funcend debug info */
    next(S);
}

static void gen_inline_functions(TCCState *S)
{
    Sym *sym;
    int inline_generated, i;
    struct InlineFunc *fn;

    tcc_open_bf(S, ":inline:", 0);
    /* iterate while inline function are referenced */
    do {
        inline_generated = 0;
        for (i = 0; i < S->nb_inline_fns; ++i) {
            fn = S->inline_fns[i];
            sym = fn->sym;
            if (sym && (sym->c || !(sym->type.t & VT_INLINE))) {
                /* the function was used or forced (and then not internal):
                   generate its code and convert it to a normal function */
                fn->sym = NULL;
                tcc_debug_putfile(S, fn->filename);
                begin_macro(S, fn->func_str, 1);
                next(S);
                cur_text_section = text_section;
                gen_function(S, sym);
                end_macro(S);

                inline_generated = 1;
            }
        }
    } while (inline_generated);
    tcc_close(S);
}

static void free_inline_functions(TCCState *S)
{
    int i;
    /* free tokens of unused inline functions */
    for (i = 0; i < S->nb_inline_fns; ++i) {
        struct InlineFunc *fn = S->inline_fns[i];
        if (fn->sym)
            tok_str_free(S, fn->func_str);
    }
    dynarray_reset(S, &S->inline_fns, &S->nb_inline_fns);
}

/* 'l' is VT_LOCAL or VT_CONST to define default storage type, or VT_CMP
   if parsing old style parameter decl list (and FUNC_SYM is set then) */
static int decl0(TCCState* S, int l, int is_for_loop_init, Sym *func_sym)
{
    int v, has_init, r, oldint;
    CType type, btype;
    Sym *sym;
    AttributeDef ad, adbase;

    while (1) {
	if (S->tccpp_tok == TOK_STATIC_ASSERT) {
	    CString error_str;
	    int c;

	    next(S);
	    skip(S, '(');
	    c = expr_const(S);

	    if (S->tccpp_tok == ')') {
		if (!c)
		    tcc_error(S, "_Static_assert fail");
		next(S);
		goto static_assert_out;
	    }

	    skip(S, ',');
	    parse_mult_str(S, &error_str, "string constant");
	    if (c == 0)
		tcc_error(S, "%s", (char *)error_str.data);
	    cstr_free(S, &error_str);
	    skip(S, ')');
	  static_assert_out:
            skip(S, ';');
	    continue;
	}

        oldint = 0;
        if (!parse_btype(S, &btype, &adbase)) {
            if (is_for_loop_init)
                return 0;
            /* skip redundant ';' if not in old parameter decl scope */
            if (S->tccpp_tok == ';' && l != VT_CMP) {
                next(S);
                continue;
            }
            if (l != VT_CONST)
                break;
            if (S->tccpp_tok == TOK_ASM1 || S->tccpp_tok == TOK_ASM2 || S->tccpp_tok == TOK_ASM3) {
                /* global asm block */
                asm_global_instr(S);
                continue;
            }
            if (S->tccpp_tok >= TOK_UIDENT) {
               /* special test for old K&R protos without explicit int
                  type. Only accepted when defining global data */
                btype.t = VT_INT;
                oldint = 1;
            } else {
                if (S->tccpp_tok != TOK_EOF)
                    expect(S, "declaration");
                break;
            }
        }

        if (S->tccpp_tok == ';') {
	    if ((btype.t & VT_BTYPE) == VT_STRUCT) {
		v = btype.ref->v;
		if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) >= SYM_FIRST_ANOM)
        	    tcc_warning(S, "unnamed struct/union that defines no instances");
                next(S);
                continue;
	    }
            if (IS_ENUM(btype.t)) {
                next(S);
                continue;
            }
        }

        while (1) { /* iterate thru each declaration */
            type = btype;
	    ad = adbase;
            type_decl(S, &type, &ad, &v, TYPE_DIRECT);
#if 0
            {
                char buf[500];
                type_to_str(S, buf, sizeof(buf), &type, get_tok_str(S, v, NULL));
                printf("type = '%s'\n", buf);
            }
#endif
            if ((type.t & VT_BTYPE) == VT_FUNC) {
                if ((type.t & VT_STATIC) && (l == VT_LOCAL))
                    tcc_error(S, "function without file scope cannot be static");
                /* if old style function prototype, we accept a
                   declaration list */
                sym = type.ref;
                if (sym->f.func_type == FUNC_OLD && l == VT_CONST)
                    decl0(S, VT_CMP, 0, sym);
#ifdef TCC_TARGET_MACHO
                if (sym->f.func_alwinl
                    && ((type.t & (VT_EXTERN | VT_INLINE))
                        == (VT_EXTERN | VT_INLINE))) {
                    /* always_inline functions must be handled as if they
                       don't generate multiple global defs, even if extern
                       inline, i.e. GNU inline semantics for those.  Rewrite
                       them into static inline.  */
                    type.t &= ~VT_EXTERN;
                    type.t |= VT_STATIC;
                }
#endif
                /* always compile 'extern inline' */
                if (type.t & VT_EXTERN)
                    type.t &= ~VT_INLINE;

            } else if (oldint) {
                tcc_warning(S, "type defaults to int");
            }

            if (gnu_ext && (S->tccpp_tok == TOK_ASM1 || S->tccpp_tok == TOK_ASM2 || S->tccpp_tok == TOK_ASM3)) {
                ad.asm_label = asm_label_instr(S);
                /* parse one last attribute list, after asm label */
                parse_attribute(S, &ad);
            #if 0
                /* gcc does not allow __asm__("label") with function definition,
                   but why not ... */
                if (Stccpp_tok == '{')
                    expect(S, ";");
            #endif
            }

#ifdef TCC_TARGET_PE
            if (ad.a.dllimport || ad.a.dllexport) {
                if (type.t & VT_STATIC)
                    tcc_error(S, "cannot have dll linkage with static");
                if (type.t & VT_TYPEDEF) {
                    tcc_warning(S, "'%s' attribute ignored for typedef",
                        ad.a.dllimport ? (ad.a.dllimport = 0, "dllimport") :
                        (ad.a.dllexport = 0, "dllexport"));
                } else if (ad.a.dllimport) {
                    if ((type.t & VT_BTYPE) == VT_FUNC)
                        ad.a.dllimport = 0;
                    else
                        type.t |= VT_EXTERN;
                }
            }
#endif
            if (S->tccpp_tok == '{') {
                if (l != VT_CONST)
                    tcc_error(S, "cannot use local functions");
                if ((type.t & VT_BTYPE) != VT_FUNC)
                    expect(S, "function definition");

                /* reject abstract declarators in function definition
                   make old style params without decl have int type */
                sym = type.ref;
                while ((sym = sym->next) != NULL) {
                    if (!(sym->v & ~SYM_FIELD))
                        expect(S, "identifier");
                    if (sym->type.t == VT_VOID)
                        sym->type = S->tccgen_int_type;
                }

                /* apply post-declaraton attributes */
                merge_funcattr(&type.ref->f, &ad.f);

                /* put function symbol */
                type.t &= ~VT_EXTERN;
                sym = external_sym(S, v, &type, 0, &ad);

                /* static inline functions are just recorded as a kind
                   of macro. Their code will be emitted at the end of
                   the compilation unit only if they are used */
                if (sym->type.t & VT_INLINE) {
                    struct InlineFunc *fn;
                    fn = tcc_malloc(S, sizeof *fn + strlen(S->tccpp_file->filename));
                    strcpy(fn->filename, S->tccpp_file->filename);
                    fn->sym = sym;
		    skip_or_save_block(S, &fn->func_str);
                    dynarray_add(S, &S->inline_fns,
				 &S->nb_inline_fns, fn);
                } else {
                    /* compute text section */
                    cur_text_section = ad.section;
                    if (!cur_text_section)
                        cur_text_section = text_section;
                    gen_function(S, sym);
                }
                break;
            } else {
		if (l == VT_CMP) {
		    /* find parameter in function parameter list */
		    for (sym = func_sym->next; sym; sym = sym->next)
			if ((sym->v & ~SYM_FIELD) == v)
			    goto found;
		    tcc_error(S, "declaration for parameter '%s' but no such parameter",
			      get_tok_str(S, v, NULL));
found:
		    if (type.t & VT_STORAGE) /* 'register' is okay */
		        tcc_error(S, "storage class specified for '%s'",
				  get_tok_str(S, v, NULL));
		    if (sym->type.t != VT_VOID)
		        tcc_error(S, "redefinition of parameter '%s'",
				  get_tok_str(S, v, NULL));
		    convert_parameter_type(S, &type);
		    sym->type = type;
		} else if (type.t & VT_TYPEDEF) {
                    /* save typedefed type  */
                    /* XXX: test storage specifiers ? */
                    sym = sym_find(S, v);
                    if (sym && sym->sym_scope == S->tccgen_local_scope) {
                        if (!is_compatible_types(&sym->type, &type)
                            || !(sym->type.t & VT_TYPEDEF))
                            tcc_error(S, "incompatible redefinition of '%s'",
                                get_tok_str(S, v, NULL));
                        sym->type = type;
                    } else {
                        sym = sym_push(S, v, &type, 0, 0);
                    }
                    sym->a = ad.a;
                    sym->f = ad.f;
                    if (S->tccgen_debug_modes)
                        tcc_debug_typedef (S, sym);
		} else if ((type.t & VT_BTYPE) == VT_VOID
			   && !(type.t & VT_EXTERN)) {
		    tcc_error(S, "declaration of void object");
                } else {
                    r = 0;
                    if ((type.t & VT_BTYPE) == VT_FUNC) {
                        /* external function definition */
                        /* specific case for func_call attribute */
                        type.ref->f = ad.f;
                    } else if (!(type.t & VT_ARRAY)) {
                        /* not lvalue if array */
                        r |= VT_LVAL;
                    }
                    has_init = (S->tccpp_tok == '=');
                    if (has_init && (type.t & VT_VLA))
                        tcc_error(S, "variable length array cannot be initialized");
                    if (((type.t & VT_EXTERN) && (!has_init || l != VT_CONST))
		        || (type.t & VT_BTYPE) == VT_FUNC
                        /* as with GCC, uninitialized global arrays with no size
                           are considered extern: */
                        || ((type.t & VT_ARRAY) && !has_init
                            && l == VT_CONST && type.ref->c < 0)
                        ) {
                        /* external variable or function */
                        type.t |= VT_EXTERN;
                        sym = external_sym(S, v, &type, r, &ad);
                        if (ad.alias_target) {
                            /* Aliases need to be emitted when their target
                               symbol is emitted, even if perhaps unreferenced.
                               We only support the case where the base is
                               already defined, otherwise we would need
                               deferring to emit the aliases until the end of
                               the compile unit.  */
                            Sym *alias_target = sym_find(S, ad.alias_target);
                            ElfSym *esym = elfsym(S, alias_target);
                            if (!esym)
                                tcc_error(S, "unsupported forward __alias__ attribute");
                            put_extern_sym2(S, sym, esym->st_shndx,
                                            esym->st_value, esym->st_size, 1);
                        }
                    } else {
                        if (type.t & VT_STATIC)
                            r |= VT_CONST;
                        else
                            r |= l;
                        if (has_init)
                            next(S);
                        else if (l == VT_CONST)
                            /* uninitialized global variables may be overridden */
                            type.t |= VT_EXTERN;
                        decl_initializer_alloc(S, &type, &ad, r, has_init, v, l);
                    }
                }
                if (S->tccpp_tok != ',') {
                    if (is_for_loop_init)
                        return 1;
                    skip(S, ';');
                    break;
                }
                next(S);
            }
        }
    }
    return 0;
}

static void decl(TCCState* S, int l)
{
    decl0(S, l, 0, NULL);
}

/* ------------------------------------------------------------------------- */
#undef gjmp_addr
#undef gjmp
/* ------------------------------------------------------------------------- */