binscout 0.6.0

Headless, agent-first binary analysis: functions, CFGs, xrefs, strings, and IR over a JSON CLI or MCP server
Documentation
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
//! M3 IR: minimal P-code-inspired lift over [`Insn`].
//!
//! One machine instruction lifts to one [`IrInsn`]. The `Vec` return reserves
//! multi-op expansion (e.g. `push` as adjust-plus-store); M3 always emits a
//! single element. Operands reuse the disassembler's strings verbatim and
//! `target` carries direct branch/call destinations from
//! [`Insn::branch_target`]. Flag and stack-pointer effects are scoped:
//! integer add/sub/cmp record [`IrEffects::sets_flags`] and x86
//! push/pop/call/operand-free-ret record [`IrEffects::sp_delta`];
//! everything else reports none (see [`IrEffects`] for the bounds).
//!
//! [`IrOp::Phi`] is never emitted by these lifters; M4 synthesizes it at CFG
//! merges. Unknown mnemonics become [`IrOp::Nop`] with `warning` set instead
//! of failing, so analysis can proceed over partial lifts.
//!
//! # ISA coverage
//!
//! `lift_x86` covers both 32- and 64-bit x86 (same mnemonic table; only the
//! decoder differs). The `rep`/`repe`/`repne`/`lock` prefixes are stripped by
//! the disassembler, so prefixed string/atomic operations lift as their core
//! mnemonic (`rep movsb` lifts as `movsb`):
//!
//! - `COPY`: `mov`, `movsx`, `movsxd`, `movzx`, `movabs`, `movbe`, `xchg`,
//!   `leave`, `enter`, `cbw`/`cwde`/`cdqe`/`cdq`/`cqo`, `bswap`, all 16
//!   `cmovcc`/`setcc` forms, `movs`/`movsb`/`movsw`/`movsd`/`movsq`, scalar
//!   and packed SSE/AVX moves (`movss`, `movaps`, `movups`, `movdqa`,
//!   `vmovaps`, ...), shuffles and unpacks (`shufps`, `pshufb`, `unpcklps`,
//!   `vshufps`, ...), inserts/extracts/broadcasts/converts (`pinsrw`,
//!   `vextractps`, `vbroadcastss`, `vpmovsxbw`, ...), `bextr`/`pdep`/`pext`,
//!   x87 `fld1`/`fabs`/`fxch`/`fcmovcc`/`fnstsw`, `rdtsc`/`rdtscp`/`rdpmc`,
//!   `rdrand`/`rdseed`, `xgetbv`.
//! - `LOAD`: `pop`, `popa`, `popad`, `popf`/`popfd`/`popfq`, `lods*`,
//!   `xlat`, `in`/`ins*`, `movntdqa`, x87 `fld`/`fild`/`fbld`/`fldenv`/
//!   `fldcw`, `frstor`/`fxrstor`/`xrstor` (x87 stack/control effects
//!   unmodeled).
//! - `STORE`: `push`, `pusha`, `pushad`, `pushf`/`pushfd`/`pushfq`,
//!   `stos*`, `out`/`outs*`, nontemporal `movnt*`/`vmovnt*`, x87
//!   `fst`/`fstp`/`fist*`/`fbstp`/`fnstcw`/`fnstenv`/`fnsave`/`fxsave`/
//!   `xsave`.
//! - `ADD`: `add`, `adc`, `adcx`, `adox`, `lea` (address math, not a load),
//!   `inc`, x87 `fadd`/`faddp`/`fiadd`, packed `addps`/`addpd`/`addss`/
//!   `addsd`/`paddb`-`paddq`/`haddps`/`phaddw` plus `v`-prefixed AVX forms.
//!   `SUB`: `sub`, `sbb`, `dec`, `neg`, x87 `fsub`/`fsubp`/`fsubr`/
//!   `fisub`/`fisubr`, packed `subps`/`psubb`-`psubq`/`hsubps`/`phsubw` plus
//!   AVX, `psadbw`/`mpsadbw` (+`v` forms). `MUL`: `mul`, `imul`, `mulx`,
//!   x87 `fmul`/`fmulp`/`fimul`, packed `mulps`/`pmullw`/`pmulld`/
//!   `pmuludq`/`pmulhw`/`pmaddwd` plus AVX, and 48 FMA `vfmadd*`/`vfmsub*`/
//!   `vfnmadd*`/`vfnmsub*` forms. `DIV`: `div`, `idiv`, x87
//!   `fdiv`/`fdivp`/`fdivr`/`fidiv`/`fidivr`, packed `divps` plus AVX.
//!   `AND`: `and`, `pand`/`vpand`, `pandn`/`vpandn`, `andps`/`andpd`/
//!   `vandps`/`vandpd`, `andn`, `blsi`/`blsr`/`bzhi`. `OR`: `or`,
//!   `por`/`vpor`, `orps`/`orpd`/`vorps`/`vorpd`. `XOR`: `xor`, `pxor`/
//!   `vpxor`, `not`, `xorps`/`xorpd`/`vxorps`/`vxorpd`, `blsmsk`, AES
//!   (`aesenc`, `aesenclast`, `aesdec`, `aesdeclast`, `aeskeygenassist`,
//!   `aesimc`), `pclmulqdq` (all four imm-folded spellings), SHA
//!   (`sha1msg1`, `sha1msg2`, `sha1nexte`, `sha1rnds4`, `sha256rnds2`,
//!   `sha256msg1`, `sha256msg2`), `crc32` (crypto folded to XOR).
//!   `SHL`: `shl`, `sal`, `shld`, `shlx`, `psllw`/`pslld`/`psllq`/
//!   `pslldq` plus AVX. `SHR`: `shr`, `sar`, plus rotates `rol`, `ror`,
//!   `rcl`, `rcr` (approximated as shifts; exact bit motion is out of
//!   scope), `shrd`, `sarx`/`shrx`/`rorx`, `psrlw`/`psrld`/`psrlq`/
//!   `psrldq`, `psraw`/`psrad` plus AVX, `palignr`/`vpalignr`.
//! - `CMP`: `cmp`, `test`, string `cmps*`/`scas*`, `comiss`/`comisd`/
//!   `ucomiss`/`ucomisd` (+`v` forms), `vtestps`/`vtestpd`/`vptest`,
//!   imm-folded `cmp<cc>ps`/`pd`/`ss`/`sd` and `vcmp<cc>*` pseudo-ops
//!   (`cmpeqps`, `vcmpltps`, ...), x87 `fcom`/`fcomp`/`fcompp`/`fucom*`/
//!   `fcomi*`/`fucomi*`/`ftst`/`fxam`/`ficom`/`ficomp`, `pcmpeqb`-`pcmpgtq`
//!   (+`v` forms), `pcmpestri`/`pcmpistri` (+`m` forms), `phminposuw`/
//!   `vphminposuw`.
//! - `BRANCH`: direct `jmp` (indirect keeps operands, no target).
//!   `CBRANCH`: all `jcc` forms plus `jcxz`/`jecxz`/`jrcxz` and
//!   `loop`/`loope`/`loopne`. `CALL`: `call`, `syscall`, `sysenter`, `int`,
//!   `int3`. `RETURN`: `ret`, `retn`, `retf`, `iret`, `iretd`, `iretq`,
//!   `sysret`, `sysexit`.
//! - `NOP`: `nop`, `pause`, `hlt`, `fwait`, `endbr32`, `endbr64`, fences
//!   (`mfence`, `lfence`, `sfence`), `clflush`/`clflushopt`/`clwb`, `emms`,
//!   `femms`, `prefetch*`, flag-only `clc`/`stc`/`cmc`/`cld`/`std`/`cli`/
//!   `sti`/`clac`/`stac`/`lahf`/`sahf`, `monitor`/`mwait`, `vzeroupper`/
//!   `vzeroall`, x87 `fnop`/`ffree`/`fdecstp`/`fincstp`/`fnclex`/`fninit`.
//!   Still unmapped by design: `cpuid`, `popcnt`/`tzcnt`/`lzcnt`/`bsf`/
//!   `bsr` (bit counts/scans), `cmpxchg`/`xadd` (conditional/atomic RMW),
//!   `ud2`, x87 transcendentals.
//!
//! `lift_aarch64` covers A64 mnemonics as printed by the disassembler (which
//! folds common aliases: `movz`/`movn`/`orr`-with-zero print as `mov`,
//! `orn`/`not` print as `mvn`, `adds`-with-zero-dest prints as `cmn`,
//! `sbc`-with-zero prints as `ngc`, `madd`-with-zero prints as `mul`,
//! `csinc`-with-zero prints as `cset`, `ubfm`/`sbfm`/`bfm` always print as
//! `lsl`/`lsr`/`asr`/`sxtb`/`ubfx`/`bfi`/..., `lslv`/`lsrv`/`asrv`/`rorv`
//! print as `lsl`/`lsr`/`asr`/`ror`, and `dmb`/`dsb` carry their option in
//! the operands, not the mnemonic):
//!
//! - `COPY`: `mov`, `movz`, `movn`, `movk`, `mvn`, `movi`, `dup`, `smov`,
//!   `umov`, `adr`, `adrp`, `ext`, `tbl`, `tbx`, `uzp1`/`uzp2`, `trn1`/
//!   `trn2`, `zip1`/`zip2`, `rev`/`rev16`/`rev32`/`rev64`, `rbit`,
//!   `csel`/`csinc`/`csinv`/`csneg` (plus `cinc`/`cset`/`cinv`/`csetm`/
//!   `cneg` spellings), `fcsel`, `uxtb`/`uxth`/`sxtb`/`sxth`/`sxtw`,
//!   `ubfx`/`sbfx`/`ubfiz`/`sbfiz`/`bfc`/`bfi`/`bfxil`, `extr`, `clz`,
//!   `cls`, `cnt`, `abs`, `sqabs`, narrowing `xtn`/`xtn2`/`sqxtn`/
//!   `sqxtn2`/`sqxtun`/`sqxtun2`/`uqxtn`/`uqxtn2`, FP `fmov`/`fabs`,
//!   `scvtf`/`ucvtf`, `fcvt`/`fcvtl`/`fcvtn`/`fcvtxn`/`fcvtns`/`fcvtnu`/
//!   `fcvtps`/`fcvtpu`/`fcvtms`/`fcvtmu`/`fcvtas`/`fcvtau`/`fcvtzs`/
//!   `fcvtzu`/`fjcvtzs`/`bfcvt`, `frint*`, min/max selects (`fmax`/`fmin`/
//!   `fmaxnm`/`fminnm`/`smax`/`smin`/`umax`/`umin` and `p`/`v` forms),
//!   `frecpe`/`frecps`/`frecpx`/`frsqrte`/`frsqrts`, atomic swaps
//!   (`swp*`, store half unmodeled), atomic max/min (`ldsmax*`/
//!   `ldsmin*`/`ldumax*`/`ldumin*`, store half unmodeled).
//! - `LOAD`: `ldr`, `ldp`, `ldpsw`, `ldur`, `ldrb`, `ldrh`, `ldrsb`,
//!   `ldrsh`, `ldrsw`, unscaled `ldurb`-`ldursw`, unprivileged
//!   `ldtr`-`ldtrsw`, acquire/exclusive `ldar*`/`ldaxr*`/`ldxr*`/`ldlar*`/
//!   `ldapr*`/`ldapur*`, `ldnp`, SIMD `ld1`-`ld4`/`ld1r`-`ld4r`,
//!   pointer-authenticated `ldraa`/`ldrab`. `STORE`: `str`, `stp`, `stur`,
//!   `strb`, `strh`, unscaled `sturb`/`sturh`, unprivileged `sttr*`,
//!   release/exclusive `stlr*`/`stxr*`/`stllr*`/`stlur*`, `stnp`, SIMD
//!   `st1`-`st4`, store-only atomics (`stadd*`, `stclr*`, `stset*`,
//!   `steor*`, `stsmax*`, `stsmin*`, `stumax*`, `stumin*`; read-modify-write
//!   folded to STORE, ALU part unmodeled).
//! - `ADD`: `add`, `adds`, `adc`, `adcs`, FP `fadd`/`faddp`, `fcadd`, `addv`, `saddlv`/`uaddlv`, `saddlp`/`sadalp`/
//!   `uaddlp`/`uadalp`, `addp`, long `saddl`/`saddw`/`uaddl`/`uaddw` (+`2`),
//!   halving `shadd`/`uhadd`/`srhadd`/`urhadd`, saturating `sqadd`/
//!   `uqadd`/`suqadd`/`usqadd`, `saba`/`uaba`, narrowing `addhn`/
//!   `raddhn` (+`2`), atomic `ldadd*`. `SUB`: `sub`, `subs`, `sbc`,
//!   `sbcs`, `neg`, `negs`, `ngc`, `ngcs`, `fsub`, `fneg`, `sqneg`,
//!   `fabd`, long `ssubl`/`ssubw`/`usubl`/`usubw` (+`2`), halving
//!   `shsub`/`uhsub`, saturating `sqsub`/`uqsub`, `sabd`/`uabd`,
//!   narrowing `subhn`/`rsubhn` (+`2`), `sabdl`/`uabdl` (+`2`), `subp`/
//!   `subps`. `MUL`: `mul`, `madd`, `msub`, `mneg`, `mla`, `mls`,
//!   `smaddl`/`smsubl`/`umaddl`/`umsubl`, `smnegl`/`umnegl`, `smull`,
//!   `smulh`, `umull`, `umulh` (+`2`/`l2` long forms, `smlal`/`smlsl`/
//!   `umlsl` families), FP `fmul`/`fmulx`, `fmla`/`fmls`/`fmlal`/`fmlsl`/
//!   `fcmla` (+`2`), `fmadd`/`fmsub`/`fnmadd`/`fnmsub`/`fnmul`,
//!   polynomial `pmul`/`pmull`/`pmull2`, dot products `sdot`/`udot`,
//!   saturating `sqdmulh`/`sqrdmulh`/`sqdmull`/`sqdmlal`/`sqdmlsl`/
//!   `sqrdmlah`/`sqrdmlsh` (+`2`). `DIV`: `sdiv`, `udiv`, `fdiv`.
//!   `AND`: `and`, `ands`, `bic`, `bics`, atomic `ldclr*`. `OR`: `orr`,
//!   `orn`, bitwise selects `bsl`/`bit`/`bif`, atomic `ldset*`. `XOR`:
//!   `eor`, `eon`, `mvni`, `eor3`, `bcax`, `rax1`, `xar`, AES
//!   (`aese`/`aesd`/`aesmc`/`aesimc`), SHA-1/SHA-2/SHA-512
//!   (`sha1c`/`sha1h`/`sha1m`/`sha1p`/`sha1su0`/`sha1su1`/`sha256h`/
//!   `sha256h2`/`sha256su0`/`sha256su1`/`sha512h`/`sha512h2`/`sha512su0`/
//!   `sha512su1`), SM3/SM4 (`sm3tt1a`-`sm3ss1`, `sm4e`/`sm4ekey`), CRC
//!   (`crc32b`/`crc32h`/`crc32w`/`crc32x` and `cb`/`ch`/`cw`/`cx`),
//!   atomic `ldeor*` (crypto folded to XOR). `SHL`: `lsl`, `lslv`, `shl`,
//!   `sshl`/`ushl`/`srshl`/`urshl`/`sqrshl`/`uqrshl`, saturating
//!   `sqshl`/`uqshl`/`sqshlu`, `sli`, long `shll`/`sshll`/`ushll` (+`2`).
//!   `SHR`: `lsr`, `lsrv`, `asr`, `asrv`, `ror`, `rorv`, `ushr`/`sshr`/
//!   `urshr`/`srshr`, accumulate `usra`/`ssra`/`ursra`/`srsra`, `sri`,
//!   narrowing `shrn`/`rshrn`/`sqshrn`/`sqrshrn`/`sqshrun`/`sqrshrun`/
//!   `uqshrn`/`uqrshrn` (+`2`).
//! - `CMP`: `cmp`, `cmn`, `tst`, `ccmn`, `ccmp`, `cmtst`, FP `fcmp`/
//!   `fcmpe`/`fccmp`/`fccmpe`, vector `cmeq`/`cmgt`/`cmge`/`cmle`/
//!   `cmlt`/`cmhi`/`cmhs`/`cmtst`, FP `fcmeq`/`fcmge`/`fcmgt`/`fcmle`/
//!   `fcmlt`/`facge`/`facgt`, compare-and-swap `cas*` (conditional store
//!   unmodeled). `BRANCH`: direct `b`/`br` (indirect keeps operands).
//!   `CBRANCH`: `cbz`, `cbnz`, `tbz`, `tbnz`, `b.<cond>`, `bc.<cond>`.
//!   `CALL`: direct `bl`/`blr` (indirect keeps operands) plus `svc`,
//!   `hvc`, `smc`. `RETURN`: `ret`, `eret`. `NOP`: `nop`, `hint`,
//!   `dmb`, `dsb`, `isb`, `brk`, `hlt`, barriers `sb`/`ssbb`/`pssbb`,
//!   `clrex`, waits/events `wfe`/`wfi`/`sev`/`sevl`/`yield`,
//!   `esb`/`csdb`/`psb`/`tsb`, prefetches `prfm`/`prfum`, `sys`/`sysl`,
//!   flag sets `rmif`/`setf8`/`setf16`, `dcps1`-`dcps3`/`drps`. Still
//!   unmapped by design: `mrs`/`msr` (system registers), `udf`, pointer
//!   authentication, MTE tags.
//!
//! `lift_arm32` covers ARMv7/ARM32 and Thumb mnemonics as printed by the
//! disassembler (one `.w`/`.n` width suffix, one condition suffix, and one
//! flag `s` stripped first; the decoder has no VFP/NEON or media
//! instructions, so those stay unmapped rather than mislifting):
//!
//! - `COPY`: `mov`, `movw`, `movt`, `mvn`, `adr`, `swp`, `swpb`, `clz`,
//!   `rev`, `rev16`, `revsh`, `sxth`, `sxtb`, `uxth`, `uxtb`. `LOAD`:
//!   `ldr`, `ldrb`, `ldrh`, `ldrsb`, `ldrsh`, `ldrd`, `ldm` (plus `ldmia`/
//!   `ldmdb`/`ldmda`), unprivileged `ldrt`/`ldrbt`/`ldrht`/`ldrsbt`/
//!   `ldrsht`, exclusives `ldrex`/`ldrexb`/`ldrexh`/`ldrexd`
//!   (`pop`/`ldm*` naming `pc` lifts to `RETURN`, or `CBRANCH` when
//!   conditioned). `STORE`: `str`, `strb`, `strh`, `strd`, `stm` (plus
//!   `stmia`/`stmdb`/`stmda`), unprivileged `strt`/`strbt`/`strht`,
//!   exclusives `strex`/`strexb`/`strexh`/`strexd`, `push`.
//! - `ADD`: `add`, `adc`, saturating `qadd`, `qdadd`, `qadd16`, `qadd8`,
//!   `uqadd16`, `uqadd8`. `SUB`: `sub`, `sbc`, `rsb`, `rsc`, saturating
//!   `qsub`, `qdsub`, `qsub16`, `qsub8`, `uqsub16`, `uqsub8`. `MUL`:
//!   `mul`, `mla`, `umaal`, `mls`, halfword `smlabb`/`smlabt`/`smlatb`/
//!   `smlatt`, `smlawb`/`smlawt`, `smulbb`/`smulbt`/`smultb`/`smultt`,
//!   `smlalbb`/`smlalbt`/`smlaltb`/`smlaltt`. `DIV`: `sdiv`, `udiv`
//!   (Thumb only; ARM-mode divide is outside the decoder). `AND`: `and`,
//!   `bic`. `OR`: `orr`, `orn`. `XOR`: `eor`. `SHL`: `lsl`. `SHR`:
//!   `lsr`, `asr`, `ror`, `rrx`.
//! - `CMP`: `cmp`, `cmn`, `tst`, `teq`. `BRANCH`: plain `b`/`bx` (indirect
//!   keeps operands), `tbb`/`tbh`. `CBRANCH`: conditioned `b`/`bx`,
//!   `cbz`/`cbnz`. `CALL`: direct `bl`/`blx` (indirect keeps operands) plus
//!   `svc`, `hvc`, `smc`. `RETURN`: `pop`/`ldm*` with `pc`, plain `bx lr`.
//!   `NOP`: `nop`, `dmb`, `dsb`, `isb`, `bkpt`, `it`, `hint`, `wfe`/
//!   `wfi`/`sev`/`yield`, `cpsid`/`cpsie`, `setend`, `dbg` (mostly Thumb;
//!   ARM-mode hints/barriers are outside the decoder).
//!
//! # Warning semantics
//!
//! Lifting never fails: a mnemonic outside all tables (e.g. x86 `cpuid`,
//! ARM `mrs`) lifts to [`IrOp::Nop`] with `warning` set to
//! `"unlifted '<mnem>'; emitted NOP"`. `warning` is `None` for every mapped
//! mnemonic, so `warning.is_some()` exactly marks approximate coverage.
//! Downstream passes treat unlifted instructions as fall-through (`Seq`),
//! never as control flow.
//!
//! JSON pairs `addr` with `addr_hex` and `target` with `target_hex`
//! (see [`crate::addr_hex`]).
use serde::{Deserialize, Serialize};

