logicaffeine-forge 0.10.1

Copy-and-patch JIT for LOGOS — executable-memory layer and stencil runtime (native only).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
//! J1: the straight-line micro-op compiler — bytecode in, native code out.
//!
//! [`MicroOp`] is the JIT's input IR: the integer subset of the bytecode VM's
//! register operations. [`compile_straightline`] lowers each op to a fixed
//! stencil micro-sequence over the frame/operand-stack machine model and glues
//! the result into one executable chain:
//!
//! ```text
//! LoadConst{dst,v}  →  const(v); slot_set(dst)
//! Move{dst,src}     →  slot_get(src); slot_set(dst)
//! Add{dst,l,r}      →  slot_get(l); slot_get(r); addi; slot_set(dst)
//! …                    (Sub/Mul/Lt/Eq identical shape)
//! Gt{dst,l,r}       →  slot_get(r); slot_get(l); lti; slot_set(dst)   (swap)
//! Return{src}       →  slot_get(src); return
//! ```
//!
//! Anything outside this subset is the caller's tier-up bail (`None` from the
//! adapter) — the VM keeps running it.

use std::sync::atomic::{AtomicI64, Ordering};

use crate::buffer::{HoleValue, JitBuffer, JitChain};
use crate::{
    V_BINOP, V_BRANCH, V_CONST, V_FBINOP, V_FMOV, V_MOV, V_RET, V_SQRTF, V_DIVF, V_I2F, V_BRANCHF,
    V_ARRLD_RPTR, V_ARRLD_RPTR_C, V_ARRST_RPTR, V_ARRST_RPTR_C,
    ST_ADD3, ST_ADDF3, ST_AND3, ST_ARRLD, ST_ARRLD2_ADDF, ST_ARRLD2_MULF, ST_ARRLD2_SUBF, ST_ARRLDB, ST_ARRLDB_U, ST_ARRLD_U, ST_ARRST, ST_ARRSTB, ST_ARRSTB_U, ST_ARRST_U, ST_BREQ, ST_BREQF, ST_BRLEF, ST_BRLT, ST_CALL, ST_PUSH, ST_LIST_CLEAR,
    ST_ARRLD_I32, ST_ARRLD_I32_U, ST_ARRST_I32, ST_ARRST_I32_U,
    ST_ARRLD2_ADD, ST_ARRLD2_ADD_U, ST_ARRLD2_SUB, ST_ARRLD2_SUB_U, ST_ARRLD2_MUL, ST_ARRLD2_MUL_U,
    ST_ARRLDAFF_NONE, ST_ARRLDAFF_NONE_U, ST_ARRLDAFF_ADD, ST_ARRLDAFF_ADD_U,
    ST_ARRLDAFF_SUB, ST_ARRLDAFF_SUB_U, ST_ARRLDAFF_MUL, ST_ARRLDAFF_MUL_U,
    ST_ARRRMW_ADD, ST_ARRRMW_ADD_U, ST_ARRRMW_SUB, ST_ARRRMW_SUB_U, ST_ARRRMW_MUL, ST_ARRRMW_MUL_U,
    ST_ARRRMW_AND, ST_ARRRMW_AND_U, ST_ARRRMW_OR, ST_ARRRMW_OR_U, ST_ARRRMW_XOR, ST_ARRRMW_XOR_U,
    ST_ARRRMW_ADDF, ST_ARRRMW_ADDF_U, ST_ARRRMW_SUBF, ST_ARRRMW_SUBF_U, ST_ARRRMW_MULF, ST_ARRRMW_MULF_U,
    ST_FMAF,
    ST_ARRCONDSWAP_GT, ST_ARRCONDSWAP_GT_U, ST_ARRCONDSWAP_LT, ST_ARRCONDSWAP_LT_U,
    ST_ARRCONDSWAP_GE, ST_ARRCONDSWAP_GE_U, ST_ARRCONDSWAP_LE, ST_ARRCONDSWAP_LE_U,
    ST_ARRSWAP, ST_ARRSWAP_U,
    ST_BRLTF, ST_BRZ, ST_CALL_PRECISE, ST_CONSTST, ST_DEOPT, ST_DEOPT_AT, ST_DIV3C,
    ST_DIVPOW2, ST_MAGICDIV,
    ST_DIVF3C, ST_EQ3, ST_EQF3, ST_I2F2,
    ST_JUMP, ST_LE3, ST_LEF3, ST_LT3, ST_LTF3, ST_MOD3C, ST_MOVSS, ST_MUL3, ST_MULF3, ST_NE3,
    ST_ALLOCLIST, ST_CALL_SELF, ST_CALL_SELF_COPY, ST_LISTTRIPLE, ST_MAPHGET, ST_MAPHHAS, ST_MAPHSET, ST_MEMMEM, ST_NEF3,
    ST_NOTB2,
    ST_NOTI2, ST_OR3, ST_RET2,
    ST_SHL3, ST_SHR3, ST_SQRTF2, ST_SUB3,
    ST_SUBF3, ST_XOR3,
};

/// Frame slot index (a VM register number).
pub type Slot = u16;

/// The kernel's locked maximum LOGOS call depth, BAKED into the precise and
/// self call stencils (their holes are all spoken for, so the limit cannot be
/// a runtime hole). It mirrors `logicaffeine_compile::semantics::MAX_CALL_DEPTH`
/// and the matching constant in `stencils/int_stencils.rs`; the callers in the
/// JIT crate pass that same value, asserted here at stencil selection so a
/// drift fails loudly instead of silently capping native recursion at the wrong
/// depth (which would diverge from the kernel's depth-exceeded error).
pub const BAKED_CALL_DEPTH: i64 = 2_500;

/// Comparison kinds for [`MicroOp::Branch`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Cmp {
    /// `lhs < rhs`
    Lt,
    /// `lhs > rhs`
    Gt,
    /// `lhs <= rhs`
    LtEq,
    /// `lhs >= rhs`
    GtEq,
    /// `lhs == rhs`
    Eq,
    /// `lhs != rhs`
    NotEq,
}

impl Cmp {
    /// Evaluate over two i64s.
    pub fn eval(self, a: i64, b: i64) -> bool {
        match self {
            Cmp::Lt => a < b,
            Cmp::Gt => a > b,
            Cmp::LtEq => a <= b,
            Cmp::GtEq => a >= b,
            Cmp::Eq => a == b,
            Cmp::NotEq => a != b,
        }
    }

    /// The comparison with the opposite truth value.
    pub fn negated(self) -> Cmp {
        match self {
            Cmp::Lt => Cmp::GtEq,
            Cmp::Gt => Cmp::LtEq,
            Cmp::LtEq => Cmp::Gt,
            Cmp::GtEq => Cmp::Lt,
            Cmp::Eq => Cmp::NotEq,
            Cmp::NotEq => Cmp::Eq,
        }
    }
}

/// The float binary operation of a fused [`MicroOp::ArrLoad2F`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FOp {
    /// IEEE addition.
    Add,
    /// IEEE subtraction.
    Sub,
    /// IEEE multiplication.
    Mul,
}

impl FOp {
    /// Apply over two f64s.
    pub fn eval(self, a: f64, b: f64) -> f64 {
        match self {
            FOp::Add => a + b,
            FOp::Sub => a - b,
            FOp::Mul => a * b,
        }
    }
}

/// The integer binary operation of a fused two-buffer [`MicroOp::ArrLoad2`]:
/// `frame[dst] = a[i-1] <op> b[j-1]`, the two elements loaded from two
/// (possibly distinct) pinned 8-byte int buffers. Add/Mul commute; Sub does
/// not (it only fuses `a[i] - b[j]`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum IOp {
    /// Wrapping i64 addition.
    Add,
    /// Wrapping i64 subtraction.
    Sub,
    /// Wrapping i64 multiplication.
    Mul,
}

impl IOp {
    /// Apply over two i64s with the kernel's wrapping semantics.
    pub fn eval(self, a: i64, b: i64) -> i64 {
        match self {
            IOp::Add => a.wrapping_add(b),
            IOp::Sub => a.wrapping_sub(b),
            IOp::Mul => a.wrapping_mul(b),
        }
    }
}

/// The index-arithmetic shape folded into a fused [`MicroOp::ArrLoadAffine`].
/// The computed 1-based index is `(frame[a] OP frame[b]).wrapping_add(c)` for a
/// two-slot op, or `frame[a].wrapping_add(c)` for [`AffOp::None`] (a single slot
/// plus a constant). The op wraps with the kernel's exact i64 semantics so the
/// load is bit-identical to the un-fused index arithmetic + `ArrLoad`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AffOp {
    /// No second slot: `idx = frame[a] + c` (the `w + 1` shape).
    None,
    /// `idx = (frame[a] + frame[b]) + c` (the `i*n + j + 1` tail).
    Add,
    /// `idx = (frame[a] - frame[b]) + c` (the `w - wi + 1` shape).
    Sub,
    /// `idx = (frame[a] * frame[b]) + c`.
    Mul,
}

impl AffOp {
    /// Compute the 1-based index with the kernel's wrapping i64 semantics.
    pub fn eval(self, a: i64, b: i64, c: i64) -> i64 {
        match self {
            AffOp::None => a.wrapping_add(c),
            AffOp::Add => a.wrapping_add(b).wrapping_add(c),
            AffOp::Sub => a.wrapping_sub(b).wrapping_add(c),
            AffOp::Mul => a.wrapping_mul(b).wrapping_add(c),
        }
    }
}

/// The operation of a fused read-modify-write [`MicroOp::ArrRMW`]:
/// `buf[idx-1] = buf[idx-1] <op> operand`. The INT ops (Add/Sub/Mul wrap as the
/// kernel's i64 arithmetic; And/Or/Xor bitwise) treat the element and operand as
/// raw i64; the FLOAT ops (AddF/SubF/MulF) reinterpret both as f64 (the nbody
/// velocity/position-update idiom). Sub/SubF are the only non-commutative
/// members — the peephole fuses them only when the loaded element is the LEFT
/// operand.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RmwOp {
    /// Wrapping addition.
    Add,
    /// Wrapping subtraction (`buf[i] - operand`).
    Sub,
    /// Wrapping multiplication.
    Mul,
    /// Bitwise AND.
    And,
    /// Bitwise OR.
    Or,
    /// Bitwise XOR.
    Xor,
    /// IEEE addition (operands reinterpreted as f64).
    AddF,
    /// IEEE subtraction (`buf[i] - operand`, as f64).
    SubF,
    /// IEEE multiplication (as f64).
    MulF,
}

impl RmwOp {
    /// Whether the op reinterprets its operands as f64.
    pub fn is_float(self) -> bool {
        matches!(self, RmwOp::AddF | RmwOp::SubF | RmwOp::MulF)
    }

    /// Apply over the raw i64 bits of the element and operand — the bit-exact
    /// spec every stencil must match. Float ops reinterpret the bits as f64,
    /// compute, and re-encode (copy-and-patch never reassociates, so ordering is
    /// exact).
    pub fn eval(self, a: i64, b: i64) -> i64 {
        match self {
            RmwOp::Add => a.wrapping_add(b),
            RmwOp::Sub => a.wrapping_sub(b),
            RmwOp::Mul => a.wrapping_mul(b),
            RmwOp::And => a & b,
            RmwOp::Or => a | b,
            RmwOp::Xor => a ^ b,
            RmwOp::AddF => (f64::from_bits(a as u64) + f64::from_bits(b as u64)).to_bits() as i64,
            RmwOp::SubF => (f64::from_bits(a as u64) - f64::from_bits(b as u64)).to_bits() as i64,
            RmwOp::MulF => (f64::from_bits(a as u64) * f64::from_bits(b as u64)).to_bits() as i64,
        }
    }
}

/// The source operand appended by a [`MicroOp::StrAppend`] — the right-hand side
/// of a `Set text to text + <s>` in a pinned mutable-Text build loop.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StrSrc {
    /// A single ASCII byte VALUE living in a frame slot (the `Kind::TextByte`
    /// lane: `text + ch` where `ch` is a 1-char ASCII text). The helper appends
    /// `byte as char` — bit-identical to the VM concatenating the 1-char `Text`.
    Byte(Slot),
    /// A constant ASCII byte slice baked into the chain (`text + "XXXXX"`): a
    /// `'static` pointer + length. The helper appends the whole slice — identical
    /// to the VM's `Text + Text` concatenation of the literal.
    Const {
        /// `'static` pointer to the constant's bytes.
        ptr: i64,
        /// The constant's byte length.
        len: i64,
    },
}

