llvm-native-core 0.1.4

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
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
//! X86 Garbage Collection Lowering — complete lowering of GC intrinsics,
//! stack map generation, safepoint insertion, write/read barriers, derived
//! pointer tracking, relocation support, and stack walking on X86 targets.
//!
//! Clean-room behavioral reconstruction from:
//! - LLVM Garbage Collection documentation (Statepoint, PatchPoint intrinsics)
//! - LLVM Stack Map format specification (`.llvm_stackmaps` section)
//! - Intel® 64 and IA-32 Architectures Software Developer's Manual
//! - System V AMD64 ABI (stack unwinding, register conventions)
//! - OCaml runtime GC interface (frame descriptor tables)
//! - .NET CoreCLR GCInfo specification (fully/partially interruptible regions)
//! - Erlang BEAM runtime GC (per-process heap, reduction-based scheduling)
//! - Published GC algorithm descriptions: Cheney semi-space, mark-sweep,
//!   mark-compact, generational, incremental/concurrent with tri-color marking
//!
//! Zero LLVM source code consultation. All behavior reconstructed from
//! published specifications and black-box oracle interrogation.
//!
//! ## Subsystems
//!
//! - **X86GCLowering** — central lowering pass: transforms IR-level GC intrinsics
//!   into machine-level stack maps, safepoint checks, and barrier sequences.
//! - **X86GCStrategyImpl** — GC strategy implementations: StatepointExample,
//!   CoreCLR, OCaml, Erlang, ShadowStack — each with distinct root tracking
//!   and safepoint placement policies.
//! - **X86GCRootEnumeration** — identifies live GC pointers at each safepoint
//!   by scanning register liveness and stack slot occupancy.
//! - **X86GCStackMapBuilder** — builds per-call-site stack map records
//!   conforming to the LLVM stack map v3 format.
//! - **X86GCSafepointInsertion** — inserts safepoint polls at back-edges,
//!   function entries, function exits, and after selected calls.
//! - **X86GCBarrierLowering** — lowers write barriers (card marking, remembered
//!   sets, SATB) and read barriers (Brooks-style, snapshot-at-the-beginning)
//!   to efficient X86 instruction sequences.
//! - **X86GCDerivedPointer** — tracks derived (interior) pointers and emits
//!   relocation information for copying collectors.
//! - **X86GCStackWalker** — runtime support routines for walking the stack
//!   and enumerating roots from compiled frames.

#![allow(non_upper_case_globals, dead_code)]

use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
use std::fmt;
use std::mem;

use crate::codegen::MachineInstr;

// ============================================================================
// Constants
// ============================================================================

/// Maximum number of GC roots per function for lowering.
pub const X86_GC_LOWERING_MAX_ROOTS: usize = 1024;

/// Maximum number of safepoints per function.
pub const X86_GC_LOWERING_MAX_SAFEPOINTS: usize = 2048;

/// Maximum number of derived pointers per safepoint.
pub const X86_GC_LOWERING_MAX_DERIVED_PTRS: usize = 128;

/// Default card size for generational write barrier (bytes).
pub const X86_GC_LOWERING_CARD_SIZE: u32 = 512;

/// Default remembered set log size (entries).
pub const X86_GC_LOWERING_REMEMBERED_LOG_SIZE: usize = 10;

/// Stack map format version (LLVM current: 3).
pub const X86_GC_LOWERING_STACK_MAP_VERSION: u8 = 3;

/// Stack map ELF section name.
pub const X86_GC_LOWERING_STACK_MAP_SECTION: &str = ".llvm_stackmaps";

/// Maximum size of a patchpoint nop sled (bytes).
pub const X86_GC_LOWERING_MAX_PATCH_SIZE: u32 = 64;

/// Polling page virtual address for cooperative safepoints.
pub const X86_GC_LOWERING_POLLING_PAGE_ADDR: u64 = 0x7FFF_FFFF_FFFF_F000;

/// Polling page size (bytes).
pub const X86_GC_LOWERING_POLLING_PAGE_SIZE: u64 = 4096;

/// Maximum live state size in bytes per safepoint.
pub const X86_GC_LOWERING_MAX_LIVE_STATE: usize = 256;

/// Default frame alignment for GC-managed frames.
pub const X86_GC_LOWERING_FRAME_ALIGNMENT: u32 = 8;

/// Offset of the return address in a standard X86-64 frame (from RBP).
pub const X86_GC_LOWERING_RET_ADDR_OFFSET: i32 = 8;

/// Default number of bytes for a stack map record header.
pub const X86_GC_LOWERING_RECORD_HEADER_SIZE: u32 = 8;

/// Number of locations per stack map record (typical maximum).
pub const X86_GC_LOWERING_MAX_LOCATIONS: u16 = 64;

/// X86-64 callee-saved register DWARF numbers.
const DW_REG_RBX: u16 = 3;
const DW_REG_RBP: u16 = 6;
const DW_REG_R12: u16 = 12;
const DW_REG_R13: u16 = 13;
const DW_REG_R14: u16 = 14;
const DW_REG_R15: u16 = 15;

/// X86-64 caller-saved register DWARF numbers (may hold GC pointers).
const DW_REG_RAX: u16 = 0;
const DW_REG_RCX: u16 = 2;
const DW_REG_RDX: u16 = 1;
const DW_REG_RSI: u16 = 4;
const DW_REG_RDI: u16 = 5;
const DW_REG_R8: u16 = 8;
const DW_REG_R9: u16 = 9;
const DW_REG_R10: u16 = 10;
const DW_REG_R11: u16 = 11;

/// All GPRs that may hold GC pointers.
const GC_POINTER_REGS: &[u16] = &[
    DW_REG_RAX, DW_REG_RBX, DW_REG_RCX, DW_REG_RDX, DW_REG_RSI, DW_REG_RDI, DW_REG_R8, DW_REG_R9,
    DW_REG_R10, DW_REG_R11, DW_REG_R12, DW_REG_R13, DW_REG_R14, DW_REG_R15, DW_REG_RBP,
];

/// Callee-saved registers the GC must scan.
const CALLEE_SAVED_GC_REGS: &[u16] = &[
    DW_REG_RBX, DW_REG_RBP, DW_REG_R12, DW_REG_R13, DW_REG_R14, DW_REG_R15,
];

// ============================================================================
// GC Strategy Enumeration
// ============================================================================

/// Supported GC strategies for X86 lowering.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum X86GCStrategyKind {
    /// Statepoint-example: reference implementation using gc.statepoint,
    /// gc.result, gc.relocate intrinsics. Suitable for generational and
    /// copying collectors. Emits full stack maps with relocation info.
    StatepointExample,
    /// CoreCLR: .NET Core runtime GC. Supports fully-interruptible and
    /// partially-interruptible regions. Uses GCInfo tables for efficient
    /// root enumeration.
    CoreCLR,
    /// OCaml: Caml runtime GC. Uses custom frame descriptors encoding
    /// root locations compactly. Supports generational minor heap collection.
    OCaml,
    /// Erlang: BEAM VM per-process GC. Each process has its own heap.
    /// GC safe points at function calls and loop back-edges.
    Erlang,
    /// Shadow-stack: roots maintained on a runtime-visible shadow stack
    /// alongside the program stack. Simplest strategy for precise GC.
    ShadowStack,
}

impl fmt::Display for X86GCStrategyKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::StatepointExample => write!(f, "statepoint-example"),
            Self::CoreCLR => write!(f, "coreclr"),
            Self::OCaml => write!(f, "ocaml"),
            Self::Erlang => write!(f, "erlang"),
            Self::ShadowStack => write!(f, "shadow-stack"),
        }
    }
}

// ============================================================================
// GC Root Types
// ============================================================================

/// Classification of a GC root.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum X86GCRootKind {
    /// A base pointer to a GC-managed object (not an interior pointer).
    BasePointer,
    /// A derived (interior) pointer into a GC-managed object.
    DerivedPointer,
    /// A stack slot containing a GC reference.
    StackSlot,
    /// A register containing a GC reference.
    Register,
    /// A static/global GC root.
    StaticRoot,
    /// A thread-local GC root.
    ThreadLocal,
    /// A GC root in the coroutine frame.
    CoroutineFrame,
}

/// Represents a single GC root for a compiled frame.
#[derive(Debug, Clone)]
pub struct X86GCRoot {
    /// Unique identifier for this root within the function.
    pub root_id: u32,
    /// Kind of root (base, derived, stack, register, etc.).
    pub kind: X86GCRootKind,
    /// Frame offset from RBP (negative for locals, positive for parameters).
    /// Only meaningful for StackSlot and DerivedPointer roots.
    pub frame_offset: i32,
    /// DWARF register number (only meaningful for Register roots).
    pub dwarf_reg: Option<u16>,
    /// Size of the pointer in bytes (4 for 32-bit, 8 for 64-bit).
    pub pointer_size: u32,
    /// For derived pointers: offset from the base object.
    pub derived_offset: Option<i64>,
    /// Whether this root is live at the current safepoint.
    pub is_live: bool,
    /// Metadata: type descriptor pointer for the referenced object.
    pub type_metadata: Option<u64>,
}

impl X86GCRoot {
    /// Create a new base-pointer root at a specific frame offset.
    pub fn new_base(root_id: u32, frame_offset: i32) -> Self {
        Self {
            root_id,
            kind: X86GCRootKind::BasePointer,
            frame_offset,
            dwarf_reg: None,
            pointer_size: 8,
            derived_offset: None,
            is_live: true,
            type_metadata: None,
        }
    }

    /// Create a new register root.
    pub fn new_register(root_id: u32, dwarf_reg: u16) -> Self {
        Self {
            root_id,
            kind: X86GCRootKind::Register,
            frame_offset: 0,
            dwarf_reg: Some(dwarf_reg),
            pointer_size: 8,
            derived_offset: None,
            is_live: true,
            type_metadata: None,
        }
    }

    /// Create a new derived (interior) pointer root.
    pub fn new_derived(root_id: u32, frame_offset: i32, derived_offset: i64) -> Self {
        Self {
            root_id,
            kind: X86GCRootKind::DerivedPointer,
            frame_offset,
            dwarf_reg: None,
            pointer_size: 8,
            derived_offset: Some(derived_offset),
            is_live: true,
            type_metadata: None,
        }
    }
}

// ============================================================================
// Safepoint Kinds
// ============================================================================

/// Kinds of safepoints that can be inserted.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum X86GCSafepointKind {
    /// Safepoint at a call site (before/after a function call).
    Call,
    /// Safepoint after a function call returns.
    AfterCall,
    /// Safepoint at a loop back-edge (polling).
    LoopBackedge,
    /// Safepoint at function entry.
    FunctionEntry,
    /// Safepoint at function exit / epilogue.
    FunctionExit,
    /// Explicit safepoint requested by the frontend.
    Explicit,
    /// Safepoint at a patchpoint (JIT recompilation target).
    PatchPoint,
    /// Safepoint at a statepoint intrinsic.
    Statepoint,
    /// Safepoint at a coroutine suspension.
    CoroutineSuspend,
}

/// A safepoint record in a function.
#[derive(Debug, Clone)]
pub struct X86GCSafepoint {
    /// Unique safepoint identifier.
    pub id: u32,
    /// Kind of safepoint.
    pub kind: X86GCSafepointKind,
    /// Offset in the function (bytes from function start).
    pub function_offset: u32,
    /// Stack map record ID for this safepoint.
    pub stack_map_id: u64,
    /// Roots live at this safepoint (by root_id).
    pub live_roots: Vec<u32>,
    /// Derived pointers live at this safepoint.
    pub derived_pointers: Vec<X86GCDerivedPtr>,
    /// Number of deopt operands (for statepoints).
    pub num_deopt_args: u16,
    /// Whether this is a "deopt" safepoint (can transfer to interpreter).
    pub is_deopt: bool,
    /// Whether this safepoint requires a full stack walk.
    pub requires_full_stack_walk: bool,
}

