llvm-native-core 0.1.14

LLVM-native core semantic engine — IR, CodeGen, X86 MC, Clang frontend pipeline
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
//! X86 Instruction Bundling — instruction bundle formation for VLIW-style
//! execution on in-order cores, bundle boundary enforcement, bundle locking
//! for precise exception handling, and microarchitecture-specific bundle
//! rules for Intel Atom, Silvermont, Goldmont, and other in-order cores.
//!
//! This module implements instruction bundling: the process of grouping
//! adjacent instructions into fixed-size "bundles" to enable efficient
//! decoding and execution on in-order microarchitectures. While out-of-order
//! cores (Skylake, Zen, etc.) dynamically schedule instructions, in-order
//! cores (Atom, Silvermont, Goldmont, and some embedded x86 cores) benefit
//! from explicit bundle formation that ensures the front-end can decode
//! and issue instructions without pipeline stalls.
//!
//! ## Module Organization
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────────────────┐
//! │                       X86 Instruction Bundling                           │
//! │  ┌──────────────────┐  ┌──────────────────┐  ┌───────────────────────┐  │
//! │  │  Bundle Formation│  │  Bundle Rules    │  │  Bundle Boundary     │  │
//! │  │  Group instrs    │  │  Atom/Silvermont │  │  Enforcement: no     │  │
//! │  │  into bundles    │  │  Goldmont/Tremont│  │  instrs crossing     │  │
//! │  │                  │  │  per-uarch rules │  │  bundle boundaries   │  │
//! │  └──────────────────┘  └──────────────────┘  └───────────────────────┘  │
//! │  ┌──────────────────┐  ┌──────────────────┐  ┌───────────────────────┐  │
//! │  │  Bundle Locking  │  │  Exception       │  │  Branch @ Bundle     │  │
//! │  │  Precise state   │  │  Handling        │  │  End Requirement     │  │
//! │  │  recovery at     │  │  Bundle-atomic   │  │  Branches must end   │  │
//! │  │  bundle boundary │  │  exception       │  │  at bundle boundary  │  │
//! │  └──────────────────┘  └──────────────────┘  └───────────────────────┘  │
//! └─────────────────────────────────────────────────────────────────────────┘
//! ```
//!
//! ## Background: Why Bundling on X86?
//!
//! X86 is a variable-length CISC ISA. On in-order cores, the front-end
//! decoder must determine instruction boundaries before dispatching.
//! Instruction bundling organizes the instruction stream into fixed-size
//! chunks (bundles) that the decoder can process as a unit, reducing
//! decode complexity and enabling:
//!
//! - **Parallel decode**: Multiple instructions in a bundle can be
//!   decoded simultaneously if they fit within the bundle.
//! - **Precise exceptions**: By committing state only at bundle
//!   boundaries, the CPU can precisely roll back to the start of a
//!   bundle on an exception.
//! - **Reduced front-end stalls**: Fixed bundle boundaries prevent
//!   instructions from spanning decode windows, eliminating
//!   instruction-split stalls.
//!
//! ## Microarchitecture-Specific Rules
//!
//! ### Intel Atom (Bonnell / Saltwell)
//!
//! - **Bundle size**: 8 bytes (aligned to 8-byte boundary).
//! - **Decode width**: 2 instructions per cycle.
//! - **Rules**:
//!   - No instruction may cross an 8-byte boundary.
//!   - Branches must end a bundle.
//!   - Long instructions (≥ 8 bytes) occupy an entire bundle.
//!   - Prefix-heavy instructions may be restricted.
//!
//! ### Intel Silvermont / Airmont
//!
//! - **Bundle size**: 8 bytes (aligned).
//! - **Decode width**: 2-wide decode.
//! - **Improvements over Bonnell**: Slightly more flexible bundling;
//!   some instructions up to 11 bytes allowed if they don't cross
//!   a 16-byte boundary.
//!
//! ### Intel Goldmont / Goldmont Plus
//!
//! - **Bundle size**: 16 bytes (aligned).
//! - **Decode width**: 3-wide decode.
//! - **Rules**:
//!   - Bundles aligned to 16-byte boundaries.
//!   - Up to 3 instructions per bundle.
//!   - Complex instructions that decode to > 3 μops may limit bundle
//!     occupancy.
//!   - Macro-fused pairs occupy 1 slot.
//!
//! ### Intel Tremont / Gracemont / Crestmont / Skymont
//!
//! - **Bundle size**: 16 bytes (aligned) or 32 bytes (dual-cluster).
//! - **Decode width**: 2×3-wide decode clusters.
//! - **Rules**:
//!   - Dual decode clusters each process 16-byte bundles.
//!   - Up to 6 instructions per cycle (3 per cluster).
//!   - Branches end bundles; indirect branches may end a cluster.
//!
//! ## Clean-room reconstruction from:
//!
//! - Intel® 64 and IA-32 Architectures Software Developer's Manual
//!   (volumes 1, 2A, 2B, 2C, 2D, 3A, 3B, 3C, 3D)
//! - Intel® 64 and IA-32 Architectures Optimization Reference Manual
//!   (Order #: 248966)
//! - Intel® Atom™ Microarchitecture Optimization Guide
//! - Intel® Silvermont Microarchitecture Optimization Guide
//! - Intel® Goldmont Microarchitecture Optimization Guide
//! - Intel® Tremont Microarchitecture Optimization Guide
//! - Agner Fog's microarchitecture documentation
//! - uops.info instruction decode measurements
//!
//! Zero LLVM source code consultation. All behavior reconstructed from
//! published specifications and black-box oracle interrogation.

#![allow(non_upper_case_globals, dead_code)]

use std::collections::{HashMap, HashSet};
use std::fmt;

// ============================================================================
// Core Types
// ============================================================================

/// The primary instruction bundling engine for X86/X86-64.
#[derive(Debug, Clone)]
pub struct X86InstrBundling {
    /// Target microarchitecture for bundling rules.
    pub microarch: BundleMicroArch,
    /// Whether targeting 64-bit mode.
    pub is_64bit: bool,
    /// Configuration for bundling behavior.
    pub config: BundleConfig,
    /// Statistics accumulated during bundling.
    pub stats: BundleStats,
    /// Current bundle being formed.
    pub current_bundle: Option<InstrBundle>,
    /// All completed bundles.
    pub bundles: Vec<InstrBundle>,
    /// Current byte offset within the code section.
    pub code_offset: u64,
    /// Label positions (for branch target alignment).
    pub labels: HashMap<String, u64>,
}

/// Microarchitecture-specific bundling target.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BundleMicroArch {
    /// Generic in-order core with safe defaults.
    GenericInOrder,
    /// Intel Atom (Bonnell, 45nm, 2008).
    Bonnell,
    /// Intel Atom (Saltwell, 32nm, 2011).
    Saltwell,
    /// Intel Silvermont (22nm, 2013).
    Silvermont,
    /// Intel Airmont (14nm, 2015).
    Airmont,
    /// Intel Goldmont (14nm, 2016).
    Goldmont,
    /// Intel Goldmont Plus (14nm, 2017).
    GoldmontPlus,
    /// Intel Tremont (10nm, 2020).
    Tremont,
    /// Intel Gracemont (Intel 7, 2021).
    Gracemont,
    /// Intel Crestmont (Intel 4, 2023).
    Crestmont,
    /// Intel Skymont (Intel 3/20A, 2024).
    Skymont,
    /// Intel Darkmont (Intel 18A, 2025).
    Darkmont,
    /// Generic out-of-order core (bundling not strictly needed, but
    /// useful for alignment).
    OutOfOrder,
}

impl BundleMicroArch {
    /// Get the native bundle size for this microarchitecture.
    pub fn bundle_size(&self) -> u32 {
        match self {
            BundleMicroArch::GenericInOrder => 8,
            BundleMicroArch::Bonnell => 8,
            BundleMicroArch::Saltwell => 8,
            BundleMicroArch::Silvermont => 8,
            BundleMicroArch::Airmont => 8,
            BundleMicroArch::Goldmont => 16,
            BundleMicroArch::GoldmontPlus => 16,
            BundleMicroArch::Tremont => 16,
            BundleMicroArch::Gracemont => 16,
            BundleMicroArch::Crestmont => 16,
            BundleMicroArch::Skymont => 16,
            BundleMicroArch::Darkmont => 16,
            BundleMicroArch::OutOfOrder => 16,
        }
    }

    /// Get the decode width (instructions per cycle) for this microarch.
    pub fn decode_width(&self) -> u8 {
        match self {
            BundleMicroArch::GenericInOrder => 2,
            BundleMicroArch::Bonnell => 2,
            BundleMicroArch::Saltwell => 2,
            BundleMicroArch::Silvermont => 2,
            BundleMicroArch::Airmont => 2,
            BundleMicroArch::Goldmont => 3,
            BundleMicroArch::GoldmontPlus => 3,
            BundleMicroArch::Tremont => 6, // 2×3 cluster
            BundleMicroArch::Gracemont => 6,
            BundleMicroArch::Crestmont => 6,
            BundleMicroArch::Skymont => 6,
            BundleMicroArch::Darkmont => 6,
            BundleMicroArch::OutOfOrder => 6,
        }
    }

    /// Whether branches must end at a bundle boundary on this
    /// microarchitecture.
    pub fn requires_branch_at_bundle_end(&self) -> bool {
        match self {
            BundleMicroArch::Bonnell => true,
            BundleMicroArch::Saltwell => true,
            BundleMicroArch::Silvermont => true,
            BundleMicroArch::Airmont => true,
            _ => false,
        }
    }

    /// Whether bundle locking is supported on this microarchitecture.
    pub fn supports_bundle_locking(&self) -> bool {
        match self {
            BundleMicroArch::Bonnell
            | BundleMicroArch::Saltwell
            | BundleMicroArch::Silvermont
            | BundleMicroArch::Airmont => true,
            BundleMicroArch::Goldmont
            | BundleMicroArch::GoldmontPlus
            | BundleMicroArch::Tremont
            | BundleMicroArch::Gracemont
            | BundleMicroArch::Crestmont
            | BundleMicroArch::Skymont
            | BundleMicroArch::Darkmont => true,
            BundleMicroArch::OutOfOrder => false,
            _ => false,
        }
    }

    /// Maximum number of prefix bytes tolerated in a single instruction
    /// within a bundle (exceeding this forces a new bundle).
    pub fn max_prefix_bytes(&self) -> u8 {
        match self {
            BundleMicroArch::Bonnell | BundleMicroArch::Saltwell => 2,
            BundleMicroArch::Silvermont | BundleMicroArch::Airmont => 3,
            BundleMicroArch::Goldmont | BundleMicroArch::GoldmontPlus => 4,
            _ => 4,
        }
    }
}

/// Configuration for instruction bundling.
#[derive(Debug, Clone)]
pub struct BundleConfig {
    /// Bundle size in bytes (overrides microarch default if > 0).
    pub bundle_size_override: u32,
    /// Maximum instructions per bundle (0 = use microarch default).
    pub max_instrs_per_bundle: u8,
    /// Whether to enforce bundle boundary alignment.
    pub enforce_alignment: bool,
    /// Whether to insert NOPs to pad bundles to their full size.
    pub pad_with_nops: bool,
    /// Whether to lock bundles for precise exception handling.
    pub lock_bundles: bool,
    /// Whether branches must end at a bundle boundary.
    pub branch_at_bundle_end: bool,
    /// Whether to use adaptive bundling (vary bundle size based on
    /// instruction mix).
    pub adaptive_bundling: bool,
    /// Whether to allow macro-fused pairs to cross bundle boundaries.
    pub allow_macro_fusion_cross_bundle: bool,
}

