summaryrefslogtreecommitdiff
path: root/gcc/config/m68hc11/m68hc11.c
blob: bd3d606f25b46aca8c3bf5c4b7de0d4f991f057b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
/* Subroutines for code generation on Motorola 68HC11 and 68HC12.
   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005
   Free Software Foundation, Inc.
   Contributed by Stephane Carrez (stcarrez@nerim.fr)

This file is part of GCC.

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

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

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.

Note:
   A first 68HC11 port was made by Otto Lind (otto@coactive.com)
   on gcc 2.6.3.  I have used it as a starting point for this port.
   However, this new port is a complete re-write.  Its internal
   design is completely different.  The generated code is not
   compatible with the gcc 2.6.3 port.

   The gcc 2.6.3 port is available at:

   ftp.unina.it/pub/electronics/motorola/68hc11/gcc/gcc-6811-fsf.tar.gz

*/

#include <stdio.h>
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "rtl.h"
#include "tree.h"
#include "tm_p.h"
#include "regs.h"
#include "hard-reg-set.h"
#include "real.h"
#include "insn-config.h"
#include "conditions.h"
#include "output.h"
#include "insn-attr.h"
#include "flags.h"
#include "recog.h"
#include "expr.h"
#include "libfuncs.h"
#include "toplev.h"
#include "basic-block.h"
#include "function.h"
#include "ggc.h"
#include "reload.h"
#include "target.h"
#include "target-def.h"

static void emit_move_after_reload (rtx, rtx, rtx);
static rtx simplify_logical (enum machine_mode, int, rtx, rtx *);
static void m68hc11_emit_logical (enum machine_mode, int, rtx *);
static void m68hc11_reorg (void);
static int go_if_legitimate_address_internal (rtx, enum machine_mode, int);
static int register_indirect_p (rtx, enum machine_mode, int);
static rtx m68hc11_expand_compare (enum rtx_code, rtx, rtx);
static int must_parenthesize (rtx);
static int m68hc11_address_cost (rtx);
static int m68hc11_shift_cost (enum machine_mode, rtx, int);
static int m68hc11_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
static bool m68hc11_rtx_costs (rtx, int, int, int *);
static tree m68hc11_handle_fntype_attribute (tree *, tree, tree, int, bool *);
const struct attribute_spec m68hc11_attribute_table[];

void create_regs_rtx (void);

static void asm_print_register (FILE *, int);
static void m68hc11_output_function_epilogue (FILE *, HOST_WIDE_INT);
static void m68hc11_asm_out_constructor (rtx, int);
static void m68hc11_asm_out_destructor (rtx, int);
static void m68hc11_file_start (void);
static void m68hc11_encode_section_info (tree, rtx, int);
static const char *m68hc11_strip_name_encoding (const char* str);
static unsigned int m68hc11_section_type_flags (tree, const char*, int);
static int autoinc_mode (rtx);
static int m68hc11_make_autoinc_notes (rtx *, void *);
static void m68hc11_init_libfuncs (void);
static rtx m68hc11_struct_value_rtx (tree, int);
static bool m68hc11_return_in_memory (tree, tree);

/* Must be set to 1 to produce debug messages.  */
int debug_m6811 = 0;

extern FILE *asm_out_file;

rtx ix_reg;
rtx iy_reg;
rtx d_reg;
rtx m68hc11_soft_tmp_reg;
static GTY(()) rtx stack_push_word;
static GTY(()) rtx stack_pop_word;
static GTY(()) rtx z_reg;
static GTY(()) rtx z_reg_qi;
static int regs_inited = 0;

/* Set to 1 by expand_prologue() when the function is an interrupt handler.  */
int current_function_interrupt;

/* Set to 1 by expand_prologue() when the function is a trap handler.  */
int current_function_trap;

/* Set to 1 when the current function is placed in 68HC12 banked
   memory and must return with rtc.  */
int current_function_far;

/* Min offset that is valid for the indirect addressing mode.  */
HOST_WIDE_INT m68hc11_min_offset = 0;

/* Max offset that is valid for the indirect addressing mode.  */
HOST_WIDE_INT m68hc11_max_offset = 256;

/* The class value for base registers.  */
enum reg_class m68hc11_base_reg_class = A_REGS;

/* The class value for index registers.  This is NO_REGS for 68HC11.  */
enum reg_class m68hc11_index_reg_class = NO_REGS;

enum reg_class m68hc11_tmp_regs_class = NO_REGS;

/* Tables that tell whether a given hard register is valid for
   a base or an index register.  It is filled at init time depending
   on the target processor.  */
unsigned char m68hc11_reg_valid_for_base[FIRST_PSEUDO_REGISTER];
unsigned char m68hc11_reg_valid_for_index[FIRST_PSEUDO_REGISTER];

/* A correction offset which is applied to the stack pointer.
   This is 1 for 68HC11 and 0 for 68HC12.  */
int m68hc11_sp_correction;

#define ADDR_STRICT       0x01  /* Accept only registers in class A_REGS  */
#define ADDR_INCDEC       0x02  /* Post/Pre inc/dec */
#define ADDR_INDEXED      0x04  /* D-reg index */
#define ADDR_OFFSET       0x08
#define ADDR_INDIRECT     0x10  /* Accept (mem (mem ...)) for [n,X] */
#define ADDR_CONST        0x20  /* Accept const and symbol_ref  */

int m68hc11_addr_mode;
int m68hc11_mov_addr_mode;

/* Comparison operands saved by the "tstxx" and "cmpxx" expand patterns.  */
rtx m68hc11_compare_op0;
rtx m68hc11_compare_op1;


const struct processor_costs *m68hc11_cost;

/* Costs for a 68HC11.  */
static const struct processor_costs m6811_cost = {
  /* add */
  COSTS_N_INSNS (2),
  /* logical */
  COSTS_N_INSNS (2),
  /* non-constant shift */
  COSTS_N_INSNS (20),
  /* shiftQI const */
  { COSTS_N_INSNS (0), COSTS_N_INSNS (1), COSTS_N_INSNS (2),
    COSTS_N_INSNS (3), COSTS_N_INSNS (4), COSTS_N_INSNS (3),
    COSTS_N_INSNS (2), COSTS_N_INSNS (1) },

  /* shiftHI const */
  { COSTS_N_INSNS (0), COSTS_N_INSNS (1), COSTS_N_INSNS (4),
    COSTS_N_INSNS (6), COSTS_N_INSNS (8), COSTS_N_INSNS (6),
    COSTS_N_INSNS (4), COSTS_N_INSNS (2),
    COSTS_N_INSNS (2), COSTS_N_INSNS (4),
    COSTS_N_INSNS (6), COSTS_N_INSNS (8), COSTS_N_INSNS (10),
    COSTS_N_INSNS (8), COSTS_N_INSNS (6), COSTS_N_INSNS (4)
  },
  /* mulQI */
  COSTS_N_INSNS (20),
  /* mulHI */
  COSTS_N_INSNS (20 * 4),
  /* mulSI */
  COSTS_N_INSNS (20 * 16),
  /* divQI */
  COSTS_N_INSNS (20),
  /* divHI */
  COSTS_N_INSNS (80),
  /* divSI */
  COSTS_N_INSNS (100)
};

/* Costs for a 68HC12.  */
static const struct processor_costs m6812_cost = {
  /* add */
  COSTS_N_INSNS (2),
  /* logical */
  COSTS_N_INSNS (2),
  /* non-constant shift */
  COSTS_N_INSNS (20),
  /* shiftQI const */
  { COSTS_N_INSNS (0), COSTS_N_INSNS (1), COSTS_N_INSNS (2),
    COSTS_N_INSNS (3), COSTS_N_INSNS (4), COSTS_N_INSNS (3),
    COSTS_N_INSNS (2), COSTS_N_INSNS (1) },

  /* shiftHI const */
  { COSTS_N_INSNS (0), COSTS_N_INSNS (1), COSTS_N_INSNS (4),
    COSTS_N_INSNS (6), COSTS_N_INSNS (8), COSTS_N_INSNS (6),
    COSTS_N_INSNS (4), COSTS_N_INSNS (2),
    COSTS_N_INSNS (2), COSTS_N_INSNS (4), COSTS_N_INSNS (6),
    COSTS_N_INSNS (8), COSTS_N_INSNS (10), COSTS_N_INSNS (8),
    COSTS_N_INSNS (6), COSTS_N_INSNS (4)
  },
  /* mulQI */
  COSTS_N_INSNS (3),
  /* mulHI */
  COSTS_N_INSNS (3),
  /* mulSI */
  COSTS_N_INSNS (3 * 4),
  /* divQI */
  COSTS_N_INSNS (12),
  /* divHI */
  COSTS_N_INSNS (12),
  /* divSI */
  COSTS_N_INSNS (100)
};

/* Initialize the GCC target structure.  */
#undef TARGET_ATTRIBUTE_TABLE
#define TARGET_ATTRIBUTE_TABLE m68hc11_attribute_table

#undef TARGET_ASM_ALIGNED_HI_OP
#define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"

#undef TARGET_ASM_FUNCTION_EPILOGUE
#define TARGET_ASM_FUNCTION_EPILOGUE m68hc11_output_function_epilogue

#undef TARGET_ASM_FILE_START
#define TARGET_ASM_FILE_START m68hc11_file_start
#undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
#define TARGET_ASM_FILE_START_FILE_DIRECTIVE true

#undef TARGET_DEFAULT_TARGET_FLAGS
#define TARGET_DEFAULT_TARGET_FLAGS TARGET_DEFAULT

#undef TARGET_ENCODE_SECTION_INFO
#define TARGET_ENCODE_SECTION_INFO  m68hc11_encode_section_info

#undef TARGET_SECTION_TYPE_FLAGS
#define TARGET_SECTION_TYPE_FLAGS m68hc11_section_type_flags

#undef TARGET_RTX_COSTS
#define TARGET_RTX_COSTS m68hc11_rtx_costs
#undef TARGET_ADDRESS_COST
#define TARGET_ADDRESS_COST m68hc11_address_cost

#undef TARGET_MACHINE_DEPENDENT_REORG
#define TARGET_MACHINE_DEPENDENT_REORG m68hc11_reorg

#undef TARGET_INIT_LIBFUNCS
#define TARGET_INIT_LIBFUNCS m68hc11_init_libfuncs

#undef TARGET_STRUCT_VALUE_RTX
#define TARGET_STRUCT_VALUE_RTX m68hc11_struct_value_rtx
#undef TARGET_RETURN_IN_MEMORY
#define TARGET_RETURN_IN_MEMORY m68hc11_return_in_memory
#undef TARGET_CALLEE_COPIES
#define TARGET_CALLEE_COPIES hook_callee_copies_named

#undef TARGET_STRIP_NAME_ENCODING
#define TARGET_STRIP_NAME_ENCODING m68hc11_strip_name_encoding

struct gcc_target targetm = TARGET_INITIALIZER;

int
m68hc11_override_options (void)
{
  memset (m68hc11_reg_valid_for_index, 0,
	  sizeof (m68hc11_reg_valid_for_index));
  memset (m68hc11_reg_valid_for_base, 0, sizeof (m68hc11_reg_valid_for_base));

  /* Compilation with -fpic generates a wrong code.  */
  if (flag_pic)
    {
      warning ("-f%s ignored for 68HC11/68HC12 (not supported)",
	       (flag_pic > 1) ? "PIC" : "pic");
      flag_pic = 0;
    }

  /* Do not enable -fweb because it breaks the 32-bit shift patterns
     by breaking the match_dup of those patterns.  The shift patterns
     will no longer be recognized after that.  */
  flag_web = 0;

  /* Configure for a 68hc11 processor.  */
  if (TARGET_M6811)
    {
      target_flags &= ~(TARGET_AUTO_INC_DEC | TARGET_MIN_MAX);
      m68hc11_cost = &m6811_cost;
      m68hc11_min_offset = 0;
      m68hc11_max_offset = 256;
      m68hc11_index_reg_class = NO_REGS;
      m68hc11_base_reg_class = A_REGS;
      m68hc11_reg_valid_for_base[HARD_X_REGNUM] = 1;
      m68hc11_reg_valid_for_base[HARD_Y_REGNUM] = 1;
      m68hc11_reg_valid_for_base[HARD_Z_REGNUM] = 1;
      m68hc11_sp_correction = 1;
      m68hc11_tmp_regs_class = D_REGS;
      m68hc11_addr_mode = ADDR_OFFSET;
      m68hc11_mov_addr_mode = 0;
      if (m68hc11_soft_reg_count < 0)
	m68hc11_soft_reg_count = 4;
    }

  /* Configure for a 68hc12 processor.  */
  if (TARGET_M6812)
    {
      m68hc11_cost = &m6812_cost;
      m68hc11_min_offset = -65536;
      m68hc11_max_offset = 65536;
      m68hc11_index_reg_class = D_REGS;
      m68hc11_base_reg_class = A_OR_SP_REGS;
      m68hc11_reg_valid_for_base[HARD_X_REGNUM] = 1;
      m68hc11_reg_valid_for_base[HARD_Y_REGNUM] = 1;
      m68hc11_reg_valid_for_base[HARD_Z_REGNUM] = 1;
      m68hc11_reg_valid_for_base[HARD_SP_REGNUM] = 1;
      m68hc11_reg_valid_for_index[HARD_D_REGNUM] = 1;
      m68hc11_sp_correction = 0;
      m68hc11_tmp_regs_class = TMP_REGS;
      m68hc11_addr_mode = ADDR_INDIRECT | ADDR_OFFSET | ADDR_CONST
        | (TARGET_AUTO_INC_DEC ? ADDR_INCDEC : 0);
      m68hc11_mov_addr_mode = ADDR_OFFSET | ADDR_CONST
        | (TARGET_AUTO_INC_DEC ? ADDR_INCDEC : 0);
      target_flags |= MASK_NO_DIRECT_MODE;
      if (m68hc11_soft_reg_count < 0)
	m68hc11_soft_reg_count = 0;

      if (TARGET_LONG_CALLS)
        current_function_far = 1;
    }
  return 0;
}


void
m68hc11_conditional_register_usage (void)
{
  int i;

  if (m68hc11_soft_reg_count > SOFT_REG_LAST - SOFT_REG_FIRST)
    m68hc11_soft_reg_count = SOFT_REG_LAST - SOFT_REG_FIRST;

  for (i = SOFT_REG_FIRST + m68hc11_soft_reg_count; i < SOFT_REG_LAST; i++)
    {
      fixed_regs[i] = 1;
      call_used_regs[i] = 1;
    }

  /* For 68HC12, the Z register emulation is not necessary when the
     frame pointer is not used.  The frame pointer is eliminated and
     replaced by the stack register (which is a BASE_REG_CLASS).  */
  if (TARGET_M6812 && flag_omit_frame_pointer && optimize)
    {
      fixed_regs[HARD_Z_REGNUM] = 1;
    }
}


/* Reload and register operations.  */


void
create_regs_rtx (void)
{
  /*  regs_inited = 1; */
  ix_reg = gen_rtx_REG (HImode, HARD_X_REGNUM);
  iy_reg = gen_rtx_REG (HImode, HARD_Y_REGNUM);
  d_reg = gen_rtx_REG (HImode, HARD_D_REGNUM);
  m68hc11_soft_tmp_reg = gen_rtx_REG (HImode, SOFT_TMP_REGNUM);

  stack_push_word = gen_rtx_MEM (HImode,
			     gen_rtx_PRE_DEC (HImode,
				      gen_rtx_REG (HImode, HARD_SP_REGNUM)));
  stack_pop_word = gen_rtx_MEM (HImode,
			    gen_rtx_POST_INC (HImode,
				     gen_rtx_REG (HImode, HARD_SP_REGNUM)));

}

/* Value is 1 if hard register REGNO can hold a value of machine-mode MODE.
    - 8 bit values are stored anywhere (except the SP register).
    - 16 bit values can be stored in any register whose mode is 16
    - 32 bit values can be stored in D, X registers or in a soft register
      (except the last one because we need 2 soft registers)
    - Values whose size is > 32 bit are not stored in real hard
      registers.  They may be stored in soft registers if there are
      enough of them.  */
int
hard_regno_mode_ok (int regno, enum machine_mode mode)
{
  switch (GET_MODE_SIZE (mode))
    {
    case 8:
      return S_REGNO_P (regno) && m68hc11_soft_reg_count >= 4;

    case 4:
      return (X_REGNO_P (regno)
	      || (S_REGNO_P (regno) && m68hc11_soft_reg_count >= 2));

    case 2:
      return G_REGNO_P (regno);

    case 1:
      /* We have to accept a QImode in X or Y registers.  Otherwise, the
         reload pass will fail when some (SUBREG:QI (REG:HI X)) are defined
         in the insns.  Reload fails if the insn rejects the register class 'a'
         as well as if it accepts it.  Patterns that failed were
         zero_extend_qihi2 and iorqi3.  */

      return G_REGNO_P (regno) && !SP_REGNO_P (regno);

    default:
      return 0;
    }
}

int
m68hc11_hard_regno_rename_ok (int reg1, int reg2)
{
  /* Don't accept renaming to Z register.  We will replace it to
     X,Y or D during machine reorg pass.  */
  if (reg2 == HARD_Z_REGNUM)
    return 0;

  /* Don't accept renaming D,X to Y register as the code will be bigger.  */
  if (TARGET_M6811 && reg2 == HARD_Y_REGNUM
      && (D_REGNO_P (reg1) || X_REGNO_P (reg1)))
    return 0;

  return 1;
}

enum reg_class
preferred_reload_class (rtx operand, enum reg_class class)
{
  enum machine_mode mode;

  mode = GET_MODE (operand);

  if (debug_m6811)
    {
      printf ("Preferred reload: (class=%s): ", reg_class_names[class]);
    }

  if (class == D_OR_A_OR_S_REGS && SP_REG_P (operand))
    return m68hc11_base_reg_class;

  if (class >= S_REGS && (GET_CODE (operand) == MEM
			  || GET_CODE (operand) == CONST_INT))
    {
      /* S_REGS class must not be used.  The movhi template does not
         work to move a memory to a soft register.
         Restrict to a hard reg.  */
      switch (class)
	{
	default:
	case G_REGS:
	case D_OR_A_OR_S_REGS:
	  class = A_OR_D_REGS;
	  break;
	case A_OR_S_REGS:
	  class = A_REGS;
	  break;
	case D_OR_SP_OR_S_REGS:
	  class = D_OR_SP_REGS;
	  break;
	case D_OR_Y_OR_S_REGS:
	  class = D_OR_Y_REGS;
	  break;
	case D_OR_X_OR_S_REGS:
	  class = D_OR_X_REGS;
	  break;
	case SP_OR_S_REGS:
	  class = SP_REGS;
	  break;
	case Y_OR_S_REGS:
	  class = Y_REGS;
	  break;
	case X_OR_S_REGS:
	  class = X_REGS;
	  break;
	case D_OR_S_REGS:
	  class = D_REGS;
	}
    }
  else if (class == Y_REGS && GET_CODE (operand) == MEM)
    {
      class = Y_REGS;
    }
  else if (class == A_OR_D_REGS && GET_MODE_SIZE (mode) == 4)
    {
      class = D_OR_X_REGS;
    }
  else if (class >= S_REGS && S_REG_P (operand))
    {
      switch (class)
	{
	default:
	case G_REGS:
	case D_OR_A_OR_S_REGS:
	  class = A_OR_D_REGS;
	  break;
	case A_OR_S_REGS:
	  class = A_REGS;
	  break;
	case D_OR_SP_OR_S_REGS:
	  class = D_OR_SP_REGS;
	  break;
	case D_OR_Y_OR_S_REGS:
	  class = D_OR_Y_REGS;
	  break;
	case D_OR_X_OR_S_REGS:
	  class = D_OR_X_REGS;
	  break;
	case SP_OR_S_REGS:
	  class = SP_REGS;
	  break;
	case Y_OR_S_REGS:
	  class = Y_REGS;
	  break;
	case X_OR_S_REGS:
	  class = X_REGS;
	  break;
	case D_OR_S_REGS:
	  class = D_REGS;
	}
    }
  else if (class >= S_REGS)
    {
      if (debug_m6811)
	{
	  printf ("Class = %s for: ", reg_class_names[class]);
	  fflush (stdout);
	  debug_rtx (operand);
	}
    }

  if (debug_m6811)
    {
      printf (" => class=%s\n", reg_class_names[class]);
      fflush (stdout);
      debug_rtx (operand);
    }

  return class;
}

/* Return 1 if the operand is a valid indexed addressing mode.
   For 68hc11:  n,r    with n in [0..255] and r in A_REGS class
   For 68hc12:  n,r    no constraint on the constant, r in A_REGS class.  */
