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
|
/* tc-xtensa.c -- Assemble Xtensa instructions.
Copyright 2003 Free Software Foundation, Inc.
This file is part of GAS, the GNU Assembler.
GAS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GAS; see the file COPYING. If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */
#include <string.h>
#include "as.h"
#include "sb.h"
#include "safe-ctype.h"
#include "tc-xtensa.h"
#include "frags.h"
#include "subsegs.h"
#include "xtensa-relax.h"
#include "xtensa-istack.h"
#include "dwarf2dbg.h"
#include "struc-symbol.h"
#include "xtensa-config.h"
#ifndef uint32
#define uint32 unsigned int
#endif
#ifndef int32
#define int32 signed int
#endif
/* Notes:
There are 3 forms for instructions,
1) the MEMORY format -- this is the encoding 2 or 3 byte instruction
2) the TInsn -- handles instructions/labels and literals;
all operands are assumed to be expressions
3) the IStack -- a stack of TInsn. this allows us to
reason about the generated expansion instructions
Naming conventions (used somewhat inconsistently):
The xtensa_ functions are exported
The xg_ functions are internal
We also have a couple of different extensibility mechanisms.
1) The idiom replacement:
This is used when a line is first parsed to
replace an instruction pattern with another instruction
It is currently limited to replacements of instructions
with constant operands.
2) The xtensa-relax.c mechanism that has stronger instruction
replacement patterns. When an instruction's immediate field
does not fit the next instruction sequence is attempted.
In addition, "narrow" opcodes are supported this way. */
/* Define characters with special meanings to GAS. */
const char comment_chars[] = "#";
const char line_comment_chars[] = "#";
const char line_separator_chars[] = ";";
const char EXP_CHARS[] = "eE";
const char FLT_CHARS[] = "rRsSfFdDxXpP";
/* Flag to indicate whether the hardware supports the density option.
If not, enabling density instructions (via directives or --density flag)
is illegal. */
#if STATIC_LIBISA
bfd_boolean density_supported = XCHAL_HAVE_DENSITY;
#else
bfd_boolean density_supported = TRUE;
#endif
#define XTENSA_FETCH_WIDTH 4
/* Flags for properties of the last instruction in a segment. */
#define FLAG_IS_A0_WRITER 0x1
#define FLAG_IS_BAD_LOOPEND 0x2
/* We define a special segment names ".literal" to place literals
into. The .fini and .init sections are special because they
contain code that is moved together by the linker. We give them
their own special .fini.literal and .init.literal sections. */
#define LITERAL_SECTION_NAME xtensa_section_rename (".literal")
#define FINI_SECTION_NAME xtensa_section_rename (".fini")
#define INIT_SECTION_NAME xtensa_section_rename (".init")
#define FINI_LITERAL_SECTION_NAME xtensa_section_rename (".fini.literal")
#define INIT_LITERAL_SECTION_NAME xtensa_section_rename (".init.literal")
/* This type is used for the directive_stack to keep track of the
state of the literal collection pools. */
typedef struct lit_state_struct
{
const char *lit_seg_name;
const char *init_lit_seg_name;
const char *fini_lit_seg_name;
segT lit_seg;
segT init_lit_seg;
segT fini_lit_seg;
} lit_state;
static lit_state default_lit_sections;
/* We keep lists of literal segments. The seg_list type is the node
for such a list. The *_literal_head locals are the heads of the
various lists. All of these lists have a dummy node at the start. */
typedef struct seg_list_struct
{
struct seg_list_struct *next;
segT seg;
} seg_list;
static seg_list literal_head_h;
static seg_list *literal_head = &literal_head_h;
static seg_list init_literal_head_h;
static seg_list *init_literal_head = &init_literal_head_h;
static seg_list fini_literal_head_h;
static seg_list *fini_literal_head = &fini_literal_head_h;
/* Lists of symbols. We keep a list of symbols that label the current
instruction, so that we can adjust the symbols when inserting alignment
for various instructions. We also keep a list of all the symbols on
literals, so that we can fix up those symbols when the literals are
later moved into the text sections. */
typedef struct sym_list_struct
{
struct sym_list_struct *next;
symbolS *sym;
} sym_list;
static sym_list *insn_labels = NULL;
static sym_list *free_insn_labels = NULL;
static sym_list *saved_insn_labels = NULL;
static sym_list *literal_syms;
/* Global flag to indicate when we are emitting literals. */
int generating_literals = 0;
/* Structure for saving the current state before emitting literals. */
typedef struct emit_state_struct
{
const char *name;
segT now_seg;
subsegT now_subseg;
int generating_literals;
} emit_state;
/* Directives. */
typedef enum
{
directive_none = 0,
directive_literal,
directive_density,
directive_generics,
directive_relax,
directive_freeregs,
directive_longcalls,
directive_literal_prefix
} directiveE;
typedef struct
{
const char *name;
bfd_boolean can_be_negated;
} directive_infoS;
const directive_infoS directive_info[] =
{
{"none", FALSE},
{"literal", FALSE},
{"density", TRUE},
{"generics", TRUE},
{"relax", TRUE},
{"freeregs", FALSE},
{"longcalls", TRUE},
{"literal_prefix", FALSE}
};
bfd_boolean directive_state[] =
{
FALSE, /* none */
FALSE, /* literal */
#if STATIC_LIBISA && !XCHAL_HAVE_DENSITY
FALSE, /* density */
#else
TRUE, /* density */
#endif
TRUE, /* generics */
TRUE, /* relax */
FALSE, /* freeregs */
FALSE, /* longcalls */
FALSE /* literal_prefix */
};
enum xtensa_relax_statesE
{
RELAX_ALIGN_NEXT_OPCODE,
/* Use the first opcode of the next fragment to determine the
alignment requirements. This is ONLY used for LOOPS
currently. */
RELAX_DESIRE_ALIGN_IF_TARGET,
/* These are placed in front of labels. They will all be converted
to RELAX_DESIRE_ALIGN / RELAX_LOOP_END or rs_fill of 0 before
relaxation begins. */
RELAX_ADD_NOP_IF_A0_B_RETW,
/* These are placed in front of conditional branches. It will be
turned into a NOP (using a1) if the branch is immediately
followed by a RETW or RETW.N. Otherwise it will be turned into
an rs_fill of 0 before relaxation begins. */
RELAX_ADD_NOP_IF_PRE_LOOP_END,
/* These are placed after JX instructions. It will be turned into a
NOP if there is one instruction before a loop end label.
Otherwise it will be turned into an rs_fill of 0 before
relaxation begins. This is used to avoid a hardware TIE
interlock issue prior to T1040. */
RELAX_ADD_NOP_IF_SHORT_LOOP,
/* These are placed after LOOP instructions. It will be turned into
a NOP when: (1) there are less than 3 instructions in the loop;
we place 2 of these in a row to add up to 2 NOPS in short loops;
or (2) The instructions in the loop do not include a branch or
jump. Otherwise it will be turned into an rs_fill of 0 before
relaxation begins. This is used to avoid hardware bug
PR3830. */
RELAX_ADD_NOP_IF_CLOSE_LOOP_END,
/* These are placed after LOOP instructions. It will be turned into
a NOP if there are less than 12 bytes to the end of some other
loop's end. Otherwise it will be turned into an rs_fill of 0
before relaxation begins. This is used to avoid hardware bug
PR3830. */
RELAX_DESIRE_ALIGN,
/* The next fragment like its first instruction to NOT cross a
4-byte boundary. */
RELAX_LOOP_END,
/* This will be turned into a NOP or NOP.N if the previous
instruction is expanded to negate a loop. */
RELAX_LOOP_END_ADD_NOP,
/* When the code density option is available, this will generate a
NOP.N marked RELAX_NARROW. Otherwise, it will create an rs_fill
fragment with a NOP in it. */
RELAX_LITERAL,
/* Another fragment could generate an expansion here but has not yet. */
RELAX_LITERAL_NR,
/* Expansion has been generated by an instruction that generates a
literal. However, the stretch has NOT been reported yet in this
fragment. */
RELAX_LITERAL_FINAL,
/* Expansion has been generated by an instruction that generates a
literal. */
RELAX_LITERAL_POOL_BEGIN,
RELAX_LITERAL_POOL_END,
/* Technically these are not relaxations at all, but mark a location
to store literals later. Note that fr_var stores the frchain for
BEGIN frags and fr_var stores now_seg for END frags. */
RELAX_NARROW,
/* The last instruction in this fragment (at->fr_opcode) can be
freely replaced with a single wider instruction if a future
alignment desires or needs it. */
RELAX_IMMED,
/* The last instruction in this fragment (at->fr_opcode) contains
the value defined by fr_symbol (fr_offset = 0). If the value
does not fit, use the specified expansion. This is similar to
"NARROW", except that these may not be expanded in order to align
code. */
RELAX_IMMED_STEP1,
/* The last instruction in this fragment (at->fr_opcode) contains a
literal. It has already been expanded at least 1 step. */
RELAX_IMMED_STEP2
/* The last instruction in this fragment (at->fr_opcode) contains a
literal. It has already been expanded at least 2 steps. */
};
/* This is used as a stopper to bound the number of steps that
can be taken. */
#define RELAX_IMMED_MAXSTEPS (RELAX_IMMED_STEP2 - RELAX_IMMED)
typedef bfd_boolean (*frag_predicate) (const fragS *);
/* Directive functions. */
static bfd_boolean use_generics
PARAMS ((void));
static bfd_boolean use_longcalls
PARAMS ((void));
static bfd_boolean code_density_available
PARAMS ((void));
static bfd_boolean can_relax
PARAMS ((void));
static void directive_push
PARAMS ((directiveE, bfd_boolean, const void *));
static void directive_pop
PARAMS ((directiveE *, bfd_boolean *, const char **,
unsigned int *, const void **));
static void directive_balance
PARAMS ((void));
static bfd_boolean inside_directive
PARAMS ((directiveE));
static void get_directive
PARAMS ((directiveE *, bfd_boolean *));
static void xtensa_begin_directive
PARAMS ((int));
static void xtensa_end_directive
PARAMS ((int));
static void xtensa_literal_prefix
PARAMS ((char const *, int));
static void xtensa_literal_position
PARAMS ((int));
static void xtensa_literal_pseudo
PARAMS ((int));
/* Parsing and Idiom Translation Functions. */
static const char *expression_end
PARAMS ((const char *));
static unsigned tc_get_register
PARAMS ((const char *));
static void expression_maybe_register
PARAMS ((xtensa_operand, expressionS *));
static int tokenize_arguments
PARAMS ((char **, char *));
static bfd_boolean parse_arguments
PARAMS ((TInsn *, int, char **));
static int xg_translate_idioms
PARAMS ((char **, int *, char **));
static int xg_translate_sysreg_op
PARAMS ((char **, int *, char **));
static void xg_reverse_shift_count
PARAMS ((char **));
static int xg_arg_is_constant
PARAMS ((char *, offsetT *));
static void xg_replace_opname
PARAMS ((char **, char *));
static int xg_check_num_args
PARAMS ((int *, int, char *, char **));
/* Functions for dealing with the Xtensa ISA. */
static bfd_boolean operand_is_immed
PARAMS ((xtensa_operand));
static bfd_boolean operand_is_pcrel_label
PARAMS ((xtensa_operand));
static int get_relaxable_immed
PARAMS ((xtensa_opcode));
static xtensa_opcode get_opcode_from_buf
PARAMS ((const char *));
static bfd_boolean is_direct_call_opcode
PARAMS ((xtensa_opcode));
static bfd_boolean is_call_opcode
PARAMS ((xtensa_opcode));
static bfd_boolean is_entry_opcode
PARAMS ((xtensa_opcode));
static bfd_boolean is_loop_opcode
PARAMS ((xtensa_opcode));
static bfd_boolean is_the_loop_opcode
PARAMS ((xtensa_opcode));
static bfd_boolean is_jx_opcode
PARAMS ((xtensa_opcode));
static bfd_boolean is_windowed_return_opcode
PARAMS ((xtensa_opcode));
static bfd_boolean is_conditional_branch_opcode
PARAMS ((xtensa_opcode));
static bfd_boolean is_branch_or_jump_opcode
PARAMS ((xtensa_opcode));
static bfd_reloc_code_real_type opnum_to_reloc
PARAMS ((int));
static int reloc_to_opnum
PARAMS ((bfd_reloc_code_real_type));
static void xtensa_insnbuf_set_operand
PARAMS ((xtensa_insnbuf, xtensa_opcode, xtensa_operand, int32,
const char *, unsigned int));
static uint32 xtensa_insnbuf_get_operand
PARAMS ((xtensa_insnbuf, xtensa_opcode, int));
static void xtensa_insnbuf_set_immediate_field
PARAMS ((xtensa_opcode, xtensa_insnbuf, int32, const char *,
unsigned int));
static bfd_boolean is_negatable_branch
PARAMS ((TInsn *));
/* Various Other Internal Functions. */
static bfd_boolean is_unique_insn_expansion
PARAMS ((TransitionRule *));
static int xg_get_insn_size
PARAMS ((TInsn *));
static int xg_get_build_instr_size
PARAMS ((BuildInstr *));
static bfd_boolean xg_is_narrow_insn
PARAMS ((TInsn *));
static bfd_boolean xg_is_single_relaxable_insn
PARAMS ((TInsn *));
static int xg_get_max_narrow_insn_size
PARAMS ((xtensa_opcode));
static int xg_get_max_insn_widen_size
PARAMS ((xtensa_opcode));
static int xg_get_max_insn_widen_literal_size
PARAMS ((xtensa_opcode));
static bfd_boolean xg_is_relaxable_insn
PARAMS ((TInsn *, int));
static symbolS *get_special_literal_symbol
PARAMS ((void));
static symbolS *get_special_label_symbol
PARAMS ((void));
static bfd_boolean xg_build_to_insn
PARAMS ((TInsn *, TInsn *, BuildInstr *));
static bfd_boolean xg_build_to_stack
PARAMS ((IStack *, TInsn *, BuildInstr *));
static bfd_boolean xg_expand_to_stack
PARAMS ((IStack *, TInsn *, int));
static bfd_boolean xg_expand_narrow
PARAMS ((TInsn *, TInsn *));
static bfd_boolean xg_immeds_fit
PARAMS ((const TInsn *));
static bfd_boolean xg_symbolic_immeds_fit
PARAMS ((const TInsn *, segT, fragS *, offsetT, long));
static bfd_boolean xg_check_operand
PARAMS ((int32, xtensa_operand));
static int is_dnrange
PARAMS ((fragS *, symbolS *, long));
static int xg_assembly_relax
PARAMS ((IStack *, TInsn *, segT, fragS *, offsetT, int, long));
static void xg_force_frag_space
PARAMS ((int));
static void xg_finish_frag
PARAMS ((char *, enum xtensa_relax_statesE, int, bfd_boolean));
static bfd_boolean is_branch_jmp_to_next
PARAMS ((TInsn *, fragS *));
static void xg_add_branch_and_loop_targets
PARAMS ((TInsn *));
static bfd_boolean xg_instruction_matches_rule
PARAMS ((TInsn *, TransitionRule *));
static TransitionRule *xg_instruction_match
PARAMS ((TInsn *));
static bfd_boolean xg_build_token_insn
PARAMS ((BuildInstr *, TInsn *, TInsn *));
static bfd_boolean xg_simplify_insn
PARAMS ((TInsn *, TInsn *));
static bfd_boolean xg_expand_assembly_insn
PARAMS ((IStack *, TInsn *));
static symbolS *xg_assemble_literal
PARAMS ((TInsn *));
static void xg_assemble_literal_space
PARAMS ((int));
static symbolS *xtensa_create_literal_symbol
PARAMS ((segT, fragS *));
static void xtensa_add_literal_sym
PARAMS ((symbolS *));
static void xtensa_add_insn_label
PARAMS ((symbolS *));
static void xtensa_clear_insn_labels
PARAMS ((void));
static bfd_boolean get_is_linkonce_section
PARAMS ((bfd *, segT));
static bfd_boolean xg_emit_insn
PARAMS ((TInsn *, bfd_boolean));
static bfd_boolean xg_emit_insn_to_buf
PARAMS ((TInsn *, char *, fragS *, offsetT, bfd_boolean));
static bfd_boolean xg_add_opcode_fix
PARAMS ((xtensa_opcode, int, expressionS *, fragS *, offsetT));
static void xg_resolve_literals
PARAMS ((TInsn *, symbolS *));
static void xg_resolve_labels
PARAMS ((TInsn *, symbolS *));
static void xg_assemble_tokens
PARAMS ((TInsn *));
static bfd_boolean is_register_writer
PARAMS ((const TInsn *, const char *, int));
static bfd_boolean is_bad_loopend_opcode
PARAMS ((const TInsn *));
static bfd_boolean is_unaligned_label
PARAMS ((symbolS *));
static fragS *next_non_empty_frag
PARAMS ((const fragS *));
static xtensa_opcode next_frag_opcode
PARAMS ((const fragS *));
static void update_next_frag_nop_state
PARAMS ((fragS *));
static bfd_boolean next_frag_is_branch_target
PARAMS ((const fragS *));
static bfd_boolean next_frag_is_loop_target
PARAMS ((const fragS *));
static addressT next_frag_pre_opcode_bytes
PARAMS ((const fragS *));
static bfd_boolean is_next_frag_target
PARAMS ((const fragS *, const fragS *));
static void xtensa_mark_literal_pool_location
PARAMS ((void));
static void xtensa_move_labels
PARAMS ((fragS *, valueT, bfd_boolean));
static void assemble_nop
PARAMS ((size_t, char *));
static addressT get_expanded_loop_offset
PARAMS ((xtensa_opcode));
static fragS *get_literal_pool_location
PARAMS ((segT));
static void set_literal_pool_location
PARAMS ((segT, fragS *));
/* Helpers for xtensa_end(). */
static void xtensa_cleanup_align_frags
PARAMS ((void));
static void xtensa_fix_target_frags
PARAMS ((void));
static bfd_boolean frag_can_negate_branch
PARAMS ((fragS *));
static void xtensa_fix_a0_b_retw_frags
PARAMS ((void));
static bfd_boolean next_instrs_are_b_retw
PARAMS ((fragS *));
static void xtensa_fix_b_j_loop_end_frags
PARAMS ((void));
static bfd_boolean next_instr_is_loop_end
PARAMS ((fragS *));
static void xtensa_fix_close_loop_end_frags
PARAMS ((void));
static size_t min_bytes_to_other_loop_end
PARAMS ((fragS *, fragS *, offsetT, size_t));
static size_t unrelaxed_frag_min_size
PARAMS ((fragS *));
static void xtensa_fix_short_loop_frags
PARAMS ((void));
static size_t count_insns_to_loop_end
PARAMS ((fragS *, bfd_boolean, size_t));
static size_t unrelaxed_frag_min_insn_count
PARAMS ((fragS *));
static bfd_boolean branch_before_loop_end
PARAMS ((fragS *));
static bfd_boolean unrelaxed_frag_has_b_j
PARAMS ((fragS *));
static void xtensa_sanity_check
PARAMS ((void));
static bfd_boolean is_empty_loop
PARAMS ((const TInsn *, fragS *));
static bfd_boolean is_local_forward_loop
PARAMS ((const TInsn *, fragS *));
/* Alignment Functions. */
static size_t get_text_align_power
PARAMS ((int));
static addressT get_text_align_max_fill_size
PARAMS ((int, bfd_boolean, bfd_boolean));
static addressT get_text_align_fill_size
PARAMS ((addressT, int, int, bfd_boolean, bfd_boolean));
static size_t get_text_align_nop_count
PARAMS ((size_t, bfd_boolean));
static size_t get_text_align_nth_nop_size
PARAMS ((size_t, size_t, bfd_boolean));
static addressT get_noop_aligned_address
PARAMS ((fragS *, addressT));
static addressT get_widen_aligned_address
PARAMS ((fragS *, addressT));
/* Helpers for xtensa_relax_frag(). */
static long relax_frag_text_align
PARAMS ((fragS *, long));
static long relax_frag_add_nop
PARAMS ((fragS *));
static long relax_frag_narrow
PARAMS ((fragS *, long));
static bfd_boolean future_alignment_required
PARAMS ((fragS *, long));
static long relax_frag_immed
PARAMS ((segT, fragS *, long, int, int *));
/* Helpers for md_convert_frag(). */
static void convert_frag_align_next_opcode
PARAMS ((fragS *));
static void convert_frag_narrow
PARAMS ((fragS *));
static void convert_frag_immed
PARAMS ((segT, fragS *, int));
static fixS *fix_new_exp_in_seg
PARAMS ((segT, subsegT, fragS *, int, int, expressionS *, int,
bfd_reloc_code_real_type));
static void convert_frag_immed_finish_loop
PARAMS ((segT, fragS *, TInsn *));
static offsetT get_expression_value
PARAMS ((segT, expressionS *));
/* Flags for the Last Instruction in Each Subsegment. */
static unsigned get_last_insn_flags
PARAMS ((segT, subsegT));
static void set_last_insn_flags
PARAMS ((segT, subsegT, unsigned, bfd_boolean));
/* Segment list functions. */
static void xtensa_remove_section
PARAMS ((segT));
static void xtensa_insert_section
PARAMS ((segT, segT));
static void xtensa_move_seg_list_to_beginning
PARAMS ((seg_list *));
static void xtensa_move_literals
PARAMS ((void));
static void xtensa_reorder_seg_list
PARAMS ((seg_list *, segT));
static void xtensa_reorder_segments
PARAMS ((void));
static segT get_last_sec
PARAMS ((void));
static void xtensa_switch_to_literal_fragment
PARAMS ((emit_state *));
static void xtensa_switch_section_emit_state
PARAMS ((emit_state *, segT, subsegT));
static void xtensa_restore_emit_state
PARAMS ((emit_state *));
static void cache_literal_section
PARAMS ((seg_list *, const char *, segT *));
static segT retrieve_literal_seg
PARAMS ((seg_list *, const char *));
static segT seg_present
PARAMS ((const char *));
static void add_seg_list
PARAMS ((seg_list *, segT));
/* Property Table (e.g., ".xt.insn" and ".xt.lit") Functions. */
static void xtensa_create_property_segments
PARAMS ((frag_predicate, const char *, xt_section_type));
static segment_info_type *retrieve_segment_info
PARAMS ((segT));
static segT retrieve_xtensa_section
PARAMS ((char *));
static bfd_boolean section_has_property
PARAMS ((segT sec, frag_predicate));
static void add_xt_block_frags
PARAMS ((segT, segT, xtensa_block_info **, frag_predicate));
static bfd_boolean get_frag_is_literal
PARAMS ((const fragS *));
static bfd_boolean get_frag_is_insn
PARAMS ((const fragS *));
/* Import from elf32-xtensa.c in BFD library. */
extern char *xtensa_get_property_section_name
PARAMS ((asection *, const char *));
/* TInsn and IStack functions. */
static bfd_boolean tinsn_has_symbolic_operands
PARAMS ((const TInsn *));
static bfd_boolean tinsn_has_invalid_symbolic_operands
PARAMS ((const TInsn *));
static bfd_boolean tinsn_has_complex_operands
PARAMS ((const TInsn *));
static bfd_boolean tinsn_to_insnbuf
PARAMS ((TInsn *, xtensa_insnbuf));
static bfd_boolean tinsn_check_arguments
PARAMS ((const TInsn *));
static void tinsn_from_chars
PARAMS ((TInsn *, char *));
static void tinsn_immed_from_frag
PARAMS ((TInsn *, fragS *));
static int get_num_stack_text_bytes
PARAMS ((IStack *));
static int get_num_stack_literal_bytes
PARAMS ((IStack *));
/* Expression Utilities. */
bfd_boolean expr_is_const
PARAMS ((const expressionS *));
offsetT get_expr_const
PARAMS ((const expressionS *));
void set_expr_const
PARAMS ((expressionS *, offsetT));
void set_expr_symbol_offset
PARAMS ((expressionS *, symbolS *, offsetT));
bfd_boolean expr_is_equal
PARAMS ((expressionS *, expressionS *));
static void copy_expr
PARAMS ((expressionS *, const expressionS *));
#ifdef XTENSA_SECTION_RENAME
static void build_section_rename
PARAMS ((const char *));
static void add_section_rename
PARAMS ((char *, char *));
#endif
/* ISA imported from bfd. */
extern xtensa_isa xtensa_default_isa;
extern int target_big_endian;
static xtensa_opcode xtensa_addi_opcode;
static xtensa_opcode xtensa_addmi_opcode;
static xtensa_opcode xtensa_call0_opcode;
static xtensa_opcode xtensa_call4_opcode;
static xtensa_opcode xtensa_call8_opcode;
static xtensa_opcode xtensa_call12_opcode;
static xtensa_opcode xtensa_callx0_opcode;
static xtensa_opcode xtensa_callx4_opcode;
static xtensa_opcode xtensa_callx8_opcode;
static xtensa_opcode xtensa_callx12_opcode;
static xtensa_opcode xtensa_entry_opcode;
static xtensa_opcode xtensa_isync_opcode;
static xtensa_opcode xtensa_j_opcode;
static xtensa_opcode xtensa_jx_opcode;
static xtensa_opcode xtensa_loop_opcode;
static xtensa_opcode xtensa_loopnez_opcode;
static xtensa_opcode xtensa_loopgtz_opcode;
static xtensa_opcode xtensa_nop_n_opcode;
static xtensa_opcode xtensa_or_opcode;
static xtensa_opcode xtensa_ret_opcode;
static xtensa_opcode xtensa_ret_n_opcode;
static xtensa_opcode xtensa_retw_opcode;
static xtensa_opcode xtensa_retw_n_opcode;
static xtensa_opcode xtensa_rsr_opcode;
static xtensa_opcode xtensa_waiti_opcode;
/* Command-line Options. */
bfd_boolean use_literal_section = TRUE;
static bfd_boolean align_targets = TRUE;
static bfd_boolean align_only_targets = FALSE;
static bfd_boolean software_a0_b_retw_interlock = TRUE;
static bfd_boolean has_a0_b_retw = FALSE;
static bfd_boolean workaround_a0_b_retw = TRUE;
static bfd_boolean software_avoid_b_j_loop_end = TRUE;
static bfd_boolean workaround_b_j_loop_end = TRUE;
static bfd_boolean maybe_has_b_j_loop_end = FALSE;
static bfd_boolean software_avoid_short_loop = TRUE;
static bfd_boolean workaround_short_loop = TRUE;
static bfd_boolean maybe_has_short_loop = FALSE;
static bfd_boolean software_avoid_close_loop_end = TRUE;
static bfd_boolean workaround_close_loop_end = TRUE;
static bfd_boolean maybe_has_close_loop_end = FALSE;
/* When avoid_short_loops is true, all loops with early exits must
have at least 3 instructions. avoid_all_short_loops is a modifier
to the avoid_short_loop flag. In addition to the avoid_short_loop
actions, all straightline loopgtz and loopnez must have at least 3
instructions. */
static bfd_boolean software_avoid_all_short_loops = TRUE;
static bfd_boolean workaround_all_short_loops = TRUE;
/* This is on a per-instruction basis. */
static bfd_boolean specific_opcode = FALSE;
enum
{
option_density = OPTION_MD_BASE,
option_no_density,
option_relax,
option_no_relax,
option_generics,
option_no_generics,
option_text_section_literals,
option_no_text_section_literals,
option_align_targets,
option_no_align_targets,
option_align_only_targets,
option_no_align_only_targets,
option_longcalls,
option_no_longcalls,
option_workaround_a0_b_retw,
option_no_workaround_a0_b_retw,
option_workaround_b_j_loop_end,
option_no_workaround_b_j_loop_end,
option_workaround_short_loop,
option_no_workaround_short_loop,
option_workaround_all_short_loops,
option_no_workaround_all_short_loops,
option_workaround_close_loop_end,
option_no_workaround_close_loop_end,
option_no_workarounds,
#ifdef XTENSA_SECTION_RENAME
option_literal_section_name,
option_text_section_name,
option_data_section_name,
option_bss_section_name,
option_rename_section_name,
#endif
option_eb,
option_el
};
const char *md_shortopts = "";
struct option md_longopts[] =
{
{"density", no_argument, NULL, option_density},
{"no-density", no_argument, NULL, option_no_density},
/* At least as early as alameda, --[no-]relax didn't work as
documented, so as of albany, --[no-]relax is equivalent to
--[no-]generics. Both of these will be deprecated in
BearValley. */
{"relax", no_argument, NULL, option_generics},
{"no-relax", no_argument, NULL, option_no_generics},
{"generics", no_argument, NULL, option_generics},
{"no-generics", no_argument, NULL, option_no_generics},
{"text-section-literals", no_argument, NULL, option_text_section_literals},
{"no-text-section-literals", no_argument, NULL,
option_no_text_section_literals},
/* This option was changed from -align-target to -target-align
because it conflicted with the "-al" option. */
{"target-align", no_argument, NULL, option_align_targets},
{"no-target-align", no_argument, NULL,
option_no_align_targets},
#if 0
/* This option should do a better job aligning targets because
it will only attempt to align targets that are the target of a
branch. */
{ "target-align-only", no_argument, NULL, option_align_only_targets },
{ "no-target-align-only", no_argument, NULL, option_no_align_only_targets },
#endif /* 0 */
{"longcalls", no_argument, NULL, option_longcalls},
{"no-longcalls", no_argument, NULL, option_no_longcalls},
{"no-workaround-a0-b-retw", no_argument, NULL,
option_no_workaround_a0_b_retw},
{"workaround-a0-b-retw", no_argument, NULL, option_workaround_a0_b_retw},
{"no-workaround-b-j-loop-end", no_argument, NULL,
option_no_workaround_b_j_loop_end},
{"workaround-b-j-loop-end", no_argument, NULL,
option_workaround_b_j_loop_end},
{"no-workaround-short-loops", no_argument, NULL,
option_no_workaround_short_loop},
{"workaround-short-loops", no_argument, NULL, option_workaround_short_loop},
{"no-workaround-all-short-loops", no_argument, NULL,
option_no_workaround_all_short_loops},
{"workaround-all-short-loop", no_argument, NULL,
option_workaround_all_short_loops},
{"no-workaround-close-loop-end", no_argument, NULL,
option_no_workaround_close_loop_end},
{"workaround-close-loop-end", no_argument, NULL,
option_workaround_close_loop_end},
{"no-workarounds", no_argument, NULL, option_no_workarounds},
#ifdef XTENSA_SECTION_RENAME
{"literal-section-name", required_argument, NULL,
option_literal_section_name},
{"text-section-name", required_argument, NULL,
option_text_section_name},
{"data-section-name", required_argument, NULL,
option_data_section_name},
{"rename-section", required_argument, NULL,
option_rename_section_name},
{"bss-section-name", required_argument, NULL,
option_bss_section_name},
#endif /* XTENSA_SECTION_RENAME */
{NULL, no_argument, NULL, 0}
};
size_t md_longopts_size = sizeof md_longopts;
int
md_parse_option (c, arg)
int c;
char *arg;
{
switch (c)
{
case option_density:
if (!density_supported)
{
as_bad (_("'--density' option not supported in this Xtensa "
"configuration"));
return 0;
}
directive_state[directive_density] = TRUE;
return 1;
case option_no_density:
directive_state[directive_density] = FALSE;
return 1;
case option_generics:
directive_state[directive_generics] = TRUE;
return 1;
case option_no_generics:
directive_state[directive_generics] = FALSE;
return 1;
case option_longcalls:
directive_state[directive_longcalls] = TRUE;
return 1;
case option_no_longcalls:
directive_state[directive_longcalls] = FALSE;
return 1;
case option_text_section_literals:
use_literal_section = FALSE;
return 1;
case option_no_text_section_literals:
use_literal_section = TRUE;
return 1;
case option_workaround_a0_b_retw:
workaround_a0_b_retw = TRUE;
software_a0_b_retw_interlock = TRUE;
return 1;
case option_no_workaround_a0_b_retw:
workaround_a0_b_retw = FALSE;
software_a0_b_retw_interlock = FALSE;
return 1;
case option_workaround_b_j_loop_end:
workaround_b_j_loop_end = TRUE;
software_avoid_b_j_loop_end = TRUE;
return 1;
case option_no_workaround_b_j_loop_end:
workaround_b_j_loop_end = FALSE;
software_avoid_b_j_loop_end = FALSE;
return 1;
case option_workaround_short_loop:
workaround_short_loop = TRUE;
software_avoid_short_loop = TRUE;
return 1;
case option_no_workaround_short_loop:
workaround_short_loop = FALSE;
software_avoid_short_loop = FALSE;
return 1;
case option_workaround_all_short_loops:
workaround_all_short_loops = TRUE;
software_avoid_all_short_loops = TRUE;
return 1;
case option_no_workaround_all_short_loops:
workaround_all_short_loops = FALSE;
software_avoid_all_short_loops = FALSE;
return 1;
case option_workaround_close_loop_end:
workaround_close_loop_end = TRUE;
software_avoid_close_loop_end = TRUE;
return 1;
case option_no_workaround_close_loop_end:
workaround_close_loop_end = FALSE;
software_avoid_close_loop_end = FALSE;
return 1;
case option_no_workarounds:
workaround_a0_b_retw = FALSE;
software_a0_b_retw_interlock = FALSE;
workaround_b_j_loop_end = FALSE;
software_avoid_b_j_loop_end = FALSE;
workaround_short_loop = FALSE;
software_avoid_short_loop = FALSE;
workaround_all_short_loops = FALSE;
software_avoid_all_short_loops = FALSE;
workaround_close_loop_end = FALSE;
software_avoid_close_loop_end = FALSE;
return 1;
case option_align_targets:
align_targets = TRUE;
return 1;
case option_no_align_targets:
align_targets = FALSE;
return 1;
case option_align_only_targets:
align_only_targets = TRUE;
return 1;
case option_no_align_only_targets:
align_only_targets = FALSE;
return 1;
#ifdef XTENSA_SECTION_RENAME
case option_literal_section_name:
add_section_rename (".literal", arg);
as_warn (_("'--literal-section-name' is deprecated; "
"use '--rename-section .literal=NEWNAME'"));
return 1;
case option_text_section_name:
add_section_rename (".text", arg);
as_warn (_("'--text-section-name' is deprecated; "
"use '--rename-section .text=NEWNAME'"));
return 1;
case option_data_section_name:
add_section_rename (".data", arg);
as_warn (_("'--data-section-name' is deprecated; "
"use '--rename-section .data=NEWNAME'"));
return 1;
case option_bss_section_name:
add_section_rename (".bss", arg);
as_warn (_("'--bss-section-name' is deprecated; "
"use '--rename-section .bss=NEWNAME'"));
return 1;
case option_rename_section_name:
build_section_rename (arg);
return 1;
#endif /* XTENSA_SECTION_RENAME */
case 'Q':
/* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
should be emitted or not. FIXME: Not implemented. */
return 1;
default:
return 0;
}
}
void
md_show_usage (stream)
FILE *stream;
{
fputs ("\nXtensa options:\n"
"--[no-]density [Do not] emit density instructions\n"
"--[no-]relax [Do not] perform branch relaxation\n"
"--[no-]generics [Do not] transform instructions\n"
"--[no-]longcalls [Do not] emit 32-bit call sequences\n"
"--[no-]target-align [Do not] try to align branch targets\n"
"--[no-]text-section-literals\n"
" [Do not] put literals in the text section\n"
"--no-workarounds Do not use any Xtensa workarounds\n"
#ifdef XTENSA_SECTION_RENAME
"--rename-section old=new(:old1=new1)*\n"
" Rename section 'old' to 'new'\n"
"\nThe following Xtensa options are deprecated\n"
"--literal-section-name Name of literal section (default .literal)\n"
"--text-section-name Name of text section (default .text)\n"
"--data-section-name Name of data section (default .data)\n"
"--bss-section-name Name of bss section (default .bss)\n"
#endif
, stream);
}
/* Directive data and functions. */
typedef struct state_stackS_struct
{
directiveE directive;
bfd_boolean negated;
bfd_boolean old_state;
const char *file;
unsigned int line;
const void *datum;
struct state_stackS_struct *prev;
} state_stackS;
state_stackS *directive_state_stack;
const pseudo_typeS md_pseudo_table[] =
{
{"align", s_align_bytes, 0}, /* Defaulting is invalid (0) */
{"literal_position", xtensa_literal_position, 0},
{"frame", s_ignore, 0}, /* formerly used for STABS debugging */
{"word", cons, 4},
{"begin", xtensa_begin_directive, 0},
{"end", xtensa_end_directive, 0},
{"literal", xtensa_literal_pseudo, 0},
{NULL, 0, 0},
};
bfd_boolean
use_generics ()
{
return directive_state[directive_generics];
}
bfd_boolean
use_longcalls ()
{
return directive_state[directive_longcalls];
}
bfd_boolean
code_density_available ()
{
return directive_state[directive_density];
}
bfd_boolean
can_relax ()
{
return use_generics ();
}
static void
directive_push (directive, negated, datum)
directiveE directive;
bfd_boolean negated;
const void *datum;
{
char *file;
unsigned int line;
state_stackS *stack = (state_stackS *) xmalloc (sizeof (state_stackS));
as_where (&file, &line);
stack->directive = directive;
stack->negated = negated;
stack->old_state = directive_state[directive];
stack->file = file;
stack->line = line;
stack->datum = datum;
stack->prev = directive_state_stack;
directive_state_stack = stack;
directive_state[directive] = !negated;
}
static void
directive_pop (directive, negated, file, line, datum)
directiveE *directive;
bfd_boolean *negated;
const char **file;
unsigned int *line;
const void **datum;
{
state_stackS *top = directive_state_stack;
if (!directive_state_stack)
{
as_bad (_("unmatched end directive"));
*directive = directive_none;
return;
}
directive_state[directive_state_stack->directive] = top->old_state;
*directive = top->directive;
*negated = top->negated;
*file = top->file;
*line = top->line;
*datum = top->datum;
directive_state_stack = top->prev;
free (top);
}
static void
directive_balance ()
{
while (directive_state_stack)
{
directiveE directive;
bfd_boolean negated;
const char *file;
unsigned int line;
const void *datum;
directive_pop (&directive, &negated, &file, &line, &datum);
as_warn_where ((char *) file, line,
_(".begin directive with no matching .end directive"));
}
}
static bfd_boolean
inside_directive (dir)
directiveE dir;
{
state_stackS *top = directive_state_stack;
while (top && top->directive != dir)
top = top->prev;
return (top != NULL);
}
static void
get_directive (directive, negated)
directiveE *directive;
bfd_boolean *negated;
{
int len;
unsigned i;
if (strncmp (input_line_pointer, "no-", 3) != 0)
*negated = FALSE;
else
{
*negated = TRUE;
input_line_pointer += 3;
}
len = strspn (input_line_pointer,
"abcdefghijklmnopqrstuvwxyz_/0123456789.");
for (i = 0; i < sizeof (directive_info) / sizeof (*directive_info); ++i)
{
if (strncmp (input_line_pointer, directive_info[i].name, len) == 0)
{
input_line_pointer += len;
*directive = (directiveE) i;
if (*negated && !directive_info[i].can_be_negated)
as_bad (_("directive %s can't be negated"),
directive_info[i].name);
return;
}
}
as_bad (_("unknown directive"));
*directive = (directiveE) XTENSA_UNDEFINED;
}
static void
xtensa_begin_directive (ignore)
int ignore ATTRIBUTE_UNUSED;
{
directiveE directive;
bfd_boolean negated;
emit_state *state;
int len;
lit_state *ls;
md_flush_pending_output ();
get_directive (&directive, &negated);
if (directive == (directiveE) XTENSA_UNDEFINED)
{
discard_rest_of_line ();
return;
}
switch (directive)
{
case directive_literal:
if (!inside_directive (directive_literal))
{
/* Previous labels go with whatever follows this directive, not with
the literal, so save them now. */
saved_insn_labels = insn_labels;
insn_labels = NULL;
}
state = (emit_state *) xmalloc (sizeof (emit_state));
xtensa_switch_to_literal_fragment (state);
directive_push (directive_literal, negated, state);
break;
case directive_literal_prefix:
/* Check to see if the current fragment is a literal
fragment. If it is, then this operation is not allowed. */
if (frag_now->tc_frag_data.is_literal)
{
as_bad (_("cannot set literal_prefix inside literal fragment"));
return;
}
/* Allocate the literal state for this section and push
onto the directive stack. */
ls = xmalloc (sizeof (lit_state));
assert (ls);
*ls = default_lit_sections;
directive_push (directive_literal_prefix, negated, ls);
/* Parse the new prefix from the input_line_pointer. */
SKIP_WHITESPACE ();
len = strspn (input_line_pointer,
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz_/0123456789.$");
/* Process the new prefix. */
xtensa_literal_prefix (input_line_pointer, len);
/* Skip the name in the input line. */
input_line_pointer += len;
break;
case directive_freeregs:
/* This information is currently unused, but we'll accept the statement
and just discard the rest of the line. This won't check the syntax,
but it will accept every correct freeregs directive. */
input_line_pointer += strcspn (input_line_pointer, "\n");
directive_push (directive_freeregs, negated, 0);
break;
case directive_density:
if (!density_supported && !negated)
{
as_warn (_("Xtensa density option not supported; ignored"));
break;
}
/* fall through */
default:
directive_push (directive, negated, 0);
break;
}
demand_empty_rest_of_line ();
}
static void
xtensa_end_directive (ignore)
int ignore ATTRIBUTE_UNUSED;
{
directiveE begin_directive, end_directive;
bfd_boolean begin_negated, end_negated;
const char *file;
unsigned int line;
emit_state *state;
lit_state *s;
md_flush_pending_output ();
get_directive (&end_directive, &end_negated);
if (end_directive == (directiveE) XTENSA_UNDEFINED)
{
discard_rest_of_line ();
return;
}
if (end_directive == directive_density && !density_supported && !end_negated)
{
as_warn (_("Xtensa density option not supported; ignored"));
demand_empty_rest_of_line ();
return;
}
directive_pop (&begin_directive, &begin_negated, &file, &line,
(const void **) &state);
if (begin_directive != directive_none)
{
if (begin_directive != end_directive || begin_negated != end_negated)
{
as_bad (_("does not match begin %s%s at %s:%d"),
begin_negated ? "no-" : "",
directive_info[begin_directive].name, file, line);
}
else
{
switch (end_directive)
{
case directive_literal:
frag_var (rs_fill, 0, 0, 0, NULL, 0, NULL);
xtensa_restore_emit_state (state);
free (state);
if (!inside_directive (directive_literal))
{
/* Restore the list of current labels. */
xtensa_clear_insn_labels ();
insn_labels = saved_insn_labels;
}
break;
case directive_freeregs:
break;
case directive_literal_prefix:
/* Restore the default collection sections from saved state. */
s = (lit_state *) state;
assert (s);
if (use_literal_section)
default_lit_sections = *s;
/* free the state storage */
free (s);
break;
default:
break;
}
}
}
demand_empty_rest_of_line ();
}
/* Place an aligned literal fragment at the current location. */
static void
xtensa_literal_position (ignore)
int ignore ATTRIBUTE_UNUSED;
{
if (inside_directive (directive_literal))
as_warn (_(".literal_position inside literal directive; ignoring"));
else if (!use_literal_section)
xtensa_mark_literal_pool_location ();
demand_empty_rest_of_line ();
xtensa_clear_insn_labels ();
}
/* Support .literal label, value@plt + offset. */
static void
xtensa_literal_pseudo (ignored)
int ignored ATTRIBUTE_UNUSED;
{
emit_state state;
char *p, *base_name;
char c;
expressionS expP;
segT dest_seg;
if (inside_directive (directive_literal))
{
as_bad (_(".literal not allowed inside .begin literal region"));
ignore_rest_of_line ();
return;
}
/* Previous labels go with whatever follows this directive, not with
the literal, so save them now. */
saved_insn_labels = insn_labels;
insn_labels = NULL;
/* If we are using text-section literals, then this is the right value... */
dest_seg = now_seg;
base_name = input_line_pointer;
xtensa_switch_to_literal_fragment (&state);
/* ...but if we aren't using text-section-literals, then we
need to put them in the section we just switched to. */
if (use_literal_section)
dest_seg = now_seg;
/* All literals are aligned to four-byte boundaries
which is handled by switch to literal fragment. */
/* frag_align (2, 0, 0); */
c = get_symbol_end ();
/* Just after name is now '\0'. */
p = input_line_pointer;
*p = c;
SKIP_WHITESPACE ();
if (*input_line_pointer != ',' && *input_line_pointer != ':')
{
as_bad (_("expected comma or colon after symbol name; "
"rest of line ignored"));
ignore_rest_of_line ();
xtensa_restore_emit_state (&state);
return;
}
*p = 0;
colon (base_name);
do
{
input_line_pointer++; /* skip ',' or ':' */
expr (0, &expP);
/* We only support 4-byte literals with .literal. */
emit_expr (&expP, 4);
}
while (*input_line_pointer == ',');
*p = c;
demand_empty_rest_of_line ();
xtensa_restore_emit_state (&state);
/* Restore the list of current labels. */
xtensa_clear_insn_labels ();
insn_labels = saved_insn_labels;
}
static void
xtensa_literal_prefix (start, len)
char const *start;
int len;
{
segT s_now; /* Storage for the current seg and subseg. */
subsegT ss_now;
char *name; /* Pointer to the name itself. */
char *newname;
if (!use_literal_section)
return;
/* Store away the current section and subsection. */
s_now = now_seg;
ss_now = now_subseg;
/* Get a null-terminated copy of the name. */
name = xmalloc (len + 1);
assert (name);
strncpy (name, start, len);
name[len] = 0;
/* Allocate the sections (interesting note: the memory pointing to
the name is actually used for the name by the new section). */
newname = xmalloc (len + strlen (".literal") + 1);
strcpy (newname, name);
strcpy (newname + len, ".literal");
/* Note that retrieve_literal_seg does not create a segment if
it already exists. */
default_lit_sections.lit_seg = NULL; /* retrieved on demand */
/* Canonicalizing section names allows renaming literal
sections to occur correctly. */
default_lit_sections.lit_seg_name =
tc_canonicalize_symbol_name (newname);
free (name);
/* Restore the current section and subsection and set the
generation into the old segment. */
subseg_set (s_now, ss_now);
}
/* Parsing and Idiom Translation. */
static const char *
expression_end (name)
const char *name;
{
while (1)
{
switch (*name)
{
case ';':
case '\0':
case ',':
return name;
case ' ':
case '\t':
++name;
continue;
default:
return 0;
}
}
}
#define ERROR_REG_NUM ((unsigned) -1)
static unsigned
tc_get_register (prefix)
const char *prefix;
{
unsigned reg;
const char *next_expr;
const char *old_line_pointer;
SKIP_WHITESPACE ();
old_line_pointer = input_line_pointer;
if (*input_line_pointer == '$')
++input_line_pointer;
/* Accept "sp" as a synonym for "a1". */
if (input_line_pointer[0] == 's' && input_line_pointer[1] == 'p'
&& expression_end (input_line_pointer + 2))
{
input_line_pointer += 2;
return 1; /* AR[1] */
}
while (*input_line_pointer++ == *prefix++)
;
--input_line_pointer;
--prefix;
if (*prefix)
{
as_bad (_("bad register name: %s"), old_line_pointer);
return ERROR_REG_NUM;
}
if (!ISDIGIT ((unsigned char) *input_line_pointer))
{
as_bad (_("bad register number: %s"), input_line_pointer);
return ERROR_REG_NUM;
}
reg = 0;
while (ISDIGIT ((int) *input_line_pointer))
reg = reg * 10 + *input_line_pointer++ - '0';
if (!(next_expr = expression_end (input_line_pointer)))
{
as_bad (_("bad register name: %s"), old_line_pointer);
return ERROR_REG_NUM;
}
input_line_pointer = (char *) next_expr;
return reg;
}
#define PLT_SUFFIX "@PLT"
#define plt_suffix "@plt"
static void
expression_maybe_register (opnd, tok)
xtensa_operand opnd;
expressionS *tok;
{
char *kind = xtensa_operand_kind (opnd);
if ((strlen (kind) == 1)
&& (*kind == 'l' || *kind == 'L' || *kind == 'i' || *kind == 'r'))
{
segT t = expression (tok);
if (t == absolute_section && operand_is_pcrel_label (opnd))
{
assert (tok->X_op == O_constant);
tok->X_op = O_symbol;
tok->X_add_symbol = &abs_symbol;
}
if (tok->X_op == O_symbol
&& (!strncmp (input_line_pointer, PLT_SUFFIX,
strlen (PLT_SUFFIX) - 1)
|| !strncmp (input_line_pointer, plt_suffix,
strlen (plt_suffix) - 1)))
{
symbol_get_tc (tok->X_add_symbol)->plt = 1;
input_line_pointer += strlen (plt_suffix);
}
}
else
{
unsigned reg = tc_get_register (kind);
if (reg != ERROR_REG_NUM) /* Already errored */
{
uint32 buf = reg;
if ((xtensa_operand_encode (opnd, &buf) != xtensa_encode_result_ok)
|| (reg != xtensa_operand_decode (opnd, buf)))
as_bad (_("register number out of range"));
}
tok->X_op = O_register;
tok->X_add_symbol = 0;
tok->X_add_number = reg;
}
}
/* Split up the arguments for an opcode or pseudo-op. */
static int
tokenize_arguments (args, str)
char **args;
char *str;
{
char *old_input_line_pointer;
bfd_boolean saw_comma = FALSE;
bfd_boolean saw_arg = FALSE;
int num_args = 0;
char *arg_end, *arg;
int arg_len;
/* Save and restore input_line_pointer around this function. */
old_input_line_pointer = input_line_pointer;
input_line_pointer = str;
while (*input_line_pointer)
{
SKIP_WHITESPACE ();
switch (*input_line_pointer)
{
case '\0':
goto fini;
case ',':
input_line_pointer++;
if (saw_comma || !saw_arg)
goto err;
saw_comma = TRUE;
break;
default:
if (!saw_comma && saw_arg)
goto err;
arg_end = input_line_pointer + 1;
while (!expression_end (arg_end))
arg_end += 1;
arg_len = arg_end - input_line_pointer;
arg = (char *) xmalloc (arg_len + 1);
args[num_args] = arg;
strncpy (arg, input_line_pointer, arg_len);
arg[arg_len] = '\0';
input_line_pointer = arg_end;
num_args += 1;
saw_comma = FALSE;
saw_arg = TRUE;
break;
}
}
fini:
if (saw_comma)
goto err;
input_line_pointer = old_input_line_pointer;
return num_args;
err:
input_line_pointer = old_input_line_pointer;
return -1;
}
/* Parse the arguments to an opcode. Return true on error. */
static bfd_boolean
parse_arguments (insn, num_args, arg_strings)
TInsn *insn;
int num_args;
char **arg_strings;
{
expressionS *tok = insn->tok;
xtensa_opcode opcode = insn->opcode;
bfd_boolean had_error = TRUE;
xtensa_isa isa = xtensa_default_isa;
int n;
int opcode_operand_count;
int actual_operand_count = 0;
xtensa_operand opnd = NULL;
char *old_input_line_pointer;
if (insn->insn_type == ITYPE_LITERAL)
opcode_operand_count = 1;
else
opcode_operand_count = xtensa_num_operands (isa, opcode);
memset (tok, 0, sizeof (*tok) * MAX_INSN_ARGS);
/* Save and restore input_line_pointer around this function. */
old_input_line_pointer = input_line_pointer;
for (n = 0; n < num_args; n++)
{
input_line_pointer = arg_strings[n];
if (actual_operand_count >= opcode_operand_count)
{
as_warn (_("too many arguments"));
goto err;
}
assert (actual_operand_count < MAX_INSN_ARGS);
opnd = xtensa_get_operand (isa, opcode, actual_operand_count);
expression_maybe_register (opnd, tok);
if (tok->X_op == O_illegal || tok->X_op == O_absent)
goto err;
actual_operand_count++;
tok++;
}
insn->ntok = tok - insn->tok;
had_error = FALSE;
err:
input_line_pointer = old_input_line_pointer;
return had_error;
}
static void
xg_reverse_shift_count (cnt_argp)
char **cnt_argp;
{
char *cnt_arg, *new_arg;
cnt_arg = *cnt_argp;
/* replace the argument with "31-(argument)" */
new_arg = (char *) xmalloc (strlen (cnt_arg) + 6);
sprintf (new_arg, "31-(%s)", cnt_arg);
free (cnt_arg);
*cnt_argp = new_arg;
}
/* If "arg" is a constant expression, return non-zero with the value
in *valp. */
static int
xg_arg_is_constant (arg, valp)
char *arg;
offsetT *valp;
{
expressionS exp;
char *save_ptr = input_line_pointer;
input_line_pointer = arg;
expression (&exp);
input_line_pointer = save_ptr;
if (exp.X_op == O_constant)
{
*valp = exp.X_add_number;
return 1;
}
return 0;
}
static void
xg_replace_opname (popname, newop)
char **popname;
char *newop;
{
free (*popname);
*popname = (char *) xmalloc (strlen (newop) + 1);
strcpy (*popname, newop);
}
static int
xg_check_num_args (pnum_args, expected_num, opname, arg_strings)
int *pnum_args;
int expected_num;
char *opname;
char **arg_strings;
{
int num_args = *pnum_args;
if (num_args < expected_num)
{
as_bad (_("not enough operands (%d) for '%s'; expected %d"),
num_args, opname, expected_num);
return -1;
}
if (num_args > expected_num)
{
as_warn (_("too many operands (%d) for '%s'; expected %d"),
num_args, opname, expected_num);
while (num_args-- > expected_num)
{
free (arg_strings[num_args]);
arg_strings[num_args] = 0;
}
*pnum_args = expected_num;
return -1;
}
return 0;
}
static int
xg_translate_sysreg_op (popname, pnum_args, arg_strings)
char **popname;
int *pnum_args;
char **arg_strings;
{
char *opname, *new_opname;
offsetT val;
bfd_boolean has_underbar = FALSE;
opname = *popname;
if (*opname == '_')
{
has_underbar = TRUE;
opname += 1;
}
/* Opname == [rw]ur... */
if (opname[3] == '\0')
{
/* If the register is not specified as part of the opcode,
then get it from the operand and move it to the opcode. */
if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
return -1;
if (!xg_arg_is_constant (arg_strings[1], &val))
{
as_bad (_("register number for `%s' is not a constant"), opname);
return -1;
}
if ((unsigned) val > 255)
{
as_bad (_("register number (%ld) for `%s' is out of range"),
val, opname);
return -1;
}
/* Remove the last argument, which is now part of the opcode. */
free (arg_strings[1]);
arg_strings[1] = 0;
*pnum_args = 1;
/* Translate the opcode. */
new_opname = (char *) xmalloc (8);
sprintf (new_opname, "%s%cur%u", (has_underbar ? "_" : ""),
opname[0], (unsigned) val);
free (*popname);
*popname = new_opname;
}
return 0;
}
/* If the instruction is an idiom (i.e., a built-in macro), translate it.
Returns non-zero if an error was found. */
static int
xg_translate_idioms (popname, pnum_args, arg_strings)
char **popname;
int *pnum_args;
char **arg_strings;
{
char *opname = *popname;
bfd_boolean has_underbar = FALSE;
if (*opname == '_')
{
has_underbar = TRUE;
opname += 1;
}
if (strcmp (opname, "mov") == 0)
{
if (!has_underbar && code_density_available ())
xg_replace_opname (popname, "mov.n");
else
{
if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
return -1;
xg_replace_opname (popname, (has_underbar ? "_or" : "or"));
arg_strings[2] = (char *) xmalloc (strlen (arg_strings[1]) + 1);
strcpy (arg_strings[2], arg_strings[1]);
*pnum_args = 3;
}
return 0;
}
if (strcmp (opname, "bbsi.l") == 0)
{
if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
return -1;
xg_replace_opname (popname, (has_underbar ? "_bbsi" : "bbsi"));
if (target_big_endian)
xg_reverse_shift_count (&arg_strings[1]);
return 0;
}
if (strcmp (opname, "bbci.l") == 0)
{
if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
return -1;
xg_replace_opname (popname, (has_underbar ? "_bbci" : "bbci"));
if (target_big_endian)
xg_reverse_shift_count (&arg_strings[1]);
return 0;
}
if (strcmp (opname, "nop") == 0)
{
if (!has_underbar && code_density_available ())
xg_replace_opname (popname, "nop.n");
else
{
if (xg_check_num_args (pnum_args, 0, opname, arg_strings))
return -1;
xg_replace_opname (popname, (has_underbar ? "_or" : "or"));
arg_strings[0] = (char *) xmalloc (3);
arg_strings[1] = (char *) xmalloc (3);
arg_strings[2] = (char *) xmalloc (3);
strcpy (arg_strings[0], "a1");
strcpy (arg_strings[1], "a1");
strcpy (arg_strings[2], "a1");
*pnum_args = 3;
}
return 0;
}
if ((opname[0] == 'r' || opname[0] == 'w')
&& opname[1] == 'u'
&& opname[2] == 'r')
return xg_translate_sysreg_op (popname, pnum_args, arg_strings);
/* WIDENING DENSITY OPCODES
questionable relaxations (widening) from old "tai" idioms:
ADD.N --> ADD
BEQZ.N --> BEQZ
RET.N --> RET
RETW.N --> RETW
MOVI.N --> MOVI
MOV.N --> MOV
NOP.N --> NOP
Note: this incomplete list was imported to match the "tai"
behavior; other density opcodes are not handled.
The xtensa-relax code may know how to do these but it doesn't do
anything when these density opcodes appear inside a no-density
region. Somehow GAS should either print an error when that happens
or do the widening. The old "tai" behavior was to do the widening.
For now, I'll make it widen but print a warning.
FIXME: GAS needs to detect density opcodes inside no-density
regions and treat them as errors. This code should be removed
when that is done. */
if (use_generics ()
&& !has_underbar
&& density_supported
&& !code_density_available ())
{
if (strcmp (opname, "add.n") == 0)
xg_replace_opname (popname, "add");
else if (strcmp (opname, "beqz.n") == 0)
xg_replace_opname (popname, "beqz");
else if (strcmp (opname, "ret.n") == 0)
xg_replace_opname (popname, "ret");
else if (strcmp (opname, "retw.n") == 0)
xg_replace_opname (popname, "retw");
else if (strcmp (opname, "movi.n") == 0)
xg_replace_opname (popname, "movi");
else if (strcmp (opname, "mov.n") == 0)
{
if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
return -1;
xg_replace_opname (popname, "or");
arg_strings[2] = (char *) xmalloc (strlen (arg_strings[1]) + 1);
strcpy (arg_strings[2], arg_strings[1]);
*pnum_args = 3;
}
else if (strcmp (opname, "nop.n") == 0)
{
if (xg_check_num_args (pnum_args, 0, opname, arg_strings))
return -1;
xg_replace_opname (popname, "or");
arg_strings[0] = (char *) xmalloc (3);
arg_strings[1] = (char *) xmalloc (3);
arg_strings[2] = (char *) xmalloc (3);
strcpy (arg_strings[0], "a1");
strcpy (arg_strings[1], "a1");
strcpy (arg_strings[2], "a1");
*pnum_args = 3;
}
}
return 0;
}
/* Functions for dealing with the Xtensa ISA. */
/* Return true if the given operand is an immed or target instruction,
i.e., has a reloc associated with it. Currently, this is only true
if the operand kind is "i, "l" or "L". */
static bfd_boolean
operand_is_immed (opnd)
xtensa_operand opnd;
{
const char *opkind = xtensa_operand_kind (opnd);
if (opkind[0] == '\0' || opkind[1] != '\0')
return FALSE;
switch (opkind[0])
{
case 'i':
case 'l':
case 'L':
return TRUE;
}
return FALSE;
}
/* Return true if the given operand is a pc-relative label. This is
true for "l", "L", and "r" operand kinds. */
bfd_boolean
operand_is_pcrel_label (opnd)
xtensa_operand opnd;
{
const char *opkind = xtensa_operand_kind (opnd);
if (opkind[0] == '\0' || opkind[1] != '\0')
return FALSE;
switch (opkind[0])
{
case 'r':
case 'l':
case 'L':
return TRUE;
}
return FALSE;
}
/* Currently the assembler only allows us to use a single target per
fragment. Because of this, only one operand for a given
instruction may be symbolic. If there is an operand of kind "lrL",
the last one is chosen. Otherwise, the result is the number of the
last operand of type "i", and if there are none of those, we fail
and return -1. */
int
get_relaxable_immed (opcode)
xtensa_opcode opcode;
{
int last_immed = -1;
int noperands, opi;
xtensa_operand operand;
if (opcode == XTENSA_UNDEFINED)
return -1;
noperands = xtensa_num_operands (xtensa_default_isa, opcode);
for (opi = noperands - 1; opi >= 0; opi--)
{
operand = xtensa_get_operand (xtensa_default_isa, opcode, opi);
if (operand_is_pcrel_label (operand))
return opi;
if (last_immed == -1 && operand_is_immed (operand))
last_immed = opi;
}
return last_immed;
}
xtensa_opcode
get_opcode_from_buf (buf)
const char *buf;
{
static xtensa_insnbuf insnbuf = NULL;
xtensa_opcode opcode;
xtensa_isa isa = xtensa_default_isa;
if (!insnbuf)
insnbuf = xtensa_insnbuf_alloc (isa);
xtensa_insnbuf_from_chars (isa, insnbuf, buf);
opcode = xtensa_decode_insn (isa, insnbuf);
return opcode;
}
static bfd_boolean
is_direct_call_opcode (opcode)
xtensa_opcode opcode;
{
if (opcode == XTENSA_UNDEFINED)
return FALSE;
return (opcode == xtensa_call0_opcode
|| opcode == xtensa_call4_opcode
|| opcode == xtensa_call8_opcode
|| opcode == xtensa_call12_opcode);
}
static bfd_boolean
is_call_opcode (opcode)
xtensa_opcode opcode;
{
if (is_direct_call_opcode (opcode))
return TRUE;
if (opcode == XTENSA_UNDEFINED)
return FALSE;
return (opcode == xtensa_callx0_opcode
|| opcode == xtensa_callx4_opcode
|| opcode == xtensa_callx8_opcode
|| opcode == xtensa_callx12_opcode);
}
/* Return true if the opcode is an entry opcode. This is used because
"entry" adds an implicit ".align 4" and also the entry instruction
has an extra check for an operand value. */
static bfd_boolean
is_entry_opcode (opcode)
xtensa_opcode opcode;
{
if (opcode == XTENSA_UNDEFINED)
return FALSE;
return (opcode == xtensa_entry_opcode);
}
/* Return true if it is one of the loop opcodes. Loops are special
because they need automatic alignment and they have a relaxation so
complex that we hard-coded it. */
static bfd_boolean
is_loop_opcode (opcode)
xtensa_opcode opcode;
{
if (opcode == XTENSA_UNDEFINED)
return FALSE;
return (opcode == xtensa_loop_opcode
|| opcode == xtensa_loopnez_opcode
|| opcode == xtensa_loopgtz_opcode);
}
static bfd_boolean
is_the_loop_opcode (opcode)
xtensa_opcode opcode;
{
if (opcode == XTENSA_UNDEFINED)
return FALSE;
return (opcode == xtensa_loop_opcode);
}
static bfd_boolean
is_jx_opcode (opcode)
xtensa_opcode opcode;
{
if (opcode == XTENSA_UNDEFINED)
return FALSE;
return (opcode == xtensa_jx_opcode);
}
/* Return true if the opcode is a retw or retw.n.
Needed to add nops to avoid a hardware interlock issue. */
static bfd_boolean
is_windowed_return_opcode (opcode)
xtensa_opcode opcode;
{
if (opcode == XTENSA_UNDEFINED)
return FALSE;
return (opcode == xtensa_retw_opcode || opcode == xtensa_retw_n_opcode);
}
/* Return true if the opcode type is "l" and the opcode is NOT a jump. */
static bfd_boolean
is_conditional_branch_opcode (opcode)
xtensa_opcode opcode;
{
xtensa_isa isa = xtensa_default_isa;
int num_ops, i;
if (opcode == xtensa_j_opcode && opcode != XTENSA_UNDEFINED)
return FALSE;
num_ops = xtensa_num_operands (isa, opcode);
for (i = 0; i < num_ops; i++)
{
xtensa_operand operand = xtensa_get_operand (isa, opcode, i);
if (strcmp (xtensa_operand_kind (operand), "l") == 0)
return TRUE;
}
return FALSE;
}
/* Return true if the given opcode is a conditional branch
instruction, i.e., currently this is true if the instruction
is a jx or has an operand with 'l' type and is not a loop. */
bfd_boolean
is_branch_or_jump_opcode (opcode)
xtensa_opcode opcode;
{
int opn, op_count;
if (opcode == XTENSA_UNDEFINED)
return FALSE;
if (is_loop_opcode (opcode))
return FALSE;
if (is_jx_opcode (opcode))
return TRUE;
op_count = xtensa_num_operands (xtensa_default_isa, opcode);
for (opn = 0; opn < op_count; opn++)
{
xtensa_operand opnd =
xtensa_get_operand (xtensa_default_isa, opcode, opn);
const char *opkind = xtensa_operand_kind (opnd);
if (opkind && opkind[0] == 'l' && opkind[1] == '\0')
return TRUE;
}
return FALSE;
}
/* Convert from operand numbers to BFD relocation type code.
Return BFD_RELOC_NONE on failure. */
bfd_reloc_code_real_type
opnum_to_reloc (opnum)
int opnum;
{
switch (opnum)
{
case 0:
return BFD_RELOC_XTENSA_OP0;
case 1:
return BFD_RELOC_XTENSA_OP1;
case 2:
return BFD_RELOC_XTENSA_OP2;
default:
break;
}
return BFD_RELOC_NONE;
}
/* Convert from BFD relocation type code to operand number.
Return -1 on failure. */
int
reloc_to_opnum (reloc)
bfd_reloc_code_real_type reloc;
{
switch (reloc)
{
case BFD_RELOC_XTENSA_OP0:
return 0;
case BFD_RELOC_XTENSA_OP1:
return 1;
case BFD_RELOC_XTENSA_OP2:
return 2;
default:
break;
}
return -1;
}
static void
xtensa_insnbuf_set_operand (insnbuf, opcode, operand, value, file, line)
xtensa_insnbuf insnbuf;
xtensa_opcode opcode;
xtensa_operand operand;
int32 value;
const char *file;
unsigned int line;
{
xtensa_encode_result encode_result;
uint32 valbuf = value;
encode_result = xtensa_operand_encode (operand, &valbuf);
switch (encode_result)
{
case xtensa_encode_result_ok:
break;
case xtensa_encode_result_align:
as_bad_where ((char *) file, line,
_("operand %d not properly aligned for '%s'"),
value, xtensa_opcode_name (xtensa_default_isa, opcode));
break;
case xtensa_encode_result_not_in_table:
as_bad_where ((char *) file, line,
_("operand %d not in immediate table for '%s'"),
value, xtensa_opcode_name (xtensa_default_isa, opcode));
break;
case xtensa_encode_result_too_high:
as_bad_where ((char *) file, line,
_("operand %d too large for '%s'"), value,
xtensa_opcode_name (xtensa_default_isa, opcode));
break;
case xtensa_encode_result_too_low:
as_bad_where ((char *) file, line,
_("operand %d too small for '%s'"), value,
xtensa_opcode_name (xtensa_default_isa, opcode));
break;
case xtensa_encode_result_not_ok:
as_bad_where ((char *) file, line,
_("operand %d is invalid for '%s'"), value,
xtensa_opcode_name (xtensa_default_isa, opcode));
break;
default:
abort ();
}
xtensa_operand_set_field (operand, insnbuf, valbuf);
}
static uint32
xtensa_insnbuf_get_operand (insnbuf, opcode, opnum)
xtensa_insnbuf insnbuf;
xtensa_opcode opcode;
int opnum;
{
xtensa_operand op = xtensa_get_operand (xtensa_default_isa, opcode, opnum);
return xtensa_operand_decode (op, xtensa_operand_get_field (op, insnbuf));
}
static void
xtensa_insnbuf_set_immediate_field (opcode, insnbuf, value, file, line)
xtensa_opcode opcode;
xtensa_insnbuf insnbuf;
int32 value;
const char *file;
unsigned int line;
{
xtensa_isa isa = xtensa_default_isa;
int last_opnd = xtensa_num_operands (isa, opcode) - 1;
xtensa_operand operand = xtensa_get_operand (isa, opcode, last_opnd);
xtensa_insnbuf_set_operand (insnbuf, opcode, operand, value, file, line);
}
static bfd_boolean
is_negatable_branch (insn)
TInsn *insn;
{
xtensa_isa isa = xtensa_default_isa;
int i;
int num_ops = xtensa_num_operands (isa, insn->opcode);
for (i = 0; i < num_ops; i++)
{
xtensa_operand opnd = xtensa_get_operand (isa, insn->opcode, i);
char *kind = xtensa_operand_kind (opnd);
if (strlen (kind) == 1 && *kind == 'l')
return TRUE;
}
return FALSE;
}
/* Various Other Internal Functions. */
static bfd_boolean
is_unique_insn_expansion (r)
TransitionRule *r;
{
if (!r->to_instr || r->to_instr->next != NULL)
return FALSE;
if (r->to_instr->typ != INSTR_INSTR)
return FALSE;
return TRUE;
}
static int
xg_get_insn_size (insn)
TInsn *insn;
{
assert (insn->insn_type == ITYPE_INSN);
return xtensa_insn_length (xtensa_default_isa, insn->opcode);
}
static int
xg_get_build_instr_size (insn)
BuildInstr *insn;
{
assert (insn->typ == INSTR_INSTR);
return xtensa_insn_length (xtensa_default_isa, insn->opcode);
}
bfd_boolean
xg_is_narrow_insn (insn)
TInsn *insn;
{
TransitionTable *table = xg_build_widen_table ();
TransitionList *l;
int num_match = 0;
assert (insn->insn_type == ITYPE_INSN);
assert (insn->opcode < table->num_opcodes);
for (l = table->table[insn->opcode]; l != NULL; l = l->next)
{
TransitionRule *rule = l->rule;
if (xg_instruction_matches_rule (insn, rule)
&& is_unique_insn_expansion (rule))
{
/* It only generates one instruction... */
assert (insn->insn_type == ITYPE_INSN);
/* ...and it is a larger instruction. */
if (xg_get_insn_size (insn)
< xg_get_build_instr_size (rule->to_instr))
{
num_match++;
if (num_match > 1)
return FALSE;
}
}
}
return (num_match == 1);
}
bfd_boolean
xg_is_single_relaxable_insn (insn)
TInsn *insn;
{
TransitionTable *table = xg_build_widen_table ();
TransitionList *l;
int num_match = 0;
assert (insn->insn_type == ITYPE_INSN);
assert (insn->opcode < table->num_opcodes);
for (l = table->table[insn->opcode]; l != NULL; l = l->next)
{
TransitionRule *rule = l->rule;
if (xg_instruction_matches_rule (insn, rule)
&& is_unique_insn_expansion (rule))
{
assert (insn->insn_type == ITYPE_INSN);
/* ... and it is a larger instruction. */
if (xg_get_insn_size (insn)
<= xg_get_build_instr_size (rule->to_instr))
{
num_match++;
if (num_match > 1)
return FALSE;
}
}
}
return (num_match == 1);
}
/* Return the largest size instruction that this instruction can
expand to. Currently, in all cases, this is 3 bytes. Of course we
could just calculate this once and generate a table. */
int
xg_get_max_narrow_insn_size (opcode)
xtensa_opcode opcode;
{
/* Go ahead and compute it, but it better be 3. */
TransitionTable *table = xg_build_widen_table ();
TransitionList *l;
int old_size = xtensa_insn_length (xtensa_default_isa, opcode);
assert (opcode < table->num_opcodes);
/* Actually we can do better. Check to see of Only one applies. */
for (l = table->table[opcode]; l != NULL; l = l->next)
{
TransitionRule *rule = l->rule;
/* If it only generates one instruction. */
if (is_unique_insn_expansion (rule))
{
int new_size = xtensa_insn_length (xtensa_default_isa,
rule->to_instr->opcode);
if (new_size > old_size)
{
assert (new_size == 3);
return 3;
}
}
}
return old_size;
}
/* Return the maximum number of bytes this opcode can expand to. */
int
xg_get_max_insn_widen_size (opcode)
xtensa_opcode opcode;
{
TransitionTable *table = xg_build_widen_table ();
TransitionList *l;
int max_size = xtensa_insn_length (xtensa_default_isa, opcode);
assert (opcode < table->num_opcodes);
for (l = table->table[opcode]; l != NULL; l = l->next)
{
TransitionRule *rule = l->rule;
BuildInstr *build_list;
int this_size = 0;
if (!rule)
continue;
build_list = rule->to_instr;
if (is_unique_insn_expansion (rule))
{
assert (build_list->typ == INSTR_INSTR);
this_size = xg_get_max_insn_widen_size (build_list->opcode);
}
else
for (; build_list != NULL; build_list = build_list->next)
{
switch (build_list->typ)
{
case INSTR_INSTR:
this_size += xtensa_insn_length (xtensa_default_isa,
build_list->opcode);
break;
case INSTR_LITERAL_DEF:
case INSTR_LABEL_DEF:
default:
break;
}
}
if (this_size > max_size)
max_size = this_size;
}
return max_size;
}
/* Return the maximum number of literal bytes this opcode can generate. */
int
xg_get_max_insn_widen_literal_size (opcode)
xtensa_opcode opcode;
{
TransitionTable *table = xg_build_widen_table ();
TransitionList *l;
int max_size = 0;
assert (opcode < table->num_opcodes);
for (l = table->table[opcode]; l != NULL; l = l->next)
{
TransitionRule *rule = l->rule;
BuildInstr *build_list;
int this_size = 0;
if (!rule)
continue;
build_list = rule->to_instr;
if (is_unique_insn_expansion (rule))
{
assert (build_list->typ == INSTR_INSTR);
this_size = xg_get_max_insn_widen_literal_size (build_list->opcode);
}
else
for (; build_list != NULL; build_list = build_list->next)
{
switch (build_list->typ)
{
case INSTR_LITERAL_DEF:
/* hard coded 4-byte literal. */
this_size += 4;
break;
case INSTR_INSTR:
case INSTR_LABEL_DEF:
default:
break;
}
}
if (this_size > max_size)
max_size = this_size;
}
return max_size;
}
bfd_boolean
xg_is_relaxable_insn (insn, lateral_steps)
TInsn *insn;
int lateral_steps;
{
int steps_taken = 0;
TransitionTable *table = xg_build_widen_table ();
TransitionList *l;
assert (insn->insn_type == ITYPE_INSN);
assert (insn->opcode < table->num_opcodes);
for (l = table->table[insn->opcode]; l != NULL; l = l->next)
{
TransitionRule *rule = l->rule;
if (xg_instruction_matches_rule (insn, rule))
{
if (steps_taken == lateral_steps)
return TRUE;
steps_taken++;
}
}
return FALSE;
}
static symbolS *
get_special_literal_symbol ()
{
static symbolS *sym = NULL;
if (sym == NULL)
sym = symbol_find_or_make ("SPECIAL_LITERAL0\001");
return sym;
}
static symbolS *
get_special_label_symbol ()
{
static symbolS *sym = NULL;
if (sym == NULL)
sym = symbol_find_or_make ("SPECIAL_LABEL0\001");
return sym;
}
/* Return true on success. */
bfd_boolean
xg_build_to_insn (targ, insn, bi)
TInsn *targ;
TInsn *insn;
BuildInstr *bi;
{
BuildOp *op;
symbolS *sym;
memset (targ, 0, sizeof (TInsn));
switch (bi->typ)
{
case INSTR_INSTR:
op = bi->ops;
targ->opcode = bi->opcode;
targ->insn_type = ITYPE_INSN;
targ->is_specific_opcode = FALSE;
for (; op != NULL; op = op->next)
{
int op_num = op->op_num;
int op_data = op->op_data;
assert (op->op_num < MAX_INSN_ARGS);
if (targ->ntok <= op_num)
targ->ntok = op_num + 1;
switch (op->typ)
{
case OP_CONSTANT:
set_expr_const (&targ->tok[op_num], op_data);
break;
case OP_OPERAND:
assert (op_data < insn->ntok);
copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
break;
case OP_LITERAL:
sym = get_special_literal_symbol ();
set_expr_symbol_offset (&targ->tok[op_num], sym, 0);
break;
case OP_LABEL:
sym = get_special_label_symbol ();
set_expr_symbol_offset (&targ->tok[op_num], sym, 0);
break;
default:
/* currently handles:
OP_OPERAND_LOW8
OP_OPERAND_HI24S
OP_OPERAND_F32MINUS */
if (xg_has_userdef_op_fn (op->typ))
{
assert (op_data < insn->ntok);
if (expr_is_const (&insn->tok[op_data]))
{
long val;
copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
val = xg_apply_userdef_op_fn (op->typ,
targ->tok[op_num].
X_add_number);
targ->tok[op_num].X_add_number = val;
}
else
return FALSE; /* We cannot use a relocation for this. */
break;
}
assert (0);
break;
}
}
break;
case INSTR_LITERAL_DEF:
op = bi->ops;
targ->opcode = XTENSA_UNDEFINED;
targ->insn_type = ITYPE_LITERAL;
targ->is_specific_opcode = FALSE;
for (; op != NULL; op = op->next)
{
int op_num = op->op_num;
int op_data = op->op_data;
assert (op->op_num < MAX_INSN_ARGS);
if (targ->ntok <= op_num)
targ->ntok = op_num + 1;
switch (op->typ)
{
case OP_OPERAND:
assert (op_data < insn->ntok);
copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
break;
case OP_LITERAL:
case OP_CONSTANT:
case OP_LABEL:
default:
assert (0);
break;
}
}
break;
case INSTR_LABEL_DEF:
op = bi->ops;
targ->opcode = XTENSA_UNDEFINED;
targ->insn_type = ITYPE_LABEL;
targ->is_specific_opcode = FALSE;
/* Literal with no ops. is a label? */
assert (op == NULL);
break;
default:
assert (0);
}
return TRUE;
}
/* Return true on success. */
bfd_boolean
xg_build_to_stack (istack, insn, bi)
IStack *istack;
TInsn *insn;
BuildInstr *bi;
{
for (; bi != NULL; bi = bi->next)
{
TInsn *next_insn = istack_push_space (istack);
if (!xg_build_to_insn (next_insn, insn, bi))
return FALSE;
}
return TRUE;
}
/* Return true on valid expansion. */
bfd_boolean
xg_expand_to_stack (istack, insn, lateral_steps)
IStack *istack;
TInsn *insn;
int lateral_steps;
{
int stack_size = istack->ninsn;
int steps_taken = 0;
TransitionTable *table = xg_build_widen_table ();
TransitionList *l;
assert (insn->insn_type == ITYPE_INSN);
assert (insn->opcode < table->num_opcodes);
for (l = table->table[insn->opcode]; l != NULL; l = l->next)
{
TransitionRule *rule = l->rule;
if (xg_instruction_matches_rule (insn, rule))
{
if (lateral_steps == steps_taken)
{
int i;
/* This is it. Expand the rule to the stack. */
if (!xg_build_to_stack (istack, insn, rule->to_instr))
return FALSE;
/* Check to see if it fits. */
for (i = stack_size; i < istack->ninsn; i++)
{
TInsn *insn = &istack->insn[i];
if (insn->insn_type == ITYPE_INSN
&& !tinsn_has_symbolic_operands (insn)
&& !xg_immeds_fit (insn))
{
istack->ninsn = stack_size;
return FALSE;
}
}
return TRUE;
}
steps_taken++;
}
}
return FALSE;
}
bfd_boolean
xg_expand_narrow (targ, insn)
TInsn *targ;
TInsn *insn;
{
TransitionTable *table = xg_build_widen_table ();
TransitionList *l;
assert (insn->insn_type == ITYPE_INSN);
assert (insn->opcode < table->num_opcodes);
for (l = table->table[insn->opcode]; l != NULL; l = l->next)
{
TransitionRule *rule = l->rule;
if (xg_instruction_matches_rule (insn, rule)
&& is_unique_insn_expansion (rule))
{
/* Is it a larger instruction? */
if (xg_get_insn_size (insn)
<= xg_get_build_instr_size (rule->to_instr))
{
xg_build_to_insn (targ, insn, rule->to_instr);
return FALSE;
}
}
}
return TRUE;
}
/* Assumes: All immeds are constants. Check that all constants fit
into their immeds; return false if not. */
static bfd_boolean
xg_immeds_fit (insn)
const TInsn *insn;
{
int i;
int n = insn->ntok;
assert (insn->insn_type == ITYPE_INSN);
for (i = 0; i < n; ++i)
{
const expressionS *expr = &insn->tok[i];
xtensa_operand opnd = xtensa_get_operand (xtensa_default_isa,
insn->opcode, i);
if (!operand_is_immed (opnd))
continue;
switch (expr->X_op)
{
case O_register:
case O_constant:
{
if (xg_check_operand (expr->X_add_number, opnd))
return FALSE;
}
break;
default:
/* The symbol should have a fixup associated with it. */
assert (FALSE);
break;
}
}
return TRUE;
}
/* This should only be called after we have an initial
estimate of the addresses. */
static bfd_boolean
xg_symbolic_immeds_fit (insn, pc_seg, pc_frag, pc_offset, stretch)
const TInsn *insn;
segT pc_seg;
fragS *pc_frag;
offsetT pc_offset;
long stretch;
{
symbolS *symbolP;
offsetT target, pc, new_offset;
int i;
int n = insn->ntok;
assert (insn->insn_type == ITYPE_INSN);
for (i = 0; i < n; ++i)
{
const expressionS *expr = &insn->tok[i];
xtensa_operand opnd = xtensa_get_operand (xtensa_default_isa,
insn->opcode, i);
if (!operand_is_immed (opnd))
continue;
switch (expr->X_op)
{
case O_register:
case O_constant:
if (xg_check_operand (expr->X_add_number, opnd))
return FALSE;
break;
case O_symbol:
/* We only allow symbols for pc-relative stuff.
If pc_frag == 0, then we don't have frag locations yet. */
if (pc_frag == 0)
return FALSE;
/* If it is PC-relative and the symbol is in the same segment as
the PC.... */
if (!xtensa_operand_isPCRelative (opnd)
|| S_GET_SEGMENT (expr->X_add_symbol) != pc_seg)
return FALSE;
symbolP = expr->X_add_symbol;
target = S_GET_VALUE (symbolP) + expr->X_add_number;
pc = pc_frag->fr_address + pc_offset;
/* If frag has yet to be reached on this pass, assume it
will move by STRETCH just as we did. If this is not so,
it will be because some frag between grows, and that will
force another pass. Beware zero-length frags. There
should be a faster way to do this. */
if (stretch && is_dnrange (pc_frag, symbolP, stretch))
target += stretch;
new_offset = xtensa_operand_do_reloc (opnd, target, pc);
if (xg_check_operand (new_offset, opnd))
return FALSE;
break;
default:
/* The symbol should have a fixup associated with it. */
return FALSE;
}
}
return TRUE;
}
/* This will check to see if the value can be converted into the
operand type. It will return true if it does not fit. */
static bfd_boolean
xg_check_operand (value, operand)
int32 value;
xtensa_operand operand;
{
uint32 valbuf = value;
return (xtensa_operand_encode (operand, &valbuf) != xtensa_encode_result_ok);
}
/* Check if a symbol is pointing to somewhere after
the start frag, given that the segment has stretched
by stretch during relaxation.
This is more complicated than it might appear at first blush
because of the stretching that goes on. Here is how the check
works:
If the symbol and the frag are in the same segment, then
the symbol could be down range. Note that this function
assumes that start_frag is in now_seg.
If the symbol is pointing to a frag with an address greater than
than the start_frag's address, then it _could_ be down range.
The problem comes because target_frag may or may not have had
stretch bytes added to its address already, depending on if it is
before or after start frag. (And if we knew that, then we wouldn't
need this function.) start_frag has definitely already had stretch
bytes added to its address.
If target_frag's address hasn't been adjusted yet, then to
determine if it comes after start_frag, we need to subtract
stretch from start_frag's address.
If target_frag's address has been adjusted, then it might have
been adjusted such that it comes after start_frag's address minus
stretch bytes.
So, in that case, we scan for it down stream to within
stretch bytes. We could search to the end of the fr_chain, but
that ends up taking too much time (over a minute on some gnu
tests). */
int
is_dnrange (start_frag, sym, stretch)
fragS *start_frag;
symbolS *sym;
long stretch;
{
if (S_GET_SEGMENT (sym) == now_seg)
{
fragS *cur_frag = symbol_get_frag (sym);
if (cur_frag->fr_address >= start_frag->fr_address - stretch)
{
int distance = stretch;
while (cur_frag && distance >= 0)
{
distance -= cur_frag->fr_fix;
if (cur_frag == start_frag)
return 0;
cur_frag = cur_frag->fr_next;
}
return 1;
}
}
return 0;
}
/* Relax the assembly instruction at least "min_steps".
Return the number of steps taken. */
int
xg_assembly_relax (istack, insn, pc_seg, pc_frag, pc_offset, min_steps,
stretch)
IStack *istack;
TInsn *insn;
segT pc_seg;
fragS *pc_frag; /* If pc_frag == 0, then no pc-relative. */
offsetT pc_offset; /* Offset in fragment. */
int min_steps; /* Minimum number of conversion steps. */
long stretch; /* Number of bytes stretched so far. */
{
int steps_taken = 0;
/* assert (has no symbolic operands)
Some of its immeds don't fit.
Try to build a relaxed version.
This may go through a couple of stages
of single instruction transformations before
we get there. */
TInsn single_target;
TInsn current_insn;
int lateral_steps = 0;
int istack_size = istack->ninsn;
if (xg_symbolic_immeds_fit (insn, pc_seg, pc_frag, pc_offset, stretch)
&& steps_taken >= min_steps)
{
istack_push (istack, insn);
return steps_taken;
}
tinsn_copy (¤t_insn, insn);
/* Walk through all of the single instruction expansions. */
while (xg_is_single_relaxable_insn (¤t_insn))
{
int error_val = xg_expand_narrow (&single_target, ¤t_insn);
assert (!error_val);
if (xg_symbolic_immeds_fit (&single_target, pc_seg, pc_frag, pc_offset,
stretch))
{
steps_taken++;
if (steps_taken >= min_steps)
{
istack_push (istack, &single_target);
return steps_taken;
}
}
tinsn_copy (¤t_insn, &single_target);
}
/* Now check for a multi-instruction expansion. */
while (xg_is_relaxable_insn (¤t_insn, lateral_steps))
{
if (xg_symbolic_immeds_fit (¤t_insn, pc_seg, pc_frag, pc_offset,
stretch))
{
if (steps_taken >= min_steps)
{
istack_push (istack, ¤t_insn);
return steps_taken;
}
}
steps_taken++;
if (xg_expand_to_stack (istack, ¤t_insn, lateral_steps))
{
if (steps_taken >= min_steps)
return steps_taken;
}
lateral_steps++;
istack->ninsn = istack_size;
}
/* It's not going to work -- use the original. */
istack_push (istack, insn);
return steps_taken;
}
static void
xg_force_frag_space (size)
int size;
{
/* This may have the side effect of creating a new fragment for the
space to go into. I just do not like the name of the "frag"
functions. */
frag_grow (size);
}
void
xg_finish_frag (last_insn, state, max_growth, is_insn)
char *last_insn;
enum xtensa_relax_statesE state;
int max_growth;
bfd_boolean is_insn;
{
/* Finish off this fragment so that it has at LEAST the desired
max_growth. If it doesn't fit in this fragment, close this one
and start a new one. In either case, return a pointer to the
beginning of the growth area. */
fragS *old_frag;
xg_force_frag_space (max_growth);
old_frag = frag_now;
frag_now->fr_opcode = last_insn;
if (is_insn)
frag_now->tc_frag_data.is_insn = TRUE;
frag_var (rs_machine_dependent, max_growth, max_growth,
state, frag_now->fr_symbol, frag_now->fr_offset, last_insn);
/* Just to make sure that we did not split it up. */
assert (old_frag->fr_next == frag_now);
}
static bfd_boolean
is_branch_jmp_to_next (insn, fragP)
TInsn *insn;
fragS *fragP;
{
xtensa_isa isa = xtensa_default_isa;
int i;
int num_ops = xtensa_num_operands (isa, insn->opcode);
int target_op = -1;
symbolS *sym;
fragS *target_frag;
if (is_loop_opcode (insn->opcode))
return FALSE;
for (i = 0; i < num_ops; i++)
{
xtensa_operand opnd = xtensa_get_operand (isa, insn->opcode, i);
char *kind = xtensa_operand_kind (opnd);
if (strlen (kind) == 1 && *kind == 'l')
{
target_op = i;
break;
}
}
if (target_op == -1)
return FALSE;
if (insn->ntok <= target_op)
return FALSE;
if (insn->tok[target_op].X_op != O_symbol)
return FALSE;
sym = insn->tok[target_op].X_add_symbol;
if (sym == NULL)
return FALSE;
if (insn->tok[target_op].X_add_number != 0)
return FALSE;
target_frag = symbol_get_frag (sym);
if (target_frag == NULL)
return FALSE;
if (is_next_frag_target (fragP->fr_next, target_frag)
&& S_GET_VALUE (sym) == target_frag->fr_address)
return TRUE;
return FALSE;
}
static void
xg_add_branch_and_loop_targets (insn)
TInsn *insn;
{
xtensa_isa isa = xtensa_default_isa;
int num_ops = xtensa_num_operands (isa, insn->opcode);
if (is_loop_opcode (insn->opcode))
{
int i = 1;
xtensa_operand opnd = xtensa_get_operand (isa, insn->opcode, i);
char *kind = xtensa_operand_kind (opnd);
if (strlen (kind) == 1 && *kind == 'l')
if (insn->tok[i].X_op == O_symbol)
symbol_get_tc (insn->tok[i].X_add_symbol)->is_loop_target = TRUE;
return;
}
/* Currently, we do not add branch targets. This is an optimization
for later that tries to align only branch targets, not just any
label in a text section. */
if (align_only_targets)
{
int i;
for (i = 0; i < insn->ntok && i < num_ops; i++)
{
xtensa_operand opnd = xtensa_get_operand (isa, insn->opcode, i);
char *kind = xtensa_operand_kind (opnd);
if (strlen (kind) == 1 && *kind == 'l'
&& insn->tok[i].X_op == O_symbol)
{
symbolS *sym = insn->tok[i].X_add_symbol;
symbol_get_tc (sym)->is_branch_target = TRUE;
if (S_IS_DEFINED (sym))
symbol_get_frag (sym)->tc_frag_data.is_branch_target = TRUE;
}
}
}
}
/* Return the transition rule that matches or NULL if none matches. */
bfd_boolean
xg_instruction_matches_rule (insn, rule)
TInsn *insn;
TransitionRule *rule;
{
PreconditionList *condition_l;
if (rule->opcode != insn->opcode)
return FALSE;
for (condition_l = rule->conditions;
condition_l != NULL;
condition_l = condition_l->next)
{
expressionS *exp1;
expressionS *exp2;
Precondition *cond = condition_l->precond;
switch (cond->typ)
{
case OP_CONSTANT:
/* The expression must be the constant. */
assert (cond->op_num < insn->ntok);
exp1 = &insn->tok[cond->op_num];
if (!expr_is_const (exp1))
return FALSE;
switch (cond->cmp)
{
case OP_EQUAL:
if (get_expr_const (exp1) != cond->op_data)
return FALSE;
break;
case OP_NOTEQUAL:
if (get_expr_const (exp1) == cond->op_data)
return FALSE;
break;
}
break;
case OP_OPERAND:
assert (cond->op_num < insn->ntok);
assert (cond->op_data < insn->ntok);
exp1 = &insn->tok[cond->op_num];
exp2 = &insn->tok[cond->op_data];
switch (cond->cmp)
{
case OP_EQUAL:
if (!expr_is_equal (exp1, exp2))
return FALSE;
break;
case OP_NOTEQUAL:
if (expr_is_equal (exp1, exp2))
return FALSE;
break;
}
break;
case OP_LITERAL:
case OP_LABEL:
default:
return FALSE;
}
}
return TRUE;
}
TransitionRule *
xg_instruction_match (insn)
TInsn *insn;
{
TransitionTable *table = xg_build_simplify_table ();
TransitionList *l;
assert (insn->opcode < table->num_opcodes);
/* Walk through all of the possible transitions. */
for (l = table->table[insn->opcode]; l != NULL; l = l->next)
{
TransitionRule *rule = l->rule;
if (xg_instruction_matches_rule (insn, rule))
return rule;
}
return NULL;
}
/* Return false if no error. */
bfd_boolean
xg_build_token_insn (instr_spec, old_insn, new_insn)
BuildInstr *instr_spec;
TInsn *old_insn;
TInsn *new_insn;
{
int num_ops = 0;
BuildOp *b_op;
switch (instr_spec->typ)
{
case INSTR_INSTR:
new_insn->insn_type = ITYPE_INSN;
new_insn->opcode = instr_spec->opcode;
new_insn->is_specific_opcode = FALSE;
break;
case INSTR_LITERAL_DEF:
new_insn->insn_type = ITYPE_LITERAL;
new_insn->opcode = XTENSA_UNDEFINED;
new_insn->is_specific_opcode = FALSE;
break;
case INSTR_LABEL_DEF:
as_bad (_("INSTR_LABEL_DEF not supported yet"));
break;
}
for (b_op = instr_spec->ops; b_op != NULL; b_op = b_op->next)
{
expressionS *exp;
const expressionS *src_exp;
num_ops++;
switch (b_op->typ)
{
case OP_CONSTANT:
/* The expression must be the constant. */
assert (b_op->op_num < MAX_INSN_ARGS);
exp = &new_insn->tok[b_op->op_num];
set_expr_const (exp, b_op->op_data);
break;
case OP_OPERAND:
assert (b_op->op_num < MAX_INSN_ARGS);
assert (b_op->op_data < (unsigned) old_insn->ntok);
src_exp = &old_insn->tok[b_op->op_data];
exp = &new_insn->tok[b_op->op_num];
copy_expr (exp, src_exp);
break;
case OP_LITERAL:
case OP_LABEL:
as_bad (_("can't handle generation of literal/labels yet"));
assert (0);
default:
as_bad (_("can't handle undefined OP TYPE"));
assert (0);
}
}
new_insn->ntok = num_ops;
return FALSE;
}
/* Return true if it was simplified. */
bfd_boolean
xg_simplify_insn (old_insn, new_insn)
TInsn *old_insn;
TInsn *new_insn;
{
TransitionRule *rule = xg_instruction_match (old_insn);
BuildInstr *insn_spec;
if (rule == NULL)
return FALSE;
insn_spec = rule->to_instr;
/* There should only be one. */
assert (insn_spec != NULL);
assert (insn_spec->next == NULL);
if (insn_spec->next != NULL)
return FALSE;
xg_build_token_insn (insn_spec, old_insn, new_insn);
return TRUE;
}
/* xg_expand_assembly_insn: (1) Simplify the instruction, i.e., l32i ->
l32i.n. (2) Check the number of operands. (3) Place the instruction
tokens into the stack or if we can relax it at assembly time, place
multiple instructions/literals onto the stack. Return false if no
error. */
static bfd_boolean
xg_expand_assembly_insn (istack, orig_insn)
IStack *istack;
TInsn *orig_insn;
{
int noperands;
TInsn new_insn;
memset (&new_insn, 0, sizeof (TInsn));
/* On return, we will be using the "use_tokens" with "use_ntok".
This will reduce things like addi to addi.n. */
if (code_density_available () && !orig_insn->is_specific_opcode)
{
if (xg_simplify_insn (orig_insn, &new_insn))
orig_insn = &new_insn;
}
noperands = xtensa_num_operands (xtensa_default_isa, orig_insn->opcode);
if (orig_insn->ntok < noperands)
{
as_bad (_("found %d operands for '%s': Expected %d"),
orig_insn->ntok,
xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode),
noperands);
return TRUE;
}
if (orig_insn->ntok > noperands)
as_warn (_("found too many (%d) operands for '%s': Expected %d"),
orig_insn->ntok,
xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode),
noperands);
/* If there are not enough operands, we will assert above. If there
are too many, just cut out the extras here. */
orig_insn->ntok = noperands;
/* Cases:
Instructions with all constant immeds:
Assemble them and relax the instruction if possible.
Give error if not possible; no fixup needed.
Instructions with symbolic immeds:
Assemble them with a Fix up (that may cause instruction expansion).
Also close out the fragment if the fixup may cause instruction expansion.
There are some other special cases where we need alignment.
1) before certain instructions with required alignment (OPCODE_ALIGN)
2) before labels that have jumps (LABEL_ALIGN)
3) after call instructions (RETURN_ALIGN)
Multiple of these may be possible on the same fragment.
If so, make sure to satisfy the required alignment.
Then try to get the desired alignment. */
if (tinsn_has_invalid_symbolic_operands (orig_insn))
return TRUE;
if (orig_insn->is_specific_opcode || !can_relax ())
{
istack_push (istack, orig_insn);
return FALSE;
}
if (tinsn_has_symbolic_operands (orig_insn))
{
if (tinsn_has_complex_operands (orig_insn))
xg_assembly_relax (istack, orig_insn, 0, 0, 0, 0, 0);
else
istack_push (istack, orig_insn);
}
else
{
if (xg_immeds_fit (orig_insn))
istack_push (istack, orig_insn);
else
xg_assembly_relax (istack, orig_insn, 0, 0, 0, 0, 0);
}
#if 0
for (i = 0; i < istack->ninsn; i++)
{
if (xg_simplify_insn (&new_insn, &istack->insn[i]))
istack->insn[i] = new_insn;
}
#endif
return FALSE;
}
/* Currently all literals that are generated here are 32-bit L32R targets. */
symbolS *
xg_assemble_literal (insn)
/* const */ TInsn *insn;
{
emit_state state;
symbolS *lit_sym = NULL;
/* size = 4 for L32R. It could easily be larger when we move to
larger constants. Add a parameter later. */
offsetT litsize = 4;
offsetT litalign = 2; /* 2^2 = 4 */
expressionS saved_loc;
set_expr_symbol_offset (&saved_loc, frag_now->fr_symbol, frag_now_fix ());
assert (insn->insn_type == ITYPE_LITERAL);
assert (insn->ntok = 1); /* must be only one token here */
xtensa_switch_to_literal_fragment (&state);
/* Force a 4-byte align here. Note that this opens a new frag, so all
literals done with this function have a frag to themselves. That's
important for the way text section literals work. */
frag_align (litalign, 0, 0);
emit_expr (&insn->tok[0], litsize);
assert (frag_now->tc_frag_data.literal_frag == NULL);
frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg);
frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now);
lit_sym = frag_now->fr_symbol;
frag_now->tc_frag_data.is_literal = TRUE;
/* Go back. */
xtensa_restore_emit_state (&state);
return lit_sym;
}
static void
xg_assemble_literal_space (size)
/* const */ int size;
{
emit_state state;
/* We might have to do something about this alignment. It only
takes effect if something is placed here. */
offsetT litalign = 2; /* 2^2 = 4 */
fragS *lit_saved_frag;
expressionS saved_loc;
assert (size % 4 == 0);
set_expr_symbol_offset (&saved_loc, frag_now->fr_symbol, frag_now_fix ());
xtensa_switch_to_literal_fragment (&state);
/* Force a 4-byte align here. */
frag_align (litalign, 0, 0);
xg_force_frag_space (size);
lit_saved_frag = frag_now;
frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg);
frag_now->tc_frag_data.is_literal = TRUE;
frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now);
xg_finish_frag (0, RELAX_LITERAL, size, FALSE);
/* Go back. */
xtensa_restore_emit_state (&state);
frag_now->tc_frag_data.literal_frag = lit_saved_frag;
}
symbolS *
xtensa_create_literal_symbol (sec, frag)
segT sec;
fragS *frag;
{
static int lit_num = 0;
static char name[256];
symbolS *symbolP;
sprintf (name, ".L_lit_sym%d", lit_num);
/* Create a local symbol. If it is in a linkonce section, we have to
be careful to make sure that if it is used in a relocation that the
symbol will be in the output file. */
if (get_is_linkonce_section (stdoutput, sec))
{
symbolP = symbol_new (name, sec, 0, frag);
S_CLEAR_EXTERNAL (symbolP);
/* symbolP->local = 1; */
}
else
symbolP = symbol_new (name, sec, 0, frag);
xtensa_add_literal_sym (symbolP);
frag->tc_frag_data.is_literal = TRUE;
lit_num++;
return symbolP;
}
static void
xtensa_add_literal_sym (sym)
symbolS *sym;
{
sym_list *l;
l = (sym_list *) xmalloc (sizeof (sym_list));
l->sym = sym;
l->next = literal_syms;
literal_syms = l;
}
static void
xtensa_add_insn_label (sym)
symbolS *sym;
{
sym_list *l;
if (!free_insn_labels)
l = (sym_list *) xmalloc (sizeof (sym_list));
else
{
l = free_insn_labels;
free_insn_labels = l->next;
}
l->sym = sym;
l->next = insn_labels;
insn_labels = l;
}
static void
xtensa_clear_insn_labels (void)
{
sym_list **pl;
for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
;
*pl = insn_labels;
insn_labels = NULL;
}
/* Return true if the section flags are marked linkonce
or the name is .gnu.linkonce*. */
bfd_boolean
get_is_linkonce_section (abfd, sec)
bfd *abfd ATTRIBUTE_UNUSED;
segT sec;
{
flagword flags, link_once_flags;
flags = bfd_get_section_flags (abfd, sec);
link_once_flags = (flags & SEC_LINK_ONCE);
/* Flags might not be set yet. */
if (!link_once_flags)
{
static size_t len = sizeof ".gnu.linkonce.t.";
if (strncmp (segment_name (sec), ".gnu.linkonce.t.", len - 1) == 0)
link_once_flags = SEC_LINK_ONCE;
}
return (link_once_flags != 0);
}
/* Emit an instruction to the current fragment. If record_fix is true,
then this instruction will not change and we can go ahead and record
the fixup. If record_fix is false, then the instruction may change
and we are going to close out this fragment. Go ahead and set the
fr_symbol and fr_offset instead of adding a fixup. */
static bfd_boolean
xg_emit_insn (t_insn, record_fix)
TInsn *t_insn;
bfd_boolean record_fix;
{
bfd_boolean ok = TRUE;
xtensa_isa isa = xtensa_default_isa;
xtensa_opcode opcode = t_insn->opcode;
bfd_boolean has_fixup = FALSE;
int noperands;
int i, byte_count;
fragS *oldfrag;
size_t old_size;
char *f;
static xtensa_insnbuf insnbuf = NULL;
/* Use a static pointer to the insn buffer so we don't have to call
malloc each time through. */
if (!insnbuf)
insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
has_fixup = tinsn_to_insnbuf (t_insn, insnbuf);
noperands = xtensa_num_operands (isa, opcode);
assert (noperands == t_insn->ntok);
byte_count = xtensa_insn_length (isa, opcode);
oldfrag = frag_now;
/* This should NEVER cause us to jump into a new frag;
we've already reserved space. */
old_size = frag_now_fix ();
f = frag_more (byte_count);
assert (oldfrag == frag_now);
/* This needs to generate a record that lists the parts that are
instructions. */
if (!frag_now->tc_frag_data.is_insn)
{
/* If we are at the beginning of a fragment, switch this
fragment to an instruction fragment. */
if (now_seg != absolute_section && old_size != 0)
as_warn (_("instruction fragment may contain data"));
frag_now->tc_frag_data.is_insn = TRUE;
}
xtensa_insnbuf_to_chars (isa, insnbuf, f);
dwarf2_emit_insn (byte_count);
/* Now spit out the opcode fixup.... */
if (!has_fixup)
return !ok;
for (i = 0; i < noperands; ++i)
{
expressionS *expr = &t_insn->tok[i];
switch (expr->X_op)
{
case O_symbol:
if (get_relaxable_immed (opcode) == i)
{
if (record_fix)
{
if (!xg_add_opcode_fix (opcode, i, expr, frag_now,
f - frag_now->fr_literal))
ok = FALSE;
}
else
{
/* Write it to the fr_offset, fr_symbol. */
frag_now->fr_symbol = expr->X_add_symbol;
frag_now->fr_offset = expr->X_add_number;
}
}
else
{
as_bad (_("invalid operand %d on '%s'"),
i, xtensa_opcode_name (isa, opcode));
ok = FALSE;
}
break;
case O_constant:
case O_register:
break;
default:
as_bad (_("invalid expression for operand %d on '%s'"),
i, xtensa_opcode_name (isa, opcode));
ok = FALSE;
break;
}
}
return !ok;
}
static bfd_boolean
xg_emit_insn_to_buf (t_insn, buf, fragP, offset, build_fix)
TInsn *t_insn;
char *buf;
fragS *fragP;
offsetT offset;
bfd_boolean build_fix;
{
static xtensa_insnbuf insnbuf = NULL;
bfd_boolean has_symbolic_immed = FALSE;
bfd_boolean ok = TRUE;
if (!insnbuf)
insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
has_symbolic_immed = tinsn_to_insnbuf (t_insn, insnbuf);
if (has_symbolic_immed && build_fix)
{
/* Add a fixup. */
int opnum = get_relaxable_immed (t_insn->opcode);
expressionS *exp = &t_insn->tok[opnum];
if (!xg_add_opcode_fix (t_insn->opcode,
opnum, exp, fragP, offset))
ok = FALSE;
}
fragP->tc_frag_data.is_insn = TRUE;
xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, buf);
return ok;
}
/* Put in a fixup record based on the opcode.
Return true on success. */
bfd_boolean
xg_add_opcode_fix (opcode, opnum, expr, fragP, offset)
xtensa_opcode opcode;
int opnum;
expressionS *expr;
fragS *fragP;
offsetT offset;
{
bfd_reloc_code_real_type reloc;
reloc_howto_type *howto;
int insn_length;
fixS *the_fix;
reloc = opnum_to_reloc (opnum);
if (reloc == BFD_RELOC_NONE)
{
as_bad (_("invalid relocation operand %i on '%s'"),
opnum, xtensa_opcode_name (xtensa_default_isa, opcode));
return FALSE;
}
howto = bfd_reloc_type_lookup (stdoutput, reloc);
if (!howto)
{
as_bad (_("undefined symbol for opcode \"%s\"."),
xtensa_opcode_name (xtensa_default_isa, opcode));
return FALSE;
}
insn_length = xtensa_insn_length (xtensa_default_isa, opcode);
the_fix = fix_new_exp (fragP, offset, insn_length, expr,
howto->pc_relative, reloc);
if (expr->X_add_symbol &&
(S_IS_EXTERNAL (expr->X_add_symbol) || S_IS_WEAK (expr->X_add_symbol)))
the_fix->fx_plt = TRUE;
return TRUE;
}
void
xg_resolve_literals (insn, lit_sym)
TInsn *insn;
symbolS *lit_sym;
{
symbolS *sym = get_special_literal_symbol ();
int i;
if (lit_sym == 0)
return;
assert (insn->insn_type == ITYPE_INSN);
for (i = 0; i < insn->ntok; i++)
if (insn->tok[i].X_add_symbol == sym)
insn->tok[i].X_add_symbol = lit_sym;
}
void
xg_resolve_labels (insn, label_sym)
TInsn *insn;
symbolS *label_sym;
{
symbolS *sym = get_special_label_symbol ();
int i;
/* assert(!insn->is_literal); */
for (i = 0; i < insn->ntok; i++)
if (insn->tok[i].X_add_symbol == sym)
insn->tok[i].X_add_symbol = label_sym;
}
static void
xg_assemble_tokens (insn)
/*const */ TInsn *insn;
{
/* By the time we get here, there's not too much left to do.
1) Check our assumptions.
2) Check if the current instruction is "narrow".
If so, then finish the frag, create another one.
We could also go back to change some previous
"narrow" frags into no-change ones if we have more than
MAX_NARROW_ALIGNMENT of them without alignment restrictions
between them.
Cases:
1) It has constant operands and doesn't fit.
Go ahead and assemble it so it will fail.
2) It has constant operands that fit.
If narrow and !is_specific_opcode,
assemble it and put in a relocation
else
assemble it.
3) It has a symbolic immediate operand
a) Find the worst-case relaxation required
b) Find the worst-case literal pool space required.
Insert appropriate alignment & space in the literal.
Assemble it.
Add the relocation. */
assert (insn->insn_type == ITYPE_INSN);
if (!tinsn_has_symbolic_operands (insn))
{
if (xg_is_narrow_insn (insn) && !insn->is_specific_opcode)
{
/* assemble it but add max required space */
int max_size = xg_get_max_narrow_insn_size (insn->opcode);
int min_size = xg_get_insn_size (insn);
char *last_insn;
assert (max_size == 3);
/* make sure we have enough space to widen it */
xg_force_frag_space (max_size);
/* Output the instruction. It may cause an error if some
operands do not fit. */
last_insn = frag_more (0);
if (xg_emit_insn (insn, TRUE))
as_warn (_("instruction with constant operands does not fit"));
xg_finish_frag (last_insn, RELAX_NARROW, max_size - min_size, TRUE);
}
else
{
/* Assemble it. No relocation needed. */
int max_size = xg_get_insn_size (insn);
xg_force_frag_space (max_size);
if (xg_emit_insn (insn, FALSE))
as_warn (_("instruction with constant operands does not "
"fit without widening"));
/* frag_more (max_size); */
/* Special case for jx. If the jx is the next to last
instruction in a loop, we will add a NOP after it. This
avoids a hardware issue that could occur if the jx jumped
to the next instruction. */
if (software_avoid_b_j_loop_end
&& is_jx_opcode (insn->opcode))
{
maybe_has_b_j_loop_end = TRUE;
/* add 2 of these */
frag_now->tc_frag_data.is_insn = TRUE;
frag_var (rs_machine_dependent, 4, 4,
RELAX_ADD_NOP_IF_PRE_LOOP_END,
frag_now->fr_symbol, frag_now->fr_offset, NULL);
}
}
}
else
{
/* Need to assemble it with space for the relocation. */
if (!insn->is_specific_opcode)
{
/* Assemble it but add max required space. */
char *last_insn;
int min_size = xg_get_insn_size (insn);
int max_size = xg_get_max_insn_widen_size (insn->opcode);
int max_literal_size =
xg_get_max_insn_widen_literal_size (insn->opcode);
#if 0
symbolS *immed_sym = xg_get_insn_immed_symbol (insn);
set_frag_segment (frag_now, now_seg);
#endif /* 0 */
/* Make sure we have enough space to widen the instruction.
This may open a new fragment. */
xg_force_frag_space (max_size);
if (max_literal_size != 0)
xg_assemble_literal_space (max_literal_size);
/* Output the instruction. It may cause an error if some
operands do not fit. Emit the incomplete instruction. */
last_insn = frag_more (0);
xg_emit_insn (insn, FALSE);
xg_finish_frag (last_insn, RELAX_IMMED, max_size - min_size, TRUE);
/* Special cases for loops:
close_loop_end should be inserted AFTER short_loop.
Make sure that CLOSE loops are processed BEFORE short_loops
when converting them. */
/* "short_loop": add a NOP if the loop is < 4 bytes. */
if (software_avoid_short_loop
&& is_loop_opcode (insn->opcode))
{
maybe_has_short_loop = TRUE;
frag_now->tc_frag_data.is_insn = TRUE;
frag_var (rs_machine_dependent, 4, 4,
RELAX_ADD_NOP_IF_SHORT_LOOP,
frag_now->fr_symbol, frag_now->fr_offset, NULL);
frag_now->tc_frag_data.is_insn = TRUE;
frag_var (rs_machine_dependent, 4, 4,
RELAX_ADD_NOP_IF_SHORT_LOOP,
frag_now->fr_symbol, frag_now->fr_offset, NULL);
}
/* "close_loop_end": Add up to 12 bytes of NOPs to keep a
loop at least 12 bytes away from another loop's loop
end. */
if (software_avoid_close_loop_end
&& is_loop_opcode (insn->opcode))
{
maybe_has_close_loop_end = TRUE;
frag_now->tc_frag_data.is_insn = TRUE;
frag_var (rs_machine_dependent, 12, 12,
RELAX_ADD_NOP_IF_CLOSE_LOOP_END,
frag_now->fr_symbol, frag_now->fr_offset, NULL);
}
}
else
{
/* Assemble it in place. No expansion will be required,
but we'll still need a relocation record. */
int max_size = xg_get_insn_size (insn);
xg_force_frag_space (max_size);
if (xg_emit_insn (insn, TRUE))
as_warn (_("instruction's constant operands do not fit"));
}
}
}
/* Return true if the instruction can write to the specified
integer register. */
static bfd_boolean
is_register_writer (insn, regset, regnum)
const TInsn *insn;
const char *regset;
int regnum;
{
int i;
int num_ops;
xtensa_isa isa = xtensa_default_isa;
num_ops = xtensa_num_operands (isa, insn->opcode);
for (i = 0; i < num_ops; i++)
{
xtensa_operand operand = xtensa_get_operand (isa, insn->opcode, i);
char inout = xtensa_operand_inout (operand);
if (inout == '>' || inout == '=')
{
if (strcmp (xtensa_operand_kind (operand), regset) == 0)
{
if ((insn->tok[i].X_op == O_register)
&& (insn->tok[i].X_add_number == regnum))
return TRUE;
}
}
}
return FALSE;
}
static bfd_boolean
is_bad_loopend_opcode (tinsn)
const TInsn * tinsn;
{
xtensa_opcode opcode = tinsn->opcode;
if (opcode == XTENSA_UNDEFINED)
return FALSE;
if (opcode == xtensa_call0_opcode
|| opcode == xtensa_callx0_opcode
|| opcode == xtensa_call4_opcode
|| opcode == xtensa_callx4_opcode
|| opcode == xtensa_call8_opcode
|| opcode == xtensa_callx8_opcode
|| opcode == xtensa_call12_opcode
|| opcode == xtensa_callx12_opcode
|| opcode == xtensa_isync_opcode
|| opcode == xtensa_ret_opcode
|| opcode == xtensa_ret_n_opcode
|| opcode == xtensa_retw_opcode
|| opcode == xtensa_retw_n_opcode
|| opcode == xtensa_waiti_opcode)
return TRUE;
/* An RSR of LCOUNT is illegal as the last opcode in a loop. */
if (opcode == xtensa_rsr_opcode
&& tinsn->ntok >= 2
&& tinsn->tok[1].X_op == O_constant
&& tinsn->tok[1].X_add_number == 2)
return TRUE;
return FALSE;
}
/* Labels that begin with ".Ln" or ".LM" are unaligned.
This allows the debugger to add unaligned labels.
Also, the assembler generates stabs labels that need
not be aligned: FAKE_LABEL_NAME . {"F", "L", "endfunc"}. */
bfd_boolean
is_unaligned_label (sym)
symbolS *sym;
{
const char *name = S_GET_NAME (sym);
static size_t fake_size = 0;
if (name
&& name[0] == '.'
&& name[1] == 'L' && (name[2] == 'n' || name[2] == 'M'))
return TRUE;
/* FAKE_LABEL_NAME followed by "F", "L" or "endfunc" */
if (fake_size == 0)
fake_size = strlen (FAKE_LABEL_NAME);
if (name
&& strncmp (FAKE_LABEL_NAME, name, fake_size) == 0
&& (name[fake_size] == 'F'
|| name[fake_size] == 'L'
|| (name[fake_size] == 'e'
&& strncmp ("endfunc", name+fake_size, 7) == 0)))
return TRUE;
return FALSE;
}
fragS *
next_non_empty_frag (fragP)
const fragS *fragP;
{
fragS *next_fragP = fragP->fr_next;
/* Sometimes an empty will end up here due storage allocation issues.
So we have to skip until we find something legit. */
while (next_fragP && next_fragP->fr_fix == 0)
next_fragP = next_fragP->fr_next;
if (next_fragP == NULL || next_fragP->fr_fix == 0)
return NULL;
return next_fragP;
}
xtensa_opcode
next_frag_opcode (fragP)
const fragS * fragP;
{
const fragS *next_fragP = next_non_empty_frag (fragP);
static xtensa_insnbuf insnbuf = NULL;
xtensa_isa isa = xtensa_default_isa;
if (!insnbuf)
insnbuf = xtensa_insnbuf_alloc (isa);
if (next_fragP == NULL)
return XTENSA_UNDEFINED;
xtensa_insnbuf_from_chars (isa, insnbuf, next_fragP->fr_literal);
return xtensa_decode_insn (isa, insnbuf);
}
/* Return true if the target frag is one of the next non-empty frags. */
bfd_boolean
is_next_frag_target (fragP, target)
const fragS *fragP;
const fragS *target;
{
if (fragP == NULL)
return FALSE;
for (; fragP; fragP = fragP->fr_next)
{
if (fragP == target)
return TRUE;
if (fragP->fr_fix != 0)
return FALSE;
if (fragP->fr_type == rs_fill && fragP->fr_offset != 0)
return FALSE;
if ((fragP->fr_type == rs_align || fragP->fr_type == rs_align_code)
&& ((fragP->fr_address % (1 << fragP->fr_offset)) != 0))
return FALSE;
if (fragP->fr_type == rs_space)
return FALSE;
}
return FALSE;
}
/* If the next legit fragment is an end-of-loop marker,
switch its state so it will instantiate a NOP. */
static void
update_next_frag_nop_state (fragP)
fragS *fragP;
{
fragS *next_fragP = fragP->fr_next;
while (next_fragP && next_fragP->fr_fix == 0)
{
if (next_fragP->fr_type == rs_machine_dependent
&& next_fragP->fr_subtype == RELAX_LOOP_END)
{
next_fragP->fr_subtype = RELAX_LOOP_END_ADD_NOP;
return;
}
next_fragP = next_fragP->fr_next;
}
}
static bfd_boolean
next_frag_is_branch_target (fragP)
const fragS *fragP;
{
/* Sometimes an empty will end up here due storage allocation issues,
so we have to skip until we find something legit. */
for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next)
{
if (fragP->tc_frag_data.is_branch_target)
return TRUE;
if (fragP->fr_fix != 0)
break;
}
return FALSE;
}
static bfd_boolean
next_frag_is_loop_target (fragP)
const fragS *fragP;
{
/* Sometimes an empty will end up here due storage allocation issues.
So we have to skip until we find something legit. */
for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next)
{
if (fragP->tc_frag_data.is_loop_target)
return TRUE;
if (fragP->fr_fix != 0)
break;
}
return FALSE;
}
static addressT
next_frag_pre_opcode_bytes (fragp)
const fragS *fragp;
{
const fragS *next_fragp = fragp->fr_next;
xtensa_opcode next_opcode = next_frag_opcode (fragp);
if (!is_loop_opcode (next_opcode))
return 0;
/* Sometimes an empty will end up here due storage allocation issues.
So we have to skip until we find something legit. */
while (next_fragp->fr_fix == 0)
next_fragp = next_fragp->fr_next;
if (next_fragp->fr_type != rs_machine_dependent)
return 0;
/* There is some implicit knowledge encoded in here.
The LOOP instructions that are NOT RELAX_IMMED have
been relaxed. */
if (next_fragp->fr_subtype > RELAX_IMMED)
return get_expanded_loop_offset (next_opcode);
return 0;
}
/* Mark a location where we can later insert literal frags. Update
the section's literal_pool_loc, so subsequent literals can be
placed nearest to their use. */
static void
xtensa_mark_literal_pool_location ()
{
/* Any labels pointing to the current location need
to be adjusted to after the literal pool. */
emit_state s;
fragS *pool_location;
frag_align (2, 0, 0);
/* We stash info in the fr_var of these frags
so we can later move the literal's fixes into this
frchain's fix list. We can use fr_var because fr_var's
interpretation depends solely on the fr_type and subtype. */
pool_location = frag_now;
frag_variant (rs_machine_dependent, 0, (int) frchain_now,
RELAX_LITERAL_POOL_BEGIN, NULL, 0, NULL);
frag_variant (rs_machine_dependent, 0, (int) now_seg,
RELAX_LITERAL_POOL_END, NULL, 0, NULL);
/* Now put a frag into the literal pool that points to this location. */
set_literal_pool_location (now_seg, pool_location);
xtensa_switch_to_literal_fragment (&s);
/* Close whatever frag is there. */
frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
frag_now->tc_frag_data.literal_frag = pool_location;
frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
xtensa_restore_emit_state (&s);
}
/* The "loops_ok" argument is provided to allow ignoring labels that
define loop ends. This fixes a bug where the NOPs to align a
loop opcode were included in a previous zero-cost loop:
loop a0, loopend
<loop1 body>
loopend:
loop a2, loopend2
<loop2 body>
would become:
loop a0, loopend
<loop1 body>
nop.n <===== bad!
loopend:
loop a2, loopend2
<loop2 body>
This argument is used to prevent moving the NOP to before the
loop-end label, which is what you want in this special case. */
static void
xtensa_move_labels (new_frag, new_offset, loops_ok)
fragS *new_frag;
valueT new_offset;
bfd_boolean loops_ok;
{
sym_list *lit;
for (lit = insn_labels; lit; lit = lit->next)
{
symbolS *lit_sym = lit->sym;
if (loops_ok || symbol_get_tc (lit_sym)->is_loop_target == 0)
{
S_SET_VALUE (lit_sym, new_offset);
symbol_set_frag (lit_sym, new_frag);
}
}
}
/* Assemble a NOP of the requested size in the buffer. User must have
allocated "buf" with at least "size" bytes. */
void
assemble_nop (size, buf)
size_t size;
char *buf;
{
static xtensa_insnbuf insnbuf = NULL;
TInsn t_insn;
if (!insnbuf)
insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
tinsn_init (&t_insn);
switch (size)
{
case 2:
t_insn.opcode = xtensa_nop_n_opcode;
t_insn.ntok = 0;
if (t_insn.opcode == XTENSA_UNDEFINED)
as_fatal (_("opcode 'NOP.N' unavailable in this configuration"));
tinsn_to_insnbuf (&t_insn, insnbuf);
xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, buf);
break;
case 3:
t_insn.opcode = xtensa_or_opcode;
assert (t_insn.opcode != XTENSA_UNDEFINED);
if (t_insn.opcode == XTENSA_UNDEFINED)
as_fatal (_("opcode 'OR' unavailable in this configuration"));
set_expr_const (&t_insn.tok[0], 1);
set_expr_const (&t_insn.tok[1], 1);
set_expr_const (&t_insn.tok[2], 1);
t_insn.ntok = 3;
tinsn_to_insnbuf (&t_insn, insnbuf);
xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, buf);
break;
default:
as_fatal (_("invalid %d-byte NOP requested"), size);
}
}
/* Return the number of bytes for the offset of the expanded loop
instruction. This should be incorporated into the relaxation
specification but is hard-coded here. This is used to auto-align
the loop instruction. It is invalid to call this function if the
configuration does not have loops or if the opcode is not a loop
opcode. */
static addressT
get_expanded_loop_offset (opcode)
xtensa_opcode opcode;
{
/* This is the OFFSET of the loop instruction in the expanded loop.
This MUST correspond directly to the specification of the loop
expansion. It will be validated on fragment conversion. */
if (opcode == XTENSA_UNDEFINED)
as_fatal (_("get_expanded_loop_offset: undefined opcode"));
if (opcode == xtensa_loop_opcode)
return 0;
if (opcode == xtensa_loopnez_opcode)
return 3;
if (opcode == xtensa_loopgtz_opcode)
return 6;
as_fatal (_("get_expanded_loop_offset: invalid opcode"));
return 0;
}
fragS *
get_literal_pool_location (seg)
segT seg;
{
return seg_info (seg)->tc_segment_info_data.literal_pool_loc;
}
static void
set_literal_pool_location (seg, literal_pool_loc)
segT seg;
fragS *literal_pool_loc;
{
seg_info (seg)->tc_segment_info_data.literal_pool_loc = literal_pool_loc;
}
/* External Functions and Other GAS Hooks. */
const char *
xtensa_target_format ()
{
return (target_big_endian ? "elf32-xtensa-be" : "elf32-xtensa-le");
}
void
xtensa_file_arch_init (abfd)
bfd *abfd;
{
bfd_set_private_flags (abfd, 0x100 | 0x200);
}
void
md_number_to_chars (buf, val, n)
char *buf;
valueT val;
int n;
{
if (target_big_endian)
number_to_chars_bigendian (buf, val, n);
else
number_to_chars_littleendian (buf, val, n);
}
/* This function is called once, at assembler startup time. It should
set up all the tables, etc. that the MD part of the assembler will
need. */
void
md_begin ()
{
segT current_section = now_seg;
int current_subsec = now_subseg;
xtensa_isa isa;
#if STATIC_LIBISA
isa = xtensa_isa_init ();
#else
/* ISA was already initialized by xtensa_init(). */
isa = xtensa_default_isa;
#endif
/* Set up the .literal, .fini.literal and .init.literal sections. */
memset (&default_lit_sections, 0, sizeof (default_lit_sections));
default_lit_sections.init_lit_seg_name = INIT_LITERAL_SECTION_NAME;
default_lit_sections.fini_lit_seg_name = FINI_LITERAL_SECTION_NAME;
default_lit_sections.lit_seg_name = LITERAL_SECTION_NAME;
subseg_set (current_section, current_subsec);
xtensa_addi_opcode = xtensa_opcode_lookup (isa, "addi");
xtensa_addmi_opcode = xtensa_opcode_lookup (isa, "addmi");
xtensa_call0_opcode = xtensa_opcode_lookup (isa, "call0");
xtensa_call4_opcode = xtensa_opcode_lookup (isa, "call4");
xtensa_call8_opcode = xtensa_opcode_lookup (isa, "call8");
xtensa_call12_opcode = xtensa_opcode_lookup (isa, "call12");
xtensa_callx0_opcode = xtensa_opcode_lookup (isa, "callx0");
xtensa_callx4_opcode = xtensa_opcode_lookup (isa, "callx4");
xtensa_callx8_opcode = xtensa_opcode_lookup (isa, "callx8");
xtensa_callx12_opcode = xtensa_opcode_lookup (isa, "callx12");
xtensa_entry_opcode = xtensa_opcode_lookup (isa, "entry");
xtensa_isync_opcode = xtensa_opcode_lookup (isa, "isync");
xtensa_j_opcode = xtensa_opcode_lookup (isa, "j");
xtensa_jx_opcode = xtensa_opcode_lookup (isa, "jx");
xtensa_loop_opcode = xtensa_opcode_lookup (isa, "loop");
xtensa_loopnez_opcode = xtensa_opcode_lookup (isa, "loopnez");
xtensa_loopgtz_opcode = xtensa_opcode_lookup (isa, "loopgtz");
xtensa_nop_n_opcode = xtensa_opcode_lookup (isa, "nop.n");
xtensa_or_opcode = xtensa_opcode_lookup (isa, "or");
xtensa_ret_opcode = xtensa_opcode_lookup (isa, "ret");
xtensa_ret_n_opcode = xtensa_opcode_lookup (isa, "ret.n");
xtensa_retw_opcode = xtensa_opcode_lookup (isa, "retw");
xtensa_retw_n_opcode = xtensa_opcode_lookup (isa, "retw.n");
xtensa_rsr_opcode = xtensa_opcode_lookup (isa, "rsr");
xtensa_waiti_opcode = xtensa_opcode_lookup (isa, "waiti");
}
/* tc_frob_label hook */
void
xtensa_frob_label (sym)
symbolS *sym;
{
if (generating_literals)
xtensa_add_literal_sym (sym);
else
xtensa_add_insn_label (sym);
if (symbol_get_tc (sym)->is_loop_target
&& (get_last_insn_flags (now_seg, now_subseg)
& FLAG_IS_BAD_LOOPEND) != 0)
as_bad (_("invalid last instruction for a zero-overhead loop"));
/* No target aligning in the absolute section. */
if (now_seg != absolute_section
&& align_targets
&& !is_unaligned_label (sym)
&& !frag_now->tc_frag_data.is_literal)
{
/* frag_now->tc_frag_data.is_insn = TRUE; */
frag_var (rs_machine_dependent, 4, 4,
RELAX_DESIRE_ALIGN_IF_TARGET,
frag_now->fr_symbol, frag_now->fr_offset, NULL);
xtensa_move_labels (frag_now, 0, TRUE);
/* If the label is already known to be a branch target, i.e., a
forward branch, mark the frag accordingly. Backward branches
are handled by xg_add_branch_and_loop_targets. */
if (symbol_get_tc (sym)->is_branch_target)
symbol_get_frag (sym)->tc_frag_data.is_branch_target = TRUE;
/* Loops only go forward, so they can be identified here. */
if (symbol_get_tc (sym)->is_loop_target)
symbol_get_frag (sym)->tc_frag_data.is_loop_target = TRUE;
}
}
/* md_flush_pending_output hook */
void
xtensa_flush_pending_output ()
{
/* If there is a non-zero instruction fragment, close it. */
if (frag_now_fix () != 0 && frag_now->tc_frag_data.is_insn)
{
frag_wane (frag_now);
frag_new (0);
}
frag_now->tc_frag_data.is_insn = FALSE;
xtensa_clear_insn_labels ();
}
void
md_assemble (str)
char *str;
{
xtensa_isa isa = xtensa_default_isa;
char *opname;
unsigned opnamelen;
bfd_boolean has_underbar = FALSE;
char *arg_strings[MAX_INSN_ARGS];
int num_args;
IStack istack; /* Put instructions into here. */
TInsn orig_insn; /* Original instruction from the input. */
int i;
symbolS *lit_sym = NULL;
if (frag_now->tc_frag_data.is_literal)
{
static bfd_boolean reported = 0;
if (reported < 4)
as_bad (_("cannot assemble '%s' into a literal fragment"), str);
if (reported == 3)
as_bad (_("..."));
reported++;
return;
}
istack_init (&istack);
tinsn_init (&orig_insn);
/* Split off the opcode. */
opnamelen = strspn (str, "abcdefghijklmnopqrstuvwxyz_/0123456789.");
opname = xmalloc (opnamelen + 1);
memcpy (opname, str, opnamelen);
opname[opnamelen] = '\0';
num_args = tokenize_arguments (arg_strings, str + opnamelen);
if (num_args == -1)
{
as_bad (_("syntax error"));
return;
}
if (xg_translate_idioms (&opname, &num_args, arg_strings))
return;
/* Check for an underbar prefix. */
if (*opname == '_')
{
has_underbar = TRUE;
opname += 1;
}
orig_insn.insn_type = ITYPE_INSN;
orig_insn.ntok = 0;
orig_insn.is_specific_opcode = (has_underbar || !use_generics ());
specific_opcode = orig_insn.is_specific_opcode;
orig_insn.opcode = xtensa_opcode_lookup (isa, opname);
if (orig_insn.opcode == XTENSA_UNDEFINED)
{
as_bad (_("unknown opcode %s"), opname);
return;
}
if (frag_now_fix () != 0 && !frag_now->tc_frag_data.is_insn)
{
frag_wane (frag_now);
frag_new (0);
}
if (software_a0_b_retw_interlock)
{
if ((get_last_insn_flags (now_seg, now_subseg) & FLAG_IS_A0_WRITER) != 0
&& is_conditional_branch_opcode (orig_insn.opcode))
{
has_a0_b_retw = TRUE;
/* Mark this fragment with the special RELAX_ADD_NOP_IF_A0_B_RETW.
After the first assembly pass we will check all of them and
add a nop if needed. */
frag_now->tc_frag_data.is_insn = TRUE;
frag_var (rs_machine_dependent, 4, 4,
RELAX_ADD_NOP_IF_A0_B_RETW,
frag_now->fr_symbol, frag_now->fr_offset, NULL);
frag_now->tc_frag_data.is_insn = TRUE;
frag_var (rs_machine_dependent, 4, 4,
RELAX_ADD_NOP_IF_A0_B_RETW,
frag_now->fr_symbol, frag_now->fr_offset, NULL);
}
}
/* Special case: The call instructions should be marked "specific opcode"
to keep them from expanding. */
if (!use_longcalls () && is_direct_call_opcode (orig_insn.opcode))
orig_insn.is_specific_opcode = TRUE;
/* Parse the arguments. */
if (parse_arguments (&orig_insn, num_args, arg_strings))
{
as_bad (_("syntax error"));
return;
}
/* Free the opcode and argument strings, now that they've been parsed. */
free (has_underbar ? opname - 1 : opname);
opname = 0;
while (num_args-- > 0)
free (arg_strings[num_args]);
/* Check for the right number and type of arguments. */
if (tinsn_check_arguments (&orig_insn))
return;
/* See if the instruction implies an aligned section. */
if (is_entry_opcode (orig_insn.opcode) || is_loop_opcode (orig_insn.opcode))
record_alignment (now_seg, 2);
xg_add_branch_and_loop_targets (&orig_insn);
/* Special cases for instructions that force an alignment... */
if (!orig_insn.is_specific_opcode && is_loop_opcode (orig_insn.opcode))
{
size_t max_fill;
frag_now->tc_frag_data.is_insn = TRUE;
frag_now->tc_frag_data.is_no_density = !code_density_available ();
max_fill = get_text_align_max_fill_size
(get_text_align_power (XTENSA_FETCH_WIDTH),
TRUE, frag_now->tc_frag_data.is_no_density);
frag_var (rs_machine_dependent, max_fill, max_fill,
RELAX_ALIGN_NEXT_OPCODE, frag_now->fr_symbol,
frag_now->fr_offset, NULL);
xtensa_move_labels (frag_now, 0, FALSE);
}
/* Special-case for "entry" instruction. */
if (is_entry_opcode (orig_insn.opcode))
{
/* Check that the second opcode (#1) is >= 16. */
if (orig_insn.ntok >= 2)
{
expressionS *exp = &orig_insn.tok[1];
switch (exp->X_op)
{
case O_constant:
if (exp->X_add_number < 16)
as_warn (_("entry instruction with stack decrement < 16"));
break;
default:
as_warn (_("entry instruction with non-constant decrement"));
}
}
if (!orig_insn.is_specific_opcode)
{
xtensa_mark_literal_pool_location ();
/* Automatically align ENTRY instructions. */
xtensa_move_labels (frag_now, 0, TRUE);
frag_align (2, 0, 0);
}
}
/* Any extra alignment frags have been inserted now, and we're about to
emit a new instruction so clear the list of labels. */
xtensa_clear_insn_labels ();
if (software_a0_b_retw_interlock)
set_last_insn_flags (now_seg, now_subseg, FLAG_IS_A0_WRITER,
is_register_writer (&orig_insn, "a", 0));
set_last_insn_flags (now_seg, now_subseg, FLAG_IS_BAD_LOOPEND,
is_bad_loopend_opcode (&orig_insn));
/* Finish it off:
assemble_tokens (opcode, tok, ntok);
expand the tokens from the orig_insn into the
stack of instructions that will not expand
unless required at relaxation time. */
if (xg_expand_assembly_insn (&istack, &orig_insn))
return;
for (i = 0; i < istack.ninsn; i++)
{
TInsn *insn = &istack.insn[i];
if (insn->insn_type == ITYPE_LITERAL)
{
assert (lit_sym == NULL);
lit_sym = xg_assemble_literal (insn);
}
else
{
if (lit_sym)
xg_resolve_literals (insn, lit_sym);
xg_assemble_tokens (insn);
}
}
/* Now, if the original opcode was a call... */
if (align_targets && is_call_opcode (orig_insn.opcode))
{
frag_now->tc_frag_data.is_insn = TRUE;
frag_var (rs_machine_dependent, 4, 4,
RELAX_DESIRE_ALIGN,
frag_now->fr_symbol,
frag_now->fr_offset,
NULL);
}
}
/* TC_CONS_FIX_NEW hook: Check for "@PLT" suffix on symbol references.
If found, use an XTENSA_PLT reloc for 4-byte values. Otherwise, this
is the same as the standard code in read.c. */
void
xtensa_cons_fix_new (frag, where, size, exp)
fragS *frag;
int where;
int size;
expressionS *exp;
{
bfd_reloc_code_real_type r;
bfd_boolean plt = FALSE;
if (*input_line_pointer == '@')
{
if (!strncmp (input_line_pointer, PLT_SUFFIX, strlen (PLT_SUFFIX) - 1)
&& !strncmp (input_line_pointer, plt_suffix,
strlen (plt_suffix) - 1))
{
as_bad (_("undefined @ suffix '%s', expected '%s'"),
input_line_pointer, plt_suffix);
ignore_rest_of_line ();
return;
}
input_line_pointer += strlen (plt_suffix);
plt = TRUE;
}
switch (size)
{
case 1:
r = BFD_RELOC_8;
break;
case 2:
r = BFD_RELOC_16;
break;
case 4:
r = plt ? BFD_RELOC_XTENSA_PLT : BFD_RELOC_32;
break;
case 8:
r = BFD_RELOC_64;
break;
default:
as_bad (_("unsupported BFD relocation size %u"), size);
r = BFD_RELOC_32;
break;
}
fix_new_exp (frag, where, size, exp, 0, r);
}
/* TC_FRAG_INIT hook */
void
xtensa_frag_init (frag)
fragS *frag;
{
frag->tc_frag_data.is_no_density = !code_density_available ();
}
symbolS *
md_undefined_symbol (name)
char *name ATTRIBUTE_UNUSED;
{
return NULL;
}
/* Round up a section size to the appropriate boundary. */
valueT
md_section_align (segment, size)
segT segment ATTRIBUTE_UNUSED;
valueT size;
{
return size; /* Byte alignment is fine. */
}
long
md_pcrel_from (fixP)
fixS *fixP;
{
char *insn_p;
static xtensa_insnbuf insnbuf = NULL;
int opnum;
xtensa_operand operand;
xtensa_opcode opcode;
xtensa_isa isa = xtensa_default_isa;
valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
if (fixP->fx_done)
return addr;
if (fixP->fx_r_type == BFD_RELOC_XTENSA_ASM_EXPAND)
return addr;
if (!insnbuf)
insnbuf = xtensa_insnbuf_alloc (isa);
insn_p = &fixP->fx_frag->fr_literal[fixP->fx_where];
xtensa_insnbuf_from_chars (isa, insnbuf, insn_p);
opcode = xtensa_decode_insn (isa, insnbuf);
opnum = reloc_to_opnum (fixP->fx_r_type);
if (opnum < 0)
as_fatal (_("invalid operand relocation for '%s' instruction"),
xtensa_opcode_name (isa, opcode));
if (opnum >= xtensa_num_operands (isa, opcode))
as_fatal (_("invalid relocation for operand %d in '%s' instruction"),
opnum, xtensa_opcode_name (isa, opcode));
operand = xtensa_get_operand (isa, opcode, opnum);
if (!operand)
{
as_warn_where (fixP->fx_file,
fixP->fx_line,
_("invalid relocation type %d for %s instruction"),
fixP->fx_r_type, xtensa_opcode_name (isa, opcode));
return addr;
}
if (!operand_is_pcrel_label (operand))
{
as_bad_where (fixP->fx_file,
fixP->fx_line,
_("invalid relocation for operand %d of '%s'"),
opnum, xtensa_opcode_name (isa, opcode));
return addr;
}
if (!xtensa_operand_isPCRelative (operand))
{
as_warn_where (fixP->fx_file,
fixP->fx_line,
_("non-PCREL relocation operand %d for '%s': %s"),
opnum, xtensa_opcode_name (isa, opcode),
bfd_get_reloc_code_name (fixP->fx_r_type));
return addr;
}
return 0 - xtensa_operand_do_reloc (operand, 0, addr);
}
/* tc_symbol_new_hook */
void
xtensa_symbol_new_hook (symbolP)
symbolS *symbolP;
{
symbol_get_tc (symbolP)->plt = 0;
}
/* tc_fix_adjustable hook */
bfd_boolean
xtensa_fix_adjustable (fixP)
fixS *fixP;
{
/* We need the symbol name for the VTABLE entries. */
if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
|| fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
return 0;
return 1;
}
void
md_apply_fix3 (fixP, valP, seg)
fixS *fixP;
valueT *valP;
segT seg ATTRIBUTE_UNUSED;
{
if (fixP->fx_pcrel == 0 && fixP->fx_addsy == 0)
{
/* This happens when the relocation is within the current section.
It seems this implies a PCREL operation. We'll catch it and error
if not. */
char *const fixpos = fixP->fx_frag->fr_literal + fixP->fx_where;
static xtensa_insnbuf insnbuf = NULL;
xtensa_opcode opcode;
xtensa_isa isa;
switch (fixP->fx_r_type)
{
case BFD_RELOC_XTENSA_ASM_EXPAND:
fixP->fx_done = 1;
break;
case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
as_bad (_("unhandled local relocation fix %s"),
bfd_get_reloc_code_name (fixP->fx_r_type));
break;
case BFD_RELOC_32:
case BFD_RELOC_16:
case BFD_RELOC_8:
/* The only one we support that isn't an instruction field. */
md_number_to_chars (fixpos, *valP, fixP->fx_size);
fixP->fx_done = 1;
break;
case BFD_RELOC_XTENSA_OP0:
case BFD_RELOC_XTENSA_OP1:
case BFD_RELOC_XTENSA_OP2:
isa = xtensa_default_isa;
if (!insnbuf)
insnbuf = xtensa_insnbuf_alloc (isa);
xtensa_insnbuf_from_chars (isa, insnbuf, fixpos);
opcode = xtensa_decode_insn (isa, insnbuf);
if (opcode == XTENSA_UNDEFINED)
as_fatal (_("undecodable FIX"));
xtensa_insnbuf_set_immediate_field (opcode, insnbuf, *valP,
fixP->fx_file, fixP->fx_line);
fixP->fx_frag->tc_frag_data.is_insn = TRUE;
xtensa_insnbuf_to_chars (isa, insnbuf, fixpos);
fixP->fx_done = 1;
break;
case BFD_RELOC_VTABLE_INHERIT:
case BFD_RELOC_VTABLE_ENTRY:
fixP->fx_done = 0;
break;
default:
as_bad (_("unhandled local relocation fix %s"),
bfd_get_reloc_code_name (fixP->fx_r_type));
}
}
}
char *
md_atof (type, litP, sizeP)
int type;
char *litP;
int *sizeP;
{
int prec;
LITTLENUM_TYPE words[4];
char *t;
int i;
switch (type)
{
case 'f':
prec = 2;
break;
case 'd':
prec = 4;
break;
default:
*sizeP = 0;
return "bad call to md_atof";
}
t = atof_ieee (input_line_pointer, type, words);
if (t)
input_line_pointer = t;
*sizeP = prec * 2;
for (i = prec - 1; i >= 0; i--)
{
int idx = i;
if (target_big_endian)
idx = (prec - 1 - i);
md_number_to_chars (litP, (valueT) words[idx], 2);
litP += 2;
}
return NULL;
}
int
md_estimate_size_before_relax (fragP, seg)
fragS *fragP;
segT seg ATTRIBUTE_UNUSED;
{
return fragP->tc_frag_data.text_expansion;
}
/* Translate internal representation of relocation info to BFD target
format. */
arelent *
tc_gen_reloc (section, fixp)
asection *section ATTRIBUTE_UNUSED;
fixS *fixp;
{
arelent *reloc;
reloc = (arelent *) xmalloc (sizeof (arelent));
reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
*reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
/* Make sure none of our internal relocations make it this far.
They'd better have been fully resolved by this point. */
assert ((int) fixp->fx_r_type > 0);
reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
if (reloc->howto == NULL)
{
as_bad_where (fixp->fx_file, fixp->fx_line,
_("cannot represent `%s' relocation in object file"),
bfd_get_reloc_code_name (fixp->fx_r_type));
return NULL;
}
if (!fixp->fx_pcrel != !reloc->howto->pc_relative)
{
as_fatal (_("internal error? cannot generate `%s' relocation"),
bfd_get_reloc_code_name (fixp->fx_r_type));
}
assert (!fixp->fx_pcrel == !reloc->howto->pc_relative);
reloc->addend = fixp->fx_offset;
switch (fixp->fx_r_type)
{
case BFD_RELOC_XTENSA_OP0:
case BFD_RELOC_XTENSA_OP1:
case BFD_RELOC_XTENSA_OP2:
case BFD_RELOC_XTENSA_ASM_EXPAND:
case BFD_RELOC_32:
case BFD_RELOC_XTENSA_PLT:
case BFD_RELOC_VTABLE_INHERIT:
case BFD_RELOC_VTABLE_ENTRY:
break;
case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
as_warn (_("emitting simplification relocation"));
break;
default:
as_warn (_("emitting unknown relocation"));
}
return reloc;
}
void
xtensa_end ()
{
directive_balance ();
xtensa_move_literals ();
xtensa_reorder_segments ();
xtensa_cleanup_align_frags ();
xtensa_fix_target_frags ();
if (software_a0_b_retw_interlock && has_a0_b_retw)
xtensa_fix_a0_b_retw_frags ();
if (software_avoid_b_j_loop_end && maybe_has_b_j_loop_end)
xtensa_fix_b_j_loop_end_frags ();
/* "close_loop_end" should be processed BEFORE "short_loop". */
if (software_avoid_close_loop_end && maybe_has_close_loop_end)
xtensa_fix_close_loop_end_frags ();
if (software_avoid_short_loop && maybe_has_short_loop)
xtensa_fix_short_loop_frags ();
xtensa_sanity_check ();
}
static void
xtensa_cleanup_align_frags ()
{
frchainS *frchP;
for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
{
fragS *fragP;
/* Walk over all of the fragments in a subsection. */
for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
{
if ((fragP->fr_type == rs_align
|| fragP->fr_type == rs_align_code
|| (fragP->fr_type == rs_machine_dependent
&& (fragP->fr_subtype == RELAX_DESIRE_ALIGN
|| fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)))
&& fragP->fr_fix == 0)
{
fragS * next = fragP->fr_next;
while (next
&& next->fr_type == rs_machine_dependent
&& next->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)
{
frag_wane (next);
next = next->fr_next;
}
}
}
}
}
/* Re-process all of the fragments looking to convert all of the
RELAX_DESIRE_ALIGN_IF_TARGET fragments. If there is a branch
target in the next fragment, convert this to RELAX_DESIRE_ALIGN.
If the next fragment starts with a loop target, AND the previous
fragment can be expanded to negate the branch, convert this to a
RELAX_LOOP_END. Otherwise, convert to a .fill 0. */
static void
xtensa_fix_target_frags ()
{
frchainS *frchP;
/* When this routine is called, all of the subsections are still intact
so we walk over subsections instead of sections. */
for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
{
bfd_boolean prev_frag_can_negate_branch = FALSE;
fragS *fragP;
/* Walk over all of the fragments in a subsection. */
for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
{
if (fragP->fr_type == rs_machine_dependent
&& fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)
{
if (next_frag_is_loop_target (fragP))
{
if (prev_frag_can_negate_branch)
fragP->fr_subtype = RELAX_LOOP_END;
else
{
if (!align_only_targets ||
next_frag_is_branch_target (fragP))
fragP->fr_subtype = RELAX_DESIRE_ALIGN;
else
frag_wane (fragP);
}
}
else if (!align_only_targets
|| next_frag_is_branch_target (fragP))
fragP->fr_subtype = RELAX_DESIRE_ALIGN;
else
frag_wane (fragP);
}
if (fragP->fr_fix != 0)
prev_frag_can_negate_branch = FALSE;
if (frag_can_negate_branch (fragP))
prev_frag_can_negate_branch = TRUE;
}
}
}
static bfd_boolean
frag_can_negate_branch (fragP)
fragS *fragP;
{
if (fragP->fr_type == rs_machine_dependent
&& fragP->fr_subtype == RELAX_IMMED)
{
TInsn t_insn;
tinsn_from_chars (&t_insn, fragP->fr_opcode);
if (is_negatable_branch (&t_insn))
return TRUE;
}
return FALSE;
}
/* Re-process all of the fragments looking to convert all of the
RELAX_ADD_NOP_IF_A0_B_RETW. If the next instruction is a
conditional branch or a retw/retw.n, convert this frag to one that
will generate a NOP. In any case close it off with a .fill 0. */
static void
xtensa_fix_a0_b_retw_frags ()
{
frchainS *frchP;
/* When this routine is called, all of the subsections are still intact
so we walk over subsections instead of sections. */
for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
{
fragS *fragP;
/* Walk over all of the fragments in a subsection. */
for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
{
if (fragP->fr_type == rs_machine_dependent
&& fragP->fr_subtype == RELAX_ADD_NOP_IF_A0_B_RETW)
{
if (next_instrs_are_b_retw (fragP))
relax_frag_add_nop (fragP);
else
frag_wane (fragP);
}
}
}
}
bfd_boolean
next_instrs_are_b_retw (fragP)
fragS * fragP;
{
xtensa_opcode opcode;
const fragS *next_fragP = next_non_empty_frag (fragP);
static xtensa_insnbuf insnbuf = NULL;
xtensa_isa isa = xtensa_default_isa;
int offset = 0;
if (!insnbuf)
insnbuf = xtensa_insnbuf_alloc (isa);
if (next_fragP == NULL)
return FALSE;
/* Check for the conditional branch. */
xtensa_insnbuf_from_chars (isa, insnbuf, &next_fragP->fr_literal[offset]);
opcode = xtensa_decode_insn (isa, insnbuf);
if (!is_conditional_branch_opcode (opcode))
return FALSE;
offset += xtensa_insn_length (isa, opcode);
if (offset == next_fragP->fr_fix)
{
next_fragP = next_non_empty_frag (next_fragP);
offset = 0;
}
if (next_fragP == NULL)
return FALSE;
/* Check for the retw/retw.n. */
xtensa_insnbuf_from_chars (isa, insnbuf, &next_fragP->fr_literal[offset]);
opcode = xtensa_decode_insn (isa, insnbuf);
if (is_windowed_return_opcode (opcode))
return TRUE;
return FALSE;
}
/* Re-process all of the fragments looking to convert all of the
RELAX_ADD_NOP_IF_PRE_LOOP_END. If there is one instruction and a
loop end label, convert this frag to one that will generate a NOP.
In any case close it off with a .fill 0. */
static void
xtensa_fix_b_j_loop_end_frags ()
{
frchainS *frchP;
/* When this routine is called, all of the subsections are still intact
so we walk over subsections instead of sections. */
for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
{
fragS *fragP;
/* Walk over all of the fragments in a subsection. */
for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
{
if (fragP->fr_type == rs_machine_dependent
&& fragP->fr_subtype == RELAX_ADD_NOP_IF_PRE_LOOP_END)
{
if (next_instr_is_loop_end (fragP))
relax_frag_add_nop (fragP);
else
frag_wane (fragP);
}
}
}
}
bfd_boolean
next_instr_is_loop_end (fragP)
fragS * fragP;
{
const fragS *next_fragP;
if (next_frag_is_loop_target (fragP))
return FALSE;
next_fragP = next_non_empty_frag (fragP);
if (next_fragP == NULL)
return FALSE;
if (!next_frag_is_loop_target (next_fragP))
return FALSE;
/* If the size is >= 3 then there is more than one instruction here.
The hardware bug will not fire. */
if (next_fragP->fr_fix > 3)
return FALSE;
return TRUE;
}
/* Re-process all of the fragments looking to convert all of the
RELAX_ADD_NOP_IF_CLOSE_LOOP_END. If there is an loop end that is
not MY loop's loop end within 12 bytes, add enough nops here to
make it at least 12 bytes away. In any case close it off with a
.fill 0. */
static void
xtensa_fix_close_loop_end_frags ()
{
frchainS *frchP;
/* When this routine is called, all of the subsections are still intact
so we walk over subsections instead of sections. */
for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
{
fragS *fragP;
fragS *current_target = NULL;
offsetT current_offset = 0;
/* Walk over all of the fragments in a subsection. */
for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
{
if (fragP->fr_type == rs_machine_dependent
&& fragP->fr_subtype == RELAX_IMMED)
{
/* Read it. If the instruction is a loop, get the target. */
xtensa_opcode opcode = get_opcode_from_buf (fragP->fr_opcode);
if (is_loop_opcode (opcode))
{
TInsn t_insn;
tinsn_from_chars (&t_insn, fragP->fr_opcode);
tinsn_immed_from_frag (&t_insn, fragP);
/* Get the current fragment target. */
if (fragP->fr_symbol)
{
current_target = symbol_get_frag (fragP->fr_symbol);
current_offset = fragP->fr_offset;
}
}
}
if (current_target
&& fragP->fr_type == rs_machine_dependent
&& fragP->fr_subtype == RELAX_ADD_NOP_IF_CLOSE_LOOP_END)
{
size_t min_bytes;
size_t bytes_added = 0;
#define REQUIRED_LOOP_DIVIDING_BYTES 12
/* Max out at 12. */
min_bytes = min_bytes_to_other_loop_end
(fragP->fr_next, current_target, current_offset,
REQUIRED_LOOP_DIVIDING_BYTES);
if (min_bytes < REQUIRED_LOOP_DIVIDING_BYTES)
{
while (min_bytes + bytes_added
< REQUIRED_LOOP_DIVIDING_BYTES)
{
int length = 3;
if (fragP->fr_var < length)
as_warn (_("fr_var %lu < length %d; ignoring"),
fragP->fr_var, length);
else
{
assemble_nop (length,
fragP->fr_literal + fragP->fr_fix);
fragP->fr_fix += length;
fragP->fr_var -= length;
}
bytes_added += length;
}
}
frag_wane (fragP);
}
}
}
}
size_t
min_bytes_to_other_loop_end (fragP, current_target, current_offset, max_size)
fragS *fragP;
fragS *current_target;
offsetT current_offset;
size_t max_size;
{
size_t offset = 0;
fragS *current_fragP;
for (current_fragP = fragP;
current_fragP;
current_fragP = current_fragP->fr_next)
{
if (current_fragP->tc_frag_data.is_loop_target
&& current_fragP != current_target)
return offset + current_offset;
offset += unrelaxed_frag_min_size (current_fragP);
if (offset + current_offset >= max_size)
return max_size;
}
return max_size;
}
size_t
unrelaxed_frag_min_size (fragP)
fragS * fragP;
{
size_t size = fragP->fr_fix;
/* add fill size */
if (fragP->fr_type == rs_fill)
size += fragP->fr_offset;
return size;
}
/* Re-process all of the fragments looking to convert all
of the RELAX_ADD_NOP_IF_SHORT_LOOP. If:
A)
1) the instruction size count to the loop end label
is too short (<= 2 instructions),
2) loop has a jump or branch in it
or B)
1) software_avoid_all_short_loops is true
2) The generating loop was a 'loopgtz' or 'loopnez'
3) the instruction size count to the loop end label is too short
(<= 2 instructions)
then convert this frag (and maybe the next one) to generate a NOP.
In any case close it off with a .fill 0. */
static void
xtensa_fix_short_loop_frags ()
{
frchainS *frchP;
/* When this routine is called, all of the subsections are still intact
so we walk over subsections instead of sections. */
for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
{
fragS *fragP;
fragS *current_target = NULL;
offsetT current_offset = 0;
xtensa_opcode current_opcode = XTENSA_UNDEFINED;
/* Walk over all of the fragments in a subsection. */
for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
{
/* check on the current loop */
if (fragP->fr_type == rs_machine_dependent
&& fragP->fr_subtype == RELAX_IMMED)
{
/* Read it. If the instruction is a loop, get the target. */
xtensa_opcode opcode = get_opcode_from_buf (fragP->fr_opcode);
if (is_loop_opcode (opcode))
{
TInsn t_insn;
tinsn_from_chars (&t_insn, fragP->fr_opcode);
tinsn_immed_from_frag (&t_insn, fragP);
/* Get the current fragment target. */
if (fragP->fr_symbol)
{
current_target = symbol_get_frag (fragP->fr_symbol);
current_offset = fragP->fr_offset;
current_opcode = opcode;
}
}
}
if (fragP->fr_type == rs_machine_dependent
&& fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP)
{
size_t insn_count =
count_insns_to_loop_end (fragP->fr_next, TRUE, 3);
if (insn_count < 3
&& (branch_before_loop_end (fragP->fr_next)
|| (software_avoid_all_short_loops
&& current_opcode != XTENSA_UNDEFINED
&& !is_the_loop_opcode (current_opcode))))
relax_frag_add_nop (fragP);
else
frag_wane (fragP);
}
}
}
}
size_t
count_insns_to_loop_end (base_fragP, count_relax_add, max_count)
fragS *base_fragP;
bfd_boolean count_relax_add;
size_t max_count;
{
fragS *fragP = NULL;
size_t insn_count = 0;
fragP = base_fragP;
for (; fragP && !fragP->tc_frag_data.is_loop_target; fragP = fragP->fr_next)
{
insn_count += unrelaxed_frag_min_insn_count (fragP);
if (insn_count >= max_count)
return max_count;
if (count_relax_add)
{
if (fragP->fr_type == rs_machine_dependent
&& fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP)
{
/* In order to add the appropriate number of
NOPs, we count an instruction for downstream
occurrences. */
insn_count++;
if (insn_count >= max_count)
return max_count;
}
}
}
return insn_count;
}
size_t
unrelaxed_frag_min_insn_count (fragP)
fragS *fragP;
{
size_t insn_count = 0;
int offset = 0;
if (!fragP->tc_frag_data.is_insn)
return insn_count;
/* Decode the fixed instructions. */
while (offset < fragP->fr_fix)
{
xtensa_opcode opcode = get_opcode_from_buf (fragP->fr_literal + offset);
if (opcode == XTENSA_UNDEFINED)
{
as_fatal (_("undecodable instruction in instruction frag"));
return insn_count;
}
offset += xtensa_insn_length (xtensa_default_isa, opcode);
insn_count++;
}
return insn_count;
}
bfd_boolean
branch_before_loop_end (base_fragP)
fragS *base_fragP;
{
fragS *fragP;
for (fragP = base_fragP;
fragP && !fragP->tc_frag_data.is_loop_target;
fragP = fragP->fr_next)
{
if (unrelaxed_frag_has_b_j (fragP))
return TRUE;
}
return FALSE;
}
bfd_boolean
unrelaxed_frag_has_b_j (fragP)
fragS *fragP;
{
size_t insn_count = 0;
int offset = 0;
if (!fragP->tc_frag_data.is_insn)
return FALSE;
/* Decode the fixed instructions. */
while (offset < fragP->fr_fix)
{
xtensa_opcode opcode = get_opcode_from_buf (fragP->fr_literal + offset);
if (opcode == XTENSA_UNDEFINED)
{
as_fatal (_("undecodable instruction in instruction frag"));
return insn_count;
}
if (is_branch_or_jump_opcode (opcode))
return TRUE;
offset += xtensa_insn_length (xtensa_default_isa, opcode);
}
return FALSE;
}
/* Checks to be made after initial assembly but before relaxation. */
static void
xtensa_sanity_check ()
{
char *file_name;
int line;
frchainS *frchP;
as_where (&file_name, &line);
for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
{
fragS *fragP;
/* Walk over all of the fragments in a subsection. */
for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
{
/* Currently we only check for empty loops here. */
if (fragP->fr_type == rs_machine_dependent
&& fragP->fr_subtype == RELAX_IMMED)
{
static xtensa_insnbuf insnbuf = NULL;
TInsn t_insn;
if (fragP->fr_opcode != NULL)
{
if (!insnbuf)
insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
tinsn_from_chars (&t_insn, fragP->fr_opcode);
tinsn_immed_from_frag (&t_insn, fragP);
if (is_loop_opcode (t_insn.opcode))
{
if (is_empty_loop (&t_insn, fragP))
{
new_logical_line (fragP->fr_file, fragP->fr_line);
as_bad (_("invalid empty loop"));
}
if (!is_local_forward_loop (&t_insn, fragP))
{
new_logical_line (fragP->fr_file, fragP->fr_line);
as_bad (_("loop target does not follow "
"loop instruction in section"));
}
}
}
}
}
}
new_logical_line (file_name, line);
}
#define LOOP_IMMED_OPN 1
/* Return true if the loop target is the next non-zero fragment. */
bfd_boolean
is_empty_loop (insn, fragP)
const TInsn *insn;
fragS *fragP;
{
const expressionS *expr;
symbolS *symbolP;
fragS *next_fragP;
if (insn->insn_type != ITYPE_INSN)
return FALSE;
if (!is_loop_opcode (insn->opcode))
return FALSE;
if (insn->ntok <= LOOP_IMMED_OPN)
return FALSE;
expr = &insn->tok[LOOP_IMMED_OPN];
if (expr->X_op != O_symbol)
return FALSE;
symbolP = expr->X_add_symbol;
if (!symbolP)
return FALSE;
if (symbol_get_frag (symbolP) == NULL)
return FALSE;
if (S_GET_VALUE (symbolP) != 0)
return FALSE;
/* Walk through the zero-size fragments from this one. If we find
the target fragment, then this is a zero-size loop. */
for (next_fragP = fragP->fr_next;
next_fragP != NULL;
next_fragP = next_fragP->fr_next)
{
if (next_fragP == symbol_get_frag (symbolP))
return TRUE;
if (next_fragP->fr_fix != 0)
return FALSE;
}
return FALSE;
}
bfd_boolean
is_local_forward_loop (insn, fragP)
const TInsn *insn;
fragS *fragP;
{
const expressionS *expr;
symbolS *symbolP;
fragS *next_fragP;
if (insn->insn_type != ITYPE_INSN)
return FALSE;
if (!is_loop_opcode (insn->opcode))
return FALSE;
if (insn->ntok <= LOOP_IMMED_OPN)
return FALSE;
expr = &insn->tok[LOOP_IMMED_OPN];
if (expr->X_op != O_symbol)
return FALSE;
symbolP = expr->X_add_symbol;
if (!symbolP)
return FALSE;
if (symbol_get_frag (symbolP) == NULL)
return FALSE;
/* Walk through fragments until we find the target.
If we do not find the target, then this is an invalid loop. */
for (next_fragP = fragP->fr_next;
next_fragP != NULL;
next_fragP = next_fragP->fr_next)
if (next_fragP == symbol_get_frag (symbolP))
return TRUE;
return FALSE;
}
/* Alignment Functions. */
size_t
get_text_align_power (target_size)
int target_size;
{
size_t i = 0;
for (i = 0; i < sizeof (size_t); i++)
{
if (target_size <= (1 << i))
return i;
}
as_fatal (_("get_text_align_power: argument too large"));
return 0;
}
addressT
get_text_align_max_fill_size (align_pow, use_nops, use_no_density)
int align_pow;
bfd_boolean use_nops;
bfd_boolean use_no_density;
{
if (!use_nops)
return (1 << align_pow);
if (use_no_density)
return 3 * (1 << align_pow);
return 1 + (1 << align_pow);
}
/* get_text_align_fill_size ()
Desired alignments:
give the address
target_size = size of next instruction
align_pow = get_text_align_power (target_size).
use_nops = 0
use_no_density = 0;
Loop alignments:
address = current address + loop instruction size;
target_size = 3 (for 2 or 3 byte target)
= 8 (for 8 byte target)
align_pow = get_text_align_power (target_size);
use_nops = 1
use_no_density = set appropriately
Text alignments:
address = current address + loop instruction size;
target_size = 0
align_pow = get_text_align_power (target_size);
use_nops = 0
use_no_density = 0. */
addressT
get_text_align_fill_size (address, align_pow, target_size,
use_nops, use_no_density)
addressT address;
int align_pow;
int target_size;
bfd_boolean use_nops;
bfd_boolean use_no_density;
{
/* Input arguments:
align_pow: log2 (required alignment).
target_size: alignment must allow the new_address and
new_address+target_size-1.
use_nops: if true, then we can only use 2 or 3 byte nops.
use_no_density: if use_nops and use_no_density, we can only use
3-byte nops.
Usually, for non-zero target_size, the align_pow is the power of 2
that is greater than or equal to the target_size. This handles the
2-byte, 3-byte and 8-byte instructions. */
size_t alignment = (1 << align_pow);
if (!use_nops)
{
/* This is the easy case. */
size_t mod;
mod = address % alignment;
if (mod != 0)
mod = alignment - mod;
assert ((address + mod) % alignment == 0);
return mod;
}
/* This is the slightly harder case. */
assert ((int) alignment >= target_size);
assert (target_size > 0);
if (!use_no_density)
{
size_t i;
for (i = 0; i < alignment * 2; i++)
{
if (i == 1)
continue;
if ((address + i) >> align_pow ==
(address + i + target_size - 1) >> align_pow)
return i;
}
}
else
{
size_t i;
/* Can only fill multiples of 3. */
for (i = 0; i <= alignment * 3; i += 3)
{
if ((address + i) >> align_pow ==
(address + i + target_size - 1) >> align_pow)
return i;
}
}
assert (0);
return 0;
}
/* This will assert if it is not possible. */
size_t
get_text_align_nop_count (fill_size, use_no_density)
size_t fill_size;
bfd_boolean use_no_density;
{
size_t count = 0;
if (use_no_density)
{
assert (fill_size % 3 == 0);
return (fill_size / 3);
}
assert (fill_size != 1); /* Bad argument. */
while (fill_size > 1)
{
size_t insn_size = 3;
if (fill_size == 2 || fill_size == 4)
insn_size = 2;
fill_size -= insn_size;
count++;
}
assert (fill_size != 1); /* Bad algorithm. */
return count;
}
size_t
get_text_align_nth_nop_size (fill_size, n, use_no_density)
size_t fill_size;
size_t n;
bfd_boolean use_no_density;
{
size_t count = 0;
assert (get_text_align_nop_count (fill_size, use_no_density) > n);
if (use_no_density)
return 3;
while (fill_size > 1)
{
size_t insn_size = 3;
if (fill_size == 2 || fill_size == 4)
insn_size = 2;
fill_size -= insn_size;
count++;
if (n + 1 == count)
return insn_size;
}
assert (0);
return 0;
}
/* For the given fragment, find the appropriate address
for it to begin at if we are using NOPs to align it. */
static addressT
get_noop_aligned_address (fragP, address)
fragS *fragP;
addressT address;
{
static xtensa_insnbuf insnbuf = NULL;
size_t fill_size = 0;
if (!insnbuf)
insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
switch (fragP->fr_type)
{
case rs_machine_dependent:
if (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE)
{
/* The rule is: get next fragment's FIRST instruction. Find
the smallest number of bytes that need to be added to
ensure that the next fragment's FIRST instruction will fit
in a single word.
E.G., 2 bytes : 0, 1, 2 mod 4
3 bytes: 0, 1 mod 4
If the FIRST instruction MIGHT be relaxed,
assume that it will become a 3 byte instruction. */
int target_insn_size;
xtensa_opcode opcode = next_frag_opcode (fragP);
addressT pre_opcode_bytes;
if (opcode == XTENSA_UNDEFINED)
{
as_bad_where (fragP->fr_file, fragP->fr_line,
_("invalid opcode for RELAX_ALIGN_NEXT_OPCODE"));
as_fatal (_("cannot continue"));
}
target_insn_size = xtensa_insn_length (xtensa_default_isa, opcode);
pre_opcode_bytes = next_frag_pre_opcode_bytes (fragP);
if (is_loop_opcode (opcode))
{
/* next_fragP should be the loop. */
const fragS *next_fragP = next_non_empty_frag (fragP);
xtensa_opcode next_opcode = next_frag_opcode (next_fragP);
size_t alignment;
pre_opcode_bytes += target_insn_size;
/* For loops, the alignment depends on the size of the
instruction following the loop, not the loop instruction. */
if (next_opcode == XTENSA_UNDEFINED)
target_insn_size = 3;
else
{
target_insn_size =
xtensa_insn_length (xtensa_default_isa, next_opcode);
if (target_insn_size == 2)
target_insn_size = 3; /* ISA specifies this. */
}
/* If it was 8, then we'll need a larger alignment
for the section. */
alignment = get_text_align_power (target_insn_size);
/* Is Now_seg valid */
record_alignment (now_seg, alignment);
}
else
as_fatal (_("expected loop opcode in relax align next target"));
fill_size = get_text_align_fill_size
(address + pre_opcode_bytes,
get_text_align_power (target_insn_size),
target_insn_size, TRUE, fragP->tc_frag_data.is_no_density);
}
break;
#if 0
case rs_align:
case rs_align_code:
fill_size = get_text_align_fill_size
(address, fragP->fr_offset, 1, TRUE,
fragP->tc_frag_data.is_no_density);
break;
#endif
default:
as_fatal (_("expected align_code or RELAX_ALIGN_NEXT_OPCODE"));
}
return address + fill_size;
}
/* 3 mechanisms for relaxing an alignment:
Align to a power of 2.
Align so the next fragment's instruction does not cross a word boundary.
Align the current instruction so that if the next instruction
were 3 bytes, it would not cross a word boundary.
We can align with:
zeros - This is easy; always insert zeros.
nops - 3 and 2 byte instructions
2 - 2 byte nop
3 - 3 byte nop
4 - 2, 2-byte nops
>=5 : 3 byte instruction + fn(n-3)
widening - widen previous instructions. */
static addressT
get_widen_aligned_address (fragP, address)
fragS *fragP;
addressT address;
{
addressT align_pow, new_address, loop_insn_offset;
fragS *next_frag;
int insn_size;
xtensa_opcode opcode, next_opcode;
static xtensa_insnbuf insnbuf = NULL;
if (!insnbuf)
insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
if (fragP->fr_type == rs_align || fragP->fr_type == rs_align_code)
{
align_pow = fragP->fr_offset;
new_address = ((address + ((1 << align_pow) - 1))
<< align_pow) >> align_pow;
return new_address;
}
if (fragP->fr_type == rs_machine_dependent)
{
switch (fragP->fr_subtype)
{
case RELAX_DESIRE_ALIGN:
/* The rule is: get the next fragment's FIRST instruction.
Find the smallest number of bytes needed to be added
in order to ensure that the next fragment is FIRST
instruction will fit in a single word.
i.e. 2 bytes : 0, 1, 2. mod 4
3 bytes: 0, 1 mod 4
If the FIRST instruction MIGHT be relaxed,
assume that it will become a 3-byte instruction. */
insn_size = 3;
/* Check to see if it might be 2 bytes. */
next_opcode = next_frag_opcode (fragP);
if (next_opcode != XTENSA_UNDEFINED
&& xtensa_insn_length (xtensa_default_isa, next_opcode) == 2)
insn_size = 2;
assert (insn_size <= 4);
for (new_address = address; new_address < address + 4; new_address++)
{
if (new_address >> 2 == (new_address + insn_size - 1) >> 2)
return new_address;
}
as_bad (_("internal error aligning"));
return address;
case RELAX_ALIGN_NEXT_OPCODE:
/* The rule is: get next fragment's FIRST instruction.
Find the smallest number of bytes needed to be added
in order to ensure that the next fragment's FIRST
instruction will fit in a single word.
i.e. 2 bytes : 0, 1, 2. mod 4
3 bytes: 0, 1 mod 4
If the FIRST instruction MIGHT be relaxed,
assume that it will become a 3 byte instruction. */
opcode = next_frag_opcode (fragP);
if (opcode == XTENSA_UNDEFINED)
{
as_bad_where (fragP->fr_file, fragP->fr_line,
_("invalid opcode for RELAX_ALIGN_NEXT_OPCODE"));
as_fatal (_("cannot continue"));
}
insn_size = xtensa_insn_length (xtensa_default_isa, opcode);
assert (insn_size <= 4);
assert (is_loop_opcode (opcode));
loop_insn_offset = 0;
next_frag = next_non_empty_frag (fragP);
/* If the loop has been expanded then the loop
instruction could be at an offset from this fragment. */
if (next_frag->fr_subtype != RELAX_IMMED)
loop_insn_offset = get_expanded_loop_offset (opcode);
for (new_address = address; new_address < address + 4; new_address++)
{
if ((new_address + loop_insn_offset + insn_size) >> 2 ==
(new_address + loop_insn_offset + insn_size + 2) >> 2)
return new_address;
}
as_bad (_("internal error aligning"));
return address;
default:
as_bad (_("internal error aligning"));
return address;
}
}
as_bad (_("internal error aligning"));
return address;
}
/* md_relax_frag Hook and Helper Functions. */
/* Return the number of bytes added to this fragment, given that the
input has been stretched already by "stretch". */
long
xtensa_relax_frag (fragP, stretch, stretched_p)
fragS *fragP;
long stretch;
int *stretched_p;
{
int unreported = fragP->tc_frag_data.unreported_expansion;
long new_stretch = 0;
char *file_name;
int line, lit_size;
as_where (&file_name, &line);
new_logical_line (fragP->fr_file, fragP->fr_line);
fragP->tc_frag_data.unreported_expansion = 0;
switch (fragP->fr_subtype)
{
case RELAX_ALIGN_NEXT_OPCODE:
/* Always convert. */
new_stretch = relax_frag_text_align (fragP, stretch);
break;
case RELAX_LOOP_END:
/* Do nothing. */
break;
case RELAX_LOOP_END_ADD_NOP:
/* Add a NOP and switch to .fill 0. */
new_stretch = relax_frag_add_nop (fragP);
break;
case RELAX_DESIRE_ALIGN:
/* We REALLY want to change the relaxation order here. This
should do NOTHING. The narrowing before it will either align
it or not. */
break;
case RELAX_LITERAL:
case RELAX_LITERAL_FINAL:
return 0;
case RELAX_LITERAL_NR:
lit_size = 4;
fragP->fr_subtype = RELAX_LITERAL_FINAL;
assert (unreported == lit_size);
memset (&fragP->fr_literal[fragP->fr_fix], 0, 4);
fragP->fr_var -= lit_size;
fragP->fr_fix += lit_size;
new_stretch = 4;
break;
case RELAX_NARROW:
new_stretch = relax_frag_narrow (fragP, stretch);
break;
case RELAX_IMMED:
case RELAX_IMMED_STEP1:
case RELAX_IMMED_STEP2:
/* Place the immediate. */
new_stretch = relax_frag_immed (now_seg, fragP, stretch,
fragP->fr_subtype - RELAX_IMMED,
stretched_p);
break;
case RELAX_LITERAL_POOL_BEGIN:
case RELAX_LITERAL_POOL_END:
/* No relaxation required. */
break;
default:
as_bad (_("bad relaxation state"));
}
new_logical_line (file_name, line);
return new_stretch;
}
static long
relax_frag_text_align (fragP, stretch)
fragS *fragP;
long stretch;
{
addressT old_address, old_next_address, old_size;
addressT new_address, new_next_address, new_size;
addressT growth;
/* Overview of the relaxation procedure for alignment
inside an executable section:
The old size is stored in the tc_frag_data.text_expansion field.
Calculate the new address, fix up the text_expansion and
return the growth. */
/* Calculate the old address of this fragment and the next fragment. */
old_address = fragP->fr_address - stretch;
old_next_address = (fragP->fr_address - stretch + fragP->fr_fix +
fragP->tc_frag_data.text_expansion);
old_size = old_next_address - old_address;
/* Calculate the new address of this fragment and the next fragment. */
new_address = fragP->fr_address;
new_next_address =
get_noop_aligned_address (fragP, fragP->fr_address + fragP->fr_fix);
new_size = new_next_address - new_address;
growth = new_size - old_size;
/* Fix up the text_expansion field and return the new growth. */
fragP->tc_frag_data.text_expansion += growth;
return growth;
}
/* Add a NOP (i.e., "or a1, a1, a1"). Use the 3-byte one because we
don't know about the availability of density yet. TODO: When the
flags are stored per fragment, use NOP.N when possible. */
static long
relax_frag_add_nop (fragP)
fragS *fragP;
{
static xtensa_insnbuf insnbuf = NULL;
TInsn t_insn;
char *nop_buf = fragP->fr_literal + fragP->fr_fix;
int length;
if (!insnbuf)
insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
tinsn_init (&t_insn);
t_insn.opcode = xtensa_or_opcode;
assert (t_insn.opcode != XTENSA_UNDEFINED);
t_insn.ntok = 3;
set_expr_const (&t_insn.tok[0], 1);
set_expr_const (&t_insn.tok[1], 1);
set_expr_const (&t_insn.tok[2], 1);
tinsn_to_insnbuf (&t_insn, insnbuf);
fragP->tc_frag_data.is_insn = TRUE;
xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, nop_buf);
length = xtensa_insn_length (xtensa_default_isa, t_insn.opcode);
if (fragP->fr_var < length)
{
as_warn (_("fr_var (%ld) < length (%d); ignoring"),
fragP->fr_var, length);
frag_wane (fragP);
return 0;
}
fragP->fr_fix += length;
fragP->fr_var -= length;
frag_wane (fragP);
return length;
}
static long
relax_frag_narrow (fragP, stretch)
fragS *fragP;
long stretch;
{
/* Overview of the relaxation procedure for alignment inside an
executable section: Find the number of widenings required and the
number of nop bytes required. Store the number of bytes ALREADY
widened. If there are enough instructions to widen (must go back
ONLY through NARROW fragments), mark each of the fragments as TO BE
widened, recalculate the fragment addresses. */
assert (fragP->fr_type == rs_machine_dependent
&& fragP->fr_subtype == RELAX_NARROW);
if (!future_alignment_required (fragP, 0))
{
/* If already expanded but no longer needed because of a prior
stretch, it is SAFE to unexpand because the next fragment will
NEVER start at an address > the previous time through the
relaxation. */
if (fragP->tc_frag_data.text_expansion)
{
if (stretch > 0)
{
fragP->tc_frag_data.text_expansion = 0;
return -1;
}
/* Otherwise we have to live with this bad choice. */
return 0;
}
return 0;
}
if (fragP->tc_frag_data.text_expansion == 0)
{
fragP->tc_frag_data.text_expansion = 1;
return 1;
}
return 0;
}
static bfd_boolean
future_alignment_required (fragP, stretch)
fragS *fragP;
long stretch;
{
long address = fragP->fr_address + stretch;
int num_widens = 0;
addressT aligned_address;
offsetT desired_diff;
while (fragP)
{
/* Limit this to a small search. */
if (num_widens > 8)
return FALSE;
address += fragP->fr_fix;
switch (fragP->fr_type)
{
case rs_fill:
address += fragP->fr_offset * fragP->fr_var;
break;
case rs_machine_dependent:
switch (fragP->fr_subtype)
{
case RELAX_NARROW:
/* address += fragP->fr_fix; */
num_widens++;
break;
case RELAX_IMMED:
address += (/* fragP->fr_fix + */
fragP->tc_frag_data.text_expansion);
break;
case RELAX_ALIGN_NEXT_OPCODE:
case RELAX_DESIRE_ALIGN:
/* address += fragP->fr_fix; */
aligned_address = get_widen_aligned_address (fragP, address);
desired_diff = aligned_address - address;
assert (desired_diff >= 0);
/* If there are enough wideners in between do it. */
/* return (num_widens == desired_diff); */
if (num_widens == desired_diff)
return TRUE;
if (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE)
return FALSE;
break;
default:
return FALSE;
}
break;
default:
return FALSE;
}
fragP = fragP->fr_next;
}
return FALSE;
}
static long
relax_frag_immed (segP, fragP, stretch, min_steps, stretched_p)
segT segP;
fragS *fragP;
long stretch;
int min_steps;
int *stretched_p;
{
static xtensa_insnbuf insnbuf = NULL;
TInsn t_insn;
int old_size;
bfd_boolean negatable_branch = FALSE;
bfd_boolean branch_jmp_to_next = FALSE;
IStack istack;
offsetT frag_offset;
int num_steps;
fragS *lit_fragP;
int num_text_bytes, num_literal_bytes;
int literal_diff, text_diff;
assert (fragP->fr_opcode != NULL);
if (!insnbuf)
insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
tinsn_from_chars (&t_insn, fragP->fr_opcode);
tinsn_immed_from_frag (&t_insn, fragP);
negatable_branch = is_negatable_branch (&t_insn);
old_size = xtensa_insn_length (xtensa_default_isa, t_insn.opcode);
if (software_avoid_b_j_loop_end)
branch_jmp_to_next = is_branch_jmp_to_next (&t_insn, fragP);
/* Special case: replace a branch to the next instruction with a NOP.
This is required to work around a hardware bug in T1040.0 and also
serves as an optimization. */
if (branch_jmp_to_next
&& ((old_size == 2) || (old_size == 3))
&& !next_frag_is_loop_target (fragP))
return 0;
/* Here is the fun stuff: Get the immediate field from this
instruction. If it fits, we are done. If not, find the next
instruction sequence that fits. */
frag_offset = fragP->fr_opcode - fragP->fr_literal;
istack_init (&istack);
num_steps = xg_assembly_relax (&istack, &t_insn, segP, fragP, frag_offset,
min_steps, stretch);
if (num_steps < min_steps)
{
as_fatal (_("internal error: relaxation failed"));
return 0;
}
if (num_steps > RELAX_IMMED_MAXSTEPS)
{
as_fatal (_("internal error: relaxation requires too many steps"));
return 0;
}
fragP->fr_subtype = (int) RELAX_IMMED + num_steps;
/* Figure out the number of bytes needed. */
lit_fragP = 0;
num_text_bytes = get_num_stack_text_bytes (&istack) - old_size;
num_literal_bytes = get_num_stack_literal_bytes (&istack);
literal_diff = num_literal_bytes - fragP->tc_frag_data.literal_expansion;
text_diff = num_text_bytes - fragP->tc_frag_data.text_expansion;
/* It MUST get larger. If not, we could get an infinite loop. */
know (num_text_bytes >= 0);
know (literal_diff >= 0 && text_diff >= 0);
fragP->tc_frag_data.text_expansion = num_text_bytes;
fragP->tc_frag_data.literal_expansion = num_literal_bytes;
/* Find the associated expandable literal for this. */
if (literal_diff != 0)
{
lit_fragP = fragP->tc_frag_data.literal_frag;
if (lit_fragP)
{
assert (literal_diff == 4);
lit_fragP->tc_frag_data.unreported_expansion += literal_diff;
/* We expect that the literal section state has NOT been
modified yet. */
assert (lit_fragP->fr_type == rs_machine_dependent
&& lit_fragP->fr_subtype == RELAX_LITERAL);
lit_fragP->fr_subtype = RELAX_LITERAL_NR;
/* We need to mark this section for another iteration
of relaxation. */
(*stretched_p)++;
}
}
/* This implicitly uses the assumption that a branch is negated
when the size of the output increases by at least 2 bytes. */
if (negatable_branch && num_text_bytes >= 2)
{
/* If next frag is a loop end, then switch it to add a NOP. */
update_next_frag_nop_state (fragP);
}
return text_diff;
}
/* md_convert_frag Hook and Helper Functions. */
void
md_convert_frag (abfd, sec, fragp)
bfd *abfd ATTRIBUTE_UNUSED;
segT sec;
fragS *fragp;
{
char *file_name;
int line;
as_where (&file_name, &line);
new_logical_line (fragp->fr_file, fragp->fr_line);
switch (fragp->fr_subtype)
{
case RELAX_ALIGN_NEXT_OPCODE:
/* Always convert. */
convert_frag_align_next_opcode (fragp);
break;
case RELAX_DESIRE_ALIGN:
/* Do nothing. If not aligned already, too bad. */
break;
case RELAX_LITERAL:
case RELAX_LITERAL_FINAL:
break;
case RELAX_NARROW:
/* No conversion. */
convert_frag_narrow (fragp);
break;
case RELAX_IMMED:
case RELAX_IMMED_STEP1:
case RELAX_IMMED_STEP2:
/* Place the immediate. */
convert_frag_immed (sec, fragp, fragp->fr_subtype - RELAX_IMMED);
break;
case RELAX_LITERAL_NR:
if (use_literal_section)
{
/* This should have been handled during relaxation. When
relaxing a code segment, literals sometimes need to be
added to the corresponding literal segment. If that
literal segment has already been relaxed, then we end up
in this situation. Marking the literal segments as data
would make this happen less often (since GAS always relaxes
code before data), but we could still get into trouble if
there are instructions in a segment that is not marked as
containing code. Until we can implement a better solution,
cheat and adjust the addresses of all the following frags.
This could break subsequent alignments, but the linker's
literal coalescing will do that anyway. */
fragS *f;
fragp->fr_subtype = RELAX_LITERAL_FINAL;
assert (fragp->tc_frag_data.unreported_expansion == 4);
memset (&fragp->fr_literal[fragp->fr_fix], 0, 4);
fragp->fr_var -= 4;
fragp->fr_fix += 4;
for (f = fragp->fr_next; f; f = f->fr_next)
f->fr_address += 4;
}
else
as_bad (_("invalid relaxation fragment result"));
break;
}
fragp->fr_var = 0;
new_logical_line (file_name, line);
}
void
convert_frag_align_next_opcode (fragp)
fragS *fragp;
{
char *nop_buf; /* Location for Writing. */
size_t i;
bfd_boolean use_no_density = fragp->tc_frag_data.is_no_density;
addressT aligned_address;
size_t fill_size, nop_count;
aligned_address = get_noop_aligned_address (fragp, fragp->fr_address +
fragp->fr_fix);
fill_size = aligned_address - (fragp->fr_address + fragp->fr_fix);
nop_count = get_text_align_nop_count (fill_size, use_no_density);
nop_buf = fragp->fr_literal + fragp->fr_fix;
for (i = 0; i < nop_count; i++)
{
size_t nop_size;
nop_size = get_text_align_nth_nop_size (fill_size, i, use_no_density);
assemble_nop (nop_size, nop_buf);
nop_buf += nop_size;
}
fragp->fr_fix += fill_size;
fragp->fr_var -= fill_size;
}
static void
convert_frag_narrow (fragP)
fragS *fragP;
{
static xtensa_insnbuf insnbuf = NULL;
TInsn t_insn, single_target;
int size, old_size, diff, error_val;
offsetT frag_offset;
if (fragP->tc_frag_data.text_expansion == 0)
{
/* No conversion. */
fragP->fr_var = 0;
return;
}
assert (fragP->fr_opcode != NULL);
if (!insnbuf)
insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
tinsn_from_chars (&t_insn, fragP->fr_opcode);
tinsn_immed_from_frag (&t_insn, fragP);
/* Just convert it to a wide form.... */
size = 0;
old_size = xtensa_insn_length (xtensa_default_isa, t_insn.opcode);
tinsn_init (&single_target);
frag_offset = fragP->fr_opcode - fragP->fr_literal;
error_val = xg_expand_narrow (&single_target, &t_insn);
if (error_val)
as_bad (_("unable to widen instruction"));
size = xtensa_insn_length (xtensa_default_isa, single_target.opcode);
xg_emit_insn_to_buf (&single_target, fragP->fr_opcode,
fragP, frag_offset, TRUE);
diff = size - old_size;
assert (diff >= 0);
assert (diff <= fragP->fr_var);
fragP->fr_var -= diff;
fragP->fr_fix += diff;
/* clean it up */
fragP->fr_var = 0;
}
static void
convert_frag_immed (segP, fragP, min_steps)
segT segP;
fragS *fragP;
int min_steps;
{
char *immed_instr = fragP->fr_opcode;
static xtensa_insnbuf insnbuf = NULL;
TInsn orig_t_insn;
bfd_boolean expanded = FALSE;
char *fr_opcode = fragP->fr_opcode;
bfd_boolean branch_jmp_to_next = FALSE;
int size;
assert (fragP->fr_opcode != NULL);
if (!insnbuf)
insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
tinsn_from_chars (&orig_t_insn, fragP->fr_opcode);
tinsn_immed_from_frag (&orig_t_insn, fragP);
/* Here is the fun stuff: Get the immediate field from this
instruction. If it fits, we're done. If not, find the next
instruction sequence that fits. */
if (software_avoid_b_j_loop_end)
branch_jmp_to_next = is_branch_jmp_to_next (&orig_t_insn, fragP);
if (branch_jmp_to_next && !next_frag_is_loop_target (fragP))
{
/* Conversion just inserts a NOP and marks the fix as completed. */
size = xtensa_insn_length (xtensa_default_isa, orig_t_insn.opcode);
assemble_nop (size, fragP->fr_opcode);
fragP->fr_var = 0;
}
else
{
IStack istack;
int i;
symbolS *lit_sym = NULL;
int total_size = 0;
int old_size;
int diff;
symbolS *gen_label = NULL;
offsetT frag_offset;
/* It does not fit. Find something that does and
convert immediately. */
frag_offset = fragP->fr_opcode - fragP->fr_literal;
istack_init (&istack);
xg_assembly_relax (&istack, &orig_t_insn,
segP, fragP, frag_offset, min_steps, 0);
old_size = xtensa_insn_length (xtensa_default_isa, orig_t_insn.opcode);
/* Assemble this right inline. */
/* First, create the mapping from a label name to the REAL label. */
total_size = 0;
for (i = 0; i < istack.ninsn; i++)
{
TInsn *t_insn = &istack.insn[i];
int size = 0;
fragS *lit_frag;
switch (t_insn->insn_type)
{
case ITYPE_LITERAL:
if (lit_sym != NULL)
as_bad (_("multiple literals in expansion"));
/* First find the appropriate space in the literal pool. */
lit_frag = fragP->tc_frag_data.literal_frag;
if (lit_frag == NULL)
as_bad (_("no registered fragment for literal"));
if (t_insn->ntok != 1)
as_bad (_("number of literal tokens != 1"));
/* Set the literal symbol and add a fixup. */
lit_sym = lit_frag->fr_symbol;
break;
case ITYPE_LABEL:
assert (gen_label == NULL);
gen_label = symbol_new (FAKE_LABEL_NAME, now_seg,
fragP->fr_opcode - fragP->fr_literal +
total_size, fragP);
break;
case ITYPE_INSN:
size = xtensa_insn_length (xtensa_default_isa, t_insn->opcode);
total_size += size;
break;
}
}
total_size = 0;
for (i = 0; i < istack.ninsn; i++)
{
TInsn *t_insn = &istack.insn[i];
fragS *lit_frag;
int size;
segT target_seg;
switch (t_insn->insn_type)
{
case ITYPE_LITERAL:
lit_frag = fragP->tc_frag_data.literal_frag;
/* already checked */
assert (lit_frag != NULL);
assert (lit_sym != NULL);
assert (t_insn->ntok == 1);
/* add a fixup */
target_seg = S_GET_SEGMENT (lit_sym);
assert (target_seg);
fix_new_exp_in_seg (target_seg, 0, lit_frag, 0, 4,
&t_insn->tok[0], FALSE, BFD_RELOC_32);
break;
case ITYPE_LABEL:
break;
case ITYPE_INSN:
xg_resolve_labels (t_insn, gen_label);
xg_resolve_literals (t_insn, lit_sym);
size = xtensa_insn_length (xtensa_default_isa, t_insn->opcode);
total_size += size;
xg_emit_insn_to_buf (t_insn, immed_instr, fragP,
immed_instr - fragP->fr_literal, TRUE);
immed_instr += size;
break;
}
}
diff = total_size - old_size;
assert (diff >= 0);
if (diff != 0)
expanded = TRUE;
assert (diff <= fragP->fr_var);
fragP->fr_var -= diff;
fragP->fr_fix += diff;
}
/* Clean it up. */
fragP->fr_var = 0;
/* Check for undefined immediates in LOOP instructions. */
if (is_loop_opcode (orig_t_insn.opcode))
{
symbolS *sym;
sym = orig_t_insn.tok[1].X_add_symbol;
if (sym != NULL && !S_IS_DEFINED (sym))
{
as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym));
return;
}
sym = orig_t_insn.tok[1].X_op_symbol;
if (sym != NULL && !S_IS_DEFINED (sym))
{
as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym));
return;
}
}
if (expanded && is_loop_opcode (orig_t_insn.opcode))
convert_frag_immed_finish_loop (segP, fragP, &orig_t_insn);
if (expanded && is_direct_call_opcode (orig_t_insn.opcode))
{
/* Add an expansion note on the expanded instruction. */
fix_new_exp_in_seg (now_seg, 0, fragP, fr_opcode - fragP->fr_literal, 4,
&orig_t_insn.tok[0], TRUE,
BFD_RELOC_XTENSA_ASM_EXPAND);
}
}
/* Add a new fix expression into the desired segment. We have to
switch to that segment to do this. */
static fixS *
fix_new_exp_in_seg (new_seg, new_subseg,
frag, where, size, exp, pcrel, r_type)
segT new_seg;
subsegT new_subseg;
fragS *frag;
int where;
int size;
expressionS *exp;
int pcrel;
bfd_reloc_code_real_type r_type;
{
fixS *new_fix;
segT seg = now_seg;
subsegT subseg = now_subseg;
assert (new_seg != 0);
subseg_set (new_seg, new_subseg);
if (r_type == BFD_RELOC_32
&& exp->X_add_symbol
&& symbol_get_tc (exp->X_add_symbol)->plt == 1)
{
r_type = BFD_RELOC_XTENSA_PLT;
}
new_fix = fix_new_exp (frag, where, size, exp, pcrel, r_type);
subseg_set (seg, subseg);
return new_fix;
}
/* Relax a loop instruction so that it can span loop >256 bytes. */
/*
loop as, .L1
.L0:
rsr as, LEND
wsr as, LBEG
addi as, as, lo8(label-.L1)
addmi as, as, mid8(label-.L1)
wsr as, LEND
isync
rsr as, LCOUNT
addi as, as, 1
.L1:
<<body>>
label: */
static void
convert_frag_immed_finish_loop (segP, fragP, t_insn)
segT segP;
fragS *fragP;
TInsn *t_insn;
{
TInsn loop_insn;
TInsn addi_insn;
TInsn addmi_insn;
unsigned long target;
static xtensa_insnbuf insnbuf = NULL;
unsigned int loop_length, loop_length_hi, loop_length_lo;
xtensa_isa isa = xtensa_default_isa;
addressT loop_offset;
addressT addi_offset = 9;
addressT addmi_offset = 12;
if (!insnbuf)
insnbuf = xtensa_insnbuf_alloc (isa);
/* Get the loop offset. */
loop_offset = get_expanded_loop_offset (t_insn->opcode);
/* Validate that there really is a LOOP at the loop_offset. */
tinsn_from_chars (&loop_insn, fragP->fr_opcode + loop_offset);
if (!is_loop_opcode (loop_insn.opcode))
{
as_bad_where (fragP->fr_file, fragP->fr_line,
_("loop relaxation specification does not correspond"));
assert (0);
}
addi_offset += loop_offset;
addmi_offset += loop_offset;
assert (t_insn->ntok == 2);
target = get_expression_value (segP, &t_insn->tok[1]);
know (symbolP);
know (symbolP->sy_frag);
know (!(S_GET_SEGMENT (symbolP) == absolute_section)
|| symbol_get_frag (symbolP) == &zero_address_frag);
loop_length = target - (fragP->fr_address + fragP->fr_fix);
loop_length_hi = loop_length & ~0x0ff;
loop_length_lo = loop_length & 0x0ff;
if (loop_length_lo >= 128)
{
loop_length_lo -= 256;
loop_length_hi += 256;
}
/* Because addmi sign-extends the immediate, 'loop_length_hi' can be at most
32512. If the loop is larger than that, then we just fail. */
if (loop_length_hi > 32512)
as_bad_where (fragP->fr_file, fragP->fr_line,
_("loop too long for LOOP instruction"));
tinsn_from_chars (&addi_insn, fragP->fr_opcode + addi_offset);
assert (addi_insn.opcode == xtensa_addi_opcode);
tinsn_from_chars (&addmi_insn, fragP->fr_opcode + addmi_offset);
assert (addmi_insn.opcode == xtensa_addmi_opcode);
set_expr_const (&addi_insn.tok[2], loop_length_lo);
tinsn_to_insnbuf (&addi_insn, insnbuf);
fragP->tc_frag_data.is_insn = TRUE;
xtensa_insnbuf_to_chars (isa, insnbuf, fragP->fr_opcode + addi_offset);
set_expr_const (&addmi_insn.tok[2], loop_length_hi);
tinsn_to_insnbuf (&addmi_insn, insnbuf);
xtensa_insnbuf_to_chars (isa, insnbuf, fragP->fr_opcode + addmi_offset);
}
static offsetT
get_expression_value (segP, exp)
segT segP;
expressionS *exp;
{
if (exp->X_op == O_constant)
return exp->X_add_number;
if (exp->X_op == O_symbol)
{
/* Find the fragment. */
symbolS *sym = exp->X_add_symbol;
assert (S_GET_SEGMENT (sym) == segP
|| S_GET_SEGMENT (sym) == absolute_section);
return (S_GET_VALUE (sym) + exp->X_add_number);
}
as_bad (_("invalid expression evaluation type %d"), exp->X_op);
return 0;
}
/* A map that keeps information on a per-subsegment basis. This is
maintained during initial assembly, but is invalid once the
subsegments are smashed together. I.E., it cannot be used during
the relaxation. */
typedef struct subseg_map_struct
{
/* the key */
segT seg;
subsegT subseg;
/* the data */
unsigned flags;
struct subseg_map_struct *next;
} subseg_map;
static subseg_map *sseg_map = NULL;
static unsigned
get_last_insn_flags (seg, subseg)
segT seg;
subsegT subseg;
{
subseg_map *subseg_e;
for (subseg_e = sseg_map; subseg_e != NULL; subseg_e = subseg_e->next)
if (seg == subseg_e->seg && subseg == subseg_e->subseg)
return subseg_e->flags;
return 0;
}
static void
set_last_insn_flags (seg, subseg, fl, val)
segT seg;
subsegT subseg;
unsigned fl;
bfd_boolean val;
{
subseg_map *subseg_e;
for (subseg_e = sseg_map; subseg_e; subseg_e = subseg_e->next)
if (seg == subseg_e->seg && subseg == subseg_e->subseg)
break;
if (!subseg_e)
{
subseg_e = (subseg_map *) xmalloc (sizeof (subseg_map));
memset (subseg_e, 0, sizeof (subseg_map));
subseg_e->seg = seg;
subseg_e->subseg = subseg;
subseg_e->flags = 0;
subseg_e->next = sseg_map;
sseg_map = subseg_e;
}
if (val)
subseg_e->flags |= fl;
else
subseg_e->flags &= ~fl;
}
/* Segment Lists and emit_state Stuff. */
/* Remove the segment from the global sections list. */
static void
xtensa_remove_section (sec)
segT sec;
{
/* Handle brain-dead bfd_section_list_remove macro, which
expect the address of the prior section's "next" field, not
just the address of the section to remove. */
segT *ps_next_ptr = &stdoutput->sections;
while (*ps_next_ptr != sec && *ps_next_ptr != NULL)
ps_next_ptr = &(*ps_next_ptr)->next;
assert (*ps_next_ptr != NULL);
bfd_section_list_remove (stdoutput, ps_next_ptr);
}
static void
xtensa_insert_section (after_sec, sec)
segT after_sec;
segT sec;
{
segT *after_sec_next;
if (after_sec == NULL)
after_sec_next = &stdoutput->sections;
else
after_sec_next = &after_sec->next;
bfd_section_list_insert (stdoutput, after_sec_next, sec);
}
static void
xtensa_move_seg_list_to_beginning (head)
seg_list *head;
{
head = head->next;
while (head)
{
segT literal_section = head->seg;
/* Move the literal section to the front of the section list. */
assert (literal_section);
xtensa_remove_section (literal_section);
xtensa_insert_section (NULL, literal_section);
head = head->next;
}
}
void
xtensa_move_literals ()
{
seg_list *segment;
frchainS *frchain_from, *frchain_to;
fragS *search_frag, *next_frag, *last_frag, *literal_pool, *insert_after;
fragS **frag_splice;
emit_state state;
segT dest_seg;
fixS *fix, *next_fix, **fix_splice;
sym_list *lit;
/* As clunky as this is, we can't rely on frag_var
and frag_variant to get called in all situations. */
segment = literal_head->next;
while (segment)
{
frchain_from = seg_info (segment->seg)->frchainP;
search_frag = frchain_from->frch_root;
while (search_frag)
{
search_frag->tc_frag_data.is_literal = TRUE;
search_frag = search_frag->fr_next;
}
segment = segment->next;
}
if (use_literal_section)
return;
segment = literal_head->next;
while (segment)
{
frchain_from = seg_info (segment->seg)->frchainP;
search_frag = frchain_from->frch_root;
literal_pool = NULL;
frchain_to = NULL;
frag_splice = &(frchain_from->frch_root);
while (!search_frag->tc_frag_data.literal_frag)
{
assert (search_frag->fr_fix == 0
|| search_frag->fr_type == rs_align);
search_frag = search_frag->fr_next;
}
assert (search_frag->tc_frag_data.literal_frag->fr_subtype
== RELAX_LITERAL_POOL_BEGIN);
xtensa_switch_section_emit_state (&state, segment->seg, 0);
/* Make sure that all the frags in this series are closed, and
that there is at least one left over of zero-size. This
prevents us from making a segment with an frchain without any
frags in it. */
frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
last_frag = frag_now;
frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
while (search_frag != frag_now)
{
next_frag = search_frag->fr_next;
/* First, move the frag out of the literal section and
to the appropriate place. */
if (search_frag->tc_frag_data.literal_frag)
{
literal_pool = search_frag->tc_frag_data.literal_frag;
assert (literal_pool->fr_subtype == RELAX_LITERAL_POOL_BEGIN);
/* Note that we set this fr_var to be a fix
chain when we created the literal pool location
as RELAX_LITERAL_POOL_BEGIN. */
frchain_to = (frchainS *) literal_pool->fr_var;
}
insert_after = literal_pool;
while (insert_after->fr_next->fr_subtype != RELAX_LITERAL_POOL_END)
insert_after = insert_after->fr_next;
dest_seg = (segT) insert_after->fr_next->fr_var;
*frag_splice = next_frag;
search_frag->fr_next = insert_after->fr_next;
insert_after->fr_next = search_frag;
search_frag->tc_frag_data.lit_seg = dest_seg;
/* Now move any fixups associated with this frag to the
right section. */
fix = frchain_from->fix_root;
fix_splice = &(frchain_from->fix_root);
while (fix)
{
next_fix = fix->fx_next;
if (fix->fx_frag == search_frag)
{
*fix_splice = next_fix;
fix->fx_next = frchain_to->fix_root;
frchain_to->fix_root = fix;
if (frchain_to->fix_tail == NULL)
frchain_to->fix_tail = fix;
}
else
fix_splice = &(fix->fx_next);
fix = next_fix;
}
search_frag = next_frag;
}
if (frchain_from->fix_root != NULL)
{
frchain_from = seg_info (segment->seg)->frchainP;
as_warn (_("fixes not all moved from %s"), segment->seg->name);
assert (frchain_from->fix_root == NULL);
}
frchain_from->fix_tail = NULL;
xtensa_restore_emit_state (&state);
segment = segment->next;
}
/* Now fix up the SEGMENT value for all the literal symbols. */
for (lit = literal_syms; lit; lit = lit->next)
{
symbolS *lit_sym = lit->sym;
segT dest_seg = symbol_get_frag (lit_sym)->tc_frag_data.lit_seg;
S_SET_SEGMENT (lit_sym, dest_seg);
}
}
static void
xtensa_reorder_seg_list (head, after)
seg_list *head;
segT after;
{
/* Move all of the sections in the section list to come
after "after" in the gnu segment list. */
head = head->next;
while (head)
{
segT literal_section = head->seg;
/* Move the literal section after "after". */
assert (literal_section);
if (literal_section != after)
{
xtensa_remove_section (literal_section);
xtensa_insert_section (after, literal_section);
}
head = head->next;
}
}
/* Push all the literal segments to the end of the gnu list. */
void
xtensa_reorder_segments ()
{
segT sec;
segT last_sec;
int old_count = 0;
int new_count = 0;
for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
old_count++;
/* Now that we have the last section, push all the literal
sections to the end. */
last_sec = get_last_sec ();
xtensa_reorder_seg_list (literal_head, last_sec);
xtensa_reorder_seg_list (init_literal_head, last_sec);
xtensa_reorder_seg_list (fini_literal_head, last_sec);
/* Now perform the final error check. */
for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
new_count++;
assert (new_count == old_count);
}
segT
get_last_sec ()
{
segT last_sec = stdoutput->sections;
while (last_sec->next != NULL)
last_sec = last_sec->next;
return last_sec;
}
/* Change the emit state (seg, subseg, and frag related stuff) to the
correct location. Return a emit_state which can be passed to
xtensa_restore_emit_state to return to current fragment. */
void
xtensa_switch_to_literal_fragment (result)
emit_state *result;
{
/* When we mark a literal pool location, we want to put a frag in
the literal pool that points to it. But to do that, we want to
switch_to_literal_fragment. But literal sections don't have
literal pools, so their location is always null, so we would
recurse forever. This is kind of hacky, but it works. */
static bfd_boolean recursive = FALSE;
fragS *pool_location = get_literal_pool_location (now_seg);
bfd_boolean is_init =
(now_seg && !strcmp (segment_name (now_seg), INIT_SECTION_NAME));
bfd_boolean is_fini =
(now_seg && !strcmp (segment_name (now_seg), FINI_SECTION_NAME));
if (pool_location == NULL
&& !use_literal_section
&& !recursive
&& !is_init && ! is_fini)
{
as_warn (_("inlining literal pool; "
"specify location with .literal_position."));
recursive = TRUE;
xtensa_mark_literal_pool_location ();
recursive = FALSE;
}
/* Special case: If we are in the ".fini" or ".init" section, then
we will ALWAYS be generating to the ".fini.literal" and
".init.literal" sections. */
if (is_init)
{
cache_literal_section (init_literal_head,
default_lit_sections.init_lit_seg_name,
&default_lit_sections.init_lit_seg);
xtensa_switch_section_emit_state (result,
default_lit_sections.init_lit_seg, 0);
}
else if (is_fini)
{
cache_literal_section (fini_literal_head,
default_lit_sections.fini_lit_seg_name,
&default_lit_sections.fini_lit_seg);
xtensa_switch_section_emit_state (result,
default_lit_sections.fini_lit_seg, 0);
}
else
{
cache_literal_section (literal_head,
default_lit_sections.lit_seg_name,
&default_lit_sections.lit_seg);
xtensa_switch_section_emit_state (result,
default_lit_sections.lit_seg, 0);
}
if (!use_literal_section &&
!is_init && !is_fini &&
get_literal_pool_location (now_seg) != pool_location)
{
/* Close whatever frag is there. */
frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
frag_now->tc_frag_data.literal_frag = pool_location;
frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
}
/* Do a 4 byte align here. */
frag_align (2, 0, 0);
}
/* Call this function before emitting data into the literal section.
This is a helper function for xtensa_switch_to_literal_fragment.
This is similar to a .section new_now_seg subseg. */
void
xtensa_switch_section_emit_state (state, new_now_seg, new_now_subseg)
emit_state *state;
segT new_now_seg;
subsegT new_now_subseg;
{
state->name = now_seg->name;
state->now_seg = now_seg;
state->now_subseg = now_subseg;
state->generating_literals = generating_literals;
generating_literals++;
subseg_new (segment_name (new_now_seg), new_now_subseg);
}
/* Use to restore the emitting into the normal place. */
void
xtensa_restore_emit_state (state)
emit_state *state;
{
generating_literals = state->generating_literals;
subseg_new (state->name, state->now_subseg);
}
/* Get a segment of a given name. If the segment is already
present, return it; otherwise, create a new one. */
static void
cache_literal_section (head, name, seg)
seg_list *head;
const char *name;
segT *seg;
{
segT current_section = now_seg;
int current_subsec = now_subseg;
if (*seg != 0)
return;
*seg = retrieve_literal_seg (head, name);
subseg_set (current_section, current_subsec);
}
/* Get a segment of a given name. If the segment is already
present, return it; otherwise, create a new one. */
static segT
retrieve_literal_seg (head, name)
seg_list *head;
const char *name;
{
segT ret = 0;
assert (head);
ret = seg_present (name);
if (!ret)
{
ret = subseg_new (name, (subsegT) 0);
add_seg_list (head, ret);
bfd_set_section_flags (stdoutput, ret, SEC_HAS_CONTENTS |
SEC_READONLY | SEC_ALLOC | SEC_LOAD | SEC_CODE);
bfd_set_section_alignment (stdoutput, ret, 2);
}
return ret;
}
/* Return a segment of a given name if it is present. */
static segT
seg_present (name)
const char *name;
{
segT seg;
seg = stdoutput->sections;
while (seg)
{
if (!strcmp (segment_name (seg), name))
return seg;
seg = seg->next;
}
return 0;
}
/* Add a segment to a segment list. */
static void
add_seg_list (head, seg)
seg_list *head;
segT seg;
{
seg_list *n;
n = (seg_list *) xmalloc (sizeof (seg_list));
assert (n);
n->seg = seg;
n->next = head->next;
head->next = n;
}
/* Set up Property Tables after Relaxation. */
#define XTENSA_INSN_SEC_NAME ".xt.insn"
#define XTENSA_LIT_SEC_NAME ".xt.lit"
void
xtensa_post_relax_hook ()
{
xtensa_move_seg_list_to_beginning (literal_head);
xtensa_move_seg_list_to_beginning (init_literal_head);
xtensa_move_seg_list_to_beginning (fini_literal_head);
xtensa_create_property_segments (get_frag_is_insn,
XTENSA_INSN_SEC_NAME,
xt_literal_sec);
if (use_literal_section)
xtensa_create_property_segments (get_frag_is_literal,
XTENSA_LIT_SEC_NAME,
xt_insn_sec);
}
static bfd_boolean
get_frag_is_literal (fragP)
const fragS *fragP;
{
assert (fragP != NULL);
return (fragP->tc_frag_data.is_literal);
}
static bfd_boolean
get_frag_is_insn (fragP)
const fragS *fragP;
{
assert (fragP != NULL);
return (fragP->tc_frag_data.is_insn);
}
static void
xtensa_create_property_segments (property_function, section_name_base,
sec_type)
frag_predicate property_function;
const char * section_name_base;
xt_section_type sec_type;
{
segT *seclist;
/* Walk over all of the current segments.
Walk over each fragment
For each fragment that has instructions
Build an instruction record (append where possible). */
for (seclist = &stdoutput->sections;
seclist && *seclist;
seclist = &(*seclist)->next)
{
segT sec = *seclist;
if (section_has_property (sec, property_function))
{
char *property_section_name =
xtensa_get_property_section_name (sec, section_name_base);
segT insn_sec = retrieve_xtensa_section (property_section_name);
segment_info_type *xt_seg_info = retrieve_segment_info (insn_sec);
xtensa_block_info **xt_blocks =
&xt_seg_info->tc_segment_info_data.blocks[sec_type];
/* Walk over all of the frchains here and add new sections. */
add_xt_block_frags (sec, insn_sec, xt_blocks, property_function);
}
}
/* Now we fill them out.... */
for (seclist = &stdoutput->sections;
seclist && *seclist;
seclist = &(*seclist)->next)
{
segment_info_type *seginfo;
xtensa_block_info *block;
segT sec = *seclist;
seginfo = seg_info (sec);
block = seginfo->tc_segment_info_data.blocks[sec_type];
if (block)
{
xtensa_block_info *cur_block;
/* This is a section with some data. */
size_t num_recs = 0;
size_t rec_size;
for (cur_block = block; cur_block; cur_block = cur_block->next)
num_recs++;
rec_size = num_recs * 8;
bfd_set_section_size (stdoutput, sec, rec_size);
/* In order to make this work with the assembler, we have to
build some frags and then build the "fixups" for it. It
would be easier to just set the contents then set the
arlents. */
if (num_recs)
{
/* Allocate a fragment and leak it. */
fragS *fragP;
size_t frag_size;
fixS *fixes;
frchainS *frchainP;
size_t i;
char *frag_data;
frag_size = sizeof (fragS) + rec_size;
fragP = (fragS *) xmalloc (frag_size);
memset (fragP, 0, frag_size);
fragP->fr_address = 0;
fragP->fr_next = NULL;
fragP->fr_fix = rec_size;
fragP->fr_var = 0;
fragP->fr_type = rs_fill;
/* the rest are zeros */
frchainP = seginfo->frchainP;
frchainP->frch_root = fragP;
frchainP->frch_last = fragP;
fixes = (fixS *) xmalloc (sizeof (fixS) * num_recs);
memset (fixes, 0, sizeof (fixS) * num_recs);
seginfo->fix_root = fixes;
seginfo->fix_tail = &fixes[num_recs - 1];
cur_block = block;
frag_data = &fragP->fr_literal[0];
for (i = 0; i < num_recs; i++)
{
fixS *fix = &fixes[i];
assert (cur_block);
/* Write the fixup. */
if (i != num_recs - 1)
fix->fx_next = &fixes[i + 1];
else
fix->fx_next = NULL;
fix->fx_size = 4;
fix->fx_done = 0;
fix->fx_frag = fragP;
fix->fx_where = i * 8;
fix->fx_addsy = section_symbol (cur_block->sec);
fix->fx_offset = cur_block->offset;
fix->fx_r_type = BFD_RELOC_32;
fix->fx_file = "Internal Assembly";
fix->fx_line = 0;
/* Write the length. */
md_number_to_chars (&frag_data[4 + 8 * i],
cur_block->size, 4);
cur_block = cur_block->next;
}
}
}
}
}
segment_info_type *
retrieve_segment_info (seg)
segT seg;
{
segment_info_type *seginfo;
seginfo = (segment_info_type *) bfd_get_section_userdata (stdoutput, seg);
if (!seginfo)
{
frchainS *frchainP;
seginfo = (segment_info_type *) xmalloc (sizeof (*seginfo));
memset ((PTR) seginfo, 0, sizeof (*seginfo));
seginfo->fix_root = NULL;
seginfo->fix_tail = NULL;
seginfo->bfd_section = seg;
seginfo->sym = 0;
/* We will not be dealing with these, only our special ones. */
#if 0
if (seg == bfd_abs_section_ptr)
abs_seg_info = seginfo;
else if (seg == bfd_und_section_ptr)
und_seg_info = seginfo;
else
#endif
bfd_set_section_userdata (stdoutput, seg, (PTR) seginfo);
#if 0
seg_fix_rootP = &segment_info[seg].fix_root;
seg_fix_tailP = &segment_info[seg].fix_tail;
#endif
frchainP = (frchainS *) xmalloc (sizeof (frchainS));
frchainP->frch_root = NULL;
frchainP->frch_last = NULL;
frchainP->frch_next = NULL;
frchainP->frch_seg = seg;
frchainP->frch_subseg = 0;
frchainP->fix_root = NULL;
frchainP->fix_tail = NULL;
/* Do not init the objstack. */
/* obstack_begin (&frchainP->frch_obstack, chunksize); */
/* frchainP->frch_frag_now = fragP; */
frchainP->frch_frag_now = NULL;
seginfo->frchainP = frchainP;
}
return seginfo;
}
segT
retrieve_xtensa_section (sec_name)
char *sec_name;
{
bfd *abfd = stdoutput;
flagword flags, out_flags, link_once_flags;
segT s;
flags = bfd_get_section_flags (abfd, now_seg);
link_once_flags = (flags & SEC_LINK_ONCE);
if (link_once_flags)
link_once_flags |= (flags & SEC_LINK_DUPLICATES);
out_flags = (SEC_RELOC | SEC_HAS_CONTENTS | SEC_READONLY | link_once_flags);
s = bfd_make_section_old_way (abfd, sec_name);
if (s == NULL)
as_bad (_("could not create section %s"), sec_name);
if (!bfd_set_section_flags (abfd, s, out_flags))
as_bad (_("invalid flag combination on section %s"), sec_name);
return s;
}
bfd_boolean
section_has_property (sec, property_function)
segT sec;
frag_predicate property_function;
{
segment_info_type *seginfo = seg_info (sec);
fragS *fragP;
if (seginfo && seginfo->frchainP)
{
for (fragP = seginfo->frchainP->frch_root; fragP; fragP = fragP->fr_next)
{
if (property_function (fragP)
&& (fragP->fr_type != rs_fill || fragP->fr_fix != 0))
return TRUE;
}
}
return FALSE;
}
/* Two types of block sections exist right now: literal and insns. */
void
add_xt_block_frags (sec, xt_block_sec, xt_block, property_function)
segT sec;
segT xt_block_sec;
xtensa_block_info **xt_block;
frag_predicate property_function;
{
segment_info_type *seg_info;
segment_info_type *xt_seg_info;
bfd_vma seg_offset;
fragS *fragP;
xt_seg_info = retrieve_segment_info (xt_block_sec);
seg_info = retrieve_segment_info (sec);
/* Build it if needed. */
while (*xt_block != NULL)
xt_block = &(*xt_block)->next;
/* We are either at NULL at the beginning or at the end. */
/* Walk through the frags. */
seg_offset = 0;
if (seg_info->frchainP)
{
for (fragP = seg_info->frchainP->frch_root;
fragP;
fragP = fragP->fr_next)
{
if (property_function (fragP)
&& (fragP->fr_type != rs_fill || fragP->fr_fix != 0))
{
if (*xt_block != NULL)
{
if ((*xt_block)->offset + (*xt_block)->size
== fragP->fr_address)
(*xt_block)->size += fragP->fr_fix;
else
xt_block = &((*xt_block)->next);
}
if (*xt_block == NULL)
{
xtensa_block_info *new_block = (xtensa_block_info *)
xmalloc (sizeof (xtensa_block_info));
new_block->sec = sec;
new_block->offset = fragP->fr_address;
new_block->size = fragP->fr_fix;
new_block->next = NULL;
*xt_block = new_block;
}
}
}
}
}
/* Instruction Stack Functions (from "xtensa-istack.h"). */
void
istack_init (stack)
IStack *stack;
{
memset (stack, 0, sizeof (IStack));
stack->ninsn = 0;
}
bfd_boolean
istack_empty (stack)
IStack *stack;
{
return (stack->ninsn == 0);
}
bfd_boolean
istack_full (stack)
IStack *stack;
{
return (stack->ninsn == MAX_ISTACK);
}
/* Return a pointer to the top IStack entry.
It is an error to call this if istack_empty () is true. */
TInsn *
istack_top (stack)
IStack *stack;
{
int rec = stack->ninsn - 1;
assert (!istack_empty (stack));
return &stack->insn[rec];
}
/* Add a new TInsn to an IStack.
It is an error to call this if istack_full () is true. */
void
istack_push (stack, insn)
IStack *stack;
TInsn *insn;
{
int rec = stack->ninsn;
assert (!istack_full (stack));
tinsn_copy (&stack->insn[rec], insn);
stack->ninsn++;
}
/* Clear space for the next TInsn on the IStack and return a pointer
to it. It is an error to call this if istack_full () is true. */
TInsn *
istack_push_space (stack)
IStack *stack;
{
int rec = stack->ninsn;
TInsn *insn;
assert (!istack_full (stack));
insn = &stack->insn[rec];
memset (insn, 0, sizeof (TInsn));
stack->ninsn++;
return insn;
}
/* Remove the last pushed instruction. It is an error to call this if
istack_empty () returns true. */
void
istack_pop (stack)
IStack *stack;
{
int rec = stack->ninsn - 1;
assert (!istack_empty (stack));
stack->ninsn--;
memset (&stack->insn[rec], 0, sizeof (TInsn));
}
/* TInsn functions. */
void
tinsn_init (dst)
TInsn *dst;
{
memset (dst, 0, sizeof (TInsn));
}
void
tinsn_copy (dst, src)
TInsn *dst;
const TInsn *src;
{
tinsn_init (dst);
memcpy (dst, src, sizeof (TInsn));
}
/* Get the ``num''th token of the TInsn.
It is illegal to call this if num > insn->ntoks. */
expressionS *
tinsn_get_tok (insn, num)
TInsn *insn;
int num;
{
assert (num < insn->ntok);
return &insn->tok[num];
}
/* Return true if ANY of the operands in the insn are symbolic. */
static bfd_boolean
tinsn_has_symbolic_operands (insn)
const TInsn *insn;
{
int i;
int n = insn->ntok;
assert (insn->insn_type == ITYPE_INSN);
for (i = 0; i < n; ++i)
{
switch (insn->tok[i].X_op)
{
case O_register:
case O_constant:
break;
default:
return TRUE;
}
}
return FALSE;
}
bfd_boolean
tinsn_has_invalid_symbolic_operands (insn)
const TInsn *insn;
{
int i;
int n = insn->ntok;
assert (insn->insn_type == ITYPE_INSN);
for (i = 0; i < n; ++i)
{
switch (insn->tok[i].X_op)
{
case O_register:
case O_constant:
break;
default:
if (i == get_relaxable_immed (insn->opcode))
break;
as_bad (_("invalid symbolic operand %d on '%s'"),
i, xtensa_opcode_name (xtensa_default_isa, insn->opcode));
return TRUE;
}
}
return FALSE;
}
/* For assembly code with complex expressions (e.g. subtraction),
we have to build them in the literal pool so that
their results are calculated correctly after relaxation.
The relaxation only handles expressions that
boil down to SYMBOL + OFFSET. */
static bfd_boolean
tinsn_has_complex_operands (insn)
const TInsn *insn;
{
int i;
int n = insn->ntok;
assert (insn->insn_type == ITYPE_INSN);
for (i = 0; i < n; ++i)
{
switch (insn->tok[i].X_op)
{
case O_register:
case O_constant:
case O_symbol:
break;
default:
return TRUE;
}
}
return FALSE;
}
/* Convert the constant operands in the t_insn to insnbuf.
Return true if there is a symbol in the immediate field.
Before this is called,
1) the number of operands are correct
2) the t_insn is a ITYPE_INSN
3) ONLY the relaxable_ is built
4) All operands are O_constant, O_symbol. All constants fit
The return value tells whether there are any remaining O_symbols. */
static bfd_boolean
tinsn_to_insnbuf (t_insn, insnbuf)
TInsn *t_insn;
xtensa_insnbuf insnbuf;
{
xtensa_isa isa = xtensa_default_isa;
xtensa_opcode opcode = t_insn->opcode;
bfd_boolean has_fixup = FALSE;
int noperands = xtensa_num_operands (isa, opcode);
int i;
uint32 opnd_value;
char *file_name;
int line;
assert (t_insn->insn_type == ITYPE_INSN);
if (noperands != t_insn->ntok)
as_fatal (_("operand number mismatch"));
xtensa_encode_insn (isa, opcode, insnbuf);
for (i = 0; i < noperands; ++i)
{
expressionS *expr = &t_insn->tok[i];
xtensa_operand operand = xtensa_get_operand (isa, opcode, i);
switch (expr->X_op)
{
case O_register:
/* The register number has already been checked in
expression_maybe_register, so we don't need to check here. */
opnd_value = expr->X_add_number;
(void) xtensa_operand_encode (operand, &opnd_value);
xtensa_operand_set_field (operand, insnbuf, opnd_value);
break;
case O_constant:
as_where (&file_name, &line);
/* It is a constant and we called this function,
then we have to try to fit it. */
xtensa_insnbuf_set_operand (insnbuf, opcode, operand,
expr->X_add_number, file_name, line);
break;
case O_symbol:
default:
has_fixup = TRUE;
break;
}
}
return has_fixup;
}
/* Check the instruction arguments. Return true on failure. */
bfd_boolean
tinsn_check_arguments (insn)
const TInsn *insn;
{
xtensa_isa isa = xtensa_default_isa;
xtensa_opcode opcode = insn->opcode;
if (opcode == XTENSA_UNDEFINED)
{
as_bad (_("invalid opcode"));
return TRUE;
}
if (xtensa_num_operands (isa, opcode) > insn->ntok)
{
as_bad (_("too few operands"));
return TRUE;
}
if (xtensa_num_operands (isa, opcode) < insn->ntok)
{
as_bad (_("too many operands"));
return TRUE;
}
return FALSE;
}
/* Load an instruction from its encoded form. */
static void
tinsn_from_chars (t_insn, f)
TInsn *t_insn;
char *f;
{
static xtensa_insnbuf insnbuf = NULL;
int i;
xtensa_opcode opcode;
xtensa_isa isa = xtensa_default_isa;
if (!insnbuf)
insnbuf = xtensa_insnbuf_alloc (isa);
xtensa_insnbuf_from_chars (isa, insnbuf, f);
opcode = xtensa_decode_insn (isa, insnbuf);
/* Find the immed. */
tinsn_init (t_insn);
t_insn->insn_type = ITYPE_INSN;
t_insn->is_specific_opcode = FALSE; /* Must not be specific. */
t_insn->opcode = opcode;
t_insn->ntok = xtensa_num_operands (isa, opcode);
for (i = 0; i < t_insn->ntok; i++)
{
set_expr_const (&t_insn->tok[i],
xtensa_insnbuf_get_operand (insnbuf, opcode, i));
}
}
/* Read the value of the relaxable immed from the fr_symbol and fr_offset. */
static void
tinsn_immed_from_frag (t_insn, fragP)
TInsn *t_insn;
fragS *fragP;
{
xtensa_opcode opcode = t_insn->opcode;
int opnum;
if (fragP->fr_symbol)
{
opnum = get_relaxable_immed (opcode);
set_expr_symbol_offset (&t_insn->tok[opnum],
fragP->fr_symbol, fragP->fr_offset);
}
}
static int
get_num_stack_text_bytes (istack)
IStack *istack;
{
int i;
int text_bytes = 0;
for (i = 0; i < istack->ninsn; i++)
{
TInsn *t_insn = &istack->insn[i];
if (t_insn->insn_type == ITYPE_INSN)
text_bytes += xg_get_insn_size (t_insn);
}
return text_bytes;
}
static int
get_num_stack_literal_bytes (istack)
IStack *istack;
{
int i;
int lit_bytes = 0;
for (i = 0; i < istack->ninsn; i++)
{
TInsn *t_insn = &istack->insn[i];
if (t_insn->insn_type == ITYPE_LITERAL && t_insn->ntok == 1)
lit_bytes += 4;
}
return lit_bytes;
}
/* Expression utilities. */
/* Return true if the expression is an integer constant. */
bfd_boolean
expr_is_const (s)
const expressionS *s;
{
return (s->X_op == O_constant);
}
/* Get the expression constant.
Calling this is illegal if expr_is_const () returns true. */
offsetT
get_expr_const (s)
const expressionS *s;
{
assert (expr_is_const (s));
return s->X_add_number;
}
/* Set the expression to a constant value. */
void
set_expr_const (s, val)
expressionS *s;
offsetT val;
{
s->X_op = O_constant;
s->X_add_number = val;
s->X_add_symbol = NULL;
s->X_op_symbol = NULL;
}
/* Set the expression to a symbol + constant offset. */
void
set_expr_symbol_offset (s, sym, offset)
expressionS *s;
symbolS *sym;
offsetT offset;
{
s->X_op = O_symbol;
s->X_add_symbol = sym;
s->X_op_symbol = NULL; /* unused */
s->X_add_number = offset;
}
bfd_boolean
expr_is_equal (s1, s2)
expressionS *s1;
expressionS *s2;
{
if (s1->X_op != s2->X_op)
return FALSE;
if (s1->X_add_symbol != s2->X_add_symbol)
return FALSE;
if (s1->X_op_symbol != s2->X_op_symbol)
return FALSE;
if (s1->X_add_number != s2->X_add_number)
return FALSE;
return TRUE;
}
static void
copy_expr (dst, src)
expressionS *dst;
const expressionS *src;
{
memcpy (dst, src, sizeof (expressionS));
}
/* Support for Tensilica's "--rename-section" option. */
#ifdef XTENSA_SECTION_RENAME
struct rename_section_struct
{
char *old_name;
char *new_name;
struct rename_section_struct *next;
};
static struct rename_section_struct *section_rename;
/* Parse the string oldname=new_name:oldname2=new_name2
and call add_section_rename. */
void
build_section_rename (arg)
const char *arg;
{
char *this_arg = NULL;
char *next_arg = NULL;
for (this_arg = strdup (arg); this_arg != NULL; this_arg = next_arg)
{
if (this_arg)
{
next_arg = strchr (this_arg, ':');
if (next_arg)
{
*next_arg = '\0';
next_arg++;
}
}
{
char *old_name = this_arg;
char *new_name = strchr (this_arg, '=');
if (*old_name == '\0')
{
as_warn (_("ignoring extra '-rename-section' delimiter ':'"));
continue;
}
if (!new_name || new_name[1] == '\0')
{
as_warn (_("ignoring invalid '-rename-section' "
"specification: '%s'"), old_name);
continue;
}
*new_name = '\0';
new_name++;
add_section_rename (old_name, new_name);
}
}
}
static void
add_section_rename (old_name, new_name)
char *old_name;
char *new_name;
{
struct rename_section_struct *r = section_rename;
/* Check for invalid section renaming. */
for (r = section_rename; r != NULL; r = r->next)
{
if (strcmp (r->old_name, old_name) == 0)
as_bad (_("section %s renamed multiple times"), old_name);
if (strcmp (r->new_name, new_name) == 0)
as_bad (_("multiple sections remapped to output section %s"),
new_name);
}
/* Now add it. */
r = (struct rename_section_struct *)
xmalloc (sizeof (struct rename_section_struct));
r->old_name = strdup (old_name);
r->new_name = strdup (new_name);
r->next = section_rename;
section_rename = r;
}
const char *
xtensa_section_rename (name)
const char *name;
{
struct rename_section_struct *r = section_rename;
for (r = section_rename; r != NULL; r = r->next)
if (strcmp (r->old_name, name) == 0)
return r->new_name;
return name;
}
#endif /* XTENSA_SECTION_RENAME */
|