aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 745750c92df6b128c46e9a5c626f935107619050 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
//! # `yaxpeax-power`, a decoder for the `power` instruction set family.
//!
//! this covers the Power ISA up through v3.1, as well as settings to decode instructions as
//! PowerPC. the interplay between POWER, PowerPC, and the Power ISA are somewhat historically
//! interesting, and as the instruction sets are interrelated, the decoder here strives to support
//! the whole lineage through which they are related.
//!
//! a whirlwind tour (i think accurate, but i'm not totally sure!):
//! * In The Beginning there was the RS/6000, later also called "POWER1".
//! * later, POWER2 came to market. this was, so far as i know, architecturally very similar, and
//!   came alongside the still-similar PowerPC 601. it's not clear to me how the 601 relates to the
//!   PowerPC ISA, PowerPC 603, and later PowerPC processors.
//! * POWER3 involves some elements of PowerPC, but i'm not sure what (yet?).
//! * POWER4 and PowerPC are roughly overlapping. there are revisions here and it's not
//!   clear to me when 2.0 and 2.01 happen in relation to particular product naming.
//! * POWER5 is formalized in the Power ISA v2.02 book
//! * subsequent revisions of the Power 2 ISA are implemented in some later "PowerPC" processor
//!   models
//! * subsequent revisions of the Power 2 ISA *also* are implemented in POWER6, POWER7, POWER8.
//! * Power ISA 3.0 is implemented in POWER9, as well as OpenPOWER and friends.
//! * Power ISA 3.1 is implemented in POWER10, and POWER11.
//! * and who knows what POWER12(?) will bring! not me in 2026.
//!
//! so: along the POWER1 to POWER12 lineage, architectures are backwards compatible ... for the
//! most part. there are some divergences where instructions that were valid in POWER1 were dropped
//! in moving to PowerPC and what became Power v2.0, but many moved with only different mnemonics.
//! from v2.0 onwards, though, backwards compatibility reigns supreme.
//!
//! `yaxpeax-power` configures the disassembler based on requested Power ISA revision, and
//! provides aliases for particular processors where appropriate.
//!
//! ## usage
//!
//! `yaxpeax-power` usage demo:
//! ```
//! use yaxpeax_arch::Decoder;
//!
//! let decoder = yaxpeax_power::InstDecoder::default();
//! let expected = "lvewx vr0, 0, r10";
//! let data = [0x7c, 0x00, 0x50, 0x8e];
//! let mut reader = yaxpeax_arch::U8Reader::new(&data[..]);
//! let inst = decoder.decode(&mut reader).unwrap();
//! assert_eq!(format!("{}", inst), expected);
//!
//! // or, in fewer words:
//! let inst = decoder.decode_slice(&data).unwrap();
//! assert_eq!(format!("{}", inst), expected);
//! ```

#![no_std]
#![deny(missing_docs)]

use core::fmt;

use yaxpeax_arch::{
    Decoder, Arch, Reader, AddressDiff, ReadError, LengthedInstruction,
};

#[cfg(feature = "fmt")]
/// structs and functions useful for either printing a decoded instruction, custom logic to control
/// instruction formatting, or implementation of custom `DisplaySink` for `yaxpeax-power`
/// instructions.
pub mod display;
#[cfg(feature = "fmt")]
pub use display::{compute_branch_mnemonic, AbsoluteAddressFormatter, DisplayRules, NoAliasRules};

mod alias;
pub use alias::{AliasOp, InstructionAlias, OpcodeAlias};

/// the `Power` struct and [`Arch`] impl'd on it describe both the old 32-bit implementations as
/// well as 64-bit implementations and their 32-bit modes. this is mostly for expedience;
/// duplicating [`Instruction`] and impls on it for the sake of a more precise `Opcode` implies a
/// lot of library duplication for relatively little gain. (the library needs to document which
/// instructions can be decoded in which ISA revisions anyway for all the 2.0-and-later additions,
/// so adding some documentation about "before-PowerPC" is already on the docket)
///
/// this is mainly confusing for `Power` having a "`u64`" address size, even when decoding Power
/// ISA instructions for 32-bit ISA revisions (namely, "Power v1.0" as seen in POWER1/POWER2). if
/// you're in the extraordinarily rare set of users who might be interested in that distinction,
/// this is why the library is the way that it is. and if i've modeled the Power ISA incorrectly,
/// please say hello!
pub struct Power {}

impl Arch for Power {
    /// Power is bi-endian, but so far as i can tell *defaults* to big-endian and first-party
    /// software before the Linux transition to ppc64le *seems* to be big-endian also?
    type Word = yaxpeax_arch::U32be;
    type Address = u64;
    type Instruction = Instruction;
    type DecodeError = DecodeError;
    type Decoder = InstDecoder;
    type Operand = Operand;
}

/// the kinds of errors possibly encountered in trying to decode a `power` instruction
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum DecodeError {
    /// the input was insufficient to decode a full instruction (that is, there were not four bytes
    /// available).
    ExhaustedInput,
    /// the instruction encodes an opcode that is not valid.
    InvalidOpcode,
    /// the instruction encodes an operand that is not valid.
    InvalidOperand,
    /// `yaxpeax-power` doesn't know how to decode this, but it may be a valid instruction. the
    /// instruction decoder is not complete, sorry. :(
    IncompleteDecoder,
}

impl fmt::Display for DecodeError {
    fn fmt(&self, f:  &mut fmt::Formatter) -> fmt::Result {
        use yaxpeax_arch::DecodeError;
        f.write_str(self.description())
    }
}

#[cfg(feature = "std")]
extern crate std;
#[cfg(feature = "std")]
impl std::error::Error for DecodeError {
    fn description(&self) -> &str {
        <Self as yaxpeax_arch::DecodeError>::description(self)
    }
}

impl From<ReadError> for DecodeError {
    fn from(_e: ReadError) -> DecodeError {
        DecodeError::ExhaustedInput
    }
}

impl yaxpeax_arch::DecodeError for DecodeError {
    fn data_exhausted(&self) -> bool { self == &DecodeError::ExhaustedInput }
    fn bad_opcode(&self) -> bool { self == &DecodeError::InvalidOpcode }
    fn bad_operand(&self) -> bool { self == &DecodeError::InvalidOperand }
    fn description(&self) -> &'static str {
        match self {
            DecodeError::ExhaustedInput => "exhausted input",
            DecodeError::InvalidOpcode => "invalid opcode",
            DecodeError::InvalidOperand => "invalid operand",
            DecodeError::IncompleteDecoder => "incomplete decoder",
        }
    }
}

impl yaxpeax_arch::Instruction for Instruction {
    fn well_defined(&self) -> bool { true }
}

/// a `power` instruction decoder.
///
/// this can be configured to decode instruction for any supported step along the Power ISA lineage
/// via [`IsaVersion`]
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
pub struct InstDecoder {
    /// what version of the Power ISA does the processor support?
    version: IsaVersion,

    /// is the processor operating in big-endian mode? this defaults to `true`, but can be
    /// dynamically changed by writing to an MSR.
    is_be: bool,
}

/// opcode 4 has mutually-incompatible interpretations (either vector/LMA or signal processing
/// extensions). so, which do we get from the current decoder settings?
#[allow(dead_code)]
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
enum Opc4Mode {
    /// aka "VMX", present in Power v2.06+ and Cell PPE.
    AltiVec,
    /// the interesting quirk mode of VMX used by Xenon with 128 vector registers.
    VMX128,
    /// the later VSX extensions on the vector ISA.
    VSX,
    /// "Paired Singles", the paired single extension used in Gekko.
    PS,
    /// "Signal Processing", several categories of instructions included in embedded Power
    /// implementations, which were mutually exclusive with AltiVec encodings under opcode 4.
    SP,
}

/// opcode 39 also has mutually-incompatible interpretations.  so, which do we get from the current
/// decoder settings?
#[allow(dead_code)]
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
enum Opc39Mode {
    /// interpret opcode 39 as Gekko would.
    Gekko,
    /// interpret opcode 39 as a Power v2.05+ system would (`lfdp`)
    DFP,
    /// interpret opcode 39 as a POWER2 CPU would (`lfqu`)
    POWER2,
}

/// opcode 3C also has mutually-incompatible interpretations (`psq_st` or VSX, depending on ISA).
/// so, which do we get from the current decoder settings?
#[allow(dead_code)]
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
enum Opc3cMode {
    /// interpret opcode 3C as Gekko would, a `psq_st`.
    Gekko,
    /// the later VSX extensions on the vector ISA.
    VSX,
    /// interpret opcode 3C as a POWER2 CPU would (`stfq`)
    POWER2,
}

/// opcode 3D also has mutually-incompatible interpretations (`psq_stu` or VSX, depending on ISA).
/// so, which do we get from the current decoder settings?
#[allow(dead_code)]
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
enum Opc3dMode {
    /// interpret opcode 3D as Gekko would, a `psq_stu`.
    Gekko,
    /// the later VSX extensions on the vector ISA.
    VSX,
    /// the liminal space after 2.05 which defined `stfdp` for opcode 3D, and before the 3.0-era
    /// additions to VSX which defined additional XO fields.
    STFDP,
    /// interpret opcode 3D as a POWER2 CPU would (`stfqu`)
    POWER2,
}

impl InstDecoder {
    /// configure this `InstDecoder` to decode words as a big-endian processor.
    pub fn with_be(mut self, big_endian: bool) -> Self {
        self.is_be = big_endian;
        self
    }

    /// an `InstDecoder` suitably configured for decoding a `pp64le` binary, as found around
    /// Linux-related software targeting POWER8 and later.
    ///
    /// this is shorthand for selecting `IsaVersion::V207` and `with_be(false)`.
    pub fn ppc64le() -> Self {
        InstDecoder::for_cpu(IsaVersion::V2_07).with_be(false)
    }

    fn opc4_mode(&self) -> Option<Opc4Mode> {
        // default to VSX as the "v2.06 and later default"
        if self.version == IsaVersion::Xenon {
            Some(Opc4Mode::VMX128)
        } else if self.version == IsaVersion::Gekko {
            Some(Opc4Mode::PS)
        } else if self.version == IsaVersion::All || self.version.supports(IsaConstraint::VSX) {
            Some(Opc4Mode::VSX)
        } else if self.version.supports(IsaConstraint::AltiVec) {
            Some(Opc4Mode::AltiVec)
        } else {
            None
        }
    }

    fn opc39_mode(&self) -> Option<Opc39Mode> {
        if self.version == IsaVersion::Gekko {
            Some(Opc39Mode::Gekko)
        } else if self.version == IsaVersion::All || self.version.supports(IsaConstraint::PowerIsa205) {
            Some(Opc39Mode::DFP)
        } else if self.version == IsaVersion::RIOS2 {
            Some(Opc39Mode::POWER2)
        } else {
            None
        }
    }

    fn opc3c_mode(&self) -> Option<Opc3cMode> {
        if self.version == IsaVersion::Gekko {
            Some(Opc3cMode::Gekko)
        } else if self.version == IsaVersion::All || self.version.supports(IsaConstraint::VSX) {
            Some(Opc3cMode::VSX)
        } else if self.version == IsaVersion::RIOS2 {
            Some(Opc3cMode::POWER2)
        } else {
            None
        }
    }

    fn opc3d_mode(&self) -> Option<Opc3dMode> {
        if self.version == IsaVersion::Gekko {
            Some(Opc3dMode::Gekko)
        } else if self.version == IsaVersion::All || self.version.supports(IsaConstraint::VSX) {
            Some(Opc3dMode::VSX)
        } else if self.version.supports(IsaConstraint::PowerIsa205) {
            Some(Opc3dMode::STFDP)
        } else if self.version == IsaVersion::RIOS2 {
            Some(Opc3dMode::POWER2)
        } else {
            None
        }
    }
}

impl Default for InstDecoder {
    fn default() -> Self {
        Self {
            version: IsaVersion::All,
            is_be: true
        }
    }
}

/// TODO: this probably could use additional per-processor definitions, maybe?
/// >
/// > These forms of the mtcrf and mfcr instructions are intended to
/// > replace the old forms of the instructions (the forms shown in page
/// > 85), which will eventually be phased out of the architecture. The
/// > new forms are backward compatible with most processors that comply
/// > with versions of the architecture that precede Version 2.00. On
/// > those processors, the new forms are treated as the old forms.
/// >
/// > However, on some processors that comply with versions of the
/// > architecture that precede Version 2.00 the new forms may be treated
/// > as follows: (illegal instruction handler, or undefined)
#[allow(non_camel_case_types)]
#[allow(missing_docs)]
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
pub enum IsaVersion {
    /// the PowerPC implementation used in the Nintendo GameCube. additionally this `IsaVersion`
    /// describes Broadway, the PowerPC implementation used in the Nintendo Wii.
    Gekko,
    /// the PowerPC implementation used in the Sony PlayStation 3.
    ///
    /// specifically, this describes the PPU, or Power Processor Unit, in contrast to the
    /// Synergistic Processor Unit (SPU) also present in the Cell processor.
    Cell,
    /// the PowerPC implementation used in the Microsoft Xbox 360.
    Xenon,
    /// the specific Power implementation used in the move of AS/400 to a Power-based processor.
    /// this is also called "Power AS" or "PowerPC AS", and in some manuals also is mentioned as
    /// "Power A".
    Amazon,
    /// the PowerPC e500 core, which among other details included BTB locking instructions.
    e500,
    /// the PowerPC e500mc core, which (in addition to e500) includes `Embedded.Decorated Storage`.
    e500mc,
    /// the Power ISA implemented by POWER1 processors in the early RS/6000 series.
    RIOS,
    /// the Power ISA implemented by POWER2 processors later in the RS/6000 series.
    RIOS2,
    /// the Power ISA as supported by initial 32-bit PowerPC implementations.
    ///
    /// this is not quite POWER4, as the first PowerPC implementations slightly predated additions
    /// in POWER4 (and POWER3!)
    PowerPC,
    /// WIP: whatever POWER3 actually implements. frankenstein powerpc/power2 isa?
    V1_99,
    /// it is theoretically possible that Power v2.0 and v2.01 implementations may have been
    /// 32-bit.
    V2_0_32b,
    V2_01_32b,
    /// Power v2.0 as present in POWER4, or PowerPC 970, were 64-bit.
    V2_0,
    V2_01,
    V2_02,
    V2_03,
    V2_04,
    V2_05,
    V2_06,
    /// the IBM A2 processor (used in Blue Gene/Q). this processor includes instructions in the
    /// implementation-specific region of opcode space. documentation for this processor comes from
    /// `A2 Processor User's Manual for Blue Gene/Q`, retrieved from
    /// <https://wiki.alcf.anl.gov/parts/images/c/cf/A2.pdf> via Wayback Machine
    /// snapshot 01 Dec 2020, `sha256: 72294cf3315ab8b6e0b5b1c436c85bb0b81c2b7acb8e72899873f3a479398973`
    A2,
    V2_07,
    /// POWER9
    V3_0,
    V3_1,
    /// a best-effort interpretation of "decode as many opcodes as have ever been implemented in
    /// any Power processor ever". some byte sequences may have been repurposed with different
    /// interpretations across the ages, so while resulting programs "probably" make sense, they
    /// may be bogus!
    All,
}

impl IsaVersion {
    fn includes_ppc(&self) -> bool {
        match self {
            IsaVersion::RIOS |
            IsaVersion::RIOS2 => false,
            _ => true,
        }
    }

    fn supports(&self, constraint: IsaConstraint) -> bool {
        if *self == IsaVersion::All {
            return true;
        }

        match constraint {
            IsaConstraint::All => true,
            IsaConstraint::LE_POWER2 => {
                // beware: POWER2 is not V2.0! 2.0 was contemporaneous with ~POWER4.
                static LE_POWER2: &'static [IsaVersion] = &[
                    IsaVersion::RIOS, IsaVersion::RIOS2, IsaVersion::V1_99,
                ];
                LE_POWER2.contains(&self)
            }
            IsaConstraint::Fpu => {
                *self == IsaVersion::All ||
                    (*self != IsaVersion::e500 &&
                        (*self == IsaVersion::RIOS || *self == IsaVersion::RIOS2 || self.supports(IsaConstraint::Ppc)))
            }
            // the RS/6000 manual says there's nothing under opcode 59, so no single-precision
            // floats
            IsaConstraint::FpuNoSingle => {
                *self == IsaVersion::All ||
                    (*self != IsaVersion::e500 &&
                        self.supports(IsaConstraint::Ppc))
            }
            IsaConstraint::Isel => {
                // TODO: binutils says many other older POWER implementations support `isel` too
                self.supports(IsaConstraint::PowerIsa206) ||
                    *self == IsaVersion::A2 ||
                    *self == IsaVersion::e500
            }
            IsaConstraint::Btb => {
                *self == IsaVersion::e500
            }
            IsaConstraint::Xenon => {
                *self == IsaVersion::Xenon
            }
            IsaConstraint::Gekko => {
                *self == IsaVersion::Gekko
            }
            IsaConstraint::CellOrXenon => {
                *self == IsaVersion::Cell || *self == IsaVersion::Xenon
            }
            IsaConstraint::Ppc => {
                static PPC_VERSIONS: &'static [IsaVersion] = &[
                    IsaVersion::Amazon, IsaVersion::e500, IsaVersion::e500mc,
                    IsaVersion::Xenon, IsaVersion::Cell, IsaVersion::Gekko,
                    IsaVersion::A2,
                    // v2.05 was standardized in 2007, well after Xenon and Cell, so they are
                    // conspicuously missing here!
                    IsaVersion::PowerPC, IsaVersion::V1_99,
                    IsaVersion::V2_0_32b, IsaVersion::V2_01_32b,
                    IsaVersion::V2_0, IsaVersion::V2_01,
                    IsaVersion::V2_02, IsaVersion::V2_03, IsaVersion::V2_04,
                    IsaVersion::V2_05, IsaVersion::V2_06,
                    IsaVersion::A2,
                    IsaVersion::V2_07,
                    IsaVersion::V3_0, IsaVersion::V3_1
                ];
                PPC_VERSIONS.contains(&self)
            }
            IsaConstraint::Slb => {
                *self == IsaVersion::Amazon ||
                    (*self != IsaVersion::Xenon && *self != IsaVersion::Gekko && self.supports(IsaConstraint::Ppc))
            }
            IsaConstraint::Seg => {
                *self != IsaVersion::Xenon && *self != IsaVersion::Amazon
            }
            IsaConstraint::Ec | IsaConstraint::Tlb | IsaConstraint::Rfi => {
                *self != IsaVersion::Xenon &&
                    (*self == IsaVersion::RIOS || *self == IsaVersion::RIOS2 || self.supports(IsaConstraint::Ppc))
            }
            IsaConstraint::TlbSw => {
                // entries in the extended opcode table for TlbSw generally were `invalid!(Ppc)`,
                // reflecting that these instructions generally were removed by the time PowerPC
                // got to market.
                *self == IsaVersion::e500 || *self == IsaVersion::e500mc || *self == IsaVersion::A2
            }
            IsaConstraint::Embedded => {
                static ISA_EMBEDDED: &'static [IsaVersion] = &[
                    IsaVersion::V2_03, IsaVersion::V2_04,
                    IsaVersion::V2_05, IsaVersion::V2_06,
                    IsaVersion::A2,
                    IsaVersion::V2_07,
                ];
                ISA_EMBEDDED.contains(&self)
            }
            IsaConstraint::Ppc64 => {
                // it is presumably not true that all Power 2.02+ implementations support 64-bit
                // instructions (and the operating mode), but that is currently true of the
                // individual implementations mentioned in IsaVersion..
                //
                // Amazon is on this list as RS64 seems to have supported 64-bit from the get-go,
                // > The RS64 is also used in the AS/400 (called A35).
                *self == IsaVersion::Amazon ||
                *self == IsaVersion::Xenon || *self == IsaVersion::Cell ||
                    *self == IsaVersion::V2_0 || *self == IsaVersion::V2_01 ||
                    self.supports(IsaConstraint::PowerIsa202)
            }
            IsaConstraint::POWER => {
                // POWER1-specific quirks. this might mean slightly different instructions (`tlbi`
                // rather than `tlbie`), different operands (again, see `tlbi`), or instructions
                // that were deleted in the move to PowerPC. generally, POWER1 (and POWER2) are
                // constraints that should be used carefully, because "All" also supports
                // POWER1/POWER2 in support of decoding deleted instructions, but use of the ISA
                // constraint should not move decoding *away* from interpreting sequence in the
                // "latest" reasonable meaning.
                *self == IsaVersion::RIOS || *self == IsaVersion::RIOS2
            }
            IsaConstraint::POWER2 => {
                // "POWER2" is odd because it defines instructions that generally are not supported
                // *after* POWER2 (e.g. removed in PowerPC, etc)
                *self == IsaVersion::RIOS2
            }
            IsaConstraint::PowerIsa200 => {
                static ISA_GE_200: &'static [IsaVersion] = &[
                    IsaVersion::Amazon,
                    IsaVersion::Cell, IsaVersion::Xenon,
                    IsaVersion::V2_0_32b, IsaVersion::V2_01_32b,
                    IsaVersion::V2_0, IsaVersion::V2_01,
                    IsaVersion::V2_02,
                    IsaVersion::V2_03, IsaVersion::V2_04,
                    IsaVersion::V2_05, IsaVersion::V2_06,
                    IsaVersion::e500mc,
                    IsaVersion::A2,
                    IsaVersion::V2_07,
                    IsaVersion::V3_0, IsaVersion::V3_1
                ];
                ISA_GE_200.contains(&self)
            }
            IsaConstraint::PowerIsa202 => {
                static ISA_GE_202: &'static [IsaVersion] = &[
                    // > The PPE is based on Version 2.02 of the PowerPC
                    // > Architecture, which offers many of the features required
                    // > for the application spaces targeted by the CBEA
                    IsaVersion::Cell,
                    IsaVersion::V2_02,
                    IsaVersion::V2_03, IsaVersion::V2_04,
                    IsaVersion::V2_05, IsaVersion::V2_06,
                    IsaVersion::e500mc,
                    IsaVersion::A2,
                    IsaVersion::V2_07,
                    IsaVersion::V3_0, IsaVersion::V3_1
                ];
                ISA_GE_202.contains(&self)
            }
            IsaConstraint::PowerIsa203 => {
                static ISA_GE_203: &'static [IsaVersion] = &[
                    // seems that some instructions (lwsi, stswi, ..) are supported on Cell, so
                    // says RPCS3. that means Cell is 2.03+?
                    IsaVersion::Cell,
                    // notably, dcbtls and related decodes on e500
                    IsaVersion::e500,
                    IsaVersion::V2_03, IsaVersion::V2_04,
                    IsaVersion::V2_05, IsaVersion::V2_06,
                    IsaVersion::e500mc,
                    IsaVersion::A2,
                    IsaVersion::V2_07,
                    IsaVersion::V3_0, IsaVersion::V3_1
                ];
                ISA_GE_203.contains(&self)
            }
            IsaConstraint::PowerIsa205 => {
                static ISA_GE_205: &'static [IsaVersion] = &[
                    IsaVersion::e500, IsaVersion::e500mc,
                    IsaVersion::A2,
                    IsaVersion::V2_05,
                    IsaVersion::V2_06, IsaVersion::V2_07,
                    IsaVersion::V3_0, IsaVersion::V3_1,
                ];
                ISA_GE_205.contains(&self)
            }
            IsaConstraint::PowerIsa206 => {
                static ISA_GE_206: &'static [IsaVersion] = &[
                    IsaVersion::e500, IsaVersion::e500mc,
                    IsaVersion::V2_06, IsaVersion::V2_07,
                    IsaVersion::V3_0, IsaVersion::V3_1,
                ];
                ISA_GE_206.contains(&self)
            }
            IsaConstraint::PowerIsa207 => {
                static ISA_GE_207: &'static [IsaVersion] = &[
                    IsaVersion::V2_07,
                    IsaVersion::V3_0, IsaVersion::V3_1,
                ];
                ISA_GE_207.contains(&self)
            }
            IsaConstraint::PowerIsa30 => {
                static ISA_GE_30: &'static [IsaVersion] = &[
                    IsaVersion::V3_0, IsaVersion::V3_1,
                ];
                ISA_GE_30.contains(&self)
            }

            IsaConstraint::DFP => {
                static DFP_VERSIONS: &'static [IsaVersion] = &[
                    // v2.05 was standardized in 2007, well after Xenon and Cell, so they are
                    // conspicuously missing here!
                    IsaVersion::V2_05, IsaVersion::V2_06, IsaVersion::V2_07,
                    IsaVersion::V3_0, IsaVersion::V3_1
                ];
                DFP_VERSIONS.contains(&self)
            }

            IsaConstraint::DFPQA | IsaConstraint::DFP25 => {
                static DFP25_VERSIONS: &'static [IsaVersion] = &[
                    IsaVersion::V2_05,
                    IsaVersion::V2_06, IsaVersion::V2_07,
                    IsaVersion::V3_0, IsaVersion::V3_1
                ];
                DFP25_VERSIONS.contains(&self)
            }

            IsaConstraint::DFP26 => {
                static DFP26_VERSIONS: &'static [IsaVersion] = &[
                    IsaVersion::V2_06, IsaVersion::V2_07,
                    IsaVersion::V3_0, IsaVersion::V3_1
                ];
                DFP26_VERSIONS.contains(&self)
            }

            IsaConstraint::DFP27 => {
                static DFP27_VERSIONS: &'static [IsaVersion] = &[
                    IsaVersion::V2_07,
                    IsaVersion::V3_0, IsaVersion::V3_1
                ];
                DFP27_VERSIONS.contains(&self)
            }

            IsaConstraint::AltiVec_LE206 => {
                self.supports(IsaConstraint::AltiVec) && !self.supports(IsaConstraint::PowerIsa206)
            }

            IsaConstraint::AltiVec => {
                static ALTIVEC_VERSIONS: &'static [IsaVersion] = &[
                    IsaVersion::Xenon, IsaVersion::Cell,
                    /* V2.02 does not have AltiVec */
                    IsaVersion::V2_03, IsaVersion::V2_04,
                    IsaVersion::V2_05, IsaVersion::V2_06, IsaVersion::V2_07,
                    IsaVersion::V3_0, IsaVersion::V3_1
                ];
                ALTIVEC_VERSIONS.contains(&self)
            }

            IsaConstraint::AltiVec_NonPower => {
                static ALTIVEC_VERSIONS: &'static [IsaVersion] = &[
                    // HELP: does Cell support `dss`, `dssall`, etc?
                    IsaVersion::Xenon, IsaVersion::Cell,
                ];
                ALTIVEC_VERSIONS.contains(&self)
            }

            IsaConstraint::VMX128 => {
                *self == IsaVersion::Xenon
            }

            IsaConstraint::VSX => {
                *self == IsaVersion::V2_07 || *self == IsaVersion::V3_0 || *self == IsaVersion::V3_1
            }

            IsaConstraint::LMA => {
                panic!("unhandled constraint: {:?}", constraint);
            }
        }
    }
}

/// a decoded Power ISA instruction.
///
/// `Instruction` describes the "simplest" book-defined interpretation: no aliasing is applied,
/// `Opcode` maps to individual opcodes with headings in the Power ISA books.
///
/// see `compute_alias` for reinterpretation as book-defined assembler aliases. note that for
/// display purposes, the default behavior is already to display assembler aliases where defined.
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
pub struct Instruction {
    opcode: Opcode,
    operands: [Option<Operand>; 5],
    // the state `Rc` on instructions..
    rc: bool,
    // the state of `AA` on instructions. only relevant for branches.
    aa: bool,
    // the state of `LK` on instructions. only relevant for branches(?)
    lk: bool,
    // the state of `OE` on instructions. only relevant for some arithmetic(?)
    oe: bool,
    // which form of hinting does the conditional branch in this instruction have?
    //
    // if this instruction is not branch-hinted, this is None.
    hint_style: Option<HintStyle>,
    len: u64,
}

/// this is only public to use in tests with `compute_branch_mnemonic`. don't use it.
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
pub enum HintStyle {
    /// the BO field is interpreted as described in documents like:
    /// > PowerPC Microprocessor Family: The Programming Environments For 32-Bit Microprocessors
    ///
    /// which is to say, default prediction target depends on the opcode and offset sign, with a
    /// bit to optionally invert the prediction.
    Legacy,
    /// the BO field is interpreted as described in any of the Power ISA books since 2.00
    ///
    /// here, the default prediction behavior is hardware-defined, with taken and not-taken hints
    /// describe by the BO field.
    PowerIsa,
}

impl Instruction {
    fn init_from(alias: &InstructionAlias) -> Self {
        let this = Instruction {
            opcode: Opcode::XORI,
            operands: alias.operands.clone(),
            rc: alias.rc(),
            aa: alias.aa(),
            lk: alias.lk(),
            oe: alias.oe(),
            hint_style: alias.hint_style(),
            len: alias.len,
        };
        this
    }

    /// get the [`Opcode`] for this instruction.
    pub fn opcode(&self) -> Opcode {
        self.opcode
    }

    /// get the `idx`'th operand for this instruction.
    ///
    /// returns `None` if there is no operand at the requested index.
    pub fn operand(&self, idx: u8) -> Option<&Operand> {
        if let Some(op) = self.operands.get(idx as usize) {
            op.as_ref()
        } else {
            None
        }
    }

    /// get the number of operands for this instruction.
    ///
    /// `Instruction::operand` returns `Some` for any `idx` below the returned count, and `None`
    /// otherwise..
    pub fn operand_count(&self) -> usize {
        let mut count = 0;
        for op in self.operands.iter() {
            if op.is_some() {
                count += 1;
            } else {
                break;
            }
        }
        count
    }

    /// get the instruction's RECORD (`Rc`) bit.
    ///
    /// the RECORD bit indicates if the condition register should be updated as part of executing
    /// this instruction. it is indicated as a trailing `.` after instruction mnemonics in textual
    /// form.
    pub fn rc(&self) -> bool {
        self.rc
    }

    /// get the instruction's Absolute Address (`AA`) bit.
    ///
    /// the Absolute Address bit indicates if a branch uses a relative or absolute addressing mode.
    /// this is unlikely to be useful; it is redundant with both the `BranchAbs`/`BranchOffset`
    /// operand forms and `BA`/`BCA`/`BLA`/`BCLA` where `AA` already resulted in an opcode with
    /// trailing `A` meaning this same thing.
    ///
    /// for non-branches this is not meaningful and always `false`.
    pub fn aa(&self) -> bool {
        self.aa
    }