use crate::disasm::{Arch, Insn, resolve_adrp_pair};

/// Minimal P-code-inspired operation set.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum IrOp {
    /// Register-to-register or memory-to-register copy.
    Copy,
    /// Memory read.
    Load,
    /// Memory write.
    Store,
    /// Addition.
    Add,
    /// Subtraction.
    Sub,
    /// Multiplication.
    Mul,
    /// Division.
    Div,
    /// Bitwise AND.
    And,
    /// Bitwise OR.
    Or,
    /// Bitwise XOR.
    Xor,
    /// Logical left shift.
    Shl,
    /// Logical right shift.
    Shr,
    /// Comparison (integer forms record flag writes; see [`IrEffects`]).
    Cmp,
    /// Unconditional direct branch.
    Branch,
    /// Conditional direct branch.
    CBranch,
    /// Direct call.
    Call,
    /// Function return.
    Return,
    /// Merge value, synthesized at CFG joins (never lifted).
    Phi,
    /// Unmapped or effect-free operation.
    Nop,
}

impl IrOp {
    /// Uppercase opcode name (`COPY`, `LOAD`, ...).
    #[must_use]
    pub fn name(self) -> &'static str {
        match self {
            Self::Copy => "COPY",
            Self::Load => "LOAD",
            Self::Store => "STORE",
            Self::Add => "ADD",
            Self::Sub => "SUB",
            Self::Mul => "MUL",
            Self::Div => "DIV",
            Self::And => "AND",
            Self::Or => "OR",
            Self::Xor => "XOR",
            Self::Shl => "SHL",
            Self::Shr => "SHR",
            Self::Cmp => "CMP",
            Self::Branch => "BRANCH",
            Self::CBranch => "CBRANCH",
            Self::Call => "CALL",
            Self::Return => "RETURN",
            Self::Phi => "PHI",
            Self::Nop => "NOP",
        }
    }

    /// Whether this op transfers control.
    #[must_use]
    pub fn is_control(self) -> bool {
        matches!(
            self,
            Self::Branch | Self::CBranch | Self::Call | Self::Return
        )
    }
}

/// Scoped per-instruction flag/stack effects over [`IrInsn`].
///
/// The [`IrOp`] set stays unchanged: effects ride along as data so existing
/// consumers keep working. Only the first scope is modeled:
/// [`IrEffects::sets_flags`] for integer add/sub/cmp and
/// [`IrEffects::sp_delta`] for x86 push/pop/call/operand-free-ret.
///
/// Still unmodeled: FP/SIMD/vector ALU flag writes (`fadd`, `addps`,
/// `paddb`, ... report no flags although they lift as `Add`), `lea`
/// (address math, never flags), x87 FPU-condition writers (`fcom`,
/// `ftst`, `fxam`, `ficom`, packed mask writers like `pcmpeqb`, and the
/// imm-folded `cmp<cc>`/`vcmp<cc>` pseudo-ops, which target the FPU status
/// word or a mask register instead of EFLAGS/NZCV), flag reads
/// (`adc`/`sbb`/`rsc` carry-in, `cmovcc`/`setcc` conditions), preserved
/// bits (`inc`/`dec` keep CF yet report `sets_flags`), ARM saturating `Q`
/// effects (`qadd`/`qsub`), `ret` with an immediate (extra argument pop),
/// far/privileged returns (`retf`, `iret*`) and traps
/// (`syscall`/`sysenter`/`int`, `svc`/`hvc`/`smc`), ARM multi-register
/// push/pop (the delta needs register-list decoding, so they report
/// `None`), AArch64 `bl`/`blr`/`ret` (link-register moves, no SP effect),
/// and calling-convention frame effects (`enter`/`leave`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct IrEffects {
    /// Whether the instruction writes condition flags in the common
    /// integer case: x86 EFLAGS or ARM NZCV.
    ///
    /// True only for the curated integer writers (x86 `add`/`adc`/`sub`/
    /// `sbb`/`inc`/`dec`/`neg`/`cmp`/`test`/`bt`, string compares, and the
    /// EFLAGS-writing FP compares `comiss*`/`vtest*`/`vptest`/`fcomi*`;
    /// AArch64 `adds`/`adcs`/`subs`/`sbcs`/`negs`/`ngcs` and
    /// `cmp`/`cmn`/`tst`/`ccmp`/`ccmn`/`cmtst`/`fcmp*`; ARM32/Thumb
    /// compares and `s`-suffixed add/sub). Every other mnemonic reports
    /// false, including FP/SIMD ALU forms that lift as `Add`/`Sub`/`Cmp`.
    pub sets_flags: bool,
    /// Signed stack-pointer delta in bytes (`None` means no effect or an
    /// unmodeled one).
    ///
    /// Only x86 reports values: `push`/`pop`/`pushf*`/`popf*` move one
    /// slot (-/+8 on x86-64, -/+4 on x86-32), `pusha`/`popa` move 16 and
    /// `pushad`/`popad` 32, `call` pushes one return slot, and
    /// operand-free `ret`/`retn` pop one. Assigned by [`lift`], which owns
    /// the arch context: [`lift_x86`] is mode-agnostic by design (one
    /// table for both x86 modes), so its direct rows carry flags only.
    pub sp_delta: Option<i64>,
}