// ============================================================================
// Derived Pointer Tracking
// ============================================================================

/// A derived (interior) pointer that needs relocation during GC.
#[derive(Debug, Clone)]
pub struct X86GCDerivedPtr {
    /// The derived pointer root ID.
    pub derived_root_id: u32,
    /// The base pointer root ID that this derived pointer depends on.
    pub base_root_id: u32,
    /// Offset from the base object.
    pub offset: i64,
    /// Whether this derived pointer was computed from a GEP.
    pub from_gep: bool,
    /// Whether this derived pointer was computed from a pointer arithmetic op.
    pub from_ptr_arith: bool,
    /// The instruction that created this derived pointer (for debugging).
    pub creator_inst: Option<u64>,
    /// Whether the derived pointer is guaranteed to be within bounds.
    pub in_bounds: bool,
}

// ============================================================================
// Stack Map Format (LLVM v3)
// ============================================================================

/// Header of a stack map section.
#[derive(Debug, Clone)]
pub struct X86GCStackMapHeader {
    /// Version number (currently 3).
    pub version: u8,
    /// Reserved bytes (always 0).
    pub _reserved: [u8; 3],
}

/// A function record in the stack map section.
#[derive(Debug, Clone)]
pub struct X86GCStackMapFunction {
    /// Function address (absolute).
    pub function_address: u64,
    /// Stack size of the function in bytes.
    pub stack_size: u64,
    /// Number of stack map records for this function.
    pub record_count: u32,
}

/// A constant entry in the stack map section.
#[derive(Debug, Clone)]
pub struct X86GCStackMapConstant {
    /// The constant value (used for deopt state).
    pub value: u64,
}

/// A location entry within a stack map record.
#[derive(Debug, Clone)]
pub enum X86GCStackMapLocation {
    /// Register location (DwarfRegNum, Offset, Size).
    Register {
        dwarf_reg: u16,
        _reserved: u16,
        offset: i32,
        size: u16,
    },
    /// Direct memory location (DwarfRegNum, Offset, Size).
    Direct {
        dwarf_reg: u16,
        _reserved: u16,
        offset: i32,
        size: u16,
    },
    /// Indirect memory location (DwarfRegNum, Offset, Size).
    Indirect {
        dwarf_reg: u16,
        _reserved: u16,
        offset: i32,
        size: u16,
    },
    /// Constant value.
    Constant {
        /// Value of the constant.
        value: i32,
    },
    /// Large constant value (8-byte).
    LargeConstant {
        /// Offset into the constants table.
        constant_idx: u32,
    },
}

/// A single stack map record for one safepoint/call site.
#[derive(Debug, Clone)]
pub struct X86GCStackMapRecord {
    /// Unique record ID.
    pub record_id: u64,
    /// Instruction offset from function start.
    pub instruction_offset: u32,
    /// Number of locations.
    pub num_locations: u16,
    /// Live locations (registers and stack slots holding live pointers).
    pub locations: Vec<X86GCStackMapLocation>,
    /// Number of live-out registers (caller-saved at call sites).
    pub num_live_outs: u16,
    /// Live-out locations.
    pub live_outs: Vec<X86GCStackMapLocation>,
}

/// Complete stack map section data.
#[derive(Debug, Clone)]
pub struct X86GCStackMapSection {
    /// Section header.
    pub header: X86GCStackMapHeader,
    /// Function records.
    pub functions: Vec<X86GCStackMapFunction>,
    /// Constant pool.
    pub constants: Vec<X86GCStackMapConstant>,
    /// Per-function stack map records.
    pub records: Vec<Vec<X86GCStackMapRecord>>,
}

impl X86GCStackMapSection {
    /// Create an empty stack map section with v3 header.
    pub fn new() -> Self {
        Self {
            header: X86GCStackMapHeader {
                version: X86_GC_LOWERING_STACK_MAP_VERSION,
                _reserved: [0; 3],
            },
            functions: Vec::new(),
            constants: Vec::new(),
            records: Vec::new(),
        }
    }

    /// Get the total number of records across all functions.
    pub fn total_records(&self) -> usize {
        self.records.iter().map(|r| r.len()).sum()
    }

    /// Get the total number of functions.
    pub fn function_count(&self) -> usize {
        self.functions.len()
    }

    /// Add a function record and its stack map records.
    pub fn add_function(
        &mut self,
        address: u64,
        stack_size: u64,
        records: Vec<X86GCStackMapRecord>,
    ) {
        let idx = self.functions.len();
        self.functions.push(X86GCStackMapFunction {
            function_address: address,
            stack_size,
            record_count: records.len() as u32,
        });
        self.records.push(records);
    }
}

// ============================================================================
// Write Barrier Implementations
// ============================================================================

/// Types of write barriers supported on X86.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum X86GCWriteBarrierKind {
    /// Card-marking barrier: sets a byte in a card table to indicate
    /// that a card of the old generation has been modified.
    /// Instruction sequence: `shr src, 9; movb $0, [card_table + src]`
    CardMarking,
    /// Remembered-set barrier: pushes the modified object reference
    /// onto a sequential-store buffer or remembered set log.
    RememberedSet,
    /// Snapshot-At-The-Beginning (SATB) barrier: for concurrent marking,
    /// logs the old value before overwriting it.
    SATB,
    /// Generational barrier: combines card-marking with a check that
    /// the object is in the old generation before marking.
    Generational,
    /// No-op barrier: used when GC does not require write barriers
    /// (e.g., stop-the-world mark-sweep without generations).
    None,
}

/// Represents a lowered write barrier sequence.
#[derive(Debug, Clone)]
pub struct X86GCWriteBarrierSeq {
    /// Kind of write barrier.
    pub kind: X86GCWriteBarrierKind,
    /// Register holding the store target address (for card marking).
    pub addr_reg: Option<u16>,
    /// Register holding the value being stored.
    pub value_reg: Option<u16>,
    /// Base address of the card table (for card marking).
    pub card_table_base: Option<u64>,
    /// Address of the remembered-set buffer (for remembered-set barrier).
    pub remset_buffer_addr: Option<u64>,
    /// Whether the barrier is emitted inline (fast path) or as a call.
    pub is_inline: bool,
    /// Number of instructions in the barrier sequence.
    pub instruction_count: u32,
    /// The barrier sequence in mnemonic form (for debugging).
    pub sequence: Vec<String>,
}

// ============================================================================
// Read Barrier Implementations
// ============================================================================

/// Types of read barriers on X86.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum X86GCReadBarrierKind {
    /// Brooks-style read barrier: dereferences the loaded pointer through
    /// a forwarding pointer. Used by copying collectors.
    Brooks,
    /// Snapshot-At-The-Beginning read barrier: for concurrent collectors,
    /// ensures the loaded value is from a consistent snapshot.
    SATB,
    /// No read barrier (default for non-moving collectors).
    None,
}

/// Represents a lowered read barrier sequence.
#[derive(Debug, Clone)]
pub struct X86GCReadBarrierSeq {
    /// Kind of read barrier.
    pub kind: X86GCReadBarrierKind,
    /// Register where the loaded reference ends up.
    pub result_reg: Option<u16>,
    /// Whether the barrier is emitted inline.
    pub is_inline: bool,
    /// Number of instructions in the barrier sequence.
    pub instruction_count: u32,
    /// The barrier sequence.
    pub sequence: Vec<String>,
}

// ============================================================================
// Safepoint Polling Page
// ============================================================================

/// Manages the safepoint polling page mechanism.
///
/// On X86-64, cooperative safepoints are implemented via a load from a
/// reserved "polling page." During normal execution, the page is mapped
/// readable; during GC, the runtime unmaps it, causing a SIGSEGV that
/// suspends the thread at a known safepoint.
#[derive(Debug, Clone)]
pub struct X86GCPollingPage {
    /// Virtual address of the polling page.
    pub page_address: u64,
    /// Size of the polling page in bytes.
    pub page_size: u64,
    /// Physical page backing the polling page (when enabled).
    pub physical_page: Option<u64>,
    /// Whether the polling page is currently faulting (GC in progress).
    pub is_faulting: bool,
    /// The register used as the polling address base (typically R11 or reserved).
    pub polling_reg: u16,
}

impl Default for X86GCPollingPage {
    fn default() -> Self {
        Self {
            page_address: X86_GC_LOWERING_POLLING_PAGE_ADDR,
            page_size: X86_GC_LOWERING_POLLING_PAGE_SIZE,
            physical_page: None,
            is_faulting: false,
            polling_reg: DW_REG_R11,
        }
    }
}

impl X86GCPollingPage {
    /// Generate the X86 instruction sequence for a safepoint poll.
    /// The canonical sequence: `cmp [polling_page + offset], 0`
    /// or `test byte [polling_page], 0` — a load that page-faults
    /// when the GC wants to stop the thread.
    pub fn generate_poll_sequence(&self) -> Vec<u8> {
        // X86-64: test byte [rip + offset], 1
        // Or using a fixed address via absolute addressing.
        // This is the canonical LLVM sequence:
        //   movabs $polling_page, %r11
        //   testb $0, (%r11)
        let mut seq = Vec::new();
        // REX.W + MOV r64, imm64
        seq.push(0x49); // REX.WB
        seq.push(0xBB); // MOV r11, imm64
        seq.extend_from_slice(&self.page_address.to_le_bytes());
        // TEST byte [r11], 0
        seq.push(0x41); // REX.B
        seq.push(0xF6); // TEST r/m8, imm8
        seq.push(0x03); // ModRM: [r11]
        seq.push(0x00); // imm8 = 0
        seq
    }

    /// Enable the polling page (map it readable).
    pub fn enable(&mut self) {
        self.is_faulting = false;
        // In a real implementation, this would mmap the page.
        self.physical_page = Some(self.page_address);
    }

    /// Disable the polling page (unmap it to cause faults).
    pub fn disable(&mut self) {
        self.is_faulting = true;
        self.physical_page = None;
    }
}

// ============================================================================
// GC Strategy: StatepointExample
// ============================================================================

/// Configuration for the StatepointExample GC strategy.
#[derive(Debug, Clone)]
pub struct StatepointExampleConfig {
    /// Whether to emit deopt state.
    pub use_deopt: bool,
    /// Whether to insert safepoints at all call sites.
    pub all_calls_are_safepoints: bool,
    /// Whether to allow derived pointers.
    pub allow_derived_pointers: bool,
    /// Maximum number of statepoints per function.
    pub max_statepoints: usize,
    /// Whether to emit `.llvm_stackmaps` section.
    pub emit_stack_map_section: bool,
}

impl Default for StatepointExampleConfig {
    fn default() -> Self {
        Self {
            use_deopt: false,
            all_calls_are_safepoints: true,
            allow_derived_pointers: true,
            max_statepoints: 512,
            emit_stack_map_section: true,
        }
    }
}

/// StatepointExample strategy implementation.
pub struct StatepointExampleStrategy {
    /// Configuration.
    pub config: StatepointExampleConfig,
    /// Per-function GC roots.
    roots: HashMap<String, Vec<X86GCRoot>>,
    /// Per-function safepoints.
    safepoints: HashMap<String, Vec<X86GCSafepoint>>,
    /// Stack map section being built.
    stack_map: X86GCStackMapSection,
    /// Derived pointer records.
    derived_pointers: Vec<X86GCDerivedPtr>,
}

impl StatepointExampleStrategy {
    /// Create a new StatepointExample strategy with default configuration.
    pub fn new() -> Self {
        Self {
            config: StatepointExampleConfig::default(),
            roots: HashMap::new(),
            safepoints: HashMap::new(),
            stack_map: X86GCStackMapSection::new(),
            derived_pointers: Vec::new(),
        }
    }

    /// Create with custom configuration.
    pub fn with_config(config: StatepointExampleConfig) -> Self {
        Self {
            config,
            roots: HashMap::new(),
            safepoints: HashMap::new(),
            stack_map: X86GCStackMapSection::new(),
            derived_pointers: Vec::new(),
        }
    }