    /// get the instruction's LINK (`Lk`) bit.
    ///
    /// the LINK bit indicates if a branch sets the Link Register (`lr`) with the address of the
    /// instruction after the branch as part of execution. like `aa()` above, this is redundant
    /// with opcodes which include the "set link" implication: `BL`, `BCL`, `BLA`, `BCLA`.
    ///
    /// for non-branches this is not meaningful and always `false`.
    pub fn lk(&self) -> bool {
        self.lk
    }

    /// get the instruction's OE bit.
    ///
    /// the OE bit indicates if this instruction sets the OV and SO bits in XER as part of
    /// execution. this is also indicated with a trailing `o` after the instruction mnemonic but
    /// before an optional `.` in textual form.
    ///
    /// this is only relevant for XO-form instructions, mostly arithmetic.
    pub fn oe(&self) -> bool {
        self.oe
    }

    /// accumulate this instruction with a `DisplayRules` to get a tuple out which is suitable for
    /// `DisplaySink` or any general `format!()` use.
    #[cfg(feature = "fmt")]
    pub fn display_rules<'a, 'rules, Rules>(
        &'a self,
        rules: &'rules Rules,
    ) -> display::InstructionRuleBundle<'a, 'rules, Rules> {
        display::InstructionRuleBundle {
            instr: self,
            rules,
        }
    }

    /// reinterpret this instruction as a book-defined assembler alias, if one is defined.
    ///
    /// if no alias is defined for this instruction and operands, the returned `InstructionAlias`
    /// will instead be this instruction with an `AliasOp::Hardware` opcode.
    pub fn compute_alias(&self) -> InstructionAlias {
        <InstructionAlias as yaxpeax_arch::InstructionAlias<Self>>::compute_alias(self)
    }
}

impl LengthedInstruction for Instruction {
    type Unit = AddressDiff<<Power as Arch>::Address>;

    #[inline]
    fn len(&self) -> Self::Unit {
        AddressDiff::from_const(self.len)
    }

    #[inline]
    fn min_size() -> Self::Unit {
        // TODO: this is really a question of the ISA version being decoded, since VLE instructions
        // could be as small as two bytes, but everything else has a four-byte floor. not really
        // expressible in yaxpeax-arch terms..
        AddressDiff::from_const(4)
    }
}

impl Default for Instruction {
    /// the "default" instruction is `xori r0, r0, 0`.
    fn default() -> Self {
        Self {
            opcode: Opcode::XORI,
            operands: [
                // fill out the operands so this at least has the same shape as an equivalent
                // decoded instruction..
                Some(Operand::Register(Register::new(0))),
                Some(Operand::Register(Register::new(0))),
                Some(Operand::ImmU16(0)),
                None,
                None,
            ],
            rc: false,
            aa: false,
            lk: false,
            oe: false,
            hint_style: None,
            len: 4,
        }
    }
}

#[allow(non_camel_case_types)]
#[non_exhaustive]
#[allow(missing_docs)]
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)]
pub enum Opcode {
    TDI = 0,
    TD,
    TWI,
    TW,
    MULLD,
    /// "POWER mnemonic: muli"
    MULLI,
    MULLW,
    MULHWU,
    MULHW,
    MULHDU,
    MULHD,
    /// "POWER mnemonic: sfi"
    SUBFIC,
    SUBFZE,
    SUBFE,
    SUBFME,
    SUBFC,
    SUBF,
    CMPLI,
    CMPL,
    CMPI,
    CMP,
    ADD,
    ADDE,
    /// "POWER mnemonic: ai"
    ADDIC,
    ADDC,
    /// add immediate, as in `addi Rt, Ra, SI`.
    ///
    /// note that if `Ra` is 0 then the value 0 is used, not GPR 0.
    /// "POWER mnemonic: cal"
    ADDI,
    /// "POWER mnemonic: cau"
    ADDIS,
    ADDZE,
    ADDME,
    BC,
    BCA,
    BCL,
    BCLA,
    SC,
    /// the POWER1/POWER2 predecessor to "svc". this is only decoded when asking for one of those
    /// two processor types via `RIOS` or `RIOS2`.
    SVC,
    B,
    BA,
    BL,
    BLA,
    RLMI,
    RLDIMI,
    RLWIMI,
    RLWINM,
    RLWNM,
    OR,
    ORC,
    ORI,
    ORIS,
    XOR,
    XORI,
    XORIS,
    ANDI,
    ANDIS,
    AND,
    NAND,
    ANDC,
    LD,
    LDU,
    LWA,
    LWAX,
    LWAUX,
    LWZ,
    LWZU,
    LWZUX,
    LBZ,
    LBZX,
    LBZU,
    LBZUX,
    STD,
    STDU,
    STDX,
    STDUX,
    STDCX,
    STW,
    STWU,
    STWX,
    STWCX,
    STWUX,
    STB,
    STBU,
    STBX,
    STBUX,
    LHZ,
    LHZU,
    LHZX,
    LHZUX,
    LHA,
    LHAU,
    LHAX,
    LHAUX,
    LHBRX,
    LWBRX,
    LWARX,
    LWZX,
    STH,
    STHU,
    STHX,
    STHUX,
    LMW,
    STMW,
    LFS,
    LFSU,
    LFSX,
    LFSUX,
    LFD,
    LFDX,
    LFDU,
    LFDUX,

    LBDX,
    LHDX,
    LWDX,
    LDDX,
    LFDDX,

    LFDP,
    LFDPX,

    LFQ,
    STFS,
    STFSU,
    STFD,
    STFDU,
    STFQ,
    STFDP,
    STFQU,
    LSWI,
    LSWX,
    LSCBX,
    SLE,
    SLQ,

    NEG,
    MUL,
    NOR,
    ABS,
    NABS,

    DIV,
    DIVW,
    DIVWU,
    DIVS,
    DIVD,
    DIVDU,

    EXTSW,
    EXTSH,
    EXTSB,

    STFSX,
    STFSUX,
    STFDUX,

    STFDPX,

    STFDX,
    STFIWX,
    STFQUX,
    STFQX,
    STHBRX,
    STWBRX,
    STSWX,
    STSWI,

    LFQU, // POWER2 only?
    LFQX,
    LFQUX,

    TLBIA,
    TLBIE,
    /// the POWER/POWER2-only form of TLB invalidation
    TLBI,
    /// e500 and related software TLB management?
    TLBRE,
    TLBWE,
    TLBSX,
    TLBIVAX,

    SLIQ,
    SLEQ,
    SLBIA,
    SLBIE,
    SLBMTE,
    SLBMFEV,
    SLBMFEE,

    EQV,

    ECIWX,
    ECOWX,

    EIEIO,
    MBAR,

    SRW,
    SRD,
    SRE,
    SREA,
    SREQ,
    SRIQ,
    SRQ,
    SRLQ,
    SRAW,
    SRAWI,
    SRAD,
    SRADI,
    SRAIQ,
    SRLIQ,
    SRAQ,

    SLLQ,
    SLLIQ,

    DCBT,
    /// the specific form of `dcbt` as understood by POWER implementations after RIOS2 and before
    /// PowerPC and the ~v2.03 era of processors. practically this is probably only produced when
    /// decoding instructions as by `IsaVersion::Amazon`.
    ///
    /// primarily this form of `dcbt` has a narrower `TH` hint with different interpretations than
    /// later processors.
    DCBT_AS,
    DCBTLS,
    DCBI,
    DCLST,
    ICBI,

    CLI,
    CLF,
    CLCS,

    RAC,
    RRIB,
    DOZ,
    DOZI,

    TLBSYNC,
    SYNC,
    ISYNC,

    MXRCR,
    MCRXR,

    MFTB,
    MFCR,
    MFOCRF,
    MFMSR,
    MFSR,
    MFSRI,
    MFSRIN,
    MFSPR,
    MCRF,
    MCRFS,
    MTOCRF,
    MTCRF,
    MTSR,
    MTSRD,
    MTSPR,
    MTMSR,
    MTMSRD,
    /// the specific form of `mtmsrd` understood by Xenon, where L=1 changes this instruction to
    /// the unique-to-Xenon `mtmsree`.
    MTMSRD_Xenon,
    MTSRIN,
    MTSRDIN,
    MFFS,
    MCFRS,
    MTFSFI,
    MTFSF,
    MTFSB0,
    MTFSB1,

    LDX,
    LDUX,
    LDARX,
    SLW,
    SLD,
    CNTLZW,
    CNTLZD,

    CRXOR,
    CROR,
    CRNOR,
    CRAND,
    CRNAND,
    CRANDC,
    CRORC,
    CREQV,

    BCCTR,
    BCLR,

    DCBA,
    DCBST,
    DCBTST,
    DCBF,
    DCBZ,
    /// an alternate form of `dcbz` which only exists on Xenon.
    ///
    /// `dcbz` has a bit field which is ignored in decoding in most Power implementations, but
    /// Xenia requires it to be 0 (producing `dcbz`) or 1 (producing `dcbz128`). i do not know if
    /// this impliex Xenon rejects other forms of dcbz, or if Xenia overly strict.
    DCBZ128,
    MASKIR,
    MASKG,

    /// these `Data Stream ...` instructions are defined by AltiVec but `ALTIVECPOWERISAPEM.pdf`
    /// states they are not present in AltiVec as incorporated into the Power ISA (that would be
    /// .. v2.01 or so?).
    ///
    /// it is not clear to me what implementations *do* have these instructions, but at the bare
    /// minimum these are known to exist in the Cell PPU.
    DST,
    DSTT,
    DSS,
    DSSALL,
    DSTST,
    DSTSTT,

    RLDCL,
    RLDIC,
    RLDICL,
    RLDCR,
    RLDICR,

    RFI,
    RFID,
    RFSVC,
    HRFID,

    BBELR,
    BBLELS,

    ATTN,
    MSGCLR,
    MSGSND,
    MSGCLRP,
    MSGSNDP,

    // float/double
    DRSP,
    DSUB,
    DADD,
    DADDQ,
    DMUL,
    DMULQ,
    DDIV,

    FMR,
    FABS,
    FNABS,
    FNEG,
    FRE,
    FRES,
    FRSP,
    FMUL,
    FMULS,
    FDIV,
    FDIVS,
    FSUB,
    FSUBS,
    FADD,
    FADDS,
    FSQRT,
    FSQRTS,
    FSEL,
    ISEL,
    FRIN,
    FRIM,
    FRIP,
    FRIZ,
    FRSQRTE,
    FRSQRTES,
    FLSQRT,
    FMSUB,
    FMSUBS,
    FMADD,
    FMADDS,
    FNMSUB,
    FNMSUBS,
    FNMADD,
    FNMADDS,

    FTDIV,
    FTSQRT,
    FCMPO,
    FCMPU,
    FCPSGN,

    DQUA,
    DQUAQ,
    DQUAI,
    DQUAIQ,
    DSCLI,
    DSCLIQ,
    DSCRI,
    DSCRIQ,
    DRDPQ,
    DRRND,
    DRRNDQ,
    DSUBQ,
    DDIVQ,
    DIEX,
    DIEXQ,
    DXEX,
    DXEXQ,
    DCMPO,
    DCMPU,
    DCMPOQ,
    DCMPUQ,
    DCFFIX,
    DCTFIX,
    DCFFIXQ,
    DCTFIXQ,
    DCTDP,
    DCTQPQ,
    DRINTN,
    DRINTNQ,
    DRINTX,
    DRINTXQ,
    DTSTDC,
    DTSTDCQ,
    DTSTDG,
    DTSTDGQ,
    DTSTSF,
    DTSTSFQ,
    DTSTEX,
    DTSTEXQ,
    DENBCD,
    DENBCDQ,
    DDEDPD,
    DDEDPDQ,

    FCFIDU,
    FCFIDUS,
    FCFIDS,
    FCTIW,
    FCTIWZ,
    FCTIWU,
    FCTIWUZ,
    FCTIDU,
    FCTIDUZ,
    FCFID,
    FCTID,
    FCTIDZ,

    // all vector ops
    VMHADDSHS,
    VMHRADDSHS,
    VMLADDUHM,
    VMSUMUBM,
    VMSUMMBM,
    VMSUMUHM,
    VMSUMUHS,
    VMSUMSHM,
    VMSUMSHS,
    VSEL,
    VPERM,
    VSLDOI,
    VMADDFP,
    VNMSUBFP,

    // mixed vector/lma
    VADDUBM,
    VMAXUB,
    VRLB,
    VCMPEQUB,
    VMULOUB,
    VADDFP,
    VMRGHB,
    VPKUHUM,
    MULHHWU,
    MACHHWU,
    VADDUHM,
    VMAXUH,
    VRLH,
    VMULOUH,
    VSUBFP,
    VMRGHH,
    VPKUWUM,
    MULHHWW,
    NMACHHW,
    VADDUWM,
    VMAXUW,
    VRLW,
    VMRGHW,
    VPKUHUS,
    VPKUWUS,
    MACHHWS,
    VMAXSB,
    VSLB,
    VMULOSB,
    VREFP,
    VMRGLB,
    VPKSHUS,
    MULCHWU,
    MACCHWU,
    VMAXSH,
    VSLH,
    VMULOSH,
    VRSQRTEFP,
    VMRGLH,
    VPKSWUS,
    MULCHW,
    NMACCHW,
    VADDCUW,
    VMAXSW,
    VSLW,
    VEXPTEFP,
    VMRGLW,
    VPKSHSS,
    VPKSWSS,
    VSL,
    VLOGEFP,
    MACCHWS,
    VADDUBS,
    VMINUB,
    VSRB,
    VMULEUB,
    VRFIN,
    VSPLTB,
    VUPKHSB,
    VADDUHS,
    VMINUH,
    VSRH,
    VMULEUH,
    VRFIZ,
    VSPLTH,
    VUPKHSH,
    VADDUWS,
    VMINUW,
    VSRW,
    VRFIP,
    VSPLTW,
    VUPKLSB,
    VSR,
    VRFIM,
    VUPKLSH,
    VADDSBS,
    VMINSB,
    VSRAB,
    VMULESB,
    VCFUX,
    VSPLTISB,
    VPKPX,
    MULLHWU,
    MACLHWU,
    VADDSHS,
    VMINSH,
    VSRAH,
    VMULESH,
    VCFSX,
    VSPLTISH,
    VUPKHPX,
    MULLHW,
    NMACLHW,
    VADDSWS,
    VMINSW,
    VSRAW,
    VCTUXS,
    VSPLTISW,
    VCTSXS,
    VUPKLPX,
    MACLHWS,
    VSUBUBM,
    VAVGUH,
    VAND,
    VMAXFP,
    VSLO,
    VSUBUHM,
    VAVGUW,
    VANDC,
    VCMPEQUH,
    VMINFP,
    VSRO,
    VAVGSH,
    MACHHW,
    VSUBUWM,
    VAVGUB,
    VOR,
    VCMPEQUW,
    VXOR,
    VCMPEQFP,
    VAVGSB,
    VNOR,
    MACCHW,
    VSUBCUW,
    VAVGSW,
    VSUM4UBS,
    VSUM4SHS,
    VSUM2SWS,
    VSUM4SBS,
    MACLHW,
    VCMPGEFP,
    VSUBUBS,
    MFVSCR,
    VCMPGTUB,
    VCMP4UBS,
    VSUBUHS,
    MTVSCR,
    VCMPGTUH,
    VSUBUWS,
    VCMPGTUW,
    VCMPGTFP,
    VSUBSBS,
    VCMPGTSB,
    VSUBSHS,
    VCMPGTSH,
    VSUBSWS,
    VCMPGTSW,
    VSUMSWS,
    VCMPBFP,

    /// VMX128 instructions
    VUPKHSB128,
    VUPKLSB128,
    VCMPEQFP128,
    VCMPGEFP128,
    VCMPGTFP128,
    VCMPBFP128,
    VCMPEQUW128,
    VMAXFP128,
    VMINFP128,
    VMRGHW128,
    VMRGLW128,

    LVSL128,
    LVSR128,
    LVEWX128,
    LVX128,
    STVX128,
    LVXL128,
    STVEWX128,
    STVXL128,
    LVLX128,
    LVRX128,
    STVLX128,
    STVRX128,
    LVLXL128,
    LVRXL128,
    STVLXL128,
    STVRXL128,
    VSLDOI128,

    VPERM128,
    VPKSHSS128,
    VPKSHUS128,
    VPKSWSS128,
    VPKSWUS128,
    VPKUHUM128,
    VPKUHUS128,
    VPKUWUM128,
    VPKUWUS128,
    VADDFP128,
    VSUBFP128,
    VMULFP128,
    VMADDFP128,
    VMADDCFP128,
    VNMSUBFP128,
    VMSUM3FP128,
    VMSUM4FP128,
    VAND128,
    VANDC128,
    VNOR128,
    VOR128,
    VXOR128,
    VSEL128,
    VSLO128,
    VSRO128,

    VSRAW128,
    VRLW128,
    VSLW128,
    VSRW128,

    VPERMWI128,
    VCFPSXWS128,
    VCFPUXWS128,
    VCSXWFP128,
    VCUXWFP128,
    VRFIM128,
    VRFIN128,
    VRFIP128,
    VRFIZ128,
    VPKD3D128,
    VREFP128,
    VRSQRTEFP128,
    VEXPTEFP128,
    VLOGEFP128,
    VRLIMI128,
    VSPLTW128,
    VSPLTISW128,
    VUPKD3D128,

    // first added in one of the Blue Gene CPUs, since standardized in at least 2.07
    WRTEE,
    WRTEEI,

    // AltiVec
    LVSL,
    LVSR,
    STVEPX,
    STVEPXL,
    // lvlx and lvrx are strange in that the AltiVec manual does not mention them, Power v2.03 UISA
    // mentions them only in Table 8, but does not define them, and Xenia decodes them either way.
    //
    // TODO: assuming these were underdocumented and in all AltiVec with the rest of the baseline
    // vector instructions until i learn otherwise..
    LVLX,
    LVRX,
    LVX,
    LVXL,
    LVEBX,
    LVEHX,
    LVEWX,
    STVX,
    STVXL,
    STVLX,
    STVRX,
    STVLXL,
    STVRXL,
    STVEBX,
    STVEHX,
    STVEWX,

    // this appears to be undocumented and only present in Xenon and Cell?
    LVLXL,
    LVRXL,

    // "Category: 64-bit"
    LDBRX,
    STDBRX,

    // transactional instructions on v2.07+
    TABORT,
    TABORTDC,
    TABORTDCI,
    TABORTWC,
    TABORTWCI,
    TBEGIN,
    TEND,
    TCHECK,
    TSR,
    TRECLAIM,
    TRECHKPT,

    /// the various Gekko paired singles instructions
    DCBZ_L,

    PS_SUM0,
    PS_SUM1,
    PS_MULS0,
    PS_MULS1,
    PS_MADDS0,
    PS_MADDS1,
    PS_DIV,
    PS_SUB,
    PS_ADD,
    PS_SEL,
    PS_RES,
    PS_MUL,
    PS_RSQRTE,
    PS_MSUB,
    PS_MADD,
    PS_NMSUB,
    PS_NMADD,

    PS_NEG,
    PS_MR,
    PS_NABS,
    PS_ABS,

    PS_CMPU0,
    PS_CMPO0,
    PS_CMPU1,
    PS_CMPO1,

    PS_MERGE00,
    PS_MERGE01,
    PS_MERGE10,
    PS_MERGE11,

    PSQ_L,
    PSQ_LU,
    PSQ_LX,
    PSQ_LUX,
    PSQ_ST,
    PSQ_STU,
    PSQ_STX,
    PSQ_STUX = 697,
    // v--------^
    // don't forget to keep this up to date with the array in display.rs!
}

impl Opcode {
    /// get the opcode as a lowercase string.
    #[cfg(feature = "fmt")]
    pub fn name(&self) -> &'static str {
        let id = *self as u16;
        display::OPCODES[id as usize]
    }

    /// return `true` if this instruction is one of the various forms of branch opcode.
    pub fn is_branch(&self) -> bool {
        match self {
            Opcode::B | Opcode::BL | Opcode::BA | Opcode::BLA |
            Opcode::BC | Opcode::BCL | Opcode::BCA | Opcode::BCLA |
            Opcode::BCLR |
            Opcode::BCCTR => true,
            _ => false
        }
    }
}

/// the controls for how `sync` .. syncs.
///
/// described in Book II, `Synchronize X-form` under `Memory Barrier Instructions`.
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)]
pub enum SyncControl {
    /// "heavyweight sync". the resulting sync instruction is `sync`.
    ///
    /// > The memory barrier provides an ordering function for the storage accesses associated with
    /// > all instructions that are executed by the processor executing the sync instruction. The
    /// > applicable pairs are all pairs ai ,bj in which bj is a data access, except that if ai is
    /// > the storage access caused by an icbi instruction then bj may be performed with respect to
    /// > the processor executing the sync instruction before ai is performed with respect to that
    /// > processor.
    Default = 0,
    /// "lightweight sync". the resulting sync instruction is `lwsync`.
    ///
    /// > The memory barrier provides an ordering function for the storage accesses caused by Load,
    /// > Store, and dcbz instructions that are executed by the processor executing the sync
    /// > instruction and for which the specified storage location is in storage that is Memory
    /// > Coherence Required and is neither Write Through Required nor Caching Inhibited. The
    /// > applicable pairs are all pairs ai ,bj of such accesses except those in which ai is an
    /// > access caused by a Store or dcbz instruction and bj is an access caused by a Load
    /// > instruction.
    LW = 1,
    /// the resulting instruction is `ptesync`.
    ///
    /// > The set of storage accesses that is ordered by the memory barrier is described in the
    /// > section entitled “Synchronize Instruction” in Book III, as are additional properties of
    /// > the sync instruction with L=2.
    PTE = 2,
    /// > The value L = 3 is reserved, and the results of executing a sync instruction with L=3 are
    /// > boundedly undefined.
    Reserved = 3,
}

impl SyncControl {
    #[inline(always)]
    fn from_bits(b: u8) -> Self {
        match b {
            0 => SyncControl::Default,
            1 => SyncControl::LW,
            2 => SyncControl::PTE,
            _ => SyncControl::Reserved,
        }
    }
}

/// a Power ISA GPR, with number in the range 0..31 (inclusive).
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)]
pub struct Register {
    reg: u8,
}

impl Register {
    /// create a struct describing `r<N>`.
    ///
    /// panics if `reg` is not a valid register number (e.g. is greater than 31)
    pub fn new(reg: u8) -> Self {
        assert!(reg < 32);

        Self { reg }
    }

    /// get the register number for this General Purpose Register.
    pub fn num(&self) -> u8 {
        self.reg
    }
}

/// a Power ISA FPR, with number in the range 0..31 (inclusive).
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)]
pub struct FPR {
    reg: u8,
}

impl FPR {
    /// create a struct describing `fr<N>`.
    ///
    /// panics if `reg` is not a valid register number (e.g. is greater than 31)
    pub fn new(reg: u8) -> Self {
        assert!(reg < 32);

        Self { reg }
    }

    /// get the register number for this Floating Point Register.
    pub fn num(&self) -> u8 {
        self.reg
    }
}

// from `Move To Special Purpose Register XFX-form`:
// 1 => XER
// 8 => LR
// 9 => CTR
/// a Power ISA SPR.
///
/// the specific numbers and interpretations vary across processors. help is welcome: i'd love
/// references to include, and possibly SPR tables in the library!
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)]
pub struct SPR {
    reg: u16,
}

impl SPR {
    /// create a new SPR for a given register number.
    ///
    /// because SPR register numbers are not generally portable, this may or may not be reasonable
    /// to a given Power implementation.
    pub fn new(reg: u16) -> Self {
        Self { reg }
    }

    /// get the register number for this Special-Purpose Register.
    pub fn num(&self) -> u16 {
        self.reg
    }
}

/// a Power ISA vector register, with number in the range 0..31 (inclusive).
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)]
pub struct VR {
    reg: u8,
}

impl VR {
    /// create a struct describing `vr<N>`.
    ///
    /// panics if `reg` is not a valid register number (e.g. is greater than 31)
    pub fn new(reg: u8) -> Self {
        assert!(reg < 32);

        Self { reg }
    }

    /// get the register number for this vector register.
    pub fn num(&self) -> u8 {
        self.reg
    }
}

/// a vector register used in the Xenon VMX128 instructions. unlike `VR` with a standard 32
/// vector register file, `VR128` describes the extended Xenon 128 vector register file.
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)]
pub struct VR128 {
    reg: u8,
}

impl VR128 {
    /// create a struct describing higher-number `vr<N>` from VMX128.
    ///
    /// panics if `reg` is not a valid register number (e.g. is greater than 127)
    pub fn new(reg: u8) -> Self {
        assert!(reg < 128);

        Self { reg }
    }

    /// get the register number for this VMX128 register.
    ///
    /// this register form is only present in VMX128-only instructions. Xenon,
    /// the only ISA with the VMX128 extension, results in `VR` operands for
    /// typical AltiVec instructions, and `VR128` operands for the
    /// VMX128-specific additions.
    pub fn num(&self) -> u8 {
        self.reg
    }
}

/// the Gekko-specific Graphics Quantization Registers involved in paired-singles load/store
/// operations.
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)]
pub struct GQR {
    reg: u8,
}

impl GQR {
    /// create a struct describing `qr<N>`.
    ///
    /// panics if `reg` is not a valid register number (e.g. is greater than 7)
    pub fn new(reg: u8) -> Self {
        assert!(reg < 8);

        Self { reg }
    }

    /// get the register number for this Graphics Quantization Register.
    pub fn num(&self) -> u8 {
        self.reg
    }
}

#[non_exhaustive]
#[allow(missing_docs)]
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
pub enum Operand {
    /// the control field for the behavior of a `sync` instruction.
    LL(SyncControl),

    /// a register in a Power ISA processor.
    Register(Register),

    /// a register in a Power ISA processor, or the value "0" if selecting r0.
    RegisterOrZero(Register),

    /// a memory access at `base` offset by `disp`.
    ///
    /// if `base` is `r0`, then the index is the address formed by sign-extending `disp` to address
    /// width.
    ///
    /// this is not descriptive of all POWER memory accesses; other accesses may be a pair of
    /// Register/RegisterOrZero
    MemoryDisp { base: Register, disp: i16 },

    /// typically a memory address at `base + index`.
    ///
    /// if `base` is `r0`, then the index is the literal value `0` rather than the value in `r0`.
    /// some instructions compute addresses with this operand rather than access memory, so the
    /// operand alone does not determine if an instruction does or does not perform a load/store.
    MemoryIndexed { base: Register, index: Register },

    /// a floating-point register in a Power ISA processor.
    FPR(FPR),

    /// a special-purpose register in a Power ISA processor.
    SPR(SPR),

    /// a vector register (either from `AltiVec` or later Power ISA versions which incorporate it)
    VR(VR),

    /// a vector register used in VMX128 instructions (as wide as [`VR`], but 128 of these rather than 32.)
    VR128(VR128),

    /// a graphics quantization register in a Gekko or Broadway processor.
    GQR(GQR),

    /// an immediate, sign-extended to the size of a GPR.
    ImmExts16(i16),

    /// an immediate 16-bit value, zero-extended to the size of a GPR.
    ImmU16(u16),

    /// an immediate 32-bit value.
    ImmU32(u32),

    /// the absolute address of a branch target, sign-extended to the size of PC.
    ///
    /// note that PC may be 32 bits in some implementations, or 64 bits in others.
    BranchAbs(i32),

    /// a relative branch offset, sign-extended to the size of PC.
    ///
    /// note that PC may be 32 bits in some implementations, or 64 bits in others.
    BranchOffset(i32),

    /// an immediate 16-bit value, sign-extended as a relative branch offset.
    ///
    /// this 16-bit value may be furhter sign-extended to 32 bits and handled in the same manner as
    /// `BranchOffset`, above.
    BranchOffset16(i16),

    /// an immediate 8-bit value.
    ///
    /// this is probably some kind of configuration value for the operation of an instruction,
    /// rather than a literal used as-encoded.
    Imm8(u8),

    /// a field in the `CR` special-purpose register.
    ///
    /// this references any of the 8 four-bit fields in the condition register.
    CrField(u8),

    /// a mask of fields in the Condition Register which are operated on.
    ///
    /// this is only produced as an operand in `mfocrf` or `mtocrf`. it can be converted to an
    /// equivalent `CrField`, but is a distinct type as those instructions have this operand
    /// assembled from and disassembled to a plain integer.
    CrMask(u8),

    /// a field in the `FPSCR` (Floating-Point Status and Control Register) special-purpose
    /// register.
    ///
    /// the Power ISA FPU has a distinct status and control register, `fpscr`, distinct from the
    /// scalar condition register `cr`. some instructions address individual `fpscr` fields with
    /// this operand type.
    FpscrField(u8),

    /// a single bit in the `CR` special-purpose register.
    ///
    /// this is one bit in an enclosing "CR field".
    CrBit(u8),

    /// a D3D pixel format selected as part of a VMX128 pixel pack/unpack operation.
    PixelFormat(PixelFormat),
}

/// one of the eight possible pixel formats that can be an indicated in a VMX128 d3d operation.
#[allow(missing_docs)] // HELP: if you know what these mean, fill in docs here! jeez..
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)]
pub enum PixelFormat {
    D3dColor = 0,
    NormShort2 = 1,
    NormPacked32 = 2,
    Float16_2 = 3,
    NormShort4 = 4,
    Float16_4 = 5,
    NormPacked64 = 6,
    /// the pixel format specified by the immediate encoding `0b111` is unknown to me!
    Format7 = 7,
}

impl PixelFormat {
    fn from_u8(u: u8) -> Self {
        static FORMATS: &'static [PixelFormat; 8] = &[
            PixelFormat::D3dColor, PixelFormat::NormShort2,
            PixelFormat::NormPacked32, PixelFormat::Float16_2,
            PixelFormat::NormShort4, PixelFormat::Float16_4,
            PixelFormat::NormPacked64, PixelFormat::Format7,
        ];
        FORMATS[u as usize]
    }
}

#[allow(missing_docs)]
impl Operand {
    /// reinterpret this operand as a [`SyncControl`].
    ///
    /// this will only not panic if the corresponding instruction is [`Opcode::SYNC`] (more
    /// precisely: this will panic iff the operand is not `Operand::LL`. `Operand::LL` is only
    /// produced if the opcode is `sync`.)
    pub fn as_ll(&self) -> Option<SyncControl> {
        let Operand::LL(ctrl) = self else {
            return None;
        };

        Some(*ctrl)
    }

    pub fn as_ll_unwrap(&self) -> SyncControl {
        self.as_ll().unwrap()
    }

