aboutsummaryrefslogtreecommitdiff
path: root/gdbserver/linux-ppc-low.cc
blob: 86fbc8f5d96ea1cc543b69541b5424ef1564fb4a (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
/* GNU/Linux/PowerPC specific low level interface, for the remote server for
   GDB.
   Copyright (C) 1995-2023 Free Software Foundation, Inc.

   This file is part of GDB.

   This program 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 3 of the License, or
   (at your option) any later version.

   This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include "server.h"
#include "linux-low.h"

#include "elf/common.h"
#include <sys/uio.h>
#include <elf.h>
#include <asm/ptrace.h>

#include "arch/ppc-linux-common.h"
#include "arch/ppc-linux-tdesc.h"
#include "nat/ppc-linux.h"
#include "nat/linux-ptrace.h"
#include "linux-ppc-tdesc-init.h"
#include "ax.h"
#include "tracepoint.h"

#define PPC_FIELD(value, from, len) \
	(((value) >> (32 - (from) - (len))) & ((1 << (len)) - 1))
#define PPC_SEXT(v, bs) \
	((((CORE_ADDR) (v) & (((CORE_ADDR) 1 << (bs)) - 1)) \
	  ^ ((CORE_ADDR) 1 << ((bs) - 1))) \
	 - ((CORE_ADDR) 1 << ((bs) - 1)))
#define PPC_OP6(insn)	PPC_FIELD (insn, 0, 6)
#define PPC_BO(insn)	PPC_FIELD (insn, 6, 5)
#define PPC_LI(insn)	(PPC_SEXT (PPC_FIELD (insn, 6, 24), 24) << 2)
#define PPC_BD(insn)	(PPC_SEXT (PPC_FIELD (insn, 16, 14), 14) << 2)

/* Linux target op definitions for the PowerPC architecture.  */

class ppc_target : public linux_process_target
{
public:

  const regs_info *get_regs_info () override;

  const gdb_byte *sw_breakpoint_from_kind (int kind, int *size) override;

  bool supports_z_point_type (char z_type) override;


  void low_collect_ptrace_register (regcache *regcache, int regno,
				    char *buf) override;

  void low_supply_ptrace_register (regcache *regcache, int regno,
				   const char *buf) override;

  bool supports_tracepoints () override;

  bool supports_fast_tracepoints () override;

  int install_fast_tracepoint_jump_pad
    (CORE_ADDR tpoint, CORE_ADDR tpaddr, CORE_ADDR collector,
     CORE_ADDR lockaddr, ULONGEST orig_size, CORE_ADDR *jump_entry,
     CORE_ADDR *trampoline, ULONGEST *trampoline_size,
     unsigned char *jjump_pad_insn, ULONGEST *jjump_pad_insn_size,
     CORE_ADDR *adjusted_insn_addr, CORE_ADDR *adjusted_insn_addr_end,
     char *err) override;

  int get_min_fast_tracepoint_insn_len () override;

  struct emit_ops *emit_ops () override;

  int get_ipa_tdesc_idx () override;

protected:

  void low_arch_setup () override;

  bool low_cannot_fetch_register (int regno) override;

  bool low_cannot_store_register (int regno) override;

  bool low_supports_breakpoints () override;

  CORE_ADDR low_get_pc (regcache *regcache) override;

  void low_set_pc (regcache *regcache, CORE_ADDR newpc) override;

  bool low_breakpoint_at (CORE_ADDR pc) override;

  int low_insert_point (raw_bkpt_type type, CORE_ADDR addr,
			int size, raw_breakpoint *bp) override;

  int low_remove_point (raw_bkpt_type type, CORE_ADDR addr,
			int size, raw_breakpoint *bp) override;

  int low_get_thread_area (int lwpid, CORE_ADDR *addrp) override;
};

/* The singleton target ops object.  */

static ppc_target the_ppc_target;

/* Holds the AT_HWCAP auxv entry.  */

static unsigned long ppc_hwcap;

/* Holds the AT_HWCAP2 auxv entry.  */

static unsigned long ppc_hwcap2;


#define ppc_num_regs 73

#ifdef __powerpc64__
/* We use a constant for FPSCR instead of PT_FPSCR, because
   many shipped PPC64 kernels had the wrong value in ptrace.h.  */
static int ppc_regmap[] =
 {PT_R0 * 8,     PT_R1 * 8,     PT_R2 * 8,     PT_R3 * 8,
  PT_R4 * 8,     PT_R5 * 8,     PT_R6 * 8,     PT_R7 * 8,
  PT_R8 * 8,     PT_R9 * 8,     PT_R10 * 8,    PT_R11 * 8,
  PT_R12 * 8,    PT_R13 * 8,    PT_R14 * 8,    PT_R15 * 8,
  PT_R16 * 8,    PT_R17 * 8,    PT_R18 * 8,    PT_R19 * 8,
  PT_R20 * 8,    PT_R21 * 8,    PT_R22 * 8,    PT_R23 * 8,
  PT_R24 * 8,    PT_R25 * 8,    PT_R26 * 8,    PT_R27 * 8,
  PT_R28 * 8,    PT_R29 * 8,    PT_R30 * 8,    PT_R31 * 8,
  PT_FPR0*8,     PT_FPR0*8 + 8, PT_FPR0*8+16,  PT_FPR0*8+24,
  PT_FPR0*8+32,  PT_FPR0*8+40,  PT_FPR0*8+48,  PT_FPR0*8+56,
  PT_FPR0*8+64,  PT_FPR0*8+72,  PT_FPR0*8+80,  PT_FPR0*8+88,
  PT_FPR0*8+96,  PT_FPR0*8+104,  PT_FPR0*8+112,  PT_FPR0*8+120,
  PT_FPR0*8+128, PT_FPR0*8+136,  PT_FPR0*8+144,  PT_FPR0*8+152,
  PT_FPR0*8+160,  PT_FPR0*8+168,  PT_FPR0*8+176,  PT_FPR0*8+184,
  PT_FPR0*8+192,  PT_FPR0*8+200,  PT_FPR0*8+208,  PT_FPR0*8+216,
  PT_FPR0*8+224,  PT_FPR0*8+232,  PT_FPR0*8+240,  PT_FPR0*8+248,
  PT_NIP * 8,    PT_MSR * 8,    PT_CCR * 8,    PT_LNK * 8,
  PT_CTR * 8,    PT_XER * 8,    PT_FPR0*8 + 256,
  PT_ORIG_R3 * 8, PT_TRAP * 8 };
#else
/* Currently, don't check/send MQ.  */
static int ppc_regmap[] =
 {PT_R0 * 4,     PT_R1 * 4,     PT_R2 * 4,     PT_R3 * 4,
  PT_R4 * 4,     PT_R5 * 4,     PT_R6 * 4,     PT_R7 * 4,
  PT_R8 * 4,     PT_R9 * 4,     PT_R10 * 4,    PT_R11 * 4,
  PT_R12 * 4,    PT_R13 * 4,    PT_R14 * 4,    PT_R15 * 4,
  PT_R16 * 4,    PT_R17 * 4,    PT_R18 * 4,    PT_R19 * 4,
  PT_R20 * 4,    PT_R21 * 4,    PT_R22 * 4,    PT_R23 * 4,
  PT_R24 * 4,    PT_R25 * 4,    PT_R26 * 4,    PT_R27 * 4,
  PT_R28 * 4,    PT_R29 * 4,    PT_R30 * 4,    PT_R31 * 4,
  PT_FPR0*4,     PT_FPR0*4 + 8, PT_FPR0*4+16,  PT_FPR0*4+24,
  PT_FPR0*4+32,  PT_FPR0*4+40,  PT_FPR0*4+48,  PT_FPR0*4+56,
  PT_FPR0*4+64,  PT_FPR0*4+72,  PT_FPR0*4+80,  PT_FPR0*4+88,
  PT_FPR0*4+96,  PT_FPR0*4+104,  PT_FPR0*4+112,  PT_FPR0*4+120,
  PT_FPR0*4+128, PT_FPR0*4+136,  PT_FPR0*4+144,  PT_FPR0*4+152,
  PT_FPR0*4+160,  PT_FPR0*4+168,  PT_FPR0*4+176,  PT_FPR0*4+184,
  PT_FPR0*4+192,  PT_FPR0*4+200,  PT_FPR0*4+208,  PT_FPR0*4+216,
  PT_FPR0*4+224,  PT_FPR0*4+232,  PT_FPR0*4+240,  PT_FPR0*4+248,
  PT_NIP * 4,    PT_MSR * 4,    PT_CCR * 4,    PT_LNK * 4,
  PT_CTR * 4,    PT_XER * 4,    PT_FPSCR * 4,
  PT_ORIG_R3 * 4, PT_TRAP * 4
 };

static int ppc_regmap_e500[] =
 {PT_R0 * 4,     PT_R1 * 4,     PT_R2 * 4,     PT_R3 * 4,
  PT_R4 * 4,     PT_R5 * 4,     PT_R6 * 4,     PT_R7 * 4,
  PT_R8 * 4,     PT_R9 * 4,     PT_R10 * 4,    PT_R11 * 4,
  PT_R12 * 4,    PT_R13 * 4,    PT_R14 * 4,    PT_R15 * 4,
  PT_R16 * 4,    PT_R17 * 4,    PT_R18 * 4,    PT_R19 * 4,
  PT_R20 * 4,    PT_R21 * 4,    PT_R22 * 4,    PT_R23 * 4,
  PT_R24 * 4,    PT_R25 * 4,    PT_R26 * 4,    PT_R27 * 4,
  PT_R28 * 4,    PT_R29 * 4,    PT_R30 * 4,    PT_R31 * 4,
  -1,            -1,            -1,            -1,
  -1,            -1,            -1,            -1,
  -1,            -1,            -1,            -1,
  -1,            -1,            -1,            -1,
  -1,            -1,            -1,            -1,
  -1,            -1,            -1,            -1,
  -1,            -1,            -1,            -1,
  -1,            -1,            -1,            -1,
  PT_NIP * 4,    PT_MSR * 4,    PT_CCR * 4,    PT_LNK * 4,
  PT_CTR * 4,    PT_XER * 4,    -1,
  PT_ORIG_R3 * 4, PT_TRAP * 4
 };
#endif

/* Check whether the kernel provides a register set with number
   REGSET_ID of size REGSETSIZE for process/thread TID.  */

static int
ppc_check_regset (int tid, int regset_id, int regsetsize)
{
  void *buf = alloca (regsetsize);
  struct iovec iov;

  iov.iov_base = buf;
  iov.iov_len = regsetsize;

  if (ptrace (PTRACE_GETREGSET, tid, regset_id, &iov) >= 0
      || errno == ENODATA)
    return 1;
  return 0;
}

bool
ppc_target::low_cannot_store_register (int regno)
{
  const struct target_desc *tdesc = current_process ()->tdesc;

#ifndef __powerpc64__
  /* Some kernels do not allow us to store fpscr.  */
  if (!(ppc_hwcap & PPC_FEATURE_HAS_SPE)
      && regno == find_regno (tdesc, "fpscr"))
    return true;
#endif

  /* Some kernels do not allow us to store orig_r3 or trap.  */
  if (regno == find_regno (tdesc, "orig_r3")
      || regno == find_regno (tdesc, "trap"))
    return true;

  return false;
}

bool
ppc_target::low_cannot_fetch_register (int regno)
{
  return false;
}

void
ppc_target::low_collect_ptrace_register (regcache *regcache, int regno,
					 char *buf)
{
  memset (buf, 0, sizeof (long));

  if (__BYTE_ORDER == __LITTLE_ENDIAN)
    {
      /* Little-endian values always sit at the left end of the buffer.  */
      collect_register (regcache, regno, buf);
    }
  else if (__BYTE_ORDER == __BIG_ENDIAN)
    {
      /* Big-endian values sit at the right end of the buffer.  In case of
	 registers whose sizes are smaller than sizeof (long), we must use a
	 padding to access them correctly.  */
      int size = register_size (regcache->tdesc, regno);

      if (size < sizeof (long))
	collect_register (regcache, regno, buf + sizeof (long) - size);
      else
	collect_register (regcache, regno, buf);
    }
  else
    perror_with_name ("Unexpected byte order");
}

void
ppc_target::low_supply_ptrace_register (regcache *regcache, int regno,
					const char *buf)
{
  if (__BYTE_ORDER == __LITTLE_ENDIAN)
    {
      /* Little-endian values always sit at the left end of the buffer.  */
      supply_register (regcache, regno, buf);
    }
  else if (__BYTE_ORDER == __BIG_ENDIAN)
    {
      /* Big-endian values sit at the right end of the buffer.  In case of
	 registers whose sizes are smaller than sizeof (long), we must use a
	 padding to access them correctly.  */
      int size = register_size (regcache->tdesc, regno);

      if (size < sizeof (long))
	supply_register (regcache, regno, buf + sizeof (long) - size);
      else
	supply_register (regcache, regno, buf);
    }
  else
    perror_with_name ("Unexpected byte order");
}

bool
ppc_target::low_supports_breakpoints ()
{
  return true;
}

CORE_ADDR
ppc_target::low_get_pc (regcache *regcache)
{
  if (register_size (regcache->tdesc, 0) == 4)
    {
      unsigned int pc;
      collect_register_by_name (regcache, "pc", &pc);
      return (CORE_ADDR) pc;
    }
  else
    {
      unsigned long pc;
      collect_register_by_name (regcache, "pc", &pc);
      return (CORE_ADDR) pc;
    }
}

void
ppc_target::low_set_pc (regcache *regcache, CORE_ADDR pc)
{
  if (register_size (regcache->tdesc, 0) == 4)
    {
      unsigned int newpc = pc;
      supply_register_by_name (regcache, "pc", &newpc);
    }
  else
    {
      unsigned long newpc = pc;
      supply_register_by_name (regcache, "pc", &newpc);
    }
}

#ifndef __powerpc64__
static int ppc_regmap_adjusted;
#endif


/* Correct in either endianness.
   This instruction is "twge r2, r2", which GDB uses as a software
   breakpoint.  */
static const unsigned int ppc_breakpoint = 0x7d821008;
#define ppc_breakpoint_len 4

/* Implementation of target ops method "sw_breakpoint_from_kind".  */

const gdb_byte *
ppc_target::sw_breakpoint_from_kind (int kind, int *size)
{
  *size = ppc_breakpoint_len;
  return (const gdb_byte *) &ppc_breakpoint;
}

bool
ppc_target::low_breakpoint_at (CORE_ADDR where)
{
  unsigned int insn;

  read_memory (where, (unsigned char *) &insn, 4);
  if (insn == ppc_breakpoint)
    return true;
  /* If necessary, recognize more trap instructions here.  GDB only uses
     the one.  */

  return false;
}

/* Implement supports_z_point_type target-ops.
   Returns true if type Z_TYPE breakpoint is supported.

   Handling software breakpoint at server side, so tracepoints
   and breakpoints can be inserted at the same location.  */

bool
ppc_target::supports_z_point_type (char z_type)
{
  switch (z_type)
    {
    case Z_PACKET_SW_BP:
      return true;
    case Z_PACKET_HW_BP:
    case Z_PACKET_WRITE_WP:
    case Z_PACKET_ACCESS_WP:
    default:
      return false;
    }
}

/* Implement the low_insert_point linux target op.
   Returns 0 on success, -1 on failure and 1 on unsupported.  */

int
ppc_target::low_insert_point (raw_bkpt_type type, CORE_ADDR addr,
			      int size, raw_breakpoint *bp)
{
  switch (type)
    {
    case raw_bkpt_type_sw:
      return insert_memory_breakpoint (bp);

    case raw_bkpt_type_hw:
    case raw_bkpt_type_write_wp:
    case raw_bkpt_type_access_wp:
    default:
      /* Unsupported.  */
      return 1;
    }
}

/* Implement the low_remove_point linux target op.
   Returns 0 on success, -1 on failure and 1 on unsupported.  */

int
ppc_target::low_remove_point (raw_bkpt_type type, CORE_ADDR addr,
			      int size, raw_breakpoint *bp)
{
  switch (type)
    {
    case raw_bkpt_type_sw:
      return remove_memory_breakpoint (bp);

    case raw_bkpt_type_hw:
    case raw_bkpt_type_write_wp:
    case raw_bkpt_type_access_wp:
    default:
      /* Unsupported.  */
      return 1;
    }
}

/* Provide only a fill function for the general register set.  ps_lgetregs
   will use this for NPTL support.  */

static void ppc_fill_gregset (struct regcache *regcache, void *buf)
{
  int i;

  ppc_target *my_ppc_target = (ppc_target *) the_linux_target;

  for (i = 0; i < 32; i++)
    my_ppc_target->low_collect_ptrace_register (regcache, i,
						(char *) buf + ppc_regmap[i]);

  for (i = 64; i < 70; i++)
    my_ppc_target->low_collect_ptrace_register (regcache, i,
						(char *) buf + ppc_regmap[i]);

  for (i = 71; i < 73; i++)
    my_ppc_target->low_collect_ptrace_register (regcache, i,
						(char *) buf + ppc_regmap[i]);
}

/* Program Priority Register regset fill function.  */

static void
ppc_fill_pprregset (struct regcache *regcache, void *buf)
{
  char *ppr = (char *) buf;

  collect_register_by_name (regcache, "ppr", ppr);
}

/* Program Priority Register regset store function.  */

static void
ppc_store_pprregset (struct regcache *regcache, const void *buf)
{
  const char *ppr = (const char *) buf;

  supply_register_by_name (regcache, "ppr", ppr);
}

/* Data Stream Control Register regset fill function.  */

static void
ppc_fill_dscrregset (struct regcache *regcache, void *buf)
{
  char *dscr = (char *) buf;

  collect_register_by_name (regcache, "dscr", dscr);
}

/* Data Stream Control Register regset store function.  */

static void
ppc_store_dscrregset (struct regcache *regcache, const void *buf)
{
  const char *dscr = (const char *) buf;

  supply_register_by_name (regcache, "dscr", dscr);
}

/* Target Address Register regset fill function.  */

static void
ppc_fill_tarregset (struct regcache *regcache, void *buf)
{
  char *tar = (char *) buf;

  collect_register_by_name (regcache, "tar", tar);
}

/* Target Address Register regset store function.  */

static void
ppc_store_tarregset (struct regcache *regcache, const void *buf)
{
  const char *tar = (const char *) buf;

  supply_register_by_name (regcache, "tar", tar);
}

/* Event-Based Branching regset store function.  Unless the inferior
   has a perf event open, ptrace can return in error when reading and
   writing to the regset, with ENODATA.  For reading, the registers
   will correctly show as unavailable.  For writing, gdbserver
   currently only caches any register writes from P and G packets and
   the stub always tries to write all the regsets when resuming the
   inferior, which would result in frequent warnings.  For this
   reason, we don't define a fill function.  This also means that the
   client-side regcache will be dirty if the user tries to write to
   the EBB registers.  G packets that the client sends to write to
   unrelated registers will also include data for EBB registers, even
   if they are unavailable.  */

static void
ppc_store_ebbregset (struct regcache *regcache, const void *buf)
{
  const char *regset = (const char *) buf;

  /* The order in the kernel regset is: EBBRR, EBBHR, BESCR.  In the
     .dat file is BESCR, EBBHR, EBBRR.  */
  supply_register_by_name (regcache, "ebbrr", &regset[0]);
  supply_register_by_name (regcache, "ebbhr", &regset[8]);
  supply_register_by_name (regcache, "bescr", &regset[16]);
}

/* Performance Monitoring Unit regset fill function.  */

static void
ppc_fill_pmuregset (struct regcache *regcache, void *buf)
{
  char *regset = (char *) buf;

  /* The order in the kernel regset is SIAR, SDAR, SIER, MMCR2, MMCR0.
     In the .dat file is MMCR0, MMCR2, SIAR, SDAR, SIER.  */
  collect_register_by_name (regcache, "siar", &regset[0]);
  collect_register_by_name (regcache, "sdar", &regset[8]);
  collect_register_by_name (regcache, "sier", &regset[16]);
  collect_register_by_name (regcache, "mmcr2", &regset[24]);
  collect_register_by_name (regcache, "mmcr0", &regset[32]);
}

/* Performance Monitoring Unit regset store function.  */

static void
ppc_store_pmuregset (struct regcache *regcache, const void *buf)
{
  const char *regset = (const char *) buf;

  supply_register_by_name (regcache, "siar", &regset[0]);
  supply_register_by_name (regcache, "sdar", &regset[8]);
  supply_register_by_name (regcache, "sier", &regset[16]);
  supply_register_by_name (regcache, "mmcr2", &regset[24]);
  supply_register_by_name (regcache, "mmcr0", &regset[32]);
}

/* Hardware Transactional Memory special-purpose register regset fill
   function.  */

static void
ppc_fill_tm_sprregset (struct regcache *regcache, void *buf)
{
  int i, base;
  char *regset = (char *) buf;

  base = find_regno (regcache->tdesc, "tfhar");
  for (i = 0; i < 3; i++)
    collect_register (regcache, base + i, &regset[i * 8]);
}

/* Hardware Transactional Memory special-purpose register regset store
   function.  */

static void
ppc_store_tm_sprregset (struct regcache *regcache, const void *buf)
{
  int i, base;
  const char *regset = (const char *) buf;

  base = find_regno (regcache->tdesc, "tfhar");
  for (i = 0; i < 3; i++)
    supply_register (regcache, base + i, &regset[i * 8]);
}

/* For the same reasons as the EBB regset, none of the HTM
   checkpointed regsets have a fill function.  These registers are
   only available if the inferior is in a transaction.  */

/* Hardware Transactional Memory checkpointed general-purpose regset
   store function.  */

static void
ppc_store_tm_cgprregset (struct regcache *regcache, const void *buf)
{
  int i, base, size, endian_offset;
  const char *regset = (const char *) buf;

  base = find_regno (regcache->tdesc, "cr0");
  size = register_size (regcache->tdesc, base);

  gdb_assert (size == 4 || size == 8);

  for (i = 0; i < 32; i++)
    supply_register (regcache, base + i, &regset[i * size]);

  endian_offset = 0;

  if ((size == 8) && (__BYTE_ORDER == __BIG_ENDIAN))
    endian_offset = 4;

  supply_register_by_name (regcache, "ccr",
			   &regset[PT_CCR * size + endian_offset]);

  supply_register_by_name (regcache, "cxer",
			   &regset[PT_XER * size + endian_offset]);

  supply_register_by_name (regcache, "clr", &regset[PT_LNK * size]);
  supply_register_by_name (regcache, "cctr", &regset[PT_CTR * size]);
}

/* Hardware Transactional Memory checkpointed floating-point regset
   store function.  */

static void
ppc_store_tm_cfprregset (struct regcache *regcache, const void *buf)
{
  int i, base;
  const char *regset = (const char *) buf;

  base = find_regno (regcache->tdesc, "cf0");

  for (i = 0; i < 32; i++)
    supply_register (regcache, base + i, &regset[i * 8]);

  supply_register_by_name (regcache, "cfpscr", &regset[32 * 8]);
}

/* Hardware Transactional Memory checkpointed vector regset store
   function.  */

static void
ppc_store_tm_cvrregset (struct regcache *regcache, const void *buf)
{
  int i, base;
  const char *regset = (const char *) buf;
  int vscr_offset = 0;

  base = find_regno (regcache->tdesc, "cvr0");

  for (i = 0; i < 32; i++)
    supply_register (regcache, base + i, &regset[i * 16]);

  if (__BYTE_ORDER == __BIG_ENDIAN)
    vscr_offset = 12;

  supply_register_by_name (regcache, "cvscr",
			   &regset[32 * 16 + vscr_offset]);

  supply_register_by_name (regcache, "cvrsave", &regset[33 * 16]);
}

/* Hardware Transactional Memory checkpointed vector-scalar regset
   store function.  */

static void
ppc_store_tm_cvsxregset (struct regcache *regcache, const void *buf)
{
  int i, base;
  const char *regset = (const char *) buf;

  base = find_regno (regcache->tdesc, "cvs0h");
  for (i = 0; i < 32; i++)
    supply_register (regcache, base + i, &regset[i * 8]);
}

/* Hardware Transactional Memory checkpointed Program Priority
   Register regset store function.  */

static void
ppc_store_tm_cpprregset (struct regcache *regcache, const void *buf)
{
  const char *cppr = (const char *) buf;

  supply_register_by_name (regcache, "cppr", cppr);
}

/* Hardware Transactional Memory checkpointed Data Stream Control
   Register regset store function.  */

static void
ppc_store_tm_cdscrregset (struct regcache *regcache, const void *buf)
{
  const char *cdscr = (const char *) buf;

  supply_register_by_name (regcache, "cdscr", cdscr);
}

/* Hardware Transactional Memory checkpointed Target Address Register
   regset store function.  */

static void
ppc_store_tm_ctarregset (struct regcache *regcache, const void *buf)
{
  const char *ctar = (const char *) buf;

  supply_register_by_name (regcache, "ctar", ctar);
}

static void
ppc_fill_vsxregset (struct regcache *regcache, void *buf)
{
  int i, base;
  char *regset = (char *) buf;

  base = find_regno (regcache->tdesc, "vs0h");
  for (i = 0; i < 32; i++)
    collect_register (regcache, base + i, &regset[i * 8]);
}

static void
ppc_store_vsxregset (struct regcache *regcache, const void *buf)
{
  int i, base;
  const char *regset = (const char *) buf;

  base = find_regno (regcache->tdesc, "vs0h");
  for (i = 0; i < 32; i++)
    supply_register (regcache, base + i, &regset[i * 8]);
}

static void
ppc_fill_vrregset (struct regcache *regcache, void *buf)
{
  int i, base;
  char *regset = (char *) buf;
  int vscr_offset = 0;

  base = find_regno (regcache->tdesc, "vr0");
  for (i = 0; i < 32; i++)
    collect_register (regcache, base + i, &regset[i * 16]);

  if (__BYTE_ORDER == __BIG_ENDIAN)
    vscr_offset = 12;

  collect_register_by_name (regcache, "vscr",
			    &regset[32 * 16 + vscr_offset]);

  collect_register_by_name (regcache, "vrsave", &regset[33 * 16]);
}

static void
ppc_store_vrregset (struct regcache *regcache, const void *buf)
{
  int i, base;
  const char *regset = (const char *) buf;
  int vscr_offset = 0;

  base = find_regno (regcache->tdesc, "vr0");
  for (i = 0; i < 32; i++)
    supply_register (regcache, base + i, &regset[i * 16]);

  if (__BYTE_ORDER == __BIG_ENDIAN)
    vscr_offset = 12;

  supply_register_by_name (regcache, "vscr",
			   &regset[32 * 16 + vscr_offset]);
  supply_register_by_name (regcache, "vrsave", &regset[33 * 16]);
}

struct gdb_evrregset_t
{
  unsigned long evr[32];
  unsigned long long acc;
  unsigned long spefscr;
};

static void
ppc_fill_evrregset (struct regcache *regcache, void *buf)
{
  int i, ev0;
  struct gdb_evrregset_t *regset = (struct gdb_evrregset_t *) buf;

  ev0 = find_regno (regcache->tdesc, "ev0h");
  for (i = 0; i < 32; i++)
    collect_register (regcache, ev0 + i, &regset->evr[i]);

  collect_register_by_name (regcache, "acc", &regset->acc);
  collect_register_by_name (regcache, "spefscr", &regset->spefscr);
}

static void
ppc_store_evrregset (struct regcache *regcache, const void *buf)
{
  int i, ev0;
  const struct gdb_evrregset_t *regset = (const struct gdb_evrregset_t *) buf;

  ev0 = find_regno (regcache->tdesc, "ev0h");
  for (i = 0; i < 32; i++)
    supply_register (regcache, ev0 + i, &regset->evr[i]);

  supply_register_by_name (regcache, "acc", &regset->acc);
  supply_register_by_name (regcache, "spefscr", &regset->spefscr);
}

static struct regset_info ppc_regsets[] = {
  /* List the extra register sets before GENERAL_REGS.  That way we will
     fetch them every time, but still fall back to PTRACE_PEEKUSER for the
     general registers.  Some kernels support these, but not the newer
     PPC_PTRACE_GETREGS.  */
  { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_TM_CTAR, 0, EXTENDED_REGS,
    NULL, ppc_store_tm_ctarregset },
  { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_TM_CDSCR, 0, EXTENDED_REGS,
    NULL, ppc_store_tm_cdscrregset },
  { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_TM_CPPR, 0, EXTENDED_REGS,
    NULL, ppc_store_tm_cpprregset },
  { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_TM_CVSX, 0, EXTENDED_REGS,
    NULL, ppc_store_tm_cvsxregset },
  { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_TM_CVMX, 0, EXTENDED_REGS,
    NULL, ppc_store_tm_cvrregset },
  { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_TM_CFPR, 0, EXTENDED_REGS,
    NULL, ppc_store_tm_cfprregset },
  { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_TM_CGPR, 0, EXTENDED_REGS,
    NULL, ppc_store_tm_cgprregset },
  { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_TM_SPR, 0, EXTENDED_REGS,
    ppc_fill_tm_sprregset, ppc_store_tm_sprregset },
  { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_EBB, 0, EXTENDED_REGS,
    NULL, ppc_store_ebbregset },
  { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_PMU, 0, EXTENDED_REGS,
    ppc_fill_pmuregset, ppc_store_pmuregset },
  { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_TAR, 0, EXTENDED_REGS,
    ppc_fill_tarregset, ppc_store_tarregset },
  { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_PPR, 0, EXTENDED_REGS,
    ppc_fill_pprregset, ppc_store_pprregset },
  { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PPC_DSCR, 0, EXTENDED_REGS,
    ppc_fill_dscrregset, ppc_store_dscrregset },
  { PTRACE_GETVSXREGS, PTRACE_SETVSXREGS, 0, 0, EXTENDED_REGS,
  ppc_fill_vsxregset, ppc_store_vsxregset },
  { PTRACE_GETVRREGS, PTRACE_SETVRREGS, 0, 0, EXTENDED_REGS,
    ppc_fill_vrregset, ppc_store_vrregset },
  { PTRACE_GETEVRREGS, PTRACE_SETEVRREGS, 0, 0, EXTENDED_REGS,
    ppc_fill_evrregset, ppc_store_evrregset },
  { 0, 0, 0, 0, GENERAL_REGS, ppc_fill_gregset, NULL },
  NULL_REGSET
};

static struct usrregs_info ppc_usrregs_info =
  {
    ppc_num_regs,
    ppc_regmap,
  };

static struct regsets_info ppc_regsets_info =
  {
    ppc_regsets, /* regsets */
    0, /* num_regsets */
    NULL, /* disabled_regsets */
  };

static struct regs_info myregs_info =
  {
    NULL, /* regset_bitmap */
    &ppc_usrregs_info,
    &ppc_regsets_info
  };

const regs_info *
ppc_target::get_regs_info ()
{
  return &myregs_info;
}

void
ppc_target::low_arch_setup ()
{
  const struct target_desc *tdesc;
  struct regset_info *regset;
  struct ppc_linux_features features = ppc_linux_no_features;

  int tid = lwpid_of (current_thread);

  features.wordsize = ppc_linux_target_wordsize (tid);

  if (features.wordsize == 4)
      tdesc = tdesc_powerpc_32l;
  else
      tdesc = tdesc_powerpc_64l;

  current_process ()->tdesc = tdesc;

  /* The value of current_process ()->tdesc needs to be set for this
     call.  */
  ppc_hwcap = linux_get_hwcap (current_thread->id.pid (), features.wordsize);
  ppc_hwcap2 = linux_get_hwcap2 (current_thread->id.pid (), features.wordsize);

  features.isa205 = ppc_linux_has_isa205 (ppc_hwcap);

  if (ppc_hwcap & PPC_FEATURE_HAS_VSX)
    features.vsx = true;

  if (ppc_hwcap & PPC_FEATURE_HAS_ALTIVEC)
    features.altivec = true;

  if ((ppc_hwcap2 & PPC_FEATURE2_DSCR)
      && ppc_check_regset (tid, NT_PPC_DSCR, PPC_LINUX_SIZEOF_DSCRREGSET)
      && ppc_check_regset (tid, NT_PPC_PPR, PPC_LINUX_SIZEOF_PPRREGSET))
    {
      features.ppr_dscr = true;
      if ((ppc_hwcap2 & PPC_FEATURE2_ARCH_2_07)
	  && (ppc_hwcap2 & PPC_FEATURE2_TAR)
	  && (ppc_hwcap2 & PPC_FEATURE2_EBB)
	  && ppc_check_regset (tid, NT_PPC_TAR,
			       PPC_LINUX_SIZEOF_TARREGSET)
	  && ppc_check_regset (tid, NT_PPC_EBB,
			       PPC_LINUX_SIZEOF_EBBREGSET)
	  && ppc_check_regset (tid, NT_PPC_PMU,
			       PPC_LINUX_SIZEOF_PMUREGSET))
	{
	  features.isa207 = true;
	  if ((ppc_hwcap2 & PPC_FEATURE2_HTM)
	      && ppc_check_regset (tid, NT_PPC_TM_SPR,
				   PPC_LINUX_SIZEOF_TM_SPRREGSET))
	    features.htm = true;
	}
    }

  tdesc = ppc_linux_match_description (features);

  /* On 32-bit machines, check for SPE registers.
     Set the low target's regmap field as appropriately.  */
#ifndef __powerpc64__
  if (ppc_hwcap & PPC_FEATURE_HAS_SPE)
    tdesc = tdesc_powerpc_e500l;

  if (!ppc_regmap_adjusted)
    {
      if (ppc_hwcap & PPC_FEATURE_HAS_SPE)
	ppc_usrregs_info.regmap = ppc_regmap_e500;

      /* If the FPSCR is 64-bit wide, we need to fetch the whole
	 64-bit slot and not just its second word.  The PT_FPSCR
	 supplied in a 32-bit GDB compilation doesn't reflect
	 this.  */
      if (register_size (tdesc, 70) == 8)
	ppc_regmap[70] = (48 + 2*32) * sizeof (long);

      ppc_regmap_adjusted = 1;
   }
#endif

  current_process ()->tdesc = tdesc;

  for (regset = ppc_regsets; regset->size >= 0; regset++)
    switch (regset->get_request)
      {
      case PTRACE_GETVRREGS:
	regset->size = features.altivec ? PPC_LINUX_SIZEOF_VRREGSET : 0;
	break;
      case PTRACE_GETVSXREGS:
	regset->size = features.vsx ? PPC_LINUX_SIZEOF_VSXREGSET : 0;
	break;
      case PTRACE_GETEVRREGS:
	if (ppc_hwcap & PPC_FEATURE_HAS_SPE)
	  regset->size = 32 * 4 + 8 + 4;
	else
	  regset->size = 0;
	break;
      case PTRACE_GETREGSET:
	switch (regset->nt_type)
	  {
	  case NT_PPC_PPR:
	    regset->size = (features.ppr_dscr ?
			    PPC_LINUX_SIZEOF_PPRREGSET : 0);
	    break;
	  case NT_PPC_DSCR:
	    regset->size = (features.ppr_dscr ?
			    PPC_LINUX_SIZEOF_DSCRREGSET : 0);
	    break;
	  case NT_PPC_TAR:
	    regset->size = (features.isa207 ?
			    PPC_LINUX_SIZEOF_TARREGSET : 0);
	    break;
	  case NT_PPC_EBB:
	    regset->size = (features.isa207 ?
			    PPC_LINUX_SIZEOF_EBBREGSET : 0);
	    break;
	  case NT_PPC_PMU:
	    regset->size = (features.isa207 ?
			    PPC_LINUX_SIZEOF_PMUREGSET : 0);
	    break;
	  case NT_PPC_TM_SPR:
	    regset->size = (features.htm ?
			    PPC_LINUX_SIZEOF_TM_SPRREGSET : 0);
	    break;
	  case NT_PPC_TM_CGPR:
	    if (features.wordsize == 4)
	      regset->size = (features.htm ?
			      PPC32_LINUX_SIZEOF_CGPRREGSET : 0);
	    else
	      regset->size = (features.htm ?
			      PPC64_LINUX_SIZEOF_CGPRREGSET : 0);
	    break;
	  case NT_PPC_TM_CFPR:
	    regset->size = (features.htm ?
			    PPC_LINUX_SIZEOF_CFPRREGSET : 0);
	    break;
	  case NT_PPC_TM_CVMX:
	    regset->size = (features.htm ?
			    PPC_LINUX_SIZEOF_CVMXREGSET : 0);
	    break;
	  case NT_PPC_TM_CVSX:
	    regset->size = (features.htm ?
			    PPC_LINUX_SIZEOF_CVSXREGSET : 0);
	    break;
	  case NT_PPC_TM_CPPR:
	    regset->size = (features.htm ?
			    PPC_LINUX_SIZEOF_CPPRREGSET : 0);
	    break;
	  case NT_PPC_TM_CDSCR:
	    regset->size = (features.htm ?
			    PPC_LINUX_SIZEOF_CDSCRREGSET : 0);
	    break;
	  case NT_PPC_TM_CTAR:
	    regset->size = (features.htm ?
			    PPC_LINUX_SIZEOF_CTARREGSET : 0);
	    break;
	  default:
	    break;
	  }
	break;
      default:
	break;
      }
}

/* Implementation of target ops method "supports_tracepoints".  */

bool
ppc_target::supports_tracepoints ()
{
  return true;
}

/* Get the thread area address.  This is used to recognize which
   thread is which when tracing with the in-process agent library.  We
   don't read anything from the address, and treat it as opaque; it's
   the address itself that we assume is unique per-thread.  */

int
ppc_target::low_get_thread_area (int lwpid, CORE_ADDR *addr)
{
  struct lwp_info *lwp = find_lwp_pid (ptid_t (lwpid));
  struct thread_info *thr = get_lwp_thread (lwp);
  struct regcache *regcache = get_thread_regcache (thr, 1);
  ULONGEST tp = 0;

#ifdef __powerpc64__
  if (register_size (regcache->tdesc, 0) == 8)
    collect_register_by_name (regcache, "r13", &tp);
  else
#endif
    collect_register_by_name (regcache, "r2", &tp);

  *addr = tp;

  return 0;
}

#ifdef __powerpc64__

/* Older glibc doesn't provide this.  */

#ifndef EF_PPC64_ABI
#define EF_PPC64_ABI 3
#endif

/* Returns 1 if inferior is using ELFv2 ABI.  Undefined for 32-bit
   inferiors.  */

static int
is_elfv2_inferior (void)
{
  /* To be used as fallback if we're unable to determine the right result -
     assume inferior uses the same ABI as gdbserver.  */
#if _CALL_ELF == 2
  const int def_res = 1;
#else
  const int def_res = 0;
#endif
  CORE_ADDR phdr;
  Elf64_Ehdr ehdr;

  const struct target_desc *tdesc = current_process ()->tdesc;
  int wordsize = register_size (tdesc, 0);

  if (!linux_get_auxv (current_thread->id.pid (), wordsize, AT_PHDR, &phdr))
    return def_res;

  /* Assume ELF header is at the beginning of the page where program headers
     are located.  If it doesn't look like one, bail.  */

  read_inferior_memory (phdr & ~0xfff, (unsigned char *) &ehdr, sizeof ehdr);
  if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG))
    return def_res;

  return (ehdr.e_flags & EF_PPC64_ABI) == 2;
}

