shape-jit 0.3.2

Tiered JIT compiler (Cranelift) for the Shape virtual machine
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
//! MIR Terminator → Cranelift IR compilation.
//!
//! Terminators end basic blocks: Goto (jump), SwitchBool (branch),
//! Call (function call), Return, Unreachable (trap).

use cranelift::codegen::ir::FuncRef;
use cranelift::prelude::*;

use super::MirToIR;
use shape_vm::mir::types::*;

impl<'a, 'b> MirToIR<'a, 'b> {
    /// Compile a MIR terminator.
    pub(crate) fn compile_terminator(
        &mut self,
        terminator: &Terminator,
    ) -> Result<(), String> {
        match &terminator.kind {
            TerminatorKind::Goto(target) => {
                let target_block = self.block_map.get(target).ok_or_else(|| {
                    format!("MirToIR: unknown block target {}", target)
                })?;
                self.builder.ins().jump(*target_block, &[]);
                Ok(())
            }

            TerminatorKind::SwitchBool {
                operand,
                true_bb,
                false_bb,
            } => {
                let cond_val = self.compile_operand(operand)?;

                let true_block = self.block_map.get(true_bb).ok_or_else(|| {
                    format!("MirToIR: unknown true block {}", true_bb)
                })?;
                let false_block = self.block_map.get(false_bb).ok_or_else(|| {
                    format!("MirToIR: unknown false block {}", false_bb)
                })?;

                // Convert condition to I8 bool based on its Cranelift type.
                let cond_type = self.builder.func.dfg.value_type(cond_val);
                let is_true = if cond_type == types::I8 {
                    // Native bool: 0 = false, nonzero = true.
                    cond_val
                } else if cond_type == types::F64 {
                    // Native F64: truthy if != 0.0 and not NaN
                    let zero = self.builder.ins().f64const(0.0);
                    // fcmp ordered-not-equal: false for NaN and 0.0
                    self.builder
                        .ins()
                        .fcmp(FloatCC::OrderedNotEqual, cond_val, zero)
                } else if cond_type == types::I32 {
                    // Native I32: truthy if != 0
                    let zero = self.builder.ins().iconst(types::I32, 0);
                    self.builder.ins().icmp(IntCC::NotEqual, cond_val, zero)
                } else {
                    // v2-boundary: I64 (NaN-boxed) truthiness check uses NaN-box tags
                    let tag_null = self
                        .builder
                        .ins()
                        .iconst(types::I64, 0i64);
                    let tag_none = self
                        .builder
                        .ins()
                        .iconst(types::I64, 0i64);
                    let tag_false = self
                        .builder
                        .ins()
                        .iconst(types::I64, 0i64);
                    let zero = self.builder.ins().iconst(types::I64, 0i64);
                    let not_null = self
                        .builder
                        .ins()
                        .icmp(IntCC::NotEqual, cond_val, tag_null);
                    let not_none = self
                        .builder
                        .ins()
                        .icmp(IntCC::NotEqual, cond_val, tag_none);
                    let not_false = self
                        .builder
                        .ins()
                        .icmp(IntCC::NotEqual, cond_val, tag_false);
                    let not_zero = self
                        .builder
                        .ins()
                        .icmp(IntCC::NotEqual, cond_val, zero);
                    let t1 = self.builder.ins().band(not_null, not_none);
                    let t2 = self.builder.ins().band(t1, not_false);
                    self.builder.ins().band(t2, not_zero)
                };

                self.builder
                    .ins()
                    .brif(is_true, *true_block, &[], *false_block, &[]);
                Ok(())
            }

            TerminatorKind::Call {
                func,
                args,
                destination,
                next,
            } => {
                // ── v2 TYPED-ARRAY METHOD FAST PATH ──────────────────────
                // Intercept `arr.length()` / `arr.len()` / `arr.push(v)`
                // when the receiver is a v2 typed-array slot. Bypass
                // `jit_call_method` and emit inline FFI calls.
                if let Operand::Constant(MirConstant::Method(method_name)) = func {
                    if let Some(receiver_arg) = args.first() {
                        if let Some(receiver_place) = match receiver_arg {
                            Operand::Copy(p)
                            | Operand::Move(p)
                            | Operand::MoveExplicit(p) => Some(p.clone()),
                            _ => None,
                        } {
                            if let Some(elem_kind) =
                                self.v2_typed_array_elem_kind(&receiver_place)
                            {
                                if let Some(()) = self.try_emit_v2_array_method(
                                    method_name,
                                    &receiver_place,
                                    &args[1..],
                                    destination,
                                    elem_kind,
                                )? {
                                    let next_block = self.block_map.get(next).ok_or_else(
                                        || format!("MirToIR: unknown call continuation block {}", next),
                                    )?;
                                    self.builder.ins().jump(*next_block, &[]);
                                    return Ok(());
                                }
                            }
                        }
                    }
                }

                // ── v2 TYPED HASHMAP<string, V> METHOD FAST PATH ─────────
                // Intercept `m.get(k)` / `m.set(k, v)` / `m.has(k)` /
                // `m.length()` when the receiver is a `HashMap<string, I64|F64>`
                // slot. Bypasses the generic method-dispatch trampoline.
                if let Operand::Constant(MirConstant::Method(method_name)) = func {
                    if let Some(receiver_arg) = args.first() {
                        if let Some(receiver_place) = match receiver_arg {
                            Operand::Copy(p)
                            | Operand::Move(p)
                            | Operand::MoveExplicit(p) => Some(p.clone()),
                            _ => None,
                        } {
                            if let Some(kinds) =
                                self.v2_typed_str_map_kinds(&receiver_place)
                            {
                                if let Some(()) = self.try_emit_v2_typed_map_method(
                                    method_name,
                                    &receiver_place,
                                    &args[1..],
                                    destination,
                                    kinds,
                                )? {
                                    let next_block = self.block_map.get(next).ok_or_else(
                                        || format!("MirToIR: unknown call continuation block {}", next),
                                    )?;
                                    self.builder.ins().jump(*next_block, &[]);
                                    return Ok(());
                                }
                            }
                        }
                    }
                }

                // ── V3-S6c MONOMORPHIZED METHOD-CALL ROUTING ─────────────
                // PATH α-prime per supervisor 2026-05-15 ratification. If
                // this Method-call site was specialized at bytecode-compile
                // time (V3-S6b side-table populated by
                // `try_monomorphize_method_call` success — both the
                // type-only and closure-aware mirrors), bypass the
                // `jit_call_method` trampoline + the `handle_int_map`
                // ckpt3_surface path entirely. Emit a direct Cranelift
                // FuncRef call to `user_func_refs[specialized_idx]`,
                // mirroring the direct-Function-call codegen at lines
                // ~807-867 below.
                //
                // ABI alignment is structurally guaranteed by
                // `crates/shape-vm/src/compiler/monomorphization/substitution.rs:2247`
                // (`inline_closure_body_into_specialization` doc-comment):
                // the specialized function preserves the original
                // parameter list verbatim — closure params are kept (the
                // closure pointer sits unused after body inlining); no
                // capture hoisting (Phase D/E territory). So Method-call
                // args `[receiver, closure_obj, ...]` map 1:1 to the
                // specialized function's `(self, closure_param, ...)`
                // signature.
                //
                // ADR-006 §2.7.5 stamp-at-compile-time: the side-table
                // lookup at JIT codegen is COMPILE TIME (this terminator
                // pass), NEVER runtime — the routing decision is encoded
                // into emitted Cranelift IR, not a runtime tag-byte read.
                //
                // Bypasses the V3-S6b dual-consumer SIGSEGV class
                // (`concrete_types[slot] = Array(I64)` activates both
                // `parametric_method_return_kind_from_receiver` AND
                // `v2_typed_array_elem_kind` → handle_int_map ckpt3_surface)
                // because no consume-via-stamp is performed; the routing
                // is structural at the Call terminator.
                if let Operand::Constant(MirConstant::Method(_)) = func {
                    let key = (terminator.span, self.caller_function_id);
                    if let Some(&specialized_idx) =
                        self.monomorphized_method_call_sites.get(&key)
                    {
                        let func_ref_opt = self
                            .user_func_refs
                            .get(&(specialized_idx as u16))
                            .copied();
                        if let Some(func_ref) = func_ref_opt {
                            // Mirror of the direct-Function-call codegen at
                            // terminators.rs:807-867 below. ABI =
                            // fn(ctx_ptr, arg0, ..., argN) -> i32 deopt
                            // signal. Args widened to I64 uniformly per
                            // R4.2E.
                            let mut arg_vals = Vec::with_capacity(args.len() + 1);
                            arg_vals.push(self.ctx_ptr);
                            for arg in args.iter() {
                                let val = self.compile_operand(arg)?;
                                let val_ty = self.builder.func.dfg.value_type(val);
                                let boxed = if val_ty == types::I64 {
                                    val
                                } else if val_ty == types::F64 {
                                    self.builder.ins().bitcast(
                                        types::I64,
                                        MemFlags::new(),
                                        val,
                                    )
                                } else if val_ty == types::I32 {
                                    self.builder.ins().sextend(types::I64, val)
                                } else if val_ty == types::I8 {
                                    self.builder.ins().uextend(types::I64, val)
                                } else if val_ty == types::I16 {
                                    self.builder.ins().sextend(types::I64, val)
                                } else {
                                    val
                                };
                                arg_vals.push(boxed);
                            }

                            let inst =
                                self.builder.ins().call(func_ref, &arg_vals);
                            let signal = self.builder.inst_results(inst)[0];

                            // Deopt: signal < 0 propagates by immediate
                            // return of the negative signal.
                            let zero =
                                self.builder.ins().iconst(types::I32, 0);
                            let is_error = self.builder.ins().icmp(
                                IntCC::SignedLessThan,
                                signal,
                                zero,
                            );
                            let deopt_block = self.builder.create_block();
                            let continue_block = self.builder.create_block();
                            self.builder.ins().brif(
                                is_error,
                                deopt_block,
                                &[],
                                continue_block,
                                &[],
                            );

                            self.builder.switch_to_block(deopt_block);
                            self.builder.seal_block(deopt_block);
                            self.builder.ins().return_(&[signal]);

                            self.builder.switch_to_block(continue_block);
                            self.builder.seal_block(continue_block);
                            let stack_offset =
                                crate::context::STACK_OFFSET as i32;
                            let result = self.builder.ins().load(
                                types::I64,
                                MemFlags::new(),
                                self.ctx_ptr,
                                stack_offset,
                            );

                            self.release_old_value_if_heap(destination)?;
                            self.write_place(destination, result)?;
                            self.reload_referenced_locals();

                            let next_block =
                                self.block_map.get(next).ok_or_else(|| {
                                    format!(
                                        "MirToIR: unknown call continuation \
                                         block {}",
                                        next
                                    )
                                })?;
                            self.builder.ins().jump(*next_block, &[]);
                            return Ok(());
                        }
                        // FuncRef miss: side-table had specialized_idx but
                        // user_func_refs lacks it (declaration race or
                        // sub_program-rebased index space). Fall through to
                        // the existing jit_call_method trampoline path
                        // below — preserves V3-S6b baseline behaviour.
                    }
                    // Side-table miss (not a monomorphized site, or this
                    // caller-function didn't specialize this call). Fall
                    // through to the existing jit_call_method trampoline
                    // path below.
                }

                // ── METHOD CALL PATH ─────────────────────────────────────
                // Method calls use MirConstant::Method(name). The MIR args
                // are [receiver, arg0, arg1, ...]. We need to push them to
                // ctx.stack in the format jit_call_method expects:
                //   [receiver, arg0, ..., method_name_string, arg_count_number]
                // then call jit_call_method(ctx, total_count).
                if let Operand::Constant(MirConstant::Method(method_name)) = func {
                    let stack_base_offset = crate::context::STACK_OFFSET as i32;
                    let sp_offset = crate::context::STACK_PTR_OFFSET as i32;

                    let old_sp = self.builder.ins().load(
                        types::I64,
                        MemFlags::new(),
                        self.ctx_ptr,
                        sp_offset,
                    );

                    // args[0] = receiver, args[1..] = actual method arguments.
                    // ADR-006 §2.7.7 / Q9: every push into ctx.stack writes
                    // the producing-site kind into `stack_kinds` in lockstep.
                    // `jit_call_method` doesn't currently consume kinds (it
                    // dispatches by method name + receiver type via the
                    // §2.7.10/Q11 path), but the lockstep invariant
                    // requires the writes; the kind track at these slots
                    // surfaces a kind-source gap correctly if some future
                    // FFI reads them.
                    //
                    // R4.2E: VM-stack slots are 8-byte-wide I64 bit-patterns.
                    // Widen narrow Cranelift types inline (sextend/uextend/
                    // bitcast) so the method-dispatch trampoline reads a
                    // uniform u64 from ctx.stack[sp+i]. No NaN-boxing tagging
                    // is applied — raw bit-patterns only.
                    for (i, arg) in args.iter().enumerate() {
                        // Source kind for the parallel-kind track,
                        // falling back to the §2.7.5 carrier kind
                        // (`UInt64`) for opaque-source operands —
                        // NOT a Bool-default fallback.
                        let _ = i;
                        let arg_kind = self.operand_slot_kind_or_carrier(arg);

                        let val = self.compile_operand(arg)?;
                        let val_ty = self.builder.func.dfg.value_type(val);
                        let boxed = if val_ty == types::I64 {
                            val
                        } else if val_ty == types::F64 {
                            self.builder.ins().bitcast(types::I64, MemFlags::new(), val)
                        } else if val_ty == types::I32 {
                            self.builder.ins().sextend(types::I64, val)
                        } else if val_ty == types::I8 {
                            self.builder.ins().uextend(types::I64, val)
                        } else if val_ty == types::I16 {
                            self.builder.ins().sextend(types::I64, val)
                        } else {
                            val
                        };
                        let slot_idx = self.builder.ins().iadd_imm(old_sp, i as i64);
                        let byte_off = self.builder.ins().ishl_imm(slot_idx, 3);
                        let abs_off = self.builder.ins().iadd_imm(byte_off, stack_base_offset as i64);
                        let store_addr = self.builder.ins().iadd(self.ctx_ptr, abs_off);
                        self.builder.ins().store(MemFlags::new(), boxed, store_addr, 0);
                        // §2.7.7 / Q9 lockstep parallel-kind write.
                        self.emit_kind_track_write(slot_idx, arg_kind);
                    }

                    // v2-boundary: method name pushed as NaN-boxed string to ctx.stack
                    let method_str_bits = crate::ffi::value_ffi::box_string(method_name.clone());
                    let method_val = self.builder.ins().iconst(types::I64, method_str_bits as i64);
                    let method_slot_idx = self.builder.ins().iadd_imm(old_sp, args.len() as i64);
                    let method_byte_off = self.builder.ins().ishl_imm(method_slot_idx, 3);
                    let method_abs_off = self.builder.ins().iadd_imm(method_byte_off, stack_base_offset as i64);
                    let method_addr = self.builder.ins().iadd(self.ctx_ptr, method_abs_off);
                    self.builder.ins().store(MemFlags::new(), method_val, method_addr, 0);
                    // Method name is a heap String — kind = `NativeKind::String`.
                    self.emit_kind_track_write(
                        method_slot_idx,
                        shape_value::NativeKind::String,
                    );

                    // v2-boundary: arg_count pushed as raw i64 to ctx.stack.
                    // jit_call_method decodes this via direct `as usize` — no NaN-box.
                    let actual_arg_count = if args.is_empty() { 0 } else { args.len() - 1 };
                    let argc_bits = actual_arg_count as i64;
                    let argc_val = self.builder.ins().iconst(types::I64, argc_bits as i64);
                    let argc_slot_idx = self.builder.ins().iadd_imm(old_sp, (args.len() + 1) as i64);
                    let argc_byte_off = self.builder.ins().ishl_imm(argc_slot_idx, 3);
                    let argc_abs_off = self.builder.ins().iadd_imm(argc_byte_off, stack_base_offset as i64);
                    let argc_addr = self.builder.ins().iadd(self.ctx_ptr, argc_abs_off);
                    self.builder.ins().store(MemFlags::new(), argc_val, argc_addr, 0);
                    // arg_count sentinel slot — UInt64 carrier kind per §2.7.5.
                    self.emit_kind_track_write(
                        argc_slot_idx,
                        shape_value::NativeKind::UInt64,
                    );

                    // Update stack_ptr: receiver + args + method_name + arg_count
                    let total_items = args.len() + 2; // args (including receiver) + method_name + arg_count
                    let new_sp = self.builder.ins().iadd_imm(old_sp, total_items as i64);
                    self.builder.ins().store(MemFlags::new(), new_sp, self.ctx_ptr, sp_offset);

                    // Call jit_call_method(ctx, total_count)
                    let count_val = self.builder.ins().iconst(types::I64, total_items as i64);
                    let inst = self.builder.ins().call(
                        self.ffi.call_method,
                        &[self.ctx_ptr, count_val],
                    );
                    let result = self.builder.inst_results(inst)[0];

                    // Restore stack_ptr to old value
                    self.builder.ins().store(MemFlags::new(), old_sp, self.ctx_ptr, sp_offset);

                    // ── r5c-2-bz-b-jit-err-surface: VM-trampoline error deopt ─
                    // `jit_call_method` returns a `u64` value-shaped result; it
                    // has no negative-signal channel like a JIT-compiled callee.
                    // When the VM-side method handler surfaces an `Err` (e.g.
                    // `Set.add()` with a non-string key) the FFI body sets
                    // `JITContext.pending_call_error = 1` and the returned bits
                    // are a placeholder. Continuing would feed that placeholder
                    // into `write_place` and a later refcount-retain — and
                    // `TAG_NULL` (a non-zero NaN-box sentinel) passes the
                    // `bits != 0` guard in `jit_arc_*_retain`, dereferencing
                    // `TAG_NULL - 16` → SIGSEGV. Instead: load the flag and
                    // deopt the JIT frame BEFORE `write_place`/retain by
                    // returning `SIGNAL_TRAMPOLINE_ERROR`. The `JITExecutor`
                    // surfaces the VM's clean error message stored in the
                    // `JIT_RUNTIME_ERROR` thread-local. This is the W12
                    // architectural fall-through — a genuine JIT failure
                    // abandons the JIT frame; the VM produces the error.
                    let err_flag_offset =
                        crate::context::PENDING_CALL_ERROR_OFFSET as i32;
                    let err_flag = self.builder.ins().load(
                        types::I8,
                        MemFlags::new(),
                        self.ctx_ptr,
                        err_flag_offset,
                    );
                    let zero_i8 = self.builder.ins().iconst(types::I8, 0);
                    let has_err = self.builder.ins().icmp(
                        IntCC::NotEqual,
                        err_flag,
                        zero_i8,
                    );
                    let err_deopt_block = self.builder.create_block();
                    let err_continue_block = self.builder.create_block();
                    self.builder.ins().brif(
                        has_err,
                        err_deopt_block,
                        &[],
                        err_continue_block,
                        &[],
                    );
                    self.builder.switch_to_block(err_deopt_block);
                    self.builder.seal_block(err_deopt_block);
                    // Cranelift I32 iconst convention: pass the unsigned
                    // bit-pattern of the signed deopt code.
                    let trampoline_err_signal = self.builder.ins().iconst(
                        types::I32,
                        (crate::context::SIGNAL_TRAMPOLINE_ERROR as u32) as i64,
                    );
                    self.builder.ins().return_(&[trampoline_err_signal]);

                    self.builder.switch_to_block(err_continue_block);
                    self.builder.seal_block(err_continue_block);

                    // Store result to destination
                    self.release_old_value_if_heap(destination)?;
                    self.write_place(destination, result)?;

                    // Reload locals that may have been mutated via references
                    self.reload_referenced_locals();

                    // Jump to continuation block
                    let next_block = self.block_map.get(next).ok_or_else(|| {
                        format!("MirToIR: unknown call continuation block {}", next)
                    })?;
                    self.builder.ins().jump(*next_block, &[]);
                    return Ok(());
                }

                // ── BUILTIN FUNCTION PATH ─────────────────────────────
                // Known builtins like "print" that aren't user functions.
                // Dispatch directly to the FFI implementation.
                if let Operand::Constant(MirConstant::Function(name)) = func {
                    if name == "print" && self.function_indices.get(name.as_str()).is_none() {
                        // W11-jit-new-array (ADR-006 §2.7.5 stamp-at-compile-
                        // time): when the operand's `NativeKind` is proven
                        // (Int64 / Float64 / Bool), dispatch to the matching
                        // kinded entry point so the FFI sees the raw native
                        // value rather than a deleted-ValueWord-shape u64.
                        // The kind-blind `jit_print` falls through for
                        // operands whose kind the MIR could not prove
                        // (heap arms remain a §2.7.5 follow-up).
                        let kind_hint = if args.is_empty() {
                            None
                        } else {
                            self.operand_slot_kind(&args[0])
                        };
                        let val = if args.is_empty() {
                            self.builder.ins().iconst(types::I64, 0i64)
                        } else {
                            self.compile_operand_raw(&args[0])?
                        };
                        use shape_value::heap_value::HeapKind;
                        use shape_vm::type_tracking::NativeKind;
                        match kind_hint {
                            Some(NativeKind::Int64 | NativeKind::IntSize) => {
                                let val_ty = self.builder.func.dfg.value_type(val);
                                let widened = if val_ty == types::I64 {
                                    val
                                } else if val_ty == types::I32 {
                                    self.builder.ins().sextend(types::I64, val)
                                } else if val_ty == types::I8 {
                                    self.builder.ins().uextend(types::I64, val)
                                } else {
                                    val
                                };
                                self.builder.ins().call(self.ffi.print_i64, &[widened]);
                            }
                            // r5c-2-β-CKPT-C u64-carrier-disambiguation
                            // (2026-05-20): the UNSIGNED integer kinds route
                            // to `jit_print_u64`, NOT `jit_print_i64`.
                            // Pre-fix both kinds shared the signed arm,
                            // which reinterprets the raw bits as `i64` — so
                            // `print(x)` for `let x: u64 =
                            // 18446744073709551615` displayed `-1` on the
                            // JIT (VM correctly displayed the unsigned
                            // value). The carrier `NativeKind` is the
                            // discriminator; widening of narrower unsigned
                            // values is zero-extension (`uextend`).
                            Some(NativeKind::UInt64 | NativeKind::UIntSize) => {
                                let val_ty = self.builder.func.dfg.value_type(val);
                                let widened = if val_ty == types::I64 {
                                    val
                                } else if val_ty == types::I32
                                    || val_ty == types::I8
                                {
                                    self.builder.ins().uextend(types::I64, val)
                                } else {
                                    val
                                };
                                self.builder.ins().call(self.ffi.print_u64, &[widened]);
                            }
                            Some(NativeKind::Float64) => {
                                let val_ty = self.builder.func.dfg.value_type(val);
                                let coerced = if val_ty == types::F64 {
                                    val
                                } else if val_ty == types::I64 {
                                    self.builder
                                        .ins()
                                        .bitcast(types::F64, MemFlags::new(), val)
                                } else {
                                    val
                                };
                                self.builder.ins().call(self.ffi.print_f64, &[coerced]);
                            }
                            Some(NativeKind::Bool) => {
                                let val_ty = self.builder.func.dfg.value_type(val);
                                let coerced = if val_ty == types::I8 {
                                    val
                                } else if val_ty == types::I64
                                    || val_ty == types::I32
                                {
                                    self.builder.ins().ireduce(types::I8, val)
                                } else {
                                    val
                                };
                                self.builder.ins().call(self.ffi.print_bool, &[coerced]);
                            }
                            // ── Heap-arm kinded dispatch (§2.7.5 carriers) ──
                            // W12-jit-print-heap-arm-classification
                            // (Phase 3 cluster-0 Round 8A, 2026-05-13):
                            // ADR-006 §2.7.5 stamp-at-compile-time. The
                            // operand's `NativeKind` is proven at MIR-
                            // emit time (via `operand_slot_kind`'s
                            // producing-site classification); route to
                            // the matching kinded FFI entry that reads
                            // the typed `Arc<T>` payload directly. The
                            // FFI bodies take `(ctx_ptr, bits)` and
                            // delegate to the canonical VM-side
                            // `ValueFormatter::format_kinded` for
                            // VM == JIT identical output. No NaN-box tag
                            // decode, no `is_heap_kind` probe — kind IS
                            // the discriminator (§2.7.7 #4 / #7).
                            //
                            // The Option / Result arms below match the
                            // Round 7A `jit_v2_make_option_*` /
                            // `_make_result_*` producer-site Arc-shape
                            // carrier per §2.7.17 — `Arc::into_raw(Arc<
                            // OptionData|ResultData>) as u64` with the
                            // matching `Ptr(HeapKind::Option|Result)`
                            // kind label.
                            Some(NativeKind::Ptr(HeapKind::Option)) => {
                                let val_ty = self.builder.func.dfg.value_type(val);
                                let widened = if val_ty == types::I64 {
                                    val
                                } else if val_ty == types::F64 {
                                    self.builder
                                        .ins()
                                        .bitcast(types::I64, MemFlags::new(), val)
                                } else {
                                    val
                                };
                                self.builder.ins().call(
                                    self.ffi.print_option,
                                    &[self.ctx_ptr, widened],
                                );
                            }
                            Some(NativeKind::Ptr(HeapKind::Result)) => {
                                let val_ty = self.builder.func.dfg.value_type(val);
                                let widened = if val_ty == types::I64 {
                                    val
                                } else if val_ty == types::F64 {
                                    self.builder
                                        .ins()
                                        .bitcast(types::I64, MemFlags::new(), val)
                                } else {
                                    val
                                };
                                self.builder.ins().call(
                                    self.ffi.print_result,
                                    &[self.ctx_ptr, widened],
                                );
                            }
                            // ── §2.7.5 String carrier arm ───────────────
                            // W12-jit-string-carrier-unification (Phase 3
                            // cluster-0 Round 12 T2/T3, 2026-05-13). The
                            // `MirConstant::Str` / `MirConstant::StringId`
                            // sites in `mir_compiler/ownership.rs` now emit
                            // `arc_string_constant(s)` → `Arc::into_raw(
                            // Arc<String>) as u64` per ADR-006 §2.7.5.
                            // `jit_print_str` reads `&String` via
                            // `ValueSlot::from_raw(bits) + KindedSlot::new(
                            // ..., NativeKind::String)` and delegates to the
                            // canonical `ValueFormatter::format_kinded` — VM
                            // == JIT identical output.
                            //
                            // The String EnumPayload extractor at Round 8A
                            // (`infer_enum_payload_kind` switched to the
                            // full `native_kind_from_concrete_type` mapping)
                            // also produces this same §2.7.5 carrier per
                            // §2.7.17 receiver-recovery soundness, so
                            // `print(Err("x"))` reaches this arm with the
                            // matching carrier shape.
                            Some(NativeKind::String) => {
                                let val_ty = self.builder.func.dfg.value_type(val);
                                let widened = if val_ty == types::I64 {
                                    val
                                } else if val_ty == types::F64 {
                                    self.builder
                                        .ins()
                                        .bitcast(types::I64, MemFlags::new(), val)
                                } else {
                                    val
                                };
                                self.builder.ins().call(
                                    self.ffi.print_str,
                                    &[self.ctx_ptr, widened],
                                );
                            }
                            // ── TypedObject SURFACE-and-stop (residual) ──
                            // The JIT-internal TypedObject path
                            // (`box_typed_object` at `value_ffi.rs:516-518`
                            // returning `unified_box(HK_TYPED_OBJECT, *const
                            // u8)` over a JIT-owned `TypedObject` struct, NOT
                            // the VM-side `Arc<TypedObjectStorage>`) cannot
                            // be reused unchanged for `jit_print_typed_object`
                            // — that body expects an `Arc::into_raw(Arc<
                            // TypedObjectStorage>) as u64` carrier (a
                            // different Rust type with different layout).
                            // Migrating the JIT-side TypedObject to the VM-
                            // side `Arc<TypedObjectStorage>` is a larger
                            // surgery (W11 TypedArray family invariant + 17+
                            // JIT-internal consumers in `typed_object/`,
                            // `data.rs`, `property_access.rs`, etc.).
                            //
                            // Per W12-jit-string-carrier-unification surface-
                            // and-stop discipline (round dispatch §"Surface-
                            // and-stop expected"): "If the TypedObject
                            // migration scope exceeds the budget OR breaks
                            // the W11-jit-new-array TypedArray<T> shape ...
                            // STOP and surface to disambiguate." This arm
                            // stays SURFACE; cluster-1 absorbs the
                            // TypedObject producer migration via a separate
                            // sub-cluster.
                            Some(NativeKind::Ptr(HeapKind::TypedObject)) => {
                                tracing::debug!(
                                    target: "shape_jit",
                                    "jit-mir print: SURFACE \u{a7}2.7.5 \
                                     carrier-mismatch \u{2014} operand \
                                     NativeKind Ptr(TypedObject) stamped but \
                                     the JIT-side `box_typed_object` producer \
                                     at `value_ffi.rs:516-518` emits a \
                                     JIT-internal `TypedObject` struct under \
                                     NaN-box wrap, NOT the VM-side \
                                     `Arc<TypedObjectStorage>` the \
                                     `jit_print_typed_object` body expects. \
                                     Migrating the JIT TypedObject to \
                                     `Arc<TypedObjectStorage>` is out of W12 \
                                     T2/T3 scope per the round's surface-and-\
                                     stop discipline (round dispatch text). \
                                     Cluster-1 follow-up: W17 jit-typed-object-\
                                     arc-storage-migration.",
                                );
                                return Err(format!(
                                    "Route A surface-and-stop: SURFACE \
                                     §2.7.5 carrier-mismatch — `print` \
                                     Call-terminator operand NativeKind \
                                     Ptr(TypedObject); the JIT-side \
                                     `box_typed_object` at \
                                     `ffi/value_ffi.rs:516-518` emits a \
                                     JIT-internal `TypedObject` struct under \
                                     NaN-box wrap (a JIT-owned type, NOT the \
                                     VM-side `Arc<TypedObjectStorage>` the \
                                     `jit_print_typed_object` body reads). \
                                     Migrating the JIT TypedObject to \
                                     `Arc<TypedObjectStorage>` is out of \
                                     W12 T2/T3 scope per the round's \
                                     surface-and-stop discipline. Tracked \
                                     for cluster-1 follow-up \
                                     W17-jit-typed-object-arc-storage-\
                                     migration. kind_hint={:?}",
                                    kind_hint,
                                ));
                            }
                            // ── Phase 3 cluster-2 Round 3 cw-D-fam12
                            //    Scalar Char (Family 1) arm ─────────────
                            //
                            // ADR-006 §2.7.5 amendment (Round 19 S1.5
                            // W12-nativekind-scalar-additions, 2026-05-14):
                            // Char is a 4-byte scalar carrier (codepoint
                            // inline in low 32 bits of `ValueSlot`, no Arc
                            // wrapping). Both the post-amendment scalar
                            // label `NativeKind::Char` and the
                            // pre-amendment heap arm
                            // `NativeKind::Ptr(HeapKind::Char)` recognize
                            // the same carrier shape (per
                            // `KindedSlot::as_char` accessor accepting
                            // both labels at `kinded_slot.rs:594-597`).
                            // Both route to `jit_print_char(u32)` which
                            // takes the codepoint directly — mirror of
                            // `jit_print_i64` / `jit_print_f64` /
                            // `jit_print_bool` (scalar-by-value FFI shape,
                            // no ctx_ptr threading needed).
                            Some(NativeKind::Char)
                            | Some(NativeKind::Ptr(HeapKind::Char)) => {
                                let val_ty = self.builder.func.dfg.value_type(val);
                                // Narrow to I32: ValueSlot::from_char stores
                                // `c as u64` (zero-extended); the low 32 bits
                                // carry the codepoint per `ValueSlot::as_char`
                                // (`self.0 as u32`). Cranelift FFI passes I32
                                // directly.
                                let narrowed = if val_ty == types::I32 {
                                    val
                                } else if val_ty == types::I64
                                    || val_ty == types::I8
                                {
                                    // I64: truncate to low 32 bits.
                                    // I8: zero-extend then narrow (covers
                                    // any upstream code that materializes a
                                    // char as a byte).
                                    if val_ty == types::I64 {
                                        self.builder.ins().ireduce(types::I32, val)
                                    } else {
                                        self.builder.ins().uextend(types::I32, val)
                                    }
                                } else {
                                    val
                                };
                                self.builder.ins().call(
                                    self.ffi.print_char,
                                    &[narrowed],
                                );
                            }
                            // ── Phase 3 cluster-2 Round 3 cw-D-fam12
                            //    Concurrency-primitive family (Family 2):
                            //    Mutex / Atomic / Lazy / Channel arms ──
                            //
                            // ADR-006 §2.7.25 concurrency-primitive
                            // rebuild trio (Mutex / Atomic / Lazy) + the
                            // §2.7.20 Channel precedent share the same
                            // `Arc::into_raw(Arc<XData>) as u64` carrier
                            // shape and the same `<X:state>` opaque-tag
                            // print format (per `printing.rs:451-464`
                            // Channel + `:534-551` Mutex/Atomic/Lazy).
                            // Each delegates to `print_kinded_inner` so
                            // VM == JIT identical output is preserved
                            // (no NaN-box tag decode, no `is_heap_kind`
                            // probe — kind IS the discriminator per
                            // §2.7.7 #4 / #7).
                            Some(NativeKind::Ptr(HeapKind::Mutex)) => {
                                let val_ty = self.builder.func.dfg.value_type(val);
                                let widened = if val_ty == types::I64 {
                                    val
                                } else if val_ty == types::F64 {
                                    self.builder
                                        .ins()
                                        .bitcast(types::I64, MemFlags::new(), val)
                                } else {
                                    val
                                };
                                self.builder.ins().call(
                                    self.ffi.print_mutex,
                                    &[self.ctx_ptr, widened],
                                );
                            }
                            Some(NativeKind::Ptr(HeapKind::Atomic)) => {
                                let val_ty = self.builder.func.dfg.value_type(val);
                                let widened = if val_ty == types::I64 {
                                    val
                                } else if val_ty == types::F64 {
                                    self.builder
                                        .ins()
                                        .bitcast(types::I64, MemFlags::new(), val)
                                } else {
                                    val
                                };
                                self.builder.ins().call(
                                    self.ffi.print_atomic,
                                    &[self.ctx_ptr, widened],
                                );
                            }
                            Some(NativeKind::Ptr(HeapKind::Lazy)) => {
                                let val_ty = self.builder.func.dfg.value_type(val);
                                let widened = if val_ty == types::I64 {
                                    val
                                } else if val_ty == types::F64 {
                                    self.builder
                                        .ins()
                                        .bitcast(types::I64, MemFlags::new(), val)
                                } else {
                                    val
                                };
                                self.builder.ins().call(
                                    self.ffi.print_lazy,
                                    &[self.ctx_ptr, widened],
                                );
                            }
                            Some(NativeKind::Ptr(HeapKind::Channel)) => {
                                let val_ty = self.builder.func.dfg.value_type(val);
                                let widened = if val_ty == types::I64 {
                                    val
                                } else if val_ty == types::F64 {
                                    self.builder
                                        .ins()
                                        .bitcast(types::I64, MemFlags::new(), val)
                                } else {
                                    val
                                };
                                self.builder.ins().call(
                                    self.ffi.print_channel,
                                    &[self.ctx_ptr, widened],
                                );
                            }
                            // ── Phase 3 cluster-2 Round 4 cw-D-fam3
                            //    Collection family (Family 3) arms ────
                            //
                            // ADR-006 §2.7.5.B per-HeapKind-family kinded
                            // jit_print dispatch arms (Family 3 amendment
                            // extension, 2026-05-16). Per cluster-2-
                            // inventory §E.5 the Collection family is
                            // HashMap (ord 17), HashSet (21), Deque (23),
                            // PriorityQueue (25), Range (26), Iterator (22)
                            // — all `Arc<XData>` heap-arm carriers per
                            // §2.7.5 stamp-at-compile-time (HashMap is
                            // `Arc<HashMapKindedRef>` per Wave 2 Round 3b
                            // C2-joint ckpt-2 Q25.B SUPERSEDED; the inner
                            // per-V monomorphization dispatches at the
                            // carrier's variant tag, transparent to the
                            // FFI body which only forwards the outer
                            // typed-Arc bits + the §2.7.5 kind label to
                            // `print_kinded_inner`). Each delegates to
                            // `print_kinded_inner` so VM == JIT identical
                            // output is preserved (no NaN-box tag decode,
                            // no `is_heap_kind` probe — kind IS the
                            // discriminator per §2.7.7 #4 / #7).
                            //
                            // Per inventory §E.5 categorization the
                            // Iterator HeapKind is part of the Collection
                            // family (NOT the pure-discriminator family);
                            // `HeapValue::Iterator(Arc<IteratorState>)`
                            // participates in the §2.3 typed-Arc payload
                            // pattern per ADR-006 §2.7.16 / Q17
                            // W13-iterator-state, and the dispatch arm in
                            // `format_heap_kind` at `printing.rs:430`
                            // reads the bits as `*const IteratorState`.
                            // ADR-006 §2.7.5.B 2026-05-16
                            Some(NativeKind::Ptr(HeapKind::HashMap)) => {
                                let val_ty = self.builder.func.dfg.value_type(val);
                                let widened = if val_ty == types::I64 {
                                    val
                                } else if val_ty == types::F64 {
                                    self.builder
                                        .ins()
                                        .bitcast(types::I64, MemFlags::new(), val)
                                } else {
                                    val
                                };
                                self.builder.ins().call(
                                    self.ffi.print_hashmap,
                                    &[self.ctx_ptr, widened],
                                );
                            }
                            Some(NativeKind::Ptr(HeapKind::HashSet)) => {
                                let val_ty = self.builder.func.dfg.value_type(val);
                                let widened = if val_ty == types::I64 {
                                    val
                                } else if val_ty == types::F64 {
                                    self.builder
                                        .ins()
                                        .bitcast(types::I64, MemFlags::new(), val)
                                } else {
                                    val
                                };
                                self.builder.ins().call(
                                    self.ffi.print_hashset,
                                    &[self.ctx_ptr, widened],
                                );
                            }
                            Some(NativeKind::Ptr(HeapKind::Deque)) => {
                                let val_ty = self.builder.func.dfg.value_type(val);
                                let widened = if val_ty == types::I64 {
                                    val
                                } else if val_ty == types::F64 {
                                    self.builder
                                        .ins()
                                        .bitcast(types::I64, MemFlags::new(), val)
                                } else {
                                    val
                                };
                                self.builder.ins().call(
                                    self.ffi.print_deque,
                                    &[self.ctx_ptr, widened],
                                );
                            }
                            Some(NativeKind::Ptr(HeapKind::PriorityQueue)) => {
                                let val_ty = self.builder.func.dfg.value_type(val);
                                let widened = if val_ty == types::I64 {
                                    val
                                } else if val_ty == types::F64 {
                                    self.builder
                                        .ins()
                                        .bitcast(types::I64, MemFlags::new(), val)
                                } else {
                                    val
                                };
                                self.builder.ins().call(
                                    self.ffi.print_priority_queue,
                                    &[self.ctx_ptr, widened],
                                );
                            }
                            Some(NativeKind::Ptr(HeapKind::Range)) => {
                                let val_ty = self.builder.func.dfg.value_type(val);
                                let widened = if val_ty == types::I64 {
                                    val
                                } else if val_ty == types::F64 {
                                    self.builder
                                        .ins()
                                        .bitcast(types::I64, MemFlags::new(), val)
                                } else {
                                    val
                                };
                                self.builder.ins().call(
                                    self.ffi.print_range,
                                    &[self.ctx_ptr, widened],
                                );
                            }
                            Some(NativeKind::Ptr(HeapKind::Iterator)) => {
                                let val_ty = self.builder.func.dfg.value_type(val);
                                let widened = if val_ty == types::I64 {
                                    val
                                } else if val_ty == types::F64 {
                                    self.builder
                                        .ins()
                                        .bitcast(types::I64, MemFlags::new(), val)
                                } else {
                                    val
                                };
                                self.builder.ins().call(
                                    self.ffi.print_iterator,
                                    &[self.ctx_ptr, widened],
                                );
                            }
                            // ── W11-fup-C (Phase 3d, 2026-05-18): v2-raw
                            //    TypedArray<T> arm ────────────────────────
                            //
                            // ADR-006 §2.7.5 stamp-at-compile-time. The
                            // MIR-time `concrete_type_to_native_kind` arm at
                            // `mir_compiler/types.rs:175` stamps
                            // `Ptr(HeapKind::TypedArray)` for every
                            // `ConcreteType::Array(_)` slot. The runtime
                            // carrier is the v2-raw `*mut TypedArray<T>`
                            // pointer produced by
                            // `crate::ffi::v2::jit_v2_array_new_<kind>` —
                            // NOT `Arc<TypedArrayData>` (that enum + outer
                            // `HeapValue::TypedArray(Arc<_>)` arm + the
                            // `TypedBuffer<T>` wrapper layer were retired
                            // across V3-S5 ckpt-1..ckpt-4 per W12 audit
                            // §3.5 / §3.6 + ADR-006 §2.7.24 Q25.A
                            // SUPERSEDED).
                            //
                            // `jit_print_typed_array` reads the v2-raw
                            // HeapHeader `_pad` byte for the element kind
                            // (per `v2_array_detect::as_v2_typed_array` at
                            // `printing.rs:133`) — no Bool-default
                            // fabrication, no kind from raw bits; the
                            // element type stamped at allocation time is
                            // the authoritative source. VM == JIT
                            // identical output via the canonical
                            // `ValueFormatter::format_kinded` path.
                            Some(NativeKind::Ptr(HeapKind::TypedArray)) => {
                                let val_ty = self.builder.func.dfg.value_type(val);
                                let widened = if val_ty == types::I64 {
                                    val
                                } else if val_ty == types::F64 {
                                    self.builder
                                        .ins()
                                        .bitcast(types::I64, MemFlags::new(), val)
                                } else {
                                    val
                                };
                                self.builder.ins().call(
                                    self.ffi.print_typed_array,
                                    &[self.ctx_ptr, widened],
                                );
                            }
                            // ── NotImplemented(SURFACE): unproven kind /
                            //    unwired heap arm ─────────────────────
                            //
                            // ADR-006 §2.7.5 / §2.7.7 #4 / #7. The kind-
                            // blind `jit_print` fallback (which routed
                            // through the deleted-W-series
                            // `format_value_word` per `ffi/conversion.
                            // rs` lines 200-217) was retired in Round 8A
                            // verification (2026-05-13). Round 7A's
                            // smoke 1.5 close depended on this fallback
                            // because `infer_enum_payload_kind` used
                            // the scalar-only `elem_slot_kind_for_
                            // concrete` classifier, leaving `Err(String)`
                            // / `Some(typed_object)` payload slots
                            // without a kind stamp. Round 8A
                            // verification extended that classifier to
                            // the full `native_kind_from_concrete_type`
                            // mapping (per §2.7.17 receiver-recovery
                            // soundness — `jit_arc_*_payload` returns
                            // the inner `KindedSlot.slot.raw()`
                            // verbatim, preserving the §2.7.5 carrier
                            // shape for every NativeKind variant). With
                            // that extension the kinded arms above
                            // catch every EnumPayload-derived print
                            // operand on Smoke 1.5 — both Ok(I64) and
                            // Err(String) reach their matching kinded
                            // entry.
                            //
                            // Remaining `_`-arm operands are either:
                            // - kind-source gaps upstream of the print
                            //   site (the §2.7.5 conduit doesn't yet
                            //   stamp every MIR shape — e.g. closure-
                            //   return locals without `concrete_types`
                            //   propagation), or
                            // - heap arms beyond {Option, Result,
                            //   String, TypedObject} not yet wired with
                            //   per-kind FFI bodies (TypedArray,
                            //   HashMap, HashSet, ...).
                            //
                            // Both are honest surface-and-stop cases.
                            // No tag-decode, no Bool-default fallback
                            // per CLAUDE.md "Forbidden rationalizations"
                            // + "Renames to refuse on sight". The
                            // pre-Round-8A "preserved baseline"
                            // rationalization was itself the W-series
                            // walk-back the supervisor refuses on
                            // sight (CLAUDE.md "Just a small fallback
                            // for this one edge case" / "Mark this as
                            // a follow-up for a later phase").
                            _ => {
                                tracing::debug!(
                                    target: "shape_jit",
                                    kind_hint = ?kind_hint,
                                    "jit-mir print: SURFACE \u{a7}2.7.5 \u{2014} \
                                     operand NativeKind not proven or unwired \
                                     heap arm. ADR-006 \u{a7}2.7.5 / \u{a7}2.7.7 #4 / #7 \
                                     \u{2014} extend producer-site classification at \
                                     the upstream MIR shape (the \u{a7}2.7.5 \
                                     conduit's producing-site walk) or wire \
                                     the kinded FFI body for the heap kind. \
                                     No kind-blind fallback per CLAUDE.md \
                                     \"Forbidden rationalizations\".",
                                );
                                return Err(format!(
                                    "Route A surface-and-stop: \
                                     NotImplemented(SURFACE) — \
                                     `print` Call-terminator operand \
                                     NativeKind is {:?}; either the \
                                     §2.7.5 producer-site \
                                     classification conduit doesn't \
                                     stamp this operand's kind at the \
                                     upstream MIR shape (extend \
                                     `infer_*_kind` for that shape per \
                                     §2.7.5 / §2.7.7 #4), or the heap \
                                     arm isn't wired with a per-kind \
                                     `jit_print_<heap_kind>` FFI body. \
                                     The pre-Round-8A kind-blind \
                                     `jit_print` fallback was retired \
                                     in Round 8A verification per \
                                     CLAUDE.md \"Forbidden \
                                     rationalizations\" (\"just a small \
                                     fallback for this one edge case\" \
                                     refused on sight).",
                                    kind_hint,
                                ));
                            }
                        }

                        // v2-boundary: None represented as TAG_NULL in NaN-boxed ABI
                        let none_val = self.builder.ins().iconst(types::I64, 0i64);
                        self.release_old_value_if_heap(destination)?;
                        self.write_place(destination, none_val)?;
                        self.reload_referenced_locals();
                        let next_block = self.block_map.get(next).ok_or_else(|| {
                            format!("MirToIR: unknown call continuation block {}", next)
                        })?;
                        self.builder.ins().jump(*next_block, &[]);
                        return Ok(());
                    }
                }

                // ── W15.2-LANG-6 MATH-BUILTIN PATH ───────────────────────
                // Per `docs/cluster-audits/v0.3-w15-book-truth-reaudit.md`
                // §6.6: bare-name math builtins (`sqrt`, `abs`, `sin`,
                // `cos`, etc.) parse as `Expr::FunctionCall { name, ... }`
                // and the bytecode VM dispatches them via
                // `OpCode::BuiltinCall(Sqrt|Abs|...)`
                // (`crates/shape-vm/src/compiler/helpers.rs:4263`). MIR
                // lowering at `crates/shape-vm/src/mir/lowering/expr.rs:2003`
                // does NOT perform the equivalent producer-side
                // classification: it leaks `MirConstant::Function("sqrt")`
                // into MIR, which the JIT cannot resolve through
                // `function_indices` (the math builtin is not a user
                // function). Pre-fix the indirect-call path fell through:
                // `compile_constant` returned `iconst(I64, 0)` callee bits,
                // `jit_call_value` surface-and-stopped, returning
                // `TAG_NULL` which the F64 caller-side bitcast decoded as
                // `NaN` — the W15.1 §6.6 reproducer.
                //
                // Symmetric defect to W12-enum-constructor-mir-lowering
                // (Ok/Err/Some) and W12-collection-constructor-mir-lowering
                // (HashMap/Set/...) — both already fixed at MIR-lowering
                // time. The math-builtin family routes here instead because
                // the kinded FFI bodies (`jit_sqrt_f64`, ...) take native
                // f64 and have a much smaller signature surface than the
                // EnumStore / collection-ctor MIR shapes — the W15.1 §6.6
                // close-wave scope is the bug-class minimum (book §6.6
                // reproducer + the math-builtin family the same MIR shape
                // misses), not an extra MIR statement.
                //
                // ADR-006 §2.7.5 stamp-at-compile-time: the arg's
                // `NativeKind::Float64` (or `Int64` widened via
                // `fcvt_from_sint`) is the producing-site discriminator.
                // No NaN-box decode at the FFI boundary, no Bool-default
                // fallback per §2.7.7 #9. Result is native f64; the
                // `write_place` consumer's `ensure_kind` rebuilds the
                // destination slot's typed carrier.
                if let Operand::Constant(MirConstant::Function(name)) = func {
                    if self.function_indices.get(name.as_str()).is_none()
                        && args.len() == 1
                    {
                        use shape_vm::type_tracking::NativeKind;
                        let math_fn: Option<FuncRef> = match name.as_str() {
                            "sqrt" => Some(self.ffi.sqrt_f64),
                            "abs" => Some(self.ffi.abs_f64),
                            "floor" => Some(self.ffi.floor_f64),
                            "ceil" => Some(self.ffi.ceil_f64),
                            "round" => Some(self.ffi.round_f64),
                            "sin" => Some(self.ffi.sin_f64),
                            "cos" => Some(self.ffi.cos_f64),
                            "tan" => Some(self.ffi.tan_f64),
                            "asin" => Some(self.ffi.asin_f64),
                            "acos" => Some(self.ffi.acos_f64),
                            "atan" => Some(self.ffi.atan_f64),
                            "exp" => Some(self.ffi.exp_f64),
                            "ln" => Some(self.ffi.ln_f64),
                            _ => None,
                        };
                        if let Some(ffi_ref) = math_fn {
                            // Resolve arg to a native f64 Cranelift value.
                            // `Float64` arms widen via `compile_operand`'s
                            // F64 SSA; `Int64` arms convert via
                            // `fcvt_from_sint`. Other kinds surface (no
                            // Bool-default per §2.7.7 #9).
                            let arg_kind = self.operand_slot_kind(&args[0]);
                            let arg_val = self.compile_operand(&args[0])?;
                            let arg_ty = self.builder.func.dfg.value_type(arg_val);
                            let f64_arg = match arg_kind {
                                Some(NativeKind::Float64)
                                | Some(NativeKind::NullableFloat64) => {
                                    if arg_ty == types::F64 {
                                        arg_val
                                    } else if arg_ty == types::I64 {
                                        // Native bit-pattern carrier
                                        // (§2.7.5 widened in
                                        // `compile_operand`'s carrier
                                        // path).
                                        self.builder
                                            .ins()
                                            .bitcast(types::F64, MemFlags::new(), arg_val)
                                    } else {
                                        arg_val
                                    }
                                }
                                Some(NativeKind::Int64)
                                | Some(NativeKind::UInt64)
                                | Some(NativeKind::IntSize)
                                | Some(NativeKind::UIntSize) => {
                                    let i64_val = if arg_ty == types::I64 {
                                        arg_val
                                    } else if arg_ty == types::I32 {
                                        self.builder.ins().sextend(types::I64, arg_val)
                                    } else if arg_ty == types::I8 {
                                        self.builder.ins().sextend(types::I64, arg_val)
                                    } else {
                                        arg_val
                                    };
                                    self.builder.ins().fcvt_from_sint(types::F64, i64_val)
                                }
                                Some(NativeKind::Int32)
                                | Some(NativeKind::UInt32) => {
                                    let i32_val = if arg_ty == types::I32 {
                                        arg_val
                                    } else if arg_ty == types::I64 {
                                        self.builder.ins().ireduce(types::I32, arg_val)
                                    } else {
                                        arg_val
                                    };
                                    self.builder.ins().fcvt_from_sint(types::F64, i32_val)
                                }
                                other => {
                                    return Err(format!(
                                        "Route A surface-and-stop: \
                                         NotImplemented(SURFACE) — math \
                                         builtin `{}` arg NativeKind is \
                                         {:?}; the §2.7.5 producer-site \
                                         classification conduit does not \
                                         stamp this kind at the upstream \
                                         MIR shape. No Bool-default \
                                         fallback per ADR-006 §2.7.7 #9 \
                                         / CLAUDE.md \"Forbidden \
                                         rationalizations\". W15.2-LANG-6 \
                                         jit-math-nan-poisoning.",
                                        name, other,
                                    ));
                                }
                            };

                            let inst = self.builder.ins().call(ffi_ref, &[f64_arg]);
                            let result_f64 = self.builder.inst_results(inst)[0];

                            self.release_old_value_if_heap(destination)?;
                            self.write_place(destination, result_f64)?;
                            self.reload_referenced_locals();

                            let next_block = self.block_map.get(next).ok_or_else(|| {
                                format!(
                                    "MirToIR: unknown call continuation block {}",
                                    next
                                )
                            })?;
                            self.builder.ins().jump(*next_block, &[]);
                            return Ok(());
                        }
                    }
                }

                // ── ENUM CONSTRUCTOR PATH ─────────────────────────────
                // Qualified function calls like "Shape::Circle" that don't
                // exist in the function index are enum variant constructors.
                // The legacy path lowered the payload as a heterogeneous
                // array via the kind-blind `jit_new_array` +
                // `jit_array_push_elem` ABI (the deleted ValueWord-shape
                // path).
                //
                // Route A (ADR-006 §2.7.14 / W11-jit-new-array close) does
                // not yet provide a heterogeneous-element carrier — that's
                // the same `op_new_array` surface the VM-side handler
                // returns (`shape-vm executor/objects/object_creation.rs:
                // 316`). Per §2.7.14 forbidden list ("Bool-default fallback
                // for unknown element kinds") surface-and-stop instead of
                // fabricating a kind. Unit variants (empty args) still
                // route through the normal call dispatch below.
                if let Operand::Constant(MirConstant::Function(name)) = func {
                    if name.contains("::")
                        && self.function_indices.get(name.as_str()).is_none()
                    {
                        return Err(format!(
                            "Route A surface-and-stop: SURFACE — enum \
                             constructor `{}` depends on a heterogeneous- \
                             element-array carrier that Route A does not \
                             yet supply (parallel to op_new_array's VM-side \
                             surface in shape-vm `object_creation.rs:316`). \
                             Tracked as W11-jit-new-array follow-up per \
                             ADR-006 §2.7.4. ADR-006 §2.7.14 / §2.7.5.",
                            name
                        ));
                    }
                }

                // Resolve function ID from the func operand.
                // Direct calls use MirConstant::Function(name) → look up index.
                // Indirect calls (closures/first-class functions) fall back to
                // jit_call_value which reads the callee from the stack.
                let func_id: Option<u16> = match func {
                    Operand::Constant(MirConstant::Function(name)) => {
                        self.function_indices.get(name.as_str()).copied()
                    }
                    _ => None,
                };

                // ── Session 2: STACK-CLOSURE DIRECT-DISPATCH FAST PATH ──
                // When the callee operand loads from a slot that was
                // populated by `emit_stack_closure`, the stored value is a
                // raw `StackClosure*` — not NaN-boxed, not `HK_CLOSURE` —
                // so `jit_call_value` can't dispatch it. Instead, use the
                // slot's side-table (`stack_closure_call_info`) to look up
                // the function_id and per-capture offsets, load captures
                // from the `StackSlot` at their native width, and call the
                // corresponding user_func_ref with captures prepended to the
                // user arg list. This matches the calling convention of
                // closure bodies (first N params are captures).
                let stack_closure_info: Option<(
                    super::StackClosureCallInfo,
                    cranelift::codegen::ir::StackSlot,
                )> = match func {
                    Operand::Copy(Place::Local(slot))
                    | Operand::Move(Place::Local(slot))
                    | Operand::MoveExplicit(Place::Local(slot)) => self
                        .stack_closure_call_info
                        .get(slot)
                        .cloned()
                        .and_then(|info| {
                            self.stack_closure_slots
                                .get(slot)
                                .copied()
                                .map(|ss| (info, ss))
                        }),
                    _ => None,
                };

                if let Some((info, stack_slot)) = stack_closure_info {
                    if let Some(func_ref) =
                        self.user_func_refs.get(&info.function_id).copied()
                    {
                        // Prepend captures as args. Each capture is loaded
                        // from the stack slot at its recorded offset with
                        // its recorded native type, then widened to I64 for
                        // the uniform callee ABI.
                        let mut arg_vals = Vec::with_capacity(
                            info.capture_offsets.len() + args.len() + 1,
                        );
                        arg_vals.push(self.ctx_ptr);
                        for (off, ty) in info
                            .capture_offsets
                            .iter()
                            .zip(info.capture_types.iter())
                        {
                            let raw = self
                                .builder
                                .ins()
                                .stack_load(*ty, stack_slot, *off);
                            let widened = if *ty == types::I64 {
                                raw
                            } else if *ty == types::F64 {
                                self.builder
                                    .ins()
                                    .bitcast(types::I64, MemFlags::new(), raw)
                            } else if *ty == types::I32 || *ty == types::I16 {
                                self.builder.ins().sextend(types::I64, raw)
                            } else if *ty == types::I8 {
                                self.builder.ins().uextend(types::I64, raw)
                            } else {
                                raw
                            };
                            arg_vals.push(widened);
                        }
                        // User args follow, widened uniformly.
                        for arg in args.iter() {
                            let val = self.compile_operand(arg)?;
                            let val_ty = self.builder.func.dfg.value_type(val);
                            let boxed = if val_ty == types::I64 {
                                val
                            } else if val_ty == types::F64 {
                                self.builder
                                    .ins()
                                    .bitcast(types::I64, MemFlags::new(), val)
                            } else if val_ty == types::I32 || val_ty == types::I16 {
                                self.builder.ins().sextend(types::I64, val)
                            } else if val_ty == types::I8 {
                                self.builder.ins().uextend(types::I64, val)
                            } else {
                                val
                            };
                            arg_vals.push(boxed);
                        }

                        let inst = self.builder.ins().call(func_ref, &arg_vals);
                        let signal = self.builder.inst_results(inst)[0];

                        // Deopt: if signal < 0, propagate the error.
                        let zero = self.builder.ins().iconst(types::I32, 0);
                        let is_error = self.builder.ins().icmp(
                            IntCC::SignedLessThan,
                            signal,
                            zero,
                        );
                        let deopt_block = self.builder.create_block();
                        let continue_block = self.builder.create_block();
                        self.builder.ins().brif(
                            is_error,
                            deopt_block,
                            &[],
                            continue_block,
                            &[],
                        );
                        self.builder.switch_to_block(deopt_block);
                        self.builder.seal_block(deopt_block);
                        self.builder.ins().return_(&[signal]);

                        self.builder.switch_to_block(continue_block);
                        self.builder.seal_block(continue_block);
                        let stack_offset = crate::context::STACK_OFFSET as i32;
                        let result = self.builder.ins().load(
                            types::I64,
                            MemFlags::new(),
                            self.ctx_ptr,
                            stack_offset,
                        );

                        self.release_old_value_if_heap(destination)?;
                        self.write_place(destination, result)?;
                        self.reload_referenced_locals();

                        let next_block = self.block_map.get(next).ok_or_else(|| {
                            format!(
                                "MirToIR: unknown call continuation block {}",
                                next
                            )
                        })?;
                        self.builder.ins().jump(*next_block, &[]);
                        return Ok(());
                    }
                }

                // Check if we have a direct FuncRef for the callee.
                let func_ref = func_id.and_then(|fid| self.user_func_refs.get(&fid).copied());

                let result = if let Some(func_ref) = func_ref {
                    // ── DIRECT CALL PATH ──────────────────────────────────
                    // Compile args as SSA values and pass as native Cranelift params.
                    // ABI: fn(ctx_ptr, arg0, arg1, ..., argN) -> i32
                    // The callee stores its return value to ctx.stack[0].
                    // R4.2E: callee ABI uses uniform I64 params. Widen narrow
                    // Cranelift types inline to I64 bit-patterns (sextend /
                    // uextend / bitcast) — NOT NaN-boxing tagging.
                    let mut arg_vals = Vec::with_capacity(args.len() + 1);
                    arg_vals.push(self.ctx_ptr);
                    for arg in args.iter() {
                        let val = self.compile_operand(arg)?;
                        let val_ty = self.builder.func.dfg.value_type(val);
                        let boxed = if val_ty == types::I64 {
                            val
                        } else if val_ty == types::F64 {
                            self.builder.ins().bitcast(types::I64, MemFlags::new(), val)
                        } else if val_ty == types::I32 {
                            self.builder.ins().sextend(types::I64, val)
                        } else if val_ty == types::I8 {
                            self.builder.ins().uextend(types::I64, val)
                        } else if val_ty == types::I16 {
                            self.builder.ins().sextend(types::I64, val)
                        } else {
                            val
                        };
                        arg_vals.push(boxed);
                    }

                    let inst = self.builder.ins().call(func_ref, &arg_vals);
                    let signal = self.builder.inst_results(inst)[0];

                    // Deopt: if signal < 0, the callee encountered an error.
                    // Propagate by returning the negative signal immediately.
                    let zero = self.builder.ins().iconst(types::I32, 0);
                    let is_error =
                        self.builder
                            .ins()
                            .icmp(IntCC::SignedLessThan, signal, zero);
                    let deopt_block = self.builder.create_block();
                    let continue_block = self.builder.create_block();
                    self.builder
                        .ins()
                        .brif(is_error, deopt_block, &[], continue_block, &[]);

                    // Deopt block: return the error signal.
                    self.builder.switch_to_block(deopt_block);
                    self.builder.seal_block(deopt_block);
                    self.builder.ins().return_(&[signal]);

                    // Continue block: read return value from ctx.stack[0].
                    self.builder.switch_to_block(continue_block);
                    self.builder.seal_block(continue_block);
                    let stack_offset = crate::context::STACK_OFFSET as i32;
                    self.builder.ins().load(
                        types::I64,
                        MemFlags::new(),
                        self.ctx_ptr,
                        stack_offset,
                    )
                } else {
                    // ── INDIRECT CALL (closures/first-class functions) ────
                    //
                    // ADR-006 §2.7.7 / Q9 + §2.7.11 / Q12: every push into
                    // `JITContext.stack` writes the producing-site kind into
                    // the parallel `stack_kinds` track in lockstep. The
                    // callee's kind classifies the dispatch shape inside
                    // `jit_call_value` (Closure → raw-Arc closure path,
                    // FunctionRef / UInt64 → function-id path); per-arg
                    // kinds flow into the trampoline VM's frame setup as
                    // §2.7.11/Q12 carriers.
                    let stack_base_offset = crate::context::STACK_OFFSET as i32;
                    let sp_offset = crate::context::STACK_PTR_OFFSET as i32;

                    let old_sp = self.builder.ins().load(
                        types::I64,
                        MemFlags::new(),
                        self.ctx_ptr,
                        sp_offset,
                    );

                    // Source the callee kind from the producing site.
                    // Precise kinds — `Ptr(HeapKind::Closure)` for
                    // closure-bearing slots seeded by `infer_slot_kinds::
                    // ClosureCapture`, `UInt64` for `MirConstant::Function`
                    // inline function refs — drive the §2.7.11/Q12 callee
                    // classification at `jit_call_value` exactly. For
                    // opaque-source slots whose inference left `None`,
                    // the documented §2.7.5 carrier kind `UInt64` flows in
                    // instead — `jit_call_value`'s UInt64 arm preserves
                    // the existing JIT-internal NaN-box bit-shape dispatch
                    // (cases 1 / 2 — inline function refs and legacy
                    // HK_CLOSURE callees). NOT a Bool-default fallback
                    // (§2.7.7 #9 forbidden); `UInt64` is the §2.7.5
                    // carrier kind for I64-wide raw bits without further
                    // classification.
                    let callee_kind = self.operand_slot_kind_or_carrier(func);

                    // R4.2E: indirect-call callee pushed to ctx.stack as raw
                    // I64 bit-pattern. Widen narrow types inline — closures
                    // are already I64 in practice but the code path is type-
                    // agnostic.
                    let callee_val = self.compile_operand(func)?;
                    let callee_ty = self.builder.func.dfg.value_type(callee_val);
                    let callee_boxed = if callee_ty == types::I64 {
                        callee_val
                    } else if callee_ty == types::F64 {
                        self.builder.ins().bitcast(types::I64, MemFlags::new(), callee_val)
                    } else if callee_ty == types::I32 {
                        self.builder.ins().sextend(types::I64, callee_val)
                    } else if callee_ty == types::I8 {
                        self.builder.ins().uextend(types::I64, callee_val)
                    } else if callee_ty == types::I16 {
                        self.builder.ins().sextend(types::I64, callee_val)
                    } else {
                        callee_val
                    };
                    let callee_slot_idx = old_sp;
                    let callee_byte_off = self.builder.ins().ishl_imm(callee_slot_idx, 3);
                    let callee_abs_off = self.builder.ins().iadd_imm(callee_byte_off, stack_base_offset as i64);
                    let callee_addr = self.builder.ins().iadd(self.ctx_ptr, callee_abs_off);
                    self.builder.ins().store(MemFlags::new(), callee_boxed, callee_addr, 0);
                    // Lockstep parallel-kind track write (§2.7.7 / Q9).
                    self.emit_kind_track_write(callee_slot_idx, callee_kind);

                    // R4.2E: indirect-call args pushed to ctx.stack as raw
                    // I64 bit-patterns. Widen narrow Cranelift types inline.
                    for (i, arg) in args.iter().enumerate() {
                        // Source the arg kind from the producing site for the
                        // parallel-kind track. Falls back to `UInt64`
                        // (the §2.7.5 carrier kind for I64-wide raw bits)
                        // when the producing-site inference is opaque —
                        // not a Bool-default fallback.
                        let _ = i;
                        let arg_kind = self.operand_slot_kind_or_carrier(arg);

                        let val = self.compile_operand(arg)?;
                        let val_ty = self.builder.func.dfg.value_type(val);
                        let boxed = if val_ty == types::I64 {
                            val
                        } else if val_ty == types::F64 {
                            self.builder.ins().bitcast(types::I64, MemFlags::new(), val)
                        } else if val_ty == types::I32 {
                            self.builder.ins().sextend(types::I64, val)
                        } else if val_ty == types::I8 {
                            self.builder.ins().uextend(types::I64, val)
                        } else if val_ty == types::I16 {
                            self.builder.ins().sextend(types::I64, val)
                        } else {
                            val
                        };
                        let slot_idx = self.builder.ins().iadd_imm(old_sp, (i + 1) as i64);
                        let byte_off = self.builder.ins().ishl_imm(slot_idx, 3);
                        let abs_off = self.builder.ins().iadd_imm(byte_off, stack_base_offset as i64);
                        let store_addr = self.builder.ins().iadd(self.ctx_ptr, abs_off);
                        self.builder.ins().store(MemFlags::new(), boxed, store_addr, 0);
                        // Lockstep parallel-kind track write (§2.7.7 / Q9).
                        self.emit_kind_track_write(slot_idx, arg_kind);
                    }

                    // v2-boundary: arg_count stored as a raw i64 on ctx.stack.
                    // jit_call_value decodes this via direct `as usize` — no NaN-box.
                    //
                    // ADR-006 §2.7.11/Q12 + §2.7.5: the arg_count sentinel
                    // slot's kind is `NativeKind::UInt64` — the documented
                    // "I64-wide raw bits carrier kind" / function-id-class
                    // kind already used for FFI-boundary scalar sentinels
                    // (cf. `dispatch_call_via_trampoline_vm` callee/arg
                    // kind companion). NOT a Bool-default fallback.
                    let total_items = 1 + args.len() + 1; // callee + args + arg_count
                    let argc_slot_idx = self.builder.ins().iadd_imm(old_sp, (1 + args.len()) as i64);
                    let argc_byte_off = self.builder.ins().ishl_imm(argc_slot_idx, 3);
                    let argc_abs_off = self.builder.ins().iadd_imm(argc_byte_off, stack_base_offset as i64);
                    let argc_addr = self.builder.ins().iadd(self.ctx_ptr, argc_abs_off);
                    let argc_val = self.builder.ins().iconst(types::I64,
                        args.len() as i64);
                    self.builder.ins().store(MemFlags::new(), argc_val, argc_addr, 0);
                    // Lockstep parallel-kind track write (§2.7.7 / Q9).
                    self.emit_kind_track_write(
                        argc_slot_idx,
                        shape_value::NativeKind::UInt64,
                    );

                    // Update stack_ptr
                    let new_sp = self.builder.ins().iadd_imm(old_sp, total_items as i64);
                    self.builder.ins().store(MemFlags::new(), new_sp, self.ctx_ptr, sp_offset);

                    // jit_call_value reads callee + args + arg_count from stack
                    // AND the parallel-kind track for the §2.7.11/Q12
                    // callee-classification dispatch.
                    let inst = self.builder.ins().call(
                        self.ffi.call_value,
                        &[self.ctx_ptr],
                    );
                    self.builder.inst_results(inst)[0]
                };

                // ── r5c-2-bz-b-jit-err-surface: VM-trampoline error deopt ──
                // `jit_call_value` (via `dispatch_call_via_trampoline_vm`)
                // can hit a VM `Err` and return a value-shaped placeholder.
                // Deopt the JIT frame BEFORE `write_place` / refcount-retain
                // when `pending_call_error` is set — same shape as the
                // `MirConstant::Method` path above.
                {
                    let err_flag_offset =
                        crate::context::PENDING_CALL_ERROR_OFFSET as i32;
                    let err_flag = self.builder.ins().load(
                        types::I8,
                        MemFlags::new(),
                        self.ctx_ptr,
                        err_flag_offset,
                    );
                    let zero_i8 = self.builder.ins().iconst(types::I8, 0);
                    let has_err = self.builder.ins().icmp(
                        IntCC::NotEqual,
                        err_flag,
                        zero_i8,
                    );
                    let err_deopt_block = self.builder.create_block();
                    let err_continue_block = self.builder.create_block();
                    self.builder.ins().brif(
                        has_err,
                        err_deopt_block,
                        &[],
                        err_continue_block,
                        &[],
                    );
                    self.builder.switch_to_block(err_deopt_block);
                    self.builder.seal_block(err_deopt_block);
                    let trampoline_err_signal = self.builder.ins().iconst(
                        types::I32,
                        (crate::context::SIGNAL_TRAMPOLINE_ERROR as u32) as i64,
                    );
                    self.builder.ins().return_(&[trampoline_err_signal]);
                    self.builder.switch_to_block(err_continue_block);
                    self.builder.seal_block(err_continue_block);
                }

                // 4. Store result to destination
                self.release_old_value_if_heap(destination)?;
                self.write_place(destination, result)?;

                // 4b. Reload locals that may have been mutated via references
                self.reload_referenced_locals();

                // 5. Jump to continuation block
                let next_block = self.block_map.get(next).ok_or_else(|| {
                    format!("MirToIR: unknown call continuation block {}", next)
                })?;
                self.builder.ins().jump(*next_block, &[]);
                Ok(())
            }

            TerminatorKind::Return => {
                // Write return value to ctx.stack[0] and set return_type_tag
                // for native type preservation (avoids NaN-boxing on return path).
                let return_slot = SlotId(0);
                // W11-jit-new-array: when SlotId(0) is not declared, the
                // program has no value-bearing return (e.g. top-level ending
                // with `print(x)`). Stamp UNIT so the executor's typed
                // dispatch maps it to `WireValue::Null` — pre-W11 this
                // arm left the tag at its zero default (NANBOXED) and the
                // executor.rs:267 SURFACE fired.
                if !self.locals.contains_key(&return_slot) {
                    let tag = self.builder.ins().iconst(
                        types::I8,
                        crate::context::RETURN_TAG_UNIT as i64,
                    );
                    let tag_offset = crate::context::RETURN_TYPE_TAG_OFFSET as i32;
                    self.builder
                        .ins()
                        .store(MemFlags::new(), tag, self.ctx_ptr, tag_offset);
                    let sp_zero = self.builder.ins().iconst(types::I64, 0);
                    let sp_offset = crate::context::STACK_PTR_OFFSET as i32;
                    self.builder
                        .ins()
                        .store(MemFlags::new(), sp_zero, self.ctx_ptr, sp_offset);
                    let signal = self.builder.ins().iconst(types::I32, 0);
                    self.builder.ins().return_(&[signal]);
                    return Ok(());
                }
                if let Some(&var) = self.locals.get(&return_slot) {
                    let ret_val = self.builder.use_var(var);
                    let val_type = self.builder.func.dfg.value_type(ret_val);
                    let stack_offset = crate::context::STACK_OFFSET as i32;

                    if val_type == types::F64 {
                        // Native f64: store raw bits, set tag=1
                        let as_bits = self.builder.ins().bitcast(types::I64, MemFlags::new(), ret_val);
                        self.builder.ins().store(MemFlags::new(), as_bits, self.ctx_ptr, stack_offset);
                        let tag = self.builder.ins().iconst(types::I8, crate::context::RETURN_TAG_F64 as i64);
                        let tag_offset = crate::context::RETURN_TYPE_TAG_OFFSET as i32;
                        self.builder.ins().store(MemFlags::new(), tag, self.ctx_ptr, tag_offset);
                    } else if val_type == types::I8 {
                        // Native bool: zero-extend to I64, set tag=4
                        let extended = self.builder.ins().uextend(types::I64, ret_val);
                        self.builder.ins().store(MemFlags::new(), extended, self.ctx_ptr, stack_offset);
                        let tag = self.builder.ins().iconst(types::I8, crate::context::RETURN_TAG_BOOL as i64);
                        let tag_offset = crate::context::RETURN_TYPE_TAG_OFFSET as i32;
                        self.builder.ins().store(MemFlags::new(), tag, self.ctx_ptr, tag_offset);
                    } else if val_type == types::I32 {
                        // Native i32 (NativeKind::Int32 / UInt32): sign-extend
                        // to I64 and stamp RETURN_TAG_I32 so the host marshals
                        // the value as an integer rather than a NaN-boxed
                        // ValueWord. Pre-W11 close this arm fell through to
                        // the `RETURN_TAG_NANBOXED` default — the §2.7.5
                        // kind-source gap the executor.rs:267 SURFACE
                        // documented (W11-jit-new-array unblocks).
                        let extended = self.builder.ins().sextend(types::I64, ret_val);
                        self.builder.ins().store(MemFlags::new(), extended, self.ctx_ptr, stack_offset);
                        let tag = self.builder.ins().iconst(types::I8, crate::context::RETURN_TAG_I32 as i64);
                        let tag_offset = crate::context::RETURN_TYPE_TAG_OFFSET as i32;
                        self.builder.ins().store(MemFlags::new(), tag, self.ctx_ptr, tag_offset);
                    } else {
                        // I64. Under strict typing the return-slot kind is
                        // statically known: an `Int64` slot is a raw native
                        // integer (RETURN_TAG_I64); a slot with no inferred
                        // kind is a `()` / no-value return (RETURN_TAG_UNIT);
                        // any other kind that happened to widen to I64 in
                        // Cranelift (Ptr to a v2 heap value, String, etc.)
                        // is still the deleted NaN-boxed ABI's residual —
                        // those paths hit the executor.rs:267 SURFACE.
                        //
                        // W11-jit-new-array stamps RETURN_TAG_I64 from the
                        // bytecode compiler's slot kind (§2.7.5 stamp-at-
                        // compile-time): when the return slot's
                        // `NativeKind` is `Int64`/`UInt64`/`IntSize`/
                        // `UIntSize` (and their nullable variants), the
                        // value is a raw native integer. `None` kind means
                        // the slot was never written with a value-bearing
                        // expression (typical for top-level programs ending
                        // in `print(x)`) — stamp UNIT. All other I64 arms
                        // fall through to RETURN_TAG_NANBOXED — the §2.7.5
                        // kind-source gap surfaced at `executor.rs:267`.
                        use shape_vm::type_tracking::NativeKind;
                        let return_kind = super::types::slot_kind_for_local(
                            &self.slot_kinds,
                            return_slot.0,
                        );
                        let raw_int = matches!(
                            return_kind,
                            Some(
                                NativeKind::Int64
                                    | NativeKind::UInt64
                                    | NativeKind::IntSize
                                    | NativeKind::UIntSize
                                    | NativeKind::NullableInt64
                                    | NativeKind::NullableUInt64
                                    | NativeKind::NullableIntSize
                                    | NativeKind::NullableUIntSize
                            )
                        );
                        self.builder.ins().store(MemFlags::new(), ret_val, self.ctx_ptr, stack_offset);
                        let tag_value = if raw_int {
                            crate::context::RETURN_TAG_I64
                        } else if return_kind.is_none() {
                            crate::context::RETURN_TAG_UNIT
                        } else {
                            crate::context::RETURN_TAG_NANBOXED
                        };
                        let tag = self.builder.ins().iconst(types::I8, tag_value as i64);
                        let tag_offset = crate::context::RETURN_TYPE_TAG_OFFSET as i32;
                        self.builder.ins().store(MemFlags::new(), tag, self.ctx_ptr, tag_offset);
                    }

                    // Set stack_ptr to 1
                    let one = self.builder.ins().iconst(types::I64, 1);
                    let sp_offset = crate::context::STACK_PTR_OFFSET as i32;
                    self.builder
                        .ins()
                        .store(MemFlags::new(), one, self.ctx_ptr, sp_offset);
                }

                let signal = self.builder.ins().iconst(types::I32, 0);
                self.builder.ins().return_(&[signal]);
                Ok(())
            }

            TerminatorKind::Unreachable => {
                self.builder.ins().trap(TrapCode::User(0));
                Ok(())
            }
        }
    }

