fusevm 0.26.0

Language-agnostic bytecode VM with fused superinstructions and a 3-tier Cranelift JIT (linear/block/tracing)
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
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
//! ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
//! EXTENSION — NO `csrc/` COUNTERPART. Closed-world ahead-of-time compiler:
//! lowers a whole [`Chunk`] to native machine code via Cranelift. Shared by
//! every fusevm frontend (stryke / zshrs / awkrs / vimlrs) so AOT lives here
//! once; each frontend's `--build` calls this, then links the result.
//!
//! # Model — "AOT threaded code"
//!
//! The bytecode dispatch loop ([`VM::run`]) is replaced by a native function
//! with one Cranelift block per op. Each op block calls the per-op runtime
//! step (`VM::aot_exec_op`, reached through the `extern "C"`
//! [`fusevm_aot_exec_op`] shim), which runs that op's semantics via the same
//! `VM::exec_op` the interpreter uses — the single source of truth — and
//! returns the **next instruction index** (or `-1` to terminate). The native
//! code branches on that:
//!
//! ```text
//!   entry → dispatch(0)
//!   dispatch(ip): br_table ip → [block_0, …, block_{n-1}]  (default → ret)
//!   block_i: next = exec_op(vm, i);
//!            if next < 0 → ret  else → dispatch(next)
//!   ret:     finish(vm); return
//! ```
//!
//! Routing every op through `dispatch` (rather than static fall-through) keeps
//! the lowering uniform and correct for data-dependent targets — `Op::Jump`,
//! the `JumpIf*` family, and intra-chunk `Op::Call`/`Op::Return`, whose target
//! is only known at run time — without the native code ever reading the `VM`
//! struct layout. Op *semantics* still live in the runtime (linked in), so the
//! interpreter dispatch loop is gone but the work each op does is unchanged.
//!
//! # Native op specialization (in progress)
//!
//! Layered on top of the threaded path, [`build_entry`] lowers chunks that are
//! integer/boolean computations directly to native IR. `analyze_native` runs
//! an abstract interpretation over the operand stack — tracking int-vs-bool
//! `Kind`s, finding basic-block leaders, and checking join consistency — and,
//! when the whole chunk qualifies, `build_entry_native` emits one Cranelift
//! block per leader with the operand stack held in frontend `Variable`s (an
//! `i64` and an `f64` variable per position; the plan's `Kind`s say which is
//! live). This covers integer **and float** arithmetic/comparisons — including
//! `int→float` promotion mirroring the interpreter (except where that
//! promotion would round an integer past `2^53` and the frontend installs a
//! numeric hook — see `native_rounds_operand`) — modulo (`Mod`: integer
//! `srem` guarding the divisors that would trap, or an `fmod` libcall for
//! floats) and power (`Pow`/`PowFloat` via a `powf` libcall), the math
//! intrinsics (`AbsFloat`/`SqrtFloat`/`Ceil`/`Floor`/`Trunc`/`RoundFloat` as
//! single instructions; `Sin`/`Cos`/`Tan`/`Exp`/`Log`/… and `Atan2Float` via
//! libcalls; `AbsInt`/`TruncInt`; `GcdInt`/`LcmInt` as internal Euclid loops;
//! and the awk scalar ops — `AwkDiv`/`AwkMod` and their JIT twins
//! `AwkDivJit`/`AwkModJit`/`AwkLshiftJit`/`AwkRshiftJit`/`AwkComplJit` (a native
//! error-return branch on a bad operand), plus `AwkSqrtJit`/`AwkLogJit`
//! (warn-and-return-NaN on a negative argument) — integer bitwise/shift ops
//! (`BitAnd`/…/`Shl`/`Shr`), `Inc`/`Dec`, booleans (`LoadTrue`/`LoadFalse`/
//! `LogNot`/`LogAnd`/`LogOr`), three-way compare (`Spaceship`), stack shuffles
//! (`Dup`/`Dup2`/`Swap`/`Rot`/`Pop`), control flow (`Jump`/`JumpIf*`, including
//! the value-keeping `JumpIf*Keep`), integer slots and globals
//! (`GetVar`/`SetVar`/`DeclareVar`) — both held in SSA registers, guarded by a
//! definite-assignment analysis — and the slot super-ops the
//! compiler emits for hot loops (`PreIncSlot`/`PostIncSlot`/…,
//! `AddAssignSlotVoid`, the fused `SlotLtIntJumpIfFalse` /
//! `SlotIncLtIntJumpBack`, and `AccumSumLoop` — whose internal
//! `while i < limit { sum += i; i += 1 }` is emitted as a real native loop).
//! The lowered ops use no per-op interpreter dispatch; only the final result is
//! boxed back into the VM, so a fully-scalar loop runs entirely in registers.
//!
//! # Inline/shim boundary (in progress)
//!
//! Chunks that mix scalar work with heap ops no longer fall back wholesale. The
//! *spill* half of the boundary is implemented for **sink ops** (`Print`/
//! `PrintLn`): the native code spills the top `n` register scalars onto the
//! boxed `vm.stack` (per `Kind`), runs the op via the [`fusevm_aot_exec_op`]
//! shim, and continues — `vm.stack` is empty before and after, so no reload or
//! type guard is needed. This lets a hot numeric loop with embedded output run
//! native.
//!
//! The *reload* half is started too, for **source ops whose result kind is
//! statically known** (`AwkGetFieldNum`, always `Float`): run the op via the
//! shim, then reload its pushed value into a register via `fusevm_aot_pop_float`
//! — no type guard needed because the kind is static.
//!
//! Slots and globals are typed per a chunk-wide kind inferred during analysis,
//! so a float slot/global (e.g. an awk `sum += 0.5` accumulator) lowers to an
//! `f64` register; a slot used with mixed Int/Float kinds falls back.
//!
//! # Deopt (one-way exit to the interpreter)
//!
//! A chunk no longer has to be lowerable end to end. Anything the native path
//! can't handle at a given op — a string/array/hash/heap op, a heap constant
//! load, or an *operand-type* mismatch (a `Bool` fed to `Add`, a non-scalar
//! stored to a slot) — becomes a **deopt point**: the analysis lowers everything
//! around it and stops propagating past it, and codegen emits a deopt there. So
//! a hot numeric loop alongside an occasional string op runs native, deopting
//! only where it must. The chunk falls back to threaded wholesale only for the
//! genuinely structural reasons that leave nothing to lower (stack underflow, an
//! inconsistent kind join, a mixed-kind slot, a non-numeric final result, or a
//! conditionally-assigned slot at a deopt point).
//!
//! For cases the register model can't represent, native code takes a *deopt*:
//! `emit_deopt` writes the **definitely-assigned** register-cached slots/globals
//! back to the VM (an unassigned one is left `Undef`, not a register's zero).
//! Because a register can't distinguish a real `0` from `Undef`, a chunk where a
//! slot/global is only *maybe* assigned (conditionally written) at a deopt point
//! falls back to threaded rather than risk the resumed interpreter reading the
//! wrong value. The deopt also spills
//! the live operand stack onto the boxed stack, then `fusevm_aot_resume`s the
//! interpreter at the deopt ip (which owns the rest of the run). `Op::Div` uses
//! this for its rare divide-by-zero case (which yields `Undef`): the common path
//! is a native `fdiv`, and only a zero divisor deopts.
//!
//! `GetStatus` (`$?`) is lowered as a statically-typed `Status` source (it
//! always pushes a `Value::Status`): the code is carried in an `i64` register,
//! float-promotes in arithmetic, is truthy when **zero** (shell success), and
//! boxes back as `Value::Status`.
//!
//! Still deferred: source ops whose result kind is **truly dynamic** (array/hash
//! reads, `ReadLine`, calls) — these reuse the deopt machinery but need a
//! runtime type guard on the boxed value (deopt when it isn't the expected
//! scalar). Anything not yet specialized falls back wholesale to
//! `build_entry_threaded`.
//!
//! # Builtin calls
//!
//! [`Op::CallBuiltin`] crosses the same boundary — operands spilled from
//! registers, the handler run through the shim, its result threaded back as a
//! boxed `Obj` handle — with one addition: the shim's returned next-ip is
//! *honoured*, because a builtin is the one shimmed op that can end the run (a
//! handler calling [`VM::request_halt`], e.g. a shell `exit`) or move `vm.ip`.
//!
//! It is gated on [`Chunk::builtin_argc_is_arity`] and off by default. Native
//! codegen needs `argc` to be the op's exact stack effect, and that is a
//! per-frontend property, not a guarantee of the opcode: zshrs emits
//! `CallBuiltin(BUILTIN_XTRACE_ARGS, 2)` for a handler that pops one value and
//! peeks the rest. Without the declaration a builtin call stays a deopt point,
//! and — since a shell chunk's *second* op is already a builtin — such a chunk
//! lowers through `build_entry_threaded` instead (see below).
//!
//! # Choosing between the two paths
//!
//! A deopt is one-way: the interpreter owns everything after it. So a plan that
//! lowers a short prefix and deopts is *worse* than no plan at all, because the
//! threaded path lowers every op to a native block. `NativePlan::worth_lowering`
//! makes that call — the native path wins when it lowers the chunk outright,
//! when it covers a loop, or when it covers most of the ops; otherwise the
//! threaded path takes it. Without that gate a one-op plan silently beat a full
//! threaded lowering, and every shell script compiled to a native driver of two
//! calls (`push_int`, then `resume`) with the whole program interpreted.
//!
//! [`build_entry`] is generic over [`Module`], so the in-memory JIT path used
//! to validate the compiler ([`run_chunk_native`]) and the on-disk object path
//! (`ObjectModule`, wired in the linker stage) share identical codegen.
//! ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

// The AOT shims and entry pass `*mut VM` across `extern "C"` boundaries, but
// both sides are Rust compilation units linked together (the emitted object, the
// fusevm runtime, and the frontend). The pointer is an opaque handle — C code
// never dereferences it — so the FFI-safety lint is a false positive here.
#![allow(improper_ctypes, improper_ctypes_definitions)]

use crate::chunk::Chunk;
use crate::op::Op;
use crate::value::Value;
use crate::vm::{VMResult, VM};

use cranelift_codegen::ir::condcodes::{FloatCC, IntCC};
use cranelift_codegen::ir::immediates::Ieee64;
use cranelift_codegen::ir::{types, AbiParam, Block, BlockArg, FuncRef, InstBuilder};
use cranelift_codegen::settings::{self, Configurable};
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext, Switch, Variable};
use cranelift_jit::{JITBuilder, JITModule};
use cranelift_module::{default_libcall_names, DataDescription, FuncId, Linkage, Module};
use cranelift_object::{ObjectBuilder, ObjectModule};
use std::collections::{BTreeSet, HashMap};
use std::path::Path;

/// Magic and format version stamped in front of the embedded serialized chunk.
///
/// The blob is `bincode`, which is positional and self-describes nothing: a
/// [`crate::Chunk`] field added or reordered changes the layout, and an object
/// built by another fusevm deserializes into a bincode error at best. Without a
/// stamp the runtime could only call that "corrupt", which names the symptom and
/// not the cause; with one it can say the object was built by a different
/// fusevm, which is what actually happened. Bump the last byte whenever
/// `Chunk`'s serialized shape changes.
///
/// `FVAOT003` — 001 was the unstamped layout, 002 predates
/// `Chunk::builtin_argc_is_arity`. Bumped on every `Chunk` layout change so an
/// object built by an older fusevm is REJECTED with a rebuild message instead of
/// mis-deserializing.
pub const AOT_CHUNK_MAGIC: &[u8; 8] = b"FVAOT003";

/// Exported symbol of the embedded serialized chunk (defined in the object).
pub const AOT_CHUNK_BLOB_SYMBOL: &str = "fusevm_aot_chunk_blob";
/// Exported symbol holding the embedded chunk's length (u64, little-endian).
pub const AOT_CHUNK_LEN_SYMBOL: &str = "fusevm_aot_chunk_len";

/// The embedded blob: [`AOT_CHUNK_MAGIC`] followed by the bincode chunk.
fn stamped(chunk: &Chunk) -> Result<Vec<u8>, String> {
    let body = bincode::serialize(chunk).map_err(|e| format!("aot: serialize chunk: {e}"))?;
    let mut blob = Vec::with_capacity(AOT_CHUNK_MAGIC.len() + body.len());
    blob.extend_from_slice(AOT_CHUNK_MAGIC);
    blob.extend_from_slice(&body);
    Ok(blob)
}

/// Exported symbol name of the compiled entry function.
pub const AOT_ENTRY_SYMBOL: &str = "fusevm_aot_entry";

/// Per-op runtime step, called by the native driver once per op. Runs the op at
/// `ip` via the shared `VM::aot_exec_op` and returns the next instruction
/// index, or `-1` when the run terminates.
///
/// # Safety
/// `vm` must be a valid, uniquely-borrowed `*mut VM` for the duration of the
/// call (the driver always passes the live VM it was handed).
#[no_mangle]
pub extern "C" fn fusevm_aot_exec_op(vm: *mut VM, ip: u64) -> i64 {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_exec_op(ip as usize) }
}

/// Finalize the run, computing the tail result if no op stored one. Called once
/// by the native driver's return path.
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_finish(vm: *mut VM) {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_finish() }
}

/// Store an integer result computed by natively-lowered AOT code (see
/// `VM::aot_set_int_result`). The native fast path keeps intermediate values
/// in registers, so it reports its final result through this shim rather than
/// the boxed stack-tail logic in [`fusevm_aot_finish`].
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_set_int_result(vm: *mut VM, n: i64) {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_set_int_result(n) }
}

/// Store a float result from natively-lowered AOT code (see
/// `VM::aot_set_float_result`). The float analog of
/// [`fusevm_aot_set_int_result`].
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_set_float_result(vm: *mut VM, f: f64) {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_set_float_result(f) }
}

/// Box the boxed-stack top into the value arena, returning its handle
/// (`VM::aot_box`). Used to thread a heap value through a register.
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_box(vm: *mut VM) -> i64 {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_box() }
}

/// Push the arena value for `handle` back onto the boxed stack
/// (`VM::aot_unbox`), so a shimmed op can consume it.
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_unbox(vm: *mut VM, handle: i64) {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_unbox(handle) }
}

/// Store a boxed (heap) result from a register handle
/// (`VM::aot_set_obj_result`).
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_set_obj_result(vm: *mut VM, handle: i64) {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_set_obj_result(handle) }
}

/// Clone the value behind `handle` into a fresh owned handle, returned
/// (`VM::aot_clone`). Used when reading an `Obj` slot.
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_clone(vm: *mut VM, handle: i64) -> i64 {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_clone(handle) }
}

/// Free an owned handle (`VM::aot_free`); negative is the empty-slot sentinel.
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_free(vm: *mut VM, handle: i64) {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_free(handle) }
}

/// Truthiness of a boxed handle, consuming it (`VM::aot_truthy`).
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_truthy(vm: *mut VM, handle: i64) -> i64 {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_truthy(handle) }
}

/// Truthiness of a boxed handle, leaving it owned (`VM::aot_truthy_keep`).
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_truthy_keep(vm: *mut VM, handle: i64) -> i64 {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_truthy_keep(handle) }
}

/// Deopt writeback of an `Obj` slot from a register handle
/// (`VM::aot_store_slot_obj`).
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_store_slot_obj(vm: *mut VM, idx: u32, handle: i64) {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_store_slot_obj(idx, handle) }
}

/// Global analog of [`fusevm_aot_store_slot_obj`] (`VM::aot_store_global_obj`).
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_store_global_obj(vm: *mut VM, idx: u32, handle: i64) {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_store_global_obj(idx, handle) }
}

/// Store an error result (by code) from natively-lowered AOT code that hit a
/// runtime fault (e.g. awk division by zero). The native side branches to a
/// block that calls this and returns early, leaving `aot_result` an `Error`.
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_set_error(vm: *mut VM, code: u32) {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_set_error(code) }
}

/// Deopt writeback: store a register-cached int slot back to the VM frame (see
/// `VM::aot_store_slot_int`).
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_store_slot_int(vm: *mut VM, idx: u32, n: i64) {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_store_slot_int(idx, n) }
}

/// Entry-guard load of a seeded param slot: return its integer value, flagging a
/// guard failure when the slot is not an `Int` (see `VM::aot_load_slot_int`).
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_load_slot_int(vm: *mut VM, idx: u32) -> i64 {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_load_slot_int(idx) }
}

/// Read and clear the native entry-guard failure flag (see `VM::aot_guard_taken`).
/// Returns 1 when a seeded slot mismatched the speculated register kind.
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_guard_taken(vm: *mut VM) -> i64 {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_guard_taken() as i64 }
}

/// Float analog of [`fusevm_aot_store_slot_int`].
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_store_slot_float(vm: *mut VM, idx: u32, f: f64) {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_store_slot_float(idx, f) }
}

/// Global analog of [`fusevm_aot_store_slot_int`].
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_store_global_int(vm: *mut VM, idx: u32, n: i64) {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_store_global_int(idx, n) }
}

/// Float global analog of [`fusevm_aot_store_slot_int`].
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_store_global_float(vm: *mut VM, idx: u32, f: f64) {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_store_global_float(idx, f) }
}

/// Deopt exit: resume the interpreter from `ip` with reconstructed state (see
/// `VM::aot_resume`).
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_resume(vm: *mut VM, ip: u32) {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_resume(ip) }
}

/// Reload a float from the boxed operand stack into a native register — the
/// reload half of the boundary, for source ops with a statically-`Float` result
/// (see `VM::aot_pop_float`).
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_pop_float(vm: *mut VM) -> f64 {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_pop_float() }
}

/// Reload an integer from the boxed operand stack (see `VM::aot_pop_int`).
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_pop_int(vm: *mut VM) -> i64 {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_pop_int() }
}

/// Spill a `Status` code onto the boxed operand stack (see
/// `VM::aot_push_status`).
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_push_status(vm: *mut VM, n: i64) {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_push_status(n) }
}

/// Store a `Status` result from natively-lowered AOT code (see
/// `VM::aot_set_status_result`).
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_set_status_result(vm: *mut VM, n: i64) {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_set_status_result(n) }
}

/// Spill an integer register onto the boxed operand stack ahead of a shimmed
/// (non-lowered) op — the spill half of the inline/shim boundary (see
/// `VM::aot_push_int`).
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_push_int(vm: *mut VM, n: i64) {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_push_int(n) }
}

/// Float analog of [`fusevm_aot_push_int`].
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_push_float(vm: *mut VM, f: f64) {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_push_float(f) }
}

/// Bool analog of [`fusevm_aot_push_int`].
///
/// # Safety
/// Same contract as [`fusevm_aot_exec_op`].
#[no_mangle]
pub extern "C" fn fusevm_aot_push_bool(vm: *mut VM, n: i64) {
    debug_assert!(!vm.is_null());
    // SAFETY: see the function contract; the driver owns the VM for the run.
    unsafe { (*vm).aot_push_bool(n) }
}

/// Emit an awk negative-argument warning to stderr, keyed by `code` (0 = sqrt,
/// 1 = log), formatting `value` exactly as the interpreter does. Pure (no VM
/// access); used by `AwkSqrtJit`/`AwkLogJit`'s negative branch.
#[no_mangle]
pub extern "C" fn fusevm_aot_awk_warn(code: u32, value: f64) {
    let func = if code == 0 { "sqrt" } else { "log" };
    eprintln!("awk: warning: {func}: received negative argument {value}");
}

/// Math libcall for `Op::Pow` — there is no single instruction for `powf`, so
/// native code calls it exactly as compiled Rust/C would. Pure (no VM access).
#[no_mangle]
pub extern "C" fn fusevm_aot_powf(a: f64, b: f64) -> f64 {
    a.powf(b)
}

/// Math libcall for the float case of `Op::Mod` (`f64` remainder). Matches the
/// interpreter's `a % b`. Pure (no VM access).
#[no_mangle]
pub extern "C" fn fusevm_aot_fmod(a: f64, b: f64) -> f64 {
    a % b
}

/// Unary transcendental dispatcher for the `*Float` math ops, keyed by `id` (see
/// the id assignment in `build_entry_native`'s codegen). One libcall import
/// covers all of them. Pure (no VM access).
#[no_mangle]
pub extern "C" fn fusevm_aot_unary_math(id: u32, x: f64) -> f64 {
    match id {
        0 => x.sin(),
        1 => x.cos(),
        2 => x.tan(),
        3 => x.asin(),
        4 => x.acos(),
        5 => x.atan(),
        6 => x.sinh(),
        7 => x.cosh(),
        8 => x.tanh(),
        9 => x.exp(),
        10 => x.ln(),
        11 => x.log2(),
        12 => x.log10(),
        _ => f64::NAN,
    }
}

/// Libcall for `Op::MulAddModFloor`: `(a * b + c) % k`, exact in `i128`.
#[no_mangle]
pub extern "C" fn fusevm_aot_muladdmod_floor(a: i64, b: i64, c: i64, k: i64) -> i64 {
    crate::floor_rem_i128(a as i128 * b as i128 + c as i128, k)
}

/// Libcall for `Op::MulModFloor`: `(a * b) % k` with the product taken exactly in
/// `i128`, so a product that leaves `i64` needs no bignum and cannot overflow.
/// `k == 0` yields `0`, matching the interpreter. Pure (no VM access).
#[no_mangle]
pub extern "C" fn fusevm_aot_mulmod_floor(a: i64, b: i64, k: i64) -> i64 {
    crate::floor_rem_i128(a as i128 * b as i128, k)
}

/// Math libcall for `Op::Atan2Float` (`y.atan2(x)`). Pure (no VM access).
#[no_mangle]
pub extern "C" fn fusevm_aot_atan2(y: f64, x: f64) -> f64 {
    y.atan2(x)
}

/// Build a Cranelift ISA for the host. `is_pic=false` matches the in-process
/// JIT (used by [`run_chunk_native`]); the object path overrides this.
fn host_isa() -> Result<cranelift_codegen::isa::OwnedTargetIsa, String> {
    let mut fb = settings::builder();
    let _ = fb.set("use_colocated_libcalls", "false");
    let _ = fb.set("is_pic", "false");
    let _ = fb.set("opt_level", "speed");
    let flags = settings::Flags::new(fb);
    let isa_builder = cranelift_native::builder().map_err(|e| format!("aot: native ISA: {e}"))?;
    isa_builder
        .finish(flags)
        .map_err(|e| format!("aot: ISA finish: {e}"))
}

/// Lower `chunk` to a native entry function in `module` and return its id. The
/// emitted function has signature `extern "C" fn(*mut VM) -> i64` (the return
/// is a status word, always 0; the run's result is left in the VM). Generic
/// over [`Module`] so JIT and object backends share this codegen.
///
/// Two codegen strategies, selected per chunk:
///
/// * `build_entry_native` — when `analyze_native` approves the whole chunk
///   (an integer/boolean computation over the supported op set, including
///   control flow and slots), every op is lowered to real native IR
///   (`iadd`/`icmp`/`brif`/…) with the operand stack and slots held in
///   registers and **no per-op interpreter dispatch**; only the final result
///   is boxed back into the VM.
/// * `build_entry_threaded` — the general fallback: one block per op, each
///   running its semantics through the [`fusevm_aot_exec_op`] shim. Handles
///   everything the native path doesn't yet cover (strings, arrays, hashes,
///   calls, float arithmetic, …).
pub fn build_entry<M: Module>(module: &mut M, chunk: &Chunk) -> Result<FuncId, String> {
    build_named(module, chunk, AOT_ENTRY_SYMBOL)
}

/// Like [`build_entry`], but exports the lowered driver under `symbol` instead of
/// the fixed [`AOT_ENTRY_SYMBOL`]. Lets a caller emit *many* chunks (a program's
/// method/block bodies) into one object, each under its own symbol, and resolve
/// them by name — see [`compile_program_object`]. Every function still lowers to
/// native code (the fully-native path when the chunk qualifies, else the threaded
/// path); only the export name differs.
/// Which lowering the AOT compiler emits for a chunk — the decision
/// [`build_named`] makes, exposed so a frontend can see (and pin in a test) what
/// its own compiler's output actually gets. Both this and `build_named` route
/// through `NativePlan::worth_lowering`, so they cannot disagree.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Lowering {
    /// Register-native: ops become real machine arithmetic, the operand stack
    /// lives in registers. `covered` of the chunk's ops lower this way and
    /// `deopts` of them exit to the interpreter; `deopts == 0` with
    /// `covered == chunk.ops.len()` means the interpreter never runs.
    Native {
        /// Ops lowered to native IR.
        covered: usize,
        /// Ops among them that exit to the interpreter at run time.
        deopts: usize,
    },
    /// One native block per op, each running that op through the runtime shim.
    /// Still machine code — the whole chunk, with no bytecode dispatch loop —
    /// but each op's *work* is a call into the runtime rather than inline IR.
    /// This is what a shell chunk gets, and what the analysis falls back to.
    Threaded,
}

/// The lowering [`build_named`] would pick for `chunk`. Cheap: it runs the
/// analysis, not codegen.
pub fn lowering_for(chunk: &Chunk) -> Lowering {
    match analyze_native(chunk) {
        Some(plan) if plan.worth_lowering(chunk) => Lowering::Native {
            covered: plan.native_ops,
            deopts: plan.deopt_points.len(),
        },
        _ => Lowering::Threaded,
    }
}

pub fn build_named<M: Module>(
    module: &mut M,
    chunk: &Chunk,
    symbol: &str,
) -> Result<FuncId, String> {
    match analyze_native(chunk) {
        // A plan that lowers too little is worse than no plan: its deopt is
        // one-way, so the interpreter gets everything after it, whereas the
        // threaded path lowers every op. See `NativePlan::worth_lowering`.
        Some(plan) if plan.worth_lowering(chunk) => {
            build_entry_native(module, chunk, &plan, symbol)
        }
        _ => build_entry_threaded(module, chunk, symbol),
    }
}

/// Abstract value kind tracked while validating a chunk for native lowering.
/// The native path must know each stack value's representation to emit the
/// right instruction (integer vs float) and the right result/branch handling,
/// and to reject divergent cases: a `Bool` fed into arithmetic or stored in a
/// slot, or left as the program result, would not match the interpreter (which
/// would float-promote, or produce a `Value::Bool`). `Int`/`Bool`/`Status` are
/// carried as `i64` at runtime; `Float` as `f64`. `Status` (an exit code from
/// `$?`) holds the code as `i64` but coerces to float in arithmetic — like the
/// interpreter's non-Int branch — boxes back as `Value::Status`, and is truthy
/// when the code is **zero** (shell success), inverted from `Int`.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Kind {
    Int,
    Bool,
    Float,
    Status,
    /// A boxed heap value (string/array/hash/…) that can't fit a scalar
    /// register. Carried as an `i64` *handle* into the VM's value arena (so it
    /// rides the `ivars`, like an integer); produced by heap sources and
    /// consumed by heap ops run through the `exec_op` shim. Not numeric and not
    /// int-like — feeding it to arithmetic/bitwise deopts.
    Obj,
}

impl Kind {
    /// Numeric kinds may feed arithmetic and comparisons (a `Bool` may not).
    /// `Status` participates as a float-coercing operand.
    fn is_numeric(self) -> bool {
        matches!(self, Kind::Int | Kind::Float | Kind::Status)
    }

    /// Int-like kinds are carried as `i64` and feed integer bitwise/shift ops
    /// and `Inc`/`Dec` directly (a `Bool`'s 0/1 equals its `to_int`, so it works
    /// unchanged); `Float`/`Status` do not (they coerce).
    fn is_intlike(self) -> bool {
        matches!(self, Kind::Int | Kind::Bool)
    }

    /// Whether an arithmetic/comparison with this operand promotes to float.
    /// Both Int ⇒ integer; anything else (Float or Status) ⇒ float.
    fn promotes_float(self) -> bool {
        !matches!(self, Kind::Int)
    }
}

/// Whether lowering this operand pair through [`load_f64`] would convert an
/// integer-carried operand to `f64` — and so round it, once it is past `2^53`.
///
/// True exactly when the pair takes the float path (one side promotes) *and*
/// some side is not already a `Float`, since that side reaches
/// `fcvt_from_sint`. `Int`/`Int` never takes the float path, and `Float`/
/// `Float` converts nothing, so both are exact and keep their native code.
///
/// Under the coercing (awk/shell) policy that rounding **is** the semantics and
/// this is never consulted — it is read only behind [`Chunk::int_overflow_deopt`],
/// the frontend's declaration that a [`crate::NumericHook`] is installed and
/// must not be bypassed by native codegen. See [`native_rounds_operand`].
fn promotion_rounds_int(a: Kind, b: Kind) -> bool {
    (a.promotes_float() || b.promotes_float()) && (a != Kind::Float || b != Kind::Float)
}

/// Whether `chunk`'s numeric policy forbids lowering this operand pair
/// natively, because doing so would answer from a rounded `f64`.
///
/// The interpreter's `arith_int_fast` / `cmp_int_fast` hand exactly these
/// operands to the [`crate::NumericHook`] — the host is the only party that can
/// represent the integer exactly. `3**34` is 16_677_181_699_666_569 and its
/// `f64` image is 16_677_181_699_666_568, so `3**34 == (3**34).to_f` is `false`
/// exactly and `true` once the integer has been rounded. Native code that
/// answered here would silently outvote the host, with no runtime opportunity
/// for it to intervene.
///
/// The decision is on the compile-time [`Kind`]s, not on a runtime value (the
/// same choice the block JIT's `pop_pair_promote` makes), so only a chunk that
/// genuinely mixes integer and float operands loses its native code. Deopting
/// is not a wrong answer, only a slower one: the resumed interpreter runs the
/// op through the hook.
///
/// Gated on [`Chunk::int_overflow_deopt`] because that flag is the only
/// build-time channel the closed-world compiler has for the numeric policy —
/// unlike the block JIT, AOT codegen runs before any `VM` exists (see
/// [`run_chunk_native`], which calls `build_entry` before `VM::new`), so the
/// thread-local `jit::strict_numeric()` is unset and unusable here.
fn native_rounds_operand(chunk: &Chunk, a: Kind, b: Kind) -> bool {
    chunk.int_overflow_deopt && promotion_rounds_int(a, b)
}

/// Record that the slot/global keyed by `key` holds kind `k` (its single kind
/// for the whole chunk, since its register has one type). Returns `false` on a
/// mixed-kind conflict (e.g. a slot stored both Int and Float), which
/// disqualifies native lowering.
fn set_var_kind(map: &mut HashMap<u32, Kind>, key: u32, k: Kind) -> bool {
    match map.get(&key) {
        Some(&existing) => existing == k,
        None => {
            map.insert(key, k);
            true
        }
    }
}

/// The kind of the slot/global keyed by `key` in a finished plan (absent ⇒ an
/// unused index ⇒ `Int`).
fn var_ty_kind(plan: &NativePlan, key: u32) -> Kind {
    plan.var_kinds.get(&key).copied().unwrap_or(Kind::Int)
}

/// Stack effect of a "boxed heap op" that the native path runs through the
/// `exec_op` shim: `(operands_popped, pushed_result_kind)`. The operands are
/// staged onto the boxed stack (scalars spilled by kind, `Obj` handles unboxed),
/// the op runs, then its result is reloaded — boxed into the arena for an `Obj`
/// result, or popped into a register for a scalar one (`None` ⇒ pushes nothing).
/// Pop counts MUST exactly match the interpreter's op (else the boxed stack
/// desyncs). `Concat` has its own arm; everything here is fixed- or
/// operand-encoded-arity with no register-resident operands of its own.
fn heap_op_effect(chunk: &Chunk, op: &Op) -> Option<(usize, Option<Kind>)> {
    Some(match op {
        // A frontend builtin: pops `argc` operands, pushes exactly one result
        // (`VM::exec_op`'s `Op::CallBuiltin` arm always pushes the handler's
        // return). The result's type is decided by the handler at run time, so
        // it comes back as a boxed `Obj` handle rather than a typed scalar.
        // Codegen has its OWN arm for this op (it must also honour the shim's
        // returned next-ip); this entry is what `analyze_native` reads for the
        // stack effect.
        //
        // Gated on `Chunk::builtin_argc_is_arity`, because `argc` is only a
        // stack arity when the frontend says so — a handler that peeks its
        // arguments instead of popping them would desync the boxed stack. Also
        // sound only in a chunk that register-caches no slot or global (a
        // handler holds `&mut VM`) — see `builtin_var_conflict`.
        Op::CallBuiltin(_, argc) if chunk.builtin_argc_is_arity => {
            (*argc as usize, Some(Kind::Obj))
        }
        Op::StringRepeat => (2, Some(Kind::Obj)),
        Op::StringLen => (1, Some(Kind::Int)),
        // String comparisons: pop 2 (stringified operands), push Bool; `StrCmp`
        // pushes Int (-1/0/1). Pure stack ops (no named var, no alias hazard).
        Op::StrEq | Op::StrNe | Op::StrLt | Op::StrGt | Op::StrLe | Op::StrGe => {
            (2, Some(Kind::Bool))
        }
        Op::StrCmp => (2, Some(Kind::Int)),
        Op::MakeArray(n) => (*n as usize, Some(Kind::Obj)),
        Op::MakeHash(n) => (*n as usize, Some(Kind::Obj)),
        Op::Range => (2, Some(Kind::Obj)),
        Op::RangeStep => (3, Some(Kind::Obj)),
        // Input + shell/string expansion sources (host-dispatched in the shim;
        // pure stack effect, push a boxed string/array). Keep these on the
        // native driver instead of the threaded fallback.
        Op::ReadLine => (0, Some(Kind::Obj)),
        Op::Glob | Op::GlobRecursive => (1, Some(Kind::Obj)),
        Op::TildeExpand | Op::BraceExpand | Op::WordSplit => (1, Some(Kind::Obj)),
        // Shell-conditional predicates → Bool: glob match (`[[ x = pat ]]`/case),
        // regex match (`=~`), file tests (`-f`/`-d`/...). Pure stack effect.
        Op::StrMatch | Op::RegexMatch => (2, Some(Kind::Bool)),
        Op::TestFile(_) => (1, Some(Kind::Bool)),
        // `${var...}` parameter expansion: pops `argc` modifier args + the name,
        // pushes the expanded value (Obj). The arg count per modifier MUST match
        // the interpreter exactly. The name is a stack value (not a global index),
        // so no register-alias hazard.
        Op::ExpandParam(m) => {
            use crate::op::param_mod::*;
            let argc = match *m {
                DEFAULT | ASSIGN | ERROR | ALTERNATE | STRIP_SHORT | STRIP_LONG | RSTRIP_SHORT
                | RSTRIP_LONG => 1,
                SUBST_FIRST | SUBST_ALL | SLICE => 2,
                _ => 0,
            };
            (argc + 1, Some(Kind::Obj))
        }
        // ── AWK string/print ops ──────────────────────────────────────────
        // Dispatched through the frontend awk host in the shim; the awk record /
        // fields / arrays live in that host (not in fusevm registers), so these
        // have a pure operand-stack effect and no register-alias hazard. Results
        // are boxed (`Obj`) since awk values carry numeric-string duality. The
        // `u8` payload is the operand count (matching the op's contract). NOT
        // differential-testable in fusevm core (needs the awk host) — verified
        // end-to-end in awkrs.
        Op::AwkToLower | Op::AwkToUpper => (1, Some(Kind::Obj)),
        Op::AwkIndex => (2, Some(Kind::Obj)),
        Op::AwkLength(n) | Op::AwkSubstr(n) | Op::AwkSprintf(n) => (*n as usize, Some(Kind::Obj)),
        Op::AwkPrint(n) | Op::AwkPrintf(n) => (*n as usize, None),
        // NOTE: `split`/`sub`/`gsub`/`match` are intentionally NOT lowered here.
        // The engine could (pure stack effect, counts boxed), but awkrs's own AOT
        // compiler rejects them upstream ("unsupported builtin call"), so they
        // can't be verified end-to-end yet. Lowering them requires the awkrs
        // frontend to emit them first — added here only once verifiable.
        // Named array/hash element ops operate on VM-scope heap state
        // (`self.globals[name]`) entirely inside the shim; the native code only
        // stages the stack operands and boxes the result. SOUND ONLY when the
        // name is not also a register-cached global — `analyze_native` bails on
        // that overlap (see `heap_op_name`).
        Op::ArrayGet(_) => (1, Some(Kind::Obj)),
        Op::ArraySet(_) => (2, None),
        Op::ArrayPush(_) => (1, None),
        Op::ArrayLen(_) => (0, Some(Kind::Int)),
        Op::ArrayPop(_) => (0, Some(Kind::Obj)),
        Op::ArrayShift(_) => (0, Some(Kind::Obj)),
        Op::HashGet(_) => (1, Some(Kind::Obj)),
        Op::HashSet(_) => (2, None),
        Op::DeclareArray(_) => (0, None),
        Op::DeclareHash(_) => (0, None),
        // Whole-array / hash-view ops, also on `self.globals[name]`.
        Op::GetArray(_) => (0, Some(Kind::Obj)),
        Op::SetArray(_) => (1, None),
        Op::HashKeys(_) => (0, Some(Kind::Obj)),
        Op::HashValues(_) => (0, Some(Kind::Obj)),
        Op::HashDelete(_) => (1, Some(Kind::Obj)),
        Op::HashExists(_) => (1, Some(Kind::Bool)),
        _ => return None,
    })
}