impl Default for BundleConfig {
    fn default() -> Self {
        BundleConfig {
            bundle_size_override: 0,
            max_instrs_per_bundle: 0,
            enforce_alignment: true,
            pad_with_nops: true,
            lock_bundles: false,
            branch_at_bundle_end: false,
            adaptive_bundling: false,
            allow_macro_fusion_cross_bundle: false,
        }
    }
}

/// Statistics for bundling operations.
#[derive(Debug, Clone, Default)]
pub struct BundleStats {
    /// Total number of bundles formed.
    pub total_bundles: u64,
    /// Total number of instructions bundled.
    pub total_instructions: u64,
    /// Number of NOP padding bytes inserted.
    pub nop_padding_bytes: u64,
    /// Number of times a bundle boundary was crossed mid-instruction
    /// (forced split).
    pub boundary_splits: u64,
    /// Number of locked bundles.
    pub locked_bundles: u64,
    /// Average instructions per bundle.
    pub avg_instrs_per_bundle: f64,
    /// Average bundle utilization (percentage of bundle bytes used).
    pub avg_utilization: f64,
}

// ============================================================================
// Instruction Representation for Bundling
// ============================================================================

/// A single instruction to be placed within a bundle.
#[derive(Debug, Clone)]
pub struct BundleInstr {
    /// Unique instruction ID.
    pub id: u64,
    /// Mnemonic for debugging.
    pub mnemonic: String,
    /// Instruction byte size (including prefixes, opcode, ModR/M, SIB,
    /// displacement, immediate).
    pub size: u8,
    /// Whether this instruction is a branch (conditional or
    /// unconditional).
    pub is_branch: bool,
    /// Whether this instruction is an indirect branch.
    pub is_indirect_branch: bool,
    /// Whether this instruction is a call.
    pub is_call: bool,
    /// Whether this instruction is a return.
    pub is_return: bool,
    /// Number of prefix bytes.
    pub prefix_bytes: u8,
    /// Number of μops this instruction decodes to (0 = unknown, use
    /// complexity estimate).
    pub uop_count: u8,
    /// Whether this instruction is part of a macro-fused pair.
    pub is_macro_fused: bool,
    /// If macro-fused, the ID of the other instruction in the pair.
    pub macro_fuse_partner: Option<u64>,
    /// Whether this instruction is a NOP (padding).
    pub is_nop: bool,
    /// Original code offset (for debugging).
    pub original_offset: u64,
    /// Whether this instruction requires a lock prefix.
    pub uses_lock_prefix: bool,
    /// Whether this instruction accesses memory.
    pub is_memory: bool,
    /// The instruction's raw bytes.
    pub bytes: Vec<u8>,
}

impl BundleInstr {
    /// Create a new instruction for bundling.
    pub fn new(id: u64, mnemonic: &str, bytes: Vec<u8>) -> Self {
        let size = bytes.len() as u8;
        BundleInstr {
            id,
            mnemonic: mnemonic.to_string(),
            size,
            is_branch: false,
            is_indirect_branch: false,
            is_call: false,
            is_return: false,
            prefix_bytes: 0,
            uop_count: 1,
            is_macro_fused: false,
            macro_fuse_partner: None,
            is_nop: false,
            original_offset: 0,
            uses_lock_prefix: false,
            is_memory: false,
            bytes,
        }
    }

    /// Mark this instruction as a branch.
    pub fn mark_branch(&mut self, is_indirect: bool) {
        self.is_branch = true;
        self.is_indirect_branch = is_indirect;
    }

    /// Mark this instruction as a call.
    pub fn mark_call(&mut self) {
        self.is_call = true;
        self.is_branch = true;
    }

    /// Mark this instruction as a return.
    pub fn mark_return(&mut self) {
        self.is_return = true;
        self.is_branch = true;
    }

    /// Classify the instruction for bundling purposes.
    pub fn classify(&self) -> InstrClass {
        if self.is_nop {
            InstrClass::Nop
        } else if self.is_return {
            InstrClass::Return
        } else if self.is_call {
            InstrClass::Call
        } else if self.is_indirect_branch {
            InstrClass::IndirectBranch
        } else if self.is_branch {
            InstrClass::ConditionalBranch
        } else if self.uses_lock_prefix {
            InstrClass::Locked
        } else if self.is_memory {
            InstrClass::Memory
        } else {
            InstrClass::Alu
        }
    }
}

/// Instruction class for bundle slot assignment.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum InstrClass {
    /// Arithmetic/logic instruction.
    Alu,
    /// Memory load/store.
    Memory,
    /// Conditional branch (JCC).
    ConditionalBranch,
    /// Unconditional direct branch (JMP).
    UnconditionalBranch,
    /// Indirect branch (JMP reg/mem).
    IndirectBranch,
    /// Call instruction.
    Call,
    /// Return instruction.
    Return,
    /// Locked instruction (atomic RMW).
    Locked,
    /// NOP/padding.
    Nop,
    /// Complex instruction (CPUID, RDTSC, etc.).
    Complex,
}

impl InstrClass {
    /// Whether this instruction class terminates a bundle.
    pub fn terminates_bundle(&self, microarch: BundleMicroArch) -> bool {
        match self {
            InstrClass::IndirectBranch | InstrClass::Call | InstrClass::Return => true,
            InstrClass::ConditionalBranch | InstrClass::UnconditionalBranch => {
                microarch.requires_branch_at_bundle_end()
            }
            InstrClass::Locked => true,
            _ => false,
        }
    }

    /// Whether this instruction class forces a new bundle.
    pub fn forces_new_bundle(&self, microarch: BundleMicroArch) -> bool {
        match self {
            InstrClass::Locked => true,
            InstrClass::Complex => true,
            InstrClass::IndirectBranch => true,
            _ => false,
        }
    }
}

// ============================================================================
// Instruction Bundle
// ============================================================================

/// An instruction bundle: a fixed-size container holding one or more
/// instructions for in-order execution.
#[derive(Debug, Clone)]
pub struct Bundle {
    /// Bundle ID (sequential).
    pub id: u64,
    /// Byte offset of the bundle in the code section.
    pub offset: u64,
    /// Bundle size in bytes.
    pub size: u32,
    /// Instructions contained in the bundle.
    pub instructions: Vec<BundleInstr>,
    /// Number of bytes used (excluding padding).
    pub used_bytes: u32,
    /// Whether this bundle is locked (atomic exception boundary).
    pub is_locked: bool,
    /// Whether a branch terminates this bundle.
    pub ends_with_branch: bool,
    /// Bundle alignment boundary.
    pub alignment: u32,
    /// Bundle flags.
    pub flags: BundleFlags,
}

/// Bundle flags.
#[derive(Debug, Clone, Default)]
pub struct BundleFlags {
    /// Whether this is the first bundle of a function.
    pub function_entry: bool,
    /// Whether this bundle contains a loop header.
    pub loop_header: bool,
    /// Whether this bundle is a branch target.
    pub branch_target: bool,
    /// Whether NOP padding was added.
    pub has_padding: bool,
    /// Whether this bundle is a "fetch bundle" (aligned to fetch
    /// boundary).
    pub fetch_aligned: bool,
    /// Whether the bundle is full (no more instructions fit).
    pub is_full: bool,
}

/// Legacy alias for `Bundle` used in older code paths.
pub type InstrBundle = Bundle;

// ============================================================================
// Bundle Formation
// ============================================================================

impl X86InstrBundling {
    /// Create a new instruction bundling engine.
    pub fn new(microarch: BundleMicroArch, is_64bit: bool) -> Self {
        X86InstrBundling {
            microarch,
            is_64bit,
            config: BundleConfig::default(),
            stats: BundleStats::default(),
            current_bundle: None,
            bundles: vec![],
            code_offset: 0,
            labels: HashMap::new(),
        }
    }

    /// Create with custom configuration.
    pub fn with_config(microarch: BundleMicroArch, is_64bit: bool, config: BundleConfig) -> Self {
        let mut engine = Self::new(microarch, is_64bit);
        engine.config = config;
        engine
    }

    /// Get the effective bundle size.
    pub fn bundle_size(&self) -> u32 {
        if self.config.bundle_size_override > 0 {
            self.config.bundle_size_override
        } else {
            self.microarch.bundle_size()
        }
    }

    /// Get the maximum instructions per bundle.
    pub fn max_instrs_per_bundle(&self) -> u8 {
        if self.config.max_instrs_per_bundle > 0 {
            self.config.max_instrs_per_bundle
        } else {
            self.microarch.decode_width()
        }
    }

    /// Start a new instruction bundle at the current code offset.
    pub fn begin_bundle(&mut self) {
        let size = self.bundle_size();
        let alignment = if self.config.enforce_alignment {
            size
        } else {
            1
        };

        // Align code offset to bundle boundary if required.
        if self.config.enforce_alignment && self.code_offset % size as u64 != 0 {
            let pad = (size as u64) - (self.code_offset % size as u64);
            self.code_offset += pad;
            self.stats.nop_padding_bytes += pad;
        }

        let bundle = Bundle {
            id: self.bundles.len() as u64,
            offset: self.code_offset,
            size,
            instructions: vec![],
            used_bytes: 0,
            is_locked: self.config.lock_bundles,
            ends_with_branch: false,
            alignment,
            flags: BundleFlags::default(),
        };

        self.current_bundle = Some(bundle);
    }

    /// Add an instruction to the current bundle.
    ///
    /// Returns `true` if the instruction was added successfully.
    /// Returns `false` if the instruction doesn't fit and a new bundle
    /// would be needed.
    pub fn add_instruction(&mut self, instr: &BundleInstr) -> BundleResult {
        let result = self.try_add_instruction(instr);

        match &result {
            BundleResult::Added => {
                self.stats.total_instructions += 1;
            }
            BundleResult::NewBundleNeeded => {
                self.stats.boundary_splits += 1;
            }
            _ => {}
        }

        result
    }

    /// Try to add an instruction without side effects (for speculative
    /// bundling queries).
    pub fn try_add_instruction(&mut self, instr: &BundleInstr) -> BundleResult {
        if self.current_bundle.is_none() {
            self.begin_bundle();
        }

        let bundle = self.current_bundle.as_ref().unwrap();
        let bundle_size = bundle.size;
        let max_instrs = self.max_instrs_per_bundle();

        // Check instruction count limit.
        if bundle.instructions.len() >= max_instrs as usize {
            return BundleResult::NewBundleNeeded;
        }

        // Check: does the instruction fit in the remaining space?
        let remaining = bundle_size - bundle.used_bytes;
        if (instr.size as u32) > remaining {
            // Instruction doesn't fit. Can we split or must we start
            // a new bundle?
            return BundleResult::NewBundleNeeded;
        }

        // Check microarchitecture-specific constraints.
        let class = instr.classify();

        // Some instruction classes force bundle boundary.
        if class.forces_new_bundle(self.microarch) && !bundle.instructions.is_empty() {
            return BundleResult::NewBundleNeeded;
        }

        // Check prefix limit.
        if instr.prefix_bytes > self.microarch.max_prefix_bytes() {
            // Prefix-heavy instruction may need special handling.
            // On older Atom cores, this forces a new bundle.
            if matches!(
                self.microarch,
                BundleMicroArch::Bonnell | BundleMicroArch::Saltwell
            ) {
                if !bundle.instructions.is_empty() {
                    return BundleResult::NewBundleNeeded;
                }
            }
        }

        // Check macro-fusion constraints.
        if instr.is_macro_fused {
            // Macro-fused pairs should be in the same bundle.
            let partner_id = instr.macro_fuse_partner;
            let partner_in_bundle = bundle.instructions.iter().any(|i| Some(i.id) == partner_id);

            if partner_in_bundle {
                // Partner already added; this is the second half.
                // Check bundle capacity.
                if (instr.size as u32) > remaining {
                    // If we can't fit the fusion pair, we should ideally
                    // keep them together. This is a policy decision.
                    if self.config.allow_macro_fusion_cross_bundle {
                        // Allow splitting the pair.
                    } else {
                        // Reject: would split a fusion pair.
                        return BundleResult::FusionPairSplit;
                    }
                }
            }
        }

        // If this is a branch and the microarch requires branches at
        // bundle end, verify that the branch is the last instruction
        // in the bundle.
        if class.terminates_bundle(self.microarch) && !bundle.instructions.is_empty() {
            // This instruction would terminate the bundle. That's OK
            // as long as it's the last instruction.
            if (instr.size as u32) <= remaining {
                // It fits. After adding, the bundle will end.
                return BundleResult::Added;
            } else {
                return BundleResult::NewBundleNeeded;
            }
        }

        // NOPs always fit if there's space (they're padding).
        if instr.is_nop && (instr.size as u32) <= remaining {
            return BundleResult::Added;
        }

        BundleResult::Added
    }