#endif

/* Generate a ds-form instruction in BUF and return the number of bytes written

   0      6     11   16          30 32
   | OPCD | RST | RA |     DS    |XO|  */

__attribute__((unused)) /* Maybe unused due to conditional compilation.  */
static int
gen_ds_form (uint32_t *buf, int opcd, int rst, int ra, int ds, int xo)
{
  uint32_t insn;

  gdb_assert ((opcd & ~0x3f) == 0);
  gdb_assert ((rst & ~0x1f) == 0);
  gdb_assert ((ra & ~0x1f) == 0);
  gdb_assert ((xo & ~0x3) == 0);

  insn = (rst << 21) | (ra << 16) | (ds & 0xfffc) | (xo & 0x3);
  *buf = (opcd << 26) | insn;
  return 1;
}

/* Followings are frequently used ds-form instructions.  */

#define GEN_STD(buf, rs, ra, offset)	gen_ds_form (buf, 62, rs, ra, offset, 0)
#define GEN_STDU(buf, rs, ra, offset)	gen_ds_form (buf, 62, rs, ra, offset, 1)
#define GEN_LD(buf, rt, ra, offset)	gen_ds_form (buf, 58, rt, ra, offset, 0)
#define GEN_LDU(buf, rt, ra, offset)	gen_ds_form (buf, 58, rt, ra, offset, 1)

