logicaffeine-compile 0.10.1

LOGOS compilation pipeline - codegen and interpreter
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
//! The bytecode dispatch loop.
//!
//! Registers live in one contiguous `Vec<Value>`; `base` is the current frame's
//! offset into it, so every register access is `registers[base + r]`. Calls use
//! register windowing — the callee's frame starts at the caller's `args_start`,
//! so arguments are passed with zero copying.

use super::instruction::{CompiledProgram, Constant, FuncIdx, Op, Reg};
use super::value::Value;
use super::MAX_REGISTER_FILE;
use logicaffeine_runtime::{ChanId, RtPayload, SelectArm, TaskId};

/// LEVER B callee analysis — may a region CALL this function while passing a
/// pinned list argument? Returns `(list_params_stable, returns_list_param)`,
/// both SOUND under-approximations:
/// - `list_params_stable`: the body has NO `ListPush` and NO sub-`Call` (either
///   could reallocate a list-param's buffer, staling the caller's derived raw
///   pointer). So every list-param buffer keeps its address across the call.
/// - `returns_list_param`: every `Return` traces — through `Move`s, to a
///   fixpoint — to a list PARAMETER slot, and all list params share one element
///   kind (the returned list kind is then unambiguous from the signature). A
///   purely scalar return makes this `false` and rides `ret` instead.
fn analyze_list_call_safety(
    body: &[Op],
    param_count: u16,
    param_kinds: &[Option<super::native_tier::ParamKind>],
    register_count: usize,
) -> (bool, bool) {
    use super::native_tier::ParamKind;
    let n = register_count.max(param_count as usize);
    // Slots that (transitively via Move) hold a list-parameter handle.
    let mut is_param_list = vec![false; n];
    for i in 0..param_count as usize {
        if matches!(param_kinds.get(i), Some(Some(ParamKind::List(_)))) {
            is_param_list[i] = true;
        }
    }
    loop {
        let mut changed = false;
        for op in body {
            if let Op::Move { dst, src } = *op {
                let (d, s) = (dst as usize, src as usize);
                if d < n && s < n && is_param_list[s] && !is_param_list[d] {
                    is_param_list[d] = true;
                    changed = true;
                }
            }
        }
        if !changed {
            break;
        }
    }
    let list_params_stable =
        !body.iter().any(|op| matches!(op, Op::ListPush { .. } | Op::Call { .. }));
    // All list params must share a single element kind so the return's list kind
    // is unambiguous from the signature alone.
    let mut elem: Option<super::native_tier::PinElem> = None;
    let mut uniform = true;
    for pk in param_kinds {
        if let Some(ParamKind::List(e)) = pk {
            if elem.is_some() && elem != Some(*e) {
                uniform = false;
            }
            elem = Some(*e);
        }
    }
    let returns: Vec<u16> = body
        .iter()
        .filter_map(|op| if let Op::Return { src } = *op { Some(src) } else { None })
        .collect();
    let returns_list_param = uniform
        && !returns.is_empty()
        && returns
            .iter()
            .all(|&s| (s as usize) < n && is_param_list[s as usize]);
    (list_params_stable, returns_list_param)
}

/// Whether `LOGOS_JIT_CANARY=1` armed the region-frame sentinel guard
/// (read once; the per-region path stays branch-cheap). Off by default and
/// in release, so normal runs pay nothing — it is a diagnostic for native
/// out-of-bounds writes.
fn jit_canary_enabled() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| std::env::var("LOGOS_JIT_CANARY").is_ok_and(|v| v == "1"))
}

/// How a native region run left the loop.
enum RegionExit {
    /// Fell out the loop's exit edge — resume at this pc.
    At(usize),
    /// Hit an in-region `Return` — perform the function return with this value.
    Return(Value),
}

/// What the native boundary decided for one call.
enum NativeDisposition {
    /// Completed natively; here is the re-boxed result.
    Done(Value),
    /// Run this call on bytecode (not compiled / guard mismatch / replay
    /// deopt).
    Interpret,
    /// Precise deopt: push these frames and resume at `resume_pc`.
    Materialize {
        resume_pc: usize,
        frames: Vec<super::native_tier::NativeFrame>,
        list_args: Vec<Value>,
    },
}

#[derive(Clone, Copy)]
struct CallFrame {
    return_pc: usize,
    return_reg: Reg,
    caller_base: usize,
    restore_len: usize,
    /// Iterator-stack depth at call entry; a Return unwinds any iterators the
    /// callee left open (e.g. `Return` inside a `Repeat`).
    iter_depth: usize,
    /// The function whose body this frame runs — selects the per-frame
    /// named-register map for loop regions tiering up inside it.
    func: u16,
    /// Absolute register index of this call's argument window (`callee_base`) and
    /// how many arguments it holds. On return these slots are nulled: as the
    /// callee's parameters they persist below `restore_len`, and a collection
    /// argument would otherwise leave a live `Rc` clone in the caller's frame,
    /// inflating `strong_count` and forcing needless copy-on-write later. Zero
    /// `arg_count` (native/scheduler frames) clears nothing.
    arg_lo: usize,
    arg_count: u16,
}

/// The outcome of one `run_until_block` slice (T11). A non-concurrent program
/// always returns `Done` on the first slice, so `run()` behaves exactly as the
/// old single-shot loop. A concurrent task returns `Blocked` at each scheduler
/// op; the driver reads [`Vm::take_pending`] and re-enters after the block clears.
pub(crate) enum VmStep {
    /// The (sub)program ran to completion (`Halt` or code exhausted) with the
    /// given result payload (the main/return value, `Nothing` if none).
    Done(crate::interpreter::RuntimeValue),
    /// Suspended at a concurrency op; [`Vm::take_pending`] carries the request.
    Blocked,
    /// Suspended by the debug stepper after exhausting its per-call op budget
    /// (`STEPPED = true` only). Resumable on the next [`Vm::run_steps`]. The
    /// production path (`run_until_block`, `STEPPED = false`) never yields this.
    Paused,
}

/// A read-only view of the paused VM for the Studio debug drawer: the program
/// counter, the live call frames (Main first, current last) with their register
/// values, the named globals, and the output so far. Values are rendered with the
/// same `to_display_string` the `Show` op uses.
pub(crate) struct DebugView {
    pub pc: usize,
    pub current_func: Option<u16>,
    pub frames: Vec<DebugFrameView>,
    pub globals: Vec<(String, String)>,
    pub output: Vec<String>,
}

/// One call frame's registers in a [`DebugView`]. `func` is `None` for Main; `base`
/// is the frame's start offset in the linear register file (its stack address).
pub(crate) struct DebugFrameView {
    pub func: Option<u16>,
    pub base: usize,
    /// `(index, type-name, display-value)` per register, e.g. `(1, "Int", "6")`.
    pub registers: Vec<(u16, String, String)>,
}

/// One heap-allocated object (list / map / set / tuple / text / struct) reachable
/// from the current frame or the globals — the heap-viewer's unit. `id` is the live
/// allocation address (so two roots sharing one object share an `id` → aliasing), and
/// `rc` is its reference count.
pub(crate) struct HeapObjView {
    pub id: usize,
    pub kind: String,
    pub summary: String,
    /// The underlying storage layout (e.g. `packed Vec<i64>`, `columnar`) — teaches
    /// how the data is actually laid out in memory.
    pub storage: String,
    pub rc: usize,
    pub referenced_by: Vec<String>,
}

/// The heap identity of a value — its allocation address, kind, reference count, and
/// storage-layout label. `None` for inline scalars (Int/Float/Bool/Char/…), which live
/// in the register slot itself and are not heap objects.
fn heap_identity(val: &Value) -> Option<(usize, String, usize, String)> {
    use crate::interpreter::RuntimeValue as RV;
    use std::rc::Rc;
    let s = |x: &str| x.to_string();
    match val.as_runtime_ref()? {
        RV::List(rc) => Some((Rc::as_ptr(rc) as usize, s("list"), Rc::strong_count(rc), rc.borrow().storage_label().to_string())),
        RV::Map(rc) => Some((Rc::as_ptr(rc) as usize, s("map"), Rc::strong_count(rc), s("hash map"))),
        RV::Set(rc) => Some((Rc::as_ptr(rc) as usize, s("set"), Rc::strong_count(rc), s("vec set"))),
        RV::Tuple(rc) => Some((Rc::as_ptr(rc) as usize, s("tuple"), Rc::strong_count(rc), s("fixed tuple"))),
        RV::Text(rc) => Some((Rc::as_ptr(rc) as usize, s("text"), Rc::strong_count(rc), s("Rc<String>"))),
        RV::Struct(b) => Some((&**b as *const _ as usize, s("struct"), 1, s("field map"))),
        RV::Inductive(b) => Some((&**b as *const _ as usize, s("enum"), 1, s("tagged variant"))),
        _ => None,
    }
}

/// The resumable execution state of a single-task program — enough to pause it and
/// resume in a freshly-built `tier: None` VM. The debugger owns the
/// [`CompiledProgram`] and rebuilds the VM each step (it cannot hold a borrowing
/// `Vm<'p>` across steps), threading this snapshot through. Concurrency request
/// state is intentionally omitted (the debugger is single-task, bytecode-tier).
#[derive(Clone)]
pub(crate) struct DebugVmState {
    registers: Vec<Value>,
    base: usize,
    globals: Vec<Option<Value>>,
    lines: Vec<String>,
    iter_stack: Vec<(Vec<Value>, usize)>,
    sched_active: bool,
    sched_pc: usize,
    sched_call_stack: Vec<CallFrame>,
}

impl DebugVmState {
    /// The pc the program is stopped at (the op about to execute).
    pub(crate) fn pc(&self) -> usize {
        self.sched_pc
    }
    /// Call-stack depth (0 = in Main), for step-over / step-out.
    pub(crate) fn call_depth(&self) -> usize {
        self.sched_call_stack.len()
    }
}

/// A concurrency request a suspended [`Vm`] hands to the scheduler driver — the
/// VM analog of the tree-walker's `BlockingRequest`. A spawned child travels as a
/// fully-built `Vm` (sharing the parent's `&'p program`), which the driver wraps
/// in its own task.
pub(crate) enum VmBlock {
    /// Create a channel (`None` = the scheduler's default capacity); resume with its id.
    NewChan(Option<usize>),
    /// Send a value into a channel (blocks if full).
    Send(ChanId, RtPayload),
    /// Receive from a channel (blocks if empty); resume with the value.
    Recv(ChanId),
    /// Non-blocking send; resume with `Bool(success)`.
    TrySend(ChanId, RtPayload),
    /// Non-blocking receive; resume with the value or `Nothing`.
    TryRecv(ChanId),
    /// Close a channel.
    Close(ChanId),
    /// Spawn a child *by descriptor* — function index + materialised args — so the
    /// driver builds the child `Vm` (the cooperative driver inline, a work-stealing
    /// worker locally over its own program). `want_handle` distinguishes a launch
    /// that binds a task handle. Resume with the child's `TaskId`.
    SpawnDesc { func: FuncIdx, args: Vec<RtPayload>, want_handle: bool },
    /// Await a task's completion; resume with its result payload.
    Await(TaskId),
    /// Abort a task.
    Abort(TaskId),
    /// Block on the first ready select arm; resume with the winning arm index.
    Select(Vec<SelectArm>),
    /// Sleep for some logical ticks.
    Sleep(u64),
    /// Dial the relay (async); resume when connected. Carries the URL value.
    NetConnect(RtPayload),
    /// Subscribe our inbox (async); resume when subscribed. Carries the topic value.
    NetListen(RtPayload),
    /// Encode + publish to a peer; resume immediately. Carries `(peer, message)`.
    NetSend(RtPayload, RtPayload),
    /// Batch-stream a list to a peer; resume immediately. Carries `(peer, list)`.
    NetStream(RtPayload, RtPayload),
    /// Await a message (or batch stream, if the flag) from a peer (blocks); resume with the value.
    /// Carries `(peer, stream_flag)`.
    NetAwait(RtPayload, bool),
    /// Resolve an address value into a PeerAgent handle (its canonical topic); resume with the peer.
    /// Carries the address value.
    NetMakePeer(RtPayload),
    /// CRDT sync point: publish the current counter, merge what has arrived, resume with the merged
    /// value. Carries `(topic, current)`.
    NetSync(RtPayload, RtPayload),
}

pub struct Vm<'p> {
    program: &'p CompiledProgram,
    /// The constant pool MATERIALISED into runtime values once at construction.
    /// A `LoadConst` then clones the pre-built `Value` — for a heap `Text` that
    /// is an `Rc` refcount bump, not a fresh `String`+`Rc` allocation, so a
    /// 1-char literal reloaded every iteration of a hot loop (string_search's
    /// `ch`) costs no heap traffic. The pool keeps a live reference, so a
    /// freshly-loaded literal is never the sole owner and the in-place
    /// `add_assign` append correctly declines to mutate it.
    const_pool: Vec<Value>,
    registers: Vec<Value>,
    base: usize,
    /// One element per `Show` (a shown value may itself contain newlines —
    /// it is still ONE output line, like the tree-walker's emit callback).
    lines: Vec<String>,
    /// Live `Repeat` snapshots: (elements, next index). Stack-disciplined —
    /// `IterPrepare` pushes, `IterPop` pops, nesting nests.
    iter_stack: Vec<(Vec<Value>, usize)>,
    /// Promoted globals (None = not yet defined; reading one is the
    /// "Undefined variable" error).
    globals: Vec<Option<Value>>,
    /// Policy registry + interner for `Check` statements (absent ⇒ the
    /// tree-walker's "Security Check requires policies" error).
    policy_ctx: Option<(&'p crate::analysis::PolicyRegistry, &'p crate::intern::Interner)>,
    /// The pluggable native tier (None = pure bytecode, e.g. WASM).
    tier: Option<&'p dyn super::native_tier::NativeTier>,
    /// Per-function call counts (profiling toward the tier threshold).
    hot: Vec<u32>,
    /// Per-function native state.
    native: Vec<super::native_tier::NativeSlot>,
    /// Back-edge counts for MAIN loops (keyed by loop-head pc). FxHash: this
    /// is probed once per back-edge crossing of every Main loop that has not
    /// (or cannot) tier up — a per-iteration cost on the bytecode path.
    region_hot: rustc_hash::FxHashMap<usize, u32>,
    /// Compiled Main-loop regions (keyed by loop-head pc; same probe rate).
    regions: rustc_hash::FxHashMap<usize, super::native_tier::RegionSlot>,
    /// Per-region (loop-head pc) collection registers this region mutates IN
    /// PLACE. Under value semantics these are copy-on-write'd at region ENTRY
    /// (`ensure_reg_owned`) so the native code's in-place writes cannot alias a
    /// shared allocation — the perf-preserving follow-up to the correctness-first
    /// decline. Only populated when the region is provably alias-free (a mutated
    /// collection never escapes it), so entry-COW alone isolates it soundly.
    region_cow_regs: rustc_hash::FxHashMap<usize, Vec<u16>>,
    /// Per-pc dead-region bitset: once a loop head is known `Failed`
    /// (un-tierable, or demoted after repeated guard misses) its entry here is
    /// set, so the back-edge hook short-circuits with a single `Vec<bool>`
    /// index instead of re-hashing `regions` on every iteration. Loops that
    /// never tier (effectful bodies, `Text` ops, list-param fns) are the common
    /// case and pay only this O(1) check after the first failure. Indexed by
    /// loop-head pc; sized to the code length.
    region_blacklist: Vec<bool>,
    /// Program arguments for the `args()` system native — full argv, index 0 is
    /// the program name (mirrors the compiled binary's `env::args()`). Empty
    /// when none were supplied.
    program_args: Vec<String>,
    /// Per-program native-tier context: the EXODIA 4.7 entry table plus the
    /// shared deopt-status and live-depth cells every chain patches.
    native_ctx: super::native_tier::NativeCtx,
    /// The off-thread native compiler (HOTSWAP §6), present only when the VM was
    /// given the process-installed `&'static` tier via [`Vm::with_bg_native_tier`].
    /// `None` ⇒ compile synchronously on this thread (the retained fallback, and the
    /// only path for a borrowed `&'p` tier). Native-only: needs `std::thread`+forge.
    #[cfg(not(target_arch = "wasm32"))]
    bg: Option<super::bg_compile::BgCompiler>,
    /// Axis-1 warm-bytecode side-table (HOTSWAP §7 / P11): re-optimized function
    /// bodies appended here, in the same pc space *after* `program.code`. A `Call`
    /// to a function with a `warm_entry` jumps into this buffer instead of the
    /// baseline `entry_pc`. Pure bytecode — no forge, no `rustc` — so it is the
    /// browser's hot-swap tier. Empty until a body is installed, and every read
    /// path is gated on `pc >= program.code.len()`, so the baseline run loop is
    /// byte-for-byte unchanged when nothing is warm.
    warm_code: Vec<Op>,
    /// Per-function warm entry (indexed by function index): the absolute pc of the
    /// body in the unified `program.code ++ warm_code` space, and its register
    /// window. `None` ⇒ the function runs its baseline body.
    warm_entry: Vec<Option<WarmEntry>>,

    /// Resumable-execution state for the scheduler driver (T11). When a task
    /// suspends at a concurrency op, `run_until_block` saves its `pc` + call stack
    /// here and restores them on the next slice. A non-concurrent run never sets
    /// `sched_active`, so it starts fresh at pc 0 — byte-for-byte the old loop.
    sched_active: bool,
    sched_pc: usize,
    sched_call_stack: Vec<CallFrame>,
    /// The concurrency request produced by the last `Blocked` slice (taken by the
    /// driver). `None` between slices and for a non-concurrent run.
    pending: Option<VmBlock>,
    /// The register the next resume value is delivered into (`None` for a block
    /// that yields nothing, e.g. `Send`/`Close`).
    resume_slot: Option<Reg>,
    /// Accumulated `Select` arms awaiting a `SelectWait`: each runtime arm plus
    /// the register a winning recv arm binds its value into. Persists across the
    /// block so `deliver_select` can route the received value to the right arm.
    select_pending: Vec<(SelectArm, Option<Reg>)>,
    /// WS6 (Phase 13): the browser WASM-JIT tier. Consulted from `Op::Call` only under the
    /// `wasm-jit` feature; entirely absent from the default build (and behind the native x86
    /// forge tier on native, so it is the JIT tier specifically where forge cannot run —
    /// wasm32).
    #[cfg(feature = "wasm-jit")]
    wasm_tier: super::wasm_jit::WasmTier,
}

/// A warm function body's location in the unified pc space (`program.code` then
/// `warm_code`) plus the register window it executes in.
#[derive(Clone, Copy, Debug)]
struct WarmEntry {
    entry_pc: usize,
    register_count: usize,
}