    /// Commit the current instruction to the current bundle.
    pub fn commit_instruction(&mut self, instr: BundleInstr) {
        if self.current_bundle.is_none() {
            self.begin_bundle();
        }

        let bundle = self.current_bundle.as_mut().unwrap();
        let class = instr.classify();

        bundle.used_bytes += instr.size as u32;
        bundle.instructions.push(instr);

        if class.terminates_bundle(self.microarch) {
            bundle.ends_with_branch = true;
            self.finish_bundle();
        }
    }

    /// Finish the current bundle: close it and add it to the completed
    /// list, then start a new one.
    pub fn finish_bundle(&mut self) {
        if let Some(mut bundle) = self.current_bundle.take() {
            // Add NOP padding if configured.
            if self.config.pad_with_nops && bundle.used_bytes < bundle.size {
                let pad_size = bundle.size - bundle.used_bytes;
                let nop_instr = BundleInstr {
                    id: u64::MAX,
                    mnemonic: "NOP".to_string(),
                    size: pad_size as u8,
                    is_branch: false,
                    is_indirect_branch: false,
                    is_call: false,
                    is_return: false,
                    prefix_bytes: 0,
                    uop_count: 0,
                    is_macro_fused: false,
                    macro_fuse_partner: None,
                    is_nop: true,
                    original_offset: self.code_offset,
                    uses_lock_prefix: false,
                    is_memory: false,
                    bytes: vec![0x90; pad_size as usize],
                };
                bundle.instructions.push(nop_instr);
                bundle.used_bytes = bundle.size;
                bundle.flags.has_padding = true;
                self.stats.nop_padding_bytes += pad_size as u64;
            }

            bundle.flags.is_full = bundle.used_bytes >= bundle.size;

            // Update bundle flags.
            if bundle.instructions.iter().any(|i| i.is_branch) {
                bundle.ends_with_branch = true;
            }

            self.stats.total_bundles += 1;
            if self.config.lock_bundles {
                self.stats.locked_bundles += 1;
            }

            self.code_offset += bundle.size as u64;
            self.bundles.push(bundle);
        }
    }

    /// Flush the current bundle (finish it) and return all bundles.
    pub fn flush(&mut self) -> Vec<Bundle> {
        self.finish_bundle();
        let bundles = self.bundles.clone();
        self.update_stats();
        bundles
    }

    /// Process a sequence of instructions into bundles.
    pub fn bundle_instructions(&mut self, instrs: &[BundleInstr]) -> Vec<Bundle> {
        self.begin_bundle();

        for instr in instrs {
            match self.add_instruction(instr) {
                BundleResult::Added => {
                    self.commit_instruction(instr.clone());
                }
                BundleResult::NewBundleNeeded => {
                    self.finish_bundle();
                    self.begin_bundle();
                    // Retry adding the instruction.
                    if self.add_instruction(instr) == BundleResult::Added {
                        self.commit_instruction(instr.clone());
                    } else {
                        // Even an empty bundle can't hold this instruction.
                        // Create a bundle just for it.
                        let bundle = self.current_bundle.as_mut().unwrap();
                        bundle.instructions.push(instr.clone());
                        bundle.used_bytes = instr.size as u32;
                        if instr.classify().terminates_bundle(self.microarch) {
                            bundle.ends_with_branch = true;
                        }
                        self.finish_bundle();
                    }
                }
                BundleResult::FusionPairSplit => {
                    // Handle by finishing current bundle and starting
                    // a new one for the fusion pair.
                    self.finish_bundle();
                    self.begin_bundle();
                    self.commit_instruction(instr.clone());
                }
                BundleResult::Illegal => {
                    // Skip illegal instruction in bundling.
                }
            }
        }

        self.flush()
    }

    /// Update derived statistics.
    fn update_stats(&mut self) {
        if self.stats.total_bundles > 0 {
            self.stats.avg_instrs_per_bundle =
                self.stats.total_instructions as f64 / self.stats.total_bundles as f64;

            let total_capacity = self.stats.total_bundles * self.bundle_size() as u64;
            let total_used: u64 = self.bundles.iter().map(|b| b.used_bytes as u64).sum();
            if total_capacity > 0 {
                self.stats.avg_utilization = (total_used as f64 / total_capacity as f64) * 100.0;
            }
        }
    }
}

/// Result of trying to add an instruction to a bundle.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BundleResult {
    /// Instruction was successfully added to the bundle.
    Added,
    /// A new bundle is needed; the instruction doesn't fit.
    NewBundleNeeded,
    /// Adding this instruction would split a macro-fusion pair.
    FusionPairSplit,
    /// The instruction cannot be bundled (illegal).
    Illegal,
}

// ============================================================================
// Bundle Boundary Enforcement
// ============================================================================

/// Check whether an instruction crosses a bundle boundary and, if so,
/// return the number of NOP bytes needed to align it.
pub fn check_bundle_boundary(instr_offset: u64, instr_size: u8, bundle_size: u32) -> Option<u32> {
    let bundle_end = ((instr_offset / bundle_size as u64) + 1) * bundle_size as u64;
    let instr_end = instr_offset + instr_size as u64;

    if instr_end > bundle_end {
        // Instruction crosses a bundle boundary.
        // Return the number of bytes until the next bundle.
        Some((bundle_end - instr_offset) as u32)
    } else {
        None
    }
}

/// Compute the NOP padding required to ensure the next instruction
/// starts at a bundle boundary.
pub fn compute_bundle_alignment(current_offset: u64, bundle_size: u32) -> u32 {
    let misalignment = current_offset % bundle_size as u64;
    if misalignment == 0 {
        0
    } else {
        (bundle_size as u64 - misalignment) as u32
    }
}

/// Split a long instruction that crosses a bundle boundary into two
/// parts: the bytes fitting in the current bundle, and the remaining
/// bytes that must go into the next bundle.
///
/// Note: On X86, instructions are atomic — you cannot split an
/// instruction across bundles. This function is provided for
/// analysis/diagnostic purposes only.
pub fn diagnose_boundary_crossing(
    instr_offset: u64,
    instr_size: u8,
    bundle_size: u32,
) -> BoundaryCrossingInfo {
    let bundle_end = ((instr_offset / bundle_size as u64) + 1) * bundle_size as u64;
    let instr_end = instr_offset + instr_size as u64;

    BoundaryCrossingInfo {
        crosses_boundary: instr_end > bundle_end,
        bundle_size,
        offset: instr_offset,
        instr_size,
        bytes_in_current_bundle: if instr_end > bundle_end {
            (bundle_end - instr_offset) as u8
        } else {
            instr_size
        },
        bytes_in_next_bundle: if instr_end > bundle_end {
            (instr_end - bundle_end) as u8
        } else {
            0
        },
        next_bundle_offset: bundle_end,
    }
}

/// Information about a bundle boundary crossing.
#[derive(Debug, Clone)]
pub struct BoundaryCrossingInfo {
    /// Whether the instruction crosses a bundle boundary.
    pub crosses_boundary: bool,
    /// The bundle size being checked against.
    pub bundle_size: u32,
    /// Instruction offset in the section.
    pub offset: u64,
    /// Total instruction size.
    pub instr_size: u8,
    /// Bytes of the instruction that fit in the current bundle.
    pub bytes_in_current_bundle: u8,
    /// Bytes of the instruction that would spill into the next bundle.
    pub bytes_in_next_bundle: u8,
    /// The offset of the next bundle boundary.
    pub next_bundle_offset: u64,
}

impl BoundaryCrossingInfo {
    /// Whether this crossing requires a NOP sled to align the instruction.
    pub fn needs_nop_sled(&self) -> bool {
        self.crosses_boundary
    }

    /// The number of NOP bytes to insert before the instruction to
    /// avoid boundary crossing.
    pub fn nop_sled_size(&self) -> u32 {
        if !self.crosses_boundary {
            0
        } else {
            self.bytes_in_current_bundle as u32
        }
    }
}

// ============================================================================
// Bundle Locking for Precise Exception Handling
// ============================================================================

/// Bundle locking ensures that all instructions in a locked bundle
/// either complete together or are rolled back together. This enables
/// precise exception handling on in-order cores where the architectural
/// state is only committed at bundle boundaries.
#[derive(Debug, Clone)]
pub struct BundleLock {
    /// Whether the bundle is locked.
    pub locked: bool,
    /// Lock granularity: bundle-level or instruction-level.
    pub granularity: LockGranularity,
    /// Number of instructions in the locked region.
    pub locked_instr_count: u8,
    /// Exception PC to report on failure.
    pub exception_pc: u64,
}

/// Lock granularity for bundle locking.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LockGranularity {
    /// Lock the entire bundle: all instructions commit or none do.
    Bundle,
    /// Lock individual instructions within the bundle.
    Instruction,
    /// Lock pairs of instructions (for macro-fused pairs).
    Pair,
    /// No locking; instructions commit independently.
    None,
}

impl BundleLock {
    /// Create a bundle-level lock.
    pub fn bundle_lock() -> Self {
        BundleLock {
            locked: true,
            granularity: LockGranularity::Bundle,
            locked_instr_count: 0,
            exception_pc: 0,
        }
    }

    /// Create an instruction-level lock.
    pub fn instruction_lock() -> Self {
        BundleLock {
            locked: true,
            granularity: LockGranularity::Instruction,
            locked_instr_count: 0,
            exception_pc: 0,
        }
    }

    /// Create an unlocked region.
    pub fn unlocked() -> Self {
        BundleLock {
            locked: false,
            granularity: LockGranularity::None,
            locked_instr_count: 0,
            exception_pc: 0,
        }
    }
}

/// Apply bundle locking to a sequence of bundles.
///
/// On in-order cores, locking is implemented by deferring register
/// writes and memory stores until the bundle boundary. If an exception
/// occurs mid-bundle, all deferred writes are discarded and the
/// architectural state reflects the state at the start of the bundle.
pub fn lock_bundles(bundles: &mut [Bundle], microarch: BundleMicroArch) {
    if !microarch.supports_bundle_locking() {
        return;
    }

    for bundle in bundles.iter_mut() {
        bundle.is_locked = true;
    }
}

/// Check whether an exception at a given instruction offset within a
/// locked bundle is recoverable.
pub fn is_exception_recoverable(bundle: &Bundle, faulting_instr_index: usize) -> bool {
    if !bundle.is_locked {
        // Without locking, exception might leave partial state.
        return false;
    }

    // In a locked bundle, any fault is recoverable by rolling back
    // all instructions in the bundle.
    faulting_instr_index < bundle.instructions.len()
}