/// One lifted IR operation.
///
/// JSON pairs `addr` with `addr_hex` and `target` with `target_hex`
/// (see [`crate::addr_hex`]); old rows without the twins still
/// deserialize. Rows written before effects existed (no `effects` key)
/// also still deserialize, defaulting to no modeled effects.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct IrInsn {
    /// Address of the source machine instruction.
    pub addr: u64,
    /// Lifted operation.
    pub op: IrOp,
    /// Input operands (disassembler strings).
    pub inputs: Vec<String>,
    /// Output operands (disassembler strings).
    pub outputs: Vec<String>,
    /// Absolute target for direct branches/calls, if any.
    pub target: Option<u64>,
    /// Set when the mnemonic had no mapping and `op` fell back to [`IrOp::Nop`].
    pub warning: Option<String>,
    /// Scoped flag/stack effects (see [`IrEffects`]).
    ///
    /// Defaults to no modeled effects so pre-effect rows still decode.
    #[serde(default)]
    pub effects: IrEffects,
}

impl Serialize for IrInsn {
    fn serialize<S: serde::Serializer>(
        &self,
        serializer: S,
    ) -> std::result::Result<S::Ok, S::Error> {
        use serde::ser::SerializeStruct;
        let mut st = serializer.serialize_struct("IrInsn", 8)?;
        st.serialize_field("addr", &self.addr)?;
        st.serialize_field("addr_hex", &crate::addr_hex(self.addr))?;
        st.serialize_field("op", &self.op)?;
        st.serialize_field("inputs", &self.inputs)?;
        st.serialize_field("outputs", &self.outputs)?;
        st.serialize_field("target", &self.target)?;
        st.serialize_field("target_hex", &self.target.map(crate::addr_hex))?;
        st.serialize_field("warning", &self.warning)?;
        st.serialize_field("effects", &self.effects)?;
        st.end()
    }
}

/// Lift one instruction for its architecture.
///
/// Flag effects come from the per-arch lifters; stack-slot magnitudes need
/// the mode, so `sp_delta` is assigned here where the arch is known
/// (see [`IrEffects::sp_delta`]).
#[must_use]
pub fn lift(arch: Arch, insn: &Insn) -> Vec<IrInsn> {
    let mut rows = match arch {
        Arch::X86_64 | Arch::X86_32 => lift_x86(insn),
        Arch::Aarch64 => lift_aarch64(insn),
        Arch::Arm32 | Arch::Thumb => lift_arm32(insn),
    };
    if let [row] = rows.as_mut_slice()
        && row.warning.is_none()
    {
        row.effects.sp_delta = sp_delta_for(arch, insn);
    }
    rows
}

/// Lift a decoded range, preserving order.
///
/// AArch64 pairs thread the immediate predecessor so an `adrp`+`add`/`ldr`
/// second row carries its resolved address (see
/// [`lift_aarch64_with_prev`]); every other arch lifts each instruction
/// independently.
#[must_use]
pub fn lift_range(arch: Arch, insns: &[Insn]) -> Vec<IrInsn> {
    if arch != Arch::Aarch64 {
        return insns.iter().flat_map(|i| lift(arch, i)).collect();
    }
    let mut out = Vec::new();
    for (idx, insn) in insns.iter().enumerate() {
        let prev = idx.checked_sub(1).and_then(|p| insns.get(p));
        let mut rows = lift_aarch64_with_prev(insn, prev);
        if let [row] = rows.as_mut_slice()
            && row.warning.is_none()
        {
            row.effects.sp_delta = sp_delta_for(arch, insn);
        }
        out.extend(rows);
    }
    out
}

/// Whether `mnem` has a lift mapping for `arch`.
///
/// Anything else lifts to fallback [`IrOp::Nop`] with a warning instead of
/// failing, so `!is_mapped(..)` exactly predicts `warning.is_some()` on the
/// lifted row. ARM32/Thumb reduce the mnemonic through suffix stripping
/// first, so conditioned/suffixed forms (`addeq`, `bl.w`) count as mapped.
#[must_use]
pub fn is_mapped(arch: Arch, mnem: &str) -> bool {
    match arch {
        Arch::X86_64 | Arch::X86_32 => {
            is_x86_copy(mnem)
                || is_x86_load(mnem)
                || is_x86_store(mnem)
                || is_x86_alu(mnem, IrOp::Add)
                || is_x86_alu(mnem, IrOp::Sub)
                || is_x86_alu(mnem, IrOp::Mul)
                || is_x86_alu(mnem, IrOp::Div)
                || is_x86_alu(mnem, IrOp::And)
                || is_x86_alu(mnem, IrOp::Or)
                || is_x86_alu(mnem, IrOp::Xor)
                || is_x86_alu(mnem, IrOp::Shl)
                || is_x86_alu(mnem, IrOp::Shr)
                || is_x86_cmp(mnem)
                || mnem == "jmp"
                || is_x86_jcc(mnem)
                || is_x86_call(mnem)
                || is_x86_return(mnem)
                || is_x86_nop(mnem)
        }
        Arch::Aarch64 => {
            is_arm_copy(mnem)
                || is_arm_load(mnem)
                || is_arm_store(mnem)
                || is_arm_alu(mnem, IrOp::Add)
                || is_arm_alu(mnem, IrOp::Sub)
                || is_arm_alu(mnem, IrOp::Mul)
                || is_arm_alu(mnem, IrOp::Div)
                || is_arm_alu(mnem, IrOp::And)
                || is_arm_alu(mnem, IrOp::Or)
                || is_arm_alu(mnem, IrOp::Xor)
                || is_arm_alu(mnem, IrOp::Shl)
                || is_arm_alu(mnem, IrOp::Shr)
                || ARM_CMP.contains(&mnem)
                || matches!(mnem, "b" | "br" | "bl" | "blr")
                || is_arm_cbranch(mnem)
                || is_arm_call(mnem)
                || is_arm_return(mnem)
                || is_arm_nop(mnem)
        }
        Arch::Arm32 | Arch::Thumb => arm_base_mnem(split_arm_mnem(mnem).0),
    }
}

/// How completely one [`IrOp`] family is lifted.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CoverageStatus {
    /// Every listed member lifts; notes may still bound the semantics.
    Implemented,
    /// Listed members lift, but known effects are approximated or dropped.
    Partial,
    /// Never produced by the lifters (only [`IrOp::Phi`]).
    Unmapped,
}

/// One lifted family's machine-readable coverage.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FamilyCoverage {
    /// Lifted operation.
    pub op: IrOp,
    /// Whether the family is fully, partially, or never lifted.
    pub status: CoverageStatus,
    /// Liftable mnemonics (ARM32/Thumb: base forms before suffix stripping).
    pub mnemonics: Vec<String>,
    /// Non-literal members (e.g. AArch64 `b.<cond>`).
    pub patterns: Vec<String>,
    /// Bounds that keep `implemented` from meaning semantically complete.
    pub note: Option<String>,
}

/// The unknown-mnemonic rule: unmapped input stays [`IrOp::Nop`] with a
/// warning, distinguishable from a genuine `NOP`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FallbackCoverage {
    /// Always [`IrOp::Nop`].
    pub op: IrOp,
    /// Always [`CoverageStatus::Unmapped`].
    pub status: CoverageStatus,
    /// Warning template (`<mnem>` is the unmapped mnemonic).
    pub warning: String,
    /// How to tell fallback NOP from a genuine one.
    pub distinguish: String,
}

/// Machine-readable lift coverage for one architecture: the [`coverage`]
/// query's row shape.
///
/// This is the machine-readable form of the ISA tables in the module docs,
/// built from the same slices the dispatch predicates consult, so it cannot
/// drift from the lifters. `implemented` never means semantically complete:
/// see `limitations` plus each family's `status`/`note`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Coverage {
    /// Architecture name (`x86_64`, `x86_32`, `aarch64`, `arm32`, `thumb`).
    pub arch: String,
    /// Per-operation families in [`IrOp`] declaration order.
    pub families: Vec<FamilyCoverage>,
    /// Engine-wide bounds (operand strings, flags, stack effects, no SSA).
    pub limitations: Vec<String>,
    /// The unknown-mnemonic rule.
    pub fallback: FallbackCoverage,
    /// Arch-wide mnemonic reading rule, if any (ARM32/Thumb suffixes).
    pub arch_note: Option<String>,
}

/// Engine-wide lift limitations shared by every architecture.
pub const COVERAGE_LIMITATIONS: &[&str] = &[
    "operands reuse the disassembler's strings verbatim; no operand decoding beyond that",
    "flag effects are scoped: integer add/sub/cmp record sets_flags (x86 EFLAGS writers, ARM NZCV writers); FP/SIMD/vector forms, lea, x87 FPU-condition writers, flag reads (adc/sbb carry-in), and preserved bits (inc/dec keep CF) are unmodeled",
    "stack effects are scoped: x86 push/pop/call and operand-free ret record sp_delta in bytes (pusha/popad as multi-slot constants); ret-with-immediate, far/privileged returns, traps, ARM multi-register push/pop, and calling-convention frame effects are unmodeled",
    "no SSA/PHI production: Phi is never emitted by lifters; it is synthesized at CFG merges",
    "implemented families are not semantically complete: listed mnemonics lift without warning, but the bounds above still apply",
];

fn fam(
    op: IrOp,
    status: CoverageStatus,
    mnemonics: &[&str],
    patterns: &[&str],
    note: Option<&str>,
) -> FamilyCoverage {
    FamilyCoverage {
        op,
        status,
        mnemonics: mnemonics.iter().map(|s| (*s).to_string()).collect(),
        patterns: patterns.iter().map(|s| (*s).to_string()).collect(),
        note: note.map(str::to_string),
    }
}

/// Genuine-vs-fallback NOP note shared by every architecture.
const NOP_NOTE: &str = "genuine effect-free mnemonics lift with no warning; unmapped mnemonics also lift to NOP but carry a warning";

fn phi_family() -> FamilyCoverage {
    fam(
        IrOp::Phi,
        CoverageStatus::Unmapped,
        &[],
        &[],
        Some("never emitted by lifters; synthesized at CFG merges"),
    )
}

fn x86_families() -> Vec<FamilyCoverage> {
    use CoverageStatus::{Implemented, Partial};
    vec![
        fam(
            IrOp::Copy,
            Implemented,
            X86_COPY,
            &[],
            Some(
                "xchg swaps both operands; leave/enter copy the frame pointer; cmovcc/setcc conditions, SIMD lanes/shuffles, and x87 stack effects unmodeled",
            ),
        ),
        fam(
            IrOp::Load,
            Implemented,
            X86_LOAD,
            &[],
            Some(
                "pop lifts as LOAD; single-slot pop/popf record sp_delta, popa/popad as multi-slot constants; x87 control/state loads approximated as LOAD",
            ),
        ),
        fam(
            IrOp::Store,
            Implemented,
            X86_STORE,
            &[],
            Some(
                "push lifts as STORE; single-slot push/pushf record sp_delta, pusha/pushad as multi-slot constants; x87 control/state stores approximated as STORE",
            ),
        ),
        fam(
            IrOp::Add,
            Implemented,
            X86_ADD,
            &[],
            Some(
                "lea lifts as address math, not a load; only integer add/adc/inc record sets_flags, FP/SIMD forms report none",
            ),
        ),
        fam(
            IrOp::Sub,
            Implemented,
            X86_SUB,
            &[],
            Some(
                "only integer sub/sbb/dec/neg record sets_flags; FP/SIMD forms report none; carry-in reads unmodeled",
            ),
        ),
        fam(IrOp::Mul, Implemented, X86_MUL, &[], None),
        fam(IrOp::Div, Implemented, X86_DIV, &[], None),
        fam(IrOp::And, Implemented, X86_AND, &[], None),
        fam(IrOp::Or, Implemented, X86_OR, &[], None),
        fam(
            IrOp::Xor,
            Partial,
            X86_XOR,
            &[],
            Some(
                "not folds bitwise complement to XOR; AES/SHA/carryless-multiply rounds and crc32 folded to XOR, exact transforms unmodeled",
            ),
        ),
        fam(IrOp::Shl, Implemented, X86_SHL, &[], None),
        fam(
            IrOp::Shr,
            Partial,
            X86_SHR,
            &[],
            Some(
                "rol/ror/rcl/rcr/shlx/shrx/sarx/rorx approximated as shifts; exact bit motion out of scope",
            ),
        ),
        fam(
            IrOp::Cmp,
            Partial,
            X86_CMP,
            X86_CMP_PATTERNS,
            Some(
                "integer/EFLAGS compares (cmp/test/bt/cmps/scas, comiss/vtest/fcomi families) record sets_flags; x87-condition and mask writers report none; imm-folded cmp<cc>/vcmp<cc> pseudo-ops match by pattern",
            ),
        ),
        fam(
            IrOp::Branch,
            Implemented,
            &["jmp"],
            &[],
            Some("indirect forms keep operands without target"),
        ),
        fam(IrOp::CBranch, Implemented, X86_JCC, &[], None),
        fam(
            IrOp::Call,
            Implemented,
            X86_CALL,
            &[],
            Some(
                "indirect calls keep operands without target; call records sp_delta, traps (syscall/int/...) report none",
            ),
        ),
        fam(
            IrOp::Return,
            Implemented,
            X86_RETURN,
            &[],
            Some(
                "operand-free ret/retn record sp_delta; ret-with-immediate and far/privileged returns report none",
            ),
        ),
        phi_family(),
        fam(IrOp::Nop, Implemented, X86_NOP, &[], Some(NOP_NOTE)),
    ]
}