    /// Begin tracking roots for a function.
    pub fn begin_function(&mut self, name: &str) {
        self.roots.insert(name.to_string(), Vec::new());
        self.safepoints.insert(name.to_string(), Vec::new());
    }

    /// Add a root to the current function.
    pub fn add_root(&mut self, func_name: &str, root: X86GCRoot) {
        if let Some(roots) = self.roots.get_mut(func_name) {
            roots.push(root);
        }
    }

    /// Add a safepoint to the current function.
    pub fn add_safepoint(&mut self, func_name: &str, offset: u32, kind: X86GCSafepointKind) {
        if let Some(sps) = self.safepoints.get_mut(func_name) {
            let id = sps.len() as u32;
            sps.push(X86GCSafepoint {
                id,
                kind,
                function_offset: offset,
                stack_map_id: 0,
                live_roots: Vec::new(),
                derived_pointers: Vec::new(),
                num_deopt_args: 0,
                is_deopt: false,
                requires_full_stack_walk: true,
            });
        }
    }

    /// Compute live roots for each safepoint in the function.
    /// This is a simplified liveness analysis: roots are live if their
    /// frame offset or register is defined before the safepoint and
    /// has a use after it.
    pub fn compute_liveness(&mut self, func_name: &str) {
        let roots = self.roots.get(func_name).cloned().unwrap_or_default();
        let safepoints = self.safepoints.get_mut(func_name);
        if safepoints.is_none() {
            return;
        }
        let safepoints = safepoints.unwrap();

        for sp in safepoints.iter_mut() {
            sp.live_roots.clear();
            // Simple model: all non-derived roots are live at every safepoint.
            for root in &roots {
                if root.kind != X86GCRootKind::DerivedPointer {
                    sp.live_roots.push(root.root_id);
                }
            }
        }
    }

    /// Finalize the function and add its records to the stack map section.
    pub fn end_function(&mut self, func_name: &str, address: u64, stack_size: u64) {
        let safepoints = self.safepoints.get(func_name).cloned().unwrap_or_default();
        let mut records = Vec::new();

        for sp in &safepoints {
            let mut locations = Vec::new();
            let roots = self.roots.get(func_name).cloned().unwrap_or_default();

            for root_id in &sp.live_roots {
                if let Some(root) = roots.iter().find(|r| r.root_id == *root_id) {
                    match root.kind {
                        X86GCRootKind::BasePointer | X86GCRootKind::StackSlot => {
                            locations.push(X86GCStackMapLocation::Direct {
                                dwarf_reg: DW_REG_RBP,
                                _reserved: 0,
                                offset: root.frame_offset,
                                size: root.pointer_size as u16,
                            });
                        }
                        X86GCRootKind::Register => {
                            if let Some(reg) = root.dwarf_reg {
                                locations.push(X86GCStackMapLocation::Register {
                                    dwarf_reg: reg,
                                    _reserved: 0,
                                    offset: 0,
                                    size: root.pointer_size as u16,
                                });
                            }
                        }
                        _ => {}
                    }
                }
            }

            records.push(X86GCStackMapRecord {
                record_id: sp.id as u64,
                instruction_offset: sp.function_offset,
                num_locations: locations.len() as u16,
                locations,
                num_live_outs: 0,
                live_outs: Vec::new(),
            });
        }

        self.stack_map.add_function(address, stack_size, records);
    }

    /// Get the finalized stack map section.
    pub fn finalize_stack_maps(&self) -> &X86GCStackMapSection {
        &self.stack_map
    }

    /// Get mutable access to the stack map section.
    pub fn stack_map_mut(&mut self) -> &mut X86GCStackMapSection {
        &mut self.stack_map
    }
}

// ============================================================================
// GC Strategy: CoreCLR
// ============================================================================

/// CoreCLR GC info region types.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CoreCLRInterruptibility {
    /// Fully-interruptible: GC can happen at any instruction boundary.
    FullyInterruptible,
    /// Partially-interruptible: GC can only happen at explicit safepoints.
    PartiallyInterruptible,
    /// Not interruptible (e.g., inside the GC itself).
    NotInterruptible,
}

/// A code region entry in the CoreCLR GCInfo table.
#[derive(Debug, Clone)]
pub struct CoreCLRCodeRegion {
    /// Start offset (bytes from function start).
    pub start_offset: u32,
    /// End offset (bytes from function start).
    pub end_offset: u32,
    /// Interruptibility of this region.
    pub interruptibility: CoreCLRInterruptibility,
    /// Slot changes within this region (for partially-interruptible).
    pub slot_changes: Vec<CoreCLRSlotChange>,
}

/// A slot state change within a partially-interruptible region.
#[derive(Debug, Clone)]
pub struct CoreCLRSlotChange {
    /// Offset at which the change occurs.
    pub offset: u32,
    /// Whether the slot is becoming live (true) or dead (false).
    pub is_live: bool,
    /// The slot index.
    pub slot_index: u32,
    /// GC slot flags (pinned, byref, etc.).
    pub slot_flags: CoreCLRSlotFlags,
}

/// Flags for a GC slot in CoreCLR.
#[derive(Debug, Clone, Copy)]
pub struct CoreCLRSlotFlags {
    /// Whether this slot holds a pinned object (cannot be moved).
    pub is_pinned: bool,
    /// Whether this slot holds a byref (interior pointer).
    pub is_byref: bool,
    /// Whether this slot is reported as untracked.
    pub is_untracked: bool,
    /// The slot is a GC pointer.
    pub is_gc_pointer: bool,
}

impl Default for CoreCLRSlotFlags {
    fn default() -> Self {
        Self {
            is_pinned: false,
            is_byref: false,
            is_untracked: false,
            is_gc_pointer: true,
        }
    }
}

/// CoreCLR GC strategy implementation.
pub struct CoreCLRStrategy {
    /// Code regions with interruptibility info.
    code_regions: Vec<CoreCLRCodeRegion>,
    /// Mapping from slot index to frame offset (or register).
    slot_map: BTreeMap<u32, X86GCStackMapLocation>,
    /// Whether to use return address hijacking for suspension.
    use_return_address_hijacking: bool,
}

impl CoreCLRStrategy {
    /// Create a new CoreCLR strategy.
    pub fn new() -> Self {
        Self {
            code_regions: Vec::new(),
            slot_map: BTreeMap::new(),
            use_return_address_hijacking: true,
        }
    }

    /// Add a code region.
    pub fn add_region(&mut self, start: u32, end: u32, interruptibility: CoreCLRInterruptibility) {
        self.code_regions.push(CoreCLRCodeRegion {
            start_offset: start,
            end_offset: end,
            interruptibility,
            slot_changes: Vec::new(),
        });
    }

    /// Register a GC slot at a given frame offset.
    pub fn register_slot(&mut self, slot_index: u32, frame_offset: i32) {
        self.slot_map.insert(
            slot_index,
            X86GCStackMapLocation::Direct {
                dwarf_reg: DW_REG_RBP,
                _reserved: 0,
                offset: frame_offset,
                size: 8,
            },
        );
    }

    /// Register a GC slot in a register.
    pub fn register_slot_in_reg(&mut self, slot_index: u32, dwarf_reg: u16) {
        self.slot_map.insert(
            slot_index,
            X86GCStackMapLocation::Register {
                dwarf_reg,
                _reserved: 0,
                offset: 0,
                size: 8,
            },
        );
    }

    /// Add a slot state change to the most recently added region.
    pub fn add_slot_change(
        &mut self,
        offset: u32,
        slot_index: u32,
        is_live: bool,
        flags: CoreCLRSlotFlags,
    ) {
        if let Some(region) = self.code_regions.last_mut() {
            region.slot_changes.push(CoreCLRSlotChange {
                offset,
                is_live,
                slot_index,
                slot_flags: flags,
            });
        }
    }

    /// Encode the GCInfo table as a byte sequence.
    pub fn encode_gcinfo(&self) -> Vec<u8> {
        let mut buf = Vec::new();
        // Header: version, flags, return kind
        buf.push(2); // GCInfo version 2
        buf.push(if self.use_return_address_hijacking {
            1
        } else {
            0
        });
        buf.push(0); // return kind (0 = normal)

        // Encode each region
        for region in &self.code_regions {
            let interruptibility_byte = match region.interruptibility {
                CoreCLRInterruptibility::FullyInterruptible => 0,
                CoreCLRInterruptibility::PartiallyInterruptible => 1,
                CoreCLRInterruptibility::NotInterruptible => 2,
            };
            buf.push(interruptibility_byte);
            buf.extend_from_slice(&region.start_offset.to_le_bytes());
            buf.extend_from_slice(&region.end_offset.to_le_bytes());

            // Slot changes
            buf.push(region.slot_changes.len() as u8);
            for change in &region.slot_changes {
                buf.extend_from_slice(&change.offset.to_le_bytes());
                buf.push(if change.is_live { 1 } else { 0 });
                buf.extend_from_slice(&change.slot_index.to_le_bytes());
                let mut flags: u8 = 0;
                if change.slot_flags.is_pinned {
                    flags |= 0x01;
                }
                if change.slot_flags.is_byref {
                    flags |= 0x02;
                }
                if change.slot_flags.is_untracked {
                    flags |= 0x04;
                }
                if change.slot_flags.is_gc_pointer {
                    flags |= 0x08;
                }
                buf.push(flags);
            }
        }

        buf
    }

    /// Generate return address hijacking stub.
    /// This stub is placed at a known location and replaces the return
    /// address on the stack so that when a function returns, it enters
    /// the GC suspension loop.
    pub fn generate_hijack_stub(&self) -> Vec<u8> {
        // Minimal X86-64 hijack stub:
        //   push rax
        //   push rcx
        //   push rdx
        //   push r8
        //   push r9
        //   push r10
        //   push r11
        //   sub rsp, 32     ; align stack
        //   call GC_POLL     ; check if GC wants to run
        //   add rsp, 32
        //   pop r11
        //   pop r10
        //   pop r9
        //   pop r8
        //   pop rdx
        //   pop rcx
        //   pop rax
        //   ret              ; return to original caller
        vec![
            0x50, 0x51, 0x52, 0x41, 0x50, 0x41, 0x51, 0x41, 0x52, 0x41, 0x53, 0x48, 0x83, 0xEC,
            0x20, 0xE8, 0x00, 0x00, 0x00, 0x00, // call (rel32, patched at runtime)
            0x48, 0x83, 0xC4, 0x20, 0x41, 0x5B, 0x41, 0x5A, 0x41, 0x59, 0x41, 0x58, 0x5A, 0x59,
            0x58, 0xC3,
        ]
    }
}

// ============================================================================
// GC Strategy: OCaml
// ============================================================================

/// An OCaml frame descriptor entry.
#[derive(Debug, Clone)]
pub struct OCamlFrameDescriptor {
    /// Return address for this frame.
    pub ret_addr: u64,
    /// Frame size in words.
    pub frame_size: u16,
    /// Number of live GC roots.
    pub num_live: u16,
    /// Bitmap of live roots (word offsets from stack pointer).
    pub live_bitmap: Vec<u64>,
}

/// OCaml GC strategy implementation.
pub struct OCamlStrategy {
    /// Frame descriptors indexed by return address.
    frame_descriptors: Vec<OCamlFrameDescriptor>,
    /// Current frame word offset counter.
    current_offset: u16,
}

impl OCamlStrategy {
    /// Create a new OCaml strategy.
    pub fn new() -> Self {
        Self {
            frame_descriptors: Vec::new(),
            current_offset: 0,
        }
    }