static int
register_indirect_p (rtx operand, enum machine_mode mode, int addr_mode)
{
  rtx base, offset;

  switch (GET_CODE (operand))
    {
    case MEM:
      if ((addr_mode & ADDR_INDIRECT) && GET_MODE_SIZE (mode) <= 2)
        return register_indirect_p (XEXP (operand, 0), mode,
                                    addr_mode & (ADDR_STRICT | ADDR_OFFSET));
      return 0;

    case POST_INC:
    case PRE_INC:
    case POST_DEC:
    case PRE_DEC:
      if (addr_mode & ADDR_INCDEC)
	return register_indirect_p (XEXP (operand, 0), mode,
                                    addr_mode & ADDR_STRICT);
      return 0;

    case PLUS:
      base = XEXP (operand, 0);
      if (GET_CODE (base) == MEM)
	return 0;

      offset = XEXP (operand, 1);
      if (GET_CODE (offset) == MEM)
	return 0;

      /* Indexed addressing mode with 2 registers.  */
      if (GET_CODE (base) == REG && GET_CODE (offset) == REG)
        {
          if (!(addr_mode & ADDR_INDEXED))
            return 0;

          addr_mode &= ADDR_STRICT;
          if (REGNO_OK_FOR_BASE_P2 (REGNO (base), addr_mode)
              && REGNO_OK_FOR_INDEX_P2 (REGNO (offset), addr_mode))
            return 1;

          if (REGNO_OK_FOR_BASE_P2 (REGNO (offset), addr_mode)
              && REGNO_OK_FOR_INDEX_P2 (REGNO (base), addr_mode))
            return 1;

          return 0;
        }

      if (!(addr_mode & ADDR_OFFSET))
        return 0;

      if (GET_CODE (base) == REG)
	{
          if (!VALID_CONSTANT_OFFSET_P (offset, mode))
	    return 0;

	  if (!(addr_mode & ADDR_STRICT))
	    return 1;

	  return REGNO_OK_FOR_BASE_P2 (REGNO (base), 1);
	}

      if (GET_CODE (offset) == REG)
	{
	  if (!VALID_CONSTANT_OFFSET_P (base, mode))
	    return 0;

	  if (!(addr_mode & ADDR_STRICT))
	    return 1;

	  return REGNO_OK_FOR_BASE_P2 (REGNO (offset), 1);
	}
      return 0;

    case REG:
      return REGNO_OK_FOR_BASE_P2 (REGNO (operand), addr_mode & ADDR_STRICT);

    case CONST_INT:
      if (addr_mode & ADDR_CONST)
        return VALID_CONSTANT_OFFSET_P (operand, mode);
      return 0;

    default:
      return 0;
    }
}

/* Returns 1 if the operand fits in a 68HC11 indirect mode or in
   a 68HC12 1-byte index addressing mode.  */
int
m68hc11_small_indexed_indirect_p (rtx operand, enum machine_mode mode)
{
  rtx base, offset;
  int addr_mode;

  if (GET_CODE (operand) == REG && reload_in_progress
      && REGNO (operand) >= FIRST_PSEUDO_REGISTER
      && reg_equiv_memory_loc[REGNO (operand)])
    {
      operand = reg_equiv_memory_loc[REGNO (operand)];
      operand = eliminate_regs (operand, 0, NULL_RTX);
    }

  if (GET_CODE (operand) != MEM)
    return 0;

  operand = XEXP (operand, 0);
  if (CONSTANT_ADDRESS_P (operand))
    return 1;

  if (PUSH_POP_ADDRESS_P (operand))
    return 1;

  addr_mode = m68hc11_mov_addr_mode | (reload_completed ? ADDR_STRICT : 0);
  if (!register_indirect_p (operand, mode, addr_mode))
    return 0;

  if (TARGET_M6812 && GET_CODE (operand) == PLUS
      && (reload_completed | reload_in_progress))
    {
      base = XEXP (operand, 0);
      offset = XEXP (operand, 1);

      /* The offset can be a symbol address and this is too big
         for the operand constraint.  */
      if (GET_CODE (base) != CONST_INT && GET_CODE (offset) != CONST_INT)
        return 0;

      if (GET_CODE (base) == CONST_INT)
	offset = base;

      switch (GET_MODE_SIZE (mode))
	{
	case 8:
	  if (INTVAL (offset) < -16 + 6 || INTVAL (offset) > 15 - 6)
	    return 0;
	  break;

	case 4:
	  if (INTVAL (offset) < -16 + 2 || INTVAL (offset) > 15 - 2)
	    return 0;
	  break;

	default:
	  if (INTVAL (offset) < -16 || INTVAL (offset) > 15)
	    return 0;
	  break;
	}
    }
  return 1;
}

int
m68hc11_register_indirect_p (rtx operand, enum machine_mode mode)
{
  int addr_mode;

  if (GET_CODE (operand) == REG && reload_in_progress
      && REGNO (operand) >= FIRST_PSEUDO_REGISTER
      && reg_equiv_memory_loc[REGNO (operand)])
    {
      operand = reg_equiv_memory_loc[REGNO (operand)];
      operand = eliminate_regs (operand, 0, NULL_RTX);
    }
  if (GET_CODE (operand) != MEM)
    return 0;

  operand = XEXP (operand, 0);
  addr_mode = m68hc11_addr_mode | (reload_completed ? ADDR_STRICT : 0);
  return register_indirect_p (operand, mode, addr_mode);
}

static int
go_if_legitimate_address_internal (rtx operand, enum machine_mode mode,
                                   int strict)
{
  int addr_mode;

  if (CONSTANT_ADDRESS_P (operand) && TARGET_M6812)
    {
      /* Reject the global variables if they are too wide.  This forces
         a load of their address in a register and generates smaller code.  */
      if (GET_MODE_SIZE (mode) == 8)
	return 0;

      return 1;
    }
  addr_mode = m68hc11_addr_mode | (strict ? ADDR_STRICT : 0);
  if (register_indirect_p (operand, mode, addr_mode))
    {
      return 1;
    }
  if (PUSH_POP_ADDRESS_P (operand))
    {
      return 1;
    }
  if (symbolic_memory_operand (operand, mode))
    {
      return 1;
    }
  return 0;
}

int
m68hc11_go_if_legitimate_address (rtx operand, enum machine_mode mode,
                                  int strict)
{
  int result;

  if (debug_m6811)
    {
      printf ("Checking: ");
      fflush (stdout);
      debug_rtx (operand);
    }

  result = go_if_legitimate_address_internal (operand, mode, strict);

  if (debug_m6811)
    {
      printf (" -> %s\n", result == 0 ? "NO" : "YES");
    }

  if (result == 0)
    {
      if (debug_m6811)
	{
	  printf ("go_if_legitimate%s, ret 0: %d:",
		  (strict ? "_strict" : ""), mode);
	  fflush (stdout);
	  debug_rtx (operand);
	}
    }
  return result;
}

int
m68hc11_legitimize_address (rtx *operand ATTRIBUTE_UNUSED,
                            rtx old_operand ATTRIBUTE_UNUSED,
                            enum machine_mode mode ATTRIBUTE_UNUSED)
{
  return 0;
}


int
m68hc11_reload_operands (rtx operands[])
{
  enum machine_mode mode;

  if (regs_inited == 0)
    create_regs_rtx ();

  mode = GET_MODE (operands[1]);

  /* Input reload of indirect addressing (MEM (PLUS (REG) (CONST))).  */
  if (A_REG_P (operands[0]) && memory_reload_operand (operands[1], mode))
    {
      rtx big_offset = XEXP (XEXP (operands[1], 0), 1);
      rtx base = XEXP (XEXP (operands[1], 0), 0);

      if (GET_CODE (base) != REG)
	{
	  rtx tmp = base;
	  base = big_offset;
	  big_offset = tmp;
	}

      /* If the offset is out of range, we have to compute the address
         with a separate add instruction.  We try to do with with an 8-bit
         add on the A register.  This is possible only if the lowest part
         of the offset (i.e., big_offset % 256) is a valid constant offset
         with respect to the mode.  If it's not, we have to generate a
         16-bit add on the D register.  From:
       
         (SET (REG X (MEM (PLUS (REG X) (CONST_INT 1000)))))
       
         we generate:
        
         [(SET (REG D) (REG X)) (SET (REG X) (REG D))]
         (SET (REG A) (PLUS (REG A) (CONST_INT 1000 / 256)))
         [(SET (REG D) (REG X)) (SET (REG X) (REG D))]
         (SET (REG X) (MEM (PLUS (REG X) (CONST_INT 1000 % 256)))
       
         (SET (REG X) (PLUS (REG X) (CONST_INT 1000 / 256 * 256)))
         (SET (REG X) (MEM (PLUS (REG X) (CONST_INT 1000 % 256)))) 

      */
      if (!VALID_CONSTANT_OFFSET_P (big_offset, mode))
	{
	  int vh, vl;
	  rtx reg = operands[0];
	  rtx offset;
	  int val = INTVAL (big_offset);


	  /* We use the 'operands[0]' as a scratch register to compute the
	     address. Make sure 'base' is in that register.  */
	  if (!rtx_equal_p (base, operands[0]))
	    {
	      emit_move_insn (reg, base);
	    }

	  if (val > 0)
	    {
	      vh = val >> 8;
	      vl = val & 0x0FF;
	    }
	  else
	    {
	      vh = (val >> 8) & 0x0FF;
	      vl = val & 0x0FF;
	    }

	  /* Create the lowest part offset that still remains to be added.
	     If it's not a valid offset, do a 16-bit add.  */
	  offset = GEN_INT (vl);
	  if (!VALID_CONSTANT_OFFSET_P (offset, mode))
	    {
	      emit_insn (gen_rtx_SET (VOIDmode, reg,
				  gen_rtx_PLUS (HImode, reg, big_offset)));
	      offset = const0_rtx;
	    }
	  else
	    {
	      emit_insn (gen_rtx_SET (VOIDmode, reg,
				  gen_rtx_PLUS (HImode, reg,
					   GEN_INT (vh << 8))));
	    }
	  emit_move_insn (operands[0],
			  gen_rtx_MEM (GET_MODE (operands[1]),
				   gen_rtx_PLUS (Pmode, reg, offset)));
	  return 1;
	}
    }

  /* Use the normal gen_movhi pattern.  */
  return 0;
}

void
m68hc11_emit_libcall (const char *name, enum rtx_code code,
                      enum machine_mode dmode, enum machine_mode smode,
                      int noperands, rtx *operands)
{
  rtx ret;
  rtx insns;
  rtx libcall;
  rtx equiv;

  start_sequence ();
  libcall = gen_rtx_SYMBOL_REF (Pmode, name);
  switch (noperands)
    {
    case 2:
      ret = emit_library_call_value (libcall, NULL_RTX, LCT_CONST,
                                     dmode, 1, operands[1], smode);
      equiv = gen_rtx_fmt_e (code, dmode, operands[1]);
      break;

    case 3:
      ret = emit_library_call_value (libcall, NULL_RTX,
                                     LCT_CONST, dmode, 2,
                                     operands[1], smode, operands[2],
                                     smode);
      equiv = gen_rtx_fmt_ee (code, dmode, operands[1], operands[2]);
      break;

    default:
      abort ();
    }

  insns = get_insns ();
  end_sequence ();
  emit_libcall_block (insns, operands[0], ret, equiv);
}

/* Returns true if X is a PRE/POST increment decrement
   (same as auto_inc_p() in rtlanal.c but do not take into
   account the stack).  */
int
m68hc11_auto_inc_p (rtx x)
{
  return GET_CODE (x) == PRE_DEC
    || GET_CODE (x) == POST_INC
    || GET_CODE (x) == POST_DEC || GET_CODE (x) == PRE_INC;
}


/* Predicates for machine description.  */

int
memory_reload_operand (rtx operand, enum machine_mode mode ATTRIBUTE_UNUSED)
{
  return GET_CODE (operand) == MEM
    && GET_CODE (XEXP (operand, 0)) == PLUS
    && ((GET_CODE (XEXP (XEXP (operand, 0), 0)) == REG
	 && GET_CODE (XEXP (XEXP (operand, 0), 1)) == CONST_INT)
	|| (GET_CODE (XEXP (XEXP (operand, 0), 1)) == REG
	    && GET_CODE (XEXP (XEXP (operand, 0), 0)) == CONST_INT));
}

int
m68hc11_symbolic_p (rtx operand, enum machine_mode mode)
{
  if (GET_CODE (operand) == MEM)
    {
      rtx op = XEXP (operand, 0);

      if (symbolic_memory_operand (op, mode))
	return 1;
    }
  return 0;
}

int
m68hc11_indirect_p (rtx operand, enum machine_mode mode)
{
  if (GET_CODE (operand) == MEM && GET_MODE (operand) == mode)
    {
      rtx op = XEXP (operand, 0);
      int addr_mode;

      if (m68hc11_page0_symbol_p (op))
        return 1;

      if (symbolic_memory_operand (op, mode))
	return TARGET_M6812;

      if (reload_in_progress)
        return 1;

      operand = XEXP (operand, 0);
      addr_mode = m68hc11_addr_mode | (reload_completed ? ADDR_STRICT : 0);
      return register_indirect_p (operand, mode, addr_mode);
    }
  return 0;
}

int
memory_indexed_operand (rtx operand, enum machine_mode mode ATTRIBUTE_UNUSED)
{
  if (GET_CODE (operand) != MEM)
    return 0;

  operand = XEXP (operand, 0);
  if (GET_CODE (operand) == PLUS)
    {
      if (GET_CODE (XEXP (operand, 0)) == REG)
	operand = XEXP (operand, 0);
      else if (GET_CODE (XEXP (operand, 1)) == REG)
	operand = XEXP (operand, 1);
    }
  return GET_CODE (operand) == REG
    && (REGNO (operand) >= FIRST_PSEUDO_REGISTER
	|| A_REGNO_P (REGNO (operand)));
}

int
push_pop_operand_p (rtx operand)
{
  if (GET_CODE (operand) != MEM)
    {
      return 0;
    }
  operand = XEXP (operand, 0);
  return PUSH_POP_ADDRESS_P (operand);
}

/* Returns 1 if OP is either a symbol reference or a sum of a symbol
   reference and a constant.  */

int
symbolic_memory_operand (rtx op, enum machine_mode mode)
{
  switch (GET_CODE (op))
    {
    case SYMBOL_REF:
    case LABEL_REF:
      return 1;

    case CONST:
      op = XEXP (op, 0);
      return ((GET_CODE (XEXP (op, 0)) == SYMBOL_REF
	       || GET_CODE (XEXP (op, 0)) == LABEL_REF)
	      && GET_CODE (XEXP (op, 1)) == CONST_INT);

      /* ??? This clause seems to be irrelevant.  */
    case CONST_DOUBLE:
      return GET_MODE (op) == mode;

    case PLUS:
      return symbolic_memory_operand (XEXP (op, 0), mode)
	&& symbolic_memory_operand (XEXP (op, 1), mode);

    default:
      return 0;
    }
}

/* Emit the code to build the trampoline used to call a nested function.
   
   68HC11               68HC12

   ldy #&CXT            movw #&CXT,*_.d1
   sty *_.d1            jmp FNADDR
   jmp FNADDR

*/
void
m68hc11_initialize_trampoline (rtx tramp, rtx fnaddr, rtx cxt)
{
  const char *static_chain_reg = reg_names[STATIC_CHAIN_REGNUM];

  /* Skip the '*'.  */
  if (*static_chain_reg == '*')
    static_chain_reg++;
  if (TARGET_M6811)
    {
      emit_move_insn (gen_rtx_MEM (HImode, tramp), GEN_INT (0x18ce));
      emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, 2)), cxt);
      emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, 4)),
                      GEN_INT (0x18df));
      emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, 6)),
                      gen_rtx_CONST (QImode,
                                     gen_rtx_SYMBOL_REF (Pmode,
                                                         static_chain_reg)));
      emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, 7)),
                      GEN_INT (0x7e));
      emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, 8)), fnaddr);
    }
  else
    {
      emit_move_insn (gen_rtx_MEM (HImode, tramp), GEN_INT (0x1803));
      emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, 2)), cxt);
      emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, 4)),
                      gen_rtx_CONST (HImode,
                                     gen_rtx_SYMBOL_REF (Pmode,
                                                         static_chain_reg)));
      emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, 6)),
                      GEN_INT (0x06));
      emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, 7)), fnaddr);
    }
}

/* Declaration of types.  */

/* Handle an "tiny_data" attribute; arguments as in
   struct attribute_spec.handler.  */
static tree
m68hc11_handle_page0_attribute (tree *node, tree name,
                                tree args ATTRIBUTE_UNUSED,
                                int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
{
  tree decl = *node;

  if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
    {
      DECL_SECTION_NAME (decl) = build_string (6, ".page0");
    }
  else
    {
      warning ("%qs attribute ignored", IDENTIFIER_POINTER (name));
      *no_add_attrs = true;
    }

  return NULL_TREE;
}

const struct attribute_spec m68hc11_attribute_table[] =
{
  /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
  { "interrupt", 0, 0, false, true,  true,  m68hc11_handle_fntype_attribute },
  { "trap",      0, 0, false, true,  true,  m68hc11_handle_fntype_attribute },
  { "far",       0, 0, false, true,  true,  m68hc11_handle_fntype_attribute },
  { "near",      0, 0, false, true,  true,  m68hc11_handle_fntype_attribute },
  { "page0",     0, 0, false, false, false, m68hc11_handle_page0_attribute },
  { NULL,        0, 0, false, false, false, NULL }
};

/* Keep track of the symbol which has a `trap' attribute and which uses
   the `swi' calling convention.  Since there is only one trap, we only
   record one such symbol.  If there are several, a warning is reported.  */
static rtx trap_handler_symbol = 0;

/* Handle an attribute requiring a FUNCTION_TYPE, FIELD_DECL or TYPE_DECL;
   arguments as in struct attribute_spec.handler.  */
static tree
m68hc11_handle_fntype_attribute (tree *node, tree name,
                                 tree args ATTRIBUTE_UNUSED,
                                 int flags ATTRIBUTE_UNUSED,
                                 bool *no_add_attrs)
{
  if (TREE_CODE (*node) != FUNCTION_TYPE
      && TREE_CODE (*node) != METHOD_TYPE
      && TREE_CODE (*node) != FIELD_DECL
      && TREE_CODE (*node) != TYPE_DECL)
    {
      warning ("%qs attribute only applies to functions",
	       IDENTIFIER_POINTER (name));
      *no_add_attrs = true;
    }

  return NULL_TREE;
}
/* Undo the effects of the above.  */

static const char *
m68hc11_strip_name_encoding (const char *str)
{
  return str + (*str == '*' || *str == '@' || *str == '&');
}

static void
m68hc11_encode_label (tree decl)
{
  const char *str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
  int len = strlen (str);
  char *newstr = alloca (len + 2);

  newstr[0] = '@';
  strcpy (&newstr[1], str);

  XSTR (XEXP (DECL_RTL (decl), 0), 0) = ggc_alloc_string (newstr, len + 1);
}

/* Return 1 if this is a symbol in page0  */
int
m68hc11_page0_symbol_p (rtx x)
{
  switch (GET_CODE (x))
    {
    case SYMBOL_REF:
      return XSTR (x, 0) != 0 && XSTR (x, 0)[0] == '@';

    case CONST:
      return m68hc11_page0_symbol_p (XEXP (x, 0));

    case PLUS:
      if (!m68hc11_page0_symbol_p (XEXP (x, 0)))
        return 0;

      return GET_CODE (XEXP (x, 1)) == CONST_INT
        && INTVAL (XEXP (x, 1)) < 256
        && INTVAL (XEXP (x, 1)) >= 0;

    default:
      return 0;
    }
}

/* We want to recognize trap handlers so that we handle calls to traps
   in a special manner (by issuing the trap).  This information is stored
   in SYMBOL_REF_FLAG.  */

static void
m68hc11_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
{
  tree func_attr;
  int trap_handler;
  int is_far = 0;
  
  if (TREE_CODE (decl) == VAR_DECL)
    {
      if (lookup_attribute ("page0", DECL_ATTRIBUTES (decl)) != 0)
        m68hc11_encode_label (decl);
      return;
    }

  if (TREE_CODE (decl) != FUNCTION_DECL)
    return;

  func_attr = TYPE_ATTRIBUTES (TREE_TYPE (decl));


  if (lookup_attribute ("far", func_attr) != NULL_TREE)
    is_far = 1;
  else if (lookup_attribute ("near", func_attr) == NULL_TREE)
    is_far = TARGET_LONG_CALLS != 0;

  trap_handler = lookup_attribute ("trap", func_attr) != NULL_TREE;
  if (trap_handler && is_far)
    {
      warning ("%<trap%> and %<far%> attributes are not compatible, ignoring %<far%>");
      trap_handler = 0;
    }
  if (trap_handler)
    {
      if (trap_handler_symbol != 0)
        warning ("%<trap%> attribute is already used");
      else
        trap_handler_symbol = XEXP (rtl, 0);
    }
  SYMBOL_REF_FLAG (XEXP (rtl, 0)) = is_far;
}

static unsigned int
m68hc11_section_type_flags (tree decl, const char *name, int reloc)
{
  unsigned int flags = default_section_type_flags (decl, name, reloc);

  if (strncmp (name, ".eeprom", 7) == 0)
    {
      flags |= SECTION_WRITE | SECTION_CODE | SECTION_OVERRIDE;
    }

  return flags;
}

int
m68hc11_is_far_symbol (rtx sym)
{
  if (GET_CODE (sym) == MEM)
    sym = XEXP (sym, 0);

  return SYMBOL_REF_FLAG (sym);
}

int
m68hc11_is_trap_symbol (rtx sym)
{
  if (GET_CODE (sym) == MEM)
    sym = XEXP (sym, 0);

  return trap_handler_symbol != 0 && rtx_equal_p (trap_handler_symbol, sym);
}


/* Argument support functions.  */

/* Define the offset between two registers, one to be eliminated, and the
   other its replacement, at the start of a routine.  */
int
m68hc11_initial_elimination_offset (int from, int to)
{
  int trap_handler;
  tree func_attr;
  int size;
  int regno;

  /* For a trap handler, we must take into account the registers which
     are pushed on the stack during the trap (except the PC).  */
  func_attr = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
  current_function_interrupt = lookup_attribute ("interrupt",
						 func_attr) != NULL_TREE;
  trap_handler = lookup_attribute ("trap", func_attr) != NULL_TREE;

  if (lookup_attribute ("far", func_attr) != 0)
    current_function_far = 1;
  else if (lookup_attribute ("near", func_attr) != 0)
    current_function_far = 0;
  else
    current_function_far = (TARGET_LONG_CALLS != 0
                            && !current_function_interrupt
                            && !trap_handler);

  if (trap_handler && from == ARG_POINTER_REGNUM)
    size = 7;

  /* For a function using 'call/rtc' we must take into account the
     page register which is pushed in the call.  */
  else if (current_function_far && from == ARG_POINTER_REGNUM)
    size = 1;
  else
    size = 0;

  if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
    {
      /* 2 is for the saved frame.
         1 is for the 'sts' correction when creating the frame.  */
      return get_frame_size () + 2 + m68hc11_sp_correction + size;
    }

  if (from == FRAME_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
    {
      return m68hc11_sp_correction;
    }

  /* Push any 2 byte pseudo hard registers that we need to save.  */
  for (regno = SOFT_REG_FIRST; regno < SOFT_REG_LAST; regno++)
    {
      if (regs_ever_live[regno] && !call_used_regs[regno])
	{
	  size += 2;
	}
    }

  if (from == ARG_POINTER_REGNUM && to == HARD_SP_REGNUM)
    {
      return get_frame_size () + size;
    }

  if (from == FRAME_POINTER_REGNUM && to == HARD_SP_REGNUM)
    {
      return size;
    }
  return 0;
}

/* Initialize a variable CUM of type CUMULATIVE_ARGS
   for a call to a function whose data type is FNTYPE.
   For a library call, FNTYPE is 0.  */

void
m68hc11_init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype, rtx libname)
{
  tree ret_type;

  z_replacement_completed = 0;
  cum->words = 0;
  cum->nregs = 0;

  /* For a library call, we must find out the type of the return value.
     When the return value is bigger than 4 bytes, it is returned in
     memory.  In that case, the first argument of the library call is a
     pointer to the memory location.  Because the first argument is passed in
     register D, we have to identify this, so that the first function
     parameter is not passed in D either.  */
  if (fntype == 0)
    {
      const char *name;
      size_t len;

      if (libname == 0 || GET_CODE (libname) != SYMBOL_REF)
	return;

      /* If the library ends in 'di' or in 'df', we assume it's
         returning some DImode or some DFmode which are 64-bit wide.  */
      name = XSTR (libname, 0);
      len = strlen (name);
      if (len > 3
	  && ((name[len - 2] == 'd'
	       && (name[len - 1] == 'f' || name[len - 1] == 'i'))
	      || (name[len - 3] == 'd'
		  && (name[len - 2] == 'i' || name[len - 2] == 'f'))))
	{
	  /* We are in.  Mark the first parameter register as already used.  */
	  cum->words = 1;
	  cum->nregs = 1;
	}
      return;
    }

  ret_type = TREE_TYPE (fntype);

  if (ret_type && aggregate_value_p (ret_type, fntype))
    {
      cum->words = 1;
      cum->nregs = 1;
    }
}

/* Update the data in CUM to advance over an argument
   of mode MODE and data type TYPE.
   (TYPE is null for libcalls where that information may not be available.)  */

void
m68hc11_function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
                              tree type, int named ATTRIBUTE_UNUSED)
{
  if (mode != BLKmode)
    {
      if (cum->words == 0 && GET_MODE_SIZE (mode) == 4)
	{
	  cum->nregs = 2;
	  cum->words = GET_MODE_SIZE (mode);
	}
      else
	{
	  cum->words += GET_MODE_SIZE (mode);
	  if (cum->words <= HARD_REG_SIZE)
	    cum->nregs = 1;
	}
    }
  else
    {
      cum->words += int_size_in_bytes (type);
    }
  return;
}