    /// reinterpret this operand as a general-purpose register, `Register`.
    pub fn as_reg(&self) -> Option<Register> {
        let Operand::Register(reg) = self else {
            return None;
        };

        Some(*reg)
    }

    pub fn as_reg_unwrap(&self) -> Register {
        self.as_reg().unwrap()
    }

    /// reinterpret this operand as a `Register`, with the additional meaning that this operand has
    /// a value of 0 if the `Register` would be `r0`.
    ///
    /// generally, this operand is an index or offset, where "0" is intended to mean "this operand
    /// does not contribute to an effective address calculation".
    pub fn as_reg_or_zero(&self) -> Option<Register> {
        let Operand::RegisterOrZero(reg) = self else {
            return None;
        };

        Some(*reg)
    }

    pub fn as_reg_or_zero_unwrap(&self) -> Register {
        self.as_reg_or_zero().unwrap()
    }

    /// reinterpret this operand as a memory access to the given register and offset.
    ///
    /// this does not describe all memory operands, but `MemoryDisp` and `MemoryIndexed` together
    /// cover most.
    pub fn as_mem_disp(&self) -> Option<(Register, i16)> {
        let Operand::MemoryDisp { base, disp } = self else {
            return None;
        };

        Some((*base, *disp))
    }

    pub fn as_mem_disp_unwrap(&self) -> (Register, i16) {
        self.as_mem_disp().unwrap()
    }

    /// reinterpret this operand as an indexed access (probably) to `base + reg`.
    ///
    /// this does not describe all memory operands; register-indexed memory accesses in particular
    /// are not reflected here.
    pub fn as_mem_indexed(&self) -> Option<(Register, Register)> {
        let Operand::MemoryIndexed { base, index } = self else {
            return None;
        };

        Some((*base, *index))
    }

    pub fn as_mem_indexed_unwrap(&self) -> (Register, Register) {
        self.as_mem_indexed().unwrap()
    }

    /// reinterpret this operand as a floating-point register.
    pub fn as_fpr(&self) -> Option<FPR> {
        let Operand::FPR(fpr) = self else {
            return None;
        };

        Some(*fpr)
    }

    pub fn as_fpr_unwrap(&self) -> FPR {
        self.as_fpr().unwrap()
    }

    /// reinterpret this operand as a special-purpose register.
    ///
    /// there are some common special-purpose registers across POWER implementations, but
    /// ultimately the specifics of what special-purpose register numbers refer to is a
    /// per-processor question.
    pub fn as_spr(&self) -> Option<SPR> {
        let Operand::SPR(spr) = self else {
            return None;
        };

        Some(*spr)
    }

    pub fn as_spr_unwrap(&self) -> SPR {
        self.as_spr().unwrap()
    }

    /// reinterpret this operand as a vector register as used in VMX instructions.
    pub fn as_vr(&self) -> Option<VR> {
        let Operand::VR(vr) = self else {
            return None;
        };

        Some(*vr)
    }

    pub fn as_vr_unwrap(&self) -> VR {
        self.as_vr().unwrap()
    }

    /// reinterpret this operand as a vector register in a VMX128 instruction, addressing the wider
    /// 128-wide vector register file.
    pub fn as_vr128(&self) -> Option<VR128> {
        let Operand::VR128(vr) = self else {
            return None;
        };

        Some(*vr)
    }

    pub fn as_vr128_unwrap(&self) -> VR128 {
        self.as_vr128().unwrap()
    }

    /// reinterpret this operand as a Gekko/Broadway Graphics Quantization Register, used in some
    /// paired-single load/store operations.
    pub fn as_gqr(&self) -> Option<GQR> {
        let Operand::GQR(gqr) = self else {
            return None;
        };

        Some(*gqr)
    }

    pub fn as_gqr_unwrap(&self) -> GQR {
        self.as_gqr().unwrap()
    }

    /// reinterpret this operand as a bitmask describing CR fields.
    ///
    /// this operand is only used in `mtocrf` and `mfocrf`, where the "o" means "one", meaning the
    /// returned bitmask has only one bit set reflecting one field in the condition register.
    pub fn as_cr_mask(&self) -> Option<u8> {
        let Operand::CrMask(cr) = self else {
            return None;
        };

        Some(*cr)
    }

    pub fn as_cr_mask_unwrap(&self) -> u8 {
        self.as_cr_mask().unwrap()
    }

    /// reinterpret this operand as a CR field.
    ///
    /// the referenced condition register field indicates any of cr0 through to cr7.
    pub fn as_cr_field(&self) -> Option<u8> {
        let Operand::CrField(cr) = self else {
            return None;
        };

        Some(*cr)
    }

    pub fn as_cr_field_unwrap(&self) -> u8 {
        self.as_cr_field().unwrap()
    }

    /// reinterpret this operand as a single bit in a CR field.
    ///
    /// the bit is relative to the start of the condition register, which indicates both which of
    /// the eight CR fields and which of the four bits is referenced in the selected CR field.
    pub fn as_cr_bit(&self) -> Option<u8> {
        let Operand::CrBit(i) = self else {
            return None;
        };

        Some(*i)
    }

    pub fn as_cr_bit_unwrap(&self) -> u8 {
        self.as_cr_bit().unwrap()
    }

    /// reinterpret this operand as an FPSCR field.
    pub fn as_fpscr_field(&self) -> Option<u8> {
        let Operand::FpscrField(cr) = self else {
            return None;
        };

        Some(*cr)
    }

    pub fn as_fpscr_field_unwrap(&self) -> u8 {
        self.as_fpscr_field().unwrap()
    }

    /// reinterpret this operand as a Xenon D3D instruction pixel format.
    pub fn as_pixel_format(&self) -> Option<PixelFormat> {
        let Operand::PixelFormat(pf) = self else {
            return None;
        };

        Some(*pf)
    }

    pub fn as_pixel_format_unwrap(&self) -> PixelFormat {
        self.as_pixel_format().unwrap()
    }

    /// reinterpret this operand as an absolute branch destination.
    ///
    /// see the documentation on `Operand::BranchAbs` for advice on how to interpret this operand.
    pub fn as_branch_absolute(&self) -> Option<i32> {
        let Operand::BranchAbs(dest) = self else {
            return None;
        };

        Some(*dest)
    }

    pub fn as_branch_absolute_unwrap(&self) -> i32 {
        self.as_branch_absolute().unwrap()
    }

    /// reinterpret this operand as a branch offset.
    ///
    /// see the documentation on `Operand::BranchOffset` for advice on how to interpret this operand.
    pub fn as_branch_offset(&self) -> Option<i32> {
        let Operand::BranchOffset(offs) = self else {
            return None;
        };

        Some(*offs)
    }

    pub fn as_branch_offset_unwrap(&self) -> i32 {
        self.as_branch_offset().unwrap()
    }

    /// reinterpret this operand as a 16-bit branch offset.
    ///
    /// see the documentation on `Operand::BranchOffset16` for advice on how to interpret this operand.
    pub fn as_branch_offset16(&self) -> Option<i16> {
        let Operand::BranchOffset16(offs) = self else {
            return None;
        };

        Some(*offs)
    }

    pub fn as_branch_offset16_unwrap(&self) -> i16 {
        self.as_branch_offset16().unwrap()
    }

    /// reinterpret this operand as a 32-bit unsigned immediate.
    pub fn as_imm32(&self) -> Option<u32> {
        let Operand::ImmU32(i) = self else {
            return None;
        };

        Some(*i)
    }

    pub fn as_imm32_unwrap(&self) -> u32 {
        self.as_imm32().unwrap()
    }

    /// reinterpret this operand as a 16-bit signed immediate.
    ///
    /// if the underlying value was smaller (such as the 5-bit immediate in many instructions), it
    /// was sign-extended up to 16 bits as part of decoding.
    pub fn as_signed_imm(&self) -> Option<i16> {
        let Operand::ImmExts16(i) = self else {
            return None;
        };

        Some(*i)
    }

    pub fn as_signed_imm_unwrap(&self) -> i16 {
        self.as_signed_imm().unwrap()
    }

    /// reinterpret this operand as a 16-bit unsigned immediate.
    pub fn as_imm16(&self) -> Option<u16> {
        let Operand::ImmU16(i) = self else {
            return None;
        };

        Some(*i)
    }

    pub fn as_imm16_unwrap(&self) -> u16 {
        self.as_imm16().unwrap()
    }

    /// reinterpret this operand as an 8-bit unsigned immediate.
    pub fn as_imm8(&self) -> Option<u8> {
        let Operand::Imm8(i) = self else {
            return None;
        };

        Some(*i)
    }

    pub fn as_imm8_unwrap(&self) -> u8 {
        self.as_imm8().unwrap()
    }
}

#[allow(non_camel_case_types)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
enum Format {
    // FP Single/Double
    A,
    D,
    Ddot,
    // an invented alternate to `D` for D-form instructions that access memory.
    Dmem,
    DS,
    // Embedded? but in 2.07 with VSE and all..
    E,
    I,
    M,
    MD,
    MDS,
    B,
    SC,
    VX,
    VC,
    VCdot,
    X,
    XS,
    XL,
    XFL,
    XO,
    XFX,
    Z22,
    Z23,
    PS_LST,
}

#[derive(Copy, Clone, PartialEq, Eq)]
enum PrimaryLookup {
    Illegal,
    // parts of decode space reserved for implementations. this complicates "all" architecture
    // decoding..
    ImplementationDefined,
    Undefined,
    Opc(Opcode, Format),
    Extension(&'static ExtensionTable),
    // handling major opcode 4 is not amenable to table lookup. the arm for this is more or less
    // open-coded with some tables involved. in particular, it is either the opcode for SPE (signal
    // processing extensions) or AltiVec/VMX/VSX vector extensions.
    Opc4,
    // opcodes 5 and 6 are used in VMX128, but not others.
    Opc5,
    Opc6,
    // these opcodes mostly are Gekko-only, but 0x38 overlaps with LFQ, present on other processors.
    Opc38,
    Opc39,
    Opc3C,
    Opc3D,
}

impl fmt::Debug for PrimaryLookup {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            PrimaryLookup::Illegal => {
                f.write_str("illegal opcode")
            }
            PrimaryLookup::ImplementationDefined => {
                f.write_str("implementation defined")
            },
            PrimaryLookup::Undefined => {
                f.write_str("undefined")
            },
            PrimaryLookup::Opc(opc, fmt) => {
                write!(f, "opcode={:?}, format={:?}", opc, fmt)
            },
            PrimaryLookup::Extension(_table) => {
                f.write_str("extension opcode")
            },
            PrimaryLookup::Opc4 => {
                f.write_str("opcode 4 extension space")
            },
            PrimaryLookup::Opc5 => {
                f.write_str("opcode 5 extension space (vmx128)")
            },
            PrimaryLookup::Opc6 => {
                f.write_str("opcode 6 extension space (vmx128)")
            },
            PrimaryLookup::Opc38 => {
                f.write_str("opcode 0x38")
            },
            PrimaryLookup::Opc39 => {
                f.write_str("opcode 0x39")
            },
            PrimaryLookup::Opc3C => {
                f.write_str("opcode 0x3c")
            },
            PrimaryLookup::Opc3D => {
                f.write_str("opcode 0x3d")
            },
        }
    }
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
struct OpcodeRecord {
    lookup: PrimaryLookup,
    isa_constraint: IsaConstraint,
    category: Category,
}

macro_rules! invalid {
    () => {
        Some(OpcodeRecord {
            lookup: PrimaryLookup::Illegal,
            isa_constraint: IsaConstraint::All,
            category: Category::B,
        })
    };
    ($const:ident) => {
        Some(OpcodeRecord {
            lookup: PrimaryLookup::Illegal,
            isa_constraint: IsaConstraint::$const,
            category: Category::B,
        })
    };
}

macro_rules! opc {
    ($opc:ident, $format:ident) => {
        Some(OpcodeRecord {
            lookup: PrimaryLookup::Opc(Opcode::$opc, Format::$format),
            isa_constraint: IsaConstraint::All,
            category: Category::B,
        })
    };
    ($opc:ident, $format:ident, $constraint:ident) => {
        Some(OpcodeRecord {
            lookup: PrimaryLookup::Opc(Opcode::$opc, Format::$format),
            isa_constraint: IsaConstraint::$constraint,
            category: Category::B,
        })
    };
}

// PPC: instructions defined by PowerPC (ref: `SR28-5124-01_The_PowerPC_Architecture_May94.pdf`)
//
// some weird details incldue, "has `fres`, does not have `fre`". for another example, see
// `frsqrtes`, which presumably is 2.01 later or ...something?
macro_rules! ppc {
    ($opc:ident, $format:ident) => {
        opc!($opc, $format, Ppc)
    };
}

macro_rules! p202 {
    ($opc:ident, $format:ident) => {
        opc!($opc, $format, PowerIsa202)
    };
}
macro_rules! p203 {
    ($opc:ident, $format:ident) => {
        opc!($opc, $format, PowerIsa203)
    };
}
macro_rules! p206 {
    ($opc:ident, $format:ident) => {
        opc!($opc, $format, PowerIsa206)
    };
}

macro_rules! p207 {
    ($opc:ident, $format:ident) => {
        opc!($opc, $format, PowerIsa207)
    };
}

// DFP: Decimal Floating-Point
macro_rules! dfp {
    ($opc:ident, $format:ident) => {
        opc!($opc, $format, DFP)
    };
}
macro_rules! dfp25 {
    ($opc:ident, $format:ident) => {
        opc!($opc, $format, DFP25)
    };
}
macro_rules! dfp26 {
    ($opc:ident, $format:ident) => {
        opc!($opc, $format, DFP26)
    };
}
macro_rules! dfp27 {
    ($opc:ident, $format:ident) => {
        opc!($opc, $format, DFP27)
    };
}
// DFPQA: Decimal Floating-Point Quantum Adjust, defined in Power ISA v2.05
macro_rules! dfpqa {
    ($opc:ident, $format:ident) => {
        opc!($opc, $format, DFPQA)
    };
}

#[derive(Copy, Clone, PartialEq, Eq)]
struct ExtensionTable {
    records: &'static [Option<OpcodeRecord>],
    bit_offset: u8,
    bit_count: u8,
}

impl ExtensionTable {
    fn extract_idx(&self, word: u32) -> usize {
        let mask = (1 << self.bit_count) - 1;
        ((word >> self.bit_offset) & mask) as usize
    }
}

// in at least Power ISA v3.0 (`PowerISA_public.v3.0B.pdf`), appendix D
// > Power ISA Instruction Set Sorted by Opcode
// includes a helpful column "Version" enumerating what versions each still-in-v3.00 instruction
// was added.
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
enum IsaConstraint {
    // PowerPC "AS" and later
    //
    // this is a handful of adjustments on PowerPC that were made for the Amazon processors in
    // some versions of the AS/400 as well as later IBM i systems. namely, this is morally similar
    // to "PowerPC + Slb"
    // PowerPC_AS,

    // PowerPC "P" and later
    //
    // this is effectively just "PowerPC everywhere else", so it's not actually been used below.
    // PowerPC_P,

    // only in original POWER (RS/6000)
    // .. no instructions here, afaik.
    //
    POWER,

    // only in POWER2 (not POWER, not PowerPC?)
    POWER2,
    // POWER2
    LE_POWER2,

    // Power V2.00
    //
    // this generally matches with the Power Amazon processors in the RS/6000.
    PowerIsa200,

    // Power V2.02
    //
    // this controls relatively few details, but bclr/bcctr/etc use of the BH field to control
    // prediction hints were added somewhere between PowerPC 1994 and v2.02.
    PowerIsa202,

    // Power V2.03
    //
    // in many cases this added new instructions, but for some instructions (DBCT, DBCTST) being at
    // this version or later implies an additional hint operand.
    PowerIsa203,

    // Power V2.05
    PowerIsa205,

    // Power V2.06
    PowerIsa206,

    // implementations including an FPU.
    //
    // this is generally all of them, but notably the e500 did not.
    Fpu,

    // implementations including an FPU that supports single-precision floats.
    //
    // notably RIOS did not support single-precision floats, and they only came along in PowerPC?
    // this surprising fact brought to you by the RS/6000 Hardware Technical Reference not
    // describing any instruction under opcode 59 and objdump agreeing with it.
    FpuNoSingle,

    // support for the `isel` instruction.
    //
    // the support of `isel` is *curious*. and certainly not mainline until POWER7 aka v2.06.
    Isel,

    // BTB locking instructions
    //
    // these are BBLELS and BBELR, which (so far as I know) only existed on PowerPC e500
    Btb,

    // SLB management instructions
    //
    // these are SLBIE and SLBIA, which are optional as of the 1994 PowerPC manual, but not
    // described as optional in v2.02 or later. Xenon does not include them.
    Slb,

    // segment register manipulation instructions
    //
    // these are MTSR, MTSRIN, MFSR, MFSRIN, as well as the removed MTSRD, MTSRDIN. this set of
    // instructions became optional with PowerPC. Xenon and Gekko (+Broadway) opts to not have them.
    Seg,

    // external control instructions
    //
    // these are part of the Virtual ISA, and some processors (Xenon) do not care about them.
    Ec,

    // instructions to return from interrupts.
    //
    // these are RFI, RFID. they are seemingly in AS and PowerPC, but not Xenon?
    Rfi,

    // TLB management instructions
    //
    // these are TLBIE, TLBIA, TLBSYNC. these are part of the Virtual ISA, and some processors
    // (Xenon) do not care about them.
    Tlb,

    // software TLB management instructions
    //
    // the extact TLB management instructions vary across (especially earlier) Power ISA
    // processors. TlbSw is a subset of instructions used in e200, e500, etc for TLB management
    // which were less common. these are generally derived from the AIX 5.3 manual as well.
    TlbSw,

    // only in Power implementations for "embedded" uses
    //
    // this was an option in v2.03 and later implementations, but removed by Power ISA v3.0.
    //
    // some instructiosn in Embedded overlap with non-embedded instructions, so for IsaVersion that
    // overlap with this support it's possible that were previously ignored fields are instead used
    // to pick alternate opcodes. instructions *were* in Embedded, removed, and not interpreted in
    // a different way later, are reported as their Embedded form under IsaVersion::All
    Embedded,

    // only in Power implementations which include AltiVec (Motorola G4, Cell, Xenon)
    //
    // Xenon is interesting in that it supports AltiVec instructions *and* VMX128, below.
    //
    // TODO: this is assumed to be equivalent to the Vector category in v2.06+
    AltiVec,

    // only in implementations which include AltiVec, but before AltiVec was made part of the Power
    // ISA.
    //
    // the relevant instructions here are described in
    //
    // > ALTIVECPOWERISAPEM, AltiVec Technology Programming Environments Manual
    //
    // and encompass `dss`, `dssall`, `dst`, `dstst`, and `dststt`.
    AltiVec_NonPower,

    // only in Power implementations which include AltiVec, but removed in v2.06
    //
    // TODO: does Cell support these? probably...
    AltiVec_LE206,

    // Xenon
    VMX128,

    // a handful of cases where at least xenia goes beyond the manual (see `sc`), unsure what
    // hardware actually supports or not.
    Xenon,

    // and in even fewer cases, an instruction is known to Xenia and at least RPCS3 so it is
    // presumably in Cell *and* Xenon, but not documented in any manuals.
    CellOrXenon,

    // outside opc4, a few instructions are either gekko-only or have bytes interpreted differently
    // by gekko/broadway
    Gekko,

    // only in Power implementations which extended VMX to VSX (Power >=v2.06, POWER7)
    VSX,

    // > LMA: Legacy Integer Multiply-Accumulate
    LMA,

    // Power implementations that include Decimal Floating-Point (Power >=v2.05, POWER6)
    //
    // some later implementations extend the set of instructions in Decimal Floating-Point, which
    // have later constraints.
    DFP,
    // Decimal Floating-Point Quantum Adjustment operations (Power >=v2.05)
    DFPQA,
    // Decimal Floating-Point additions for Power 2.05. instructions that are not part of specific
    // subdivisions of DFP but added in 2.05 all the same.
    DFP25,
    // Decimal Floating-Point additions for Power 2.06. among other things, this includes..
    // extended rounding mode control? see `drrnd` for one example.
    DFP26,
    // Decimal Floating-Point additions for Power 2.07
    DFP27,

    // PowerPC ("AP" in v2.01)
    //
    // notably, Gekko is here (as is Broadway)
    Ppc,

    // "Category: 64-bit", added in Power v2.06, but Xenia decodes these so i infer these were
    // likely present in other 64-bit PowerPC implementations..? Power v2.06 only has LDBRX/STDBRX
    // here.
    //
    // there *are* other 64-bit only instructions though, ldu, std, stdu, ...
    Ppc64,
    // Power ISA v2.07
    PowerIsa207,
    // Power ISA v3.0
    PowerIsa30,
    // actually all POWER revisions to date.
    All,
}

// "AltiVec™ Technology Programming Enviroments Manual, Rev. 3", `ALTIVECPEM.pdf`
// (`sha256: c16686d07e509cf059d9d11d7c3c807dea259497fc74d8fced41846f865406cd`) includes Appendix
// A.6 Instruction Set Legend, which describes which of UISA or VEA various instructions are in.
//
// UISA: .. all of them except
// VEA: DSS, DSSALL, DSTST, DSTSTT, DSTT

// appendix I. Opcode Maps in Power 2.01 document (ppc_isa.isa, sha256: TODO)
// describes what machines are applicable for a given definition with the following notation:
// * A: PowerPC
// * P: PowerPC
// * 2: POWER2
// * All: All of the above
//
// future manuals instead use this section of the opcode map to declare instruction *categories*
// which are defined in section `1.3.5 Categories`. reproduced below:
//
// * B: Base  ; Required for all implementations
// * S: Server ; Required for Server implementations
// * E: Embedded : Required for Embedded implementations
// * ATB: Alternate Time Base ; An additional Time Base; see Book II
// * CS: Cache Specification ; Specify a specific cache for some instructions; see Book II
// * DFP: Decimal Floating-Point ; Decimal Floating-Point facilities
// * DS: Decorated Storage ; Decorated Storage facilities
// * EMB: Elemental Memory Barriers ; More granular memory barrier support
// * E.CD: Embedded.Cache Debug ; Provides direct access to cache data and directory content
// * E.CI: Embedded.Cache Initialization ; Instructions that invalidate the entire cache
// * E.DC: Embedded.Device Control ; Legacy Device Control bus support
// * E.ED: Embedded.Enhanced Debug ; Embedded Enhanced Debug facility; see Book III-E
// * E.PD: Embedded.External PID ; Embedded External PID facility; see Book III-E
// * E.HV: Embedded.Hypervisor ; Embedded Logical Partitioning and hypervisor facilities
// * E.HV.LRAT: Embedded.Hypervisor.LRAT ; Embedded Hypervisor Logical to Real Address Translation facility; see Book III-E
// * E.LE: Embedded.Little-Endian ; Embedded Little-Endian page attribute; see Book III-E
// * E.MMUF: Embedded.MMU Type FSL ; Type FSL Storage Control
// * E.PT: Embedded.Page Table ; Embedded Page Table facility; see Book III-E
// * E.TWC: Embedded.TLB Write Conditional ; Embedded TLB Write Conditional facility; see Book III-E
// * E.PM: Embedded.Performance Monitor ; Embedded Performance Monitor example; see Book III-E
// * E.PC: Embedded.Processor Control ; Embedded Processor Control facility; see Book III-E
// * ECL: Embedded Cache Locking ; Embedded Cache Locking facility; see Book III-E
// * EM: Embedded Multi-Threading ; Embedded Multi-Threading; see Book III-E
// * EM.TM: Embedded Multi-Threading.Thread Management ; Embedded Multi-Threading Thread Management Facility
// * EC: External Control ; External Control facility; see Book II
// * EXP: External Proxy ; External Proxy facility; see Book III-E
// * FP: Floating-Point ; Floating-Point Facilities
// * FP: Floating-Point.Record ; Floating-Point instructions with Rc=1
// * LMA: Legacy Integer Multiply-Accumulate1 ; Legacy Integer Multiply-accumulate instructions
// * LMV: Legacy Move Assist ; Determine Left most Zero Byte instruction
// * LSQ: Load/Store Quadword ; Load/Store Quadword instructions; see Book III-S
// * MMC: Memory Coherence ; Requirement for Memory Coherence; see Book II
// * MA: Move Assist ; Move Assist instructions
// * PCR: Processor Compatibility ; Processor Compatibility Register
// * S.PM: Server.Performance Monitor ; Server Performance Monitor example; see Book III-S
// * S.PC: Server.Processor Control ; Server Processor Control Facility; see Book III-S
// * S.RPTA: Server.Relaxed Page Table Alignment ; HTAB alignment on 256 KB boundary; see Book III-S
// * SP: Signal Processing Engine1, 2 ; Facility for signal processing
// * SP.FD: SPE.Embedded Float Scalar Double ; GPR-based Floating-Point double-precision instruction set
// * SP.FS: SPE.Embedded Float Scalar Single ; GPR-based Floating-Point single-precision instruction set
// * SP.FV: SPE.Embedded Float Vector ; GPR-based Floating-Point Vector instruction set
// * SCPM: Store Conditional Page Mobility ; Store Conditional accounting for page movement; see Book II
// * STM: Stream ; Stream variant of dcbt instruction; see Book II
// * SAO: Strong Access Order ; Assist for X86 and Sparc emulation; see Book II
// * TRC: Trace ; Trace Facility ; see Book III-S
// * TM: Transactional Memory ; Full hardware Transactional Memory support
// * VLE: Variable Length Encoding ; Variable Length Encoding facility; see Book VLE
// * VSX: Vector-Scalar Extension ; Vector-Scalar Extension. Requires implementation of Floating-Point and Vector categories
// * V: Vector1 ; Vector facilities
// * V.LE: Vector.Little-Endian ; Little-Endian support for Vector storage operations.
// * V.GBB: Vector.Crypto ; Cryptographic instruction set
// * V.XOR: Vector.RAID ; Vector Permute-XOR instruction
// * WT: Wait ; wait instruction; see Book II
// * 64: 64-Bit ; Required for 64-bit implementations; not defined for 32-bit impl’s
//
// further, there are categories "Phased-In" and "Phased-Out" which are apparently not reported in opcode maps..?
//
// TODO: Category has since gone unused, and `IsaConstraint` has partially taken its place. the two
// should get rethought, probably?
#[allow(non_camel_case_types)]
#[allow(dead_code)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
enum Category {
    // Base  ; Required for all implementations
    B,
    // Server ; Required for Server implementations
    S,
    // Embedded : Required for Embedded implementations
    E,
    // Alternate Time Base ; An additional Time Base; see Book II
    ATB,
    // Cache Specification ; Specify a specific cache for some instructions; see Book II
    CS,
    // Decimal Floating-Point ; Decimal Floating-Point facilities
    DFP,
    // Decorated Storage ; Decorated Storage facilities
    DS,
    // Elemental Memory Barriers ; More granular memory barrier support
    EMB,
    // Embedded.Cache Debug ; Provides direct access to cache data and directory content
    E_CD,
    // Embedded.Cache Initialization ; Instructions that invalidate the entire cache
    E_CI,
    // Embedded.Device Control ; Legacy Device Control bus support
    E_DC,
    // Embedded.Enhanced Debug ; Embedded Enhanced Debug facility; see Book III-E
    E_ED,
    // Embedded.External PID ; Embedded External PID facility; see Book III-E
    E_PD,
    // Embedded.Hypervisor ; Embedded Logical Partitioning and hypervisor facilities
    E_HV,
    // Embedded.Hypervisor.LRAT ; Embedded Hypervisor Logical to Real Address Translation facility; see Book III-E
    E_HV_LRAT,
    // Embedded.Little-Endian ; Embedded Little-Endian page attribute; see Book III-E
    E_LE,
    // Embedded.MMU Type FSL ; Type FSL Storage Control
    E_MMUF,
    // Embedded.Page Table ; Embedded Page Table facility; see Book III-E
    E_PT,
    // Embedded.TLB Write Conditional ; Embedded TLB Write Conditional facility; see Book III-E
    E_TWC,
    // Embedded.Performance Monitor ; Embedded Performance Monitor example; see Book III-E
    E_PM,
    // Embedded.Processor Control ; Embedded Processor Control facility; see Book III-E
    E_PC,
    // Embedded Cache Locking ; Embedded Cache Locking facility; see Book III-E
    ECL,
    // Embedded Multi-Threading ; Embedded Multi-Threading; see Book III-E
    EM,
    // Embedded Multi-Threading.Thread Management ; Embedded Multi-Threading Thread Management Facility
    EM_TM,
    // External Control ; External Control facility; see Book II
    EC,
    // External Proxy ; External Proxy facility; see Book III-E
    EXP,
    // Floating-Point ; Floating-Point Facilities
    FP,
    // Floating-Point.Record ; Floating-Point instructions with Rc=1
    FP_R,
    // Legacy Integer Multiply-Accumulate1 ; Legacy Integer Multiply-accumulate instructions
    LMA,
    // Legacy Move Assist ; Determine Left most Zero Byte instruction
    LMV,
    // Load/Store Quadword ; Load/Store Quadword instructions; see Book III-S
    LSQ,
    // Memory Coherence ; Requirement for Memory Coherence; see Book II
    MMC,
    // Move Assist ; Move Assist instructions
    MA,
    // Processor Compatibility ; Processor Compatibility Register
    PCR,
    // Server.Performance Monitor ; Server Performance Monitor example; see Book III-S
    S_PM,
    // Server.Processor Control ; Server Processor Control Facility; see Book III-S
    S_PC,
    // Server.Relaxed Page Table Alignment ; HTAB alignment on 256 KB boundary; see Book III-S
    S_RPTA,
    // Signal Processing Engine1, 2 ; Facility for signal processing
    SP,
    // SPE.Embedded Float Scalar Double ; GPR-based Floating-Point double-precision instruction set
    SP_FD,
    // SPE.Embedded Float Scalar Single ; GPR-based Floating-Point single-precision instruction set
    SP_FS,
    // SPE.Embedded Float Vector ; GPR-based Floating-Point Vector instruction set
    SP_FV,
    // Store Conditional Page Mobility ; Store Conditional accounting for page movement; see Book II
    SCPM,
    // Stream ; Stream variant of dcbt instruction; see Book II
    STM,
    // Strong Access Order ; Assist for X86 and Sparc emulation; see Book II
    SAO,
    // Trace ; Trace Facility ; see Book III-S
    TRC,
    // Transactional Memory ; Full hardware Transactional Memory support
    TM,
    // Variable Length Encoding ; Variable Length Encoding facility; see Book VLE
    VLE,
    // Vector-Scalar Extension ; Vector-Scalar Extension. Requires implementation of Floating-Point and Vector categories
    VSX,
    // Vector1 ; Vector facilities
    V,
    // Vector.Little-Endian ; Little-Endian support for Vector storage operations.
    V_LE,
    // Vector.Crypto ; Cryptographic instruction set
    V_GBB,
    // Vector.RAID ; Vector Permute-XOR instruction
    V_XOR,
    // Wait ; wait instruction; see Book II
    WT,
    // 64-Bit ; Required for 64-bit implementations; not defined for 32-bit impl’s
    B_64,
}