impl<'p> Vm<'p> {
    pub fn new(program: &'p CompiledProgram) -> Self {
        Vm {
            program,
            const_pool: program.constants.iter().map(const_to_value).collect(),
            registers: vec![Value::nothing(); program.register_count],
            base: 0,
            lines: Vec::new(),
            iter_stack: Vec::new(),
            globals: vec![None; program.globals.len()],
            policy_ctx: None,
            tier: None,
            hot: vec![0; program.functions.len()],
            native: (0..program.functions.len())
                .map(|_| super::native_tier::NativeSlot::Untried)
                .collect(),
            region_hot: rustc_hash::FxHashMap::default(),
            regions: rustc_hash::FxHashMap::default(),
            region_cow_regs: rustc_hash::FxHashMap::default(),
            region_blacklist: vec![false; program.code.len()],
            program_args: Vec::new(),
            native_ctx: super::native_tier::NativeCtx {
                table: std::sync::Arc::new(super::native_tier::FnTable::new(
                    program.functions.len(),
                )),
                status: std::sync::Arc::new(std::sync::atomic::AtomicI64::new(0)),
                depth: std::sync::Arc::new(std::sync::atomic::AtomicI64::new(0)),
            },
            #[cfg(not(target_arch = "wasm32"))]
            bg: None,
            warm_code: Vec::new(),
            warm_entry: vec![None; program.functions.len()],
            sched_active: false,
            sched_pc: 0,
            sched_call_stack: Vec::new(),
            pending: None,
            resume_slot: None,
            select_pending: Vec::new(),
            #[cfg(feature = "wasm-jit")]
            wasm_tier: super::wasm_jit::WasmTier::new(50),
        }
    }

    /// Install a re-optimized body as function `fi`'s warm tier (HOTSWAP §7 / P11):
    /// append it to `warm_code` after `program.code`, rebasing its 0-relative jumps
    /// into that unified pc space, and point `warm_entry[fi]` at it. Subsequent calls
    /// to `fi` run this body. The body shares the program's constant pool (a
    /// `FnBytecode` preserves constant indices), so only jumps are relocated.
    pub fn install_warm_bytecode(&mut self, fi: usize, fnbc: &super::fn_bytecode::FnBytecode) -> bool {
        // Refuse a structurally-invalid body (out-of-range jump/call, missing terminal
        // op) or one whose arity disagrees with the baseline function — a corrupt cache
        // entry or a buggy producer then falls back to baseline instead of fetching past
        // the warm buffer (panic) or reading the wrong registers (HOTSWAP §P12 robustness).
        if !fnbc.is_well_formed(self.program.functions.len()) {
            return false;
        }
        match self.program.functions.get(fi) {
            Some(f) if f.param_count == fnbc.param_count => {}
            _ => return false,
        }
        let abs_base = self.program.code.len() + self.warm_code.len();
        self.warm_code
            .extend(fnbc.code.iter().map(|&op| super::fn_bytecode::rebase(op, abs_base as isize)));
        if fi >= self.warm_entry.len() {
            self.warm_entry.resize(fi + 1, None);
        }
        self.warm_entry[fi] = Some(WarmEntry {
            entry_pc: abs_base,
            register_count: fnbc.register_count,
        });
        let name = self.fn_name(fi);
        super::tier_trace::trace_transition(fi, &name, super::tier_trace::ExecTier::Warm);
        true
    }

    /// Mark a loop head permanently `Failed` and record it in the per-pc
    /// blacklist so the back-edge hook never probes `regions` for it again.
    /// Every `RegionSlot::Failed` transition routes through here so the bitset
    /// can never drift out of sync with the map.
    fn mark_region_failed(&mut self, head: usize) {
        self.regions.insert(head, super::native_tier::RegionSlot::Failed);
        if let Some(slot) = self.region_blacklist.get_mut(head) {
            *slot = true;
        }
    }

    /// The collection registers a region mutates IN PLACE (the collection
    /// operand of every mutation op in its body).
    fn region_mutated_collection_regs(body: &[Op]) -> rustc_hash::FxHashSet<u16> {
        let mut s = rustc_hash::FxHashSet::default();
        for op in body {
            match op {
                Op::ListPush { list: c, .. }
                | Op::SetAdd { set: c, .. }
                | Op::RemoveFrom { collection: c, .. }
                | Op::SetIndex { collection: c, .. }
                | Op::SetIndexUnchecked { collection: c, .. }
                | Op::ListPop { list: c, .. } => {
                    s.insert(*c);
                }
                _ => {}
            }
        }
        s
    }

    /// Collection registers that hold a FRESH, uniquely-owned collection for the
    /// whole region: a `NewEmpty*{dst=C}` op DOMINATES every in-place mutation of
    /// `C`. Such a collection is created anew on each entry to its live range, so its
    /// mutation can NEVER alias — it needs no entry copy-on-write, and any use of
    /// `C`'s register BEFORE the fresh definition is a disjoint (scalar) live range
    /// that register-recycling left behind (fannkuch's `Set r to r-1` scratch landing
    /// on `perm`'s slot before `perm` is created). Excluding these from the mutated
    /// set keeps the region tier-able under value semantics WITHOUT weakening
    /// soundness: a genuinely shared/aliased mutation has no dominating fresh
    /// definition, so it stays in the set and is COW'd or declined.
    ///
    /// `body` is `program.code[head..=back]`; region-relative index `i` is pc `head+i`.
    fn region_fresh_collection_regs(body: &[Op], head: usize) -> rustc_hash::FxHashSet<u16> {
        let n = body.len();
        let mut out = rustc_hash::FxHashSet::default();
        if n == 0 {
            return out;
        }
        // Region-relative successors (an edge leaving [head, back] is dropped — a
        // fresh definition need only dominate mutations WITHIN the region).
        let rel = |target: usize| -> Option<usize> { target.checked_sub(head).filter(|&r| r < n) };
        let mut succs: Vec<Vec<usize>> = vec![Vec::new(); n];
        for (i, op) in body.iter().enumerate() {
            match op {
                Op::Jump { target } => {
                    if let Some(r) = rel(*target) {
                        succs[i].push(r);
                    }
                }
                Op::JumpIfFalse { target, .. } | Op::JumpIfTrue { target, .. } => {
                    if let Some(r) = rel(*target) {
                        succs[i].push(r);
                    }
                    if i + 1 < n {
                        succs[i].push(i + 1);
                    }
                }
                Op::Return { .. } | Op::ReturnNothing | Op::Halt => {}
                _ => {
                    if i + 1 < n {
                        succs[i].push(i + 1);
                    }
                }
            }
        }
        let mut preds: Vec<Vec<usize>> = vec![Vec::new(); n];
        for (i, ss) in succs.iter().enumerate() {
            for &s in ss {
                preds[s].push(i);
            }
        }
        // Iterative dominators over the region CFG (entry = relative 0 = `head`).
        // `dom[i][k]` == node k dominates node i. Regions are small, so the O(n²) set
        // representation is fine. Unreachable nodes keep the full (all-true) set —
        // harmless: they never execute, so any "fresh" verdict on them is moot.
        let mut dom: Vec<Vec<bool>> = vec![vec![true; n]; n];
        dom[0] = vec![false; n];
        dom[0][0] = true;
        let mut changed = true;
        while changed {
            changed = false;
            for i in 1..n {
                if preds[i].is_empty() {
                    continue;
                }
                let mut new = vec![true; n];
                for &p in &preds[i] {
                    for (k, nk) in new.iter_mut().enumerate() {
                        *nk &= dom[p][k];
                    }
                }
                new[i] = true;
                if new != dom[i] {
                    dom[i] = new;
                    changed = true;
                }
            }
        }
        // Mutation positions per collection reg, and fresh-definition positions.
        let mut muts: rustc_hash::FxHashMap<u16, Vec<usize>> = rustc_hash::FxHashMap::default();
        let mut news: rustc_hash::FxHashMap<u16, Vec<usize>> = rustc_hash::FxHashMap::default();
        for (i, op) in body.iter().enumerate() {
            match op {
                Op::ListPush { list: c, .. }
                | Op::SetAdd { set: c, .. }
                | Op::RemoveFrom { collection: c, .. }
                | Op::SetIndex { collection: c, .. }
                | Op::SetIndexUnchecked { collection: c, .. }
                | Op::ListPop { list: c, .. } => muts.entry(*c).or_default().push(i),
                Op::NewEmptyList { dst }
                | Op::NewEmptySet { dst }
                | Op::NewEmptyMap { dst }
                | Op::NewEmptyListI32 { dst } => news.entry(*dst).or_default().push(i),
                _ => {}
            }
        }
        for (c, mpos) in &muts {
            if let Some(npos) = news.get(c) {
                // Fresh iff SOME fresh-definition position dominates EVERY mutation.
                if npos.iter().any(|&q| mpos.iter().all(|&m| dom[m][q])) {
                    out.insert(*c);
                }
            }
        }
        out
    }

    /// True if any mutated-collection register is COPIED, ALIASED, redefined, or
    /// otherwise escapes the region — so region-entry copy-on-write alone cannot
    /// keep it isolated and the region must run on the value-semantic VM. An
    /// in-place collection mutation and a pure read use the collection soundly
    /// (its buffer stays private to the region); ANY other operand use of a
    /// mutated register — or any un-modelled op — fails closed.
    fn region_mutation_escapes(body: &[Op], m: &rustc_hash::FxHashSet<u16>) -> bool {
        let h = |r: &u16| m.contains(r);
        let rng = |s: u16, c: u16| (s..s.saturating_add(c)).any(|r| m.contains(&r));
        body.iter().any(|op| Self::op_escapes_mutated(op, &h, &rng))
    }

    fn op_escapes_mutated(
        op: &Op,
        h: &impl Fn(&u16) -> bool,
        rng: &impl Fn(u16, u16) -> bool,
    ) -> bool {
        match op {
            // In-place mutation: the collection operand is isolated by the entry
            // COW; only the OTHER operands can leak/redefine it.
            Op::ListPush { value, .. } | Op::SetAdd { value, .. } | Op::RemoveFrom { value, .. } => {
                h(value)
            }
            Op::SetIndex { index, value, .. } | Op::SetIndexUnchecked { index, value, .. } => {
                h(index) || h(value)
            }
            Op::ListPop { dst, .. } => h(dst),
            // Pure reads of a collection.
            Op::Index { dst, index, .. } | Op::IndexUnchecked { dst, index, .. } => {
                h(dst) || h(index)
            }
            Op::Length { dst, .. } => h(dst),
            Op::Contains { dst, value, .. } => h(dst) || h(value),
            Op::RegionBoundsGuard { bound, iv, .. } => h(bound) || h(iv),
            // Creating a FRESH collection in a mutated register is safe: the new
            // buffer is uniquely owned, so its in-place mutation cannot alias
            // (an aliasing copy would still be caught by the other arms). This is
            // the fresh-list-per-iteration pattern (`Let mutable p be a new Seq`).
            Op::NewEmptyList { .. }
            | Op::NewEmptySet { .. }
            | Op::NewEmptyMap { .. }
            | Op::NewEmptyListI32 { .. } => false,
            Op::NewRange { start, end, .. } => h(start) || h(end),
            Op::NewList { start, count, .. } | Op::NewTuple { start, count, .. } => rng(*start, *count),
            // Scalars / control flow: decline only if a mutated reg is an operand
            // (a redefinition of the collection reg, or a scalar read of it).
            Op::LoadConst { dst, .. }
            | Op::GlobalGet { dst, .. }
            | Op::LoadToday { dst }
            | Op::LoadNow { dst }
            | Op::Args { dst }
            | Op::IterNext { dst, .. } => h(dst),
            // A call-site COW barrier may redefine (clone) the register — decline a
            // region if it targets a mutated collection reg. In practice it never
            // appears in a tier-able region (it sits beside a `Call`, and a region
            // with a call declines regardless).
            Op::EnsureOwned { reg } => h(reg),
            Op::Move { dst, src }
            | Op::Not { dst, src }
            | Op::AddAssign { dst, src }
            | Op::FormatValue { dst, src, .. } => h(dst) || h(src),
            Op::Add { dst, lhs, rhs }
            | Op::Sub { dst, lhs, rhs }
            | Op::Mul { dst, lhs, rhs }
            | Op::Div { dst, lhs, rhs }
            | Op::ExactDiv { dst, lhs, rhs }
            | Op::FloorDiv { dst, lhs, rhs }
            | Op::Mod { dst, lhs, rhs }
            | Op::Lt { dst, lhs, rhs }
            | Op::Gt { dst, lhs, rhs }
            | Op::LtEq { dst, lhs, rhs }
            | Op::GtEq { dst, lhs, rhs }
            | Op::Eq { dst, lhs, rhs }
            | Op::NotEq { dst, lhs, rhs }
            | Op::ApproxEq { dst, lhs, rhs }
            | Op::Pow { dst, lhs, rhs }
            | Op::BitXor { dst, lhs, rhs }
            | Op::BitAnd { dst, lhs, rhs }
            | Op::BitOr { dst, lhs, rhs }
            | Op::Shl { dst, lhs, rhs }
            | Op::Shr { dst, lhs, rhs } => h(dst) || h(lhs) || h(rhs),
            Op::MagicDivU { dst, lhs, .. } | Op::DivPow2 { dst, lhs, .. } => h(dst) || h(lhs),
            // Collection-producing binops READ their operands and yield a FRESH,
            // independent result — decline only if a mutated reg is involved.
            Op::Concat { dst, lhs, rhs }
            | Op::SeqConcat { dst, lhs, rhs }
            | Op::UnionOp { dst, lhs, rhs }
            | Op::IntersectOp { dst, lhs, rhs } => h(dst) || h(lhs) || h(rhs),
            // Enum-arm test/bind and struct-field read: scalar-shaped, no alias.
            Op::TestArm { dst, target, .. } | Op::BindArm { dst, target, .. } => h(dst) || h(target),
            Op::GetField { dst, obj, .. } => h(dst) || h(obj),
            Op::DestructureTuple { src, start, count } => h(src) || rng(*start, *count),
            Op::Jump { .. } | Op::ReturnNothing | Op::IterPop => false,
            Op::JumpIfFalse { cond, .. } | Op::JumpIfTrue { cond, .. } => h(cond),
            Op::IterPrepare { iterable } => h(iterable),
            Op::Sleep { duration } => h(duration),
            // Everything else — a Move/Concat producing a live copy, calls,
            // closures, spawns, channels, CRDTs, global stores, returns, struct/
            // tuple/inductive builders, Show, slices, deep-clones — could retain
            // or alias the collection. Fail closed.
            _ => true,
        }
    }

    /// Back-edge hook for hot loops in ANY frame (`Jump` to an earlier pc):
    /// profile, compile when hot, and — when ready and the guard passes — run
    /// the region natively. `named`/`frame_regs` describe the ENCLOSING frame
    /// (Main's or a function's). `cur_func` is the enclosing function index (for
    /// the region-entry COW's mutable-param check). Returns the pc to resume at
    /// (the loop's exit).
    fn try_region(
        &mut self,
        head: usize,
        back_pc: usize,
        named: &[bool],
        frame_regs: usize,
        depth_now: usize,
        cur_func: Option<u16>,
    ) -> Option<RegionExit> {
        use super::native_tier::{RegionSlot, REGION_TIER_THRESHOLD};
        let tier = self.tier?;
        match self.regions.get(&head) {
            Some(RegionSlot::Failed) => return None,
            Some(RegionSlot::Ready { .. }) => {}
            None => {
                let n = self.region_hot.entry(head).or_insert(0);
                *n += 1;
                if *n < REGION_TIER_THRESHOLD {
                    return None;
                }
                // Region extent: every jump leaving [head, back_pc] must
                // agree on ONE exit pc.
                let body = &self.program.code[head..=back_pc];
                let mut exit: Option<usize> = None;
                for op in body {
                    if let Op::Jump { target } | Op::JumpIfFalse { target, .. }
                    | Op::JumpIfTrue { target, .. } = *op
                    {
                        if !(head..=back_pc).contains(&target) {
                            match exit {
                                None => exit = Some(target),
                                Some(e) if e == target => {}
                                _ => {
                                    self.mark_region_failed(head);
                                    return None;
                                }
                            }
                        }
                    }
                }
                let Some(exit_pc) = exit else {
                    self.mark_region_failed(head);
                    return None;
                };
                // Value semantics: a region that mutates a collection IN PLACE
                // may be writing a SHARED (aliased) allocation — native code
                // writes through the `Rc` directly, a reference-semantics
                // miscompile. We tier it soundly by copy-on-write'ing each
                // mutated collection at region ENTRY (isolating it), PROVIDED no
                // mutated collection escapes the region (then entry-COW is not
                // enough — decline, run on the value-semantic VM). A `mutable`
                // param is intentionally shared with the caller, so its in-place
                // mutation is correct and it is NOT COW'd.
                let mut cow_regs: Vec<u16> = Vec::new();
                if crate::semantics::collections::value_semantics_enabled() {
                    let mut mutated = Self::region_mutated_collection_regs(body);
                    // A collection created FRESH in-region (its `NewEmpty` dominates
                    // every mutation) is uniquely owned by construction: it needs no
                    // entry-COW, and its register's earlier recycled-scratch uses no
                    // longer read as a spurious alias-escape (the fannkuch `perm`
                    // whose slot a `Set r to r-1` scratch reused before `perm` exists).
                    for r in Self::region_fresh_collection_regs(body, head) {
                        mutated.remove(&r);
                    }
                    if !mutated.is_empty() {
                        if Self::region_mutation_escapes(body, &mutated) {
                            self.mark_region_failed(head);
                            return None;
                        }
                        let mutable_params = cur_func
                            .and_then(|fi| self.program.functions.get(fi as usize))
                            .map(|f| f.mutable_param_regs.as_slice())
                            .unwrap_or(&[]);
                        cow_regs =
                            mutated.into_iter().filter(|r| !mutable_params.contains(r)).collect();
                    }
                }
                let reg_count = u16::try_from(frame_regs).ok()?;
                // Speculation seed: the kinds sitting in this frame's
                // registers RIGHT NOW. The adapter compiles against them;
                // the guard set re-checks them on every entry.
                let observed: Vec<super::native_tier::ObservedKind> = (0..frame_regs)
                    .map(|r| {
                        use crate::interpreter::{ListRepr, RuntimeValue};
                        use super::native_tier::ObservedKind;
                        let rt = self.registers.get(self.base + r).map(|v| v.as_runtime());
                        match rt.as_deref() {
                            Some(RuntimeValue::Int(_)) => ObservedKind::Int,
                            // A BigInt is a promoted (overflowed) integer — still an
                            // integer kind, so the region tiers the slot as Int. The
                            // entry guard re-checks the representation: a real BigInt in
                            // an Int-guarded slot fails the guard and stays in the exact
                            // VM, so the native i64 fast path is never entered with a box.
                            Some(RuntimeValue::BigInt(_)) => ObservedKind::Int,
                            Some(RuntimeValue::Float(_)) => ObservedKind::Float,
                            Some(RuntimeValue::Bool(_)) => ObservedKind::Bool,
                            Some(RuntimeValue::List(rc)) => match &*rc.borrow() {
                                ListRepr::Ints(_) => ObservedKind::IntList,
                                ListRepr::IntsI32(_) => ObservedKind::IntListI32,
                                ListRepr::Floats(_) => ObservedKind::FloatList,
                                ListRepr::Bools(_) => ObservedKind::BoolList,
                                ListRepr::Boxed(_)
                                | ListRepr::Strings { .. }
                                | ListRepr::Structs { .. }
                                | ListRepr::Inductives { .. }
                                | ListRepr::WireStructs { .. }
                                | ListRepr::WireColumn { .. } => ObservedKind::Other,
                            },
                            Some(RuntimeValue::Map(_)) => ObservedKind::Map,
                            // An ASCII Text rides the byte-pin lane (char index ==
                            // byte index, char count == byte length). The metrics
                            // cache makes the ASCII test O(1) per crossing. A
                            // non-ASCII Text stays Other → the region bails and the
                            // per-char decode path runs, so the JIT never diverges.
                            Some(RuntimeValue::Text(rc))
                                if crate::semantics::collections::text_is_ascii(rc) =>
                            {
                                ObservedKind::TextBytes
                            }
                            _ => ObservedKind::Other,
                        }
                    })
                    .collect();
                let callees: Vec<super::native_tier::CalleeSig> = {
                    let prog = &self.program;
                    let code_len = prog.code.len();
                    prog.functions
                        .iter()
                        .map(|f| {
                            let end = prog
                                .functions
                                .iter()
                                .map(|h| h.entry_pc)
                                .filter(|&pc| pc > f.entry_pc)
                                .min()
                                .unwrap_or(code_len);
                            let (list_params_stable, returns_list_param) = analyze_list_call_safety(
                                &prog.code[f.entry_pc..end],
                                f.param_count,
                                &f.param_kinds,
                                f.register_count,
                            );
                            super::native_tier::CalleeSig {
                                param_kinds: f.param_kinds.clone(),
                                ret: f.ret_kind,
                                list_params_stable,
                                returns_list_param,
                            }
                        })
                        .collect()
                };
                // PRECISE REGION LIVE-OUT: a name bound INSIDE this loop is
                // lexically dead at the loop exit, so it must NOT be written
                // back — dropping it from `named` lets the JIT's copy-prop / CSE
                // / fusion treat it as true scratch. `loop_locals[head]` is the
                // compiler's exact per-loop set; absent (no loop record) keeps
                // the conservative full `named`.
                let liveout_off = std::env::var("LOGOS_LIVEOUT").as_deref() == Ok("0");
                if std::env::var_os("LOGOS_LIVEOUT_TRACE").is_some() {
                    let ll = self.program.loop_locals.get(&head);
                    let nnamed = named.iter().filter(|&&n| n).count();
                    let freed = ll.map_or(0, |m| {
                        named.iter().enumerate().filter(|(r, &n)| n && m.get(*r).copied().unwrap_or(false)).count()
                    });
                    eprintln!("liveout-trace: head={head} named={nnamed} loop_locals={} freed={freed}", ll.is_some());
                }
                let region_named: Vec<bool> = match self.program.loop_locals.get(&head) {
                    Some(locals) if !liveout_off => named
                        .iter()
                        .enumerate()
                        .map(|(r, &n)| n && !locals.get(r).copied().unwrap_or(false))
                        .collect(),
                    _ => named.to_vec(),
                };
                match tier.compile_region(
                    body,
                    head,
                    exit_pc,
                    &self.program.constants,
                    reg_count,
                    &region_named,
                    &observed,
                    &self.native_ctx,
                    &callees,
                ) {
                    Some(rf) => {
                        self.regions.insert(head, RegionSlot::Ready { rf, exit_pc, misses: 0 });
                        if !cow_regs.is_empty() {
                            self.region_cow_regs.insert(head, cow_regs);
                        }
                    }
                    None => {
                        self.mark_region_failed(head);
                        return None;
                    }
                }
            }
        }
        let result = self.run_ready_region(head, depth_now, cur_func);
        if result.is_none() {
            // Guard failure or side exit: count it; a region that keeps
            // missing re-runs work every entry — demote to pure bytecode.
            let mut demote = false;
            if let Some(RegionSlot::Ready { misses, .. }) = self.regions.get_mut(&head) {
                *misses += 1;
                if *misses >= super::native_tier::REGION_DEMOTE_AFTER {
                    demote = true;
                }
            }
            if demote {
                self.mark_region_failed(head);
            }
        }
        result
    }