/* Define where to put the arguments to a function.
   Value is zero to push the argument on the stack,
   or a hard register in which to store the argument.

   MODE is the argument's machine mode.
   TYPE is the data type of the argument (as a tree).
    This is null for libcalls where that information may
    not be available.
   CUM is a variable of type CUMULATIVE_ARGS which gives info about
    the preceding args and about the function being called.
   NAMED is nonzero if this argument is a named parameter
    (otherwise it is an extra parameter matching an ellipsis).  */

struct rtx_def *
m68hc11_function_arg (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
                      tree type ATTRIBUTE_UNUSED, int named ATTRIBUTE_UNUSED)
{
  if (cum->words != 0)
    {
      return NULL_RTX;
    }

  if (mode != BLKmode)
    {
      if (GET_MODE_SIZE (mode) == 2 * HARD_REG_SIZE)
	return gen_rtx_REG (mode, HARD_X_REGNUM);

      if (GET_MODE_SIZE (mode) > HARD_REG_SIZE)
	{
	  return NULL_RTX;
	}
      return gen_rtx_REG (mode, HARD_D_REGNUM);
    }
  return NULL_RTX;
}

/* If defined, a C expression which determines whether, and in which direction,
   to pad out an argument with extra space.  The value should be of type
   `enum direction': either `upward' to pad above the argument,
   `downward' to pad below, or `none' to inhibit padding.

   Structures are stored left shifted in their argument slot.  */
int
m68hc11_function_arg_padding (enum machine_mode mode, tree type)
{
  if (type != 0 && AGGREGATE_TYPE_P (type))
    return upward;

  /* Fall back to the default.  */
  return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
}


/* Function prologue and epilogue.  */

/* Emit a move after the reload pass has completed.  This is used to
   emit the prologue and epilogue.  */
static void
emit_move_after_reload (rtx to, rtx from, rtx scratch)
{
  rtx insn;

  if (TARGET_M6812 || H_REG_P (to) || H_REG_P (from))
    {
      insn = emit_move_insn (to, from);
    }
  else
    {
      emit_move_insn (scratch, from);
      insn = emit_move_insn (to, scratch);
    }

  /* Put a REG_INC note to tell the flow analysis that the instruction
     is necessary.  */
  if (IS_STACK_PUSH (to))
    {
      REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_INC,
					    XEXP (XEXP (to, 0), 0),
					    REG_NOTES (insn));
    }
  else if (IS_STACK_POP (from))
    {
      REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_INC,
					    XEXP (XEXP (from, 0), 0),
					    REG_NOTES (insn));
    }

  /* For 68HC11, put a REG_INC note on `sts _.frame' to prevent the cse-reg
     to think that sp == _.frame and later replace a x = sp with x = _.frame.
     The problem is that we are lying to gcc and use `txs' for x = sp
     (which is not really true because txs is really x = sp + 1).  */
  else if (TARGET_M6811 && SP_REG_P (from))
    {
      REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_INC,
					    from,
					    REG_NOTES (insn));
    }
}

int
m68hc11_total_frame_size (void)
{
  int size;
  int regno;

  size = get_frame_size ();
  if (current_function_interrupt)
    {
      size += 3 * HARD_REG_SIZE;
    }
  if (frame_pointer_needed)
    size += HARD_REG_SIZE;

  for (regno = SOFT_REG_FIRST; regno <= SOFT_REG_LAST; regno++)
    if (regs_ever_live[regno] && !call_used_regs[regno])
      size += HARD_REG_SIZE;

  return size;
}

static void
m68hc11_output_function_epilogue (FILE *out ATTRIBUTE_UNUSED,
                                  HOST_WIDE_INT size ATTRIBUTE_UNUSED)
{
  /* We catch the function epilogue generation to have a chance
     to clear the z_replacement_completed flag.  */
  z_replacement_completed = 0;
}

void
expand_prologue (void)
{
  tree func_attr;
  int size;
  int regno;
  rtx scratch;

  if (reload_completed != 1)
    abort ();

  size = get_frame_size ();

  create_regs_rtx ();

  /* Generate specific prologue for interrupt handlers.  */
  func_attr = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
  current_function_interrupt = lookup_attribute ("interrupt",
						 func_attr) != NULL_TREE;
  current_function_trap = lookup_attribute ("trap", func_attr) != NULL_TREE;
  if (lookup_attribute ("far", func_attr) != NULL_TREE)
    current_function_far = 1;
  else if (lookup_attribute ("near", func_attr) != NULL_TREE)
    current_function_far = 0;
  else
    current_function_far = (TARGET_LONG_CALLS != 0
                            && !current_function_interrupt
                            && !current_function_trap);

  /* Get the scratch register to build the frame and push registers.
     If the first argument is a 32-bit quantity, the D+X registers
     are used.  Use Y to compute the frame.  Otherwise, X is cheaper.
     For 68HC12, this scratch register is not used.  */
  if (current_function_args_info.nregs == 2)
    scratch = iy_reg;
  else
    scratch = ix_reg;

  /* Save current stack frame.  */
  if (frame_pointer_needed)
    emit_move_after_reload (stack_push_word, hard_frame_pointer_rtx, scratch);

  /* For an interrupt handler, we must preserve _.tmp, _.z and _.xy.
     Other soft registers in page0 need not to be saved because they
     will be restored by C functions.  For a trap handler, we don't
     need to preserve these registers because this is a synchronous call.  */
  if (current_function_interrupt)
    {
      emit_move_after_reload (stack_push_word, m68hc11_soft_tmp_reg, scratch);
      emit_move_after_reload (stack_push_word,
			      gen_rtx_REG (HImode, SOFT_Z_REGNUM), scratch);
      emit_move_after_reload (stack_push_word,
			      gen_rtx_REG (HImode, SOFT_SAVED_XY_REGNUM),
			      scratch);
    }

  /* Allocate local variables.  */
  if (TARGET_M6812 && (size > 4 || size == 3))
    {
      emit_insn (gen_addhi3 (stack_pointer_rtx,
			     stack_pointer_rtx, GEN_INT (-size)));
    }
  else if ((!optimize_size && size > 8) || (optimize_size && size > 10))
    {
      rtx insn;

      insn = gen_rtx_PARALLEL
	(VOIDmode,
	 gen_rtvec (2,
		    gen_rtx_SET (VOIDmode,
				 stack_pointer_rtx,
				 gen_rtx_PLUS (HImode,
					       stack_pointer_rtx,
					       GEN_INT (-size))),
		    gen_rtx_CLOBBER (VOIDmode, scratch)));
      emit_insn (insn);
    }
  else
    {
      int i;

      /* Allocate by pushing scratch values.  */
      for (i = 2; i <= size; i += 2)
	emit_move_after_reload (stack_push_word, ix_reg, 0);

      if (size & 1)
	emit_insn (gen_addhi3 (stack_pointer_rtx,
			       stack_pointer_rtx, constm1_rtx));
    }

  /* Create the frame pointer.  */
  if (frame_pointer_needed)
    emit_move_after_reload (hard_frame_pointer_rtx,
			    stack_pointer_rtx, scratch);

  /* Push any 2 byte pseudo hard registers that we need to save.  */
  for (regno = SOFT_REG_FIRST; regno <= SOFT_REG_LAST; regno++)
    {
      if (regs_ever_live[regno] && !call_used_regs[regno])
	{
	  emit_move_after_reload (stack_push_word,
				  gen_rtx_REG (HImode, regno), scratch);
	}
    }
}

void
expand_epilogue (void)
{
  int size;
  register int regno;
  int return_size;
  rtx scratch;

  if (reload_completed != 1)
    abort ();

  size = get_frame_size ();

  /* If we are returning a value in two registers, we have to preserve the
     X register and use the Y register to restore the stack and the saved
     registers.  Otherwise, use X because it's faster (and smaller).  */
  if (current_function_return_rtx == 0)
    return_size = 0;
  else if (GET_CODE (current_function_return_rtx) == MEM)
    return_size = HARD_REG_SIZE;
  else
    return_size = GET_MODE_SIZE (GET_MODE (current_function_return_rtx));

  if (return_size > HARD_REG_SIZE && return_size <= 2 * HARD_REG_SIZE)
    scratch = iy_reg;
  else
    scratch = ix_reg;

  /* Pop any 2 byte pseudo hard registers that we saved.  */
  for (regno = SOFT_REG_LAST; regno >= SOFT_REG_FIRST; regno--)
    {
      if (regs_ever_live[regno] && !call_used_regs[regno])
	{
	  emit_move_after_reload (gen_rtx_REG (HImode, regno),
				  stack_pop_word, scratch);
	}
    }

  /* de-allocate auto variables */
  if (TARGET_M6812 && (size > 4 || size == 3))
    {
      emit_insn (gen_addhi3 (stack_pointer_rtx,
			     stack_pointer_rtx, GEN_INT (size)));
    }
  else if ((!optimize_size && size > 8) || (optimize_size && size > 10))
    {
      rtx insn;

      insn = gen_rtx_PARALLEL
	(VOIDmode,
	 gen_rtvec (2,
		    gen_rtx_SET (VOIDmode,
				 stack_pointer_rtx,
				 gen_rtx_PLUS (HImode,
					       stack_pointer_rtx,
					       GEN_INT (size))),
		    gen_rtx_CLOBBER (VOIDmode, scratch)));
      emit_insn (insn);
    }
  else
    {
      int i;

      for (i = 2; i <= size; i += 2)
	emit_move_after_reload (scratch, stack_pop_word, scratch);
      if (size & 1)
	emit_insn (gen_addhi3 (stack_pointer_rtx,
			       stack_pointer_rtx, const1_rtx));
    }

  /* For an interrupt handler, restore ZTMP, ZREG and XYREG.  */
  if (current_function_interrupt)
    {
      emit_move_after_reload (gen_rtx_REG (HImode, SOFT_SAVED_XY_REGNUM),
			      stack_pop_word, scratch);
      emit_move_after_reload (gen_rtx_REG (HImode, SOFT_Z_REGNUM),
			      stack_pop_word, scratch);
      emit_move_after_reload (m68hc11_soft_tmp_reg, stack_pop_word, scratch);
    }

  /* Restore previous frame pointer.  */
  if (frame_pointer_needed)
    emit_move_after_reload (hard_frame_pointer_rtx, stack_pop_word, scratch);

  /* If the trap handler returns some value, copy the value
     in D, X onto the stack so that the rti will pop the return value
     correctly.  */
  else if (current_function_trap && return_size != 0)
    {
      rtx addr_reg = stack_pointer_rtx;

      if (!TARGET_M6812)
	{
	  emit_move_after_reload (scratch, stack_pointer_rtx, 0);
	  addr_reg = scratch;
	}
      emit_move_after_reload (gen_rtx_MEM (HImode,
				       gen_rtx_PLUS (HImode, addr_reg,
						const1_rtx)), d_reg, 0);
      if (return_size > HARD_REG_SIZE)
	emit_move_after_reload (gen_rtx_MEM (HImode,
					 gen_rtx_PLUS (HImode, addr_reg,
						  GEN_INT (3))), ix_reg, 0);
    }

  emit_jump_insn (gen_return ());
}


/* Low and High part extraction for 68HC11.  These routines are
   similar to gen_lowpart and gen_highpart but they have been
   fixed to work for constants and 68HC11 specific registers.  */

rtx
m68hc11_gen_lowpart (enum machine_mode mode, rtx x)
{
  /* We assume that the low part of an auto-inc mode is the same with
     the mode changed and that the caller split the larger mode in the
     correct order.  */
  if (GET_CODE (x) == MEM && m68hc11_auto_inc_p (XEXP (x, 0)))
    {
      return gen_rtx_MEM (mode, XEXP (x, 0));
    }

  /* Note that a CONST_DOUBLE rtx could represent either an integer or a
     floating-point constant.  A CONST_DOUBLE is used whenever the
     constant requires more than one word in order to be adequately
     represented.  */
  if (GET_CODE (x) == CONST_DOUBLE)
    {
      long l[2];

      if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
	{
	  REAL_VALUE_TYPE r;

	  if (GET_MODE (x) == SFmode)
	    {
	      REAL_VALUE_FROM_CONST_DOUBLE (r, x);
	      REAL_VALUE_TO_TARGET_SINGLE (r, l[0]);
	    }
	  else
	    {
	      rtx first, second;

	      split_double (x, &first, &second);
	      return second;
	    }
	  if (mode == SImode)
	    return GEN_INT (l[0]);

	  return gen_int_mode (l[0], HImode);
	}
      else
	{
	  l[0] = CONST_DOUBLE_LOW (x);
	}
      if (mode == SImode)
	return GEN_INT (l[0]);
      else if (mode == HImode && GET_MODE (x) == SFmode)
	return gen_int_mode (l[0], HImode);
      else
	abort ();
    }

  if (mode == QImode && D_REG_P (x))
    return gen_rtx_REG (mode, HARD_B_REGNUM);

  /* gen_lowpart crashes when it is called with a SUBREG.  */
  if (GET_CODE (x) == SUBREG && SUBREG_BYTE (x) != 0)
    {
      if (mode == SImode)
	return gen_rtx_SUBREG (mode, SUBREG_REG (x), SUBREG_BYTE (x) + 4);
      else if (mode == HImode)
	return gen_rtx_SUBREG (mode, SUBREG_REG (x), SUBREG_BYTE (x) + 2);
      else
	abort ();
    }
  x = gen_lowpart (mode, x);

  /* Return a different rtx to avoid to share it in several insns
     (when used by a split pattern).  Sharing addresses within
     a MEM breaks the Z register replacement (and reloading).  */
  if (GET_CODE (x) == MEM)
    x = copy_rtx (x);
  return x;
}