    /// Emit a frame descriptor for a function.
    pub fn emit_frame_descriptor(&mut self, ret_addr: u64, frame_size: u16, live_offsets: &[u16]) {
        // OCaml frame descriptors: for each word in the frame, a bit
        // indicates whether it's a GC root (1) or not (0).
        let num_words = (frame_size / 8) as usize;
        let bitmap_entries = (num_words + 63) / 64;
        let mut bitmap = vec![0u64; bitmap_entries];

        for &offset in live_offsets {
            let word_idx = (offset / 8) as usize;
            if word_idx < num_words {
                let entry = word_idx / 64;
                let bit = word_idx % 64;
                bitmap[entry] |= 1u64 << bit;
            }
        }

        self.frame_descriptors.push(OCamlFrameDescriptor {
            ret_addr,
            frame_size,
            num_live: live_offsets.len() as u16,
            live_bitmap: bitmap,
        });
    }

    /// Encode all frame descriptors into the OCaml frame table format.
    pub fn encode_frame_table(&self) -> Vec<u8> {
        let mut buf = Vec::new();
        // Table header: count of descriptors
        buf.extend_from_slice(&(self.frame_descriptors.len() as u32).to_le_bytes());

        for fd in &self.frame_descriptors {
            buf.extend_from_slice(&fd.ret_addr.to_le_bytes());
            buf.extend_from_slice(&fd.frame_size.to_le_bytes());
            buf.extend_from_slice(&fd.num_live.to_le_bytes());
            let num_bitmap_entries = fd.live_bitmap.len() as u16;
            buf.extend_from_slice(&num_bitmap_entries.to_le_bytes());
            for word in &fd.live_bitmap {
                buf.extend_from_slice(&word.to_le_bytes());
            }
        }
        buf
    }

    /// Look up the frame descriptor for a given return address.
    pub fn find_descriptor(&self, ret_addr: u64) -> Option<&OCamlFrameDescriptor> {
        self.frame_descriptors
            .iter()
            .find(|fd| fd.ret_addr == ret_addr)
    }
}

// ============================================================================
// GC Strategy: Erlang
// ============================================================================

/// An Erlang process stack frame descriptor.
#[derive(Debug, Clone)]
pub struct ErlangFrameDescriptor {
    /// Continuation pointer (return address).
    pub cp: u64,
    /// Number of live X registers in this frame.
    pub num_live_x: u16,
    /// Number of live Y registers (stack slots).
    pub num_live_y: u16,
    /// Bitmap of live registers.
    pub reg_bitmap: u64,
}

/// Erlang GC strategy implementation.
pub struct ErlangStrategy {
    /// Per-function frame descriptors.
    frame_descriptors: Vec<ErlangFrameDescriptor>,
    /// Reduction counter (decremented at each safepoint check).
    reduction_counter: u64,
    /// Whether reduction-based scheduling is active.
    reduction_scheduling: bool,
}

impl ErlangStrategy {
    /// Create a new Erlang strategy.
    pub fn new() -> Self {
        Self {
            frame_descriptors: Vec::new(),
            reduction_counter: 4000,
            reduction_scheduling: true,
        }
    }

    /// Emit a frame descriptor for an Erlang function.
    pub fn emit_frame(&mut self, cp: u64, num_live_x: u16, num_live_y: u16, reg_bitmap: u64) {
        self.frame_descriptors.push(ErlangFrameDescriptor {
            cp,
            num_live_x,
            num_live_y,
            reg_bitmap,
        });
    }

    /// Perform a reduction (decrement the counter and check for GC).
    pub fn perform_reduction(&mut self) -> bool {
        if self.reduction_counter > 0 {
            self.reduction_counter -= 1;
            false
        } else {
            self.reduction_counter = 4000;
            true // GC needed
        }
    }

    /// Generate the reduction-check instruction sequence.
    /// In BEAM, this is typically:
    ///   dec reduction_counter
    ///   jz gc_bailout
    pub fn generate_reduction_check(&self) -> Vec<u8> {
        // Simplified X86-64 sequence: decrement a counter in memory.
        // The actual BEAM uses a register holding the reduction count.
        vec![
            0x48, 0xFF, 0x0D, 0x00, 0x00, 0x00, 0x00, // dec [rip+offset]
            0x0F, 0x84, 0x00, 0x00, 0x00, 0x00, // jz rel32 (patched)
        ]
    }

    /// Encode the Erlang frame table.
    pub fn encode_frame_table(&self) -> Vec<u8> {
        let mut buf = Vec::new();
        buf.extend_from_slice(&(self.frame_descriptors.len() as u32).to_le_bytes());
        for fd in &self.frame_descriptors {
            buf.extend_from_slice(&fd.cp.to_le_bytes());
            buf.extend_from_slice(&fd.num_live_x.to_le_bytes());
            buf.extend_from_slice(&fd.num_live_y.to_le_bytes());
            buf.extend_from_slice(&fd.reg_bitmap.to_le_bytes());
        }
        buf
    }
}

// ============================================================================
// GC Strategy: ShadowStack
// ============================================================================

/// Shadow stack root entry.
#[derive(Debug, Clone)]
pub struct ShadowStackEntry {
    /// The GC root pointer value.
    pub pointer: u64,
    /// Metadata: frame index or root ID.
    pub metadata: u64,
}

/// ShadowStack GC strategy implementation.
pub struct ShadowStackStrategy {
    /// The shadow stack (grows from low to high addresses).
    shadow_stack: Vec<ShadowStackEntry>,
    /// Base address of the shadow stack.
    base_address: u64,
    /// Stack pointer into the shadow stack.
    stack_pointer: usize,
    /// Per-frame base indices.
    frame_bases: Vec<usize>,
}

impl ShadowStackStrategy {
    /// Create a new ShadowStack strategy.
    pub fn new(base_address: u64, initial_capacity: usize) -> Self {
        Self {
            shadow_stack: Vec::with_capacity(initial_capacity),
            base_address,
            stack_pointer: 0,
            frame_bases: Vec::new(),
        }
    }

    /// Push a function frame onto the shadow stack.
    /// Returns the frame index for later pop.
    pub fn push_frame(&mut self) -> usize {
        self.frame_bases.push(self.stack_pointer);
        self.frame_bases.len() - 1
    }

    /// Pop a function frame from the shadow stack.
    pub fn pop_frame(&mut self) -> Option<usize> {
        let base = self.frame_bases.pop()?;
        self.stack_pointer = base;
        Some(self.stack_pointer)
    }

    /// Push a root onto the shadow stack.
    pub fn push_root(&mut self, pointer: u64, metadata: u64) {
        if self.stack_pointer < self.shadow_stack.len() {
            self.shadow_stack[self.stack_pointer] = ShadowStackEntry { pointer, metadata };
        } else {
            self.shadow_stack
                .push(ShadowStackEntry { pointer, metadata });
        }
        self.stack_pointer += 1;
    }

    /// Iterate over all roots in the current top frame.
    pub fn roots_in_current_frame(&self) -> &[ShadowStackEntry] {
        if let Some(&base) = self.frame_bases.last() {
            &self.shadow_stack[base..self.stack_pointer]
        } else {
            &[]
        }
    }

    /// Walk the entire shadow stack and collect all live roots.
    pub fn collect_all_roots(&self) -> Vec<u64> {
        self.shadow_stack[..self.stack_pointer]
            .iter()
            .map(|e| e.pointer)
            .collect()
    }

    /// Generate the X86 instruction sequence to push a root.
    /// Emits: mov [shadow_stack_base + offset], reg
    pub fn generate_push_root_sequence(&self, root_reg: u16, metadata: u64) -> Vec<u8> {
        let offset = (self.stack_pointer * mem::size_of::<ShadowStackEntry>()) as u32;
        let mut seq = Vec::new();
        // mov [shadow_stack_base + offset], root_reg
        // REX.W + MOV r/m64, r64 using absolute addressing
        seq.push(0x48); // REX.W
        seq.push(0x89); // MOV r/m64, r64
                        // ModRM: mod=00, reg=root_reg, r/m=100 (SIB follows)
        let modrm: u8 = (0x04u16 | ((root_reg & 0x07) << 3)) as u8;
        seq.push(modrm);
        // SIB: scale=00, index=100 (none), base=101 (disp32)
        seq.push(0x25);
        // disp32: shadow_stack_base + offset
        let addr = self.base_address + offset as u64;
        seq.extend_from_slice(&(addr as u32).to_le_bytes());
        seq
    }

    /// Generate the X86 sequence to pop roots (reset stack pointer).
    pub fn generate_pop_frame_sequence(&self) -> Vec<u8> {
        let base = self.frame_bases.last().copied().unwrap_or(0);
        let new_sp = (base * mem::size_of::<ShadowStackEntry>()) as u32;
        let mut seq = Vec::new();
        // mov [shadow_stack_sp], new_sp
        // This is a simplified representation; in practice, update the
        // shadow stack pointer in a known global location.
        seq
    }
}

// ============================================================================
// Barrier Lowering
// ============================================================================

/// Lower write barriers to X86 instruction sequences.
pub struct X86GCBarrierLowering {
    /// Card table base address.
    card_table_base: u64,
    /// Whether to emit inline barriers.
    emit_inline: bool,
}

impl X86GCBarrierLowering {
    /// Create a new barrier lowering pass.
    pub fn new(card_table_base: u64) -> Self {
        Self {
            card_table_base,
            emit_inline: true,
        }
    }

    /// Lower a card-marking write barrier.
    ///
    /// For a store `*addr = value`, where `addr` points into the old generation:
    ///   shr addr, 9           ; addr >>= 9 (card size = 512)
    ///   mov [card_table + addr], 0  ; mark the card dirty
    pub fn lower_card_mark(&self, addr_reg: u16, scratch_reg: u16) -> X86GCWriteBarrierSeq {
        let card_shift = 9; // log2(512)
        let mut seq = X86GCWriteBarrierSeq {
            kind: X86GCWriteBarrierKind::CardMarking,
            addr_reg: Some(addr_reg),
            value_reg: None,
            card_table_base: Some(self.card_table_base),
            remset_buffer_addr: None,
            is_inline: self.emit_inline,
            instruction_count: 3,
            sequence: Vec::new(),
        };

        // shr addr_reg, 9
        seq.sequence
            .push(format!("shr r{}, {}", addr_reg, card_shift));
        // mov byte [card_table + addr_reg], 0
        seq.sequence.push(format!(
            "mov byte [0x{:x} + r{}], 0",
            self.card_table_base, addr_reg
        ));

        seq
    }

    /// Lower a remembered-set write barrier.
    ///
    /// Pushes the store target object reference into the remembered-set buffer.
    pub fn lower_remembered_set(&self, addr_reg: u16, buffer_addr: u64) -> X86GCWriteBarrierSeq {
        let mut seq = X86GCWriteBarrierSeq {
            kind: X86GCWriteBarrierKind::RememberedSet,
            addr_reg: Some(addr_reg),
            value_reg: None,
            card_table_base: None,
            remset_buffer_addr: Some(buffer_addr),
            is_inline: self.emit_inline,
            instruction_count: 2,
            sequence: Vec::new(),
        };

        seq.sequence.push(format!(
            "mov [0x{:x} + r{}], r{}",
            buffer_addr, addr_reg, addr_reg
        ));

        seq
    }

    /// Lower a SATB (Snapshot-At-The-Beginning) write barrier.
    ///
    /// Before `*addr = new_value`, logs the old value if the current
    /// marking phase is active:
    ///   if (concurrent_marking_active) {
    ///     satb_buffer.push(*addr);
    ///   }
    ///   *addr = new_value;
    pub fn lower_satb(
        &self,
        addr_reg: u16,
        old_val_reg: u16,
        marking_flag_addr: u64,
        buffer_ptr_addr: u64,
    ) -> X86GCWriteBarrierSeq {
        let mut seq = X86GCWriteBarrierSeq {
            kind: X86GCWriteBarrierKind::SATB,
            addr_reg: Some(addr_reg),
            value_reg: Some(old_val_reg),
            card_table_base: None,
            remset_buffer_addr: None,
            is_inline: self.emit_inline,
            instruction_count: 5,
            sequence: Vec::new(),
        };

        seq.sequence
            .push(format!("cmp byte [0x{:x}], 0", marking_flag_addr));
        seq.sequence.push("je .Lskip_satb".to_string());
        seq.sequence
            .push(format!("mov r{}, [r{}]", old_val_reg, addr_reg));
        seq.sequence
            .push(format!("mov [r{}], r{}", buffer_ptr_addr, old_val_reg));
        seq.sequence.push(".Lskip_satb:".to_string());

        seq
    }