/// Whether `op` makes the native path hold a slot or global in a register.
/// `Op::CallBuiltin` runs a frontend handler with `&mut VM`, and both
/// [`VM::slots`] and [`VM::globals`] are public — a handler may read or write
/// either, which would leave those registers stale (or lose a write). fusevm
/// cannot see what an arbitrary frontend handler touches, so rather than assume,
/// a chunk that calls a builtin AND register-caches any variable is refused for
/// the native path entirely (`build_named` lowers it through the threaded path,
/// which keeps every variable in the VM where the handler expects it).
fn caches_var(op: &Op) -> bool {
    matches!(
        op,
        Op::GetSlot(_)
            | Op::SetSlot(_)
            | Op::PreIncSlot(_)
            | Op::PreIncSlotVoid(_)
            | Op::PreDecSlot(_)
            | Op::PostIncSlot(_)
            | Op::PostDecSlot(_)
            | Op::SlotLtIntJumpIfFalse(_, _, _)
            | Op::SlotIncLtIntJumpBack(_, _, _)
            | Op::AddAssignSlotVoid(_, _)
            | Op::AccumSumLoop(_, _, _)
            | Op::GetVar(_)
            | Op::SetVar(_)
            | Op::DeclareVar(_)
    )
}

/// Whether `chunk` mixes a builtin call with register-cached variables — the
/// unsound combination described on [`caches_var`]. Seeded slots count: the
/// driver holds those in registers too.
fn builtin_var_conflict(chunk: &Chunk) -> bool {
    // Only when builtin calls are lowered inline. With the flag off they stay
    // deopt points, and a deopt writes the registers back before the
    // interpreter runs the handler — so there is nothing to go stale.
    chunk.builtin_argc_is_arity
        && chunk
            .ops
            .iter()
            .any(|o| matches!(o, Op::CallBuiltin(_, _)))
        && (chunk.aot_seeded_slots > 0 || chunk.ops.iter().any(caches_var))
}

/// Name-pool index of a heap op that reads/writes `self.globals[name]` in the
/// shim. Such a name must NOT also be a register-cached global (the native path
/// would hold a stale register for it), so `analyze_native` bails on overlap.
fn heap_op_name(op: &Op) -> Option<u16> {
    match op {
        Op::ArrayGet(n)
        | Op::ArraySet(n)
        | Op::ArrayPush(n)
        | Op::ArrayLen(n)
        | Op::ArrayPop(n)
        | Op::ArrayShift(n)
        | Op::HashGet(n)
        | Op::HashSet(n)
        | Op::DeclareArray(n)
        | Op::DeclareHash(n)
        | Op::GetArray(n)
        | Op::SetArray(n)
        | Op::HashKeys(n)
        | Op::HashValues(n)
        | Op::HashDelete(n)
        | Op::HashExists(n) => Some(*n),
        _ => None,
    }
}

/// Imported-shim handles used to emit a deopt exit.
struct DeoptRefs {
    store_slot_int: FuncRef,
    store_slot_float: FuncRef,
    store_slot_obj: FuncRef,
    store_global_int: FuncRef,
    store_global_float: FuncRef,
    store_global_obj: FuncRef,
    push_int: FuncRef,
    push_float: FuncRef,
    push_bool: FuncRef,
    push_status: FuncRef,
    /// Unbox a register handle back onto the boxed stack (for `Kind::Obj`
    /// operand positions when spilling at a deopt).
    unbox: FuncRef,
    resume: FuncRef,
}

/// Emit a one-way deopt exit into the current block: reconstruct the full VM
/// state the resumed interpreter needs — write register-cached slots/globals
/// back to the VM, spill the live operand-stack registers (`kinds`) onto the
/// boxed stack bottom-first — then `resume` interpretation at `deopt_ip` and
/// return. The block is terminated by the `return`.
#[allow(clippy::too_many_arguments)]
fn emit_deopt(
    b: &mut FunctionBuilder,
    plan: &NativePlan,
    vm_var: Variable,
    ivars: &[Variable],
    fvars: &[Variable],
    slot_vars: &[Variable],
    global_vars: &[Variable],
    refs: &DeoptRefs,
    kinds: &[Kind],
    assigned: &BTreeSet<u32>,
    deopt_ip: usize,
) {
    // Slots/globals: write back the may-assigned ones (`assigned`), boxed per
    // kind. Each such register is a phi over incoming paths, so it holds the
    // taken path's value (or the zero-init the native code already used where the
    // slot wasn't assigned on that path); a slot never assigned on any path here
    // isn't in the set and stays `Undef` in the VM.
    for (i, &sv) in slot_vars.iter().enumerate() {
        if !assigned.contains(&(i as u32)) {
            continue;
        }
        let v = b.use_var(sv);
        let vm = b.use_var(vm_var);
        let idx = b.ins().iconst(types::I32, i as i64);
        match var_ty_kind(plan, i as u32) {
            Kind::Float => {
                b.ins().call(refs.store_slot_float, &[vm, idx, v]);
            }
            Kind::Obj => {
                b.ins().call(refs.store_slot_obj, &[vm, idx, v]);
            }
            _ => {
                b.ins().call(refs.store_slot_int, &[vm, idx, v]);
            }
        }
    }
    for (i, &gv) in global_vars.iter().enumerate() {
        if !assigned.contains(&(i as u32 | GLOBAL_TAG)) {
            continue;
        }
        let v = b.use_var(gv);
        let vm = b.use_var(vm_var);
        let idx = b.ins().iconst(types::I32, i as i64);
        match var_ty_kind(plan, i as u32 | GLOBAL_TAG) {
            Kind::Float => {
                b.ins().call(refs.store_global_float, &[vm, idx, v]);
            }
            Kind::Obj => {
                b.ins().call(refs.store_global_obj, &[vm, idx, v]);
            }
            _ => {
                b.ins().call(refs.store_global_int, &[vm, idx, v]);
            }
        }
    }
    // Operand stack: spill bottom-most first (interpreter order).
    for (pos, &k) in kinds.iter().enumerate() {
        let vm = b.use_var(vm_var);
        match k {
            Kind::Float => {
                let v = b.use_var(fvars[pos]);
                b.ins().call(refs.push_float, &[vm, v]);
            }
            Kind::Bool => {
                let v = b.use_var(ivars[pos]);
                b.ins().call(refs.push_bool, &[vm, v]);
            }
            Kind::Status => {
                let v = b.use_var(ivars[pos]);
                b.ins().call(refs.push_status, &[vm, v]);
            }
            Kind::Int => {
                let v = b.use_var(ivars[pos]);
                b.ins().call(refs.push_int, &[vm, v]);
            }
            // Obj: the register holds an arena handle; unbox it back onto the
            // boxed stack so the resumed interpreter sees the real value.
            Kind::Obj => {
                let v = b.use_var(ivars[pos]);
                b.ins().call(refs.unbox, &[vm, v]);
            }
        }
    }
    let vm = b.use_var(vm_var);
    let ipc = b.ins().iconst(types::I32, deopt_ip as i64);
    b.ins().call(refs.resume, &[vm, ipc]);
    let z = b.ins().iconst(types::I64, 0);
    b.ins().return_(&[z]);
}

/// Upper bound on distinct slots (or globals) a chunk may use and still lower
/// natively (each becomes an SSA register variable); larger spaces fall back to
/// threaded. Both stay well under `GLOBAL_TAG`, so the two index spaces never
/// collide in the unified definite-assignment set.
const NATIVE_SLOT_LIMIT: usize = 4096;

/// Tag bit OR-ed into a global's index to key it in the unified
/// definite-assignment set, keeping it disjoint from slot keys (both index
/// spaces are bounded by `NATIVE_SLOT_LIMIT`, far below this bit).
const GLOBAL_TAG: u32 = 0x1_0000;

/// A validated plan for native lowering: where the basic-block boundaries are,
/// the operand-stack *kinds* on entry to each block (length = depth), the
/// maximum stack depth (how many SSA stack variables to allocate), the stack
/// kinds when control falls off the end (whose top selects the result), and how
/// many slot register variables to allocate.
struct NativePlan {
    /// Basic-block leader ips (each becomes one Cranelift block).
    leaders: BTreeSet<usize>,
    /// Operand-stack kinds on entry to each leader (its depth is the length).
    entry_kinds: HashMap<usize, Vec<Kind>>,
    /// Maximum operand-stack depth reached anywhere (SSA var count).
    max_depth: usize,
    /// Operand-stack kinds when control falls off the end (empty ⇒ `Halted`).
    end_kinds: Vec<Kind>,
    /// Number of slot register variables to allocate (max slot index + 1).
    slot_count: usize,
    /// Number of global register variables to allocate (max global index + 1).
    global_count: usize,
    /// Kind of each slot/global (key = slot index, or global index | GLOBAL_TAG),
    /// so codegen types its register `i64` or `f64`. Absent ⇒ unused ⇒ `Int`.
    var_kinds: HashMap<u32, Kind>,
    /// Ips of non-lowerable ops reached on a native path: codegen emits a deopt
    /// there (spill state, resume the interpreter) instead of lowering the op.
    deopt_points: BTreeSet<usize>,
    /// Definite-assignment set at each ip that may emit a deopt (the
    /// `deopt_points`, plus every `Div`). Codegen writes back only these
    /// slots/globals, so an unassigned slot stays `Undef` for the resumed
    /// interpreter rather than becoming a register's zero.
    inits_at: HashMap<usize, BTreeSet<u32>>,
    /// Leading slots the caller seeds before the run (`Chunk::aot_seeded_slots`):
    /// codegen loads and integer-guards each on entry instead of zero-initing it.
    seeded_slots: u16,
    /// How many ops this plan actually lowers (reached natively and not a deopt
    /// point). With the deopt machinery a plan may cover as little as ONE op and
    /// hand the rest to the interpreter — see `NativePlan::worth_lowering`.
    native_ops: usize,
    /// Whether the natively-covered region contains a back-edge (a jump to an ip
    /// at or before itself), i.e. a loop.
    native_loop: bool,
}

impl NativePlan {
    /// Whether the native path is the better lowering for this chunk, or whether
    /// [`build_entry_threaded`] should take it instead.
    ///
    /// A deopt is one-way: everything after it is interpreted. So a plan that
    /// lowers a short prefix and deopts leaves the whole body to the interpreter,
    /// while the threaded path lowers EVERY op to a native block. The native path
    /// is the better choice when it lowers the chunk outright, when it covers a
    /// loop (where the run's time goes), or when it covers most of the ops.
    ///
    /// Without this, a plan of one op silently beat a full threaded lowering: a
    /// shell chunk begins `LoadInt` (lowerable) then `CallBuiltin`, so every zsh
    /// script compiled to a native driver of one `push_int` and a `resume`, with
    /// the entire program interpreted — machine code in name only.
    fn worth_lowering(&self, chunk: &Chunk) -> bool {
        self.deopt_points.is_empty() || self.native_loop || self.native_ops * 2 >= chunk.ops.len()
    }
}

/// Whether `chunk` lowers natively — thin wrapper over `analyze_native` used
/// by tests to assert which path a chunk takes.
#[cfg(test)]
fn native_lowerable(chunk: &Chunk) -> bool {
    analyze_native(chunk).is_some()
}

/// Control-flow successor ips of `ops[ip]` in the native CFG — the same edges
/// `analyze_native` propagates over. A deopt point has none (native execution
/// ends there). MUST stay in sync with the branch arms in `analyze_native`.
fn native_successors(chunk: &Chunk, ip: usize, deopt_points: &BTreeSet<usize>) -> Vec<usize> {
    if deopt_points.contains(&ip) {
        return Vec::new();
    }
    match &chunk.ops[ip] {
        Op::Jump(t) => vec![*t],
        Op::JumpIfTrue(t)
        | Op::JumpIfFalse(t)
        | Op::JumpIfTrueKeep(t)
        | Op::JumpIfFalseKeep(t)
        | Op::SlotLtIntJumpIfFalse(_, _, t)
        | Op::SlotIncLtIntJumpBack(_, _, t) => vec![*t, ip + 1],
        _ => vec![ip + 1],
    }
}

/// Slots/globals this op assigns, keyed as in the definite-assignment set.
/// MUST mirror the gen sites in `analyze_native`.
fn op_assigns(op: &Op, out: &mut BTreeSet<u32>) {
    match op {
        Op::SetSlot(s)
        | Op::PreIncSlot(s)
        | Op::PreDecSlot(s)
        | Op::PostIncSlot(s)
        | Op::PostDecSlot(s)
        | Op::PreIncSlotVoid(s)
        | Op::SlotIncLtIntJumpBack(s, _, _) => {
            out.insert(u32::from(*s));
        }
        Op::SetVar(g) | Op::DeclareVar(g) => {
            out.insert(u32::from(*g) | GLOBAL_TAG);
        }
        Op::AddAssignSlotVoid(a, _) => {
            out.insert(u32::from(*a));
        }
        Op::AccumSumLoop(sum, i, _) => {
            out.insert(u32::from(*sum));
            out.insert(u32::from(*i));
        }
        _ => {}
    }
}

/// May-assigned (union) dataflow: slots/globals assigned on *some* native path
/// to each ip (entry state). Where the definite-assignment set is the
/// intersection (must-assigned), this is the union. A deopt point where the two
/// differ has a conditionally-assigned slot whose register value can't be
/// soundly written back — a register can't tell a real `0` from `Undef` — so
/// such a chunk falls back to threaded.
fn may_assigned(
    chunk: &Chunk,
    n: usize,
    deopt_points: &BTreeSet<usize>,
) -> HashMap<usize, BTreeSet<u32>> {
    let mut may: HashMap<usize, BTreeSet<u32>> = HashMap::new();
    may.insert(0, BTreeSet::new());
    let mut work = vec![0usize];
    while let Some(ip) = work.pop() {
        if ip >= n {
            continue;
        }
        let mut out = may.get(&ip).cloned().unwrap_or_default();
        op_assigns(&chunk.ops[ip], &mut out);
        for s in native_successors(chunk, ip, deopt_points) {
            if s >= n {
                continue;
            }
            let newly = !may.contains_key(&s);
            let e = may.entry(s).or_default();
            let before = e.len();
            e.extend(out.iter().copied());
            // Queue on first visit (so every reachable ip is processed) or when
            // the set grows (re-converge).
            if newly || e.len() != before {
                work.push(s);
            }
        }
    }
    may
}

/// Validate `chunk` for native lowering and, if it qualifies, return a
/// `NativePlan`. Performs an abstract interpretation over the operand stack:
/// it tracks per-position `Kind`s, discovers basic-block leaders, and checks
/// that the stack state is identical on every edge into a join point (which
/// well-formed structured bytecode always satisfies). Any unsupported op, stack
/// underflow, type violation, inconsistent join, or non-integer result makes it
/// return `None`, so the threaded path takes over.
fn analyze_native(chunk: &Chunk) -> Option<NativePlan> {
    let ops = &chunk.ops;
    let n = ops.len();
    if n == 0 {
        return Some(NativePlan {
            leaders: BTreeSet::new(),
            entry_kinds: HashMap::new(),
            max_depth: 0,
            end_kinds: Vec::new(),
            slot_count: 0,
            global_count: 0,
            var_kinds: HashMap::new(),
            deopt_points: BTreeSet::new(),
            inits_at: HashMap::new(),
            seeded_slots: 0,
            native_ops: 0,
            native_loop: false,
        });
    }

    // Per-ip entry state: the operand-stack kinds and the set of slots/globals
    // that are *definitely* assigned on every path here (globals tagged with
    // `GLOBAL_TAG` to share one set). `state` doubles as the visited set; the
    // program-end pseudo-point is `n`. Kinds use must-match-exactly merging at
    // joins (structured bytecode guarantees this); init sets use intersection
    // and re-converge (a node is re-queued when its set shrinks).
    let mut state: HashMap<usize, (Vec<Kind>, BTreeSet<u32>)> = HashMap::new();
    let mut leaders: BTreeSet<usize> = BTreeSet::new();
    let mut max_depth = 0usize;
    let mut max_slot: Option<u16> = None;
    let mut max_global: Option<u16> = None;
    // Single kind per slot/global (Int or Float), determined as the analysis
    // runs; a mixed-kind variable disqualifies the chunk.
    let mut var_kind: HashMap<u32, Kind> = HashMap::new();
    // Ips of non-lowerable ops reached natively; each becomes a deopt exit.
    let mut deopt_points: BTreeSet<usize> = BTreeSet::new();
    let mut end_state: Option<Vec<Kind>> = None;

    // Seed the leading `aot_seeded_slots` as definitely-assigned integer slots on
    // entry: the caller fills them before the chunk runs, and codegen's entry guard
    // unboxes each as an `Int` (deopting to the interpreter otherwise). Speculating
    // `Int` means an integer-argument method lowers; a body that uses a seeded slot
    // with a conflicting kind (e.g. stores a `Float` to it) fails the mixed-kind
    // check below and falls back, and a non-integer argument trips the entry guard.
    let seeded = chunk.aot_seeded_slots;
    if seeded as usize > NATIVE_SLOT_LIMIT {
        return None;
    }
    // A builtin handler may touch `vm.slots`/`vm.globals` behind the native
    // path's back; refuse to hold either in a register in the same chunk.
    if builtin_var_conflict(chunk) {
        return None;
    }
    let mut entry_inits = BTreeSet::new();
    for s in 0..seeded {
        entry_inits.insert(u32::from(s));
        var_kind.insert(u32::from(s), Kind::Int);
        max_slot = Some(max_slot.map_or(s, |m| m.max(s)));
    }
    state.insert(0, (Vec::new(), entry_inits));
    leaders.insert(0);
    let mut work = vec![0usize];

    while let Some(ip) = work.pop() {
        let (mut st, inits) = state.get(&ip)?.clone();
        max_depth = max_depth.max(st.len());

        // In a per-op arm: when the op can't be lowered with these operand kinds,
        // make it a deopt point (the interpreter runs it) rather than bailing the
        // whole chunk. `continue` skips the (now empty) successor propagation.
        macro_rules! deopt_unless {
            ($ok:expr) => {
                if !($ok) {
                    deopt_points.insert(ip);
                    continue;
                }
            };
        }

        // Bound the slot/global spaces we'd materialize as registers — over
        // every slot or global any op touches.
        let mut tslot = [0u16; 2];
        let mut nslot = 0usize;
        let mut tglob: Option<u16> = None;
        match &ops[ip] {
            Op::GetSlot(s)
            | Op::SetSlot(s)
            | Op::PreIncSlot(s)
            | Op::PreIncSlotVoid(s)
            | Op::PreDecSlot(s)
            | Op::PostIncSlot(s)
            | Op::PostDecSlot(s)
            | Op::SlotLtIntJumpIfFalse(s, _, _)
            | Op::SlotIncLtIntJumpBack(s, _, _) => {
                tslot[0] = *s;
                nslot = 1;
            }
            Op::AddAssignSlotVoid(a, b) | Op::AccumSumLoop(a, b, _) => {
                tslot[0] = *a;
                tslot[1] = *b;
                nslot = 2;
            }
            Op::GetVar(g) | Op::SetVar(g) | Op::DeclareVar(g) => tglob = Some(*g),
            _ => {}
        }
        for &s in &tslot[..nslot] {
            if s as usize >= NATIVE_SLOT_LIMIT {
                return None;
            }
            max_slot = Some(max_slot.map_or(s, |m| m.max(s)));
        }
        if let Some(g) = tglob {
            if g as usize >= NATIVE_SLOT_LIMIT {
                return None;
            }
            max_global = Some(max_global.map_or(g, |m| m.max(g)));
        }

        // Successors after applying ops[ip]: (successor ip, kinds, init set).
        let mut succs: Vec<(usize, Vec<Kind>, BTreeSet<u32>)> = Vec::new();
        match &ops[ip] {
            Op::Nop => succs.push((ip + 1, st, inits)),
            Op::LoadInt(_) => {
                st.push(Kind::Int);
                max_depth = max_depth.max(st.len());
                succs.push((ip + 1, st, inits));
            }
            // Read a slot's value with the slot's tracked kind (Int or Float).
            Op::GetSlot(s) => {
                let k = var_kind.get(&u32::from(*s)).copied().unwrap_or(Kind::Int);
                st.push(k);
                max_depth = max_depth.max(st.len());
                succs.push((ip + 1, st, inits));
            }
            // Read a global's value with its tracked kind.
            Op::GetVar(g) => {
                let k = var_kind
                    .get(&(u32::from(*g) | GLOBAL_TAG))
                    .copied()
                    .unwrap_or(Kind::Int);
                st.push(k);
                max_depth = max_depth.max(st.len());
                succs.push((ip + 1, st, inits));
            }
            Op::LoadFloat(_) => {
                st.push(Kind::Float);
                max_depth = max_depth.max(st.len());
                succs.push((ip + 1, st, inits));
            }
            Op::LoadConst(idx) => {
                // Numeric constants lower; a heap constant (string, etc.) is a
                // deopt point — the interpreter pushes it and runs on from there.
                match chunk.constants.get(*idx as usize)? {
                    Value::Int(_) => {
                        st.push(Kind::Int);
                        max_depth = max_depth.max(st.len());
                        succs.push((ip + 1, st, inits));
                    }
                    Value::Float(_) => {
                        st.push(Kind::Float);
                        max_depth = max_depth.max(st.len());
                        succs.push((ip + 1, st, inits));
                    }
                    // A heap constant (string/array/…) loads as a boxed handle:
                    // the shim pushes the real value, the native code boxes it
                    // into the arena and threads the handle (`Kind::Obj`).
                    _ => {
                        st.push(Kind::Obj);
                        max_depth = max_depth.max(st.len());
                        succs.push((ip + 1, st, inits));
                    }
                }
            }
            // String concat: pops two values (scalar or boxed), pushes a boxed
            // string. Runs through the shim — scalar operands are spilled by
            // kind, boxed operands are unboxed — then the result is re-boxed.
            Op::Concat => {
                let _b = st.pop()?;
                let _a = st.pop()?;
                st.push(Kind::Obj);
                max_depth = max_depth.max(st.len());
                succs.push((ip + 1, st, inits));
            }
            // Arithmetic promotes: result is Float if either operand is Float,
            // else Int — mirroring the interpreter's `arith_int_fast`.
            Op::Add | Op::Sub | Op::Mul => {
                let b = st.pop()?;
                let a = st.pop()?;
                deopt_unless!(a.is_numeric() && b.is_numeric());
                // A mixed pair under a numeric hook rounds the integer operand;
                // the interpreter delegates it, so native code must not answer.
                deopt_unless!(!native_rounds_operand(chunk, a, b));
                let r = if a.promotes_float() || b.promotes_float() {
                    Kind::Float
                } else {
                    Kind::Int
                };
                st.push(r);
                succs.push((ip + 1, st, inits));
            }
            // Modulo: both Int → integer `srem` (with the `y==0 → 0` guard);
            // otherwise the float branch via an `fmod` libcall. (Bool operands
            // fall back, like the other arithmetic.)
            Op::Mod => {
                let b = st.pop()?;
                let a = st.pop()?;
                deopt_unless!(a.is_numeric() && b.is_numeric());
                // `Op::Mod` routes through the same `arith_int_fast` as
                // `Add`/`Sub`/`Mul`, so it delegates on the same condition.
                deopt_unless!(!native_rounds_operand(chunk, a, b));
                let r = if a.promotes_float() || b.promotes_float() {
                    Kind::Float
                } else {
                    Kind::Int
                };
                st.push(r);
                succs.push((ip + 1, st, inits));
            }
            // Division: always Float, except divide-by-zero yields `Undef` — a
            // value the register model can't represent, so the zero case takes a
            // deopt exit (rare). Operands must be numeric.
            Op::Div => {
                let b = st.pop()?;
                let a = st.pop()?;
                deopt_unless!(a.is_numeric() && b.is_numeric());
                st.push(Kind::Float);
                succs.push((ip + 1, st, inits));
            }
            // `a.powf(b)` — always Float, via a `powf` libcall.
            Op::Pow => {
                let b = st.pop()?;
                let a = st.pop()?;
                deopt_unless!(a.is_numeric() && b.is_numeric());
                st.push(Kind::Float);
                succs.push((ip + 1, st, inits));
            }
            // Unary float math (single instructions + transcendental libcalls):
            // operand coerced to f64, result Float.
            Op::AbsFloat
            | Op::SqrtFloat
            | Op::CeilFloat
            | Op::FloorFloat
            | Op::TruncFloat
            | Op::RoundFloat
            | Op::SinFloat
            | Op::CosFloat
            | Op::TanFloat
            | Op::AsinFloat
            | Op::AcosFloat
            | Op::AtanFloat
            | Op::SinhFloat
            | Op::CoshFloat
            | Op::TanhFloat
            | Op::ExpFloat
            | Op::LogFloat
            | Op::Log2Float
            | Op::Log10Float => {
                deopt_unless!(st.pop()?.is_numeric());
                st.push(Kind::Float);
                succs.push((ip + 1, st, inits));
            }
            // `to_int` of the operand → Int (Float truncates toward zero,
            // saturating like `f as i64`).
            Op::TruncInt => {
                deopt_unless!(st.pop()?.is_numeric());
                st.push(Kind::Int);
                succs.push((ip + 1, st, inits));
            }
            // Integer absolute value (wrapping); int-like operand only.
            Op::AbsInt => {
                deopt_unless!(st.pop()?.is_intlike());
                st.push(Kind::Int);
                succs.push((ip + 1, st, inits));
            }
            // Binary float math libcalls → Float.
            Op::PowFloat | Op::Atan2Float => {
                let b = st.pop()?;
                let a = st.pop()?;
                deopt_unless!(a.is_numeric() && b.is_numeric());
                st.push(Kind::Float);
                succs.push((ip + 1, st, inits));
            }
            // Fused exact `(a * b) % k` → Int. Int-like operands only: a float
            // operand would take the interpreter's unfused `Mul`/`Mod` path, so
            // it deopts instead of silently truncating.
            Op::MulModFloor => {
                let k = st.pop()?;
                let b = st.pop()?;
                let a = st.pop()?;
                deopt_unless!(a.is_intlike() && b.is_intlike() && k.is_intlike());
                st.push(Kind::Int);
                succs.push((ip + 1, st, inits));
            }
            Op::MulAddModFloor => {
                let k = st.pop()?;
                let c = st.pop()?;
                let b = st.pop()?;
                let a = st.pop()?;
                deopt_unless!(a.is_intlike() && b.is_intlike() && c.is_intlike() && k.is_intlike());
                st.push(Kind::Int);
                succs.push((ip + 1, st, inits));
            }
            // gcd/lcm via internal Euclid loops on magnitudes → Int. Int-like
            // operands only (the interpreter's `to_int().unsigned_abs()`).
            Op::GcdInt | Op::LcmInt => {
                let b = st.pop()?;
                let a = st.pop()?;
                deopt_unless!(a.is_intlike() && b.is_intlike());
                st.push(Kind::Int);
                succs.push((ip + 1, st, inits));
            }
            // awk `/` and `%` — float result, but a runtime error (not `Undef`)
            // on a zero divisor, so they get a native error-return branch. The
            // Jit and non-Jit variants are identical in shape and message.
            Op::AwkDivJit | Op::AwkModJit | Op::AwkDiv | Op::AwkMod => {
                let b = st.pop()?;
                let a = st.pop()?;
                deopt_unless!(a.is_numeric() && b.is_numeric());
                st.push(Kind::Float);
                succs.push((ip + 1, st, inits));
            }
            // awk lshift/rshift — two operands, error on negatives, Float result.
            Op::AwkLshiftJit | Op::AwkRshiftJit => {
                let b = st.pop()?;
                let a = st.pop()?;
                deopt_unless!(a.is_numeric() && b.is_numeric());
                st.push(Kind::Float);
                succs.push((ip + 1, st, inits));
            }
            // awk compl — one operand, error on negative, Float result.
            Op::AwkComplJit => {
                deopt_unless!(st.pop()?.is_numeric());
                st.push(Kind::Float);
                succs.push((ip + 1, st, inits));
            }
            // awk sqrt/log — one operand, Float result. Negative input warns to
            // stderr and yields NaN (no error/halt), so no fallback is needed.
            Op::AwkSqrtJit | Op::AwkLogJit => {
                deopt_unless!(st.pop()?.is_numeric());
                st.push(Kind::Float);
                succs.push((ip + 1, st, inits));
            }
            // Sink ops (inline/shim boundary): consume `n` scalars and push
            // nothing, straight-line. The spilled operands are scalar by
            // construction (everything on the native stack is), so no reload or
            // type guard is needed — just spill and let the interpreter run it.
            Op::Print(n) | Op::PrintLn(n) => {
                let n = *n as usize;
                if st.len() < n {
                    return None;
                }
                st.truncate(st.len() - n);
                succs.push((ip + 1, st, inits));
            }
            // `SetStatus` is a silent sink: pop one scalar, set `$?`, push none.
            Op::SetStatus => {
                if st.pop().is_none() {
                    return None;
                }
                succs.push((ip + 1, st, inits));
            }
            // Source op (reload half): awk numeric `$N` field read. Pops nothing,
            // pushes a value that is *statically* Float, so it reloads into a
            // float register with no type guard.
            Op::AwkGetFieldNum(_) => {
                st.push(Kind::Float);
                max_depth = max_depth.max(st.len());
                succs.push((ip + 1, st, inits));
            }
            Op::Negate => {
                let a = st.pop()?;
                deopt_unless!(a.is_numeric());
                // Int → Int; Float/Status → Float (`-to_float`).
                st.push(if a == Kind::Int {
                    Kind::Int
                } else {
                    Kind::Float
                });
                succs.push((ip + 1, st, inits));
            }
            // `$?` — always pushes a `Status` value; reloads into a register.
            Op::GetStatus => {
                st.push(Kind::Status);
                max_depth = max_depth.max(st.len());
                succs.push((ip + 1, st, inits));
            }
            Op::NumEq | Op::NumNe | Op::NumLt | Op::NumGt | Op::NumLe | Op::NumGe => {
                let b = st.pop()?;
                let a = st.pop()?;
                deopt_unless!(a.is_numeric() && b.is_numeric());
                // Comparison answers about the *rounded* operand otherwise —
                // `cmp_int_fast` hands exactly this pair to the host.
                deopt_unless!(!native_rounds_operand(chunk, a, b));
                st.push(Kind::Bool);
                succs.push((ip + 1, st, inits));
            }
            Op::LoadTrue | Op::LoadFalse => {
                st.push(Kind::Bool);
                max_depth = max_depth.max(st.len());
                succs.push((ip + 1, st, inits));
            }
            // Inc/Dec coerce to int and yield int; int-like operands only.
            Op::Inc | Op::Dec => {
                deopt_unless!(st.pop()?.is_intlike());
                st.push(Kind::Int);
                succs.push((ip + 1, st, inits));
            }
            // `!truthy` for any kind → Bool.
            Op::LogNot => {
                st.pop()?;
                st.push(Kind::Bool);
                succs.push((ip + 1, st, inits));
            }
            // Ruby truthiness of any lowered kind → Bool. A native register is
            // never `nil`/`Undef` (those deopt), so a numeric operand is always
            // truthy and a `Bool` operand is already the answer.
            Op::RubyTruthy => {
                st.pop()?;
                st.push(Kind::Bool);
                succs.push((ip + 1, st, inits));
            }
            // Bitwise/shift: int-like operands (carried as i64), int result.
            Op::BitAnd | Op::BitOr | Op::BitXor | Op::Shl | Op::Shr => {
                let b = st.pop()?;
                let a = st.pop()?;
                deopt_unless!(a.is_intlike() && b.is_intlike());
                st.push(Kind::Int);
                succs.push((ip + 1, st, inits));
            }
            Op::BitNot => {
                deopt_unless!(st.pop()?.is_intlike());
                st.push(Kind::Int);
                succs.push((ip + 1, st, inits));
            }
            // Stack shuffles — kind-preserving permutations of the top values.
            Op::Dup2 => {
                let len = st.len();
                if len < 2 {
                    return None;
                }
                let a = st[len - 2];
                let b = st[len - 1];
                st.push(a);
                st.push(b);
                max_depth = max_depth.max(st.len());
                succs.push((ip + 1, st, inits));
            }
            Op::Swap => {
                let len = st.len();
                if len < 2 {
                    return None;
                }
                st.swap(len - 1, len - 2);
                succs.push((ip + 1, st, inits));
            }
            Op::Rot => {
                let len = st.len();
                if len < 3 {
                    return None;
                }
                // [a, b, c] → [b, c, a]
                st.swap(len - 3, len - 2);
                st.swap(len - 2, len - 1);
                succs.push((ip + 1, st, inits));
            }
            // Logical and/or: truthiness of both operands → Bool (not
            // short-circuit — both operands are already evaluated on the stack).
            Op::LogAnd | Op::LogOr => {
                st.pop()?;
                st.pop()?;
                st.push(Kind::Bool);
                succs.push((ip + 1, st, inits));
            }
            // Three-way compare → Int (-1 / 0 / 1).
            Op::Spaceship => {
                let b = st.pop()?;
                let a = st.pop()?;
                deopt_unless!(a.is_numeric() && b.is_numeric());
                st.push(Kind::Int);
                succs.push((ip + 1, st, inits));
            }
            // Branch on truthiness but keep the value on the stack (both arms).
            Op::JumpIfTrueKeep(t) | Op::JumpIfFalseKeep(t) => {
                let t = *t;
                if t > n {
                    return None;
                }
                if st.is_empty() {
                    return None;
                }
                if t < n {
                    leaders.insert(t);
                }
                if ip + 1 < n {
                    leaders.insert(ip + 1);
                }
                succs.push((t, st.clone(), inits.clone()));
                succs.push((ip + 1, st, inits));
            }
            // Slot read-modify-write super-ops. They read via `to_int` (so an
            // unassigned slot coerces to 0, matching a zero-init register — no
            // definite-assignment needed), write an int slot (gen), and push the
            // new (Pre*) or old (Post*) value as Int. Int slots only.
            Op::PreIncSlot(s) | Op::PreDecSlot(s) | Op::PostIncSlot(s) | Op::PostDecSlot(s) => {
                if !set_var_kind(&mut var_kind, u32::from(*s), Kind::Int) {
                    return None; // these ops are int-only
                }
                let mut ni = inits;
                ni.insert(u32::from(*s));
                st.push(Kind::Int);
                max_depth = max_depth.max(st.len());
                succs.push((ip + 1, st, ni));
            }
            Op::PreIncSlotVoid(s) => {
                if !set_var_kind(&mut var_kind, u32::from(*s), Kind::Int) {
                    return None;
                }
                let mut ni = inits;
                ni.insert(u32::from(*s));
                succs.push((ip + 1, st, ni));
            }
            Op::AddAssignSlotVoid(a, b) => {
                if !set_var_kind(&mut var_kind, u32::from(*a), Kind::Int)
                    || !set_var_kind(&mut var_kind, u32::from(*b), Kind::Int)
                {
                    return None;
                }
                let mut ni = inits;
                ni.insert(u32::from(*a)); // writes slot a (reads a and b coerce)
                succs.push((ip + 1, st, ni));
            }
            // Runs a whole `while i < limit { sum += i; i += 1 }` internally;
            // straight-line from the outside (one successor). Writes both slots.
            Op::AccumSumLoop(sum_s, i_s, _limit) => {
                if !set_var_kind(&mut var_kind, u32::from(*sum_s), Kind::Int)
                    || !set_var_kind(&mut var_kind, u32::from(*i_s), Kind::Int)
                {
                    return None;
                }
                let mut ni = inits;
                ni.insert(u32::from(*sum_s));
                ni.insert(u32::from(*i_s));
                succs.push((ip + 1, st, ni));
            }
            // Fused loop ops: a slot/limit comparison branch and an
            // increment-compare-jumpback. No stack change; the read coerces.
            Op::SlotLtIntJumpIfFalse(s, _limit, t) => {
                if !set_var_kind(&mut var_kind, u32::from(*s), Kind::Int) {
                    return None;
                }
                let t = *t;
                if t > n {
                    return None;
                }
                if t < n {
                    leaders.insert(t);
                }
                if ip + 1 < n {
                    leaders.insert(ip + 1);
                }
                succs.push((t, st.clone(), inits.clone()));
                succs.push((ip + 1, st, inits));
            }
            Op::SlotIncLtIntJumpBack(s, _limit, t) => {
                if !set_var_kind(&mut var_kind, u32::from(*s), Kind::Int) {
                    return None;
                }
                let t = *t;
                if t > n {
                    return None;
                }
                let mut ni = inits;
                ni.insert(u32::from(*s)); // increments (writes) slot s
                if t < n {
                    leaders.insert(t);
                }
                if ip + 1 < n {
                    leaders.insert(ip + 1);
                }
                succs.push((t, st.clone(), ni.clone()));
                succs.push((ip + 1, st, ni));
            }
            // Store a slot. The operand must be Int or Float (Bool/etc. aren't
            // storable as a typed register); its kind becomes the slot's kind.
            Op::SetSlot(s) => {
                let k = st.pop()?;
                deopt_unless!(k == Kind::Int || k == Kind::Float || k == Kind::Obj);
                let key = u32::from(*s);
                if !set_var_kind(&mut var_kind, key, k) {
                    return None; // mixed-kind slot
                }
                let mut ni = inits;
                ni.insert(key); // this slot is now definitely assigned
                succs.push((ip + 1, st, ni));
            }
            // Globals mirror slots: `SetVar`/`DeclareVar` store Int, Float, or a
            // boxed `Obj` and set the global's kind; `GetVar` (above) reads it.
            Op::SetVar(g) | Op::DeclareVar(g) => {
                let k = st.pop()?;
                deopt_unless!(k == Kind::Int || k == Kind::Float || k == Kind::Obj);
                let key = u32::from(*g) | GLOBAL_TAG;
                if !set_var_kind(&mut var_kind, key, k) {
                    return None;
                }
                let mut ni = inits;
                ni.insert(key);
                succs.push((ip + 1, st, ni));
            }
            Op::Pop => {
                st.pop()?;
                succs.push((ip + 1, st, inits));
            }
            Op::Dup => {
                let k = *st.last()?;
                // Duplicating a boxed handle would alias one arena slot to two
                // owners (double-free on consume); deopt instead.
                deopt_unless!(k != Kind::Obj);
                st.push(k);
                max_depth = max_depth.max(st.len());
                succs.push((ip + 1, st, inits));
            }
            Op::Jump(t) => {
                let t = *t;
                if t > n {
                    return None;
                }
                if t < n {
                    leaders.insert(t);
                }
                succs.push((t, st, inits));
            }
            Op::JumpIfTrue(t) | Op::JumpIfFalse(t) => {
                let t = *t;
                if t > n {
                    return None;
                }
                st.pop()?; // condition consumed (truthiness works for any kind)
                if t < n {
                    leaders.insert(t);
                }
                if ip + 1 < n {
                    leaders.insert(ip + 1);
                }
                succs.push((t, st.clone(), inits.clone()));
                succs.push((ip + 1, st, inits));
            }
            // Boxed heap ops (string repeat/len, array/hash construction,
            // ranges): pop their operands, push the result kind (Obj or scalar).
            // Run through the shim with box/unbox staging in codegen.
            op if heap_op_effect(chunk, op).is_some() => {
                let (pops, res) = heap_op_effect(chunk, op).unwrap();
                for _ in 0..pops {
                    st.pop()?;
                }
                if let Some(k) = res {
                    st.push(k);
                    max_depth = max_depth.max(st.len());
                }
                succs.push((ip + 1, st, inits));
            }
            // A non-lowerable op: deopt here rather than bail the whole chunk.
            // Don't propagate past it — the resumed interpreter owns everything
            // after this point on this path. `state[ip]` already holds the entry
            // (kinds, inits) the deopt needs; `succs` stays empty.
            _ => {
                deopt_points.insert(ip);
            }
        }

        for (s, sk, si) in succs {
            if s == n {
                match &end_state {
                    None => end_state = Some(sk),
                    Some(prev) if *prev != sk => return None,
                    Some(_) => {}
                }
                continue;
            }
            match state.get_mut(&s) {
                None => {
                    state.insert(s, (sk, si));
                    work.push(s);
                }
                Some((pk, pi)) => {
                    if *pk != sk {
                        return None;
                    }
                    // Intersect the init-slot sets; re-converge if this shrank it.
                    let before = pi.len();
                    pi.retain(|x| si.contains(x));
                    if pi.len() != before {
                        work.push(s);
                    }
                }
            }
        }
    }

    // Definite assignment: every reachable `GetSlot`/`GetVar` must read a
    // slot/global assigned on all paths to it. Otherwise the interpreter would
    // observe `Undef` where the native path would see a register, so we fall
    // back. (The slot/global RMW ops coerce via `to_int`, so they don't need
    // this — an unassigned read coerces to 0, matching a zero-init register.)
    for (&ip, (_kinds, inits)) in &state {
        match &ops[ip] {
            Op::GetSlot(s) if !inits.contains(&u32::from(*s)) => return None,
            Op::GetVar(g) if !inits.contains(&(u32::from(*g) | GLOBAL_TAG)) => return None,
            _ => {}
        }
    }

    // Named array/hash ops read/write `self.globals[name]` inside the shim. If
    // that same name is also a register-cached global (`GetVar`/`SetVar`), the
    // register holds the live value and the shim would see a stale
    // `self.globals[name]` — so bail to the threaded path on any such overlap.
    for &ip in state.keys() {
        if let Some(name) = heap_op_name(&ops[ip]) {
            if var_kind.contains_key(&(u32::from(name) | GLOBAL_TAG)) {
                return None;
            }
        }
    }

    // The result is the stack top when control falls off the end; it must be
    // numeric (a `Bool` there would box to the wrong `Value` variant).
    let end_kinds = match end_state {
        Some(es) => {
            if let Some(top) = es.last() {
                // A numeric or boxed (`Obj`) top can be boxed as the result; a
                // bare `Bool` cannot (it would box to the wrong Value variant).
                if !top.is_numeric() && *top != Kind::Obj {
                    return None;
                }
            }
            es
        }
        // No path reaches the end (e.g. an infinite loop): the ret block is
        // unreachable, so there is no result to box.
        None => Vec::new(),
    };

    // If every reachable op is a deopt point there is nothing to lower, so the
    // threaded path (no deopt overhead) is strictly better.
    if state.len() == deopt_points.len() {
        return None;
    }

    // Slots/globals to spill at each ip that may emit a deopt (the deopt points
    // plus every `Div`, whose divide-by-zero deopts): the **may**-assigned set —
    // every slot/global assigned on *some* path here. A deopt resumes the
    // interpreter, which reads these from the VM; `emit_deopt` writes each one
    // back from its register. That register is a phi over all incoming paths, so
    // it already holds the taken path's value — an assigned value, or the
    // zero-init the native code itself used for a slot not yet assigned on that
    // path — so the resumed VM stays consistent with native execution. (Spilling
    // only the *definitely*-assigned set instead forced a bail whenever any slot
    // was merely maybe-assigned — e.g. a variable written inside a loop and live
    // at a later deopt such as the program's final builtin call — which sent
    // every such chunk, including all nested-loop programs, to the interpreter.)
    // A deopt exit needs `may`-assigned state to spill the live slots/globals so
    // the resumed interpreter sees them. `deopt_points` alone under-counts the
    // ops that deopt at runtime: `Div` (divide-by-zero) and — with the opt-in
    // flag — every integer `Add`/`Sub`/`Mul` (overflow) also deopt but are NOT in
    // `deopt_points`. Compute `may` whenever any of those are present, else the
    // spill set is empty and a slot set natively is lost across the deopt.
    let has_ovf_arith =
        chunk.int_overflow_deopt && ops.iter().any(|o| matches!(o, Op::Add | Op::Sub | Op::Mul));
    let has_div = ops.iter().any(|o| matches!(o, Op::Div));
    // `CallBuiltin` also deopts at run time — on the cold path where the handler
    // halted the VM or moved `vm.ip` — without being in `deopt_points`.
    let has_builtin = ops.iter().any(|o| matches!(o, Op::CallBuiltin(_, _)));
    let may = if deopt_points.is_empty() && !has_ovf_arith && !has_div && !has_builtin {
        HashMap::new()
    } else {
        may_assigned(chunk, n, &deopt_points)
    };
    let mut inits_at: HashMap<usize, BTreeSet<u32>> = HashMap::new();
    for &ip in state.keys() {
        if deopt_points.contains(&ip)
            || matches!(ops[ip], Op::Div | Op::CallBuiltin(_, _))
            || (chunk.int_overflow_deopt && matches!(ops[ip], Op::Add | Op::Sub | Op::Mul))
        {
            inits_at.insert(ip, may.get(&ip).cloned().unwrap_or_default());
        }
    }

    let entry_kinds = leaders
        .iter()
        .map(|&l| (l, state.get(&l).map(|(k, _)| k.clone()).unwrap_or_default()))
        .collect();
    let slot_count = max_slot.map_or(0, |m| m as usize + 1);
    let global_count = max_global.map_or(0, |m| m as usize + 1);

    // Coverage, for `NativePlan::worth_lowering`: the ops this plan really
    // lowers, and whether any of them jumps backwards (a loop).
    let native_ops = state
        .keys()
        .filter(|ip| !deopt_points.contains(ip))
        .count();
    let native_loop = state.keys().any(|&ip| {
        !deopt_points.contains(&ip)
            && native_successors(chunk, ip, &deopt_points)
                .iter()
                .any(|&s| s <= ip)
    });

    Some(NativePlan {
        leaders,
        entry_kinds,
        max_depth,
        end_kinds,
        slot_count,
        global_count,
        var_kinds: var_kind,
        deopt_points,
        inits_at,
        seeded_slots: seeded,
        native_ops,
        native_loop,
    })
}