rtx
m68hc11_gen_highpart (enum machine_mode mode, rtx x)
{
  /* We assume that the high part of an auto-inc mode is the same with
     the mode changed and that the caller split the larger mode in the
     correct order.  */
  if (GET_CODE (x) == MEM && m68hc11_auto_inc_p (XEXP (x, 0)))
    {
      return gen_rtx_MEM (mode, XEXP (x, 0));
    }

  /* Note that a CONST_DOUBLE rtx could represent either an integer or a
     floating-point constant.  A CONST_DOUBLE is used whenever the
     constant requires more than one word in order to be adequately
     represented.  */
  if (GET_CODE (x) == CONST_DOUBLE)
    {
      long l[2];

      if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
	{
	  REAL_VALUE_TYPE r;

	  if (GET_MODE (x) == SFmode)
	    {
	      REAL_VALUE_FROM_CONST_DOUBLE (r, x);
	      REAL_VALUE_TO_TARGET_SINGLE (r, l[1]);
	    }
	  else
	    {
	      rtx first, second;

	      split_double (x, &first, &second);
	      return first;
	    }
	  if (mode == SImode)
	    return GEN_INT (l[1]);

	  return gen_int_mode ((l[1] >> 16), HImode);
	}
      else
	{
	  l[1] = CONST_DOUBLE_HIGH (x);
	}

      if (mode == SImode)
	return GEN_INT (l[1]);
      else if (mode == HImode && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
	return gen_int_mode ((l[0] >> 16), HImode);
      else
	abort ();
    }
  if (GET_CODE (x) == CONST_INT)
    {
      HOST_WIDE_INT val = INTVAL (x);

      if (mode == QImode)
	{
	  return gen_int_mode (val >> 8, QImode);
	}
      else if (mode == HImode)
	{
	  return gen_int_mode (val >> 16, HImode);
	}
    }
  if (mode == QImode && D_REG_P (x))
    return gen_rtx_REG (mode, HARD_A_REGNUM);

  /* There is no way in GCC to represent the upper part of a word register.
     To obtain the 8-bit upper part of a soft register, we change the
     reg into a mem rtx.  This is possible because they are physically
     located in memory.  There is no offset because we are big-endian.  */
  if (mode == QImode && S_REG_P (x))
    {
      int pos;

      /* Avoid the '*' for direct addressing mode when this
         addressing mode is disabled.  */
      pos = TARGET_NO_DIRECT_MODE ? 1 : 0;
      return gen_rtx_MEM (QImode,
		      gen_rtx_SYMBOL_REF (Pmode,
			       &reg_names[REGNO (x)][pos]));
    }

  /* gen_highpart crashes when it is called with a SUBREG.  */
  if (GET_CODE (x) == SUBREG)
    {
      return gen_rtx_SUBREG (mode, XEXP (x, 0), XEXP (x, 1));
    }
  if (GET_CODE (x) == REG)
    {
      if (REGNO (x) < FIRST_PSEUDO_REGISTER)
        return gen_rtx_REG (mode, REGNO (x));
      else
        return gen_rtx_SUBREG (mode, x, 0);
    }

  if (GET_CODE (x) == MEM)
    {
      x = change_address (x, mode, 0);

      /* Return a different rtx to avoid to share it in several insns
	 (when used by a split pattern).  Sharing addresses within
	 a MEM breaks the Z register replacement (and reloading).  */
      if (GET_CODE (x) == MEM)
	x = copy_rtx (x);
      return x;
    }
  abort ();
}


/* Obscure register manipulation.  */

/* Finds backward in the instructions to see if register 'reg' is
   dead.  This is used when generating code to see if we can use 'reg'
   as a scratch register.  This allows us to choose a better generation
   of code when we know that some register dies or can be clobbered.  */

int
dead_register_here (rtx x, rtx reg)
{
  rtx x_reg;
  rtx p;

  if (D_REG_P (reg))
    x_reg = gen_rtx_REG (SImode, HARD_X_REGNUM);
  else
    x_reg = 0;

  for (p = PREV_INSN (x); p && GET_CODE (p) != CODE_LABEL; p = PREV_INSN (p))
    if (INSN_P (p))
      {
	rtx body;

	body = PATTERN (p);

	if (GET_CODE (body) == CALL_INSN)
	  break;
	if (GET_CODE (body) == JUMP_INSN)
	  break;

	if (GET_CODE (body) == SET)
	  {
	    rtx dst = XEXP (body, 0);

	    if (GET_CODE (dst) == REG && REGNO (dst) == REGNO (reg))
	      break;
	    if (x_reg && rtx_equal_p (dst, x_reg))
	      break;

	    if (find_regno_note (p, REG_DEAD, REGNO (reg)))
	      return 1;
	  }
	else if (reg_mentioned_p (reg, p)
		 || (x_reg && reg_mentioned_p (x_reg, p)))
	  break;
      }

  /* Scan forward to see if the register is set in some insns and never
     used since then.  */
  for (p = x /*NEXT_INSN (x) */ ; p; p = NEXT_INSN (p))
    {
      rtx body;

      if (GET_CODE (p) == CODE_LABEL
	  || GET_CODE (p) == JUMP_INSN
	  || GET_CODE (p) == CALL_INSN || GET_CODE (p) == BARRIER)
	break;

      if (GET_CODE (p) != INSN)
	continue;

      body = PATTERN (p);
      if (GET_CODE (body) == SET)
	{
	  rtx src = XEXP (body, 1);
	  rtx dst = XEXP (body, 0);

	  if (GET_CODE (dst) == REG
	      && REGNO (dst) == REGNO (reg) && !reg_mentioned_p (reg, src))
	    return 1;
	}

      /* Register is used (may be in source or in dest).  */
      if (reg_mentioned_p (reg, p)
	  || (x_reg != 0 && GET_MODE (p) == SImode
	      && reg_mentioned_p (x_reg, p)))
	break;
    }
  return p == 0 ? 1 : 0;
}


/* Code generation operations called from machine description file.  */

/* Print the name of register 'regno' in the assembly file.  */
static void
asm_print_register (FILE *file, int regno)
{
  const char *name = reg_names[regno];

  if (TARGET_NO_DIRECT_MODE && name[0] == '*')
    name++;

  fprintf (file, "%s", name);
}

/* A C compound statement to output to stdio stream STREAM the
   assembler syntax for an instruction operand X.  X is an RTL
   expression.

   CODE is a value that can be used to specify one of several ways
   of printing the operand.  It is used when identical operands
   must be printed differently depending on the context.  CODE
   comes from the `%' specification that was used to request
   printing of the operand.  If the specification was just `%DIGIT'
   then CODE is 0; if the specification was `%LTR DIGIT' then CODE
   is the ASCII code for LTR.

   If X is a register, this macro should print the register's name.
   The names can be found in an array `reg_names' whose type is
   `char *[]'.  `reg_names' is initialized from `REGISTER_NAMES'.

   When the machine description has a specification `%PUNCT' (a `%'
   followed by a punctuation character), this macro is called with
   a null pointer for X and the punctuation character for CODE.

   The M68HC11 specific codes are:

   'b' for the low part of the operand.
   'h' for the high part of the operand
       The 'b' or 'h' modifiers have no effect if the operand has
       the QImode and is not a S_REG_P (soft register).  If the
       operand is a hard register, these two modifiers have no effect.
   't' generate the temporary scratch register.  The operand is
       ignored.
   'T' generate the low-part temporary scratch register.  The operand is
       ignored.  */

void
print_operand (FILE *file, rtx op, int letter)
{
  if (letter == 't')
    {
      asm_print_register (file, SOFT_TMP_REGNUM);
      return;
    }
  else if (letter == 'T')
    {
      asm_print_register (file, SOFT_TMP_REGNUM);
      fprintf (file, "+1");
      return;
    }
  else if (letter == '#')
    {
      asm_fprintf (file, "%I");
    }

  if (GET_CODE (op) == REG)
    {
      if (letter == 'b' && S_REG_P (op))
	{
	  asm_print_register (file, REGNO (op));
	  fprintf (file, "+1");
	}
      else if (letter == 'b' && D_REG_P (op))
	{
	  asm_print_register (file, HARD_B_REGNUM);
	}
      else
	{
	  asm_print_register (file, REGNO (op));
	}
      return;
    }

  if (GET_CODE (op) == SYMBOL_REF && (letter == 'b' || letter == 'h'))
    {
      if (letter == 'b')
	asm_fprintf (file, "%I%%lo(");
      else
	asm_fprintf (file, "%I%%hi(");

      output_addr_const (file, op);
      fprintf (file, ")");
      return;
    }

  /* Get the low or high part of the operand when 'b' or 'h' modifiers
     are specified.  If we already have a QImode, there is nothing to do.  */
  if (GET_MODE (op) == HImode || GET_MODE (op) == VOIDmode)
    {
      if (letter == 'b')
	{
	  op = m68hc11_gen_lowpart (QImode, op);
	}
      else if (letter == 'h')
	{
	  op = m68hc11_gen_highpart (QImode, op);
	}
    }

  if (GET_CODE (op) == MEM)
    {
      rtx base = XEXP (op, 0);
      switch (GET_CODE (base))
	{
	case PRE_DEC:
	  if (TARGET_M6812)
	    {
	      fprintf (file, "%u,-", GET_MODE_SIZE (GET_MODE (op)));
	      asm_print_register (file, REGNO (XEXP (base, 0)));
	    }
	  else
	    abort ();
	  break;

	case POST_DEC:
	  if (TARGET_M6812)
	    {
	      fprintf (file, "%u,", GET_MODE_SIZE (GET_MODE (op)));
	      asm_print_register (file, REGNO (XEXP (base, 0)));
	      fprintf (file, "-");
	    }
	  else
	    abort ();
	  break;

	case POST_INC:
	  if (TARGET_M6812)
	    {
	      fprintf (file, "%u,", GET_MODE_SIZE (GET_MODE (op)));
	      asm_print_register (file, REGNO (XEXP (base, 0)));
	      fprintf (file, "+");
	    }
	  else
	    abort ();
	  break;

	case PRE_INC:
	  if (TARGET_M6812)
	    {
	      fprintf (file, "%u,+", GET_MODE_SIZE (GET_MODE (op)));
	      asm_print_register (file, REGNO (XEXP (base, 0)));
	    }
	  else
	    abort ();
	  break;

        case MEM:
          if (TARGET_M6812)
            {
              fprintf (file, "[");
              print_operand_address (file, XEXP (base, 0));
              fprintf (file, "]");
            }
          else
            abort ();
          break;

	default:
          if (m68hc11_page0_symbol_p (base))
            fprintf (file, "*");

	  output_address (base);
	  break;
	}
    }
  else if (GET_CODE (op) == CONST_DOUBLE && GET_MODE (op) == SFmode)
    {
      REAL_VALUE_TYPE r;
      long l;

      REAL_VALUE_FROM_CONST_DOUBLE (r, op);
      REAL_VALUE_TO_TARGET_SINGLE (r, l);
      asm_fprintf (file, "%I0x%lx", l);
    }
  else if (GET_CODE (op) == CONST_DOUBLE && GET_MODE (op) == DFmode)
    {
      char dstr[30];

      real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (op),
		       sizeof (dstr), 0, 1);
      asm_fprintf (file, "%I0r%s", dstr);
    }
  else
    {
      int need_parenthesize = 0;

      if (letter != 'i')
	asm_fprintf (file, "%I");
      else
        need_parenthesize = must_parenthesize (op);

      if (need_parenthesize)
        fprintf (file, "(");

      output_addr_const (file, op);
      if (need_parenthesize)
        fprintf (file, ")");
    }
}

/* Returns true if the operand 'op' must be printed with parenthesis
   around it.  This must be done only if there is a symbol whose name
   is a processor register.  */
static int
must_parenthesize (rtx op)
{
  const char *name;

  switch (GET_CODE (op))
    {
    case SYMBOL_REF:
      name = XSTR (op, 0);
      /* Avoid a conflict between symbol name and a possible
         register.  */
      return (strcasecmp (name, "a") == 0
	      || strcasecmp (name, "b") == 0
	      || strcasecmp (name, "d") == 0
	      || strcasecmp (name, "x") == 0
	      || strcasecmp (name, "y") == 0
	      || strcasecmp (name, "ix") == 0
	      || strcasecmp (name, "iy") == 0
	      || strcasecmp (name, "pc") == 0
	      || strcasecmp (name, "sp") == 0
	      || strcasecmp (name, "ccr") == 0) ? 1 : 0;

    case PLUS:
    case MINUS:
      return must_parenthesize (XEXP (op, 0))
	|| must_parenthesize (XEXP (op, 1));

    case MEM:
    case CONST:
    case ZERO_EXTEND:
    case SIGN_EXTEND:
      return must_parenthesize (XEXP (op, 0));

    case CONST_DOUBLE:
    case CONST_INT:
    case LABEL_REF:
    case CODE_LABEL:
    default:
      return 0;
    }
}

/* A C compound statement to output to stdio stream STREAM the
   assembler syntax for an instruction operand that is a memory
   reference whose address is ADDR.  ADDR is an RTL expression.  */

void
print_operand_address (FILE *file, rtx addr)
{
  rtx base;
  rtx offset;
  int need_parenthesis = 0;

  switch (GET_CODE (addr))
    {
    case REG:
      if (!REG_P (addr) || !REG_OK_FOR_BASE_STRICT_P (addr))
	abort ();

      fprintf (file, "0,");
      asm_print_register (file, REGNO (addr));
      break;

    case MEM:
      base = XEXP (addr, 0);
      switch (GET_CODE (base))
	{
	case PRE_DEC:
	  if (TARGET_M6812)
	    {
	      fprintf (file, "%u,-", GET_MODE_SIZE (GET_MODE (addr)));
	      asm_print_register (file, REGNO (XEXP (base, 0)));
	    }
	  else
	    abort ();
	  break;

	case POST_DEC:
	  if (TARGET_M6812)
	    {
	      fprintf (file, "%u,", GET_MODE_SIZE (GET_MODE (addr)));
	      asm_print_register (file, REGNO (XEXP (base, 0)));
	      fprintf (file, "-");
	    }
	  else
	    abort ();
	  break;

	case POST_INC:
	  if (TARGET_M6812)
	    {
	      fprintf (file, "%u,", GET_MODE_SIZE (GET_MODE (addr)));
	      asm_print_register (file, REGNO (XEXP (base, 0)));
	      fprintf (file, "+");
	    }
	  else
	    abort ();
	  break;

	case PRE_INC:
	  if (TARGET_M6812)
	    {
	      fprintf (file, "%u,+", GET_MODE_SIZE (GET_MODE (addr)));
	      asm_print_register (file, REGNO (XEXP (base, 0)));
	    }
	  else
	    abort ();
	  break;

	default:
	  need_parenthesis = must_parenthesize (base);
	  if (need_parenthesis)
	    fprintf (file, "(");

	  output_addr_const (file, base);
	  if (need_parenthesis)
	    fprintf (file, ")");
	  break;
	}
      break;

    case PLUS:
      base = XEXP (addr, 0);
      offset = XEXP (addr, 1);
      if (!G_REG_P (base) && G_REG_P (offset))
	{
	  base = XEXP (addr, 1);
	  offset = XEXP (addr, 0);
	}
      if ((CONSTANT_ADDRESS_P (base)) && (CONSTANT_ADDRESS_P (offset)))
	{
	  need_parenthesis = must_parenthesize (addr);

	  if (need_parenthesis)
	    fprintf (file, "(");

	  output_addr_const (file, base);
	  fprintf (file, "+");
	  output_addr_const (file, offset);
	  if (need_parenthesis)
	    fprintf (file, ")");
	}
      else if (REG_P (base) && REG_OK_FOR_BASE_STRICT_P (base))
	{
	  if (REG_P (offset))
	    {
	      if (TARGET_M6812)
		{
		  asm_print_register (file, REGNO (offset));
		  fprintf (file, ",");
		  asm_print_register (file, REGNO (base));
		}
	      else
		abort ();
	    }
	  else
	    {
              need_parenthesis = must_parenthesize (offset);
              if (need_parenthesis)
                fprintf (file, "(");

	      output_addr_const (file, offset);
              if (need_parenthesis)
                fprintf (file, ")");
	      fprintf (file, ",");
	      asm_print_register (file, REGNO (base));
	    }
	}
      else
	{
	  abort ();
	}
      break;

    default:
      if (GET_CODE (addr) == CONST_INT
	  && INTVAL (addr) < 0x8000 && INTVAL (addr) >= -0x8000)
	{
	  fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (addr));
	}
      else
	{
	  need_parenthesis = must_parenthesize (addr);
	  if (need_parenthesis)
	    fprintf (file, "(");

	  output_addr_const (file, addr);
	  if (need_parenthesis)
	    fprintf (file, ")");
	}
      break;
    }
}


/* Splitting of some instructions.  */

static rtx
m68hc11_expand_compare (enum rtx_code code, rtx op0, rtx op1)
{
  rtx ret = 0;

  if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
    abort ();
  else
    {
      emit_insn (gen_rtx_SET (VOIDmode, cc0_rtx,
			      gen_rtx_COMPARE (VOIDmode, op0, op1)));
      ret = gen_rtx_fmt_ee (code, VOIDmode, cc0_rtx, const0_rtx);
    }

  return ret;
}

rtx
m68hc11_expand_compare_and_branch (enum rtx_code code, rtx op0, rtx op1,
                                   rtx label)
{
  rtx tmp;

  switch (GET_MODE (op0))
    {
    case QImode:
    case HImode:
      tmp = m68hc11_expand_compare (code, op0, op1);
      tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
				  gen_rtx_LABEL_REF (VOIDmode, label),
				  pc_rtx);
      emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
      return 0;
#if 0

      /* SCz: from i386.c  */
    case SFmode:
    case DFmode:
      /* Don't expand the comparison early, so that we get better code
         when jump or whoever decides to reverse the comparison.  */
      {
	rtvec vec;
	int use_fcomi;

	code = m68hc11_prepare_fp_compare_args (code, &m68hc11_compare_op0,
						&m68hc11_compare_op1);

	tmp = gen_rtx_fmt_ee (code, m68hc11_fp_compare_mode (code),
			      m68hc11_compare_op0, m68hc11_compare_op1);
	tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
				    gen_rtx_LABEL_REF (VOIDmode, label),
				    pc_rtx);
	tmp = gen_rtx_SET (VOIDmode, pc_rtx, tmp);

	use_fcomi = ix86_use_fcomi_compare (code);
	vec = rtvec_alloc (3 + !use_fcomi);
	RTVEC_ELT (vec, 0) = tmp;
	RTVEC_ELT (vec, 1)
	  = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 18));
	RTVEC_ELT (vec, 2)
	  = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 17));
	if (!use_fcomi)
	  RTVEC_ELT (vec, 3)
	    = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (HImode));

	emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, vec));
	return;
      }
#endif

    case SImode:
      /* Expand SImode branch into multiple compare+branch.  */
      {
	rtx lo[2], hi[2], label2;
	enum rtx_code code1, code2, code3;

	if (CONSTANT_P (op0) && !CONSTANT_P (op1))
	  {
	    tmp = op0;
	    op0 = op1;
	    op1 = tmp;
	    code = swap_condition (code);
	  }
	lo[0] = m68hc11_gen_lowpart (HImode, op0);
	lo[1] = m68hc11_gen_lowpart (HImode, op1);
	hi[0] = m68hc11_gen_highpart (HImode, op0);
	hi[1] = m68hc11_gen_highpart (HImode, op1);

	/* Otherwise, if we are doing less-than, op1 is a constant and the
	   low word is zero, then we can just examine the high word.  */

	if (GET_CODE (hi[1]) == CONST_INT && lo[1] == const0_rtx
	    && (code == LT || code == LTU))
	  {
	    return m68hc11_expand_compare_and_branch (code, hi[0], hi[1],
						      label);
	  }

	/* Otherwise, we need two or three jumps.  */

	label2 = gen_label_rtx ();

	code1 = code;
	code2 = swap_condition (code);
	code3 = unsigned_condition (code);

	switch (code)
	  {
	  case LT:
	  case GT:
	  case LTU:
	  case GTU:
	    break;

	  case LE:
	    code1 = LT;
	    code2 = GT;
	    break;
	  case GE:
	    code1 = GT;
	    code2 = LT;
	    break;
	  case LEU:
	    code1 = LTU;
	    code2 = GTU;
	    break;
	  case GEU:
	    code1 = GTU;
	    code2 = LTU;
	    break;

	  case EQ:
	    code1 = UNKNOWN;
	    code2 = NE;
	    break;
	  case NE:
	    code2 = UNKNOWN;
	    break;

	  default:
	    abort ();
	  }

	/*
	 * a < b =>
	 *    if (hi(a) < hi(b)) goto true;
	 *    if (hi(a) > hi(b)) goto false;
	 *    if (lo(a) < lo(b)) goto true;
	 *  false:
	 */
	if (code1 != UNKNOWN)
	  m68hc11_expand_compare_and_branch (code1, hi[0], hi[1], label);
	if (code2 != UNKNOWN)
	  m68hc11_expand_compare_and_branch (code2, hi[0], hi[1], label2);

	m68hc11_expand_compare_and_branch (code3, lo[0], lo[1], label);

	if (code2 != UNKNOWN)
	  emit_label (label2);
	return 0;
      }

    default:
      abort ();
    }
  return 0;
}

/* Return the increment/decrement mode of a MEM if it is such.
   Return CONST if it is anything else.  */
static int
autoinc_mode (rtx x)
{
  if (GET_CODE (x) != MEM)
    return CONST;

  x = XEXP (x, 0);
  if (GET_CODE (x) == PRE_INC
      || GET_CODE (x) == PRE_DEC
      || GET_CODE (x) == POST_INC
      || GET_CODE (x) == POST_DEC)
    return GET_CODE (x);

  return CONST;
}

static int
m68hc11_make_autoinc_notes (rtx *x, void *data)
{
  rtx insn;
  
  switch (GET_CODE (*x))
    {
    case PRE_DEC:
    case PRE_INC:
    case POST_DEC:
    case POST_INC:
      insn = (rtx) data;
      REG_NOTES (insn) = alloc_EXPR_LIST (REG_INC, XEXP (*x, 0),
                                          REG_NOTES (insn));
      return -1;

    default:
      return 0;
    }
}

/* Split a DI, SI or HI move into several smaller move operations.
   The scratch register 'scratch' is used as a temporary to load
   store intermediate values.  It must be a hard register.  */