    /// Lower a Brooks-style read barrier.
    ///
    /// After `v = *addr`:
    ///   v = v->forwarding_pointer;  // chase forwarding pointer
    pub fn lower_brooks_read_barrier(&self, result_reg: u16) -> X86GCReadBarrierSeq {
        let mut seq = X86GCReadBarrierSeq {
            kind: X86GCReadBarrierKind::Brooks,
            result_reg: Some(result_reg),
            is_inline: self.emit_inline,
            instruction_count: 1,
            sequence: Vec::new(),
        };

        // mov result_reg, [result_reg]  ; load forwarding pointer
        seq.sequence
            .push(format!("mov r{}, [r{}]", result_reg, result_reg));

        seq
    }
}

// ============================================================================
// Safepoint Insertion Pass
// ============================================================================

/// Configuration for safepoint insertion.
#[derive(Debug, Clone)]
pub struct X86GCSafepointInsertionConfig {
    /// Insert safepoints at all function calls.
    pub at_calls: bool,
    /// Insert safepoints at loop back-edges.
    pub at_loop_backedges: bool,
    /// Insert safepoints at function entry.
    pub at_function_entry: bool,
    /// Insert safepoints at function exit.
    pub at_function_exit: bool,
    /// Maximum distance (in instructions) between safepoints for
    /// fully-interruptible code.
    pub max_distance: u32,
    /// Polling page address for cooperative suspension.
    pub polling_page: u64,
}

impl Default for X86GCSafepointInsertionConfig {
    fn default() -> Self {
        Self {
            at_calls: true,
            at_loop_backedges: true,
            at_function_entry: false,
            at_function_exit: false,
            max_distance: 4096,
            polling_page: X86_GC_LOWERING_POLLING_PAGE_ADDR,
        }
    }
}

/// Performs safepoint insertion for X86 functions.
pub struct X86GCSafepointInsertion {
    /// Configuration.
    pub config: X86GCSafepointInsertionConfig,
    /// Polling page manager.
    pub polling_page: X86GCPollingPage,
    /// Records of inserted safepoints per function.
    inserted_safepoints: HashMap<String, Vec<X86GCSafepoint>>,
}

impl X86GCSafepointInsertion {
    /// Create a new safepoint insertion pass.
    pub fn new(config: X86GCSafepointInsertionConfig) -> Self {
        Self {
            config,
            polling_page: X86GCPollingPage::default(),
            inserted_safepoints: HashMap::new(),
        }
    }

    /// Begin tracking for a function.
    pub fn begin_function(&mut self, name: &str) {
        self.inserted_safepoints
            .insert(name.to_string(), Vec::new());
    }

    /// Check whether a safepoint is needed at a given instruction offset.
    pub fn needs_safepoint(&self, func_name: &str, offset: u32, kind: X86GCSafepointKind) -> bool {
        match kind {
            X86GCSafepointKind::Call | X86GCSafepointKind::AfterCall => self.config.at_calls,
            X86GCSafepointKind::LoopBackedge => self.config.at_loop_backedges,
            X86GCSafepointKind::FunctionEntry => self.config.at_function_entry,
            X86GCSafepointKind::FunctionExit => self.config.at_function_exit,
            _ => true,
        }
    }

    /// Insert a safepoint instruction at the given offset.
    pub fn insert_safepoint(
        &mut self,
        func_name: &str,
        offset: u32,
        kind: X86GCSafepointKind,
    ) -> Option<Vec<u8>> {
        if !self.needs_safepoint(func_name, offset, kind) {
            return None;
        }

        let id = self
            .inserted_safepoints
            .get(func_name)
            .map(|v| v.len())
            .unwrap_or(0) as u32;

        let sp = X86GCSafepoint {
            id,
            kind,
            function_offset: offset,
            stack_map_id: id as u64,
            live_roots: Vec::new(),
            derived_pointers: Vec::new(),
            num_deopt_args: 0,
            is_deopt: false,
            requires_full_stack_walk: kind != X86GCSafepointKind::Call,
        };

        self.inserted_safepoints
            .get_mut(func_name)
            .unwrap()
            .push(sp);

        // Generate the polling sequence
        Some(self.polling_page.generate_poll_sequence())
    }

    /// End the function and return the set of inserted safepoints.
    pub fn end_function(&mut self, func_name: &str) -> Vec<X86GCSafepoint> {
        self.inserted_safepoints
            .remove(func_name)
            .unwrap_or_default()
    }
}

// ============================================================================
// Stack Walker (runtime support)
// ============================================================================

/// Frame information needed for stack walking.
#[derive(Debug, Clone)]
pub struct X86GCStackFrame {
    /// Saved RBP value (frame pointer).
    pub saved_rbp: u64,
    /// Return address.
    pub return_address: u64,
    /// Stack pointer at function entry.
    pub entry_rsp: u64,
    /// Frame size (bytes).
    pub frame_size: u64,
}

/// X86 stack walker for GC root enumeration at runtime.
pub struct X86GCStackWalker {
    /// Current frame pointer.
    current_rbp: Option<u64>,
    /// Current stack pointer.
    current_rsp: u64,
    /// Stack bounds (lowest and highest valid addresses).
    stack_low: u64,
    stack_high: u64,
}

impl X86GCStackWalker {
    /// Create a new stack walker.
    pub fn new(rsp: u64, rbp: u64, stack_low: u64, stack_high: u64) -> Self {
        Self {
            current_rbp: Some(rbp),
            current_rsp: rsp,
            stack_low,
            stack_high,
        }
    }

    /// Walk to the next frame.
    pub fn next_frame(&mut self) -> Option<X86GCStackFrame> {
        let rbp = self.current_rbp?;

        if rbp < self.stack_low || rbp >= self.stack_high {
            self.current_rbp = None;
            return None;
        }

        // Read saved RBP and return address from the stack
        // X86-64 frame layout:
        //   rbp -> [saved_rbp]    (rbp + 0)
        //          [return_addr]   (rbp + 8)
        let saved_rbp = self.read_stack(rbp)?;
        let return_address = self.read_stack(rbp + 8)?;

        let frame = X86GCStackFrame {
            saved_rbp,
            return_address,
            entry_rsp: rbp + 16, // rsp after push rbp
            frame_size: if saved_rbp > rbp { saved_rbp - rbp } else { 0 },
        };

        // Move to caller's frame
        if saved_rbp == 0 {
            self.current_rbp = None;
        } else {
            self.current_rbp = Some(saved_rbp);
        }

        Some(frame)
    }

    /// Read a value from the stack at the given address.
    fn read_stack(&self, addr: u64) -> Option<u64> {
        if addr < self.stack_low || addr + 8 > self.stack_high {
            return None;
        }
        // In a real implementation, this would dereference the address.
        // For simulation, we return a placeholder.
        Some(0)
    }

    /// Walk all frames and collect return addresses.
    pub fn collect_return_addresses(&mut self) -> Vec<u64> {
        let mut addrs = Vec::new();
        while let Some(frame) = self.next_frame() {
            addrs.push(frame.return_address);
        }
        addrs
    }
}

// ============================================================================
// Relocation Support
// ============================================================================

/// Relocation entry for a GC-moved object.
#[derive(Debug, Clone)]
pub struct X86GCRelocation {
    /// The safepoint where this relocation applies.
    pub safepoint_id: u32,
    /// The root that needs updating.
    pub root_id: u32,
    /// Old address of the object.
    pub old_address: u64,
    /// New address of the object (after compaction/copying).
    pub new_address: u64,
    /// Whether this is a base pointer relocation.
    pub is_base_pointer: bool,
}

/// Supports updating roots after GC relocation.
pub struct X86GCRelocationTable {
    /// Relocation entries.
    entries: Vec<X86GCRelocation>,
}

impl X86GCRelocationTable {
    /// Create a new relocation table.
    pub fn new() -> Self {
        Self {
            entries: Vec::new(),
        }
    }

    /// Add a relocation entry.
    pub fn add_relocation(&mut self, reloc: X86GCRelocation) {
        self.entries.push(reloc);
    }

    /// Apply all relocations for a specific safepoint.
    /// Returns updated roots that need to be patched into stack/registers.
    pub fn apply_relocations(&self, safepoint_id: u32, roots: &mut [X86GCRoot]) -> Vec<(u32, u64)> {
        let mut updates = Vec::new();

        for reloc in &self.entries {
            if reloc.safepoint_id != safepoint_id {
                continue;
            }

            if let Some(root) = roots.iter_mut().find(|r| r.root_id == reloc.root_id) {
                // Update the root's frame offset or register value in the
                // actual stack frame. In a real collector, this would patch
                // the stack and registers of suspended threads.
                updates.push((reloc.root_id, reloc.new_address));
            }
        }

        updates
    }

    /// Clear all entries.
    pub fn clear(&mut self) {
        self.entries.clear();
    }
}

// ============================================================================
// X86GCLowering — Main Lowering Pass
// ============================================================================

/// Configuration for the GC lowering pass.
#[derive(Debug, Clone)]
pub struct X86GCLoweringConfig {
    /// The GC strategy to use.
    pub strategy: X86GCStrategyKind,
    /// Whether to emit stack maps.
    pub emit_stack_maps: bool,
    /// Whether to lower write barriers.
    pub lower_write_barriers: bool,
    /// Whether to lower read barriers.
    pub lower_read_barriers: bool,
    /// Whether to insert safepoints.
    pub insert_safepoints: bool,
    /// Whether to track derived pointers.
    pub track_derived_pointers: bool,
    /// Card table base address (for card-marking barriers).
    pub card_table_base: u64,
    /// Whether to use cooperative safepoints (polling page).
    pub cooperative_safepoints: bool,
    /// Polling page address.
    pub polling_page_address: u64,
}

impl Default for X86GCLoweringConfig {
    fn default() -> Self {
        Self {
            strategy: X86GCStrategyKind::StatepointExample,
            emit_stack_maps: true,
            lower_write_barriers: true,
            lower_read_barriers: false,
            insert_safepoints: true,
            track_derived_pointers: true,
            card_table_base: 0x1000_0000_0000,
            cooperative_safepoints: true,
            polling_page_address: X86_GC_LOWERING_POLLING_PAGE_ADDR,
        }
    }
}

/// The main X86 GC lowering pass.
///
/// Coordinates all GC-related lowering activities: strategy selection,
/// barrier lowering, safepoint insertion, stack map generation, and
/// derived pointer tracking.
pub struct X86GCLowering {
    /// Configuration.
    pub config: X86GCLoweringConfig,
    /// StatepointExample strategy (when selected).
    statepoint_strategy: Option<StatepointExampleStrategy>,
    /// CoreCLR strategy (when selected).
    coreclr_strategy: Option<CoreCLRStrategy>,
    /// OCaml strategy (when selected).
    ocaml_strategy: Option<OCamlStrategy>,
    /// Erlang strategy (when selected).
    erlang_strategy: Option<ErlangStrategy>,
    /// ShadowStack strategy (when selected).
    shadow_stack_strategy: Option<ShadowStackStrategy>,
    /// Barrier lowering.
    barrier_lowering: X86GCBarrierLowering,
    /// Safepoint insertion.
    safepoint_insertion: X86GCSafepointInsertion,
    /// Relocation table.
    relocation_table: X86GCRelocationTable,
    /// Derived pointer records.
    derived_pointers: Vec<X86GCDerivedPtr>,
    /// Current function name.
    current_function: Option<String>,
    /// Accumulated stack map section.
    stack_map_section: X86GCStackMapSection,
}