static CR_OPS: ExtensionTable = ExtensionTable {
    bit_offset: 1,
    bit_count: 10,
    records: &[
        // 0x0_00
        opc!(MCRF, XL), None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_10
        opc!(BCLR, XL), None, opc!(RFID, XL, Rfi), None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_20
        None, opc!(CRNOR, XL), None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_30
        None, None, opc!(RFI, XL, Rfi) /* parens */, invalid!(Ppc), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_40
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_50
        None, None, opc!(RFSVC, XL, LE_POWER2), None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_60
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_70
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_80
        None, opc!(CRANDC, XL), None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_90
        None, None, None, None, None, None, opc!(ISYNC, XL), None,
        None, None, None, None, None, None, None, None,
        // 0x0_a0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_b0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_c0
        None, opc!(CRXOR, XL), None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_d0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_e0
        None, opc!(CRNAND, XL), None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_f0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_00
        None, opc!(CRAND, XL), None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_10
        None, None, p202!(HRFID, XL), None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_20
        None, opc!(CREQV, XL), None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_30
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_40
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_50
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_60
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_70
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_80
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_90
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_a0
        None, opc!(CRORC, XL), None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_b0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_c0
        None, opc!(CROR, XL), None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_d0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_e0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_f0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_00
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_10
        opc!(BCCTR, XL), None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_20
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_30
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_40
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_50
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_60
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_70
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_80
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_90
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_a0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_b0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_c0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_d0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_e0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_f0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_00
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_10
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_20
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_30
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_40
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_50
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_60
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_70
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_80
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_90
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_a0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_b0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_c0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_d0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_e0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_f0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
    ]
};

static FX_DWD_ROT: ExtensionTable = ExtensionTable {
    bit_offset: 1,
    bit_count: 4,
    records: &[
        opc!(RLDICL, MD, Ppc64), opc!(RLDICL, MD, Ppc64), opc!(RLDICR, MD, Ppc64), opc!(RLDICR, MD, Ppc64),
        opc!(RLDIC, MD, Ppc64), opc!(RLDIC, MD, Ppc64), opc!(RLDIMI, MD, Ppc64), opc!(RLDIMI, MD, Ppc64),
        opc!(RLDCL, MDS, Ppc64), opc!(RLDCR, MDS, Ppc64), None, None,
        None, None, None, None,
    ]
};

// AltiVec OP
macro_rules! avop {
    ($opc:ident) => {
        opc!($opc, VX, AltiVec)
    };
    ($opc:ident, $format:ident) => {
        opc!($opc, $format, AltiVec)
    };
}

// VSX OP
/*
macro_rules! vop {
    ($opc:ident) => {
        opc!($opc, VX, VSX)
    };
    ($opc:ident, $format:ident) => {
        opc!($opc, $format, VSX)
    };
}
*/

macro_rules! lma {
    ($opc:ident) => {
        opc!($opc, XO, LMA)
    };
    ($opc:ident, $format:ident) => {
        opc!($opc, $format, LMA)
    };
}
// in at least Power v2.06+ the extension tables were expanded to include bit 31, which previously
// was used for Lk, Rc, etc per-instruction. the semantic of this bit is unchanged, and so for
// table concision (aka "i'm lazy") i'm leaving handling of that bit to after the table lookup.
//
// the remaining parts of `table_section == 0b00 or 0b01`. the low bit is compressed out,
// and the high bit of table section is compressed out, so only 9 bits are needed to index
// the table.
//
// in Power 2.07 and above many additional entries here become valid as VSX had many additions.
static VECTOR_LMA: ExtensionTable = ExtensionTable {
    bit_offset: 0,
    bit_count: 11,
    // the records here are an interleaving of `Table 8: (Left)` and `Table 8: (Center-Left)`
    records: &[
        // 0x0_00
        avop!(VADDUBM), avop!(VMAXUB), avop!(VRLB), avop!(VCMPEQUB, VC), avop!(VMULOUB), avop!(VADDFP), avop!(VMRGHB), avop!(VPKUHUM),
        // 0x0_20
        lma!(MULHHWU), None,         None,       None,           lma!(MACHHWU, XO), None,     None,         None,
        // 0x0_40
        avop!(VADDUHM), avop!(VMAXUH), avop!(VRLH), avop!(VCMPEQUH, VC), avop!(VMULOUH), avop!(VSUBFP), avop!(VMRGHH), avop!(VPKUWUM),
        // 0x0_60
        lma!(MULHHWW), None,         None,       None,           lma!(MACHHW, XO),  None,     lma!(NMACHHW),None,
        // 0x0_80
        avop!(VADDUWM), avop!(VMAXUW), avop!(VRLW), avop!(VCMPEQUW, VC), None, None, avop!(VMRGHW), avop!(VPKUHUS),
        // 0x0_a0
        None,          None,         None,       None,         /*TODO: "long" */  None,          None,         None,         None,
        // 0x0_c0
        None,          None,         None,       avop!(VCMPEQFP, VC), None, None, None, avop!(VPKUWUS),
        // 0x0_e0
        None,          None,         None,       None,           lma!(MACHHWS, XO), None,     /*TODO: "long" */ None,         None,
        // 0x1_00
        None,          avop!(VMAXSB), avop!(VSLB), None, avop!(VMULOSB), avop!(VREFP),     avop!(VMRGLB), avop!(VPKSHUS),
        // 0x1_20
        lma!(MULCHWU), None,         None,       None,           lma!(MACCHWU, XO),          None,         None,         None,
        // 0x1_40
        None,          avop!(VMAXSH), avop!(VSLH), None, avop!(VMULOSH), avop!(VRSQRTEFP), avop!(VMRGLH), avop!(VPKSWUS),
        // 0x1_60
        lma!(MULCHW),  None,         None,       None,           lma!(MACCHW, XO),           lma!(NMACCHW, XO),  /*TODO: "long" */ None,         None,
        // 0x1_80
        avop!(VADDCUW), avop!(VMAXSW), avop!(VSLW), None, None,          avop!(VEXPTEFP),  avop!(VMRGLW), avop!(VPKSHSS),
        // 0x1_a0
        None,          None,         None,       None,           /*TODO: "long" */None,          None,         None,         None,
        // 0x1_c0
        None,          None,         avop!(VSL),  avop!(VCMPGEFP, VC),None, avop!(VLOGEFP),   None,         avop!(VPKSWSS),
        // 0x1_e0
        None,          None,         None,       None,           lma!(MACCHWS, XO),          None,         /*TODO: "long" */ None,         None,
        // 0x2_00
        avop!(VADDUBS), avop!(VMINUB), avop!(VSRB), avop!(VCMPGTUB, VC), avop!(VMULEUB), avop!(VRFIN), avop!(VSPLTB), avop!(VUPKHSB),
        // 0x2_20
        None,          None,         None,       None,           None,          None,         None,         None,
        // 0x2_40
        avop!(VADDUHS), avop!(VMINUH), avop!(VSRH), avop!(VCMPGTUH, VC), avop!(VMULEUH), avop!(VRFIZ), avop!(VSPLTH), avop!(VUPKHSH),
        // 0x2_60
        None,          None,         None,       None,           None,          None,         None,         None,
        // 0x2_80
        avop!(VADDUWS), avop!(VMINUW), avop!(VSRW), avop!(VCMPGTUW, VC), None,          avop!(VRFIP), avop!(VSPLTW), avop!(VUPKLSB),
        // 0x2_a0
        None,          None,         None,       None,           None,          None,         None,         None,
        // 0x2_c0
        None,          None,         avop!(VSR),  avop!(VCMPGTFP, VC), None,          avop!(VRFIM), None,         avop!(VUPKLSH),
        // 0x2_e0
        None,          None,         None,       None,           None,          None,         None,         None,
        // 0x3_00
        avop!(VADDSBS), avop!(VMINSB), avop!(VSRAB),avop!(VCMPGTSB, VC), avop!(VMULESB), avop!(VCFUX), avop!(VSPLTISB), avop!(VPKPX),
        // 0x3_20
        lma!(MULLHWU), None,         None,       None,           lma!(MACLHWU, XO),          None,         None,         None,
        // 0x3_40
        avop!(VADDSHS), avop!(VMINSH), avop!(VSRAH),avop!(VCMPGTSH, VC), avop!(VMULESH), avop!(VCFSX), avop!(VSPLTISH), avop!(VUPKHPX),
        // 0x3_60
        lma!(MULLHW),  None,         None,       None,           lma!(MACLHW, XO),           None,         lma!(NMACLHW, XO),         None,
        // 0x3_80
        avop!(VADDSWS), avop!(VMINSW), avop!(VSRAW),avop!(VCMPGTSW, VC), None,          avop!(VCTUXS),avop!(VSPLTISW), None,
        // 0x3_a0
        None,          None,         None,       None,           /*TODO: "long" */ None,          None,         None,         None,
        // 0x3_c0
        None,          None,         None,       avop!(VCMPBFP, VC),  None,          avop!(VCTSXS),None,           avop!(VUPKLPX),
        // 0x3_e0
        None,          None,         None,       None,           lma!(MACLHWS, XO),          None,         /*TODO: "long" */ None,         None,
        // 0x4_00
        // DOC BUG: Power ISA manuals before V3.0 had an error in Table 8 that
        // swapped VAVGUB, VAVGUH, and VAVGUW, ordering them as VAVGUH, VAVGUW, VAVGUB. their
        // extended opcodes in the table did not match the extended opcodes in the instruction
        // descriptions, either. unfortunate.
        avop!(VSUBUBM), avop!(VAVGUB), avop!(VAND), avop!(VCMPEQUB, VCdot),
                                                                 None,          avop!(VMAXFP), avop!(VSLO), None,
        // 0x4_20
        None,          None,         None,       None,           None,          None,         None,         None,
        // 0x4_40
        avop!(VSUBUHM), avop!(VAVGUH), avop!(VANDC),avop!(VCMPEQUH, VCdot),
                                                                 None,          avop!(VMINFP), avop!(VSRO), None,
        // 0x4_60
        None,          None,         None,       None,           lma!(MACHHW /* TODO: prime */, XO),          None,         None,         None,
        // 0x4_80
        avop!(VSUBUWM), avop!(VAVGUW), avop!(VOR),  avop!(VCMPEQUW, VCdot), None,   None,         None,       None,
        // 0x4_a0
        None,          None,         None,       None,           None,          None,         None,         None,
        // 0x4_c0
        None,          None,         avop!(VXOR), avop!(VCMPEQFP, VCdot), None,   None,         None,       None,
        // 0x4_e0
        None,          None,         None,       None,           None,          None,         None,         None,
        // 0x5_00
        None,          avop!(VAVGSB), avop!(VNOR), None,           None,          None,         None,       None,
        // 0x5_20
        None,          None,         None,       None,           None,          None,         None,         None,
        // 0x5_40
        None,          avop!(VAVGSH), None,       None,           None,          None,         None,       None,
        // 0x5_60
        None,          None,         None,       None,           lma!(MACCHW /* TODO: prime */, XO),          None,         None,         None,
        // 0x5_80
        avop!(VSUBCUW), avop!(VAVGSW), None,       None,           None,          None,         None,       None,
        // 0x5_a0
        None,          None,         None,       None,           None,          None,         None,         None,
        // 0x5_c0
        None,          None,         None,       avop!(VCMPGEFP, VCdot), None,   None,         None,       None,
        // 0x5_e0
        None,          None,         None,       None,           None,          None,         None,         None,
        // 0x6_00
        avop!(VSUBUBS), None,         avop!(MFVSCR),avop!(VCMPGTUB, VCdot), avop!(VSUM4UBS),   None,         None,       None,
        // 0x6_20
        None,          None,         None,       None,           None,          None,         None,         None,
        // 0x6_40
        avop!(VSUBUHS), None,         avop!(MTVSCR),avop!(VCMPGTUH, VCdot), avop!(VSUM4SHS),   None,         None,       None,
        // 0x6_60
        None,          None,         None,       None,           None,          None,         None,         None,
        // 0x6_80
        avop!(VSUBUWS), None,         None,       avop!(VCMPGTUW, VCdot), avop!(VSUM2SWS),    None,         None,       None,
        // 0x6_a0
        None,          None,         None,       None,           None,          None,         None,         None,
        // 0x6_c0
        None,          None,         None,       avop!(VCMPGTFP, VCdot), None,              None,         None,       None,
        // 0x6_e0
        None,          None,         None,       None,           None,          None,         None,         None,
        // 0x7_00
        avop!(VSUBSBS), None,         None,       avop!(VCMPGTSB, VCdot), avop!(VSUM4SBS),    None,         None,       None,
        // 0x7_20
        None,          None,         None,       None,           None,          None,         None,         None,
        // 0x7_40
        avop!(VSUBSHS), None,         None,       avop!(VCMPGTSH, VCdot), None,              None,         None,       None,
        // 0x7_60
        None,          None,         None,       None,           lma!(MACLHW /* TODO: prime */, XO),          None,         None,         None,
        // 0x7_80
        avop!(VSUBSWS), None,         None,       avop!(VCMPGTSW, VCdot), avop!(VSUMSWS),     None,         None,       None,
        // 0x7_a0
        None,          None,         None,       None,           None,          None,         None,         None,
        // 0x7_c0
        None,          None,         None,       avop!(VCMPBFP, VCdot),  None,              None,         None,       None,
        // 0x7_e0
        None,          None,         None,       None,           None,          None,         None,         None,
    ]
};

static FX_EXTENDED_OPS: ExtensionTable = ExtensionTable {
    bit_offset: 1,
    bit_count: 10,
    records: &[
        // 0x0_00
        opc!(CMP, X), None, None, None, opc!(TW, X), None, avop!(LVSL, X), avop!(LVEBX, X),
        opc!(SUBFC, XO), opc!(MULHDU, XO, Ppc64), opc!(ADDC, XO), opc!(MULHWU, XO, Ppc), None, None, None, opc!(ISEL, A, Isel),
        // 0x0_10
        None, None, None, opc!(MFCR, XFX), opc!(LWARX, X, Ppc), opc!(LDX, X, Ppc64), None, opc!(LWZX, X),
        opc!(SLW, X), None, opc!(CNTLZW, X), opc!(SLD, X, Ppc64), opc!(AND, X), opc!(MASKG, X, LE_POWER2), None /* RLDICL* */, None,
        // 0x0_20
        opc!(CMPL, X), None, None, None, None, None, avop!(LVSR, X), avop!(LVEHX, X),
        opc!(SUBF, XO, Ppc), None, None, None, None, None, None, opc!(ISEL, A, Isel),
        // 0x0_30
        None, None, None, None, None, opc!(LDUX, X, Ppc64), opc!(DCBST, X, Ppc), opc!(LWZUX, X),
        None, None, opc!(CNTLZD, X, Ppc64), None, opc!(ANDC, X), None, None /* asterisk */ , None,
        // 0x0_40
        None, None, None, None, opc!(TD, X, Ppc64), None, None, avop!(LVEWX, X),
        None, opc!(MULHD, XO, Ppc64), None, opc!(MULHW, XO, Ppc), None, None, None, opc!(ISEL, A, Isel),
        // 0x0_50
        None, None, opc!(MTSRD, X, Seg), opc!(MFMSR, X), opc!(LDARX, X, Ppc64), None, opc!(DCBF, X, Ppc), opc!(LBZX, X),
        None, None, None, None, None, None, None /* asterisk */, None,
        // 0x0_60
        None, None, None, None, None, None, None, avop!(LVX, X),
        opc!(NEG, XO), None, None, opc!(MUL, XO, LE_POWER2), None, None, None, opc!(ISEL, A, Isel),
        // 0x0_70
        None, None, opc!(MTSRDIN, X, Seg), None, None, None, opc!(CLF, X, LE_POWER2), opc!(LBZUX, X),
        None, None, None, None, opc!(NOR, X), None, None /* asterisk */, None,
        // 0x0_80
        None, None, None, p207!(WRTEE, E /* TODO: not sure where this was added, after PPC, before 2.07? */), None, None, None, avop!(STVEBX, X),
        opc!(SUBFE, XO), None, opc!(ADDE, XO), None, None, None, p207!(MSGSNDP, X), opc!(ISEL, A, Isel),
        // 0x0_90
        opc!(MTCRF, XFX), None, opc!(MTMSR, X), None, None, opc!(STDX, X, Ppc64), opc!(STWCX, X, Ppc), opc!(STWX, X),
        opc!(SLQ, X, LE_POWER2), opc!(SLE, X, LE_POWER2), None, None, None, None, None /* RLDIC* */, None /* opcode 31 ext 62 RLWIMI* */,
        // 0x0_a0
        None, None, None, p207!(WRTEEI, E /* TODO: not sure where this was added, after PPC, before 2.07? */), None, None, p203!(DCBTLS, X), avop!(STVEHX, X),
        None, None, None, None, None, None, p207!(MSGCLRP, X), opc!(ISEL, A, Isel),
        // 0x0_b0
        None, None, opc!(MTMSRD, X, Ppc64), None, None, opc!(STDUX, X, Ppc64), None, opc!(STWUX, X),
        opc!(SLIQ, X, LE_POWER2), None, None, None, None, None, None /* RLDIC* */, None /* RLWINM* */,
        // 0x0_c0
        None, None, None, None, None, None, None, avop!(STVEWX, X),
        opc!(SUBFZE, XO), None, opc!(ADDZE, XO), None, None, None, p207!(MSGSND, X), opc!(ISEL, A, Isel),
        // 0x0_d0
        None, None, opc!(MTSR, X, Seg), None, None, None, opc!(STDCX, X, Ppc64), opc!(STBX, X),
        opc!(SLLQ, X, LE_POWER2), opc!(SLEQ, X, LE_POWER2), None, None, None, None, None /* RLDIMI* */, None /* opcode 31 shadow RLMI* */,
        // 0x0_e0
        None, None, None, None, None, None, None, avop!(STVX, X),
        opc!(SUBFME, XO), opc!(MULLD, XO, Ppc64), opc!(ADDME, XO), opc!(MULLW, XO), None, None, p207!(MSGCLR, X), opc!(ISEL, A, Isel),
        // 0x0_f0
        None, None, opc!(MTSRIN, X, Seg), None, None, None, opc!(DCBTST, X), opc!(STBUX, X),
        opc!(SLLIQ, X, LE_POWER2), None, None, None, None, None, None /* RLDIMI* */, None /* RLWNM* */,
        // 0x1_00
        None, None, None, None, None, None, invalid!(Ppc), None,
        opc!(DOZ, XO, LE_POWER2), None, opc!(ADD, XO), None, None, None, None, opc!(ISEL, A, Isel),
        // 0x1_10
        None, None, invalid!(Ppc), None, None, opc!(LSCBX, X, LE_POWER2), opc!(DCBT, X), opc!(LHZX, X),
        None, None, None, None, opc!(EQV, X), None, None /* RLDCL* */, None /* ORI* */,
        // 0x1_20
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, opc!(ISEL, A, Isel),
        // 0x1_30
        None, None, opc!(TLBIE, X), None, invalid!(Ppc), None, opc!(ECIWX, X, Ec), opc!(LHZUX, X),
        None, None, None, None, opc!(XOR, X), None, None /* RLDCR* */, None /* ORIS */,
        // 0x1_40
        None, None, None, invalid!(Ppc), None, None, None, None,
        None, None, None, opc!(DIV, XO, LE_POWER2), None, None, None, opc!(ISEL, A, Isel),
        // 0x1_50
        None, None, None, opc!(MFSPR, XFX), None, opc!(LWAX, X, Ppc64), opc!(DST, X, AltiVec_NonPower), opc!(LHAX, X),
        None, None, None, None, None, None, None, None /* XORI* */,
        // 0x1_60
        None, None, None, None, None, None, None, avop!(LVXL, X),
        opc!(ABS, XO, LE_POWER2), None, None, opc!(DIVS, XO, LE_POWER2), None, None, None, opc!(ISEL, A, Isel),
        // 0x1_70
        None, None, opc!(TLBIA, X, Tlb), opc!(MFTB, XFX, Ppc), None, opc!(LWAUX, X, Ppc64), opc!(DSTST, X, AltiVec_NonPower), opc!(LHAUX, X),
        None, None, None, None, None, None, None, None /* XORIS* */,
        // 0x1_80
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, opc!(ISEL, A, Isel),
        // 0x1_90
        None, None, opc!(SLBMTE, X, Slb), None, None, None, None, opc!(STHX, X),
        None, None, None, None, opc!(ORC, X), None, None, None /* ANDI* */,
        // 0x1_a0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, opc!(ISEL, A, Isel),
        // 0x1_b0
        None, None, opc!(SLBIE, X, Slb), None, None, None, opc!(ECOWX, X, Ec), opc!(STHUX, X),
        None, None, None, None, opc!(OR, X), None, None, None /* ANDIS* */,
        // 0x1_c0
        None, None, None, invalid!(Ppc), None, None, invalid!(Ppc), None,
        None, opc!(DIVDU, XO, Ppc64), None, opc!(DIVWU, XO, Ppc), None, None, None, opc!(ISEL, A, Isel),
        // 0x1_d0
        None, None, None, opc!(MTSPR, XFX), None, None, opc!(DCBI, X, Ppc) /* parens */, None /* LMW* */,
        None, None, None, None, opc!(NAND, X), None, None, None,
        // 0x1_e0
        None, None, None, None, None, None, invalid!(Ppc), avop!(STVXL, X),
        opc!(NABS, XO, LE_POWER2), opc!(DIVD, XO, Ppc64), None, opc!(DIVW, XO, Ppc), None, None, None, opc!(ISEL, A, Isel),
        // 0x1_f0
        None, None, opc!(SLBIA, X, Slb), None, None, None, opc!(CLI, X, LE_POWER2), None /* STMW* */,
        None, None, None, None, None, None, None, None,
        // 0x2_00
        opc!(MCRXR, X), None, None, None, None, None, opc!(BBLELS, X, Btb), opc!(LVLX, X, AltiVec_LE206),
        opc!(SUBFC, XO), opc!(MULHDU, XO, Ppc64), opc!(ADDC, XO), opc!(MULHWU, XO, Ppc), None, None, None, opc!(ISEL, A, Isel),
        // 0x2_10
        None, None, None, opc!(CLCS, X, LE_POWER2), opc!(LDBRX, X, Ppc64), opc!(LSWX, X), opc!(LWBRX, X), opc!(LFSX, X, Fpu),
        opc!(SRW, X), opc!(RRIB, X, LE_POWER2), None, opc!(SRD, X, Ppc64), None, opc!(MASKIR, X, LE_POWER2), None, None,
        // 0x2_20
        None, None, None, None, None, None, opc!(BBELR, X, Btb), opc!(LVRX, X, AltiVec_LE206),
        opc!(SUBF, XO, Ppc), None, None, None, None, None, None, opc!(ISEL, A, Isel),
        // 0x2_30
        None, None, None, None, None, None, opc!(TLBSYNC, X, Tlb), opc!(LFSUX, X, Fpu),
        None, None, None, None, None, None, None, None,
        // 0x2_40
        None, None, None, None, None, None, None, None,
        None, opc!(MULHD, XO, Ppc64), None, opc!(MULHW, XO, Ppc), None, None, None, opc!(ISEL, A, Isel),
        // 0x2_50
        None, None, None, opc!(MFSR, X, Seg), None, opc!(LSWI, X), opc!(SYNC, X), opc!(LFDX, X),
        None, None, None, None, None, None, None, None,
        // 0x2_60
        None, None, None, opc!(LDDX, X), None, None, None, None,
        opc!(NEG, XO), None, None, opc!(MUL, X, LE_POWER2), None, None, None, opc!(ISEL, A, Isel),
        // 0x2_70
        None, None, None, opc!(MFSRI, X, LE_POWER2), None, None, opc!(DCLST, X, LE_POWER2), opc!(LFDUX, X, Fpu),
        None, None, None, None, None, None, None, None,
        // 0x2_80
        None, None, None, None, None, None, None, opc!(STVLX, X, AltiVec_LE206),
        opc!(SUBFE, XO), None, opc!(ADDE, XO), None, None, None, p207!(TBEGIN, X), opc!(ISEL, A, Isel),
        // 0x2_90
        None, None, None, opc!(MFSRIN, X, Seg), opc!(STDBRX, X, Ppc64), opc!(STSWX, X), opc!(STWBRX, X), opc!(STFSX, X, Fpu),
        opc!(SRQ, X, LE_POWER2), opc!(SRE, X, LE_POWER2), None, None, None, None, None, None,
        // 0x2_a0
        None, None, None, None, None, None, None, opc!(STVRX, X, AltiVec_LE206),
        None, None, None, None, None, None, p207!(TEND, X), opc!(ISEL, A, Isel),
        // 0x2_b0
        None, None, None, None, None, None, None, opc!(STFSUX, X),
        opc!(SRIQ, X, LE_POWER2), None, None, None, None, None, None, None,
        // 0x2_c0
        None, None, None, None, None, None, None, None,
        opc!(SUBFZE, XO), None, opc!(ADDZE, XO), None, None, None, p207!(TCHECK, X), opc!(ISEL, A, Isel),
        // 0x2_d0
        None, None, None, None, None, opc!(STSWI, X), None, opc!(STFDX, X, Fpu),
        opc!(SRLQ, X, LE_POWER2), opc!(SREQ, X, LE_POWER2), None, None, None, None, None, None,
        // 0x2_e0
        None, None, None, None, None, None, None, None,
        opc!(SUBFME, XO), opc!(MULLD, XO, Ppc64), opc!(ADDME, XO), opc!(MULLW, XO), None, None, p207!(TSR, X), opc!(ISEL, A, Isel),
        // 0x2_f0
        None, None, None, None, None, None, p203!(DCBA, X) /* parens */, opc!(STFDUX, X),
        opc!(SRLIQ, X, LE_POWER2), None, None, None, None, None, None, None,
        // 0x3_00
        None, None, None, None, None, None, None, opc!(STVEPXL, X),
        opc!(DOZ, XO, LE_POWER2), None, opc!(ADD, XO), None, None, None, p207!(TABORTWC, X), opc!(ISEL, A, Isel),
        // 0x3_10
        None, None, opc!(TLBIVAX, X, TlbSw), None, None, None, opc!(LHBRX, X), opc!(LFDPX, X),
        opc!(SRAW, X), None, opc!(SRAD, X, Ppc64), None, None, None, None, None,
        // 0x3_20
        None, None, None, None, None, None, None, opc!(STVEPX, X),
        None, None, None, None, None, None, p207!(TABORTDC, X), opc!(ISEL, A, Isel),
        // 0x3_30
        None, None, opc!(RAC, X, LE_POWER2), None, None, None, opc!(DSS, X, AltiVec_NonPower), opc!(LFQUX, X, POWER2),
        opc!(SRAWI, X), None, opc!(SRADI, XS, Ppc64), opc!(SRADI, XS, Ppc64), None, None, None, None,
        // 0x3_40
        None, None, None, None, None, None, None, None,
        None, None, None, opc!(DIV, XO, LE_POWER2), None, None, p207!(TABORTWCI, X), opc!(ISEL, A, Isel),
        // 0x3_50
        None, None, None, opc!(SLBMFEV, X, Slb), None, None, opc!(EIEIO, X, Ppc), None,
        None, None, None, None, None, None, None, None,
        // 0x3_60
        None, None, None, None, None, None, None, None,
        opc!(ABS, XO, LE_POWER2), None, None, opc!(DIVS, XO, LE_POWER2), None, None, p207!(TABORTDCI, X), opc!(ISEL, A, Isel),
        // 0x3_70
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_80
        None, None, None, None, None, None, None, avop!(STVLXL, X),
        None, None, None, None, None, None, p207!(TABORT, X), opc!(ISEL, A, Isel),
        // 0x3_90
        None, None, opc!(TLBSX, X, TlbSw), opc!(SLBMFEE, X, Slb), None, None, opc!(STHBRX, X), opc!(STFDPX, X),
        opc!(SRAQ, X, LE_POWER2), opc!(SREA, X, LE_POWER2), opc!(EXTSH, X), None, None, None, None, None,
        // 0x3_a0
        None, None, None, None, None, None, None, avop!(STVRXL, X),
        None, None, None, None, None, None, p207!(TRECLAIM, X), opc!(ISEL, A, Isel),
        // 0x3_b0
        None, None, opc!(TLBRE, X, TlbSw), None, None, None, None, opc!(STFQUX, X, POWER2),
        opc!(SRAIQ, X, LE_POWER2), None, opc!(EXTSB, X, Ppc), None, None, None, None, None,
        // 0x3_c0
        None, None, None, None, None, None, invalid!(Ppc), None,
        None, opc!(DIVDU, XO, Ppc64), None, opc!(DIVWU, XO, Ppc), None, None, None, opc!(ISEL, A, Isel),
        // 0x3_d0
        None, None, opc!(TLBWE, X, TlbSw), None, None, None, opc!(ICBI, X, Ppc), opc!(STFIWX, X, Ppc),
        None, None, opc!(EXTSW, X, Ppc64), None, None, None, None, None,
        // 0x3_e0
        None, None, None, None, None, None, invalid!(Ppc), None,
        opc!(NABS, XO, LE_POWER2), opc!(DIVD, XO, Ppc64), None, opc!(DIVW, XO, Ppc), None, None, p207!(TRECHKPT, X), opc!(ISEL, A, Isel),
        // 0x3_f0
        None, None, invalid!(Ppc), None, None, None, ppc!(DCBZ, X), None,
        None, None, None, None, None, None, None, None,
    ]
};

static FX_DS_LOADS: ExtensionTable = ExtensionTable {
    bit_offset: 0,
    bit_count: 2,
    records: &[
        opc!(LD, DS, Ppc64), opc!(LDU, DS, Ppc64), opc!(LWA, DS, Ppc64), None,
    ]
};

static FX_DS_STORES: ExtensionTable = ExtensionTable {
    bit_offset: 0,
    bit_count: 2,
    records: &[
        opc!(STD, DS, Ppc64), opc!(STDU, DS, Ppc64), None, None,
    ]
};