void
m68hc11_split_move (rtx to, rtx from, rtx scratch)
{
  rtx low_to, low_from;
  rtx high_to, high_from;
  rtx insn;
  enum machine_mode mode;
  int offset = 0;
  int autoinc_from = autoinc_mode (from);
  int autoinc_to = autoinc_mode (to);

  mode = GET_MODE (to);

  /* If the TO and FROM contain autoinc modes that are not compatible
     together (one pop and the other a push), we must change one to
     an offsetable operand and generate an appropriate add at the end.  */
  if (TARGET_M6812 && GET_MODE_SIZE (mode) > 2)
    {
      rtx reg;
      int code;

      /* The source uses an autoinc mode which is not compatible with
         a split (this would result in a word swap).  */
      if (autoinc_from == PRE_INC || autoinc_from == POST_DEC)
        {
          code = GET_CODE (XEXP (from, 0));
          reg = XEXP (XEXP (from, 0), 0);
          offset = GET_MODE_SIZE (GET_MODE (from));
          if (code == POST_DEC)
            offset = -offset;

          if (code == PRE_INC)
            emit_insn (gen_addhi3 (reg, reg, GEN_INT (offset)));

          m68hc11_split_move (to, gen_rtx_MEM (GET_MODE (from), reg), scratch);
          if (code == POST_DEC)
            emit_insn (gen_addhi3 (reg, reg, GEN_INT (offset)));
          return;
        }

      /* Likewise for destination.  */
      if (autoinc_to == PRE_INC || autoinc_to == POST_DEC)
        {
          code = GET_CODE (XEXP (to, 0));
          reg = XEXP (XEXP (to, 0), 0);
          offset = GET_MODE_SIZE (GET_MODE (to));
          if (code == POST_DEC)
            offset = -offset;

          if (code == PRE_INC)
            emit_insn (gen_addhi3 (reg, reg, GEN_INT (offset)));

          m68hc11_split_move (gen_rtx_MEM (GET_MODE (to), reg), from, scratch);
          if (code == POST_DEC)
            emit_insn (gen_addhi3 (reg, reg, GEN_INT (offset)));
          return;
        }

      /* The source and destination auto increment modes must be compatible
         with each other: same direction.  */
      if ((autoinc_to != autoinc_from
           && autoinc_to != CONST && autoinc_from != CONST)
          /* The destination address register must not be used within
             the source operand because the source address would change
             while doing the copy.  */
          || (autoinc_to != CONST
              && reg_mentioned_p (XEXP (XEXP (to, 0), 0), from)
              && !IS_STACK_PUSH (to)))
        {
          /* Must change the destination.  */
          code = GET_CODE (XEXP (to, 0));
          reg = XEXP (XEXP (to, 0), 0);
          offset = GET_MODE_SIZE (GET_MODE (to));
          if (code == PRE_DEC || code == POST_DEC)
            offset = -offset;

          if (code == PRE_DEC || code == PRE_INC)
            emit_insn (gen_addhi3 (reg, reg, GEN_INT (offset)));
          m68hc11_split_move (gen_rtx_MEM (GET_MODE (to), reg), from, scratch);
          if (code == POST_DEC || code == POST_INC)
            emit_insn (gen_addhi3 (reg, reg, GEN_INT (offset)));

          return;
        }

      /* Likewise, the source address register must not be used within
         the destination operand.  */
      if (autoinc_from != CONST
          && reg_mentioned_p (XEXP (XEXP (from, 0), 0), to)
          && !IS_STACK_PUSH (to))
        {
          /* Must change the source.  */
          code = GET_CODE (XEXP (from, 0));
          reg = XEXP (XEXP (from, 0), 0);
          offset = GET_MODE_SIZE (GET_MODE (from));
          if (code == PRE_DEC || code == POST_DEC)
            offset = -offset;

          if (code == PRE_DEC || code == PRE_INC)
            emit_insn (gen_addhi3 (reg, reg, GEN_INT (offset)));
          m68hc11_split_move (to, gen_rtx_MEM (GET_MODE (from), reg), scratch);
          if (code == POST_DEC || code == POST_INC)
            emit_insn (gen_addhi3 (reg, reg, GEN_INT (offset)));

          return;
        }
    }

  if (GET_MODE_SIZE (mode) == 8)
    mode = SImode;
  else if (GET_MODE_SIZE (mode) == 4)
    mode = HImode;
  else
    mode = QImode;

  if (TARGET_M6812
      && IS_STACK_PUSH (to)
      && reg_mentioned_p (gen_rtx_REG (HImode, HARD_SP_REGNUM), from))
    {
      if (mode == SImode)
        {
          offset = 4;
        }
      else if (mode == HImode)
        {
          offset = 2;
        }
      else
        offset = 0;
    }

  low_to = m68hc11_gen_lowpart (mode, to);
  high_to = m68hc11_gen_highpart (mode, to);

  low_from = m68hc11_gen_lowpart (mode, from);
  if (mode == SImode && GET_CODE (from) == CONST_INT)
    {
      if (INTVAL (from) >= 0)
	high_from = const0_rtx;
      else
	high_from = constm1_rtx;
    }
  else
    high_from = m68hc11_gen_highpart (mode, from);

  if (offset)
    {
      high_from = adjust_address (high_from, mode, offset);
      low_from = high_from;
    }

  /* When copying with a POST_INC mode, we must copy the
     high part and then the low part to guarantee a correct
     32/64-bit copy.  */
  if (TARGET_M6812
      && GET_MODE_SIZE (mode) >= 2
      && autoinc_from != autoinc_to
      && (autoinc_from == POST_INC || autoinc_to == POST_INC))
    {
      rtx swap;

      swap = low_to;
      low_to = high_to;
      high_to = swap;

      swap = low_from;
      low_from = high_from;
      high_from = swap;
    }
  if (mode == SImode)
    {
      m68hc11_split_move (low_to, low_from, scratch);
      m68hc11_split_move (high_to, high_from, scratch);
    }
  else if (H_REG_P (to) || H_REG_P (from)
	   || (low_from == const0_rtx
	       && high_from == const0_rtx
	       && ! push_operand (to, GET_MODE (to))
	       && ! H_REG_P (scratch))
	   || (TARGET_M6812
	       && (!m68hc11_register_indirect_p (from, GET_MODE (from))
		   || m68hc11_small_indexed_indirect_p (from,
							GET_MODE (from)))
	       && (!m68hc11_register_indirect_p (to, GET_MODE (to))
		   || m68hc11_small_indexed_indirect_p (to, GET_MODE (to)))))
    {
      insn = emit_move_insn (low_to, low_from);
      for_each_rtx (&PATTERN (insn), m68hc11_make_autoinc_notes, insn);

      insn = emit_move_insn (high_to, high_from);
      for_each_rtx (&PATTERN (insn), m68hc11_make_autoinc_notes, insn);
    }
  else
    {
      insn = emit_move_insn (scratch, low_from);
      for_each_rtx (&PATTERN (insn), m68hc11_make_autoinc_notes, insn);
      insn = emit_move_insn (low_to, scratch);
      for_each_rtx (&PATTERN (insn), m68hc11_make_autoinc_notes, insn);

      insn = emit_move_insn (scratch, high_from);
      for_each_rtx (&PATTERN (insn), m68hc11_make_autoinc_notes, insn);
      insn = emit_move_insn (high_to, scratch);
      for_each_rtx (&PATTERN (insn), m68hc11_make_autoinc_notes, insn);
    }
}

static rtx
simplify_logical (enum machine_mode mode, int code, rtx operand, rtx *result)
{
  int val;
  int mask;

  *result = 0;
  if (GET_CODE (operand) != CONST_INT)
    return operand;

  if (mode == HImode)
    mask = 0x0ffff;
  else
    mask = 0x0ff;

  val = INTVAL (operand);
  switch (code)
    {
    case IOR:
      if ((val & mask) == 0)
	return 0;
      if ((val & mask) == mask)
	*result = constm1_rtx;
      break;

    case AND:
      if ((val & mask) == 0)
	*result = const0_rtx;
      if ((val & mask) == mask)
	return 0;
      break;

    case XOR:
      if ((val & mask) == 0)
	return 0;
      break;
    }
  return operand;
}

static void
m68hc11_emit_logical (enum machine_mode mode, int code, rtx *operands)
{
  rtx result;
  int need_copy;

  need_copy = (rtx_equal_p (operands[0], operands[1])
	       || rtx_equal_p (operands[0], operands[2])) ? 0 : 1;

  operands[1] = simplify_logical (mode, code, operands[1], &result);
  operands[2] = simplify_logical (mode, code, operands[2], &result);

  if (result && GET_CODE (result) == CONST_INT)
    {
      if (!H_REG_P (operands[0]) && operands[3]
	  && (INTVAL (result) != 0 || IS_STACK_PUSH (operands[0])))
	{
	  emit_move_insn (operands[3], result);
	  emit_move_insn (operands[0], operands[3]);
	}
      else
	{
	  emit_move_insn (operands[0], result);
	}
    }
  else if (operands[1] != 0 && operands[2] != 0)
    {
      rtx insn;

      if (!H_REG_P (operands[0]) && operands[3])
	{
	  emit_move_insn (operands[3], operands[1]);
	  emit_insn (gen_rtx_SET (mode,
				  operands[3],
				  gen_rtx_fmt_ee (code, mode,
						  operands[3], operands[2])));
	  insn = emit_move_insn (operands[0], operands[3]);
	}
      else
	{
	  insn = emit_insn (gen_rtx_SET (mode,
					 operands[0],
					 gen_rtx_fmt_ee (code, mode,
							 operands[0],
							 operands[2])));
	}
    }

  /* The logical operation is similar to a copy.  */
  else if (need_copy)
    {
      rtx src;

      if (GET_CODE (operands[1]) == CONST_INT)
	src = operands[2];
      else
	src = operands[1];

      if (!H_REG_P (operands[0]) && !H_REG_P (src))
	{
	  emit_move_insn (operands[3], src);
	  emit_move_insn (operands[0], operands[3]);
	}
      else
	{
	  emit_move_insn (operands[0], src);
	}
    }
}

void
m68hc11_split_logical (enum machine_mode mode, int code, rtx *operands)
{
  rtx low[4];
  rtx high[4];

  low[0] = m68hc11_gen_lowpart (mode, operands[0]);
  low[1] = m68hc11_gen_lowpart (mode, operands[1]);
  low[2] = m68hc11_gen_lowpart (mode, operands[2]);

  high[0] = m68hc11_gen_highpart (mode, operands[0]);

  if (mode == SImode && GET_CODE (operands[1]) == CONST_INT)
    {
      if (INTVAL (operands[1]) >= 0)
	high[1] = const0_rtx;
      else
	high[1] = constm1_rtx;
    }
  else
    high[1] = m68hc11_gen_highpart (mode, operands[1]);

  if (mode == SImode && GET_CODE (operands[2]) == CONST_INT)
    {
      if (INTVAL (operands[2]) >= 0)
	high[2] = const0_rtx;
      else
	high[2] = constm1_rtx;
    }
  else
    high[2] = m68hc11_gen_highpart (mode, operands[2]);

  low[3] = operands[3];
  high[3] = operands[3];
  if (mode == SImode)
    {
      m68hc11_split_logical (HImode, code, low);
      m68hc11_split_logical (HImode, code, high);
      return;
    }

  m68hc11_emit_logical (mode, code, low);
  m68hc11_emit_logical (mode, code, high);
}


/* Code generation.  */

void
m68hc11_output_swap (rtx insn ATTRIBUTE_UNUSED, rtx operands[])
{
  /* We have to be careful with the cc_status.  An address register swap
     is generated for some comparison.  The comparison is made with D
     but the branch really uses the address register.  See the split
     pattern for compare.  The xgdx/xgdy preserve the flags but after
     the exchange, the flags will reflect to the value of X and not D.
     Tell this by setting the cc_status according to the cc_prev_status.  */
  if (X_REG_P (operands[1]) || X_REG_P (operands[0]))
    {
      if (cc_prev_status.value1 != 0
	  && (D_REG_P (cc_prev_status.value1)
	      || X_REG_P (cc_prev_status.value1)))
	{
	  cc_status = cc_prev_status;
	  if (D_REG_P (cc_status.value1))
	    cc_status.value1 = gen_rtx_REG (GET_MODE (cc_status.value1),
					HARD_X_REGNUM);
	  else
	    cc_status.value1 = gen_rtx_REG (GET_MODE (cc_status.value1),
					HARD_D_REGNUM);
	}
      else
	CC_STATUS_INIT;

      output_asm_insn ("xgdx", operands);
    }
  else
    {
      if (cc_prev_status.value1 != 0
	  && (D_REG_P (cc_prev_status.value1)
	      || Y_REG_P (cc_prev_status.value1)))
	{
	  cc_status = cc_prev_status;
	  if (D_REG_P (cc_status.value1))
	    cc_status.value1 = gen_rtx_REG (GET_MODE (cc_status.value1),
					HARD_Y_REGNUM);
	  else
	    cc_status.value1 = gen_rtx_REG (GET_MODE (cc_status.value1),
					HARD_D_REGNUM);
	}
      else
	CC_STATUS_INIT;

      output_asm_insn ("xgdy", operands);
    }
}

/* Returns 1 if the next insn after 'insn' is a test of the register 'reg'.
   This is used to decide whether a move that set flags should be used
   instead.  */
int
next_insn_test_reg (rtx insn, rtx reg)
{
  rtx body;

  insn = next_nonnote_insn (insn);
  if (GET_CODE (insn) != INSN)
    return 0;

  body = PATTERN (insn);
  if (sets_cc0_p (body) != 1)
    return 0;

  if (rtx_equal_p (XEXP (body, 1), reg) == 0)
    return 0;

  return 1;
}

/* Generate the code to move a 16-bit operand into another one.  */

void
m68hc11_gen_movhi (rtx insn, rtx *operands)
{
  int reg;

  /* Move a register or memory to the same location.
     This is possible because such insn can appear
     in a non-optimizing mode.  */
  if (operands[0] == operands[1] || rtx_equal_p (operands[0], operands[1]))
    {
      cc_status = cc_prev_status;
      return;
    }

  if (TARGET_M6812)
    {
      if (IS_STACK_PUSH (operands[0]) && H_REG_P (operands[1]))
	{
          cc_status = cc_prev_status;
	  switch (REGNO (operands[1]))
	    {
	    case HARD_X_REGNUM:
	    case HARD_Y_REGNUM:
	    case HARD_D_REGNUM:
	      output_asm_insn ("psh%1", operands);
	      break;
            case HARD_SP_REGNUM:
              output_asm_insn ("sts\t2,-sp", operands);
              break;
	    default:
	      abort ();
	    }
	  return;
	}
      if (IS_STACK_POP (operands[1]) && H_REG_P (operands[0]))
	{
          cc_status = cc_prev_status;
	  switch (REGNO (operands[0]))
	    {
	    case HARD_X_REGNUM:
	    case HARD_Y_REGNUM:
	    case HARD_D_REGNUM:
	      output_asm_insn ("pul%0", operands);
	      break;
	    default:
	      abort ();
	    }
	  return;
	}
      if (H_REG_P (operands[0]) && H_REG_P (operands[1]))
	{
          m68hc11_notice_keep_cc (operands[0]);
	  output_asm_insn ("tfr\t%1,%0", operands);
	}
      else if (H_REG_P (operands[0]))
	{
	  if (SP_REG_P (operands[0]))
	    output_asm_insn ("lds\t%1", operands);
	  else
	    output_asm_insn ("ld%0\t%1", operands);
	}
      else if (H_REG_P (operands[1]))
	{
	  if (SP_REG_P (operands[1]))
	    output_asm_insn ("sts\t%0", operands);
	  else
	    output_asm_insn ("st%1\t%0", operands);
	}
      else
	{
	  rtx from = operands[1];
	  rtx to = operands[0];

	  if ((m68hc11_register_indirect_p (from, GET_MODE (from))
	       && !m68hc11_small_indexed_indirect_p (from, GET_MODE (from)))
	      || (m68hc11_register_indirect_p (to, GET_MODE (to))
		  && !m68hc11_small_indexed_indirect_p (to, GET_MODE (to))))
	    {
	      rtx ops[3];

	      if (operands[2])
		{
		  ops[0] = operands[2];
		  ops[1] = from;
		  ops[2] = 0;
		  m68hc11_gen_movhi (insn, ops);
		  ops[0] = to;
		  ops[1] = operands[2];
		  m68hc11_gen_movhi (insn, ops);
		}
	      else
		{
		  /* !!!! SCz wrong here.  */
                  fatal_insn ("move insn not handled", insn);
		}
	    }
	  else
	    {
	      if (GET_CODE (from) == CONST_INT && INTVAL (from) == 0)
		{
		  output_asm_insn ("clr\t%h0", operands);
		  output_asm_insn ("clr\t%b0", operands);
		}
	      else
		{
                  m68hc11_notice_keep_cc (operands[0]);
		  output_asm_insn ("movw\t%1,%0", operands);
		}
	    }
	}
      return;
    }

  if (IS_STACK_POP (operands[1]) && H_REG_P (operands[0]))
    {
      cc_status = cc_prev_status;
      switch (REGNO (operands[0]))
	{
	case HARD_X_REGNUM:
	case HARD_Y_REGNUM:
	  output_asm_insn ("pul%0", operands);
	  break;
	case HARD_D_REGNUM:
	  output_asm_insn ("pula", operands);
	  output_asm_insn ("pulb", operands);
	  break;
	default:
	  abort ();
	}
      return;
    }
  /* Some moves to a hard register are special. Not all of them
     are really supported and we have to use a temporary
     location to provide them (either the stack of a temp var).  */
  if (H_REG_P (operands[0]))
    {
      switch (REGNO (operands[0]))
	{
	case HARD_D_REGNUM:
	  if (X_REG_P (operands[1]))
	    {
	      if (optimize && find_regno_note (insn, REG_DEAD, HARD_X_REGNUM))
		{
		  m68hc11_output_swap (insn, operands);
		}
	      else if (next_insn_test_reg (insn, operands[0]))
		{
		  output_asm_insn ("stx\t%t0\n\tldd\t%t0", operands);
		}
	      else
		{
                  m68hc11_notice_keep_cc (operands[0]);
		  output_asm_insn ("pshx\n\tpula\n\tpulb", operands);
		}
	    }
	  else if (Y_REG_P (operands[1]))
	    {
	      if (optimize && find_regno_note (insn, REG_DEAD, HARD_Y_REGNUM))
		{
		  m68hc11_output_swap (insn, operands);
		}
	      else
		{
		  /* %t means *ZTMP scratch register.  */
		  output_asm_insn ("sty\t%t1", operands);
		  output_asm_insn ("ldd\t%t1", operands);
		}
	    }
	  else if (SP_REG_P (operands[1]))
	    {
	      CC_STATUS_INIT;
	      if (ix_reg == 0)
		create_regs_rtx ();
	      if (optimize == 0 || dead_register_here (insn, ix_reg) == 0)
		output_asm_insn ("xgdx", operands);
	      output_asm_insn ("tsx", operands);
	      output_asm_insn ("xgdx", operands);
	    }
	  else if (IS_STACK_POP (operands[1]))
	    {
	      output_asm_insn ("pula\n\tpulb", operands);
	    }
	  else if (GET_CODE (operands[1]) == CONST_INT
		   && INTVAL (operands[1]) == 0)
	    {
	      output_asm_insn ("clra\n\tclrb", operands);
	    }
	  else
	    {
	      output_asm_insn ("ldd\t%1", operands);
	    }
	  break;

	case HARD_X_REGNUM:
	  if (D_REG_P (operands[1]))
	    {
	      if (optimize && find_regno_note (insn, REG_DEAD, HARD_D_REGNUM))
		{
		  m68hc11_output_swap (insn, operands);
		}
	      else if (next_insn_test_reg (insn, operands[0]))
		{
		  output_asm_insn ("std\t%t0\n\tldx\t%t0", operands);
		}
	      else
		{
		  m68hc11_notice_keep_cc (operands[0]);
		  output_asm_insn ("pshb", operands);
		  output_asm_insn ("psha", operands);
		  output_asm_insn ("pulx", operands);
		}
	    }
	  else if (Y_REG_P (operands[1]))
	    {
              /* When both D and Y are dead, use the sequence xgdy, xgdx
                 to move Y into X.  The D and Y registers are modified.  */
              if (optimize && find_regno_note (insn, REG_DEAD, HARD_Y_REGNUM)
                  && dead_register_here (insn, d_reg))
                {
                  output_asm_insn ("xgdy", operands);
                  output_asm_insn ("xgdx", operands);
                  CC_STATUS_INIT;
                }
              else if (!optimize_size)
                {
                  output_asm_insn ("sty\t%t1", operands);
                  output_asm_insn ("ldx\t%t1", operands);
                }
              else
                {
                  CC_STATUS_INIT;
                  output_asm_insn ("pshy", operands);
                  output_asm_insn ("pulx", operands);
                }
	    }
	  else if (SP_REG_P (operands[1]))
	    {
	      /* tsx, tsy preserve the flags */
	      cc_status = cc_prev_status;
	      output_asm_insn ("tsx", operands);
	    }
	  else
	    {
	      output_asm_insn ("ldx\t%1", operands);
	    }
	  break;

	case HARD_Y_REGNUM:
	  if (D_REG_P (operands[1]))
	    {
	      if (optimize && find_regno_note (insn, REG_DEAD, HARD_D_REGNUM))
		{
		  m68hc11_output_swap (insn, operands);
		}
	      else
		{
		  output_asm_insn ("std\t%t1", operands);
		  output_asm_insn ("ldy\t%t1", operands);
		}
	    }
	  else if (X_REG_P (operands[1]))
	    {
              /* When both D and X are dead, use the sequence xgdx, xgdy
                 to move X into Y.  The D and X registers are modified.  */
              if (optimize && find_regno_note (insn, REG_DEAD, HARD_X_REGNUM)
                  && dead_register_here (insn, d_reg))
                {
                  output_asm_insn ("xgdx", operands);
                  output_asm_insn ("xgdy", operands);
                  CC_STATUS_INIT;
                }
              else if (!optimize_size)
                {
                  output_asm_insn ("stx\t%t1", operands);
                  output_asm_insn ("ldy\t%t1", operands);
                }
              else
                {
                  CC_STATUS_INIT;
                  output_asm_insn ("pshx", operands);
                  output_asm_insn ("puly", operands);
                }
	    }
	  else if (SP_REG_P (operands[1]))
	    {
	      /* tsx, tsy preserve the flags */
	      cc_status = cc_prev_status;
	      output_asm_insn ("tsy", operands);
	    }
          else
	    {
	      output_asm_insn ("ldy\t%1", operands);
	    }
	  break;

	case HARD_SP_REGNUM:
	  if (D_REG_P (operands[1]))
	    {
	      m68hc11_notice_keep_cc (operands[0]);
	      output_asm_insn ("xgdx", operands);
	      output_asm_insn ("txs", operands);
	      output_asm_insn ("xgdx", operands);
	    }
	  else if (X_REG_P (operands[1]))
	    {
	      /* tys, txs preserve the flags */
	      cc_status = cc_prev_status;
	      output_asm_insn ("txs", operands);
	    }
	  else if (Y_REG_P (operands[1]))
	    {
	      /* tys, txs preserve the flags */
	      cc_status = cc_prev_status;
	      output_asm_insn ("tys", operands);
	    }
	  else
	    {
	      /* lds sets the flags but the des does not.  */
	      CC_STATUS_INIT;
	      output_asm_insn ("lds\t%1", operands);
	      output_asm_insn ("des", operands);
	    }
	  break;

	default:
	  fatal_insn ("invalid register in the move instruction", insn);
	  break;
	}
      return;
    }
  if (SP_REG_P (operands[1]) && REG_P (operands[0])
      && REGNO (operands[0]) == HARD_FRAME_POINTER_REGNUM)
    {
      output_asm_insn ("sts\t%0", operands);
      return;
    }

  if (IS_STACK_PUSH (operands[0]) && H_REG_P (operands[1]))
    {
      cc_status = cc_prev_status;
      switch (REGNO (operands[1]))
	{
	case HARD_X_REGNUM:
	case HARD_Y_REGNUM:
	  output_asm_insn ("psh%1", operands);
	  break;
	case HARD_D_REGNUM:
	  output_asm_insn ("pshb", operands);
	  output_asm_insn ("psha", operands);
	  break;
	default:
	  abort ();
	}
      return;
    }

  /* Operand 1 must be a hard register.  */
  if (!H_REG_P (operands[1]))
    {
      fatal_insn ("invalid operand in the instruction", insn);
    }

  reg = REGNO (operands[1]);
  switch (reg)
    {
    case HARD_D_REGNUM:
      output_asm_insn ("std\t%0", operands);
      break;

    case HARD_X_REGNUM:
      output_asm_insn ("stx\t%0", operands);
      break;

    case HARD_Y_REGNUM:
      output_asm_insn ("sty\t%0", operands);
      break;

    case HARD_SP_REGNUM:
      if (ix_reg == 0)
	create_regs_rtx ();

      if (REG_P (operands[0]) && REGNO (operands[0]) == SOFT_TMP_REGNUM)
        {
          output_asm_insn ("pshx", operands);
          output_asm_insn ("tsx", operands);
          output_asm_insn ("inx", operands);
          output_asm_insn ("inx", operands);
          output_asm_insn ("stx\t%0", operands);
          output_asm_insn ("pulx", operands);
        }
          
      else if (reg_mentioned_p (ix_reg, operands[0]))
	{
	  output_asm_insn ("sty\t%t0", operands);
	  output_asm_insn ("tsy", operands);
	  output_asm_insn ("sty\t%0", operands);
	  output_asm_insn ("ldy\t%t0", operands);
	}
      else
	{
	  output_asm_insn ("stx\t%t0", operands);
	  output_asm_insn ("tsx", operands);
	  output_asm_insn ("stx\t%0", operands);
	  output_asm_insn ("ldx\t%t0", operands);
	}
      CC_STATUS_INIT;
      break;

    default:
      fatal_insn ("invalid register in the move instruction", insn);
      break;
    }
}