    /// The Ready-path body of [`Vm::try_region`]: guards, pinning, the native
    /// run, and write-back. None = guard failure or side exit (the caller
    /// counts misses).
    fn run_ready_region(
        &mut self,
        head: usize,
        depth_now: usize,
        cur_func: Option<u16>,
    ) -> Option<RegionExit> {
        use super::native_tier::RegionSlot;
        // Region-entry copy-on-write: isolate each collection this region mutates
        // in place, so the native code's in-place writes cannot leak through a
        // shared `Rc`. A no-op when already uniquely owned; a one-time deep clone
        // when aliased. `mutable`-param collections were excluded at formation
        // (their sharing is intentional), so this only isolates value bindings.
        if let Some(regs) = self.region_cow_regs.get(&head) {
            for r in regs.clone() {
                self.ensure_reg_owned(r, cur_func);
            }
        }
        let Some(RegionSlot::Ready { rf, exit_pc, .. }) = self.regions.get(&head) else {
            unreachable!()
        };
        // Guard: every live-in slot must hold exactly the kind the region
        // speculated on; copy the raw representation in (floats as bits).
        {
            use crate::interpreter::RuntimeValue;
            use super::native_tier::SlotKind;
            for &(r, kind) in rf.guard_set() {
                let v = self.registers.get(self.base + r as usize)?;
                match (kind, &*v.as_runtime()) {
                    (SlotKind::Int, RuntimeValue::Int(_)) => {}
                    (SlotKind::Float, RuntimeValue::Float(_)) => {}
                    (SlotKind::Bool, RuntimeValue::Bool(_)) => {}
                    _ => return None,
                }
            }
        }
        // Frequently re-entered regions (sift-down loops) cannot afford a
        // heap allocation per entry — reuse one thread-local buffer.
        thread_local! {
            static REGION_FRAME: std::cell::RefCell<Vec<i64>> =
                const { std::cell::RefCell::new(Vec::new()) };
        }
        // `LOGOS_JIT_CANARY=1` guards the region frame with a sentinel
        // canary past its live span (`need` = frame proper + call-arena
        // headroom): any region-native write beyond `need` trips it loudly
        // at the source. Off by default (and in release) so normal runs
        // pay nothing.
        let frame_canary: usize = if jit_canary_enabled() { 64 } else { 0 };
        const FRAME_SENTINEL: i64 = 0x6262_6262_6262_6262u64 as i64;
        let need = rf.frame_size() + rf.arena_slots();
        let frame_cell = REGION_FRAME.with(|f| {
            let mut frame = f.take();
            // Zero only the region frame proper. The call-arena headroom is
            // REUSED untouched (16MiB for calling regions — zeroing it per
            // entry would memset megabytes every loop crossing): callee
            // chains write-before-read by the kind gates and the call
            // stencil plants each limit slot, so stale slots are
            // unobservable — the same contract as the function tier's
            // thread-local arena.
            if frame.len() < need + frame_canary {
                frame.resize(need + frame_canary, 0);
            }
            frame[..rf.frame_size()].fill(0);
            for c in &mut frame[need..need + frame_canary] {
                *c = FRAME_SENTINEL;
            }
            frame
        });
        let mut frame = frame_cell;
        {
            use crate::interpreter::RuntimeValue;
            use super::native_tier::SlotKind;
            for &(r, kind) in rf.guard_set() {
                frame[r as usize] =
                    match (kind, &*self.registers[self.base + r as usize].as_runtime()) {
                        (SlotKind::Int, RuntimeValue::Int(n)) => *n,
                        (SlotKind::Float, RuntimeValue::Float(f)) => f.to_bits() as i64,
                        (SlotKind::Bool, RuntimeValue::Bool(b)) => *b as i64,
                        _ => unreachable!("guard verified the discriminant above"),
                    };
            }
        }
        // Pin arrays: borrow each DISTINCT Rc once (held across the whole
        // native run — zero refcount/borrow traffic inside the loop), check
        // the speculated repr, and plant buffer pointer + length in the
        // dedicated frame slots. Aliased registers resolve to the same
        // buffer. Handles drop before write-back (in-place arrays need none;
        // the deopt replay is sound by prefix-idempotence).
        // The register-file base pointer, captured ONCE before any pin takes an
        // (immutable) borrow of `self.registers` through a list/map `Rc`. A
        // `TextMut` pin plants a `*mut Value` to a register CELL derived from
        // this pointer — the cell is stable across the native run (the register
        // file never reallocates while a region runs), so the append helper can
        // grow the accumulator through it. Reading/writing a cell through this
        // raw pointer does not conflict with the handles' shared borrows.
        let reg_base_ptr: *mut Value = self.registers.as_mut_ptr();
        let (outcome, text_mut_snapshots) = {
            use crate::interpreter::{ListRepr, RuntimeValue};
            let pins = rf.array_set();
            let mut handles: Vec<(usize, std::cell::RefMut<'_, ListRepr>)> =
                Vec::with_capacity(pins.len());
            // Parallel to `handles`: a buffer is "mutated" if ANY pin aliasing it
            // writes in place under a deopt-capable, non-precise region — it needs
            // a full-content snapshot/restore across a classic replay deopt.
            let mut handle_mutated: Vec<bool> = Vec::with_capacity(pins.len());
            let mut map_handles: Vec<(
                usize,
                std::cell::RefMut<'_, crate::interpreter::MapStorage>,
            )> = Vec::new();
            // A pinned MUTABLE Text accumulator grows THROUGH the VM register
            // cell (the planted `*mut Value`). A classic replay-from-head Deopt
            // would re-run the appends the native prefix already landed in the
            // cell — a double-append — so snapshot each distinct accumulator's
            // entry `Value` and restore it before a classic `Deopt` replay
            // (precise regions resume at the faulting op and never replay, so
            // they keep the live grown value). `(register slot, entry Value)`.
            let mut text_mut_snapshots: Vec<(usize, Value)> = Vec::new();
            for pin in pins {
                if pin.elem == super::native_tier::PinElem::Map {
                    let v = self.registers.get(self.base + pin.reg as usize)?;
                    let Some(RuntimeValue::Map(rc)) = v.as_runtime_ref() else { return None };
                    let key = std::rc::Rc::as_ptr(rc) as usize;
                    if !map_handles.iter().any(|(k, _)| *k == key) {
                        let Ok(b) = rc.try_borrow_mut() else { return None };
                        map_handles.push((key, b));
                    }
                    let idx = map_handles.iter().position(|(k, _)| *k == key).unwrap();
                    let storage = &mut *map_handles[idx].1;
                    frame[pin.vec_slot as usize] =
                        storage as *mut crate::interpreter::MapStorage as i64;
                    frame[pin.ptr_slot as usize] = 0;
                    frame[pin.len_slot as usize] = 0;
                    continue;
                }
                if pin.elem == super::native_tier::PinElem::TextBytes {
                    // A pinned ASCII Text rides its BYTE buffer: char index ==
                    // byte index, char count == byte length. RE-CHECK ASCII at
                    // every entry (the speculation seed is not a standing
                    // guarantee — a region could be re-entered with a non-ASCII
                    // Text in the same register) — decline (deopt to bytecode)
                    // otherwise so the per-char decode path runs and the output
                    // never diverges from the tree-walker. `Rc<String>` is
                    // read-only (no RefCell): a TextBytes pin is never written,
                    // so the snapshot/rollback machinery does not apply.
                    let v = self.registers.get(self.base + pin.reg as usize)?;
                    let Some(RuntimeValue::Text(rc)) = v.as_runtime_ref() else { return None };
                    if !crate::semantics::collections::text_is_ascii(rc) {
                        return None;
                    }
                    frame[pin.vec_slot as usize] = 0;
                    frame[pin.ptr_slot as usize] = rc.as_bytes().as_ptr() as i64;
                    frame[pin.len_slot as usize] = rc.len() as i64;
                    continue;
                }
                if pin.elem == super::native_tier::PinElem::TextMut {
                    // A pinned MUTABLE Text accumulator: plant a `*mut Value` to
                    // the VM REGISTER CELL (stable for the run; the `Rc<String>`
                    // inside it reallocs/COWs on append). Decline (deopt to
                    // bytecode) if the observed value is not a Text. Snapshot the
                    // entry `Value` for the classic-Deopt rollback (one per
                    // distinct accumulator register). The cell is reached through
                    // `reg_base_ptr` (no `self.registers` borrow that would
                    // conflict with the live list/map handles).
                    if pin.reg as usize >= self.registers.len().saturating_sub(self.base) {
                        return None;
                    }
                    let slot = self.base + pin.reg as usize;
                    let cell: *mut Value = unsafe { reg_base_ptr.add(slot) };
                    if !matches!(unsafe { (*cell).as_runtime_ref() }, Some(RuntimeValue::Text(_))) {
                        return None;
                    }
                    if pin.mutated && !text_mut_snapshots.iter().any(|(s, _)| *s == slot) {
                        text_mut_snapshots.push((slot, unsafe { (*cell).clone() }));
                    }
                    frame[pin.vec_slot as usize] = cell as i64;
                    frame[pin.ptr_slot as usize] = 0;
                    frame[pin.len_slot as usize] = 0;
                    continue;
                }
                let v = self.registers.get(self.base + pin.reg as usize)?;
                let Some(RuntimeValue::List(rc)) = v.as_runtime_ref() else { return None };
                let key = std::rc::Rc::as_ptr(rc) as usize;
                if !handles.iter().any(|(k, _)| *k == key) {
                    let Ok(b) = rc.try_borrow_mut() else { return None };
                    handles.push((key, b));
                    handle_mutated.push(false);
                }
                let idx = handles.iter().position(|(k, _)| *k == key).unwrap();
                if pin.mutated {
                    handle_mutated[idx] = true;
                }
                let payload = &mut *handles[idx].1;
                use super::native_tier::PinElem;
                let (vec_handle, ptr, len) = match (payload, pin.elem) {
                    (ListRepr::Ints(v), PinElem::Int) => {
                        (v as *mut Vec<i64> as i64, v.as_mut_ptr() as *mut i64, v.len())
                    }
                    (ListRepr::IntsI32(v), PinElem::IntI32) => {
                        (v as *mut Vec<i32> as i64, v.as_mut_ptr() as *mut i64, v.len())
                    }
                    // Maps never reach this arm (their pin path is below) —
                    // a list register observed as Map is a guard failure.
                    (_, PinElem::Map) => return None,
                    (ListRepr::Floats(v), PinElem::Float) => {
                        (v as *mut Vec<f64> as i64, v.as_mut_ptr() as *mut i64, v.len())
                    }
                    (ListRepr::Bools(v), PinElem::Bool) => {
                        (v as *mut Vec<bool> as i64, v.as_mut_ptr() as *mut i64, v.len())
                    }
                    // Repr changed since compile (promotion) — guard failure.
                    _ => return None,
                };
                frame[pin.vec_slot as usize] = vec_handle;
                frame[pin.ptr_slot as usize] = ptr as i64;
                frame[pin.len_slot as usize] = len as i64;
                // LEVER B calling-convention invariant (matches the function
                // tier): a list register's frame slot MIRRORS its vec handle, so
                // staging a pinned array into a call's argument window (a `Move`
                // from the register slot) passes the live `*mut Vec`, not the
                // zeroed register cell. Harmless for non-call regions (the slot
                // is never read for a pinned array, and the frame is discarded on
                // deopt — the VM register keeps the real Rc).
                frame[pin.reg as usize] = vec_handle;
            }
            // HOISTED bounds checks (V8 loop bound-check elimination): with
            // the pinned lengths in hand, verify ONCE that every covered loop
            // access stays in bounds for the whole run. Any failure declines
            // the region — the VM replays the loop on bytecode, where the
            // accesses are checked and produce the exact error.
            for hg in rf.hoist_guards() {
                let len = frame[hg.len_slot as usize];
                let bound = match &*self.registers.get(self.base + hg.bound_reg as usize)?.as_runtime() {
                    RuntimeValue::Int(n) => *n,
                    _ => return None,
                };
                let iv = match &*self.registers.get(self.base + hg.iv_reg as usize)?.as_runtime() {
                    RuntimeValue::Int(n) => *n,
                    _ => return None,
                };
                if len < bound.saturating_add(hg.add_max as i64)
                    || iv.saturating_add(hg.add_min as i64) < 1
                {
                    return None;
                }
            }
            // Entry lengths of every pinned list buffer — the rollback target
            // if a mid-region side-exit forces discard-and-replay. `ListPush`
            // APPENDS, so it is NOT replay-idempotent; on deopt each pushed
            // buffer is truncated back to its entry length before the VM
            // replays the loop on bytecode, which then re-pushes cleanly
            // instead of duplicating. Read-only and in-place (SetIndex) buffers
            // keep their entry length, so the truncate is a no-op for them.
            let entry_lens: Vec<usize> = handles.iter().map(|(_, h)| h.len()).collect();
            // A buffer written IN PLACE (SetIndex) replays unsoundly under the
            // classic discard-replay deopt — the write already landed in the
            // SHARED buffer, so the bytecode replay-from-head double-applies it
            // (a read-modify-write or swap is not idempotent). Snapshot its full
            // contents on entry; a classic `Deopt` restores them (subsuming the
            // length, so a buffer that BOTH pushes and writes in place is covered
            // too). Push-only / read-only buffers keep the cheap length truncate.
            let entry_snapshots: Vec<Option<ListRepr>> = handles
                .iter()
                .zip(handle_mutated.iter())
                .map(|((_, h), &mt)| if mt { Some((**h).clone()) } else { None })
                .collect();
            let out = rf.run(&mut frame[..need], depth_now);
            if matches!(out, super::native_tier::RegionOutcome::Deopt) {
                for (((_, h), &n), snap) in handles
                    .iter_mut()
                    .zip(entry_lens.iter())
                    .zip(entry_snapshots.into_iter())
                {
                    match snap {
                        Some(s) => **h = s,
                        None => h.truncate(n),
                    }
                }
            }
            // The list/map handles drop here, releasing their `self.registers`
            // borrows; the text-accumulator rollback (which needs `&mut
            // self.registers`) runs after the block, gated on a classic Deopt.
            (out, text_mut_snapshots)
        };
        // Roll each pinned mutable-Text accumulator back to its entry `Value` on
        // a classic Deopt so the bytecode replay-from-head re-appends from the
        // pre-region prefix instead of doubling the native prefix's appends (the
        // appends landed directly in the VM register cell). Precise side exits
        // and successful completions keep the live grown value.
        if matches!(outcome, super::native_tier::RegionOutcome::Deopt) {
            for (slot, snap) in text_mut_snapshots {
                self.registers[slot] = snap;
            }
        }
        for (k, c) in frame[need..need + frame_canary].iter().enumerate() {
            assert_eq!(
                *c, FRAME_SENTINEL,
                "REGION_FRAME OVERFLOW: canary slot {k} (frame + {}) clobbered by region native code",
                need + k
            );
        }
        match outcome {
            super::native_tier::RegionOutcome::Completed => {}
            // Side exit: discard the private frame — VM registers still hold
            // the state of this back-edge crossing, so falling back to
            // bytecode re-runs the remaining iterations deterministically up
            // to the faulting op and raises the exact kernel error there.
            // (In-place array writes already landed; the replay recomputes
            // the same prefix values, so they are unobservable.)
            super::native_tier::RegionOutcome::Deopt => return None,
            // PRECISE side exit (push+SetIndex regions): the buffers were NOT
            // truncated (the truncate above is gated on `Deopt`), so completed
            // iterations' pushes and in-place writes stand. Materialize every
            // touched non-array scalar — all-int by the adapter's gate — from
            // the frame into the VM registers, then resume the bytecode AT the
            // faulting op (NOT the loop head): the faulting op re-runs exactly
            // once, raising the precise error or continuing, with no replay of
            // the completed prefix. Array handle registers keep their live Rc
            // (the region mutated through their pins).
            super::native_tier::RegionOutcome::DeoptAt { resume_pc } => {
                use super::native_tier::SlotKind;
                // Re-box each touched register by ITS kind at the faulting op
                // (from the region's kind flow): Int/Bool/Float from the frame's
                // raw bits; `None` keeps the VM register's current value (a
                // pinned array mutated in place, or a read-only/unknown slot).
                // Cloned so the `rf` borrow ends before the register writes.
                let kinds: Option<Vec<Option<SlotKind>>> =
                    rf.precise_kinds(resume_pc).map(|k| k.to_vec());
                let mut regs: Vec<u16> = rf
                    .guard_set()
                    .iter()
                    .map(|(r, _)| *r)
                    .chain(rf.free_set().iter().copied())
                    .chain(rf.write_set().iter().map(|(r, _)| *r))
                    .collect();
                regs.sort_unstable();
                regs.dedup();
                for r in regs {
                    let bits = frame[r as usize];
                    let v = match kinds
                        .as_ref()
                        .and_then(|k| k.get(r as usize).copied().flatten())
                    {
                        Some(SlotKind::Int) => Value::int(bits),
                        Some(SlotKind::Bool) => Value::bool(bits != 0),
                        Some(SlotKind::Float) => Value::float(f64::from_bits(bits as u64)),
                        None => continue,
                    };
                    self.set(r, v);
                }
                REGION_FRAME.with(|f| f.replace(frame));
                return Some(RegionExit::At(resume_pc));
            }
        }
        let writes: Vec<(u16, super::native_tier::SlotKind)> = rf.write_set().to_vec();
        let region_return = rf.region_return();
        let exit = *exit_pc;
        for (r, kind) in writes {
            use super::native_tier::SlotKind;
            let bits = frame[r as usize];
            let v = match kind {
                SlotKind::Int => Value::int(bits),
                SlotKind::Bool => Value::bool(bits != 0),
                SlotKind::Float => Value::float(f64::from_bits(bits as u64)),
            };
            self.set(r, v);
        }
        let result = match region_return {
            Some(rr) if frame[rr.flag_slot as usize] != 0 => {
                use super::native_tier::{RegionReturnKind, SlotKind};
                let bits = frame[rr.value_slot as usize];
                let v = match rr.kind {
                    RegionReturnKind::Slot(SlotKind::Int) => Value::int(bits),
                    RegionReturnKind::Slot(SlotKind::Bool) => Value::bool(bits != 0),
                    RegionReturnKind::Slot(SlotKind::Float) => {
                        Value::float(f64::from_bits(bits as u64))
                    }
                    RegionReturnKind::Register => self.reg(bits as u16).clone(),
                };
                RegionExit::Return(v)
            }
            _ => RegionExit::At(exit),
        };
        REGION_FRAME.with(|f| f.replace(frame));
        Some(result)
    }

    /// Install a native tier: hot functions in the integer subset run as
    /// JIT-compiled machine code, guarded per call (non-Int args deopt to
    /// the bytecode path).
    pub fn with_native_tier(mut self, tier: &'p dyn super::native_tier::NativeTier) -> Self {
        self.tier = Some(tier);
        self
    }

    /// Pre-install an AOT-native function for `fi` (HOTSWAP §Axis-3): the VM dispatches
    /// to it via the existing `NativeSlot::Ready` path from the first call (it is not
    /// `Untried`, so it skips the hotness threshold and the forge compile). Requires a
    /// native tier to be installed (the dispatch is gated on `self.tier`); on desktop
    /// the forge tier is always present alongside. Absent ⇒ the function stays on
    /// VM+JIT — the AOT artifact is strictly optional, no gap at the seam.
    #[cfg(not(target_arch = "wasm32"))]
    pub fn install_aot_native(&mut self, fi: usize, nf: Box<dyn super::native_tier::NativeFn>) {
        if fi < self.native.len() {
            let name = self.fn_name(fi);
            super::tier_trace::trace_transition(fi, &name, super::tier_trace::ExecTier::NativeAot);
            self.native[fi] = super::native_tier::NativeSlot::Ready(nf);
        }
    }

    /// Copy-on-write for value semantics (VM side; mirrors the tree-walker's
    /// `ensure_collection_owned`). Before mutating the collection in register
    /// `reg`, deep-clone it if another holder shares the allocation (`Rc` strong
    /// count > 1). Gated behind the migration flag — off by default, so the hot
    /// path is untouched. NOTE: the `mutable`-parameter exemption is NOT yet
    /// wired on the VM (compiled bytecode carries no param-mutability marker);
    /// that is the remaining VM-compiler work before the flag can be flipped on.
    fn ensure_reg_owned(&mut self, reg: Reg, cur_func: Option<u16>) {
        if !crate::semantics::collections::value_semantics_enabled() {
            return;
        }
        // A `mutable` parameter passes by reference: mutate the shared allocation
        // in place so the caller observes it (mirrors the tree-walker's skip-COW).
        let is_mutable_param = cur_func
            .and_then(|fi| self.program.functions.get(fi as usize))
            .is_some_and(|f| f.mutable_param_regs.contains(&reg));
        if is_mutable_param {
            return;
        }
        use crate::interpreter::RuntimeValue;
        use std::rc::Rc;
        let di = self.base + reg as usize;
        let shared = matches!(
            self.registers.get(di).map(|v| v.as_runtime()).as_deref(),
            Some(RuntimeValue::List(rc)) if Rc::strong_count(rc) > 1
        ) || matches!(
            self.registers.get(di).map(|v| v.as_runtime()).as_deref(),
            Some(RuntimeValue::Map(rc)) if Rc::strong_count(rc) > 1
        ) || matches!(
            self.registers.get(di).map(|v| v.as_runtime()).as_deref(),
            Some(RuntimeValue::Set(rc)) if Rc::strong_count(rc) > 1
        );
        if shared {
            let owned = self.registers[di].as_runtime().deep_clone();
            self.registers[di] = Value::from_runtime(owned);
        }
    }

    /// Null this call's argument-window registers after it returns. Dead once the
    /// call is over, but as the callee's params they persist below `restore_len`;
    /// left set, a collection argument keeps a live `Rc` clone in the caller frame
    /// that spuriously inflates `strong_count` and forces later copy-on-write. Zero
    /// `arg_count` (native/scheduler frames) does nothing.
    #[inline]
    fn clear_arg_window(&mut self, frame: &CallFrame) {
        let end = (frame.arg_lo + frame.arg_count as usize).min(self.registers.len());
        for slot in frame.arg_lo..end {
            self.registers[slot] = Value::nothing();
        }
    }

    /// Same as [`Vm::clear_arg_window`] for a call that completes INLINE (a native /
    /// WASM / builtin dispatch that never pushes a `CallFrame`): the argument window
    /// starts at the current base's `args_start`.
    #[inline]
    fn clear_args(&mut self, args_start: Reg, arg_count: u16) {
        let lo = self.base + args_start as usize;
        let end = (lo + arg_count as usize).min(self.registers.len());
        for slot in lo..end {
            self.registers[slot] = Value::nothing();
        }
    }

    /// Resolve a function's source name for the tier trace; empty when no interner is
    /// available (the trace then prints just the index).
    fn fn_name(&self, fi: usize) -> String {
        match (self.program.functions.get(fi), self.policy_ctx) {
            (Some(f), Some((_, interner))) => interner.resolve(f.name).to_string(),
            _ => String::new(),
        }
    }

    /// Install the process tier AND a background compiler (HOTSWAP §6): hot functions
    /// are compiled on a worker thread instead of stalling the interpreter. The tier
    /// must be `&'static` (the process-installed forge backend) so it can cross to the
    /// worker — `&'static dyn NativeTier` is `Send` because `NativeTier: Sync`. The
    /// interpreter still runs the chains and is the sole `FnTable` writer; the worker
    /// only compiles. Falls back to [`Vm::with_native_tier`] (synchronous) for a
    /// borrowed `&'p` tier, which cannot be shared with a thread.
    #[cfg(not(target_arch = "wasm32"))]
    pub fn with_bg_native_tier(
        mut self,
        tier: &'static dyn super::native_tier::NativeTier,
    ) -> Self {
        self.tier = Some(tier);
        self.bg = Some(super::bg_compile::BgCompiler::new(tier));
        self
    }

    /// Apply every background-compiled result that has come back: publish the native
    /// entry to the `FnTable` and flip the slot to `Ready` (or `Failed`). The
    /// interpreter is the sole `FnTable` writer, so this only ever runs on this
    /// thread, at the profiling points. No-op when there is no background compiler.
    #[cfg(not(target_arch = "wasm32"))]
    fn drain_bg_compiles(&mut self) {
        use super::bg_compile::CompileResult;
        use super::native_tier::NativeSlot;
        loop {
            let res = match self.bg.as_mut() {
                Some(b) => b.try_drain(),
                None => return,
            };
            let Some(res) = res else { return };
            match res {
                CompileResult::Function { fi, nf } => match nf {
                    Some(nf) => {
                        self.native_ctx.table.publish(fi, nf.entry_ptr(), nf.published_regc());
                        let name = self.fn_name(fi);
                        super::tier_trace::trace_transition(fi, &name, super::tier_trace::ExecTier::NativeForge);
                        self.native[fi] = NativeSlot::Ready(nf);
                    }
                    None => self.native[fi] = NativeSlot::Failed,
                },
            }
        }
    }

    /// Block until every outstanding background compile has come back and been
    /// published — the determinism hook the differential tests use so the native tier
    /// engages predictably regardless of thread scheduling. No-op without a background
    /// compiler.
    #[cfg(not(target_arch = "wasm32"))]
    pub fn drain_pending_compiles(&mut self) {
        use super::bg_compile::CompileResult;
        use super::native_tier::NativeSlot;
        let results = match self.bg.as_mut() {
            Some(b) => b.drain_blocking(),
            None => return,
        };
        for res in results {
            match res {
                CompileResult::Function { fi, nf } => match nf {
                    Some(nf) => {
                        self.native_ctx.table.publish(fi, nf.entry_ptr(), nf.published_regc());
                        let name = self.fn_name(fi);
                        super::tier_trace::trace_transition(fi, &name, super::tier_trace::ExecTier::NativeForge);
                        self.native[fi] = NativeSlot::Ready(nf);
                    }
                    None => self.native[fi] = NativeSlot::Failed,
                },
            }
        }
    }

    /// Supply the program arguments read by the `args()` system native. The
    /// vector is the full argv (index 0 is the program name), matching the
    /// compiled binary's `env::args()`.
    pub fn with_program_args(mut self, args: Vec<String>) -> Self {
        self.program_args = args;
        self
    }

    /// Tier dispatch for `Call`: Some(result) = the native fast path ran.
    /// Precise-deopt frame materialization — deliberately OUT of the
    /// dispatch loop (cold path; keeping its body inline bloats the hot
    /// match and costs i-cache on every dispatched op).
    #[cold]
    #[inline(never)]
    #[allow(clippy::too_many_arguments)]
    fn materialize_native_frames(
        &mut self,
        frames: &[super::native_tier::NativeFrame],
        list_args: &[Value],
        args_start: super::instruction::Reg,
        dst: super::instruction::Reg,
        func: u16,
        pc: usize,
        call_stack: &mut Vec<CallFrame>,
    ) -> Result<(), String> {
        use super::native_tier::RegBox;
        let mut frame_base = self.base + args_start as usize;
        let mut caller_base = self.base;
        for (k, fr) in frames.iter().enumerate() {
            if k > 0 {
                caller_base = frame_base;
                frame_base += fr.offset;
            }
            let restore_len = self.registers.len();
            let needed = frame_base + fr.regs.len();
            if needed > MAX_REGISTER_FILE {
                return Err("vm: register file limit exceeded".to_string());
            }
            if self.registers.len() < needed {
                self.registers.resize(needed, Value::nothing());
            }
            call_stack.push(CallFrame {
                return_pc: if k == 0 { pc + 1 } else { fr.return_pc },
                return_reg: if k == 0 { dst } else { fr.return_reg },
                caller_base,
                restore_len,
                iter_depth: self.iter_stack.len(),
                func,
                arg_lo: 0,
                arg_count: 0,
            });
            for (r, bits) in fr.regs.iter().enumerate() {
                let v = match fr.kinds[r] {
                    RegBox::Dead | RegBox::Resolved => continue,
                    RegBox::Int => Value::int(*bits),
                    RegBox::Bool => Value::bool(*bits != 0),
                    RegBox::Float => Value::float(f64::from_bits(*bits as u64)),
                    RegBox::ListParam(j) => match list_args.get(j as usize) {
                        Some(v) => v.clone(),
                        None => return Err("vm: deopt pin index out of range".to_string()),
                    },
                };
                self.registers[frame_base + r] = v;
            }
            for (r, v) in &fr.resolved {
                self.registers[frame_base + *r as usize] = v.clone();
            }
        }
        self.base = frame_base;
        Ok(())
    }

    fn try_native(
        &mut self,
        func: u16,
        args_start: super::instruction::Reg,
        arg_count: u16,
        bytecode_depth: usize,
    ) -> NativeDisposition {
        use super::native_tier::{NativeSlot, ParamKind, NATIVE_TIER_THRESHOLD};
        let Some(tier) = self.tier else { return NativeDisposition::Interpret };
        let fi = func as usize;
        // Publish any background-compiled chains that have come back (sole writer).
        #[cfg(not(target_arch = "wasm32"))]
        self.drain_bg_compiles();
        match self.native.get(fi) {
            None | Some(NativeSlot::Failed) => return NativeDisposition::Interpret,
            // Background compile in flight — keep running bytecode until it lands.
            Some(NativeSlot::Pending) => return NativeDisposition::Interpret,
            _ => {}
        }
        if matches!(self.native[fi], NativeSlot::Untried) {
            self.hot[fi] += 1;
            if self.hot[fi] < NATIVE_TIER_THRESHOLD {
                return NativeDisposition::Interpret;
            }
            let f = &self.program.functions[fi];
            if !f.captures.is_empty() {
                self.native[fi] = NativeSlot::Failed;
                return NativeDisposition::Interpret;
            }
            // A parameter whose declared type has no native representation
            // (Map, Text, nested Seq, …) can never enter native code —
            // fail once instead of bumping the hot counter forever.
            if f.param_kinds.iter().any(|k| k.is_none()) {
                self.native[fi] = NativeSlot::Failed;
                return NativeDisposition::Interpret;
            }
            let end = self
                .program
                .functions
                .iter()
                .map(|g| g.entry_pc)
                .filter(|&e| e > f.entry_pc)
                .min()
                .unwrap_or(self.program.code.len());
            let callees: Vec<super::native_tier::CalleeSig> = {
                let prog = &self.program;
                let code_len = prog.code.len();
                prog.functions
                    .iter()
                    .map(|g| {
                        let end = prog
                            .functions
                            .iter()
                            .map(|h| h.entry_pc)
                            .filter(|&pc| pc > g.entry_pc)
                            .min()
                            .unwrap_or(code_len);
                        let (list_params_stable, returns_list_param) = analyze_list_call_safety(
                            &prog.code[g.entry_pc..end],
                            g.param_count,
                            &g.param_kinds,
                            g.register_count,
                        );
                        super::native_tier::CalleeSig {
                            param_kinds: g.param_kinds.clone(),
                            ret: g.ret_kind,
                            list_params_stable,
                            returns_list_param,
                        }
                    })
                    .collect()
            };
            // With a background compiler, ship the compile off-thread and keep
            // running bytecode (HOTSWAP §6); the result is drained + published on a
            // later call. Without one (a borrowed `&'p` tier, or wasm), compile
            // synchronously — the retained fallback.
            #[cfg(not(target_arch = "wasm32"))]
            if self.bg.is_some() {
                let req = super::bg_compile::CompileRequest::Function(
                    super::bg_compile::FunctionRequest {
                        fi,
                        code: self.program.code[f.entry_pc..end].to_vec(),
                        entry_pc: f.entry_pc,
                        constants: std::sync::Arc::from(self.program.constants.clone()),
                        param_count: f.param_count,
                        register_count: f.register_count as u16,
                        param_kinds: f.param_kinds.clone(),
                        ret_kind: f.ret_kind,
                        callees,
                        ctx: self.native_ctx.clone(),
                    },
                );
                self.bg.as_mut().unwrap().submit(req);
                self.native[fi] = NativeSlot::Pending;
                return NativeDisposition::Interpret;
            }
            match tier.compile_function(
                &self.program.code[f.entry_pc..end],
                f.entry_pc,
                &self.program.constants,
                f.param_count,
                f.register_count as u16,
                func,
                &f.param_kinds,
                f.ret_kind,
                &self.native_ctx,
                &callees,
            ) {
                Some(nf) => {
                    self.native_ctx.table.publish(fi, nf.entry_ptr(), nf.published_regc());
                    let name = self.fn_name(fi);
                    super::tier_trace::trace_transition(fi, &name, super::tier_trace::ExecTier::NativeForge);
                    self.native[fi] = NativeSlot::Ready(nf);
                }
                None => {
                    self.native[fi] = NativeSlot::Failed;
                    return NativeDisposition::Interpret;
                }
            }
        }
        // The per-call guard: every argument must match its DECLARED kind
        // (floats travel as raw bits in the i64 slot; lists pin), else the
        // call stays interpreted.
        let base = self.base + args_start as usize;
        // Hot boundary: a stack buffer instead of a per-call Vec — functions
        // like gcd cross bytecode→native hundreds of thousands of times.
        if arg_count as usize > 16 {
            return NativeDisposition::Interpret;
        }
        let kinds = &self.program.functions[fi].param_kinds;
        let mut args = [0i64; 16];
        for k in 0..arg_count as usize {
            let Some(v) = self.registers.get(base + k) else {
                return NativeDisposition::Interpret;
            };
            args[k] = match kinds.get(k).copied().flatten() {
                Some(ParamKind::Scalar(super::native_tier::SlotKind::Int)) | None => {
                    match v.as_int() {
                        Some(n) => n,
                        None => return NativeDisposition::Interpret,
                    }
                }
                Some(ParamKind::Scalar(super::native_tier::SlotKind::Bool)) => {
                    match v.as_bool() {
                        Some(b) => b as i64,
                        None => return NativeDisposition::Interpret,
                    }
                }
                Some(ParamKind::Scalar(super::native_tier::SlotKind::Float)) => {
                    match v.as_float() {
                        Some(f) => f.to_bits() as i64,
                        None => return NativeDisposition::Interpret,
                    }
                }
                // List params ride the pin lane; the register slot is a
                // placeholder native code never reads as a scalar.
                Some(ParamKind::List(_)) => 0,
            };
        }
        // Pin list parameters: one borrow per DISTINCT Rc for the whole
        // call (recursion reuses the same pins via pass-through identity).
        // Empty lists retag in place to the declared element repr.
        let outcome = {
            use crate::interpreter::{ListRepr, RuntimeValue};
            use super::native_tier::PinElem;
            let mut handles: Vec<(usize, std::cell::RefMut<'_, ListRepr>)> = Vec::new();
            let mut pins: Vec<i64> = Vec::new();
            let mut pin_args: Vec<Value> = Vec::new();
            for (k, pk) in kinds.iter().enumerate().take(arg_count as usize) {
                let Some(ParamKind::List(elem)) = pk else { continue };
                let Some(v) = self.registers.get(base + k) else {
                    return NativeDisposition::Interpret;
                };
                let Some(RuntimeValue::List(rc)) = v.as_runtime_ref() else {
                    return NativeDisposition::Interpret;
                };
                pin_args.push(v.clone());
                let key = std::rc::Rc::as_ptr(rc) as usize;
                if !handles.iter().any(|(hk, _)| *hk == key) {
                    let Ok(mut b) = rc.try_borrow_mut() else {
                        return NativeDisposition::Interpret;
                    };
                    // Declared-elem retag for the empty list (the shared
                    // empty starts as Ints).
                    let empty = match &*b {
                        ListRepr::Ints(v) => v.is_empty(),
                        _ => false,
                    };
                    if empty {
                        match elem {
                            PinElem::Float => *b = ListRepr::Floats(Vec::new()),
                            PinElem::Bool => *b = ListRepr::Bools(Vec::new()),
                            PinElem::IntI32 => *b = ListRepr::IntsI32(Vec::new()),
                            PinElem::Int => {}
                            // Function params never pin maps or texts (declared
                            // kinds only produce Int/Float/Bool list elems).
                            PinElem::Map | PinElem::TextBytes | PinElem::TextMut => {
                                return NativeDisposition::Interpret
                            }
                        }
                    }
                    handles.push((key, b));
                }
                let idx = handles.iter().position(|(hk, _)| *hk == key).unwrap();
                let payload = &mut *handles[idx].1;
                let (vec_handle, ptr, len) = match (payload, elem) {
                    (ListRepr::Ints(v), PinElem::Int) => {
                        (v as *mut Vec<i64> as i64, v.as_mut_ptr() as i64, v.len())
                    }
                    (ListRepr::IntsI32(v), PinElem::IntI32) => {
                        (v as *mut Vec<i32> as i64, v.as_mut_ptr() as i64, v.len())
                    }
                    (ListRepr::Floats(v), PinElem::Float) => {
                        (v as *mut Vec<f64> as i64, v.as_mut_ptr() as i64, v.len())
                    }
                    (ListRepr::Bools(v), PinElem::Bool) => {
                        (v as *mut Vec<bool> as i64, v.as_mut_ptr() as i64, v.len())
                    }
                    _ => return NativeDisposition::Interpret,
                };
                pins.push(vec_handle);
                pins.push(ptr);
                pins.push(len as i64);
                // The marshalled argument slot mirrors the vec handle —
                // native list registers always carry their handle.
                args[k] = vec_handle;
            }
            let NativeSlot::Ready(nf) = &self.native[fi] else { unreachable!() };
            let args_slice = &args[..arg_count as usize];
            // The native callee occupies one LOGOS frame on top of the
            // bytecode stack; its self-calls count from there against
            // MAX_CALL_DEPTH.
            let out = nf.call(args_slice, &pins, bytecode_depth + 1);
            drop(handles);
            (out, pin_args, pins)
        };
        let (out, pin_args, pins_for_ret) = outcome;
        let NativeSlot::Ready(nf) = &self.native[fi] else { unreachable!() };
        match out {
            // The backend already re-boxed (list-returning functions own
            // their allocation registry).
            super::native_tier::NativeOutcome::ReturnValue(v) => NativeDisposition::Done(v),
            super::native_tier::NativeOutcome::Return(raw) => {
                NativeDisposition::Done(match nf.ret() {
                    super::native_tier::NativeRet::Scalar(k) => match k {
                        super::native_tier::SlotKind::Bool => Value::bool(raw != 0),
                        super::native_tier::SlotKind::Float => {
                            Value::float(f64::from_bits(raw as u64))
                        }
                        super::native_tier::SlotKind::Int => Value::int(raw),
                    },
                    // By-handle return that was NOT registry-owned: it is
                    // one of the caller's list arguments — match the pin
                    // handles (every triple's first slot) and clone that
                    // argument, preserving identity.
                    super::native_tier::NativeRet::ListByHandle => {
                        let mut found: Option<Value> = None;
                        for (k, chunk) in pins_for_ret.chunks(3).enumerate() {
                            if chunk.first() == Some(&raw) {
                                found = pin_args.get(k).cloned();
                                break;
                            }
                        }
                        match found {
                            Some(v) => v,
                            None => return NativeDisposition::Interpret,
                        }
                    }
                    // Return-by-parameter: the result IS the caller's list
                    // argument (same Rc — identity preserved).
                    super::native_tier::NativeRet::ListParam(j) => {
                        let mut nth = 0usize;
                        let mut found: Option<Value> = None;
                        for (k, pk) in kinds.iter().enumerate().take(arg_count as usize) {
                            if matches!(pk, Some(ParamKind::List(_))) {
                                if k == j as usize {
                                    found = pin_args.get(nth).cloned();
                                    break;
                                }
                                nth += 1;
                            }
                        }
                        match found {
                            Some(v) => v,
                            None => return NativeDisposition::Interpret,
                        }
                    }
                })
            }
            // Plain side exit: every effect was confined to the private
            // frame — replaying the whole call on bytecode is sound and
            // raises the exact kernel error at the exact point.
            super::native_tier::NativeOutcome::Deopt => NativeDisposition::Interpret,
            // Precise side exit: effects landed; materialize the chain.
            super::native_tier::NativeOutcome::DeoptAt { resume_pc, frames } => {
                NativeDisposition::Materialize { resume_pc, frames, list_args: pin_args }
            }
        }
    }

    /// Provide the policy registry (and the interner its symbols live in) for
    /// `Check` statements.
    pub fn with_policy_ctx(
        mut self,
        registry: &'p crate::analysis::PolicyRegistry,
        interner: &'p crate::intern::Interner,
    ) -> Self {
        self.policy_ctx = Some((registry, interner));
        self
    }

    /// The output lines, one per `Show`.
    pub fn lines(&self) -> &[String] {
        &self.lines
    }

    /// The output as one string (one trailing newline per `Show`).
    pub fn output(&self) -> String {
        let mut s = String::new();
        for l in &self.lines {
            s.push_str(l);
            s.push('\n');
        }
        s
    }

    /// Consume the VM, returning its output lines.
    pub fn into_lines(self) -> Vec<String> {
        self.lines
    }

    /// Take this slice's output lines (the scheduler driver merges each task's
    /// output into a shared sink as it is produced, preserving per-task order).
    pub(crate) fn drain_lines(&mut self) -> Vec<String> {
        std::mem::take(&mut self.lines)
    }

    /// Run the whole program to completion (the non-concurrent entry). A
    /// concurrent program never reaches here — it is driven through
    /// [`Vm::run_until_block`] by the scheduler.
    pub fn run(&mut self) -> Result<(), String> {
        // Hermetic program start: no ambient exchange rates carried in from a prior run on this
        // thread (mirrors a fresh AOT process). The resumable `run_until_block` path is left alone so
        // rates installed mid-program survive across concurrency suspensions.
        logicaffeine_base::money::clear_ambient_rates();
        match self.run_until_block()? {
            VmStep::Done(_) => Ok(()),
            VmStep::Blocked => {
                Err("vm: concurrency op requires the scheduler driver".to_string())
            }
            VmStep::Paused => unreachable!("run_until_block (STEPPED = false) never pauses"),
        }
    }

    /// Run one slice: from a fresh start (or resumed from a prior block) until the
    /// program completes ([`VmStep::Done`]) or a concurrency op suspends it
    /// ([`VmStep::Blocked`], request in [`Vm::take_pending`]). Keeping `pc` and the
    /// call stack as loop-locals (restored once on entry, saved once on a block)
    /// leaves the hot dispatch path byte-for-byte identical to the old `run`.
    pub(crate) fn run_until_block(&mut self) -> Result<VmStep, String> {
        self.run_until_block_impl::<false>(u64::MAX)
    }

    /// Step the debug interpreter forward by at most `step_budget` ops, then pause
    /// ([`VmStep::Paused`], resumable on the next call). Used only by the Studio
    /// debug drawer; production callers use [`Vm::run_until_block`]
    /// (`STEPPED = false`), whose monomorphization elides every budget check — the
    /// hot dispatch path stays the byte-for-byte old loop.
    pub(crate) fn run_steps(&mut self, step_budget: u64) -> Result<VmStep, String> {
        self.run_until_block_impl::<true>(step_budget)
    }

    fn run_until_block_impl<const STEPPED: bool>(
        &mut self,
        step_budget: u64,
    ) -> Result<VmStep, String> {
        let mut pc;
        let mut call_stack: Vec<CallFrame>;
        if self.sched_active {
            pc = self.sched_pc;
            call_stack = std::mem::take(&mut self.sched_call_stack);
            self.sched_active = false;
        } else {
            pc = 0usize;
            call_stack = Vec::new();
        }

        // The loop ends when the top-level program code is exhausted. A warm body
        // lives past `program.code.len()` but is only ever entered via a `Call` (which
        // pushes a frame), so `!call_stack.is_empty()` keeps the loop alive while one
        // is executing; without any warm body installed a live frame already implies
        // `pc < program.code.len()`, so this disjunct is a no-op for the baseline path.
        let mut executed: u64 = 0;
        while pc < self.program.code.len() || !call_stack.is_empty() {
            if STEPPED {
                if executed >= step_budget {
                    // Pause exactly as a concurrency block saves its slice (pc +
                    // call stack into the scheduler slots), but with no pending
                    // request — the debugger resumes on the next `run_steps`.
                    self.sched_pc = pc;
                    self.sched_call_stack = call_stack;
                    self.sched_active = true;
                    return Ok(VmStep::Paused);
                }
                executed += 1;
            }
            let op = if pc < self.program.code.len() {
                self.program.code[pc]
            } else {
                self.warm_code[pc - self.program.code.len()]
            };
            match op {
                Op::LoadConst { dst, idx } => {
                    let v = self.const_pool[idx as usize].clone();
                    self.set(dst, v);
                    pc += 1;
                }
                Op::Move { dst, src } => {
                    self.set(dst, self.reg(src).clone());
                    pc += 1;
                }
                Op::EnsureOwned { reg } => {
                    // Call-site copy-on-write barrier: isolate a shared collection
                    // before it is passed to a mutable-borrow callee. No-op when the
                    // register is a `mutable`-exempt param (its own writes COW) or the
                    // collection is already uniquely owned.
                    self.ensure_reg_owned(reg, call_stack.last().map(|f| f.func));
                    pc += 1;
                }
                Op::Add { dst, lhs, rhs } => { self.binop(dst, lhs, rhs, Value::add)?; pc += 1; }
                Op::AddAssign { dst, src } => { self.add_assign(dst, src)?; pc += 1; }
                Op::Sub { dst, lhs, rhs } => { self.binop(dst, lhs, rhs, Value::sub)?; pc += 1; }
                Op::Mul { dst, lhs, rhs } => { self.binop(dst, lhs, rhs, Value::mul)?; pc += 1; }
                Op::Div { dst, lhs, rhs } => { self.binop(dst, lhs, rhs, Value::div)?; pc += 1; }
                Op::ExactDiv { dst, lhs, rhs } => { self.binop(dst, lhs, rhs, Value::exact_div)?; pc += 1; }
                Op::FloorDiv { dst, lhs, rhs } => { self.binop(dst, lhs, rhs, Value::floor_div)?; pc += 1; }
                Op::Mod { dst, lhs, rhs } => { self.binop(dst, lhs, rhs, Value::modulo)?; pc += 1; }
                Op::DivPow2 { dst, lhs, k } => {
                    // `lhs / 2^k` (lhs is Oracle-proven Int) — identical result
                    // to `Op::Div` by `2^k`, so it matches the tree-walker.
                    let v = self.reg(lhs).div(&Value::int(1i64 << k))?;
                    self.set(dst, v);
                    pc += 1;
                }
                Op::MagicDivU { dst, lhs, magic, more, mul_back } => {
                    // `lhs / c` / `lhs % c` by the precomputed magic reciprocal
                    // (`magic`/`more`). Emitted only for an Oracle-proven Int,
                    // non-negative `lhs`, so the result is bit-identical to
                    // `Op::Div`/`Op::Mod` by the constant `c`.
                    let x = self.reg(lhs).as_int().ok_or_else(|| {
                        "MagicDivU on a non-Int operand".to_string()
                    })?;
                    let v = crate::vm::compiler::magic_eval(x, magic, more, mul_back);
                    self.set(dst, Value::int(v));
                    pc += 1;
                }
                Op::Lt { dst, lhs, rhs } => { self.binop(dst, lhs, rhs, Value::lt)?; pc += 1; }
                Op::Gt { dst, lhs, rhs } => { self.binop(dst, lhs, rhs, Value::gt)?; pc += 1; }
                Op::LtEq { dst, lhs, rhs } => { self.binop(dst, lhs, rhs, Value::lte)?; pc += 1; }
                Op::GtEq { dst, lhs, rhs } => { self.binop(dst, lhs, rhs, Value::gte)?; pc += 1; }
                Op::Eq { dst, lhs, rhs } => {
                    let v = self.reg(lhs).eq_op(self.reg(rhs));
                    self.set(dst, v);
                    pc += 1;
                }
                Op::ApproxEq { dst, lhs, rhs } => {
                    let v = crate::semantics::arith::approx_eq(
                        self.reg(lhs).as_runtime().clone(),
                        self.reg(rhs).as_runtime().clone(),
                    )
                    .map(Value::from_runtime)?;
                    self.set(dst, v);
                    pc += 1;
                }
                Op::NotEq { dst, lhs, rhs } => {
                    let v = self.reg(lhs).neq_op(self.reg(rhs));
                    self.set(dst, v);
                    pc += 1;
                }
                Op::Not { dst, src } => {
                    let v = self.reg(src).not_op()?;
                    self.set(dst, v);
                    pc += 1;
                }
                Op::Concat { dst, lhs, rhs } => { self.binop(dst, lhs, rhs, Value::concat)?; pc += 1; }
                Op::SeqConcat { dst, lhs, rhs } => { self.binop(dst, lhs, rhs, Value::seq_concat)?; pc += 1; }
                Op::Pow { dst, lhs, rhs } => { self.binop(dst, lhs, rhs, Value::pow)?; pc += 1; }
                Op::BitXor { dst, lhs, rhs } => { self.binop(dst, lhs, rhs, Value::bitxor)?; pc += 1; }
                Op::BitAnd { dst, lhs, rhs } => { self.binop(dst, lhs, rhs, Value::bitand)?; pc += 1; }
                Op::BitOr { dst, lhs, rhs } => { self.binop(dst, lhs, rhs, Value::bitor)?; pc += 1; }
                Op::Shl { dst, lhs, rhs } => { self.binop(dst, lhs, rhs, Value::shl)?; pc += 1; }
                Op::Shr { dst, lhs, rhs } => { self.binop(dst, lhs, rhs, Value::shr)?; pc += 1; }
                Op::Jump { target } => {
                    // A back-edge is a hot-loop candidate in EVERY frame
                    // (OSR everywhere); the enclosing frame picks the
                    // named-register map the write-back contract consults.
                    // Skip the whole region machinery (the per-frame named-map
                    // selection + the `regions` hashmap probe) once this loop
                    // head is blacklisted — an un-tierable / demoted region pays
                    // only this O(1) `Vec<bool>` index per back-edge instead of
                    // re-hashing every iteration.
                    // `target < program.code.len()` keeps the region machinery
                    // (blacklist indexed by program pc, `program.code[head..]` scans)
                    // off warm-body back-edges; always true on the baseline path.
                    if target < pc
                        && target < self.program.code.len()
                        && self.tier.is_some()
                        && !self.region_blacklist[target]
                    {
                        let (named, frame_regs): (&[bool], usize) = match call_stack.last() {
                            None => (&self.program.named_regs, self.program.register_count),
                            Some(f) => {
                                let fun = &self.program.functions[f.func as usize];
                                (&fun.named_regs, fun.register_count)
                            }
                        };
                        let cur_func = call_stack.last().map(|f| f.func);
                        match self.try_region(target, pc, named, frame_regs, call_stack.len(), cur_func)
                        {
                            Some(RegionExit::At(exit)) => {
                                pc = exit;
                                continue;
                            }
                            Some(RegionExit::Return(value)) => {
                                // The region hit a `Return` — perform the
                                // actual function return, exactly like the
                                // Op::Return arm.
                                let frame =
                                    call_stack.pop().ok_or("vm: return with no caller")?;
                                self.iter_stack.truncate(frame.iter_depth);
                                self.registers.truncate(frame.restore_len);
                                self.base = frame.caller_base;
                                self.set(frame.return_reg, value);
                                self.clear_arg_window(&frame);
                                pc = frame.return_pc;
                                continue;
                            }
                            None => {}
                        }
                    }
                    pc = target;
                }
                Op::JumpIfFalse { cond, target } => {
                    if !self.reg(cond).is_truthy() { pc = target; } else { pc += 1; }
                }
                Op::JumpIfTrue { cond, target } => {
                    if self.reg(cond).is_truthy() { pc = target; } else { pc += 1; }
                }
                Op::GlobalGet { dst, idx } => {
                    match &self.globals[idx as usize] {
                        Some(v) => {
                            let v = v.clone();
                            self.set(dst, v);
                        }
                        None => {
                            return Err(format!(
                                "Undefined variable: {}",
                                self.program.globals[idx as usize]
                            ));
                        }
                    }
                    pc += 1;
                }
                Op::GlobalSet { idx, src } => {
                    self.globals[idx as usize] = Some(self.reg(src).clone());
                    pc += 1;
                }
                Op::MakeClosure { dst, func, locals_start } => {
                    use crate::interpreter::{ClosureValue, RuntimeValue};
                    let f = self
                        .program
                        .functions
                        .get(func as usize)
                        .ok_or("vm: MakeClosure on undefined function index")?;
                    let mut captured_env = std::collections::HashMap::new();
                    let mut local_k: Reg = 0;
                    for (sym, global_idx) in &f.captures {
                        match global_idx {
                            Some(gidx) => {
                                // Snapshot the global IF it is defined; an
                                // undefined one is simply not captured — the
                                // body falls through to the live global.
                                if let Some(v) = &self.globals[*gidx as usize] {
                                    captured_env.insert(*sym, v.as_runtime().deep_clone());
                                }
                            }
                            None => {
                                let v = self.reg(locals_start + local_k).as_runtime().deep_clone();
                                captured_env.insert(*sym, v);
                                local_k += 1;
                            }
                        }
                    }
                    let param_names = vec![crate::intern::Symbol::default(); f.param_count as usize];
                    self.set(
                        dst,
                        Value::from_runtime(RuntimeValue::Function(Box::new(ClosureValue {
                            body_index: func as usize,
                            captured_env,
                            param_names,
                            generated: None,
                        }))),
                    );
                    pc += 1;
                }
                Op::CallValue { dst, callee, args_start, arg_count, name_for_err } => {
                    use crate::interpreter::RuntimeValue;
                    let closure = match &*self.reg(callee).as_runtime() {
                        RuntimeValue::Function(c) => (**c).clone(),
                        other => {
                            return Err(if name_for_err == u32::MAX {
                                format!("Cannot call value of type {}", other.type_name())
                            } else {
                                match &self.program.constants[name_for_err as usize] {
                                    Constant::Text(n) => format!("Unknown function: {}", n),
                                    _ => format!("Cannot call value of type {}", other.type_name()),
                                }
                            });
                        }
                    };
                    if call_stack.len() >= crate::semantics::MAX_CALL_DEPTH {
                        return Err(crate::semantics::CALL_DEPTH_ERR.to_string());
                    }
                    let f = self
                        .program
                        .functions
                        .get(closure.body_index)
                        .ok_or("vm: CallValue on undefined function index")?;
                    if arg_count as usize != f.param_count as usize {
                        return Err(format!(
                            "Closure expects {} arguments, got {}",
                            f.param_count, arg_count
                        ));
                    }
                    let entry_pc = f.entry_pc;
                    let reg_count = f.register_count;
                    let captures = f.captures.clone();
                    let param_count = f.param_count;

                    let callee_base = self.base + args_start as usize;
                    let restore_len = self.registers.len();
                    let needed = callee_base + reg_count;
                    if needed > MAX_REGISTER_FILE {
                        return Err("vm: register file limit exceeded".to_string());
                    }
                    if self.registers.len() < needed {
                        self.registers.resize(needed, Value::nothing());
                    }
                    call_stack.push(CallFrame {
                        return_pc: pc + 1,
                        return_reg: dst,
                        caller_base: self.base,
                        restore_len,
                        iter_depth: self.iter_stack.len(),
                        func: closure.body_index as u16,
                        arg_lo: callee_base,
                        arg_count,
                    });
                    self.base = callee_base;
                    // Bind captures: value slots then present flags — both
                    // deep-cloned PER CALL (the tree-walker re-clones each
                    // invocation).
                    let cap_count = captures.len() as Reg;
                    for (k, (sym, _)) in captures.iter().enumerate() {
                        let (v, present) = match closure.captured_env.get(sym) {
                            Some(v) => (Value::from_runtime(v.deep_clone()), true),
                            None => (Value::nothing(), false),
                        };
                        self.set(param_count + k as Reg, v);
                        self.set(param_count + cap_count + k as Reg, Value::bool(present));
                    }
                    pc = entry_pc;
                }
                Op::CallBuiltin { dst, builtin, args_start, arg_count } => {
                    let mut args = Vec::with_capacity(arg_count as usize);
                    for k in 0..arg_count {
                        args.push(self.reg(args_start + k).as_runtime().clone());
                    }
                    let v = crate::semantics::builtins::call_builtin(builtin, args)?;
                    self.set(dst, Value::from_runtime(v));
                    self.clear_args(args_start, arg_count);
                    pc += 1;
                }
                Op::Call { dst, func, args_start, arg_count } => {
                    if call_stack.len() >= crate::semantics::MAX_CALL_DEPTH {
                        return Err(crate::semantics::CALL_DEPTH_ERR.to_string());
                    }
                    match self.try_native(func, args_start, arg_count, call_stack.len()) {
                        NativeDisposition::Done(v) => {
                            self.set(dst, v);
                            self.clear_args(args_start, arg_count);
                            pc += 1;
                            continue;
                        }
                        NativeDisposition::Interpret => {}
                        // Precise deopt: every native frame becomes a real
                        // CallFrame (registers re-boxed per the adapter's
                        // kind capture), then the interpreter resumes AT
                        // the faulting op — effects stay, the kernel
                        // raises the exact error from the exact state.
                        NativeDisposition::Materialize { resume_pc, frames, list_args } => {
                            self.materialize_native_frames(
                                &frames, &list_args, args_start, dst, func, pc, &mut call_stack,
                            )?;
                            pc = resume_pc;
                            continue;
                        }
                    }
                    // WS6 (Phase 13): the WASM-JIT tier — the browser JIT path, consulted
                    // here (behind the native forge tier above, which is absent on wasm32).
                    // A hot pure-integer function runs its emitted WebAssembly module instead
                    // of the bytecode; non-Int args or an ineligible body fall through.
                    #[cfg(feature = "wasm-jit")]
                    {
                        let prog = self.program;
                        let abase = self.base + args_start as usize;
                        let mut ints: Vec<i64> = Vec::with_capacity(arg_count as usize);
                        let mut all_int = true;
                        for k in 0..arg_count as usize {
                            match &*self.registers[abase + k].as_runtime() {
                                crate::interpreter::RuntimeValue::Int(n) => ints.push(*n),
                                _ => {
                                    all_int = false;
                                    break;
                                }
                            }
                        }
                        if all_int {
                            if let Some(r) = self.wasm_tier.on_call(prog, func, &ints) {
                                self.set(dst, Value::int(r));
                                pc += 1;
                                continue;
                            }
                        }
                    }
                    // Dispatch order (HOTSWAP §7): FnTable native (above) →
                    // warm_bytecode → baseline. A warm body runs from the unified
                    // pc space past `program.code`.
                    let (entry_pc, reg_count) = match self.warm_entry.get(func as usize).and_then(|w| *w) {
                        Some(w) => (w.entry_pc, w.register_count),
                        None => {
                            let f = self
                                .program
                                .functions
                                .get(func as usize)
                                .ok_or("vm: call to undefined function index")?;
                            (f.entry_pc, f.register_count)
                        }
                    };
                    let callee_base = self.base + args_start as usize;
                    let restore_len = self.registers.len();
                    let needed = callee_base + reg_count;
                    if needed > MAX_REGISTER_FILE {
                        return Err("vm: register file limit exceeded".to_string());
                    }
                    if self.registers.len() < needed {
                        self.registers.resize(needed, Value::nothing());
                    }
                    call_stack.push(CallFrame {
                        return_pc: pc + 1,
                        return_reg: dst,
                        caller_base: self.base,
                        restore_len,
                        iter_depth: self.iter_stack.len(),
                        func,
                        arg_lo: callee_base,
                        arg_count,
                    });
                    self.base = callee_base;
                    pc = entry_pc;
                }
                Op::Return { src } => {
                    let frame = call_stack.pop().ok_or("vm: return with no caller")?;
                    let rv = self.reg(src).clone();
                    self.iter_stack.truncate(frame.iter_depth);
                    self.registers.truncate(frame.restore_len);
                    self.base = frame.caller_base;
                    let slot = self.base + frame.return_reg as usize;
                    self.registers[slot] = rv;
                    self.clear_arg_window(&frame);
                    pc = frame.return_pc;
                }
                Op::ReturnNothing => {
                    let frame = call_stack.pop().ok_or("vm: return with no caller")?;
                    self.iter_stack.truncate(frame.iter_depth);
                    self.registers.truncate(frame.restore_len);
                    self.base = frame.caller_base;
                    let slot = self.base + frame.return_reg as usize;
                    self.registers[slot] = Value::nothing();
                    self.clear_arg_window(&frame);
                    pc = frame.return_pc;
                }
                Op::NewList { dst, start, count } => {
                    let mut items = Vec::with_capacity(count as usize);
                    for k in 0..count {
                        items.push(self.reg(start + k).clone());
                    }
                    self.set(dst, Value::list(items));
                    pc += 1;
                }
                Op::NewEmptyList { dst } => {
                    // Allocation reuse: if `dst` already holds a SOLE-OWNED
                    // Ints list (e.g. the previous loop iteration's, now dead),
                    // clear it in place and reuse its buffer/capacity instead
                    // of allocating a fresh Rc + Vec. Sound: refcount 1 means
                    // no other holder can observe the clear.
                    use crate::interpreter::{ListRepr, RuntimeValue};
                    use std::rc::Rc;
                    let di = self.base + dst as usize;
                    let reused = matches!(
                        self.registers.get(di).map(|v| v.as_runtime()).as_deref(),
                        Some(RuntimeValue::List(rc))
                            if Rc::strong_count(rc) == 1
                                && Rc::weak_count(rc) == 0
                                && matches!(&*rc.borrow(), ListRepr::Ints(_))
                    );
                    if reused {
                        if let RuntimeValue::List(rc) = &*self.registers[di].as_runtime() {
                            if let ListRepr::Ints(buf) = &mut *rc.borrow_mut() {
                                buf.clear();
                            }
                        }
                    } else {
                        self.set(dst, Value::empty_list());
                    }
                    pc += 1;
                }
                Op::NewEmptyListI32 { dst } => {
                    // Mirror NewEmptyList's allocation reuse, but for the
                    // half-width `IntsI32` repr (the narrowing-proven buffer).
                    use crate::interpreter::{ListRepr, RuntimeValue};
                    use std::rc::Rc;
                    let di = self.base + dst as usize;
                    let reused = matches!(
                        self.registers.get(di).map(|v| v.as_runtime()).as_deref(),
                        Some(RuntimeValue::List(rc))
                            if Rc::strong_count(rc) == 1
                                && Rc::weak_count(rc) == 0
                                && matches!(&*rc.borrow(), ListRepr::IntsI32(_))
                    );
                    if reused {
                        if let RuntimeValue::List(rc) = &*self.registers[di].as_runtime() {
                            if let ListRepr::IntsI32(buf) = &mut *rc.borrow_mut() {
                                buf.clear();
                            }
                        }
                    } else {
                        self.set(dst, Value::empty_list_i32());
                    }
                    pc += 1;
                }
                Op::NewEmptySet { dst } => { self.set(dst, Value::empty_set()); pc += 1; }
                Op::NewEmptyMap { dst } => { self.set(dst, Value::empty_map()); pc += 1; }
                Op::NewRange { dst, start, end } => {
                    let (lo, hi) = match (self.reg(start).as_int(), self.reg(end).as_int()) {
                        (Some(lo), Some(hi)) => (lo, hi),
                        _ => return Err("Range requires Int bounds".to_string()),
                    };
                    self.set(dst, Value::int_range(lo, hi));
                    pc += 1;
                }
                Op::ListPush { list, value } => {
                    let v = self.reg(value).clone();
                    self.ensure_reg_owned(list, call_stack.last().map(|f| f.func));
                    self.reg(list).list_push(v)?;
                    pc += 1;
                }
                Op::SetAdd { set, value } => {
                    let v = self.reg(value).clone();
                    self.ensure_reg_owned(set, call_stack.last().map(|f| f.func));
                    self.reg(set).set_add(v)?;
                    pc += 1;
                }
                Op::RemoveFrom { collection, value } => {
                    self.ensure_reg_owned(collection, call_stack.last().map(|f| f.func));
                    self.reg(collection).remove_from(self.reg(value))?;
                    pc += 1;
                }
                Op::SetIndex { collection, index, value }
                | Op::SetIndexUnchecked { collection, index, value } => {
                    use crate::interpreter::RuntimeValue;
                    // Struct field set via index syntax (`Set item "f" of s to
                    // v`) — VALUE semantics, mirroring the tree-walker: clone
                    // the struct, insert, write the new struct back.
                    let is_struct_text = matches!(
                        (&*self.reg(collection).as_runtime(), &*self.reg(index).as_runtime()),
                        (RuntimeValue::Struct(_), RuntimeValue::Text(_))
                    );
                    if is_struct_text {
                        let field = match &*self.reg(index).as_runtime() {
                            RuntimeValue::Text(t) => t.to_string(),
                            _ => unreachable!(),
                        };
                        let new_val = self.reg(value).as_runtime().clone();
                        let target = self.reg_mut(collection);
                        match target.as_runtime_mut() {
                            RuntimeValue::Struct(st) => {
                                st.fields.insert(field, new_val);
                            }
                            _ => unreachable!(),
                        }
                    } else {
                        let v = self.reg(value).clone();
                        self.ensure_reg_owned(collection, call_stack.last().map(|f| f.func));
                        self.reg(collection).index_set(self.reg(index), v)?;
                    }
                    pc += 1;
                }
                // The bytecode interpreter checks both: a sound proof makes
                // the `IndexUnchecked` check never fire, so keeping it is
                // free defense-in-depth. Only the JIT elides.
                Op::Index { dst, collection, index }
                | Op::IndexUnchecked { dst, collection, index } => {
                    let v = self.reg(collection).index_get(self.reg(index))?;
                    self.set(dst, v);
                    pc += 1;
                }
                Op::Length { dst, collection } => {
                    let n = self.reg(collection).len()?;
                    self.set(dst, Value::int(n));
                    pc += 1;
                }
                // Pure metadata for the native region tier — the interpreter's
                // checked accesses make it a no-op (the hoist it enables only
                // applies inside a compiled region, verified at region entry).
                Op::RegionBoundsGuard { .. } => {
                    pc += 1;
                }
                Op::Contains { dst, collection, value } => {
                    let b = self.reg(collection).contains(self.reg(value))?;
                    self.set(dst, Value::bool(b));
                    pc += 1;
                }
                Op::ListPushField { obj, field, src } => {
                    let field_name = match &self.program.constants[field as usize] {
                        Constant::Text(s) => s.clone(),
                        other => return Err(format!("vm: field name is not Text: {:?}", other)),
                    };
                    let val = self.reg(src).as_runtime().clone();
                    crate::semantics::collections::push_to_struct_field(
                        &self.reg(obj).as_runtime(),
                        &field_name,
                        val,
                    )?;
                    pc += 1;
                }
                Op::CheckPolicy { subject, predicate, is_capability, object, source_text } => {
                    let (registry, interner) = match self.policy_ctx {
                        Some(ctx) => ctx,
                        None => {
                            return Err(
                                "Security Check requires policies. Use compiled Rust or add ## Policy block."
                                    .to_string(),
                            );
                        }
                    };
                    let source = match &self.program.constants[source_text as usize] {
                        Constant::Text(s) => s.clone(),
                        other => return Err(format!("vm: check source is not Text: {:?}", other)),
                    };
                    let obj_val = if object != Reg::MAX {
                        Some(self.reg(object).as_runtime().clone())
                    } else {
                        None
                    };
                    crate::semantics::policy::check_policy(
                        registry,
                        interner,
                        &self.reg(subject).as_runtime(),
                        predicate,
                        is_capability,
                        obj_val.as_ref(),
                        &source,
                    )?;
                    pc += 1;
                }
                Op::FormatValue { dst, src, spec, debug_prefix } => {
                    let mut out = String::new();
                    if debug_prefix != u32::MAX {
                        match &self.program.constants[debug_prefix as usize] {
                            Constant::Text(p) => {
                                out.push_str(p);
                                out.push('=');
                            }
                            other => {
                                return Err(format!("vm: debug prefix is not Text: {:?}", other));
                            }
                        }
                    }
                    if spec != u32::MAX {
                        let spec_s = match &self.program.constants[spec as usize] {
                            Constant::Text(s) => s.as_str(),
                            other => return Err(format!("vm: format spec is not Text: {:?}", other)),
                        };
                        out.push_str(&crate::semantics::format::apply_format_spec(
                            &self.reg(src).as_runtime(),
                            spec_s,
                        ));
                    } else {
                        out.push_str(&self.reg(src).to_display_string());
                    }
                    self.set(dst, Value::text(out));
                    pc += 1;
                }
                Op::SliceOp { dst, collection, start, end } => {
                    let v = crate::semantics::collections::slice(
                        &self.reg(collection).as_runtime(),
                        &self.reg(start).as_runtime(),
                        &self.reg(end).as_runtime(),
                    )?;
                    self.set(dst, Value::from_runtime(v));
                    pc += 1;
                }
                Op::DeepClone { dst, src } => {
                    let v = self.reg(src).as_runtime().deep_clone();
                    self.set(dst, Value::from_runtime(v));
                    pc += 1;
                }
                Op::NewTuple { dst, start, count } => {
                    use crate::interpreter::RuntimeValue;
                    let mut items = Vec::with_capacity(count as usize);
                    for k in 0..count {
                        items.push(self.reg(start + k).as_runtime().clone());
                    }
                    self.set(
                        dst,
                        Value::from_runtime(RuntimeValue::Tuple(std::rc::Rc::new(items))),
                    );
                    pc += 1;
                }
                Op::UnionOp { dst, lhs, rhs } => {
                    let v = crate::semantics::collections::union(
                        &self.reg(lhs).as_runtime(),
                        &self.reg(rhs).as_runtime(),
                    )?;
                    self.set(dst, Value::from_runtime(v));
                    pc += 1;
                }
                Op::IntersectOp { dst, lhs, rhs } => {
                    let v = crate::semantics::collections::intersection(
                        &self.reg(lhs).as_runtime(),
                        &self.reg(rhs).as_runtime(),
                    )?;
                    self.set(dst, Value::from_runtime(v));
                    pc += 1;
                }
                Op::LoadToday { dst } => {
                    self.set(dst, Value::from_runtime(crate::semantics::temporal::today()));
                    pc += 1;
                }
                Op::LoadNow { dst } => {
                    self.set(dst, Value::from_runtime(crate::semantics::temporal::now()));
                    pc += 1;
                }
                Op::NewStruct { dst, type_name } => {
                    use crate::interpreter::{RuntimeValue, StructValue};
                    let name = match &self.program.constants[type_name as usize] {
                        Constant::Text(s) => s.clone(),
                        other => return Err(format!("vm: NewStruct name is not Text: {:?}", other)),
                    };
                    self.set(
                        dst,
                        Value::from_runtime(RuntimeValue::Struct(Box::new(StructValue {
                            type_name: name,
                            fields: std::collections::HashMap::new(),
                        }))),
                    );
                    pc += 1;
                }
                Op::StructInsert { obj, field, value } => {
                    use crate::interpreter::RuntimeValue;
                    let field_name = match &self.program.constants[field as usize] {
                        Constant::Text(s) => s.clone(),
                        other => return Err(format!("vm: field name is not Text: {:?}", other)),
                    };
                    let v = self.reg(value).as_runtime().clone();
                    match self.reg_mut(obj).as_runtime_mut() {
                        RuntimeValue::Struct(s) => {
                            s.fields.insert(field_name, v);
                        }
                        _ => return Err("Cannot set field on non-struct value".to_string()),
                    }
                    pc += 1;
                }
                Op::GetField { dst, obj, field } => {
                    use crate::interpreter::RuntimeValue;
                    let field_name = match &self.program.constants[field as usize] {
                        Constant::Text(s) => s.as_str(),
                        other => return Err(format!("vm: field name is not Text: {:?}", other)),
                    };
                    let v = match &*self.reg(obj).as_runtime() {
                        RuntimeValue::Struct(s) => s
                            .fields
                            .get(field_name)
                            .cloned()
                            .ok_or_else(|| format!("Field '{}' not found", field_name))?,
                        other => {
                            return Err(format!("Cannot access field on {}", other.type_name()));
                        }
                    };
                    self.set(dst, Value::from_runtime(v));
                    pc += 1;
                }
                Op::NewInductive { dst, type_name, ctor, args_start, count } => {
                    use crate::interpreter::{InductiveValue, RuntimeValue};
                    let inductive_type = match &self.program.constants[type_name as usize] {
                        Constant::Text(s) => s.clone(),
                        other => return Err(format!("vm: enum name is not Text: {:?}", other)),
                    };
                    let constructor = match &self.program.constants[ctor as usize] {
                        Constant::Text(s) => s.clone(),
                        other => return Err(format!("vm: variant name is not Text: {:?}", other)),
                    };
                    let mut args = Vec::with_capacity(count as usize);
                    for k in 0..count {
                        args.push(self.reg(args_start + k).as_runtime().clone());
                    }
                    self.set(
                        dst,
                        Value::from_runtime(RuntimeValue::Inductive(Box::new(InductiveValue {
                            inductive_type,
                            constructor,
                            args,
                        }))),
                    );
                    pc += 1;
                }
                Op::TestArm { dst, target, variant } => {
                    use crate::interpreter::RuntimeValue;
                    let variant_name = match &self.program.constants[variant as usize] {
                        Constant::Text(s) => s.as_str(),
                        other => return Err(format!("vm: variant name is not Text: {:?}", other)),
                    };
                    let matched = match &*self.reg(target).as_runtime() {
                        RuntimeValue::Struct(s) => s.type_name == variant_name,
                        RuntimeValue::Inductive(ind) => ind.constructor == variant_name,
                        _ => false,
                    };
                    self.set(dst, Value::bool(matched));
                    pc += 1;
                }
                Op::BindArm { dst, target, field, index } => {
                    use crate::interpreter::RuntimeValue;
                    let v = match &*self.reg(target).as_runtime() {
                        RuntimeValue::Struct(s) => {
                            let field_name = match &self.program.constants[field as usize] {
                                Constant::Text(s) => s.as_str(),
                                other => {
                                    return Err(format!("vm: field name is not Text: {:?}", other));
                                }
                            };
                            s.fields.get(field_name).cloned()
                        }
                        RuntimeValue::Inductive(ind) => ind.args.get(index as usize).cloned(),
                        _ => None,
                    };
                    if let Some(v) = v {
                        self.set(dst, Value::from_runtime(v));
                    }
                    pc += 1;
                }
                Op::CrdtBump { obj, field, amount, negate } => {
                    use crate::interpreter::RuntimeValue;
                    let amount_int = match &*self.reg(amount).as_runtime() {
                        RuntimeValue::Int(n) => *n,
                        _ => {
                            return Err(if negate {
                                "CRDT decrement amount must be an integer".to_string()
                            } else {
                                "CRDT increment amount must be an integer".to_string()
                            });
                        }
                    };
                    let amount_int = if negate { amount_int.wrapping_neg() } else { amount_int };
                    let field_name = match &self.program.constants[field as usize] {
                        Constant::Text(s) => s.clone(),
                        other => return Err(format!("vm: field name is not Text: {:?}", other)),
                    };
                    match self.reg_mut(obj).as_runtime_mut() {
                        RuntimeValue::Struct(s) => {
                            let current =
                                s.fields.get(&field_name).cloned().unwrap_or(RuntimeValue::Int(0));
                            let new_val = crate::semantics::arith::crdt_counter_bump(
                                current,
                                amount_int,
                                &field_name,
                            )?;
                            s.fields.insert(field_name, new_val);
                        }
                        _ => {
                            return Err(if negate {
                                "Cannot decrease field on non-struct value".to_string()
                            } else {
                                "Cannot increase field on non-struct value".to_string()
                            });
                        }
                    }
                    pc += 1;
                }
                Op::CrdtMerge { target, source } => {
                    use crate::interpreter::RuntimeValue;
                    let source_fields = match &*self.reg(source).as_runtime() {
                        RuntimeValue::Struct(s) => s.fields.clone(),
                        _ => return Err("Merge source must be a struct".to_string()),
                    };
                    match self.reg_mut(target).as_runtime_mut() {
                        RuntimeValue::Struct(s) => {
                            for (field_name, incoming) in source_fields {
                                let current =
                                    s.fields.get(&field_name).cloned().unwrap_or(RuntimeValue::Int(0));
                                let merged =
                                    crate::semantics::arith::crdt_merge_field(&current, incoming);
                                s.fields.insert(field_name, merged);
                            }
                        }
                        _ => return Err("Merge target must be a struct".to_string()),
                    }
                    pc += 1;
                }
                Op::NewCrdt { dst, kind } => {
                    use crate::interpreter::RuntimeValue;
                    use crate::semantics::crdt::{next_replica_id, CrdtValue};
                    let cv = match kind {
                        0 => CrdtValue::new_set(next_replica_id()),
                        1 => CrdtValue::new_seq(next_replica_id()),
                        3 => CrdtValue::new_set_remove_wins(next_replica_id()),
                        _ => CrdtValue::new_register(next_replica_id()),
                    };
                    self.set(
                        dst,
                        Value::from_runtime(RuntimeValue::Crdt(std::rc::Rc::new(
                            std::cell::RefCell::new(cv),
                        ))),
                    );
                    pc += 1;
                }
                Op::CrdtAppend { seq, value } => {
                    use crate::interpreter::RuntimeValue;
                    let v = self.reg(value).as_runtime().clone();
                    let seq_rt = self.reg(seq).as_runtime();
                    match &*seq_rt {
                        RuntimeValue::Crdt(rc) => rc.borrow_mut().append(&v)?,
                        RuntimeValue::List(_) => {
                            crate::semantics::collections::list_push(&seq_rt, v)?
                        }
                        other => return Err(format!("Cannot append to {}", other.type_name())),
                    }
                    pc += 1;
                }
                Op::CrdtResolve { obj, field, value } => {
                    use crate::interpreter::RuntimeValue;
                    let v = self.reg(value).as_runtime().clone();
                    let field_name = match &self.program.constants[field as usize] {
                        Constant::Text(s) => s.clone(),
                        other => return Err(format!("vm: field name is not Text: {:?}", other)),
                    };
                    match self.reg_mut(obj).as_runtime_mut() {
                        RuntimeValue::Struct(s) => {
                            // A real divergent register resolves in place via its shared
                            // `Rc`; a plain field is overwritten — same fallback as the
                            // tree-walker's `Resolve`.
                            let is_register =
                                matches!(s.fields.get(&field_name), Some(RuntimeValue::Crdt(_)));
                            if is_register {
                                if let Some(RuntimeValue::Crdt(rc)) = s.fields.get(&field_name) {
                                    rc.borrow_mut().resolve(&v)?;
                                }
                            } else {
                                s.fields.insert(field_name, v);
                            }
                        }
                        other => {
                            return Err(format!("Cannot resolve a field on {}", other.type_name()))
                        }
                    }
                    pc += 1;
                }
                Op::IterPrepare { iterable } => {
                    let items = crate::semantics::collections::iteration_snapshot(
                        &self.reg(iterable).as_runtime(),
                    )?;
                    self.iter_stack
                        .push((items.into_iter().map(Value::from_runtime).collect(), 0));
                    pc += 1;
                }
                Op::IterNext { dst, exit } => {
                    let (items, idx) = self
                        .iter_stack
                        .last_mut()
                        .ok_or("vm: IterNext with no live iterator")?;
                    if *idx < items.len() {
                        let v = items[*idx].clone();
                        *idx += 1;
                        self.set(dst, v);
                        pc += 1;
                    } else {
                        pc = exit;
                    }
                }
                Op::IterPop => {
                    self.iter_stack.pop().ok_or("vm: IterPop with no live iterator")?;
                    pc += 1;
                }
                Op::ListPop { list, dst } => {
                    self.ensure_reg_owned(list, call_stack.last().map(|f| f.func));
                    let v = crate::semantics::collections::list_pop(&self.reg(list).as_runtime())?;
                    self.set(dst, Value::from_runtime(v));
                    pc += 1;
                }
                Op::Sleep { duration } => {
                    // A VM `Sleep` only ever runs inside a task (a non-concurrent program
                    // with `Sleep` routes to the async tree-walker, never the VM). Route it
                    // through the scheduler's virtual timer — the same logical-tick scale as
                    // a `Select` `After` arm — by yielding `VmBlock::Sleep`. Blocking on a
                    // real host timer here would stall the cooperative scheduler (and errors
                    // outright on wasm).
                    let ticks = self.as_ticks(duration)?;
                    if ticks > 0 {
                        return Ok(self.block(pc + 1, call_stack, VmBlock::Sleep(ticks), None));
                    }
                    pc += 1;
                }
                Op::DestructureTuple { src, start, count } => {
                    use crate::interpreter::RuntimeValue;
                    match &*self.reg(src).as_runtime() {
                        RuntimeValue::Tuple(items) => {
                            // Arity is LOUD — a silent truncation binds ghosts.
                            if items.len() != count as usize {
                                return Err(format!(
                                    "Cannot bind a {}-tuple to {} names",
                                    items.len(),
                                    count
                                ));
                            }
                            let items: Vec<Value> = items
                                .iter()
                                .take(count as usize)
                                .cloned()
                                .map(Value::from_runtime)
                                .collect();
                            for (i, v) in items.into_iter().enumerate() {
                                self.set(start + i as Reg, v);
                            }
                        }
                        other => {
                            return Err(format!(
                                "Expected tuple for pattern, got {}",
                                other.type_name()
                            ));
                        }
                    }
                    pc += 1;
                }
                Op::Show { src } => {
                    self.lines.push(self.reg(src).to_display_string());
                    pc += 1;
                }
                Op::Args { dst } => {
                    use crate::interpreter::RuntimeValue;
                    let items: Vec<RuntimeValue> = self
                        .program_args
                        .iter()
                        .map(|s| RuntimeValue::Text(std::rc::Rc::new(s.clone())))
                        .collect();
                    let list = RuntimeValue::List(std::rc::Rc::new(std::cell::RefCell::new(
                        crate::interpreter::ListRepr::from_values(items),
                    )));
                    self.set(dst, Value::from_runtime(list));
                    pc += 1;
                }
                // Go-like concurrency (T11). Each op materialises its operands,
                // then suspends the slice — `self.block` saves the resume point
                // and the request; the scheduler driver services it and re-enters.
                Op::ChanNew { dst, cap, .. } => {
                    let capacity = if cap < 0 { None } else { Some(cap as usize) };
                    return Ok(self.block(pc + 1, call_stack, VmBlock::NewChan(capacity), Some(dst)));
                }
                Op::ChanSend { chan, val } => {
                    let ch = self.as_chan(chan)?;
                    let payload = self.materialize_reg(val)?;
                    return Ok(self.block(pc + 1, call_stack, VmBlock::Send(ch, payload), None));
                }
                Op::ChanRecv { dst, chan } => {
                    let ch = self.as_chan(chan)?;
                    return Ok(self.block(pc + 1, call_stack, VmBlock::Recv(ch), Some(dst)));
                }
                Op::ChanTrySend { dst, chan, val } => {
                    let ch = self.as_chan(chan)?;
                    let payload = self.materialize_reg(val)?;
                    return Ok(self.block(pc + 1, call_stack, VmBlock::TrySend(ch, payload), Some(dst)));
                }
                Op::ChanTryRecv { dst, chan } => {
                    let ch = self.as_chan(chan)?;
                    return Ok(self.block(pc + 1, call_stack, VmBlock::TryRecv(ch), Some(dst)));
                }
                Op::ChanClose { chan } => {
                    let ch = self.as_chan(chan)?;
                    return Ok(self.block(pc + 1, call_stack, VmBlock::Close(ch), None));
                }
                Op::TaskAwait { dst, handle } => {
                    let tid = self.as_task(handle)?;
                    return Ok(self.block(pc + 1, call_stack, VmBlock::Await(tid), Some(dst)));
                }
                Op::TaskAbort { handle } => {
                    let tid = self.as_task(handle)?;
                    return Ok(self.block(pc + 1, call_stack, VmBlock::Abort(tid), None));
                }
                Op::Spawn { func, args_start, arg_count } => {
                    let args = self.materialize_args(args_start, arg_count)?;
                    let req = VmBlock::SpawnDesc { func, args, want_handle: false };
                    return Ok(self.block(pc + 1, call_stack, req, None));
                }
                Op::SpawnHandle { dst, func, args_start, arg_count } => {
                    let args = self.materialize_args(args_start, arg_count)?;
                    let req = VmBlock::SpawnDesc { func, args, want_handle: true };
                    return Ok(self.block(pc + 1, call_stack, req, Some(dst)));
                }
                Op::SelectArmRecv { chan, var } => {
                    let ch = self.as_chan(chan)?;
                    self.select_pending.push((SelectArm::Recv(ch), Some(var)));
                    pc += 1;
                }
                Op::SelectArmTimeout { ticks } => {
                    let t = self.as_ticks(ticks)?;
                    self.select_pending.push((SelectArm::Timeout(t), None));
                    pc += 1;
                }
                Op::SelectWait { dst_arm } => {
                    let arms: Vec<SelectArm> =
                        self.select_pending.iter().map(|(a, _)| a.clone()).collect();
                    return Ok(self.block(pc + 1, call_stack, VmBlock::Select(arms), Some(dst_arm)));
                }
                // Peer networking: materialise the operands and suspend; the async VM driver
                // services the request through the shared `NetInbox` (the same inbox the
                // tree-walker uses) and resumes — `NetAwait` resumes with the received value.
                Op::NetConnect { url } => {
                    let u = self.materialize_reg(url)?;
                    return Ok(self.block(pc + 1, call_stack, VmBlock::NetConnect(u), None));
                }
                Op::NetListen { topic } => {
                    let t = self.materialize_reg(topic)?;
                    return Ok(self.block(pc + 1, call_stack, VmBlock::NetListen(t), None));
                }
                Op::NetSend { to, msg } => {
                    let t = self.materialize_reg(to)?;
                    let m = self.materialize_reg(msg)?;
                    return Ok(self.block(pc + 1, call_stack, VmBlock::NetSend(t, m), None));
                }
                Op::NetStream { to, values } => {
                    let t = self.materialize_reg(to)?;
                    let v = self.materialize_reg(values)?;
                    return Ok(self.block(pc + 1, call_stack, VmBlock::NetStream(t, v), None));
                }
                Op::NetAwait { dst, from, stream } => {
                    let f = self.materialize_reg(from)?;
                    return Ok(self.block(pc + 1, call_stack, VmBlock::NetAwait(f, stream), Some(dst)));
                }
                Op::NetMakePeer { dst, addr } => {
                    let a = self.materialize_reg(addr)?;
                    return Ok(self.block(pc + 1, call_stack, VmBlock::NetMakePeer(a), Some(dst)));
                }
                Op::NetSync { dst, topic } => {
                    let current = self.materialize_reg(dst)?;
                    let t = self.materialize_reg(topic)?;
                    return Ok(self.block(pc + 1, call_stack, VmBlock::NetSync(t, current), Some(dst)));
                }
                Op::FailWith { msg } => {
                    return Err(match &self.program.constants[msg as usize] {
                        Constant::Text(s) => s.clone(),
                        other => format!("vm: FailWith constant is not Text: {:?}", other),
                    });
                }
                Op::Halt => break,
            }
        }
        Ok(VmStep::Done(crate::interpreter::RuntimeValue::Nothing))
    }

    #[inline]
    fn reg(&self, r: Reg) -> &Value {
        &self.registers[self.base + r as usize]
    }

    #[inline]
    fn set(&mut self, r: Reg, v: Value) {
        let slot = self.base + r as usize;
        self.registers[slot] = v;
    }

    #[inline]
    fn reg_mut(&mut self, r: Reg) -> &mut Value {
        let slot = self.base + r as usize;
        &mut self.registers[slot]
    }

    // ─── Scheduler-driver hooks (T11) ───────────────────────────────────────

    /// Save the suspended `pc` + call stack and the request; the slice returns
    /// [`VmStep::Blocked`]. `slot` is the register the resume value lands in.
    fn block(
        &mut self,
        resume_pc: usize,
        call_stack: Vec<CallFrame>,
        req: VmBlock,
        slot: Option<Reg>,
    ) -> VmStep {
        self.sched_pc = resume_pc;
        self.sched_call_stack = call_stack;
        self.sched_active = true;
        self.pending = Some(req);
        self.resume_slot = slot;
        VmStep::Blocked
    }

    // ─── Debug-drawer hooks (single-task, bytecode tier) ─────────────────────

    /// Build a [`DebugView`] of the paused VM: the call frames with their register
    /// values, the globals, the output, and the program counter (the op about to
    /// execute). Reconstructs each frame's register window from the saved call
    /// stack — Main has base 0; frame `k`'s base is the next frame's `caller_base`
    /// (the innermost is the live `self.base`).
    pub(crate) fn debug_view(&self) -> DebugView {
        let prog = self.program;
        let cs = &self.sched_call_stack;
        let mut frames = Vec::with_capacity(cs.len() + 1);
        frames.push(self.frame_view(None, 0, prog.register_count));
        for (k, fr) in cs.iter().enumerate() {
            let base = cs.get(k + 1).map(|n| n.caller_base).unwrap_or(self.base);
            let count = prog.functions.get(fr.func as usize).map(|f| f.register_count).unwrap_or(0);
            frames.push(self.frame_view(Some(fr.func), base, count));
        }
        let globals = prog
            .globals
            .iter()
            .enumerate()
            .filter_map(|(i, name)| {
                self.globals
                    .get(i)
                    .and_then(|o| o.as_ref())
                    .map(|v| (name.clone(), v.to_display_string()))
            })
            .collect();
        DebugView {
            pc: self.sched_pc,
            current_func: cs.last().map(|f| f.func),
            frames,
            globals,
            output: self.lines.clone(),
        }
    }

    fn frame_view(&self, func: Option<u16>, base: usize, count: usize) -> DebugFrameView {
        let registers = (0..count)
            .filter_map(|i| {
                self.registers.get(base + i).map(|v| {
                    // `as_runtime` (not `as_runtime_ref`) so inline scalars report their
                    // type too — under the narrow-value repr `as_runtime_ref` is `None` for
                    // an inline Int/Float/Bool, but the type is still well-defined.
                    let kind = v.as_runtime().type_name().to_string();
                    (i as u16, kind, v.to_display_string())
                })
            })
            .collect();
        DebugFrameView { func, base, registers }
    }

    /// Walk the roots (the current frame's registers + the globals) and collect the
    /// distinct heap objects they reach, recording each object's reference count and
    /// every root that points at it — so a shared list shows up once with two
    /// referrers (the `let b be a` aliasing that trips up every beginner).
    pub(crate) fn debug_heap(&self) -> Vec<HeapObjView> {
        let prog = self.program;
        let (count, is_main) = match self.sched_call_stack.last() {
            Some(fr) => (
                prog.functions.get(fr.func as usize).map(|f| f.register_count).unwrap_or(0),
                false,
            ),
            None => (prog.register_count, true),
        };
        let name_of = |i: usize| -> String {
            if is_main {
                prog.reg_names
                    .iter()
                    .find(|(r, _)| *r as usize == i)
                    .map(|(_, n)| n.clone())
                    .unwrap_or_else(|| format!("R{i}"))
            } else {
                format!("R{i}")
            }
        };
        let mut objs: Vec<HeapObjView> = Vec::new();
        let mut add = |v: &Value, root: String, objs: &mut Vec<HeapObjView>| {
            if let Some((id, kind, rc, storage)) = heap_identity(v) {
                match objs.iter_mut().find(|o| o.id == id) {
                    Some(o) => {
                        if !o.referenced_by.contains(&root) {
                            o.referenced_by.push(root);
                        }
                    }
                    None => objs.push(HeapObjView {
                        id,
                        kind,
                        summary: v.to_display_string(),
                        storage,
                        rc,
                        referenced_by: vec![root],
                    }),
                }
            }
        };
        for i in 0..count {
            if let Some(v) = self.registers.get(self.base + i) {
                add(v, name_of(i), &mut objs);
            }
        }
        for (i, name) in prog.globals.iter().enumerate() {
            if let Some(Some(v)) = self.globals.get(i) {
                add(v, name.clone(), &mut objs);
            }
        }
        objs
    }

    /// Clone the resumable execution state so the debugger can carry it between
    /// steps (it rebuilds the VM each step — see [`DebugVmState`]).
    pub(crate) fn save_debug_state(&self) -> DebugVmState {
        DebugVmState {
            registers: self.registers.clone(),
            base: self.base,
            globals: self.globals.clone(),
            lines: self.lines.clone(),
            iter_stack: self.iter_stack.clone(),
            sched_active: self.sched_active,
            sched_pc: self.sched_pc,
            sched_call_stack: self.sched_call_stack.clone(),
        }
    }

    /// Restore a snapshot taken by [`Vm::save_debug_state`].
    pub(crate) fn restore_debug_state(&mut self, st: DebugVmState) {
        self.registers = st.registers;
        self.base = st.base;
        self.globals = st.globals;
        self.lines = st.lines;
        self.iter_stack = st.iter_stack;
        self.sched_active = st.sched_active;
        self.sched_pc = st.sched_pc;
        self.sched_call_stack = st.sched_call_stack;
    }

    /// Take the pending concurrency request (the driver services it).
    pub(crate) fn take_pending(&mut self) -> Option<VmBlock> {
        self.pending.take()
    }

    /// Deliver a resume value into the slot the last block reserved (if any).
    pub(crate) fn deliver_resume(&mut self, value: Value) {
        if let Some(slot) = self.resume_slot.take() {
            self.set(slot, value);
        }
    }

    /// Deliver a resolved `Select`: the received value (when a recv arm won) into
    /// that arm's var register, and the winning arm index into the `SelectWait`'s
    /// destination register.
    pub(crate) fn deliver_select(&mut self, arm: usize, value: Value) {
        let var = self.select_pending.get(arm).and_then(|(_, v)| *v);
        if let Some(reg) = var {
            self.set(reg, value);
        }
        if let Some(slot) = self.resume_slot.take() {
            self.set(slot, Value::from_runtime(crate::interpreter::RuntimeValue::Int(arm as i64)));
        }
        self.select_pending.clear();
    }

    /// Read a select-timeout register as a non-negative logical tick count.
    fn as_ticks(&self, r: Reg) -> Result<u64, String> {
        use crate::interpreter::RuntimeValue;
        Ok(match &*self.reg(r).as_runtime() {
            RuntimeValue::Int(n) => (*n).max(0) as u64,
            RuntimeValue::Duration(d) => (*d).max(0) as u64,
            RuntimeValue::Span { months, days } => {
                (((*months as i64) * 30 + *days as i64) * 86_400).max(0) as u64
            }
            other => {
                return Err(format!(
                    "select timeout must be a number or duration, found {}",
                    other.type_name()
                ))
            }
        })
    }

    /// Read a channel handle from register `r`.
    fn as_chan(&self, r: Reg) -> Result<ChanId, String> {
        match &*self.reg(r).as_runtime() {
            crate::interpreter::RuntimeValue::Chan(id) => Ok(*id),
            other => Err(format!("expected a channel, found {}", other.type_name())),
        }
    }

    /// Read a task handle from register `r`.
    fn as_task(&self, r: Reg) -> Result<TaskId, String> {
        match &*self.reg(r).as_runtime() {
            crate::interpreter::RuntimeValue::TaskHandle(id) => Ok(*id),
            other => Err(format!("expected a task handle, found {}", other.type_name())),
        }
    }

    /// Materialise register `r`'s value into a Send-able payload for a channel.
    fn materialize_reg(&self, r: Reg) -> Result<RtPayload, String> {
        let rt = self.reg(r).as_runtime();
        crate::concurrency::marshal::materialize(&rt)
            .map_err(|e| format!("cannot send value through a channel: {:?}", e))
    }

    /// Materialise a contiguous register window `[args_start, args_start+count)`
    /// into `Send`-able payloads — the spawn arguments that cross to the child
    /// task (and, under work-stealing, to its worker thread).
    fn materialize_args(&self, args_start: Reg, arg_count: u16) -> Result<Vec<RtPayload>, String> {
        (0..arg_count as Reg).map(|i| self.materialize_reg(args_start + i)).collect()
    }

    /// Install the spawn entry-state for `functions[func](args)` into THIS VM:
    /// rebuild the payload args into a base-0 register window, push a sentinel
    /// frame so the body's `Return` terminates the task cleanly (result in
    /// register 0), and arm the scheduler `pc` at the function's `entry_pc`.
    ///
    /// Shared by both drivers: the cooperative one calls it on a freshly-cloned
    /// child (see [`Vm::spawn_task_vm`]); a work-stealing worker calls it on a VM
    /// built locally over its own borrow of the program.
    pub(crate) fn setup_task(&mut self, func: FuncIdx, args: &[RtPayload]) {
        let (entry_pc, reg_count) = {
            let f = &self.program.functions[func as usize];
            (f.entry_pc, f.register_count)
        };
        if self.registers.len() < reg_count {
            self.registers.resize(reg_count, Value::nothing());
        }
        for (i, a) in args.iter().enumerate() {
            self.registers[i] = Value::from_runtime(crate::concurrency::marshal::rebuild(a.clone()));
        }
        let restore_len = self.registers.len();
        self.sched_call_stack = vec![CallFrame {
            return_pc: self.program.code.len(),
            return_reg: 0,
            caller_base: 0,
            restore_len,
            iter_depth: 0,
            func,
            arg_lo: 0,
            arg_count: 0,
        }];
        self.sched_active = true;
        self.sched_pc = entry_pc;
    }

    /// Build a fresh child VM that runs `functions[func](args)` — a spawned task —
    /// sharing this VM's `&'p program` and run context (tier, policy, program
    /// args). Used by the cooperative driver, which builds children inline.
    pub(crate) fn spawn_task_vm(&self, func: FuncIdx, args: &[RtPayload]) -> Vm<'p> {
        let mut child = Vm::new(self.program);
        child.policy_ctx = self.policy_ctx;
        child.tier = self.tier;
        child.program_args = self.program_args.clone();
        child.setup_task(func, args);
        child
    }

    fn binop(
        &mut self,
        dst: Reg,
        lhs: Reg,
        rhs: Reg,
        f: impl Fn(&Value, &Value) -> Result<Value, String>,
    ) -> Result<(), String> {
        let v = f(self.reg(lhs), self.reg(rhs))?;
        self.set(dst, v);
        Ok(())
    }

    /// `R[dst] = R[dst] + R[src]`, extending in place when `R[dst]` is a
    /// Text this register exclusively owns (`Rc` count 1 — no alias, capture
    /// snapshot, or iterator can observe the mutation). The two in-place arms
    /// transcribe the kernel's `(Text, Text)` / `(Text, other)` add rules;
    /// every other shape — shared Rc included — takes the kernel itself.
    fn add_assign(&mut self, dst: Reg, src: Reg) -> Result<(), String> {
        use crate::interpreter::RuntimeValue;
        use std::rc::Rc;
        let di = self.base + dst as usize;
        let si = self.base + src as usize;
        if di != si {
            let (a, b) = if di < si {
                let (lo, hi) = self.registers.split_at_mut(si);
                (&mut lo[di], &hi[0])
            } else {
                let (lo, hi) = self.registers.split_at_mut(di);
                (&mut hi[0], &lo[si])
            };
            // Only the heap Text arm takes the in-place fast path; peek without
            // promoting (`as_runtime_mut` would box an inline scalar) so the
            // common non-Text case stays inline and falls to the kernel below.
            if matches!(a.as_runtime_ref(), Some(RuntimeValue::Text(_))) {
                if let RuntimeValue::Text(rc) = a.as_runtime_mut() {
                    if let Some(s) = Rc::get_mut(rc) {
                        match &*b.as_runtime() {
                            RuntimeValue::Text(t) => s.push_str(t),
                            other => s.push_str(&other.to_display_string()),
                        }
                        return Ok(());
                    }
                }
            }
        }
        let v = self.reg(dst).add(self.reg(src))?;
        self.set(dst, v);
        Ok(())
    }
}

fn const_to_value(c: &Constant) -> Value {
    use crate::interpreter::RuntimeValue;
    match c {
        Constant::Int(n) => Value::int(*n),
        Constant::Float(f) => Value::float(*f),
        Constant::Bool(b) => Value::bool(*b),
        Constant::Text(s) => Value::text(s.clone()),
        Constant::Char(c) => Value::from_runtime(RuntimeValue::Char(*c)),
        Constant::Nothing => Value::nothing(),
        Constant::Duration(n) => Value::from_runtime(RuntimeValue::Duration(*n)),
        Constant::Date(d) => Value::from_runtime(RuntimeValue::Date(*d)),
        Constant::Moment(n) => Value::from_runtime(RuntimeValue::Moment(*n)),
        Constant::Span { months, days } => {
            Value::from_runtime(RuntimeValue::Span { months: *months, days: *days })
        }
        Constant::Time(n) => Value::from_runtime(RuntimeValue::Time(*n)),
    }
}

#[cfg(test)]
mod string_build_fastpath {
    //! Structural proof for Task B: the constant pool is materialised ONCE
    //! (a `LoadConst` of a Text bumps a shared `Rc` instead of allocating a
    //! fresh `String`), and the sole-owned in-place append (`AddAssign`)
    //! grows the accumulator's own buffer rather than reallocating each step.
    use super::*;
    use crate::interpreter::RuntimeValue;
    use std::rc::Rc;