/* Generate a d-form instruction in BUF.

   0      6     11   16             32
   | OPCD | RST | RA |       D      |  */

static int
gen_d_form (uint32_t *buf, int opcd, int rst, int ra, int si)
{
  uint32_t insn;

  gdb_assert ((opcd & ~0x3f) == 0);
  gdb_assert ((rst & ~0x1f) == 0);
  gdb_assert ((ra & ~0x1f) == 0);

  insn = (rst << 21) | (ra << 16) | (si & 0xffff);
  *buf = (opcd << 26) | insn;
  return 1;
}

/* Followings are frequently used d-form instructions.  */

#define GEN_ADDI(buf, rt, ra, si)	gen_d_form (buf, 14, rt, ra, si)
#define GEN_ADDIS(buf, rt, ra, si)	gen_d_form (buf, 15, rt, ra, si)
#define GEN_LI(buf, rt, si)		GEN_ADDI (buf, rt, 0, si)
#define GEN_LIS(buf, rt, si)		GEN_ADDIS (buf, rt, 0, si)
#define GEN_ORI(buf, rt, ra, si)	gen_d_form (buf, 24, rt, ra, si)
#define GEN_ORIS(buf, rt, ra, si)	gen_d_form (buf, 25, rt, ra, si)
#define GEN_LWZ(buf, rt, ra, si)	gen_d_form (buf, 32, rt, ra, si)
#define GEN_STW(buf, rt, ra, si)	gen_d_form (buf, 36, rt, ra, si)
#define GEN_STWU(buf, rt, ra, si)	gen_d_form (buf, 37, rt, ra, si)

/* Generate a xfx-form instruction in BUF and return the number of bytes
   written.

   0      6     11         21        31 32
   | OPCD | RST |    RI    |    XO   |/|  */

static int
gen_xfx_form (uint32_t *buf, int opcd, int rst, int ri, int xo)
{
  uint32_t insn;
  unsigned int n = ((ri & 0x1f) << 5) | ((ri >> 5) & 0x1f);

  gdb_assert ((opcd & ~0x3f) == 0);
  gdb_assert ((rst & ~0x1f) == 0);
  gdb_assert ((xo & ~0x3ff) == 0);

  insn = (rst << 21) | (n << 11) | (xo << 1);
  *buf = (opcd << 26) | insn;
  return 1;
}

/* Followings are frequently used xfx-form instructions.  */

#define GEN_MFSPR(buf, rt, spr)		gen_xfx_form (buf, 31, rt, spr, 339)
#define GEN_MTSPR(buf, rt, spr)		gen_xfx_form (buf, 31, rt, spr, 467)
#define GEN_MFCR(buf, rt)		gen_xfx_form (buf, 31, rt, 0, 19)
#define GEN_MTCR(buf, rt)		gen_xfx_form (buf, 31, rt, 0x3cf, 144)
#define GEN_SYNC(buf, L, E)             gen_xfx_form (buf, 31, L & 0x3, \
						      E & 0xf, 598)
#define GEN_LWSYNC(buf)			GEN_SYNC (buf, 1, 0)


/* Generate a x-form instruction in BUF and return the number of bytes written.

   0      6     11   16   21       31 32
   | OPCD | RST | RA | RB |   XO   |RC|  */

static int
gen_x_form (uint32_t *buf, int opcd, int rst, int ra, int rb, int xo, int rc)
{
  uint32_t insn;

  gdb_assert ((opcd & ~0x3f) == 0);
  gdb_assert ((rst & ~0x1f) == 0);
  gdb_assert ((ra & ~0x1f) == 0);
  gdb_assert ((rb & ~0x1f) == 0);
  gdb_assert ((xo & ~0x3ff) == 0);
  gdb_assert ((rc & ~1) == 0);

  insn = (rst << 21) | (ra << 16) | (rb << 11) | (xo << 1) | rc;
  *buf = (opcd << 26) | insn;
  return 1;
}

/* Followings are frequently used x-form instructions.  */