void
m68hc11_gen_movqi (rtx insn, rtx *operands)
{
  /* Move a register or memory to the same location.
     This is possible because such insn can appear
     in a non-optimizing mode.  */
  if (operands[0] == operands[1] || rtx_equal_p (operands[0], operands[1]))
    {
      cc_status = cc_prev_status;
      return;
    }

  if (TARGET_M6812)
    {

      if (H_REG_P (operands[0]) && H_REG_P (operands[1]))
	{
          m68hc11_notice_keep_cc (operands[0]);
	  output_asm_insn ("tfr\t%1,%0", operands);
	}
      else if (H_REG_P (operands[0]))
	{
	  if (Q_REG_P (operands[0]))
	    output_asm_insn ("lda%0\t%b1", operands);
	  else if (D_REG_P (operands[0]))
	    output_asm_insn ("ldab\t%b1", operands);
	  else
	    goto m6811_move;
	}
      else if (H_REG_P (operands[1]))
	{
	  if (Q_REG_P (operands[1]))
	    output_asm_insn ("sta%1\t%b0", operands);
	  else if (D_REG_P (operands[1]))
	    output_asm_insn ("stab\t%b0", operands);
	  else
	    goto m6811_move;
	}
      else
	{
	  rtx from = operands[1];
	  rtx to = operands[0];

	  if ((m68hc11_register_indirect_p (from, GET_MODE (from))
	       && !m68hc11_small_indexed_indirect_p (from, GET_MODE (from)))
	      || (m68hc11_register_indirect_p (to, GET_MODE (to))
		  && !m68hc11_small_indexed_indirect_p (to, GET_MODE (to))))
	    {
	      rtx ops[3];

	      if (operands[2])
		{
		  ops[0] = operands[2];
		  ops[1] = from;
		  ops[2] = 0;
		  m68hc11_gen_movqi (insn, ops);
		  ops[0] = to;
		  ops[1] = operands[2];
		  m68hc11_gen_movqi (insn, ops);
		}
	      else
		{
		  /* !!!! SCz wrong here.  */
                  fatal_insn ("move insn not handled", insn);
		}
	    }
	  else
	    {
	      if (GET_CODE (from) == CONST_INT && INTVAL (from) == 0)
		{
		  output_asm_insn ("clr\t%b0", operands);
		}
	      else
		{
                  m68hc11_notice_keep_cc (operands[0]);
		  output_asm_insn ("movb\t%b1,%b0", operands);
		}
	    }
	}
      return;
    }

 m6811_move:
  if (H_REG_P (operands[0]))
    {
      switch (REGNO (operands[0]))
	{
	case HARD_B_REGNUM:
	case HARD_D_REGNUM:
	  if (X_REG_P (operands[1]))
	    {
	      if (optimize && find_regno_note (insn, REG_DEAD, HARD_X_REGNUM))
		{
		  m68hc11_output_swap (insn, operands);
		}
	      else
		{
		  output_asm_insn ("stx\t%t1", operands);
		  output_asm_insn ("ldab\t%T0", operands);
		}
	    }
	  else if (Y_REG_P (operands[1]))
	    {
	      if (optimize && find_regno_note (insn, REG_DEAD, HARD_Y_REGNUM))
		{
		  m68hc11_output_swap (insn, operands);
		}
	      else
		{
		  output_asm_insn ("sty\t%t1", operands);
		  output_asm_insn ("ldab\t%T0", operands);
		}
	    }
	  else if (!DB_REG_P (operands[1]) && !D_REG_P (operands[1])
		   && !DA_REG_P (operands[1]))
	    {
	      output_asm_insn ("ldab\t%b1", operands);
	    }
	  else if (DA_REG_P (operands[1]))
	    {
	      output_asm_insn ("tab", operands);
	    }
	  else
	    {
	      cc_status = cc_prev_status;
	      return;
	    }
	  break;

	case HARD_A_REGNUM:
	  if (X_REG_P (operands[1]))
	    {
	      output_asm_insn ("stx\t%t1", operands);
	      output_asm_insn ("ldaa\t%T0", operands);
	    }
	  else if (Y_REG_P (operands[1]))
	    {
	      output_asm_insn ("sty\t%t1", operands);
	      output_asm_insn ("ldaa\t%T0", operands);
	    }
	  else if (!DB_REG_P (operands[1]) && !D_REG_P (operands[1])
		   && !DA_REG_P (operands[1]))
	    {
	      output_asm_insn ("ldaa\t%b1", operands);
	    }
	  else if (!DA_REG_P (operands[1]))
	    {
	      output_asm_insn ("tba", operands);
	    }
	  else
	    {
	      cc_status = cc_prev_status;
	    }
	  break;

	case HARD_X_REGNUM:
	  if (D_REG_P (operands[1]))
	    {
	      if (optimize && find_regno_note (insn, REG_DEAD, HARD_D_REGNUM))
		{
		  m68hc11_output_swap (insn, operands);
		}
	      else
		{
		  output_asm_insn ("stab\t%T1", operands);
		  output_asm_insn ("ldx\t%t1", operands);
		}
	      CC_STATUS_INIT;
	    }
	  else if (Y_REG_P (operands[1]))
	    {
	      output_asm_insn ("sty\t%t0", operands);
	      output_asm_insn ("ldx\t%t0", operands);
	    }
	  else if (GET_CODE (operands[1]) == CONST_INT)
	    {
	      output_asm_insn ("ldx\t%1", operands);
	    }
	  else if (dead_register_here (insn, d_reg))
	    {
	      output_asm_insn ("ldab\t%b1", operands);
	      output_asm_insn ("xgdx", operands);
	    }
	  else if (!reg_mentioned_p (operands[0], operands[1]))
	    {
	      output_asm_insn ("xgdx", operands);
	      output_asm_insn ("ldab\t%b1", operands);
	      output_asm_insn ("xgdx", operands);
	    }
	  else
	    {
	      output_asm_insn ("pshb", operands);
	      output_asm_insn ("ldab\t%b1", operands);
	      output_asm_insn ("stab\t%T1", operands);
	      output_asm_insn ("ldx\t%t1", operands);
	      output_asm_insn ("pulb", operands);
	      CC_STATUS_INIT;
	    }
	  break;

	case HARD_Y_REGNUM:
	  if (D_REG_P (operands[1]))
	    {
	      output_asm_insn ("stab\t%T1", operands);
	      output_asm_insn ("ldy\t%t1", operands);
	      CC_STATUS_INIT;
	    }
	  else if (X_REG_P (operands[1]))
	    {
	      output_asm_insn ("stx\t%t1", operands);
	      output_asm_insn ("ldy\t%t1", operands);
	      CC_STATUS_INIT;
	    }
	  else if (GET_CODE (operands[1]) == CONST_INT)
	    {
	      output_asm_insn ("ldy\t%1", operands);
	    }
	  else if (dead_register_here (insn, d_reg))
	    {
	      output_asm_insn ("ldab\t%b1", operands);
	      output_asm_insn ("xgdy", operands);
	    }
	  else if (!reg_mentioned_p (operands[0], operands[1]))
	    {
	      output_asm_insn ("xgdy", operands);
	      output_asm_insn ("ldab\t%b1", operands);
	      output_asm_insn ("xgdy", operands);
	    }
	  else
	    {
	      output_asm_insn ("pshb", operands);
	      output_asm_insn ("ldab\t%b1", operands);
	      output_asm_insn ("stab\t%T1", operands);
	      output_asm_insn ("ldy\t%t1", operands);
	      output_asm_insn ("pulb", operands);
	      CC_STATUS_INIT;
	    }
	  break;

	default:
	  fatal_insn ("invalid register in the instruction", insn);
	  break;
	}
    }
  else if (H_REG_P (operands[1]))
    {
      switch (REGNO (operands[1]))
	{
	case HARD_D_REGNUM:
	case HARD_B_REGNUM:
	  output_asm_insn ("stab\t%b0", operands);
	  break;

	case HARD_A_REGNUM:
	  output_asm_insn ("staa\t%b0", operands);
	  break;

	case HARD_X_REGNUM:
	  output_asm_insn ("xgdx\n\tstab\t%b0\n\txgdx", operands);
	  break;

	case HARD_Y_REGNUM:
	  output_asm_insn ("xgdy\n\tstab\t%b0\n\txgdy", operands);
	  break;

	default:
	  fatal_insn ("invalid register in the move instruction", insn);
	  break;
	}
      return;
    }
  else
    {
      fatal_insn ("operand 1 must be a hard register", insn);
    }
}

/* Generate the code for a ROTATE or ROTATERT on a QI or HI mode.
   The source and destination must be D or A and the shift must
   be a constant.  */
void
m68hc11_gen_rotate (enum rtx_code code, rtx insn, rtx operands[])
{
  int val;
  
  if (GET_CODE (operands[2]) != CONST_INT
      || (!D_REG_P (operands[0]) && !DA_REG_P (operands[0])))
    fatal_insn ("invalid rotate insn", insn);

  val = INTVAL (operands[2]);
  if (code == ROTATERT)
    val = GET_MODE_SIZE (GET_MODE (operands[0])) * BITS_PER_UNIT - val;

  if (GET_MODE (operands[0]) != QImode)
    CC_STATUS_INIT;

  /* Rotate by 8-bits if the shift is within [5..11].  */
  if (val >= 5 && val <= 11)
    {
      if (TARGET_M6812)
	output_asm_insn ("exg\ta,b", operands);
      else
	{
	  output_asm_insn ("psha", operands);
	  output_asm_insn ("tba", operands);
	  output_asm_insn ("pulb", operands);
	}
      val -= 8;
    }

  /* If the shift is big, invert the rotation.  */
  else if (val >= 12)
    {
      val = val - 16;
    }

  if (val > 0)
    {
      while (--val >= 0)
        {
          /* Set the carry to bit-15, but don't change D yet.  */
          if (GET_MODE (operands[0]) != QImode)
            {
              output_asm_insn ("asra", operands);
              output_asm_insn ("rola", operands);
            }

          /* Rotate B first to move the carry to bit-0.  */
          if (D_REG_P (operands[0]))
            output_asm_insn ("rolb", operands);

          if (GET_MODE (operands[0]) != QImode || DA_REG_P (operands[0]))
            output_asm_insn ("rola", operands);
        }
    }
  else
    {
      while (++val <= 0)
        {
          /* Set the carry to bit-8 of D.  */
          if (GET_MODE (operands[0]) != QImode)
            output_asm_insn ("tap", operands);

          /* Rotate B first to move the carry to bit-7.  */
          if (D_REG_P (operands[0]))
            output_asm_insn ("rorb", operands);

          if (GET_MODE (operands[0]) != QImode || DA_REG_P (operands[0]))
            output_asm_insn ("rora", operands);
        }
    }
}



/* Store in cc_status the expressions that the condition codes will
   describe after execution of an instruction whose pattern is EXP.
   Do not alter them if the instruction would not alter the cc's.  */

void
m68hc11_notice_update_cc (rtx exp, rtx insn ATTRIBUTE_UNUSED)
{
  /* recognize SET insn's.  */
  if (GET_CODE (exp) == SET)
    {
      /* Jumps do not alter the cc's.  */
      if (SET_DEST (exp) == pc_rtx)
	;

      /* NOTE: most instructions don't affect the carry bit, but the
         bhi/bls/bhs/blo instructions use it.  This isn't mentioned in
         the conditions.h header.  */

      /* Function calls clobber the cc's.  */
      else if (GET_CODE (SET_SRC (exp)) == CALL)
	{
	  CC_STATUS_INIT;
	}

      /* Tests and compares set the cc's in predictable ways.  */
      else if (SET_DEST (exp) == cc0_rtx)
	{
	  cc_status.flags = 0;
	  cc_status.value1 = XEXP (exp, 0);
	  cc_status.value2 = XEXP (exp, 1);
	}
      else
	{
	  /* All other instructions affect the condition codes.  */
	  cc_status.flags = 0;
	  cc_status.value1 = XEXP (exp, 0);
	  cc_status.value2 = XEXP (exp, 1);
	}
    }
  else
    {
      /* Default action if we haven't recognized something
         and returned earlier.  */
      CC_STATUS_INIT;
    }

  if (cc_status.value2 != 0)
    switch (GET_CODE (cc_status.value2))
      {
	/* These logical operations can generate several insns.
	   The flags are setup according to what is generated.  */
      case IOR:
      case XOR:
      case AND:
	break;

	/* The (not ...) generates several 'com' instructions for
	   non QImode.  We have to invalidate the flags.  */
      case NOT:
	if (GET_MODE (cc_status.value2) != QImode)
	  CC_STATUS_INIT;
	break;

      case PLUS:
      case MINUS:
      case MULT:
      case DIV:
      case UDIV:
      case MOD:
      case UMOD:
      case NEG:
	if (GET_MODE (cc_status.value2) != VOIDmode)
	  cc_status.flags |= CC_NO_OVERFLOW;
	break;

	/* The asl sets the overflow bit in such a way that this
	   makes the flags unusable for a next compare insn.  */
      case ASHIFT:
      case ROTATE:
      case ROTATERT:
	if (GET_MODE (cc_status.value2) != VOIDmode)
	  cc_status.flags |= CC_NO_OVERFLOW;
	break;

	/* A load/store instruction does not affect the carry.  */
      case MEM:
      case SYMBOL_REF:
      case REG:
      case CONST_INT:
	cc_status.flags |= CC_NO_OVERFLOW;
	break;

      default:
	break;
      }
  if (cc_status.value1 && GET_CODE (cc_status.value1) == REG
      && cc_status.value2
      && reg_overlap_mentioned_p (cc_status.value1, cc_status.value2))
    cc_status.value2 = 0;

  else if (cc_status.value1 && side_effects_p (cc_status.value1))
    cc_status.value1 = 0;

  else if (cc_status.value2 && side_effects_p (cc_status.value2))
    cc_status.value2 = 0;
}

/* The current instruction does not affect the flags but changes
   the register 'reg'.  See if the previous flags can be kept for the
   next instruction to avoid a comparison.  */
void
m68hc11_notice_keep_cc (rtx reg)
{
  if (reg == 0
      || cc_prev_status.value1 == 0
      || rtx_equal_p (reg, cc_prev_status.value1)
      || (cc_prev_status.value2
          && reg_mentioned_p (reg, cc_prev_status.value2)))
    CC_STATUS_INIT;
  else
    cc_status = cc_prev_status;
}



/* Machine Specific Reorg.  */

/* Z register replacement:

   GCC treats the Z register as an index base address register like
   X or Y.  In general, it uses it during reload to compute the address
   of some operand.  This helps the reload pass to avoid to fall into the
   register spill failure.

   The Z register is in the A_REGS class.  In the machine description,
   the 'A' constraint matches it.  The 'x' or 'y' constraints do not.

   It can appear everywhere an X or Y register can appear, except for
   some templates in the clobber section (when a clobber of X or Y is asked).
   For a given instruction, the template must ensure that no more than
   2 'A' registers are used.  Otherwise, the register replacement is not
   possible.

   To replace the Z register, the algorithm is not terrific:
   1. Insns that do not use the Z register are not changed
   2. When a Z register is used, we scan forward the insns to see
   a potential register to use: either X or Y and sometimes D.
   We stop when a call, a label or a branch is seen, or when we
   detect that both X and Y are used (probably at different times, but it does
   not matter).
   3. The register that will be used for the replacement of Z is saved
   in a .page0 register or on the stack.  If the first instruction that
   used Z, uses Z as an input, the value is loaded from another .page0
   register.  The replacement register is pushed on the stack in the
   rare cases where a compare insn uses Z and we couldn't find if X/Y
   are dead.
   4. The Z register is replaced in all instructions until we reach
   the end of the Z-block, as detected by step 2.
   5. If we detect that Z is still alive, its value is saved.
   If the replacement register is alive, its old value is loaded.

   The Z register can be disabled with -ffixed-z.
*/

struct replace_info
{
  rtx first;
  rtx replace_reg;
  int need_save_z;
  int must_load_z;
  int must_save_reg;
  int must_restore_reg;
  rtx last;
  int regno;
  int x_used;
  int y_used;
  int can_use_d;
  int found_call;
  int z_died;
  int z_set_count;
  rtx z_value;
  int must_push_reg;
  int save_before_last;
  int z_loaded_with_sp;
};

static int m68hc11_check_z_replacement (rtx, struct replace_info *);
static void m68hc11_find_z_replacement (rtx, struct replace_info *);
static void m68hc11_z_replacement (rtx);
static void m68hc11_reassign_regs (rtx);

int z_replacement_completed = 0;

/* Analyze the insn to find out which replacement register to use and
   the boundaries of the replacement.
   Returns 0 if we reached the last insn to be replaced, 1 if we can
   continue replacement in next insns.  */