    /// The `Rc<String>` backing register `r` (or panic if it isn't a Text).
    fn text_ptr(vm: &Vm, r: usize) -> *const String {
        match vm.registers[r].as_runtime_ref() {
            Some(RuntimeValue::Text(rc)) => Rc::as_ptr(rc),
            other => panic!("register {r} is not a Text: {other:?}"),
        }
    }

    fn text_strong(vm: &Vm, r: usize) -> usize {
        match vm.registers[r].as_runtime_ref() {
            Some(RuntimeValue::Text(rc)) => Rc::strong_count(rc),
            other => panic!("register {r} is not a Text: {other:?}"),
        }
    }

    /// Two `LoadConst`s of the SAME Text-constant index hand out the same
    /// `Rc` allocation — proof the pool is materialised once, so reloading a
    /// literal in a loop is a refcount bump, not a `String` allocation.
    #[test]
    fn loadconst_text_shares_one_allocation() {
        let prog = CompiledProgram {
            constants: vec![Constant::Text("abc".to_string())],
            code: vec![
                Op::LoadConst { dst: 0, idx: 0 },
                Op::LoadConst { dst: 1, idx: 0 },
                Op::Halt,
            ],
            register_count: 2,
            ..Default::default()
        };
        let mut vm = Vm::new(&prog);
        vm.run().unwrap();
        assert_eq!(
            text_ptr(&vm, 0),
            text_ptr(&vm, 1),
            "two loads of the same Text constant must share one Rc allocation"
        );
        // And the live constant keeps a reference, so a register's clone is
        // never the sole owner — an in-place append on a freshly-loaded
        // literal must therefore NOT fire (it would corrupt the pool).
        assert!(text_strong(&vm, 0) >= 3, "pool + two registers all reference the constant");
    }