#define GEN_OR(buf, ra, rs, rb)		gen_x_form (buf, 31, rs, ra, rb, 444, 0)
#define GEN_MR(buf, ra, rs)		GEN_OR (buf, ra, rs, rs)
#define GEN_LWARX(buf, rt, ra, rb)	gen_x_form (buf, 31, rt, ra, rb, 20, 0)
#define GEN_STWCX(buf, rs, ra, rb)	gen_x_form (buf, 31, rs, ra, rb, 150, 1)
/* Assume bf = cr7.  */
#define GEN_CMPW(buf, ra, rb)		gen_x_form (buf, 31, 28, ra, rb, 0, 0)


/* Generate a md-form instruction in BUF and return the number of bytes written.

   0      6    11   16   21   27   30 31 32
   | OPCD | RS | RA | sh | mb | XO |sh|Rc|  */

static int
gen_md_form (uint32_t *buf, int opcd, int rs, int ra, int sh, int mb,
	     int xo, int rc)
{
  uint32_t insn;
  unsigned int n = ((mb & 0x1f) << 1) | ((mb >> 5) & 0x1);
  unsigned int sh0_4 = sh & 0x1f;
  unsigned int sh5 = (sh >> 5) & 1;

  gdb_assert ((opcd & ~0x3f) == 0);
  gdb_assert ((rs & ~0x1f) == 0);
  gdb_assert ((ra & ~0x1f) == 0);
  gdb_assert ((sh & ~0x3f) == 0);
  gdb_assert ((mb & ~0x3f) == 0);
  gdb_assert ((xo & ~0x7) == 0);
  gdb_assert ((rc & ~0x1) == 0);

  insn = (rs << 21) | (ra << 16) | (sh0_4 << 11) | (n << 5)
	 | (sh5 << 1) | (xo << 2) | (rc & 1);
  *buf = (opcd << 26) | insn;
  return 1;
}

/* The following are frequently used md-form instructions.  */

#define GEN_RLDICL(buf, ra, rs ,sh, mb) \
				gen_md_form (buf, 30, rs, ra, sh, mb, 0, 0)
#define GEN_RLDICR(buf, ra, rs ,sh, mb) \
				gen_md_form (buf, 30, rs, ra, sh, mb, 1, 0)

/* Generate a i-form instruction in BUF and return the number of bytes written.

   0      6                          30 31 32
   | OPCD |            LI            |AA|LK|  */

static int
gen_i_form (uint32_t *buf, int opcd, int li, int aa, int lk)
{
  uint32_t insn;

  gdb_assert ((opcd & ~0x3f) == 0);

  insn = (li & 0x3fffffc) | (aa & 1) | (lk & 1);
  *buf = (opcd << 26) | insn;
  return 1;
}

/* The following are frequently used i-form instructions.  */

#define GEN_B(buf, li)		gen_i_form (buf, 18, li, 0, 0)
#define GEN_BL(buf, li)		gen_i_form (buf, 18, li, 0, 1)

/* Generate a b-form instruction in BUF and return the number of bytes written.

   0      6    11   16               30 31 32
   | OPCD | BO | BI |      BD        |AA|LK|  */

static int
gen_b_form (uint32_t *buf, int opcd, int bo, int bi, int bd,
	    int aa, int lk)
{
  uint32_t insn;

  gdb_assert ((opcd & ~0x3f) == 0);
  gdb_assert ((bo & ~0x1f) == 0);
  gdb_assert ((bi & ~0x1f) == 0);

  insn = (bo << 21) | (bi << 16) | (bd & 0xfffc) | (aa & 1) | (lk & 1);
  *buf = (opcd << 26) | insn;
  return 1;
}

/* The following are frequently used b-form instructions.  */
/* Assume bi = cr7.  */
#define GEN_BNE(buf, bd)  gen_b_form (buf, 16, 0x4, (7 << 2) | 2, bd, 0 ,0)

/* GEN_LOAD and GEN_STORE generate 64- or 32-bit load/store for ppc64 or ppc32
   respectively.  They are primary used for save/restore GPRs in jump-pad,
   not used for bytecode compiling.  */

#ifdef __powerpc64__
#define GEN_LOAD(buf, rt, ra, si, is_64)	(is_64 ? \
						 GEN_LD (buf, rt, ra, si) : \
						 GEN_LWZ (buf, rt, ra, si))
#define GEN_STORE(buf, rt, ra, si, is_64)	(is_64 ? \
						 GEN_STD (buf, rt, ra, si) : \
						 GEN_STW (buf, rt, ra, si))
#else
#define GEN_LOAD(buf, rt, ra, si, is_64)	GEN_LWZ (buf, rt, ra, si)
#define GEN_STORE(buf, rt, ra, si, is_64)	GEN_STW (buf, rt, ra, si)
#endif

/* Generate a sequence of instructions to load IMM in the register REG.
   Write the instructions in BUF and return the number of bytes written.  */

static int
gen_limm (uint32_t *buf, int reg, uint64_t imm, int is_64)
{
  uint32_t *p = buf;

  if ((imm + 32768) < 65536)
    {
      /* li	reg, imm[15:0] */
      p += GEN_LI (p, reg, imm);
    }
  else if ((imm >> 32) == 0)
    {
      /* lis	reg, imm[31:16]
	 ori	reg, reg, imm[15:0]
	 rldicl reg, reg, 0, 32 */
      p += GEN_LIS (p, reg, (imm >> 16) & 0xffff);
      if ((imm & 0xffff) != 0)
	p += GEN_ORI (p, reg, reg, imm & 0xffff);
      /* Clear upper 32-bit if sign-bit is set.  */
      if (imm & (1u << 31) && is_64)
	p += GEN_RLDICL (p, reg, reg, 0, 32);
    }
  else
    {
      gdb_assert (is_64);
      /* lis    reg, <imm[63:48]>
	 ori    reg, reg, <imm[48:32]>
	 rldicr reg, reg, 32, 31
	 oris   reg, reg, <imm[31:16]>
	 ori    reg, reg, <imm[15:0]> */
      p += GEN_LIS (p, reg, ((imm >> 48) & 0xffff));
      if (((imm >> 32) & 0xffff) != 0)
	p += GEN_ORI (p, reg, reg, ((imm >> 32) & 0xffff));
      p += GEN_RLDICR (p, reg, reg, 32, 31);
      if (((imm >> 16) & 0xffff) != 0)
	p += GEN_ORIS (p, reg, reg, ((imm >> 16) & 0xffff));
      if ((imm & 0xffff) != 0)
	p += GEN_ORI (p, reg, reg, (imm & 0xffff));
    }

  return p - buf;
}

/* Generate a sequence for atomically exchange at location LOCK.
   This code sequence clobbers r6, r7, r8.  LOCK is the location for
   the atomic-xchg, OLD_VALUE is expected old value stored in the
   location, and R_NEW is a register for the new value.  */

static int
gen_atomic_xchg (uint32_t *buf, CORE_ADDR lock, int old_value, int r_new,
		 int is_64)
{
  const int r_lock = 6;
  const int r_old = 7;
  const int r_tmp = 8;
  uint32_t *p = buf;

  /*
  1: lwarx   TMP, 0, LOCK
     cmpwi   TMP, OLD
     bne     1b
     stwcx.  NEW, 0, LOCK
     bne     1b */

  p += gen_limm (p, r_lock, lock, is_64);
  p += gen_limm (p, r_old, old_value, is_64);

  p += GEN_LWARX (p, r_tmp, 0, r_lock);
  p += GEN_CMPW (p, r_tmp, r_old);
  p += GEN_BNE (p, -8);
  p += GEN_STWCX (p, r_new, 0, r_lock);
  p += GEN_BNE (p, -16);

  return p - buf;
}

/* Generate a sequence of instructions for calling a function
   at address of FN.  Return the number of bytes are written in BUF.  */

static int
gen_call (uint32_t *buf, CORE_ADDR fn, int is_64, int is_opd)
{
  uint32_t *p = buf;

  /* Must be called by r12 for caller to calculate TOC address. */
  p += gen_limm (p, 12, fn, is_64);
  if (is_opd)
    {
      p += GEN_LOAD (p, 11, 12, 16, is_64);
      p += GEN_LOAD (p, 2, 12, 8, is_64);
      p += GEN_LOAD (p, 12, 12, 0, is_64);
    }
  p += GEN_MTSPR (p, 12, 9);		/* mtctr  r12 */
  *p++ = 0x4e800421;			/* bctrl */

  return p - buf;
}

/* Copy the instruction from OLDLOC to *TO, and update *TO to *TO + size
   of instruction.  This function is used to adjust pc-relative instructions
   when copying.  */

static void
ppc_relocate_instruction (CORE_ADDR *to, CORE_ADDR oldloc)
{
  uint32_t insn, op6;
  long rel, newrel;

  read_inferior_memory (oldloc, (unsigned char *) &insn, 4);
  op6 = PPC_OP6 (insn);

  if (op6 == 18 && (insn & 2) == 0)
    {
      /* branch && AA = 0 */
      rel = PPC_LI (insn);
      newrel = (oldloc - *to) + rel;

      /* Out of range. Cannot relocate instruction.  */
      if (newrel >= (1 << 25) || newrel < -(1 << 25))
	return;

      insn = (insn & ~0x3fffffc) | (newrel & 0x3fffffc);
    }
  else if (op6 == 16 && (insn & 2) == 0)
    {
      /* conditional branch && AA = 0 */

      /* If the new relocation is too big for even a 26-bit unconditional
	 branch, there is nothing we can do.  Just abort.

	 Otherwise, if it can be fit in 16-bit conditional branch, just
	 copy the instruction and relocate the address.

	 If the it's  big for conditional-branch (16-bit), try to invert the
	 condition and jump with 26-bit branch.  For example,

	 beq  .Lgoto
	 INSN1

	 =>

	 bne  1f (+8)
	 b    .Lgoto
       1:INSN1

	 After this transform, we are actually jump from *TO+4 instead of *TO,
	 so check the relocation again because it will be 1-insn farther then
	 before if *TO is after OLDLOC.


	 For BDNZT (or so) is transformed from

	 bdnzt  eq, .Lgoto
	 INSN1

	 =>

	 bdz    1f (+12)
	 bf     eq, 1f (+8)
	 b      .Lgoto
       1:INSN1

	 See also "BO field encodings".  */

      rel = PPC_BD (insn);
      newrel = (oldloc - *to) + rel;

      if (newrel < (1 << 15) && newrel >= -(1 << 15))
	insn = (insn & ~0xfffc) | (newrel & 0xfffc);
      else if ((PPC_BO (insn) & 0x14) == 0x4 || (PPC_BO (insn) & 0x14) == 0x10)
	{
	  newrel -= 4;

	  /* Out of range. Cannot relocate instruction.  */
	  if (newrel >= (1 << 25) || newrel < -(1 << 25))
	    return;

	  if ((PPC_BO (insn) & 0x14) == 0x4)
	    insn ^= (1 << 24);
	  else if ((PPC_BO (insn) & 0x14) == 0x10)
	    insn ^= (1 << 22);

	  /* Jump over the unconditional branch.  */
	  insn = (insn & ~0xfffc) | 0x8;
	  target_write_memory (*to, (unsigned char *) &insn, 4);
	  *to += 4;

	  /* Build a unconditional branch and copy LK bit.  */
	  insn = (18 << 26) | (0x3fffffc & newrel) | (insn & 0x3);
	  target_write_memory (*to, (unsigned char *) &insn, 4);
	  *to += 4;

	  return;
	}
      else if ((PPC_BO (insn) & 0x14) == 0)
	{
	  uint32_t bdnz_insn = (16 << 26) | (0x10 << 21) | 12;
	  uint32_t bf_insn = (16 << 26) | (0x4 << 21) | 8;

	  newrel -= 8;

	  /* Out of range. Cannot relocate instruction.  */
	  if (newrel >= (1 << 25) || newrel < -(1 << 25))
	    return;

	  /* Copy BI field.  */
	  bf_insn |= (insn & 0x1f0000);

	  /* Invert condition.  */
	  bdnz_insn |= (insn ^ (1 << 22)) & (1 << 22);
	  bf_insn |= (insn ^ (1 << 24)) & (1 << 24);

	  target_write_memory (*to, (unsigned char *) &bdnz_insn, 4);
	  *to += 4;
	  target_write_memory (*to, (unsigned char *) &bf_insn, 4);
	  *to += 4;

	  /* Build a unconditional branch and copy LK bit.  */
	  insn = (18 << 26) | (0x3fffffc & newrel) | (insn & 0x3);
	  target_write_memory (*to, (unsigned char *) &insn, 4);
	  *to += 4;

	  return;
	}
      else /* (BO & 0x14) == 0x14, branch always.  */
	{
	  /* Out of range. Cannot relocate instruction.  */
	  if (newrel >= (1 << 25) || newrel < -(1 << 25))
	    return;

	  /* Build a unconditional branch and copy LK bit.  */
	  insn = (18 << 26) | (0x3fffffc & newrel) | (insn & 0x3);
	  target_write_memory (*to, (unsigned char *) &insn, 4);
	  *to += 4;

	  return;
	}
    }

  target_write_memory (*to, (unsigned char *) &insn, 4);
  *to += 4;
}

bool
ppc_target::supports_fast_tracepoints ()
{
  return true;
}

/* Implement install_fast_tracepoint_jump_pad of target_ops.
   See target.h for details.  */