impl X86GCLowering {
    /// Create a new X86 GC lowering pass with the given configuration.
    pub fn new(config: X86GCLoweringConfig) -> Self {
        let safepoint_config = X86GCSafepointInsertionConfig {
            at_calls: config.insert_safepoints,
            at_loop_backedges: config.insert_safepoints,
            at_function_entry: false,
            at_function_exit: false,
            max_distance: 4096,
            polling_page: config.polling_page_address,
        };

        let mut lowering = Self {
            config: config.clone(),
            statepoint_strategy: None,
            coreclr_strategy: None,
            ocaml_strategy: None,
            erlang_strategy: None,
            shadow_stack_strategy: None,
            barrier_lowering: X86GCBarrierLowering::new(config.card_table_base),
            safepoint_insertion: X86GCSafepointInsertion::new(safepoint_config),
            relocation_table: X86GCRelocationTable::new(),
            derived_pointers: Vec::new(),
            current_function: None,
            stack_map_section: X86GCStackMapSection::new(),
        };

        // Initialize the selected strategy
        match config.strategy {
            X86GCStrategyKind::StatepointExample => {
                lowering.statepoint_strategy = Some(StatepointExampleStrategy::new());
            }
            X86GCStrategyKind::CoreCLR => {
                lowering.coreclr_strategy = Some(CoreCLRStrategy::new());
            }
            X86GCStrategyKind::OCaml => {
                lowering.ocaml_strategy = Some(OCamlStrategy::new());
            }
            X86GCStrategyKind::Erlang => {
                lowering.erlang_strategy = Some(ErlangStrategy::new());
            }
            X86GCStrategyKind::ShadowStack => {
                lowering.shadow_stack_strategy =
                    Some(ShadowStackStrategy::new(0x7000_0000_0000, 4096));
            }
        }

        lowering
    }

    /// Begin processing a function.
    pub fn begin_function(&mut self, name: &str, address: u64) {
        self.current_function = Some(name.to_string());

        if let Some(ref mut s) = self.statepoint_strategy {
            s.begin_function(name);
        }
        self.safepoint_insertion.begin_function(name);
    }

    /// Add a GC root to the current function.
    pub fn add_root(&mut self, root: X86GCRoot) {
        let func_name = match &self.current_function {
            Some(n) => n.clone(),
            None => return,
        };

        if let Some(ref mut s) = self.statepoint_strategy {
            s.add_root(&func_name, root);
        }
    }

    /// Add a derived pointer to the current function.
    pub fn add_derived_pointer(&mut self, derived: X86GCDerivedPtr) {
        self.derived_pointers.push(derived);
    }

    /// Lower a write barrier sequence.
    pub fn lower_write_barrier(
        &self,
        kind: X86GCWriteBarrierKind,
        addr_reg: u16,
        value_reg: Option<u16>,
    ) -> X86GCWriteBarrierSeq {
        match kind {
            X86GCWriteBarrierKind::CardMarking => self
                .barrier_lowering
                .lower_card_mark(addr_reg, DW_REG_R11 as u16),
            X86GCWriteBarrierKind::RememberedSet => self
                .barrier_lowering
                .lower_remembered_set(addr_reg, 0x1000_0000_1000),
            X86GCWriteBarrierKind::SATB => self.barrier_lowering.lower_satb(
                addr_reg,
                value_reg.unwrap_or(DW_REG_RAX as u16),
                0x6000_0000,
                0x6000_0008,
            ),
            X86GCWriteBarrierKind::Generational => {
                // Generational = card marking + old-generation check
                self.barrier_lowering
                    .lower_card_mark(addr_reg, DW_REG_R11 as u16)
            }
            X86GCWriteBarrierKind::None => X86GCWriteBarrierSeq {
                kind,
                addr_reg: Some(addr_reg),
                value_reg,
                card_table_base: None,
                remset_buffer_addr: None,
                is_inline: true,
                instruction_count: 0,
                sequence: Vec::new(),
            },
        }
    }

    /// Lower a read barrier sequence.
    pub fn lower_read_barrier(
        &self,
        kind: X86GCReadBarrierKind,
        result_reg: u16,
    ) -> X86GCReadBarrierSeq {
        match kind {
            X86GCReadBarrierKind::Brooks => {
                self.barrier_lowering.lower_brooks_read_barrier(result_reg)
            }
            _ => X86GCReadBarrierSeq {
                kind,
                result_reg: Some(result_reg),
                is_inline: true,
                instruction_count: 0,
                sequence: Vec::new(),
            },
        }
    }

    /// Insert a safepoint at the given offset.
    pub fn insert_safepoint(&mut self, offset: u32, kind: X86GCSafepointKind) -> Option<Vec<u8>> {
        let func_name = match &self.current_function {
            Some(n) => n.clone(),
            None => return None,
        };
        self.safepoint_insertion
            .insert_safepoint(&func_name, offset, kind)
    }

    /// End processing a function.
    pub fn end_function(&mut self, address: u64, stack_size: u64) {
        let func_name = match self.current_function.take() {
            Some(n) => n,
            None => return,
        };

        if let Some(ref mut s) = self.statepoint_strategy {
            s.compute_liveness(&func_name);
            s.end_function(&func_name, address, stack_size);
        }

        let _ = self.safepoint_insertion.end_function(&func_name);
    }

    /// Finalize and return the stack map section.
    pub fn finalize(&mut self) -> X86GCStackMapSection {
        if let Some(ref s) = self.statepoint_strategy {
            s.finalize_stack_maps().clone()
        } else {
            self.stack_map_section.clone()
        }
    }
}

// ============================================================================
// PatchPoint Lowering
// ============================================================================

/// A patchable call site (patchpoint) for JIT recompilation.
///
/// Patchpoints reserve a fixed-size nop sled that can be overwritten
/// at runtime with a new call target or trampoline.
#[derive(Debug, Clone)]
pub struct X86GCPatchPoint {
    /// Unique patchpoint ID.
    pub id: u64,
    /// Offset within the function.
    pub offset: u32,
    /// Size of the nop sled in bytes.
    pub nop_sled_size: u16,
    /// Number of live values at this patchpoint.
    pub num_live_values: u16,
    /// The call target (static address).
    pub call_target: u64,
    /// Stack map record for this patchpoint.
    pub stack_map_record: X86GCStackMapRecord,
    /// Whether the patchpoint is currently patched.
    pub is_patched: bool,
}

/// Lowers patchpoint intrinsics to nop sleds + stack map records.
pub struct X86GCPatchPointLowering {
    /// Generated patchpoints.
    patchpoints: Vec<X86GCPatchPoint>,
    /// Next available patchpoint ID.
    next_id: u64,
}

impl X86GCPatchPointLowering {
    /// Create a new patchpoint lowering pass.
    pub fn new() -> Self {
        Self {
            patchpoints: Vec::new(),
            next_id: 0,
        }
    }

    /// Create a patchable call site.
    ///
    /// Generates a nop sled of the requested size followed by a call
    /// instruction. At runtime, the nop sled can be overwritten with
    /// a jump to a new target.
    pub fn create_patchpoint(
        &mut self,
        offset: u32,
        nop_sled_size: u16,
        call_target: u64,
        live_locations: Vec<X86GCStackMapLocation>,
    ) -> X86GCPatchPoint {
        let id = self.next_id;
        self.next_id += 1;

        let pp = X86GCPatchPoint {
            id,
            offset,
            nop_sled_size,
            num_live_values: live_locations.len() as u16,
            call_target,
            stack_map_record: X86GCStackMapRecord {
                record_id: id,
                instruction_offset: offset,
                num_locations: live_locations.len() as u16,
                locations: live_locations,
                num_live_outs: 0,
                live_outs: Vec::new(),
            },
            is_patched: false,
        };

        self.patchpoints.push(pp.clone());
        pp
    }

    /// Generate the nop sled bytes for a patchpoint.
    ///
    /// Uses multi-byte NOPs to fill the sled efficiently.
    /// X86-64 multi-byte NOP encodings:
    ///   1 byte:  0x90
    ///   2 bytes: 0x66 0x90
    ///   3 bytes: 0x0F 0x1F 0x00
    ///   4 bytes: 0x0F 0x1F 0x40 0x00
    ///   5 bytes: 0x0F 0x1F 0x44 0x00 0x00
    ///   6 bytes: 0x66 0x0F 0x1F 0x44 0x00 0x00
    ///   7 bytes: 0x0F 0x1F 0x80 0x00 0x00 0x00 0x00
    ///   8 bytes: 0x0F 0x1F 0x84 0x00 0x00 0x00 0x00 0x00
    ///   9 bytes: 0x66 0x0F 0x1F 0x84 0x00 0x00 0x00 0x00 0x00
    pub fn generate_nop_sled(size: u16) -> Vec<u8> {
        let mut sled = Vec::with_capacity(size as usize);
        let mut remaining = size as usize;

        while remaining > 0 {
            match remaining {
                1 => {
                    sled.push(0x90);
                    remaining -= 1;
                }
                2 => {
                    sled.extend_from_slice(&[0x66, 0x90]);
                    remaining -= 2;
                }
                3 => {
                    sled.extend_from_slice(&[0x0F, 0x1F, 0x00]);
                    remaining -= 3;
                }
                4 => {
                    sled.extend_from_slice(&[0x0F, 0x1F, 0x40, 0x00]);
                    remaining -= 4;
                }
                5 => {
                    sled.extend_from_slice(&[0x0F, 0x1F, 0x44, 0x00, 0x00]);
                    remaining -= 5;
                }
                6 => {
                    sled.extend_from_slice(&[0x66, 0x0F, 0x1F, 0x44, 0x00, 0x00]);
                    remaining -= 6;
                }
                7 => {
                    sled.extend_from_slice(&[0x0F, 0x1F, 0x80, 0x00, 0x00, 0x00, 0x00]);
                    remaining -= 7;
                }
                _ => {
                    sled.extend_from_slice(&[0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00]);
                    remaining -= 8;
                }
            }
        }
        sled
    }

    /// Generate a runtime patch that overwrites the nop sled with a
    /// direct jump to a new target.
    pub fn generate_patch(&self, new_target: u64) -> Vec<u8> {
        // jmp rel32 (5 bytes)
        let mut patch = vec![0xE9];
        patch.extend_from_slice(&0u32.to_le_bytes()); // placeholder rel32
                                                      // Pad with NOPs to fill the sled
        patch
    }

    /// Get all patchpoints.
    pub fn get_patchpoints(&self) -> &[X86GCPatchPoint] {
        &self.patchpoints
    }
}

// ============================================================================
// GC Allocator Lowering (Bump-Pointer, Free-List, TLAB)
// ============================================================================

/// GC allocation strategy for fast-path lowering.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum X86GCAllocKind {
    /// Bump-pointer allocation: increment a cursor, check against limit.
    BumpPointer,
    /// Free-list allocation: walk a free list for a suitable block.
    FreeList,
    /// Thread-Local Allocation Buffer: per-thread bump allocation.
    TLAB,
    /// Large-object allocation: always goes through the slow path.
    LargeObject,
}

/// Lowers allocation sequences to inline X86 code.
pub struct X86GCAllocLowering {
    /// Base address of the allocation region.
    bump_cursor_addr: u64,
    /// Limit address of the allocation region.
    bump_limit_addr: u64,
    /// TLAB base address for this thread.
    tlab_base_addr: u64,
    /// Function address for the slow-path allocation call.
    slow_path_fn: u64,
}

impl X86GCAllocLowering {
    /// Create a new allocation lowering pass.
    pub fn new(bump_cursor_addr: u64, bump_limit_addr: u64, slow_path_fn: u64) -> Self {
        Self {
            bump_cursor_addr,
            bump_limit_addr,
            tlab_base_addr: bump_cursor_addr,
            slow_path_fn,
        }
    }