    /// `Set s to s + ch` repeated: once the accumulator owns its buffer, each
    /// append reuses the SAME allocation (pointer stable while capacity holds).
    /// The first append, where `s` is the just-loaded shared `""` constant,
    /// must allocate a fresh owned buffer (the pool stays intact); subsequent
    /// appends grow it in place.
    #[test]
    fn add_assign_appends_in_place_after_first() {
        // r0 = "" (shared constant), r1 = "x" (shared constant).
        // r0 += r1, capture; r0 += r1 a few more times, pointer stays put.
        let prog = CompiledProgram {
            constants: vec![Constant::Text(String::new()), Constant::Text("x".to_string())],
            code: vec![
                Op::LoadConst { dst: 0, idx: 0 },
                Op::LoadConst { dst: 1, idx: 1 },
                Op::AddAssign { dst: 0, src: 1 },
                Op::AddAssign { dst: 0, src: 1 },
                Op::AddAssign { dst: 0, src: 1 },
                Op::AddAssign { dst: 0, src: 1 },
                Op::Halt,
            ],
            register_count: 2,
            ..Default::default()
        };
        // Run only the first two appends to capture the owned buffer, then the
        // rest, by stepping the whole thing and re-checking — simplest: run to
        // completion, the invariant we assert is the FINAL value's correctness
        // plus that the rhs constant was never mutated.
        let mut vm = Vm::new(&prog);
        vm.run().unwrap();
        // The accumulator built "xxxx".
        match vm.registers[0].as_runtime_ref() {
            Some(RuntimeValue::Text(rc)) => assert_eq!(&***rc, "xxxx"),
            other => panic!("r0 not text: {other:?}"),
        }
        // The shared constant "x" was NOT corrupted by the in-place appends.
        match vm.registers[1].as_runtime_ref() {
            Some(RuntimeValue::Text(rc)) => assert_eq!(&***rc, "x"),
            other => panic!("r1 not text: {other:?}"),
        }
    }