/// Native fast path: emit real integer/float IR for a chunk that
/// `analyze_native` has approved, following the `plan` it produced. One
/// Cranelift block per basic-block leader; the operand stack lives in frontend
/// [`Variable`]s (so SSA/phi construction at joins — including loop back-edges —
/// is automatic on `seal_all_blocks`).
///
/// Each stack position has two parallel variables — one `i64` (`Kind::Int`
/// and `Kind::Bool`) and one `f64` (`Kind::Float`) — and codegen replays
/// the plan's per-position `Kind`s to read/write the right one and to insert
/// `int→float` promotions where the interpreter would. Integer slots get their
/// own SSA register variables (analysis proved definite assignment). Only the
/// final result is boxed back into the VM — no per-op dispatch, no per-op
/// shim calls.
fn build_entry_native<M: Module>(
    module: &mut M,
    chunk: &Chunk,
    plan: &NativePlan,
    symbol: &str,
) -> Result<FuncId, String> {
    let ptr_ty = module.target_config().pointer_type();

    // Imported typed shims.
    let mut ires_sig = module.make_signature();
    ires_sig.params.push(AbiParam::new(ptr_ty));
    ires_sig.params.push(AbiParam::new(types::I64));
    let ires_id = module
        .declare_function("fusevm_aot_set_int_result", Linkage::Import, &ires_sig)
        .map_err(|e| format!("aot: declare set_int_result: {e}"))?;

    let mut fres_sig = module.make_signature();
    fres_sig.params.push(AbiParam::new(ptr_ty));
    fres_sig.params.push(AbiParam::new(types::F64));
    let fres_id = module
        .declare_function("fusevm_aot_set_float_result", Linkage::Import, &fres_sig)
        .map_err(|e| format!("aot: declare set_float_result: {e}"))?;

    let mut serr_sig = module.make_signature();
    serr_sig.params.push(AbiParam::new(ptr_ty));
    serr_sig.params.push(AbiParam::new(types::I32));
    let serr_id = module
        .declare_function("fusevm_aot_set_error", Linkage::Import, &serr_sig)
        .map_err(|e| format!("aot: declare set_error: {e}"))?;

    // Math libcalls: fn(f64, f64) -> f64 (powf, fmod).
    let mut math_sig = module.make_signature();
    math_sig.params.push(AbiParam::new(types::F64));
    math_sig.params.push(AbiParam::new(types::F64));
    math_sig.returns.push(AbiParam::new(types::F64));
    let powf_id = module
        .declare_function("fusevm_aot_powf", Linkage::Import, &math_sig)
        .map_err(|e| format!("aot: declare powf: {e}"))?;
    let fmod_id = module
        .declare_function("fusevm_aot_fmod", Linkage::Import, &math_sig)
        .map_err(|e| format!("aot: declare fmod: {e}"))?;
    let atan2_id = module
        .declare_function("fusevm_aot_atan2", Linkage::Import, &math_sig)
        .map_err(|e| format!("aot: declare atan2: {e}"))?;

    // Fused exact `(a*b) % k`: fn(i64, i64, i64) -> i64.
    let mut mulmod_sig = module.make_signature();
    mulmod_sig.params.push(AbiParam::new(types::I64));
    mulmod_sig.params.push(AbiParam::new(types::I64));
    mulmod_sig.params.push(AbiParam::new(types::I64));
    mulmod_sig.returns.push(AbiParam::new(types::I64));
    let mulmod_id = module
        .declare_function("fusevm_aot_mulmod_floor", Linkage::Import, &mulmod_sig)
        .map_err(|e| format!("aot: declare mulmod_floor: {e}"))?;

    // Fused exact `(a*b + c) % k`: fn(i64, i64, i64, i64) -> i64.
    let mut muladdmod_sig = module.make_signature();
    for _ in 0..4 {
        muladdmod_sig.params.push(AbiParam::new(types::I64));
    }
    muladdmod_sig.returns.push(AbiParam::new(types::I64));
    let muladdmod_id = module
        .declare_function(
            "fusevm_aot_muladdmod_floor",
            Linkage::Import,
            &muladdmod_sig,
        )
        .map_err(|e| format!("aot: declare muladdmod_floor: {e}"))?;

    // Unary transcendental dispatcher: fn(u32 id, f64) -> f64.
    let mut unary_sig = module.make_signature();
    unary_sig.params.push(AbiParam::new(types::I32));
    unary_sig.params.push(AbiParam::new(types::F64));
    unary_sig.returns.push(AbiParam::new(types::F64));
    let unary_id = module
        .declare_function("fusevm_aot_unary_math", Linkage::Import, &unary_sig)
        .map_err(|e| format!("aot: declare unary_math: {e}"))?;

    // awk negative-arg warning: fn(u32 code, f64) -> ().
    let mut warn_sig = module.make_signature();
    warn_sig.params.push(AbiParam::new(types::I32));
    warn_sig.params.push(AbiParam::new(types::F64));
    let warn_id = module
        .declare_function("fusevm_aot_awk_warn", Linkage::Import, &warn_sig)
        .map_err(|e| format!("aot: declare awk_warn: {e}"))?;

    // Inline/shim boundary: the per-op interpreter step (for sink ops) and the
    // scalar spill helpers that stage operands onto the boxed stack first.
    let mut exec_sig = module.make_signature();
    exec_sig.params.push(AbiParam::new(ptr_ty));
    exec_sig.params.push(AbiParam::new(types::I64));
    exec_sig.returns.push(AbiParam::new(types::I64));
    let exec_id = module
        .declare_function("fusevm_aot_exec_op", Linkage::Import, &exec_sig)
        .map_err(|e| format!("aot: declare exec_op: {e}"))?;

    let mut pushi_sig = module.make_signature();
    pushi_sig.params.push(AbiParam::new(ptr_ty));
    pushi_sig.params.push(AbiParam::new(types::I64));
    let pushi_id = module
        .declare_function("fusevm_aot_push_int", Linkage::Import, &pushi_sig)
        .map_err(|e| format!("aot: declare push_int: {e}"))?;
    let pushb_id = module
        .declare_function("fusevm_aot_push_bool", Linkage::Import, &pushi_sig)
        .map_err(|e| format!("aot: declare push_bool: {e}"))?;

    let mut pushf_sig = module.make_signature();
    pushf_sig.params.push(AbiParam::new(ptr_ty));
    pushf_sig.params.push(AbiParam::new(types::F64));
    let pushf_id = module
        .declare_function("fusevm_aot_push_float", Linkage::Import, &pushf_sig)
        .map_err(|e| format!("aot: declare push_float: {e}"))?;

    let mut popf_sig = module.make_signature();
    popf_sig.params.push(AbiParam::new(ptr_ty));
    popf_sig.returns.push(AbiParam::new(types::F64));
    let popf_id = module
        .declare_function("fusevm_aot_pop_float", Linkage::Import, &popf_sig)
        .map_err(|e| format!("aot: declare pop_float: {e}"))?;

    // Boxed-value (heap) shims: box (vm) -> i64 handle, unbox/set_obj_result
    // (vm, i64). `unbox`/`set_obj_result` share `pushi_sig` ((vm, i64)).
    let mut box_sig = module.make_signature();
    box_sig.params.push(AbiParam::new(ptr_ty));
    box_sig.returns.push(AbiParam::new(types::I64));
    let box_id = module
        .declare_function("fusevm_aot_box", Linkage::Import, &box_sig)
        .map_err(|e| format!("aot: declare box: {e}"))?;
    let unbox_id = module
        .declare_function("fusevm_aot_unbox", Linkage::Import, &pushi_sig)
        .map_err(|e| format!("aot: declare unbox: {e}"))?;
    let obj_res_id = module
        .declare_function("fusevm_aot_set_obj_result", Linkage::Import, &pushi_sig)
        .map_err(|e| format!("aot: declare set_obj_result: {e}"))?;

    // Deopt writeback shims: store_slot/global (vm, i32, i64|f64) and
    // resume (vm, i32).
    let mut ssi_sig = module.make_signature();
    ssi_sig.params.push(AbiParam::new(ptr_ty));
    ssi_sig.params.push(AbiParam::new(types::I32));
    ssi_sig.params.push(AbiParam::new(types::I64));
    let ssi_id = module
        .declare_function("fusevm_aot_store_slot_int", Linkage::Import, &ssi_sig)
        .map_err(|e| format!("aot: declare store_slot_int: {e}"))?;
    let sgi_id = module
        .declare_function("fusevm_aot_store_global_int", Linkage::Import, &ssi_sig)
        .map_err(|e| format!("aot: declare store_global_int: {e}"))?;

    let mut ssf_sig = module.make_signature();
    ssf_sig.params.push(AbiParam::new(ptr_ty));
    ssf_sig.params.push(AbiParam::new(types::I32));
    ssf_sig.params.push(AbiParam::new(types::F64));
    let ssf_id = module
        .declare_function("fusevm_aot_store_slot_float", Linkage::Import, &ssf_sig)
        .map_err(|e| format!("aot: declare store_slot_float: {e}"))?;
    let sgf_id = module
        .declare_function("fusevm_aot_store_global_float", Linkage::Import, &ssf_sig)
        .map_err(|e| format!("aot: declare store_global_float: {e}"))?;
    // Obj (boxed) slot/global writeback share the (vm, i32, i64) shape.
    let ssobj_id = module
        .declare_function("fusevm_aot_store_slot_obj", Linkage::Import, &ssi_sig)
        .map_err(|e| format!("aot: declare store_slot_obj: {e}"))?;
    let sgobj_id = module
        .declare_function("fusevm_aot_store_global_obj", Linkage::Import, &ssi_sig)
        .map_err(|e| format!("aot: declare store_global_obj: {e}"))?;

    // Heap-handle ownership shims: clone (vm, i64) -> i64, free (vm, i64).
    let mut clone_sig = module.make_signature();
    clone_sig.params.push(AbiParam::new(ptr_ty));
    clone_sig.params.push(AbiParam::new(types::I64));
    clone_sig.returns.push(AbiParam::new(types::I64));
    let clone_id = module
        .declare_function("fusevm_aot_clone", Linkage::Import, &clone_sig)
        .map_err(|e| format!("aot: declare clone: {e}"))?;
    let free_id = module
        .declare_function("fusevm_aot_free", Linkage::Import, &pushi_sig)
        .map_err(|e| format!("aot: declare free: {e}"))?;
    // Boxed-value truthiness, (vm, i64 handle) -> i64 — same shape as `clone`.
    let truthy_id = module
        .declare_function("fusevm_aot_truthy", Linkage::Import, &clone_sig)
        .map_err(|e| format!("aot: declare truthy: {e}"))?;
    let truthy_keep_id = module
        .declare_function("fusevm_aot_truthy_keep", Linkage::Import, &clone_sig)
        .map_err(|e| format!("aot: declare truthy_keep: {e}"))?;
    // `finish` (vm) -> (): the stack-tail result rule, needed on the one native
    // exit that leaves the result on the BOXED stack — a builtin that halted the
    // VM (see the `Op::CallBuiltin` arm). Every other exit reports its result
    // from a register through `set_*_result`.
    let mut fin_sig = module.make_signature();
    fin_sig.params.push(AbiParam::new(ptr_ty));
    let fin_id = module
        .declare_function("fusevm_aot_finish", Linkage::Import, &fin_sig)
        .map_err(|e| format!("aot: declare finish: {e}"))?;

    let mut resume_sig = module.make_signature();
    resume_sig.params.push(AbiParam::new(ptr_ty));
    resume_sig.params.push(AbiParam::new(types::I32));
    let resume_id = module
        .declare_function("fusevm_aot_resume", Linkage::Import, &resume_sig)
        .map_err(|e| format!("aot: declare resume: {e}"))?;

    // Entry-guard shims: load_slot_int (vm, i32) -> i64 and guard_taken (vm) -> i64.
    let mut lsi_sig = module.make_signature();
    lsi_sig.params.push(AbiParam::new(ptr_ty));
    lsi_sig.params.push(AbiParam::new(types::I32));
    lsi_sig.returns.push(AbiParam::new(types::I64));
    let lsi_id = module
        .declare_function("fusevm_aot_load_slot_int", Linkage::Import, &lsi_sig)
        .map_err(|e| format!("aot: declare load_slot_int: {e}"))?;
    let mut guard_sig = module.make_signature();
    guard_sig.params.push(AbiParam::new(ptr_ty));
    guard_sig.returns.push(AbiParam::new(types::I64));
    let guard_id = module
        .declare_function("fusevm_aot_guard_taken", Linkage::Import, &guard_sig)
        .map_err(|e| format!("aot: declare guard_taken: {e}"))?;

    // Status source/result: pop_int (vm) -> i64, push_status/set_status_result
    // (vm, i64).
    let mut popi_sig = module.make_signature();
    popi_sig.params.push(AbiParam::new(ptr_ty));
    popi_sig.returns.push(AbiParam::new(types::I64));
    let popi_id = module
        .declare_function("fusevm_aot_pop_int", Linkage::Import, &popi_sig)
        .map_err(|e| format!("aot: declare pop_int: {e}"))?;
    let push_status_id = module
        .declare_function("fusevm_aot_push_status", Linkage::Import, &pushi_sig)
        .map_err(|e| format!("aot: declare push_status: {e}"))?;
    let sres_id = module
        .declare_function("fusevm_aot_set_status_result", Linkage::Import, &pushi_sig)
        .map_err(|e| format!("aot: declare set_status_result: {e}"))?;

    // Exported entry: fn(vm) -> i64.
    let mut entry_sig = module.make_signature();
    entry_sig.params.push(AbiParam::new(ptr_ty));
    entry_sig.returns.push(AbiParam::new(types::I64));
    let entry_id = module
        .declare_function(symbol, Linkage::Export, &entry_sig)
        .map_err(|e| format!("aot: declare entry: {e}"))?;

    let mut ctx = module.make_context();
    ctx.func.signature = entry_sig;
    let mut fbctx = FunctionBuilderContext::new();
    {
        let mut b = FunctionBuilder::new(&mut ctx.func, &mut fbctx);
        let ires_ref = module.declare_func_in_func(ires_id, b.func);
        let fres_ref = module.declare_func_in_func(fres_id, b.func);
        let serr_ref = module.declare_func_in_func(serr_id, b.func);
        let powf_ref = module.declare_func_in_func(powf_id, b.func);
        let fmod_ref = module.declare_func_in_func(fmod_id, b.func);
        let mulmod_ref = module.declare_func_in_func(mulmod_id, b.func);
        let muladdmod_ref = module.declare_func_in_func(muladdmod_id, b.func);
        let atan2_ref = module.declare_func_in_func(atan2_id, b.func);
        let unary_ref = module.declare_func_in_func(unary_id, b.func);
        let warn_ref = module.declare_func_in_func(warn_id, b.func);
        let exec_ref = module.declare_func_in_func(exec_id, b.func);
        let pushi_ref = module.declare_func_in_func(pushi_id, b.func);
        let pushb_ref = module.declare_func_in_func(pushb_id, b.func);
        let pushf_ref = module.declare_func_in_func(pushf_id, b.func);
        let popf_ref = module.declare_func_in_func(popf_id, b.func);
        let popi_ref = module.declare_func_in_func(popi_id, b.func);
        let sres_ref = module.declare_func_in_func(sres_id, b.func);
        let push_status_ref = module.declare_func_in_func(push_status_id, b.func);
        let box_ref = module.declare_func_in_func(box_id, b.func);
        let unbox_ref = module.declare_func_in_func(unbox_id, b.func);
        let obj_res_ref = module.declare_func_in_func(obj_res_id, b.func);
        let clone_ref = module.declare_func_in_func(clone_id, b.func);
        let free_ref = module.declare_func_in_func(free_id, b.func);
        let truthy_ref = module.declare_func_in_func(truthy_id, b.func);
        let truthy_keep_ref = module.declare_func_in_func(truthy_keep_id, b.func);
        let fin_ref = module.declare_func_in_func(fin_id, b.func);
        let lsi_ref = module.declare_func_in_func(lsi_id, b.func);
        let guard_ref = module.declare_func_in_func(guard_id, b.func);
        let resume_ref = module.declare_func_in_func(resume_id, b.func);
        let deopt_refs = DeoptRefs {
            store_slot_int: module.declare_func_in_func(ssi_id, b.func),
            store_slot_float: module.declare_func_in_func(ssf_id, b.func),
            store_slot_obj: module.declare_func_in_func(ssobj_id, b.func),
            store_global_int: module.declare_func_in_func(sgi_id, b.func),
            store_global_float: module.declare_func_in_func(sgf_id, b.func),
            store_global_obj: module.declare_func_in_func(sgobj_id, b.func),
            push_int: pushi_ref,
            push_float: pushf_ref,
            push_bool: pushb_ref,
            push_status: push_status_ref,
            unbox: unbox_ref,
            resume: resume_ref,
        };

        // The VM pointer and each operand-stack position are frontend Variables;
        // `seal_all_blocks` then builds the SSA phis at all joins for us. Each
        // stack position has an i64 var and an f64 var; the plan's Kinds say
        // which is live (consistent across all edges into any join). Slots are
        // int-only register variables (definite-assignment proven by analysis).
        let vm_var = b.declare_var(ptr_ty);
        let ivars: Vec<Variable> = (0..plan.max_depth)
            .map(|_| b.declare_var(types::I64))
            .collect();
        let fvars: Vec<Variable> = (0..plan.max_depth)
            .map(|_| b.declare_var(types::F64))
            .collect();
        // Slots/globals are typed per the plan's kind (i64 or f64).
        let var_ty = |key: u32| -> types::Type {
            if plan.var_kinds.get(&key) == Some(&Kind::Float) {
                types::F64
            } else {
                types::I64
            }
        };
        let slot_vars: Vec<Variable> = (0..plan.slot_count)
            .map(|i| b.declare_var(var_ty(i as u32)))
            .collect();
        let global_vars: Vec<Variable> = (0..plan.global_count)
            .map(|i| b.declare_var(var_ty(i as u32 | GLOBAL_TAG)))
            .collect();

        let n = chunk.ops.len();
        let entry_block = b.create_block();
        b.append_block_params_for_function_params(entry_block);
        let ret_block = b.create_block();
        let blocks: HashMap<usize, Block> = plan
            .leaders
            .iter()
            .map(|&l| (l, b.create_block()))
            .collect();

        // Resolve a branch/fallthrough target to its block (program end → ret).
        let block_for = |ip: usize| -> Block {
            if ip >= n {
                ret_block
            } else {
                blocks[&ip]
            }
        };

        // Entry: stash the VM pointer, zero-init slot/global registers (definite
        // assignment guarantees this 0 is never actually read; it only keeps the
        // variables defined on every edge for Cranelift's SSA construction),
        // then jump to the first block (or ret if empty).
        b.switch_to_block(entry_block);
        let vm_param = b.block_params(entry_block)[0];
        b.def_var(vm_var, vm_param);
        let seeded = plan.seeded_slots as usize;
        if !slot_vars.is_empty() || !global_vars.is_empty() {
            let zi = b.ins().iconst(types::I64, 0);
            let zf = b.ins().f64const(Ieee64::with_bits(0f64.to_bits()));
            // `Obj` slots start at the -1 "empty handle" sentinel so the first
            // `SetSlot` frees nothing; scalar slots zero-init (never read before
            // assignment, per definite-assignment).
            let neg1 = b.ins().iconst(types::I64, -1);
            for (i, &sv) in slot_vars.iter().enumerate() {
                if i < seeded {
                    // Seeded param slot: load its (speculated integer) value from
                    // the VM frame. The shim flags a guard failure when the slot
                    // isn't an `Int`; that is checked once below.
                    let vm = b.use_var(vm_var);
                    let idx = b.ins().iconst(types::I32, i as i64);
                    let call = b.ins().call(lsi_ref, &[vm, idx]);
                    let v = b.inst_results(call)[0];
                    b.def_var(sv, v);
                } else {
                    let init = match var_ty_kind(plan, i as u32) {
                        Kind::Obj => neg1,
                        Kind::Float => zf,
                        _ => zi,
                    };
                    b.def_var(sv, init);
                }
            }
            for (i, &gv) in global_vars.iter().enumerate() {
                let init = match var_ty_kind(plan, i as u32 | GLOBAL_TAG) {
                    Kind::Obj => neg1,
                    Kind::Float => zf,
                    _ => zi,
                };
                b.def_var(gv, init);
            }
        }
        let start_target = if n == 0 { ret_block } else { blocks[&0] };
        if seeded > 0 {
            // A seeded slot held a non-`Int` value: deopt by running the
            // interpreter from ip 0 (it reads the boxed slots directly) and
            // returning. The driver's return value is ignored — the result is the
            // `aot_result` the resumed interpreter stores — so this must NOT flow
            // through `ret_block`, which would re-box the (uncomputed) native
            // end-state register over the interpreter's result.
            let guard_deopt = b.create_block();
            let vm = b.use_var(vm_var);
            let call = b.ins().call(guard_ref, &[vm]);
            let failed = b.inst_results(call)[0];
            b.ins().brif(failed, guard_deopt, &[], start_target, &[]);
            b.switch_to_block(guard_deopt);
            let vm = b.use_var(vm_var);
            let zero = b.ins().iconst(types::I32, 0);
            b.ins().call(resume_ref, &[vm, zero]);
            let status = b.ins().iconst(types::I64, 0);
            b.ins().return_(&[status]);
            b.seal_block(guard_deopt);
        } else {
            b.ins().jump(start_target, &[]);
        }

        // `kinds` is the compile-time operand-stack kind stack (its length is
        // the depth). Push: write var at index `kinds.len()`, then push a Kind.
        // Pop: pop a Kind, then read the var at the new `kinds.len()`. All the
        // `unwrap`s / index math are sound: `analyze_native` proved depths, the
        // op set, and the per-position kinds.
        let mut kinds: Vec<Kind> = Vec::new();
        let mut terminated = true; // no open block yet
        for ip in 0..n {
            if let Some(&blk) = blocks.get(&ip) {
                if !terminated {
                    b.ins().jump(blk, &[]); // fallthrough into the leader
                }
                b.switch_to_block(blk);
                kinds = plan.entry_kinds[&ip].clone();
                terminated = false;
            } else if terminated {
                continue; // unreachable op after an unconditional terminator
            }

            // Non-lowerable op reached natively: deopt here (reconstruct state,
            // resume the interpreter) instead of lowering it. Handled uniformly
            // for every deopt point — string/heap ops and heap constant loads.
            if plan.deopt_points.contains(&ip) {
                emit_deopt(
                    &mut b,
                    plan,
                    vm_var,
                    &ivars,
                    &fvars,
                    &slot_vars,
                    &global_vars,
                    &deopt_refs,
                    &kinds,
                    &plan.inits_at[&ip],
                    ip,
                );
                terminated = true;
                continue;
            }

            match &chunk.ops[ip] {
                Op::Nop => {}
                Op::LoadInt(k) => {
                    let v = b.ins().iconst(types::I64, *k);
                    let idx = kinds.len();
                    b.def_var(ivars[idx], v);
                    kinds.push(Kind::Int);
                }
                Op::LoadFloat(f) => {
                    let v = b.ins().f64const(Ieee64::with_bits(f.to_bits()));
                    let idx = kinds.len();
                    b.def_var(fvars[idx], v);
                    kinds.push(Kind::Float);
                }
                Op::LoadConst(ci) => {
                    let idx = kinds.len();
                    match &chunk.constants[*ci as usize] {
                        Value::Int(c) => {
                            let v = b.ins().iconst(types::I64, *c);
                            b.def_var(ivars[idx], v);
                            kinds.push(Kind::Int);
                        }
                        Value::Float(f) => {
                            let v = b.ins().f64const(Ieee64::with_bits(f.to_bits()));
                            b.def_var(fvars[idx], v);
                            kinds.push(Kind::Float);
                        }
                        // Heap constant: run the shim (pushes the real Value onto
                        // the boxed stack), then box it into the arena and thread
                        // the handle through a register as `Kind::Obj`.
                        _ => {
                            let vm = b.use_var(vm_var);
                            let ipc = b.ins().iconst(types::I64, ip as i64);
                            b.ins().call(exec_ref, &[vm, ipc]);
                            let vm2 = b.use_var(vm_var);
                            let call = b.ins().call(box_ref, &[vm2]);
                            let h = b.inst_results(call)[0];
                            b.def_var(ivars[idx], h);
                            kinds.push(Kind::Obj);
                        }
                    }
                }
                // Concat: spill both operands onto the boxed stack (scalars by
                // kind, `Obj` handles unboxed), run the shim (pops 2, pushes the
                // joined string), then box the result handle. Net: pop 2, push 1
                // `Obj`. vm.stack is empty before and after.
                Op::Concat => {
                    let ky = kinds.pop().unwrap();
                    let iy = kinds.len();
                    let kx = kinds.pop().unwrap();
                    let ix = kinds.len();
                    // Bottom-most first (interpreter order): x then y.
                    for (pos, k) in [(ix, kx), (iy, ky)] {
                        let vm = b.use_var(vm_var);
                        match k {
                            Kind::Float => {
                                let v = b.use_var(fvars[pos]);
                                b.ins().call(pushf_ref, &[vm, v]);
                            }
                            Kind::Bool => {
                                let v = b.use_var(ivars[pos]);
                                b.ins().call(pushb_ref, &[vm, v]);
                            }
                            Kind::Status => {
                                let v = b.use_var(ivars[pos]);
                                b.ins().call(push_status_ref, &[vm, v]);
                            }
                            Kind::Int => {
                                let v = b.use_var(ivars[pos]);
                                b.ins().call(pushi_ref, &[vm, v]);
                            }
                            Kind::Obj => {
                                let v = b.use_var(ivars[pos]);
                                b.ins().call(unbox_ref, &[vm, v]);
                            }
                        }
                    }
                    let vm = b.use_var(vm_var);
                    let ipc = b.ins().iconst(types::I64, ip as i64);
                    b.ins().call(exec_ref, &[vm, ipc]);
                    let vm2 = b.use_var(vm_var);
                    let call = b.ins().call(box_ref, &[vm2]);
                    let h = b.inst_results(call)[0];
                    b.def_var(ivars[ix], h);
                    kinds.push(Kind::Obj);
                }
                // A frontend builtin. Same spill/run/reload boundary as the heap
                // ops below — stage `argc` operands onto the boxed stack, run the
                // handler through the shim, box its pushed result back into a
                // register — with one addition: the shim's RETURN VALUE is
                // honoured. `VM::aot_exec_op` answers the next ip, and a builtin
                // is the one shimmed op that can change it: a handler may halt
                // the VM (`VM::request_halt` — a shell `exit`) or store a result,
                // which answers -1, and a handler that moves `vm.ip` answers some
                // other target. Ignoring that (as the straight-line heap ops do)
                // would run on past a halt.
                //
                // Sound because `analyze_native` refuses any chunk that mixes a
                // builtin with register-cached slots/globals (`caches_var`), so
                // there is no variable state a handler could stale — only the
                // operand-stack registers, which the cold path spills.
                Op::CallBuiltin(_, argc) if chunk.builtin_argc_is_arity => {
                    let pops = *argc as usize;
                    let base = kinds.len() - pops;
                    // Operands, bottom-most first (the interpreter's order).
                    for pos in base..kinds.len() {
                        let vm = b.use_var(vm_var);
                        match kinds[pos] {
                            Kind::Float => {
                                let v = b.use_var(fvars[pos]);
                                b.ins().call(pushf_ref, &[vm, v]);
                            }
                            Kind::Bool => {
                                let v = b.use_var(ivars[pos]);
                                b.ins().call(pushb_ref, &[vm, v]);
                            }
                            Kind::Status => {
                                let v = b.use_var(ivars[pos]);
                                b.ins().call(push_status_ref, &[vm, v]);
                            }
                            Kind::Int => {
                                let v = b.use_var(ivars[pos]);
                                b.ins().call(pushi_ref, &[vm, v]);
                            }
                            Kind::Obj => {
                                let v = b.use_var(ivars[pos]);
                                b.ins().call(unbox_ref, &[vm, v]);
                            }
                        }
                    }
                    let vm = b.use_var(vm_var);
                    let ipc = b.ins().iconst(types::I64, ip as i64);
                    let call = b.ins().call(exec_ref, &[vm, ipc]);
                    let next = b.inst_results(call)[0];
                    kinds.truncate(base);

                    // -1 ⇒ the run is over and `aot_exec_op` already stored the
                    // result; return without touching `aot_result`.
                    let halt_blk = b.create_block();
                    let check_blk = b.create_block();
                    let moved_blk = b.create_block();
                    let cont_blk = b.create_block();
                    let is_halt = b.ins().icmp_imm(IntCC::SignedLessThan, next, 0);
                    b.ins().brif(is_halt, halt_blk, &[], check_blk, &[]);

                    b.switch_to_block(halt_blk);
                    // The op ended the run. Its result — an `ExecFlow::Ret`
                    // value the shim already stored, or whatever the handler
                    // left on the boxed stack before halting — is reported by
                    // `finish`, exactly as the threaded path's ret block does.
                    // Returning without it would answer `Halted` where the
                    // interpreter answers the handler's value.
                    let vmm = b.use_var(vm_var);
                    b.ins().call(fin_ref, &[vmm]);
                    let z = b.ins().iconst(types::I64, 0);
                    b.ins().return_(&[z]);

                    // Anything but `ip + 1` means the handler moved `vm.ip`:
                    // hand the rest of the run to the interpreter there.
                    b.switch_to_block(check_blk);
                    let straight = b.ins().icmp_imm(IntCC::Equal, next, ip as i64 + 1);
                    b.ins().brif(straight, cont_blk, &[], moved_blk, &[]);

                    b.switch_to_block(moved_blk);
                    // The result is on the boxed stack, ABOVE the operands the
                    // registers still hold. Lift it into the arena, spill the
                    // register stack underneath it, then put it back on top so
                    // the interpreter resumes on the stack the op really left.
                    let vmm = b.use_var(vm_var);
                    let call = b.ins().call(box_ref, &[vmm]);
                    let saved = b.inst_results(call)[0];
                    for (pos, &k) in kinds.iter().enumerate() {
                        let vm = b.use_var(vm_var);
                        match k {
                            Kind::Float => {
                                let v = b.use_var(fvars[pos]);
                                b.ins().call(pushf_ref, &[vm, v]);
                            }
                            Kind::Bool => {
                                let v = b.use_var(ivars[pos]);
                                b.ins().call(pushb_ref, &[vm, v]);
                            }
                            Kind::Status => {
                                let v = b.use_var(ivars[pos]);
                                b.ins().call(push_status_ref, &[vm, v]);
                            }
                            Kind::Int => {
                                let v = b.use_var(ivars[pos]);
                                b.ins().call(pushi_ref, &[vm, v]);
                            }
                            Kind::Obj => {
                                let v = b.use_var(ivars[pos]);
                                b.ins().call(unbox_ref, &[vm, v]);
                            }
                        }
                    }
                    let vmm = b.use_var(vm_var);
                    b.ins().call(unbox_ref, &[vmm, saved]);
                    let vmm = b.use_var(vm_var);
                    let nip = b.ins().ireduce(types::I32, next);
                    b.ins().call(resume_ref, &[vmm, nip]);
                    let z = b.ins().iconst(types::I64, 0);
                    b.ins().return_(&[z]);

                    // Straight-line path: box the pushed result into a register.
                    b.switch_to_block(cont_blk);
                    let vmm = b.use_var(vm_var);
                    let call = b.ins().call(box_ref, &[vmm]);
                    b.def_var(ivars[base], b.inst_results(call)[0]);
                    kinds.push(Kind::Obj);
                }
                // Boxed heap ops (string repeat/len, array/hash construction,
                // ranges): stage the top `pops` operands onto the boxed stack
                // (scalars by kind, Obj handles unboxed; bottom-most first), run
                // the shim, then reload the result (box an Obj, pop a scalar).
                op if heap_op_effect(chunk, op).is_some() => {
                    let (pops, res) = heap_op_effect(chunk, op).unwrap();
                    let base = kinds.len() - pops;
                    for pos in base..kinds.len() {
                        let vm = b.use_var(vm_var);
                        match kinds[pos] {
                            Kind::Float => {
                                let v = b.use_var(fvars[pos]);
                                b.ins().call(pushf_ref, &[vm, v]);
                            }
                            Kind::Bool => {
                                let v = b.use_var(ivars[pos]);
                                b.ins().call(pushb_ref, &[vm, v]);
                            }
                            Kind::Status => {
                                let v = b.use_var(ivars[pos]);
                                b.ins().call(push_status_ref, &[vm, v]);
                            }
                            Kind::Int => {
                                let v = b.use_var(ivars[pos]);
                                b.ins().call(pushi_ref, &[vm, v]);
                            }
                            Kind::Obj => {
                                let v = b.use_var(ivars[pos]);
                                b.ins().call(unbox_ref, &[vm, v]);
                            }
                        }
                    }
                    let vm = b.use_var(vm_var);
                    let ipc = b.ins().iconst(types::I64, ip as i64);
                    b.ins().call(exec_ref, &[vm, ipc]);
                    kinds.truncate(base);
                    match res {
                        Some(Kind::Obj) => {
                            let vm2 = b.use_var(vm_var);
                            let call = b.ins().call(box_ref, &[vm2]);
                            b.def_var(ivars[base], b.inst_results(call)[0]);
                            kinds.push(Kind::Obj);
                        }
                        Some(Kind::Float) => {
                            let vm2 = b.use_var(vm_var);
                            let call = b.ins().call(popf_ref, &[vm2]);
                            b.def_var(fvars[base], b.inst_results(call)[0]);
                            kinds.push(Kind::Float);
                        }
                        // Int/Bool/Status scalar result: pop into an i64 register
                        // under its exact kind (e.g. StringLen→Int, HashExists→Bool).
                        Some(k) => {
                            let vm2 = b.use_var(vm_var);
                            let call = b.ins().call(popi_ref, &[vm2]);
                            b.def_var(ivars[base], b.inst_results(call)[0]);
                            kinds.push(k);
                        }
                        None => {}
                    }
                }
                // `a` is the lower slot, `b`(=y) the top; matches the
                // interpreter's `arith_int_fast` (int if both Int, else float).
                op @ (Op::Add | Op::Sub | Op::Mul) => {
                    // Peek operand positions/kinds WITHOUT popping: an
                    // overflow-checked int op may deopt (like `Div`), and
                    // `emit_deopt` needs the operands still on the abstract stack.
                    let d = kinds.len();
                    let ky = kinds[d - 1];
                    let iy = d - 1;
                    let kx = kinds[d - 2];
                    let ix = d - 2;
                    if ky.promotes_float() || kx.promotes_float() {
                        let y = load_f64(&mut b, &ivars, &fvars, iy, ky);
                        let x = load_f64(&mut b, &ivars, &fvars, ix, kx);
                        let r = match op {
                            Op::Add => b.ins().fadd(x, y),
                            Op::Sub => b.ins().fsub(x, y),
                            _ => b.ins().fmul(x, y),
                        };
                        kinds.pop();
                        kinds.pop();
                        b.def_var(fvars[ix], r);
                        kinds.push(Kind::Float);
                    } else if chunk.int_overflow_deopt {
                        // Overflow-checked integer op: native i64 on the common
                        // path; on signed i64 overflow, deopt to the interpreter
                        // (which runs the op through the frontend's numeric hook —
                        // e.g. Python bignum promotion) instead of wrapping.
                        let y = b.use_var(ivars[iy]);
                        let x = b.use_var(ivars[ix]);
                        let (r, overflow) = match op {
                            // add overflow: (x^r) & (y^r) < 0
                            Op::Add => {
                                let r = b.ins().iadd(x, y);
                                let xr = b.ins().bxor(x, r);
                                let yr = b.ins().bxor(y, r);
                                let a = b.ins().band(xr, yr);
                                (r, b.ins().icmp_imm(IntCC::SignedLessThan, a, 0))
                            }
                            // sub overflow: (x^y) & (x^r) < 0
                            Op::Sub => {
                                let r = b.ins().isub(x, y);
                                let xy = b.ins().bxor(x, y);
                                let xr = b.ins().bxor(x, r);
                                let a = b.ins().band(xy, xr);
                                (r, b.ins().icmp_imm(IntCC::SignedLessThan, a, 0))
                            }
                            // mul overflow: high half != sign-extension of low half
                            _ => {
                                let r = b.ins().imul(x, y);
                                let hi = b.ins().smulhi(x, y);
                                let sr = b.ins().sshr_imm(r, 63);
                                (r, b.ins().icmp(IntCC::NotEqual, hi, sr))
                            }
                        };
                        let deopt_blk = b.create_block();
                        let ok_blk = b.create_block();
                        b.ins().brif(overflow, deopt_blk, &[], ok_blk, &[]);

                        b.switch_to_block(deopt_blk);
                        emit_deopt(
                            &mut b,
                            plan,
                            vm_var,
                            &ivars,
                            &fvars,
                            &slot_vars,
                            &global_vars,
                            &deopt_refs,
                            &kinds,
                            &plan.inits_at[&ip],
                            ip,
                        );

                        b.switch_to_block(ok_blk);
                        kinds.pop();
                        kinds.pop();
                        b.def_var(ivars[ix], r);
                        kinds.push(Kind::Int);
                    } else {
                        let y = b.use_var(ivars[iy]);
                        let x = b.use_var(ivars[ix]);
                        let r = match op {
                            Op::Add => b.ins().iadd(x, y),
                            Op::Sub => b.ins().isub(x, y),
                            _ => b.ins().imul(x, y),
                        };
                        kinds.pop();
                        kinds.pop();
                        b.def_var(ivars[ix], r);
                        kinds.push(Kind::Int);
                    }
                }
                // Modulo. Both Int → integer `srem`, guarding the two divisors
                // that would trap natively but yield 0 in the interpreter:
                // `y == 0` (its explicit `→ 0`) and `y == -1` (where
                // `x % -1 == 0` for all x, also dodging the `srem(INT_MIN, -1)`
                // overflow trap). Otherwise the float branch via `fmod`.
                Op::Mod => {
                    let ky = kinds.pop().unwrap();
                    let iy = kinds.len();
                    let kx = kinds.pop().unwrap();
                    let ix = kinds.len();
                    if ky.promotes_float() || kx.promotes_float() {
                        let y = load_f64(&mut b, &ivars, &fvars, iy, ky);
                        let x = load_f64(&mut b, &ivars, &fvars, ix, kx);
                        let call = b.ins().call(fmod_ref, &[x, y]);
                        let r = b.inst_results(call)[0];
                        b.def_var(fvars[ix], r);
                        kinds.push(Kind::Float);
                    } else {
                        let y = b.use_var(ivars[iy]);
                        let x = b.use_var(ivars[ix]);
                        let zero = b.ins().iconst(types::I64, 0);
                        let one = b.ins().iconst(types::I64, 1);
                        let y_is_zero = b.ins().icmp_imm(IntCC::Equal, y, 0);
                        let y_is_neg1 = b.ins().icmp_imm(IntCC::Equal, y, -1);
                        let special = b.ins().bor(y_is_zero, y_is_neg1);
                        let safe_y = b.ins().select(special, one, y);
                        let rem = b.ins().srem(x, safe_y);
                        let r = b.ins().select(special, zero, rem);
                        b.def_var(ivars[ix], r);
                        kinds.push(Kind::Int);
                    }
                }
                // Division: native `fdiv` on the common (nonzero-divisor) path;
                // a divide-by-zero deopts to the interpreter (which produces the
                // `Undef` the register model can't hold). Operands are NOT popped
                // before the guard, so the deopt spills the full stack and the
                // interpreter re-executes the Div at `ip`.
                Op::Div => {
                    let d = kinds.len();
                    let ky = kinds[d - 1];
                    let kx = kinds[d - 2];
                    let divisor = load_f64(&mut b, &ivars, &fvars, d - 1, ky);
                    let dividend = load_f64(&mut b, &ivars, &fvars, d - 2, kx);
                    let zero = b.ins().f64const(Ieee64::with_bits(0f64.to_bits()));
                    let is_zero = b.ins().fcmp(FloatCC::Equal, divisor, zero);
                    let deopt_blk = b.create_block();
                    let ok_blk = b.create_block();
                    b.ins().brif(is_zero, deopt_blk, &[], ok_blk, &[]);

                    b.switch_to_block(deopt_blk);
                    emit_deopt(
                        &mut b,
                        plan,
                        vm_var,
                        &ivars,
                        &fvars,
                        &slot_vars,
                        &global_vars,
                        &deopt_refs,
                        &kinds,
                        &plan.inits_at[&ip],
                        ip,
                    );

                    b.switch_to_block(ok_blk);
                    let q = b.ins().fdiv(dividend, divisor);
                    kinds.pop();
                    kinds.pop();
                    let ix = kinds.len();
                    b.def_var(fvars[ix], q);
                    kinds.push(Kind::Float);
                }
                // Power: always Float, via the `powf` libcall.
                Op::Pow => {
                    let ky = kinds.pop().unwrap();
                    let iy = kinds.len();
                    let kx = kinds.pop().unwrap();
                    let ix = kinds.len();
                    let y = load_f64(&mut b, &ivars, &fvars, iy, ky);
                    let x = load_f64(&mut b, &ivars, &fvars, ix, kx);
                    let call = b.ins().call(powf_ref, &[x, y]);
                    let r = b.inst_results(call)[0];
                    b.def_var(fvars[ix], r);
                    kinds.push(Kind::Float);
                }
                // Unary float math: single Cranelift instructions where they
                // exist (`RoundFloat`'s round-ties-even is `nearest`).
                op @ (Op::AbsFloat
                | Op::SqrtFloat
                | Op::CeilFloat
                | Op::FloorFloat
                | Op::TruncFloat
                | Op::RoundFloat) => {
                    let ka = kinds.pop().unwrap();
                    let idx = kinds.len();
                    let x = load_f64(&mut b, &ivars, &fvars, idx, ka);
                    let r = match op {
                        Op::AbsFloat => b.ins().fabs(x),
                        Op::SqrtFloat => b.ins().sqrt(x),
                        Op::CeilFloat => b.ins().ceil(x),
                        Op::FloorFloat => b.ins().floor(x),
                        Op::TruncFloat => b.ins().trunc(x),
                        _ => b.ins().nearest(x), // RoundFloat (ties to even)
                    };
                    b.def_var(fvars[idx], r);
                    kinds.push(Kind::Float);
                }
                // Unary transcendentals: one shared libcall, keyed by id.
                op @ (Op::SinFloat
                | Op::CosFloat
                | Op::TanFloat
                | Op::AsinFloat
                | Op::AcosFloat
                | Op::AtanFloat
                | Op::SinhFloat
                | Op::CoshFloat
                | Op::TanhFloat
                | Op::ExpFloat
                | Op::LogFloat
                | Op::Log2Float
                | Op::Log10Float) => {
                    let id: i64 = match op {
                        Op::SinFloat => 0,
                        Op::CosFloat => 1,
                        Op::TanFloat => 2,
                        Op::AsinFloat => 3,
                        Op::AcosFloat => 4,
                        Op::AtanFloat => 5,
                        Op::SinhFloat => 6,
                        Op::CoshFloat => 7,
                        Op::TanhFloat => 8,
                        Op::ExpFloat => 9,
                        Op::LogFloat => 10,
                        Op::Log2Float => 11,
                        _ => 12, // Log10Float
                    };
                    let ka = kinds.pop().unwrap();
                    let idx = kinds.len();
                    let x = load_f64(&mut b, &ivars, &fvars, idx, ka);
                    let idc = b.ins().iconst(types::I32, id);
                    let call = b.ins().call(unary_ref, &[idc, x]);
                    let r = b.inst_results(call)[0];
                    b.def_var(fvars[idx], r);
                    kinds.push(Kind::Float);
                }
                // `to_int`: Float truncates (saturating, like `f as i64`); an
                // int-like value passes through unchanged.
                Op::TruncInt => {
                    let ka = kinds.pop().unwrap();
                    let idx = kinds.len();
                    let r = if ka == Kind::Float {
                        let f = b.use_var(fvars[idx]);
                        b.ins().fcvt_to_sint_sat(types::I64, f)
                    } else {
                        b.use_var(ivars[idx])
                    };
                    b.def_var(ivars[idx], r);
                    kinds.push(Kind::Int);
                }
                // Integer absolute value, wrapping (`abs(i64::MIN) == i64::MIN`).
                Op::AbsInt => {
                    let _ = kinds.pop().unwrap();
                    let idx = kinds.len();
                    let x = b.use_var(ivars[idx]);
                    let neg = b.ins().ineg(x);
                    let is_neg = b.ins().icmp_imm(IntCC::SignedLessThan, x, 0);
                    let r = b.ins().select(is_neg, neg, x);
                    b.def_var(ivars[idx], r);
                    kinds.push(Kind::Int);
                }
                // Binary float math libcalls. `a` is the lower operand, `b` the
                // top — matching the interpreter's pop order.
                op @ (Op::PowFloat | Op::Atan2Float) => {
                    let kb = kinds.pop().unwrap();
                    let iy = kinds.len();
                    let ka = kinds.pop().unwrap();
                    let ix = kinds.len();
                    let top = load_f64(&mut b, &ivars, &fvars, iy, kb);
                    let low = load_f64(&mut b, &ivars, &fvars, ix, ka);
                    let call = match op {
                        // a.powf(b): base=low, exponent=top
                        Op::PowFloat => b.ins().call(powf_ref, &[low, top]),
                        // y.atan2(x): pops x (top) then y (low) → atan2(y, x)
                        _ => b.ins().call(atan2_ref, &[low, top]),
                    };
                    let r = b.inst_results(call)[0];
                    b.def_var(fvars[ix], r);
                    kinds.push(Kind::Float);
                }
                // Fused exact `(a*b) % k` via the i128 libcall — no overflow
                // check needed (the product is exact and `|r| < |k|`).
                Op::MulModFloor => {
                    let _ = kinds.pop().unwrap();
                    let ik = kinds.len();
                    let _ = kinds.pop().unwrap();
                    let iy = kinds.len();
                    let _ = kinds.pop().unwrap();
                    let ix = kinds.len();
                    let av = b.use_var(ivars[ix]);
                    let bv = b.use_var(ivars[iy]);
                    let kv = b.use_var(ivars[ik]);
                    let call = b.ins().call(mulmod_ref, &[av, bv, kv]);
                    let r = b.inst_results(call)[0];
                    b.def_var(ivars[ix], r);
                    kinds.push(Kind::Int);
                }
                Op::MulAddModFloor => {
                    let _ = kinds.pop().unwrap();
                    let ik = kinds.len();
                    let _ = kinds.pop().unwrap();
                    let ic = kinds.len();
                    let _ = kinds.pop().unwrap();
                    let iy = kinds.len();
                    let _ = kinds.pop().unwrap();
                    let ix = kinds.len();
                    let av = b.use_var(ivars[ix]);
                    let bv = b.use_var(ivars[iy]);
                    let cv = b.use_var(ivars[ic]);
                    let kv = b.use_var(ivars[ik]);
                    let call = b.ins().call(muladdmod_ref, &[av, bv, cv, kv]);
                    let r = b.inst_results(call)[0];
                    b.def_var(ivars[ix], r);
                    kinds.push(Kind::Int);
                }
                // gcd via an internal Euclid loop on the magnitudes (`urem`,
                // guarded by the `y != 0` loop condition). Result is `x` as i64.
                Op::GcdInt => {
                    let _ = kinds.pop().unwrap();
                    let iy = kinds.len();
                    let _ = kinds.pop().unwrap();
                    let ix = kinds.len();
                    let av = b.use_var(ivars[ix]);
                    let a0 = uabs(&mut b, av);
                    let bv = b.use_var(ivars[iy]);
                    let b0 = uabs(&mut b, bv);
                    let xv = b.declare_var(types::I64);
                    let yv = b.declare_var(types::I64);
                    b.def_var(xv, a0);
                    b.def_var(yv, b0);
                    let header = b.create_block();
                    let body = b.create_block();
                    let exit = b.create_block();
                    b.ins().jump(header, &[]);
                    b.switch_to_block(header);
                    let y = b.use_var(yv);
                    let cont = b.ins().icmp_imm(IntCC::NotEqual, y, 0);
                    b.ins().brif(cont, body, &[], exit, &[]);
                    b.switch_to_block(body);
                    let x = b.use_var(xv);
                    let y2 = b.use_var(yv);
                    let t = b.ins().urem(x, y2);
                    b.def_var(xv, y2); // x = y
                    b.def_var(yv, t); // y = x % y
                    b.ins().jump(header, &[]);
                    b.switch_to_block(exit);
                    let g = b.use_var(xv);
                    // Saturate the one magnitude an i64 cannot hold: `gcd(0,
                    // i64::MIN)` is `2^63`, whose i64 bit pattern is negative.
                    // The interpreter and the linear/block/trace JIT both
                    // answer `i64::MAX` there, so the unsigned `g` is clamped
                    // the same way — `g` with its sign bit set is exactly
                    // "greater than i64::MAX as u64".
                    let sat = b.ins().iconst(types::I64, i64::MAX);
                    let too_big = b.ins().icmp_imm(IntCC::SignedLessThan, g, 0);
                    let g = b.ins().select(too_big, sat, g);
                    b.def_var(ivars[ix], g);
                    kinds.push(Kind::Int);
                }
                // lcm = (a/gcd).saturating_mul(b), capped at i64::MAX; 0 if either
                // operand is 0. Euclid loop plus a saturating u64 multiply.
                Op::LcmInt => {
                    let _ = kinds.pop().unwrap();
                    let iy = kinds.len();
                    let _ = kinds.pop().unwrap();
                    let ix = kinds.len();
                    let av = b.use_var(ivars[ix]);
                    let a0 = uabs(&mut b, av);
                    let bv = b.use_var(ivars[iy]);
                    let b0 = uabs(&mut b, bv);
                    let rv = b.declare_var(types::I64);
                    let a_is0 = b.ins().icmp_imm(IntCC::Equal, a0, 0);
                    let b_is0 = b.ins().icmp_imm(IntCC::Equal, b0, 0);
                    let any0 = b.ins().bor(a_is0, b_is0);
                    let zero_b = b.create_block();
                    let comp_b = b.create_block();
                    let done_b = b.create_block();
                    b.ins().brif(any0, zero_b, &[], comp_b, &[]);

                    b.switch_to_block(zero_b);
                    let z = b.ins().iconst(types::I64, 0);
                    b.def_var(rv, z);
                    b.ins().jump(done_b, &[]);

                    b.switch_to_block(comp_b);
                    let xv = b.declare_var(types::I64);
                    let yv = b.declare_var(types::I64);
                    b.def_var(xv, a0);
                    b.def_var(yv, b0);
                    let gh = b.create_block();
                    let gb = b.create_block();
                    let ge = b.create_block();
                    b.ins().jump(gh, &[]);
                    b.switch_to_block(gh);
                    let y = b.use_var(yv);
                    let cont = b.ins().icmp_imm(IntCC::NotEqual, y, 0);
                    b.ins().brif(cont, gb, &[], ge, &[]);
                    b.switch_to_block(gb);
                    let x = b.use_var(xv);
                    let y2 = b.use_var(yv);
                    let t = b.ins().urem(x, y2);
                    b.def_var(xv, y2);
                    b.def_var(yv, t);
                    b.ins().jump(gh, &[]);
                    b.switch_to_block(ge);
                    let g = b.use_var(xv);
                    let p = b.ins().udiv(a0, g); // a / gcd
                    let prod = b.ins().imul(p, b0); // low 64 bits of p*b
                    let hi = b.ins().umulhi(p, b0); // high bits ⇒ overflow if != 0
                    let ovf = b.ins().icmp_imm(IntCC::NotEqual, hi, 0);
                    let umax = b.ins().iconst(types::I64, -1); // u64::MAX bits
                    let sat = b.ins().select(ovf, umax, prod);
                    let imax = b.ins().iconst(types::I64, i64::MAX);
                    let lt = b.ins().icmp(IntCC::UnsignedLessThan, sat, imax);
                    let res = b.ins().select(lt, sat, imax); // min(sat, i64::MAX)
                    b.def_var(rv, res);
                    b.ins().jump(done_b, &[]);

                    b.switch_to_block(done_b);
                    let r = b.use_var(rv);
                    b.def_var(ivars[ix], r);
                    kinds.push(Kind::Int);
                }
                // awk `/` and `%`: float result, with a runtime error on a zero
                // divisor (the native code branches to set the error and returns
                // early; the ok path continues). Codes match `VM::aot_set_error`.
                op @ (Op::AwkDivJit | Op::AwkModJit | Op::AwkDiv | Op::AwkMod) => {
                    let is_div = matches!(op, Op::AwkDivJit | Op::AwkDiv);
                    let kb = kinds.pop().unwrap();
                    let iy = kinds.len();
                    let ka = kinds.pop().unwrap();
                    let ix = kinds.len();
                    let divisor = load_f64(&mut b, &ivars, &fvars, iy, kb);
                    let dividend = load_f64(&mut b, &ivars, &fvars, ix, ka);
                    let zero = b.ins().f64const(Ieee64::with_bits(0f64.to_bits()));
                    let is_zero = b.ins().fcmp(FloatCC::Equal, divisor, zero);
                    let err_blk = b.create_block();
                    let ok_blk = b.create_block();
                    b.ins().brif(is_zero, err_blk, &[], ok_blk, &[]);

                    b.switch_to_block(err_blk);
                    let vm = b.use_var(vm_var);
                    // code 0 = "division by zero attempted", 1 = the `%' message.
                    let code = b.ins().iconst(types::I32, if is_div { 0 } else { 1 });
                    b.ins().call(serr_ref, &[vm, code]);
                    let status = b.ins().iconst(types::I64, 0);
                    b.ins().return_(&[status]);

                    b.switch_to_block(ok_blk);
                    let r = if is_div {
                        b.ins().fdiv(dividend, divisor)
                    } else {
                        let call = b.ins().call(fmod_ref, &[dividend, divisor]);
                        b.inst_results(call)[0]
                    };
                    b.def_var(fvars[ix], r);
                    kinds.push(Kind::Float);
                }
                // awk lshift/rshift: error on negative operands, else convert to
                // int, shift (logical for rshift) with `(n as u32) & 0x3f`, and
                // convert back to float — mirroring the interpreter exactly.
                op @ (Op::AwkLshiftJit | Op::AwkRshiftJit) => {
                    let kb = kinds.pop().unwrap();
                    let iy = kinds.len();
                    let ka = kinds.pop().unwrap();
                    let ix = kinds.len();
                    let n = load_f64(&mut b, &ivars, &fvars, iy, kb);
                    let a = load_f64(&mut b, &ivars, &fvars, ix, ka);
                    let zero = b.ins().f64const(Ieee64::with_bits(0f64.to_bits()));
                    let a_neg = b.ins().fcmp(FloatCC::LessThan, a, zero);
                    let n_neg = b.ins().fcmp(FloatCC::LessThan, n, zero);
                    let bad = b.ins().bor(a_neg, n_neg);
                    let err_blk = b.create_block();
                    let ok_blk = b.create_block();
                    b.ins().brif(bad, err_blk, &[], ok_blk, &[]);

                    b.switch_to_block(err_blk);
                    let vm = b.use_var(vm_var);
                    let code = b.ins().iconst(
                        types::I32,
                        if matches!(op, Op::AwkLshiftJit) { 2 } else { 3 },
                    );
                    b.ins().call(serr_ref, &[vm, code]);
                    let st0 = b.ins().iconst(types::I64, 0);
                    b.ins().return_(&[st0]);

                    b.switch_to_block(ok_blk);
                    let ai = b.ins().fcvt_to_sint_sat(types::I64, a);
                    let shift_u32 = b.ins().fcvt_to_uint_sat(types::I32, n);
                    let shift_u64 = b.ins().uextend(types::I64, shift_u32);
                    let shift = b.ins().band_imm(shift_u64, 0x3f);
                    let r = if matches!(op, Op::AwkLshiftJit) {
                        let sh = b.ins().ishl(ai, shift);
                        b.ins().fcvt_from_sint(types::F64, sh)
                    } else {
                        // logical right shift on the u64 bit pattern → f64
                        let sh = b.ins().ushr(ai, shift);
                        b.ins().fcvt_from_uint(types::F64, sh)
                    };
                    b.def_var(fvars[ix], r);
                    kinds.push(Kind::Float);
                }
                // awk compl: error on negative, else `!(a as i64)` as float.
                Op::AwkComplJit => {
                    let ka = kinds.pop().unwrap();
                    let idx = kinds.len();
                    let a = load_f64(&mut b, &ivars, &fvars, idx, ka);
                    let zero = b.ins().f64const(Ieee64::with_bits(0f64.to_bits()));
                    let a_neg = b.ins().fcmp(FloatCC::LessThan, a, zero);
                    let err_blk = b.create_block();
                    let ok_blk = b.create_block();
                    b.ins().brif(a_neg, err_blk, &[], ok_blk, &[]);

                    b.switch_to_block(err_blk);
                    let vm = b.use_var(vm_var);
                    let code = b.ins().iconst(types::I32, 4);
                    b.ins().call(serr_ref, &[vm, code]);
                    let st0 = b.ins().iconst(types::I64, 0);
                    b.ins().return_(&[st0]);

                    b.switch_to_block(ok_blk);
                    let ai = b.ins().fcvt_to_sint_sat(types::I64, a);
                    let v = b.ins().bnot(ai);
                    let r = b.ins().fcvt_from_sint(types::F64, v);
                    b.def_var(fvars[idx], r);
                    kinds.push(Kind::Float);
                }
                // awk sqrt/log: warn-to-stderr + NaN on negative input (no halt),
                // else the result. The two arms merge their value in fvars[idx].
                op @ (Op::AwkSqrtJit | Op::AwkLogJit) => {
                    let ka = kinds.pop().unwrap();
                    let idx = kinds.len();
                    let a = load_f64(&mut b, &ivars, &fvars, idx, ka);
                    let zero = b.ins().f64const(Ieee64::with_bits(0f64.to_bits()));
                    let neg = b.ins().fcmp(FloatCC::LessThan, a, zero);
                    let warn_blk = b.create_block();
                    let ok_blk = b.create_block();
                    let cont_blk = b.create_block();
                    b.ins().brif(neg, warn_blk, &[], ok_blk, &[]);

                    b.switch_to_block(warn_blk);
                    let code = b
                        .ins()
                        .iconst(types::I32, if matches!(op, Op::AwkSqrtJit) { 0 } else { 1 });
                    b.ins().call(warn_ref, &[code, a]);
                    let nan = b.ins().f64const(Ieee64::with_bits(f64::NAN.to_bits()));
                    b.def_var(fvars[idx], nan);
                    b.ins().jump(cont_blk, &[]);

                    b.switch_to_block(ok_blk);
                    let r = if matches!(op, Op::AwkSqrtJit) {
                        b.ins().sqrt(a)
                    } else {
                        let ln_id = b.ins().iconst(types::I32, 10); // ln
                        let call = b.ins().call(unary_ref, &[ln_id, a]);
                        b.inst_results(call)[0]
                    };
                    b.def_var(fvars[idx], r);
                    b.ins().jump(cont_blk, &[]);

                    b.switch_to_block(cont_blk);
                    kinds.push(Kind::Float);
                }
                // Sink ops: spill the top n scalar registers onto the boxed
                // stack (bottom-most first, matching the interpreter's order),
                // then run the op through the shim. vm.stack is empty before and
                // after (the op pops exactly what we pushed). `SetStatus` is the
                // n=1 silent case.
                Op::Print(_) | Op::PrintLn(_) | Op::SetStatus => {
                    let n = match &chunk.ops[ip] {
                        Op::Print(c) | Op::PrintLn(c) => *c as usize,
                        _ => 1, // SetStatus
                    };
                    let base = kinds.len() - n;
                    for pos in base..kinds.len() {
                        let vm = b.use_var(vm_var);
                        match kinds[pos] {
                            Kind::Float => {
                                let v = b.use_var(fvars[pos]);
                                b.ins().call(pushf_ref, &[vm, v]);
                            }
                            Kind::Bool => {
                                let v = b.use_var(ivars[pos]);
                                b.ins().call(pushb_ref, &[vm, v]);
                            }
                            Kind::Status => {
                                let v = b.use_var(ivars[pos]);
                                b.ins().call(push_status_ref, &[vm, v]);
                            }
                            Kind::Int => {
                                let v = b.use_var(ivars[pos]);
                                b.ins().call(pushi_ref, &[vm, v]);
                            }
                            Kind::Obj => {
                                let v = b.use_var(ivars[pos]);
                                b.ins().call(unbox_ref, &[vm, v]);
                            }
                        }
                    }
                    let vm = b.use_var(vm_var);
                    let ipc = b.ins().iconst(types::I64, ip as i64);
                    // Straight-line sink: the returned next-ip is ignored.
                    b.ins().call(exec_ref, &[vm, ipc]);
                    kinds.truncate(base);
                }
                // Source op: run via the shim (it pushes a Float onto vm.stack),
                // then reload that Float into a register. Statically Float, so no
                // guard. vm.stack is empty before and after.
                Op::AwkGetFieldNum(_) => {
                    let vm = b.use_var(vm_var);
                    let ipc = b.ins().iconst(types::I64, ip as i64);
                    b.ins().call(exec_ref, &[vm, ipc]);
                    let vm2 = b.use_var(vm_var);
                    let call = b.ins().call(popf_ref, &[vm2]);
                    let v = b.inst_results(call)[0];
                    let idx = kinds.len();
                    b.def_var(fvars[idx], v);
                    kinds.push(Kind::Float);
                }
                // `$?` source: run via the shim (it pushes a Status), reload the
                // code into a register. Statically Status, so no guard.
                Op::GetStatus => {
                    let vm = b.use_var(vm_var);
                    let ipc = b.ins().iconst(types::I64, ip as i64);
                    b.ins().call(exec_ref, &[vm, ipc]);
                    let vm2 = b.use_var(vm_var);
                    let call = b.ins().call(popi_ref, &[vm2]);
                    let v = b.inst_results(call)[0];
                    let idx = kinds.len();
                    b.def_var(ivars[idx], v);
                    kinds.push(Kind::Status);
                }
                Op::Negate => {
                    let k = kinds.pop().unwrap();
                    let idx = kinds.len();
                    if k == Kind::Int {
                        let x = b.use_var(ivars[idx]);
                        let r = b.ins().ineg(x);
                        b.def_var(ivars[idx], r);
                        kinds.push(Kind::Int);
                    } else {
                        // Float or Status → `-to_float`, yielding Float.
                        let x = load_f64(&mut b, &ivars, &fvars, idx, k);
                        let r = b.ins().fneg(x);
                        b.def_var(fvars[idx], r);
                        kinds.push(Kind::Float);
                    }
                }
                op @ (Op::NumEq | Op::NumNe | Op::NumLt | Op::NumGt | Op::NumLe | Op::NumGe) => {
                    let ky = kinds.pop().unwrap();
                    let iy = kinds.len();
                    let kx = kinds.pop().unwrap();
                    let ix = kinds.len();
                    let pred = if ky == Kind::Float || kx == Kind::Float {
                        let y = load_f64(&mut b, &ivars, &fvars, iy, ky);
                        let x = load_f64(&mut b, &ivars, &fvars, ix, kx);
                        let cc = match op {
                            Op::NumEq => FloatCC::Equal,
                            Op::NumNe => FloatCC::NotEqual,
                            Op::NumLt => FloatCC::LessThan,
                            Op::NumGt => FloatCC::GreaterThan,
                            Op::NumLe => FloatCC::LessThanOrEqual,
                            _ => FloatCC::GreaterThanOrEqual,
                        };
                        b.ins().fcmp(cc, x, y)
                    } else {
                        let y = b.use_var(ivars[iy]);
                        let x = b.use_var(ivars[ix]);
                        let cc = match op {
                            Op::NumEq => IntCC::Equal,
                            Op::NumNe => IntCC::NotEqual,
                            Op::NumLt => IntCC::SignedLessThan,
                            Op::NumGt => IntCC::SignedGreaterThan,
                            Op::NumLe => IntCC::SignedLessThanOrEqual,
                            _ => IntCC::SignedGreaterThanOrEqual,
                        };
                        b.ins().icmp(cc, x, y)
                    };
                    // Normalize the i8 predicate to i64 0/1 (the interpreter
                    // pushes a `Bool`, carried here as an integer 0/1).
                    let one = b.ins().iconst(types::I64, 1);
                    let zero = b.ins().iconst(types::I64, 0);
                    let r = b.ins().select(pred, one, zero);
                    b.def_var(ivars[ix], r);
                    kinds.push(Kind::Bool);
                }
                // Slots are register-resident, typed Int or Float per the plan.
                Op::GetSlot(slot) => {
                    let k = var_ty_kind(plan, *slot as u32);
                    let v = b.use_var(slot_vars[*slot as usize]);
                    let idx = kinds.len();
                    if k == Kind::Obj {
                        // Reading clones the value into a fresh owned handle; the
                        // slot keeps its own handle.
                        let vm = b.use_var(vm_var);
                        let call = b.ins().call(clone_ref, &[vm, v]);
                        b.def_var(ivars[idx], b.inst_results(call)[0]);
                    } else {
                        store_raw(&mut b, &ivars, &fvars, idx, k, v);
                    }
                    kinds.push(k);
                }
                Op::SetSlot(slot) => {
                    let k = kinds.pop().unwrap(); // == the slot's kind
                    let idx = kinds.len();
                    let v = load_raw(&mut b, &ivars, &fvars, idx, k);
                    let sv = slot_vars[*slot as usize];
                    if k == Kind::Obj {
                        // Free the slot's previous handle (init sentinel -1 ⇒
                        // no-op) before it is overwritten.
                        let old = b.use_var(sv);
                        let vm = b.use_var(vm_var);
                        b.ins().call(free_ref, &[vm, old]);
                    }
                    b.def_var(sv, v);
                }
                // Globals mirror slots (Int/Float/Obj register variables).
                Op::GetVar(g) => {
                    let k = var_ty_kind(plan, *g as u32 | GLOBAL_TAG);
                    let v = b.use_var(global_vars[*g as usize]);
                    let idx = kinds.len();
                    if k == Kind::Obj {
                        let vm = b.use_var(vm_var);
                        let call = b.ins().call(clone_ref, &[vm, v]);
                        b.def_var(ivars[idx], b.inst_results(call)[0]);
                    } else {
                        store_raw(&mut b, &ivars, &fvars, idx, k, v);
                    }
                    kinds.push(k);
                }
                Op::SetVar(g) | Op::DeclareVar(g) => {
                    let k = kinds.pop().unwrap();
                    let idx = kinds.len();
                    let v = load_raw(&mut b, &ivars, &fvars, idx, k);
                    let gv = global_vars[*g as usize];
                    if k == Kind::Obj {
                        let old = b.use_var(gv);
                        let vm = b.use_var(vm_var);
                        b.ins().call(free_ref, &[vm, old]);
                    }
                    b.def_var(gv, v);
                }
                // Slot read-modify-write super-ops (slots are i64 registers).
                op @ (Op::PreIncSlot(slot) | Op::PreDecSlot(slot)) => {
                    let sv = slot_vars[*slot as usize];
                    let old = b.use_var(sv);
                    let nv = b.ins().iadd_imm(
                        old,
                        if matches!(op, Op::PreIncSlot(_)) {
                            1
                        } else {
                            -1
                        },
                    );
                    b.def_var(sv, nv);
                    let idx = kinds.len();
                    b.def_var(ivars[idx], nv); // pre: push the new value
                    kinds.push(Kind::Int);
                }
                op @ (Op::PostIncSlot(slot) | Op::PostDecSlot(slot)) => {
                    let sv = slot_vars[*slot as usize];
                    let old = b.use_var(sv);
                    let nv = b.ins().iadd_imm(
                        old,
                        if matches!(op, Op::PostIncSlot(_)) {
                            1
                        } else {
                            -1
                        },
                    );
                    b.def_var(sv, nv);
                    let idx = kinds.len();
                    b.def_var(ivars[idx], old); // post: push the old value
                    kinds.push(Kind::Int);
                }
                Op::PreIncSlotVoid(slot) => {
                    let sv = slot_vars[*slot as usize];
                    let old = b.use_var(sv);
                    let nv = b.ins().iadd_imm(old, 1);
                    b.def_var(sv, nv);
                }
                Op::AddAssignSlotVoid(a, b_slot) => {
                    let av = slot_vars[*a as usize];
                    let x = b.use_var(av);
                    let y = b.use_var(slot_vars[*b_slot as usize]);
                    let sum = b.ins().iadd(x, y);
                    b.def_var(av, sum);
                }
                // Emit the internal `while i < limit { sum += i; i += 1 }` as a
                // real native loop over the two slot registers. Three on-the-fly
                // blocks (not plan leaders); `seal_all_blocks` builds the phis.
                // Execution continues in `exit` after this op.
                Op::AccumSumLoop(sum_s, i_s, limit) => {
                    let sum_v = slot_vars[*sum_s as usize];
                    let i_v = slot_vars[*i_s as usize];
                    let header = b.create_block();
                    let body = b.create_block();
                    let exit = b.create_block();
                    b.ins().jump(header, &[]);

                    b.switch_to_block(header);
                    let lim = b.ins().iconst(types::I64, *limit as i64);
                    let i_cur = b.use_var(i_v);
                    let cond = b.ins().icmp(IntCC::SignedLessThan, i_cur, lim);
                    b.ins().brif(cond, body, &[], exit, &[]);

                    b.switch_to_block(body);
                    let s = b.use_var(sum_v);
                    let i2 = b.use_var(i_v);
                    let ns = b.ins().iadd(s, i2);
                    b.def_var(sum_v, ns);
                    let ni = b.ins().iadd_imm(i2, 1);
                    b.def_var(i_v, ni);
                    b.ins().jump(header, &[]);

                    // Fall through into `exit`; subsequent ops emit here.
                    b.switch_to_block(exit);
                }
                Op::SlotLtIntJumpIfFalse(slot, limit, t) => {
                    let v = b.use_var(slot_vars[*slot as usize]);
                    // jump to target when slot >= limit, i.e. NOT (slot < limit).
                    let lt = b.ins().icmp_imm(IntCC::SignedLessThan, v, *limit as i64);
                    b.ins().brif(lt, block_for(ip + 1), &[], block_for(*t), &[]);
                    terminated = true;
                }
                Op::SlotIncLtIntJumpBack(slot, limit, t) => {
                    let sv = slot_vars[*slot as usize];
                    let old = b.use_var(sv);
                    let nv = b.ins().iadd_imm(old, 1);
                    b.def_var(sv, nv);
                    // jump back to target while the incremented slot < limit.
                    let lt = b.ins().icmp_imm(IntCC::SignedLessThan, nv, *limit as i64);
                    b.ins().brif(lt, block_for(*t), &[], block_for(ip + 1), &[]);
                    terminated = true;
                }
                Op::Pop => {
                    let k = kinds.pop().unwrap();
                    if k == Kind::Obj {
                        // Discarding a boxed value frees its handle.
                        let h = b.use_var(ivars[kinds.len()]);
                        let vm = b.use_var(vm_var);
                        b.ins().call(free_ref, &[vm, h]);
                    }
                }
                Op::Dup => {
                    let k = *kinds.last().unwrap();
                    let src = kinds.len() - 1;
                    let dst = kinds.len();
                    if k == Kind::Float {
                        let v = b.use_var(fvars[src]);
                        b.def_var(fvars[dst], v);
                    } else {
                        let v = b.use_var(ivars[src]);
                        b.def_var(ivars[dst], v);
                    }
                    kinds.push(k);
                }
                Op::LoadTrue | Op::LoadFalse => {
                    let v = b
                        .ins()
                        .iconst(types::I64, matches!(&chunk.ops[ip], Op::LoadTrue) as i64);
                    let idx = kinds.len();
                    b.def_var(ivars[idx], v);
                    kinds.push(Kind::Bool);
                }
                op @ (Op::Inc | Op::Dec) => {
                    let _ = kinds.pop().unwrap(); // int-like ⇒ value lives in ivars
                    let idx = kinds.len();
                    let x = b.use_var(ivars[idx]);
                    let r = match op {
                        Op::Inc => b.ins().iadd_imm(x, 1),
                        _ => b.ins().iadd_imm(x, -1),
                    };
                    b.def_var(ivars[idx], r);
                    kinds.push(Kind::Int);
                }
                Op::LogNot => {
                    let k = kinds.pop().unwrap();
                    let idx = kinds.len();
                    let pred = truthy(
                        &mut b, vm_var, truthy_ref, truthy_keep_ref, &ivars, &fvars, idx, k, true,
                    );
                    // !truthy: truthy ⇒ 0, falsy ⇒ 1.
                    let one = b.ins().iconst(types::I64, 1);
                    let zero = b.ins().iconst(types::I64, 0);
                    let r = b.ins().select(pred, zero, one);
                    b.def_var(ivars[idx], r);
                    kinds.push(Kind::Bool);
                }
                Op::RubyTruthy => {
                    let k = kinds.pop().unwrap();
                    let idx = kinds.len();
                    // A `Bool` register already holds 0/1; every other lowered kind
                    // (Int/Float/…) is a non-nil, non-false value, so Ruby-truthy is
                    // a constant `true`.
                    let r = match k {
                        Kind::Bool => b.use_var(ivars[idx]),
                        _ => b.ins().iconst(types::I64, 1),
                    };
                    b.def_var(ivars[idx], r);
                    kinds.push(Kind::Bool);
                }
                op @ (Op::BitAnd | Op::BitOr | Op::BitXor | Op::Shl | Op::Shr) => {
                    let _ = kinds.pop().unwrap();
                    let iy = kinds.len();
                    let _ = kinds.pop().unwrap();
                    let ix = kinds.len();
                    let y = b.use_var(ivars[iy]);
                    let x = b.use_var(ivars[ix]);
                    // Cranelift masks the shift amount to the operand width
                    // (0–63 for i64), matching the interpreter's `& 63`.
                    let r = match op {
                        Op::BitAnd => b.ins().band(x, y),
                        Op::BitOr => b.ins().bor(x, y),
                        Op::BitXor => b.ins().bxor(x, y),
                        Op::Shl => b.ins().ishl(x, y),
                        _ => b.ins().sshr(x, y),
                    };
                    b.def_var(ivars[ix], r);
                    kinds.push(Kind::Int);
                }
                Op::BitNot => {
                    let _ = kinds.pop().unwrap();
                    let idx = kinds.len();
                    let x = b.use_var(ivars[idx]);
                    let r = b.ins().bnot(x);
                    b.def_var(ivars[idx], r);
                    kinds.push(Kind::Int);
                }
                Op::Dup2 => {
                    let len = kinds.len();
                    let ka = kinds[len - 2];
                    let kb = kinds[len - 1];
                    let a = load_raw(&mut b, &ivars, &fvars, len - 2, ka);
                    let bv = load_raw(&mut b, &ivars, &fvars, len - 1, kb);
                    store_raw(&mut b, &ivars, &fvars, len, ka, a);
                    store_raw(&mut b, &ivars, &fvars, len + 1, kb, bv);
                    kinds.push(ka);
                    kinds.push(kb);
                }
                Op::Swap => {
                    let len = kinds.len();
                    let (ix, iy) = (len - 2, len - 1);
                    let kx = kinds[ix];
                    let ky = kinds[iy];
                    let x = load_raw(&mut b, &ivars, &fvars, ix, kx);
                    let y = load_raw(&mut b, &ivars, &fvars, iy, ky);
                    store_raw(&mut b, &ivars, &fvars, ix, ky, y);
                    store_raw(&mut b, &ivars, &fvars, iy, kx, x);
                    kinds.swap(ix, iy);
                }
                Op::Rot => {
                    let len = kinds.len();
                    let (ia, ib, ic) = (len - 3, len - 2, len - 1);
                    let ka = kinds[ia];
                    let kb = kinds[ib];
                    let kc = kinds[ic];
                    let a = load_raw(&mut b, &ivars, &fvars, ia, ka);
                    let bv = load_raw(&mut b, &ivars, &fvars, ib, kb);
                    let c = load_raw(&mut b, &ivars, &fvars, ic, kc);
                    // [a, b, c] → [b, c, a]
                    store_raw(&mut b, &ivars, &fvars, ia, kb, bv);
                    store_raw(&mut b, &ivars, &fvars, ib, kc, c);
                    store_raw(&mut b, &ivars, &fvars, ic, ka, a);
                    kinds[ia] = kb;
                    kinds[ib] = kc;
                    kinds[ic] = ka;
                }
                op @ (Op::LogAnd | Op::LogOr) => {
                    let kb = kinds.pop().unwrap();
                    let iy = kinds.len();
                    let ka = kinds.pop().unwrap();
                    let ix = kinds.len();
                    let ta = truthy(
                        &mut b, vm_var, truthy_ref, truthy_keep_ref, &ivars, &fvars, ix, ka, true,
                    );
                    let tb = truthy(
                        &mut b, vm_var, truthy_ref, truthy_keep_ref, &ivars, &fvars, iy, kb, true,
                    );
                    let combined = match op {
                        Op::LogAnd => b.ins().band(ta, tb),
                        _ => b.ins().bor(ta, tb),
                    };
                    let one = b.ins().iconst(types::I64, 1);
                    let zero = b.ins().iconst(types::I64, 0);
                    let r = b.ins().select(combined, one, zero);
                    b.def_var(ivars[ix], r);
                    kinds.push(Kind::Bool);
                }
                Op::Spaceship => {
                    let kb = kinds.pop().unwrap();
                    let iy = kinds.len();
                    let ka = kinds.pop().unwrap();
                    let ix = kinds.len();
                    let m1 = b.ins().iconst(types::I64, -1);
                    let zero = b.ins().iconst(types::I64, 0);
                    let p1 = b.ins().iconst(types::I64, 1);
                    let (lt, gt) = if ka.promotes_float() || kb.promotes_float() {
                        let y = load_f64(&mut b, &ivars, &fvars, iy, kb);
                        let x = load_f64(&mut b, &ivars, &fvars, ix, ka);
                        (
                            b.ins().fcmp(FloatCC::LessThan, x, y),
                            b.ins().fcmp(FloatCC::GreaterThan, x, y),
                        )
                    } else {
                        let y = b.use_var(ivars[iy]);
                        let x = b.use_var(ivars[ix]);
                        (
                            b.ins().icmp(IntCC::SignedLessThan, x, y),
                            b.ins().icmp(IntCC::SignedGreaterThan, x, y),
                        )
                    };
                    // x<y ? -1 : (x>y ? 1 : 0)
                    let mid = b.ins().select(gt, p1, zero);
                    let r = b.ins().select(lt, m1, mid);
                    b.def_var(ivars[ix], r);
                    kinds.push(Kind::Int);
                }
                Op::JumpIfTrueKeep(t) => {
                    // Peek (don't pop): the value stays live on both arms.
                    let k = *kinds.last().unwrap();
                    let idx = kinds.len() - 1;
                    let cond = truthy(
                        &mut b,
                        vm_var,
                        truthy_ref,
                        truthy_keep_ref,
                        &ivars,
                        &fvars,
                        idx,
                        k,
                        false,
                    );
                    b.ins()
                        .brif(cond, block_for(*t), &[], block_for(ip + 1), &[]);
                    terminated = true;
                }
                Op::JumpIfFalseKeep(t) => {
                    // Peek (don't pop): the value stays live on both arms.
                    let k = *kinds.last().unwrap();
                    let idx = kinds.len() - 1;
                    let cond = truthy(
                        &mut b, vm_var, truthy_ref, truthy_keep_ref, &ivars, &fvars, idx, k, false,
                    );
                    b.ins()
                        .brif(cond, block_for(ip + 1), &[], block_for(*t), &[]);
                    terminated = true;
                }
                Op::Jump(t) => {
                    b.ins().jump(block_for(*t), &[]);
                    terminated = true;
                }
                Op::JumpIfTrue(t) => {
                    let k = kinds.pop().unwrap();
                    let idx = kinds.len();
                    let cond = truthy(
                        &mut b, vm_var, truthy_ref, truthy_keep_ref, &ivars, &fvars, idx, k, true,
                    );
                    b.ins()
                        .brif(cond, block_for(*t), &[], block_for(ip + 1), &[]);
                    terminated = true;
                }
                Op::JumpIfFalse(t) => {
                    let k = kinds.pop().unwrap();
                    let idx = kinds.len();
                    let cond = truthy(
                        &mut b, vm_var, truthy_ref, truthy_keep_ref, &ivars, &fvars, idx, k, true,
                    );
                    // truthy ⇒ fallthrough; false ⇒ branch to the target.
                    b.ins()
                        .brif(cond, block_for(ip + 1), &[], block_for(*t), &[]);
                    terminated = true;
                }
                // Deopt points are handled before the match; any op reaching
                // here was admitted as lowerable by `analyze_native`.
                other => unreachable!("analyze_native admitted unsupported op {other:?}"),
            }
        }

        // Fall off the end of the last block into ret.
        if !terminated {
            b.ins().jump(ret_block, &[]);
        }

        // Ret: box the stack top as the result, mirroring the interpreter's
        // stack-tail rule (empty ⇒ `aot_result` stays unset ⇒ `Halted`). The
        // top kind is Int or Float (analysis rejects a Bool result).
        b.switch_to_block(ret_block);
        if let Some(&topk) = plan.end_kinds.last() {
            let idx = plan.end_kinds.len() - 1;
            let vm = b.use_var(vm_var);
            match topk {
                Kind::Float => {
                    let v = b.use_var(fvars[idx]);
                    b.ins().call(fres_ref, &[vm, v]);
                }
                Kind::Status => {
                    let v = b.use_var(ivars[idx]);
                    b.ins().call(sres_ref, &[vm, v]);
                }
                // Obj: the register holds an arena handle; box it as the result.
                Kind::Obj => {
                    let v = b.use_var(ivars[idx]);
                    b.ins().call(obj_res_ref, &[vm, v]);
                }
                // Int (Bool is rejected as a result by analysis).
                _ => {
                    let v = b.use_var(ivars[idx]);
                    b.ins().call(ires_ref, &[vm, v]);
                }
            }
        }
        let status = b.ins().iconst(types::I64, 0);
        b.ins().return_(&[status]);

        b.seal_all_blocks();
        b.finalize();
    }
    module
        .define_function(entry_id, &mut ctx)
        .map_err(|e| format!("aot: define native entry: {e}"))?;
    module.clear_context(&mut ctx);
    Ok(entry_id)
}