static FP_SINGLE_EXTENDED_OPS: ExtensionTable = ExtensionTable {
    bit_offset: 1,
    bit_count: 10,
    records: &[
        // 0x0_00
        None, None, dfp!(DADD, X), dfpqa!(DQUA, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_10
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x0_20
        None, None, dfp!(DMUL, X), dfpqa!(DRRND, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_30
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x0_40
        None, None, dfp!(DSCLI, Z22), dfpqa!(DQUAI, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_50
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x0_60
        None, None, dfp!(DSCRI, Z22), dfpqa!(DRINTX, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_70
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x0_80
        None, None, dfp!(DCMPO, X), None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_90
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x0_a0
        None, None, dfp25!(DTSTEX, X), None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_b0
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x0_c0
        None, None, dfp25!(DTSTDC, Z22), None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_d0
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x0_e0
        None, None, dfp25!(DTSTDG, Z22), dfpqa!(DRINTN, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_f0
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x1_00
        None, None, dfp!(DCTDP, X), dfpqa!(DQUA, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_10
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x1_20
        None, None, dfp!(DCTFIX, X), dfpqa!(DRRND, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_30
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x1_40
        None, None, dfp!(DDEDPD, X), dfpqa!(DQUAI, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_50
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x1_60
        None, None, dfp!(DXEX, X), dfpqa!(DRINTX, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_70
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x1_80
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_90
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x1_a0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_b0
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x1_c0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_d0
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x1_e0
        None, None, None, dfpqa!(DRINTN, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_f0
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x2_00
        None, None, dfp!(DSUB, X), dfpqa!(DQUA, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_10
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x2_20
        None, None, dfp!(DDIV, X), dfpqa!(DRRND, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_30
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x2_40
        None, None, dfp26!(DSCLI, Z22), dfpqa!(DQUAI, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_50
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x2_60
        None, None, dfp26!(DSCRI, Z22), dfpqa!(DRINTX, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_70
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x2_80
        None, None, dfp!(DCMPU, X), None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_90
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x2_a0
        None, None, dfp25!(DTSTSF, X), None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_b0
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x2_c0
        None, None, dfp25!(DTSTDC, Z22), None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_d0
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x2_e0
        None, None, dfp25!(DTSTDG, Z22), dfpqa!(DRINTN, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_f0
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x3_00
        None, None, dfp!(DRSP, X), dfpqa!(DQUA, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_10
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x3_20
        None, None, dfp27!(DCFFIX, X), dfpqa!(DRRND, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_30
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x3_40
        None, None, dfp!(DENBCD, X), dfpqa!(DQUAI, Z23), None, None, None, None,
        None, None, None, None, None, None, dfp26!(FCFIDS, X), None,
        // 0x3_50
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x3_60
        None, None, dfp!(DIEX, X), dfpqa!(DRINTX, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_70
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x3_80
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_90
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x3_a0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_b0
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x3_c0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, dfp26!(FCFIDUS, X), None,
        // 0x3_d0
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
        // 0x3_e0
        None, None, None, dfpqa!(DRINTN, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_f0
        None, None, opc!(FDIVS, A, FpuNoSingle), None, opc!(FSUBS, A, FpuNoSingle), opc!(FADDS, A, FpuNoSingle), opc!(FSQRTS, A, FpuNoSingle), None,
        opc!(FRES, A, FpuNoSingle), opc!(FMULS, A, FpuNoSingle), p203!(FRSQRTES, A), None, opc!(FMSUBS, A, FpuNoSingle), opc!(FMADDS, A, FpuNoSingle), opc!(FNMSUBS, A, FpuNoSingle), opc!(FNMADDS, A, FpuNoSingle),
    ]
};

static FP_DOUBLE_EXTENDED_OPS: ExtensionTable = ExtensionTable {
    bit_offset: 1,
    bit_count: 10,
    records: &[
        // 0x0_00
        opc!(FCMPU, X), None, dfp!(DADDQ, X), dfpqa!(DQUAQ, Z23), None, None, None, None,
        dfp!(FCPSGN, X), None, None, None, opc!(FRSP, X), None, opc!(FCTIW, X), opc!(FCTIWZ, X),
        // 0x0_10
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x0_20
        opc!(FCMPO, X), None, dfp!(DMULQ, X), dfpqa!(DRRNDQ, Z23), None, None, opc!(MTFSB1, X), None,
        opc!(FNEG, X), None, None, None, None, None, None, None,
        // 0x0_30
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x0_40
        opc!(MCRFS, X), None, dfp!(DSCLIQ, Z22), dfpqa!(DQUAIQ, Z23), None, None, opc!(MTFSB0, X), None,
        opc!(FMR, X, Fpu), None, None, None, None, None, None, None,
        // 0x0_50
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x0_60
        None, None, dfp!(DSCRIQ, Z22), dfpqa!(DRINTXQ, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_70
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x0_80
        p206!(FTDIV, X), None, dfp!(DCMPOQ, X), None, None, None, opc!(MTFSFI, X), None,
        opc!(FNABS, X), None, None, None, None, None, p206!(FCTIWU, X), p206!(FCTIWUZ, X),
        // 0x0_90
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x0_a0
        p206!(FTSQRT, X), None, dfp25!(DTSTEXQ, X), None, None, None, None, None,
        None, None, None, None, None, None, p206!(FCTIWU, X), p206!(FCTIWUZ, X),
        // 0x0_b0
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x0_c0
        None, None, dfp25!(DTSTDCQ, Z22), None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_d0
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x0_e0
        None, None, dfp25!(DTSTDGQ, Z22), dfpqa!(DRINTNQ, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x0_f0
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x1_00
        None, None, dfp!(DCTQPQ, X), dfpqa!(DQUAQ, Z23), None, None, None, None,
        opc!(FABS, X), None, None, None, None, None, None, None,
        // 0x1_10
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x1_20
        None, None, dfp!(DCTFIXQ, X), /* typo in Power ISA <3.0, "drrnd" */ dfpqa!(DRRNDQ, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_30
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x1_40
        None, None, dfp!(DDEDPDQ, X), dfpqa!(DQUAIQ, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_50
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x1_60
        None, None, dfp!(DXEXQ, X), dfpqa!(DRINTXQ, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x1_70
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x1_80
        None, None, None, None, None, None, None, None,
        p203!(FRIN, X), None, None, None, None, None, None, None,
        // 0x1_90
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x1_a0
        None, None, None, None, None, None, None, None,
        p203!(FRIZ, X), None, None, None, None, None, None, None,
        // 0x1_b0
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x1_c0
        None, None, None, None, None, None, None, None,
        p203!(FRIP, X), None, None, None, None, None, None, None,
        // 0x1_d0
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x1_e0
        None, None, None, dfpqa!(DRINTNQ, Z23), None, None, None, None,
        p203!(FRIM, X), None, None, None, None, None, None, None,
        // 0x1_f0
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x2_00
        None, None, dfp!(DSUBQ, X), dfpqa!(DQUAQ, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_10
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x2_20
        None, None, dfp!(DDIVQ, X), /* typo in Power ISA <3.0, "drrnd" */ dfpqa!(DRRNDQ, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_30
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x2_40
        None, None, p206!(DSCLIQ, Z22), dfpqa!(DQUAIQ, Z23), None, None, None, opc!(MFFS, X),
        None, None, None, None, None, None, None, None,
        // 0x2_50
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x2_60
        None, None, p206!(DSCRIQ, Z22), dfpqa!(DRINTXQ, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_70
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x2_80
        None, None, dfp!(DCMPUQ, X), None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_90
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x2_a0
        None, None, dfp25!(DTSTSFQ, X), None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_b0
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x2_c0
        None, None, dfp25!(DTSTDCQ, Z22), None, None, None, None, opc!(MTFSF, XFL),
        None, None, None, None, None, None, None, None,
        // 0x2_d0
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x2_e0
        None, None, dfp25!(DTSTDGQ, Z22), dfpqa!(DRINTNQ, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x2_f0
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x3_00
        None, None, dfp!(DRDPQ, X), dfpqa!(DQUAQ, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_10
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x3_20
        None, None, dfp!(DCFFIXQ, X), /* typo in Power ISA <3.0, "drrnd" */ dfpqa!(DRRNDQ, Z23), None, None, None, None,
        None, None, None, None, None, None, opc!(FCTID, X), opc!(FCTIDZ, X),
        // 0x3_30
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x3_40
        None, None, dfp!(DENBCDQ, X), dfpqa!(DQUAIQ, Z23), None, None, None, None, // TODO: FMRGEW, vsx (book 2.07+)
        None, None, None, None, None, None, opc!(FCFID, X), None,
        // 0x3_50
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x3_60
        None, None, dfp!(DIEXQ, X), dfpqa!(DRINTXQ, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_70
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x3_80
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_90
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x3_a0
        None, None, None, None, None, None, None, None,
        None, None, None, None, None, None, p206!(FCTIDU, X), p206!(FCTIDUZ, X),
        // 0x3_b0
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x3_c0
        None, None, None, None, None, None, None, None, // TODO: FMRGEW, vsx (book 2.07+)
        None, None, None, None, None, None, p206!(FCFIDU, X), None,
        // 0x3_d0
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
        // 0x3_e0
        None, None, None, dfpqa!(DRINTNQ, Z23), None, None, None, None,
        None, None, None, None, None, None, None, None,
        // 0x3_f0
        None, None, opc!(FDIV, A, Fpu), None, opc!(FSUB, A, Fpu), opc!(FADD, A, Fpu), ppc!(FSQRT, A), ppc!(FSEL, A),
        p206!(FRE, A), opc!(FMUL, A, Fpu), ppc!(FRSQRTE, A), None, opc!(FMSUB, A, Fpu), opc!(FMADD, A, Fpu), opc!(FNMSUB, A, Fpu), opc!(FNMADD, A, Fpu),
    ],
};

const fn opc(op: Opcode, f: Format) -> PrimaryLookup {
    PrimaryLookup::Opc(op, f)
}

const fn opc_b(op: Opcode) -> PrimaryLookup {
    opc(op, Format::B)
}

const fn opc_d(op: Opcode) -> PrimaryLookup {
    opc(op, Format::D)
}

const fn opc_d_dot(op: Opcode) -> PrimaryLookup {
    opc(op, Format::Ddot)
}

const fn opc_d_mem(op: Opcode) -> PrimaryLookup {
    opc(op, Format::Dmem)
}

const fn opc_i(op: Opcode) -> PrimaryLookup {
    opc(op, Format::I)
}

const fn opc_m(op: Opcode) -> PrimaryLookup {
    opc(op, Format::M)
}

const fn opc_sc(op: Opcode) -> PrimaryLookup {
    opc(op, Format::SC)
}

impl InstDecoder {
    /// create a new `InstDecoder` for the provided `IsaVersion`.
    pub fn for_cpu(version: IsaVersion) -> Self {
        let mut this = Self::default();
        this.version = version;
        this
    }

    /// decode the provided slice into an instruction.
    ///
    /// if the slice is too short or does not describe a valid instruction, this will fail with
    /// `DecodeError`. this is a short-hand for using [`yaxpeax_arch::U8Reader`].
    pub fn decode_slice(&self, bytes: &[u8]) -> Result<Instruction, DecodeError> {
        let mut reader = yaxpeax_arch::U8Reader::new(bytes);
        self.decode(&mut reader)
    }

    fn decode_paired_single_opc4(&self, inst: &mut Instruction, word: u32) -> Result<(), <Power as Arch>::DecodeError> {
        let opc = (word >> 1) & 0x3ff;

        let rt = (word >> 21) & 0x1f;
        let ra = (word >> 16) & 0x1f;
        let rb = (word >> 11) & 0x1f;

        if opc == 0x3f6 {
            inst.opcode = Opcode::DCBZ_L;
            inst.operands[0] = Some(Operand::Register(Register { reg: ra as u8 }));
            inst.operands[1] = Some(Operand::Register(Register { reg: rb as u8 }));
            return Ok(());
        }

        let opc_lo = opc & 0x1f;
        let opc_hi = (opc >> 5) & 0x1f;

        #[allow(non_camel_case_types)]
        #[derive(Copy, Clone, Debug)]
        enum OperandCode {
            TB,
            TAB,
            TAC,
            TACB,
            PS_LST,
        }

        fn read_ops(inst: &mut Instruction, code: OperandCode, word: u32) {
            let rt = (word >> 21) & 0x1f;
            let ra = (word >> 16) & 0x1f;
            let rb = (word >> 11) & 0x1f;
            let rc = (word >> 6) & 0x1f;
            let st = word & 1;

            match code {
                OperandCode::TB => {
                    inst.rc = st != 0;
                    inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                    inst.operands[1] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                }
                OperandCode::TAB => {
                    inst.rc = st != 0;
                    inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                    inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 }));
                    inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                }
                OperandCode::TAC => {
                    inst.rc = st != 0;
                    inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                    inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 }));
                    inst.operands[2] = Some(Operand::FPR(FPR { reg: rc as u8 }));
                }
                OperandCode::TACB => {
                    inst.rc = st != 0;
                    inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                    inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 }));
                    inst.operands[2] = Some(Operand::FPR(FPR { reg: rc as u8 }));
                    inst.operands[3] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                }
                OperandCode::PS_LST => {
                    let w = (word >> 10) & 1;
                    let gqr = (word >> 7) & 0b111;
                    inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                    inst.operands[1] = Some(Operand::Register(Register { reg: ra as u8 }));
                    inst.operands[2] = Some(Operand::Register(Register { reg: rb as u8 }));
                    inst.operands[3] = Some(Operand::Imm8(w as u8));
                    inst.operands[4] = Some(Operand::GQR(GQR { reg: gqr as u8 }));
                }
            }
        }

        use OperandCode::*;

        static OPS: [Option<(Opcode, OperandCode)>; 32] = [
            None,                       None,                       None,                        None,
            None,                       None,                       None,                        None,
            None,                       None,                       Some((Opcode::PS_SUM0, TACB)),  Some((Opcode::PS_SUM1, TACB)),
            Some((Opcode::PS_MULS0, TAC)),Some((Opcode::PS_MULS1, TAC)),Some((Opcode::PS_MADDS0, TACB)),Some((Opcode::PS_MADDS1, TACB)),
            None,                       None,                       Some((Opcode::PS_DIV, TAB)),   None,
            Some((Opcode::PS_SUB, TAB)),Some((Opcode::PS_ADD, TAB)),None,                        Some((Opcode::PS_SEL, TACB)),
            Some((Opcode::PS_RES, TB)), Some((Opcode::PS_MUL, TAC)),Some((Opcode::PS_RSQRTE, TB)),None,
            Some((Opcode::PS_MSUB, TACB)), Some((Opcode::PS_MADD, TACB)), Some((Opcode::PS_NMSUB, TACB)), Some((Opcode::PS_NMADD, TACB)),
        ];

        if let Some((opc, oper_code)) = OPS[opc_lo as usize] {
            inst.opcode = opc;
            read_ops(inst, oper_code, word);
            Ok(())
        } else {
            if opc_lo == 0 {
                static OPS: [Opcode; 4] = [
                    Opcode::PS_CMPU0,
                    Opcode::PS_CMPO0,
                    Opcode::PS_CMPU1,
                    Opcode::PS_CMPO1,
                ];
                if opc_hi > 3 {
                    return Err(DecodeError::InvalidOpcode);
                }
                inst.opcode = OPS[opc_hi as usize & 0b11];
                inst.operands[0] = Some(Operand::CrField((rt as u8) >> 2));
                inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 }));
                inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                Ok(())
            } else if opc_lo == 6 {
                inst.opcode = if opc_hi & 1 == 0 {
                    Opcode::PSQ_LX
                } else {
                    Opcode::PSQ_LUX
                };
                read_ops(inst, PS_LST, word);
                Ok(())
            } else if opc_lo == 7 {
                inst.opcode = if opc_hi & 1 == 0 {
                    Opcode::PSQ_STX
                } else {
                    Opcode::PSQ_STUX
                };
                read_ops(inst, PS_LST, word);
                Ok(())
            } else if opc_lo == 8 {
                inst.opcode = if opc_hi == 1 {
                    Opcode::PS_NEG
                } else if opc_hi == 2 {
                    Opcode::PS_MR
                } else if opc_hi == 4 {
                    Opcode::PS_NABS
                } else if opc_hi == 8 {
                    Opcode::PS_ABS
                } else {
                    return Err(DecodeError::InvalidOpcode);
                };
                read_ops(inst, TB, word);
                Ok(())
            } else if opc_lo == 16 {
                if opc_hi < 16 || opc_hi >= 20 {
                    return Err(DecodeError::InvalidOpcode);
                }
                static OPS: [Opcode; 4] = [
                    Opcode::PS_MERGE00,
                    Opcode::PS_MERGE01,
                    Opcode::PS_MERGE10,
                    Opcode::PS_MERGE11,
                ];
                inst.opcode = OPS[opc_hi as usize - 16];
                read_ops(inst, TAB, word);
                Ok(())
            } else {
                Err(DecodeError::InvalidOpcode)
            }
        }
    }

    fn decode_vmx128_opc4(&self, inst: &mut Instruction, word: u32) -> Result<(), <Power as Arch>::DecodeError> {
        let vdh = (word >> 2) & 0b11;
        let vd128 = (word >> 21) & 0b11111;
        let vd = vd128 | (vdh << 5);

        let opc_lo = (word >> 4) & 1;

        if opc_lo == 0 {
            let low_low = word & 0b110011;
            // std::eprintln!("low low: {:06b}", low_low);

            if low_low != 0b000011 {
                // and we're back to non-altivec!
                return self.decode_vector(inst, word);
            }
            let opc_mid = (word >> 5) & 1;
            if opc_mid != 0 {
                return Err(DecodeError::InvalidOpcode);
            }

            let ra = (word >> 16) & 0b11111;
            let rb = (word >> 11) & 0b11111;

            let opc_hi = (word >> 6) & 0x1f;
            static OPC4_VMX128: [Option<Opcode>; 32] = [
                Some(Opcode::LVSL128),  Some(Opcode::LVSR128),
                Some(Opcode::LVEWX128), Some(Opcode::LVX128),
                None,                   None,
                Some(Opcode::STVEWX128),Some(Opcode::STVX128),
                None,                   None,
                None,                   Some(Opcode::LVXL128),
                None                   ,None,
                None,                   Some(Opcode::STVXL128),
                Some(Opcode::LVLX128),  Some(Opcode::LVRX128),
                None,                   None,
                Some(Opcode::STVLX128), Some(Opcode::STVRX128),
                None, None,
                Some(Opcode::LVLXL128), Some(Opcode::LVRXL128),
                None,                   None,
                Some(Opcode::STVLXL128),Some(Opcode::STVRXL128),
                None,                   None,
            ];
            inst.opcode = OPC4_VMX128[opc_hi as usize].ok_or(DecodeError::InvalidOpcode)?;
            inst.operands[0] = Some(Operand::VR128(VR128 { reg: vd as u8 }));
            inst.operands[1] = Some(Operand::MemoryIndexed {
                base: Register { reg: ra as u8 },
                index: Register { reg: rb as u8 },
            });
        } else {
            inst.opcode = Opcode::VSLDOI128;

            let vdh = (word >> 2) & 0b11;
            let vd128 = (word >> 21) & 0b11111;
            let vd = vd128 | (vdh << 5);

            let vahh = (word >> 10) & 1;
            let vahl = (word >> 5) & 1;
            let vah = (vahh << 1) | vahl;
            let va128 = (word >> 16) & 0b11111;
            let va = va128 | (vah << 5);

            let vbh = (word >> 0) & 0b11;
            let vb128 = (word >> 11) & 0b11111;
            let vb = vb128 | (vbh << 5);

            inst.operands[0] = Some(Operand::VR128(VR128 { reg: vd as u8 }));
            inst.operands[1] = Some(Operand::VR128(VR128 { reg: va as u8 }));
            inst.operands[2] = Some(Operand::VR128(VR128 { reg: vb as u8 }));
            inst.operands[3] = Some(Operand::Imm8((word >> 6) as u8 & 0b1111));
        }

        Ok(())
    }

    fn decode_vmx128_opc5(&self, inst: &mut Instruction, word: u32) -> Result<(), <Power as Arch>::DecodeError> {
        let vdh = (word >> 2) & 0b11;
        let vd128 = (word >> 21) & 0b11111;
        let vd = vd128 | (vdh << 5);

        let vahh = (word >> 10) & 1;
        let vahl = (word >> 5) & 1;
        let vah = (vahh << 1) | vahl;
        let va128 = (word >> 16) & 0b11111;
        let va = va128 | (vah << 5);

        let vbh = (word >> 0) & 0b11;
        let vb128 = (word >> 11) & 0b11111;
        let vb = vb128 | (vbh << 5);

        inst.operands[0] = Some(Operand::VR128(VR128 { reg: vd as u8 }));
        inst.operands[1] = Some(Operand::VR128(VR128 { reg: va as u8 }));
        inst.operands[2] = Some(Operand::VR128(VR128 { reg: vb as u8 }));

        let opc_lo = (word >> 4) & 1;

        if opc_lo == 0 {
            let vc = (word >> 6) & 0b111;
            let opc_hi = (word >> 9) & 1;

            if opc_hi == 0 {
                inst.opcode = Opcode::VPERM128;
                inst.operands[3] = Some(Operand::VR128(VR128 { reg: vc as u8 }));
            } else {
                static OPS: [Opcode; 8] = [
                    Opcode::VPKSHSS128, Opcode::VPKSHUS128,
                    Opcode::VPKSWSS128, Opcode::VPKSWUS128,
                    Opcode::VPKUHUM128, Opcode::VPKUHUS128,
                    Opcode::VPKUWUM128, Opcode::VPKUWUS128,
                ];
                inst.opcode = OPS[vc as usize];
            }
        } else {
            let opc = (word >> 6) & 0b1111;
            static OPS: [Option<Opcode>; 16] = [
                Some(Opcode::VADDFP128), Some(Opcode::VSUBFP128),
                Some(Opcode::VMULFP128), Some(Opcode::VMADDFP128),
                Some(Opcode::VMADDCFP128), Some(Opcode::VNMSUBFP128),
                Some(Opcode::VMSUM3FP128), Some(Opcode::VMSUM4FP128),
                Some(Opcode::VAND128), Some(Opcode::VANDC128),
                Some(Opcode::VNOR128), Some(Opcode::VOR128),
                Some(Opcode::VXOR128), Some(Opcode::VSEL128),
                Some(Opcode::VSLO128), Some(Opcode::VSRO128),
            ];
            inst.opcode = OPS[opc as usize].ok_or(DecodeError::InvalidOpcode)?;
            if inst.opcode == Opcode::VMADDCFP128 || inst.opcode == Opcode::VNMSUBFP128 {
                inst.operands[3] = inst.operands[2];
                inst.operands[2] = inst.operands[0];
            } else if inst.opcode == Opcode::VMADDFP128 ||
                      inst.opcode == Opcode::VSEL128 {
                inst.operands[3] = inst.operands[0];
            }
        }

        Ok(())
    }

    fn decode_vmx128_opc6(&self, inst: &mut Instruction, word: u32) -> Result<(), <Power as Arch>::DecodeError> {
        // VMX128 uses opcode extension bits more aggressively than most other Power ISA bits, so
        // not as much of a table here..
        //
        // working heavily off of
        // https://github.com/xenia-project/xenia/blob/cc72de0/docs/ppc/vmx128.txt ...
        let opc_bit_low = (word >> 4) & 1;

        if opc_bit_low == 0 {
            // arguably the index is three bits at 7 and above
            let idx = (word >> 6) & 0b1111;

            let vbh = word & 0b11;
            let vdh = (word >> 2) & 0b11;
            let vb128 = (word >> 11) & 0b11111;
            let vd128 = (word >> 21) & 0b11111;

            let vd = vd128 | (vdh << 5);
            let vb = vb128 | (vbh << 5);

            inst.operands[0] = Some(Operand::VR128(VR128 { reg: vd as u8 }));

            if idx >= 0b1110 {
                inst.operands[1] = Some(Operand::VR128(VR128 { reg: vb as u8 }));
                if idx == 0b1110 {
                    inst.opcode = Opcode::VUPKHSB128;
                } else /* idx == 0b1111 */ {
                    inst.opcode = Opcode::VUPKLSB128;
                }

                if word & 0b000000_00000_11111_00000_1000010_00_00 != 0 {
                    // xenia documents this as VA128==0/VAh==0, but decodes as these unpack
                    // instructions regardless of the bits that are present. HELP: not sure what
                    // hardware tolerates.
                    // return Err(DecodeError::InvalidOperand);
                }
            } else {
                let va128 = (word >> 16) & 0b11111;
                let vahh = (word >> 10) & 1;
                let vahl = (word >> 5) & 1;
                let vah = (vahh << 1) | vahl;
                let va = va128 | (vah << 5);
                inst.operands[1] = Some(Operand::VR128(VR128 { reg: va as u8 }));
                inst.operands[2] = Some(Operand::VR128(VR128 { reg: vb as u8 }));

                static OPCS: [Opcode; 14] = [
                    Opcode::VCMPEQFP128,  Opcode::VCMPEQFP128,
                    Opcode::VCMPGEFP128,  Opcode::VCMPGEFP128,
                    Opcode::VCMPGTFP128,  Opcode::VCMPGTFP128,
                    Opcode::VCMPBFP128,   Opcode::VCMPBFP128,
                    Opcode::VCMPEQUW128,  Opcode::VCMPEQUW128,
                    Opcode::VMAXFP128,    Opcode::VMINFP128,
                    Opcode::VMRGHW128,    Opcode::VMRGLW128,
                ];
                inst.opcode = OPCS[idx as usize];
                if idx <= 0b1010 {
                    let r = (word >> 6) & 1;
                    inst.rc = r != 0;
                }
            }
            Ok(())
        } else {
            // you'll want altivec_instructions_reordered.txt handy and tuned to instructions
            // starting with `0 0 0 1 1 0` for this next bit.

            let vb128 = (word >> 11) & 0b11111;
            let va128 = (word >> 16) & 0b11111;
            let vd128 = (word >> 21) & 0b11111;

            let vbh = word & 0b11;
            let vdh = (word >> 2) & 0b11;
            let vahh = (word >> 10) & 1;
            let vahl = (word >> 5) & 1;
            let vah = (vahh << 1) | vahl;

            let vd = (vdh << 5) | vd128;
            let vb = (vbh << 5) | vb128;
            let va = (vah << 5) | va128;

            inst.operands[0] = Some(Operand::VR128(VR128 { reg: vd as u8 }));
            inst.operands[1] = Some(Operand::VR128(VR128 { reg: vb as u8 }));

            let opc_hi = (word >> 9) & 1;

            if opc_hi == 0 {
                let opc_lo = (word >> 6) & 0b111;
                inst.operands[1] = Some(Operand::VR128(VR128 { reg: va as u8 }));
                inst.operands[2] = Some(Operand::VR128(VR128 { reg: vb as u8 }));
                static SHIFT_OPS: [Option<Opcode>; 8] = [
                    None, Some(Opcode::VRLW128), None, Some(Opcode::VSLW128),
                    None, Some(Opcode::VSRAW128), None, Some(Opcode::VSRW128),
                ];
                inst.opcode = SHIFT_OPS[opc_lo as usize].ok_or(DecodeError::InvalidOpcode)?;
                return Ok(());
            }

            if (word >> 5) & 1 == 0 {

                if (word >> 10) & 1 == 0 {
                    inst.opcode = Opcode::VPERMWI128;
                    let perml = (word >> 16) & 0b11111;
                    let permh = (word >> 6) & 0b111;
                    let perm = (permh << 5) | perml;

                    inst.operands[2] = Some(Operand::Imm8(perm as u8));

                } else {
                    // vpkd3d128 or vrlimi128, depending on ..
                    let z = (word >> 6) & 0b11;
                    if (word >> 8) & 1 == 0 {
                        let x = (va128 >> 2) as u8;
                        let y = va128 & 0b11;

                        inst.opcode = Opcode::VPKD3D128;
                        inst.operands[2] = Some(
                            Operand::PixelFormat(PixelFormat::from_u8(x))
                        );
                        inst.operands[3] = Some(Operand::Imm8(y as u8));
                        inst.operands[4] = Some(Operand::Imm8(z as u8));
                    } else {
                        inst.opcode = Opcode::VRLIMI128;
                        inst.operands[2] = Some(Operand::Imm8(va128 as u8));
                        inst.operands[3] = Some(Operand::Imm8(z as u8));
                    }
                }
                return Ok(());
            }

            // we've ruled out bits 4, 5, and 9 for choosing the opcode at this point.
            // that leaves bits 6, 7, 8, and 10. we'll collect them in a funny way, but it'll do..

            let opc_bits = (word >> 6) & 0b1_1111;
            let idx = (opc_bits & 0b111) | ((opc_bits >> 1) & 0b1_000);

            static OPCODES: [Option<Opcode>; 16] = [
                Some(Opcode::VCFPSXWS128),
                Some(Opcode::VCFPUXWS128),
                Some(Opcode::VCSXWFP128),
                Some(Opcode::VCUXWFP128),
                Some(Opcode::VRFIM128),
                Some(Opcode::VRFIN128),
                Some(Opcode::VRFIP128),
                Some(Opcode::VRFIZ128),

                Some(Opcode::VREFP128),
                Some(Opcode::VRSQRTEFP128),
                Some(Opcode::VEXPTEFP128),
                Some(Opcode::VLOGEFP128),
                Some(Opcode::VSPLTW128),
                Some(Opcode::VSPLTISW128),
                None,
                Some(Opcode::VUPKD3D128),
            ];

            inst.opcode = OPCODES[idx as usize].ok_or(DecodeError::InvalidOpcode)?;

            if idx < 4 || idx >= 12  {
                if inst.opcode == Opcode::VSPLTISW128 {
                    let extended = ((va128 << 3) as i8) >> 3;
                    inst.operands[1] = Some(Operand::ImmExts16(extended as i16));
                } else if inst.opcode == Opcode::VUPKD3D128 {
                    let x = (va128 >> 2) as u8;
                    inst.operands[2] = Some(
                        Operand::PixelFormat(PixelFormat::from_u8(x))
                    );
                } else {
                    inst.operands[2] = Some(Operand::Imm8(va128 as u8));
                }
            }

            Ok(())
        }
    }

    fn decode_vector(&self, inst: &mut Instruction, word: u32) -> Result<(), <Power as Arch>::DecodeError> {
        let table = VECTOR_LMA;

        assert!(table.bit_count != 0);
        let idx = table.extract_idx(word);

        let table_section = (idx >> 4) & 0b11;
        let opc_idx = idx & 0b1111;
        let _row = idx >> 6;
        // std::eprintln!("table/opc: {:02b}, opc={}, row={:05b}", table_section, opc_idx, row);

        if table_section == 0b10 {
            // as of Power v2.06, this section is best described under:
            // > Table 8 (Right-Center) Extended opcodes for primary opcode 4 [Categroy: V & LMA]
            static LMA: [Option<Opcode>; 16] = [
                Some(Opcode::VMHADDSHS), Some(Opcode::VMHRADDSHS), Some(Opcode::VMLADDUHM), None,
                Some(Opcode::VMSUMUBM), Some(Opcode::VMSUMMBM), Some(Opcode::VMSUMUHM), Some(Opcode::VMSUMUHS),
                Some(Opcode::VMSUMSHM), Some(Opcode::VMSUMSHS), Some(Opcode::VSEL), Some(Opcode::VPERM),
                Some(Opcode::VSLDOI), None, Some(Opcode::VMADDFP), Some(Opcode::VNMSUBFP),
            ];
            inst.opcode = LMA[opc_idx as usize].ok_or(DecodeError::InvalidOpcode)?;

            let rc = ((word >> 6) & 0b11111) as u8;
            let rb = ((word >> 11) & 0b11111) as u8;
            let ra = ((word >> 16) & 0b11111) as u8;
            let rt = ((word >> 21) & 0b11111) as u8;

            inst.operands[0] = Some(Operand::VR(VR { reg: rt }));

            if inst.opcode == Opcode::VSEL || inst.opcode == Opcode::VPERM {
                inst.operands[1] = Some(Operand::VR(VR { reg: ra }));
                inst.operands[2] = Some(Operand::VR(VR { reg: rb }));
                inst.operands[3] = Some(Operand::VR(VR { reg: rc }));
            } else if inst.opcode == Opcode::VMADDFP || inst.opcode == Opcode::VNMSUBFP {
                inst.operands[1] = Some(Operand::VR(VR { reg: ra }));
                inst.operands[2] = Some(Operand::VR(VR { reg: rc }));
                inst.operands[3] = Some(Operand::VR(VR { reg: rb }));
            } else if inst.opcode == Opcode::VSLDOI {
                inst.operands[1] = Some(Operand::VR(VR { reg: ra }));
                inst.operands[2] = Some(Operand::VR(VR { reg: rb }));
                inst.operands[3] = Some(Operand::Imm8(rc & 0b1111));
            } else {
                inst.operands[1] = Some(Operand::VR(VR { reg: ra }));
                inst.operands[2] = Some(Operand::VR(VR { reg: rb }));
                inst.operands[3] = Some(Operand::VR(VR { reg: rc }));
            }
            return Ok(());
        } else if table_section == 0b11 {
            // as of Power v2.06, this section is zero:
            // > Table 8 (Right) Extended opcodes for primary opcode 4
            //
            // starting in v2.07, and more in v3.00, instructions become defined in this section.
            if self.version.supports(IsaConstraint::PowerIsa207) {
                // TODO:
                return Err(DecodeError::IncompleteDecoder);
            }

            // panic!("table section {:02b}, idx {:#x}", table_section, opc_idx);
            return Err(DecodeError::InvalidOpcode);
        }

        if opc_idx & 1 != 0 {
            // as of at least Power v2.07, no LMA-table entry had a low bit set in the opcode.
            return Err(DecodeError::InvalidOpcode);
        }

        let compressed_idx = (opc_idx >> 1) | ((idx >> 6) << 4);

        // the remaining parts of `table_section == 0b00 or 0b01`. the low bit is compressed out,
        // and the high bit of table section is compressed out, so only 9 bits are needed to index
        // the table.
        match table.records[compressed_idx] {
            Some(OpcodeRecord { lookup, isa_constraint, category: _ }) => {
                if !self.version.supports(isa_constraint) {
                    // std::eprintln!("bailing because {:?} does not support {:?}", self.version, isa_constraint);
                    return Err(DecodeError::InvalidOpcode);
                }
                // std::eprintln!("{:?} supports {:?}", self.version, isa_constraint);
                match lookup {
                    PrimaryLookup::Opc(opc, format) => {
                        inst.opcode = opc;
                        match format {
                            Format::VC => {
                                let rb = (word >> 11) & 0b11111;
                                let ra = (word >> 16) & 0b11111;
                                let rt = (word >> 21) & 0b11111;
                                inst.operands[0] = Some(Operand::VR(VR { reg: rt as u8 }));
                                inst.operands[1] = Some(Operand::VR(VR { reg: ra as u8 }));
                                inst.operands[2] = Some(Operand::VR(VR { reg: rb as u8 }));
                            }
                            Format::VCdot => {
                                inst.rc = true;
                                let rb = (word >> 11) & 0b11111;
                                let ra = (word >> 16) & 0b11111;
                                let rt = (word >> 21) & 0b11111;
                                inst.operands[0] = Some(Operand::VR(VR { reg: rt as u8 }));
                                inst.operands[1] = Some(Operand::VR(VR { reg: ra as u8 }));
                                inst.operands[2] = Some(Operand::VR(VR { reg: rb as u8 }));
                            }
                            Format::VX => {
                                static TWO_REGS: &'static [Opcode] = &[
                                    Opcode::VUPKHPX, Opcode::VUPKHSB,
                                    Opcode::VUPKHSH, Opcode::VUPKLPX,
                                    Opcode::VUPKLSB, Opcode::VUPKLSH,
                                    Opcode::VRFIM, Opcode::VRFIN,
                                    Opcode::VRFIP, Opcode::VRFIZ,
                                    Opcode::VEXPTEFP, Opcode::VLOGEFP,
                                    Opcode::VREFP, Opcode::VRSQRTEFP,
                                ];

                                let rb = (word >> 11) & 0b11111;
                                let ra = (word >> 16) & 0b11111;
                                let rt = (word >> 21) & 0b11111;
                                inst.operands[0] = Some(Operand::VR(VR { reg: rt as u8 }));
                                if TWO_REGS.contains(&opc) {
                                    inst.operands[1] = Some(Operand::VR(VR { reg: rb as u8 }));
                                } else {
                                    if opc == Opcode::VSPLTB {
                                        inst.operands[1] = Some(Operand::VR(VR { reg: rb as u8 }));
                                        inst.operands[2] = Some(Operand::Imm8(ra as u8 & 0b1111));
                                    } else if opc == Opcode::VSPLTISW ||
                                        opc == Opcode::VSPLTISH ||
                                        opc == Opcode::VSPLTISB {
                                        // `SIM` is sign-extended to the opcode's selected lane width
                                        let sim = ((ra << 3) as i8) >> 3;
                                        inst.operands[1] = Some(Operand::ImmExts16(sim as i16));
                                    } else if opc == Opcode::VSPLTH {
                                        inst.operands[1] = Some(Operand::VR(VR { reg: rb as u8 }));
                                        inst.operands[2] = Some(Operand::Imm8(ra as u8 & 0b111));
                                    } else if opc == Opcode::VSPLTW {
                                        inst.operands[1] = Some(Operand::VR(VR { reg: rb as u8 }));
                                        inst.operands[2] = Some(Operand::Imm8(ra as u8 & 0b11));
                                    } else if opc == Opcode::VCTSXS || opc == Opcode::VCTUXS ||
                                              opc == Opcode::VCFSX  || opc == Opcode::VCFUX {
                                        inst.operands[1] = Some(Operand::VR(VR { reg: rb as u8 }));
                                        inst.operands[2] = Some(Operand::Imm8(ra as u8));
                                    } else if opc == Opcode::MTVSCR {
                                        inst.operands[0] = Some(Operand::VR(VR { reg: rb as u8 }));
                                    } else if opc == Opcode::VCFUX || opc == Opcode::MFVSCR {
                                    } else {
                                        inst.operands[1] = Some(Operand::VR(VR { reg: ra as u8 }));
                                        inst.operands[2] = Some(Operand::VR(VR { reg: rb as u8 }));
                                    }
                                }
                            }
                            other => {
                                panic!("unhandled vsx instruction format {:?}", other);
                            }
                        }
                        Ok(())
                    }
                    other => {
                        panic!("other: {:?}", other);
                    }
                }
            }
            None => {
                // TODO:
                // panic!("table section {:02b}, idx {:#x}", table_section, compressed_idx);
                // std::eprintln!("table section {:02b}, idx {:#x}", table_section, compressed_idx);
                Err(DecodeError::InvalidOpcode)
            }
        }
    }
}

// TODO: v2.03 mentions Variable Length Encoding as 16-up-to-32-bit encoding that mandates
// big-endian instruction pages
// > defines alternative instruction encodings and definitions intended to increase instruction
// > density for very low end implementations. It was derived from an APU description developed by
// > Freescale Semiconductor.
#[allow(non_snake_case)]
impl Decoder<Power> for InstDecoder {
    fn decode_into<T: Reader<<Power as Arch>::Address, <Power as Arch>::Word>>(&self, inst: &mut Instruction, words: &mut T) -> Result<(), <Power as Arch>::DecodeError> {
        inst.opcode = Opcode::XORI;
        inst.operands = [None; 5];
        inst.rc = false;
        inst.lk = false;
        inst.aa = false;
        inst.oe = false;

        let word = words.next()?;

        let mut word = word.0;

        if !self.is_be {
            word = word.swap_bytes();
        }

        // TODO: handling for compressed encodings
        inst.len = 4;

        let opc = word >> 26;

        const fn undef() -> PrimaryLookup {
            PrimaryLookup::Undefined
        }

        /// taken from PowerISA V2.03 Appendix F "Opcode Maps", table 1:
        static OP_LUT: [PrimaryLookup; 64] = [
            // opc: 00
            PrimaryLookup::ImplementationDefined, undef(), opc_d(Opcode::TDI), opc_d(Opcode::TWI),
            PrimaryLookup::Opc4, PrimaryLookup::Opc5, PrimaryLookup::Opc6, opc_d(Opcode::MULLI),
            // opc: 08
            opc_d(Opcode::SUBFIC), opc_d(Opcode::DOZI), opc_d(Opcode::CMPLI), opc_d(Opcode::CMPI),
            opc_d(Opcode::ADDIC), opc_d_dot(Opcode::ADDIC), opc_d(Opcode::ADDI), opc_d(Opcode::ADDIS),
            // opc: 10
            opc_b(Opcode::BC), opc_sc(Opcode::SC), opc_i(Opcode::B), PrimaryLookup::Extension(&CR_OPS),
            opc_m(Opcode::RLWIMI), opc_m(Opcode::RLWINM), PrimaryLookup::Opc(Opcode::RLMI, Format::X), opc_m(Opcode::RLWNM),
            // opc: 18
            opc_d(Opcode::ORI), opc_d(Opcode::ORIS), opc_d(Opcode::XORI), opc_d(Opcode::XORIS),
            opc_d_dot(Opcode::ANDI), opc_d_dot(Opcode::ANDIS), PrimaryLookup::Extension(&FX_DWD_ROT), PrimaryLookup::Extension(&FX_EXTENDED_OPS),
            // opc: 20
            opc_d_mem(Opcode::LWZ), opc_d_mem(Opcode::LWZU), opc_d_mem(Opcode::LBZ), opc_d_mem(Opcode::LBZU),
            opc_d_mem(Opcode::STW), opc_d_mem(Opcode::STWU), opc_d_mem(Opcode::STB), opc_d_mem(Opcode::STBU),
            // opc: 28
            opc_d_mem(Opcode::LHZ), opc_d_mem(Opcode::LHZU), opc_d_mem(Opcode::LHA), opc_d_mem(Opcode::LHAU),
            opc_d_mem(Opcode::STH), opc_d_mem(Opcode::STHU), opc_d_mem(Opcode::LMW), opc_d_mem(Opcode::STMW),
            // opc: 30
            opc_d_mem(Opcode::LFS), opc_d_mem(Opcode::LFSU), opc_d_mem(Opcode::LFD), opc_d_mem(Opcode::LFDU),
            opc_d_mem(Opcode::STFS), opc_d_mem(Opcode::STFSU), opc_d_mem(Opcode::STFD), opc_d_mem(Opcode::STFDU),
            // opc: 38
            PrimaryLookup::Opc38, PrimaryLookup::Opc39, PrimaryLookup::Extension(&FX_DS_LOADS), PrimaryLookup::Extension(&FP_SINGLE_EXTENDED_OPS),
            PrimaryLookup::Opc3C, PrimaryLookup::Opc3D, PrimaryLookup::Extension(&FX_DS_STORES), PrimaryLookup::Extension(&FP_DOUBLE_EXTENDED_OPS),
        ];

        let (opc, enc) = match OP_LUT[opc as usize] {
            PrimaryLookup::ImplementationDefined => {
                let mut decoded = false;
                if self.version == IsaVersion::A2 || self.version == IsaVersion::All {
                    let xo = (word >> 1) & 0x3_ff;
                    if xo == 256 {
                        inst.opcode = Opcode::ATTN;
                        decoded = true;
                    }
                }
                if decoded {
                    return Ok(());
                }
                return Err(DecodeError::IncompleteDecoder);
            }
            PrimaryLookup::Opc4 => {
                // TODO: require the selected isa version to include Vector (or SPE)
                // std::eprintln!("decode mode: {:?}", self.opc4_mode());
                match self.opc4_mode() {
                    Some(Opc4Mode::SP) => {
                        panic!("incomplete: SP.*");
                    }
                    Some(Opc4Mode::AltiVec) | Some(Opc4Mode::VSX) => {
                        return self.decode_vector(inst, word);
                    }
                    Some(Opc4Mode::VMX128) => {
                        return self.decode_vmx128_opc4(inst, word);
                    }
                    Some(Opc4Mode::PS) => {
                        return self.decode_paired_single_opc4(inst, word);
                    }
                    None => {
                        return Err(DecodeError::InvalidOpcode);
                    }
                }
            }
            PrimaryLookup::Opc5 => {
                if self.version.supports(IsaConstraint::VMX128) {
                    return self.decode_vmx128_opc5(inst, word);
                } else {
                    return Err(DecodeError::InvalidOpcode);
                }
            }
            PrimaryLookup::Opc6 => {
                if self.version.supports(IsaConstraint::VMX128) {
                    return self.decode_vmx128_opc6(inst, word);
                } else {
                    return Err(DecodeError::InvalidOpcode);
                }
            }
            PrimaryLookup::Illegal |
            PrimaryLookup::Undefined => {
                return Err(DecodeError::InvalidOpcode);
            }
            PrimaryLookup::Extension(table) => {
                if table.bit_count == 0 {
                    panic!("unimplemented table: opcode {}", opc);
                }
                let idx = table.extract_idx(word);

                match table.records[idx] {
                    Some(record) => {
                        if !self.version.supports(record.isa_constraint) {
                            return Err(DecodeError::InvalidOpcode);
                        }
                        match record.lookup {
                            PrimaryLookup::Opc(opc, enc) => {
                                (opc, enc)
                            }
                            PrimaryLookup::Illegal => {
                                return Err(DecodeError::InvalidOpcode);
                            },
                            PrimaryLookup::Undefined => {
                                return Err(DecodeError::InvalidOpcode);
                            },
                            PrimaryLookup::Extension(_) => {
                                panic!("primary extension -> primary extension? {:x}", idx);
                            },
                            PrimaryLookup::ImplementationDefined => {
                                panic!("implementation defined?!");
                            }
                            PrimaryLookup::Opc4 | PrimaryLookup::Opc5 | PrimaryLookup::Opc6 |
                            PrimaryLookup::Opc38 | PrimaryLookup::Opc39 | PrimaryLookup::Opc3C | PrimaryLookup::Opc3D => {
                                panic!("TODO: impossible");
                            }
                        }
                    },
                    None => {
//                        panic!("no record for primary {:02x}/secondary {:03x}", opc, idx);
                        return Err(DecodeError::InvalidOpcode);
                    }
                }
            }
            PrimaryLookup::Opc38 => {
                // lfq is generally much more likely to be what someone expects to decode, but if
                // you're gekko then you get psq_l!
                if self.version == IsaVersion::All || !self.version.supports(IsaConstraint::Gekko) {
                    (Opcode::LFQ, Format::Dmem)
                } else {
                    (Opcode::PSQ_L, Format::PS_LST)
                }
            }
            PrimaryLookup::Opc39 => {
                match self.opc39_mode() {
                    Some(Opc39Mode::Gekko) => {
                        (Opcode::PSQ_LU, Format::PS_LST)
                    }
                    Some(Opc39Mode::POWER2) => {
                        (Opcode::LFQU, Format::DS)
                    }
                    Some(Opc39Mode::DFP) => {
                        (Opcode::LFDP, Format::DS)
                    }
                    None => {
                        return Err(DecodeError::InvalidOpcode);
                    }
                }
            }
            PrimaryLookup::Opc3C => {
                match self.opc3c_mode() {
                    Some(Opc3cMode::Gekko) => {
                        (Opcode::PSQ_ST, Format::PS_LST)
                    }
                    Some(Opc3cMode::POWER2) => {
                        (Opcode::STFQ, Format::DS)
                    }
                    Some(Opc3cMode::VSX) => {
                        // under VSX (in 2.07 and later) opcode 60 is used for an extension table.
                        // see:
                        // > Table 23: EXT60: Extended Opcode Map for Primary Opcode 60
                        return Err(DecodeError::IncompleteDecoder);
                    }
                    None => {
                        return Err(DecodeError::InvalidOpcode);
                    }
                }
            }
            PrimaryLookup::Opc3D => {
                match self.opc3d_mode() {
                    Some(Opc3dMode::Gekko) => {
                        (Opcode::PSQ_STU, Format::PS_LST)
                    }
                    Some(Opc3dMode::POWER2) => {
                        (Opcode::STFQU, Format::DS)
                    }
                    Some(Opc3dMode::STFDP) => {
                        (Opcode::STFDP, Format::X)
                    }
                    Some(Opc3dMode::VSX) => {
                        // in 2.05 and later
                        // > Table 14: EXT61: Extended Opcode Map for Primary Opcode 61
                        // but at least for xo=00 we can still say stfdp..
                        if word & 0b11 == 0b00 {
                            (Opcode::STFDP, Format::X)
                        } else {
                            return Err(DecodeError::IncompleteDecoder);
                        }
                    }
                    None => {
                        return Err(DecodeError::InvalidOpcode);
                    }
                }
            }
            PrimaryLookup::Opc(opc, enc) => {
                if opc == Opcode::TDI && !self.version.supports(IsaConstraint::Ppc64) {
                    // it's a bit difficult to fit this one-off constraint in the table above, so
                    // for now it's only checked later, at this point..
                    return Err(DecodeError::InvalidOpcode);
                }
                (opc, enc)
            }
        };

        inst.opcode = opc;

        match enc {
            Format::A => {
                let rt = (word >> 21) & 0x1f;
                let ra = (word >> 16) & 0x1f;
                let rb = (word >> 11) & 0x1f;
                let rc = (word >> 6) & 0x1f;
                let lk = word & 1;

                inst.rc = lk != 0;

                match inst.opcode {
                    Opcode::ISEL => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::RegisterOrZero(Register { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::Register(Register { reg: rb as u8 }));
                        inst.operands[3] = Some(Operand::CrBit(rc as u8));
                        // `isel` specifically ignores Rc
                        inst.rc = false;
                    }
                    Opcode::FRE | Opcode::FRES => {
                        // TODO: not sure when `fre`/`fres` were added. as of 2.03 these are
                        // "phased-in". what CPU first supported them?

                        if self.version.supports(IsaConstraint::Ppc) {
                            inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                            inst.operands[1] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                        } else {
                            return Err(DecodeError::InvalidOpcode);
                        }
                    }
                    Opcode::FSQRT | Opcode::FSQRTS | Opcode::FRSQRTE | Opcode::FRSQRTES => {
                        // TODO: this probably should be an fsqrt isa constraint that gekko does
                        // not support, rather than .. this
                        if self.version == IsaVersion::Gekko {
                            if inst.opcode == Opcode::FSQRT || inst.opcode == Opcode::FSQRTS {
                                return Err(DecodeError::InvalidOpcode);
                            }
                        } else if !self.version.supports(IsaConstraint::Fpu) {
                            return Err(DecodeError::InvalidOpcode);
                        }
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                    }
                    Opcode::FADD | Opcode::FADDS |
                    Opcode::FSUB | Opcode::FSUBS |
                    Opcode::FDIV | Opcode::FDIVS => {
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                    }
                    Opcode::FMUL | Opcode::FMULS => {
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rc as u8 }));
                    }
                    Opcode::FMADD | Opcode::FMSUB |
                    Opcode::FMADDS | Opcode::FMSUBS |
                    Opcode::FNMADD | Opcode::FNMSUB |
                    Opcode::FNMADDS | Opcode::FNMSUBS |
                    Opcode::FSEL  => {
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rc as u8 }));
                        inst.operands[3] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                    }
                    _ => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::Register(Register { reg: rb as u8 }));
                    }
                }
            }
            Format::I => {
                let li = (word >> 2) & 0xff_ff_ff;
                let aa = (word >> 1) & 1;
                let lk = word & 1;

                inst.aa = aa != 0;
                inst.lk = lk != 0;

                assert_eq!(inst.opcode, Opcode::B);

                let extended = ((li << 8) as i32) >> 6;

                inst.opcode = if aa == 0 {
                    inst.operands[0] = Some(Operand::BranchOffset(extended as i32));
                    if lk == 0 {
                        Opcode::B
                    } else {
                        Opcode::BL
                    }
                } else {
                    inst.operands[0] = Some(Operand::BranchAbs(extended as i32));
                    if lk == 0 {
                        Opcode::BA
                    } else {
                        Opcode::BLA
                    }
                }
            }
            Format::B => {
                let bo = (word >> 21) & 0x1f;
                let bi = (word >> 16) & 0x1f;
                let bd = (word >> 2) & 0x3f_ff;
                let aa = (word >> 1) & 1;
                let lk = word & 1;

                inst.aa = aa != 0;
                inst.lk = lk != 0;

                inst.operands[0] = Some(Operand::Imm8(bo as u8));
                inst.operands[1] = Some(Operand::CrBit(bi as u8));

                if aa == 0 {
                    inst.operands[2] = Some(Operand::BranchOffset16((bd << 2) as i16));
                } else {
                    inst.operands[2] = Some(Operand::BranchAbs((bd << 2) as i16 as i32));
                };

                inst.opcode = if inst.aa {
                    if inst.lk {
                        Opcode::BCLA
                    } else {
                        Opcode::BCA
                    }
                } else {
                    if inst.lk {
                        Opcode::BCL
                    } else {
                        Opcode::BC
                    }
                };

                if self.version == IsaVersion::RIOS || self.version == IsaVersion::RIOS2 {
                    inst.hint_style = None;
                } else if !self.version.supports(IsaConstraint::PowerIsa200) || self.version == IsaVersion::e500 {
                    inst.hint_style = Some(HintStyle::Legacy);
                } else {
                    inst.hint_style = Some(HintStyle::PowerIsa);
                }
            }
            Format::SC => {
                let lev = (word >> 5) & 0x7f;
                let aa = (word >> 1) & 1;

                if self.version == IsaVersion::RIOS || self.version == IsaVersion::RIOS2 {
                    // in the *old* days, POWER1 and POWER2, what is now `sc` was instead `svc` for
                    // `supervisor call`, and had slightly different interpretation..
                    // HELP: i would love a POWER1/POWER2 referece. this is reverse engineered out
                    // of the AIX 5.3 assembler reference, lacking those.
                    inst.opcode = Opcode::SVC;
                    let lk = word & 1;
                    inst.lk = lk != 0;
                    inst.aa = aa != 0;

                    if aa == 1 {
                        // svca, which has different operands!
                        let sv = (word >> 2) & 0x3f_ff;
                        inst.operands[0] = Some(Operand::ImmU16(sv as u16));
                    } else {
                        // svc or svcl
                        let fl1 = (word >> 12) & 0xf;
                        let fl2 = (word >> 2) & 0x7;
                        inst.operands[0] = Some(Operand::ImmU16(lev as u16));
                        inst.operands[1] = Some(Operand::Imm8(fl1 as u8));
                        inst.operands[2] = Some(Operand::Imm8(fl2 as u8));
                    }

                    return Ok(());
                }

                if aa != 1 {
                    // SC-form mandates bit 30 is set at least in 2.03+..
                    // except Xenia doesn't require aa=1.. what does hardware want?
                    //
                    // ppcd is super strict about should-be-zero/must-be-zero, gekko may actually
                    // reject this.
                    if !self.version.supports(IsaConstraint::Xenon) && self.version != IsaVersion::Gekko {
                        return Err(DecodeError::InvalidOperand);
                    }
                }

                // > The use of the LEV field is described in Book III. The
                // > LEV values greater than 1 are reserved, and bits 0:5 of
                // > the LEV field (instruction bits 20:25) are treated as a
                // > reserved field.
                // TODO: error(?) if lev > 1
                inst.operands[0] = Some(Operand::Imm8(lev as u8));
            }
            // the specific form of some D-form instructions that have the semantic of the rc bit
            // being set.
            Format::Ddot => {
                let rt = (word >> 21) & 0x1f;
                let ra = (word >> 16) & 0x1f;
                let si = word & 0xffff;

                inst.rc = true;

                match inst.opcode {
                    Opcode::ANDI | Opcode::ANDIS => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[2] = Some(Operand::ImmU16(si as u16));
                    }
                    _ => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::ImmExts16(si as i16));
                    }
                }
            }
            Format::D => {
                let rt = (word >> 21) & 0x1f;
                let ra = (word >> 16) & 0x1f;
                let si = word & 0xffff;

                match opc {
                    Opcode::CMPI => {
                        let bf = rt >> 2;
                        let l = rt & 1;
                        if l == 1 && !self.version.supports(IsaConstraint::Ppc64) {
                            return Err(DecodeError::InvalidOpcode);
                        }
                        inst.operands[0] = Some(Operand::CrField(bf as u8));
                        inst.operands[1] = Some(Operand::Imm8(l as u8));
                        inst.operands[2] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[3] = Some(Operand::ImmExts16(si as i16));
                    }
                    Opcode::CMPLI => {
                        let bf = rt >> 2;
                        let l = rt & 1;
                        // the L bit is only respected where 64b *could* have existed, which is to
                        // say "after POWER2"..
                        if self.version != IsaVersion::RIOS && self.version != IsaVersion::RIOS2 {
                            if l == 1 && !self.version.supports(IsaConstraint::Ppc64) {
                                return Err(DecodeError::InvalidOpcode);
                            }
                        }
                        inst.operands[0] = Some(Operand::CrField(bf as u8));
                        inst.operands[1] = Some(Operand::Imm8(l as u8));
                        inst.operands[2] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[3] = Some(Operand::ImmU16(si as u16));
                    }
                    Opcode::TDI | Opcode::TWI => {
                        inst.operands[0] = Some(Operand::Imm8(rt as u8));
                        inst.operands[1] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::ImmExts16(si as i16));
                    }
                    Opcode::ADDI | Opcode::ADDIS => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::RegisterOrZero(Register { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::ImmExts16(si as i16));
                    }
                    Opcode::ADDIC | Opcode::SUBFIC | Opcode::MULLI => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::ImmExts16(si as i16));
                    }
                    // some D-form instructions have unsigned immediates instead
                    Opcode::ORI | Opcode::ORIS |
                    Opcode::XORI | Opcode::XORIS => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[2] = Some(Operand::ImmU16(si as u16));
                    }
                    Opcode::DOZI => {
                        if !self.version.supports(IsaConstraint::LE_POWER2) {
                            return Err(DecodeError::InvalidOpcode);
                        }

                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::ImmExts16(si as i16));
                    }
                    _ => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[2] = Some(Operand::ImmExts16(si as i16));
                    }
                }
            }
            Format::Dmem => {
                let rt = (word >> 21) & 0x1f;
                let ra = (word >> 16) & 0x1f;
                let si = word & 0xffff;

                match inst.opcode {
                    Opcode::LFS | Opcode::LFD | Opcode::LFSU | Opcode::LFDU |
                    Opcode::STFS | Opcode::STFD | Opcode::STFSU | Opcode::STFDU => {
                        if !self.version.supports(IsaConstraint::Fpu) {
                            return Err(DecodeError::InvalidOpcode);
                        }

                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                    },
                    Opcode::LFQ => {
                        if !self.version.supports(IsaConstraint::LE_POWER2) {
                            return Err(DecodeError::InvalidOpcode);
                        }
                        if !self.version.supports(IsaConstraint::Fpu) {
                            return Err(DecodeError::InvalidOpcode);
                        }

                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                    },
                    _ => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                    }
                }
                inst.operands[1] = Some(Operand::MemoryDisp { base: Register { reg: ra as u8 }, disp: si as i16 });
            }
            Format::DS => {
                let rt = (word >> 21) & 0x1f;
                let ra = (word >> 16) & 0x1f;
                let ds = (word >> 2) & 0x3fff;
                // for some of these, like `lfdp`, there's an extension op which should be checked
                // and in later revs, means different opcodes.
                let xo = word & 3;

                if inst.opcode == Opcode::LDU {
                    if ra == rt || ra == 0 {
                        // TODO: xenia decodes this. what does hardware do?
                        // TODO: rpcs3 does too!
                        if !self.version.supports(IsaConstraint::CellOrXenon) {
                            return Err(DecodeError::InvalidOperand);
                        }
                    }
                }

                if inst.opcode == Opcode::STFQ || inst.opcode == Opcode::STFQU {
                    if xo == 0b00 {
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                    } else {
                        // we shouldn't ever be here if the isa isn't POWER2. for All we'd have
                        // gotten to stfdp instead anyway.
                        #[cfg(test)]
                        assert_eq!(self.version, IsaVersion::RIOS2);
                        return Err(DecodeError::IncompleteDecoder);
                    }
                } else if inst.opcode == Opcode::LFDP {
                    if xo == 0b00 {
                        if rt & 1 != 0 {
                            return Err(DecodeError::InvalidOperand);
                        }
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                    } else if xo == 0b10 || xo == 0b11 {
                        // VSX loads (lxsd etc)
                        return Err(DecodeError::IncompleteDecoder);
                    } else {
                        // xo == 1
                        return Err(DecodeError::InvalidOpcode);
                    }
                } else if inst.opcode == Opcode::LFQU {
                    if xo == 0b00 {
                        if !(self.version == IsaVersion::All || self.version == IsaVersion::RIOS2) {
                            return Err(DecodeError::InvalidOperand);
                        }
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                    } else {
                        // we're in opcode 57, which (afaik) only has one op defined. so bail.
                        return Err(DecodeError::InvalidOpcode);
                    }
                } else {
                    inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                }
                // `ea` is computed from `ra` + `exts(ds || 0b00)`
                inst.operands[1] = Some(Operand::MemoryDisp { base: Register { reg: ra as u8 }, disp: (ds << 2) as i16 });
            }
            Format::X => {
                let rt = (word >> 21) & 0x1f;
                let ra = (word >> 16) & 0x1f;
                let rb = (word >> 11) & 0x1f;
                let _xo = (word >> 1) & 0x1_ff;
                let lk = word & 1;

                // extension instructions interpret bits out of X-form in interesting ways.
                // sometimes.
                match opc {
                    /*
                     * POWER1, POWER2
                     */
                    Opcode::RLMI => {
                        // rlmi only existed in the POWER family architecture (that is, POWER +
                        // POWER2). so says AIX 5.3 aka alangref.pdf.
                        if !self.version.supports(IsaConstraint::POWER) {
                            return Err(DecodeError::InvalidOpcode);
                        }
                        let mb = (word >> 6) & 0x1f;
                        let me = (word >> 1) & 0x1f;
                        inst.operands[0] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[2] = Some(Operand::Register(Register { reg: rb as u8 }));
                        inst.operands[3] = Some(Operand::Imm8(mb as u8));
                        inst.operands[4] = Some(Operand::Imm8(me as u8));
                        inst.rc = lk != 0;
                    }
                    Opcode::SRQ |
                    Opcode::SLEQ |
                    Opcode::SREQ |
                    Opcode::SLLQ |
                    Opcode::SRLQ |
                    Opcode::SLE |
                    Opcode::SRE |
                    Opcode::SLQ |
                    Opcode::SRAQ |
                    Opcode::SREA |
                    Opcode::MASKG |
                    Opcode::MASKIR |
                    Opcode::RRIB => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[2] = Some(Operand::Register(Register { reg: rb as u8 }));
                        inst.rc = lk != 0;
                    }
                    Opcode::SLLIQ |
                    Opcode::SRAIQ |
                    Opcode::SRLIQ |
                    Opcode::SLIQ |
                    Opcode::SRIQ => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[2] = Some(Operand::Imm8(rb as u8));
                        inst.rc = lk != 0;
                    }
                    Opcode::CLI |
                    Opcode::CLF => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rb as u8 }));
                    }
                    Opcode::DCLST => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rb as u8 }));
                    }
                    Opcode::CLCS => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: ra as u8 }));
                    }
                    Opcode::DIV |
                    Opcode::DIVS |
                    Opcode::DOZ => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::Register(Register { reg: rb as u8 }));
                        inst.rc = lk != 0;
                    }
                    Opcode::LSCBX |
                    Opcode::RAC => {
                        inst.rc = lk != 0;
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::MemoryIndexed {
                            base: Register { reg: ra as u8 },
                            index: Register { reg: rb as u8 },
                        });
                    }
                    Opcode::LBDX |
                    Opcode::LHDX |
                    Opcode::LWDX => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::MemoryIndexed {
                            base: Register { reg: ra as u8 },
                            index: Register { reg: rb as u8 },
                        });
                    }
                    Opcode::LDDX => {
                        if self.version != IsaVersion::e500mc && self.version != IsaVersion::All {
                            return Err(DecodeError::InvalidOpcode);
                         }

                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::MemoryIndexed {
                            base: Register { reg: ra as u8 },
                            index: Register { reg: rb as u8 },
                        });
                    }
                    Opcode::MUL => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::Register(Register { reg: rb as u8 }));

                        inst.opcode = Opcode::MUL;
                        let oe = (word >> 10) & 1;
                        inst.oe = oe != 0;
                        inst.rc = lk != 0;
                    }
                    /* end of POWER/POWER2 instructions */
                    /* a few SLB-related (Power AS?) instructions */
                    Opcode::SLBIE => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rb as u8 }));
                    }
                    Opcode::SLBIA => {
                        /* no operands */
                    }
                    Opcode::SLBMFEE |
                    Opcode::SLBMFEV |
                    Opcode::SLBMTE => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rb as u8 }));
                    }
                    /* and the end of those */
                    Opcode::SYNC => {
                        if self.version.supports(IsaConstraint::PowerIsa203) {
                            inst.operands[0] = Some(Operand::LL(SyncControl::from_bits(rt as u8 & 0b11)));
                        }
                    }
                    /*
                     * CMP // BF, L, RA, RB
                     * CMPL // BF, L, RA, RB
                     */
                    Opcode::CMP |
                    Opcode::CMPL => {
                        let bf = rt >> 2;
                        let l = rt & 1;
                        if l == 1 && !self.version.supports(IsaConstraint::Ppc64) {
                            return Err(DecodeError::InvalidOpcode);
                        }
                        inst.operands[0] = Some(Operand::CrField(bf as u8));
                        inst.operands[1] = Some(Operand::Imm8(l as u8));
                        inst.operands[2] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[3] = Some(Operand::Register(Register { reg: rb as u8 }));
                    }
                    /*
                     * // operands: RA, RS, RB
                     * AND
                     * XOR
                     * NAND
                     * OR // v2.03 mentions PPR_PRI as a new feature when `or Rx, Rx, Rx`, w/ Rx=1, Rx=6, Rx=2
                     * NOR
                     * EQV
                     * ANDC
                     * ORC
                     *
                     * [Category: 64-Bit]
                     * // operands: RA, RS, RB
                     * SLD
                     * SRD
                     * SRAD
                     */
                    Opcode::AND |
                    Opcode::ANDC |
                    Opcode::NAND |
                    Opcode::XOR |
                    Opcode::EQV |
                    Opcode::OR |
                    Opcode::ORC |
                    Opcode::NOR |
                    Opcode::SLD |
                    Opcode::SRD |
                    Opcode::SRAD => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[2] = Some(Operand::Register(Register { reg: rb as u8 }));
                        inst.rc = lk != 0;
                    }

                    /* ISA v2.03
                     * // operands: RA, RS
                     * EXTSB
                     * EXTSH
                     * CNTLZW
                     *
                     * EXTSW // RA, RS [64-Bit]
                     * CNTLZD // RA, RS [Base.Phased-In]
                     * POPCNTB // RA, RS [Base.Phased-In]
                     * SLW
                     * SRW
                     * SRAWI // RA, RS, SH
                     * SRAW // RA, RS, RB
                     */
                    Opcode::CNTLZW |
                    Opcode::CNTLZD |
                    Opcode::EXTSB |
                    Opcode::EXTSH |
                    Opcode::EXTSW => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.rc = lk != 0;
                    }