    /// The buffer-stability proof: drive the loop manually so we can read the
    /// accumulator's `Rc::as_ptr` between appends. After the first append
    /// (which clones off the shared constant into an owned buffer with spare
    /// capacity), every later append keeps the SAME allocation.
    #[test]
    fn add_assign_reuses_buffer_allocation() {
        let prog = CompiledProgram {
            constants: vec![Constant::Text("seed-with-capacity-headroom".to_string()), Constant::Text("z".to_string())],
            code: vec![
                Op::LoadConst { dst: 0, idx: 0 },
                Op::LoadConst { dst: 1, idx: 1 },
                Op::AddAssign { dst: 0, src: 1 }, // first: clone off the shared constant
                Op::AddAssign { dst: 0, src: 1 }, // owned now → in place
                Op::AddAssign { dst: 0, src: 1 },
                Op::Halt,
            ],
            register_count: 2,
            ..Default::default()
        };
        // Manually dispatch up to the marker so we can inspect between appends.
        // Reserve generous capacity on the first owned buffer so growth never
        // forces a realloc within this test.
        let mut vm = Vm::new(&prog);
        // Step 1+2: loads.
        vm.set(0, const_to_value(&prog.constants[0]));
        vm.set(1, const_to_value(&prog.constants[1]));
        // First append: must produce an OWNED buffer (sole owner now).
        vm.add_assign(0, 1).unwrap();
        // Force headroom so the next appends don't realloc for capacity.
        if let RuntimeValue::Text(rc) = vm.registers[0].as_runtime_mut() {
            if let Some(s) = Rc::get_mut(rc) {
                s.reserve(64);
            }
        }
        let p_after_first = text_ptr(&vm, 0);
        vm.add_assign(0, 1).unwrap();
        let p_after_second = text_ptr(&vm, 0);
        vm.add_assign(0, 1).unwrap();
        let p_after_third = text_ptr(&vm, 0);
        assert_eq!(p_after_first, p_after_second, "append must reuse the owned buffer");
        assert_eq!(p_after_second, p_after_third, "append must reuse the owned buffer");
        match vm.registers[0].as_runtime_ref() {
            Some(RuntimeValue::Text(rc)) => assert_eq!(&***rc, "seed-with-capacity-headroomzzz"),
            other => panic!("r0 not text: {other:?}"),
        }
    }
}

#[cfg(test)]
mod debug_stepping {
    //! The debug stepper (`STEPPED = true`): `run_steps` advances exactly one op
    //! per call, yields `Paused` between ops, exposes the paused state via
    //! `debug_view`, and — the soundness invariant — produces output BYTE-IDENTICAL
    //! to a single-shot `run()`. This is the contract the Studio debug drawer rides.
    use super::*;