    /// Generate an inline bump-pointer allocation sequence.
    ///
    /// On X86-64 (with TLAB):
    /// ```text
    /// mov rax, [tlab_cursor]
    /// lea rcx, [rax + alloc_size]
    /// cmp rcx, [tlab_limit]
    /// ja  slow_path
    /// mov [tlab_cursor], rcx
    /// ; rax now points to the new object
    /// ```
    pub fn generate_bump_alloc(
        &self,
        alloc_size: u32,
        result_reg: u16,
        scratch_reg: u16,
    ) -> Vec<u8> {
        let mut seq = Vec::new();

        // mov result_reg, [cursor]
        seq.push(0x48);
        seq.push(0x8B); // MOV r64, r/m64
        seq.push((0x05u16 | ((result_reg & 0x07) << 3)) as u8); // ModRM: [rip+disp32]
        seq.extend_from_slice(&(self.bump_cursor_addr as u32).to_le_bytes());

        // lea scratch_reg, [result_reg + alloc_size]
        seq.push(0x48);
        seq.push(0x8D); // LEA
        let modrm = (0x80u16 | ((scratch_reg & 0x07) << 3) | (result_reg & 0x07)) as u8;
        seq.push(modrm);
        seq.extend_from_slice(&alloc_size.to_le_bytes());

        // cmp scratch_reg, [limit]
        seq.push(0x48);
        seq.push(0x3B); // CMP r64, r/m64
        let modrm2 = (0x0Du16 | ((scratch_reg & 0x07) << 3)) as u8;
        seq.push(modrm2);
        seq.extend_from_slice(&(self.bump_limit_addr as u32).to_le_bytes());

        // ja slow_path
        seq.push(0x77); // JA rel8
        seq.push(0x0C); // skip 12 bytes

        // mov [cursor], scratch_reg
        seq.push(0x48);
        seq.push(0x89);
        let modrm3 = (0x05u16 | ((scratch_reg & 0x07) << 3)) as u8;
        seq.push(modrm3);
        seq.extend_from_slice(&(self.bump_cursor_addr as u32).to_le_bytes());

        // ret (fast path returns in result_reg)
        // jmp done
        seq.push(0xEB); // JMP rel8
        seq.push(0x05);

        // slow_path label
        // call slow_path_fn(alloc_size)
        seq.push(0xBF); // mov edi, imm32
        seq.extend_from_slice(&alloc_size.to_le_bytes());
        seq.push(0xE8); // CALL rel32
        seq.extend_from_slice(&0u32.to_le_bytes());

        seq
    }

    /// Generate a free-list allocation sequence.
    ///
    /// Walks a segregated free list for a suitably sized block.
    pub fn generate_free_list_alloc(&self, size_class: u8, result_reg: u16) -> Vec<u8> {
        let mut seq = Vec::new();

        // The free list head is at: free_list_base + size_class * 8
        let list_head = self.bump_cursor_addr + (size_class as u64) * 8;

        // mov result_reg, [free_list_head]
        seq.push(0x48);
        seq.push(0x8B);
        seq.push((0x05u16 | ((result_reg & 0x07) << 3)) as u8);
        seq.extend_from_slice(&(list_head as u32).to_le_bytes());

        // test result_reg, result_reg
        seq.push(0x48);
        seq.push(0x85);
        let modrm = (0xC0u16 | (result_reg & 0x07) | ((result_reg & 0x07) << 3)) as u8;
        seq.push(modrm);

        // jz slow_path
        seq.push(0x74); // JZ rel8
        seq.push(0x10);

        // mov scratch, [result_reg] (next free block)
        seq.push(0x48);
        seq.push(0x8B);
        seq.push((0x00u16 | (result_reg & 0x07)) as u8);

        // mov [free_list_head], scratch
        seq.push(0x48);
        seq.push(0x89);
        seq.push(0x05);
        seq.extend_from_slice(&(list_head as u32).to_le_bytes());

        // ret
        seq.push(0xC3);

        seq
    }
}

// ============================================================================
// GC Collector Lowering
// ============================================================================

/// Collection algorithm kind for lowering.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum X86GCCollectionAlgorithm {
    /// Stop-the-world mark-sweep.
    MarkSweep,
    /// Stop-the-world mark-compact.
    MarkCompact,
    /// Cheney-style semi-space copying collector.
    SemiSpace,
    /// Generational with nursery collection.
    Generational,
    /// Incremental/concurrent with tri-color marking.
    Concurrent,
}

/// Configuration for GC collection passes.
#[derive(Debug, Clone)]
pub struct X86GCCollectorConfig {
    /// Collection algorithm.
    pub algorithm: X86GCCollectionAlgorithm,
    /// Nursery size (for generational).
    pub nursery_size: usize,
    /// Semi-space size (for copying).
    pub semispace_size: usize,
    /// Whether to run finalizers after collection.
    pub run_finalizers: bool,
    /// Whether to process weak references.
    pub process_weak_refs: bool,
    /// Maximum pause time target (milliseconds).
    pub max_pause_ms: u32,
}

impl Default for X86GCCollectorConfig {
    fn default() -> Self {
        Self {
            algorithm: X86GCCollectionAlgorithm::MarkSweep,
            nursery_size: 4 * 1024 * 1024,
            semispace_size: 8 * 1024 * 1024,
            run_finalizers: true,
            process_weak_refs: true,
            max_pause_ms: 10,
        }
    }
}

// ============================================================================
// Relocation and Stack Walking Runtime Support
// ============================================================================

/// Runtime descriptor for locating GC roots in a compiled frame.
#[derive(Debug, Clone)]
pub struct X86GCFrameDescriptor {
    /// Return address identifying this frame.
    pub return_address: u64,
    /// Frame size in bytes.
    pub frame_size: u32,
    /// Offsets of stack-slot roots from RBP (negative for locals).
    pub stack_roots: Vec<i32>,
    /// DWARF numbers of register roots.
    pub register_roots: Vec<u16>,
    /// Whether the function has a frame pointer.
    pub has_frame_pointer: bool,
    /// Whether the function uses a red zone.
    pub uses_red_zone: bool,
}

/// Registry of frame descriptors for stack walking.
pub struct X86GCFrameRegistry {
    /// Map from return address to frame descriptor.
    frames: BTreeMap<u64, X86GCFrameDescriptor>,
}

impl X86GCFrameRegistry {
    /// Create a new frame registry.
    pub fn new() -> Self {
        Self {
            frames: BTreeMap::new(),
        }
    }

    /// Register a frame descriptor.
    pub fn register(&mut self, desc: X86GCFrameDescriptor) {
        self.frames.insert(desc.return_address, desc);
    }

    /// Look up a frame descriptor by return address.
    pub fn lookup(&self, return_address: u64) -> Option<&X86GCFrameDescriptor> {
        self.frames.get(&return_address)
    }

    /// Find the closest frame descriptor for a given PC.
    /// Performs a binary search to find the frame whose return address
    /// is the nearest preceding address to the PC.
    pub fn find_containing_frame(&self, pc: u64) -> Option<&X86GCFrameDescriptor> {
        self.frames.range(..=pc).next_back().map(|(_, desc)| desc)
    }

    /// Walk the stack starting from a given RBP and RSP.
    /// Yields frame descriptors and their associated frame pointers.
    pub fn walk_stack(
        &self,
        mut rbp: u64,
        rsp: u64,
        stack_bottom: u64,
    ) -> Vec<(X86GCFrameDescriptor, u64)> {
        let mut frames = Vec::new();
        let mut depth = 0;
        const MAX_DEPTH: usize = 1024;

        while rbp > rsp && rbp < stack_bottom && depth < MAX_DEPTH {
            // Return address is at rbp + 8
            let ret_addr = rbp + 8; // Would dereference in real code
            if let Some(desc) = self.find_containing_frame(ret_addr) {
                frames.push((desc.clone(), rbp));
            }
            // Move to caller's frame: saved RBP is at [rbp]
            rbp = rbp; // Would dereference: rbp = *((u64*)rbp)
            depth += 1;
        }
        frames
    }
}

// ============================================================================
// GC Safepoint Insertion — Back-Edge Polling
// ============================================================================

/// Context for inserting safepoint polls at loop back-edges.
pub struct X86GCBackEdgePollInserter {
    /// Polling page address.
    polling_page: u64,
    /// Maximum distance between polls (in instructions).
    max_poll_distance: u32,
    /// Current instruction counter.
    inst_counter: u32,
    /// Whether a poll has been inserted at the current position.
    poll_inserted: bool,
}

impl X86GCBackEdgePollInserter {
    /// Create a new back-edge poll inserter.
    pub fn new(polling_page: u64, max_distance: u32) -> Self {
        Self {
            polling_page,
            max_poll_distance: max_distance,
            inst_counter: 0,
            poll_inserted: false,
        }
    }

    /// Advance the instruction counter. Returns true if a poll is needed.
    pub fn advance(&mut self, num_insts: u32) -> bool {
        self.inst_counter += num_insts;
        self.poll_inserted = false;
        if self.inst_counter >= self.max_poll_distance {
            self.inst_counter = 0;
            self.poll_inserted = true;
            true
        } else {
            false
        }
    }