/// Read operand-stack position `idx` as an `f64`, promoting an integer with
/// `int→float` conversion the way the interpreter's `to_float` does — which is
/// exact only while the integer is one an `f64` can hold. Past `2^53` it
/// rounds, so under a numeric hook the mixed pairs never reach here at all:
/// [`native_rounds_operand`] has already made the op a deopt point. Never
/// called on a `Bool` (analysis rejects booleans feeding arithmetic).
fn load_f64(
    b: &mut FunctionBuilder,
    ivars: &[Variable],
    fvars: &[Variable],
    idx: usize,
    k: Kind,
) -> cranelift_codegen::ir::Value {
    if k == Kind::Float {
        b.use_var(fvars[idx])
    } else {
        let v = b.use_var(ivars[idx]);
        b.ins().fcvt_from_sint(types::F64, v)
    }
}

/// Read operand-stack position `idx` in its own representation (no coercion),
/// picking the i64 or f64 variable per its `Kind`. Used by the stack-shuffle
/// ops, which move values around unchanged.
fn load_raw(
    b: &mut FunctionBuilder,
    ivars: &[Variable],
    fvars: &[Variable],
    idx: usize,
    k: Kind,
) -> cranelift_codegen::ir::Value {
    if k == Kind::Float {
        b.use_var(fvars[idx])
    } else {
        b.use_var(ivars[idx])
    }
}