int
ppc_target::install_fast_tracepoint_jump_pad (CORE_ADDR tpoint,
					      CORE_ADDR tpaddr,
					      CORE_ADDR collector,
					      CORE_ADDR lockaddr,
					      ULONGEST orig_size,
					      CORE_ADDR *jump_entry,
					      CORE_ADDR *trampoline,
					      ULONGEST *trampoline_size,
					      unsigned char *jjump_pad_insn,
					      ULONGEST *jjump_pad_insn_size,
					      CORE_ADDR *adjusted_insn_addr,
					      CORE_ADDR *adjusted_insn_addr_end,
					      char *err)
{
  uint32_t buf[256];
  uint32_t *p = buf;
  int j, offset;
  CORE_ADDR buildaddr = *jump_entry;
  const CORE_ADDR entryaddr = *jump_entry;
  int rsz, min_frame, frame_size, tp_reg;
#ifdef __powerpc64__
  struct regcache *regcache = get_thread_regcache (current_thread, 0);
  int is_64 = register_size (regcache->tdesc, 0) == 8;
  int is_opd = is_64 && !is_elfv2_inferior ();
#else
  int is_64 = 0, is_opd = 0;
#endif

#ifdef __powerpc64__
  if (is_64)
    {
      /* Minimum frame size is 32 bytes for ELFv2, and 112 bytes for ELFv1.  */
      rsz = 8;
      min_frame = 112;
      frame_size = (40 * rsz) + min_frame;
      tp_reg = 13;
    }
  else
    {
#endif
      rsz = 4;
      min_frame = 16;
      frame_size = (40 * rsz) + min_frame;
      tp_reg = 2;
#ifdef __powerpc64__
    }
#endif

  /* Stack frame layout for this jump pad,

     High	thread_area (r13/r2)    |
		tpoint			- collecting_t obj
		PC/<tpaddr>		| +36
		CTR			| +35
		LR			| +34
		XER			| +33
		CR			| +32
		R31			|
		R29			|
		...			|
		R1			| +1
		R0			- collected registers
		...			|
		...			|
     Low	Back-chain		-


     The code flow of this jump pad,

     1. Adjust SP
     2. Save GPR and SPR
     3. Prepare argument
     4. Call gdb_collector
     5. Restore GPR and SPR
     6. Restore SP
     7. Build a jump for back to the program
     8. Copy/relocate original instruction
     9. Build a jump for replacing original instruction.  */

  /* Adjust stack pointer.  */
  if (is_64)
    p += GEN_STDU (p, 1, 1, -frame_size);		/* stdu   r1,-frame_size(r1) */
  else
    p += GEN_STWU (p, 1, 1, -frame_size);		/* stwu   r1,-frame_size(r1) */

  /* Store GPRs.  Save R1 later, because it had just been modified, but
     we want the original value.  */
  for (j = 2; j < 32; j++)
    p += GEN_STORE (p, j, 1, min_frame + j * rsz, is_64);
  p += GEN_STORE (p, 0, 1, min_frame + 0 * rsz, is_64);
  /* Set r0 to the original value of r1 before adjusting stack frame,
     and then save it.  */
  p += GEN_ADDI (p, 0, 1, frame_size);
  p += GEN_STORE (p, 0, 1, min_frame + 1 * rsz, is_64);

  /* Save CR, XER, LR, and CTR.  */
  p += GEN_MFCR (p, 3);					/* mfcr   r3 */
  p += GEN_MFSPR (p, 4, 1);				/* mfxer  r4 */
  p += GEN_MFSPR (p, 5, 8);				/* mflr   r5 */
  p += GEN_MFSPR (p, 6, 9);				/* mfctr  r6 */
  p += GEN_STORE (p, 3, 1, min_frame + 32 * rsz, is_64);/* std    r3, 32(r1) */
  p += GEN_STORE (p, 4, 1, min_frame + 33 * rsz, is_64);/* std    r4, 33(r1) */
  p += GEN_STORE (p, 5, 1, min_frame + 34 * rsz, is_64);/* std    r5, 34(r1) */
  p += GEN_STORE (p, 6, 1, min_frame + 35 * rsz, is_64);/* std    r6, 35(r1) */

  /* Save PC<tpaddr>  */
  p += gen_limm (p, 3, tpaddr, is_64);
  p += GEN_STORE (p, 3, 1, min_frame + 36 * rsz, is_64);


  /* Setup arguments to collector.  */
  /* Set r4 to collected registers.  */
  p += GEN_ADDI (p, 4, 1, min_frame);
  /* Set r3 to TPOINT.  */
  p += gen_limm (p, 3, tpoint, is_64);

  /* Prepare collecting_t object for lock.  */
  p += GEN_STORE (p, 3, 1, min_frame + 37 * rsz, is_64);
  p += GEN_STORE (p, tp_reg, 1, min_frame + 38 * rsz, is_64);
  /* Set R5 to collecting object.  */
  p += GEN_ADDI (p, 5, 1, 37 * rsz);

  p += GEN_LWSYNC (p);
  p += gen_atomic_xchg (p, lockaddr, 0, 5, is_64);
  p += GEN_LWSYNC (p);

  /* Call to collector.  */
  p += gen_call (p, collector, is_64, is_opd);

  /* Simply write 0 to release the lock.  */
  p += gen_limm (p, 3, lockaddr, is_64);
  p += gen_limm (p, 4, 0, is_64);
  p += GEN_LWSYNC (p);
  p += GEN_STORE (p, 4, 3, 0, is_64);

  /* Restore stack and registers.  */
  p += GEN_LOAD (p, 3, 1, min_frame + 32 * rsz, is_64);	/* ld	r3, 32(r1) */
  p += GEN_LOAD (p, 4, 1, min_frame + 33 * rsz, is_64);	/* ld	r4, 33(r1) */
  p += GEN_LOAD (p, 5, 1, min_frame + 34 * rsz, is_64);	/* ld	r5, 34(r1) */
  p += GEN_LOAD (p, 6, 1, min_frame + 35 * rsz, is_64);	/* ld	r6, 35(r1) */
  p += GEN_MTCR (p, 3);					/* mtcr	  r3 */
  p += GEN_MTSPR (p, 4, 1);				/* mtxer  r4 */
  p += GEN_MTSPR (p, 5, 8);				/* mtlr   r5 */
  p += GEN_MTSPR (p, 6, 9);				/* mtctr  r6 */

  /* Restore GPRs.  */
  for (j = 2; j < 32; j++)
    p += GEN_LOAD (p, j, 1, min_frame + j * rsz, is_64);
  p += GEN_LOAD (p, 0, 1, min_frame + 0 * rsz, is_64);
  /* Restore SP.  */
  p += GEN_ADDI (p, 1, 1, frame_size);

  /* Flush instructions to inferior memory.  */
  target_write_memory (buildaddr, (unsigned char *) buf, (p - buf) * 4);

  /* Now, insert the original instruction to execute in the jump pad.  */
  *adjusted_insn_addr = buildaddr + (p - buf) * 4;
  *adjusted_insn_addr_end = *adjusted_insn_addr;
  ppc_relocate_instruction (adjusted_insn_addr_end, tpaddr);

  /* Verify the relocation size.  If should be 4 for normal copy,
     8 or 12 for some conditional branch.  */
  if ((*adjusted_insn_addr_end - *adjusted_insn_addr == 0)
      || (*adjusted_insn_addr_end - *adjusted_insn_addr > 12))
    {
      sprintf (err, "E.Unexpected instruction length = %d"
		    "when relocate instruction.",
		    (int) (*adjusted_insn_addr_end - *adjusted_insn_addr));
      return 1;
    }

  buildaddr = *adjusted_insn_addr_end;
  p = buf;
  /* Finally, write a jump back to the program.  */
  offset = (tpaddr + 4) - buildaddr;
  if (offset >= (1 << 25) || offset < -(1 << 25))
    {
      sprintf (err, "E.Jump back from jump pad too far from tracepoint "
		    "(offset 0x%x > 26-bit).", offset);
      return 1;
    }
  /* b <tpaddr+4> */
  p += GEN_B (p, offset);
  target_write_memory (buildaddr, (unsigned char *) buf, (p - buf) * 4);
  *jump_entry = buildaddr + (p - buf) * 4;

  /* The jump pad is now built.  Wire in a jump to our jump pad.  This
     is always done last (by our caller actually), so that we can
     install fast tracepoints with threads running.  This relies on
     the agent's atomic write support.  */
  offset = entryaddr - tpaddr;
  if (offset >= (1 << 25) || offset < -(1 << 25))
    {
      sprintf (err, "E.Jump back from jump pad too far from tracepoint "
		    "(offset 0x%x > 26-bit).", offset);
      return 1;
    }
  /* b <jentry> */
  GEN_B ((uint32_t *) jjump_pad_insn, offset);
  *jjump_pad_insn_size = 4;

  return 0;
}

/* Returns the minimum instruction length for installing a tracepoint.  */

int
ppc_target::get_min_fast_tracepoint_insn_len ()
{
  return 4;
}

/* Emits a given buffer into the target at current_insn_ptr.  Length
   is in units of 32-bit words.  */

static void
emit_insns (uint32_t *buf, int n)
{
  n = n * sizeof (uint32_t);
  target_write_memory (current_insn_ptr, (unsigned char *) buf, n);
  current_insn_ptr += n;
}

#define __EMIT_ASM(NAME, INSNS)					\
  do								\
    {								\
      extern uint32_t start_bcax_ ## NAME [];			\
      extern uint32_t end_bcax_ ## NAME [];			\
      emit_insns (start_bcax_ ## NAME,				\
		  end_bcax_ ## NAME - start_bcax_ ## NAME);	\
      __asm__ (".section .text.__ppcbcax\n\t"			\
	       "start_bcax_" #NAME ":\n\t"			\
	       INSNS "\n\t"					\
	       "end_bcax_" #NAME ":\n\t"			\
	       ".previous\n\t");				\
    } while (0)

#define _EMIT_ASM(NAME, INSNS)		__EMIT_ASM (NAME, INSNS)
#define EMIT_ASM(INSNS)			_EMIT_ASM (__LINE__, INSNS)

/*

  Bytecode execution stack frame - 32-bit

	|  LR save area           (SP + 4)
 SP' -> +- Back chain             (SP + 0)
	|  Save r31   for access saved arguments
	|  Save r30   for bytecode stack pointer
	|  Save r4    for incoming argument *value
	|  Save r3    for incoming argument regs
 r30 -> +- Bytecode execution stack
	|
	|  64-byte (8 doublewords) at initial.
	|  Expand stack as needed.
	|
	+-
	|  Some padding for minimum stack frame and 16-byte alignment.
	|  16 bytes.
 SP     +- Back-chain (SP')

  initial frame size
  = 16 + (4 * 4) + 64
  = 96

   r30 is the stack-pointer for bytecode machine.
       It should point to next-empty, so we can use LDU for pop.
   r3  is used for cache of the high part of TOP value.
       It was the first argument, pointer to regs.
   r4  is used for cache of the low part of TOP value.
       It was the second argument, pointer to the result.
       We should set *result = TOP after leaving this function.

 Note:
 * To restore stack at epilogue
   => sp = r31
 * To check stack is big enough for bytecode execution.
   => r30 - 8 > SP + 8
 * To return execution result.
   => 0(r4) = TOP

 */

/* Regardless of endian, register 3 is always high part, 4 is low part.
   These defines are used when the register pair is stored/loaded.
   Likewise, to simplify code, have a similar define for 5:6. */

#if __BYTE_ORDER == __LITTLE_ENDIAN
#define TOP_FIRST	"4"
#define TOP_SECOND	"3"
#define TMP_FIRST	"6"
#define TMP_SECOND	"5"
#else
#define TOP_FIRST	"3"
#define TOP_SECOND	"4"
#define TMP_FIRST	"5"
#define TMP_SECOND	"6"
#endif

/* Emit prologue in inferior memory.  See above comments.  */

static void
ppc_emit_prologue (void)
{
  EMIT_ASM (/* Save return address.  */
	    "mflr  0		\n"
	    "stw   0, 4(1)	\n"
	    /* Adjust SP.  96 is the initial frame size.  */
	    "stwu  1, -96(1)	\n"
	    /* Save r30 and incoming arguments.  */
	    "stw   31, 96-4(1)	\n"
	    "stw   30, 96-8(1)	\n"
	    "stw   4, 96-12(1)	\n"
	    "stw   3, 96-16(1)	\n"
	    /* Point r31 to original r1 for access arguments.  */
	    "addi  31, 1, 96	\n"
	    /* Set r30 to pointing stack-top.  */
	    "addi  30, 1, 64	\n"
	    /* Initial r3/TOP to 0.  */
	    "li    3, 0		\n"
	    "li    4, 0		\n");
}

/* Emit epilogue in inferior memory.  See above comments.  */

static void
ppc_emit_epilogue (void)
{
  EMIT_ASM (/* *result = TOP */
	    "lwz   5, -12(31)	\n"
	    "stw   " TOP_FIRST ", 0(5)	\n"
	    "stw   " TOP_SECOND ", 4(5)	\n"
	    /* Restore registers.  */
	    "lwz   31, -4(31)	\n"
	    "lwz   30, -8(31)	\n"
	    /* Restore SP.  */
	    "lwz   1, 0(1)      \n"
	    /* Restore LR.  */
	    "lwz   0, 4(1)	\n"
	    /* Return 0 for no-error.  */
	    "li    3, 0		\n"
	    "mtlr  0		\n"
	    "blr		\n");
}

/* TOP = stack[--sp] + TOP  */

static void
ppc_emit_add (void)
{
  EMIT_ASM ("lwzu  " TMP_FIRST ", 8(30)	\n"
	    "lwz   " TMP_SECOND ", 4(30)\n"
	    "addc  4, 6, 4	\n"
	    "adde  3, 5, 3	\n");
}

/* TOP = stack[--sp] - TOP  */

static void
ppc_emit_sub (void)
{
  EMIT_ASM ("lwzu " TMP_FIRST ", 8(30)	\n"
	    "lwz " TMP_SECOND ", 4(30)	\n"
	    "subfc  4, 4, 6	\n"
	    "subfe  3, 3, 5	\n");
}

/* TOP = stack[--sp] * TOP  */

static void
ppc_emit_mul (void)
{
  EMIT_ASM ("lwzu " TMP_FIRST ", 8(30)	\n"
	    "lwz " TMP_SECOND ", 4(30)	\n"
	    "mulhwu 7, 6, 4	\n"
	    "mullw  3, 6, 3	\n"
	    "mullw  5, 4, 5	\n"
	    "mullw  4, 6, 4	\n"
	    "add    3, 5, 3	\n"
	    "add    3, 7, 3	\n");
}

/* TOP = stack[--sp] << TOP  */

static void
ppc_emit_lsh (void)
{
  EMIT_ASM ("lwzu " TMP_FIRST ", 8(30)	\n"
	    "lwz " TMP_SECOND ", 4(30)	\n"
	    "subfic 3, 4, 32\n"		/* r3 = 32 - TOP */
	    "addi   7, 4, -32\n"	/* r7 = TOP - 32 */
	    "slw    5, 5, 4\n"		/* Shift high part left */
	    "slw    4, 6, 4\n"		/* Shift low part left */
	    "srw    3, 6, 3\n"		/* Shift low to high if shift < 32 */
	    "slw    7, 6, 7\n"		/* Shift low to high if shift >= 32 */
	    "or     3, 5, 3\n"
	    "or     3, 7, 3\n");	/* Assemble high part */
}

/* Top = stack[--sp] >> TOP
   (Arithmetic shift right)  */

static void
ppc_emit_rsh_signed (void)
{
  EMIT_ASM ("lwzu " TMP_FIRST ", 8(30)	\n"
	    "lwz " TMP_SECOND ", 4(30)	\n"
	    "addi   7, 4, -32\n"	/* r7 = TOP - 32 */
	    "sraw   3, 5, 4\n"		/* Shift high part right */
	    "cmpwi  7, 1\n"
	    "blt    0, 1f\n"		/* If shift <= 32, goto 1: */
	    "sraw   4, 5, 7\n"		/* Shift high to low */
	    "b      2f\n"
	    "1:\n"
	    "subfic 7, 4, 32\n"		/* r7 = 32 - TOP */
	    "srw    4, 6, 4\n"		/* Shift low part right */
	    "slw    5, 5, 7\n"		/* Shift high to low */
	    "or     4, 4, 5\n"		/* Assemble low part */
	    "2:\n");
}

/* Top = stack[--sp] >> TOP
   (Logical shift right)  */

static void
ppc_emit_rsh_unsigned (void)
{
  EMIT_ASM ("lwzu " TMP_FIRST ", 8(30)	\n"
	    "lwz " TMP_SECOND ", 4(30)	\n"
	    "subfic 3, 4, 32\n"		/* r3 = 32 - TOP */
	    "addi   7, 4, -32\n"	/* r7 = TOP - 32 */
	    "srw    6, 6, 4\n"		/* Shift low part right */
	    "slw    3, 5, 3\n"		/* Shift high to low if shift < 32 */
	    "srw    7, 5, 7\n"		/* Shift high to low if shift >= 32 */
	    "or     6, 6, 3\n"
	    "srw    3, 5, 4\n"		/* Shift high part right */
	    "or     4, 6, 7\n");	/* Assemble low part */
}

/* Emit code for signed-extension specified by ARG.  */

static void
ppc_emit_ext (int arg)
{
  switch (arg)
    {
    case 8:
      EMIT_ASM ("extsb  4, 4\n"
		"srawi 3, 4, 31");
      break;
    case 16:
      EMIT_ASM ("extsh  4, 4\n"
		"srawi 3, 4, 31");
      break;
    case 32:
      EMIT_ASM ("srawi 3, 4, 31");
      break;
    default:
      emit_error = 1;
    }
}

/* Emit code for zero-extension specified by ARG.  */

static void
ppc_emit_zero_ext (int arg)
{
  switch (arg)
    {
    case 8:
      EMIT_ASM ("clrlwi 4,4,24\n"
		"li 3, 0\n");
      break;
    case 16:
      EMIT_ASM ("clrlwi 4,4,16\n"
		"li 3, 0\n");
      break;
    case 32:
      EMIT_ASM ("li 3, 0");
      break;
    default:
      emit_error = 1;
    }
}

/* TOP = !TOP
   i.e., TOP = (TOP == 0) ? 1 : 0;  */

static void
ppc_emit_log_not (void)
{
  EMIT_ASM ("or      4, 3, 4	\n"
	    "cntlzw  4, 4	\n"
	    "srwi    4, 4, 5	\n"
	    "li      3, 0	\n");
}

/* TOP = stack[--sp] & TOP  */

static void
ppc_emit_bit_and (void)
{
  EMIT_ASM ("lwzu " TMP_FIRST ", 8(30)	\n"
	    "lwz " TMP_SECOND ", 4(30)	\n"
	    "and  4, 6, 4	\n"
	    "and  3, 5, 3	\n");
}

/* TOP = stack[--sp] | TOP  */

static void
ppc_emit_bit_or (void)
{
  EMIT_ASM ("lwzu " TMP_FIRST ", 8(30)	\n"
	    "lwz " TMP_SECOND ", 4(30)	\n"
	    "or  4, 6, 4	\n"
	    "or  3, 5, 3	\n");
}

/* TOP = stack[--sp] ^ TOP  */

static void
ppc_emit_bit_xor (void)
{
  EMIT_ASM ("lwzu " TMP_FIRST ", 8(30)	\n"
	    "lwz " TMP_SECOND ", 4(30)	\n"
	    "xor  4, 6, 4	\n"
	    "xor  3, 5, 3	\n");
}

/* TOP = ~TOP
   i.e., TOP = ~(TOP | TOP)  */

static void
ppc_emit_bit_not (void)
{
  EMIT_ASM ("nor  3, 3, 3	\n"
	    "nor  4, 4, 4	\n");
}

/* TOP = stack[--sp] == TOP  */

static void
ppc_emit_equal (void)
{
  EMIT_ASM ("lwzu " TMP_FIRST ", 8(30)	\n"
	    "lwz " TMP_SECOND ", 4(30)	\n"
	    "xor     4, 6, 4	\n"
	    "xor     3, 5, 3	\n"
	    "or      4, 3, 4	\n"
	    "cntlzw  4, 4	\n"
	    "srwi    4, 4, 5	\n"
	    "li      3, 0	\n");
}

/* TOP = stack[--sp] < TOP
   (Signed comparison)  */

static void
ppc_emit_less_signed (void)
{
  EMIT_ASM ("lwzu " TMP_FIRST ", 8(30)	\n"
	    "lwz " TMP_SECOND ", 4(30)	\n"
	    "cmplw   6, 6, 4		\n"
	    "cmpw    7, 5, 3		\n"
	    /* CR6 bit 0 = low less and high equal */
	    "crand   6*4+0, 6*4+0, 7*4+2\n"
	    /* CR7 bit 0 = (low less and high equal) or high less */
	    "cror    7*4+0, 7*4+0, 6*4+0\n"
	    "mfcr    4			\n"
	    "rlwinm  4, 4, 29, 31, 31	\n"
	    "li      3, 0		\n");
}

/* TOP = stack[--sp] < TOP
   (Unsigned comparison)  */

static void
ppc_emit_less_unsigned (void)
{
  EMIT_ASM ("lwzu " TMP_FIRST ", 8(30)	\n"
	    "lwz " TMP_SECOND ", 4(30)	\n"
	    "cmplw   6, 6, 4		\n"
	    "cmplw   7, 5, 3		\n"
	    /* CR6 bit 0 = low less and high equal */
	    "crand   6*4+0, 6*4+0, 7*4+2\n"
	    /* CR7 bit 0 = (low less and high equal) or high less */
	    "cror    7*4+0, 7*4+0, 6*4+0\n"
	    "mfcr    4			\n"
	    "rlwinm  4, 4, 29, 31, 31	\n"
	    "li      3, 0		\n");
}

/* Access the memory address in TOP in size of SIZE.
   Zero-extend the read value.  */

static void
ppc_emit_ref (int size)
{
  switch (size)
    {
    case 1:
      EMIT_ASM ("lbz   4, 0(4)\n"
		"li    3, 0");
      break;
    case 2:
      EMIT_ASM ("lhz   4, 0(4)\n"
		"li    3, 0");
      break;
    case 4:
      EMIT_ASM ("lwz   4, 0(4)\n"
		"li    3, 0");
      break;
    case 8:
      if (__BYTE_ORDER == __LITTLE_ENDIAN)
	EMIT_ASM ("lwz   3, 4(4)\n"
		  "lwz   4, 0(4)");
      else
	EMIT_ASM ("lwz   3, 0(4)\n"
		  "lwz   4, 4(4)");
      break;
    }
}

/* TOP = NUM  */

static void
ppc_emit_const (LONGEST num)
{
  uint32_t buf[10];
  uint32_t *p = buf;

  p += gen_limm (p, 3, num >> 32 & 0xffffffff, 0);
  p += gen_limm (p, 4, num & 0xffffffff, 0);

  emit_insns (buf, p - buf);
  gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf)));
}