    /// `Let x be 6. Let y be 7. Show x times y.` hand-lowered to bytecode.
    fn mul_program() -> CompiledProgram {
        CompiledProgram {
            constants: vec![Constant::Int(6), Constant::Int(7)],
            code: vec![
                Op::LoadConst { dst: 0, idx: 0 },
                Op::LoadConst { dst: 1, idx: 1 },
                Op::Mul { dst: 2, lhs: 0, rhs: 1 },
                Op::Show { src: 2 },
                Op::Halt,
            ],
            register_count: 3,
            ..Default::default()
        }
    }

    #[test]
    fn run_steps_advances_one_op_and_pauses() {
        let prog = mul_program();
        let mut vm = Vm::new(&prog);
        // First op (LoadConst R0 = 6) → paused at pc 1 with R0 visible.
        assert!(matches!(vm.run_steps(1).unwrap(), VmStep::Paused));
        let v = vm.debug_view();
        assert_eq!(v.pc, 1, "stopped before the second instruction");
        assert_eq!(v.frames.len(), 1, "single Main frame");
        assert_eq!(v.frames[0].registers[0], (0u16, "Int".to_string(), "6".to_string()));
        // Second op (LoadConst R1 = 7).
        assert!(matches!(vm.run_steps(1).unwrap(), VmStep::Paused));
        let v = vm.debug_view();
        assert_eq!(v.pc, 2);
        assert_eq!(v.frames[0].registers[1], (1u16, "Int".to_string(), "7".to_string()));
        // Third op (Mul R2 = R0 * R1).
        assert!(matches!(vm.run_steps(1).unwrap(), VmStep::Paused));
        assert_eq!(vm.debug_view().frames[0].registers[2], (2u16, "Int".to_string(), "42".to_string()));
    }