// TODO:
//                    Opcode::SRL |
                    Opcode::SLW |
                    Opcode::SRW |
                    Opcode::SRAW => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[2] = Some(Operand::Register(Register { reg: rb as u8 }));
                        inst.rc = lk != 0;
                    }
                    Opcode::SRAWI => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[2] = Some(Operand::Imm8(rb as u8));
                        inst.rc = lk != 0;
                    }
                    /*
                     * at least v2.01, also in older Power by a different mnemonic.
                     * // RS, RA, NB
                     * LSWI
                     * STSWI
                     *
                     * TW // TO, RA, RB
                     * TD // TO, RA, RB
                     */
                    Opcode::LSWI | Opcode::STSWI => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::RegisterOrZero(Register { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::Imm8(if rb == 0 { 32 } else { rb as u8 }));
                    }
                    // v2.03 3.2.2.1 Obsolete Data Cache Instructions [Category: Vector.Phased-Out]
                    /*
                     * 4. [Category: Floating-Point]
                     * // BF
                     * mcrxr
                     * // RT, RA
                     * mfapidi
                     * mtdcrux
                     * mfdcrux
                     * // FRS, RA, RB
                     * lfsx
                     * lfsux
                     * lfdx
                     * lfdux
                     * stfsx
                     * stfsux
                     * stfdx
                     * stfdux
                     * stfiwx
                     * // FRT, FRB
                     * fmr
                     * fneg
                     * fabs
                     * fnabs
                     * frsp
                     * fctid
                     * fctidz
                     * fctiw
                     * fctliwz
                     * // FRT, FRB [Floating-Point.Phased-In
                     * fcfid
                     * frin
                     * frip
                     * friz
                     * frim
                     * // operands: BF, FRA, FRB
                     * fcmpu
                     * fcmpo
                     *
                     * .. skipping 5.9.3 Vector Logical Instructions .. see below
                     * .. skipping 6.3.9 SPE Instruction Set .. see below
                     * .. skipping "Vector .. " for expedience .. Chapter 7 v2.03 page 259 (PDF: 282)
                     */
                    Opcode::MTFSB0 | Opcode::MTFSB1 => {
                        inst.operands[0] = Some(Operand::Imm8(rt as u8));
                        inst.rc = lk != 0;
                    }
                    Opcode::MCRXR => {
                        inst.operands[0] = Some(Operand::CrField(rt as u8 >> 2));
                    }
                    Opcode::MTFSFI => {
                        inst.operands[0] = Some(Operand::FpscrField((rt >> 2) as u8));
                        inst.operands[1] = Some(Operand::Imm8((rb >> 1) as u8));
                        inst.rc = lk != 0;
                    }
                    Opcode::FCMPO | Opcode::FCMPU => {
                        inst.operands[0] = Some(Operand::CrField((rt >> 2) as u8));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                    }
                    Opcode::FCTID | Opcode::FCTIDZ |
                    Opcode::FCTIDU | Opcode::FCTIDUZ |
                    Opcode::FCFID => {
                        if !self.version.supports(IsaConstraint::Ppc64) {
                            return Err(DecodeError::InvalidOpcode);
                        }
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                        inst.rc = lk != 0;
                    }
                    Opcode::FRSP | Opcode::FCTIW | Opcode::FCTIWU | Opcode::FCTIWZ | Opcode::FCTIWUZ |
                    Opcode::DCFFIX | Opcode::DCTFIX |
                    Opcode::FMR | Opcode::FNEG | Opcode::FABS | Opcode::FNABS => {
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                        inst.rc = lk != 0;
                    }
                    Opcode::LFSX | Opcode::LFDX | Opcode::LFSUX | Opcode::LFDUX |
                    Opcode::STFSX | Opcode::STFDX | Opcode::STFSUX | Opcode::STFDUX |
                    Opcode::STFIWX | Opcode::STFQUX => {
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::MemoryIndexed {
                            base: Register { reg: ra as u8 },
                            index: Register { reg: rb as u8 },
                        });
                    }
                    Opcode::FCPSGN |
                    Opcode::DADD | Opcode::DSUB | Opcode::DMUL | Opcode::DDIV => {
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                        inst.rc = lk != 0;
                    }
                    Opcode::FCFIDU | Opcode::FCFIDS | Opcode::FCFIDUS |
                    Opcode::FRIM | Opcode::FRIN |
                    Opcode::FRIP | Opcode::FRIZ |
                    Opcode::DCTDP | Opcode::DRSP |
                    Opcode::DXEX => {
                        if !self.version.supports(IsaConstraint::Fpu) {
                            // some floating point opcodes (frim, frin, frip, friz) are only v2.03
                            // and later, but also require an FPU to be present. e500 and friends
                            // do not meet this bar, but the most convenient place to check that is
                            // here.
                            return Err(DecodeError::InvalidOpcode);
                        }
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                        inst.rc = lk != 0;
                    }
                    Opcode::DCFFIXQ |
                    Opcode::DCTQPQ => {
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 & !1 }));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                        inst.rc = lk != 0;
                    }
                    Opcode::DCTFIXQ => {
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: rb as u8 & !1 }));
                        inst.rc = lk != 0;
                    }
                    Opcode::DRDPQ => {
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 & !1 }));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: rb as u8 & !1 }));
                        inst.rc = lk != 0;
                    }
                    Opcode::DXEXQ => {
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: rb as u8 & !1 }));
                        inst.rc = lk != 0;
                    }
                    Opcode::DIEX => {
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                        inst.rc = lk != 0;
                    }
                    Opcode::DIEXQ => {
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 & !1 }));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 & !1 }));
                        inst.rc = lk != 0;
                    }
                    Opcode::DADDQ | Opcode::DSUBQ | Opcode::DMULQ | Opcode::DDIVQ => {
                        if rt & 1 != 0 || ra & 1 != 0 || rb & 1 != 0 {
                            return Err(DecodeError::InvalidOpcode);
                        }

                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                        inst.rc = lk != 0;
                    }
                    Opcode::DCMPO | Opcode::DCMPU => {
                        inst.operands[0] = Some(Operand::CrField((rt >> 2) as u8));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                    }
                    Opcode::DCMPOQ | Opcode::DCMPUQ => {
                        inst.operands[0] = Some(Operand::CrField((rt >> 2) as u8));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 & !1 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 & !1 }));
                    }
                    Opcode::DTSTEX => {
                        inst.operands[0] = Some(Operand::CrField((rt >> 2) as u8));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                    }
                    Opcode::FTDIV |
                    Opcode::DTSTSF => {
                        inst.operands[0] = Some(Operand::CrField((rt >> 2) as u8));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                    }
                    Opcode::DTSTSFQ => {
                        inst.operands[0] = Some(Operand::CrField((rt >> 2) as u8));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 & !1 }));
                    }
                    Opcode::DTSTEXQ => {
                        inst.operands[0] = Some(Operand::CrField((rt >> 2) as u8));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 & !1 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 & !1 }));
                    }
                    Opcode::FTSQRT => {
                        inst.operands[0] = Some(Operand::CrField((rt >> 2) as u8));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                    }
                    /* TODO: not sure when exactly this was added (ppc?)
                     */
                    Opcode::MCRFS => {
                        inst.operands[0] = Some(Operand::CrField((rt >> 2) as u8));
                        inst.operands[1] = Some(Operand::FpscrField((ra >> 2) as u8));
                    }
                    /* Vector (AltiVec, also Power v2.03)
                     * lvsl
                     * ... many more
                     */
                    Opcode::LVX |
                    Opcode::LVEBX | Opcode::LVEHX | Opcode::LVEWX |
                    Opcode::STVX |
                    Opcode::LVXL | Opcode::STVXL |
                    Opcode::STVEBX | Opcode::STVEHX | Opcode::STVEWX |
                    Opcode::LVSL | Opcode::LVSR |
                    Opcode::LVLX |// Opcode::LVLXL |
                    Opcode::LVRX |// Opcode::LVRXL |
                    Opcode::STVRX | Opcode::STVRXL |
                    Opcode::STVLX | Opcode::STVLXL => {
                        inst.operands[0] = Some(Operand::VR(VR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::MemoryIndexed {
                            base: Register { reg: ra as u8 },
                            index: Register { reg: rb as u8 },
                        });
                    }
                    Opcode::STVEPXL => {
                        // with some dismay, ALTIVECPOWERISAPEM.pdf lists opc=0x3f, xo=0x307 is
                        // STVEPXL, but Xenia and RPCS3 say it's "lvlxl", which no manual defines.
                        if self.version == IsaVersion::Xenon || self.version == IsaVersion::Cell {
                            inst.opcode = Opcode::LVLXL;
                        } else if !self.version.supports(IsaConstraint::AltiVec) {
                            return Err(DecodeError::InvalidOpcode);
                        }

                        inst.operands[0] = Some(Operand::VR(VR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::MemoryIndexed {
                            base: Register { reg: ra as u8 },
                            index: Register { reg: rb as u8 },
                        });
                    }
                    Opcode::STVEPX => {
                        // with some dismay, ALTIVECPOWERISAPEM.pdf lists opc=0x3f, xo=0x327 is
                        // STVEPX, but Xenia and RPCS3 say it's "lvrxl", which no manual defines.
                        if self.version == IsaVersion::Xenon || self.version == IsaVersion::Cell {
                            inst.opcode = Opcode::LVRXL;
                        } else if !self.version.supports(IsaConstraint::AltiVec) {
                            return Err(DecodeError::InvalidOpcode);
                        }

                        inst.operands[0] = Some(Operand::VR(VR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::MemoryIndexed {
                            base: Register { reg: ra as u8 },
                            index: Register { reg: rb as u8 },
                        });
                    }
                    /* Embedded Floating-Point (EVX-Form)
                     *
                     * [SPE.Embedded Float Scalar Single]
                     * // operands: RT, RA
                     * EFSABS
                     * EFSNEG
                     * EFSNABS
                     * // operands RT, RA, RB
                     * EFSADD
                     * EFSSUB
                     * EFSMUL
                     * EFSDIV
                     * // operands BF, RA, RB
                     * EFSCMPGT
                     * EFSCMPLT
                     * EFSCMPEQ
                     * EFSTSTGT
                     * EFSTSTLT
                     * EFSTSTEQ
                     * // operands RT, RB
                     * EFSCFSI
                     * EFSCFUI
                     * EFSCFSF
                     * EFSCFUF
                     * EFSCTSI
                     * EFSCTUI
                     * EFSCTSIZ
                     * EFSCTUIZ
                     * EFSCTSF
                     * EFSCTUF
                     * // [SPE.Embedded Float Scalar Double]
                     * // operands RT, RA
                     * EFDABS
                     * EFDNEG
                     * EFDNABS
                     * // operands: RT, RA, RB
                     * EFDADD
                     * EFDSUB
                     * EFDMUL
                     * EFDDIV
                     * // operands: BF, RA, RB
                     * EFDCMPGT
                     * EFDCMPLT
                     * EFDCMPEQ
                     * EFDTSTGT
                     * EFDTSTLT
                     * EFDTSTEQ
                     *
                     * // operands: RT, RB
                     * EFDCFSI
                     * EFDCFUI
                     * EFDCFSID
                     * EFDCFUID
                     * EFDCFSF
                     * EFDCFUF
                     * EFDCTSI
                     * EFDCTUI
                     * EFDCTSIDZ
                     * EFDCTUIDZ
                     * EFDCTSIZ
                     * EFDCTUIZ
                     * EFDCSF
                     * EFDCTUF
                     * EFDCFS
                     * EFSCFD
                     *
                     */
                    /* Fixed-Point Instructions
                     *
                     * // Determine Leftmost Zero Byte
                     * DLMZB
                     *
                     * // Legacy Integer Multiply-Accumulate Instructions
                     * MULCHW
                     * MULCHWU
                     * MULHHW
                     * MULHHWU
                     * MULLHW
                     * MULLHWU
                     *
                     * // unure what exactly
                     * ICBI
                     * ICBT [Embedded]
                     * DCBT [Server, Embedded] (AND DIFFERENT OPERANDS FOR EACH?!)
                     * DCBTST [Server, Embedded] (AND DIFFERENT OPERANDS FOR EACH?!)
                     * DCBZ
                     * DCBST
                     */
                    Opcode::DSS => {
                        inst.operands[0] = Some(Operand::Imm8(rt as u8 & 0b11));
                        if rt >> 4 == 1 {
                            inst.opcode = Opcode::DSSALL;
                        }
                    }
                    Opcode::DST => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rb as u8 }));
                        inst.operands[2] = Some(Operand::Imm8(rt as u8 & 0b11));
                        if rt >> 4 == 1 {
                            inst.opcode = Opcode::DSTT;
                        }
                    }
                    Opcode::DSTST => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rb as u8 }));
                        inst.operands[2] = Some(Operand::Imm8(rt as u8 & 0b11));
                        if rt >> 4 == 1 {
                            inst.opcode = Opcode::DSTSTT;
                        }
                    }
                    Opcode::ICBI |
                    Opcode::DCBI => {
                        inst.operands[0] = Some(Operand::MemoryIndexed {
                            base: Register { reg: ra as u8 },
                            index: Register { reg: rb as u8 },
                        });
                    }
                    Opcode::DCBF => {
                        inst.operands[0] = Some(Operand::MemoryIndexed {
                            base: Register { reg: ra as u8 },
                            index: Register { reg: rb as u8 },
                        });
                        if self.version.supports(IsaConstraint::PowerIsa205) {
                            // in Power v2.05 this field was extended to two bits (L=3 is now
                            // valid and means `dcbflp`)
                            inst.operands[1] = Some(Operand::Imm8(rt as u8 & 0b11));
                        } else if self.version.supports(IsaConstraint::PowerIsa202) {
                            // before Power v2.02 there was no L operand to `dcbf`. in 2.02 and
                            // later, L=1 means the alias `dcbfl`.
                            inst.operands[1] = Some(Operand::Imm8(rt as u8 & 0b1));
                        }
                    }