/* Set TOP to the value of register REG by calling get_raw_reg function
   with two argument, collected buffer and register number.  */

static void
ppc_emit_reg (int reg)
{
  uint32_t buf[13];
  uint32_t *p = buf;

  /* fctx->regs is passed in r3 and then saved in -16(31).  */
  p += GEN_LWZ (p, 3, 31, -16);
  p += GEN_LI (p, 4, reg);	/* li	r4, reg */
  p += gen_call (p, get_raw_reg_func_addr (), 0, 0);

  emit_insns (buf, p - buf);
  gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf)));

  if (__BYTE_ORDER == __LITTLE_ENDIAN)
    {
      EMIT_ASM ("mr 5, 4\n"
		"mr 4, 3\n"
		"mr 3, 5\n");
    }
}

/* TOP = stack[--sp] */

static void
ppc_emit_pop (void)
{
  EMIT_ASM ("lwzu " TOP_FIRST ", 8(30)	\n"
	    "lwz " TOP_SECOND ", 4(30)	\n");
}

/* stack[sp++] = TOP

   Because we may use up bytecode stack, expand 8 doublewords more
   if needed.  */

static void
ppc_emit_stack_flush (void)
{
  /* Make sure bytecode stack is big enough before push.
     Otherwise, expand 64-byte more.  */

  EMIT_ASM ("  stw   " TOP_FIRST ", 0(30)	\n"
	    "  stw   " TOP_SECOND ", 4(30)\n"
	    "  addi  5, 30, -(8 + 8)	\n"
	    "  cmpw  7, 5, 1		\n"
	    "  bgt   7, 1f		\n"
	    "  stwu  31, -64(1)		\n"
	    "1:addi  30, 30, -8		\n");
}

/* Swap TOP and stack[sp-1]  */

static void
ppc_emit_swap (void)
{
  EMIT_ASM ("lwz  " TMP_FIRST ", 8(30)	\n"
	    "lwz  " TMP_SECOND ", 12(30)	\n"
	    "stw  " TOP_FIRST ", 8(30)	\n"
	    "stw  " TOP_SECOND ", 12(30)	\n"
	    "mr   3, 5		\n"
	    "mr   4, 6		\n");
}

/* Discard N elements in the stack.  Also used for ppc64.  */

static void
ppc_emit_stack_adjust (int n)
{
  uint32_t buf[6];
  uint32_t *p = buf;

  n = n << 3;
  if ((n >> 15) != 0)
    {
      emit_error = 1;
      return;
    }

  p += GEN_ADDI (p, 30, 30, n);

  emit_insns (buf, p - buf);
  gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf)));
}

/* Call function FN.  */

static void
ppc_emit_call (CORE_ADDR fn)
{
  uint32_t buf[11];
  uint32_t *p = buf;

  p += gen_call (p, fn, 0, 0);

  emit_insns (buf, p - buf);
  gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf)));
}

/* FN's prototype is `LONGEST(*fn)(int)'.
   TOP = fn (arg1)
  */

static void
ppc_emit_int_call_1 (CORE_ADDR fn, int arg1)
{
  uint32_t buf[15];
  uint32_t *p = buf;

  /* Setup argument.  arg1 is a 16-bit value.  */
  p += gen_limm (p, 3, (uint32_t) arg1, 0);
  p += gen_call (p, fn, 0, 0);

  emit_insns (buf, p - buf);
  gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf)));

  if (__BYTE_ORDER == __LITTLE_ENDIAN)
    {
      EMIT_ASM ("mr 5, 4\n"
		"mr 4, 3\n"
		"mr 3, 5\n");
    }
}

/* FN's prototype is `void(*fn)(int,LONGEST)'.
   fn (arg1, TOP)

   TOP should be preserved/restored before/after the call.  */

static void
ppc_emit_void_call_2 (CORE_ADDR fn, int arg1)
{
  uint32_t buf[21];
  uint32_t *p = buf;

  /* Save TOP.  0(30) is next-empty.  */
  p += GEN_STW (p, 3, 30, 0);
  p += GEN_STW (p, 4, 30, 4);

  /* Setup argument.  arg1 is a 16-bit value.  */
  if (__BYTE_ORDER == __LITTLE_ENDIAN)
    {
       p += GEN_MR (p, 5, 4);
       p += GEN_MR (p, 6, 3);
    }
  else
    {
       p += GEN_MR (p, 5, 3);
       p += GEN_MR (p, 6, 4);
    }
  p += gen_limm (p, 3, (uint32_t) arg1, 0);
  p += gen_call (p, fn, 0, 0);

  /* Restore TOP */
  p += GEN_LWZ (p, 3, 30, 0);
  p += GEN_LWZ (p, 4, 30, 4);

  emit_insns (buf, p - buf);
  gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf)));
}

/* Note in the following goto ops:

   When emitting goto, the target address is later relocated by
   write_goto_address.  OFFSET_P is the offset of the branch instruction
   in the code sequence, and SIZE_P is how to relocate the instruction,
   recognized by ppc_write_goto_address.  In current implementation,
   SIZE can be either 24 or 14 for branch of conditional-branch instruction.
 */

/* If TOP is true, goto somewhere.  Otherwise, just fall-through.  */

static void
ppc_emit_if_goto (int *offset_p, int *size_p)
{
  EMIT_ASM ("or.    3, 3, 4	\n"
	    "lwzu " TOP_FIRST ", 8(30)	\n"
	    "lwz " TOP_SECOND ", 4(30)	\n"
	    "1:bne  0, 1b	\n");

  if (offset_p)
    *offset_p = 12;
  if (size_p)
    *size_p = 14;
}

/* Unconditional goto.  Also used for ppc64.  */

static void
ppc_emit_goto (int *offset_p, int *size_p)
{
  EMIT_ASM ("1:b	1b");

  if (offset_p)
    *offset_p = 0;
  if (size_p)
    *size_p = 24;
}

/* Goto if stack[--sp] == TOP  */

static void
ppc_emit_eq_goto (int *offset_p, int *size_p)
{
  EMIT_ASM ("lwzu  " TMP_FIRST ", 8(30)	\n"
	    "lwz   " TMP_SECOND ", 4(30)	\n"
	    "xor   4, 6, 4	\n"
	    "xor   3, 5, 3	\n"
	    "or.   3, 3, 4	\n"
	    "lwzu  " TOP_FIRST ", 8(30)	\n"
	    "lwz   " TOP_SECOND ", 4(30)	\n"
	    "1:beq 0, 1b	\n");

  if (offset_p)
    *offset_p = 28;
  if (size_p)
    *size_p = 14;
}

/* Goto if stack[--sp] != TOP  */

static void
ppc_emit_ne_goto (int *offset_p, int *size_p)
{
  EMIT_ASM ("lwzu  " TMP_FIRST ", 8(30)	\n"
	    "lwz   " TMP_SECOND ", 4(30)	\n"
	    "xor   4, 6, 4	\n"
	    "xor   3, 5, 3	\n"
	    "or.   3, 3, 4	\n"
	    "lwzu  " TOP_FIRST ", 8(30)	\n"
	    "lwz   " TOP_SECOND ", 4(30)	\n"
	    "1:bne 0, 1b	\n");

  if (offset_p)
    *offset_p = 28;
  if (size_p)
    *size_p = 14;
}

/* Goto if stack[--sp] < TOP  */

static void
ppc_emit_lt_goto (int *offset_p, int *size_p)
{
  EMIT_ASM ("lwzu " TMP_FIRST ", 8(30)	\n"
	    "lwz " TMP_SECOND ", 4(30)	\n"
	    "cmplw   6, 6, 4		\n"
	    "cmpw    7, 5, 3		\n"
	    /* CR6 bit 0 = low less and high equal */
	    "crand   6*4+0, 6*4+0, 7*4+2\n"
	    /* CR7 bit 0 = (low less and high equal) or high less */
	    "cror    7*4+0, 7*4+0, 6*4+0\n"
	    "lwzu    " TOP_FIRST ", 8(30)	\n"
	    "lwz     " TOP_SECOND ", 4(30)\n"
	    "1:blt   7, 1b	\n");

  if (offset_p)
    *offset_p = 32;
  if (size_p)
    *size_p = 14;
}

/* Goto if stack[--sp] <= TOP  */

static void
ppc_emit_le_goto (int *offset_p, int *size_p)
{
  EMIT_ASM ("lwzu " TMP_FIRST ", 8(30)	\n"
	    "lwz " TMP_SECOND ", 4(30)	\n"
	    "cmplw   6, 6, 4		\n"
	    "cmpw    7, 5, 3		\n"
	    /* CR6 bit 0 = low less/equal and high equal */
	    "crandc   6*4+0, 7*4+2, 6*4+1\n"
	    /* CR7 bit 0 = (low less/eq and high equal) or high less */
	    "cror    7*4+0, 7*4+0, 6*4+0\n"
	    "lwzu    " TOP_FIRST ", 8(30)	\n"
	    "lwz     " TOP_SECOND ", 4(30)\n"
	    "1:blt   7, 1b	\n");

  if (offset_p)
    *offset_p = 32;
  if (size_p)
    *size_p = 14;
}