    /// Generate the poll sequence for a back-edge.
    /// On X86-64, this is a load from the polling page that faults when
    /// the GC wants to suspend threads.
    pub fn generate_poll(&self) -> Vec<u8> {
        let mut seq = Vec::new();
        // movabs rax, polling_page
        seq.push(0x48);
        seq.push(0xB8);
        seq.extend_from_slice(&self.polling_page.to_le_bytes());
        // test byte [rax], 0
        seq.push(0xF6);
        seq.push(0x00);
        seq.push(0x00);
        seq
    }
}

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

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

    #[test]
    fn test_root_creation() {
        let base = X86GCRoot::new_base(0, -8);
        assert_eq!(base.root_id, 0);
        assert_eq!(base.kind, X86GCRootKind::BasePointer);
        assert_eq!(base.frame_offset, -8);
        assert!(base.is_live);

        let reg = X86GCRoot::new_register(1, DW_REG_RBX);
        assert_eq!(reg.kind, X86GCRootKind::Register);
        assert_eq!(reg.dwarf_reg, Some(DW_REG_RBX));

        let derived = X86GCRoot::new_derived(2, -16, 32);
        assert_eq!(derived.kind, X86GCRootKind::DerivedPointer);
        assert_eq!(derived.derived_offset, Some(32));
    }

    #[test]
    fn test_statepoint_strategy() {
        let mut strat = StatepointExampleStrategy::new();

        strat.begin_function("test_func");
        strat.add_root("test_func", X86GCRoot::new_base(0, -8));
        strat.add_root("test_func", X86GCRoot::new_register(1, DW_REG_RBX));
        strat.add_root("test_func", X86GCRoot::new_base(2, -16));

        strat.add_safepoint("test_func", 0x10, X86GCSafepointKind::Call);
        strat.add_safepoint("test_func", 0x30, X86GCSafepointKind::LoopBackedge);

        strat.compute_liveness("test_func");
        strat.end_function("test_func", 0x400000, 64);

        let sm = strat.finalize_stack_maps();
        assert_eq!(sm.function_count(), 1);
        assert_eq!(sm.total_records(), 2);
    }

    #[test]
    fn test_coreclr_strategy() {
        let mut strat = CoreCLRStrategy::new();

        strat.add_region(0x00, 0x50, CoreCLRInterruptibility::FullyInterruptible);
        strat.add_region(0x50, 0x80, CoreCLRInterruptibility::PartiallyInterruptible);

        strat.register_slot(0, -8);
        strat.register_slot(1, -16);
        strat.register_slot_in_reg(2, DW_REG_RBX);

        strat.add_slot_change(
            0x60,
            0,
            true,
            CoreCLRSlotFlags {
                is_pinned: true,
                ..Default::default()
            },
        );

        let gcinfo = strat.encode_gcinfo();
        // Header: version=2, hijack=1, ret_kind=0
        assert_eq!(gcinfo[0], 2);
        assert_eq!(gcinfo[1], 1);
        assert_eq!(gcinfo[2], 0);
        assert!(gcinfo.len() > 3);
    }

    #[test]
    fn test_ocaml_strategy() {
        let mut strat = OCamlStrategy::new();

        strat.emit_frame_descriptor(0x401000, 64, &[8, 16, 24]);
        strat.emit_frame_descriptor(0x402000, 128, &[0, 8, 16, 32, 48]);

        let table = strat.encode_frame_table();
        assert!(table.len() >= 8);

        // Check count
        let count = u32::from_le_bytes([table[0], table[1], table[2], table[3]]);
        assert_eq!(count, 2);
    }

    #[test]
    fn test_erlang_strategy() {
        let mut strat = ErlangStrategy::new();

        strat.emit_frame(0x400000, 3, 2, 0x0007);
        strat.emit_frame(0x401000, 2, 4, 0x00FF);

        // Test reduction
        assert!(!strat.perform_reduction()); // 3999 remaining
        for _ in 0..3999 {
            strat.perform_reduction();
        }
        assert!(strat.perform_reduction()); // GC needed

        let table = strat.encode_frame_table();
        let count = u32::from_le_bytes([table[0], table[1], table[2], table[3]]);
        assert_eq!(count, 2);
    }

    #[test]
    fn test_shadow_stack_strategy() {
        let mut strat = ShadowStackStrategy::new(0x70000000, 1024);

        let frame_idx = strat.push_frame();
        assert_eq!(frame_idx, 0);

        strat.push_root(0x1000, 1);
        strat.push_root(0x2000, 2);
        strat.push_root(0x3000, 3);

        let roots = strat.roots_in_current_frame();
        assert_eq!(roots.len(), 3);
        assert_eq!(roots[0].pointer, 0x1000);
        assert_eq!(roots[2].pointer, 0x3000);

        // Second frame
        strat.push_frame();
        strat.push_root(0x4000, 4);
        assert_eq!(strat.roots_in_current_frame().len(), 1);

        strat.pop_frame();
        assert_eq!(strat.roots_in_current_frame().len(), 3);
    }

    #[test]
    fn test_polling_page_sequence() {
        let poll = X86GCPollingPage::default();
        let seq = poll.generate_poll_sequence();
        // Should be 17 bytes: 2 (REX/MOV) + 8 (imm64) + 3 (REX/TEST/ModRM) + 1 (imm8)
        assert!(seq.len() >= 10);
    }

    #[test]
    fn test_card_mark_barrier() {
        let barrier = X86GCBarrierLowering::new(0x10000000);
        let seq = barrier.lower_card_mark(DW_REG_RDI as u16, DW_REG_R11 as u16);
        assert_eq!(seq.kind, X86GCWriteBarrierKind::CardMarking);
        assert!(seq.is_inline);
        assert!(!seq.sequence.is_empty());
    }

    #[test]
    fn test_safepoint_insertion() {
        let config = X86GCSafepointInsertionConfig::default();
        let mut insertion = X86GCSafepointInsertion::new(config);

        insertion.begin_function("test_safepoint");
        let seq =
            insertion.insert_safepoint("test_safepoint", 0x20, X86GCSafepointKind::LoopBackedge);
        assert!(seq.is_some());

        let safepoints = insertion.end_function("test_safepoint");
        assert_eq!(safepoints.len(), 1);
        assert_eq!(safepoints[0].kind, X86GCSafepointKind::LoopBackedge);
    }

    #[test]
    fn test_stack_walker() {
        let mut walker = X86GCStackWalker::new(
            0x7FFF_0000_0F00, // rsp
            0x7FFF_0000_0FE0, // rbp (top of stack)
            0x7FFF_0000_0000, // stack low
            0x7FFF_0000_1000, // stack high
        );

        let addrs = walker.collect_return_addresses();
        // Since read_stack returns 0 (simulated), the walker will stop
        // after one frame (saved_rbp = 0).
        assert!(addrs.len() <= 1);
    }

    #[test]
    fn test_relocation_table() {
        let mut table = X86GCRelocationTable::new();

        table.add_relocation(X86GCRelocation {
            safepoint_id: 0,
            root_id: 1,
            old_address: 0x1000,
            new_address: 0x2000,
            is_base_pointer: true,
        });

        table.add_relocation(X86GCRelocation {
            safepoint_id: 0,
            root_id: 2,
            old_address: 0x3000,
            new_address: 0x4000,
            is_base_pointer: false,
        });

        let mut roots = vec![X86GCRoot::new_base(1, -8), X86GCRoot::new_base(2, -16)];

        let updates = table.apply_relocations(0, &mut roots);
        assert_eq!(updates.len(), 2);
        assert_eq!(updates[0].1, 0x2000);
        assert_eq!(updates[1].1, 0x4000);
    }

    #[test]
    fn test_gc_lowering_full_pipeline() {
        let config = X86GCLoweringConfig::default();
        let mut lowering = X86GCLowering::new(config);

        lowering.begin_function("test_pipeline", 0x400000);

        // Add roots
        lowering.add_root(X86GCRoot::new_base(0, -8));
        lowering.add_root(X86GCRoot::new_register(1, DW_REG_RBX));

        // Add derived pointer
        lowering.add_derived_pointer(X86GCDerivedPtr {
            derived_root_id: 10,
            base_root_id: 0,
            offset: 16,
            from_gep: true,
            from_ptr_arith: false,
            creator_inst: None,
            in_bounds: true,
        });

        // Insert safepoints
        let _ = lowering.insert_safepoint(0x20, X86GCSafepointKind::Call);
        let _ = lowering.insert_safepoint(0x40, X86GCSafepointKind::LoopBackedge);

        // Lower a write barrier
        let barrier = lowering.lower_write_barrier(
            X86GCWriteBarrierKind::CardMarking,
            DW_REG_RDI as u16,
            None,
        );
        assert_eq!(barrier.kind, X86GCWriteBarrierKind::CardMarking);

        lowering.end_function(0x400000, 64);

        let stack_map = lowering.finalize();
        assert!(stack_map.total_records() > 0);
    }

    #[test]
    fn test_gc_strategy_display() {
        assert_eq!(
            X86GCStrategyKind::StatepointExample.to_string(),
            "statepoint-example"
        );
        assert_eq!(X86GCStrategyKind::CoreCLR.to_string(), "coreclr");
        assert_eq!(X86GCStrategyKind::OCaml.to_string(), "ocaml");
        assert_eq!(X86GCStrategyKind::Erlang.to_string(), "erlang");
        assert_eq!(X86GCStrategyKind::ShadowStack.to_string(), "shadow-stack");
    }

    #[test]
    fn test_stack_map_section_empty() {
        let sm = X86GCStackMapSection::new();
        assert_eq!(sm.header.version, 3);
        assert_eq!(sm.total_records(), 0);
        assert_eq!(sm.function_count(), 0);
    }

    #[test]
    fn test_coreclr_hijack_stub() {
        let strat = CoreCLRStrategy::new();
        let stub = strat.generate_hijack_stub();
        assert!(stub.len() > 20);
        // Verify RET at the end
        assert_eq!(stub.last(), Some(&0xC3));
    }

    #[test]
    fn test_patchpoint_nop_sled() {
        let sled = X86GCPatchPointLowering::generate_nop_sled(16);
        assert_eq!(sled.len(), 16);
        // Multi-byte NOPs start with 0x0F 0x1F
        assert!(sled.windows(2).any(|w| w == [0x0F, 0x1F]) || sled[0] == 0x90);
    }

    #[test]
    fn test_patchpoint_nop_sled_small() {
        for size in 1..=16 {
            let sled = X86GCPatchPointLowering::generate_nop_sled(size);
            assert_eq!(sled.len(), size as usize);
        }
    }

    #[test]
    fn test_patchpoint_creation() {
        let mut lowering = X86GCPatchPointLowering::new();
        let pp = lowering.create_patchpoint(
            0x50,
            16,
            0x401000,
            vec![X86GCStackMapLocation::Direct {
                dwarf_reg: DW_REG_RBP as u16,
                _reserved: 0,
                offset: -8,
                size: 8,
            }],
        );
        assert_eq!(pp.id, 0);
        assert_eq!(pp.nop_sled_size, 16);
        assert_eq!(pp.stack_map_record.num_locations, 1);
        assert_eq!(lowering.get_patchpoints().len(), 1);
    }

    #[test]
    fn test_bump_pointer_alloc() {
        let alloc = X86GCAllocLowering::new(0x600000, 0x601000, 0x500000);
        let seq = alloc.generate_bump_alloc(64, DW_REG_RAX as u16, DW_REG_RCX as u16);
        assert!(seq.len() > 20);
        // Should contain cmp instruction (0x3B)
        assert!(seq.contains(&0x3B));
    }

    #[test]
    fn test_free_list_alloc() {
        let alloc = X86GCAllocLowering::new(0x600000, 0x601000, 0x500000);
        let seq = alloc.generate_free_list_alloc(3, DW_REG_RAX as u16);
        assert!(seq.len() > 5);
        // Should contain test instruction
        assert!(seq.contains(&0x85));
    }

    #[test]
    fn test_collector_config_default() {
        let config = X86GCCollectorConfig::default();
        assert_eq!(config.algorithm, X86GCCollectionAlgorithm::MarkSweep);
        assert_eq!(config.nursery_size, 4 * 1024 * 1024);
        assert_eq!(config.max_pause_ms, 10);
    }

    #[test]
    fn test_frame_registry_register_and_lookup() {
        let mut registry = X86GCFrameRegistry::new();
        let desc = X86GCFrameDescriptor {
            return_address: 0x401050,
            frame_size: 64,
            stack_roots: vec![-8, -16],
            register_roots: vec![DW_REG_RBX as u16],
            has_frame_pointer: true,
            uses_red_zone: false,
        };
        registry.register(desc.clone());

        let found = registry.lookup(0x401050);
        assert!(found.is_some());
        assert_eq!(found.unwrap().frame_size, 64);
        assert_eq!(found.unwrap().stack_roots.len(), 2);
    }

    #[test]
    fn test_frame_registry_find_containing() {
        let mut registry = X86GCFrameRegistry::new();
        registry.register(X86GCFrameDescriptor {
            return_address: 0x401000,
            frame_size: 48,
            stack_roots: vec![-8],
            register_roots: vec![],
            has_frame_pointer: true,
            uses_red_zone: false,
        });
        registry.register(X86GCFrameDescriptor {
            return_address: 0x402000,
            frame_size: 96,
            stack_roots: vec![-8, -16, -24],
            register_roots: vec![DW_REG_R12 as u16],
            has_frame_pointer: true,
            uses_red_zone: false,
        });

        // PC between frames should find the first frame
        let found = registry.find_containing_frame(0x401500);
        assert!(found.is_some());
        assert_eq!(found.unwrap().return_address, 0x401000);

        // PC at exact address should find that frame
        let found2 = registry.find_containing_frame(0x402000);
        assert!(found2.is_some());
        assert_eq!(found2.unwrap().frame_size, 96);
    }

    #[test]
    fn test_back_edge_poll_inserter() {
        let mut inserter = X86GCBackEdgePollInserter::new(0x7FFFFFFFFFF000, 100);

        // Advance 50 instructions: no poll needed
        assert!(!inserter.advance(50));

        // Advance another 51: poll needed
        assert!(inserter.advance(51));
        assert!(inserter.poll_inserted);

        let poll_seq = inserter.generate_poll();
        assert!(poll_seq.len() > 5);
    }

    #[test]
    fn test_alloc_kind_discriminants() {
        assert_ne!(
            X86GCAllocKind::BumpPointer as u8,
            X86GCAllocKind::FreeList as u8
        );
        assert_ne!(
            X86GCAllocKind::TLAB as u8,
            X86GCAllocKind::LargeObject as u8
        );
    }
}