/// Unsigned absolute value of an `i64` (same bit pattern as `i64::unsigned_abs`,
/// including `i64::MIN` → `2^63`), as an i64 holding the u64 bits. Used by the
/// `GcdInt`/`LcmInt` Euclid loops, which operate on magnitudes.
fn uabs(b: &mut FunctionBuilder, v: cranelift_codegen::ir::Value) -> cranelift_codegen::ir::Value {
    let neg = b.ins().ineg(v);
    let is_neg = b.ins().icmp_imm(IntCC::SignedLessThan, v, 0);
    b.ins().select(is_neg, neg, v)
}

/// Write `v` to operand-stack position `idx`, into the variable matching `k`.
fn store_raw(
    b: &mut FunctionBuilder,
    ivars: &[Variable],
    fvars: &[Variable],
    idx: usize,
    k: Kind,
    v: cranelift_codegen::ir::Value,
) {
    if k == Kind::Float {
        b.def_var(fvars[idx], v);
    } else {
        b.def_var(ivars[idx], v);
    }
}

/// Build an i8 truthiness predicate for the value at position `idx`, matching
/// [`crate::value::Value::is_truthy`]: integers/bools are nonzero-true; floats
/// use `!= 0.0` (unordered, so `NaN` is truthy).
#[allow(clippy::too_many_arguments)]
fn truthy(
    b: &mut FunctionBuilder,
    vm_var: Variable,
    truthy_ref: FuncRef,
    truthy_keep_ref: FuncRef,
    ivars: &[Variable],
    fvars: &[Variable],
    idx: usize,
    k: Kind,
    consume: bool,
) -> cranelift_codegen::ir::Value {
    match k {
        Kind::Float => {
            let f = b.use_var(fvars[idx]);
            let z = b.ins().f64const(Ieee64::with_bits(0f64.to_bits()));
            b.ins().fcmp(FloatCC::NotEqual, f, z)
        }
        // Status is truthy when the code is ZERO (shell success) — inverted.
        Kind::Status => {
            let v = b.use_var(ivars[idx]);
            b.ins().icmp_imm(IntCC::Equal, v, 0)
        }
        // Obj: the register holds an *arena index*, not a value — testing it for
        // zero would answer about the handle, not the string/array behind it (an
        // empty string is falsy at any index). Ask the runtime, which applies
        // `Value::is_truthy`. The consuming form matches `JumpIf*`'s pop; the
        // keeping form matches `JumpIf*Keep`'s peek (the handle stays owned).
        Kind::Obj => {
            let h = b.use_var(ivars[idx]);
            let vm = b.use_var(vm_var);
            let f = if consume { truthy_ref } else { truthy_keep_ref };
            let call = b.ins().call(f, &[vm, h]);
            let r = b.inst_results(call)[0];
            b.ins().icmp_imm(IntCC::NotEqual, r, 0)
        }
        // Int / Bool: nonzero is truthy.
        _ => {
            let v = b.use_var(ivars[idx]);
            b.ins().icmp_imm(IntCC::NotEqual, v, 0)
        }
    }
}

/// Threaded fallback codegen: one Cranelift block per op, each running the op's
/// semantics through the [`fusevm_aot_exec_op`] shim and re-dispatching on the
/// returned next-ip. See the module docs for the control-flow shape.
fn build_entry_threaded<M: Module>(
    module: &mut M,
    chunk: &Chunk,
    symbol: &str,
) -> Result<FuncId, String> {
    let ptr_ty = module.target_config().pointer_type();

    // Imported runtime shims.
    let mut exec_sig = module.make_signature();
    exec_sig.params.push(AbiParam::new(ptr_ty));
    exec_sig.params.push(AbiParam::new(types::I64));
    exec_sig.returns.push(AbiParam::new(types::I64));
    let exec_id = module
        .declare_function("fusevm_aot_exec_op", Linkage::Import, &exec_sig)
        .map_err(|e| format!("aot: declare exec_op: {e}"))?;

    let mut fin_sig = module.make_signature();
    fin_sig.params.push(AbiParam::new(ptr_ty));
    let fin_id = module
        .declare_function("fusevm_aot_finish", Linkage::Import, &fin_sig)
        .map_err(|e| format!("aot: declare finish: {e}"))?;

    // Exported entry: fn(vm) -> i64.
    let mut entry_sig = module.make_signature();
    entry_sig.params.push(AbiParam::new(ptr_ty));
    entry_sig.returns.push(AbiParam::new(types::I64));
    let entry_id = module
        .declare_function(symbol, Linkage::Export, &entry_sig)
        .map_err(|e| format!("aot: declare entry: {e}"))?;

    let mut ctx = module.make_context();
    ctx.func.signature = entry_sig;
    let mut fbctx = FunctionBuilderContext::new();
    {
        let mut b = FunctionBuilder::new(&mut ctx.func, &mut fbctx);
        let exec_ref = module.declare_func_in_func(exec_id, b.func);
        let fin_ref = module.declare_func_in_func(fin_id, b.func);

        let n = chunk.ops.len();
        let entry_block = b.create_block();
        b.append_block_params_for_function_params(entry_block);
        let dispatch_block = b.create_block();
        b.append_block_param(dispatch_block, types::I64); // current ip
        let ret_block = b.create_block();
        let op_blocks: Vec<_> = (0..n).map(|_| b.create_block()).collect();

        // `vm` pointer threaded to every block via a frontend Variable.
        let vm_var = b.declare_var(ptr_ty);

        // entry: stash vm, jump into dispatch at ip 0 (or straight to ret if empty).
        b.switch_to_block(entry_block);
        let vm_param = b.block_params(entry_block)[0];
        b.def_var(vm_var, vm_param);
        if n == 0 {
            b.ins().jump(ret_block, &[]);
        } else {
            let zero = b.ins().iconst(types::I64, 0);
            b.ins().jump(dispatch_block, &[BlockArg::Value(zero)]);
        }

        // dispatch: br_table on the ip param to the matching op block.
        b.switch_to_block(dispatch_block);
        let ip_val = b.block_params(dispatch_block)[0];
        if n == 0 {
            b.ins().jump(ret_block, &[]);
        } else {
            let mut switch = Switch::new();
            for (i, blk) in op_blocks.iter().enumerate() {
                switch.set_entry(i as u128, *blk);
            }
            switch.emit(&mut b, ip_val, ret_block);
        }

        // one block per op: run it, then terminate or re-dispatch on the result.
        for (i, blk) in op_blocks.iter().enumerate() {
            b.switch_to_block(*blk);
            let vm = b.use_var(vm_var);
            let ipc = b.ins().iconst(types::I64, i as i64);
            let call = b.ins().call(exec_ref, &[vm, ipc]);
            let next = b.inst_results(call)[0];
            let is_term = b.ins().icmp_imm(IntCC::SignedLessThan, next, 0);
            b.ins().brif(
                is_term,
                ret_block,
                &[],
                dispatch_block,
                &[BlockArg::Value(next)],
            );
        }

        // ret: compute the tail result and return.
        b.switch_to_block(ret_block);
        let vm = b.use_var(vm_var);
        b.ins().call(fin_ref, &[vm]);
        let status = b.ins().iconst(types::I64, 0);
        b.ins().return_(&[status]);

        b.seal_all_blocks();
        b.finalize();
    }
    module
        .define_function(entry_id, &mut ctx)
        .map_err(|e| format!("aot: define entry: {e}"))?;
    module.clear_context(&mut ctx);
    Ok(entry_id)
}

/// Compile `chunk` to native code in-process via Cranelift, run it on a fresh
/// VM, and return the result. The emitted native entry drives the whole program
/// (no interpreter dispatch loop). `register` installs any frontend builtins on
/// the VM before the run. This validates the closed-world compiler end to end
/// and is the in-memory analog of the on-disk object path.
pub fn run_chunk_native(chunk: &Chunk, register: impl FnOnce(&mut VM)) -> Result<VMResult, String> {
    Ok(run_chunk_with(chunk, register, build_entry)?.take_aot_result())
}

/// Same, but forced onto the threaded lowering — the fallback `build_named`
/// picks when a native plan would cover too little of the chunk. Tests use it to
/// run ONE chunk through BOTH lowerings and pin that they agree, which is the
/// invariant that lets `NativePlan::worth_lowering` choose freely between them.
#[cfg(test)]
fn run_chunk_threaded(
    chunk: &Chunk,
    register: impl FnOnce(&mut VM),
) -> Result<VMResult, String> {
    Ok(run_chunk_with(chunk, register, |m, c| {
        build_entry_threaded(m, c, AOT_ENTRY_SYMBOL)
    })?
    .take_aot_result())
}

/// Same as [`run_chunk_native`] but hands back the finished VM, so a test can
/// inspect runtime state the result value doesn't carry (arena occupancy).
#[cfg(test)]
fn run_chunk_native_vm(chunk: &Chunk, register: impl FnOnce(&mut VM)) -> Result<VM, String> {
    run_chunk_with(chunk, register, build_entry)
}

/// Shared body of the two runners: stand up a JIT module with every runtime shim
/// registered, lower `chunk` with `build`, and run the result on a fresh VM.
fn run_chunk_with<F>(
    chunk: &Chunk,
    register: impl FnOnce(&mut VM),
    build: F,
) -> Result<VM, String>
where
    F: FnOnce(&mut JITModule, &Chunk) -> Result<FuncId, String>,
{
    let isa = host_isa()?;
    let mut builder = JITBuilder::with_isa(isa, default_libcall_names());
    builder.symbol("fusevm_aot_exec_op", fusevm_aot_exec_op as *const u8);
    builder.symbol("fusevm_aot_finish", fusevm_aot_finish as *const u8);
    builder.symbol(
        "fusevm_aot_set_int_result",
        fusevm_aot_set_int_result as *const u8,
    );
    builder.symbol(
        "fusevm_aot_set_float_result",
        fusevm_aot_set_float_result as *const u8,
    );
    builder.symbol("fusevm_aot_powf", fusevm_aot_powf as *const u8);
    builder.symbol("fusevm_aot_fmod", fusevm_aot_fmod as *const u8);
    builder.symbol("fusevm_aot_unary_math", fusevm_aot_unary_math as *const u8);
    builder.symbol("fusevm_aot_atan2", fusevm_aot_atan2 as *const u8);
    builder.symbol(
        "fusevm_aot_mulmod_floor",
        fusevm_aot_mulmod_floor as *const u8,
    );
    builder.symbol(
        "fusevm_aot_muladdmod_floor",
        fusevm_aot_muladdmod_floor as *const u8,
    );
    builder.symbol("fusevm_aot_set_error", fusevm_aot_set_error as *const u8);
    builder.symbol("fusevm_aot_awk_warn", fusevm_aot_awk_warn as *const u8);
    builder.symbol("fusevm_aot_push_int", fusevm_aot_push_int as *const u8);
    builder.symbol("fusevm_aot_push_float", fusevm_aot_push_float as *const u8);
    builder.symbol("fusevm_aot_push_bool", fusevm_aot_push_bool as *const u8);
    builder.symbol("fusevm_aot_pop_float", fusevm_aot_pop_float as *const u8);
    builder.symbol(
        "fusevm_aot_store_slot_int",
        fusevm_aot_store_slot_int as *const u8,
    );
    builder.symbol(
        "fusevm_aot_store_slot_float",
        fusevm_aot_store_slot_float as *const u8,
    );
    builder.symbol(
        "fusevm_aot_store_global_int",
        fusevm_aot_store_global_int as *const u8,
    );
    builder.symbol(
        "fusevm_aot_store_global_float",
        fusevm_aot_store_global_float as *const u8,
    );
    builder.symbol("fusevm_aot_resume", fusevm_aot_resume as *const u8);
    builder.symbol("fusevm_aot_pop_int", fusevm_aot_pop_int as *const u8);
    builder.symbol(
        "fusevm_aot_push_status",
        fusevm_aot_push_status as *const u8,
    );
    builder.symbol("fusevm_aot_box", fusevm_aot_box as *const u8);
    builder.symbol("fusevm_aot_unbox", fusevm_aot_unbox as *const u8);
    builder.symbol("fusevm_aot_clone", fusevm_aot_clone as *const u8);
    builder.symbol("fusevm_aot_free", fusevm_aot_free as *const u8);
    builder.symbol("fusevm_aot_truthy", fusevm_aot_truthy as *const u8);
    builder.symbol(
        "fusevm_aot_truthy_keep",
        fusevm_aot_truthy_keep as *const u8,
    );
    builder.symbol(
        "fusevm_aot_store_slot_obj",
        fusevm_aot_store_slot_obj as *const u8,
    );
    builder.symbol(
        "fusevm_aot_store_global_obj",
        fusevm_aot_store_global_obj as *const u8,
    );
    builder.symbol(
        "fusevm_aot_set_obj_result",
        fusevm_aot_set_obj_result as *const u8,
    );
    builder.symbol(
        "fusevm_aot_set_status_result",
        fusevm_aot_set_status_result as *const u8,
    );
    let mut module = JITModule::new(builder);

    let entry_id = build(&mut module, chunk)?;
    module
        .finalize_definitions()
        .map_err(|e| format!("aot: finalize: {e}"))?;
    let code = module.get_finalized_function(entry_id);
    // SAFETY: `code` is the finalized entry with the declared C ABI.
    let entry: extern "C" fn(*mut VM) -> i64 = unsafe { std::mem::transmute(code) };

    let mut vm = VM::new(chunk.clone());
    register(&mut vm);
    let _ = entry(&mut vm as *mut VM);
    Ok(vm)
}

/// Build a Cranelift ISA for emitting a relocatable object (`is_pic=true`).
fn object_isa() -> Result<cranelift_codegen::isa::OwnedTargetIsa, String> {
    let mut fb = settings::builder();
    let _ = fb.set("use_colocated_libcalls", "false");
    let _ = fb.set("is_pic", "true");
    let _ = fb.set("opt_level", "speed");
    let flags = settings::Flags::new(fb);
    let isa_builder = cranelift_native::builder().map_err(|e| format!("aot: native ISA: {e}"))?;
    isa_builder
        .finish(flags)
        .map_err(|e| format!("aot: ISA finish: {e}"))
}

/// Ahead-of-time compile `chunk` to a relocatable native object file at
/// `out_path`. The object exports:
///   - [`AOT_ENTRY_SYMBOL`] — the native driver `fn(*mut VM) -> i64`,
///   - [`AOT_CHUNK_BLOB_SYMBOL`] / [`AOT_CHUNK_LEN_SYMBOL`] — the serialized
///     chunk and its length, so the runtime can rebuild the VM that the driver
///     reads ops/constants from,
/// and imports the runtime shims (`fusevm_aot_exec_op`, `fusevm_aot_finish`) plus
/// the frontend hook `fusevm_aot_register_builtins`, all resolved when the object
/// is linked against the frontend runtime (see [`fusevm_aot_run_embedded`]).
pub fn compile_object(chunk: &Chunk, out_path: &Path) -> Result<(), String> {
    let isa = object_isa()?;
    let builder = ObjectBuilder::new(isa, "fusevm_aot", default_libcall_names())
        .map_err(|e| format!("aot: object builder: {e}"))?;
    let mut module = ObjectModule::new(builder);

    let blob = stamped(chunk)?;
    let blob_id = module
        .declare_data(AOT_CHUNK_BLOB_SYMBOL, Linkage::Export, false, false)
        .map_err(|e| format!("aot: declare blob: {e}"))?;
    let mut blob_desc = DataDescription::new();
    blob_desc.define(blob.clone().into_boxed_slice());
    module
        .define_data(blob_id, &blob_desc)
        .map_err(|e| format!("aot: define blob: {e}"))?;

    let len_id = module
        .declare_data(AOT_CHUNK_LEN_SYMBOL, Linkage::Export, false, false)
        .map_err(|e| format!("aot: declare len: {e}"))?;
    let mut len_desc = DataDescription::new();
    len_desc.define(
        (blob.len() as u64)
            .to_le_bytes()
            .to_vec()
            .into_boxed_slice(),
    );
    module
        .define_data(len_id, &len_desc)
        .map_err(|e| format!("aot: define len: {e}"))?;

    build_entry(&mut module, chunk)?;

    let product = module.finish();
    let bytes = product
        .emit()
        .map_err(|e| format!("aot: emit object: {e}"))?;
    std::fs::write(out_path, bytes)
        .map_err(|e| format!("aot: write {}: {e}", out_path.display()))?;
    Ok(())
}

/// Compile a whole program into ONE relocatable object: the `main` chunk under
/// [`AOT_ENTRY_SYMBOL`], plus every `(symbol, chunk)` in `extras` under its own
/// exported symbol. Also exports the serialized main-chunk blob (like
/// [`compile_object`]) so the runtime driver can build the VM. This is what lets
/// a frontend AOT-lower a program's method/block bodies to native code — not just
/// its top-level chunk — and resolve each by symbol to build a native dispatch
/// table. Every chunk lowers through the same [`build_named`] path (fully-native
/// when it qualifies, else threaded), so each is real machine code.
pub fn compile_program_object(
    main: &Chunk,
    extras: &[(String, &Chunk)],
    out_path: &Path,
) -> Result<(), String> {
    let isa = object_isa()?;
    let builder = ObjectBuilder::new(isa, "fusevm_aot", default_libcall_names())
        .map_err(|e| format!("aot: object builder: {e}"))?;
    let mut module = ObjectModule::new(builder);

    // Serialized main chunk + length, same exported symbols `compile_object` uses,
    // so the runtime driver deserializes and builds the VM identically.
    let blob = stamped(main)?;
    let blob_id = module
        .declare_data(AOT_CHUNK_BLOB_SYMBOL, Linkage::Export, false, false)
        .map_err(|e| format!("aot: declare blob: {e}"))?;
    let mut blob_desc = DataDescription::new();
    blob_desc.define(blob.clone().into_boxed_slice());
    module
        .define_data(blob_id, &blob_desc)
        .map_err(|e| format!("aot: define blob: {e}"))?;
    let len_id = module
        .declare_data(AOT_CHUNK_LEN_SYMBOL, Linkage::Export, false, false)
        .map_err(|e| format!("aot: declare len: {e}"))?;
    let mut len_desc = DataDescription::new();
    len_desc.define(
        (blob.len() as u64)
            .to_le_bytes()
            .to_vec()
            .into_boxed_slice(),
    );
    module
        .define_data(len_id, &len_desc)
        .map_err(|e| format!("aot: define len: {e}"))?;

    // The top-level driver, then each method/block body under its own symbol.
    build_named(&mut module, main, AOT_ENTRY_SYMBOL)?;
    for (sym, chunk) in extras {
        build_named(&mut module, chunk, sym)?;
    }

    let product = module.finish();
    let bytes = product
        .emit()
        .map_err(|e| format!("aot: emit object: {e}"))?;
    std::fs::write(out_path, bytes)
        .map_err(|e| format!("aot: write {}: {e}", out_path.display()))?;
    Ok(())
}