    #[test]
    fn stepped_run_is_byte_identical_to_single_shot() {
        let prog = mul_program();

        // Stepped to completion, one op at a time.
        let mut stepper = Vm::new(&prog);
        let mut pauses = 0usize;
        loop {
            match stepper.run_steps(1).unwrap() {
                VmStep::Paused => pauses += 1,
                VmStep::Done(_) => break,
                VmStep::Blocked => unreachable!("no concurrency op in this program"),
            }
        }
        let stepped_out = stepper.into_lines();

        // Single-shot run of the very same program.
        let mut oneshot = Vm::new(&prog);
        oneshot.run().unwrap();
        let oneshot_out = oneshot.into_lines();

        assert_eq!(stepped_out, oneshot_out, "stepping must not change observable output");
        assert_eq!(stepped_out, vec!["42".to_string()]);
        assert_eq!(pauses, 4, "one pause after each of the 4 ops before Halt");
    }

    #[test]
    fn larger_budget_runs_several_ops_then_pauses() {
        let prog = mul_program();
        let mut vm = Vm::new(&prog);
        // Budget of 3 runs the two loads + the Mul, then pauses at the Show (pc 3).
        assert!(matches!(vm.run_steps(3).unwrap(), VmStep::Paused));
        let v = vm.debug_view();
        assert_eq!(v.pc, 3);
        assert_eq!(v.frames[0].registers[2], (2u16, "Int".to_string(), "42".to_string()));
        // The rest completes.
        assert!(matches!(vm.run_steps(u64::MAX).unwrap(), VmStep::Done(_)));
        assert_eq!(vm.into_lines(), vec!["42".to_string()]);
    }
}