fn aarch64_families() -> Vec<FamilyCoverage> {
    use CoverageStatus::{Implemented, Partial};
    vec![
        fam(
            IrOp::Copy,
            Implemented,
            ARM_COPY,
            &[],
            Some(
                "conditional selects, min/max selects, saturating narrowing, and crypto-adjacent permutations approximated as COPY",
            ),
        ),
        fam(
            IrOp::Load,
            Implemented,
            ARM_LOAD,
            &[],
            Some("acquire/exclusive/pair/SIMD loads carry no ordering effects"),
        ),
        fam(
            IrOp::Store,
            Implemented,
            ARM_STORE,
            &[],
            Some(
                "release/exclusive/pair/SIMD stores carry no ordering effects; store-only atomics fold the ALU part away",
            ),
        ),
        fam(
            IrOp::Add,
            Implemented,
            ARM_ADD,
            &[],
            Some("only adds/adcs record sets_flags; FP/SIMD/atomic forms report none"),
        ),
        fam(
            IrOp::Sub,
            Implemented,
            ARM_SUB,
            &[],
            Some("only subs/sbcs/negs/ngcs record sets_flags; FP/SIMD forms report none"),
        ),
        fam(IrOp::Mul, Implemented, ARM_MUL, &[], None),
        fam(IrOp::Div, Implemented, ARM_DIV, &[], None),
        fam(IrOp::And, Implemented, ARM_AND, &[], None),
        fam(IrOp::Or, Implemented, ARM_OR, &[], None),
        fam(
            IrOp::Xor,
            Partial,
            ARM_XOR,
            &[],
            Some(
                "AES/SHA/SM3/SM4 rounds, CRC, and atomic xor folded to XOR; exact transforms unmodeled",
            ),
        ),
        fam(IrOp::Shl, Implemented, ARM_SHL, &[], None),
        fam(
            IrOp::Shr,
            Partial,
            ARM_SHR,
            &[],
            Some("ror/rorv are rotates, approximated as shifts; narrowing shifts drop lanes"),
        ),
        fam(
            IrOp::Cmp,
            Partial,
            ARM_CMP,
            &[],
            Some(
                "integer/FP compares record sets_flags; vector compares and compare-and-swap report none; compare-and-swap drops the conditional store",
            ),
        ),
        fam(
            IrOp::Branch,
            Implemented,
            &["b", "br"],
            &[],
            Some("indirect forms keep operands without target"),
        ),
        fam(
            IrOp::CBranch,
            Implemented,
            ARM_CBRANCH,
            ARM_CBRANCH_PATTERNS,
            None,
        ),
        fam(
            IrOp::Call,
            Implemented,
            &["bl", "blr", "svc", "hvc", "smc"],
            &[],
            Some("indirect bl/blr keep operands without target"),
        ),
        fam(IrOp::Return, Implemented, ARM_RETURN, &[], None),
        phi_family(),
        fam(IrOp::Nop, Implemented, ARM_NOP, &[], Some(NOP_NOTE)),
    ]
}

fn arm32_families() -> Vec<FamilyCoverage> {
    use CoverageStatus::{Implemented, Partial};
    vec![
        fam(IrOp::Copy, Implemented, ARM32_COPY, &[], None),
        fam(
            IrOp::Load,
            Implemented,
            ARM32_LOAD,
            &[],
            Some(
                "pop/ldm/ldmia/ldmdb/ldmda naming pc lift as RETURN (CBRANCH when predicated), not LOAD; exclusives carry no ordering effects; push/pop-equivalent stack deltas need register-list decoding, reported as none",
            ),
        ),
        fam(
            IrOp::Store,
            Implemented,
            ARM32_STORE,
            &[],
            Some("push lifts as STORE; stack deltas need register-list decoding, reported as none"),
        ),
        fam(
            IrOp::Add,
            Implemented,
            ARM32_ADD,
            &[],
            Some("s-suffixed forms record sets_flags; saturating Q-flag effects unmodeled"),
        ),
        fam(
            IrOp::Sub,
            Implemented,
            ARM32_SUB,
            &[],
            Some("s-suffixed forms record sets_flags; carry-in reads (sbc/rsc) unmodeled"),
        ),
        fam(IrOp::Mul, Implemented, ARM32_MUL, &[], None),
        fam(IrOp::Div, Implemented, ARM32_DIV, &[], None),
        fam(IrOp::And, Implemented, ARM32_AND, &[], None),
        fam(IrOp::Or, Implemented, ARM32_OR, &[], None),
        fam(IrOp::Xor, Implemented, ARM32_XOR, &[], None),
        fam(IrOp::Shl, Implemented, ARM32_SHL, &[], None),
        fam(
            IrOp::Shr,
            Partial,
            ARM32_SHR,
            &[],
            Some("ror/rrx are rotates, approximated as shifts"),
        ),
        fam(
            IrOp::Cmp,
            Partial,
            ARM32_CMP,
            &[],
            Some("cmp/cmn/tst/teq and s-suffixed add/sub record sets_flags"),
        ),
        fam(
            IrOp::Branch,
            Implemented,
            &["b", "bx", "tbb", "tbh"],
            &[],
            Some("condition-suffixed b/bx lift as CBRANCH; bx lr lifts as RETURN"),
        ),
        fam(
            IrOp::CBranch,
            Implemented,
            &["cbz", "cbnz"],
            &[],
            Some("plus condition-suffixed b/bx and predicated pop/ldm naming pc"),
        ),
        fam(
            IrOp::Call,
            Implemented,
            &["bl", "blx", "svc", "hvc", "smc"],
            &[],
            Some("indirect bl/blx keep operands without target"),
        ),
        fam(
            IrOp::Return,
            Implemented,
            &[],
            &[],
            Some(
                "operand-dependent: bx lr and pop/ldm naming pc (predicated forms lift as CBRANCH)",
            ),
        ),
        phi_family(),
        fam(IrOp::Nop, Implemented, ARM32_NOP, &[], Some(NOP_NOTE)),
    ]
}

/// Machine-readable lift coverage for `arch`, without opening a binary.
///
/// Families publish the same mnemonic slices the dispatch predicates
/// consult, so advertised coverage cannot drift from actual lifts; unknown
/// mnemonics stay fallback [`IrOp::Nop`] with a warning per
/// [`unlifted`](self#warning-semantics) semantics.
#[must_use]
pub fn coverage(arch: Arch) -> Coverage {
    let (families, arch_note) = match arch {
        Arch::X86_64 | Arch::X86_32 => (x86_families(), None),
        Arch::Aarch64 => (aarch64_families(), None),
        Arch::Arm32 | Arch::Thumb => (
            arm32_families(),
            Some(
                "mnemonics are base forms: one .w/.n width suffix, one condition suffix, and one flag s are stripped before lookup; a stripped condition turns plain b into CBRANCH",
            ),
        ),
    };
    Coverage {
        arch: crate::db::arch_name(arch).to_string(),
        families,
        limitations: COVERAGE_LIMITATIONS
            .iter()
            .map(|s| (*s).to_string())
            .collect(),
        fallback: FallbackCoverage {
            op: IrOp::Nop,
            status: CoverageStatus::Unmapped,
            warning: "unlifted '<mnem>'; emitted NOP".to_string(),
            distinguish:
                "warning.is_some() marks fallback NOP; genuine NOP mnemonics lift with warning None"
                    .to_string(),
        },
        arch_note: arch_note.map(str::to_string),
    }
}

/// Lift one x86 (32- or 64-bit) instruction.
///
/// Mode-agnostic by design (one table for both modes): flag effects are
/// complete here, but slot-sized `sp_delta` values are assigned by
/// [`lift`], which owns the arch context.
#[must_use]
pub fn lift_x86(insn: &Insn) -> Vec<IrInsn> {
    let mnem = insn.mnem.as_str();
    // Read-modify-write with a single operand reads as well as writes.
    let alu_out_in = |insn: &Insn| -> (Vec<String>, Vec<String>) {
        match insn.ops.as_slice() {
            [] => (Vec::new(), Vec::new()),
            [dst] => (vec![dst.clone()], vec![dst.clone()]),
            [dst, rest @ ..] => (vec![dst.clone()], rest.to_vec()),
        }
    };
    let (op, inputs, outputs, target) = match mnem {
        s if is_x86_copy(s) => {
            let (outputs, inputs) = match insn.ops.as_slice() {
                [] => (Vec::new(), Vec::new()),
                // xchg swaps both operands.
                [a, b] if s == "xchg" => (vec![a.clone(), b.clone()], vec![b.clone(), a.clone()]),
                [single] => (vec![single.clone()], Vec::new()),
                [dst, rest @ ..] => (vec![dst.clone()], rest.to_vec()),
            };
            (IrOp::Copy, inputs, outputs, None)
        }
        s if is_x86_load(s) => (IrOp::Load, Vec::new(), insn.ops.clone(), None),
        s if is_x86_store(s) => (IrOp::Store, insn.ops.clone(), Vec::new(), None),
        s if is_x86_alu(s, IrOp::Add) => {
            let (outputs, inputs) = alu_out_in(insn);
            (IrOp::Add, inputs, outputs, None)
        }
        s if is_x86_alu(s, IrOp::Sub) => {
            let (outputs, inputs) = alu_out_in(insn);
            (IrOp::Sub, inputs, outputs, None)
        }
        s if is_x86_alu(s, IrOp::Mul) => {
            let (outputs, inputs) = alu_out_in(insn);
            (IrOp::Mul, inputs, outputs, None)
        }
        s if is_x86_alu(s, IrOp::Div) => {
            let (outputs, inputs) = alu_out_in(insn);
            (IrOp::Div, inputs, outputs, None)
        }
        s if is_x86_alu(s, IrOp::And) => {
            let (outputs, inputs) = alu_out_in(insn);
            (IrOp::And, inputs, outputs, None)
        }
        s if is_x86_alu(s, IrOp::Or) => {
            let (outputs, inputs) = alu_out_in(insn);
            (IrOp::Or, inputs, outputs, None)
        }
        s if is_x86_alu(s, IrOp::Xor) => {
            let (outputs, inputs) = alu_out_in(insn);
            (IrOp::Xor, inputs, outputs, None)
        }
        s if is_x86_alu(s, IrOp::Shl) => {
            let (outputs, inputs) = alu_out_in(insn);
            (IrOp::Shl, inputs, outputs, None)
        }
        s if is_x86_alu(s, IrOp::Shr) => {
            let (outputs, inputs) = alu_out_in(insn);
            (IrOp::Shr, inputs, outputs, None)
        }
        s if is_x86_cmp(s) => (IrOp::Cmp, insn.ops.clone(), Vec::new(), None),
        "jmp" => direct_or_indirect(insn, IrOp::Branch),
        s if is_x86_jcc(s) => (
            IrOp::CBranch,
            insn.ops.clone(),
            Vec::new(),
            insn.branch_target,
        ),
        s if is_x86_call(s) => direct_or_indirect(insn, IrOp::Call),
        s if is_x86_return(s) => (IrOp::Return, Vec::new(), Vec::new(), None),
        s if is_x86_nop(s) => (IrOp::Nop, Vec::new(), Vec::new(), None),
        _ => return unlifted(insn),
    };
    let sets_flags = matches!(op, IrOp::Add | IrOp::Sub | IrOp::Cmp) && x86_sets_flags(mnem);
    vec![IrInsn {
        addr: insn.addr,
        op,
        inputs,
        outputs,
        target,
        warning: None,
        effects: IrEffects {
            sets_flags,
            sp_delta: None,
        },
    }]
}

/// Lift one AArch64 instruction.
#[must_use]
pub fn lift_aarch64(insn: &Insn) -> Vec<IrInsn> {
    lift_aarch64_with_prev(insn, None)
}

/// Lift one AArch64 instruction with its immediate predecessor, if any.
///
/// When `prev` ends where `insn` begins and the two form an
/// adrp+add/ldr pair resolving to a final address, the second row's
/// `target` carries that address; otherwise the row is identical to
/// [`lift_aarch64`]. Nonadjacent or unresolvable pairs keep current
/// behavior exactly.
#[must_use]
pub fn lift_aarch64_with_prev(insn: &Insn, prev: Option<&Insn>) -> Vec<IrInsn> {
    let mnem = insn.mnem.as_str();
    let (op, inputs, outputs, mut target) = match mnem {
        s if is_arm_copy(s) => first_out_rest_in(insn, IrOp::Copy),
        s if is_arm_load(s) => first_out_rest_in(insn, IrOp::Load),
        s if is_arm_store(s) => (IrOp::Store, insn.ops.clone(), Vec::new(), None),
        s if is_arm_alu(s, IrOp::Add) => first_out_rest_in(insn, IrOp::Add),
        s if is_arm_alu(s, IrOp::Sub) => first_out_rest_in(insn, IrOp::Sub),
        s if is_arm_alu(s, IrOp::Mul) => first_out_rest_in(insn, IrOp::Mul),
        s if is_arm_alu(s, IrOp::Div) => first_out_rest_in(insn, IrOp::Div),
        s if is_arm_alu(s, IrOp::And) => first_out_rest_in(insn, IrOp::And),
        s if is_arm_alu(s, IrOp::Or) => first_out_rest_in(insn, IrOp::Or),
        s if is_arm_alu(s, IrOp::Xor) => first_out_rest_in(insn, IrOp::Xor),
        s if is_arm_alu(s, IrOp::Shl) => first_out_rest_in(insn, IrOp::Shl),
        s if is_arm_alu(s, IrOp::Shr) => first_out_rest_in(insn, IrOp::Shr),
        s if ARM_CMP.contains(&s) => (IrOp::Cmp, insn.ops.clone(), Vec::new(), None),
        "b" | "br" => direct_or_indirect(insn, IrOp::Branch),
        s if is_arm_cbranch(s) => (
            IrOp::CBranch,
            insn.ops.clone(),
            Vec::new(),
            insn.branch_target,
        ),
        "bl" | "blr" => direct_or_indirect(insn, IrOp::Call),
        s if is_arm_call(s) => (IrOp::Call, insn.ops.clone(), Vec::new(), None),
        s if is_arm_return(s) => (IrOp::Return, Vec::new(), Vec::new(), None),
        s if is_arm_nop(s) => (IrOp::Nop, Vec::new(), Vec::new(), None),
        _ => return unlifted(insn),
    };
    if target.is_none()
        && let Some(first) = prev
        && first.addr.wrapping_add(u64::from(first.size)) == insn.addr
        && let Some(resolved) = resolve_adrp_pair(first, insn)
    {
        target = Some(resolved);
    }
    let sets_flags = matches!(op, IrOp::Add | IrOp::Sub | IrOp::Cmp) && arm_sets_flags(mnem);
    vec![IrInsn {
        addr: insn.addr,
        op,
        inputs,
        outputs,
        target,
        warning: None,
        effects: IrEffects {
            sets_flags,
            sp_delta: None,
        },
    }]
}