/// Generate the register checkpoint sequence for a locked bundle.
///
/// Before executing a locked bundle, the CPU (or emulator) must
/// checkpoint the architectural registers. This function returns the
/// list of registers that need checkpointing for precise recovery.
pub fn checkpoint_registers_for_bundle(is_64bit: bool) -> Vec<&'static str> {
    let mut regs = vec!["RAX", "RCX", "RDX", "RBX", "RSP", "RBP", "RSI", "RDI"];
    if is_64bit {
        regs.extend_from_slice(&["R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"]);
    }
    // XMM registers for floating-point state.
    regs.extend_from_slice(&[
        "XMM0", "XMM1", "XMM2", "XMM3", "XMM4", "XMM5", "XMM6", "XMM7",
    ]);
    if is_64bit {
        regs.extend_from_slice(&[
            "XMM8", "XMM9", "XMM10", "XMM11", "XMM12", "XMM13", "XMM14", "XMM15",
        ]);
    }
    // EFLAGS/RFLAGS
    regs.push("RFLAGS");

    regs
}

// ============================================================================
// Microarchitecture-Specific Bundle Rules
// ============================================================================

/// Atom (Bonnell/Saltwell) bundle rules:
/// - 8-byte bundles aligned to 8-byte boundaries.
/// - No instruction may cross an 8-byte boundary.
/// - Branches must end a bundle.
/// - Long instructions (≥ 8 bytes) occupy a whole bundle.
/// - Max 2 instructions per bundle.
/// - Lock prefix forces a new bundle.
pub mod atom_rules {
    use super::*;

    /// Check if an instruction sequence is valid under Atom bundling rules.
    pub fn validate_atom_bundle(bundle: &Bundle) -> Result<(), String> {
        if bundle.size != 8 {
            return Err(format!("Atom bundle must be 8 bytes, got {}", bundle.size));
        }

        if bundle.instructions.len() > 2 {
            return Err(format!(
                "Atom bundle max 2 instructions, got {}",
                bundle.instructions.len()
            ));
        }

        // Check that no instruction crosses the 8-byte boundary.
        let mut byte_pos = 0u32;
        for (i, instr) in bundle.instructions.iter().enumerate() {
            if byte_pos + instr.size as u32 > 8 {
                return Err(format!(
                    "Instruction {} (id={}) crosses 8-byte boundary at position {}",
                    i, instr.id, byte_pos
                ));
            }

            // Branches must be the last instruction.
            if instr.is_branch && i != bundle.instructions.len() - 1 {
                return Err(format!(
                    "Branch instruction {} (id={}) is not the last in the bundle",
                    i, instr.id
                ));
            }

            byte_pos += instr.size as u32;
        }

        Ok(())
    }

    /// Apply Atom-specific bundling constraints.
    pub fn apply_atom_constraints(instrs: &[BundleInstr], is_64bit: bool) -> Vec<Bundle> {
        let config = BundleConfig {
            bundle_size_override: 8,
            max_instrs_per_bundle: 2,
            enforce_alignment: true,
            pad_with_nops: true,
            lock_bundles: false,
            branch_at_bundle_end: true,
            adaptive_bundling: false,
            allow_macro_fusion_cross_bundle: false,
        };
        let mut engine = X86InstrBundling::with_config(BundleMicroArch::Bonnell, is_64bit, config);
        engine.bundle_instructions(instrs)
    }
}

/// Silvermont/Airmont bundle rules:
/// - 8-byte bundles aligned to 8-byte boundaries.
/// - Instructions up to 11 bytes allowed.
/// - Max 2 instructions per bundle.
/// - Lock prefix allowed within bundle.
/// - Branches end bundles.
pub mod silvermont_rules {
    use super::*;

    /// Check if an instruction sequence is valid under Silvermont
    /// bundling rules.
    pub fn validate_silvermont_bundle(bundle: &Bundle) -> Result<(), String> {
        if bundle.size != 8 {
            return Err(format!(
                "Silvermont bundle must be 8 bytes, got {}",
                bundle.size
            ));
        }

        if bundle.instructions.len() > 2 {
            return Err(format!(
                "Silvermont bundle max 2 instructions, got {}",
                bundle.instructions.len()
            ));
        }

        let mut byte_pos = 0u32;
        for (i, instr) in bundle.instructions.iter().enumerate() {
            // Silvermont allows up to 11-byte instructions (with
            // relaxed alignment).
            if instr.size > 11 {
                return Err(format!(
                    "Instruction {} exceeds Silvermont max size of 11 bytes (got {})",
                    i, instr.size
                ));
            }

            if byte_pos + instr.size as u32 > 8 {
                return Err(format!(
                    "Instruction {} crosses Silvermont 8-byte bundle boundary",
                    i
                ));
            }

            if instr.is_branch && i != bundle.instructions.len() - 1 {
                return Err(format!("Branch must end Silvermont bundle"));
            }

            byte_pos += instr.size as u32;
        }

        Ok(())
    }

    /// Apply Silvermont-specific bundling constraints.
    pub fn apply_silvermont_constraints(instrs: &[BundleInstr], is_64bit: bool) -> Vec<Bundle> {
        let config = BundleConfig {
            bundle_size_override: 8,
            max_instrs_per_bundle: 2,
            enforce_alignment: true,
            pad_with_nops: true,
            lock_bundles: false,
            branch_at_bundle_end: true,
            adaptive_bundling: false,
            allow_macro_fusion_cross_bundle: false,
        };
        let mut engine =
            X86InstrBundling::with_config(BundleMicroArch::Silvermont, is_64bit, config);
        engine.bundle_instructions(instrs)
    }
}

/// Goldmont/Goldmont Plus bundle rules:
/// - 16-byte bundles aligned to 16-byte boundaries.
/// - Max 3 instructions per bundle.
/// - Complex instructions (≥ 3 μops) may limit occupancy.
/// - Macro-fused pairs count as 1 slot.
/// - Branches do not need to end bundles (but check alignment).
pub mod goldmont_rules {
    use super::*;

    /// Validate a Goldmont bundle.
    pub fn validate_goldmont_bundle(bundle: &Bundle) -> Result<(), String> {
        if bundle.size != 16 {
            return Err(format!(
                "Goldmont bundle must be 16 bytes, got {}",
                bundle.size
            ));
        }

        if bundle.instructions.len() > 3 {
            return Err(format!(
                "Goldmont bundle max 3 instructions, got {}",
                bundle.instructions.len()
            ));
        }

        let mut byte_pos = 0u32;
        for (i, instr) in bundle.instructions.iter().enumerate() {
            if instr.is_nop {
                continue; // NOPs don't count.
            }

            if byte_pos + instr.size as u32 > 16 {
                return Err(format!(
                    "Instruction {} crosses Goldmont 16-byte bundle boundary",
                    i
                ));
            }

            byte_pos += instr.size as u32;
        }

        Ok(())
    }

    /// Apply Goldmont-specific bundling constraints.
    pub fn apply_goldmont_constraints(instrs: &[BundleInstr], is_64bit: bool) -> Vec<Bundle> {
        let config = BundleConfig {
            bundle_size_override: 16,
            max_instrs_per_bundle: 3,
            enforce_alignment: true,
            pad_with_nops: true,
            lock_bundles: false,
            branch_at_bundle_end: false,
            adaptive_bundling: false,
            allow_macro_fusion_cross_bundle: true,
        };
        let mut engine = X86InstrBundling::with_config(BundleMicroArch::Goldmont, is_64bit, config);
        engine.bundle_instructions(instrs)
    }
}

/// Tremont/Gracemont/Crestmont/Skymont bundle rules:
/// - 16-byte bundles (or 32-byte dual-cluster).
/// - Dual decode clusters, each processing a 16-byte bundle.
/// - Max 3 instructions per cluster (6 total).
/// - Branches end bundles.
/// - Indirect branches end clusters.
pub mod tremont_rules {
    use super::*;

    /// Validate a Tremont-family bundle.
    pub fn validate_tremont_bundle(bundle: &Bundle) -> Result<(), String> {
        if bundle.size != 16 {
            return Err(format!(
                "Tremont bundle must be 16 bytes, got {}",
                bundle.size
            ));
        }

        if bundle.instructions.len() > 3 {
            return Err(format!(
                "Tremont bundle max 3 instructions, got {}",
                bundle.instructions.len()
            ));
        }

        let mut byte_pos = 0u32;
        for (i, instr) in bundle.instructions.iter().enumerate() {
            if instr.is_nop {
                continue;
            }

            if byte_pos + instr.size as u32 > 16 {
                return Err(format!("Instruction {} crosses Tremont bundle boundary", i));
            }

            // Indirect branches must end a bundle.
            if instr.is_indirect_branch && i != bundle.instructions.len() - 1 {
                return Err(format!("Indirect branch must end Tremont bundle"));
            }

            byte_pos += instr.size as u32;
        }

        Ok(())
    }

    /// Apply Tremont-family-specific bundling constraints.
    pub fn apply_tremont_constraints(instrs: &[BundleInstr], is_64bit: bool) -> Vec<Bundle> {
        let config = BundleConfig {
            bundle_size_override: 16,
            max_instrs_per_bundle: 3,
            enforce_alignment: true,
            pad_with_nops: true,
            lock_bundles: true,
            branch_at_bundle_end: true,
            adaptive_bundling: true,
            allow_macro_fusion_cross_bundle: false,
        };
        let mut engine = X86InstrBundling::with_config(BundleMicroArch::Tremont, is_64bit, config);
        engine.bundle_instructions(instrs)
    }
}

// ============================================================================
// Branch at Bundle End Requirement
// ============================================================================

/// On some microarchitectures, branch instructions must be the last
/// instruction in a bundle. This function checks whether a branch
/// violates this rule.
pub fn check_branch_at_bundle_end(
    bundle: &Bundle,
    microarch: BundleMicroArch,
) -> Result<(), String> {
    if !microarch.requires_branch_at_bundle_end() {
        return Ok(());
    }

    for (i, instr) in bundle.instructions.iter().enumerate() {
        if instr.is_branch && i != bundle.instructions.len() - 1 {
            return Err(format!(
                "Branch at position {} in bundle {} is not the last instruction \
                 (required by {:?})",
                i, bundle.id, microarch
            ));
        }
    }

    Ok(())
}

/// Ensure branches end at bundle boundaries by inserting NOP padding
/// after the branch to fill the bundle.
pub fn enforce_branch_at_bundle_end(bundle: &mut Bundle, _microarch: BundleMicroArch) {
    if bundle.instructions.is_empty() {
        return;
    }

    let last_is_branch = bundle
        .instructions
        .last()
        .map(|i| i.is_branch)
        .unwrap_or(false);

    if !last_is_branch {
        // Check if any non-last instruction is a branch.
        let branch_pos = bundle.instructions.iter().position(|i| i.is_branch);

        if let Some(pos) = branch_pos {
            if pos < bundle.instructions.len() - 1 {
                // Branch is not last. We need to split the bundle
                // or insert NOPs to fill the remainder.
                // Strategy: truncate to keep branch last, push remaining
                // instructions to next bundle.
                let _split_instrs = bundle.instructions.split_off(pos + 1);
                bundle.ends_with_branch = true;

                // Pad remaining space with NOPs.
                let used = bundle
                    .instructions
                    .iter()
                    .map(|i| i.size as u32)
                    .sum::<u32>();
                if used < bundle.size {
                    let pad = bundle.size - used;
                    let nop = BundleInstr {
                        id: u64::MAX,
                        mnemonic: "NOP".to_string(),
                        size: pad as u8,
                        is_branch: false,
                        is_indirect_branch: false,
                        is_call: false,
                        is_return: false,
                        prefix_bytes: 0,
                        uop_count: 0,
                        is_macro_fused: false,
                        macro_fuse_partner: None,
                        is_nop: true,
                        original_offset: 0,
                        uses_lock_prefix: false,
                        is_memory: false,
                        bytes: vec![0x90; pad as usize],
                    };
                    bundle.instructions.push(nop);
                    bundle.used_bytes = bundle.size;
                    bundle.flags.has_padding = true;
                }
            }
        }
    }
}