/* Goto if stack[--sp] > TOP  */

static void
ppc_emit_gt_goto (int *offset_p, int *size_p)
{
  EMIT_ASM ("lwzu " TMP_FIRST ", 8(30)	\n"
	    "lwz " TMP_SECOND ", 4(30)	\n"
	    "cmplw   6, 6, 4		\n"
	    "cmpw    7, 5, 3		\n"
	    /* CR6 bit 0 = low greater and high equal */
	    "crand   6*4+0, 6*4+1, 7*4+2\n"
	    /* CR7 bit 0 = (low greater and high equal) or high greater */
	    "cror    7*4+0, 7*4+1, 6*4+0\n"
	    "lwzu    " TOP_FIRST ", 8(30)	\n"
	    "lwz     " TOP_SECOND ", 4(30)\n"
	    "1:blt   7, 1b	\n");

  if (offset_p)
    *offset_p = 32;
  if (size_p)
    *size_p = 14;
}

/* Goto if stack[--sp] >= TOP  */

static void
ppc_emit_ge_goto (int *offset_p, int *size_p)
{
  EMIT_ASM ("lwzu " TMP_FIRST ", 8(30)	\n"
	    "lwz " TMP_SECOND ", 4(30)	\n"
	    "cmplw   6, 6, 4		\n"
	    "cmpw    7, 5, 3		\n"
	    /* CR6 bit 0 = low ge and high equal */
	    "crandc  6*4+0, 7*4+2, 6*4+0\n"
	    /* CR7 bit 0 = (low ge and high equal) or high greater */
	    "cror    7*4+0, 7*4+1, 6*4+0\n"
	    "lwzu    " TOP_FIRST ", 8(30)\n"
	    "lwz     " TOP_SECOND ", 4(30)\n"
	    "1:blt   7, 1b	\n");

  if (offset_p)
    *offset_p = 32;
  if (size_p)
    *size_p = 14;
}

/* Relocate previous emitted branch instruction.  FROM is the address
   of the branch instruction, TO is the goto target address, and SIZE
   if the value we set by *SIZE_P before.  Currently, it is either
   24 or 14 of branch and conditional-branch instruction.
   Also used for ppc64.  */

static void
ppc_write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
{
  long rel = to - from;
  uint32_t insn;
  int opcd;

  read_inferior_memory (from, (unsigned char *) &insn, 4);
  opcd = (insn >> 26) & 0x3f;

  switch (size)
    {
    case 14:
      if (opcd != 16
	  || (rel >= (1 << 15) || rel < -(1 << 15)))
	emit_error = 1;
      insn = (insn & ~0xfffc) | (rel & 0xfffc);
      break;
    case 24:
      if (opcd != 18
	  || (rel >= (1 << 25) || rel < -(1 << 25)))
	emit_error = 1;
      insn = (insn & ~0x3fffffc) | (rel & 0x3fffffc);
      break;
    default:
      emit_error = 1;
    }

  if (!emit_error)
    target_write_memory (from, (unsigned char *) &insn, 4);
}

/* Table of emit ops for 32-bit.  */

static struct emit_ops ppc_emit_ops_impl =
{
  ppc_emit_prologue,
  ppc_emit_epilogue,
  ppc_emit_add,
  ppc_emit_sub,
  ppc_emit_mul,
  ppc_emit_lsh,
  ppc_emit_rsh_signed,
  ppc_emit_rsh_unsigned,
  ppc_emit_ext,
  ppc_emit_log_not,
  ppc_emit_bit_and,
  ppc_emit_bit_or,
  ppc_emit_bit_xor,
  ppc_emit_bit_not,
  ppc_emit_equal,
  ppc_emit_less_signed,
  ppc_emit_less_unsigned,
  ppc_emit_ref,
  ppc_emit_if_goto,
  ppc_emit_goto,
  ppc_write_goto_address,
  ppc_emit_const,
  ppc_emit_call,
  ppc_emit_reg,
  ppc_emit_pop,
  ppc_emit_stack_flush,
  ppc_emit_zero_ext,
  ppc_emit_swap,
  ppc_emit_stack_adjust,
  ppc_emit_int_call_1,
  ppc_emit_void_call_2,
  ppc_emit_eq_goto,
  ppc_emit_ne_goto,
  ppc_emit_lt_goto,
  ppc_emit_le_goto,
  ppc_emit_gt_goto,
  ppc_emit_ge_goto
};

#ifdef __powerpc64__

/*

  Bytecode execution stack frame - 64-bit

	|  LR save area           (SP + 16)
	|  CR save area           (SP + 8)
 SP' -> +- Back chain             (SP + 0)
	|  Save r31   for access saved arguments
	|  Save r30   for bytecode stack pointer
	|  Save r4    for incoming argument *value
	|  Save r3    for incoming argument regs
 r30 -> +- Bytecode execution stack
	|
	|  64-byte (8 doublewords) at initial.
	|  Expand stack as needed.
	|
	+-
	|  Some padding for minimum stack frame.
	|  112 for ELFv1.
 SP     +- Back-chain (SP')

  initial frame size
  = 112 + (4 * 8) + 64
  = 208

   r30 is the stack-pointer for bytecode machine.
       It should point to next-empty, so we can use LDU for pop.
   r3  is used for cache of TOP value.
       It was the first argument, pointer to regs.
   r4  is the second argument, pointer to the result.
       We should set *result = TOP after leaving this function.

 Note:
 * To restore stack at epilogue
   => sp = r31
 * To check stack is big enough for bytecode execution.
   => r30 - 8 > SP + 112
 * To return execution result.
   => 0(r4) = TOP

 */

/* Emit prologue in inferior memory.  See above comments.  */

static void
ppc64v1_emit_prologue (void)
{
  /* On ELFv1, function pointers really point to function descriptor,
     so emit one here.  We don't care about contents of words 1 and 2,
     so let them just overlap out code.  */
  uint64_t opd = current_insn_ptr + 8;
  uint32_t buf[2];

  /* Mind the strict aliasing rules.  */
  memcpy (buf, &opd, sizeof buf);
  emit_insns(buf, 2);
  EMIT_ASM (/* Save return address.  */
	    "mflr  0		\n"
	    "std   0, 16(1)	\n"
	    /* Save r30 and incoming arguments.  */
	    "std   31, -8(1)	\n"
	    "std   30, -16(1)	\n"
	    "std   4, -24(1)	\n"
	    "std   3, -32(1)	\n"
	    /* Point r31 to current r1 for access arguments.  */
	    "mr    31, 1	\n"
	    /* Adjust SP.  208 is the initial frame size.  */
	    "stdu  1, -208(1)	\n"
	    /* Set r30 to pointing stack-top.  */
	    "addi  30, 1, 168	\n"
	    /* Initial r3/TOP to 0.  */
	    "li	   3, 0		\n");
}

/* Emit prologue in inferior memory.  See above comments.  */

static void
ppc64v2_emit_prologue (void)
{
  EMIT_ASM (/* Save return address.  */
	    "mflr  0		\n"
	    "std   0, 16(1)	\n"
	    /* Save r30 and incoming arguments.  */
	    "std   31, -8(1)	\n"
	    "std   30, -16(1)	\n"
	    "std   4, -24(1)	\n"
	    "std   3, -32(1)	\n"
	    /* Point r31 to current r1 for access arguments.  */
	    "mr    31, 1	\n"
	    /* Adjust SP.  208 is the initial frame size.  */
	    "stdu  1, -208(1)	\n"
	    /* Set r30 to pointing stack-top.  */
	    "addi  30, 1, 168	\n"
	    /* Initial r3/TOP to 0.  */
	    "li	   3, 0		\n");
}

/* Emit epilogue in inferior memory.  See above comments.  */

static void
ppc64_emit_epilogue (void)
{
  EMIT_ASM (/* Restore SP.  */
	    "ld    1, 0(1)      \n"
	    /* *result = TOP */
	    "ld    4, -24(1)	\n"
	    "std   3, 0(4)	\n"
	    /* Restore registers.  */
	    "ld    31, -8(1)	\n"
	    "ld    30, -16(1)	\n"
	    /* Restore LR.  */
	    "ld    0, 16(1)	\n"
	    /* Return 0 for no-error.  */
	    "li    3, 0		\n"
	    "mtlr  0		\n"
	    "blr		\n");
}

/* TOP = stack[--sp] + TOP  */

static void
ppc64_emit_add (void)
{
  EMIT_ASM ("ldu  4, 8(30)	\n"
	    "add  3, 4, 3	\n");
}

/* TOP = stack[--sp] - TOP  */

static void
ppc64_emit_sub (void)
{
  EMIT_ASM ("ldu  4, 8(30)	\n"
	    "sub  3, 4, 3	\n");
}

/* TOP = stack[--sp] * TOP  */

static void
ppc64_emit_mul (void)
{
  EMIT_ASM ("ldu    4, 8(30)	\n"
	    "mulld  3, 4, 3	\n");
}

/* TOP = stack[--sp] << TOP  */

static void
ppc64_emit_lsh (void)
{
  EMIT_ASM ("ldu  4, 8(30)	\n"
	    "sld  3, 4, 3	\n");
}

/* Top = stack[--sp] >> TOP
   (Arithmetic shift right)  */

static void
ppc64_emit_rsh_signed (void)
{
  EMIT_ASM ("ldu   4, 8(30)	\n"
	    "srad  3, 4, 3	\n");
}

/* Top = stack[--sp] >> TOP
   (Logical shift right)  */

static void
ppc64_emit_rsh_unsigned (void)
{
  EMIT_ASM ("ldu  4, 8(30)	\n"
	    "srd  3, 4, 3	\n");
}

/* Emit code for signed-extension specified by ARG.  */

static void
ppc64_emit_ext (int arg)
{
  switch (arg)
    {
    case 8:
      EMIT_ASM ("extsb  3, 3");
      break;
    case 16:
      EMIT_ASM ("extsh  3, 3");
      break;
    case 32:
      EMIT_ASM ("extsw  3, 3");
      break;
    default:
      emit_error = 1;
    }
}

/* Emit code for zero-extension specified by ARG.  */

static void
ppc64_emit_zero_ext (int arg)
{
  switch (arg)
    {
    case 8:
      EMIT_ASM ("rldicl 3,3,0,56");
      break;
    case 16:
      EMIT_ASM ("rldicl 3,3,0,48");
      break;
    case 32:
      EMIT_ASM ("rldicl 3,3,0,32");
      break;
    default:
      emit_error = 1;
    }
}

/* TOP = !TOP
   i.e., TOP = (TOP == 0) ? 1 : 0;  */

static void
ppc64_emit_log_not (void)
{
  EMIT_ASM ("cntlzd  3, 3	\n"
	    "srdi    3, 3, 6	\n");
}

/* TOP = stack[--sp] & TOP  */

static void
ppc64_emit_bit_and (void)
{
  EMIT_ASM ("ldu  4, 8(30)	\n"
	    "and  3, 4, 3	\n");
}

/* TOP = stack[--sp] | TOP  */

static void
ppc64_emit_bit_or (void)
{
  EMIT_ASM ("ldu  4, 8(30)	\n"
	    "or   3, 4, 3	\n");
}

/* TOP = stack[--sp] ^ TOP  */

static void
ppc64_emit_bit_xor (void)
{
  EMIT_ASM ("ldu  4, 8(30)	\n"
	    "xor  3, 4, 3	\n");
}

/* TOP = ~TOP
   i.e., TOP = ~(TOP | TOP)  */

static void
ppc64_emit_bit_not (void)
{
  EMIT_ASM ("nor  3, 3, 3	\n");
}

/* TOP = stack[--sp] == TOP  */

static void
ppc64_emit_equal (void)
{
  EMIT_ASM ("ldu     4, 8(30)	\n"
	    "xor     3, 3, 4	\n"
	    "cntlzd  3, 3	\n"
	    "srdi    3, 3, 6	\n");
}

/* TOP = stack[--sp] < TOP
   (Signed comparison)  */

static void
ppc64_emit_less_signed (void)
{
  EMIT_ASM ("ldu     4, 8(30)		\n"
	    "cmpd    7, 4, 3		\n"
	    "mfcr    3			\n"
	    "rlwinm  3, 3, 29, 31, 31	\n");
}

/* TOP = stack[--sp] < TOP
   (Unsigned comparison)  */

static void
ppc64_emit_less_unsigned (void)
{
  EMIT_ASM ("ldu     4, 8(30)		\n"
	    "cmpld   7, 4, 3		\n"
	    "mfcr    3			\n"
	    "rlwinm  3, 3, 29, 31, 31	\n");
}

/* Access the memory address in TOP in size of SIZE.
   Zero-extend the read value.  */

static void
ppc64_emit_ref (int size)
{
  switch (size)
    {
    case 1:
      EMIT_ASM ("lbz   3, 0(3)");
      break;
    case 2:
      EMIT_ASM ("lhz   3, 0(3)");
      break;
    case 4:
      EMIT_ASM ("lwz   3, 0(3)");
      break;
    case 8:
      EMIT_ASM ("ld    3, 0(3)");
      break;
    }
}

/* TOP = NUM  */

static void
ppc64_emit_const (LONGEST num)
{
  uint32_t buf[5];
  uint32_t *p = buf;

  p += gen_limm (p, 3, num, 1);

  emit_insns (buf, p - buf);
  gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf)));
}

/* Set TOP to the value of register REG by calling get_raw_reg function
   with two argument, collected buffer and register number.  */

static void
ppc64v1_emit_reg (int reg)
{
  uint32_t buf[15];
  uint32_t *p = buf;

  /* fctx->regs is passed in r3 and then saved in 176(1).  */
  p += GEN_LD (p, 3, 31, -32);
  p += GEN_LI (p, 4, reg);
  p += GEN_STD (p, 2, 1, 40);	/* Save TOC.  */
  p += gen_call (p, get_raw_reg_func_addr (), 1, 1);
  p += GEN_LD (p, 2, 1, 40);	/* Restore TOC.  */

  emit_insns (buf, p - buf);
  gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf)));
}

/* Likewise, for ELFv2.  */

static void
ppc64v2_emit_reg (int reg)
{
  uint32_t buf[12];
  uint32_t *p = buf;

  /* fctx->regs is passed in r3 and then saved in 176(1).  */
  p += GEN_LD (p, 3, 31, -32);
  p += GEN_LI (p, 4, reg);
  p += GEN_STD (p, 2, 1, 24);	/* Save TOC.  */
  p += gen_call (p, get_raw_reg_func_addr (), 1, 0);
  p += GEN_LD (p, 2, 1, 24);	/* Restore TOC.  */

  emit_insns (buf, p - buf);
  gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf)));
}

/* TOP = stack[--sp] */

static void
ppc64_emit_pop (void)
{
  EMIT_ASM ("ldu  3, 8(30)");
}

/* stack[sp++] = TOP

   Because we may use up bytecode stack, expand 8 doublewords more
   if needed.  */