fn first_out_rest_in(insn: &Insn, op: IrOp) -> (IrOp, Vec<String>, Vec<String>, Option<u64>) {
    match insn.ops.as_slice() {
        [] => (op, Vec::new(), Vec::new(), None),
        [dst, rest @ ..] => (op, rest.to_vec(), vec![dst.clone()], None),
    }
}

/// Lift one ARM32/Thumb instruction.
///
/// ARMv7 Display bakes the condition code and flag suffix into the mnemonic
/// (`addeq`, `adds`, `bl.w`), so `split_arm_mnem` reduces it to the base
/// first; a stripped condition turns a plain `b` into a conditional branch
/// and keeps predicated returns on the fallthrough path.
#[must_use]
pub fn lift_arm32(insn: &Insn) -> Vec<IrInsn> {
    let (base, cond) = split_arm_mnem(&insn.mnem);
    let (op, inputs, outputs, target) = match base {
        s if is_arm32_copy(s) => first_out_rest_in(insn, IrOp::Copy),
        s if is_arm32_load(s) => {
            if is_arm32_ldm(s) && ops_contain_pc(insn) {
                if cond {
                    (IrOp::CBranch, insn.ops.clone(), Vec::new(), None)
                } else {
                    (IrOp::Return, Vec::new(), Vec::new(), None)
                }
            } else {
                (IrOp::Load, Vec::new(), insn.ops.clone(), None)
            }
        }
        s if is_arm32_store(s) => (IrOp::Store, insn.ops.clone(), Vec::new(), None),
        s if is_arm32_alu(s, IrOp::Add) => first_out_rest_in(insn, IrOp::Add),
        s if is_arm32_alu(s, IrOp::Sub) => first_out_rest_in(insn, IrOp::Sub),
        s if is_arm32_alu(s, IrOp::Mul) => first_out_rest_in(insn, IrOp::Mul),
        s if is_arm32_alu(s, IrOp::Div) => first_out_rest_in(insn, IrOp::Div),
        s if is_arm32_alu(s, IrOp::And) => first_out_rest_in(insn, IrOp::And),
        s if is_arm32_alu(s, IrOp::Or) => first_out_rest_in(insn, IrOp::Or),
        s if is_arm32_alu(s, IrOp::Xor) => first_out_rest_in(insn, IrOp::Xor),
        s if is_arm32_alu(s, IrOp::Shl) => first_out_rest_in(insn, IrOp::Shl),
        s if is_arm32_alu(s, IrOp::Shr) => first_out_rest_in(insn, IrOp::Shr),
        s if ARM32_CMP.contains(&s) => (IrOp::Cmp, insn.ops.clone(), Vec::new(), None),
        "b" if cond => (
            IrOp::CBranch,
            insn.ops.clone(),
            Vec::new(),
            insn.branch_target,
        ),
        "b" => direct_or_indirect(insn, IrOp::Branch),
        "bl" | "blx" => direct_or_indirect(insn, IrOp::Call),
        "bx" if !cond && insn.ops.as_slice() == ["lr"] => {
            (IrOp::Return, Vec::new(), Vec::new(), None)
        }
        "bx" if cond => (IrOp::CBranch, insn.ops.clone(), Vec::new(), None),
        "bx" => direct_or_indirect(insn, IrOp::Branch),
        "cbz" | "cbnz" => (
            IrOp::CBranch,
            insn.ops.clone(),
            Vec::new(),
            insn.branch_target,
        ),
        "tbb" | "tbh" => (IrOp::Branch, insn.ops.clone(), Vec::new(), None),
        s if is_arm32_call(s) => (IrOp::Call, insn.ops.clone(), Vec::new(), None),
        s if is_arm32_nop(s) => (IrOp::Nop, Vec::new(), Vec::new(), None),
        _ => return unlifted(insn),
    };
    let sets_flags =
        matches!(op, IrOp::Add | IrOp::Sub | IrOp::Cmp) && arm32_sets_flags(base, &insn.mnem);
    vec![IrInsn {
        addr: insn.addr,
        op,
        inputs,
        outputs,
        target,
        warning: None,
        effects: IrEffects {
            sets_flags,
            sp_delta: None,
        },
    }]
}

/// Reduce an ARMv7 mnemonic to its base plus whether it carries a condition
/// code. Strips one `.w`/`.n` width suffix, then one condition suffix
/// (`eq`..`le`, only when the remainder is a known base so `teq`/`bics`
/// survive), then one flag `s` (same guard, so `mrs`/`svc` survive).
fn split_arm_mnem(mnem: &str) -> (&str, bool) {
    let base = mnem
        .strip_suffix(".w")
        .or_else(|| mnem.strip_suffix(".n"))
        .unwrap_or(mnem);
    if base.len() > 2 {
        let (prefix, suffix) = base.split_at(base.len() - 2);
        if ARM_CONDS.contains(&suffix) && arm_base_mnem(prefix) {
            return (prefix, true);
        }
    }
    if let Some(prefix) = base.strip_suffix('s')
        && arm_base_mnem(prefix)
    {
        return (prefix, false);
    }
    (base, false)
}

/// Condition suffixes of [`split_arm_mnem`], matching the decoder's
/// `ConditionCode` names (`hs`/`lo`, never the `cs`/`cc` aliases).
const ARM_CONDS: [&str; 14] = [
    "eq", "ne", "hs", "lo", "mi", "pl", "vs", "vc", "hi", "ls", "ge", "lt", "gt", "le",
];

/// Whether `s` is a base mnemonic of [`lift_arm32`]: the load/store/copy
/// tables, any ALU table, the compare table, or the control/nop words
/// matched inline by the lifter.
fn arm_base_mnem(s: &str) -> bool {
    is_arm32_copy(s)
        || is_arm32_load(s)
        || is_arm32_store(s)
        || is_arm32_alu(s, IrOp::Add)
        || is_arm32_alu(s, IrOp::Sub)
        || is_arm32_alu(s, IrOp::Mul)
        || is_arm32_alu(s, IrOp::Div)
        || is_arm32_alu(s, IrOp::And)
        || is_arm32_alu(s, IrOp::Or)
        || is_arm32_alu(s, IrOp::Xor)
        || is_arm32_alu(s, IrOp::Shl)
        || is_arm32_alu(s, IrOp::Shr)
        || ARM32_CMP.contains(&s)
        || is_arm32_nop(s)
        || matches!(
            s,
            "b" | "bl" | "blx" | "bx" | "cbz" | "cbnz" | "tbb" | "tbh" | "svc" | "hvc" | "smc"
        )
}

/// Whether any operand names `pc` (return-via-`pop`/`ldm` check).
fn ops_contain_pc(insn: &Insn) -> bool {
    insn.ops
        .iter()
        .any(|o| o.split(|c: char| !c.is_alphanumeric()).any(|w| w == "pc"))
}

/// Direct branch/call keeps only `target`; indirect keeps the operands.
fn direct_or_indirect(insn: &Insn, op: IrOp) -> (IrOp, Vec<String>, Vec<String>, Option<u64>) {
    match insn.branch_target {
        Some(t) => (op, Vec::new(), Vec::new(), Some(t)),
        None => (op, insn.ops.clone(), Vec::new(), None),
    }
}

fn unlifted(insn: &Insn) -> Vec<IrInsn> {
    vec![IrInsn {
        addr: insn.addr,
        op: IrOp::Nop,
        inputs: Vec::new(),
        outputs: Vec::new(),
        target: None,
        warning: Some(format!("unlifted '{}'; emitted NOP", insn.mnem)),
        effects: IrEffects::default(),
    }]
}

/// Whether x86 `mnem` writes EFLAGS in the common integer case.
///
/// Call only with a mnemonic that already lifted as Add/Sub/Cmp: plain
/// integer `add`/`adc`/`adcx`/`adox`/`inc`, `sub`/`sbb`/`dec`/`neg`,
/// integer and string compares and tests, `bt`, and the EFLAGS-writing FP
/// compares (`comiss*`, `ucomiss*`, `vtestps`/`vtestpd`, `vptest`,
/// `fcomi*`). `lea`, x87 FPU-condition writers, packed mask writers, and
/// the imm-folded `cmp<cc>`/`vcmp<cc>` pseudo-ops report false.
fn x86_sets_flags(mnem: &str) -> bool {
    matches!(
        mnem,
        "add"
            | "adc"
            | "adcx"
            | "adox"
            | "inc"
            | "sub"
            | "sbb"
            | "dec"
            | "neg"
            | "cmp"
            | "test"
            | "bt"
            | "cmps"
            | "cmpsb"
            | "cmpsw"
            | "cmpsd"
            | "cmpsq"
            | "scas"
            | "scasb"
            | "scasw"
            | "scasd"
            | "scasq"
            | "comiss"
            | "comisd"
            | "ucomiss"
            | "ucomisd"
            | "vcomiss"
            | "vcomisd"
            | "vucomiss"
            | "vucomisd"
            | "vtestps"
            | "vtestpd"
            | "vptest"
            | "fcomi"
            | "fcomip"
            | "fucomi"
            | "fucomip"
    )
}

/// Whether an AArch64 `mnem` writes NZCV in the common integer case.
///
/// Call only with a mnemonic that already lifted as Add/Sub/Cmp: the
/// `s`-suffixed integer forms (`adds`, `adcs`, `subs`, `sbcs`, `negs`,
/// `ngcs`) and the scalar compares (`cmp`, `cmn`, `tst`, `ccmp`,
/// `ccmn`, `cmtst`, `fcmp`, `fcmpe`, `fccmp`, `fccmpe`). Plain
/// `add`/`sub`, FP/SIMD ALU, vector compares, and compare-and-swap
/// report false.
fn arm_sets_flags(mnem: &str) -> bool {
    matches!(
        mnem,
        "adds"
            | "adcs"
            | "subs"
            | "sbcs"
            | "negs"
            | "ngcs"
            | "cmp"
            | "cmn"
            | "tst"
            | "ccmp"
            | "ccmn"
            | "cmtst"
            | "fcmp"
            | "fcmpe"
            | "fccmp"
            | "fccmpe"
    )
}

/// Whether an ARM32/Thumb `base`+full-mnemonic pair writes NZCV: any
/// compare base (`cmp`/`cmn`/`tst`/`teq`), or an add/sub base with the
/// flag `s` suffix (`adds`, `subs.w`, ...).
///
/// Mirrors [`split_arm_mnem`]: one `.w`/`.n` width suffix and one guarded
/// condition suffix peel first, then the trailing `s` must leave exactly
/// `base`, so `svc`/`mrs`/`teq` keep their own meaning.
fn arm32_sets_flags(base: &str, full: &str) -> bool {
    if ARM32_CMP.contains(&base) {
        return true;
    }
    if !(is_arm32_alu(base, IrOp::Add) || is_arm32_alu(base, IrOp::Sub)) {
        return false;
    }
    let w = full
        .strip_suffix(".w")
        .or_else(|| full.strip_suffix(".n"))
        .unwrap_or(full);
    let core = if w.len() > 2 {
        let (prefix, suffix) = w.split_at(w.len() - 2);
        if ARM_CONDS.contains(&suffix) && arm_base_mnem(prefix) {
            prefix
        } else {
            w
        }
    } else {
        w
    };
    core.strip_suffix('s').is_some_and(|p| p == base)
}

/// Signed stack-pointer delta in bytes for `arch`+`insn`, if the effect is
/// a single known slot move.
///
/// x86 only: `push`/`pop`/`pushf*`/`popf*` move one slot (-/+8 on x86-64,
/// -/+4 on x86-32), `pusha`/`popa` move 16 and `pushad`/`popad` 32,
/// `call` pushes one return slot, and operand-free `ret`/`retn` pop one.
/// Everything else reports `None`: `ret` with an immediate (extra
/// argument pop), far/privileged returns, traps, and all ARM mnemonics
/// (multi-register push/pop need register-list decoding; `bl`/`ret` are
/// link-register moves with no SP effect).
fn sp_delta_for(arch: Arch, insn: &Insn) -> Option<i64> {
    if !matches!(arch, Arch::X86_64 | Arch::X86_32) {
        return None;
    }
    let slot: i64 = if arch == Arch::X86_64 { 8 } else { 4 };
    match insn.mnem.as_str() {
        "push" | "pushf" | "pushfd" | "pushfq" => Some(-slot),
        "pop" | "popf" | "popfd" | "popfq" => Some(slot),
        "pusha" => Some(-16),
        "popa" => Some(16),
        "pushad" => Some(-32),
        "popad" => Some(32),
        "call" => Some(-slot),
        "ret" | "retn" if insn.ops.is_empty() => Some(slot),
        _ => None,
    }
}