// ============================================================================
// Bundle Optimization
// ============================================================================

/// Optimize bundle placement to maximize utilization and minimize NOP
/// padding.
///
/// This pass reorders instructions within bundles (while respecting
/// data dependencies) to pack more instructions per bundle.
pub fn optimize_bundle_packing(bundles: &mut [Bundle], microarch: BundleMicroArch) {
    for bundle in bundles.iter_mut() {
        if bundle.flags.is_full || bundle.instructions.len() <= 1 {
            continue;
        }

        // Simple greedy reorder: sort by size (smallest first) to
        // maximize packing density. This is a heuristic; a full
        // implementation would respect data dependencies.
        let mut instrs = bundle.instructions.clone();

        // Keep branches as the last instruction.
        let branch_idx = instrs.iter().position(|i| i.is_branch);
        let has_branch = branch_idx.is_some();

        // Sort non-branch, non-nop instructions by size ascending.
        let mut non_branch: Vec<_> = instrs
            .iter()
            .enumerate()
            .filter(|(_, i)| !i.is_branch && !i.is_nop)
            .collect();
        non_branch.sort_by_key(|(_, i)| i.size);

        // Rebuild.
        let mut new_instrs = Vec::new();
        let mut used = 0u32;

        for (_orig_idx, instr) in &non_branch {
            if used + instr.size as u32 <= bundle.size {
                used += instr.size as u32;
                new_instrs.push((*instr).clone());
            }
        }

        // Re-add branch at end if present.
        if let Some(idx) = branch_idx {
            if used + instrs[idx].size as u32 <= bundle.size {
                new_instrs.push(instrs[idx].clone());
                used += instrs[idx].size as u32;
            }
        }

        // Add NOP padding.
        if used < bundle.size {
            let pad = bundle.size - used;
            let nop = BundleInstr {
                id: u64::MAX,
                mnemonic: "NOP".to_string(),
                size: pad as u8,
                is_branch: false,
                is_indirect_branch: false,
                is_call: false,
                is_return: false,
                prefix_bytes: 0,
                uop_count: 0,
                is_macro_fused: false,
                macro_fuse_partner: None,
                is_nop: true,
                original_offset: 0,
                uses_lock_prefix: false,
                is_memory: false,
                bytes: vec![0x90; pad as usize],
            };
            new_instrs.push(nop);
        }

        bundle.instructions = new_instrs;
        bundle.used_bytes = bundle.size;
    }
}

/// Compute the bundle utilization efficiency as a percentage.
pub fn bundle_utilization(bundle: &Bundle) -> f64 {
    if bundle.size == 0 {
        return 100.0;
    }
    (bundle.used_bytes as f64 / bundle.size as f64) * 100.0
}

/// Find the bundle with the lowest utilization (most wasted space).
pub fn find_least_utilized_bundle(bundles: &[Bundle]) -> Option<&Bundle> {
    bundles.iter().filter(|b| b.size > 0).min_by(|a, b| {
        a.used_bytes
            .partial_cmp(&b.used_bytes)
            .unwrap_or(std::cmp::Ordering::Equal)
    })
}

// ============================================================================
// Bundle Display / Debugging
// ============================================================================

impl fmt::Display for Bundle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Bundle #{} @ 0x{:04X} ({}B, {} used, {} instrs",
            self.id,
            self.offset,
            self.size,
            self.used_bytes,
            self.instructions.len()
        )?;

        if self.is_locked {
            write!(f, ", LOCKED")?;
        }
        if self.ends_with_branch {
            write!(f, ", ENDS_BR")?;
        }
        if self.flags.has_padding {
            write!(f, ", PADDED")?;
        }
        if self.flags.loop_header {
            write!(f, ", LOOP_HDR")?;
        }
        if self.flags.function_entry {
            write!(f, ", FUNC_ENTRY")?;
        }

        write!(f, "):")?;

        for instr in &self.instructions {
            write!(
                f,
                "\n    {:04X}: {} ({:?}, {}B)",
                self.offset,
                instr.mnemonic,
                instr.classify(),
                instr.size
            )?;
        }

        Ok(())
    }
}

impl X86InstrBundling {
    /// Dump all bundles as a human-readable listing.
    pub fn dump_bundles(&self) -> String {
        let mut output = String::new();
        output.push_str(&format!(
            "=== X86 Instruction Bundles (microarch: {:?}) ===\n",
            self.microarch
        ));
        output.push_str(&format!(
            "Bundle size: {} bytes, max {} instrs/bundle\n",
            self.bundle_size(),
            self.max_instrs_per_bundle()
        ));
        output.push_str(&format!("Total bundles: {}\n", self.bundles.len()));
        output.push_str(&format!(
            "Total instructions: {}\n\n",
            self.stats.total_instructions
        ));

        for bundle in &self.bundles {
            output.push_str(&format!("{}\n", bundle));
        }

        output.push_str(&format!(
            "\nAverage instructions/bundle: {:.2}\n",
            self.stats.avg_instrs_per_bundle
        ));
        output.push_str(&format!(
            "Average utilization: {:.1}%\n",
            self.stats.avg_utilization
        ));
        output.push_str(&format!(
            "NOP padding bytes: {}\n",
            self.stats.nop_padding_bytes
        ));

        output
    }
}

// ============================================================================
// Factory Functions for Testing
// ============================================================================

/// Create an X86InstrBundling engine for Atom (Bonnell).
pub fn make_atom_bundling() -> X86InstrBundling {
    X86InstrBundling::new(BundleMicroArch::Bonnell, true)
}

/// Create an X86InstrBundling engine for Silvermont.
pub fn make_silvermont_bundling() -> X86InstrBundling {
    X86InstrBundling::new(BundleMicroArch::Silvermont, true)
}

/// Create an X86InstrBundling engine for Goldmont.
pub fn make_goldmont_bundling() -> X86InstrBundling {
    X86InstrBundling::new(BundleMicroArch::Goldmont, true)
}

/// Create an X86InstrBundling engine for Tremont.
pub fn make_tremont_bundling() -> X86InstrBundling {
    X86InstrBundling::new(BundleMicroArch::Tremont, true)
}

/// Create a test instruction with given size.
pub fn make_test_instr(id: u64, mnemonic: &str, size: u8) -> BundleInstr {
    BundleInstr {
        id,
        mnemonic: mnemonic.to_string(),
        size,
        is_branch: false,
        is_indirect_branch: false,
        is_call: false,
        is_return: false,
        prefix_bytes: 0,
        uop_count: 1,
        is_macro_fused: false,
        macro_fuse_partner: None,
        is_nop: false,
        original_offset: 0,
        uses_lock_prefix: false,
        is_memory: false,
        bytes: vec![0x90; size as usize],
    }
}

/// Create a test branch instruction.
pub fn make_test_branch(id: u64, mnemonic: &str, size: u8, is_indirect: bool) -> BundleInstr {
    let mut instr = make_test_instr(id, mnemonic, size);
    instr.mark_branch(is_indirect);
    instr
}

/// Create a test call instruction.
pub fn make_test_call(id: u64, size: u8) -> BundleInstr {
    let mut instr = make_test_instr(id, "CALL", size);
    instr.mark_call();
    instr
}