static void
ppc64_emit_stack_flush (void)
{
  /* Make sure bytecode stack is big enough before push.
     Otherwise, expand 64-byte more.  */

  EMIT_ASM ("  std   3, 0(30)		\n"
	    "  addi  4, 30, -(112 + 8)	\n"
	    "  cmpd  7, 4, 1		\n"
	    "  bgt   7, 1f		\n"
	    "  stdu  31, -64(1)		\n"
	    "1:addi  30, 30, -8		\n");
}

/* Swap TOP and stack[sp-1]  */

static void
ppc64_emit_swap (void)
{
  EMIT_ASM ("ld   4, 8(30)	\n"
	    "std  3, 8(30)	\n"
	    "mr   3, 4		\n");
}

/* Call function FN - ELFv1.  */

static void
ppc64v1_emit_call (CORE_ADDR fn)
{
  uint32_t buf[13];
  uint32_t *p = buf;

  p += GEN_STD (p, 2, 1, 40);	/* Save TOC.  */
  p += gen_call (p, fn, 1, 1);
  p += GEN_LD (p, 2, 1, 40);	/* Restore TOC.  */

  emit_insns (buf, p - buf);
  gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf)));
}

/* Call function FN - ELFv2.  */

static void
ppc64v2_emit_call (CORE_ADDR fn)
{
  uint32_t buf[10];
  uint32_t *p = buf;

  p += GEN_STD (p, 2, 1, 24);	/* Save TOC.  */
  p += gen_call (p, fn, 1, 0);
  p += GEN_LD (p, 2, 1, 24);	/* Restore TOC.  */

  emit_insns (buf, p - buf);
  gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf)));
}

/* FN's prototype is `LONGEST(*fn)(int)'.
   TOP = fn (arg1)
  */

static void
ppc64v1_emit_int_call_1 (CORE_ADDR fn, int arg1)
{
  uint32_t buf[13];
  uint32_t *p = buf;

  /* Setup argument.  arg1 is a 16-bit value.  */
  p += gen_limm (p, 3, arg1, 1);
  p += GEN_STD (p, 2, 1, 40);	/* Save TOC.  */
  p += gen_call (p, fn, 1, 1);
  p += GEN_LD (p, 2, 1, 40);	/* Restore TOC.  */

  emit_insns (buf, p - buf);
  gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf)));
}

/* Likewise for ELFv2.  */

static void
ppc64v2_emit_int_call_1 (CORE_ADDR fn, int arg1)
{
  uint32_t buf[10];
  uint32_t *p = buf;

  /* Setup argument.  arg1 is a 16-bit value.  */
  p += gen_limm (p, 3, arg1, 1);
  p += GEN_STD (p, 2, 1, 24);	/* Save TOC.  */
  p += gen_call (p, fn, 1, 0);
  p += GEN_LD (p, 2, 1, 24);	/* Restore TOC.  */

  emit_insns (buf, p - buf);
  gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf)));
}

/* FN's prototype is `void(*fn)(int,LONGEST)'.
   fn (arg1, TOP)

   TOP should be preserved/restored before/after the call.  */

static void
ppc64v1_emit_void_call_2 (CORE_ADDR fn, int arg1)
{
  uint32_t buf[17];
  uint32_t *p = buf;

  /* Save TOP.  0(30) is next-empty.  */
  p += GEN_STD (p, 3, 30, 0);

  /* Setup argument.  arg1 is a 16-bit value.  */
  p += GEN_MR (p, 4, 3);		/* mr	r4, r3 */
  p += gen_limm (p, 3, arg1, 1);
  p += GEN_STD (p, 2, 1, 40);	/* Save TOC.  */
  p += gen_call (p, fn, 1, 1);
  p += GEN_LD (p, 2, 1, 40);	/* Restore TOC.  */

  /* Restore TOP */
  p += GEN_LD (p, 3, 30, 0);

  emit_insns (buf, p - buf);
  gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf)));
}

/* Likewise for ELFv2.  */

static void
ppc64v2_emit_void_call_2 (CORE_ADDR fn, int arg1)
{
  uint32_t buf[14];
  uint32_t *p = buf;

  /* Save TOP.  0(30) is next-empty.  */
  p += GEN_STD (p, 3, 30, 0);

  /* Setup argument.  arg1 is a 16-bit value.  */
  p += GEN_MR (p, 4, 3);		/* mr	r4, r3 */
  p += gen_limm (p, 3, arg1, 1);
  p += GEN_STD (p, 2, 1, 24);	/* Save TOC.  */
  p += gen_call (p, fn, 1, 0);
  p += GEN_LD (p, 2, 1, 24);	/* Restore TOC.  */

  /* Restore TOP */
  p += GEN_LD (p, 3, 30, 0);

  emit_insns (buf, p - buf);
  gdb_assert ((p - buf) <= (sizeof (buf) / sizeof (*buf)));
}

/* If TOP is true, goto somewhere.  Otherwise, just fall-through.  */

static void
ppc64_emit_if_goto (int *offset_p, int *size_p)
{
  EMIT_ASM ("cmpdi  7, 3, 0	\n"
	    "ldu    3, 8(30)	\n"
	    "1:bne  7, 1b	\n");

  if (offset_p)
    *offset_p = 8;
  if (size_p)
    *size_p = 14;
}

/* Goto if stack[--sp] == TOP  */

static void
ppc64_emit_eq_goto (int *offset_p, int *size_p)
{
  EMIT_ASM ("ldu     4, 8(30)	\n"
	    "cmpd    7, 4, 3	\n"
	    "ldu     3, 8(30)	\n"
	    "1:beq   7, 1b	\n");

  if (offset_p)
    *offset_p = 12;
  if (size_p)
    *size_p = 14;
}

/* Goto if stack[--sp] != TOP  */

static void
ppc64_emit_ne_goto (int *offset_p, int *size_p)
{
  EMIT_ASM ("ldu     4, 8(30)	\n"
	    "cmpd    7, 4, 3	\n"
	    "ldu     3, 8(30)	\n"
	    "1:bne   7, 1b	\n");

  if (offset_p)
    *offset_p = 12;
  if (size_p)
    *size_p = 14;
}

/* Goto if stack[--sp] < TOP  */

static void
ppc64_emit_lt_goto (int *offset_p, int *size_p)
{
  EMIT_ASM ("ldu     4, 8(30)	\n"
	    "cmpd    7, 4, 3	\n"
	    "ldu     3, 8(30)	\n"
	    "1:blt   7, 1b	\n");

  if (offset_p)
    *offset_p = 12;
  if (size_p)
    *size_p = 14;
}

/* Goto if stack[--sp] <= TOP  */

static void
ppc64_emit_le_goto (int *offset_p, int *size_p)
{
  EMIT_ASM ("ldu     4, 8(30)	\n"
	    "cmpd    7, 4, 3	\n"
	    "ldu     3, 8(30)	\n"
	    "1:ble   7, 1b	\n");

  if (offset_p)
    *offset_p = 12;
  if (size_p)
    *size_p = 14;
}

/* Goto if stack[--sp] > TOP  */

static void
ppc64_emit_gt_goto (int *offset_p, int *size_p)
{
  EMIT_ASM ("ldu     4, 8(30)	\n"
	    "cmpd    7, 4, 3	\n"
	    "ldu     3, 8(30)	\n"
	    "1:bgt   7, 1b	\n");

  if (offset_p)
    *offset_p = 12;
  if (size_p)
    *size_p = 14;
}

/* Goto if stack[--sp] >= TOP  */

static void
ppc64_emit_ge_goto (int *offset_p, int *size_p)
{
  EMIT_ASM ("ldu     4, 8(30)	\n"
	    "cmpd    7, 4, 3	\n"
	    "ldu     3, 8(30)	\n"
	    "1:bge   7, 1b	\n");

  if (offset_p)
    *offset_p = 12;
  if (size_p)
    *size_p = 14;
}

/* Table of emit ops for 64-bit ELFv1.  */

static struct emit_ops ppc64v1_emit_ops_impl =
{
  ppc64v1_emit_prologue,
  ppc64_emit_epilogue,
  ppc64_emit_add,
  ppc64_emit_sub,
  ppc64_emit_mul,
  ppc64_emit_lsh,
  ppc64_emit_rsh_signed,
  ppc64_emit_rsh_unsigned,
  ppc64_emit_ext,
  ppc64_emit_log_not,
  ppc64_emit_bit_and,
  ppc64_emit_bit_or,
  ppc64_emit_bit_xor,
  ppc64_emit_bit_not,
  ppc64_emit_equal,
  ppc64_emit_less_signed,
  ppc64_emit_less_unsigned,
  ppc64_emit_ref,
  ppc64_emit_if_goto,
  ppc_emit_goto,
  ppc_write_goto_address,
  ppc64_emit_const,
  ppc64v1_emit_call,
  ppc64v1_emit_reg,
  ppc64_emit_pop,
  ppc64_emit_stack_flush,
  ppc64_emit_zero_ext,
  ppc64_emit_swap,
  ppc_emit_stack_adjust,
  ppc64v1_emit_int_call_1,
  ppc64v1_emit_void_call_2,
  ppc64_emit_eq_goto,
  ppc64_emit_ne_goto,
  ppc64_emit_lt_goto,
  ppc64_emit_le_goto,
  ppc64_emit_gt_goto,
  ppc64_emit_ge_goto
};

/* Table of emit ops for 64-bit ELFv2.  */

static struct emit_ops ppc64v2_emit_ops_impl =
{
  ppc64v2_emit_prologue,
  ppc64_emit_epilogue,
  ppc64_emit_add,
  ppc64_emit_sub,
  ppc64_emit_mul,
  ppc64_emit_lsh,
  ppc64_emit_rsh_signed,
  ppc64_emit_rsh_unsigned,
  ppc64_emit_ext,
  ppc64_emit_log_not,
  ppc64_emit_bit_and,
  ppc64_emit_bit_or,
  ppc64_emit_bit_xor,
  ppc64_emit_bit_not,
  ppc64_emit_equal,
  ppc64_emit_less_signed,
  ppc64_emit_less_unsigned,
  ppc64_emit_ref,
  ppc64_emit_if_goto,
  ppc_emit_goto,
  ppc_write_goto_address,
  ppc64_emit_const,
  ppc64v2_emit_call,
  ppc64v2_emit_reg,
  ppc64_emit_pop,
  ppc64_emit_stack_flush,
  ppc64_emit_zero_ext,
  ppc64_emit_swap,
  ppc_emit_stack_adjust,
  ppc64v2_emit_int_call_1,
  ppc64v2_emit_void_call_2,
  ppc64_emit_eq_goto,
  ppc64_emit_ne_goto,
  ppc64_emit_lt_goto,
  ppc64_emit_le_goto,
  ppc64_emit_gt_goto,
  ppc64_emit_ge_goto
};

#endif

/* Implementation of target ops method "emit_ops".  */

emit_ops *
ppc_target::emit_ops ()
{
#ifdef __powerpc64__
  struct regcache *regcache = get_thread_regcache (current_thread, 0);

  if (register_size (regcache->tdesc, 0) == 8)
    {
      if (is_elfv2_inferior ())
	return &ppc64v2_emit_ops_impl;
      else
	return &ppc64v1_emit_ops_impl;
    }
#endif
  return &ppc_emit_ops_impl;
}

/* Implementation of target ops method "get_ipa_tdesc_idx".  */

int
ppc_target::get_ipa_tdesc_idx ()
{
  struct regcache *regcache = get_thread_regcache (current_thread, 0);
  const struct target_desc *tdesc = regcache->tdesc;

#ifdef __powerpc64__
  if (tdesc == tdesc_powerpc_64l)
    return PPC_TDESC_BASE;
  if (tdesc == tdesc_powerpc_altivec64l)
    return PPC_TDESC_ALTIVEC;
  if (tdesc == tdesc_powerpc_vsx64l)
    return PPC_TDESC_VSX;
  if (tdesc == tdesc_powerpc_isa205_64l)
    return PPC_TDESC_ISA205;
  if (tdesc == tdesc_powerpc_isa205_altivec64l)
    return PPC_TDESC_ISA205_ALTIVEC;
  if (tdesc == tdesc_powerpc_isa205_vsx64l)
    return PPC_TDESC_ISA205_VSX;
  if (tdesc == tdesc_powerpc_isa205_ppr_dscr_vsx64l)
    return PPC_TDESC_ISA205_PPR_DSCR_VSX;
  if (tdesc == tdesc_powerpc_isa207_vsx64l)
    return PPC_TDESC_ISA207_VSX;
  if (tdesc == tdesc_powerpc_isa207_htm_vsx64l)
    return PPC_TDESC_ISA207_HTM_VSX;
#endif

  if (tdesc == tdesc_powerpc_32l)
    return PPC_TDESC_BASE;
  if (tdesc == tdesc_powerpc_altivec32l)
    return PPC_TDESC_ALTIVEC;
  if (tdesc == tdesc_powerpc_vsx32l)
    return PPC_TDESC_VSX;
  if (tdesc == tdesc_powerpc_isa205_32l)
    return PPC_TDESC_ISA205;
  if (tdesc == tdesc_powerpc_isa205_altivec32l)
    return PPC_TDESC_ISA205_ALTIVEC;
  if (tdesc == tdesc_powerpc_isa205_vsx32l)
    return PPC_TDESC_ISA205_VSX;
  if (tdesc == tdesc_powerpc_isa205_ppr_dscr_vsx32l)
    return PPC_TDESC_ISA205_PPR_DSCR_VSX;
  if (tdesc == tdesc_powerpc_isa207_vsx32l)
    return PPC_TDESC_ISA207_VSX;
  if (tdesc == tdesc_powerpc_isa207_htm_vsx32l)
    return PPC_TDESC_ISA207_HTM_VSX;
  if (tdesc == tdesc_powerpc_e500l)
    return PPC_TDESC_E500;

  return 0;
}

/* The linux target ops object.  */

linux_process_target *the_linux_target = &the_ppc_target;

void
initialize_low_arch (void)
{
  /* Initialize the Linux target descriptions.  */

  init_registers_powerpc_32l ();
  init_registers_powerpc_altivec32l ();
  init_registers_powerpc_vsx32l ();
  init_registers_powerpc_isa205_32l ();
  init_registers_powerpc_isa205_altivec32l ();
  init_registers_powerpc_isa205_vsx32l ();
  init_registers_powerpc_isa205_ppr_dscr_vsx32l ();
  init_registers_powerpc_isa207_vsx32l ();
  init_registers_powerpc_isa207_htm_vsx32l ();
  init_registers_powerpc_e500l ();
#if __powerpc64__
  init_registers_powerpc_64l ();
  init_registers_powerpc_altivec64l ();
  init_registers_powerpc_vsx64l ();
  init_registers_powerpc_isa205_64l ();
  init_registers_powerpc_isa205_altivec64l ();
  init_registers_powerpc_isa205_vsx64l ();
  init_registers_powerpc_isa205_ppr_dscr_vsx64l ();
  init_registers_powerpc_isa207_vsx64l ();
  init_registers_powerpc_isa207_htm_vsx64l ();
#endif

  initialize_regsets_info (&ppc_regsets_info);
}