fn is_x86_copy(s: &str) -> bool {
    X86_COPY.contains(&s)
}

fn is_x86_load(s: &str) -> bool {
    X86_LOAD.contains(&s)
}

fn is_x86_store(s: &str) -> bool {
    X86_STORE.contains(&s)
}

fn is_x86_alu(s: &str, op: IrOp) -> bool {
    match op {
        IrOp::Add => X86_ADD.contains(&s),
        IrOp::Sub => X86_SUB.contains(&s),
        IrOp::Mul => X86_MUL.contains(&s),
        IrOp::Div => X86_DIV.contains(&s),
        IrOp::And => X86_AND.contains(&s),
        IrOp::Or => X86_OR.contains(&s),
        IrOp::Xor => X86_XOR.contains(&s),
        // Rotates approximate to shifts; exact bit motion is out of scope for M3.
        IrOp::Shl => X86_SHL.contains(&s),
        IrOp::Shr => X86_SHR.contains(&s),
        _ => false,
    }
}

/// Shared dispatch/coverage tables: the `is_x86_*` predicates consult these,
/// and [`coverage`] publishes the same slices, so the machine-readable
/// report cannot drift from the lifters.
const X86_COPY: &[&str] = &[
    "mov",
    "movsx",
    "movsxd",
    "movzx",
    "movabs",
    "movbe",
    "xchg",
    "leave",
    "enter",
    "cbw",
    "cwde",
    "cdqe",
    "cdq",
    "cqo",
    "bswap",
    "cmovo",
    "cmovno",
    "cmovb",
    "cmovae",
    "cmove",
    "cmovne",
    "cmovbe",
    "cmova",
    "cmovs",
    "cmovns",
    "cmovp",
    "cmovnp",
    "cmovl",
    "cmovge",
    "cmovle",
    "cmovg",
    "seto",
    "setno",
    "setb",
    "setae",
    "sete",
    "setne",
    "setbe",
    "seta",
    "sets",
    "setns",
    "setp",
    "setnp",
    "setl",
    "setge",
    "setle",
    "setg",
    "movs",
    "movsb",
    "movsw",
    "movsd",
    "movsq",
    "movss",
    "movaps",
    "movups",
    "movapd",
    "movupd",
    "movdqa",
    "movdqu",
    "movd",
    "movq",
    "movlps",
    "movlpd",
    "movhps",
    "movhpd",
    "movlhps",
    "movhlps",
    "movmskps",
    "movmskpd",
    "lddqu",
    "shufps",
    "shufpd",
    "pshufb",
    "pshufd",
    "pshufhw",
    "pshuflw",
    "unpcklps",
    "unpcklpd",
    "unpckhps",
    "unpckhpd",
    "punpcklbw",
    "punpcklwd",
    "punpckldq",
    "punpcklqdq",
    "punpckhbw",
    "punpckhwd",
    "punpckhdq",
    "punpckhqdq",
    "pinsrw",
    "pextrw",
    "bextr",
    "pdep",
    "pext",
    "vmovaps",
    "vmovups",
    "vmovapd",
    "vmovupd",
    "vmovdqa",
    "vmovdqu",
    "vmovss",
    "vmovsd",
    "vmovd",
    "vmovq",
    "vmovlps",
    "vmovlpd",
    "vmovhps",
    "vmovhpd",
    "vshufps",
    "vshufpd",
    "vpshufb",
    "vpshufd",
    "vpshufhw",
    "vpshuflw",
    "vunpcklps",
    "vunpcklpd",
    "vunpckhps",
    "vunpckhpd",
    "vpunpcklbw",
    "vpunpcklwd",
    "vpunpckldq",
    "vpunpcklqdq",
    "vpunpckhbw",
    "vpunpckhwd",
    "vpunpckhdq",
    "vpunpckhqdq",
    "vpermps",
    "vpermd",
    "vpermq",
    "vpermilps",
    "vpermilpd",
    "vperm2f128",
    "vinsertps",
    "vextractps",
    "vinsertf128",
    "vextractf128",
    "vpinsrb",
    "vpinsrw",
    "vpinsrd",
    "vpinsrq",
    "vpextrb",
    "vpextrw",
    "vpextrd",
    "vpextrq",
    "vbroadcastss",
    "vbroadcastsd",
    "vpbroadcastb",
    "vpbroadcastw",
    "vpbroadcastd",
    "vpbroadcastq",
    "vpmovsxbw",
    "vpmovsxbd",
    "vpmovsxbq",
    "vpmovsxwd",
    "vpmovsxwq",
    "vpmovsxdq",
    "vpmovzxbw",
    "vpmovzxbd",
    "vpmovzxbq",
    "vpmovzxwd",
    "vpmovzxwq",
    "vpmovzxdq",
    "vpsignb",
    "vpsignw",
    "vpsignd",
    "psignb",
    "psignw",
    "psignd",
    "fld1",
    "fldl2t",
    "fldl2e",
    "fldpi",
    "fldlg2",
    "fldln2",
    "fldz",
    "fabs",
    "fsqrt",
    "fxch",
    "frndint",
    "fnstsw",
    "fcmovb",
    "fcmove",
    "fcmovbe",
    "fcmovu",
    "fcmovnb",
    "fcmovne",
    "fcmovnbe",
    "fcmovnu",
    "rdtsc",
    "rdtscp",
    "rdpmc",
    "rdrand",
    "rdseed",
    "xgetbv",
];
const X86_LOAD: &[&str] = &[
    "pop", "popa", "popad", "popf", "popfd", "popfq", "lods", "lodsb", "lodsw", "lodsd", "lodsq",
    "xlat", "in", "insb", "insw", "insd", "movntdqa", "fld", "fild", "fbld", "fldenv", "fldcw",
    "frstor", "fxrstor", "xrstor",
];
const X86_STORE: &[&str] = &[
    "push", "pusha", "pushad", "pushf", "pushfd", "pushfq", "stos", "stosb", "stosw", "stosd",
    "stosq", "out", "outsb", "outsw", "outsd", "movntps", "movntpd", "movntdq", "movnti",
    "vmovntps", "vmovntpd", "vmovntdq", "fst", "fstp", "fist", "fistp", "fisttp", "fbstp",
    "fnstcw", "fnstenv", "fnsave", "fxsave", "xsave",
];
const X86_ADD: &[&str] = &[
    "add", "adc", "adcx", "adox", "lea", "inc", "fadd", "faddp", "fiadd", "addps", "addpd",
    "addss", "addsd", "vaddps", "vaddpd", "vaddss", "vaddsd", "paddb", "paddw", "paddd", "paddq",
    "vpaddb", "vpaddw", "vpaddd", "vpaddq", "haddps", "haddpd", "vhaddps", "vhaddpd", "phaddw",
    "phaddd",
];
const X86_SUB: &[&str] = &[
    "sub", "sbb", "dec", "neg", "fchs", "fsub", "fsubp", "fsubr", "fisub", "fisubr", "subps",
    "subpd", "subss", "subsd", "vsubps", "vsubpd", "vsubss", "vsubsd", "psubb", "psubw", "psubd",
    "psubq", "vpsubb", "vpsubw", "vpsubd", "vpsubq", "hsubps", "hsubpd", "vhsubps", "vhsubpd",
    "phsubw", "phsubd", "psadbw", "mpsadbw", "vpsadbw", "vmpsadbw",
];
const X86_MUL: &[&str] = &[
    "mul",
    "imul",
    "mulx",
    "fmul",
    "fmulp",
    "fimul",
    "mulps",
    "mulpd",
    "mulss",
    "mulsd",
    "vmulps",
    "vmulpd",
    "vmulss",
    "vmulsd",
    "pmullw",
    "pmulld",
    "vpmullw",
    "vpmulld",
    "pmuldq",
    "vpmuldq",
    "pmuludq",
    "vpmuludq",
    "pmulhw",
    "pmulhuw",
    "vpmulhw",
    "vpmulhuw",
    "pmaddwd",
    "vpmaddwd",
    "vfmadd132ps",
    "vfmadd132pd",
    "vfmadd132ss",
    "vfmadd132sd",
    "vfmadd213ps",
    "vfmadd213pd",
    "vfmadd213ss",
    "vfmadd213sd",
    "vfmadd231ps",
    "vfmadd231pd",
    "vfmadd231ss",
    "vfmadd231sd",
    "vfmsub132ps",
    "vfmsub132pd",
    "vfmsub132ss",
    "vfmsub132sd",
    "vfmsub213ps",
    "vfmsub213pd",
    "vfmsub213ss",
    "vfmsub213sd",
    "vfmsub231ps",
    "vfmsub231pd",
    "vfmsub231ss",
    "vfmsub231sd",
    "vfnmadd132ps",
    "vfnmadd132pd",
    "vfnmadd132ss",
    "vfnmadd132sd",
    "vfnmadd213ps",
    "vfnmadd213pd",
    "vfnmadd213ss",
    "vfnmadd213sd",
    "vfnmadd231ps",
    "vfnmadd231pd",
    "vfnmadd231ss",
    "vfnmadd231sd",
    "vfnmsub132ps",
    "vfnmsub132pd",
    "vfnmsub132ss",
    "vfnmsub132sd",
    "vfnmsub213ps",
    "vfnmsub213pd",
    "vfnmsub213ss",
    "vfnmsub213sd",
    "vfnmsub231ps",
    "vfnmsub231pd",
    "vfnmsub231ss",
    "vfnmsub231sd",
];
const X86_DIV: &[&str] = &[
    "div", "idiv", "fdiv", "fdivp", "fdivr", "fidiv", "fidivr", "divps", "divpd", "divss", "divsd",
    "vdivps", "vdivpd", "vdivss", "vdivsd",
];
const X86_AND: &[&str] = &[
    "and", "pand", "vpand", "pandn", "vpandn", "andps", "andpd", "vandps", "vandpd", "andn",
    "blsi", "blsr", "bzhi", "btr",
];
const X86_OR: &[&str] = &["or", "por", "vpor", "orps", "orpd", "vorps", "vorpd", "bts"];
const X86_XOR: &[&str] = &[
    "xor",
    "pxor",
    "vpxor",
    "not",
    "xorps",
    "xorpd",
    "vxorps",
    "vxorpd",
    "blsmsk",
    "btc",
    "aesenc",
    "aesenclast",
    "aesdec",
    "aesdeclast",
    "aeskeygenassist",
    "aesimc",
    "pclmullqlqdq",
    "pclmulhqlqdq",
    "pclmullqhqdq",
    "pclmulhqhqdq",
    "sha1nexte",
    "sha1msg1",
    "sha1msg2",
    "sha1rnds4",
    "sha256rnds2",
    "sha256msg1",
    "sha256msg2",
    "crc32",
];
const X86_SHL: &[&str] = &[
    "shl", "sal", "shld", "shlx", "psllw", "pslld", "psllq", "pslldq", "vpsllw", "vpslld",
    "vpsllq", "vpslldq",
];
const X86_SHR: &[&str] = &[
    "shr", "sar", "rol", "ror", "rcl", "rcr", "shrd", "shrx", "sarx", "rorx", "psrlw", "psrld",
    "psrlq", "psrldq", "vpsrlw", "vpsrld", "vpsrlq", "vpsrldq", "psraw", "psrad", "vpsraw",
    "vpsrad", "palignr", "vpalignr",
];
const X86_CMP: &[&str] = &[
    "cmp",
    "test",
    "bt",
    "cmps",
    "cmpsb",
    "cmpsw",
    "cmpsd",
    "cmpsq",
    "scas",
    "scasb",
    "scasw",
    "scasd",
    "scasq",
    "comiss",
    "comisd",
    "ucomiss",
    "ucomisd",
    "vcomiss",
    "vcomisd",
    "vucomiss",
    "vucomisd",
    "vtestps",
    "vtestpd",
    "vptest",
    "fcom",
    "fcomp",
    "fcompp",
    "fucom",
    "fucomp",
    "fucompp",
    "fcomi",
    "fcomip",
    "fucomi",
    "fucomip",
    "ftst",
    "fxam",
    "ficom",
    "ficomp",
    "pcmpeqb",
    "pcmpeqw",
    "pcmpeqd",
    "pcmpeqq",
    "vpcmpeqb",
    "vpcmpeqw",
    "vpcmpeqd",
    "vpcmpeqq",
    "pcmpgtb",
    "pcmpgtw",
    "pcmpgtd",
    "pcmpgtq",
    "vpcmpgtb",
    "vpcmpgtw",
    "vpcmpgtd",
    "vpcmpgtq",
    "pcmpestri",
    "pcmpestrm",
    "pcmpistri",
    "pcmpistrm",
    "phminposuw",
    "vphminposuw",
];
/// Imm-folded x86 compare pseudo-ops: iced-x86 folds the immediate predicate
/// into the mnemonic (`cmpeqps`, `vcmpltps`, ...), so these publish as
/// patterns with the same `<cond>` placeholder style as AArch64 `b.<cond>`.
const X86_CMP_PATTERNS: &[&str] = &[
    "cmp<cond>ps",
    "cmp<cond>pd",
    "cmp<cond>ss",
    "cmp<cond>sd",
    "vcmp<cond>ps",
    "vcmp<cond>pd",
    "vcmp<cond>ss",
    "vcmp<cond>sd",
];
const X86_JCC: &[&str] = &[
    "jo", "jno", "jb", "jnae", "jc", "jnb", "jae", "jnc", "je", "jz", "jne", "jnz", "jbe", "jna",
    "jnbe", "ja", "js", "jns", "jp", "jpe", "jnp", "jpo", "jl", "jnge", "jnl", "jge", "jle", "jng",
    "jnle", "jg", "jcxz", "jecxz", "jrcxz", "loop", "loope", "loopne",
];
const X86_CALL: &[&str] = &["call", "syscall", "sysenter", "int", "int3"];
const X86_RETURN: &[&str] = &[
    "ret", "retn", "retf", "iret", "iretd", "iretq", "sysret", "sysexit",
];
const X86_NOP: &[&str] = &[
    "nop",
    "pause",
    "hlt",
    "fwait",
    "endbr32",
    "endbr64",
    "mfence",
    "lfence",
    "sfence",
    "clflush",
    "clflushopt",
    "clwb",
    "emms",
    "femms",
    "prefetchnta",
    "prefetcht0",
    "prefetcht1",
    "prefetcht2",
    "prefetchw",
    "clc",
    "stc",
    "cmc",
    "cld",
    "std",
    "cli",
    "sti",
    "clac",
    "stac",
    "lahf",
    "sahf",
    "monitor",
    "mwait",
    "vzeroupper",
    "vzeroall",
    "fnop",
    "ffree",
    "fdecstp",
    "fincstp",
    "fnclex",
    "fninit",
];