//                    Opcode::ICBT |
                    Opcode::DCBZ => {
                        // DCBZ "existed" as far back as PowerPC (is in
                        // SR28-5124-01_The_PowerPC_Architecture_May94.pdf), but was part of the
                        // Virtual Instruction Set until 2.03. it's decoded as long as it was in
                        // the VISA to match with other decoders, objdump, etc.
                        inst.operands[0] = Some(Operand::MemoryIndexed {
                            base: Register { reg: ra as u8 },
                            index: Register { reg: rb as u8 },
                        });

                        // Xenia only decodes `dcbz` when rt=0, and indicates a `dcbz128` when
                        // rt=1, decoding nothing otherwise. RPCS3 seems to decode in any case?
                        if self.version == IsaVersion::Xenon && rt != 0 {
                            if rt == 1 {
                                inst.opcode = Opcode::DCBZ128;
                            } else {
                                return Err(DecodeError::InvalidOperand);
                            }
                        }
                    }
                    Opcode::DCBA |
                    Opcode::DCBST => {
                        inst.operands[0] = Some(Operand::MemoryIndexed {
                            base: Register { reg: ra as u8 },
                            index: Register { reg: rb as u8 },
                        });
                    }
                    Opcode::DCBT => {
                        // TODO: assuming [Server] encoding
                        inst.operands[0] = Some(Operand::MemoryIndexed {
                            base: Register { reg: ra as u8 },
                            index: Register { reg: rb as u8 },
                        });
                        // in Power v2.05 the TH field was extended to 5 bits.
                        let th = rt as u8 & 0b11111;
                        if self.version.supports(IsaConstraint::PowerIsa205) {
                            inst.operands[1] = Some(Operand::Imm8(th));
                        } else if self.version.supports(IsaConstraint::PowerIsa203) {
                            // ... but until 2.05, it was only 4 bits.
                            inst.operands[1] = Some(Operand::Imm8(th & !0b1_0000));
                        } else if self.version.supports(IsaConstraint::PowerIsa200) {
                            // the TH field, aka Touch Hint, was defined on `dcbt` first in v2.00
                            // for Power AS. later, and above, here, this finally became "public"
                            // in v2.03(?).
                            inst.opcode = Opcode::DCBT_AS;
                            inst.operands[1] = Some(Operand::Imm8(th & 0b11));
                        }
                    }
                    Opcode::DCBTLS => {
                        let th = rt as u8 & 0b1111;
                        inst.operands[0] = Some(Operand::Imm8(th as u8 & 0b1111));
                        inst.operands[1] = Some(Operand::MemoryIndexed {
                            base: Register { reg: ra as u8 },
                            index: Register { reg: rb as u8 },
                        });
                    }
                    Opcode::DCBTST => {
                        // TODO: assuming [Server] encoding
                        inst.operands[0] = Some(Operand::MemoryIndexed {
                            base: Register { reg: ra as u8 },
                            index: Register { reg: rb as u8 },
                        });
                        // like `dcbt` above, `dcbtst` gets a 4-bit hint in 2.03, expanded to 5-bit
                        // in 2.05.
                        let th = rt as u8 & 0b11111;
                        if self.version.supports(IsaConstraint::PowerIsa205) {
                            inst.operands[1] = Some(Operand::Imm8(th));
                        } else if self.version.supports(IsaConstraint::PowerIsa200) {
                            inst.operands[1] = Some(Operand::Imm8(th & !0b1_0000));
                        } else if self.version.supports(IsaConstraint::POWER) {
                            // before Power v2.00 or so this hint wasn't supported at all. mostly:
                            // this is not supported if the IsaVersion is RIOS/RIOS2.
                            return Err(DecodeError::InvalidOpcode);
                        }
                    }
                    /*
                     * // External Access Instructions [External Control]
                     * ECIWX
                     * ECOWX
                     */
                    Opcode::ECIWX | Opcode::ECOWX => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::MemoryIndexed {
                            base: Register { reg: ra as u8 },
                            index: Register { reg: rb as u8 },
                        });
                    }
                    /*
                     * // unsure what extension yet
                     *
                     * E_SLWI
                     * E_RLWI
                     * E_RLW
                     *
                     * // "Storage Control" maybe?
                     * SYNC
                     * EIEIO [Server]
                     * MBAR [Embedded]
                     * WAIT [Wait]
                     *
                     */
                    Opcode::EIEIO => {
                        // TODO: the 2.03 manual describes eieio and mbar 0 as operating
                        // differently, implicitly? what is the right thing to do for that version
                        // in the case of rt=0..
                        if self.version.supports(IsaConstraint::Embedded) &&
                            !self.version.supports(IsaConstraint::PowerIsa30) &&
                            rt != 0 {

                            let mo = rt as u8;
                            // see v2.03 and `Embedded`.
                            inst.opcode = Opcode::MBAR;
                            inst.operands[0] = Some(Operand::Imm8(mo));
                        }
                    }
                    /*
                     * // Fixed-Point Processor
                     * MTMSR
                     * MTMRSD
                     * MFMSR
                     * SLBIE
                     * SLBIA
                     * SLBMTE
                     * SLBMFEV
                     * SLBMFEE
                     * MTSR
                     * MTSRIN
                     * MFSR
                     * MFSRIN
                     * TLBIE [Server]
                     * TLBIEL [Server]
                     * TLBIA
                     * TLBSYNC
                     * RFDI [Embedded.Enhanced Debug]
                     * MTDCRX
                     * MFDCRX
                     * MFMSR
                     * WRTEE
                     * WRTEEI
                     */
                    Opcode::MTSR => {
                        inst.operands[0] = Some(Operand::Imm8(ra as u8 & 0b1111));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rt as u8 }));
                    }
                    Opcode::MTSRIN => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rb as u8 }));
                    }
                    Opcode::MFSR => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::Imm8(ra as u8 & 0b1111));
                    }
                    Opcode::MFSRI => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::Register(Register { reg: rb as u8 }));
                    }
                    Opcode::MFSRIN => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rb as u8 }));
                    }
                    Opcode::MTSRD => {
                        // HELP: actual citations for MTSRD are scarce. it's not in the RS/6000
                        // docs, nor Power v2.02+, nor PowerPC, so this is mostly rederived from
                        // binutils..
                        if !self.version.supports(IsaConstraint::Ppc64) {
                            return Err(DecodeError::InvalidOpcode);
                        }
                        inst.operands[0] = Some(Operand::Imm8(ra as u8 & 0b1111));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rt as u8 }));
                    }
                    Opcode::MTSRDIN => {
                        if !self.version.supports(IsaConstraint::Ppc64) {
                            return Err(DecodeError::InvalidOpcode);
                        }
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rb as u8 }));
                    }
                    Opcode::TLBIE => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rb as u8 }));

                        // later Power ISA versions add parameters to tlbie. 2.01+ add an L field
                        // to indicate page size for the invalidation, and 2.06+ change the L field
                        // into a register.

                        if self.version.supports(IsaConstraint::PowerIsa206) {
                            inst.operands[1] = Some(Operand::Register(Register { reg: rt as u8 }));
                        } else if self.version.supports(IsaConstraint::PowerIsa202) {
                            inst.operands[1] = Some(Operand::Imm8(rt as u8 & 1));
                        } else if self.version.supports(IsaConstraint::POWER) {
                            inst.opcode = Opcode::TLBI;
                            inst.operands[1] = inst.operands[0];
                            inst.operands[0] = Some(Operand::Register(Register { reg: ra as u8 }));
                        } else if self.version == IsaVersion::Xenon {
                            // another case of a cache-related instruction that might be
                            // intentionally.. or unintentionally.. ignored in Xenia?
                            return Err(DecodeError::InvalidOpcode);
                        } else {
                            // not POWER1, not POWER2, so v2.0 to v2.02. so just the register and no large-page support?
                        }
                    }
                    /*
                     *
                     * LBEPX
                     * LHEPX
                     * LWEPX
                     * LDEPX
                     * STBEPX
                     * STHEPX
                     * STWEPX
                     * STDEPX
                     * DCBSTEP
                     * DCBTEP
                     * DCBFEP
                     * DCBTSTEP
                     * ICBIEP
                     * DCBZEP
                     * LFDEPX
                     * STFDEPX
                     * EVLDDEPX
                     * EVSTDDEPX
                     * LVEPX
                     * LVEPXL
                     * STVEPX
                     * STVEPXL
                     *
                     * // TODO: ?? Cache Management Instructions ???
                     * DCBI
                     * DCBTLS
                     * DCBTSTLS
                     * ICBTLS
                     * ICBLC
                     * DCBLC
                     * TLBIVAX
                     * TLBRE
                     * TLBSX
                     * TLBSYNC
                     * TLBWE
                     */
                    Opcode::TLBRE |
                    Opcode::TLBWE => {
                        // no operands on either of these
                    }
                    Opcode::TLBSX |
                    Opcode::TLBIVAX => {
                        inst.operands[0] = Some(Operand::MemoryIndexed {
                            base: Register { reg: ra as u8 },
                            index: Register { reg: rb as u8 },
                        });
                    }
                    /*
                     * 9.3 Processor Control Instructions
                     * MSGSND
                     * MSGCLR
                     */
                    Opcode::MSGCLR |
                    Opcode::MSGSND |
                    Opcode::MSGCLRP |
                    Opcode::MSGSNDP => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rb as u8 }));
                    }
                    /*
                     * A.1 Embedded Cache Initialization
                     * DCI
                     * ICI
                     *
                     * A.3 Embedded Cache Debug Instructions
                     * DCREAD
                     *
                     * smaller reg1 (bf):
                     * E_CMPHL
                     * E_CMPH
                     *
                     * two regs:
                     * TLBSX
                     * TLBIVAX
                     * ICREAD
                     */
                    Opcode::LBZX |
                    Opcode::LHZX |
                    Opcode::LHAX |
                    Opcode::LWZX |
                    Opcode::LWAX |
                    Opcode::LDX |
                    Opcode::STBX |
                    Opcode::STHX |
                    Opcode::STWX |
                    Opcode::STDX => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::MemoryIndexed {
                            base: Register { reg: ra as u8 },
                            index: Register { reg: rb as u8 },
                        });
                    }
                    Opcode::LBZUX |
                    Opcode::LHZUX |
                    Opcode::LHAUX |
                    Opcode::LWZUX |
                    Opcode::LWAUX |
                    Opcode::LDUX |
                    Opcode::STBUX |
                    Opcode::STHUX |
                    Opcode::STWUX |
                    Opcode::STDUX |
                    Opcode::LHBRX |
                    Opcode::LDBRX |
                    Opcode::STHBRX |
                    Opcode::STDBRX |
                    Opcode::LSWX |
                    Opcode::STSWX |
                    // start 64-bit Load and Reserver and Store Conditional Instructions
                    Opcode::STDCX |
                    Opcode::STWCX |
                    Opcode::LDARX |
                    Opcode::LWARX |
                    // end 64-bit Load and Reserver and Store Conditional Instructions
                    Opcode::STWBRX |
                    Opcode::LWBRX => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::MemoryIndexed {
                            base: Register { reg: ra as u8 },
                            index: Register { reg: rb as u8 },
                        });
                        if opc == Opcode::STDCX || opc == Opcode::STWCX {
                            let isa_tolerates_lk0 = self.version != IsaVersion::All &&
                                                    self.version != IsaVersion::Xenon &&
                                                    self.version != IsaVersion::Cell;
                            if lk == 0 && isa_tolerates_lk0 {
                                return Err(DecodeError::InvalidOpcode);
                            }
                            // HELP: these are defined as always having `rc` set, and in fact only
                            // being valid with Rc=1, but xenia and rpcs3 decode them either way.
                            // so i'm assuming hardware also accepts this. does it?
                            inst.rc = true;
                        } else if opc == Opcode::LWARX || opc == Opcode::LDARX {
                            if self.version.supports(IsaConstraint::PowerIsa205) {
                                inst.operands[2] = Some(Operand::Imm8(lk as u8));
                            } else if lk != 0 && self.version != IsaVersion::Gekko {
                                return Err(DecodeError::InvalidOperand);
                            }
                        }
                    }
                    Opcode::MFMSR => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                    }
                    Opcode::MTMSR |
                    Opcode::MTMSRD => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        if self.version == IsaVersion::Xenon {
                            // the fact that there's a different `MTMSRD_Xenon` is unfortunate, but
                            // see the comment around aliasing in alias.rs .. we don't need to do
                            // this for MTMSR because there is no Xenon-specific aliasing (or
                            // alternate mnemonic!) rules.
                            if inst.opcode == Opcode::MTMSRD {
                                inst.opcode = Opcode::MTMSRD_Xenon;
                            }
                            inst.operands[1] = Some(Operand::Imm8(ra as u8 & 1));
                        } else if self.version.supports(IsaConstraint::PowerIsa200) {
                            // mtmsr and mtmsrd got a second operand in the form of `L` somewhere
                            // between PowerPC AS (note "v2.00" in the reference) and PowerPC
                            // v2.01. notably, v2.01 does not indicate `L` is *new* in 2.01,
                            // implying it came earlier, but the only earlier reference i can find
                            // is the AS/v2.00 one, which does not indicate `L` being decoded. so
                            // "added in 2.00 after Amazon" is a bit of a best guess from available
                            // docs.
                            inst.operands[1] = Some(Operand::Imm8(ra as u8 & 1));
                        }
                    }
                    Opcode::MFFS => {
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.rc = lk != 0;
                    }
                    Opcode::TD |
                    Opcode::TW => {
                        inst.operands[0] = Some(Operand::Imm8(rt as u8));
                        inst.operands[1] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::Register(Register { reg: rb as u8 }));
                    }
                    Opcode::STFDPX => {
                        if self.version == IsaVersion::RIOS2 {
                            inst.opcode = Opcode::STFQX;
                        } else if self.version.supports(IsaConstraint::PowerIsa205) {
                            if rt & 1 != 0 {
                                // > If FRTp is odd, the instruction form is invalid
                                //
                                // .. objdump falls back to STFQX, but only do that if we're
                                // `IsaVersion::All`:
                                if self.version == IsaVersion::All {
                                    inst.opcode = Opcode::STFQX;
                                } else {
                                    return Err(DecodeError::InvalidOperand);
                                }
                            }
                        } else {
                            // this really means POWER1
                            return Err(DecodeError::InvalidOpcode);
                        }

                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::MemoryIndexed {
                            base: Register { reg: ra as u8 },
                            index: Register { reg: rb as u8 },
                        });
                    }
                    Opcode::STFDP => {
                        let xo = word & 0b11;
                        if xo != 00 {
                            return Err(DecodeError::InvalidOpcode);
                        }

                        let rt = (word >> 21) & 0x1f;
                        let ra = (word >> 16) & 0x1f;
                        let ds = (word >> 2) & 0x3fff;

                        if rt & 1 != 0 {
                            return Err(DecodeError::InvalidOperand);
                        }

                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::MemoryDisp { base: Register { reg: ra as u8 }, disp: (ds << 2) as i16 });
                    }
                    Opcode::LFQUX => {
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::MemoryIndexed {
                            base: Register { reg: ra as u8 },
                            index: Register { reg: rb as u8 },
                        });
                    }
                    Opcode::LFDPX => {
                        if self.version == IsaVersion::RIOS2 {
                            inst.opcode = Opcode::LFQX;
                        } else if self.version.supports(IsaConstraint::PowerIsa205) {
                            if rt & 1 != 0 {
                                // > If FRTp is odd, the instruction form is invalid
                                //
                                // .. objdump falls back to LFQX, but only do that if we're
                                // `IsaVersion::All`:
                                if self.version == IsaVersion::All {
                                    inst.opcode = Opcode::LFQX;
                                } else {
                                    return Err(DecodeError::InvalidOperand);
                                }
                            }
                        } else {
                            // this really means POWER1
                            return Err(DecodeError::InvalidOpcode);
                        }

                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::MemoryIndexed {
                            base: Register { reg: ra as u8 },
                            index: Register { reg: rb as u8 },
                        });
                    }
                    Opcode::DDEDPD => {
                        inst.operands[0] = Some(Operand::Imm8((ra >> 3) as u8));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                        inst.rc = lk != 0;
                    }
                    Opcode::DENBCD => {
                        inst.operands[0] = Some(Operand::Imm8((ra >> 4) as u8));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                        inst.rc = lk != 0;
                    }
                    Opcode::DDEDPDQ => {
                        inst.operands[0] = Some(Operand::Imm8((ra >> 3) as u8));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: rt as u8 & !1 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 & !1 }));
                        inst.rc = lk != 0;
                    }
                    Opcode::DENBCDQ => {
                        inst.operands[0] = Some(Operand::Imm8((ra >> 4) as u8));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: rt as u8 & !1 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 & !1 }));
                        inst.rc = lk != 0;
                    }
                    Opcode::TRECHKPT => {
                        if lk == 0 {
                            return Err(DecodeError::InvalidOpcode);
                        }
                        inst.rc = true;
                    }
                    Opcode::TABORT |
                    Opcode::TRECLAIM => {
                        if lk == 0 {
                            return Err(DecodeError::InvalidOpcode);
                        }
                        inst.rc = true;
                        inst.operands[0] = Some(Operand::Register(Register { reg: ra as u8 }));
                    }
                    Opcode::TABORTDC |
                    Opcode::TABORTWC => {
                        if lk == 0 {
                            return Err(DecodeError::InvalidOpcode);
                        }
                        inst.rc = true;
                        inst.operands[0] = Some(Operand::Imm8(rt as u8));
                        inst.operands[1] = Some(Operand::Register(Register { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::Register(Register { reg: rb as u8 }));
                    }
                    Opcode::TABORTDCI |
                    Opcode::TABORTWCI => {
                        if lk == 0 {
                            return Err(DecodeError::InvalidOpcode);
                        }
                        inst.rc = true;
                        inst.operands[0] = Some(Operand::Imm8(rt as u8));
                        inst.operands[1] = Some(Operand::Register(Register { reg: ra as u8 }));
                        let extended = (((rb as i8) << 3) >> 3) as i16;
                        inst.operands[2] = Some(Operand::ImmExts16(extended));
                    }
                    Opcode::TBEGIN => {
                        if lk == 0 {
                            return Err(DecodeError::InvalidOpcode);
                        }
                        inst.rc = true;
                        let a = rt as u8 & 1;
                        inst.operands[0] = Some(Operand::Imm8(a));
                    }
                    Opcode::TEND => {
                        if lk == 0 {
                            return Err(DecodeError::InvalidOpcode);
                        }
                        inst.rc = true;
                        let a = rt as u8 >> 4;
                        inst.operands[0] = Some(Operand::Imm8(a));
                    }
                    Opcode::TSR => {
                        if lk == 0 {
                            return Err(DecodeError::InvalidOpcode);
                        }
                        inst.rc = true;
                        let l = rt as u8 & 1;
                        inst.operands[0] = Some(Operand::Imm8(l));
                    }
                    Opcode::TCHECK => {
                        let bf = rt as u8 >> 2;
                        inst.operands[0] = Some(Operand::CrField(bf));
                    }
                    _ => {
                        // TODO: the rest?
                    }
                }
            }
            Format::XS => {
                let rs = (word >> 21) & 0x1f;
                let ra = (word >> 16) & 0x1f;
                let sh_lo = (word >> 11) & 0x1f;
                let rc = word & 1;
                let sh_hi = (word >> 1) & 1;
                let sh = (sh_hi << 5) | sh_lo;

                inst.operands[0] = Some(Operand::Register(Register { reg: ra as u8 }));
                inst.operands[1] = Some(Operand::Register(Register { reg: rs as u8 }));
                inst.operands[2] = Some(Operand::Imm8(sh as u8));
                inst.rc = rc != 0;
            }
            Format::XO => {
                let rt = (word >> 21) & 0x1f;
                let ra = (word >> 16) & 0x1f;
                let rb = (word >> 11) & 0x1f;
                let _xo = (word >> 1) & 0x1_ff;
                let oe = (word >> 10) & 1;
                let rc = word & 1;

                // for all XO-form other than `mulhw`, `mulhwu`, `mulhd`, `mulhdu`
                if ![Opcode::MULHW, Opcode::MULHWU, Opcode::MULHD, Opcode::MULHDU].contains(&opc) {
                    inst.oe = oe != 0;
                }

                inst.rc = rc != 0;

                inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                inst.operands[1] = Some(Operand::Register(Register { reg: ra as u8 }));
                // `neg`, `addze`, `addfze`, `subfme`, `addme` are only `rt, ra`
                // also `abs` and `nabs`, for the older deleted isntructions.
                if ![
                    Opcode::NEG, Opcode::ABS, Opcode::NABS,
                    Opcode::ADDME, Opcode::SUBFME, Opcode::ADDZE, Opcode::SUBFZE].contains(&opc) {
                    inst.operands[2] = Some(Operand::Register(Register { reg: rb as u8 }));
                }
            }
            Format::XL => {
                let bt = (word >> 21) & 0x1f;
                let ba = (word >> 16) & 0x1f;
                let bb = (word >> 11) & 0x1f;
                let _xo = (word >> 1) & 0x1_ff;
                let lk = word & 1;

                match opc {
                    Opcode::BCLR | Opcode::BCCTR => {
                        if self.version == IsaVersion::RIOS || self.version == IsaVersion::RIOS2 {
                            // in ye olde POWER/POWER2, there was just no branch hinting.
                            inst.hint_style = None;
                        } else if !self.version.supports(IsaConstraint::PowerIsa200) || self.version == IsaVersion::e500 {
                            // if the ISA is old, the BO field (namely, branch hint) interpretation
                            // is different, and yax describes that using a different set of branch
                            // opcodes.
                            inst.hint_style = Some(HintStyle::Legacy);
                        } else {
                            inst.hint_style = Some(HintStyle::PowerIsa);
                        }
                        inst.operands[0] = Some(Operand::Imm8(bt as u8));
                        inst.operands[1] = Some(Operand::CrBit(ba as u8));
                        // HELP: Power v2.02 does not indicate that the BH field for prediction
                        // hints is new, but the field was unused as of PowerPC 1994. lacking a
                        // more specific version to say this was new in, the hint is only decoded
                        // for 2.02+.
                        if self.version.supports(IsaConstraint::PowerIsa202) {
                            inst.operands[2] = Some(Operand::Imm8((bb & 0x3) as u8));
                        }
                        inst.lk = lk != 0;
                    }
                    Opcode::CRAND |
                    Opcode::CRANDC |
                    Opcode::CRNAND |
                    Opcode::CRXOR |
                    Opcode::CRNOR |
                    Opcode::CREQV |
                    Opcode::CROR |
                    Opcode::CRORC => {
                        inst.operands[0] = Some(Operand::CrBit(bt as u8));
                        inst.operands[1] = Some(Operand::CrBit(ba as u8));
                        inst.operands[2] = Some(Operand::CrBit(bb as u8));
                    }
                    Opcode::MCRF => {
                        inst.operands[0] = Some(Operand::CrField((bt >> 2) as u8));
                        inst.operands[1] = Some(Operand::CrField((ba >> 2) as u8));
                    }
                    // HELP: is there any implementation with hrfid that is not 64-bit..?
                    Opcode::HRFID |
                    Opcode::RFID => {
                        if !self.version.supports(IsaConstraint::Ppc64) {
                            return Err(DecodeError::InvalidOpcode);
                        }
                    }
                    _ => {
                        // TODO: the rest?
                    }
                }
            }
            Format::XFL => {
                let l = (word >> 25) & 1;
                let flm = (word >> 17) & 0xff;
                let w = (word >> 16) & 1;
                let rb = (word >> 11) & 0x1f;
                let _xo = (word >> 1) & 0x1_ff;
                let rc = word & 1;

                inst.operands[0] = Some(Operand::Imm8(flm as u8));
                inst.operands[1] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                // additional fields were added to `mtfsf` in 2.05.
                if self.version.supports(IsaConstraint::PowerIsa205) {
                    inst.operands[2] = Some(Operand::Imm8(l as u8));
                    inst.operands[3] = Some(Operand::Imm8(w as u8));
                }
                inst.rc = rc != 0;
            }
            Format::XFX => {
                let rt = (word >> 21) & 0x1f;
                let imm_b11 = (word >> 11) & 0x3_ff;
                let _xo = (word >> 1) & 0x1_ff;
                let _lk = word & 1;

                match opc {
                    Opcode::MTSPR => {
                        let imm_lo = imm_b11 & 0x1f;
                        let imm_hi = (imm_b11 >> 5) & 0x1f;
                        let spr_nr = (imm_lo << 5) | imm_hi;
                        inst.operands[0] = Some(Operand::SPR(SPR { reg: spr_nr as u16 }));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rt as u8 }));
                    }
                    Opcode::MFSPR => {
                        let imm_lo = imm_b11 & 0x1f;
                        let imm_hi = (imm_b11 >> 5) & 0x1f;
                        let spr_nr = (imm_lo << 5) | imm_hi;
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::SPR(SPR { reg: spr_nr as u16 }));
                    }
                    // TODO: if xfx field 0 is ff
                    Opcode::MTCRF => {
                        let fxm = (imm_b11 >> 1) & 0xff;
                        // HELP: it seems like gekko might interpret b11 as a dont-care rather than
                        // picking mtocrf
                        let supports_mtocrf = self.version.supports(IsaConstraint::Ppc) && self.version != IsaVersion::Gekko;
                        if imm_b11 & 0b10_0000_0000 != 0 && supports_mtocrf {
                            // see doc comment on IsaVersion
                            inst.opcode = Opcode::MTOCRF;

                            let rest = ((imm_b11 & 0x1_ff) >> 1) as u8;
                            if rest.count_ones() != 1 {
                                return Err(DecodeError::InvalidOpcode);
                            }

                            // in what is perhaps the most confusing assembly convention i've seen,
                            // the CR field mask is printed as an immediate, rather than the one
                            // CRF that the field selects. even though this field is a one-hot mask
                            // under `mtocrf`, it is not displayed as or parsed as a `crN` field
                            // like a normal condition register field. so, while it would be most
                            // accurate to report this as `Operand::CrField`, it would need a
                            // one-off (well, two-off)
                        }
                        inst.operands[0] = Some(Operand::CrMask(fxm as u8));
                        inst.operands[1] = Some(Operand::Register(Register { reg: rt as u8 }));
                    }
                    Opcode::MFCR => {
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));

                        // v2.01 Book I Appendix F:
                        // > The following instructions in the PowerPC User Instruction Set
                        // > Architecture are new; they are not in the POWER Architecture.
                        let supports_mfocrf = self.version.supports(IsaConstraint::Ppc) && self.version != IsaVersion::Gekko;
                        if imm_b11 & 0b10_0000_0000 != 0 && supports_mfocrf {
                            if self.version.includes_ppc() {
                                inst.opcode = Opcode::MFOCRF;
                                let rest = ((imm_b11 & 0x1_ff) >> 1) as u8;
                                if rest.count_ones() != 1 {
                                    return Err(DecodeError::InvalidOpcode);
                                }
                                // see the note on MTOCRF above about why this is a `CrMask`
                                // instead of `CrField`...
                                inst.operands[1] = Some(Operand::CrMask(rest as u8));
                            } else {
                                return Err(DecodeError::InvalidOpcode);
                            }
                        }
                    }
                    Opcode::MFTB => {
                        // TODO: Server.Phased-out
                        // book v2.03:
                        // > Processors for which mfspr cannot be used to read the Time Base
                        // > include the following.
                        // > - 601
                        // > - POWER3
                        // >
                        // .. the book goes on to remark that the 601 is expected to use !MFTB
                        // anyway, because the illegal instruction handler is expected to emulate
                        // both `mftb` and the `Time Base` in the first place. "new programs should
                        // use mfspr instead of mftb".
                        inst.operands[0] = Some(Operand::Register(Register { reg: rt as u8 }));
                        // time base is also accessible as an SPR, but it is a ten-bit field so
                        // reproduce it faithfully. unfortunately, also, the low and high five bits
                        // are swapped..?
                        let tbr = (word >> 11) & 0x3ff;
                        let tbr_hi = tbr & 0x1f;
                        let tbr_lo = (tbr >> 5) & 0x1f;
                        let tbr = (tbr_hi << 5) | tbr_lo;
                        inst.operands[1] = Some(Operand::ImmU16(tbr as u16));
                    }
                    // TODO: MTDCR, MFDCR
                    _ => {
                        unreachable!("other xfx-form opcodes");
                    }
                }
            }
            Format::M => {
                let rs = (word >> 21) & 0x1f;
                let ra = (word >> 16) & 0x1f;
                let sh = (word >> 11) & 0x1f;
                let mb = (word >> 6) & 0x1f;
                let me = (word >> 1) & 0x1f;
                let rc = word & 1;

                inst.rc = rc != 0;

                inst.operands[0] = Some(Operand::Register(Register { reg: ra as u8 }));
                inst.operands[1] = Some(Operand::Register(Register { reg: rs as u8 }));
                if inst.opcode == Opcode::RLWNM {
                    inst.operands[2] = Some(Operand::Register(Register { reg: sh as u8 }));
                } else {
                    inst.operands[2] = Some(Operand::Imm8(sh as u8));
                }
                inst.operands[3] = Some(Operand::Imm8(mb as u8));
                inst.operands[4] = Some(Operand::Imm8(me as u8));
            }
            Format::E => {
                let rs = (word >> 21) & 0x1f;
                let _ra = (word >> 16) & 0x1f;
                let rt = (word >> 11) & 0x1f;

                inst.operands[0] = None;
                inst.operands[1] = None;
                inst.operands[2] = None;
                inst.operands[3] = None;

                match inst.opcode {
                    Opcode::WRTEE => {
                        inst.operands[0] = Some(Operand::Register(Register {reg: rs as u8 }));
                    }
                    Opcode::WRTEEI => {
                        inst.operands[0] = Some(Operand::Imm8((rt >> 4) as u8));
                    }
                    _ => {
                    }
                }
            }
            Format::MD => {
                let rs = (word >> 21) & 0x1f;
                let ra = (word >> 16) & 0x1f;
                let sh_lo = (word >> 11) & 0x1f;
                let sh_hi = (word >> 1) & 0x1;

                let mb_lo = (word >> 6) & 0x1f;
                let mb_hi = (word >> 5) & 0x1;
                let mb = (mb_hi << 5) | mb_lo;
                let rc = word & 1;

                inst.rc = rc != 0;
                let sh = (sh_hi << 5) | sh_lo;

                inst.operands[0] = Some(Operand::Register(Register { reg: ra as u8 }));
                inst.operands[1] = Some(Operand::Register(Register { reg: rs as u8 }));
                inst.operands[2] = Some(Operand::Imm8(sh as u8));
                inst.operands[3] = Some(Operand::Imm8(mb as u8));
            }
            Format::MDS => {
                let rs = (word >> 21) & 0x1f;
                let ra = (word >> 16) & 0x1f;
                let rb = (word >> 11) & 0x1f;
                let mb_lo = (word >> 6) & 0x1f;
                let mb_hi = (word >> 5) & 0x1;
                let mb = (mb_hi << 5) | mb_lo;
                let rc = word & 1;

                inst.rc = rc != 0;

                inst.operands[0] = Some(Operand::Register(Register { reg: ra as u8 }));
                inst.operands[1] = Some(Operand::Register(Register { reg: rs as u8 }));
                inst.operands[2] = Some(Operand::Register(Register { reg: rb as u8 }));
                inst.operands[3] = Some(Operand::Imm8(mb as u8));
            }
            Format::VX => {
                let rt = (word >> 21) & 0x1f;
                let ra = (word >> 16) & 0x1f;
                let rb = (word >> 11) & 0x1f;

                match inst.opcode {
                    Opcode::VSL | Opcode::VSLO |
                    Opcode::VSR | Opcode::VSRO |

                    Opcode::VADDCUW |
                    Opcode::VADDSBS | Opcode::VADDSHS | Opcode::VADDSWS |
                    Opcode::VADDUBM | Opcode::VADDUHM | Opcode::VADDUWM |
                    Opcode::VADDUBS | Opcode::VADDUHS | Opcode::VADDUWS |
                    Opcode::VSUBCUW |
                    Opcode::VSUBSBS | Opcode::VSUBSHS | Opcode::VSUBSWS |
                    Opcode::VSUBUBM | Opcode::VSUBUHM | Opcode::VSUBUWM |
                    Opcode::VSUBUBS | Opcode::VSUBUHS | Opcode::VSUBUWS |

                    Opcode::VMULESB | Opcode::VMULESH |
                    Opcode::VMULEUB | Opcode::VMULEUH |
                    Opcode::VMULOSB | Opcode::VMULOSH |
                    Opcode::VMULOUB | Opcode::VMULOUH |

                    Opcode::VSUMSWS | Opcode::VSUM2SWS |
                    Opcode::VSUM4SBS | Opcode::VSUM4SHS | Opcode::VSUM4UBS |

                    Opcode::VAVGSB | Opcode::VAVGSH | Opcode::VAVGSW |
                    Opcode::VAVGUB | Opcode::VAVGUH | Opcode::VAVGUW |
                    Opcode::VMAXSB | Opcode::VMAXSH | Opcode::VMAXSW |
                    Opcode::VMAXUB | Opcode::VMAXUH | Opcode::VMAXUW |
                    Opcode::VMINSB | Opcode::VMINSH | Opcode::VMINSW |
                    Opcode::VMINUB | Opcode::VMINUH | Opcode::VMINUW |

                    Opcode::VAND | Opcode::VANDC |
                    Opcode::VNOR | Opcode::VOR | Opcode::VXOR |
                    Opcode::VRLB | Opcode::VRLH | Opcode::VRLW |
                    Opcode::VSLB | Opcode::VSLH | Opcode::VSLW |
                    Opcode::VSRAB | Opcode::VSRAH | Opcode::VSRAW |
                    Opcode::VSRB | Opcode::VSRH | Opcode::VSRW |

                    Opcode::VADDFP | Opcode::VSUBFP |
                    Opcode::VMAXFP | Opcode::VMINFP |

                    Opcode::VPKSHSS | Opcode::VPKSHUS |
                    Opcode::VPKSWSS | Opcode::VPKSWUS |
                    Opcode::VPKUHUM | Opcode::VPKUHUS |
                    Opcode::VPKUWUM | Opcode::VPKUWUS |
                    Opcode::VMRGHB | Opcode::VMRGHH | Opcode::VMRGHW |
                    Opcode::VMRGLB | Opcode::VMRGLH | Opcode::VMRGLW |
                    Opcode::VPKPX => {
                        inst.operands[0] = Some(Operand::VR(VR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::VR(VR { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::VR(VR { reg: rb as u8 }));
                    }
                    Opcode::VRFIM | Opcode::VRFIN | Opcode::VRFIP | Opcode::VRFIZ |
                    Opcode::VEXPTEFP | Opcode::VLOGEFP |
                    Opcode::VREFP | Opcode::VRSQRTEFP |
                    Opcode::VUPKLPX | Opcode::VUPKLSB |
                    Opcode::VUPKLSH |
                    Opcode::VUPKHPX | Opcode::VUPKHSB |
                    Opcode::VUPKHSH => {
                        inst.operands[0] = Some(Operand::VR(VR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::VR(VR { reg: rb as u8 }));
                    }
                    Opcode::VCTSXS | Opcode::VCTUXS |
                    Opcode::VCFSX | Opcode::VCFUX => {
                        inst.operands[0] = Some(Operand::VR(VR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::VR(VR { reg: rb as u8 }));
                        inst.operands[2] = Some(Operand::Imm8(ra as u8));
                    }
                    Opcode::VSPLTB => {
                        inst.operands[0] = Some(Operand::VR(VR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::VR(VR { reg: rb as u8 }));
                        inst.operands[2] = Some(Operand::Imm8(ra as u8 & 0b1111));
                    }
                    Opcode::VSPLTH => {
                        inst.operands[0] = Some(Operand::VR(VR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::VR(VR { reg: rb as u8 }));
                        inst.operands[2] = Some(Operand::Imm8(ra as u8 & 0b111));
                    }
                    Opcode::VSPLTW => {
                        inst.operands[0] = Some(Operand::VR(VR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::VR(VR { reg: rb as u8 }));
                        inst.operands[2] = Some(Operand::Imm8(ra as u8 & 0b11));
                    }
                    Opcode::MTVSCR => {
                        inst.operands[0] = Some(Operand::VR(VR { reg: rb as u8 }));
                    }
                    Opcode::MFVSCR => {
                        inst.operands[0] = Some(Operand::VR(VR { reg: rt as u8 }));
                    }
                    other => {
                        panic!("unhandled VX-form instruction {:?}", other);
                    }
                }
            }
            Format::Z22 => {
                let rt = (word >> 21) & 0x1f;
                let ra = (word >> 16) & 0x1f;
                let sh = (word >> 10) & 0x3f;
                let lk = word & 1;
                inst.rc = lk != 0;

                match inst.opcode {
                    Opcode::DSCLI | Opcode::DSCRI => {
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::Imm8(sh as u8));
                    }
                    Opcode::DSCLIQ | Opcode::DSCRIQ => {
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 & !1 }));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 & !1 }));
                        inst.operands[2] = Some(Operand::Imm8(sh as u8));
                    }
                    Opcode::DTSTDC | Opcode::DTSTDG => {
                        inst.operands[0] = Some(Operand::CrField((rt >> 2) as u8));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8}));
                        inst.operands[2] = Some(Operand::Imm8(sh as u8));
                    }
                    Opcode::DTSTDCQ | Opcode::DTSTDGQ => {
                        inst.operands[0] = Some(Operand::CrField((rt >> 2) as u8));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 & !1 }));
                        inst.operands[2] = Some(Operand::Imm8(sh as u8));
                    }
                    _other => {
                        #[cfg(test)]
                        unreachable!("new Z22-form opcode: {:?}", inst.opcode);
                    }
                }
            }
            Format::Z23 => {
                let rt = (word >> 21) & 0x1f;
                let ra = (word >> 16) & 0x1f;
                let rb = (word >> 11) & 0x1f;
                let rmc = (word >> 9) & 0x3;
                let lk = word & 1;
                inst.rc = lk != 0;

                match inst.opcode {
                    Opcode::DQUAI => {
                        // > TE is a 5-bit signed bianry integer.
                        let te = ((ra << 3) as i8) >> 3;
                        inst.operands[0] = Some(Operand::ImmExts16(te as i16));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                        inst.operands[3] = Some(Operand::Imm8(rmc as u8));
                    }
                    Opcode::DQUAIQ => {
                        // paired registers are masked to even by objdump, we'll do similar to
                        // match..
                        inst.operands[0] = Some(Operand::Imm8(ra as u8));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: rt as u8 & !1 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 & !1 }));
                        inst.operands[3] = Some(Operand::Imm8(rmc as u8));
                    }
                    Opcode::DQUA | Opcode::DRRND => {
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                        inst.operands[3] = Some(Operand::Imm8(rmc as u8));
                    }
                    Opcode::DQUAQ => {
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 & !1 }));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 & !1 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 & !1 }));
                        inst.operands[3] = Some(Operand::Imm8(rmc as u8));
                    }
                    Opcode::DRRNDQ => {
                        inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 & !1 }));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: ra as u8 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 & !1 }));
                        inst.operands[3] = Some(Operand::Imm8(rmc as u8));
                    }
                    Opcode::DRINTN | Opcode::DRINTX => {
                        inst.operands[0] = Some(Operand::Imm8(ra as u8 & 1));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 }));
                        inst.operands[3] = Some(Operand::Imm8(rmc as u8));
                    }
                    Opcode::DRINTNQ | Opcode::DRINTXQ => {
                        inst.operands[0] = Some(Operand::Imm8(ra as u8 & 1));
                        inst.operands[1] = Some(Operand::FPR(FPR { reg: rt as u8 & !1 }));
                        inst.operands[2] = Some(Operand::FPR(FPR { reg: rb as u8 & !1 }));
                        inst.operands[3] = Some(Operand::Imm8(rmc as u8));
                    }
                    _other => {
                        #[cfg(test)]
                        unreachable!("new Z23-form opcode: {:?}", inst.opcode);
                    }
                }
            }
            Format::PS_LST => {
                let rt = (word >> 21) & 0x1f;
                let ra = (word >> 16) & 0x1f;
                let w = (word >> 15) & 1;
                let gqr = (word >> 12) & 0b111;
                let offset = (word & 0xfff) as i16;
                let disp = (offset << 4) >> 4;
                inst.operands[0] = Some(Operand::FPR(FPR { reg: rt as u8 }));
                inst.operands[1] = Some(Operand::MemoryDisp { base: Register { reg: ra as u8 }, disp });
                inst.operands[2] = Some(Operand::Imm8(w as u8));
                inst.operands[3] = Some(Operand::GQR(GQR { reg: gqr as u8 }));
            }
            Format::VC |
            Format::VCdot => {
                // VC and VCdot are only produced in vector instruction decoding
            }
        }
        Ok(())
    }
}