static int
m68hc11_check_z_replacement (rtx insn, struct replace_info *info)
{
  int this_insn_uses_ix;
  int this_insn_uses_iy;
  int this_insn_uses_z;
  int this_insn_uses_z_in_dst;
  int this_insn_uses_d;
  rtx body;
  int z_dies_here;

  /* A call is said to clobber the Z register, we don't need
     to save the value of Z.  We also don't need to restore
     the replacement register (unless it is used by the call).  */
  if (GET_CODE (insn) == CALL_INSN)
    {
      body = PATTERN (insn);

      info->can_use_d = 0;

      /* If the call is an indirect call with Z, we have to use the
         Y register because X can be used as an input (D+X).
         We also must not save Z nor restore Y.  */
      if (reg_mentioned_p (z_reg, body))
	{
	  insn = NEXT_INSN (insn);
	  info->x_used = 1;
	  info->y_used = 0;
	  info->found_call = 1;
	  info->must_restore_reg = 0;
	  info->last = NEXT_INSN (insn);
	}
      info->need_save_z = 0;
      return 0;
    }
  if (GET_CODE (insn) == CODE_LABEL
      || GET_CODE (insn) == BARRIER || GET_CODE (insn) == ASM_INPUT)
    return 0;

  if (GET_CODE (insn) == JUMP_INSN)
    {
      if (reg_mentioned_p (z_reg, insn) == 0)
	return 0;

      info->can_use_d = 0;
      info->must_save_reg = 0;
      info->must_restore_reg = 0;
      info->need_save_z = 0;
      info->last = NEXT_INSN (insn);
      return 0;
    }
  if (GET_CODE (insn) != INSN && GET_CODE (insn) != JUMP_INSN)
    {
      return 1;
    }

  /* Z register dies here.  */
  z_dies_here = find_regno_note (insn, REG_DEAD, HARD_Z_REGNUM) != NULL;

  body = PATTERN (insn);
  if (GET_CODE (body) == SET)
    {
      rtx src = XEXP (body, 1);
      rtx dst = XEXP (body, 0);

      /* Condition code is set here. We have to restore the X/Y and
         save into Z before any test/compare insn because once we save/restore
         we can change the condition codes. When the compare insn uses Z and
         we can't use X/Y, the comparison is made with the *ZREG soft register
         (this is supported by cmphi, cmpqi, tsthi, tstqi patterns).  */
      if (dst == cc0_rtx)
	{
	  if ((GET_CODE (src) == REG && REGNO (src) == HARD_Z_REGNUM)
	      || (GET_CODE (src) == COMPARE &&
		  ((rtx_equal_p (XEXP (src, 0), z_reg)
                    && H_REG_P (XEXP (src, 1)))
		   || (rtx_equal_p (XEXP (src, 1), z_reg)
                       && H_REG_P (XEXP (src, 0))))))
	    {
	      if (insn == info->first)
		{
		  info->must_load_z = 0;
		  info->must_save_reg = 0;
		  info->must_restore_reg = 0;
		  info->need_save_z = 0;
		  info->found_call = 1;
		  info->regno = SOFT_Z_REGNUM;
		  info->last = NEXT_INSN (insn);
		}
	      return 0;
	    }
	  if (reg_mentioned_p (z_reg, src) == 0)
	    {
	      info->can_use_d = 0;
	      return 0;
	    }

	  if (insn != info->first)
	    return 0;

	  /* Compare insn which uses Z.  We have to save/restore the X/Y
	     register without modifying the condition codes.  For this
	     we have to use a push/pop insn.  */
	  info->must_push_reg = 1;
	  info->last = insn;
	}

      /* Z reg is set to something new. We don't need to load it.  */
      if (Z_REG_P (dst))
	{
	  if (!reg_mentioned_p (z_reg, src))
	    {
              /* Z reg is used before being set.  Treat this as
                 a new sequence of Z register replacement.  */
	      if (insn != info->first)
		{
                  return 0;
		}
              info->must_load_z = 0;
	    }
	  info->z_set_count++;
	  info->z_value = src;
	  if (SP_REG_P (src))
	    info->z_loaded_with_sp = 1;
	}
      else if (reg_mentioned_p (z_reg, dst))
	info->can_use_d = 0;

      this_insn_uses_d = reg_mentioned_p (d_reg, src)
	| reg_mentioned_p (d_reg, dst);
      this_insn_uses_ix = reg_mentioned_p (ix_reg, src)
	| reg_mentioned_p (ix_reg, dst);
      this_insn_uses_iy = reg_mentioned_p (iy_reg, src)
	| reg_mentioned_p (iy_reg, dst);
      this_insn_uses_z = reg_mentioned_p (z_reg, src);

      /* If z is used as an address operand (like (MEM (reg z))),
         we can't replace it with d.  */
      if (this_insn_uses_z && !Z_REG_P (src)
          && !(m68hc11_arith_operator (src, GET_MODE (src))
               && Z_REG_P (XEXP (src, 0))
               && !reg_mentioned_p (z_reg, XEXP (src, 1))
               && insn == info->first
               && dead_register_here (insn, d_reg)))
	info->can_use_d = 0;

      this_insn_uses_z_in_dst = reg_mentioned_p (z_reg, dst);
      if (TARGET_M6812 && !z_dies_here
          && ((this_insn_uses_z && side_effects_p (src))
              || (this_insn_uses_z_in_dst && side_effects_p (dst))))
        {
          info->need_save_z = 1;
          info->z_set_count++;
        }
      this_insn_uses_z |= this_insn_uses_z_in_dst;

      if (this_insn_uses_z && this_insn_uses_ix && this_insn_uses_iy)
	{
	  fatal_insn ("registers IX, IY and Z used in the same INSN", insn);
	}

      if (this_insn_uses_d)
	info->can_use_d = 0;

      /* IX and IY are used at the same time, we have to restore
         the value of the scratch register before this insn.  */
      if (this_insn_uses_ix && this_insn_uses_iy)
	{
	  return 0;
	}

      if (this_insn_uses_ix && X_REG_P (dst) && GET_MODE (dst) == SImode)
        info->can_use_d = 0;

      if (info->x_used == 0 && this_insn_uses_ix)
	{
	  if (info->y_used)
	    {
	      /* We have a (set (REG:HI X) (REG:HI Z)).
	         Since we use Z as the replacement register, this insn
	         is no longer necessary.  We turn it into a note.  We must
	         not reload the old value of X.  */
	      if (X_REG_P (dst) && rtx_equal_p (src, z_reg))
		{
		  if (z_dies_here)
		    {
		      info->need_save_z = 0;
		      info->z_died = 1;
		    }
		  info->must_save_reg = 0;
		  info->must_restore_reg = 0;
		  info->found_call = 1;
		  info->can_use_d = 0;
		  PUT_CODE (insn, NOTE);
		  NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
		  NOTE_SOURCE_FILE (insn) = 0;
		  info->last = NEXT_INSN (insn);
		  return 0;
		}

	      if (X_REG_P (dst)
		  && (rtx_equal_p (src, z_reg)
		      || (z_dies_here && !reg_mentioned_p (ix_reg, src))))
		{
		  if (z_dies_here)
		    {
		      info->need_save_z = 0;
		      info->z_died = 1;
		    }
		  info->last = NEXT_INSN (insn);
		  info->must_save_reg = 0;
		  info->must_restore_reg = 0;
		}
	      else if (X_REG_P (dst) && reg_mentioned_p (z_reg, src)
		       && !reg_mentioned_p (ix_reg, src))
		{
		  if (z_dies_here)
		    {
		      info->z_died = 1;
		      info->need_save_z = 0;
		    }
		  else if (TARGET_M6812 && side_effects_p (src))
                    {
                      info->last = 0;
                      info->must_restore_reg = 0;
                      return 0;
                    }
                  else
		    {
		      info->save_before_last = 1;
		    }
		  info->must_restore_reg = 0;
		  info->last = NEXT_INSN (insn);
		}
	      else if (info->can_use_d)
		{
		  info->last = NEXT_INSN (insn);
		  info->x_used = 1;
		}
	      return 0;
	    }
	  info->x_used = 1;
	  if (z_dies_here && !reg_mentioned_p (ix_reg, src)
	      && GET_CODE (dst) == REG && REGNO (dst) == HARD_X_REGNUM)
	    {
	      info->need_save_z = 0;
	      info->z_died = 1;
	      info->last = NEXT_INSN (insn);
	      info->regno = HARD_X_REGNUM;
	      info->must_save_reg = 0;
	      info->must_restore_reg = 0;
	      return 0;
	    }
          if (rtx_equal_p (src, z_reg) && rtx_equal_p (dst, ix_reg))
            {
              info->regno = HARD_X_REGNUM;
              info->must_restore_reg = 0;
              info->must_save_reg = 0;
              return 0;
            }
	}
      if (info->y_used == 0 && this_insn_uses_iy)
	{
	  if (info->x_used)
	    {
	      if (Y_REG_P (dst) && rtx_equal_p (src, z_reg))
		{
		  if (z_dies_here)
		    {
		      info->need_save_z = 0;
		      info->z_died = 1;
		    }
		  info->must_save_reg = 0;
		  info->must_restore_reg = 0;
		  info->found_call = 1;
		  info->can_use_d = 0;
		  PUT_CODE (insn, NOTE);
		  NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
		  NOTE_SOURCE_FILE (insn) = 0;
		  info->last = NEXT_INSN (insn);
		  return 0;
		}

	      if (Y_REG_P (dst)
		  && (rtx_equal_p (src, z_reg)
		      || (z_dies_here && !reg_mentioned_p (iy_reg, src))))
		{
		  if (z_dies_here)
		    {
		      info->z_died = 1;
		      info->need_save_z = 0;
		    }
		  info->last = NEXT_INSN (insn);
		  info->must_save_reg = 0;
		  info->must_restore_reg = 0;
		}
	      else if (Y_REG_P (dst) && reg_mentioned_p (z_reg, src)
		       && !reg_mentioned_p (iy_reg, src))
		{
		  if (z_dies_here)
		    {
		      info->z_died = 1;
		      info->need_save_z = 0;
		    }
		  else if (TARGET_M6812 && side_effects_p (src))
                    {
                      info->last = 0;
                      info->must_restore_reg = 0;
                      return 0;
                    }
                  else
		    {
		      info->save_before_last = 1;
		    }
		  info->must_restore_reg = 0;
		  info->last = NEXT_INSN (insn);
		}
	      else if (info->can_use_d)
		{
		  info->last = NEXT_INSN (insn);
		  info->y_used = 1;
		}

	      return 0;
	    }
	  info->y_used = 1;
	  if (z_dies_here && !reg_mentioned_p (iy_reg, src)
	      && GET_CODE (dst) == REG && REGNO (dst) == HARD_Y_REGNUM)
	    {
	      info->need_save_z = 0;
	      info->z_died = 1;
	      info->last = NEXT_INSN (insn);
	      info->regno = HARD_Y_REGNUM;
	      info->must_save_reg = 0;
	      info->must_restore_reg = 0;
	      return 0;
	    }
          if (rtx_equal_p (src, z_reg) && rtx_equal_p (dst, iy_reg))
            {
              info->regno = HARD_Y_REGNUM;
              info->must_restore_reg = 0;
              info->must_save_reg = 0;
              return 0;
            }
	}
      if (z_dies_here)
	{
	  info->need_save_z = 0;
	  info->z_died = 1;
	  if (info->last == 0)
	    info->last = NEXT_INSN (insn);
	  return 0;
	}
      return info->last != NULL_RTX ? 0 : 1;
    }
  if (GET_CODE (body) == PARALLEL)
    {
      int i;
      char ix_clobber = 0;
      char iy_clobber = 0;
      char z_clobber = 0;
      this_insn_uses_iy = 0;
      this_insn_uses_ix = 0;
      this_insn_uses_z = 0;

      for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
	{
	  rtx x;
	  int uses_ix, uses_iy, uses_z;

	  x = XVECEXP (body, 0, i);

	  if (info->can_use_d && reg_mentioned_p (d_reg, x))
	    info->can_use_d = 0;

	  uses_ix = reg_mentioned_p (ix_reg, x);
	  uses_iy = reg_mentioned_p (iy_reg, x);
	  uses_z = reg_mentioned_p (z_reg, x);
	  if (GET_CODE (x) == CLOBBER)
	    {
	      ix_clobber |= uses_ix;
	      iy_clobber |= uses_iy;
	      z_clobber |= uses_z;
	    }
	  else
	    {
	      this_insn_uses_ix |= uses_ix;
	      this_insn_uses_iy |= uses_iy;
	      this_insn_uses_z |= uses_z;
	    }
	  if (uses_z && GET_CODE (x) == SET)
	    {
	      rtx dst = XEXP (x, 0);

	      if (Z_REG_P (dst))
		info->z_set_count++;
	    }
          if (TARGET_M6812 && uses_z && side_effects_p (x))
            info->need_save_z = 1;

	  if (z_clobber)
	    info->need_save_z = 0;
	}
      if (debug_m6811)
	{
	  printf ("Uses X:%d Y:%d Z:%d CX:%d CY:%d CZ:%d\n",
		  this_insn_uses_ix, this_insn_uses_iy,
		  this_insn_uses_z, ix_clobber, iy_clobber, z_clobber);
	  debug_rtx (insn);
	}
      if (this_insn_uses_z)
	info->can_use_d = 0;

      if (z_clobber && info->first != insn)
	{
	  info->need_save_z = 0;
	  info->last = insn;
	  return 0;
	}
      if (z_clobber && info->x_used == 0 && info->y_used == 0)
	{
	  if (this_insn_uses_z == 0 && insn == info->first)
	    {
	      info->must_load_z = 0;
	    }
	  if (dead_register_here (insn, d_reg))
	    {
	      info->regno = HARD_D_REGNUM;
	      info->must_save_reg = 0;
	      info->must_restore_reg = 0;
	    }
	  else if (dead_register_here (insn, ix_reg))
	    {
	      info->regno = HARD_X_REGNUM;
	      info->must_save_reg = 0;
	      info->must_restore_reg = 0;
	    }
	  else if (dead_register_here (insn, iy_reg))
	    {
	      info->regno = HARD_Y_REGNUM;
	      info->must_save_reg = 0;
	      info->must_restore_reg = 0;
	    }
	  if (info->regno >= 0)
	    {
	      info->last = NEXT_INSN (insn);
	      return 0;
	    }
	  if (this_insn_uses_ix == 0)
	    {
	      info->regno = HARD_X_REGNUM;
	      info->must_save_reg = 1;
	      info->must_restore_reg = 1;
	    }
	  else if (this_insn_uses_iy == 0)
	    {
	      info->regno = HARD_Y_REGNUM;
	      info->must_save_reg = 1;
	      info->must_restore_reg = 1;
	    }
	  else
	    {
	      info->regno = HARD_D_REGNUM;
	      info->must_save_reg = 1;
	      info->must_restore_reg = 1;
	    }
	  info->last = NEXT_INSN (insn);
	  return 0;
	}

      if (((info->x_used || this_insn_uses_ix) && iy_clobber)
	  || ((info->y_used || this_insn_uses_iy) && ix_clobber))
	{
	  if (this_insn_uses_z)
	    {
	      if (info->y_used == 0 && iy_clobber)
		{
		  info->regno = HARD_Y_REGNUM;
		  info->must_save_reg = 0;
		  info->must_restore_reg = 0;
		}
	      if (info->first != insn
		  && ((info->y_used && ix_clobber)
		      || (info->x_used && iy_clobber)))
		info->last = insn;
	      else
		info->last = NEXT_INSN (insn);
	      info->save_before_last = 1;
	    }
	  return 0;
	}
      if (this_insn_uses_ix && this_insn_uses_iy)
	{
          if (this_insn_uses_z)
            {
              fatal_insn ("cannot do z-register replacement", insn);
            }
	  return 0;
	}
      if (info->x_used == 0 && (this_insn_uses_ix || ix_clobber))
	{
	  if (info->y_used)
	    {
	      return 0;
	    }
	  info->x_used = 1;
	  if (iy_clobber || z_clobber)
	    {
	      info->last = NEXT_INSN (insn);
	      info->save_before_last = 1;
	      return 0;
	    }
	}

      if (info->y_used == 0 && (this_insn_uses_iy || iy_clobber))
	{
	  if (info->x_used)
	    {
	      return 0;
	    }
	  info->y_used = 1;
	  if (ix_clobber || z_clobber)
	    {
	      info->last = NEXT_INSN (insn);
	      info->save_before_last = 1;
	      return 0;
	    }
	}
      if (z_dies_here)
	{
	  info->z_died = 1;
	  info->need_save_z = 0;
	}
      return 1;
    }
  if (GET_CODE (body) == CLOBBER)
    {

      /* IX and IY are used at the same time, we have to restore
         the value of the scratch register before this insn.  */
      if (this_insn_uses_ix && this_insn_uses_iy)
	{
	  return 0;
	}
      if (info->x_used == 0 && this_insn_uses_ix)
	{
	  if (info->y_used)
	    {
	      return 0;
	    }
	  info->x_used = 1;
	}
      if (info->y_used == 0 && this_insn_uses_iy)
	{
	  if (info->x_used)
	    {
	      return 0;
	    }
	  info->y_used = 1;
	}
      return 1;
    }
  return 1;
}

static void
m68hc11_find_z_replacement (rtx insn, struct replace_info *info)
{
  int reg;

  info->replace_reg = NULL_RTX;
  info->must_load_z = 1;
  info->need_save_z = 1;
  info->must_save_reg = 1;
  info->must_restore_reg = 1;
  info->first = insn;
  info->x_used = 0;
  info->y_used = 0;
  info->can_use_d = TARGET_M6811 ? 1 : 0;
  info->found_call = 0;
  info->z_died = 0;
  info->last = 0;
  info->regno = -1;
  info->z_set_count = 0;
  info->z_value = NULL_RTX;
  info->must_push_reg = 0;
  info->save_before_last = 0;
  info->z_loaded_with_sp = 0;

  /* Scan the insn forward to find an address register that is not used.
     Stop when:
     - the flow of the program changes,
     - when we detect that both X and Y are necessary,
     - when the Z register dies,
     - when the condition codes are set.  */

  for (; insn && info->z_died == 0; insn = NEXT_INSN (insn))
    {
      if (m68hc11_check_z_replacement (insn, info) == 0)
	break;
    }

  /* May be we can use Y or X if they contain the same value as Z.
     This happens very often after the reload.  */
  if (info->z_set_count == 1)
    {
      rtx p = info->first;
      rtx v = 0;

      if (info->x_used)
	{
	  v = find_last_value (iy_reg, &p, insn, 1);
	}
      else if (info->y_used)
	{
	  v = find_last_value (ix_reg, &p, insn, 1);
	}
      if (v && (v != iy_reg && v != ix_reg) && rtx_equal_p (v, info->z_value))
	{
	  if (info->x_used)
	    info->regno = HARD_Y_REGNUM;
	  else
	    info->regno = HARD_X_REGNUM;
	  info->must_load_z = 0;
	  info->must_save_reg = 0;
	  info->must_restore_reg = 0;
	  info->found_call = 1;
	}
    }
  if (info->z_set_count == 0)
    info->need_save_z = 0;

  if (insn == 0)
    info->need_save_z = 0;

  if (info->last == 0)
    info->last = insn;

  if (info->regno >= 0)
    {
      reg = info->regno;
      info->replace_reg = gen_rtx_REG (HImode, reg);
    }
  else if (info->can_use_d)
    {
      reg = HARD_D_REGNUM;
      info->replace_reg = d_reg;
    }
  else if (info->x_used)
    {
      reg = HARD_Y_REGNUM;
      info->replace_reg = iy_reg;
    }
  else
    {
      reg = HARD_X_REGNUM;
      info->replace_reg = ix_reg;
    }
  info->regno = reg;

  if (info->must_save_reg && info->must_restore_reg)
    {
      if (insn && dead_register_here (insn, info->replace_reg))
	{
	  info->must_save_reg = 0;
	  info->must_restore_reg = 0;
	}
    }
}

/* The insn uses the Z register.  Find a replacement register for it
   (either X or Y) and replace it in the insn and the next ones until
   the flow changes or the replacement register is used.  Instructions
   are emitted before and after the Z-block to preserve the value of
   Z and of the replacement register.  */