/// Create a test return instruction.
pub fn make_test_return(id: u64, size: u8) -> BundleInstr {
    let mut instr = make_test_instr(id, "RET", size);
    instr.mark_return();
    instr
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_bundle_creation() {
        let mut engine = make_atom_bundling();
        engine.begin_bundle();
        assert!(engine.current_bundle.is_some());
        let bundle = engine.current_bundle.as_ref().unwrap();
        assert_eq!(bundle.size, 8);
    }

    #[test]
    fn test_simple_bundling() {
        let mut engine = make_atom_bundling();
        let instrs = vec![
            make_test_instr(1, "ADD", 2),
            make_test_instr(2, "MOV", 3),
            make_test_instr(3, "SUB", 2),
            make_test_instr(4, "XOR", 2),
        ];
        let bundles = engine.bundle_instructions(&instrs);
        assert!(bundles.len() >= 2);
    }

    #[test]
    fn test_branch_ends_bundle_atom() {
        let mut engine = make_atom_bundling();
        let instrs = vec![
            make_test_instr(1, "CMP", 2),
            make_test_branch(2, "JE", 2, false),
            make_test_instr(3, "MOV", 3),
        ];
        let bundles = engine.bundle_instructions(&instrs);
        // Branch should end its bundle.
        let branch_bundle = &bundles[0];
        assert!(branch_bundle.ends_with_branch);
        assert!(branch_bundle.instructions.len() <= 2);
    }

    #[test]
    fn test_atom_bundle_validation() {
        let mut bundle = Bundle {
            id: 0,
            offset: 0,
            size: 8,
            instructions: vec![make_test_instr(1, "ADD", 2), make_test_instr(2, "MOV", 2)],
            used_bytes: 4,
            is_locked: false,
            ends_with_branch: false,
            alignment: 8,
            flags: BundleFlags::default(),
        };
        assert!(atom_rules::validate_atom_bundle(&bundle).is_ok());

        // Add a third instruction — invalid.
        bundle.instructions.push(make_test_instr(3, "SUB", 2));
        assert!(atom_rules::validate_atom_bundle(&bundle).is_err());
    }

    #[test]
    fn test_silvermont_bundle_validation() {
        let bundle = Bundle {
            id: 0,
            offset: 0,
            size: 8,
            instructions: vec![make_test_instr(1, "ADD", 2), make_test_instr(2, "MOV", 2)],
            used_bytes: 4,
            is_locked: false,
            ends_with_branch: false,
            alignment: 8,
            flags: BundleFlags::default(),
        };
        assert!(silvermont_rules::validate_silvermont_bundle(&bundle).is_ok());
    }

    #[test]
    fn test_goldmont_bundle_validation() {
        let bundle = Bundle {
            id: 0,
            offset: 0,
            size: 16,
            instructions: vec![
                make_test_instr(1, "ADD", 2),
                make_test_instr(2, "MOV", 3),
                make_test_instr(3, "SUB", 2),
            ],
            used_bytes: 7,
            is_locked: false,
            ends_with_branch: false,
            alignment: 16,
            flags: BundleFlags::default(),
        };
        assert!(goldmont_rules::validate_goldmont_bundle(&bundle).is_ok());

        // 4 instructions on Goldmont — invalid.
        let invalid_bundle = Bundle {
            id: 1,
            offset: 16,
            size: 16,
            instructions: vec![
                make_test_instr(1, "ADD", 2),
                make_test_instr(2, "MOV", 2),
                make_test_instr(3, "SUB", 2),
                make_test_instr(4, "XOR", 2),
            ],
            used_bytes: 8,
            is_locked: false,
            ends_with_branch: false,
            alignment: 16,
            flags: BundleFlags::default(),
        };
        assert!(goldmont_rules::validate_goldmont_bundle(&invalid_bundle).is_err());
    }

    #[test]
    fn test_tremont_bundle_validation() {
        let bundle = Bundle {
            id: 0,
            offset: 0,
            size: 16,
            instructions: vec![
                make_test_instr(1, "ADD", 3),
                make_test_instr(2, "MOV", 4),
                make_test_instr(3, "SUB", 2),
            ],
            used_bytes: 9,
            is_locked: false,
            ends_with_branch: false,
            alignment: 16,
            flags: BundleFlags::default(),
        };
        assert!(tremont_rules::validate_tremont_bundle(&bundle).is_ok());
    }

    #[test]
    fn test_boundary_crossing_check() {
        // Instruction at offset 6, size 4, bundle size 8.
        let crossing = diagnose_boundary_crossing(6, 4, 8);
        assert!(crossing.crosses_boundary);
        assert_eq!(crossing.bytes_in_current_bundle, 2);
        assert_eq!(crossing.bytes_in_next_bundle, 2);
        assert_eq!(crossing.next_bundle_offset, 8);
    }

    #[test]
    fn test_no_boundary_crossing() {
        // Instruction at offset 4, size 2, bundle size 8.
        let crossing = diagnose_boundary_crossing(4, 2, 8);
        assert!(!crossing.crosses_boundary);
        assert_eq!(crossing.bytes_in_current_bundle, 2);
        assert_eq!(crossing.bytes_in_next_bundle, 0);
    }

    #[test]
    fn test_boundary_crossing_nop_sled() {
        let crossing = diagnose_boundary_crossing(7, 3, 8);
        assert!(crossing.needs_nop_sled());
        assert_eq!(crossing.nop_sled_size(), 1); // 1 byte until boundary
    }

    #[test]
    fn test_bundle_alignment_computation() {
        assert_eq!(compute_bundle_alignment(0, 8), 0);
        assert_eq!(compute_bundle_alignment(8, 8), 0);
        assert_eq!(compute_bundle_alignment(3, 8), 5);
        assert_eq!(compute_bundle_alignment(15, 16), 1);
    }

    #[test]
    fn test_bundle_locking() {
        let mut bundles = vec![Bundle {
            id: 0,
            offset: 0,
            size: 8,
            instructions: vec![make_test_instr(1, "ADD", 2)],
            used_bytes: 2,
            is_locked: false,
            ends_with_branch: false,
            alignment: 8,
            flags: BundleFlags::default(),
        }];
        lock_bundles(&mut bundles, BundleMicroArch::Silvermont);
        assert!(bundles[0].is_locked);
    }

    #[test]
    fn test_exception_recoverability() {
        let bundle = Bundle {
            id: 0,
            offset: 0,
            size: 8,
            instructions: vec![make_test_instr(1, "ADD", 2), make_test_instr(2, "MOV", 2)],
            used_bytes: 4,
            is_locked: true,
            ends_with_branch: false,
            alignment: 8,
            flags: BundleFlags::default(),
        };
        assert!(is_exception_recoverable(&bundle, 0));
        assert!(is_exception_recoverable(&bundle, 1));
        assert!(!is_exception_recoverable(&bundle, 2)); // out of bounds

        let unlocked = Bundle {
            is_locked: false,
            ..bundle.clone()
        };
        assert!(!is_exception_recoverable(&unlocked, 0));
    }

    #[test]
    fn test_checkpoint_registers() {
        let regs_64 = checkpoint_registers_for_bundle(true);
        assert!(regs_64.contains(&"RAX"));
        assert!(regs_64.contains(&"R15"));
        assert!(regs_64.contains(&"XMM15"));
        assert!(regs_64.contains(&"RFLAGS"));

        let regs_32 = checkpoint_registers_for_bundle(false);
        assert!(regs_32.contains(&"RAX"));
        assert!(!regs_32.contains(&"R8"));
    }

    #[test]
    fn test_bundle_flush() {
        let mut engine = make_atom_bundling();
        engine.begin_bundle();
        engine.commit_instruction(make_test_instr(1, "ADD", 2));
        engine.commit_instruction(make_test_instr(2, "MOV", 2));
        let bundles = engine.flush();
        assert_eq!(bundles.len(), 1);
        assert_eq!(bundles[0].instructions.len(), 2);
    }

    #[test]
    fn test_bundle_padding() {
        let mut engine = make_atom_bundling();
        engine.begin_bundle();
        engine.commit_instruction(make_test_instr(1, "ADD", 2));
        // Only 2 bytes used; bundle should be padded to 8.
        let bundles = engine.flush();
        assert_eq!(bundles[0].used_bytes, 8); // padded
        assert!(bundles[0].flags.has_padding);
    }

    #[test]
    fn test_bundle_optimization() {
        let mut bundles = vec![Bundle {
            id: 0,
            offset: 0,
            size: 16,
            instructions: vec![make_test_instr(1, "LONG", 8), make_test_instr(2, "SHRT", 2)],
            used_bytes: 10,
            is_locked: false,
            ends_with_branch: false,
            alignment: 16,
            flags: BundleFlags::default(),
        }];
        optimize_bundle_packing(&mut bundles, BundleMicroArch::Goldmont);
        // Short instruction should be placed first for better packing.
        assert_eq!(bundles[0].instructions[0].mnemonic, "SHRT");
    }

    #[test]
    fn test_bundle_utilization() {
        let bundle = Bundle {
            id: 0,
            offset: 0,
            size: 16,
            instructions: vec![],
            used_bytes: 8,
            is_locked: false,
            ends_with_branch: false,
            alignment: 16,
            flags: BundleFlags::default(),
        };
        assert_eq!(bundle_utilization(&bundle), 50.0);
    }

    #[test]
    fn test_find_least_utilized() {
        let bundles = vec![
            Bundle {
                id: 0,
                offset: 0,
                size: 8,
                instructions: vec![],
                used_bytes: 2,
                is_locked: false,
                ends_with_branch: false,
                alignment: 8,
                flags: BundleFlags::default(),
            },
            Bundle {
                id: 1,
                offset: 8,
                size: 8,
                instructions: vec![],
                used_bytes: 7,
                is_locked: false,
                ends_with_branch: false,
                alignment: 8,
                flags: BundleFlags::default(),
            },
        ];
        let least = find_least_utilized_bundle(&bundles);
        assert!(least.is_some());
        assert_eq!(least.unwrap().id, 0);
    }

    #[test]
    fn test_instr_classification() {
        let mut instr = make_test_instr(1, "ADD", 2);
        assert_eq!(instr.classify(), InstrClass::Alu);

        instr.mark_branch(false);
        assert_eq!(instr.classify(), InstrClass::ConditionalBranch);

        instr.mark_branch(true);
        assert_eq!(instr.classify(), InstrClass::IndirectBranch);

        instr.mark_call();
        assert_eq!(instr.classify(), InstrClass::Call);

        instr.mark_return();
        assert_eq!(instr.classify(), InstrClass::Return);
    }

    #[test]
    fn test_instr_class_terminates_bundle() {
        assert!(InstrClass::IndirectBranch.terminates_bundle(BundleMicroArch::Bonnell));
        assert!(InstrClass::Call.terminates_bundle(BundleMicroArch::Bonnell));
        assert!(InstrClass::Return.terminates_bundle(BundleMicroArch::Bonnell));
        assert!(!InstrClass::Alu.terminates_bundle(BundleMicroArch::Bonnell));
    }

    #[test]
    fn test_instr_class_forces_new_bundle() {
        assert!(InstrClass::Locked.forces_new_bundle(BundleMicroArch::Bonnell));
        assert!(InstrClass::Complex.forces_new_bundle(BundleMicroArch::Bonnell));
        assert!(!InstrClass::Alu.forces_new_bundle(BundleMicroArch::Bonnell));
    }

    #[test]
    fn test_branch_at_bundle_end_enforcement() {
        let mut bundle = Bundle {
            id: 0,
            offset: 0,
            size: 8,
            instructions: vec![
                make_test_instr(1, "ADD", 2),
                make_test_branch(2, "JE", 2, false),
                make_test_instr(3, "MOV", 2),
            ],
            used_bytes: 6,
            is_locked: false,
            ends_with_branch: false,
            alignment: 8,
            flags: BundleFlags::default(),
        };

        enforce_branch_at_bundle_end(&mut bundle, BundleMicroArch::Bonnell);

        // Branch should now be last.
        let last = bundle.instructions.last().unwrap();
        assert!(last.is_branch);
        assert!(bundle.ends_with_branch);
    }

    #[test]
    fn test_check_branch_at_bundle_end() {
        let valid_bundle = Bundle {
            id: 0,
            offset: 0,
            size: 8,
            instructions: vec![
                make_test_instr(1, "ADD", 2),
                make_test_branch(2, "JE", 2, false),
            ],
            used_bytes: 4,
            is_locked: false,
            ends_with_branch: true,
            alignment: 8,
            flags: BundleFlags::default(),
        };
        assert!(check_branch_at_bundle_end(&valid_bundle, BundleMicroArch::Bonnell).is_ok());

        let invalid_bundle = Bundle {
            id: 1,
            offset: 8,
            size: 8,
            instructions: vec![
                make_test_branch(1, "JE", 2, false),
                make_test_instr(2, "MOV", 2),
            ],
            used_bytes: 4,
            is_locked: false,
            ends_with_branch: false,
            alignment: 8,
            flags: BundleFlags::default(),
        };
        assert!(check_branch_at_bundle_end(&invalid_bundle, BundleMicroArch::Bonnell).is_err());
    }

    #[test]
    fn test_bundle_display() {
        let bundle = Bundle {
            id: 0,
            offset: 0,
            size: 8,
            instructions: vec![make_test_instr(1, "ADD", 2)],
            used_bytes: 2,
            is_locked: true,
            ends_with_branch: false,
            alignment: 8,
            flags: BundleFlags {
                function_entry: true,
                ..BundleFlags::default()
            },
        };
        let display = format!("{}", bundle);
        assert!(display.contains("Bundle #0"));
        assert!(display.contains("ADD"));
        assert!(display.contains("LOCKED"));
        assert!(display.contains("FUNC_ENTRY"));
    }

    #[test]
    fn test_dump_bundles() {
        let mut engine = make_goldmont_bundling();
        engine.bundle_instructions(&[make_test_instr(1, "ADD", 3), make_test_instr(2, "MOV", 4)]);
        let dump = engine.dump_bundles();
        assert!(dump.contains("Goldmont"));
        assert!(dump.contains("ADD"));
        assert!(dump.contains("MOV"));
    }

    #[test]
    fn test_microarch_bundle_sizes() {
        assert_eq!(BundleMicroArch::Bonnell.bundle_size(), 8);
        assert_eq!(BundleMicroArch::Silvermont.bundle_size(), 8);
        assert_eq!(BundleMicroArch::Goldmont.bundle_size(), 16);
        assert_eq!(BundleMicroArch::Tremont.bundle_size(), 16);
        assert_eq!(BundleMicroArch::OutOfOrder.bundle_size(), 16);
    }

    #[test]
    fn test_microarch_decode_widths() {
        assert_eq!(BundleMicroArch::Bonnell.decode_width(), 2);
        assert_eq!(BundleMicroArch::Goldmont.decode_width(), 3);
        assert_eq!(BundleMicroArch::Tremont.decode_width(), 6);
    }

    #[test]
    fn test_microarch_branch_at_end_requirement() {
        assert!(BundleMicroArch::Bonnell.requires_branch_at_bundle_end());
        assert!(BundleMicroArch::Silvermont.requires_branch_at_bundle_end());
        assert!(!BundleMicroArch::Goldmont.requires_branch_at_bundle_end());
        assert!(!BundleMicroArch::OutOfOrder.requires_branch_at_bundle_end());
    }

    #[test]
    fn test_microarch_bundle_locking_support() {
        assert!(BundleMicroArch::Silvermont.supports_bundle_locking());
        assert!(BundleMicroArch::Goldmont.supports_bundle_locking());
        assert!(!BundleMicroArch::OutOfOrder.supports_bundle_locking());
    }

    #[test]
    fn test_config_override_bundle_size() {
        let config = BundleConfig {
            bundle_size_override: 32,
            ..BundleConfig::default()
        };
        let engine = X86InstrBundling::with_config(BundleMicroArch::Bonnell, true, config);
        assert_eq!(engine.bundle_size(), 32);
    }

    #[test]
    fn test_config_max_instrs_override() {
        let config = BundleConfig {
            max_instrs_per_bundle: 4,
            ..BundleConfig::default()
        };
        let engine = X86InstrBundling::with_config(BundleMicroArch::Bonnell, true, config);
        assert_eq!(engine.max_instrs_per_bundle(), 4);
    }

    #[test]
    fn test_large_instruction_bundling() {
        // An 8-byte instruction on Atom should occupy a whole bundle.
        let mut engine = make_atom_bundling();
        let results = engine.bundle_instructions(&[
            make_test_instr(1, "VERYLONG", 8),
            make_test_instr(2, "ADD", 2),
        ]);
        // The 8-byte instruction should be alone in its bundle.
        assert!(results.len() >= 2);
        assert_eq!(results[0].instructions[0].size, 8);
    }

    #[test]
    fn test_atom_rules_apply() {
        let instrs = vec![
            make_test_instr(1, "ADD", 2),
            make_test_instr(2, "MOV", 2),
            make_test_instr(3, "SUB", 2),
        ];
        let bundles = atom_rules::apply_atom_constraints(&instrs, true);
        assert!(bundles.len() >= 2);
        // Max 2 per bundle.
        for b in &bundles {
            let non_nop_count = b.instructions.iter().filter(|i| !i.is_nop).count();
            assert!(non_nop_count <= 2);
        }
    }

    #[test]
    fn test_silvermont_rules_apply() {
        let instrs = vec![make_test_instr(1, "ADD", 2), make_test_instr(2, "MOV", 2)];
        let bundles = silvermont_rules::apply_silvermont_constraints(&instrs, true);
        assert_eq!(bundles.len(), 1);
        assert_eq!(bundles[0].size, 8);
    }

    #[test]
    fn test_goldmont_rules_apply() {
        let instrs = vec![
            make_test_instr(1, "ADD", 3),
            make_test_instr(2, "MOV", 4),
            make_test_instr(3, "SUB", 2),
        ];
        let bundles = goldmont_rules::apply_goldmont_constraints(&instrs, true);
        assert!(!bundles.is_empty());
        assert_eq!(bundles[0].size, 16);
    }

    #[test]
    fn test_tremont_rules_apply() {
        let instrs = vec![
            make_test_instr(1, "ADD", 3),
            make_test_instr(2, "MOV", 4),
            make_test_instr(3, "SUB", 2),
        ];
        let bundles = tremont_rules::apply_tremont_constraints(&instrs, true);
        assert!(!bundles.is_empty());
        assert_eq!(bundles[0].size, 16);
    }

    #[test]
    fn test_bundle_result_equality() {
        assert_eq!(BundleResult::Added, BundleResult::Added);
        assert_ne!(BundleResult::Added, BundleResult::NewBundleNeeded);
    }

    #[test]
    fn test_boundary_crossing_info_display() {
        let info = diagnose_boundary_crossing(7, 3, 8);
        assert!(info.crosses_boundary);
        assert_eq!(info.bytes_in_current_bundle, 1);
        assert_eq!(info.bytes_in_next_bundle, 2);
    }

    #[test]
    fn test_empty_bundle() {
        let bundle = Bundle {
            id: 0,
            offset: 0,
            size: 8,
            instructions: vec![],
            used_bytes: 0,
            is_locked: false,
            ends_with_branch: false,
            alignment: 8,
            flags: BundleFlags::default(),
        };
        // Empty bundle should be valid on all microarchs.
        assert!(atom_rules::validate_atom_bundle(&bundle).is_ok());
    }

    #[test]
    fn test_lock_granularity() {
        let lock = BundleLock::bundle_lock();
        assert!(lock.locked);
        assert_eq!(lock.granularity, LockGranularity::Bundle);

        let ilock = BundleLock::instruction_lock();
        assert_eq!(ilock.granularity, LockGranularity::Instruction);

        let unlocked = BundleLock::unlocked();
        assert!(!unlocked.locked);
    }

    #[test]
    fn test_macro_fusion_cross_bundle() {
        let config = BundleConfig {
            allow_macro_fusion_cross_bundle: false,
            ..BundleConfig::default()
        };
        let mut engine = X86InstrBundling::with_config(BundleMicroArch::Goldmont, true, config);

        let mut fused1 = make_test_instr(1, "CMP", 3);
        fused1.is_macro_fused = true;
        fused1.macro_fuse_partner = Some(2);

        let mut fused2 = make_test_instr(2, "JE", 2);
        fused2.is_macro_fused = true;
        fused2.macro_fuse_partner = Some(1);

        engine.bundle_instructions(&[fused1, fused2]);
        // Should keep fusion pair together (if possible).
        assert!(engine.bundles.len() >= 1);
    }

    #[test]
    fn test_stats_after_bundling() {
        let mut engine = make_atom_bundling();
        engine.bundle_instructions(&[make_test_instr(1, "ADD", 2), make_test_instr(2, "MOV", 2)]);
        assert!(engine.stats.total_bundles >= 1);
        assert_eq!(engine.stats.total_instructions, 2);
    }

    #[test]
    fn test_adaptive_bundling() {
        let config = BundleConfig {
            adaptive_bundling: true,
            bundle_size_override: 0,
            max_instrs_per_bundle: 0,
            ..BundleConfig::default()
        };
        let mut engine = X86InstrBundling::with_config(BundleMicroArch::Tremont, true, config);
        engine.bundle_instructions(&[
            make_test_instr(1, "ADD", 3),
            make_test_instr(2, "MOV", 4),
            make_test_instr(3, "SUB", 2),
        ]);
        // Adaptive bundling should still produce valid bundles.
        for b in &engine.bundles {
            assert!(b.instructions.len() <= 3);
        }
    }

    #[test]
    fn test_bundle_stats_accumulation() {
        let mut engine = make_atom_bundling();
        engine.bundle_instructions(&[
            make_test_instr(1, "ADD", 2),
            make_test_instr(2, "MOV", 2),
            make_test_instr(3, "SUB", 2),
            make_test_instr(4, "XOR", 2),
        ]);
        assert_eq!(engine.stats.total_instructions, 4);
        assert!(engine.stats.total_bundles >= 2);
    }

    #[test]
    fn test_bundle_ends_with_call() {
        let mut engine = X86InstrBundling::new(BundleMicroArch::Goldmont, true);
        let call = make_test_call(1, 5);
        engine.bundle_instructions(&[call]);
        let bundle = &engine.bundles[0];
        assert!(bundle.ends_with_branch);
    }

    #[test]
    fn test_bundle_ends_with_return() {
        let mut engine = X86InstrBundling::new(BundleMicroArch::Goldmont, true);
        let ret = make_test_return(1, 1);
        engine.bundle_instructions(&[ret]);
        let bundle = &engine.bundles[0];
        assert!(bundle.ends_with_branch);
    }

    #[test]
    fn test_bundle_rejects_oversized_instr() {
        let mut engine = make_atom_bundling();
        let instrs = vec![make_test_instr(1, "BIG", 10)];
        let bundles = engine.bundle_instructions(&instrs);
        // A 10-byte instruction doesn't fit in an 8-byte bundle,
        // so it should be placed in its own (possibly oversized) bundle.
        assert!(!bundles.is_empty());
    }

    #[test]
    fn test_bundle_fusion_pair_handling() {
        let config = BundleConfig {
            allow_macro_fusion_cross_bundle: false,
            ..BundleConfig::default()
        };
        let mut engine = X86InstrBundling::with_config(BundleMicroArch::Goldmont, true, config);

        let mut c = make_test_instr(1, "CMP", 3);
        c.is_macro_fused = true;
        c.macro_fuse_partner = Some(2);

        let mut j = make_test_branch(2, "JE", 2, false);
        j.is_macro_fused = true;
        j.macro_fuse_partner = Some(1);

        engine.bundle_instructions(&[c, j]);
        assert!(!engine.bundles.is_empty());
    }

    #[test]
    fn test_boundary_crossing_exact_fit() {
        // Instruction at offset 0, size 8, bundle size 8 — exact fit.
        let crossing = diagnose_boundary_crossing(0, 8, 8);
        assert!(!crossing.crosses_boundary);
        assert_eq!(crossing.bytes_in_current_bundle, 8);
        assert_eq!(crossing.bytes_in_next_bundle, 0);
    }

    #[test]
    fn test_boundary_crossing_just_over() {
        // Instruction at offset 5, size 4, bundle size 8 — overflows by 1.
        let crossing = diagnose_boundary_crossing(5, 4, 8);
        assert!(crossing.crosses_boundary);
        assert_eq!(crossing.bytes_in_current_bundle, 3);
        assert_eq!(crossing.bytes_in_next_bundle, 1);
    }

    #[test]
    fn test_bundle_alignment_large_offset() {
        // Offset 100 with 16-byte bundles.
        assert_eq!(compute_bundle_alignment(100, 16), 12);
        assert_eq!(compute_bundle_alignment(112, 16), 0);
    }

    #[test]
    fn test_lock_bundles_silvermont() {
        let mut bundles = vec![
            Bundle {
                id: 0,
                offset: 0,
                size: 8,
                instructions: vec![make_test_instr(1, "ADD", 2)],
                used_bytes: 2,
                is_locked: false,
                ends_with_branch: false,
                alignment: 8,
                flags: BundleFlags::default(),
            },
            Bundle {
                id: 1,
                offset: 8,
                size: 8,
                instructions: vec![make_test_instr(2, "MOV", 2)],
                used_bytes: 2,
                is_locked: false,
                ends_with_branch: false,
                alignment: 8,
                flags: BundleFlags::default(),
            },
        ];
        lock_bundles(&mut bundles, BundleMicroArch::Silvermont);
        assert!(bundles.iter().all(|b| b.is_locked));
    }

    #[test]
    fn test_checkpoint_registers_count() {
        let regs_64 = checkpoint_registers_for_bundle(true);
        // 8 GPRs + 8 R8-R15 + 16 XMMs + RFLAGS = 33
        assert_eq!(regs_64.len(), 33);

        let regs_32 = checkpoint_registers_for_bundle(false);
        // 8 GPRs + 8 XMMs + RFLAGS = 17
        assert_eq!(regs_32.len(), 17);
    }

    #[test]
    fn test_instr_class_discriminants() {
        // Verify each variant exists and is distinct.
        let classes = [
            InstrClass::Alu,
            InstrClass::Memory,
            InstrClass::ConditionalBranch,
            InstrClass::UnconditionalBranch,
            InstrClass::IndirectBranch,
            InstrClass::Call,
            InstrClass::Return,
            InstrClass::Locked,
            InstrClass::Nop,
            InstrClass::Complex,
        ];
        for (i, a) in classes.iter().enumerate() {
            for (j, b) in classes.iter().enumerate() {
                if i == j {
                    assert_eq!(a, b);
                } else {
                    assert_ne!(a, b);
                }
            }
        }
    }

    #[test]
    fn test_microarch_max_prefix_bytes() {
        assert_eq!(BundleMicroArch::Bonnell.max_prefix_bytes(), 2);
        assert_eq!(BundleMicroArch::Silvermont.max_prefix_bytes(), 3);
        assert_eq!(BundleMicroArch::Goldmont.max_prefix_bytes(), 4);
    }

    #[test]
    fn test_optimize_bundle_packing_idempotent() {
        let mut bundles = vec![Bundle {
            id: 0,
            offset: 0,
            size: 16,
            instructions: vec![make_test_instr(1, "B", 2), make_test_instr(2, "A", 10)],
            used_bytes: 12,
            is_locked: false,
            ends_with_branch: false,
            alignment: 16,
            flags: BundleFlags::default(),
        }];
        optimize_bundle_packing(&mut bundles, BundleMicroArch::Goldmont);
        // Should still be valid.
        assert!(goldmont_rules::validate_goldmont_bundle(&bundles[0]).is_ok());
    }

    #[test]
    fn test_find_least_utilized_empty_list() {
        let bundles: Vec<Bundle> = vec![];
        assert!(find_least_utilized_bundle(&bundles).is_none());
    }

    #[test]
    fn test_bundle_utilization_full() {
        let bundle = Bundle {
            id: 0,
            offset: 0,
            size: 8,
            instructions: vec![],
            used_bytes: 8,
            is_locked: false,
            ends_with_branch: false,
            alignment: 8,
            flags: BundleFlags::default(),
        };
        assert_eq!(bundle_utilization(&bundle), 100.0);
    }

    #[test]
    fn test_bundle_utilization_empty() {
        let bundle = Bundle {
            id: 0,
            offset: 0,
            size: 8,
            instructions: vec![],
            used_bytes: 0,
            is_locked: false,
            ends_with_branch: false,
            alignment: 8,
            flags: BundleFlags::default(),
        };
        assert_eq!(bundle_utilization(&bundle), 0.0);
    }

    #[test]
    fn test_bundle_utilization_zero_size() {
        let bundle = Bundle {
            id: 0,
            offset: 0,
            size: 0,
            instructions: vec![],
            used_bytes: 0,
            is_locked: false,
            ends_with_branch: false,
            alignment: 1,
            flags: BundleFlags::default(),
        };
        assert_eq!(bundle_utilization(&bundle), 100.0);
    }

    #[test]
    fn test_enforce_branch_at_bundle_end_empty() {
        let mut bundle = Bundle {
            id: 0,
            offset: 0,
            size: 8,
            instructions: vec![],
            used_bytes: 0,
            is_locked: false,
            ends_with_branch: false,
            alignment: 8,
            flags: BundleFlags::default(),
        };
        enforce_branch_at_bundle_end(&mut bundle, BundleMicroArch::Bonnell);
        // Should not panic on empty bundle.
        assert!(bundle.instructions.is_empty());
    }

    #[test]
    fn test_enforce_branch_at_bundle_end_branch_already_last() {
        let mut bundle = Bundle {
            id: 0,
            offset: 0,
            size: 8,
            instructions: vec![
                make_test_instr(1, "ADD", 2),
                make_test_branch(2, "JE", 2, false),
            ],
            used_bytes: 4,
            is_locked: false,
            ends_with_branch: true,
            alignment: 8,
            flags: BundleFlags::default(),
        };
        enforce_branch_at_bundle_end(&mut bundle, BundleMicroArch::Bonnell);
        // Branch was already last; should remain last.
        assert!(bundle.instructions.last().unwrap().is_branch);
    }

    #[test]
    fn test_dump_bundles_output_format() {
        let mut engine = make_atom_bundling();
        engine.bundle_instructions(&[make_test_instr(1, "PUSH", 1), make_test_instr(2, "MOV", 3)]);
        let dump = engine.dump_bundles();
        assert!(dump.contains("Bonnell"));
        assert!(dump.contains("Bundle size:"));
        assert!(dump.contains("Total bundles:"));
        assert!(dump.contains("Average"));
    }

    #[test]
    fn test_make_test_instr_defaults() {
        let instr = make_test_instr(42, "TEST", 4);
        assert_eq!(instr.id, 42);
        assert_eq!(instr.mnemonic, "TEST");
        assert_eq!(instr.size, 4);
        assert!(!instr.is_branch);
        assert!(!instr.is_call);
        assert!(!instr.is_return);
        assert!(!instr.is_nop);
        assert_eq!(instr.classify(), InstrClass::Alu);
    }

    #[test]
    fn test_config_default_values() {
        let config = BundleConfig::default();
        assert_eq!(config.bundle_size_override, 0);
        assert_eq!(config.max_instrs_per_bundle, 0);
        assert!(config.enforce_alignment);
        assert!(config.pad_with_nops);
        assert!(!config.lock_bundles);
        assert!(!config.branch_at_bundle_end);
        assert!(!config.adaptive_bundling);
        assert!(!config.allow_macro_fusion_cross_bundle);
    }

    #[test]
    fn test_engine_stats_updated_after_flush() {
        let mut engine = make_goldmont_bundling();
        engine.bundle_instructions(&[make_test_instr(1, "ADD", 3), make_test_instr(2, "MOV", 4)]);
        engine.flush();
        // After flush, stats should be updated.
        assert!(engine.stats.total_bundles > 0);
        assert!(engine.stats.avg_instrs_per_bundle > 0.0);
        assert!(engine.stats.avg_utilization > 0.0);
    }

    #[test]
    fn test_locked_bundle_count() {
        let config = BundleConfig {
            lock_bundles: true,
            ..BundleConfig::default()
        };
        let mut engine = X86InstrBundling::with_config(BundleMicroArch::Tremont, true, config);
        engine.bundle_instructions(&[make_test_instr(1, "ADD", 3), make_test_instr(2, "MOV", 4)]);
        assert!(engine.stats.locked_bundles > 0);
    }

    #[test]
    fn test_bundle_instruction_ordering() {
        // Instructions should appear in order within a bundle.
        let mut engine = make_atom_bundling();
        engine.bundle_instructions(&[
            make_test_instr(1, "FIRST", 2),
            make_test_instr(2, "SECOND", 2),
        ]);
        let bundle = &engine.bundles[0];
        assert_eq!(bundle.instructions[0].mnemonic, "FIRST");
        assert_eq!(bundle.instructions[1].mnemonic, "SECOND");
    }

    #[test]
    fn test_all_microarch_bundle_sizes_consistent() {
        let microarchs = [
            BundleMicroArch::Bonnell,
            BundleMicroArch::Saltwell,
            BundleMicroArch::Silvermont,
            BundleMicroArch::Airmont,
            BundleMicroArch::Goldmont,
            BundleMicroArch::GoldmontPlus,
            BundleMicroArch::Tremont,
            BundleMicroArch::Gracemont,
            BundleMicroArch::Crestmont,
            BundleMicroArch::Skymont,
            BundleMicroArch::Darkmont,
            BundleMicroArch::OutOfOrder,
        ];
        for ma in &microarchs {
            let size = ma.bundle_size();
            assert!(
                size == 8 || size == 16,
                "{:?} has unexpected bundle size {}",
                ma,
                size
            );
        }
    }

    #[test]
    fn test_lock_granularity_variants() {
        assert_eq!(LockGranularity::Bundle, LockGranularity::Bundle);
        assert_ne!(LockGranularity::Bundle, LockGranularity::None);
        assert_ne!(LockGranularity::Instruction, LockGranularity::None);
        assert_ne!(LockGranularity::Pair, LockGranularity::None);
    }

    #[test]
    fn test_bundle_flags_default() {
        let flags = BundleFlags::default();
        assert!(!flags.function_entry);
        assert!(!flags.loop_header);
        assert!(!flags.branch_target);
        assert!(!flags.has_padding);
        assert!(!flags.fetch_aligned);
        assert!(!flags.is_full);
    }

    #[test]
    fn test_bundle_display_includes_flags() {
        let mut bundle = Bundle {
            id: 5,
            offset: 0x40,
            size: 8,
            instructions: vec![make_test_branch(1, "JMP", 2, true)],
            used_bytes: 2,
            is_locked: true,
            ends_with_branch: true,
            alignment: 8,
            flags: BundleFlags {
                loop_header: true,
                branch_target: true,
                has_padding: true,
                ..BundleFlags::default()
            },
        };
        let display = format!("{}", bundle);
        assert!(display.contains("Bundle #5"));
        assert!(display.contains("0x0040"));
        assert!(display.contains("LOCKED"));
        assert!(display.contains("ENDS_BR"));
        assert!(display.contains("PADDED"));
        assert!(display.contains("LOOP_HDR"));
    }

    #[test]
    fn test_instr_class_terminates_bundle_all_microarchs() {
        // Returns and indirect branches should always terminate.
        for ma in &[
            BundleMicroArch::Bonnell,
            BundleMicroArch::Silvermont,
            BundleMicroArch::Goldmont,
            BundleMicroArch::Tremont,
            BundleMicroArch::OutOfOrder,
        ] {
            assert!(InstrClass::Return.terminates_bundle(*ma));
            assert!(InstrClass::IndirectBranch.terminates_bundle(*ma));
            assert!(InstrClass::Call.terminates_bundle(*ma));
        }
    }

    #[test]
    fn test_try_add_to_full_bundle() {
        let mut engine = make_atom_bundling();
        engine.begin_bundle();
        // Fill the bundle with a 6-byte instruction.
        engine.commit_instruction(make_test_instr(1, "BIG", 6));
        // Try to add a 3-byte instruction — should need new bundle.
        let result = engine.try_add_instruction(&make_test_instr(2, "ADD", 3));
        assert_eq!(result, BundleResult::NewBundleNeeded);
    }

    #[test]
    fn test_begin_bundle_alignment() {
        let mut engine = make_atom_bundling();
        engine.code_offset = 5;
        engine.begin_bundle();
        // Should have aligned to 8.
        assert_eq!(engine.code_offset, 8);
    }

    #[test]
    fn test_silvermont_allows_larger_instructions() {
        let bundle = Bundle {
            id: 0,
            offset: 0,
            size: 8,
            instructions: vec![make_test_instr(1, "LONG", 8)],
            used_bytes: 8,
            is_locked: false,
            ends_with_branch: false,
            alignment: 8,
            flags: BundleFlags::default(),
        };
        assert!(silvermont_rules::validate_silvermont_bundle(&bundle).is_ok());
    }

    #[test]
    fn test_silvermont_rejects_oversize() {
        let bundle = Bundle {
            id: 0,
            offset: 0,
            size: 8,
            instructions: vec![make_test_instr(1, "TOOLONG", 12)],
            used_bytes: 12,
            is_locked: false,
            ends_with_branch: false,
            alignment: 8,
            flags: BundleFlags::default(),
        };
        assert!(silvermont_rules::validate_silvermont_bundle(&bundle).is_err());
    }

    #[test]
    fn test_boundary_crossing_info_nop_sled_zero_when_no_crossing() {
        let info = diagnose_boundary_crossing(0, 4, 8);
        assert!(!info.needs_nop_sled());
        assert_eq!(info.nop_sled_size(), 0);
    }

    #[test]
    fn test_bundle_microarch_comparison() {
        // Verify each microarch has different characteristics.
        assert_ne!(
            BundleMicroArch::Bonnell.bundle_size(),
            BundleMicroArch::Goldmont.bundle_size()
        );
        assert_ne!(
            BundleMicroArch::Silvermont.decode_width(),
            BundleMicroArch::Tremont.decode_width()
        );
    }
}