/// Runtime entry for a linked AOT binary. Deserializes the embedded chunk,
/// builds a VM, lets the frontend register its builtins, runs the native driver,
/// and maps the result to a process exit code. A frontend's AOT `main` calls
/// this; the symbols it references are satisfied by the linked object
/// ([`compile_object`]) and the frontend runtime.
///
/// The frontend must provide `extern "C" fn fusevm_aot_register_builtins(*mut VM)`.
#[no_mangle]
pub extern "C" fn fusevm_aot_run_embedded() -> i64 {
    extern "C" {
        static fusevm_aot_chunk_blob: u8;
        static fusevm_aot_chunk_len: u64;
        fn fusevm_aot_entry(vm: *mut VM) -> i64;
        fn fusevm_aot_register_builtins(vm: *mut VM);
    }
    // SAFETY: the linked object defines the blob/len symbols; the driver and
    // register hook are resolved at link time.
    let chunk: Chunk = unsafe {
        let len = fusevm_aot_chunk_len as usize;
        let bytes = std::slice::from_raw_parts(&fusevm_aot_chunk_blob as *const u8, len);
        let body = match bytes.strip_prefix(AOT_CHUNK_MAGIC.as_slice()) {
            Some(rest) => rest,
            None => {
                let seen: String = bytes
                    .iter()
                    .take(AOT_CHUNK_MAGIC.len())
                    .map(|b| {
                        if b.is_ascii_graphic() {
                            *b as char
                        } else {
                            '.'
                        }
                    })
                    .collect();
                eprintln!(
                    "fusevm aot: this object was built by a different fusevm — \
                     expected chunk format {}, found {seen:?}; rebuild it",
                    String::from_utf8_lossy(AOT_CHUNK_MAGIC)
                );
                return 1;
            }
        };
        match bincode::deserialize(body) {
            Ok(c) => c,
            Err(e) => {
                eprintln!("fusevm aot: corrupt embedded chunk: {e}");
                return 1;
            }
        }
    };
    let mut vm = VM::new(chunk);
    // SAFETY: frontend-provided; registers builtins on the fresh VM.
    unsafe { fusevm_aot_register_builtins(&mut vm as *mut VM) };
    // SAFETY: the compiled entry has the declared C ABI and reads `vm`.
    unsafe { fusevm_aot_entry(&mut vm as *mut VM) };
    match vm.take_aot_result() {
        VMResult::Ok(Value::Int(n)) => n,
        VMResult::Ok(Value::Status(s)) => s as i64,
        VMResult::Ok(_) => 0,
        VMResult::Halted => 0,
        VMResult::Error(e) => {
            eprintln!("fusevm aot: {e}");
            1
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::chunk::ChunkBuilder;
    use crate::op::Op;
    use crate::value::Value;

    /// Native AOT result must equal the interpreter's for the same chunk.
    fn assert_native_matches_interp(chunk: Chunk) {
        assert_native_matches_interp_with(chunk, |_| {});
    }

    /// Same, for a chunk that needs frontend builtins registered on both VMs.
    fn assert_native_matches_interp_with(chunk: Chunk, register: fn(&mut VM)) {
        let interp = {
            let mut vm = VM::new(chunk.clone());
            register(&mut vm);
            vm.run()
        };
        let native = run_chunk_native(&chunk, register).expect("native compile/run");
        match (interp, native) {
            (VMResult::Ok(a), VMResult::Ok(b)) => assert_eq!(a, b, "value mismatch"),
            (VMResult::Halted, VMResult::Halted) => {}
            (VMResult::Error(a), VMResult::Error(b)) => assert_eq!(a, b, "error mismatch"),
            (i, n) => panic!("interp {i:?} != native {n:?}"),
        }
    }

    /// Whether `build_named` would actually take the native path — stricter than
    /// [`native_lowerable`], which only says a plan exists.
    fn native_path_taken(chunk: &Chunk) -> bool {
        matches!(lowering_for(chunk), Lowering::Native { .. })
    }

    #[test]
    fn native_straight_line_arithmetic() {
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::LoadInt(3), 1);
        b.emit(Op::Add, 1);
        b.emit(Op::LoadInt(4), 1);
        b.emit(Op::Mul, 1);
        let chunk = b.build();
        // This chunk must take the native fast path (no shim), and be correct.
        assert!(
            native_lowerable(&chunk),
            "arithmetic chunk should lower natively"
        );
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_arithmetic_with_negate_dup_pop() {
        // ((7 dup +) negate)  →  -14, with a dropped extra value.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(7), 1);
        b.emit(Op::Dup, 1);
        b.emit(Op::Add, 1);
        b.emit(Op::Negate, 1);
        b.emit(Op::LoadInt(99), 1);
        b.emit(Op::Pop, 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk));
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_lowerable_rejects_unsupported_and_unsafe() {
        // A heap constant now lowers natively as a boxed handle (Kind::Obj),
        // returned as the program result — verified against the interpreter.
        let mut s = ChunkBuilder::new();
        let c = s.add_constant(Value::str("x"));
        s.emit(Op::LoadConst(c), 1);
        let schunk = s.build();
        assert!(native_lowerable(&schunk), "heap LoadConst lowers as Obj");
        assert_native_matches_interp(schunk);

        // A boolean left as the program result would box to the wrong Value
        // variant (`Bool` vs the native path's `Int`), so it is rejected.
        let mut cmp = ChunkBuilder::new();
        cmp.emit(Op::LoadInt(1), 1);
        cmp.emit(Op::LoadInt(2), 1);
        cmp.emit(Op::NumLt, 1);
        assert!(!native_lowerable(&cmp.build()), "bool result rejected");

        // Storing a boolean into a slot can't be a typed register, but it now
        // partial-lowers: the comparison runs native and the SetSlot deopts to
        // the interpreter. The result must still match.
        let mut bslot = ChunkBuilder::new();
        bslot.emit(Op::LoadInt(1), 1);
        bslot.emit(Op::LoadInt(2), 1);
        bslot.emit(Op::NumLt, 1);
        bslot.emit(Op::SetSlot(0), 1);
        assert_native_matches_interp(bslot.build());

        // Underflow (Add with one operand) is rejected, not miscompiled.
        let mut bad = ChunkBuilder::new();
        bad.emit(Op::LoadInt(1), 1);
        bad.emit(Op::Add, 1);
        assert!(
            !native_lowerable(&bad.build()),
            "underflow must be rejected"
        );
    }

    #[test]
    fn native_unset_slot_read_falls_back() {
        // Reading a slot never written would yield `Undef` in the interpreter
        // but 0 in a register — so definite-assignment must reject it, and the
        // threaded path must still match the interpreter.
        let mut b = ChunkBuilder::new();
        b.emit(Op::GetSlot(0), 1);
        let chunk = b.build();
        assert!(!native_lowerable(&chunk), "unset slot read must fall back");
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_slot_set_on_one_branch_falls_back() {
        // slot 0 is assigned only on the taken-if path; at the join it is not
        // definitely assigned, so the read must fall back to threaded.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(0), 1); // condition: false ⇒ skip the set
        let jf = b.emit(Op::JumpIfFalse(0), 1);
        b.emit(Op::LoadInt(7), 1);
        b.emit(Op::SetSlot(0), 1);
        let skip = b.current_pos();
        b.patch_jump(jf, skip);
        b.emit(Op::GetSlot(0), 1);
        let chunk = b.build();
        assert!(
            !native_lowerable(&chunk),
            "slot assigned on only one path must fall back"
        );
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_slot_set_on_both_branches_lowers() {
        // slot 0 assigned on both arms ⇒ definitely assigned at the join ⇒ the
        // read lowers natively. (cond false ⇒ else ⇒ slot0 = 2.)
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(0), 1);
        let jf = b.emit(Op::JumpIfFalse(0), 1);
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::SetSlot(0), 1);
        let j = b.emit(Op::Jump(0), 1);
        let else_ip = b.current_pos();
        b.patch_jump(jf, else_ip);
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::SetSlot(0), 1);
        let end_ip = b.current_pos();
        b.patch_jump(j, end_ip);
        b.emit(Op::GetSlot(0), 1);
        let chunk = b.build();
        assert!(
            native_lowerable(&chunk),
            "both-path assignment should lower"
        );
        match run_chunk_native(&chunk, |_| {}).expect("native run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Int(2)),
            other => panic!("expected Ok(2), got {other:?}"),
        }
    }

    #[test]
    fn native_float_slot_round_trip() {
        // x = 1.5; x = x + 0.25; return x → 1.75, with a float-typed slot.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(1.5), 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::GetSlot(0), 1);
        b.emit(Op::LoadFloat(0.25), 1);
        b.emit(Op::Add, 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::GetSlot(0), 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "float slot should lower");
        match run_chunk_native(&chunk, |_| {}).expect("run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Float(1.75)),
            other => panic!("got {other:?}"),
        }
    }

    #[test]
    fn native_float_accumulator_loop() {
        // sum = 0.0; for i in 0..5 { sum += 0.5 }; return sum → 2.5.
        // A float slot accumulator — the canonical awk pattern that previously
        // fell back to the interpreter.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(0.0), 1);
        b.emit(Op::SetSlot(0), 1); // sum (float)
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::SetSlot(1), 1); // i (int)
        let top = b.current_pos();
        b.emit(Op::GetSlot(1), 1);
        b.emit(Op::LoadInt(5), 1);
        b.emit(Op::NumLt, 1);
        let exit = b.emit(Op::JumpIfFalse(0), 1);
        b.emit(Op::GetSlot(0), 1); // sum (float)
        b.emit(Op::LoadFloat(0.5), 1);
        b.emit(Op::Add, 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::GetSlot(1), 1);
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::Add, 1);
        b.emit(Op::SetSlot(1), 1);
        b.emit(Op::Jump(top), 1);
        let end = b.current_pos();
        b.patch_jump(exit, end);
        b.emit(Op::GetSlot(0), 1);
        let chunk = b.build();
        assert!(
            native_lowerable(&chunk),
            "float accumulator loop should lower"
        );
        match run_chunk_native(&chunk, |_| {}).expect("run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Float(2.5)),
            other => panic!("got {other:?}"),
        }
        // (int slot `i` and float slot `sum` coexist in the same loop.)
    }

    #[test]
    fn native_float_global() {
        // Float global round-trips like a float slot.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(3.5), 1);
        b.emit(Op::SetVar(0), 1);
        b.emit(Op::GetVar(0), 1);
        b.emit(Op::LoadFloat(1.5), 1);
        b.emit(Op::Add, 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk));
        match run_chunk_native(&chunk, |_| {}).expect("run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Float(5.0)),
            other => panic!("got {other:?}"),
        }
    }

    #[test]
    fn native_mixed_kind_slot_falls_back() {
        // A slot stored as both Int and Float can't be a single typed register,
        // so the chunk falls back to threaded (and still matches the interp).
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::LoadFloat(2.5), 1);
        b.emit(Op::SetSlot(0), 1); // same slot, different kind → conflict
        b.emit(Op::GetSlot(0), 1);
        let chunk = b.build();
        assert!(!native_lowerable(&chunk), "mixed-kind slot must fall back");
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_int_slots_round_trip() {
        // x = 41; x = x + 1; return x  →  42, fully native (slots supported).
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(41), 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::GetSlot(0), 1);
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::Add, 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::GetSlot(0), 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk));
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_empty_is_lowerable() {
        assert!(native_lowerable(&ChunkBuilder::new().build()));
    }

    #[test]
    fn native_conditional_branch() {
        // if (1 < 2) push 10 else push 20  →  10
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::NumLt, 1);
        let jf = b.emit(Op::JumpIfFalse(0), 1);
        b.emit(Op::LoadInt(10), 1);
        let j = b.emit(Op::Jump(0), 1);
        let else_ip = b.emit(Op::LoadInt(20), 1);
        let end_ip = b.current_pos();
        b.patch_jump(jf, else_ip);
        b.patch_jump(j, end_ip);
        let chunk = b.build();
        // Control flow + comparison now lower natively.
        assert!(
            native_lowerable(&chunk),
            "branch chunk should lower natively"
        );
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_counted_loop() {
        // sum = 0; for i in 0..5 { sum += i }  via slots
        let mut b = ChunkBuilder::new();
        // slot 0 = sum, slot 1 = i
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::SetSlot(1), 1);
        let top = b.current_pos();
        b.emit(Op::GetSlot(1), 1);
        b.emit(Op::LoadInt(5), 1);
        b.emit(Op::NumLt, 1);
        let exit = b.emit(Op::JumpIfFalse(0), 1);
        b.emit(Op::GetSlot(0), 1);
        b.emit(Op::GetSlot(1), 1);
        b.emit(Op::Add, 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::GetSlot(1), 1);
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::Add, 1);
        b.emit(Op::SetSlot(1), 1);
        b.emit(Op::Jump(top), 1);
        let end_ip = b.current_pos();
        b.patch_jump(exit, end_ip);
        b.emit(Op::GetSlot(0), 1);
        let chunk = b.build();
        // A real loop with a back-edge, slots, and a comparison — all native.
        assert!(native_lowerable(&chunk), "loop chunk should lower natively");
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_loop_sum_value_is_correct() {
        // Same loop, but assert the concrete value (0+1+2+3+4 = 10), so a
        // miscompiled back-edge/phi would be caught, not just an interp match.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::SetSlot(1), 1);
        let top = b.current_pos();
        b.emit(Op::GetSlot(1), 1);
        b.emit(Op::LoadInt(5), 1);
        b.emit(Op::NumLt, 1);
        let exit = b.emit(Op::JumpIfFalse(0), 1);
        b.emit(Op::GetSlot(0), 1);
        b.emit(Op::GetSlot(1), 1);
        b.emit(Op::Add, 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::GetSlot(1), 1);
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::Add, 1);
        b.emit(Op::SetSlot(1), 1);
        b.emit(Op::Jump(top), 1);
        let end_ip = b.current_pos();
        b.patch_jump(exit, end_ip);
        b.emit(Op::GetSlot(0), 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk));
        match run_chunk_native(&chunk, |_| {}).expect("native run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Int(10), "0+1+2+3+4 == 10"),
            other => panic!("expected Ok(10), got {other:?}"),
        }
    }

    #[test]
    fn native_differential_arithmetic_and_branches() {
        // Deterministic differential fuzz: build many random integer programs
        // (balanced RPN arithmetic, optionally guarded by a comparison + branch)
        // and require the native compile to match the interpreter exactly.
        let mut seed: u64 = 0x9E37_79B9_7F4A_7C15;
        let mut next = || {
            // SplitMix64 — deterministic, no external rng.
            seed = seed.wrapping_add(0x9E37_79B9_7F4A_7C15);
            let mut z = seed;
            z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
            z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
            z ^ (z >> 31)
        };

        // Emit a balanced arithmetic expression leaving exactly one Int on the
        // stack (depth grows with LoadInt, shrinks with a binop).
        fn emit_expr(b: &mut ChunkBuilder, next: &mut impl FnMut() -> u64, terms: usize) {
            b.emit(Op::LoadInt((next() % 21) as i64 - 10), 1);
            for _ in 0..terms {
                b.emit(Op::LoadInt((next() % 21) as i64 - 10), 1);
                match next() % 3 {
                    0 => b.emit(Op::Add, 1),
                    1 => b.emit(Op::Sub, 1),
                    _ => b.emit(Op::Mul, 1),
                };
            }
        }

        for _ in 0..400 {
            let mut b = ChunkBuilder::new();
            if next() % 2 == 0 {
                // Plain arithmetic expression.
                let terms = (next() % 5) as usize;
                emit_expr(&mut b, &mut next, terms);
            } else {
                // if (a < b) <expr1> else <expr2>
                b.emit(Op::LoadInt((next() % 11) as i64 - 5), 1);
                b.emit(Op::LoadInt((next() % 11) as i64 - 5), 1);
                match next() % 4 {
                    0 => b.emit(Op::NumLt, 1),
                    1 => b.emit(Op::NumGt, 1),
                    2 => b.emit(Op::NumEq, 1),
                    _ => b.emit(Op::NumGe, 1),
                };
                let jf = b.emit(Op::JumpIfFalse(0), 1);
                let then_terms = (next() % 4) as usize;
                emit_expr(&mut b, &mut next, then_terms);
                let j = b.emit(Op::Jump(0), 1);
                let else_ip = b.current_pos();
                b.patch_jump(jf, else_ip);
                let else_terms = (next() % 4) as usize;
                emit_expr(&mut b, &mut next, else_terms);
                let end_ip = b.current_pos();
                b.patch_jump(j, end_ip);
            }
            let chunk = b.build();
            assert!(
                native_lowerable(&chunk),
                "generated chunk must lower natively"
            );
            assert_native_matches_interp(chunk);
        }
    }

    #[test]
    fn native_differential_integer_bitwise() {
        // Deterministic differential fuzz over the integer op set, including
        // bitwise/shift and Inc/Dec. Every value is Int, so each chunk lowers
        // and must match the interpreter exactly (wrapping on both sides).
        let mut seed: u64 = 0x1234_5678_9ABC_DEF0;
        let mut next = || {
            seed = seed.wrapping_add(0x9E37_79B9_7F4A_7C15);
            let mut z = seed;
            z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
            z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
            z ^ (z >> 31)
        };

        for _ in 0..400 {
            let mut b = ChunkBuilder::new();
            b.emit(Op::LoadInt((next() % 17) as i64 - 8), 1);
            let terms = next() % 8;
            for _ in 0..terms {
                if next() % 3 == 0 {
                    // unary, int → int (depth unchanged)
                    match next() % 3 {
                        0 => b.emit(Op::Inc, 1),
                        1 => b.emit(Op::Dec, 1),
                        _ => b.emit(Op::BitNot, 1),
                    };
                } else {
                    // binary: push another operand, then combine (depth unchanged)
                    b.emit(Op::LoadInt((next() % 17) as i64 - 8), 1);
                    match next() % 9 {
                        0 => b.emit(Op::Add, 1),
                        1 => b.emit(Op::Sub, 1),
                        2 => b.emit(Op::Mul, 1),
                        3 => b.emit(Op::BitAnd, 1),
                        4 => b.emit(Op::BitOr, 1),
                        5 => b.emit(Op::BitXor, 1),
                        6 => b.emit(Op::Shl, 1),
                        7 => b.emit(Op::Shr, 1),
                        // operands span -8..8, so this exercises `% 0` and `% -1`.
                        _ => b.emit(Op::Mod, 1),
                    };
                }
            }
            let chunk = b.build();
            assert!(native_lowerable(&chunk), "integer chunk must lower");
            assert_native_matches_interp(chunk);
        }
    }

    #[test]
    fn native_bool_lognot_and_bitwise() {
        // !true → false; (false) & 5 == 0. A Bool flows through LogNot into a
        // bitwise op (Bool is int-like) and yields an Int result — all native.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadTrue, 1);
        b.emit(Op::LogNot, 1); // false (0)
        b.emit(Op::LoadInt(5), 1);
        b.emit(Op::BitAnd, 1); // 0 & 5 = 0
        let chunk = b.build();
        assert!(native_lowerable(&chunk));
        match run_chunk_native(&chunk, |_| {}).expect("native run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Int(0)),
            other => panic!("expected Ok(0), got {other:?}"),
        }

        // A bare Bool result is not numeric ⇒ falls back; threaded must match.
        let mut bb = ChunkBuilder::new();
        bb.emit(Op::LoadFalse, 1);
        bb.emit(Op::LogNot, 1); // true — but a Bool result, so not lowered
        let chunk = bb.build();
        assert!(!native_lowerable(&chunk), "bare bool result falls back");
        assert_native_matches_interp(chunk);
    }

    /// Run a lowerable chunk natively and assert its integer result.
    fn assert_native_int(chunk: Chunk, expect: i64) {
        assert!(native_lowerable(&chunk), "chunk should lower natively");
        match run_chunk_native(&chunk, |_| {}).expect("native run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Int(expect)),
            other => panic!("expected Ok({expect}), got {other:?}"),
        }
    }

    /// Run a lowerable chunk natively and assert its (finite) float result.
    fn assert_native_int_or_float(chunk: Chunk, expect: f64) {
        assert!(native_lowerable(&chunk), "chunk should lower natively");
        match run_chunk_native(&chunk, |_| {}).expect("native run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Float(expect)),
            other => panic!("expected Ok({expect}), got {other:?}"),
        }
    }

    #[test]
    fn native_partial_deopt() {
        // A chunk mixing native arithmetic with a non-lowerable op (Concat) now
        // lowers: the arithmetic runs native, the Concat deopts to the
        // interpreter, and the result matches end-to-end.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::LoadInt(3), 1);
        b.emit(Op::Add, 1); // 5, native
        b.emit(Op::LoadInt(4), 1); // native
        b.emit(Op::Concat, 1); // non-lowerable → deopt; "5" + "4" = "54"
        let chunk = b.build();
        assert!(
            native_lowerable(&chunk),
            "mixed chunk should lower (with deopt)"
        );
        match run_chunk_native(&chunk, |_| {}).expect("run") {
            VMResult::Ok(v) => assert_eq!(v, Value::str("54")),
            other => panic!("got {other:?}"),
        }
        assert_native_matches_interp(chunk);

        // Slot state set NATIVELY must survive the deopt: slot 0 = 99 in a
        // register, then a Concat deopts; the resumed interpreter reads slot 0.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(99), 1);
        b.emit(Op::SetSlot(0), 1); // native register
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::Concat, 1); // deopt
        b.emit(Op::Pop, 1);
        b.emit(Op::GetSlot(0), 1); // interpreter reads slot 0 → must be 99
        let chunk = b.build();
        assert!(native_lowerable(&chunk));
        match run_chunk_native(&chunk, |_| {}).expect("run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Int(99), "slot survived partial deopt"),
            other => panic!("got {other:?}"),
        }
        assert_native_matches_interp(chunk);

        // The correctness fix: an UNassigned slot must stay Undef across the
        // deopt (not be written back as a register's 0).
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::LoadUndef, 1); // not lowered → deopt; slot 0 never assigned
        b.emit(Op::Pop, 1);
        b.emit(Op::GetSlot(0), 1); // interpreter reads an unassigned slot → Undef
        let chunk = b.build();
        assert!(native_lowerable(&chunk));
        match run_chunk_native(&chunk, |_| {}).expect("run") {
            VMResult::Ok(Value::Undef) => {}
            other => panic!("expected Undef (unassigned slot), got {other:?}"),
        }
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_differential_partial_deopt() {
        // Native integer expressions, half of them followed by a non-lowerable
        // Concat (which deopts). Both the pure-native and the native-prefix-then-
        // deopt forms must match the interpreter.
        let mut seed: u64 = 0x6C62_72E0_7E1A_55D9;
        let mut next = || {
            seed = seed.wrapping_add(0x9E37_79B9_7F4A_7C15);
            let mut z = seed;
            z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
            z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
            z ^ (z >> 31)
        };
        let mut deopted = 0;
        for _ in 0..400 {
            let mut b = ChunkBuilder::new();
            b.emit(Op::LoadInt((next() % 21) as i64 - 10), 1);
            for _ in 0..(next() % 4) {
                b.emit(Op::LoadInt((next() % 21) as i64 - 10), 1);
                match next() % 3 {
                    0 => b.emit(Op::Add, 1),
                    1 => b.emit(Op::Sub, 1),
                    _ => b.emit(Op::Mul, 1),
                };
            }
            if next() % 2 == 0 {
                // append a Concat with a literal → non-lowerable, deopts.
                b.emit(Op::LoadInt((next() % 21) as i64 - 10), 1);
                b.emit(Op::Concat, 1);
                deopted += 1;
            }
            let chunk = b.build();
            assert!(native_lowerable(&chunk), "expr (±Concat) should lower");
            assert_native_matches_interp(chunk);
        }
        assert!(
            deopted > 100,
            "expected many deopting chunks, got {deopted}"
        );
    }

    #[test]
    fn native_conditional_slot_before_deopt_is_sound() {
        // Regression: a slot assigned on only ONE branch before a deopt can't be
        // soundly written back (its register can't distinguish a real value from
        // Undef), so the chunk must fall back to threaded — never miscompile.
        //   if (1) slot0 = 5;  "7"~"8" (deopt);  pop;  return slot0   → 5
        let build = |cond: i64| {
            let mut b = ChunkBuilder::new();
            b.emit(Op::LoadInt(cond), 1);
            let jf = b.emit(Op::JumpIfFalse(0), 1);
            b.emit(Op::LoadInt(5), 1);
            b.emit(Op::SetSlot(0), 1); // conditional assignment
            let skip = b.current_pos();
            b.patch_jump(jf, skip);
            b.emit(Op::LoadInt(7), 1);
            b.emit(Op::LoadInt(8), 1);
            b.emit(Op::Concat, 1); // deopt
            b.emit(Op::Pop, 1);
            b.emit(Op::GetSlot(0), 1);
            b.build()
        };
        // Maybe-assigned slot before a deopt ⇒ not lowered, falls back.
        let chunk = build(1);
        assert!(
            !native_lowerable(&chunk),
            "conditional slot before deopt must fall back, not miscompile"
        );
        // Both branch values must match the interpreter via the threaded path.
        assert_native_matches_interp(build(1)); // cond true  → slot0 = 5
        assert_native_matches_interp(build(0)); // cond false → slot0 = Undef
    }

    #[test]
    fn native_differential_cond_assign_deopt() {
        // Hammer the must/may-assigned soundness boundary: random combinations of
        // unconditional/conditional slot assignment before a deopt, then a read.
        // Whether the chunk lowers (must-assigned) or falls back (maybe-assigned),
        // the result must match the interpreter.
        let mut seed: u64 = 0x9F1C_2B3D_4E5A_6071;
        let mut next = || {
            seed = seed.wrapping_add(0x9E37_79B9_7F4A_7C15);
            let mut z = seed;
            z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
            z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
            z ^ (z >> 31)
        };
        for _ in 0..400 {
            let mut b = ChunkBuilder::new();
            let init = next() % 2 == 0; // unconditional init before the branch
            if init {
                b.emit(Op::LoadInt((next() % 9) as i64), 1);
                b.emit(Op::SetSlot(0), 1);
            }
            b.emit(Op::LoadInt((next() % 2) as i64), 1); // cond 0/1
            let jf = b.emit(Op::JumpIfFalse(0), 1);
            b.emit(Op::LoadInt((next() % 9) as i64 + 10), 1); // true-branch value
            b.emit(Op::SetSlot(0), 1);
            let j = b.emit(Op::Jump(0), 1);
            let else_ip = b.current_pos();
            b.patch_jump(jf, else_ip);
            if next() % 2 == 0 {
                // else branch also assigns (→ definitely assigned at the join)
                b.emit(Op::LoadInt((next() % 9) as i64 + 20), 1);
                b.emit(Op::SetSlot(0), 1);
            }
            let end_ip = b.current_pos();
            b.patch_jump(j, end_ip);
            // a deopt, then read slot 0 as the result
            b.emit(Op::LoadInt(7), 1);
            b.emit(Op::LoadInt(8), 1);
            b.emit(Op::Concat, 1);
            b.emit(Op::Pop, 1);
            b.emit(Op::GetSlot(0), 1);
            // Result must match the interpreter on whichever path was chosen.
            assert_native_matches_interp(b.build());
        }
    }

    #[test]
    fn native_boxed_concat_lowers() {
        // Heap string constants + Concat now lower natively via boxed handles
        // (no deopt): each LoadConst boxes into the arena, Concat unboxes/joins/
        // re-boxes, and the Obj result is returned. Must match the interpreter.
        let mut b = ChunkBuilder::new();
        let a = b.add_constant(Value::str("a"));
        let c = b.add_constant(Value::str("b"));
        b.emit(Op::LoadConst(a), 1);
        b.emit(Op::LoadConst(c), 1);
        b.emit(Op::Concat, 1); // "ab"
        let chunk = b.build();
        assert!(
            native_lowerable(&chunk),
            "boxed concat should lower natively"
        );
        assert_native_matches_interp(chunk);

        // Mixed scalar/boxed operands: "n=" . (2 + 3) → "n=5". The arithmetic
        // runs in registers, the Int is spilled, the string is unboxed.
        let mut b = ChunkBuilder::new();
        let pre = b.add_constant(Value::str("n="));
        b.emit(Op::LoadConst(pre), 1);
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::LoadInt(3), 1);
        b.emit(Op::Add, 1);
        b.emit(Op::Concat, 1);
        assert_native_matches_interp(b.build());

        // Three-way concat (boxed result fed back into Concat): "a"."b"."c".
        let mut b = ChunkBuilder::new();
        let k = |s| Value::str(s);
        let (x, y, z) = (
            b.add_constant(k("a")),
            b.add_constant(k("b")),
            b.add_constant(k("c")),
        );
        b.emit(Op::LoadConst(x), 1);
        b.emit(Op::LoadConst(y), 1);
        b.emit(Op::Concat, 1);
        b.emit(Op::LoadConst(z), 1);
        b.emit(Op::Concat, 1);
        assert_native_matches_interp(b.build());
    }

    #[test]
    fn native_named_array_hash_ops_lower() {
        // Array element round-trip: a[0]=42; a[0] → 42 (boxed Obj result).
        let mut b = ChunkBuilder::new();
        b.emit(Op::DeclareArray(0), 1);
        b.emit(Op::LoadInt(42), 1);
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::ArraySet(0), 1); // a[0] = 42  (stack [value, index])
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::ArrayGet(0), 1); // → 42
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "array element ops lower");
        assert_native_matches_interp(chunk);

        // Push in a loop, then length → 3 (scalar Int result).
        let mut b = ChunkBuilder::new();
        b.emit(Op::DeclareArray(0), 1);
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::SetSlot(0), 1); // i = 0
        b.emit(Op::GetSlot(0), 1); // leader ip 3
        b.emit(Op::ArrayPush(0), 1); // push i
        b.emit(Op::SlotIncLtIntJumpBack(0, 3, 3), 1); // i++; if i<3 goto 3
        b.emit(Op::ArrayLen(0), 1); // → 3
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "array push loop + len lowers");
        assert_native_matches_interp(chunk);

        // Hash element round-trip: h{"k"}=1; h{"k"} → 1.
        let mut b = ChunkBuilder::new();
        let k = b.add_constant(Value::str("k"));
        b.emit(Op::DeclareHash(0), 1);
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::LoadConst(k), 1);
        b.emit(Op::HashSet(0), 1); // h{"k"} = 1  (stack [value, key])
        b.emit(Op::LoadConst(k), 1);
        b.emit(Op::HashGet(0), 1); // → 1
        assert_native_matches_interp(b.build());
    }

    #[test]
    fn native_expand_param_ops_lower() {
        // `${var:-def}` (1 arg + name = 2 pops) and `${#var}` (LENGTH: 0 args +
        // name = 1 pop). No host ⇒ "" deterministically; pop counts must match
        // the interpreter exactly or the boxed stack desyncs.
        let mut b = ChunkBuilder::new();
        let name = b.add_constant(Value::str("v"));
        let def = b.add_constant(Value::str("d"));
        b.emit(Op::LoadConst(name), 1);
        b.emit(Op::LoadConst(def), 1);
        b.emit(Op::ExpandParam(crate::op::param_mod::DEFAULT), 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "ExpandParam(DEFAULT) lowers");
        assert_native_matches_interp(chunk);

        let mut b = ChunkBuilder::new();
        let name = b.add_constant(Value::str("v"));
        b.emit(Op::LoadConst(name), 1);
        b.emit(Op::ExpandParam(crate::op::param_mod::LENGTH), 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "ExpandParam(LENGTH) lowers");
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_shell_predicate_ops_lower() {
        // Glob/regex match (no host ⇒ deterministic) and file tests lower as
        // boxed-heap Bool predicates; result consumed by MakeArray for a valid
        // end. Verified against the interpreter.
        let mut b = ChunkBuilder::new();
        let s = b.add_constant(Value::str("abc"));
        let p = b.add_constant(Value::str("abc"));
        b.emit(Op::LoadConst(s), 1);
        b.emit(Op::LoadConst(p), 1);
        b.emit(Op::StrMatch, 1); // no host ⇒ s == pat ⇒ true
        b.emit(Op::MakeArray(1), 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "StrMatch lowers");
        assert_native_matches_interp(chunk);

        // TestFile: "/" is a directory ⇒ true.
        let mut b = ChunkBuilder::new();
        let root = b.add_constant(Value::str("/"));
        b.emit(Op::LoadConst(root), 1);
        b.emit(Op::TestFile(crate::op::file_test::IS_DIR), 1);
        b.emit(Op::MakeArray(1), 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "TestFile lowers");
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_expansion_source_ops_lower() {
        // Host-dispatched string-expansion ops lower as boxed sources; with no
        // host installed both interp and native take the same fallback, so the
        // result matches. (Exercises the (1 → Obj) general-arm path.)
        for op in [Op::BraceExpand, Op::WordSplit, Op::TildeExpand] {
            let mut b = ChunkBuilder::new();
            let s = b.add_constant(Value::str("a b c"));
            b.emit(Op::LoadConst(s), 1);
            b.emit(op, 1);
            let chunk = b.build();
            assert!(native_lowerable(&chunk), "expansion op lowers");
            assert_native_matches_interp(chunk);
        }
    }

    #[test]
    fn native_string_compares_lower() {
        // StrCmp → Int (-1/0/1), a valid program result.
        let mut b = ChunkBuilder::new();
        let a = b.add_constant(Value::str("abc"));
        let c = b.add_constant(Value::str("abd"));
        b.emit(Op::LoadConst(a), 1);
        b.emit(Op::LoadConst(c), 1);
        b.emit(Op::StrCmp, 1); // -1
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "StrCmp lowers");
        assert_native_matches_interp(chunk);

        // StrEq / StrLt → Bool, consumed by MakeArray so it's a valid end.
        for (op, l, r) in [
            (Op::StrEq, "x", "x"),
            (Op::StrEq, "x", "y"),
            (Op::StrLt, "a", "b"),
            (Op::StrGe, "b", "a"),
        ] {
            let mut b = ChunkBuilder::new();
            let lc = b.add_constant(Value::str(l));
            let rc = b.add_constant(Value::str(r));
            b.emit(Op::LoadConst(lc), 1);
            b.emit(Op::LoadConst(rc), 1);
            b.emit(op, 1);
            b.emit(Op::MakeArray(1), 1); // [bool]
            let chunk = b.build();
            assert!(native_lowerable(&chunk), "string compare lowers");
            assert_native_matches_interp(chunk);
        }
    }

    #[test]
    fn native_whole_array_and_hash_view_ops_lower() {
        // SetArray/GetArray whole-array round-trip: a = [1,2]; @a → [1,2].
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::MakeArray(2), 1);
        b.emit(Op::SetArray(0), 1); // a = [1,2]
        b.emit(Op::GetArray(0), 1); // → [1,2]
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "GetArray/SetArray lower");
        assert_native_matches_interp(chunk);

        // HashDelete returns the removed value: h{"x"}=5; delete h{"x"} → 5.
        let mut b = ChunkBuilder::new();
        let x = b.add_constant(Value::str("x"));
        b.emit(Op::DeclareHash(0), 1);
        b.emit(Op::LoadInt(5), 1);
        b.emit(Op::LoadConst(x), 1);
        b.emit(Op::HashSet(0), 1);
        b.emit(Op::LoadConst(x), 1);
        b.emit(Op::HashDelete(0), 1); // → 5
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "HashDelete lowers");
        assert_native_matches_interp(chunk);

        // HashExists (Bool result) consumed by MakeArray: [exists h{"x"}].
        let mut b = ChunkBuilder::new();
        let x = b.add_constant(Value::str("x"));
        b.emit(Op::DeclareHash(0), 1);
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::LoadConst(x), 1);
        b.emit(Op::HashSet(0), 1);
        b.emit(Op::LoadConst(x), 1);
        b.emit(Op::HashExists(0), 1); // Bool(true)
        b.emit(Op::MakeArray(1), 1); // [true]
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "HashExists (bool result) lowers");
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_array_global_alias_bails() {
        // Name 0 used as BOTH a register-cached global (SetVar/GetVar) and a
        // named array (ArrayPush) → the register would shadow the shim's view of
        // self.globals[0], so the chunk must fall back to threaded (not lower),
        // and threaded must still match the interpreter.
        let mut b = ChunkBuilder::new();
        b.emit(Op::DeclareArray(0), 1);
        b.emit(Op::LoadInt(7), 1);
        b.emit(Op::SetVar(0), 1); // name 0 as a global register
        b.emit(Op::LoadInt(9), 1);
        b.emit(Op::ArrayPush(0), 1); // name 0 as an array → overlap
        b.emit(Op::GetVar(0), 1);
        let chunk = b.build();
        assert!(
            !native_lowerable(&chunk),
            "global/array name overlap must bail"
        );
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_heap_ops_lower() {
        // String repeat/len, array & hash construction, and ranges now lower as
        // boxed heap ops (staged operands → shim → reloaded result), matching
        // the interpreter.
        let mut b = ChunkBuilder::new();
        let ab = b.add_constant(Value::str("ab"));
        b.emit(Op::LoadConst(ab), 1);
        b.emit(Op::LoadInt(3), 1);
        b.emit(Op::StringRepeat, 1); // "ababab"
        assert_native_matches_interp(b.build());

        let mut b = ChunkBuilder::new();
        let hello = b.add_constant(Value::str("hello"));
        b.emit(Op::LoadConst(hello), 1);
        b.emit(Op::StringLen, 1); // Int 5 (scalar result)
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "StringLen lowers");
        assert_native_matches_interp(chunk);

        // MakeArray with mixed scalar/computed/boxed elements: [1, "x", 2+3].
        let mut b = ChunkBuilder::new();
        let x = b.add_constant(Value::str("x"));
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::LoadConst(x), 1);
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::LoadInt(3), 1);
        b.emit(Op::Add, 1);
        b.emit(Op::MakeArray(3), 1);
        assert_native_matches_interp(b.build());

        // Range and a StringLen of nothing-special; both boxed-Obj / scalar.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::LoadInt(5), 1);
        b.emit(Op::Range, 1); // [1,2,3,4,5]
        assert_native_matches_interp(b.build());

        // MakeHash(2): {"k" => 1}.
        let mut b = ChunkBuilder::new();
        let kk = b.add_constant(Value::str("k"));
        b.emit(Op::LoadConst(kk), 1);
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::MakeHash(2), 1);
        assert_native_matches_interp(b.build());
    }

    #[test]
    fn native_obj_slot_accumulator_loop_lowers() {
        // The classic heap-in-loop case now runs fully native: an Obj-typed slot
        // holds a string accumulator, concatenated each iteration. Ownership
        // (clone-on-read, free-on-overwrite, take-on-consume) keeps it correct;
        // the arena stays bounded. Result must match the interpreter ("xxx").
        let mut b = ChunkBuilder::new();
        let empty = b.add_constant(Value::str(""));
        let x = b.add_constant(Value::str("x"));
        b.emit(Op::LoadInt(0), 1); // i = 0
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::LoadConst(empty), 1); // s = "" (Obj slot)
        b.emit(Op::SetSlot(1), 1);
        // loop body (leader at ip 4):
        b.emit(Op::GetSlot(1), 1); // s   (clone)
        b.emit(Op::LoadConst(x), 1); // "x"
        b.emit(Op::Concat, 1); // s . "x"
        b.emit(Op::SetSlot(1), 1); // s = ...
        b.emit(Op::SlotIncLtIntJumpBack(0, 3, 4), 1); // i++; if i<3 goto 4
        b.emit(Op::GetSlot(1), 1); // result: s
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "Obj-slot accumulator loop lowers");
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_obj_accumulator_differential_fuzz() {
        // Stress the handle-ownership/freelist across many iteration counts and
        // string pieces: every accumulator loop must lower and match the interp.
        // (A double-free / use-after-free or leak-as-wrong-value would diverge.)
        let mut seed: u64 = 0x1234_5678_9ABC_DEF0;
        let mut next = || {
            seed = seed.wrapping_add(0x9E37_79B9_7F4A_7C15);
            let mut z = seed;
            z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
            z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
            z ^ (z >> 31)
        };
        for _ in 0..200 {
            let iters = (next() % 12) as i32; // 0..11 iterations
            let piece = ["x", "ab", "", "Z9"][(next() % 4) as usize];
            let mut b = ChunkBuilder::new();
            let empty = b.add_constant(Value::str("acc:"));
            let p = b.add_constant(Value::str(piece));
            b.emit(Op::LoadInt(0), 1);
            b.emit(Op::SetSlot(0), 1);
            b.emit(Op::LoadConst(empty), 1);
            b.emit(Op::SetSlot(1), 1);
            b.emit(Op::GetSlot(1), 1); // leader ip 4
            b.emit(Op::LoadConst(p), 1);
            b.emit(Op::Concat, 1);
            b.emit(Op::SetSlot(1), 1);
            b.emit(Op::SlotIncLtIntJumpBack(0, iters, 4), 1);
            b.emit(Op::GetSlot(1), 1);
            let chunk = b.build();
            // iters<=0 still executes the body once (post-inc test), matching
            // the interpreter; either way native must agree.
            assert_native_matches_interp(chunk);
        }
    }

    #[test]
    fn native_obj_global_roundtrip_lowers() {
        // An Obj global: store a string, read it back as the result.
        let mut b = ChunkBuilder::new();
        let s = b.add_constant(Value::str("hi"));
        b.emit(Op::LoadConst(s), 1);
        b.emit(Op::SetVar(0), 1);
        b.emit(Op::GetVar(0), 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "Obj global round-trips");
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_boxed_value_then_deopt_spills() {
        // A boxed value live across a deopt point must be unboxed back onto the
        // stack so the resumed interpreter sees the real string, not a handle.
        // StringRepeat isn't lowered → deopt after the boxed Concat.
        let mut b = ChunkBuilder::new();
        let s = b.add_constant(Value::str("ab"));
        b.emit(Op::LoadConst(s), 1);
        let two = b.add_constant(Value::str("x"));
        b.emit(Op::LoadConst(two), 1);
        b.emit(Op::Concat, 1); // boxed "abx"
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::StringRepeat, 1); // not lowered → deopt; spills the Obj
        assert_native_matches_interp(b.build());
    }

    #[test]
    fn native_type_mismatch_deopts() {
        // A Bool (comparison result) fed into Add is a type mismatch for the
        // native path; it deopts and the interpreter float-promotes. The native
        // prefix still lowers, and the result matches.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::NumLt, 1); // Bool(true)
        b.emit(Op::LoadInt(5), 1);
        b.emit(Op::Add, 1); // Bool + Int → deopt → Float
        let chunk = b.build();
        assert!(
            native_lowerable(&chunk),
            "type-mismatch chunk partial-lowers"
        );
        assert_native_matches_interp(chunk);

        // SetSlot of a Bool deopts; the slot is then read back by the interp.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(3), 1);
        b.emit(Op::LoadInt(3), 1);
        b.emit(Op::NumEq, 1); // Bool(true)
        b.emit(Op::SetSlot(0), 1); // deopt
        b.emit(Op::GetSlot(0), 1);
        assert_native_matches_interp(b.build());
    }

    #[test]
    fn native_heap_const_deopts_not_bails() {
        // A heap (string) constant load is now a deopt point: a native prefix
        // before it still lowers, instead of bailing the whole chunk.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(40), 1);
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::Add, 1); // 42, native
        let c = b.add_constant(Value::str("!"));
        b.emit(Op::LoadConst(c), 1); // heap constant → deopt
        b.emit(Op::Concat, 1); // "42" + "!" = "42!"
        let chunk = b.build();
        assert!(
            native_lowerable(&chunk),
            "native prefix + heap const should lower"
        );
        match run_chunk_native(&chunk, |_| {}).expect("run") {
            VMResult::Ok(v) => assert_eq!(v, Value::str("42!")),
            other => panic!("got {other:?}"),
        }
        assert_native_matches_interp(chunk);

        // A chunk that is ONLY a heap-const load now lowers as a boxed Obj
        // source returning that value — matching the interpreter.
        let mut b = ChunkBuilder::new();
        let c = b.add_constant(Value::str("x"));
        b.emit(Op::LoadConst(c), 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "heap LoadConst lowers as Obj");
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_div() {
        // Normal division → Float.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(7.0), 1);
        b.emit(Op::LoadFloat(2.0), 1);
        b.emit(Op::Div, 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "Div should lower");
        match run_chunk_native(&chunk, |_| {}).expect("run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Float(3.5)),
            other => panic!("got {other:?}"),
        }

        // Divide-by-zero deopts to the interpreter, which yields Undef — must
        // match the interpreter exactly.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(5.0), 1);
        b.emit(Op::LoadFloat(0.0), 1);
        b.emit(Op::Div, 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk));
        match run_chunk_native(&chunk, |_| {}).expect("run") {
            VMResult::Ok(Value::Undef) => {}
            other => panic!("expected Undef from div-by-zero, got {other:?}"),
        }
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_div_deopt_reconstructs_slot_state() {
        // The deopt must write register-cached slots back to the VM so the
        // resumed interpreter sees them. Here slot 0 = 99 is set natively, then a
        // divide-by-zero forces deopt; the interpreter divides (→ Undef), pops
        // it, and the leftover 99 (which it must read correctly) is the result.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(99), 1);
        b.emit(Op::SetSlot(0), 1); // slot cached in a register natively
        b.emit(Op::LoadFloat(5.0), 1);
        b.emit(Op::LoadFloat(0.0), 1);
        b.emit(Op::Div, 1); // deopt; interpreter pushes Undef
        b.emit(Op::Pop, 1); // discard the Undef
        b.emit(Op::GetSlot(0), 1); // interpreter reads slot 0 → must be 99
        let chunk = b.build();
        assert!(native_lowerable(&chunk));
        match run_chunk_native(&chunk, |_| {}).expect("run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Int(99), "slot survived deopt"),
            other => panic!("got {other:?}"),
        }
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_differential_div() {
        // Random divisions incl. zero divisors: nonzero → native fdiv, zero →
        // deopt to interpreter (Undef). Both must match the interpreter.
        let mut seed: u64 = 0x53C5_AA21_99BE_4471;
        let mut next = || {
            seed = seed.wrapping_add(0x9E37_79B9_7F4A_7C15);
            let mut z = seed;
            z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
            z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
            z ^ (z >> 31)
        };
        for _ in 0..300 {
            // small ints incl. 0 as divisor, so ~1/9 of cases deopt.
            let a = (next() % 21) as i64 - 10;
            let bdiv = (next() % 9) as i64 - 4;
            let mut b = ChunkBuilder::new();
            b.emit(Op::LoadInt(a), 1);
            b.emit(Op::LoadInt(bdiv), 1);
            b.emit(Op::Div, 1);
            let chunk = b.build();
            assert!(native_lowerable(&chunk));
            assert_native_matches_interp(chunk);
        }
    }

    #[test]
    fn native_integer_mod() {
        // Normal, plus the two guarded divisors (% 0 and % -1 both → 0).
        for (x, y, want) in [
            (7, 3, 1),
            (-7, 3, -1), // truncated remainder (sign of dividend)
            (7, -3, 1),
            (10, 0, 0),        // interpreter's y==0 → 0
            (i64::MIN, -1, 0), // x % -1 == 0, and dodges the srem trap
            (123_456, 1000, 456),
        ] {
            let mut b = ChunkBuilder::new();
            b.emit(Op::LoadInt(x), 1);
            b.emit(Op::LoadInt(y), 1);
            b.emit(Op::Mod, 1);
            let chunk = b.build();
            assert!(native_lowerable(&chunk), "int mod should lower");
            assert_native_int(chunk, want);
        }

        // A float operand takes the fmod libcall path: 7.5 % 2.0 = 1.5.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(7.5), 1);
        b.emit(Op::LoadFloat(2.0), 1);
        b.emit(Op::Mod, 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "float mod lowers via fmod");
        match run_chunk_native(&chunk, |_| {}).expect("native run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Float(1.5)),
            other => panic!("expected Ok(1.5), got {other:?}"),
        }
    }

    #[test]
    fn native_float_math_ops() {
        // Each shim calls the same Rust method the interpreter does, so finite
        // results match bit-for-bit. Inputs chosen to stay in-domain (no NaN,
        // which would fail `==`).
        let cases: &[(Op, f64)] = &[
            (Op::AbsFloat, -3.5),
            (Op::SqrtFloat, 2.0),
            (Op::CeilFloat, 2.3),
            (Op::FloorFloat, 2.7),
            (Op::TruncFloat, -2.7),
            (Op::RoundFloat, 2.5), // ties to even → 2.0
            (Op::SinFloat, 1.0),
            (Op::CosFloat, 1.0),
            (Op::TanFloat, 1.0),
            (Op::AsinFloat, 0.5),
            (Op::AcosFloat, 0.5),
            (Op::AtanFloat, 2.0),
            (Op::SinhFloat, 1.0),
            (Op::CoshFloat, 1.0),
            (Op::TanhFloat, 1.0),
            (Op::ExpFloat, 1.5),
            (Op::LogFloat, 2.0),
            (Op::Log2Float, 8.0),
            (Op::Log10Float, 1000.0),
        ];
        for (op, input) in cases {
            let mut b = ChunkBuilder::new();
            b.emit(Op::LoadFloat(*input), 1);
            b.emit(op.clone(), 1);
            let chunk = b.build();
            assert!(native_lowerable(&chunk), "{op:?} should lower");
            assert_native_matches_interp(chunk);
        }

        // RoundFloat ties-to-even concretely: 2.5 → 2.0, 3.5 → 4.0.
        for (input, want) in [(2.5_f64, 2.0_f64), (3.5, 4.0)] {
            let mut b = ChunkBuilder::new();
            b.emit(Op::LoadFloat(input), 1);
            b.emit(Op::RoundFloat, 1);
            match run_chunk_native(&b.build(), |_| {}).expect("run") {
                VMResult::Ok(v) => assert_eq!(v, Value::Float(want)),
                other => panic!("got {other:?}"),
            }
        }
    }

    #[test]
    fn native_awk_div_mod() {
        // Good cases: float division and modulo.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(7.0), 1);
        b.emit(Op::LoadFloat(2.0), 1);
        b.emit(Op::AwkDivJit, 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk));
        match run_chunk_native(&chunk, |_| {}).expect("run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Float(3.5)),
            other => panic!("got {other:?}"),
        }

        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(7.5), 1);
        b.emit(Op::LoadFloat(2.0), 1);
        b.emit(Op::AwkModJit, 1);
        match run_chunk_native(&b.build(), |_| {}).expect("run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Float(1.5)),
            other => panic!("got {other:?}"),
        }

        // Divide-by-zero takes the native error-return branch; the error message
        // must match the interpreter's exactly.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(5.0), 1);
        b.emit(Op::LoadFloat(0.0), 1);
        b.emit(Op::AwkDivJit, 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk));
        match run_chunk_native(&chunk, |_| {}).expect("run") {
            VMResult::Error(e) => assert_eq!(e, "division by zero attempted"),
            other => panic!("expected div-by-zero error, got {other:?}"),
        }
        assert_native_matches_interp(chunk);

        // Modulo by zero: the `%` variant's distinct message.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(5.0), 1);
        b.emit(Op::LoadFloat(0.0), 1);
        b.emit(Op::AwkModJit, 1);
        let chunk = b.build();
        match run_chunk_native(&chunk, |_| {}).expect("run") {
            VMResult::Error(e) => assert_eq!(e, "division by zero attempted in `%'"),
            other => panic!("expected mod-by-zero error, got {other:?}"),
        }
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_sink_print() {
        // A chunk with a heap/IO op (Print) now lowers natively: the arithmetic
        // runs in registers, Print is spilled to the boxed stack and shimmed.
        // We assert via a *leftover* value beneath the printed args, which proves
        // the sink popped exactly `n` (stdout itself isn't capturable here).

        // 99, (5+3=8), PrintLn(1) → prints "8", pops it, leaves 99.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(99), 1);
        b.emit(Op::LoadInt(5), 1);
        b.emit(Op::LoadInt(3), 1);
        b.emit(Op::Add, 1);
        b.emit(Op::PrintLn(1), 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "scalar+Print should now lower");
        assert_native_int(chunk, 99);

        // Print(2) pops two; leftover 7 remains.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(7), 1);
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::Print(2), 1);
        assert_native_int(b.build(), 7);

        // Float and bool args exercise the per-kind spill helpers.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(42), 1);
        b.emit(Op::LoadFloat(2.5), 1);
        b.emit(Op::PrintLn(1), 1);
        assert_native_int(b.build(), 42);

        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(42), 1);
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::NumLt, 1); // Bool
        b.emit(Op::PrintLn(1), 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk));
        assert_native_int(chunk, 42);

        // And the result still matches the interpreter (which prints identically).
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(10), 1);
        b.emit(Op::LoadInt(20), 1);
        b.emit(Op::Add, 1);
        b.emit(Op::PrintLn(1), 1);
        assert_native_matches_interp(b.build());
    }

    #[test]
    fn native_getstatus_source() {
        // Bare `$?` result is a Status; with no prior SetStatus the code is 0.
        let mut b = ChunkBuilder::new();
        b.emit(Op::GetStatus, 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "GetStatus should lower");
        match run_chunk_native(&chunk, |_| {}).expect("run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Status(0)),
            other => panic!("got {other:?}"),
        }

        // Set $? = 3, then return it → Status(3).
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(3), 1);
        b.emit(Op::SetStatus, 1);
        b.emit(Op::GetStatus, 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk));
        match run_chunk_native(&chunk, |_| {}).expect("run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Status(3)),
            other => panic!("got {other:?}"),
        }

        // Status float-promotes in arithmetic: $?(=0) + 2.5 → Float(2.5).
        let mut b = ChunkBuilder::new();
        b.emit(Op::GetStatus, 1);
        b.emit(Op::LoadFloat(2.5), 1);
        b.emit(Op::Add, 1);
        assert_native_matches_interp(b.build());
    }

    #[test]
    fn native_getstatus_inverted_truthiness() {
        // Status truthiness is INVERTED: code 0 is truthy (shell success).
        // `if $? { 10 } else { 20 }` with $?=0 (success) → takes the truthy arm.
        let build = |code: i64| {
            let mut b = ChunkBuilder::new();
            b.emit(Op::LoadInt(code), 1);
            b.emit(Op::SetStatus, 1);
            b.emit(Op::GetStatus, 1);
            let jf = b.emit(Op::JumpIfFalse(0), 1);
            b.emit(Op::LoadInt(10), 1); // taken when $? truthy (code == 0)
            let j = b.emit(Op::Jump(0), 1);
            let else_ip = b.emit(Op::LoadInt(20), 1);
            let end = b.current_pos();
            b.patch_jump(jf, else_ip);
            b.patch_jump(j, end);
            b.build()
        };
        // code 0 → truthy → 10; code 1 → falsy → 20. Match the interpreter.
        for code in [0, 1, 2, -1] {
            let chunk = build(code);
            assert!(native_lowerable(&chunk));
            assert_native_matches_interp(chunk);
        }
    }

    #[test]
    fn native_differential_getstatus() {
        // $? set to a random code, then used in arithmetic / comparison /
        // branch / as result — all must match the interpreter (esp. truthiness).
        let mut seed: u64 = 0xABCD_1234_5678_9F01;
        let mut next = || {
            seed = seed.wrapping_add(0x9E37_79B9_7F4A_7C15);
            let mut z = seed;
            z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
            z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
            z ^ (z >> 31)
        };
        for _ in 0..300 {
            let code = (next() % 5) as i64 - 2; // small incl. 0
            let mut b = ChunkBuilder::new();
            b.emit(Op::LoadInt(code), 1);
            b.emit(Op::SetStatus, 1);
            b.emit(Op::GetStatus, 1);
            match next() % 4 {
                0 => {
                    // arithmetic: $? + k
                    b.emit(Op::LoadInt((next() % 7) as i64 - 3), 1);
                    b.emit(Op::Add, 1);
                }
                1 => {
                    // comparison feeding a branch
                    b.emit(Op::LoadInt(0), 1);
                    b.emit(Op::NumGt, 1);
                    let jf = b.emit(Op::JumpIfFalse(0), 1);
                    b.emit(Op::LoadInt(1), 1);
                    let j = b.emit(Op::Jump(0), 1);
                    let e = b.emit(Op::LoadInt(2), 1);
                    let end = b.current_pos();
                    b.patch_jump(jf, e);
                    b.patch_jump(j, end);
                }
                2 => {
                    // direct truthiness branch (inverted)
                    let jf = b.emit(Op::JumpIfFalse(0), 1);
                    b.emit(Op::LoadInt(100), 1);
                    let j = b.emit(Op::Jump(0), 1);
                    let e = b.emit(Op::LoadInt(200), 1);
                    let end = b.current_pos();
                    b.patch_jump(jf, e);
                    b.patch_jump(j, end);
                }
                _ => { /* bare $? result */ }
            }
            let chunk = b.build();
            if native_lowerable(&chunk) {
                assert_native_matches_interp(chunk);
            }
        }
    }

    #[test]
    fn native_source_awk_field() {
        // AwkGetFieldNum is a source whose result is statically Float; it reloads
        // into a register with no guard. With no awk host the field hook yields
        // 0.0, so $1 + 2.0 == 2.0 — and it must match the interpreter (same hook).
        let mut b = ChunkBuilder::new();
        b.emit(Op::AwkGetFieldNum(1), 1);
        b.emit(Op::LoadFloat(2.0), 1);
        b.emit(Op::Add, 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "awk field source should lower");
        match run_chunk_native(&chunk, |_| {}).expect("run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Float(2.0)),
            other => panic!("got {other:?}"),
        }
        assert_native_matches_interp(chunk);

        // The reloaded value feeds further native arithmetic and a comparison.
        let mut b = ChunkBuilder::new();
        b.emit(Op::AwkGetFieldNum(2), 1);
        b.emit(Op::AwkGetFieldNum(3), 1);
        b.emit(Op::NumLt, 1); // 0.0 < 0.0 → false
        b.emit(Op::LoadInt(5), 1);
        b.emit(Op::Add, 1); // false(0) + 5 = 5  (bool promotes via... )
        let chunk = b.build();
        // NumLt result is Bool; Bool + Int isn't lowered (Add needs numeric), so
        // this particular chunk falls back — still must match the interpreter.
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_setstatus_sink() {
        // SetStatus is a silent sink (no stdout): pops one, leaves 77.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(77), 1);
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::SetStatus, 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "SetStatus should lower as a sink");
        assert_native_int(chunk, 77);
        // Result also matches the interpreter (both set $? and leave 77).
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(77), 1);
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::SetStatus, 1);
        assert_native_matches_interp(b.build());
    }

    #[test]
    fn native_loop_with_print_lowers() {
        // sum=0; for i in 0..3 { print(i); sum += i }; return sum → 3, fully
        // native incl. the per-iteration sink. (Prints 0,1,2 to stdout.)
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::SetSlot(0), 1); // sum
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::SetSlot(1), 1); // i
        let top = b.current_pos();
        b.emit(Op::GetSlot(1), 1);
        b.emit(Op::LoadInt(3), 1);
        b.emit(Op::NumLt, 1);
        let exit = b.emit(Op::JumpIfFalse(0), 1);
        b.emit(Op::GetSlot(1), 1);
        b.emit(Op::Print(1), 1); // sink inside the hot loop
        b.emit(Op::GetSlot(0), 1);
        b.emit(Op::GetSlot(1), 1);
        b.emit(Op::Add, 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::GetSlot(1), 1);
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::Add, 1);
        b.emit(Op::SetSlot(1), 1);
        b.emit(Op::Jump(top), 1);
        let end = b.current_pos();
        b.patch_jump(exit, end);
        b.emit(Op::GetSlot(0), 1);
        let chunk = b.build();
        assert!(
            native_lowerable(&chunk),
            "loop with embedded sink should lower"
        );
        assert_native_int(chunk, 3); // 0+1+2
    }

    #[test]
    fn native_awk_sqrt_log() {
        // sqrt(16) = 4.0 exactly.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(16.0), 1);
        b.emit(Op::AwkSqrtJit, 1);
        assert_native_int_or_float(b.build(), 4.0);

        // Non-negative inputs match the interpreter (finite results / -inf).
        for x in [2.0_f64, 0.5, 100.0, 1.0, 0.0] {
            for op in [Op::AwkSqrtJit, Op::AwkLogJit] {
                let mut b = ChunkBuilder::new();
                b.emit(Op::LoadFloat(x), 1);
                b.emit(op, 1);
                let chunk = b.build();
                assert!(native_lowerable(&chunk));
                assert_native_matches_interp(chunk);
            }
        }

        // Negative input warns (to stderr) and yields NaN; verify the NaN result
        // (the `==`-based diff harness can't compare NaN).
        for op in [Op::AwkSqrtJit, Op::AwkLogJit] {
            let mut b = ChunkBuilder::new();
            b.emit(Op::LoadFloat(-4.0), 1);
            b.emit(op, 1);
            match run_chunk_native(&b.build(), |_| {}).expect("run") {
                VMResult::Ok(Value::Float(v)) => assert!(v.is_nan(), "expected NaN"),
                other => panic!("expected NaN float, got {other:?}"),
            }
        }
    }

    #[test]
    fn native_awk_div_mod_nonjit() {
        // The non-Jit AwkDiv/AwkMod lower identically to the Jit variants.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(7.0), 1);
        b.emit(Op::LoadFloat(2.0), 1);
        b.emit(Op::AwkDiv, 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk));
        match run_chunk_native(&chunk, |_| {}).expect("run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Float(3.5)),
            other => panic!("got {other:?}"),
        }

        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(7.5), 1);
        b.emit(Op::LoadFloat(2.0), 1);
        b.emit(Op::AwkMod, 1);
        match run_chunk_native(&b.build(), |_| {}).expect("run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Float(1.5)),
            other => panic!("got {other:?}"),
        }

        // Divide-by-zero error-returns with the exact interpreter message.
        for (op, msg) in [
            (Op::AwkDiv, "division by zero attempted"),
            (Op::AwkMod, "division by zero attempted in `%'"),
        ] {
            let mut b = ChunkBuilder::new();
            b.emit(Op::LoadFloat(5.0), 1);
            b.emit(Op::LoadFloat(0.0), 1);
            b.emit(op, 1);
            let chunk = b.build();
            assert!(native_lowerable(&chunk));
            match run_chunk_native(&chunk, |_| {}).expect("run") {
                VMResult::Error(e) => assert_eq!(e, msg),
                other => panic!("expected error, got {other:?}"),
            }
            assert_native_matches_interp(chunk);
        }
    }

    #[test]
    fn native_awk_bitwise() {
        // lshift(3, 4) = 48; rshift(48, 4) = 3; compl(0) = -1.
        let bin = |a: f64, n: f64, op: Op| {
            let mut b = ChunkBuilder::new();
            b.emit(Op::LoadFloat(a), 1);
            b.emit(Op::LoadFloat(n), 1);
            b.emit(op, 1);
            b.build()
        };
        assert_native_int_or_float(bin(3.0, 4.0, Op::AwkLshiftJit), 48.0);
        assert_native_int_or_float(bin(48.0, 4.0, Op::AwkRshiftJit), 3.0);

        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(0.0), 1);
        b.emit(Op::AwkComplJit, 1);
        assert_native_int_or_float(b.build(), -1.0); // !0 == -1

        // Negative operands error with the exact interpreter message.
        for (op, msg) in [
            (Op::AwkLshiftJit, "lshift: negative values are not allowed"),
            (Op::AwkRshiftJit, "rshift: negative values are not allowed"),
        ] {
            let mut b = ChunkBuilder::new();
            b.emit(Op::LoadFloat(-1.0), 1);
            b.emit(Op::LoadFloat(2.0), 1);
            b.emit(op, 1);
            let chunk = b.build();
            match run_chunk_native(&chunk, |_| {}).expect("run") {
                VMResult::Error(e) => assert_eq!(e, msg),
                other => panic!("expected error, got {other:?}"),
            }
            assert_native_matches_interp(chunk);
        }
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(-1.0), 1);
        b.emit(Op::AwkComplJit, 1);
        let chunk = b.build();
        match run_chunk_native(&chunk, |_| {}).expect("run") {
            VMResult::Error(e) => assert_eq!(e, "compl: negative value is not allowed"),
            other => panic!("expected error, got {other:?}"),
        }
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_differential_awk_bitwise() {
        // Non-negative float operands (so no error): the int↔float conversion
        // and shift semantics must match the interpreter exactly.
        let mut seed: u64 = 0x84A3_11C5_7E29_B6F1;
        let mut next = || {
            seed = seed.wrapping_add(0x9E37_79B9_7F4A_7C15);
            let mut z = seed;
            z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
            z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
            z ^ (z >> 31)
        };
        for _ in 0..300 {
            let a = (next() % 1_000_000) as f64;
            let n = (next() % 70) as f64; // spans the 0x3f mask boundary
            let mut b = ChunkBuilder::new();
            b.emit(Op::LoadFloat(a), 1);
            if next() & 1 == 0 {
                b.emit(Op::LoadFloat(n), 1);
                b.emit(
                    if next() & 2 == 0 {
                        Op::AwkLshiftJit
                    } else {
                        Op::AwkRshiftJit
                    },
                    1,
                );
            } else {
                b.emit(Op::AwkComplJit, 1);
            }
            let chunk = b.build();
            assert!(native_lowerable(&chunk));
            assert_native_matches_interp(chunk);
        }
    }

    #[test]
    fn native_gcd_lcm() {
        let gcd = |a: i64, bb: i64| {
            let mut b = ChunkBuilder::new();
            b.emit(Op::LoadInt(a), 1);
            b.emit(Op::LoadInt(bb), 1);
            b.emit(Op::GcdInt, 1);
            b.build()
        };
        assert_native_int(gcd(12, 18), 6);
        assert_native_int(gcd(17, 5), 1);
        assert_native_int(gcd(0, 5), 5);
        assert_native_int(gcd(-12, 18), 6); // unsigned_abs

        let lcm = |a: i64, bb: i64| {
            let mut b = ChunkBuilder::new();
            b.emit(Op::LoadInt(a), 1);
            b.emit(Op::LoadInt(bb), 1);
            b.emit(Op::LcmInt, 1);
            b.build()
        };
        assert_native_int(lcm(4, 6), 12);
        assert_native_int(lcm(21, 6), 42);
        assert_native_int(lcm(0, 5), 0);
        assert_native_int(lcm(i64::MAX, 2), i64::MAX); // capped at i64::MAX
    }

    #[test]
    fn native_differential_gcd_lcm() {
        // Full-range random pairs: exercises unsigned_abs of negatives,
        // i64::MIN, and lcm's saturating multiply — all must match the interp.
        let mut seed: u64 = 0xD1B5_4A32_D192_ED03;
        let mut next = || {
            seed = seed.wrapping_add(0x9E37_79B9_7F4A_7C15);
            let mut z = seed;
            z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
            z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
            z ^ (z >> 31)
        };
        for _ in 0..300 {
            let a = next() as i64;
            let bb = next() as i64;
            let op = if next() & 1 == 0 {
                Op::GcdInt
            } else {
                Op::LcmInt
            };
            let mut b = ChunkBuilder::new();
            b.emit(Op::LoadInt(a), 1);
            b.emit(Op::LoadInt(bb), 1);
            b.emit(op, 1);
            let chunk = b.build();
            assert!(native_lowerable(&chunk));
            assert_native_matches_interp(chunk);
        }
    }

    /// `Op::MulModFloor` must agree with the interpreter across the whole i64 range,
    /// which is the point of the op: the products here overflow `i64` in almost
    /// every iteration, and both paths have to reduce the EXACT product (i128 in
    /// native code, i128 in the interpreter) rather than the wrapped one.
    #[test]
    fn native_differential_mulmod() {
        let mut seed: u64 = 0x2545_F491_4F6C_DD1D;
        let mut next = || {
            seed = seed.wrapping_add(0x9E37_79B9_7F4A_7C15);
            let mut z = seed;
            z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
            z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
            z ^ (z >> 31)
        };
        // Divisors worth pinning: zero (interpreter yields 0), ±1 (always 0,
        // and the `srem(i64::MIN, -1)` trap this op sidesteps), and randoms.
        let fixed = [0i64, 1, -1, 2, -2, 7, -7, 1_000_000_007, i64::MIN, i64::MAX];
        for i in 0..300 {
            let a = next() as i64;
            let bb = next() as i64;
            let k = if i < fixed.len() {
                fixed[i]
            } else {
                next() as i64
            };
            let mut b = ChunkBuilder::new();
            b.emit(Op::LoadInt(a), 1);
            b.emit(Op::LoadInt(bb), 1);
            b.emit(Op::LoadInt(k), 1);
            b.emit(Op::MulModFloor, 1);
            let chunk = b.build();
            assert!(native_lowerable(&chunk), "mulmod_floor must lower natively");
            assert_native_matches_interp(chunk);
        }
    }

    /// The fusion must equal the unfused `Mul; Mod` pair whenever the product
    /// stays in `i64` — where the two are required to agree exactly — and must
    /// give the mathematically exact answer where it doesn't.
    #[test]
    fn mulmod_matches_unfused_and_is_exact() {
        let mut fused = ChunkBuilder::new();
        fused.emit(Op::LoadInt(123_456), 1);
        fused.emit(Op::LoadInt(789), 1);
        fused.emit(Op::LoadInt(1000), 1);
        fused.emit(Op::MulModFloor, 1);
        assert_native_int(
            fused.build(),
            crate::floor_rem_i128(123_456i128 * 789, 1000),
        );

        // 4e9^2 = 1.6e19 > i64::MAX (9.22e18): the unfused form wraps, the fused
        // one reduces the exact product.
        let a: i64 = 4_000_000_000;
        let mut big = ChunkBuilder::new();
        big.emit(Op::LoadInt(a), 1);
        big.emit(Op::LoadInt(a), 1);
        big.emit(Op::LoadInt(1_000_000_007), 1);
        big.emit(Op::MulModFloor, 1);
        let expect = crate::floor_rem_i128(a as i128 * a as i128, 1_000_000_007);
        assert_native_int(big.build(), expect);
        assert_ne!(
            expect,
            a.wrapping_mul(a) % 1_000_000_007,
            "test is vacuous unless the wrapped product differs"
        );
    }

    /// The mul-add fusion must equal the exact math on the whole i64 range —
    /// the LCG shape `(seed * A + C) % M`, whose product overflows every step.
    #[test]
    fn native_differential_muladdmod() {
        let mut seed: u64 = 0x9E37_79B9_7F4A_7C15;
        let mut next = || {
            seed = seed.wrapping_add(0xD1B5_4A32_D192_ED03);
            let mut z = seed;
            z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
            z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
            z ^ (z >> 31)
        };
        let fixed = [0i64, 1, -1, 2, -2, 1_000_000_007, i64::MIN, i64::MAX];
        for i in 0..300 {
            let a = next() as i64;
            let bb = next() as i64;
            let c = next() as i64;
            let k = if i < fixed.len() {
                fixed[i]
            } else {
                next() as i64
            };
            let mut b = ChunkBuilder::new();
            b.emit(Op::LoadInt(a), 1);
            b.emit(Op::LoadInt(bb), 1);
            b.emit(Op::LoadInt(c), 1);
            b.emit(Op::LoadInt(k), 1);
            b.emit(Op::MulAddModFloor, 1);
            let chunk = b.build();
            assert!(
                native_lowerable(&chunk),
                "muladdmod_floor must lower natively"
            );
            assert_native_matches_interp(chunk);
        }
    }

    #[test]
    fn native_int_math_ops() {
        // TruncInt of a float truncates toward zero; AbsInt is wrapping abs.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(3.9), 1);
        b.emit(Op::TruncInt, 1);
        assert_native_int(b.build(), 3);

        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(-3.9), 1);
        b.emit(Op::TruncInt, 1);
        assert_native_int(b.build(), -3);

        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(-7), 1);
        b.emit(Op::AbsInt, 1);
        assert_native_int(b.build(), 7);

        // wrapping: abs(i64::MIN) == i64::MIN
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(i64::MIN), 1);
        b.emit(Op::AbsInt, 1);
        assert_native_int(b.build(), i64::MIN);
    }

    #[test]
    fn native_binary_float_math() {
        // PowFloat: 2 ** 10 = 1024.0.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(2.0), 1);
        b.emit(Op::LoadFloat(10.0), 1);
        b.emit(Op::PowFloat, 1);
        match run_chunk_native(&b.build(), |_| {}).expect("run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Float(1024.0)),
            other => panic!("got {other:?}"),
        }

        // Atan2Float: operand order y (lower), x (top) → y.atan2(x).
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(2.0), 1); // y
        b.emit(Op::LoadFloat(3.0), 1); // x
        b.emit(Op::Atan2Float, 1);
        assert_native_matches_interp(b.build());
    }

    #[test]
    fn native_pow() {
        // 2 ** 10 = 1024.0 (always Float, via powf).
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::LoadInt(10), 1);
        b.emit(Op::Pow, 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "pow lowers via powf");
        match run_chunk_native(&chunk, |_| {}).expect("native run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Float(1024.0)),
            other => panic!("expected Ok(1024.0), got {other:?}"),
        }

        // Mixed/float base: 9.0 ** 0.5 = 3.0; matches the interpreter exactly.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(9.0), 1);
        b.emit(Op::LoadFloat(0.5), 1);
        b.emit(Op::Pow, 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk));
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_globals_round_trip() {
        // SetVar/GetVar: x = 42; return x → 42.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(42), 1);
        b.emit(Op::SetVar(0), 1);
        b.emit(Op::GetVar(0), 1);
        assert_native_int(b.build(), 42);

        // DeclareVar behaves like SetVar.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(7), 1);
        b.emit(Op::DeclareVar(0), 1);
        b.emit(Op::GetVar(0), 1);
        assert_native_int(b.build(), 7);

        // Two globals in arithmetic: 10 + 5 = 15.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(10), 1);
        b.emit(Op::SetVar(0), 1);
        b.emit(Op::LoadInt(5), 1);
        b.emit(Op::SetVar(1), 1);
        b.emit(Op::GetVar(0), 1);
        b.emit(Op::GetVar(1), 1);
        b.emit(Op::Add, 1);
        assert_native_int(b.build(), 15);
    }

    #[test]
    fn native_uninit_global_read_falls_back() {
        // Reading an unassigned global yields `Undef` in the interpreter, so it
        // must fall back; the threaded path still matches.
        let mut b = ChunkBuilder::new();
        b.emit(Op::GetVar(0), 1);
        let chunk = b.build();
        assert!(
            !native_lowerable(&chunk),
            "unset global read must fall back"
        );
        assert_native_matches_interp(chunk);
    }

    // A sentinel numeric hook: every op it's asked about returns Int(-42). In a
    // native run it is consulted ONLY if native code deopts to the interpreter,
    // so a -42 result is proof the deopt fired (and a non--42 result is proof it
    // stayed native).
    fn sentinel_hook(vm: &mut VM) {
        vm.set_numeric_hook(std::sync::Arc::new(|_op, _a, _b| Ok(Value::Int(-42))));
    }

    #[test]
    fn native_int_overflow_deopts_to_hook_when_flag_set() {
        // i64::MAX + 1 overflows. With the opt-in flag, native detects the
        // overflow and deopts to the interpreter, whose hook returns the -42
        // sentinel — i.e. the frontend's arbitrary-precision path gets to run.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(i64::MAX), 1);
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::Add, 1);
        b.set_int_overflow_deopt(true);
        let chunk = b.build();
        assert!(
            native_lowerable(&chunk),
            "int add must still lower natively"
        );
        match run_chunk_native(&chunk, sentinel_hook).expect("native run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Int(-42), "overflow must deopt to hook"),
            other => panic!("expected Ok(-42), got {other:?}"),
        }
        // Sub overflow: i64::MIN - 1.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(i64::MIN), 1);
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::Sub, 1);
        b.set_int_overflow_deopt(true);
        match run_chunk_native(&b.build(), sentinel_hook).expect("native run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Int(-42), "sub overflow must deopt"),
            other => panic!("expected Ok(-42), got {other:?}"),
        }
        // Mul overflow: i64::MAX * 2.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(i64::MAX), 1);
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::Mul, 1);
        b.set_int_overflow_deopt(true);
        match run_chunk_native(&b.build(), sentinel_hook).expect("native run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Int(-42), "mul overflow must deopt"),
            other => panic!("expected Ok(-42), got {other:?}"),
        }
    }

    #[test]
    fn native_int_no_overflow_stays_native_with_flag() {
        // 2 + 3 does not overflow: even with the flag on and a sentinel hook
        // installed, the common path stays native (no deopt) → 5, not -42.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::LoadInt(3), 1);
        b.emit(Op::Add, 1);
        b.set_int_overflow_deopt(true);
        let chunk = b.build();
        assert!(native_lowerable(&chunk));
        match run_chunk_native(&chunk, sentinel_hook).expect("native run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Int(5), "non-overflow must stay native"),
            other => panic!("expected Ok(5), got {other:?}"),
        }
    }

    #[test]
    fn native_int_overflow_wraps_without_flag() {
        // Default (flag off): unchanged behavior — native wraps at i64 and never
        // consults the hook, so awk/shell frontends are byte-identical. (This is
        // the very behavior the flag exists to override for Python.)
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(i64::MAX), 1);
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::Add, 1);
        let chunk = b.build();
        assert!(!chunk.int_overflow_deopt);
        assert!(native_lowerable(&chunk));
        match run_chunk_native(&chunk, sentinel_hook).expect("native run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Int(i64::MIN), "flag off must wrap, no hook"),
            other => panic!("expected Ok(i64::MIN), got {other:?}"),
        }
    }

    #[test]
    fn native_slot_incdec_superops() {
        // PreIncSlot: 5 → slot=6, push 6.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(5), 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::PreIncSlot(0), 1);
        assert_native_int(b.build(), 6);

        // PostIncSlot: push old 5; slot becomes 6 (observed via GetSlot).
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(5), 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::PostIncSlot(0), 1);
        assert_native_int(b.build(), 5);
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(5), 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::PostIncSlot(0), 1);
        b.emit(Op::Pop, 1);
        b.emit(Op::GetSlot(0), 1);
        assert_native_int(b.build(), 6);

        // PreDecSlot: 5 → 4. PostDecSlot: push old 5.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(5), 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::PreDecSlot(0), 1);
        assert_native_int(b.build(), 4);
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(5), 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::PostDecSlot(0), 1);
        assert_native_int(b.build(), 5);

        // PreIncSlotVoid on an *unassigned* slot: coerces to 0, becomes 1; the
        // op also marks the slot assigned so the later GetSlot lowers.
        let mut b = ChunkBuilder::new();
        b.emit(Op::PreIncSlotVoid(0), 1);
        b.emit(Op::GetSlot(0), 1);
        assert_native_int(b.build(), 1);

        // AddAssignSlotVoid: slot0(10) += slot1(7) → 17.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(10), 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::LoadInt(7), 1);
        b.emit(Op::SetSlot(1), 1);
        b.emit(Op::AddAssignSlotVoid(0, 1), 1);
        b.emit(Op::GetSlot(0), 1);
        assert_native_int(b.build(), 17);
    }

    #[test]
    fn native_accum_sum_loop() {
        // AccumSumLoop runs `while i < 5 { sum += i; i += 1 }` internally:
        // sum = 0+1+2+3+4 = 10, and i ends at 5.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::SetSlot(0), 1); // sum
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::SetSlot(1), 1); // i
        b.emit(Op::AccumSumLoop(0, 1, 5), 1);
        b.emit(Op::GetSlot(0), 1);
        assert_native_int(b.build(), 10);

        // The counter slot is left at the limit.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::SetSlot(1), 1);
        b.emit(Op::AccumSumLoop(0, 1, 5), 1);
        b.emit(Op::GetSlot(1), 1);
        assert_native_int(b.build(), 5);
    }

    #[test]
    fn native_fused_loop_ops() {
        // sum=0; i=0; if i<5 { do { sum+=i } while (++i < 5) }; return sum  → 10
        // built with the fused loop super-ops. Targets are computed by hand
        // since `patch_jump` only handles the plain jump ops.
        //  0 LoadInt 0           6 GetSlot 1
        //  1 SetSlot 0 (sum)     7 Add
        //  2 LoadInt 0           8 SetSlot 0
        //  3 SetSlot 1 (i)       9 SlotIncLtIntJumpBack(1,5,5)
        //  4 SlotLtIntJumpIfFalse(1,5,10)   10 GetSlot 0
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::SetSlot(1), 1);
        b.emit(Op::SlotLtIntJumpIfFalse(1, 5, 10), 1);
        b.emit(Op::GetSlot(0), 1);
        b.emit(Op::GetSlot(1), 1);
        b.emit(Op::Add, 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::SlotIncLtIntJumpBack(1, 5, 5), 1);
        b.emit(Op::GetSlot(0), 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "fused loop should lower natively");
        assert_native_int(chunk, 10);
    }

    #[test]
    fn native_stack_shuffles() {
        // Swap: [3,7] → [7,3]; 7 - 3 = 4.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(3), 1);
        b.emit(Op::LoadInt(7), 1);
        b.emit(Op::Swap, 1);
        b.emit(Op::Sub, 1);
        assert_native_int(b.build(), 4);

        // Dup2: [2,5] → [2,5,2,5]; +,+,+ = 14.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::LoadInt(5), 1);
        b.emit(Op::Dup2, 1);
        b.emit(Op::Add, 1);
        b.emit(Op::Add, 1);
        b.emit(Op::Add, 1);
        assert_native_int(b.build(), 14);

        // Rot: [1,2,3] → [2,3,1]; 3-1=2, 2-2=0.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::LoadInt(3), 1);
        b.emit(Op::Rot, 1);
        b.emit(Op::Sub, 1);
        b.emit(Op::Sub, 1);
        assert_native_int(b.build(), 0);
    }

    #[test]
    fn native_logical_and_spaceship() {
        // LogAnd(1,2) = true; Inc → 2 (Bool is int-like for Inc).
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::LogAnd, 1);
        b.emit(Op::Inc, 1);
        assert_native_int(b.build(), 2);

        // LogOr(0,0) = false; Inc → 1.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::LogOr, 1);
        b.emit(Op::Inc, 1);
        assert_native_int(b.build(), 1);

        // Spaceship: 3 <=> 7 = -1.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(3), 1);
        b.emit(Op::LoadInt(7), 1);
        b.emit(Op::Spaceship, 1);
        assert_native_int(b.build(), -1);

        // Float spaceship: 5.0 <=> 5.0 = 0; mixed int/float promotes.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(5), 1);
        b.emit(Op::LoadFloat(5.0), 1);
        b.emit(Op::Spaceship, 1);
        assert_native_int(b.build(), 0);
    }

    #[test]
    fn native_keep_jumps() {
        // JumpIfTrueKeep: 5 is truthy ⇒ jump keeping 5 ⇒ result 5.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(5), 1);
        let jt = b.emit(Op::JumpIfTrueKeep(0), 1);
        b.emit(Op::Pop, 1); // not-taken path rebuilds a depth-1 Int stack
        b.emit(Op::LoadInt(20), 1);
        let l = b.current_pos();
        b.patch_jump(jt, l);
        assert_native_int(b.build(), 5);

        // JumpIfFalseKeep: 0 is falsy ⇒ jump keeping 0 ⇒ result 0.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(0), 1);
        let jf = b.emit(Op::JumpIfFalseKeep(0), 1);
        b.emit(Op::Pop, 1);
        b.emit(Op::LoadInt(7), 1);
        let l = b.current_pos();
        b.patch_jump(jf, l);
        assert_native_int(b.build(), 0);
    }

    #[test]
    fn native_float_arithmetic() {
        // 1.5 * 2.0 + 1.0  →  4.0, fully native.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(1.5), 1);
        b.emit(Op::LoadFloat(2.0), 1);
        b.emit(Op::Mul, 1);
        b.emit(Op::LoadFloat(1.0), 1);
        b.emit(Op::Add, 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk));
        match run_chunk_native(&chunk, |_| {}).expect("native run") {
            VMResult::Ok(v) => assert_eq!(v, Value::Float(4.0)),
            other => panic!("expected Ok(4.0), got {other:?}"),
        }
    }

    #[test]
    fn native_mixed_int_float_promotion() {
        // 3 + 0.5  →  Float(3.5): Int promotes to float, mirroring the interp.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(3), 1);
        b.emit(Op::LoadFloat(0.5), 1);
        b.emit(Op::Add, 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk));
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_float_comparison_branch() {
        // if (1.5 < 2.5) 3.0 else 4.0  →  3.0
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadFloat(1.5), 1);
        b.emit(Op::LoadFloat(2.5), 1);
        b.emit(Op::NumLt, 1);
        let jf = b.emit(Op::JumpIfFalse(0), 1);
        b.emit(Op::LoadFloat(3.0), 1);
        let j = b.emit(Op::Jump(0), 1);
        let else_ip = b.emit(Op::LoadFloat(4.0), 1);
        let end_ip = b.current_pos();
        b.patch_jump(jf, else_ip);
        b.patch_jump(j, end_ip);
        let chunk = b.build();
        assert!(native_lowerable(&chunk));
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_float_const_pool() {
        // LoadConst of a numeric constant lowers; the float result must match.
        let mut b = ChunkBuilder::new();
        let c = b.add_constant(Value::Float(2.25));
        b.emit(Op::LoadConst(c), 1);
        b.emit(Op::LoadFloat(4.0), 1);
        b.emit(Op::Mul, 1);
        let chunk = b.build();
        assert!(native_lowerable(&chunk), "numeric const should lower");
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn native_differential_floats() {
        // Deterministic differential fuzz over mixed int/float arithmetic and
        // float comparisons. Values stay small and use only + - *, so no NaN
        // is produced and native/interp f64 bits match exactly.
        let mut seed: u64 = 0x2545_F491_4F6C_DD1D;
        let mut next = || {
            seed = seed.wrapping_add(0x9E37_79B9_7F4A_7C15);
            let mut z = seed;
            z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
            z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
            z ^ (z >> 31)
        };

        // Push a numeric literal: roughly half int, half (quarter-valued) float.
        fn push_num(b: &mut ChunkBuilder, next: &mut impl FnMut() -> u64) {
            if next() % 2 == 0 {
                b.emit(Op::LoadInt((next() % 13) as i64 - 6), 1);
            } else {
                let q = (next() % 25) as f64 / 4.0 - 3.0; // multiples of 0.25
                b.emit(Op::LoadFloat(q), 1);
            }
        }
        fn push_binop(b: &mut ChunkBuilder, next: &mut impl FnMut() -> u64) {
            match next() % 3 {
                0 => b.emit(Op::Add, 1),
                1 => b.emit(Op::Sub, 1),
                _ => b.emit(Op::Mul, 1),
            };
        }
        fn emit_expr(b: &mut ChunkBuilder, next: &mut impl FnMut() -> u64, terms: usize) {
            push_num(b, next);
            for _ in 0..terms {
                push_num(b, next);
                push_binop(b, next);
            }
        }

        // A branch whose two arms leave different kinds (Int vs Float) is a
        // legitimate join mismatch the native path rejects (it can't merge an
        // i64 and an f64 into one stack slot); such chunks fall back. So the
        // property is "if it lowers, it matches" — we also count lowerings to
        // ensure the test is not vacuous.
        let mut lowered = 0;
        for _ in 0..400 {
            let mut b = ChunkBuilder::new();
            if next() % 2 == 0 {
                let terms = (next() % 4) as usize;
                emit_expr(&mut b, &mut next, terms);
            } else {
                // if (num <cmp> num) <expr> else <expr>
                push_num(&mut b, &mut next);
                push_num(&mut b, &mut next);
                match next() % 4 {
                    0 => b.emit(Op::NumLt, 1),
                    1 => b.emit(Op::NumGt, 1),
                    2 => b.emit(Op::NumLe, 1),
                    _ => b.emit(Op::NumGe, 1),
                };
                let jf = b.emit(Op::JumpIfFalse(0), 1);
                let then_terms = (next() % 3) as usize;
                emit_expr(&mut b, &mut next, then_terms);
                let j = b.emit(Op::Jump(0), 1);
                let else_ip = b.current_pos();
                b.patch_jump(jf, else_ip);
                let else_terms = (next() % 3) as usize;
                emit_expr(&mut b, &mut next, else_terms);
                let end_ip = b.current_pos();
                b.patch_jump(j, end_ip);
            }
            let chunk = b.build();
            if native_lowerable(&chunk) {
                lowered += 1;
                assert_native_matches_interp(chunk);
            }
        }
        assert!(
            lowered > 50,
            "expected many float chunks to lower, got {lowered}"
        );
    }

    #[test]
    fn native_empty_chunk() {
        let b = ChunkBuilder::new();
        assert_native_matches_interp(b.build());
    }

    #[test]
    fn native_const_pool() {
        let mut b = ChunkBuilder::new();
        let c = b.add_constant(Value::str("hello"));
        b.emit(Op::LoadConst(c), 1);
        assert_native_matches_interp(b.build());
    }

    #[test]
    fn emits_relocatable_object_with_exported_symbols() {
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(42), 1);
        let chunk = b.build();
        let obj = std::env::temp_dir().join(format!("fusevm_aot_t_{}.o", std::process::id()));
        compile_object(&chunk, &obj).expect("emit object");
        assert!(
            std::fs::metadata(&obj)
                .map(|m| m.len() > 0)
                .unwrap_or(false),
            "non-empty .o"
        );
        // The driver + embedded-chunk symbols must be exported in the real object.
        if let Ok(o) = std::process::Command::new("nm")
            .arg("-g")
            .arg(&obj)
            .output()
        {
            let syms = String::from_utf8_lossy(&o.stdout);
            assert!(syms.contains("fusevm_aot_entry"), "entry exported:\n{syms}");
            assert!(
                syms.contains("fusevm_aot_chunk_blob"),
                "chunk blob exported:\n{syms}"
            );
            assert!(
                syms.contains("fusevm_aot_chunk_len"),
                "chunk len exported:\n{syms}"
            );
        }
        let _ = std::fs::remove_file(&obj);
    }

    // ── `CallBuiltin` lowering + the plan-coverage gate ───────────────────
    //
    // Before these, `analyze_native` returned a plan for every shell-shaped
    // chunk that lowered exactly its first op and deopted at the first
    // `CallBuiltin`, and `build_named` preferred that plan over a full threaded
    // lowering. The compiled driver was two calls — `push_int` then `resume` —
    // for any script, however large.

    /// A builtin that sums its `argc` integer operands. Frontend-shaped: pops
    /// its own arguments and returns one value.
    fn sum_builtin(vm: &mut VM, argc: u8) -> Value {
        let mut total = 0i64;
        for _ in 0..argc {
            total += vm.pop().to_int();
        }
        Value::int(total)
    }

    /// A builtin that returns a string, so its result is a genuinely boxed value
    /// (not a scalar that happens to fit a register).
    fn tag_builtin(vm: &mut VM, argc: u8) -> Value {
        let mut s = String::new();
        for _ in 0..argc {
            s.push_str(&vm.pop().to_str());
        }
        Value::str(s)
    }

    fn register_test_builtins(vm: &mut VM) {
        vm.register_builtin(0, sum_builtin);
        vm.register_builtin(1, tag_builtin);
    }

    #[test]
    fn native_callbuiltin_lowers_and_matches_interp() {
        // Operands staged from registers, handler run through the shim, result
        // threaded back as a boxed handle.
        let mut b = ChunkBuilder::new();
        b.set_builtin_argc_is_arity(true);
        b.emit(Op::LoadInt(7), 1);
        b.emit(Op::LoadInt(5), 1);
        b.emit(Op::CallBuiltin(0, 2), 1);
        let chunk = b.build();
        assert!(
            native_path_taken(&chunk),
            "a builtin call must take the native path, not deopt"
        );
        match run_chunk_native(&chunk, register_test_builtins).expect("run") {
            VMResult::Ok(v) => assert_eq!(v, Value::int(12)),
            other => panic!("got {other:?}"),
        }
        assert_native_matches_interp_with(chunk, register_test_builtins);
    }

    #[test]
    fn native_callbuiltin_result_feeds_later_ops() {
        // The boxed result must be consumable: as another builtin's operand, as
        // a branch condition, and as the chunk's result.
        let mut b = ChunkBuilder::new();
        b.set_builtin_argc_is_arity(true);
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::LoadInt(3), 1);
        b.emit(Op::CallBuiltin(0, 2), 1); // 5 (boxed)
        b.emit(Op::LoadInt(10), 1);
        b.emit(Op::CallBuiltin(0, 2), 1); // 15 — takes the boxed 5 back
        let chunk = b.build();
        assert!(native_path_taken(&chunk));
        match run_chunk_native(&chunk, register_test_builtins).expect("run") {
            VMResult::Ok(v) => assert_eq!(v, Value::int(15)),
            other => panic!("got {other:?}"),
        }
        assert_native_matches_interp_with(chunk, register_test_builtins);
    }

    #[test]
    fn native_callbuiltin_with_slots_falls_back() {
        // A handler gets `&mut VM` and both `slots` and `globals` are public, so
        // a chunk that also register-caches a variable must NOT lower natively —
        // the register could go stale behind the handler.
        let mut b = ChunkBuilder::new();
        b.set_builtin_argc_is_arity(true);
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::CallBuiltin(0, 1), 1);
        let chunk = b.build();
        assert!(
            !native_lowerable(&chunk),
            "builtin + register-cached slot must fall back to threaded"
        );
        assert_native_matches_interp_with(chunk, register_test_builtins);

        // Globals are the same hazard.
        let mut b = ChunkBuilder::new();
        b.set_builtin_argc_is_arity(true);
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::SetVar(0), 1);
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::CallBuiltin(0, 1), 1);
        let chunk = b.build();
        assert!(!native_lowerable(&chunk));
        assert_native_matches_interp_with(chunk, register_test_builtins);
    }

    #[test]
    fn native_obj_truthiness_asks_the_value_not_the_handle() {
        // A boxed operand's register holds an ARENA INDEX. Branching on that
        // index answers about the handle, not the value: an empty string is
        // falsy at every index, and index 0 would make any value falsy.
        // `tag_builtin` with no args returns "" — falsy.
        for (argc, expect) in [(0u8, 111i64), (1u8, 222)] {
            let mut b = ChunkBuilder::new();
            b.set_builtin_argc_is_arity(true);
            if argc == 1 {
                let c = b.add_constant(Value::str("x"));
                b.emit(Op::LoadConst(c), 1);
            }
            b.emit(Op::CallBuiltin(1, argc), 1); // "" (falsy) or "x" (truthy)
            let jf = b.emit(Op::JumpIfFalse(0), 1);
            b.emit(Op::LoadInt(222), 1); // truthy arm
            let end = b.emit(Op::Jump(0), 1);
            let target = b.current_pos();
            b.patch_jump(jf, target);
            b.emit(Op::LoadInt(111), 1); // falsy arm
            let after = b.current_pos();
            b.patch_jump(end, after);
            let chunk = b.build();
            assert!(native_path_taken(&chunk), "argc={argc}");
            match run_chunk_native(&chunk, register_test_builtins).expect("run") {
                VMResult::Ok(v) => assert_eq!(v, Value::int(expect), "argc={argc}"),
                other => panic!("got {other:?}"),
            }
            assert_native_matches_interp_with(chunk, register_test_builtins);
        }
    }

    #[test]
    fn short_native_prefix_prefers_threaded() {
        // The shell shape: one lowerable op, then an op the register model can't
        // hold, then a long body. The plan exists but covers 1 of N ops with no
        // loop — the threaded path lowers all N, so it must win.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::SortDefault, 1); // not lowerable → deopt point at ip 1
        for _ in 0..40 {
            b.emit(Op::LoadInt(2), 1);
            b.emit(Op::Pop, 1);
        }
        let chunk = b.build();
        let plan = analyze_native(&chunk).expect("a plan still exists");
        assert!(
            !plan.worth_lowering(&chunk),
            "a 1-op native prefix must not beat a full threaded lowering \
             (native_ops={} of {})",
            plan.native_ops,
            chunk.ops.len()
        );
    }

    #[test]
    fn native_loop_coverage_keeps_the_native_path() {
        // The converse: a hot numeric loop followed by an op that deopts still
        // belongs on the native path, however few ops it covers proportionally.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::SetSlot(0), 1);
        let top = b.current_pos();
        b.emit(Op::GetSlot(0), 1);
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::Add, 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::GetSlot(0), 1);
        b.emit(Op::LoadInt(10), 1);
        b.emit(Op::NumLt, 1);
        let back = b.emit(Op::JumpIfTrue(0), 1);
        b.patch_jump(back, top);
        b.emit(Op::GetSlot(0), 1);
        let chunk = b.build();
        let plan = analyze_native(&chunk).expect("plan");
        assert!(plan.native_loop, "the covered region contains a back-edge");
        assert!(plan.worth_lowering(&chunk));
        assert_native_matches_interp(chunk);
    }

    #[test]
    fn callbuiltin_without_the_arity_optin_stays_a_deopt() {
        // The default. zshrs emits `CallBuiltin(id, argc)` where `argc` is the
        // command's word count and the handler may only PEEK those words — so
        // `argc` is not a stack arity and the op cannot be lowered inline.
        // Without the opt-in it must stay a deopt point, and the result must
        // still match the interpreter.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(7), 1);
        b.emit(Op::LoadInt(5), 1);
        b.emit(Op::CallBuiltin(0, 2), 1);
        let chunk = b.build();
        assert!(!chunk.builtin_argc_is_arity, "opt-in is off by default");
        let plan = analyze_native(&chunk).expect("plan");
        assert!(
            plan.deopt_points.contains(&2),
            "an un-declared builtin call must be a deopt point"
        );
        assert_native_matches_interp_with(chunk, register_test_builtins);
    }

    // ── Both lowerings must agree ─────────────────────────────────────────
    //
    // `NativePlan::worth_lowering` is free to send a chunk down either path, so
    // the paths have to answer identically. Anything else turns a codegen
    // heuristic into a behaviour change.

    /// Run `chunk` through the native path, the threaded path, and the
    /// interpreter, and assert all three agree.
    fn assert_all_three_agree(label: &str, chunk: &Chunk, register: fn(&mut VM)) {
        let interp = {
            let mut vm = VM::new(chunk.clone());
            register(&mut vm);
            vm.run()
        };
        let native = run_chunk_native(chunk, register).expect("native");
        let threaded = run_chunk_threaded(chunk, register).expect("threaded");
        let same = |a: &VMResult, b: &VMResult| match (a, b) {
            (VMResult::Ok(x), VMResult::Ok(y)) => x == y,
            (VMResult::Halted, VMResult::Halted) => true,
            (VMResult::Error(x), VMResult::Error(y)) => x == y,
            _ => false,
        };
        assert!(
            same(&interp, &native),
            "{label}: interp {interp:?} != native {native:?}"
        );
        assert!(
            same(&interp, &threaded),
            "{label}: interp {interp:?} != threaded {threaded:?}"
        );
    }

    #[test]
    fn native_and_threaded_lowerings_agree() {
        // Scalar arithmetic — the fully-register-native shape.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(6), 1);
        b.emit(Op::LoadInt(7), 1);
        b.emit(Op::Mul, 1);
        assert_all_three_agree("mul", &b.build(), |_| {});

        // Float promotion.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(7), 1);
        b.emit(Op::LoadFloat(2.0), 1);
        b.emit(Op::Div, 1);
        assert_all_three_agree("div", &b.build(), |_| {});

        // Divide by zero — native `fdiv` guard deopts, threaded never lowered it.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::Div, 1);
        assert_all_three_agree("div0", &b.build(), |_| {});

        // Heap values: a string constant threaded through a boxed handle.
        let mut b = ChunkBuilder::new();
        let c = b.add_constant(Value::str("ab"));
        b.emit(Op::LoadConst(c), 1);
        b.emit(Op::LoadInt(3), 1);
        b.emit(Op::StringRepeat, 1);
        assert_all_three_agree("repeat", &b.build(), |_| {});

        // A loop with a slot accumulator.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::SetSlot(0), 1);
        let top = b.current_pos();
        b.emit(Op::GetSlot(0), 1);
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::Add, 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::GetSlot(0), 1);
        b.emit(Op::LoadInt(10), 1);
        b.emit(Op::NumLt, 1);
        let back = b.emit(Op::JumpIfTrue(0), 1);
        b.patch_jump(back, top);
        b.emit(Op::GetSlot(0), 1);
        assert_all_three_agree("loop", &b.build(), |_| {});

        // Builtin calls, both with and without the inline-lowering opt-in.
        for optin in [false, true] {
            let mut b = ChunkBuilder::new();
            b.set_builtin_argc_is_arity(optin);
            b.emit(Op::LoadInt(4), 1);
            b.emit(Op::LoadInt(9), 1);
            b.emit(Op::CallBuiltin(0, 2), 1);
            assert_all_three_agree("builtin", &b.build(), register_test_builtins);
        }
    }

    // ── The `CallBuiltin` cold paths ──────────────────────────────────────
    //
    // A builtin is the one shimmed op whose returned next-ip matters: the other
    // heap ops fall through unconditionally. Both non-fallthrough answers have
    // their own codegen block, and neither is reachable from an ordinary call.

    /// Halts the VM the way a shell `exit` does, and returns a code.
    fn halting_builtin(vm: &mut VM, _argc: u8) -> Value {
        vm.request_halt();
        Value::int(42)
    }

    /// Moves `vm.ip`, so `aot_exec_op` answers something other than `ip + 1`.
    /// `VM::run` reads `self.ip` after `exec_op` the same way, so the
    /// interpreter and the native path must land on the same instruction.
    fn jumping_builtin(vm: &mut VM, _argc: u8) -> Value {
        vm.ip = 4;
        Value::int(0)
    }

    #[test]
    fn native_callbuiltin_halt_ends_the_run() {
        // `request_halt` makes the shim answer -1. The native code must return
        // there instead of running on into the ops that follow.
        let mut b = ChunkBuilder::new();
        b.set_builtin_argc_is_arity(true);
        b.emit(Op::CallBuiltin(2, 0), 1); // halts
        b.emit(Op::Pop, 1);
        b.emit(Op::LoadInt(999), 1); // must NOT become the result
        let chunk = b.build();
        assert!(native_path_taken(&chunk), "the builtin call lowers inline");
        let reg: fn(&mut VM) = |vm| vm.register_builtin(2, halting_builtin);
        let native = run_chunk_native(&chunk, reg).expect("run");
        match &native {
            VMResult::Ok(v) => assert_ne!(*v, Value::int(999), "ran past the halt"),
            VMResult::Halted => {}
            other => panic!("got {other:?}"),
        }
        assert_all_three_agree("halt", &chunk, reg);
    }

    #[test]
    fn native_callbuiltin_moved_ip_resumes_the_interpreter() {
        // The handler jumps to ip 4, skipping the LoadInt(111) at ip 1..3.
        // The native code can't keep going in registers past an ip it didn't
        // plan for, so it spills and hands off — and must land on 222.
        let mut b = ChunkBuilder::new();
        b.set_builtin_argc_is_arity(true);
        b.emit(Op::CallBuiltin(3, 0), 1); // ip 0 — jumps to ip 4
        b.emit(Op::Pop, 1); // ip 1
        b.emit(Op::LoadInt(111), 1); // ip 2 — skipped
        b.emit(Op::Jump(6), 1); // ip 3
        b.emit(Op::Pop, 1); // ip 4 — drops the builtin's result
        b.emit(Op::LoadInt(222), 1); // ip 5
        let chunk = b.build();
        assert!(native_path_taken(&chunk));
        let reg: fn(&mut VM) = |vm| vm.register_builtin(3, jumping_builtin);
        match run_chunk_native(&chunk, reg).expect("run") {
            VMResult::Ok(v) => assert_eq!(v, Value::int(222)),
            other => panic!("got {other:?}"),
        }
        assert_all_three_agree("moved-ip", &chunk, reg);
    }

    // ── Boxed-handle bookkeeping ──────────────────────────────────────────

    #[test]
    fn native_obj_truthiness_keep_variants_peek_without_consuming() {
        // `JumpIf*Keep` leaves its condition on the stack for BOTH arms, so the
        // Obj case must peek the arena rather than take from it: consuming here
        // would free a handle that is still live, and the value that survives
        // the branch would read back as `Undef`.
        //
        // `tag_builtin` with one arg returns that arg as a string, so the
        // condition is a truthy boxed "x" that the chunk then keeps as its
        // result — proving the handle outlived the branch.
        let mut b = ChunkBuilder::new();
        b.set_builtin_argc_is_arity(true);
        let c = b.add_constant(Value::str("x"));
        b.emit(Op::LoadConst(c), 1);
        b.emit(Op::CallBuiltin(1, 1), 1); // boxed "x"
        let jt = b.emit(Op::JumpIfTrueKeep(0), 1);
        // Falsy arm: drop the kept value and answer a different string. Both
        // arms must reach the end with the same stack KIND (`Obj` here) or the
        // analysis refuses the chunk — a join it cannot type is a real bail,
        // not the behaviour under test.
        b.emit(Op::Pop, 1);
        let no = b.add_constant(Value::str("no"));
        b.emit(Op::LoadConst(no), 1);
        let end = b.emit(Op::Jump(0), 1);
        let target = b.current_pos();
        b.patch_jump(jt, target);
        let after = b.current_pos();
        b.patch_jump(end, after);
        let chunk = b.build();
        assert!(native_path_taken(&chunk));
        match run_chunk_native(&chunk, register_test_builtins).expect("run") {
            VMResult::Ok(v) => assert_eq!(v, Value::str("x"), "kept value was consumed"),
            other => panic!("got {other:?}"),
        }
        assert_all_three_agree("keep-obj", &chunk, register_test_builtins);
    }

    #[test]
    fn native_obj_logical_ops_use_the_value_not_the_handle() {
        // `LogNot`/`LogAnd`/`LogOr` admit any operand kind, so an `Obj` reaches
        // them too — and like the branches, they must ask the runtime for
        // truthiness. `tag_builtin` with 0 args returns "" (falsy) and with 1
        // arg returns a non-empty string (truthy); both live at nonzero arena
        // indices, so testing the register would answer "truthy" for each.
        for (argc, expect_not) in [(0u8, 1i64), (1u8, 0)] {
            let mut b = ChunkBuilder::new();
            b.set_builtin_argc_is_arity(true);
            if argc == 1 {
                let c = b.add_constant(Value::str("x"));
                b.emit(Op::LoadConst(c), 1);
            }
            b.emit(Op::CallBuiltin(1, argc), 1);
            b.emit(Op::LogNot, 1);
            b.emit(Op::TruncInt, 1); // Bool result would not box as the answer
            let chunk = b.build();
            assert!(native_path_taken(&chunk), "argc={argc}");
            match run_chunk_native(&chunk, register_test_builtins).expect("run") {
                VMResult::Ok(v) => assert_eq!(v, Value::int(expect_not), "argc={argc}"),
                other => panic!("got {other:?}"),
            }
            assert_all_three_agree("lognot-obj", &chunk, register_test_builtins);
        }

        // LogOr over two boxed operands: "" || "x" is true.
        let mut b = ChunkBuilder::new();
        b.set_builtin_argc_is_arity(true);
        b.emit(Op::CallBuiltin(1, 0), 1); // ""
        let c = b.add_constant(Value::str("x"));
        b.emit(Op::LoadConst(c), 1);
        b.emit(Op::CallBuiltin(1, 1), 1); // "x"
        b.emit(Op::LogOr, 1);
        b.emit(Op::TruncInt, 1);
        let chunk = b.build();
        match run_chunk_native(&chunk, register_test_builtins).expect("run") {
            VMResult::Ok(v) => assert_eq!(v, Value::int(1)),
            other => panic!("got {other:?}"),
        }
        assert_all_three_agree("logor-obj", &chunk, register_test_builtins);
    }

    #[test]
    fn native_callbuiltin_handles_are_recycled() {
        // Every builtin result is boxed into the arena. A loop that discards its
        // results must return those slots to the free list — otherwise a long-
        // running script grows the arena once per call, which is a leak in the
        // shape of correct output.
        let mut b = ChunkBuilder::new();
        b.set_builtin_argc_is_arity(true);
        b.emit(Op::LoadInt(0), 1);
        b.emit(Op::SetSlot(0), 1);
        let top = b.current_pos();
        b.emit(Op::CallBuiltin(1, 0), 1); // boxes a result
        b.emit(Op::Pop, 1); // and drops it
        b.emit(Op::GetSlot(0), 1);
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::Add, 1);
        b.emit(Op::SetSlot(0), 1);
        b.emit(Op::GetSlot(0), 1);
        b.emit(Op::LoadInt(500), 1);
        b.emit(Op::NumLt, 1);
        let back = b.emit(Op::JumpIfTrue(0), 1);
        b.patch_jump(back, top);
        b.emit(Op::GetSlot(0), 1);
        let chunk = b.build();
        // Slots + builtins can't be register-cached together, so this runs
        // threaded — which is the path a real shell loop takes, and the one
        // whose handle accounting matters most.
        let vm = run_chunk_native_vm(&chunk, register_test_builtins).expect("run");
        assert!(
            vm.aot_arena_len() <= 4,
            "arena grew to {} over 500 boxed-and-dropped results",
            vm.aot_arena_len()
        );
    }

    // ── The coverage gate's boundary ──────────────────────────────────────

    #[test]
    fn worth_lowering_boundary_is_half_the_chunk() {
        // Straight-line, no loop: the rule is "native only if it covers at least
        // half". Build a chunk whose native prefix is exactly at, and then just
        // under, that line — the two must land on opposite sides.
        let build = |prefix: usize, tail: usize| {
            let mut b = ChunkBuilder::new();
            for _ in 0..prefix {
                b.emit(Op::LoadInt(1), 1);
                b.emit(Op::Pop, 1);
            }
            b.emit(Op::SortDefault, 1); // not lowerable → deopt point
            for _ in 0..tail {
                b.emit(Op::LoadInt(2), 1);
                b.emit(Op::Pop, 1);
            }
            b.build()
        };

        // 20 covered ops of 41 total — just under half.
        let under = build(10, 10);
        let p = analyze_native(&under).expect("plan");
        assert_eq!(p.native_ops, 20);
        assert!(!p.native_loop);
        assert!(
            !p.worth_lowering(&under),
            "{} of {} is under half",
            p.native_ops,
            under.ops.len()
        );

        // 40 covered ops of 51 total — well over.
        let over = build(20, 5);
        let p = analyze_native(&over).expect("plan");
        assert_eq!(p.native_ops, 40);
        assert!(p.worth_lowering(&over));

        // Both still answer correctly whichever path they took.
        assert_all_three_agree("under-half", &under, |_| {});
        assert_all_three_agree("over-half", &over, |_| {});
    }


    #[test]
    fn lowering_for_reports_the_path_each_chunk_takes() {
        // The public read of the decision. A frontend uses this to check what
        // its compiler's output gets — which is the only way the "every script
        // lowered to two calls" regression was visible from outside fusevm.

        // Fully native, no interpreter at all.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::LoadInt(3), 1);
        b.emit(Op::Add, 1);
        let chunk = b.build();
        assert_eq!(
            lowering_for(&chunk),
            Lowering::Native {
                covered: 3,
                deopts: 0
            }
        );

        // Native with a deopt: covered ops still outnumber the tail.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(40), 1);
        b.emit(Op::LoadInt(2), 1);
        b.emit(Op::Add, 1);
        let c = b.add_constant(Value::str("!"));
        b.emit(Op::LoadConst(c), 1);
        b.emit(Op::Concat, 1);
        let chunk = b.build();
        match lowering_for(&chunk) {
            Lowering::Native { covered, .. } => assert!(covered > 1, "covered={covered}"),
            other => panic!("expected native, got {other:?}"),
        }

        // The shell shape: one lowerable op, then a long body behind a deopt.
        let mut b = ChunkBuilder::new();
        b.emit(Op::LoadInt(1), 1);
        b.emit(Op::SortDefault, 1);
        for _ in 0..40 {
            b.emit(Op::LoadInt(2), 1);
            b.emit(Op::Pop, 1);
        }
        assert_eq!(lowering_for(&b.build()), Lowering::Threaded);
    }
}