/// The integer straight-line subset the J1 compiler accepts.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MicroOp {
    /// `frame[dst] = value`.
    LoadConst {
        /// Destination slot.
        dst: Slot,
        /// Immediate value.
        value: i64,
    },
    /// `frame[dst] = frame[src]`.
    Move {
        /// Destination slot.
        dst: Slot,
        /// Source slot.
        src: Slot,
    },
    /// `frame[dst] = frame[lhs] + frame[rhs]` (wrapping).
    Add {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `frame[dst] = frame[lhs] - frame[rhs]` (wrapping).
    Sub {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `frame[dst] = frame[lhs] * frame[rhs]` (wrapping).
    Mul {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `frame[dst] = (frame[lhs] < frame[rhs]) as i64`.
    Lt {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `frame[dst] = (frame[lhs] > frame[rhs]) as i64`.
    Gt {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `frame[dst] = (frame[lhs] == frame[rhs]) as i64`.
    Eq {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `frame[dst] = frame[lhs] / frame[rhs]` (wrapping; `MIN / -1 = MIN`).
    /// A zero divisor SIDE-EXITS the chain ([`ChainOutcome::Deopt`]) before
    /// any effect — the caller replays on bytecode, where the kernel raises
    /// the exact "Division by zero" error.
    Div {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `frame[dst] = frame[lhs] / 2^k` (signed, round toward zero) via a
    /// sign-correcting shift — bit-exact with [`MicroOp::Div`] but with NO
    /// side-exit (a power-of-two divisor is never `0` or `-1`). Emitted in
    /// place of `Div` when the divisor is a region-constant power of two and
    /// the dividend is integer-typed.
    DivPow2 {
        /// Destination slot.
        dst: Slot,
        /// Dividend slot.
        lhs: Slot,
        /// Shift amount `k` (divisor = `2^k`, `1 <= k <= 62`).
        k: u32,
    },
    /// `frame[dst] = frame[lhs] / c` (`mul_back == 0`) or `frame[lhs] % c`
    /// (`mul_back == c`) by the Granlund–Montgomery / libdivide UNSIGNED
    /// magic-reciprocal sequence (`mulhi(magic, x)` + add-fixup + shift, then
    /// `x - q*c` for the remainder) — a `mul`+`shr` (~3 cycles) instead of
    /// `idiv` (~25). NO side-exit (a literal `c > 0` is never `0` or `-1`).
    /// Emitted in place of `Div`/`Mod` when the divisor is a compile-time
    /// constant non-power-of-two and the dividend is proven Int and NON-NEGATIVE
    /// (the unsigned magic equals the signed truncating result only there). The
    /// `more` byte is the `logicaffeine_data::LogosDivU64` encoding (low 6 bits
    /// = shift, `0x40` = the 65-bit add-marker path, `0x80` = pure-shift pow2).
    MagicDivU {
        /// Destination slot.
        dst: Slot,
        /// Dividend slot.
        lhs: Slot,
        /// Precomputed magic multiplier `M`.
        magic: u64,
        /// Shift / path encoding (see `logicaffeine_data::LogosDivU64`).
        more: u8,
        /// `0` selects the quotient; otherwise the divisor `c` selects the
        /// remainder `x - (x/c)*c`.
        mul_back: i64,
    },
    /// `frame[dst] = frame[lhs] % frame[rhs]` (wrapping; `MIN % -1 = 0`).
    /// Zero divisor side-exits exactly like [`MicroOp::Div`].
    Mod {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `frame[dst] = (frame[lhs] <= frame[rhs]) as i64`.
    LtEq {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `frame[dst] = (frame[lhs] >= frame[rhs]) as i64`.
    GtEq {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `frame[dst] = (frame[lhs] != frame[rhs]) as i64`.
    Neq {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// Fused compare-and-branch: transfer to `target` when `cmp(lhs, rhs)` is
    /// FALSE (the `JumpIfFalse`-on-a-fresh-comparison shape); fall through
    /// when TRUE. No comparison value is materialized — the adapter proves
    /// the scratch dead before fusing.
    Branch {
        /// The comparison.
        cmp: Cmp,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
        /// Micro-op index to transfer to when the comparison is FALSE.
        target: usize,
    },
    /// `frame[dst] = frame[lhs] & frame[rhs]` (bitwise; logical for 0/1 Bools).
    BitAnd {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `frame[dst] = frame[lhs] | frame[rhs]` (bitwise; logical for 0/1 Bools).
    BitOr {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `frame[dst] = frame[lhs] ^ frame[rhs]`.
    BitXor {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `frame[dst] = frame[lhs].wrapping_shl(frame[rhs] as u32)` — the
    /// kernel's locked shift spec (count truncates to u32, masks mod 64).
    Shl {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `frame[dst] = frame[lhs].wrapping_shr(frame[rhs] as u32)` (arithmetic).
    Shr {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `frame[dst] = !frame[src]` — bitwise NOT (the kernel's Int `not`).
    NotInt {
        /// Destination slot.
        dst: Slot,
        /// Source slot.
        src: Slot,
    },
    /// `frame[dst] = frame[src] ^ 1` — logical NOT over 0/1 Bools.
    NotBool {
        /// Destination slot.
        dst: Slot,
        /// Source slot.
        src: Slot,
    },
    /// Float ops: f64 values as raw bits in the i64 slots. Arithmetic is
    /// IEEE; equality is the kernel's EPSILON rule.
    AddF {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `bits(f(lhs) - f(rhs))`.
    SubF {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `bits(f(lhs) * f(rhs))`.
    MulF {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `bits(f(lhs) / f(rhs))`; divisor `== 0.0` side-exits like [`MicroOp::Div`].
    DivF {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `(f(lhs) < f(rhs)) as i64` (IEEE, NaN → 0).
    LtF {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `(f(lhs) > f(rhs)) as i64`.
    GtF {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `(f(lhs) <= f(rhs)) as i64`.
    LtEqF {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// `(f(lhs) >= f(rhs)) as i64`.
    GtEqF {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// The kernel's epsilon equality as a value.
    EqF {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// Negated epsilon equality as a value.
    NeqF {
        /// Destination slot.
        dst: Slot,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
    },
    /// Fused FLOAT compare-and-branch: transfer to `target` when
    /// `cmp(f(lhs), f(rhs))` is FALSE — which, under IEEE, includes every
    /// NaN-unordered comparison (matching the kernel's relations exactly).
    BranchF {
        /// The comparison.
        cmp: Cmp,
        /// Left operand slot.
        lhs: Slot,
        /// Right operand slot.
        rhs: Slot,
        /// Micro-op index to transfer to when the comparison is FALSE.
        target: usize,
    },
    /// `frame[dst] = bits(frame[src] as f64)` — the kernel's Int→Float
    /// promotion.
    IntToFloat {
        /// Destination slot.
        dst: Slot,
        /// Source slot.
        src: Slot,
    },
    /// `frame[dst] = bits(sqrt(f(frame[src])))` — the kernel's Float sqrt
    /// builtin (IEEE: negative → NaN, no error path).
    SqrtF {
        /// Destination slot.
        dst: Slot,
        /// Source slot.
        src: Slot,
    },
    /// Map get through the pinned storage: an Int hit lands in `dst`; a
    /// miss or non-Int value side-exits (the replay raises the kernel's
    /// exact error or re-runs the boxed path).
    MapGet {
        /// Destination slot.
        dst: Slot,
        /// Key slot.
        key: Slot,
        /// The pinned `*mut MapStorage` slot.
        map_slot: Slot,
        /// Runtime helper address (indirect call, no relocation).
        helper_addr: i64,
    },
    /// Map insert (always succeeds; same storage, same iteration order).
    MapSet {
        /// Value slot.
        src: Slot,
        /// Key slot.
        key: Slot,
        /// The pinned `*mut MapStorage` slot.
        map_slot: Slot,
        /// Runtime helper address.
        helper_addr: i64,
    },
    /// DIRECT self-call (mode A): entry patched post-layout into the
    /// literal pool; static frame-size bound; plain replay deopt.
    CallSelf {
        /// Caller register receiving the result.
        dst: Slot,
        /// First slot of the staged argument window.
        args_start: Slot,
        /// Live-depth cell address.
        depth_addr: i64,
        /// Shared status cell address.
        status_addr: i64,
        /// MY arena-limit slot (the bound source).
        limit_slot: Slot,
        /// The callee's (own) full frame size in slots.
        frame_size: i64,
    },
    /// DIRECT self-call with a FUSED argument copy (Lever A: the pinned-arg
    /// self-call ABI). Same model as [`MicroOp::CallSelf`], but it stages the
    /// `arg_count` contiguous scalar arguments from `src_start..` into the
    /// callee window itself rather than relying on `arg_count` separate
    /// `Move` pieces before the call — one piece replaces `arg_count + 1`.
    /// Emitted only for all-scalar (Int/Bool/Float) self-calls; list-param
    /// and pin-triple staging stay on the per-`Move` path.
    CallSelfCopy {
        /// Caller register receiving the result.
        dst: Slot,
        /// First slot of the staged argument window (the callee frame base).
        args_start: Slot,
        /// First slot of the SOURCE argument block in this frame.
        src_start: Slot,
        /// Number of contiguous scalar arguments to copy.
        arg_count: u16,
        /// Live-depth cell address.
        depth_addr: i64,
        /// Shared status cell address.
        status_addr: i64,
        /// MY arena-limit slot (the bound source).
        limit_slot: Slot,
        /// The callee's (own) full frame size in slots.
        frame_size: i64,
    },
    /// Allocate a registry-owned fresh list and plant its pin triple.
    NewList {
        /// Pin handle slot.
        vec_slot: Slot,
        /// Pin buffer-pointer slot.
        ptr_slot: Slot,
        /// Pin length slot.
        len_slot: Slot,
        /// Runtime allocator address.
        helper_addr: i64,
    },
    /// Plant a pin triple from a list handle already in `handle_slot`
    /// (a self-call's returned list, seen from the caller).
    ListTriple {
        /// Slot holding the `*mut Vec<i64>` handle.
        handle_slot: Slot,
        /// Pin handle slot.
        vec_slot: Slot,
        /// Pin buffer-pointer slot.
        ptr_slot: Slot,
        /// Pin length slot.
        len_slot: Slot,
        /// Runtime helper address.
        helper_addr: i64,
    },
    /// Map membership into `dst` (0/1).
    MapHas {
        /// Destination slot.
        dst: Slot,
        /// Key slot.
        key: Slot,
        /// The pinned `*mut MapStorage` slot.
        map_slot: Slot,
        /// Runtime helper address.
        helper_addr: i64,
    },
    /// Native SELF-CALL through the program's entry table (callee frame
    /// windowed at `base + args_start` like the VM). Missing entry, a
    /// MAX_CALL_DEPTH crossing, or arena overflow side-exits the WHOLE
    /// native stack via the shared status cell.
    Call {
        /// Result slot.
        dst: Slot,
        /// Callee frame offset within the caller's frame.
        args_start: Slot,
        /// Address of the [entry, regcount] table slot pair.
        table_addr: i64,
        /// Address of the shared live-depth cell.
        depth_addr: i64,
        /// Address of the shared deopt-status cell.
        status_addr: i64,
        /// Frame slot holding the arena END address.
        limit_slot: Slot,
        /// MAX_CALL_DEPTH.
        depth_limit: i64,
    },
    /// Pinned-array push. The contiguous regalloc backend lowers this to an
    /// INLINE fast path — `len < cap ? buffer[len++] = v` straight in registers,
    /// reading the capacity from the live `*mut Vec` (a codegen-probed field
    /// offset, never a hardcoded layout) — and calls the runtime helper ONLY on
    /// the cold realloc boundary (`len == cap`), where it reallocates and
    /// refreshes the pinned pointer/length slots. The per-piece stencil tier
    /// always calls the helper.
    ArrPush {
        /// Value slot.
        src: Slot,
        /// Frame slot holding the `*mut Vec` handle.
        vec_slot: Slot,
        /// Pinned pointer slot to refresh.
        ptr_slot: Slot,
        /// Pinned length slot to refresh.
        len_slot: Slot,
        /// The runtime helper's address.
        helper_addr: i64,
        /// 1-BYTE (`Seq of Bool`) element: the inline fast-path store writes the
        /// boolean normalization `(v != 0) as u8` (matching `logos_rt_push_bool`),
        /// not 8 raw bytes. `false` for Int/Float (8-byte raw bits).
        byte: bool,
        /// 4-byte Int element (`ListRepr::IntsI32`): the inline fast-path store
        /// TRUNCATES the value to its low 4 bytes (lossless under the narrowing
        /// proof), matching `logos_rt_push_i32`. Mutually exclusive with `byte`.
        narrow32: bool,
    },
    /// Pinned-array in-place clear through a runtime helper: truncate the buffer
    /// to empty (keep capacity) and refresh the pinned pointer/length slots
    /// (length → 0). Lowers an in-region `NewEmptyList` on a pinned array; an
    /// in-place mutation, so a PRECISE region resumes over it soundly.
    ListClear {
        /// Frame slot holding the `*mut Vec` handle.
        vec_slot: Slot,
        /// Pinned pointer slot to refresh.
        ptr_slot: Slot,
        /// Pinned length slot to refresh (set to 0).
        len_slot: Slot,
        /// The runtime helper's address.
        helper_addr: i64,
    },
    /// Pinned MUTABLE-Text append (`Set text to text + <s>`) through a runtime
    /// helper. `text_handle_slot` holds a `*mut Value` to the VM register cell
    /// holding the accumulator (planted at region entry); the helper grows the
    /// accumulator THROUGH that cell with EXACTLY the VM's `add_assign`
    /// semantics — in place when the `Rc<String>` is sole-owned, copy-on-write
    /// (a fresh `Rc` written back into the cell, the alias untouched) otherwise.
    /// `src` is the appended operand (a 1-char frame byte, or a baked constant
    /// slice). Bit-identical to the tree-walker for every alias case. The buffer
    /// reaches into the VM register file, so — like the other helper calls — it
    /// clobbers the caller-saved registers (the residents are spilled/reloaded).
    StrAppend {
        /// Frame slot holding the `*mut Value` accumulator-cell handle.
        text_handle_slot: Slot,
        /// The appended source operand.
        src: StrSrc,
        /// The runtime helper's address (`logos_rt_str_append`).
        helper_addr: i64,
    },
    /// COUNT OVERLAPPING SUBSTRING MATCHES through a runtime helper — the whole
    /// naive-search nest (string_search) collapsed to one piece. The helper
    /// reads the pinned haystack/needle byte buffers, counts overlapping needle
    /// occurrences over the outer range `[frame[i_slot], textLen - needleLen + 1]`
    /// (1-based, matching the nest), ADDS the count into `frame[count_slot]`, and
    /// advances `frame[i_slot]` to the loop's exit value. On a recoverable
    /// disagreement (a checked needle index past the needle buffer) it touches
    /// nothing and takes the deopt continuation, so the VM replays the exact
    /// nest on bytecode and raises the same error. Holes: 0 = haystack ptr slot,
    /// 1 = haystack len slot, 2 = needle ptr slot, 3 = needle len slot, 4 =
    /// needleLen value slot, 5 = i (start) slot, 6 = count accumulator slot,
    /// 7 = helper address.
    MemMem {
        /// Frame slot holding the pinned haystack buffer pointer.
        h_ptr_slot: Slot,
        /// Frame slot holding the pinned haystack length.
        h_len_slot: Slot,
        /// Frame slot holding the pinned needle buffer pointer.
        n_ptr_slot: Slot,
        /// Frame slot holding the pinned needle length.
        n_len_slot: Slot,
        /// Frame slot holding the program's `needleLen` value (inner bound).
        needle_len_slot: Slot,
        /// Frame slot holding the 1-based outer index `i` (start; written to the
        /// exit value on success).
        i_slot: Slot,
        /// Frame slot holding the running `count` accumulator (added into).
        count_slot: Slot,
        /// The runtime helper's address.
        helper_addr: i64,
    },
    /// Pinned-array load: `frame[dst] = buffer[frame[idx] - 1]` (1-based;
    /// bits regardless of element kind). Out-of-bounds (incl. 0/negative)
    /// SIDE-EXITS before any effect.
    ArrLoad {
        /// Destination slot.
        dst: Slot,
        /// Index slot (1-based value).
        idx: Slot,
        /// Frame slot holding the pinned buffer pointer.
        ptr_slot: Slot,
        /// Frame slot holding the pinned length.
        len_slot: Slot,
        /// 1-byte elements (Bool buffers) instead of 8-byte.
        byte: bool,
        /// 4-byte SIGN-EXTENDED Int elements (`ListRepr::IntsI32`): the load is a
        /// `movsxd` widening the stored `i32` to a full i64 (lossless). Mutually
        /// exclusive with `byte`; both `false` ⇒ the default 8-byte element.
        narrow32: bool,
        /// Bounds-checked (`true`) or elided (`false`, the Oracle proved
        /// the index in range — V8/LLVM bounds-check elimination). Unchecked
        /// loads have no out-of-bounds continuation.
        checked: bool,
    },
    /// FUSED affine-index load: `frame[dst] = buffer[idx - 1]` where the 1-based
    /// `idx = (frame[a] OP frame[b]).wrapping_add(const_offset)` (or
    /// `frame[a] + const_offset` for [`AffOp::None`]) is computed INSIDE the
    /// stencil — the peephole collapses the index-arithmetic chain
    /// (`<binop>(t = a OP b); [Add(t2 = t + Kc);] ArrLoad(t2)`, or
    /// `Add(t = a + Kc); ArrLoad(t)`) plus the load into ONE piece when the
    /// intermediate index temps are single-use scratch. The dominant indexed
    /// inner loops (matrix_mult `i*n+k+1`, knapsack `w - wi + 1`, the `w + 1`
    /// row read) lose worst to V8 precisely on this per-op dispatch; folding the
    /// 2-3 index ops into the load is a direct dispatch-reduction win. Out of
    /// bounds (incl. the 0/negative index the wrapping-sub trick catches)
    /// SIDE-EXITS before any effect, exactly like [`MicroOp::ArrLoad`].
    ArrLoadAffine {
        /// Destination slot.
        dst: Slot,
        /// First index operand slot.
        a: Slot,
        /// The index op (`None` = single slot + const; else two-slot binop).
        op: AffOp,
        /// Second index operand slot (ignored for [`AffOp::None`]).
        b: Slot,
        /// The constant folded into the index (the trailing `+ Kc`; `0` if none).
        const_offset: i64,
        /// Frame slot holding the pinned buffer pointer.
        ptr_slot: Slot,
        /// Frame slot holding the pinned length.
        len_slot: Slot,
        /// Bounds-checked (`true`) or elided (`false`, the Oracle proved the
        /// computed index in range). Unchecked has no out-of-bounds continuation.
        checked: bool,
    },
    /// FUSED two-load float binop: `frame[dst] = bits(f(buf[i-1]) <op>
    /// f(buf[j-1]))`, BOTH elements from the SAME pinned 8-byte buffer (one
    /// pointer slot, one length slot). The peephole collapses `ArrLoad(t1 =
    /// arr[i]); ArrLoad(t2 = arr[j]); {Add,Sub,Mul}F(dst = t1 OP t2)` when both
    /// loads hit the same array and the two scratch loads are single-use — so
    /// the two f64s never round-trip through the frame. EITHER index out of
    /// bounds SIDE-EXITS before any effect, exactly like [`MicroOp::ArrLoad`].
    ArrLoad2F {
        /// Destination slot.
        dst: Slot,
        /// First index slot (1-based value).
        i: Slot,
        /// Second index slot (1-based value).
        j: Slot,
        /// Frame slot holding the pinned buffer pointer.
        ptr_slot: Slot,
        /// Frame slot holding the pinned length.
        len_slot: Slot,
        /// The float operation applied to the two loaded values.
        op: FOp,
    },
    /// FUSED two-load INTEGER binop: `frame[dst] = a[i-1] <op> b[j-1]`, the two
    /// 8-byte elements loaded from TWO (possibly distinct) pinned int buffers
    /// (each with its own pointer + length slot). The peephole collapses
    /// `ArrLoad(t1 = a[i]); ArrLoad(t2 = b[j]); {Add,Sub,Mul}(dst = t1 OP t2)`
    /// — the matrix-multiply / dot-product idiom `c += a[..] * b[..]` — when the
    /// two scratch loads are single-use, so the loaded i64s never round-trip the
    /// frame and three dispatches collapse to one. EITHER index out of bounds
    /// SIDE-EXITS before any effect, exactly like [`MicroOp::ArrLoad`]. The two
    /// buffers may be the SAME (one self-referential dot product) or distinct.
    ArrLoad2 {
        /// Destination slot.
        dst: Slot,
        /// First index slot (1-based value), addressing buffer `a`.
        i: Slot,
        /// Second index slot (1-based value), addressing buffer `b`.
        j: Slot,
        /// Frame slot holding the pinned pointer of the first buffer (`a`).
        ptr_a: Slot,
        /// Frame slot holding the pinned length of the first buffer (`a`).
        len_a: Slot,
        /// Frame slot holding the pinned pointer of the second buffer (`b`).
        ptr_b: Slot,
        /// Frame slot holding the pinned length of the second buffer (`b`).
        len_b: Slot,
        /// The integer operation applied to the two loaded values.
        op: IOp,
        /// Bounds-checked (`true`) or elided (`false`, the Oracle proved both
        /// indices in range). Unchecked has no out-of-bounds continuation.
        checked: bool,
    },
    /// Pinned-array store: `buffer[frame[idx] - 1] = frame[src]`; bounds
    /// side-exit BEFORE the store.
    ArrStore {
        /// Source slot.
        src: Slot,
        /// Index slot (1-based value).
        idx: Slot,
        /// Frame slot holding the pinned buffer pointer.
        ptr_slot: Slot,
        /// Frame slot holding the pinned length.
        len_slot: Slot,
        /// 1-byte elements (Bool buffers; stores normalize to 0/1).
        byte: bool,
        /// 4-byte SIGN-EXTENDED Int elements (`ListRepr::IntsI32`, the VM's
        /// `LOGOS_NARROW_VM` half-width buffers): a store TRUNCATES the value to
        /// its low 4 bytes (lossless under the narrowing proof). Mutually
        /// exclusive with `byte`; both `false` ⇒ the default 8-byte element.
        narrow32: bool,
        /// Bounds-checked (`true`) or elided (`false`, the Oracle proved
        /// the index in range). Unchecked stores have no side-exit.
        checked: bool,
    },
    /// FUSED read-modify-write on a pinned 8-byte int array:
    /// `buffer[frame[idx] - 1] = buffer[frame[idx] - 1] <op> frame[operand]`
    /// in ONE stencil. The peephole collapses `ArrLoad(t = arr[idx]);
    /// <int ALU>(t2 = t OP operand); ArrStore(arr[idx] = t2)` when both array
    /// ops hit the SAME pinned buffer + index and the two scratch values
    /// (`t`, `t2`) are single-use — so the element never round-trips the frame
    /// and ONE bounds check covers the load+store. Out-of-bounds (incl.
    /// 0/negative) SIDE-EXITS before any effect, exactly like [`MicroOp::ArrStore`].
    ArrRMW {
        /// Index slot (1-based value).
        idx: Slot,
        /// Operand slot (the RHS of the op; `frame[operand]`).
        operand: Slot,
        /// Frame slot holding the pinned buffer pointer.
        ptr_slot: Slot,
        /// Frame slot holding the pinned length.
        len_slot: Slot,
        /// The integer operation applied in place.
        op: RmwOp,
        /// Bounds-checked (`true`) or elided (`false`). Unchecked has no
        /// out-of-bounds continuation.
        checked: bool,
    },
    /// FUSED conditional adjacent swap on a pinned 8-byte int array:
    /// `let a = buf[idx1-1]; let b = buf[idx2-1]; if cmp(a, b) { buf[idx1-1] = b;
    /// buf[idx2-1] = a }` in ONE stencil. The peephole collapses the sort
    /// inner-loop idiom `ArrLoad(a,i1); ArrLoad(b,i2); Branch(cmp,a,b,skip);
    /// ArrStore(i1,b); ArrStore(i2,a)` (bubble/insertion/quick sort) — 5 ops → 1,
    /// the two loaded values never round-trip the frame, the compare-branch is
    /// gone. The swap is ATOMIC (both writes or neither); a checked variant
    /// bounds-checks BOTH indices up front and SIDE-EXITS before any effect, the
    /// unchecked variant (Oracle-proven indices) has no side-exit.
    ArrCondSwap {
        /// First index slot (1-based value).
        idx1: Slot,
        /// Second index slot (1-based value).
        idx2: Slot,
        /// Frame slot holding the pinned buffer pointer.
        ptr_slot: Slot,
        /// Frame slot holding the pinned length.
        len_slot: Slot,
        /// Swap when `cmp(buf[idx1-1], buf[idx2-1])` is TRUE.
        cmp: Cmp,
        /// Bounds-checked (`true`) or elided (`false`).
        checked: bool,
    },
    /// FUSED unconditional adjacent swap on a pinned 8-byte int array:
    /// `let a = buf[idx1-1]; let b = buf[idx2-1]; buf[idx1-1] = b; buf[idx2-1] =
    /// a` in ONE stencil. The peephole collapses the `Let tmp = arr[i]; arr[i] =
    /// arr[j]; arr[j] = tmp` exchange (quicksort/heap_sort/mergesort partition &
    /// sift) — `ArrLoad(a,i); ArrLoad(b,j); ArrStore(i,b); ArrStore(j,a)` — 4 ops
    /// → 1. The swap is ATOMIC; a checked variant bounds-checks BOTH indices up
    /// front and SIDE-EXITS before any effect.
    ArrSwap {
        /// First index slot (1-based value).
        idx1: Slot,
        /// Second index slot (1-based value).
        idx2: Slot,
        /// Frame slot holding the pinned buffer pointer.
        ptr_slot: Slot,
        /// Frame slot holding the pinned length.
        len_slot: Slot,
        /// Bounds-checked (`true`) or elided (`false`).
        checked: bool,
    },
    /// FUSED float multiply-add: `frame[dst] = bits((f(a) * f(b)) + f(c))` — the
    /// peephole collapses `MulF(t = a*b); AddF(d = t + c)` (the product `t`
    /// single-use) into one stencil, cutting a dispatch on the float arithmetic
    /// chains that dominate the float cluster (nbody's `dz*dz + s`, dot
    /// products). The product and the add round SEPARATELY (`(a*b)+c`, two
    /// roundings) — NOT a hardware single-rounding FMA — so it stays bit-identical
    /// to the unfused `MulF`+`AddF`. Mem-form: every operand reads from the frame,
    /// so it preserves the threaded XMM pins (the caller only fuses when the
    /// operands are frame-resident, keeping it a spill-free win).
    FmaF {
        /// Destination slot.
        dst: Slot,
        /// First product operand slot.
        a: Slot,
        /// Second product operand slot.
        b: Slot,
        /// Addend slot.
        c: Slot,
    },
    /// Unconditional transfer to the micro-op at `target` (an index into the
    /// program; forward or backward).
    Jump {
        /// Micro-op index to transfer to.
        target: usize,
    },
    /// Transfer to `target` when frame\[cond\] is ZERO; fall through otherwise.
    JumpIfFalse {
        /// Condition slot (zero = jump).
        cond: Slot,
        /// Micro-op index to transfer to.
        target: usize,
    },
    /// Transfer to `target` when frame\[cond\] is NONZERO; fall through
    /// otherwise (the same brz stencil with swapped continuations).
    JumpIfTrue {
        /// Condition slot (nonzero = jump).
        cond: Slot,
        /// Micro-op index to transfer to.
        target: usize,
    },
    /// Terminate the chain, returning frame\[src\].
    Return {
        /// Slot whose value is returned.
        src: Slot,
    },
}

/// Compile errors — structural, found before any code is emitted.
#[derive(Debug, PartialEq, Eq)]
pub enum JitCompileError {
    /// The program is empty.
    Empty,
    /// Execution can run off the end: the final op must be Return or Jump.
    FallsOffTheEnd,
    /// A jump target is outside the program.
    BadJumpTarget {
        /// Index of the offending op.
        op_index: usize,
        /// The out-of-range target.
        target: usize,
    },
    /// Assembly failed (missing hole/patch/map errors).
    Assembly(String),
    /// An op the per-piece stencil tier has no lowering for (e.g.
    /// [`MicroOp::StrAppend`], which is emitted ONLY for the contiguous regalloc
    /// backend). The caller declines (falls back to bytecode), so this is never a
    /// hard failure — it just routes around the stencil tier.
    Unsupported(&'static str),
}

impl std::fmt::Display for JitCompileError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            JitCompileError::Empty => write!(f, "jit: empty program"),
            JitCompileError::FallsOffTheEnd => {
                write!(f, "jit: the final op must be Return or Jump")
            }
            JitCompileError::BadJumpTarget { op_index, target } => {
                write!(f, "jit: op {op_index} jumps to {target}, outside the program")
            }
            JitCompileError::Assembly(e) => write!(f, "jit: assembly failed: {e}"),
            JitCompileError::Unsupported(name) => {
                write!(f, "jit: the stencil tier has no lowering for {name}")
            }
        }
    }
}

impl std::error::Error for JitCompileError {}

/// What one chain run produced.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ChainOutcome {
    /// The chain reached its Return; this is the returned value.
    Return(i64),
    /// A checked op side-exited; the payload is the RAW status-cell value.
    /// `1` is the plain marker (replay from the entry state — every effect
    /// confined to the private frame). `(pc << 2) | 3` is a PRECISE exit:
    /// effects already landed, resume at bytecode `pc`.
    Deopt(i64),
}

impl ChainOutcome {
    /// The returned value; panics on [`ChainOutcome::Deopt`] — for tests and
    /// callers of chains that contain no checked ops.
    pub fn expect_return(self) -> i64 {
        match self {
            ChainOutcome::Return(v) => v,
            ChainOutcome::Deopt(_) => panic!("chain side-exited (deopt) — no return value"),
        }
    }

    /// True when the run side-exited.
    pub fn is_deopt(&self) -> bool {
        matches!(self, ChainOutcome::Deopt(_))
    }
}

/// A compiled chain plus its side-exit status cell (present only when the
/// program contains checked ops). The deopt stencil stores 1 through the
/// patched cell address; [`CompiledChain::run_with_frame`] reads-and-resets
/// it after every run.
#[derive(Debug)]
pub struct CompiledChain {
    chain: JitChain,
    status: Option<std::sync::Arc<AtomicI64>>,
    /// Extra cells the chain's machine code reads by baked address and that must
    /// therefore outlive the chain (the contiguous FUNCTION backend's self-call
    /// ENTRY cell: an `Arc<AtomicI64>` holding this chain's own entry address,
    /// loaded by every `CallSelf`/`CallSelfCopy` and written after mapping). The
    /// run/deopt contract is unchanged — these are pure keep-alives.
    #[allow(dead_code)]
    keepalive: Vec<std::sync::Arc<AtomicI64>>,
}

impl CompiledChain {
    /// Wrap a raw `JitChain` (from the contiguous regalloc backend) and its
    /// shared status cell as a runnable `CompiledChain` — same run/deopt
    /// contract as a stencil chain.
    pub(crate) fn from_chain(
        chain: JitChain,
        status: Option<std::sync::Arc<AtomicI64>>,
    ) -> Self {
        CompiledChain { chain, status, keepalive: Vec::new() }
    }

    /// Wrap a `JitChain` plus its status cell AND any extra cells the code reads
    /// by baked address (the self-call entry cell), which the `CompiledChain`
    /// then keeps alive for the chain's whole lifetime.
    pub(crate) fn from_chain_keepalive(
        chain: JitChain,
        status: Option<std::sync::Arc<AtomicI64>>,
        keepalive: Vec<std::sync::Arc<AtomicI64>>,
    ) -> Self {
        CompiledChain { chain, status, keepalive }
    }

    /// Run over the given frame (slot 0 = VM register 0, …) with a fresh
    /// operand stack.
    pub fn run_with_frame(&self, frame: &mut [i64]) -> ChainOutcome {
        let v = self.chain.run_with_frame(frame);
        if let Some(cell) = &self.status {
            let raw = cell.swap(0, Ordering::Relaxed);
            if raw != 0 {
                return ChainOutcome::Deopt(raw);
            }
        }
        ChainOutcome::Return(v)
    }

    /// The mapped code+pool bytes (diagnostics/tests).
    pub fn bytes(&self) -> &[u8] {
        self.chain.bytes()
    }

    /// See [`crate::buffer::JitChain::patch_marked`].
    pub fn patch_marked(&self, value: u64) -> Result<(), crate::JitError> {
        self.chain.patch_marked(value)
    }

    /// See [`crate::buffer::JitChain::has_patch_marks`].
    pub fn has_patch_marks(&self) -> bool {
        self.chain.has_patch_marks()
    }

    /// The runtime base address (diagnostics/tests).
    pub fn base(&self) -> u64 {
        self.chain.base()
    }

    /// Stencil pieces in the chain (diagnostics/tests).
    pub fn piece_count(&self) -> usize {
        self.chain.piece_count()
    }
}

/// Lower a straight-line micro-op program into one executable stencil chain.
///
/// Emit ONE memory-form piece (an op outside the variant families) with
/// explicit continuation and deopt labels — the pinned compiler's bridge
/// to the classic stencils. Pinned operands were spilled before this
/// piece and pinned destinations reload after it.
/// The fused read-modify-write stencil for an op + bounds-check mode. The
/// checked twins side-exit through cont 1 on out-of-bounds; the unchecked `_u`
/// twins have no length hole and no out-of-bounds continuation.
fn rmw_stencil(op: RmwOp, checked: bool) -> &'static crate::Stencil {
    match (op, checked) {
        (RmwOp::Add, true) => &ST_ARRRMW_ADD,
        (RmwOp::Add, false) => &ST_ARRRMW_ADD_U,
        (RmwOp::Sub, true) => &ST_ARRRMW_SUB,
        (RmwOp::Sub, false) => &ST_ARRRMW_SUB_U,
        (RmwOp::Mul, true) => &ST_ARRRMW_MUL,
        (RmwOp::Mul, false) => &ST_ARRRMW_MUL_U,
        (RmwOp::And, true) => &ST_ARRRMW_AND,
        (RmwOp::And, false) => &ST_ARRRMW_AND_U,
        (RmwOp::Or, true) => &ST_ARRRMW_OR,
        (RmwOp::Or, false) => &ST_ARRRMW_OR_U,
        (RmwOp::Xor, true) => &ST_ARRRMW_XOR,
        (RmwOp::Xor, false) => &ST_ARRRMW_XOR_U,
        (RmwOp::AddF, true) => &ST_ARRRMW_ADDF,
        (RmwOp::AddF, false) => &ST_ARRRMW_ADDF_U,
        (RmwOp::SubF, true) => &ST_ARRRMW_SUBF,
        (RmwOp::SubF, false) => &ST_ARRRMW_SUBF_U,
        (RmwOp::MulF, true) => &ST_ARRRMW_MULF,
        (RmwOp::MulF, false) => &ST_ARRRMW_MULF_U,
    }
}

/// The fused two-buffer integer-load binop stencil for an op + bounds-check
/// mode. The checked twins side-exit through cont 1 when EITHER index is out of
/// bounds; the unchecked `_u` twins have no length holes and no continuation.
fn ld2_int_stencil(op: IOp, checked: bool) -> &'static crate::Stencil {
    match (op, checked) {
        (IOp::Add, true) => &ST_ARRLD2_ADD,
        (IOp::Add, false) => &ST_ARRLD2_ADD_U,
        (IOp::Sub, true) => &ST_ARRLD2_SUB,
        (IOp::Sub, false) => &ST_ARRLD2_SUB_U,
        (IOp::Mul, true) => &ST_ARRLD2_MUL,
        (IOp::Mul, false) => &ST_ARRLD2_MUL_U,
    }
}

/// The fused affine-index load stencil for an index op + bounds-check mode. The
/// checked twins side-exit through cont 1 when the COMPUTED index is out of
/// bounds; the unchecked `_u` twins have no length hole and no continuation.
/// The pinned-array LOAD stencil for an element width: 1-byte (Bool, zero-ext),
/// 4-byte (`IntsI32`, sign-ext), or the default 8-byte (Int/Float raw bits).
/// `byte` and `narrow32` are mutually exclusive (the adapter never sets both).
fn arrld_stencil(byte: bool, narrow32: bool, checked: bool) -> &'static crate::Stencil {
    match (byte, narrow32, checked) {
        (true, _, true) => &ST_ARRLDB,
        (true, _, false) => &ST_ARRLDB_U,
        (_, true, true) => &ST_ARRLD_I32,
        (_, true, false) => &ST_ARRLD_I32_U,
        (false, false, true) => &ST_ARRLD,
        (false, false, false) => &ST_ARRLD_U,
    }
}

/// The pinned-array STORE stencil for an element width (see [`arrld_stencil`]).
fn arrst_stencil(byte: bool, narrow32: bool, checked: bool) -> &'static crate::Stencil {
    match (byte, narrow32, checked) {
        (true, _, true) => &ST_ARRSTB,
        (true, _, false) => &ST_ARRSTB_U,
        (_, true, true) => &ST_ARRST_I32,
        (_, true, false) => &ST_ARRST_I32_U,
        (false, false, true) => &ST_ARRST,
        (false, false, false) => &ST_ARRST_U,
    }
}

fn affine_stencil(op: AffOp, checked: bool) -> &'static crate::Stencil {
    match (op, checked) {
        (AffOp::None, true) => &ST_ARRLDAFF_NONE,
        (AffOp::None, false) => &ST_ARRLDAFF_NONE_U,
        (AffOp::Add, true) => &ST_ARRLDAFF_ADD,
        (AffOp::Add, false) => &ST_ARRLDAFF_ADD_U,
        (AffOp::Sub, true) => &ST_ARRLDAFF_SUB,
        (AffOp::Sub, false) => &ST_ARRLDAFF_SUB_U,
        (AffOp::Mul, true) => &ST_ARRLDAFF_MUL,
        (AffOp::Mul, false) => &ST_ARRLDAFF_MUL_U,
    }
}

/// The conditional-swap stencil for a comparison + bounds-check mode. The
/// peephole only ever emits the four orderings (Eq/NotEq swaps are nonsensical).
fn condswap_stencil(cmp: Cmp, checked: bool) -> &'static crate::Stencil {
    match (cmp, checked) {
        (Cmp::Gt, true) => &ST_ARRCONDSWAP_GT,
        (Cmp::Gt, false) => &ST_ARRCONDSWAP_GT_U,
        (Cmp::Lt, true) => &ST_ARRCONDSWAP_LT,
        (Cmp::Lt, false) => &ST_ARRCONDSWAP_LT_U,
        (Cmp::GtEq, true) => &ST_ARRCONDSWAP_GE,
        (Cmp::GtEq, false) => &ST_ARRCONDSWAP_GE_U,
        (Cmp::LtEq, true) => &ST_ARRCONDSWAP_LE,
        (Cmp::LtEq, false) => &ST_ARRCONDSWAP_LE_U,
        (Cmp::Eq | Cmp::NotEq, _) => unreachable!("cond-swap peephole never emits Eq/NotEq"),
    }
}

fn emit_mem_form(
    buf: &mut JitBuffer,
    op: &MicroOp,
    next: crate::buffer::Label,
    deopt_piece: usize,
    status: &Option<std::sync::Arc<AtomicI64>>,
) {
    match *op {
        MicroOp::Div { dst, lhs, rhs } | MicroOp::Mod { dst, lhs, rhs } => {
            let stencil = if matches!(op, MicroOp::Div { .. }) { &ST_DIV3C } else { &ST_MOD3C };
            buf.push_stencil(
                stencil,
                &[
                    HoleValue::Const(0, lhs as i64),
                    HoleValue::Const(1, rhs as i64),
                    HoleValue::Const(2, dst as i64),
                    HoleValue::Cont(0, next),
                    HoleValue::Cont(1, buf.label(deopt_piece)),
                ],
            );
        }
        MicroOp::DivPow2 { dst, lhs, k } => {
            buf.push_stencil(
                &ST_DIVPOW2,
                &[
                    HoleValue::Const(0, lhs as i64),
                    HoleValue::Const(1, k as i64),
                    HoleValue::Const(2, (1i64 << k) - 1),
                    HoleValue::Const(3, dst as i64),
                    HoleValue::Cont(0, next),
                ],
            );
        }
        MicroOp::MagicDivU { dst, lhs, magic, more, mul_back } => {
            buf.push_stencil(
                &ST_MAGICDIV,
                &[
                    HoleValue::Const(0, lhs as i64),
                    HoleValue::Const(1, magic as i64),
                    HoleValue::Const(2, more as i64),
                    HoleValue::Const(3, mul_back),
                    HoleValue::Const(4, dst as i64),
                    HoleValue::Cont(0, next),
                ],
            );
        }
        MicroOp::AddF { dst, lhs, rhs }
        | MicroOp::SubF { dst, lhs, rhs }
        | MicroOp::MulF { dst, lhs, rhs } => {
            let stencil = match op {
                MicroOp::AddF { .. } => &ST_ADDF3,
                MicroOp::SubF { .. } => &ST_SUBF3,
                _ => &ST_MULF3,
            };
            buf.push_stencil(
                stencil,
                &[
                    HoleValue::Const(0, lhs as i64),
                    HoleValue::Const(1, rhs as i64),
                    HoleValue::Const(2, dst as i64),
                    HoleValue::Cont(0, next),
                ],
            );
        }
        MicroOp::DivF { dst, lhs, rhs } => {
            buf.push_stencil(
                &ST_DIVF3C,
                &[
                    HoleValue::Const(0, lhs as i64),
                    HoleValue::Const(1, rhs as i64),
                    HoleValue::Const(2, dst as i64),
                    HoleValue::Cont(0, next),
                    HoleValue::Cont(1, buf.label(deopt_piece)),
                ],
            );
        }
        MicroOp::LtF { dst, lhs, rhs }
        | MicroOp::LtEqF { dst, lhs, rhs }
        | MicroOp::EqF { dst, lhs, rhs }
        | MicroOp::NeqF { dst, lhs, rhs } => {
            let stencil = match op {
                MicroOp::LtF { .. } => &ST_LTF3,
                MicroOp::LtEqF { .. } => &ST_LEF3,
                MicroOp::EqF { .. } => &ST_EQF3,
                _ => &ST_NEF3,
            };
            buf.push_stencil(
                stencil,
                &[
                    HoleValue::Const(0, lhs as i64),
                    HoleValue::Const(1, rhs as i64),
                    HoleValue::Const(2, dst as i64),
                    HoleValue::Cont(0, next),
                ],
            );
        }
        // a > b ⇔ b < a; a >= b ⇔ b <= a (Lever 2a): a VALUE-form float ordering
        // compare lowers mem-form by SWAPPING operands and reusing the LtF/LeF
        // stencil — IEEE-exact, NaN-false on both sides of the swap, bit-identical
        // to the tree-walker. (Float EQUALITY is epsilon-fuzzy and stays on its
        // own EqF/NeqF path — never swapped here.) This gives the pinned compiler
        // a lowering for `GtF`/`GtEqF`, so a region carrying a value-form float
        // `>`/`>=` no longer has to drop ALL its XMM pins.
        MicroOp::GtF { dst, lhs, rhs } | MicroOp::GtEqF { dst, lhs, rhs } => {
            let stencil = match op {
                MicroOp::GtF { .. } => &ST_LTF3,
                _ => &ST_LEF3,
            };
            buf.push_stencil(
                stencil,
                &[
                    HoleValue::Const(0, rhs as i64),
                    HoleValue::Const(1, lhs as i64),
                    HoleValue::Const(2, dst as i64),
                    HoleValue::Cont(0, next),
                ],
            );
        }
        MicroOp::NotInt { dst, src } | MicroOp::NotBool { dst, src }
        | MicroOp::IntToFloat { dst, src } | MicroOp::SqrtF { dst, src } => {
            let stencil = match op {
                MicroOp::NotInt { .. } => &ST_NOTI2,
                MicroOp::NotBool { .. } => &ST_NOTB2,
                MicroOp::SqrtF { .. } => &ST_SQRTF2,
                _ => &ST_I2F2,
            };
            buf.push_stencil(
                stencil,
                &[
                    HoleValue::Const(0, src as i64),
                    HoleValue::Const(1, dst as i64),
                    HoleValue::Cont(0, next),
                ],
            );
        }
        MicroOp::Call {
            dst,
            args_start,
            table_addr,
            depth_addr,
            status_addr,
            limit_slot,
            depth_limit,
        } => {
            debug_assert!(status.is_some(), "calls require the status cell");
            buf.push_stencil(
                &ST_CALL,
                &[
                    HoleValue::Const(0, table_addr),
                    HoleValue::Const(1, args_start as i64),
                    HoleValue::Const(2, dst as i64),
                    HoleValue::Const(3, depth_addr),
                    HoleValue::Const(4, status_addr),
                    HoleValue::Const(5, limit_slot as i64),
                    HoleValue::Const(6, depth_limit),
                    HoleValue::Cont(0, next),
                ],
            );
        }
        MicroOp::MapGet { dst, key, map_slot, helper_addr } => {
            buf.push_stencil(
                &ST_MAPHGET,
                &[
                    HoleValue::Const(0, map_slot as i64),
                    HoleValue::Const(1, key as i64),
                    HoleValue::Const(2, dst as i64),
                    HoleValue::Const(3, helper_addr),
                    HoleValue::Cont(0, next),
                    HoleValue::Cont(1, buf.label(deopt_piece)),
                ],
            );
        }
        MicroOp::MapSet { src, key, map_slot, helper_addr } => {
            buf.push_stencil(
                &ST_MAPHSET,
                &[
                    HoleValue::Const(0, map_slot as i64),
                    HoleValue::Const(1, key as i64),
                    HoleValue::Const(2, src as i64),
                    HoleValue::Const(3, helper_addr),
                    HoleValue::Cont(0, next),
                ],
            );
        }
        MicroOp::MemMem {
            h_ptr_slot,
            h_len_slot,
            n_ptr_slot,
            n_len_slot,
            needle_len_slot,
            i_slot,
            count_slot,
            helper_addr,
        } => {
            debug_assert!(status.is_some(), "memmem requires the status cell for its deopt exit");
            buf.push_stencil(
                &ST_MEMMEM,
                &[
                    HoleValue::Const(0, h_ptr_slot as i64),
                    HoleValue::Const(1, h_len_slot as i64),
                    HoleValue::Const(2, n_ptr_slot as i64),
                    HoleValue::Const(3, n_len_slot as i64),
                    HoleValue::Const(4, needle_len_slot as i64),
                    HoleValue::Const(5, i_slot as i64),
                    HoleValue::Const(6, count_slot as i64),
                    HoleValue::Const(7, helper_addr),
                    HoleValue::Cont(0, next),
                    HoleValue::Cont(1, buf.label(deopt_piece)),
                ],
            );
        }
        MicroOp::CallSelf { dst, args_start, depth_addr, status_addr, limit_slot, frame_size } => {
            buf.push_stencil(
                &ST_CALL_SELF,
                &[
                    HoleValue::Const(0, 0),
                    HoleValue::Const(1, args_start as i64),
                    HoleValue::Const(2, dst as i64),
                    HoleValue::Const(3, depth_addr),
                    HoleValue::Const(4, status_addr),
                    HoleValue::Const(5, limit_slot as i64),
                    HoleValue::Const(6, frame_size),
                    HoleValue::Cont(0, next),
                ],
            );
            buf.mark_patch_hole(0);
        }
        MicroOp::CallSelfCopy {
            dst,
            args_start,
            src_start,
            arg_count,
            depth_addr,
            status_addr,
            limit_slot,
            frame_size,
        } => {
            buf.push_stencil(
                &ST_CALL_SELF_COPY,
                &[
                    HoleValue::Const(0, 0),
                    HoleValue::Const(1, args_start as i64),
                    HoleValue::Const(2, dst as i64),
                    HoleValue::Const(3, depth_addr),
                    HoleValue::Const(4, status_addr),
                    HoleValue::Const(5, limit_slot as i64),
                    HoleValue::Const(6, frame_size),
                    HoleValue::Const(7, src_start as i64),
                    HoleValue::Const(8, arg_count as i64),
                    HoleValue::Cont(0, next),
                ],
            );
            buf.mark_patch_hole(0);
        }
        MicroOp::NewList { vec_slot, ptr_slot, len_slot, helper_addr } => {
            buf.push_stencil(
                &ST_ALLOCLIST,
                &[
                    HoleValue::Const(0, vec_slot as i64),
                    HoleValue::Const(1, ptr_slot as i64),
                    HoleValue::Const(2, len_slot as i64),
                    HoleValue::Const(3, helper_addr),
                    HoleValue::Cont(0, next),
                ],
            );
        }
        MicroOp::ListTriple { handle_slot, vec_slot, ptr_slot, len_slot, helper_addr } => {
            buf.push_stencil(
                &ST_LISTTRIPLE,
                &[
                    HoleValue::Const(0, handle_slot as i64),
                    HoleValue::Const(1, vec_slot as i64),
                    HoleValue::Const(2, ptr_slot as i64),
                    HoleValue::Const(3, len_slot as i64),
                    HoleValue::Const(4, helper_addr),
                    HoleValue::Cont(0, next),
                ],
            );
        }
        MicroOp::MapHas { dst, key, map_slot, helper_addr } => {
            buf.push_stencil(
                &ST_MAPHHAS,
                &[
                    HoleValue::Const(0, map_slot as i64),
                    HoleValue::Const(1, key as i64),
                    HoleValue::Const(2, dst as i64),
                    HoleValue::Const(3, helper_addr),
                    HoleValue::Cont(0, next),
                ],
            );
        }
        // Array load/store: 1-based, bounds-checked. A failed bound jumps to
        // the shared deopt terminal (region replay / function side-exit).
        // The pinned mem-form path has already spilled the pinned operands
        // (idx/ptr/len/src) to the frame, so these read frame slots exactly
        // like the unpinned chain.
        MicroOp::ArrLoad { dst, idx, ptr_slot, len_slot, byte, narrow32, checked } => {
            if checked {
                buf.push_stencil(
                    arrld_stencil(byte, narrow32, true),
                    &[
                        HoleValue::Const(0, idx as i64),
                        HoleValue::Const(1, ptr_slot as i64),
                        HoleValue::Const(2, len_slot as i64),
                        HoleValue::Const(3, dst as i64),
                        HoleValue::Cont(0, next),
                        HoleValue::Cont(1, buf.label(deopt_piece)),
                    ],
                );
            } else {
                buf.push_stencil(
                    arrld_stencil(byte, narrow32, false),
                    &[
                        HoleValue::Const(0, idx as i64),
                        HoleValue::Const(1, ptr_slot as i64),
                        HoleValue::Const(3, dst as i64),
                        HoleValue::Cont(0, next),
                    ],
                );
            }
        }
        MicroOp::ArrLoadAffine { dst, a, op, b, const_offset, ptr_slot, len_slot, checked } => {
            if checked {
                buf.push_stencil(
                    affine_stencil(op, true),
                    &[
                        HoleValue::Const(0, a as i64),
                        HoleValue::Const(1, b as i64),
                        HoleValue::Const(2, const_offset),
                        HoleValue::Const(3, ptr_slot as i64),
                        HoleValue::Const(4, len_slot as i64),
                        HoleValue::Const(5, dst as i64),
                        HoleValue::Cont(0, next),
                        HoleValue::Cont(1, buf.label(deopt_piece)),
                    ],
                );
            } else {
                buf.push_stencil(
                    affine_stencil(op, false),
                    &[
                        HoleValue::Const(0, a as i64),
                        HoleValue::Const(1, b as i64),
                        HoleValue::Const(2, const_offset),
                        HoleValue::Const(3, ptr_slot as i64),
                        HoleValue::Const(5, dst as i64),
                        HoleValue::Cont(0, next),
                    ],
                );
            }
        }
        MicroOp::ArrLoad2F { dst, i: ix, j: jx, ptr_slot, len_slot, op } => {
            let stencil = match op {
                FOp::Add => &ST_ARRLD2_ADDF,
                FOp::Sub => &ST_ARRLD2_SUBF,
                FOp::Mul => &ST_ARRLD2_MULF,
            };
            buf.push_stencil(
                stencil,
                &[
                    HoleValue::Const(0, ix as i64),
                    HoleValue::Const(1, jx as i64),
                    HoleValue::Const(2, ptr_slot as i64),
                    HoleValue::Const(3, len_slot as i64),
                    HoleValue::Const(4, dst as i64),
                    HoleValue::Cont(0, next),
                    HoleValue::Cont(1, buf.label(deopt_piece)),
                ],
            );
        }
        MicroOp::ArrLoad2 { dst, i: ix, j: jx, ptr_a, len_a, ptr_b, len_b, op, checked } => {
            if checked {
                buf.push_stencil(
                    ld2_int_stencil(op, true),
                    &[
                        HoleValue::Const(0, ix as i64),
                        HoleValue::Const(1, jx as i64),
                        HoleValue::Const(2, ptr_a as i64),
                        HoleValue::Const(3, len_a as i64),
                        HoleValue::Const(4, ptr_b as i64),
                        HoleValue::Const(5, len_b as i64),
                        HoleValue::Const(6, dst as i64),
                        HoleValue::Cont(0, next),
                        HoleValue::Cont(1, buf.label(deopt_piece)),
                    ],
                );
            } else {
                buf.push_stencil(
                    ld2_int_stencil(op, false),
                    &[
                        HoleValue::Const(0, ix as i64),
                        HoleValue::Const(1, jx as i64),
                        HoleValue::Const(2, ptr_a as i64),
                        HoleValue::Const(3, ptr_b as i64),
                        HoleValue::Const(4, dst as i64),
                        HoleValue::Cont(0, next),
                    ],
                );
            }
        }
        MicroOp::ArrStore { src, idx, ptr_slot, len_slot, byte, narrow32, checked } => {
            if checked {
                buf.push_stencil(
                    arrst_stencil(byte, narrow32, true),
                    &[
                        HoleValue::Const(0, idx as i64),
                        HoleValue::Const(1, ptr_slot as i64),
                        HoleValue::Const(2, len_slot as i64),
                        HoleValue::Const(3, src as i64),
                        HoleValue::Cont(0, next),
                        HoleValue::Cont(1, buf.label(deopt_piece)),
                    ],
                );
            } else {
                buf.push_stencil(
                    arrst_stencil(byte, narrow32, false),
                    &[
                        HoleValue::Const(0, idx as i64),
                        HoleValue::Const(1, ptr_slot as i64),
                        HoleValue::Const(3, src as i64),
                        HoleValue::Cont(0, next),
                    ],
                );
            }
        }
        MicroOp::ArrRMW { idx, operand, ptr_slot, len_slot, op, checked } => {
            if checked {
                buf.push_stencil(
                    rmw_stencil(op, true),
                    &[
                        HoleValue::Const(0, idx as i64),
                        HoleValue::Const(1, ptr_slot as i64),
                        HoleValue::Const(2, len_slot as i64),
                        HoleValue::Const(3, operand as i64),
                        HoleValue::Cont(0, next),
                        HoleValue::Cont(1, buf.label(deopt_piece)),
                    ],
                );
            } else {
                buf.push_stencil(
                    rmw_stencil(op, false),
                    &[
                        HoleValue::Const(0, idx as i64),
                        HoleValue::Const(1, ptr_slot as i64),
                        HoleValue::Const(3, operand as i64),
                        HoleValue::Cont(0, next),
                    ],
                );
            }
        }
        MicroOp::ArrCondSwap { idx1, idx2, ptr_slot, len_slot, cmp, checked } => {
            if checked {
                buf.push_stencil(
                    condswap_stencil(cmp, true),
                    &[
                        HoleValue::Const(0, idx1 as i64),
                        HoleValue::Const(1, idx2 as i64),
                        HoleValue::Const(2, ptr_slot as i64),
                        HoleValue::Const(3, len_slot as i64),
                        HoleValue::Cont(0, next),
                        HoleValue::Cont(1, buf.label(deopt_piece)),
                    ],
                );
            } else {
                buf.push_stencil(
                    condswap_stencil(cmp, false),
                    &[
                        HoleValue::Const(0, idx1 as i64),
                        HoleValue::Const(1, idx2 as i64),
                        HoleValue::Const(2, ptr_slot as i64),
                        HoleValue::Cont(0, next),
                    ],
                );
            }
        }
        MicroOp::ArrSwap { idx1, idx2, ptr_slot, len_slot, checked } => {
            if checked {
                buf.push_stencil(
                    &ST_ARRSWAP,
                    &[
                        HoleValue::Const(0, idx1 as i64),
                        HoleValue::Const(1, idx2 as i64),
                        HoleValue::Const(2, ptr_slot as i64),
                        HoleValue::Const(3, len_slot as i64),
                        HoleValue::Cont(0, next),
                        HoleValue::Cont(1, buf.label(deopt_piece)),
                    ],
                );
            } else {
                buf.push_stencil(
                    &ST_ARRSWAP_U,
                    &[
                        HoleValue::Const(0, idx1 as i64),
                        HoleValue::Const(1, idx2 as i64),
                        HoleValue::Const(2, ptr_slot as i64),
                        HoleValue::Cont(0, next),
                    ],
                );
            }
        }
        MicroOp::FmaF { dst, a, b, c } => {
            buf.push_stencil(
                &ST_FMAF,
                &[
                    HoleValue::Const(0, a as i64),
                    HoleValue::Const(1, b as i64),
                    HoleValue::Const(2, c as i64),
                    HoleValue::Const(3, dst as i64),
                    HoleValue::Cont(0, next),
                ],
            );
        }
        // Helper-call push; refreshes the pinned ptr/len after a possible
        // realloc (the mem-form path reloads them from the frame after).
        MicroOp::ArrPush { src, vec_slot, ptr_slot, len_slot, helper_addr, byte: _, narrow32: _ } => {
            buf.push_stencil(
                &ST_PUSH,
                &[
                    HoleValue::Const(0, src as i64),
                    HoleValue::Const(1, vec_slot as i64),
                    HoleValue::Const(2, ptr_slot as i64),
                    HoleValue::Const(3, len_slot as i64),
                    HoleValue::Const(4, helper_addr),
                    HoleValue::Cont(0, next),
                ],
            );
        }
        ref other => unreachable!("emit_mem_form: unsupported op {other:?} (pinned chains exclude it)"),
    }
}

/// REGISTER-THREADING compilation (EXODIA 3.1): up to four frame slots are
/// PINNED into the threaded registers r0..r3 for the whole chain. Pinned
/// operands ride registers through the generated location-variant
/// stencils; ops outside the variant families (Div, arrays, calls, …)
/// spill the pinned operands they read and reload the pinned slots they
/// write, so every memory-form piece still sees a coherent frame.
///
/// Frame-coherence contract: a pinned slot's FRAME cell is stale between
/// its spills — callers must only use pinning where nothing outside the
/// chain reads the frame mid-run (mode-A functions: replay re-enters from
/// the boundary arguments and the result returns by register).
pub fn compile_straightline_pinned(
    ops: &[MicroOp],
    pins: &[u16],
) -> Result<CompiledChain, JitCompileError> {
    compile_straightline_pinned_with(ops, pins, None)
}

/// [`compile_straightline_pinned`] with a SHARED status cell (the function
/// tier's deopt/call seam). Integer/bool slots only — no XMM float pins.
pub fn compile_straightline_pinned_with(
    ops: &[MicroOp],
    pins: &[u16],
    shared_status: Option<std::sync::Arc<AtomicI64>>,
) -> Result<CompiledChain, JitCompileError> {
    compile_straightline_pinned_float(ops, pins, &[], shared_status)
}

/// The full register-threading compiler with BOTH integer pins (`pins`,
/// threaded through the GP registers r0..r3) and FLOAT pins (`fpins`,
/// threaded through the XMM registers f0..f3). The two budgets are
/// independent — the threaded ABI carries `fn(base, sp, r0..r3, f0..f3)`, so
/// base/sp consume 2 of the 6 GP arg registers (leaving r0..r3) while all
/// four XMM pins sit in the 8 free XMM arg registers and never compete with
/// the integer pins.
///
/// A FLOAT slot pinned to `fN` keeps its f64 live in that XMM register across
/// every stencil — the pure float arith variants (V_FBINOP) read and write it
/// directly, and the mem-form float stencils (sqrt, divf, the float compares,
/// array load/store) THREAD f0..f3 unchanged, so any float pin they do not
/// themselves touch survives with no frame traffic. The ones a mem-form op
/// does read/write are spilled before / reloaded after, exactly like the GP
/// pins.
pub fn compile_straightline_pinned_float(
    ops: &[MicroOp],
    pins: &[u16],
    fpins: &[u16],
    shared_status: Option<std::sync::Arc<AtomicI64>>,
) -> Result<CompiledChain, JitCompileError> {
    if pins.is_empty() && fpins.is_empty() {
        return compile_straightline_coded(ops, shared_status, None, 0);
    }
    assert!(pins.len() <= 4, "at most four threaded GP registers");
    assert!(fpins.len() <= 6, "at most six threaded XMM registers (f0..f5)");
    debug_assert!(
        pins.iter().all(|p| !fpins.contains(p)),
        "a slot cannot be both GP- and float-pinned"
    );
    if ops.is_empty() {
        return Err(JitCompileError::Empty);
    }
    if !matches!(ops.last(), Some(MicroOp::Return { .. }) | Some(MicroOp::Jump { .. })) {
        return Err(JitCompileError::FallsOffTheEnd);
    }
    let loc = |slot: u16| -> usize {
        pins.iter().position(|&p| p == slot).map(|i| i + 1).unwrap_or(0)
    };
    // The XMM-pin location of a slot: 0 = frame, 1..4 = f0..f3.
    let floc = |slot: u16| -> usize {
        fpins.iter().position(|&p| p == slot).map(|i| i + 1).unwrap_or(0)
    };
    // Which pinned registers an op forces through MEMORY (spill before /
    // reload after) because it has no variant family.
    let mem_form_touch = |op: &MicroOp| -> (Vec<u16>, Vec<u16>) {
        let mut reads: Vec<u16> = Vec::new();
        let mut writes: Vec<u16> = Vec::new();
        match *op {
            MicroOp::Div { dst, lhs, rhs } | MicroOp::Mod { dst, lhs, rhs } => {
                reads.extend([lhs, rhs]);
                writes.push(dst);
            }
            MicroOp::DivPow2 { dst, lhs, .. } | MicroOp::MagicDivU { dst, lhs, .. } => {
                reads.push(lhs);
                writes.push(dst);
            }
            // Float add/sub/mul are register-threaded (V_FBINOP) — NOT
            // mem-form. DivF (div-by-zero side-exit) and the float comparisons
            // (no variant yet) stay mem-form.
            MicroOp::DivF { dst, lhs, rhs }
            | MicroOp::LtF { dst, lhs, rhs }
            | MicroOp::LtEqF { dst, lhs, rhs }
            | MicroOp::GtF { dst, lhs, rhs }
            | MicroOp::GtEqF { dst, lhs, rhs }
            | MicroOp::EqF { dst, lhs, rhs }
            | MicroOp::NeqF { dst, lhs, rhs } => {
                reads.extend([lhs, rhs]);
                writes.push(dst);
            }
            MicroOp::NotInt { dst, src }
            | MicroOp::NotBool { dst, src }
            | MicroOp::IntToFloat { dst, src }
            | MicroOp::SqrtF { dst, src } => {
                reads.push(src);
                writes.push(dst);
            }
            MicroOp::BranchF { lhs, rhs, .. } => reads.extend([lhs, rhs]),
            // ptr_slot/len_slot are read DIRECTLY from the frame by the array
            // stencil (or, when the array's base pointer is GP-pinned, from its
            // register via the RPTR variant) — never through the threaded-pin
            // spill path, so they are NOT listed as spill-reads here. (They are
            // loop-invariant; the array never moves unless pushed-to, and a
            // pushed array is not ptr-pinned.)
            MicroOp::ArrLoad { dst, idx, .. } => {
                reads.push(idx);
                writes.push(dst);
            }
            MicroOp::ArrLoadAffine { dst, a, op, b, .. } => {
                reads.push(a);
                if op != AffOp::None {
                    reads.push(b);
                }
                writes.push(dst);
            }
            MicroOp::ArrLoad2F { dst, i: ix, j: jx, .. } => {
                reads.extend([ix, jx]);
                writes.push(dst);
            }
            MicroOp::ArrLoad2 { dst, i: ix, j: jx, .. } => {
                reads.extend([ix, jx]);
                writes.push(dst);
            }
            MicroOp::ArrStore { src, idx, .. } => {
                reads.extend([src, idx]);
            }
            MicroOp::ArrRMW { idx, operand, .. } => {
                reads.extend([idx, operand]);
            }
            MicroOp::ArrCondSwap { idx1, idx2, .. } => {
                reads.extend([idx1, idx2]);
            }
            MicroOp::ArrSwap { idx1, idx2, .. } => {
                reads.extend([idx1, idx2]);
            }
            MicroOp::FmaF { dst, a, b, c } => {
                reads.extend([a, b, c]);
                writes.push(dst);
            }
            MicroOp::ArrPush { src, vec_slot, ptr_slot, len_slot, .. } => {
                reads.extend([src, vec_slot, ptr_slot, len_slot]);
                writes.extend([ptr_slot, len_slot]);
            }
            MicroOp::MapGet { dst, key, map_slot, .. } => {
                reads.extend([key, map_slot]);
                writes.push(dst);
            }
            MicroOp::MapSet { src, key, map_slot, .. } => {
                reads.extend([src, key, map_slot]);
            }
            MicroOp::MapHas { dst, key, map_slot, .. } => {
                reads.extend([key, map_slot]);
                writes.push(dst);
            }
            MicroOp::MemMem {
                h_ptr_slot,
                h_len_slot,
                n_ptr_slot,
                n_len_slot,
                needle_len_slot,
                i_slot,
                count_slot,
                ..
            } => {
                // The helper reads every input slot from the frame and writes
                // `i_slot`/`count_slot` back — so a pinned operand must spill
                // first and a pinned destination reload after. (The recognizer
                // runs MemMem regions UNPINNED, so in practice these retain to
                // empty; kept for correctness if a pin ever survives.)
                reads.extend([
                    h_ptr_slot,
                    h_len_slot,
                    n_ptr_slot,
                    n_len_slot,
                    needle_len_slot,
                    i_slot,
                    count_slot,
                ]);
                writes.extend([i_slot, count_slot]);
            }
            MicroOp::NewList { vec_slot, ptr_slot, len_slot, .. } => {
                writes.extend([vec_slot, ptr_slot, len_slot]);
            }
            MicroOp::ListTriple { handle_slot, vec_slot, ptr_slot, len_slot, .. } => {
                reads.push(handle_slot);
                writes.extend([vec_slot, ptr_slot, len_slot]);
            }
            MicroOp::Call { dst, .. } | MicroOp::CallSelf { dst, .. } => {
                // The callee window was staged through ordinary Moves; the
                // result lands in the frame.
                writes.push(dst);
            }
            MicroOp::CallSelfCopy { dst, src_start, arg_count, .. } => {
                // The fused stencil reads the SOURCE block from the FRAME, so
                // any source slot held in a pinned register must spill to its
                // frame cell before the copy; the result lands in the frame.
                reads.extend((src_start..src_start + arg_count).collect::<Vec<_>>());
                writes.push(dst);
            }
            _ => {}
        }
        // A touched slot needs spill/reload when it is pinned in EITHER
        // budget (GP r0..r3 or XMM f0..f3) — the spill/reload primitives
        // dispatch on which.
        let pinned = |s: u16| loc(s) != 0 || floc(s) != 0;
        reads.retain(|&r| pinned(r));
        reads.dedup();
        writes.retain(|&w| pinned(w));
        writes.dedup();
        (reads, writes)
    };
    let has_variant = |op: &MicroOp| -> bool {
        matches!(
            op,
            MicroOp::Add { .. }
                | MicroOp::Sub { .. }
                | MicroOp::Mul { .. }
                | MicroOp::BitAnd { .. }
                | MicroOp::BitOr { .. }
                | MicroOp::BitXor { .. }
                | MicroOp::Shl { .. }
                | MicroOp::Shr { .. }
                | MicroOp::Lt { .. }
                | MicroOp::LtEq { .. }
                | MicroOp::Eq { .. }
                | MicroOp::Neq { .. }
                | MicroOp::Gt { .. }
                | MicroOp::GtEq { .. }
                | MicroOp::Move { .. }
                | MicroOp::LoadConst { .. }
                | MicroOp::Return { .. }
                | MicroOp::Branch { .. }
                // Float add/sub/mul/div + sqrt — register-threaded through the
                // XMM-pin variant tables (V_FBINOP / V_DIVF / V_SQRTF), one
                // piece each. DivF still side-exits on a zero divisor; its
                // variant carries the deopt continuation.
                | MicroOp::AddF { .. }
                | MicroOp::SubF { .. }
                | MicroOp::MulF { .. }
                | MicroOp::DivF { .. }
                | MicroOp::SqrtF { .. }
                | MicroOp::IntToFloat { .. }
                | MicroOp::BranchF { .. }
        )
    };

    let n_pin = pins.len() + fpins.len();
    // PASS 1: piece index per op (prologue reloads first).
    let mut op_piece: Vec<usize> = Vec::with_capacity(ops.len());
    let mut next_piece = n_pin;
    for op in ops {
        op_piece.push(next_piece);
        next_piece += match *op {
            // EPILOGUE (Phase 2 write-set pinning): a success exit spills every
            // pinned slot (GP and XMM) back to its frame cell so the VM's
            // write-back sees the loop-carried values — `n_pin` spill pieces +
            // the return.
            MicroOp::Return { .. } => n_pin + 1,
            _ if has_variant(op) => 1,
            MicroOp::Jump { .. } => 1,
            MicroOp::JumpIfFalse { cond, .. } | MicroOp::JumpIfTrue { cond, .. } => {
                if loc(cond) != 0 { 2 } else { 1 }
            }
            _ => {
                let (r, w) = mem_form_touch(op);
                r.len() + 1 + w.len()
            }
        };
    }
    let total_op_pieces = next_piece;
    let has_checked = ops.iter().any(|op| {
        matches!(
            op,
            // Integer add/sub/mul side-exit (`cont_1`) on signed overflow so the exact
            // tier promotes to BigInt — they need the status cell + deopt terminal.
            MicroOp::Add { .. }
                | MicroOp::Sub { .. }
                | MicroOp::Mul { .. }
                | MicroOp::Div { .. }
                | MicroOp::Mod { .. }
                | MicroOp::DivF { .. }
                | MicroOp::ArrLoad { .. }
                | MicroOp::ArrLoadAffine { checked: true, .. }
                | MicroOp::ArrLoad2F { .. }
                | MicroOp::ArrLoad2 { checked: true, .. }
                | MicroOp::ArrStore { .. }
                | MicroOp::ArrRMW { checked: true, .. }
                | MicroOp::ArrCondSwap { checked: true, .. }
                | MicroOp::ArrSwap { checked: true, .. }
                | MicroOp::Call { .. }
                | MicroOp::CallSelf { .. }
                | MicroOp::CallSelfCopy { .. }
                | MicroOp::MapGet { .. }
                | MicroOp::MemMem { .. }
        )
    });
    let status: Option<std::sync::Arc<AtomicI64>> = if has_checked {
        Some(shared_status.unwrap_or_else(|| std::sync::Arc::new(AtomicI64::new(0))))
    } else {
        None
    };
    let deopt_piece = total_op_pieces;

    // PASS 2: emit.
    let mut buf = JitBuffer::new();
    // Prologue: reload each pinned slot from the frame — the GP pins into
    // r0..r3 (V_MOV), then the float pins into f0..f3 (V_FMOV). The pieces
    // are laid out GP-first, so piece index `i` for `i < pins.len()` reloads
    // GP pin `i`, and `pins.len() + j` reloads float pin `j`.
    for (i, &slot) in pins.iter().enumerate() {
        buf.push_stencil(
            V_MOV[i + 1][0],
            &[HoleValue::Const(0, slot as i64), HoleValue::Cont(0, buf.label(i + 1))],
        );
    }
    for (j, &slot) in fpins.iter().enumerate() {
        let here = pins.len() + j;
        buf.push_stencil(
            V_FMOV[j + 1][0],
            &[HoleValue::Const(0, slot as i64), HoleValue::Cont(0, buf.label(here + 1))],
        );
    }
    // Spill/reload a pinned slot — dispatching on its budget. A GP pin moves
    // through V_MOV (r0..r3 ↔ frame i64); a float pin through V_FMOV (f0..f3
    // ↔ frame f64 bits). A slot is in exactly one budget.
    let spill = |buf: &mut JitBuffer, slot: u16, after: usize| {
        if floc(slot) != 0 {
            buf.push_stencil(
                V_FMOV[0][floc(slot)],
                &[HoleValue::Const(1, slot as i64), HoleValue::Cont(0, buf.label(after))],
            );
        } else {
            buf.push_stencil(
                V_MOV[0][loc(slot)],
                &[HoleValue::Const(1, slot as i64), HoleValue::Cont(0, buf.label(after))],
            );
        }
    };
    let reload = |buf: &mut JitBuffer, slot: u16, after: usize| {
        if floc(slot) != 0 {
            buf.push_stencil(
                V_FMOV[floc(slot)][0],
                &[HoleValue::Const(0, slot as i64), HoleValue::Cont(0, buf.label(after))],
            );
        } else {
            buf.push_stencil(
                V_MOV[loc(slot)][0],
                &[HoleValue::Const(0, slot as i64), HoleValue::Cont(0, buf.label(after))],
            );
        }
    };
    // Jump threading: a continuation that lands on an unconditional `Jump`
    // follows it straight to the target, so the predecessor branches directly
    // and the per-iteration back-edge jmp every loop pays (`…; Jump(top)`) is
    // skipped. The Jump piece is still EMITTED (left as unreferenced dead code)
    // so piece indices — and every `op_piece[..]` label — stay valid.
    let no_thread = std::env::var_os("LOGOS_NOTHREAD").is_some();
    let thread = |start: usize| -> usize {
        if no_thread {
            return start;
        }
        let mut idx = start;
        for _ in 0..=ops.len() {
            match ops.get(idx) {
                Some(MicroOp::Jump { target }) => idx = *target,
                _ => break,
            }
        }
        idx
    };
    for (i, op) in ops.iter().enumerate() {
        let here = op_piece[i];
        debug_assert_eq!(
            buf.pieces_pushed(),
            here,
            "PASS1/PASS2 piece divergence before op {i} ({op:?}); pins={pins:?} fpins={fpins:?}"
        );
        let next_op = op_piece.get(thread(i + 1)).copied().unwrap_or(total_op_pieces);
        let lbl = |t: usize| -> usize { op_piece[thread(t)] };
        match *op {
            MicroOp::LoadConst { dst, value } => {
                let d = loc(dst);
                if d == 0 {
                    buf.push_stencil(
                        &ST_CONSTST,
                        &[
                            HoleValue::Const(0, value),
                            HoleValue::Const(1, dst as i64),
                            HoleValue::Cont(0, buf.label(next_op)),
                        ],
                    );
                } else {
                    buf.push_stencil(
                        V_CONST[d - 1],
                        &[HoleValue::Const(0, value), HoleValue::Cont(0, buf.label(next_op))],
                    );
                }
            }
            MicroOp::Move { dst, src } => {
                let (fd, fs) = (floc(dst), floc(src));
                if fd != 0 || fs != 0 {
                    // FLOAT move: at least one operand rides an XMM pin, so this
                    // is an f64 copy — thread it through V_FMOV (register f→f, or
                    // a frame↔register half when the other end is frame-resident).
                    // A float-pinned slot is never GP-pinned, so `loc` plays no
                    // part. Register ends read/write the XMM directly (no hole);
                    // a frame end uses hole 0 (src) or hole 1 (dst).
                    let mut holes: Vec<HoleValue> = Vec::new();
                    if fs == 0 {
                        holes.push(HoleValue::Const(0, src as i64));
                    }
                    if fd == 0 {
                        holes.push(HoleValue::Const(1, dst as i64));
                    }
                    holes.push(HoleValue::Cont(0, buf.label(next_op)));
                    buf.push_stencil(V_FMOV[fd][fs], &holes);
                } else {
                    let (d, sl) = (loc(dst), loc(src));
                    if d == 0 && sl == 0 {
                        buf.push_stencil(
                            &ST_MOVSS,
                            &[
                                HoleValue::Const(0, src as i64),
                                HoleValue::Const(1, dst as i64),
                                HoleValue::Cont(0, buf.label(next_op)),
                            ],
                        );
                    } else {
                        let mut holes: Vec<HoleValue> = Vec::new();
                        if sl == 0 {
                            holes.push(HoleValue::Const(0, src as i64));
                        }
                        if d == 0 {
                            holes.push(HoleValue::Const(1, dst as i64));
                        }
                        holes.push(HoleValue::Cont(0, buf.label(next_op)));
                        buf.push_stencil(V_MOV[d][sl], &holes);
                    }
                }
            }
            MicroOp::Add { dst, lhs, rhs }
            | MicroOp::Sub { dst, lhs, rhs }
            | MicroOp::Mul { dst, lhs, rhs }
            | MicroOp::BitAnd { dst, lhs, rhs }
            | MicroOp::BitOr { dst, lhs, rhs }
            | MicroOp::BitXor { dst, lhs, rhs }
            | MicroOp::Shl { dst, lhs, rhs }
            | MicroOp::Shr { dst, lhs, rhs }
            | MicroOp::Lt { dst, lhs, rhs }
            | MicroOp::LtEq { dst, lhs, rhs }
            | MicroOp::Eq { dst, lhs, rhs }
            | MicroOp::Neq { dst, lhs, rhs }
            | MicroOp::Gt { dst, lhs, rhs }
            | MicroOp::GtEq { dst, lhs, rhs } => {
                // Gt/GtEq map onto lt/le with swapped operands; Neq is ne.
                let (fam, a, b) = match *op {
                    MicroOp::Add { .. } => (0usize, lhs, rhs),
                    MicroOp::Sub { .. } => (1, lhs, rhs),
                    MicroOp::Mul { .. } => (2, lhs, rhs),
                    MicroOp::BitAnd { .. } => (3, lhs, rhs),
                    MicroOp::BitOr { .. } => (4, lhs, rhs),
                    MicroOp::BitXor { .. } => (5, lhs, rhs),
                    MicroOp::Shl { .. } => (6, lhs, rhs),
                    MicroOp::Shr { .. } => (7, lhs, rhs),
                    MicroOp::Lt { .. } => (8, lhs, rhs),
                    MicroOp::Gt { .. } => (8, rhs, lhs),
                    MicroOp::LtEq { .. } => (9, lhs, rhs),
                    MicroOp::GtEq { .. } => (9, rhs, lhs),
                    MicroOp::Eq { .. } => (10, lhs, rhs),
                    MicroOp::Neq { .. } => (11, lhs, rhs),
                    _ => unreachable!(),
                };
                let (d, l, r) = (loc(dst), loc(a), loc(b));
                let mut holes: Vec<HoleValue> = Vec::new();
                if l == 0 {
                    holes.push(HoleValue::Const(0, a as i64));
                }
                if r == 0 {
                    holes.push(HoleValue::Const(1, b as i64));
                }
                if d == 0 {
                    holes.push(HoleValue::Const(2, dst as i64));
                }
                holes.push(HoleValue::Cont(0, buf.label(next_op)));
                // add/sub/mul (fam 0/1/2) are the CHECKED families: their generated
                // stencil takes cont_1 to the shared deopt terminal on signed overflow,
                // so the exact tier recomputes and promotes to BigInt.
                if fam <= 2 {
                    holes.push(HoleValue::Cont(1, buf.label(deopt_piece)));
                }
                buf.push_stencil(V_BINOP[fam][d][l][r], &holes);
            }
            // FLOAT 3-address (add/sub/mul): same location encoding, float
            // variant table.
            MicroOp::AddF { dst, lhs, rhs }
            | MicroOp::SubF { dst, lhs, rhs }
            | MicroOp::MulF { dst, lhs, rhs } => {
                let fam = match *op {
                    MicroOp::AddF { .. } => 0usize,
                    MicroOp::SubF { .. } => 1,
                    MicroOp::MulF { .. } => 2,
                    _ => unreachable!(),
                };
                // Float operands are XMM-pinned → index by the FLOAT pin
                // location (floc), NOT the GP location (loc). Using loc would
                // resolve an XMM-pinned float to frame (location 0) and read a
                // STALE frame cell instead of the live f-pin.
                let (d, l, r) = (floc(dst), floc(lhs), floc(rhs));
                let mut holes: Vec<HoleValue> = Vec::new();
                if l == 0 {
                    holes.push(HoleValue::Const(0, lhs as i64));
                }
                if r == 0 {
                    holes.push(HoleValue::Const(1, rhs as i64));
                }
                if d == 0 {
                    holes.push(HoleValue::Const(2, dst as i64));
                }
                holes.push(HoleValue::Cont(0, buf.label(next_op)));
                buf.push_stencil(V_FBINOP[fam][d][l][r], &holes);
            }
            // FLOAT sqrt threaded through XMM pins (no side-exit).
            MicroOp::SqrtF { dst, src } => {
                let (d, s) = (floc(dst), floc(src));
                let mut holes: Vec<HoleValue> = Vec::new();
                if s == 0 {
                    holes.push(HoleValue::Const(0, src as i64));
                }
                if d == 0 {
                    holes.push(HoleValue::Const(1, dst as i64));
                }
                holes.push(HoleValue::Cont(0, buf.label(next_op)));
                buf.push_stencil(V_SQRTF[d][s], &holes);
            }
            // FLOAT divide threaded through XMM pins; a zero divisor side-exits
            // to the deopt terminal (cont 1), exactly like the mem-form form.
            MicroOp::DivF { dst, lhs, rhs } => {
                let (d, l, r) = (floc(dst), floc(lhs), floc(rhs));
                let mut holes: Vec<HoleValue> = Vec::new();
                if l == 0 {
                    holes.push(HoleValue::Const(0, lhs as i64));
                }
                if r == 0 {
                    holes.push(HoleValue::Const(1, rhs as i64));
                }
                if d == 0 {
                    holes.push(HoleValue::Const(2, dst as i64));
                }
                holes.push(HoleValue::Cont(0, buf.label(next_op)));
                holes.push(HoleValue::Cont(1, buf.label(deopt_piece)));
                buf.push_stencil(V_DIVF[d][l][r], &holes);
            }
            // Int→Float threaded through XMM pins: int src at GP loc, f64 dst
            // at XMM pin.
            MicroOp::IntToFloat { dst, src } => {
                let (d, s) = (floc(dst), loc(src));
                let mut holes: Vec<HoleValue> = Vec::new();
                if s == 0 {
                    holes.push(HoleValue::Const(0, src as i64));
                }
                if d == 0 {
                    holes.push(HoleValue::Const(1, dst as i64));
                }
                holes.push(HoleValue::Cont(0, buf.label(next_op)));
                buf.push_stencil(V_I2F[d][s], &holes);
            }
            // FUSED float compare-and-branch threaded through XMM pins. Same
            // NaN-correct cmp→family mapping as the unpinned form (brlef for
            // <=/>=, never brltf-swapped — NaN makes `a<=b` ≠ `!(b<a)`).
            MicroOp::BranchF { cmp, lhs, rhs, target } => {
                let t = buf.label(lbl(target));
                let n = buf.label(next_op);
                let (fam, a, b) = match cmp {
                    Cmp::Lt => (0usize, lhs, rhs),
                    Cmp::Gt => (0, rhs, lhs),
                    Cmp::LtEq => (1, lhs, rhs),
                    Cmp::GtEq => (1, rhs, lhs),
                    Cmp::Eq => (2, lhs, rhs),
                    Cmp::NotEq => (2, lhs, rhs),
                };
                let (c0, c1) = if cmp == Cmp::NotEq { (t, n) } else { (n, t) };
                let (l, r) = (floc(a), floc(b));
                let mut holes: Vec<HoleValue> = Vec::new();
                if l == 0 {
                    holes.push(HoleValue::Const(0, a as i64));
                }
                if r == 0 {
                    holes.push(HoleValue::Const(1, b as i64));
                }
                holes.push(HoleValue::Cont(0, c0));
                holes.push(HoleValue::Cont(1, c1));
                buf.push_stencil(V_BRANCHF[fam][l][r], &holes);
            }
            MicroOp::Branch { cmp, lhs, rhs, target } => {
                // TRUE -> fall through (cont 0 of the variant is TAKEN when
                // the condition holds; mirror the unpinned mapping: jump
                // when FALSE).
                let t = buf.label(lbl(target));
                let n = buf.label(next_op);
                let (fam, a, b, c_true, c_false) = match cmp {
                    Cmp::Lt => (0usize, lhs, rhs, n, t),
                    Cmp::GtEq => (0, lhs, rhs, t, n),
                    Cmp::Gt => (0, rhs, lhs, n, t),
                    Cmp::LtEq => (0, rhs, lhs, t, n),
                    Cmp::Eq => (2, lhs, rhs, n, t),
                    Cmp::NotEq => (2, lhs, rhs, t, n),
                };
                let (l, r) = (loc(a), loc(b));
                let mut holes: Vec<HoleValue> = Vec::new();
                if l == 0 {
                    holes.push(HoleValue::Const(0, a as i64));
                }
                if r == 0 {
                    holes.push(HoleValue::Const(1, b as i64));
                }
                holes.push(HoleValue::Cont(0, c_true));
                holes.push(HoleValue::Cont(1, c_false));
                buf.push_stencil(V_BRANCH[fam][l][r], &holes);
            }
            MicroOp::Jump { target } => {
                buf.push_stencil(&ST_JUMP, &[HoleValue::Cont(0, buf.label(lbl(target)))]);
            }
            MicroOp::JumpIfFalse { cond, target } | MicroOp::JumpIfTrue { cond, target } => {
                let c = loc(cond);
                let mut brz_piece = here;
                if c != 0 {
                    spill(&mut buf, cond, here + 1);
                    brz_piece = here + 1;
                }
                let _ = brz_piece;
                let (c0, c1) = if matches!(op, MicroOp::JumpIfFalse { .. }) {
                    (buf.label(next_op), buf.label(lbl(target)))
                } else {
                    (buf.label(lbl(target)), buf.label(next_op))
                };
                buf.push_stencil(
                    &ST_BRZ,
                    &[HoleValue::Const(0, cond as i64), HoleValue::Cont(0, c0), HoleValue::Cont(1, c1)],
                );
            }
            MicroOp::Return { src } => {
                // EPILOGUE: spill every pinned slot to its frame cell before
                // returning, so the VM's success write-back (which reads the
                // frame) observes loop-carried values held in registers. Both
                // budgets spill — the GP pins through V_MOV, the float pins
                // through V_FMOV (the `spill` closure dispatches on which) —
                // matching the `n_pin + 1` pieces reserved for this return.
                // The deopt terminal is separate and discards the frame, so it
                // needs no spill — only this success path does.
                let mut cursor = here;
                for &slot in pins.iter().chain(fpins.iter()) {
                    spill(&mut buf, slot, cursor + 1);
                    cursor += 1;
                }
                let sl = loc(src);
                if sl == 0 {
                    buf.push_stencil(&ST_RET2, &[HoleValue::Const(0, src as i64)]);
                } else {
                    buf.push_stencil(V_RET[sl - 1], &[]);
                }
            }
            ref other => {
                // Memory-form op: spill pinned reads, run, reload pinned
                // writes — emitted as its own piece run.
                let (reads, writes) = mem_form_touch(other);
                let mut cursor = here;
                for &slot in &reads {
                    spill(&mut buf, slot, cursor + 1);
                    cursor += 1;
                }
                let after_op = cursor + 1;
                let next_for_op = if writes.is_empty() { next_op } else { after_op };
                let next_label = buf.label(next_for_op);
                // REGISTER-FORM array access: a word array whose base pointer is
                // GP-pinned reads the pointer from its register (no per-access
                // frame load). The index/length/value stay in the frame exactly
                // as the mem-form path, so the spill/reload bookkeeping above is
                // unchanged (mem_form_touch excludes ptr/len). Falls back to the
                // frame-pointer mem-form for byte arrays / unpinned pointers.
                let rptr = |buf: &mut JitBuffer, table: &[&'static crate::Stencil; 5],
                            n: usize, idx: u16, vslot: u16, len_slot: u16, checked: bool| {
                    let mut holes = vec![
                        HoleValue::Const(0, idx as i64),
                        HoleValue::Const(3, vslot as i64),
                        HoleValue::Cont(0, next_label),
                    ];
                    if checked {
                        holes.push(HoleValue::Const(2, len_slot as i64));
                        holes.push(HoleValue::Cont(1, buf.label(deopt_piece)));
                    }
                    buf.push_stencil(table[n], &holes);
                };
                let used_rptr = match *other {
                    MicroOp::ArrLoad { dst, idx, ptr_slot, len_slot, byte: false, narrow32: false, checked }
                        if loc(ptr_slot) != 0 =>
                    {
                        let n = loc(ptr_slot);
                        let t = if checked { &V_ARRLD_RPTR_C } else { &V_ARRLD_RPTR };
                        rptr(&mut buf, t, n, idx, dst, len_slot, checked);
                        true
                    }
                    MicroOp::ArrStore { src, idx, ptr_slot, len_slot, byte: false, narrow32: false, checked }
                        if loc(ptr_slot) != 0 =>
                    {
                        let n = loc(ptr_slot);
                        let t = if checked { &V_ARRST_RPTR_C } else { &V_ARRST_RPTR };
                        rptr(&mut buf, t, n, idx, src, len_slot, checked);
                        true
                    }
                    _ => false,
                };
                if !used_rptr {
                    emit_mem_form(&mut buf, other, next_label, deopt_piece, &status);
                }
                cursor += 1;
                for (k, &slot) in writes.iter().enumerate() {
                    let after = if k + 1 == writes.len() { next_op } else { cursor + 1 };
                    reload(&mut buf, slot, after);
                    cursor += 1;
                }
            }
        }
    }
    if let Some(cell) = &status {
        let addr = cell.as_ref() as *const AtomicI64 as i64;
        buf.push_stencil(&ST_DEOPT, &[HoleValue::Const(0, addr)]);
    }
    let chain = buf.finish().map_err(|e| JitCompileError::Assembly(e.to_string()))?;
    Ok(CompiledChain { chain, status, keepalive: Vec::new() })
}

/// The returned chain is run with [`CompiledChain::run_with_frame`]; the
/// frame's slots are the program's registers (inputs pre-loaded by the
/// caller, outputs visible after the run). Programs containing checked ops
/// (Div/Mod) get a status cell and a shared side-exit terminal; their runs
/// can report [`ChainOutcome::Deopt`].
pub fn compile_straightline(ops: &[MicroOp]) -> Result<CompiledChain, JitCompileError> {
    compile_straightline_with(ops, None)
}

/// [`compile_straightline`] with a caller-supplied (per-program) status
/// cell, so every chain of one program — and the call stencils between them
/// — share a single side-exit channel. The cell's address is what
/// `MicroOp::Call::status_addr` must carry.
pub fn compile_straightline_with(
    ops: &[MicroOp],
    shared_status: Option<std::sync::Arc<AtomicI64>>,
) -> Result<CompiledChain, JitCompileError> {
    compile_straightline_coded(ops, shared_status, None, 0)
}

/// Like [`compile_straightline_with`], with an optional per-op DEOPT CODE
/// table (parallel to `ops`). An op whose code is not the plain marker `1`
/// side-exits through its own terminal piece storing that encoded value —
/// the function tier encodes `(bytecode_pc << 2) | 3` so the VM can resume
/// precisely. `None` (and code `1`) keep the shared replay terminal.
pub fn compile_straightline_coded(
    ops: &[MicroOp],
    shared_status: Option<std::sync::Arc<AtomicI64>>,
    deopt_codes: Option<&[i64]>,
    depth_addr: i64,
) -> Result<CompiledChain, JitCompileError> {
    if ops.is_empty() {
        return Err(JitCompileError::Empty);
    }
    if !matches!(ops.last(), Some(MicroOp::Return { .. }) | Some(MicroOp::Jump { .. })) {
        return Err(JitCompileError::FallsOffTheEnd);
    }
    for (i, op) in ops.iter().enumerate() {
        match *op {
            MicroOp::Jump { target }
            | MicroOp::JumpIfFalse { target, .. }
            | MicroOp::JumpIfTrue { target, .. }
            | MicroOp::Branch { target, .. }
            | MicroOp::BranchF { target, .. } => {
                if target >= ops.len() {
                    return Err(JitCompileError::BadJumpTarget { op_index: i, target });
                }
            }
            _ => {}
        }
    }

    // 3-address lowering: exactly ONE piece per micro-op (piece index ==
    // op index), so jump targets need no remapping. Programs with checked
    // ops share ONE side-exit terminal placed after every op piece; its
    // patched constant is the status cell's address.
    let has_checked = ops.iter().any(|op| {
        matches!(
            op,
            // Integer add/sub/mul side-exit (`cont_1`) on signed overflow so the exact
            // tier promotes to BigInt — they need the status cell + deopt terminal.
            MicroOp::Add { .. }
                | MicroOp::Sub { .. }
                | MicroOp::Mul { .. }
                | MicroOp::Div { .. }
                | MicroOp::Mod { .. }
                | MicroOp::DivF { .. }
                | MicroOp::ArrLoad { .. }
                | MicroOp::ArrLoadAffine { checked: true, .. }
                | MicroOp::ArrLoad2F { .. }
                | MicroOp::ArrLoad2 { checked: true, .. }
                | MicroOp::ArrStore { .. }
                | MicroOp::ArrRMW { checked: true, .. }
                | MicroOp::ArrCondSwap { checked: true, .. }
                | MicroOp::ArrSwap { checked: true, .. }
                | MicroOp::Call { .. }
                | MicroOp::CallSelf { .. }
                | MicroOp::CallSelfCopy { .. }
                | MicroOp::MapGet { .. }
                | MicroOp::MemMem { .. }
        )
    });
    let status: Option<std::sync::Arc<AtomicI64>> = if has_checked {
        Some(shared_status.unwrap_or_else(|| std::sync::Arc::new(AtomicI64::new(0))))
    } else {
        None
    };
    let deopt_piece = ops.len();
    // Distinct non-plain codes get their own terminal pieces after the
    // shared one, in first-use order (deterministic).
    let mut code_pieces: Vec<i64> = Vec::new();
    if let Some(codes) = deopt_codes {
        debug_assert_eq!(codes.len(), ops.len());
        for (i, op) in ops.iter().enumerate() {
            let coded = matches!(
                op,
                // add/sub/mul are CHECKED now (overflow → exact promotion): inside a
                // region their side-exit must carry the op's PRECISE deopt code so the
                // VM resumes with the right rollback, exactly like the other checked
                // ops — so their codes need terminal pieces too.
                MicroOp::Add { .. }
                    | MicroOp::Sub { .. }
                    | MicroOp::Mul { .. }
                    | MicroOp::Div { .. }
                    | MicroOp::Mod { .. }
                    | MicroOp::DivF { .. }
                    | MicroOp::ArrLoad { .. }
                    | MicroOp::ArrLoadAffine { checked: true, .. }
                    | MicroOp::ArrLoad2F { .. }
                    | MicroOp::ArrLoad2 { checked: true, .. }
                    | MicroOp::ArrStore { .. }
                    | MicroOp::MapGet { .. }
            );
            if coded && codes[i] != 1 && !code_pieces.contains(&codes[i]) {
                code_pieces.push(codes[i]);
            }
        }
    }
    let piece_for = |code: i64| -> usize {
        if code == 1 {
            deopt_piece
        } else {
            deopt_piece + 1 + code_pieces.iter().position(|&c| c == code).unwrap()
        }
    };
    let code_at = |i: usize| -> i64 { deopt_codes.map(|c| c[i]).unwrap_or(1) };

    let mut buf = JitBuffer::new();
    for (i, op) in ops.iter().enumerate() {
        let next = buf.label(i + 1);
        match *op {
            MicroOp::LoadConst { dst, value } => {
                buf.push_stencil(
                    &ST_CONSTST,
                    &[
                        HoleValue::Const(0, value),
                        HoleValue::Const(1, dst as i64),
                        HoleValue::Cont(0, next),
                    ],
                );
            }
            MicroOp::Move { dst, src } => {
                buf.push_stencil(
                    &ST_MOVSS,
                    &[
                        HoleValue::Const(0, src as i64),
                        HoleValue::Const(1, dst as i64),
                        HoleValue::Cont(0, next),
                    ],
                );
            }
            MicroOp::Add { dst, lhs, rhs }
            | MicroOp::Sub { dst, lhs, rhs }
            | MicroOp::Mul { dst, lhs, rhs }
            | MicroOp::Lt { dst, lhs, rhs }
            | MicroOp::LtEq { dst, lhs, rhs }
            | MicroOp::Eq { dst, lhs, rhs }
            | MicroOp::Neq { dst, lhs, rhs } => {
                let stencil = match op {
                    MicroOp::Add { .. } => &ST_ADD3,
                    MicroOp::Sub { .. } => &ST_SUB3,
                    MicroOp::Mul { .. } => &ST_MUL3,
                    MicroOp::Lt { .. } => &ST_LT3,
                    MicroOp::LtEq { .. } => &ST_LE3,
                    MicroOp::Eq { .. } => &ST_EQ3,
                    MicroOp::Neq { .. } => &ST_NE3,
                    _ => unreachable!(),
                };
                let mut holes = vec![
                    HoleValue::Const(0, lhs as i64),
                    HoleValue::Const(1, rhs as i64),
                    HoleValue::Const(2, dst as i64),
                    HoleValue::Cont(0, next),
                ];
                // add/sub/mul are CHECKED: their stencil takes cont_1 to this op's
                // deopt terminal on signed overflow so the exact tier promotes. The
                // comparisons (lt/le/eq/ne) never overflow → cont_0 only.
                if matches!(op, MicroOp::Add { .. } | MicroOp::Sub { .. } | MicroOp::Mul { .. }) {
                    holes.push(HoleValue::Cont(1, buf.label(piece_for(code_at(i)))));
                }
                buf.push_stencil(stencil, &holes);
            }
            // a > b ⇔ b < a; a >= b ⇔ b <= a: swap operands, reuse lt3/le3
            // (IEEE-exact for the float twins too — NaN is false on both
            // sides of the swap).
            MicroOp::Gt { dst, lhs, rhs }
            | MicroOp::GtEq { dst, lhs, rhs }
            | MicroOp::GtF { dst, lhs, rhs }
            | MicroOp::GtEqF { dst, lhs, rhs } => {
                let stencil = match op {
                    MicroOp::Gt { .. } => &ST_LT3,
                    MicroOp::GtEq { .. } => &ST_LE3,
                    MicroOp::GtF { .. } => &ST_LTF3,
                    _ => &ST_LEF3,
                };
                buf.push_stencil(
                    stencil,
                    &[
                        HoleValue::Const(0, rhs as i64),
                        HoleValue::Const(1, lhs as i64),
                        HoleValue::Const(2, dst as i64),
                        HoleValue::Cont(0, next),
                    ],
                );
            }
            MicroOp::BitAnd { dst, lhs, rhs }
            | MicroOp::BitOr { dst, lhs, rhs }
            | MicroOp::BitXor { dst, lhs, rhs }
            | MicroOp::Shl { dst, lhs, rhs }
            | MicroOp::Shr { dst, lhs, rhs }
            | MicroOp::AddF { dst, lhs, rhs }
            | MicroOp::SubF { dst, lhs, rhs }
            | MicroOp::MulF { dst, lhs, rhs }
            | MicroOp::LtF { dst, lhs, rhs }
            | MicroOp::LtEqF { dst, lhs, rhs }
            | MicroOp::EqF { dst, lhs, rhs }
            | MicroOp::NeqF { dst, lhs, rhs } => {
                let stencil = match op {
                    MicroOp::BitAnd { .. } => &ST_AND3,
                    MicroOp::BitOr { .. } => &ST_OR3,
                    MicroOp::BitXor { .. } => &ST_XOR3,
                    MicroOp::Shl { .. } => &ST_SHL3,
                    MicroOp::Shr { .. } => &ST_SHR3,
                    MicroOp::AddF { .. } => &ST_ADDF3,
                    MicroOp::SubF { .. } => &ST_SUBF3,
                    MicroOp::MulF { .. } => &ST_MULF3,
                    MicroOp::LtF { .. } => &ST_LTF3,
                    MicroOp::LtEqF { .. } => &ST_LEF3,
                    MicroOp::EqF { .. } => &ST_EQF3,
                    _ => &ST_NEF3,
                };
                buf.push_stencil(
                    stencil,
                    &[
                        HoleValue::Const(0, lhs as i64),
                        HoleValue::Const(1, rhs as i64),
                        HoleValue::Const(2, dst as i64),
                        HoleValue::Cont(0, next),
                    ],
                );
            }
            MicroOp::DivF { dst, lhs, rhs } => {
                buf.push_stencil(
                    &ST_DIVF3C,
                    &[
                        HoleValue::Const(0, lhs as i64),
                        HoleValue::Const(1, rhs as i64),
                        HoleValue::Const(2, dst as i64),
                        HoleValue::Cont(0, next),
                        HoleValue::Cont(1, buf.label(piece_for(code_at(i)))),
                    ],
                );
            }
            MicroOp::Call {
                dst,
                args_start,
                table_addr,
                depth_addr,
                status_addr,
                limit_slot,
                depth_limit,
            } => {
                let code = code_at(i);
                if code != 1 {
                    // Precise variant: hole 6 carries the encoded deopt
                    // value; the depth limit is BAKED at the kernel's
                    // locked MAX_CALL_DEPTH (the adapter verified it).
                    debug_assert_eq!(depth_limit, BAKED_CALL_DEPTH);
                    buf.push_stencil(
                        &ST_CALL_PRECISE,
                        &[
                            HoleValue::Const(0, table_addr),
                            HoleValue::Const(1, args_start as i64),
                            HoleValue::Const(2, dst as i64),
                            HoleValue::Const(3, depth_addr),
                            HoleValue::Const(4, status_addr),
                            HoleValue::Const(5, limit_slot as i64),
                            HoleValue::Const(6, code),
                            HoleValue::Cont(0, next),
                        ],
                    );
                } else {
                    buf.push_stencil(
                        &ST_CALL,
                        &[
                            HoleValue::Const(0, table_addr),
                            HoleValue::Const(1, args_start as i64),
                            HoleValue::Const(2, dst as i64),
                            HoleValue::Const(3, depth_addr),
                            HoleValue::Const(4, status_addr),
                            HoleValue::Const(5, limit_slot as i64),
                            HoleValue::Const(6, depth_limit),
                            HoleValue::Cont(0, next),
                        ],
                    );
                }
            }
            MicroOp::CallSelf { dst, args_start, depth_addr, status_addr, limit_slot, frame_size } => {
                buf.push_stencil(
                    &ST_CALL_SELF,
                    &[
                        HoleValue::Const(0, 0),
                        HoleValue::Const(1, args_start as i64),
                        HoleValue::Const(2, dst as i64),
                        HoleValue::Const(3, depth_addr),
                        HoleValue::Const(4, status_addr),
                        HoleValue::Const(5, limit_slot as i64),
                        HoleValue::Const(6, frame_size),
                        HoleValue::Cont(0, next),
                    ],
                );
                buf.mark_patch_hole(0);
            }
            MicroOp::CallSelfCopy {
                dst,
                args_start,
                src_start,
                arg_count,
                depth_addr,
                status_addr,
                limit_slot,
                frame_size,
            } => {
                buf.push_stencil(
                    &ST_CALL_SELF_COPY,
                    &[
                        HoleValue::Const(0, 0),
                        HoleValue::Const(1, args_start as i64),
                        HoleValue::Const(2, dst as i64),
                        HoleValue::Const(3, depth_addr),
                        HoleValue::Const(4, status_addr),
                        HoleValue::Const(5, limit_slot as i64),
                        HoleValue::Const(6, frame_size),
                        HoleValue::Const(7, src_start as i64),
                        HoleValue::Const(8, arg_count as i64),
                        HoleValue::Cont(0, next),
                    ],
                );
                buf.mark_patch_hole(0);
            }
            MicroOp::NewList { vec_slot, ptr_slot, len_slot, helper_addr } => {
                buf.push_stencil(
                    &ST_ALLOCLIST,
                    &[
                        HoleValue::Const(0, vec_slot as i64),
                        HoleValue::Const(1, ptr_slot as i64),
                        HoleValue::Const(2, len_slot as i64),
                        HoleValue::Const(3, helper_addr),
                        HoleValue::Cont(0, next),
                    ],
                );
            }
            MicroOp::ListTriple { handle_slot, vec_slot, ptr_slot, len_slot, helper_addr } => {
                buf.push_stencil(
                    &ST_LISTTRIPLE,
                    &[
                        HoleValue::Const(0, handle_slot as i64),
                        HoleValue::Const(1, vec_slot as i64),
                        HoleValue::Const(2, ptr_slot as i64),
                        HoleValue::Const(3, len_slot as i64),
                        HoleValue::Const(4, helper_addr),
                        HoleValue::Cont(0, next),
                    ],
                );
            }
            MicroOp::MapGet { dst, key, map_slot, helper_addr } => {
                buf.push_stencil(
                    &ST_MAPHGET,
                    &[
                        HoleValue::Const(0, map_slot as i64),
                        HoleValue::Const(1, key as i64),
                        HoleValue::Const(2, dst as i64),
                        HoleValue::Const(3, helper_addr),
                        HoleValue::Cont(0, next),
                        HoleValue::Cont(1, buf.label(piece_for(code_at(i)))),
                    ],
                );
            }
            MicroOp::MapSet { src, key, map_slot, helper_addr } => {
                buf.push_stencil(
                    &ST_MAPHSET,
                    &[
                        HoleValue::Const(0, map_slot as i64),
                        HoleValue::Const(1, key as i64),
                        HoleValue::Const(2, src as i64),
                        HoleValue::Const(3, helper_addr),
                        HoleValue::Cont(0, next),
                    ],
                );
            }
            MicroOp::MemMem {
                h_ptr_slot,
                h_len_slot,
                n_ptr_slot,
                n_len_slot,
                needle_len_slot,
                i_slot,
                count_slot,
                helper_addr,
            } => {
                buf.push_stencil(
                    &ST_MEMMEM,
                    &[
                        HoleValue::Const(0, h_ptr_slot as i64),
                        HoleValue::Const(1, h_len_slot as i64),
                        HoleValue::Const(2, n_ptr_slot as i64),
                        HoleValue::Const(3, n_len_slot as i64),
                        HoleValue::Const(4, needle_len_slot as i64),
                        HoleValue::Const(5, i_slot as i64),
                        HoleValue::Const(6, count_slot as i64),
                        HoleValue::Const(7, helper_addr),
                        HoleValue::Cont(0, next),
                        HoleValue::Cont(1, buf.label(piece_for(code_at(i)))),
                    ],
                );
            }
            MicroOp::MapHas { dst, key, map_slot, helper_addr } => {
                buf.push_stencil(
                    &ST_MAPHHAS,
                    &[
                        HoleValue::Const(0, map_slot as i64),
                        HoleValue::Const(1, key as i64),
                        HoleValue::Const(2, dst as i64),
                        HoleValue::Const(3, helper_addr),
                        HoleValue::Cont(0, next),
                    ],
                );
            }
            MicroOp::ArrPush { src, vec_slot, ptr_slot, len_slot, helper_addr, byte: _, narrow32: _ } => {
                buf.push_stencil(
                    &ST_PUSH,
                    &[
                        HoleValue::Const(0, src as i64),
                        HoleValue::Const(1, vec_slot as i64),
                        HoleValue::Const(2, ptr_slot as i64),
                        HoleValue::Const(3, len_slot as i64),
                        HoleValue::Const(4, helper_addr),
                        HoleValue::Cont(0, next),
                    ],
                );
            }
            MicroOp::ListClear { vec_slot, ptr_slot, len_slot, helper_addr } => {
                buf.push_stencil(
                    &ST_LIST_CLEAR,
                    &[
                        HoleValue::Const(0, vec_slot as i64),
                        HoleValue::Const(1, ptr_slot as i64),
                        HoleValue::Const(2, len_slot as i64),
                        HoleValue::Const(3, helper_addr),
                        HoleValue::Cont(0, next),
                    ],
                );
            }
            MicroOp::ArrLoad { dst, idx, ptr_slot, len_slot, byte, narrow32, checked } => {
                if checked {
                    buf.push_stencil(
                        arrld_stencil(byte, narrow32, true),
                        &[
                            HoleValue::Const(0, idx as i64),
                            HoleValue::Const(1, ptr_slot as i64),
                            HoleValue::Const(2, len_slot as i64),
                            HoleValue::Const(3, dst as i64),
                            HoleValue::Cont(0, next),
                            HoleValue::Cont(1, buf.label(piece_for(code_at(i)))),
                        ],
                    );
                } else {
                    // Bounds-check eliminated (Oracle-proven): no length hole,
                    // no out-of-bounds continuation.
                    buf.push_stencil(
                        arrld_stencil(byte, narrow32, false),
                        &[
                            HoleValue::Const(0, idx as i64),
                            HoleValue::Const(1, ptr_slot as i64),
                            HoleValue::Const(3, dst as i64),
                            HoleValue::Cont(0, next),
                        ],
                    );
                }
            }
            MicroOp::ArrLoadAffine { dst, a, op, b, const_offset, ptr_slot, len_slot, checked } => {
                if checked {
                    buf.push_stencil(
                        affine_stencil(op, true),
                        &[
                            HoleValue::Const(0, a as i64),
                            HoleValue::Const(1, b as i64),
                            HoleValue::Const(2, const_offset),
                            HoleValue::Const(3, ptr_slot as i64),
                            HoleValue::Const(4, len_slot as i64),
                            HoleValue::Const(5, dst as i64),
                            HoleValue::Cont(0, next),
                            HoleValue::Cont(1, buf.label(piece_for(code_at(i)))),
                        ],
                    );
                } else {
                    buf.push_stencil(
                        affine_stencil(op, false),
                        &[
                            HoleValue::Const(0, a as i64),
                            HoleValue::Const(1, b as i64),
                            HoleValue::Const(2, const_offset),
                            HoleValue::Const(3, ptr_slot as i64),
                            HoleValue::Const(5, dst as i64),
                            HoleValue::Cont(0, next),
                        ],
                    );
                }
            }
            MicroOp::ArrStore { src, idx, ptr_slot, len_slot, byte, narrow32, checked } => {
                if checked {
                    buf.push_stencil(
                        arrst_stencil(byte, narrow32, true),
                        &[
                            HoleValue::Const(0, idx as i64),
                            HoleValue::Const(1, ptr_slot as i64),
                            HoleValue::Const(2, len_slot as i64),
                            HoleValue::Const(3, src as i64),
                            HoleValue::Cont(0, next),
                            HoleValue::Cont(1, buf.label(piece_for(code_at(i)))),
                        ],
                    );
                } else {
                    buf.push_stencil(
                        arrst_stencil(byte, narrow32, false),
                        &[
                            HoleValue::Const(0, idx as i64),
                            HoleValue::Const(1, ptr_slot as i64),
                            HoleValue::Const(3, src as i64),
                            HoleValue::Cont(0, next),
                        ],
                    );
                }
            }
            MicroOp::ArrRMW { idx, operand, ptr_slot, len_slot, op, checked } => {
                if checked {
                    buf.push_stencil(
                        rmw_stencil(op, true),
                        &[
                            HoleValue::Const(0, idx as i64),
                            HoleValue::Const(1, ptr_slot as i64),
                            HoleValue::Const(2, len_slot as i64),
                            HoleValue::Const(3, operand as i64),
                            HoleValue::Cont(0, next),
                            HoleValue::Cont(1, buf.label(piece_for(code_at(i)))),
                        ],
                    );
                } else {
                    buf.push_stencil(
                        rmw_stencil(op, false),
                        &[
                            HoleValue::Const(0, idx as i64),
                            HoleValue::Const(1, ptr_slot as i64),
                            HoleValue::Const(3, operand as i64),
                            HoleValue::Cont(0, next),
                        ],
                    );
                }
            }
            MicroOp::ArrCondSwap { idx1, idx2, ptr_slot, len_slot, cmp, checked } => {
                if checked {
                    buf.push_stencil(
                        condswap_stencil(cmp, true),
                        &[
                            HoleValue::Const(0, idx1 as i64),
                            HoleValue::Const(1, idx2 as i64),
                            HoleValue::Const(2, ptr_slot as i64),
                            HoleValue::Const(3, len_slot as i64),
                            HoleValue::Cont(0, next),
                            HoleValue::Cont(1, buf.label(piece_for(code_at(i)))),
                        ],
                    );
                } else {
                    buf.push_stencil(
                        condswap_stencil(cmp, false),
                        &[
                            HoleValue::Const(0, idx1 as i64),
                            HoleValue::Const(1, idx2 as i64),
                            HoleValue::Const(2, ptr_slot as i64),
                            HoleValue::Cont(0, next),
                        ],
                    );
                }
            }
            MicroOp::ArrSwap { idx1, idx2, ptr_slot, len_slot, checked } => {
                if checked {
                    buf.push_stencil(
                        &ST_ARRSWAP,
                        &[
                            HoleValue::Const(0, idx1 as i64),
                            HoleValue::Const(1, idx2 as i64),
                            HoleValue::Const(2, ptr_slot as i64),
                            HoleValue::Const(3, len_slot as i64),
                            HoleValue::Cont(0, next),
                            HoleValue::Cont(1, buf.label(piece_for(code_at(i)))),
                        ],
                    );
                } else {
                    buf.push_stencil(
                        &ST_ARRSWAP_U,
                        &[
                            HoleValue::Const(0, idx1 as i64),
                            HoleValue::Const(1, idx2 as i64),
                            HoleValue::Const(2, ptr_slot as i64),
                            HoleValue::Cont(0, next),
                        ],
                    );
                }
            }
            MicroOp::FmaF { dst, a, b, c } => {
                buf.push_stencil(
                    &ST_FMAF,
                    &[
                        HoleValue::Const(0, a as i64),
                        HoleValue::Const(1, b as i64),
                        HoleValue::Const(2, c as i64),
                        HoleValue::Const(3, dst as i64),
                        HoleValue::Cont(0, next),
                    ],
                );
            }
            MicroOp::ArrLoad2F { dst, i: ix, j: jx, ptr_slot, len_slot, op } => {
                let stencil = match op {
                    FOp::Add => &ST_ARRLD2_ADDF,
                    FOp::Sub => &ST_ARRLD2_SUBF,
                    FOp::Mul => &ST_ARRLD2_MULF,
                };
                buf.push_stencil(
                    stencil,
                    &[
                        HoleValue::Const(0, ix as i64),
                        HoleValue::Const(1, jx as i64),
                        HoleValue::Const(2, ptr_slot as i64),
                        HoleValue::Const(3, len_slot as i64),
                        HoleValue::Const(4, dst as i64),
                        HoleValue::Cont(0, next),
                        HoleValue::Cont(1, buf.label(piece_for(code_at(i)))),
                    ],
                );
            }
            MicroOp::ArrLoad2 { dst, i: ix, j: jx, ptr_a, len_a, ptr_b, len_b, op, checked } => {
                if checked {
                    buf.push_stencil(
                        ld2_int_stencil(op, true),
                        &[
                            HoleValue::Const(0, ix as i64),
                            HoleValue::Const(1, jx as i64),
                            HoleValue::Const(2, ptr_a as i64),
                            HoleValue::Const(3, len_a as i64),
                            HoleValue::Const(4, ptr_b as i64),
                            HoleValue::Const(5, len_b as i64),
                            HoleValue::Const(6, dst as i64),
                            HoleValue::Cont(0, next),
                            HoleValue::Cont(1, buf.label(piece_for(code_at(i)))),
                        ],
                    );
                } else {
                    buf.push_stencil(
                        ld2_int_stencil(op, false),
                        &[
                            HoleValue::Const(0, ix as i64),
                            HoleValue::Const(1, jx as i64),
                            HoleValue::Const(2, ptr_a as i64),
                            HoleValue::Const(3, ptr_b as i64),
                            HoleValue::Const(4, dst as i64),
                            HoleValue::Cont(0, next),
                        ],
                    );
                }
            }
            MicroOp::NotInt { dst, src } | MicroOp::NotBool { dst, src }
            | MicroOp::IntToFloat { dst, src } | MicroOp::SqrtF { dst, src } => {
                let stencil = match op {
                    MicroOp::NotInt { .. } => &ST_NOTI2,
                    MicroOp::NotBool { .. } => &ST_NOTB2,
                    MicroOp::SqrtF { .. } => &ST_SQRTF2,
                    _ => &ST_I2F2,
                };
                buf.push_stencil(
                    stencil,
                    &[
                        HoleValue::Const(0, src as i64),
                        HoleValue::Const(1, dst as i64),
                        HoleValue::Cont(0, next),
                    ],
                );
            }
            MicroOp::BranchF { cmp, lhs, rhs, target } => {
                // Jump to target when cmp is FALSE — under IEEE, the false
                // path also catches every NaN-unordered pair, so the mapping
                // may ONLY swap operands (a>b ⇔ b<a; a>=b ⇔ b<=a), never
                // continuations on the truth side.
                let t = buf.label(target);
                let (stencil, a, b) = match cmp {
                    Cmp::Lt => (&ST_BRLTF, lhs, rhs),
                    Cmp::Gt => (&ST_BRLTF, rhs, lhs),
                    Cmp::LtEq => (&ST_BRLEF, lhs, rhs),
                    Cmp::GtEq => (&ST_BRLEF, rhs, lhs),
                    Cmp::Eq => (&ST_BREQF, lhs, rhs),
                    Cmp::NotEq => (&ST_BREQF, lhs, rhs),
                };
                let (c0, c1) = if cmp == Cmp::NotEq {
                    // NotEq: jump when eps-equal (the TRUE path of breqf).
                    (t, next)
                } else {
                    (next, t)
                };
                buf.push_stencil(
                    stencil,
                    &[
                        HoleValue::Const(0, a as i64),
                        HoleValue::Const(1, b as i64),
                        HoleValue::Cont(0, c0),
                        HoleValue::Cont(1, c1),
                    ],
                );
            }
            MicroOp::Div { dst, lhs, rhs } | MicroOp::Mod { dst, lhs, rhs } => {
                let stencil = match op {
                    MicroOp::Div { .. } => &ST_DIV3C,
                    _ => &ST_MOD3C,
                };
                buf.push_stencil(
                    stencil,
                    &[
                        HoleValue::Const(0, lhs as i64),
                        HoleValue::Const(1, rhs as i64),
                        HoleValue::Const(2, dst as i64),
                        HoleValue::Cont(0, next),
                        HoleValue::Cont(1, buf.label(piece_for(code_at(i)))),
                    ],
                );
            }
            MicroOp::DivPow2 { dst, lhs, k } => {
                buf.push_stencil(
                    &ST_DIVPOW2,
                    &[
                        HoleValue::Const(0, lhs as i64),
                        HoleValue::Const(1, k as i64),
                        HoleValue::Const(2, (1i64 << k) - 1),
                        HoleValue::Const(3, dst as i64),
                        HoleValue::Cont(0, next),
                    ],
                );
            }
            MicroOp::Jump { target } => {
                buf.push_stencil(&ST_JUMP, &[HoleValue::Cont(0, buf.label(target))]);
            }
            MicroOp::JumpIfFalse { cond, target } => {
                // brz: nonzero → cont 0 (fall through), zero → cont 1 (target).
                buf.push_stencil(
                    &ST_BRZ,
                    &[
                        HoleValue::Const(0, cond as i64),
                        HoleValue::Cont(0, next),
                        HoleValue::Cont(1, buf.label(target)),
                    ],
                );
            }
            MicroOp::JumpIfTrue { cond, target } => {
                // Same stencil, swapped continuations: nonzero → target.
                buf.push_stencil(
                    &ST_BRZ,
                    &[
                        HoleValue::Const(0, cond as i64),
                        HoleValue::Cont(0, buf.label(target)),
                        HoleValue::Cont(1, next),
                    ],
                );
            }
            MicroOp::Branch { cmp, lhs, rhs, target } => {
                // Jump to target when cmp is FALSE; fall through when TRUE.
                // brlt/breq put the TRUE outcome on cont 0; operand and
                // continuation swaps express all six comparison kinds.
                let t = buf.label(target);
                let (stencil, a, b, c0, c1) = match cmp {
                    Cmp::Lt => (&ST_BRLT, lhs, rhs, next, t),
                    Cmp::GtEq => (&ST_BRLT, lhs, rhs, t, next),
                    Cmp::Gt => (&ST_BRLT, rhs, lhs, next, t),
                    Cmp::LtEq => (&ST_BRLT, rhs, lhs, t, next),
                    Cmp::Eq => (&ST_BREQ, lhs, rhs, next, t),
                    Cmp::NotEq => (&ST_BREQ, lhs, rhs, t, next),
                };
                buf.push_stencil(
                    stencil,
                    &[
                        HoleValue::Const(0, a as i64),
                        HoleValue::Const(1, b as i64),
                        HoleValue::Cont(0, c0),
                        HoleValue::Cont(1, c1),
                    ],
                );
            }
            MicroOp::Return { src } => {
                buf.push_stencil(&ST_RET2, &[HoleValue::Const(0, src as i64)]);
            }
            // The mutable-Text append has NO per-piece stencil — it is emitted
            // ONLY for the contiguous regalloc backend (where it lowers to a
            // SysV helper call). Decline so the caller falls back to bytecode,
            // which is always correct.
            MicroOp::StrAppend { .. } => {
                return Err(JitCompileError::Unsupported("StrAppend"));
            }
            // The magic reciprocal: a single `mul`-high + shift piece (the
            // contiguous regalloc backend also lowers it, but the stencil tier
            // owns the mixed regions regalloc declines — e.g. a `% c` next to a
            // map insert). Holes: 0 = lhs, 1 = magic, 2 = more, 3 = mul_back,
            // 4 = dst. No deopt continuation (a literal `c > 0` never faults).
            MicroOp::MagicDivU { dst, lhs, magic, more, mul_back } => {
                buf.push_stencil(
                    &ST_MAGICDIV,
                    &[
                        HoleValue::Const(0, lhs as i64),
                        HoleValue::Const(1, magic as i64),
                        HoleValue::Const(2, more as i64),
                        HoleValue::Const(3, mul_back),
                        HoleValue::Const(4, dst as i64),
                        HoleValue::Cont(0, next),
                    ],
                );
            }
        }
    }

    if let Some(cell) = &status {
        let addr = cell.as_ref() as *const AtomicI64 as i64;
        buf.push_stencil(&ST_DEOPT, &[HoleValue::Const(0, addr)]);
        for &code in &code_pieces {
            buf.push_stencil(
                &ST_DEOPT_AT,
                &[
                    HoleValue::Const(0, addr),
                    HoleValue::Const(1, code),
                    HoleValue::Const(2, depth_addr),
                ],
            );
        }
    }

    let chain = buf.finish().map_err(|e| JitCompileError::Assembly(e.to_string()))?;
    Ok(CompiledChain { chain, status, keepalive: Vec::new() })
}

/// Evaluate the unsigned magic reciprocal for [`MicroOp::MagicDivU`]:
/// `x / c` when `mul_back == 0`, else `x % c` (`mul_back == c`). The dividend is
/// reinterpreted as `u64`, sound because the op is emitted ONLY for a proven
/// non-negative `x` (the i64 bit pattern equals the value, and unsigned and
/// signed-truncating `/`/`%` agree there). The remainder is `x - q*c` in
/// wrapping i64 (its low 64 bits equal the u64 difference). The `more` encoding
/// is the `logicaffeine_data::LogosDivU64` one: low 6 bits = shift, `0x40`
/// = the 65-bit add-marker path, `0x80` = the pure-shift power-of-two path.
#[inline]
pub fn magic_eval(x: i64, magic: u64, more: u8, mul_back: i64) -> i64 {
    const SHIFT_MASK: u8 = 0x3F;
    const ADD_MARKER: u8 = 0x40;
    const SHIFT_PATH: u8 = 0x80;
    let n = x as u64;
    let q = if more & SHIFT_PATH != 0 {
        n >> (more & SHIFT_MASK)
    } else {
        let hi = (((magic as u128) * (n as u128)) >> 64) as u64;
        if more & ADD_MARKER != 0 {
            let t = (n.wrapping_sub(hi) >> 1).wrapping_add(hi);
            t >> (more & SHIFT_MASK)
        } else {
            hi >> (more & SHIFT_MASK)
        }
    };
    if mul_back == 0 {
        q as i64
    } else {
        x.wrapping_sub((q as i64).wrapping_mul(mul_back))
    }
}

/// The reference evaluator — the independent model every differential runs
/// against. Deliberately the dumbest possible implementation: a pc loop.
/// `fuel` bounds the step count (None when caught in a loop that long).
/// A zero divisor also yields None — the reference's model of the chain's
/// side exit (differentials feed nonzero divisors when comparing values, and
/// assert Deopt agreement when not).
pub fn reference_eval(ops: &[MicroOp], frame: &mut [i64], mut fuel: u64) -> Option<i64> {
    let mut pc = 0usize;
    while pc < ops.len() {
        if fuel == 0 {
            return None;
        }
        fuel -= 1;
        match ops[pc] {
            MicroOp::LoadConst { dst, value } => frame[dst as usize] = value,
            MicroOp::Move { dst, src } => frame[dst as usize] = frame[src as usize],
            // EXACT integer arithmetic: signed overflow returns None — the JIT's
            // side-exit (deopt) is the matching signal, so the differential stays
            // consistent (overflow ⟺ reference None ⟺ JIT deopt).
            MicroOp::Add { dst, lhs, rhs } => {
                frame[dst as usize] = frame[lhs as usize].checked_add(frame[rhs as usize])?
            }
            MicroOp::Sub { dst, lhs, rhs } => {
                frame[dst as usize] = frame[lhs as usize].checked_sub(frame[rhs as usize])?
            }
            MicroOp::Mul { dst, lhs, rhs } => {
                frame[dst as usize] = frame[lhs as usize].checked_mul(frame[rhs as usize])?
            }
            MicroOp::Div { dst, lhs, rhs } => {
                let b = frame[rhs as usize];
                // Zero divisor AND the `i64::MIN / -1` overflow both deopt —
                // the exact tier recomputes (and promotes to BigInt).
                if b == 0 || (b == -1 && frame[lhs as usize] == i64::MIN) {
                    return None;
                }
                frame[dst as usize] = frame[lhs as usize].wrapping_div(b)
            }
            MicroOp::DivPow2 { dst, lhs, k } => {
                let x = frame[lhs as usize];
                let mask = (1i64 << k) - 1;
                frame[dst as usize] = x.wrapping_add((x >> 63) & mask) >> k;
            }
            MicroOp::MagicDivU { dst, lhs, magic, more, mul_back } => {
                frame[dst as usize] = magic_eval(frame[lhs as usize], magic, more, mul_back);
            }
            MicroOp::Mod { dst, lhs, rhs } => {
                let b = frame[rhs as usize];
                if b == 0 {
                    return None;
                }
                frame[dst as usize] = frame[lhs as usize].wrapping_rem(b)
            }
            MicroOp::Lt { dst, lhs, rhs } => {
                frame[dst as usize] = (frame[lhs as usize] < frame[rhs as usize]) as i64
            }
            MicroOp::Gt { dst, lhs, rhs } => {
                frame[dst as usize] = (frame[lhs as usize] > frame[rhs as usize]) as i64
            }
            MicroOp::LtEq { dst, lhs, rhs } => {
                frame[dst as usize] = (frame[lhs as usize] <= frame[rhs as usize]) as i64
            }
            MicroOp::GtEq { dst, lhs, rhs } => {
                frame[dst as usize] = (frame[lhs as usize] >= frame[rhs as usize]) as i64
            }
            MicroOp::Eq { dst, lhs, rhs } => {
                frame[dst as usize] = (frame[lhs as usize] == frame[rhs as usize]) as i64
            }
            MicroOp::Neq { dst, lhs, rhs } => {
                frame[dst as usize] = (frame[lhs as usize] != frame[rhs as usize]) as i64
            }
            MicroOp::BitAnd { dst, lhs, rhs } => {
                frame[dst as usize] = frame[lhs as usize] & frame[rhs as usize]
            }
            MicroOp::BitOr { dst, lhs, rhs } => {
                frame[dst as usize] = frame[lhs as usize] | frame[rhs as usize]
            }
            MicroOp::BitXor { dst, lhs, rhs } => {
                frame[dst as usize] = frame[lhs as usize] ^ frame[rhs as usize]
            }
            MicroOp::Shl { dst, lhs, rhs } => {
                frame[dst as usize] = frame[lhs as usize].wrapping_shl(frame[rhs as usize] as u32)
            }
            MicroOp::Shr { dst, lhs, rhs } => {
                frame[dst as usize] = frame[lhs as usize].wrapping_shr(frame[rhs as usize] as u32)
            }
            MicroOp::NotInt { dst, src } => frame[dst as usize] = !frame[src as usize],
            MicroOp::NotBool { dst, src } => frame[dst as usize] = frame[src as usize] ^ 1,
            MicroOp::AddF { dst, lhs, rhs } => {
                let a = f64::from_bits(frame[lhs as usize] as u64);
                let b = f64::from_bits(frame[rhs as usize] as u64);
                frame[dst as usize] = (a + b).to_bits() as i64
            }
            MicroOp::SubF { dst, lhs, rhs } => {
                let a = f64::from_bits(frame[lhs as usize] as u64);
                let b = f64::from_bits(frame[rhs as usize] as u64);
                frame[dst as usize] = (a - b).to_bits() as i64
            }
            MicroOp::MulF { dst, lhs, rhs } => {
                let a = f64::from_bits(frame[lhs as usize] as u64);
                let b = f64::from_bits(frame[rhs as usize] as u64);
                frame[dst as usize] = (a * b).to_bits() as i64
            }
            MicroOp::DivF { dst, lhs, rhs } => {
                let b = f64::from_bits(frame[rhs as usize] as u64);
                if b == 0.0 {
                    return None;
                }
                let a = f64::from_bits(frame[lhs as usize] as u64);
                frame[dst as usize] = (a / b).to_bits() as i64
            }
            MicroOp::LtF { dst, lhs, rhs } => {
                let a = f64::from_bits(frame[lhs as usize] as u64);
                let b = f64::from_bits(frame[rhs as usize] as u64);
                frame[dst as usize] = (a < b) as i64
            }
            MicroOp::GtF { dst, lhs, rhs } => {
                let a = f64::from_bits(frame[lhs as usize] as u64);
                let b = f64::from_bits(frame[rhs as usize] as u64);
                frame[dst as usize] = (a > b) as i64
            }
            MicroOp::LtEqF { dst, lhs, rhs } => {
                let a = f64::from_bits(frame[lhs as usize] as u64);
                let b = f64::from_bits(frame[rhs as usize] as u64);
                frame[dst as usize] = (a <= b) as i64
            }
            MicroOp::GtEqF { dst, lhs, rhs } => {
                let a = f64::from_bits(frame[lhs as usize] as u64);
                let b = f64::from_bits(frame[rhs as usize] as u64);
                frame[dst as usize] = (a >= b) as i64
            }
            // IEEE float equality (`NaN != NaN`) — identical to the
            // tree-walker/VM `values_equal` and the compiled backend.
            MicroOp::EqF { dst, lhs, rhs } => {
                let a = f64::from_bits(frame[lhs as usize] as u64);
                let b = f64::from_bits(frame[rhs as usize] as u64);
                frame[dst as usize] = (a == b) as i64
            }
            MicroOp::NeqF { dst, lhs, rhs } => {
                let a = f64::from_bits(frame[lhs as usize] as u64);
                let b = f64::from_bits(frame[rhs as usize] as u64);
                frame[dst as usize] = (a != b) as i64
            }
            MicroOp::BranchF { cmp, lhs, rhs, target } => {
                let a = f64::from_bits(frame[lhs as usize] as u64);
                let b = f64::from_bits(frame[rhs as usize] as u64);
                let truth = match cmp {
                    Cmp::Lt => a < b,
                    Cmp::Gt => a > b,
                    Cmp::LtEq => a <= b,
                    Cmp::GtEq => a >= b,
                    Cmp::Eq => a == b,
                    Cmp::NotEq => a != b,
                };
                if !truth {
                    pc = target;
                    continue;
                }
            }
            MicroOp::IntToFloat { dst, src } => {
                frame[dst as usize] = (frame[src as usize] as f64).to_bits() as i64
            }
            MicroOp::SqrtF { dst, src } => {
                frame[dst as usize] =
                    f64::from_bits(frame[src as usize] as u64).sqrt().to_bits() as i64
            }
            // Calls, pushes and map traffic touch live runtime state the
            // reference cannot model — VM-level differentials own their
            // coverage.
            MicroOp::Call { .. }
            | MicroOp::CallSelf { .. }
            | MicroOp::CallSelfCopy { .. }
            | MicroOp::ArrPush { .. }
            | MicroOp::ListClear { .. }
            | MicroOp::StrAppend { .. }
            | MicroOp::MapGet { .. }
            | MicroOp::MapSet { .. }
            | MicroOp::MapHas { .. }
            | MicroOp::NewList { .. }
            | MicroOp::ListTriple { .. } => return None,
            MicroOp::ArrLoad { dst, idx, ptr_slot, len_slot, byte, narrow32, checked } => {
                let i = frame[idx as usize];
                let len = frame[len_slot as usize];
                let im1 = i.wrapping_sub(1);
                // The reference keeps the bounds guard either way (an
                // unchecked load that the Oracle proved in-bounds is in
                // range by hypothesis; refusing OOB here is just safety).
                let _ = checked;
                if (im1 as u64) >= (len as u64) {
                    return None;
                }
                // SAFETY: differentials pin a live buffer, like the chain.
                frame[dst as usize] = unsafe {
                    if byte {
                        *(frame[ptr_slot as usize] as *const u8).add(im1 as usize) as i64
                    } else if narrow32 {
                        *(frame[ptr_slot as usize] as *const i32).add(im1 as usize) as i64
                    } else {
                        *(frame[ptr_slot as usize] as *const i64).add(im1 as usize)
                    }
                };
            }
            MicroOp::ArrLoadAffine { dst, a, op, b, const_offset, ptr_slot, len_slot, checked } => {
                let _ = checked;
                let idx = op.eval(frame[a as usize], frame[b as usize], const_offset);
                let len = frame[len_slot as usize];
                let im1 = idx.wrapping_sub(1);
                if (im1 as u64) >= (len as u64) {
                    return None;
                }
                // SAFETY: differentials pin a live 8-byte buffer, like the chain.
                frame[dst as usize] =
                    unsafe { *(frame[ptr_slot as usize] as *const i64).add(im1 as usize) };
            }
            MicroOp::ArrLoad2F { dst, i: ix, j: jx, ptr_slot, len_slot, op } => {
                let len = frame[len_slot as usize];
                let im1 = frame[ix as usize].wrapping_sub(1);
                let jm1 = frame[jx as usize].wrapping_sub(1);
                if (im1 as u64) >= (len as u64) || (jm1 as u64) >= (len as u64) {
                    return None;
                }
                // SAFETY: differentials pin a live 8-byte buffer, like the chain.
                let (a, b) = unsafe {
                    let ptr = frame[ptr_slot as usize] as *const i64;
                    (
                        f64::from_bits(*ptr.add(im1 as usize) as u64),
                        f64::from_bits(*ptr.add(jm1 as usize) as u64),
                    )
                };
                frame[dst as usize] = op.eval(a, b).to_bits() as i64;
            }
            MicroOp::ArrLoad2 { dst, i: ix, j: jx, ptr_a, len_a, ptr_b, len_b, op, checked } => {
                let _ = checked;
                let lena = frame[len_a as usize];
                let lenb = frame[len_b as usize];
                let im1 = frame[ix as usize].wrapping_sub(1);
                let jm1 = frame[jx as usize].wrapping_sub(1);
                if (im1 as u64) >= (lena as u64) || (jm1 as u64) >= (lenb as u64) {
                    return None;
                }
                // SAFETY: differentials pin live 8-byte buffers, like the chain.
                let (a, b) = unsafe {
                    let pa = frame[ptr_a as usize] as *const i64;
                    let pb = frame[ptr_b as usize] as *const i64;
                    (*pa.add(im1 as usize), *pb.add(jm1 as usize))
                };
                frame[dst as usize] = op.eval(a, b);
            }
            MicroOp::ArrStore { src, idx, ptr_slot, len_slot, byte, narrow32, checked } => {
                // The reference always validates — a sound proof never trips it.
                let _ = checked;
                let i = frame[idx as usize];
                let len = frame[len_slot as usize];
                let im1 = i.wrapping_sub(1);
                if (im1 as u64) >= (len as u64) {
                    return None;
                }
                // SAFETY: see ArrLoad.
                unsafe {
                    if byte {
                        *(frame[ptr_slot as usize] as *mut u8).add(im1 as usize) =
                            (frame[src as usize] != 0) as u8;
                    } else if narrow32 {
                        *(frame[ptr_slot as usize] as *mut i32).add(im1 as usize) =
                            frame[src as usize] as i32;
                    } else {
                        *(frame[ptr_slot as usize] as *mut i64).add(im1 as usize) =
                            frame[src as usize];
                    }
                }
            }
            MicroOp::ArrRMW { idx, operand, ptr_slot, len_slot, op, checked } => {
                let _ = checked;
                let i = frame[idx as usize];
                let len = frame[len_slot as usize];
                let im1 = i.wrapping_sub(1);
                if (im1 as u64) >= (len as u64) {
                    return None;
                }
                // SAFETY: see ArrLoad — a live 8-byte buffer is pinned.
                unsafe {
                    let cell = (frame[ptr_slot as usize] as *mut i64).add(im1 as usize);
                    *cell = op.eval(*cell, frame[operand as usize]);
                }
            }
            MicroOp::ArrCondSwap { idx1, idx2, ptr_slot, len_slot, cmp, checked } => {
                let _ = checked;
                let len = frame[len_slot as usize];
                let m1 = frame[idx1 as usize].wrapping_sub(1);
                let m2 = frame[idx2 as usize].wrapping_sub(1);
                if (m1 as u64) >= (len as u64) || (m2 as u64) >= (len as u64) {
                    return None;
                }
                // SAFETY: see ArrLoad — a live 8-byte buffer is pinned.
                unsafe {
                    let ptr = frame[ptr_slot as usize] as *mut i64;
                    let a = *ptr.add(m1 as usize);
                    let b = *ptr.add(m2 as usize);
                    if cmp.eval(a, b) {
                        *ptr.add(m1 as usize) = b;
                        *ptr.add(m2 as usize) = a;
                    }
                }
            }
            MicroOp::ArrSwap { idx1, idx2, ptr_slot, len_slot, checked } => {
                let _ = checked;
                let len = frame[len_slot as usize];
                let m1 = frame[idx1 as usize].wrapping_sub(1);
                let m2 = frame[idx2 as usize].wrapping_sub(1);
                if (m1 as u64) >= (len as u64) || (m2 as u64) >= (len as u64) {
                    return None;
                }
                // SAFETY: see ArrLoad — a live 8-byte buffer is pinned.
                unsafe {
                    let ptr = frame[ptr_slot as usize] as *mut i64;
                    let a = *ptr.add(m1 as usize);
                    let b = *ptr.add(m2 as usize);
                    *ptr.add(m1 as usize) = b;
                    *ptr.add(m2 as usize) = a;
                }
            }
            MicroOp::FmaF { dst, a, b, c } => {
                let av = f64::from_bits(frame[a as usize] as u64);
                let bv = f64::from_bits(frame[b as usize] as u64);
                let cv = f64::from_bits(frame[c as usize] as u64);
                frame[dst as usize] = ((av * bv) + cv).to_bits() as i64;
            }
            MicroOp::Jump { target } => {
                pc = target;
                continue;
            }
            MicroOp::JumpIfFalse { cond, target } => {
                if frame[cond as usize] == 0 {
                    pc = target;
                    continue;
                }
            }
            MicroOp::JumpIfTrue { cond, target } => {
                if frame[cond as usize] != 0 {
                    pc = target;
                    continue;
                }
            }
            MicroOp::Branch { cmp, lhs, rhs, target } => {
                if !cmp.eval(frame[lhs as usize], frame[rhs as usize]) {
                    pc = target;
                    continue;
                }
            }
            MicroOp::MemMem {
                h_ptr_slot,
                h_len_slot,
                n_ptr_slot,
                n_len_slot,
                needle_len_slot,
                i_slot,
                count_slot,
                ..
            } => {
                // Reference model of the naive-search collapse: count overlapping
                // matches over `[i, h_len - needle_len + 1]`, add to count, and
                // advance `i` to the exit value — the same contract the runtime
                // helper implements. A needle index past its buffer would deopt
                // (the nest's checked needle `Index`): None, like the divisors.
                let h_len = frame[h_len_slot as usize];
                let n_buf_len = frame[n_len_slot as usize];
                let needle_len = frame[needle_len_slot as usize];
                let start = frame[i_slot as usize];
                if needle_len > n_buf_len {
                    return None;
                }
                let bound = h_len - needle_len + 1;
                let mut count = 0i64;
                if needle_len == 0 {
                    if start <= bound {
                        count = bound - start + 1;
                    }
                } else {
                    // SAFETY: the pinned byte buffers are live for the eval.
                    let hay = frame[h_ptr_slot as usize] as *const u8;
                    let ndl = frame[n_ptr_slot as usize] as *const u8;
                    let mut p = start; // 1-based
                    while p <= bound {
                        let mut m = true;
                        for j in 0..needle_len {
                            let hb = unsafe { *hay.add((p + j - 1) as usize) };
                            let nb = unsafe { *ndl.add(j as usize) };
                            if hb != nb {
                                m = false;
                                break;
                            }
                        }
                        if m {
                            count += 1;
                        }
                        p += 1;
                    }
                }
                frame[count_slot as usize] += count;
                frame[i_slot as usize] = core::cmp::max(start, bound + 1);
            }
            MicroOp::Return { src } => return Some(frame[src as usize]),
        }
        pc += 1;
    }
    unreachable!("validated programs cannot fall off the end")
}