const ARM_COPY: &[&str] = &[
    "mov",
    "movz",
    "movn",
    "movk",
    "adr",
    "adrp",
    "mvn",
    "movi",
    "dup",
    "smov",
    "umov",
    "ext",
    "tbl",
    "tbx",
    "uzp1",
    "uzp2",
    "trn1",
    "trn2",
    "zip1",
    "zip2",
    "rev",
    "rev16",
    "rev32",
    "rev64",
    "rbit",
    "csel",
    "csinc",
    "csinv",
    "csneg",
    "cinc",
    "cset",
    "cinv",
    "csetm",
    "cneg",
    "fcsel",
    "uxtb",
    "uxth",
    "sxtb",
    "sxth",
    "sxtw",
    "ubfx",
    "sbfx",
    "ubfiz",
    "sbfiz",
    "bfc",
    "bfi",
    "bfxil",
    "extr",
    "clz",
    "cls",
    "cnt",
    "abs",
    "sqabs",
    "xtn",
    "xtn2",
    "sqxtn",
    "sqxtn2",
    "sqxtun",
    "sqxtun2",
    "uqxtn",
    "uqxtn2",
    "fmov",
    "fabs",
    "scvtf",
    "ucvtf",
    "fcvt",
    "fcvtl",
    "fcvtl2",
    "fcvtn",
    "fcvtn2",
    "fcvtxn",
    "fcvtxn2",
    "fcvtns",
    "fcvtnu",
    "fcvtps",
    "fcvtpu",
    "fcvtms",
    "fcvtmu",
    "fcvtas",
    "fcvtau",
    "fcvtzs",
    "fcvtzu",
    "fjcvtzs",
    "bfcvt",
    "frintn",
    "frintp",
    "frintm",
    "frintz",
    "frinta",
    "frintx",
    "frinti",
    "frint32x",
    "frint32z",
    "frint64x",
    "frint64z",
    "fmax",
    "fmin",
    "fmaxnm",
    "fminnm",
    "fmaxp",
    "fmaxnmp",
    "fminp",
    "fminnmp",
    "fmaxv",
    "fmaxnmv",
    "fminv",
    "fminnmv",
    "smax",
    "smin",
    "umax",
    "umin",
    "smaxp",
    "sminp",
    "umaxp",
    "uminp",
    "smaxv",
    "sminv",
    "umaxv",
    "uminv",
    "frecpe",
    "frecps",
    "frecpx",
    "frsqrte",
    "frsqrts",
    "swp",
    "swpl",
    "swpa",
    "swpal",
    "swpb",
    "swplb",
    "swpab",
    "swpalb",
    "swph",
    "swplh",
    "swpah",
    "swpalh",
    "ldsmax",
    "ldsmaxl",
    "ldsmaxa",
    "ldsmaxal",
    "ldsmaxh",
    "ldsmaxlh",
    "ldsmaxah",
    "ldsmaxalh",
    "ldsmaxb",
    "ldsmaxlb",
    "ldsmaxab",
    "ldsmaxalb",
    "ldsmin",
    "ldsminl",
    "ldsmina",
    "ldsminal",
    "ldsminh",
    "ldsminlh",
    "ldsminah",
    "ldsminalh",
    "ldsminb",
    "ldsminlb",
    "ldsminab",
    "ldsminalb",
    "ldumax",
    "ldumaxl",
    "ldumaxa",
    "ldumaxal",
    "ldumaxh",
    "ldumaxlh",
    "ldumaxah",
    "ldumaxalh",
    "ldumaxb",
    "ldumaxlb",
    "ldumaxab",
    "ldumaxalb",
    "ldumin",
    "lduminl",
    "ldumina",
    "lduminal",
    "lduminh",
    "lduminlh",
    "lduminah",
    "lduminalh",
    "lduminb",
    "lduminlb",
    "lduminab",
    "lduminalb",
];
const ARM_LOAD: &[&str] = &[
    "ldr", "ldp", "ldpsw", "ldur", "ldrb", "ldrh", "ldrsb", "ldrsh", "ldrsw", "ldurb", "ldurh",
    "ldursb", "ldursh", "ldursw", "ldtr", "ldtrb", "ldtrh", "ldtrsb", "ldtrsh", "ldtrsw", "ldar",
    "ldarb", "ldarh", "ldaxr", "ldaxrb", "ldaxrh", "ldaxp", "ldxr", "ldxrb", "ldxrh", "ldxp",
    "ldlar", "ldlarb", "ldlarh", "ldapr", "ldaprh", "ldaprb", "ldapur", "ldapurb", "ldapurh",
    "ldapursb", "ldapursh", "ldapursw", "ldnp", "ld1", "ld2", "ld3", "ld4", "ld1r", "ld2r", "ld3r",
    "ld4r", "ldraa", "ldrab",
];
const ARM_STORE: &[&str] = &[
    "str", "stp", "stur", "strb", "strh", "sturb", "sturh", "sttr", "sttrb", "sttrh", "stlr",
    "stlrb", "stlrh", "stxr", "stxrb", "stxrh", "stxp", "stlxr", "stlxrb", "stlxrh", "stllr",
    "stllrb", "stllrh", "stlur", "stlurb", "stlurh", "stnp", "st1", "st2", "st3", "st4", "stadd",
    "staddl", "staddh", "staddlh", "staddb", "staddlb", "stclr", "stclrl", "stclrh", "stclrlh",
    "stclrb", "stclrlb", "stset", "stsetl", "stseth", "stsetlh", "stsetb", "stsetlb", "steor",
    "steorl", "steorh", "steorlh", "steorb", "steorlb", "stsmax", "stsmaxl", "stsmaxh", "stsmaxlh",
    "stsmaxb", "stsmaxlb", "stsmin", "stsminl", "stsminh", "stsminlh", "stsminb", "stsminlb",
    "stumax", "stumaxl", "stumaxh", "stumaxlh", "stumaxb", "stumaxlb", "stumin", "stuminl",
    "stuminh", "stuminlh", "stuminb", "stuminlb",
];
const ARM_ADD: &[&str] = &[
    "add", "adds", "adc", "adcs", "fadd", "faddp", "fcadd", "addv", "saddlv", "uaddlv", "saddlp",
    "sadalp", "uaddlp", "uadalp", "addp", "saddl", "saddl2", "uaddl", "uaddl2", "saddw", "saddw2",
    "uaddw", "uaddw2", "shadd", "uhadd", "srhadd", "urhadd", "sqadd", "uqadd", "suqadd", "usqadd",
    "saba", "uaba", "sabal", "sabal2", "uabal", "uabal2", "addhn", "addhn2", "raddhn", "raddhn2",
    "ldadd", "ldaddl", "ldadda", "ldaddal", "ldaddh", "ldaddlh", "ldaddah", "ldaddalh", "ldaddb",
    "ldaddlb", "ldaddab", "ldaddalb",
];
const ARM_SUB: &[&str] = &[
    "sub", "subs", "sbc", "sbcs", "neg", "negs", "ngc", "ngcs", "fsub", "fneg", "sqneg", "fabd",
    "ssubl", "ssubl2", "usubl", "usubl2", "ssubw", "ssubw2", "usubw", "usubw2", "shsub", "uhsub",
    "sqsub", "uqsub", "sabd", "uabd", "subhn", "subhn2", "rsubhn", "rsubhn2", "sabdl", "sabdl2",
    "uabdl", "uabdl2", "subp", "subps",
];
const ARM_MUL: &[&str] = &[
    "mul", "madd", "msub", "mneg", "mla", "mls", "smaddl", "smsubl", "umaddl", "umsubl", "smnegl",
    "umnegl", "smull", "smulh", "umull", "umulh", "smull2", "umull2", "umlal", "umlal2", "smlal",
    "smlal2", "smlsl", "smlsl2", "umlsl", "umlsl2", "sqdmulh", "sqrdmulh", "sqdmull", "sqdmull2",
    "sqdmlal", "sqdmlal2", "sqdmlsl", "sqdmlsl2", "sqrdmlah", "sqrdmlsh", "fmul", "fmulx", "fmla",
    "fmls", "fmlal", "fmlal2", "fmlsl", "fmlsl2", "fcmla", "fmadd", "fmsub", "fnmadd", "fnmsub",
    "fnmul", "pmul", "pmull", "pmull2", "sdot", "udot",
];
const ARM_DIV: &[&str] = &["sdiv", "udiv", "fdiv"];
const ARM_AND: &[&str] = &[
    "and", "ands", "bic", "bics", "ldclr", "ldclrl", "ldclra", "ldclral", "ldclrh", "ldclrlh",
    "ldclrah", "ldclralh", "ldclrb", "ldclrlb", "ldclrab", "ldclralb",
];
const ARM_OR: &[&str] = &[
    "orr", "orn", "bsl", "bit", "bif", "ldset", "ldsetl", "ldseta", "ldsetal", "ldseth", "ldsetlh",
    "ldsetah", "ldsetalh", "ldsetb", "ldsetlb", "ldsetab", "ldsetalb",
];
const ARM_XOR: &[&str] = &[
    "eor",
    "eon",
    "mvni",
    "eor3",
    "bcax",
    "rax1",
    "xar",
    "aese",
    "aesd",
    "aesmc",
    "aesimc",
    "sha1c",
    "sha1h",
    "sha1m",
    "sha1p",
    "sha1su0",
    "sha1su1",
    "sha256h",
    "sha256h2",
    "sha256su0",
    "sha256su1",
    "sha512h",
    "sha512h2",
    "sha512su0",
    "sha512su1",
    "sm3tt1a",
    "sm3tt1b",
    "sm3tt2a",
    "sm3tt2b",
    "sm3partw1",
    "sm3partw2",
    "sm3ss1",
    "sm4e",
    "sm4ekey",
    "crc32b",
    "crc32h",
    "crc32w",
    "crc32x",
    "crc32cb",
    "crc32ch",
    "crc32cw",
    "crc32cx",
    "ldeor",
    "ldeorl",
    "ldeora",
    "ldeoral",
    "ldeorh",
    "ldeorlh",
    "ldeorah",
    "ldeoralh",
    "ldeorb",
    "ldeorlb",
    "ldeorab",
    "ldeoralb",
];
const ARM_SHL: &[&str] = &[
    "lsl", "lslv", "shl", "sshl", "ushl", "srshl", "urshl", "sqrshl", "uqrshl", "sqshl", "uqshl",
    "sqshlu", "sli", "shll", "shll2", "sshll", "sshll2", "ushll", "ushll2",
];
const ARM_SHR: &[&str] = &[
    "lsr",
    "lsrv",
    "asr",
    "asrv",
    "ror",
    "rorv",
    "ushr",
    "sshr",
    "urshr",
    "srshr",
    "usra",
    "ssra",
    "ursra",
    "srsra",
    "sri",
    "shrn",
    "shrn2",
    "rshrn",
    "rshrn2",
    "sqshrn",
    "sqshrn2",
    "sqrshrn",
    "sqrshrn2",
    "sqshrun",
    "sqshrun2",
    "sqrshrun",
    "sqrshrun2",
    "uqshrn",
    "uqshrn2",
    "uqrshrn",
    "uqrshrn2",
];
const ARM_CMP: &[&str] = &[
    "cmp", "cmn", "tst", "ccmn", "ccmp", "cmtst", "fcmp", "fcmpe", "fccmp", "fccmpe", "cmeq",
    "cmgt", "cmge", "cmle", "cmlt", "cmhi", "cmhs", "fcmeq", "fcmge", "fcmgt", "fcmle", "fcmlt",
    "facge", "facgt", "cas", "casl", "casa", "casal", "casb", "caslb", "casab", "casalb", "cash",
    "caslh", "casah", "casalh", "casp", "caspl", "caspa", "caspal",
];
const ARM_CBRANCH: &[&str] = &["cbz", "cbnz", "tbz", "tbnz"];
/// Non-literal AArch64 members: `b.<cond>` / `bc.<cond>` match the
/// [`is_arm_cbranch`] prefix rule, so they publish as patterns, not mnemonics.
const ARM_CBRANCH_PATTERNS: &[&str] = &["b.<cond>", "bc.<cond>"];
const ARM_CALL: &[&str] = &["svc", "hvc", "smc"];
const ARM_RETURN: &[&str] = &["ret", "eret"];
const ARM_NOP: &[&str] = &[
    "nop", "hint", "dmb", "dsb", "isb", "brk", "hlt", "sb", "ssbb", "pssbb", "clrex", "wfe", "wfi",
    "sev", "sevl", "yield", "esb", "csdb", "psb", "tsb", "prfm", "prfum", "sys", "sysl", "rmif",
    "setf8", "setf16", "dcps1", "dcps2", "dcps3", "drps",
];