static void
m68hc11_z_replacement (rtx insn)
{
  rtx replace_reg_qi;
  rtx replace_reg;
  struct replace_info info;

  /* Find trivial case where we only need to replace z with the
     equivalent soft register.  */
  if (GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == SET)
    {
      rtx body = PATTERN (insn);
      rtx src = XEXP (body, 1);
      rtx dst = XEXP (body, 0);

      if (Z_REG_P (dst) && (H_REG_P (src) && !SP_REG_P (src)))
	{
	  XEXP (body, 0) = gen_rtx_REG (GET_MODE (dst), SOFT_Z_REGNUM);
	  return;
	}
      else if (Z_REG_P (src)
	       && ((H_REG_P (dst) && !SP_REG_P (src)) || dst == cc0_rtx))
	{
	  XEXP (body, 1) = gen_rtx_REG (GET_MODE (src), SOFT_Z_REGNUM);
	  return;
	}
      else if (D_REG_P (dst)
	       && m68hc11_arith_operator (src, GET_MODE (src))
	       && D_REG_P (XEXP (src, 0)) && Z_REG_P (XEXP (src, 1)))
	{
	  XEXP (src, 1) = gen_rtx_REG (GET_MODE (src), SOFT_Z_REGNUM);
	  return;
	}
      else if (Z_REG_P (dst) && GET_CODE (src) == CONST_INT
	       && INTVAL (src) == 0)
	{
	  XEXP (body, 0) = gen_rtx_REG (GET_MODE (dst), SOFT_Z_REGNUM);
          /* Force it to be re-recognized.  */
          INSN_CODE (insn) = -1;
	  return;
	}
    }

  m68hc11_find_z_replacement (insn, &info);

  replace_reg = info.replace_reg;
  replace_reg_qi = NULL_RTX;

  /* Save the X register in a .page0 location.  */
  if (info.must_save_reg && !info.must_push_reg)
    {
      rtx dst;

      if (info.must_push_reg && 0)
	dst = gen_rtx_MEM (HImode,
		       gen_rtx_PRE_DEC (HImode,
				gen_rtx_REG (HImode, HARD_SP_REGNUM)));
      else
	dst = gen_rtx_REG (HImode, SOFT_SAVED_XY_REGNUM);

      emit_insn_before (gen_movhi (dst,
				   gen_rtx_REG (HImode, info.regno)), insn);
    }
  if (info.must_load_z && !info.must_push_reg)
    {
      emit_insn_before (gen_movhi (gen_rtx_REG (HImode, info.regno),
				   gen_rtx_REG (HImode, SOFT_Z_REGNUM)),
			insn);
    }


  /* Replace all occurrence of Z by replace_reg.
     Stop when the last instruction to replace is reached.
     Also stop when we detect a change in the flow (but it's not
     necessary; just safeguard).  */

  for (; insn && insn != info.last; insn = NEXT_INSN (insn))
    {
      rtx body;

      if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == BARRIER)
	break;

      if (GET_CODE (insn) != INSN
	  && GET_CODE (insn) != CALL_INSN && GET_CODE (insn) != JUMP_INSN)
	continue;

      body = PATTERN (insn);
      if (GET_CODE (body) == SET || GET_CODE (body) == PARALLEL
          || GET_CODE (body) == ASM_OPERANDS
	  || GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN)
	{
          rtx note;

	  if (debug_m6811 && reg_mentioned_p (replace_reg, body))
	    {
	      printf ("Reg mentioned here...:\n");
	      fflush (stdout);
	      debug_rtx (insn);
	    }

	  /* Stack pointer was decremented by 2 due to the push.
	     Correct that by adding 2 to the destination.  */
	  if (info.must_push_reg
	      && info.z_loaded_with_sp && GET_CODE (body) == SET)
	    {
	      rtx src, dst;

	      src = SET_SRC (body);
	      dst = SET_DEST (body);
	      if (SP_REG_P (src) && Z_REG_P (dst))
		emit_insn_after (gen_addhi3 (dst, dst, const2_rtx), insn);
	    }

	  /* Replace any (REG:HI Z) occurrence by either X or Y.  */
	  if (!validate_replace_rtx (z_reg, replace_reg, insn))
	    {
	      INSN_CODE (insn) = -1;
	      if (!validate_replace_rtx (z_reg, replace_reg, insn))
		fatal_insn ("cannot do z-register replacement", insn);
	    }

	  /* Likewise for (REG:QI Z).  */
	  if (reg_mentioned_p (z_reg, insn))
	    {
	      if (replace_reg_qi == NULL_RTX)
		replace_reg_qi = gen_rtx_REG (QImode, REGNO (replace_reg));
	      validate_replace_rtx (z_reg_qi, replace_reg_qi, insn);
	    }

          /* If there is a REG_INC note on Z, replace it with a
             REG_INC note on the replacement register.  This is necessary
             to make sure that the flow pass will identify the change
             and it will not remove a possible insn that saves Z.  */
          for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
            {
              if (REG_NOTE_KIND (note) == REG_INC
                  && GET_CODE (XEXP (note, 0)) == REG
                  && REGNO (XEXP (note, 0)) == REGNO (z_reg))
                {
                  XEXP (note, 0) = replace_reg;
                }
            }
	}
      if (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN)
	break;
    }

  /* Save Z before restoring the old value.  */
  if (insn && info.need_save_z && !info.must_push_reg)
    {
      rtx save_pos_insn = insn;

      /* If Z is clobber by the last insn, we have to save its value
         before the last instruction.  */
      if (info.save_before_last)
	save_pos_insn = PREV_INSN (save_pos_insn);

      emit_insn_before (gen_movhi (gen_rtx_REG (HImode, SOFT_Z_REGNUM),
				   gen_rtx_REG (HImode, info.regno)),
			save_pos_insn);
    }

  if (info.must_push_reg && info.last)
    {
      rtx new_body, body;

      body = PATTERN (info.last);
      new_body = gen_rtx_PARALLEL (VOIDmode,
			  gen_rtvec (3, body,
				     gen_rtx_USE (VOIDmode,
					      replace_reg),
				     gen_rtx_USE (VOIDmode,
					      gen_rtx_REG (HImode,
						       SOFT_Z_REGNUM))));
      PATTERN (info.last) = new_body;

      /* Force recognition on insn since we changed it.  */
      INSN_CODE (insn) = -1;

      if (!validate_replace_rtx (z_reg, replace_reg, info.last))
	{
	  fatal_insn ("invalid Z register replacement for insn", insn);
	}
      insn = NEXT_INSN (info.last);
    }

  /* Restore replacement register unless it was died.  */
  if (insn && info.must_restore_reg && !info.must_push_reg)
    {
      rtx dst;

      if (info.must_push_reg && 0)
	dst = gen_rtx_MEM (HImode,
		       gen_rtx_POST_INC (HImode,
				gen_rtx_REG (HImode, HARD_SP_REGNUM)));
      else
	dst = gen_rtx_REG (HImode, SOFT_SAVED_XY_REGNUM);

      emit_insn_before (gen_movhi (gen_rtx_REG (HImode, info.regno),
				   dst), insn);
    }

}


/* Scan all the insn and re-affects some registers
    - The Z register (if it was used), is affected to X or Y depending
      on the instruction.  */

static void
m68hc11_reassign_regs (rtx first)
{
  rtx insn;

  ix_reg = gen_rtx_REG (HImode, HARD_X_REGNUM);
  iy_reg = gen_rtx_REG (HImode, HARD_Y_REGNUM);
  z_reg = gen_rtx_REG (HImode, HARD_Z_REGNUM);
  z_reg_qi = gen_rtx_REG (QImode, HARD_Z_REGNUM);

  /* Scan all insns to replace Z by X or Y preserving the old value
     of X/Y and restoring it afterward.  */

  for (insn = first; insn; insn = NEXT_INSN (insn))
    {
      rtx body;

      if (GET_CODE (insn) == CODE_LABEL
	  || GET_CODE (insn) == NOTE || GET_CODE (insn) == BARRIER)
	continue;

      if (!INSN_P (insn))
	continue;

      body = PATTERN (insn);
      if (GET_CODE (body) == CLOBBER || GET_CODE (body) == USE)
	continue;

      if (GET_CODE (body) == CONST_INT || GET_CODE (body) == ASM_INPUT
	  || GET_CODE (body) == ASM_OPERANDS
	  || GET_CODE (body) == UNSPEC || GET_CODE (body) == UNSPEC_VOLATILE)
	continue;

      if (GET_CODE (body) == SET || GET_CODE (body) == PARALLEL
	  || GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN)
	{

	  /* If Z appears in this insn, replace it in the current insn
	     and the next ones until the flow changes or we have to
	     restore back the replacement register.  */

	  if (reg_mentioned_p (z_reg, body))
	    {
	      m68hc11_z_replacement (insn);
	    }
	}
      else
	{
	  printf ("insn not handled by Z replacement:\n");
	  fflush (stdout);
	  debug_rtx (insn);
	}
    }
}


/* Machine-dependent reorg pass.
   Specific optimizations are defined here:
    - this pass changes the Z register into either X or Y
      (it preserves X/Y previous values in a memory slot in page0).

   When this pass is finished, the global variable
   'z_replacement_completed' is set to 2.  */

static void
m68hc11_reorg (void)
{
  int split_done = 0;
  rtx insn, first;

  z_replacement_completed = 0;
  z_reg = gen_rtx_REG (HImode, HARD_Z_REGNUM);
  first = get_insns ();

  /* Some RTX are shared at this point.  This breaks the Z register
     replacement, unshare everything.  */
  unshare_all_rtl_again (first);

  /* Force a split of all splitable insn.  This is necessary for the
     Z register replacement mechanism because we end up with basic insns.  */
  split_all_insns_noflow ();
  split_done = 1;

  z_replacement_completed = 1;
  m68hc11_reassign_regs (first);

  if (optimize)
    compute_bb_for_insn ();

  /* After some splitting, there are some opportunities for CSE pass.
     This happens quite often when 32-bit or above patterns are split.  */
  if (optimize > 0 && split_done)
    {
      reload_cse_regs (first);
    }

  /* Re-create the REG_DEAD notes.  These notes are used in the machine
     description to use the best assembly directives.  */
  if (optimize)
    {
      /* Before recomputing the REG_DEAD notes, remove all of them.
         This is necessary because the reload_cse_regs() pass can
         have replaced some (MEM) with a register.  In that case,
         the REG_DEAD that could exist for that register may become
         wrong.  */
      for (insn = first; insn; insn = NEXT_INSN (insn))
        {
          if (INSN_P (insn))
            {
              rtx *pnote;

              pnote = &REG_NOTES (insn);
              while (*pnote != 0)
                {
                  if (REG_NOTE_KIND (*pnote) == REG_DEAD)
                    *pnote = XEXP (*pnote, 1);
                  else
                    pnote = &XEXP (*pnote, 1);
                }
            }
        }

      life_analysis (0, PROP_REG_INFO | PROP_DEATH_NOTES);
    }

  z_replacement_completed = 2;

  /* If optimizing, then go ahead and split insns that must be
     split after Z register replacement.  This gives more opportunities
     for peephole (in particular for consecutives xgdx/xgdy).  */
  if (optimize > 0)
    split_all_insns_noflow ();

  /* Once insns are split after the z_replacement_completed == 2,
     we must not re-run the life_analysis.  The xgdx/xgdy patterns
     are not recognized and the life_analysis pass removes some
     insns because it thinks some (SETs) are noops or made to dead
     stores (which is false due to the swap).

     Do a simple pass to eliminate the noop set that the final
     split could generate (because it was easier for split definition).  */
  {
    rtx insn;

    for (insn = first; insn; insn = NEXT_INSN (insn))
      {
	rtx body;

	if (INSN_DELETED_P (insn))
	  continue;
	if (!INSN_P (insn))
	  continue;

	/* Remove the (set (R) (R)) insns generated by some splits.  */
	body = PATTERN (insn);
	if (GET_CODE (body) == SET
	    && rtx_equal_p (SET_SRC (body), SET_DEST (body)))
	  {
	    PUT_CODE (insn, NOTE);
	    NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
	    NOTE_SOURCE_FILE (insn) = 0;
	    continue;
	  }
      }
  }
}

/* Override memcpy */

static void
m68hc11_init_libfuncs (void)
{
  memcpy_libfunc = init_one_libfunc ("__memcpy");
  memcmp_libfunc = init_one_libfunc ("__memcmp");
  memset_libfunc = init_one_libfunc ("__memset");
}



/* Cost functions.  */

/* Cost of moving memory.  */
int
m68hc11_memory_move_cost (enum machine_mode mode, enum reg_class class,
                          int in ATTRIBUTE_UNUSED)
{
  if (class <= H_REGS && class > NO_REGS)
    {
      if (GET_MODE_SIZE (mode) <= 2)
	return COSTS_N_INSNS (1) + (reload_completed | reload_in_progress);
      else
	return COSTS_N_INSNS (2) + (reload_completed | reload_in_progress);
    }
  else
    {
      if (GET_MODE_SIZE (mode) <= 2)
	return COSTS_N_INSNS (3);
      else
	return COSTS_N_INSNS (4);
    }
}


/* Cost of moving data from a register of class 'from' to on in class 'to'.
   Reload does not check the constraint of set insns when the two registers
   have a move cost of 2.  Setting a higher cost will force reload to check
   the constraints.  */
int
m68hc11_register_move_cost (enum machine_mode mode, enum reg_class from,
                            enum reg_class to)
{
  /* All costs are symmetric, so reduce cases by putting the
     lower number class as the destination.  */
  if (from < to)
    {
      enum reg_class tmp = to;
      to = from, from = tmp;
    }
  if (to >= S_REGS)
    return m68hc11_memory_move_cost (mode, S_REGS, 0);
  else if (from <= S_REGS)
    return COSTS_N_INSNS (1) + (reload_completed | reload_in_progress);
  else
    return COSTS_N_INSNS (2);
}


/* Provide the costs of an addressing mode that contains ADDR.
   If ADDR is not a valid address, its cost is irrelevant.  */

static int
m68hc11_address_cost (rtx addr)
{
  int cost = 4;

  switch (GET_CODE (addr))
    {
    case REG:
      /* Make the cost of hard registers and specially SP, FP small.  */
      if (REGNO (addr) < FIRST_PSEUDO_REGISTER)
	cost = 0;
      else
	cost = 1;
      break;

    case SYMBOL_REF:
      cost = 8;
      break;

    case LABEL_REF:
    case CONST:
      cost = 0;
      break;

    case PLUS:
      {
	register rtx plus0 = XEXP (addr, 0);
	register rtx plus1 = XEXP (addr, 1);

	if (GET_CODE (plus0) != REG)
	  break;

	switch (GET_CODE (plus1))
	  {
	  case CONST_INT:
	    if (INTVAL (plus1) >= 2 * m68hc11_max_offset
		|| INTVAL (plus1) < m68hc11_min_offset)
	      cost = 3;
	    else if (INTVAL (plus1) >= m68hc11_max_offset)
	      cost = 2;
	    else
	      cost = 1;
	    if (REGNO (plus0) < FIRST_PSEUDO_REGISTER)
	      cost += 0;
	    else
	      cost += 1;
	    break;

	  case SYMBOL_REF:
	    cost = 8;
	    break;

	  case CONST:
	  case LABEL_REF:
	    cost = 0;
	    break;

	  default:
	    break;
	  }
	break;
      }
    case PRE_DEC:
    case PRE_INC:
      if (SP_REG_P (XEXP (addr, 0)))
	cost = 1;
      break;

    default:
      break;
    }
  if (debug_m6811)
    {
      printf ("Address cost: %d for :", cost);
      fflush (stdout);
      debug_rtx (addr);
    }

  return cost;
}

static int
m68hc11_shift_cost (enum machine_mode mode, rtx x, int shift)
{
  int total;

  total = rtx_cost (x, SET);
  if (mode == QImode)
    total += m68hc11_cost->shiftQI_const[shift % 8];
  else if (mode == HImode)
    total += m68hc11_cost->shiftHI_const[shift % 16];
  else if (shift == 8 || shift == 16 || shift == 32)
    total += m68hc11_cost->shiftHI_const[8];
  else if (shift != 0 && shift != 16 && shift != 32)
    {
      total += m68hc11_cost->shiftHI_const[1] * shift;
    }

  /* For SI and others, the cost is higher.  */
  if (GET_MODE_SIZE (mode) > 2 && (shift % 16) != 0)
    total *= GET_MODE_SIZE (mode) / 2;

  /* When optimizing for size, make shift more costly so that
     multiplications are preferred.  */
  if (optimize_size && (shift % 8) != 0)
    total *= 2;
  
  return total;
}

static int
m68hc11_rtx_costs_1 (rtx x, enum rtx_code code,
                     enum rtx_code outer_code ATTRIBUTE_UNUSED)
{
  enum machine_mode mode = GET_MODE (x);
  int extra_cost = 0;
  int total;

  switch (code)
    {
    case ROTATE:
    case ROTATERT:
    case ASHIFT:
    case LSHIFTRT:
    case ASHIFTRT:
      if (GET_CODE (XEXP (x, 1)) == CONST_INT)
	{
          return m68hc11_shift_cost (mode, XEXP (x, 0), INTVAL (XEXP (x, 1)));
	}

      total = rtx_cost (XEXP (x, 0), code) + rtx_cost (XEXP (x, 1), code);
      total += m68hc11_cost->shift_var;
      return total;

    case AND:
    case XOR:
    case IOR:
      total = rtx_cost (XEXP (x, 0), code) + rtx_cost (XEXP (x, 1), code);
      total += m68hc11_cost->logical;

      /* Logical instructions are byte instructions only.  */
      total *= GET_MODE_SIZE (mode);
      return total;

    case MINUS:
    case PLUS:
      total = rtx_cost (XEXP (x, 0), code) + rtx_cost (XEXP (x, 1), code);
      total += m68hc11_cost->add;
      if (GET_MODE_SIZE (mode) > 2)
	{
	  total *= GET_MODE_SIZE (mode) / 2;
	}
      return total;

    case UDIV:
    case DIV:
    case MOD:
      total = rtx_cost (XEXP (x, 0), code) + rtx_cost (XEXP (x, 1), code);
      switch (mode)
        {
        case QImode:
          total += m68hc11_cost->divQI;
          break;

        case HImode:
          total += m68hc11_cost->divHI;
          break;

        case SImode:
        default:
          total += m68hc11_cost->divSI;
          break;
        }
      return total;
      
    case MULT:
      /* mul instruction produces 16-bit result.  */
      if (mode == HImode && GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
          && GET_CODE (XEXP (x, 1)) == ZERO_EXTEND)
        return m68hc11_cost->multQI
          + rtx_cost (XEXP (XEXP (x, 0), 0), code)
          + rtx_cost (XEXP (XEXP (x, 1), 0), code);

      /* emul instruction produces 32-bit result for 68HC12.  */
      if (TARGET_M6812 && mode == SImode
          && GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
          && GET_CODE (XEXP (x, 1)) == ZERO_EXTEND)
        return m68hc11_cost->multHI
          + rtx_cost (XEXP (XEXP (x, 0), 0), code)
          + rtx_cost (XEXP (XEXP (x, 1), 0), code);

      total = rtx_cost (XEXP (x, 0), code) + rtx_cost (XEXP (x, 1), code);
      switch (mode)
        {
        case QImode:
          total += m68hc11_cost->multQI;
          break;

        case HImode:
          total += m68hc11_cost->multHI;
          break;

        case SImode:
        default:
          total += m68hc11_cost->multSI;
          break;
        }
      return total;

    case NEG:
    case SIGN_EXTEND:
      extra_cost = COSTS_N_INSNS (2);

      /* Fall through */
    case NOT:
    case COMPARE:
    case ABS:
    case ZERO_EXTEND:
      total = extra_cost + rtx_cost (XEXP (x, 0), code);
      if (mode == QImode)
	{
	  return total + COSTS_N_INSNS (1);
	}
      if (mode == HImode)
	{
	  return total + COSTS_N_INSNS (2);
	}
      if (mode == SImode)
	{
	  return total + COSTS_N_INSNS (4);
	}
      return total + COSTS_N_INSNS (8);

    case IF_THEN_ELSE:
      if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
	return COSTS_N_INSNS (1);

      return COSTS_N_INSNS (1);

    default:
      return COSTS_N_INSNS (4);
    }
}

static bool
m68hc11_rtx_costs (rtx x, int code, int outer_code, int *total)
{
  switch (code)
    {
      /* Constants are cheap.  Moving them in registers must be avoided
         because most instructions do not handle two register operands.  */
    case CONST_INT:
    case CONST:
    case LABEL_REF:
    case SYMBOL_REF:
    case CONST_DOUBLE:
      /* Logical and arithmetic operations with a constant operand are
	 better because they are not supported with two registers.  */
      /* 'clr' is slow */
      if (outer_code == SET && x == const0_rtx)
	 /* After reload, the reload_cse pass checks the cost to change
	    a SET into a PLUS.  Make const0 cheap then.  */
	*total = 1 - reload_completed;
      else
	*total = 0;
      return true;
    
    case ROTATE:
    case ROTATERT:
    case ASHIFT:
    case LSHIFTRT:
    case ASHIFTRT:
    case MINUS:
    case PLUS:
    case AND:
    case XOR:
    case IOR:
    case UDIV:
    case DIV:
    case MOD:
    case MULT:
    case NEG:
    case SIGN_EXTEND:
    case NOT:
    case COMPARE:
    case ZERO_EXTEND:
    case IF_THEN_ELSE:
      *total = m68hc11_rtx_costs_1 (x, code, outer_code);
      return true;

    default:
      return false;
    }
}


/* Worker function for TARGET_ASM_FILE_START.  */

static void
m68hc11_file_start (void)
{
  default_file_start ();
  
  fprintf (asm_out_file, "\t.mode %s\n", TARGET_SHORT ? "mshort" : "mlong");
}


/* Worker function for TARGET_ASM_CONSTRUCTOR.  */

static void
m68hc11_asm_out_constructor (rtx symbol, int priority)
{
  default_ctor_section_asm_out_constructor (symbol, priority);
  fprintf (asm_out_file, "\t.globl\t__do_global_ctors\n");
}

/* Worker function for TARGET_ASM_DESTRUCTOR.  */

static void
m68hc11_asm_out_destructor (rtx symbol, int priority)
{
  default_dtor_section_asm_out_destructor (symbol, priority);
  fprintf (asm_out_file, "\t.globl\t__do_global_dtors\n");
}

/* Worker function for TARGET_STRUCT_VALUE_RTX.  */

static rtx
m68hc11_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
			  int incoming ATTRIBUTE_UNUSED)
{
  return gen_rtx_REG (Pmode, HARD_D_REGNUM);
}

/* Return true if type TYPE should be returned in memory.
   Blocks and data types largers than 4 bytes cannot be returned
   in the register (D + X = 4).  */

static bool
m68hc11_return_in_memory (tree type, tree fntype ATTRIBUTE_UNUSED)
{
  if (TYPE_MODE (type) == BLKmode)
    {
      HOST_WIDE_INT size = int_size_in_bytes (type);
      return (size == -1 || size > 4);
    }
  else
    return GET_MODE_SIZE (TYPE_MODE (type)) > 4;
}

#include "gt-m68hc11.h"