    /// W10 jit-call-method-user-trait-fix (2026-05-17): emit a user-type
    /// operator-trait dispatch as a method-call equivalent, writing the
    /// result into `destination`.
    ///
    /// Mirror of the `MirConstant::Method` `TerminatorKind::Call` path at
    /// `compile_terminator` (this file, lines ~315-431), reused for the
    /// MIR `Rvalue::BinaryOp` / `Rvalue::UnaryOp` sites whose source span
    /// is recorded in `operator_trait_dispatch_sites` (populated at
    /// bytecode-compile time at `crates/shape-vm/src/compiler/expressions/
    /// binary_ops.rs::emit_operator_trait_call` and the parallel Neg/Not
    /// sites at `crates/shape-vm/src/compiler/expressions/unary_ops.rs`).
    ///
    /// `receiver_operands` is a single-element slice with the receiver
    /// (lhs for binary, operand for unary). `extra_args` is the remainder
    /// (rhs for binary; empty for unary). The split shape mirrors the
    /// bytecode-side `OpCode::CallMethod` ABI where the receiver is
    /// `args[0]` and any explicit method arguments follow.
    ///
    /// Does NOT emit a continuation jump (the caller is inside a
    /// `StatementKind::Assign` handler, not a Call terminator).
    pub(crate) fn emit_user_trait_method_call(
        &mut self,
        method_name: &str,
        receiver_operands: &[shape_vm::mir::types::Operand],
        extra_args: &[shape_vm::mir::types::Operand],
        destination: &shape_vm::mir::types::Place,
    ) -> Result<(), String> {
        let stack_base_offset = crate::context::STACK_OFFSET as i32;
        let sp_offset = crate::context::STACK_PTR_OFFSET as i32;

        let old_sp = self.builder.ins().load(
            types::I64,
            MemFlags::new(),
            self.ctx_ptr,
            sp_offset,
        );

        // ADR-006 §2.7.7 / Q9 lockstep: every data push stamps the parallel-
        // kind track in the same slot. Mirrors `compile_terminator`'s
        // `MirConstant::Method` arm: args[0] = receiver, args[1..] =
        // explicit method arguments. Each operand is widened to 8-byte I64
        // per the JIT-stack ABI; no NaN-box tagging.
        let combined: Vec<&shape_vm::mir::types::Operand> = receiver_operands
            .iter()
            .chain(extra_args.iter())
            .collect();
        for (i, arg) in combined.iter().enumerate() {
            let arg_kind = self.operand_slot_kind_or_carrier(arg);
            let val = self.compile_operand(arg)?;
            let val_ty = self.builder.func.dfg.value_type(val);
            let boxed = if val_ty == types::I64 {
                val
            } else if val_ty == types::F64 {
                self.builder
                    .ins()
                    .bitcast(types::I64, MemFlags::new(), val)
            } else if val_ty == types::I32 {
                self.builder.ins().sextend(types::I64, val)
            } else if val_ty == types::I8 {
                self.builder.ins().uextend(types::I64, val)
            } else if val_ty == types::I16 {
                self.builder.ins().sextend(types::I64, val)
            } else {
                val
            };
            let slot_idx = self.builder.ins().iadd_imm(old_sp, i as i64);
            let byte_off = self.builder.ins().ishl_imm(slot_idx, 3);
            let abs_off = self
                .builder
                .ins()
                .iadd_imm(byte_off, stack_base_offset as i64);
            let store_addr = self.builder.ins().iadd(self.ctx_ptr, abs_off);
            self.builder
                .ins()
                .store(MemFlags::new(), boxed, store_addr, 0);
            self.emit_kind_track_write(slot_idx, arg_kind);
        }

        // Push method name (heap String) — kind = NativeKind::String.
        let method_str_bits =
            crate::ffi::value_ffi::box_string(method_name.to_string());
        let method_val = self
            .builder
            .ins()
            .iconst(types::I64, method_str_bits as i64);
        let method_slot_idx = self
            .builder
            .ins()
            .iadd_imm(old_sp, combined.len() as i64);
        let method_byte_off = self.builder.ins().ishl_imm(method_slot_idx, 3);
        let method_abs_off = self
            .builder
            .ins()
            .iadd_imm(method_byte_off, stack_base_offset as i64);
        let method_addr = self.builder.ins().iadd(self.ctx_ptr, method_abs_off);
        self.builder
            .ins()
            .store(MemFlags::new(), method_val, method_addr, 0);
        self.emit_kind_track_write(
            method_slot_idx,
            shape_value::NativeKind::String,
        );

        // Push arg_count = explicit args (excludes receiver) — UInt64 carrier.
        let actual_arg_count = extra_args.len() as i64;
        let argc_val = self.builder.ins().iconst(types::I64, actual_arg_count);
        let argc_slot_idx = self
            .builder
            .ins()
            .iadd_imm(old_sp, (combined.len() + 1) as i64);
        let argc_byte_off = self.builder.ins().ishl_imm(argc_slot_idx, 3);
        let argc_abs_off = self
            .builder
            .ins()
            .iadd_imm(argc_byte_off, stack_base_offset as i64);
        let argc_addr = self.builder.ins().iadd(self.ctx_ptr, argc_abs_off);
        self.builder
            .ins()
            .store(MemFlags::new(), argc_val, argc_addr, 0);
        self.emit_kind_track_write(
            argc_slot_idx,
            shape_value::NativeKind::UInt64,
        );

        // Update stack_ptr: receiver(s) + extra_args + method_name + arg_count.
        let total_items = combined.len() + 2;
        let new_sp = self.builder.ins().iadd_imm(old_sp, total_items as i64);
        self.builder
            .ins()
            .store(MemFlags::new(), new_sp, self.ctx_ptr, sp_offset);

        // Call jit_call_method(ctx, total_count).
        let count_val = self.builder.ins().iconst(types::I64, total_items as i64);
        let inst = self.builder.ins().call(
            self.ffi.call_method,
            &[self.ctx_ptr, count_val],
        );
        let result = self.builder.inst_results(inst)[0];

        // Restore stack_ptr to old value.
        self.builder
            .ins()
            .store(MemFlags::new(), old_sp, self.ctx_ptr, sp_offset);

        // ── r5c-2-bz-b-jit-err-surface: VM-trampoline error deopt ─────────
        // Same shape as the `MirConstant::Method` Call-terminator path
        // (this file, ~line 421): when the VM-side operator-trait handler
        // surfaces an `Err`, `jit_call_method` raised `pending_call_error`
        // and the returned bits are a placeholder. Deopt the JIT frame
        // (return `SIGNAL_TRAMPOLINE_ERROR`) BEFORE `write_place` so the
        // placeholder never flows into a heap-kinded refcount-retain site.
        let err_flag_offset =
            crate::context::PENDING_CALL_ERROR_OFFSET as i32;
        let err_flag = self.builder.ins().load(
            types::I8,
            MemFlags::new(),
            self.ctx_ptr,
            err_flag_offset,
        );
        let zero_i8 = self.builder.ins().iconst(types::I8, 0);
        let has_err = self.builder.ins().icmp(
            IntCC::NotEqual,
            err_flag,
            zero_i8,
        );
        let err_deopt_block = self.builder.create_block();
        let err_continue_block = self.builder.create_block();
        self.builder.ins().brif(
            has_err,
            err_deopt_block,
            &[],
            err_continue_block,
            &[],
        );
        self.builder.switch_to_block(err_deopt_block);
        self.builder.seal_block(err_deopt_block);
        let trampoline_err_signal = self.builder.ins().iconst(
            types::I32,
            (crate::context::SIGNAL_TRAMPOLINE_ERROR as u32) as i64,
        );
        self.builder.ins().return_(&[trampoline_err_signal]);

        self.builder.switch_to_block(err_continue_block);
        self.builder.seal_block(err_continue_block);

        // Write result to destination + reload referenced locals (per
        // the standard Call-terminator wind-down).
        self.write_place(destination, result)?;
        self.reload_referenced_locals();

        Ok(())
    }
}