const ARM32_COPY: &[&str] = &[
    "mov", "movw", "movt", "mvn", "adr", "swp", "swpb", "clz", "rev", "rev16", "revsh", "rbit",
    "sxth", "sxtb", "uxth", "uxtb",
];
const ARM32_LOAD: &[&str] = &[
    "ldr", "ldrb", "ldrh", "ldrsb", "ldrsh", "ldrd", "ldm", "ldmia", "ldmdb", "ldmda", "pop",
    "ldrt", "ldrbt", "ldrht", "ldrsbt", "ldrsht", "ldrex", "ldrexb", "ldrexh", "ldrexd",
];
const ARM32_STORE: &[&str] = &[
    "str", "strb", "strh", "strd", "stm", "stmia", "stmdb", "stmda", "push", "strt", "strbt",
    "strht", "strex", "strexb", "strexh", "strexd",
];
const ARM32_ADD: &[&str] = &[
    "add", "adc", "qadd", "qdadd", "qadd16", "qadd8", "uqadd16", "uqadd8",
];
const ARM32_SUB: &[&str] = &[
    "sub", "sbc", "rsb", "rsc", "qsub", "qdsub", "qsub16", "qsub8", "uqsub16", "uqsub8",
];
const ARM32_MUL: &[&str] = &[
    "mul", "mla", "umaal", "mls", "smlabb", "smlabt", "smlatb", "smlatt", "smlawb", "smlawt",
    "smulbb", "smulbt", "smultb", "smultt", "smlalbb", "smlalbt", "smlaltb", "smlaltt",
];
const ARM32_DIV: &[&str] = &["sdiv", "udiv"];
const ARM32_AND: &[&str] = &["and", "bic"];
const ARM32_OR: &[&str] = &["orr", "orn"];
const ARM32_XOR: &[&str] = &["eor"];
const ARM32_SHL: &[&str] = &["lsl"];
const ARM32_SHR: &[&str] = &["lsr", "asr", "ror", "rrx"];
const ARM32_CMP: &[&str] = &["cmp", "cmn", "tst", "teq"];
const ARM32_CALL: &[&str] = &["svc", "hvc", "smc"];
const ARM32_NOP: &[&str] = &[
    "nop", "dmb", "dsb", "isb", "bkpt", "it", "hint", "wfe", "wfi", "sev", "yield", "cpsid",
    "cpsie", "setend", "dbg",
];

fn is_x86_jcc(s: &str) -> bool {
    X86_JCC.contains(&s)
}

/// Whether `s` is an x86 compare: the literal table plus iced-x86's
/// imm-folded pseudo-ops (`cmpeqps`, `vcmpltps`, ...), which never print in
/// their generic `cmpps`/`vcmpps` form. The `cmp`/`vcmp` prefix with a lane
/// width suffix only ever matches compares; `cmpxchg` and the string
/// `cmps*` forms do not end in a width and stay literal-only.
fn is_x86_cmp(s: &str) -> bool {
    if X86_CMP.contains(&s) {
        return true;
    }
    let t = s.strip_prefix('v').unwrap_or(s);
    t.starts_with("cmp") && ["ps", "pd", "ss", "sd"].iter().any(|w| t.ends_with(w))
}

fn is_x86_call(s: &str) -> bool {
    X86_CALL.contains(&s)
}

fn is_x86_return(s: &str) -> bool {
    X86_RETURN.contains(&s)
}

fn is_x86_nop(s: &str) -> bool {
    X86_NOP.contains(&s)
}

fn is_arm_copy(s: &str) -> bool {
    ARM_COPY.contains(&s)
}

fn is_arm_load(s: &str) -> bool {
    ARM_LOAD.contains(&s)
}

fn is_arm_store(s: &str) -> bool {
    ARM_STORE.contains(&s)
}

fn is_arm_alu(s: &str, op: IrOp) -> bool {
    match op {
        IrOp::Add => ARM_ADD.contains(&s),
        IrOp::Sub => ARM_SUB.contains(&s),
        IrOp::Mul => ARM_MUL.contains(&s),
        IrOp::Div => ARM_DIV.contains(&s),
        IrOp::And => ARM_AND.contains(&s),
        IrOp::Or => ARM_OR.contains(&s),
        IrOp::Xor => ARM_XOR.contains(&s),
        IrOp::Shl => ARM_SHL.contains(&s),
        IrOp::Shr => ARM_SHR.contains(&s),
        _ => false,
    }
}

fn is_arm_cbranch(s: &str) -> bool {
    ARM_CBRANCH.contains(&s) || s.starts_with("b.") || s.starts_with("bc.")
}

fn is_arm_call(s: &str) -> bool {
    ARM_CALL.contains(&s)
}

fn is_arm_return(s: &str) -> bool {
    ARM_RETURN.contains(&s)
}

fn is_arm_nop(s: &str) -> bool {
    ARM_NOP.contains(&s)
}

fn is_arm32_copy(s: &str) -> bool {
    ARM32_COPY.contains(&s)
}

fn is_arm32_load(s: &str) -> bool {
    ARM32_LOAD.contains(&s)
}

fn is_arm32_store(s: &str) -> bool {
    ARM32_STORE.contains(&s)
}

fn is_arm32_alu(s: &str, op: IrOp) -> bool {
    match op {
        IrOp::Add => ARM32_ADD.contains(&s),
        IrOp::Sub => ARM32_SUB.contains(&s),
        IrOp::Mul => ARM32_MUL.contains(&s),
        IrOp::Div => ARM32_DIV.contains(&s),
        IrOp::And => ARM32_AND.contains(&s),
        IrOp::Or => ARM32_OR.contains(&s),
        IrOp::Xor => ARM32_XOR.contains(&s),
        IrOp::Shl => ARM32_SHL.contains(&s),
        IrOp::Shr => ARM32_SHR.contains(&s),
        _ => false,
    }
}

fn is_arm32_call(s: &str) -> bool {
    ARM32_CALL.contains(&s)
}

/// Whether `s` is a load-multiple covered by the `pop`/`ldm`-with-`pc`
/// return rule (`ldm` and its `ia`/`db`/`da` addressing-mode spellings).
fn is_arm32_ldm(s: &str) -> bool {
    s == "pop" || s == "ldm" || s == "ldmia" || s == "ldmdb" || s == "ldmda"
}

fn is_arm32_nop(s: &str) -> bool {
    ARM32_NOP.contains(&s)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn insn(mnem: &str, ops: &[&str]) -> Insn {
        Insn {
            addr: 0x1000,
            size: 1,
            bytes: vec![0x90],
            mnem: mnem.to_string(),
            ops: ops.iter().map(|s| (*s).to_string()).collect(),
            branch_target: None,
            mem_target: None,
            table_base: None,
        }
    }

    #[test]
    fn op_names_are_uppercase() {
        assert_eq!(IrOp::Copy.name(), "COPY");
        assert_eq!(IrOp::CBranch.name(), "CBRANCH");
        assert_eq!(IrOp::Nop.name(), "NOP");
        assert!(IrOp::Call.is_control());
        assert!(!IrOp::Add.is_control());
    }

    #[test]
    fn x86_mov_is_copy() {
        let v = lift_x86(&insn("mov", &["rax", "rbx"]));
        assert_eq!(v.len(), 1);
        assert_eq!(v[0].op, IrOp::Copy);
        assert_eq!(v[0].outputs, ["rax"]);
        assert_eq!(v[0].inputs, ["rbx"]);
        assert_eq!(v[0].warning, None);
    }

    #[test]
    fn x86_jcc_is_cbranch() {
        let mut i = insn("je", &["short 0x1002"]);
        i.branch_target = Some(0x1002);
        let v = lift_x86(&i);
        assert_eq!(v[0].op, IrOp::CBranch);
        assert_eq!(v[0].target, Some(0x1002));
    }

    #[test]
    fn x86_unknown_is_nop_with_warning() {
        let v = lift_x86(&insn("cpuid", &[]));
        assert_eq!(v[0].op, IrOp::Nop);
        assert!(v[0].warning.is_some());
    }

    #[test]
    fn arm_b_eq_is_cbranch() {
        let mut i = insn("b.eq", &["$+0x8"]);
        i.branch_target = Some(0x4008);
        let v = lift_aarch64(&i);
        assert_eq!(v[0].op, IrOp::CBranch);
        assert_eq!(v[0].target, Some(0x4008));
    }

    #[test]
    fn arm_unknown_is_nop_with_warning() {
        let v = lift_aarch64(&insn("mrs", &["x0", "tpidr_el0"]));
        assert_eq!(v[0].op, IrOp::Nop);
        assert!(v[0].warning.is_some());
    }

    #[test]
    fn arm_adrp_add_pair_second_row_carries_final_address() {
        let mut first = insn("adrp", &["x0", "#0x0"]);
        first.addr = 0x4000;
        first.size = 4;
        first.mem_target = Some(0x4000);
        let mut second = insn("add", &["x0", "x0", "#0x10"]);
        second.addr = 0x4004;
        second.size = 4;
        let v = lift_aarch64_with_prev(&second, Some(&first));
        assert_eq!(v.len(), 1);
        assert_eq!(v[0].op, IrOp::Add);
        assert_eq!(v[0].target, Some(0x4010));
        // Single lift keeps current behavior exactly.
        let v = lift_aarch64(&second);
        assert_eq!(v[0].target, None);
        // Nonadjacent pairs never resolve.
        let mut gapped = second.clone();
        gapped.addr = 0x4008;
        let v = lift_aarch64_with_prev(&gapped, Some(&first));
        assert_eq!(v[0].target, None);
    }

    #[test]
    fn lift_never_emits_phi() {
        for m in ["mov", "add", "jmp", "call", "ret", "nop", "cpuid"] {
            for ir in lift_x86(&insn(m, &[])) {
                assert_ne!(ir.op, IrOp::Phi, "mnem {m}");
            }
        }
        for m in ["ldr", "add", "b", "blr", "ret", "nop", "mrs"] {
            for ir in lift_aarch64(&insn(m, &[])) {
                assert_ne!(ir.op, IrOp::Phi, "mnem {m}");
            }
        }
        for m in [
            "mov", "movs", "addeq", "b", "beq", "bl", "bl.w", "blx", "bx", "cbz", "svc", "push",
            "pop", "nop",
        ] {
            for ir in lift_arm32(&insn(m, &[])) {
                assert_ne!(ir.op, IrOp::Phi, "mnem {m}");
            }
        }
    }

    #[test]
    fn arm32_mnem_suffixes_reduce_to_base() {
        assert_eq!(split_arm_mnem("bl.w"), ("bl", false));
        assert_eq!(split_arm_mnem("beq.w"), ("b", true));
        assert_eq!(split_arm_mnem("addeq"), ("add", true));
        assert_eq!(split_arm_mnem("adds"), ("add", false));
        assert_eq!(split_arm_mnem("movs"), ("mov", false));
        // Guarded strips: teq keeps its own base, mrs/svc keep the `s`.
        assert_eq!(split_arm_mnem("teq"), ("teq", false));
        assert_eq!(split_arm_mnem("mrs"), ("mrs", false));
        assert_eq!(split_arm_mnem("svc"), ("svc", false));
        assert_eq!(split_arm_mnem("bls"), ("b", true));
    }

    #[test]
    fn arm32_bx_lr_is_return_but_predicated_is_cbranch() {
        let mut i = insn("bx", &["lr"]);
        assert_eq!(lift_arm32(&i)[0].op, IrOp::Return);
        i = insn("bxeq", &["lr"]);
        let v = lift_arm32(&i);
        assert_eq!(v[0].op, IrOp::CBranch);
        assert_eq!(v[0].target, None);
        // Indirect bx without lr is a plain indirect branch.
        let i = insn("bx", &["r3"]);
        let v = lift_arm32(&i);
        assert_eq!(v[0].op, IrOp::Branch);
        assert_eq!(v[0].target, None);
    }

    #[test]
    fn arm32_pop_pc_is_return() {
        let i = insn("pop", &["{r4", "pc}"]);
        assert_eq!(lift_arm32(&i)[0].op, IrOp::Return);
        let i = insn("pop", &["{r4", "r5}"]);
        assert_eq!(lift_arm32(&i)[0].op, IrOp::Load);
        let i = insn("push", &["{r4", "lr}"]);
        assert_eq!(lift_arm32(&i)[0].op, IrOp::Store);
    }

    #[test]
    fn arm32_cond_branch_is_cbranch() {
        let mut i = insn("beq", &["$+0x8"]);
        i.branch_target = Some(0x1008);
        let v = lift_arm32(&i);
        assert_eq!(v[0].op, IrOp::CBranch);
        assert_eq!(v[0].target, Some(0x1008));
        let i = insn("cbz", &["r0", "$+0x4"]);
        let v = lift_arm32(&i);
        assert_eq!(v[0].op, IrOp::CBranch);
    }

    #[test]
    fn arm32_unknown_is_nop_with_warning() {
        let v = lift_arm32(&insn("mrs", &["r0", "cpsr"]));
        assert_eq!(v[0].op, IrOp::Nop);
        assert!(v[0].warning.is_some());
    }
}