aver-lang 0.19.0

VM and transpiler for Aver, a statically-typed language designed for AI-assisted development
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
//! `Map<K, V>` helper bodies — per-instantiation hashtable primitives.
//!
//! Strategy: monomorphise per (K, V), same as Vector / Option. Each
//! instantiation owns four wasm fns (`empty`, `set`, `get`, `len`)
//! plus shares one pair of K-keyed helpers (`hash<K>`, `eq<K, K>`)
//! across every `Map<K, *>` that uses the same K.
//!
//! Phase-3c MVP only emits these for `K = String` (the bench shape).
//! Other K kinds surface as Unimplemented when their hash / eq would
//! need writing — see `emit_hash_for` / `emit_eq_for`.
//!
//! Open-addressing layout, fixed initial capacity, no resize:
//!
//! ```text
//! struct $map_KV {
//!   mut i32 size;
//!   mut i32 cap;
//!   mut (ref null $keys_array)   keys;
//!   mut (ref null $values_array) values;
//! }
//! ```
//!
//! Empty slot marker = `keys[i] == null` (only valid for `K` that
//! cannot legitimately be null, which Aver guarantees for ref types
//! since the type system rejects null at the source level).
//!
//! Resize is a phase-3c+ extension. Cap is fixed at 16384 entries
//! today — large enough for the bench scenarios (5000 keys), small
//! enough to keep the `array.new_default` allocation under wasmtime's
//! GC heap pressure threshold.

use std::collections::HashMap;

use wasm_encoder::{BlockType, CodeSection, Function, HeapType, Instruction, RefType, ValType};

use super::WasmGcError;
use super::types::{MapSlots, TypeRegistry};
use super::wat_helper;

/// Initial bucket count — power of two so masking with `cap-1`
/// instead of `i32.rem_u` works. Sized for the bench scenarios.
const INITIAL_CAP: i32 = 16384;

#[derive(Debug, Clone, Copy)]
pub(super) struct KeyHelpers {
    /// `hash : (ref null $K) -> i32`
    pub(super) hash: u32,
    /// `eq : (ref null $K, ref null $K) -> i32`
    pub(super) eq: u32,
}

#[derive(Debug, Clone, Copy)]
pub(super) struct MapKVHelpers {
    pub(super) empty: u32,
    /// Clone-on-write `set` — allocates fresh keys/values arrays,
    /// `array.copy`s them, mutates the copies. Used when `ir::alias`
    /// flags the call site's map slot as alias-prone.
    pub(super) set: u32,
    /// In-place `set` — probes the source map's keys/values arrays
    /// directly, `array.set`s into them, returns a struct.new wrapping
    /// the same arrays with the updated size. Sound only when the IR
    /// alias pass + last-use proves the map slot is uniquely owned.
    pub(super) set_in_place: u32,
    pub(super) get: u32,
    pub(super) len: u32,
    /// `get_or_default(m, k, default) -> V`. Fused shape that backs
    /// `Option.withDefault(Map.get(m, k), default)` without ever
    /// allocating an `Option<V>`. Same probe loop as `get` but
    /// returns `values[idx]` directly on a key match and the supplied
    /// default on an empty slot.
    pub(super) get_or_default: u32,
    /// `get_pair(m, k) -> (i32 found, V value)`. Multi-result return
    /// that backs the fused `match Map.get(m, k) { Some(v) -> ...;
    /// None -> ... }` shape. Caller pops `value` into the binding
    /// slot, then branches on `found` — no Option<V> ever allocates.
    pub(super) get_pair: u32,
    /// `keys(m) -> List<K>`. Walks the keys array right-to-left,
    /// cons-prepending each occupied entry. Order is hash-bucket
    /// order, not insertion order.
    pub(super) keys: u32,
    /// `values(m) -> List<V>`. Same shape as `keys` but pulls from
    /// the values array, only when the corresponding key slot is
    /// occupied (`keys[i] != null`).
    pub(super) values: u32,
    /// `remove(m, k) -> m`. Linear-probe locate of `k`, then
    /// backwards-shift the contiguous probe chain so subsequent
    /// `get` calls still find their entries. Mutates `m` in place
    /// and returns the same handle (Aver semantics: same shape as
    /// `set`, the returned ref is structurally equal).
    pub(super) remove: u32,
    /// `entries(m) -> List<Tuple<K, V>>`. Right-to-left walk; per
    /// occupied slot builds a Tuple and prepends onto a cons list.
    pub(super) entries: u32,
    /// `from_list(l) -> Map<K, V>`. Walks `l`, struct.get's the
    /// (K, V) from each tuple, calls the per-(K, V) `set` helper.
    pub(super) from_list: u32,
    /// `__eq_Map<K,V>(a, b) -> i32`. Structural eq — `a.size ==
    /// b.size && ∀ k ∈ a: get(b, k) == Some(a[k])`. Insertion order
    /// is intentionally ignored (matches VM's `HashMap` PartialEq +
    /// the Python/Java/Rust/Haskell mainstream).
    pub(super) eq: u32,
    /// `__hash_Map<K,V>(m) -> i32`. Order-independent commutative
    /// fold — `h = 0; for (k, v) in m: h ^= djb2(k) * 33 + djb2(v)`.
    /// XOR is commutative + associative so the result is invariant
    /// to bucket ordering.
    pub(super) hash: u32,
}

#[derive(Default)]
pub(super) struct MapHelperRegistry {
    /// K (Aver type string) → its hash+eq fn indices.
    key: HashMap<String, KeyHelpers>,
    /// Canonical `Map<K, V>` → its four method indices.
    kv: HashMap<String, MapKVHelpers>,
    /// Insertion order — drives type-section + code-section emit.
    key_order: Vec<String>,
    kv_order: Vec<String>,
    /// Per-helper wasm type indices (parallel to fn indices). Stored
    /// here so the type section emit can look them up.
    key_type_indices: HashMap<String, (u32, u32)>, // (hash type idx, eq type idx)
    kv_type_indices: HashMap<String, MapKVTypeIdx>,
}

#[derive(Debug, Clone, Copy)]
struct MapKVTypeIdx {
    empty: u32,
    /// Same wasm-fn type as `set_in_place` — `(map, k, v) -> map`.
    /// Two distinct type entries because the fn-type table is
    /// indexed by type-idx, not shape, and `assign_slots` writes
    /// each helper to its own slot.
    set: u32,
    set_in_place: u32,
    get: u32,
    len: u32,
    get_or_default: u32,
    get_pair: u32,
    keys: u32,
    values: u32,
    remove: u32,
    entries: u32,
    from_list: u32,
    eq: u32,
    hash: u32,
}

impl MapHelperRegistry {
    /// Register all helpers needed for the given map instantiations.
    /// Must be called after `BuiltinRegistry::assign_slots` so the
    /// fn-idx counter is past user fns + pure builtins.
    pub(super) fn assign_slots(
        &mut self,
        map_canonicals: &[String],
        registry: &TypeRegistry,
        next_wasm_fn_idx: &mut u32,
        next_type_idx: &mut u32,
    ) -> Result<(), WasmGcError> {
        // Collect unique K names in the same order Maps appear.
        // Adds an extra synthetic K="String" up front when any
        // user-record K transitively has a String field — the
        // record's hash/eq body delegates to `hash<String>` /
        // `eq<String>` which therefore must be registered in the
        // same module even when no `Map<String, V>` is reachable
        // from the surface code.
        let mut k_names: Vec<String> = Vec::new();
        let mut k_seen: std::collections::HashSet<String> = std::collections::HashSet::new();
        for canonical in map_canonicals {
            let (k_aver, v_aver) =
                super::types::parse_map_kv(canonical).ok_or(WasmGcError::Validation(format!(
                    "MapHelperRegistry: cannot parse K, V from `{canonical}`"
                )))?;
            // If K is a record / sum whose fields include `String`,
            // ensure String hash/eq is registered first.
            let mut needs_string = false;
            if let Some(fs) = registry.record_fields.get(k_aver) {
                needs_string |= fs.iter().any(|(_, t)| t.trim() == "String");
            }
            if registry
                .variants
                .values()
                .flat_map(|v| v.iter())
                .any(|v| v.parent == k_aver)
            {
                needs_string |= registry
                    .variants
                    .values()
                    .flat_map(|vs| vs.iter())
                    .filter(|v| v.parent == k_aver)
                    .any(|v| v.fields.iter().any(|t| t.trim() == "String"));
            }
            // Map<K,V>'s structural eq + hash dispatches V via the
            // same `__hash_<V>` / `__eq_<V>` helper map K uses, so V
            // is force-registered as pseudo-K too. Skips primitive V
            // (the body emitter falls back to inline cmp for those).
            let v_aver_trim = v_aver.trim();
            if v_aver_trim == "String" {
                needs_string = true;
            }
            if needs_string && k_seen.insert("String".into()) {
                k_names.push("String".into());
            }
            if k_seen.insert(k_aver.to_string()) {
                k_names.push(k_aver.to_string());
            }
            if !super::types::TypeRegistry::is_primitive_map_key(v_aver_trim)
                && v_aver_trim != "String"
                && k_seen.insert(v_aver_trim.to_string())
            {
                k_names.push(v_aver_trim.to_string());
            }
        }

        // For every record / sum K, recursively collect all
        // records / sums used as field types. Each nested type
        // needs its own hash + eq helpers so the outer K's per-
        // field dispatch can call them. Pseudo-K = registered for
        // helpers but with no `Map<X, *>` reachable.
        let mut to_visit: Vec<String> = k_names
            .iter()
            .filter(|n| {
                registry.record_type_idx(n).is_some()
                    || registry
                        .variants
                        .values()
                        .flat_map(|v| v.iter())
                        .any(|v| v.parent == *n.as_str())
            })
            .cloned()
            .collect();
        while let Some(parent) = to_visit.pop() {
            // Collect every field type referenced by this parent
            // (record fields, or every variant's fields if it's a
            // sum type).
            let mut field_types: Vec<String> = Vec::new();
            if let Some(fields) = registry.record_fields.get(&parent) {
                for (_, t) in fields {
                    field_types.push(t.trim().to_string());
                }
            }
            for variant in registry
                .variants
                .values()
                .flat_map(|v| v.iter())
                .filter(|v| v.parent == parent)
            {
                for t in &variant.fields {
                    field_types.push(t.trim().to_string());
                }
            }
            for ft in field_types {
                let is_record = registry.record_type_idx(&ft).is_some();
                let is_sum = registry
                    .variants
                    .values()
                    .flat_map(|v| v.iter())
                    .any(|v| v.parent == ft);
                let is_carrier = ft.starts_with("Option<")
                    || ft.starts_with("Result<")
                    || ft.starts_with("Tuple<");
                if (is_record || is_sum || is_carrier) && k_seen.insert(ft.clone()) {
                    k_names.push(ft.clone());
                    if !is_carrier {
                        // Records / sums recurse into their own
                        // fields. Carriers don't (their helper bodies
                        // delegate via Call to eq_helpers/hash_
                        // helpers, which handle inner types
                        // themselves).
                        to_visit.push(ft.clone());
                    }
                    // String inside the nested type's fields →
                    // force-register String. Carriers' name
                    // contains "String" only when an inner type is
                    // literally `String`; cheap heuristic.
                    let mut nested_needs_string = false;
                    if let Some(fs) = registry.record_fields.get(&ft) {
                        nested_needs_string |= fs.iter().any(|(_, t)| t.trim() == "String");
                    }
                    if is_sum {
                        nested_needs_string |= registry
                            .variants
                            .values()
                            .flat_map(|vs| vs.iter())
                            .filter(|v| v.parent == ft)
                            .any(|v| v.fields.iter().any(|t| t.trim() == "String"));
                    }
                    if is_carrier {
                        nested_needs_string |= ft.contains("String");
                    }
                    if nested_needs_string && k_seen.insert("String".into()) {
                        k_names.push("String".into());
                    }
                }
            }
        }

        // First pass: assign K-keyed helpers (hash, eq) per unique K.
        for k_aver in &k_names {
            if !self.key.contains_key(k_aver) {
                let hash_type_idx = *next_type_idx;
                *next_type_idx += 1;
                let eq_type_idx = *next_type_idx;
                *next_type_idx += 1;
                let hash_fn = *next_wasm_fn_idx;
                *next_wasm_fn_idx += 1;
                let eq_fn = *next_wasm_fn_idx;
                *next_wasm_fn_idx += 1;
                self.key.insert(
                    k_aver.clone(),
                    KeyHelpers {
                        hash: hash_fn,
                        eq: eq_fn,
                    },
                );
                self.key_type_indices
                    .insert(k_aver.clone(), (hash_type_idx, eq_type_idx));
                self.key_order.push(k_aver.clone());
            }
        }

        // Second pass: per (K, V) helpers.
        for canonical in map_canonicals {
            if self.kv.contains_key(canonical) {
                continue;
            }
            // Nine fn type slots and nine fn idx slots:
            // empty, set, get, len, get_or_default, get_pair, keys,
            // values, remove.
            let empty_type_idx = *next_type_idx;
            *next_type_idx += 1;
            let set_type_idx = *next_type_idx;
            *next_type_idx += 1;
            let set_in_place_type_idx = *next_type_idx;
            *next_type_idx += 1;
            let get_type_idx = *next_type_idx;
            *next_type_idx += 1;
            let len_type_idx = *next_type_idx;
            *next_type_idx += 1;
            let god_type_idx = *next_type_idx;
            *next_type_idx += 1;
            let pair_type_idx = *next_type_idx;
            *next_type_idx += 1;
            let keys_type_idx = *next_type_idx;
            *next_type_idx += 1;
            let values_type_idx = *next_type_idx;
            *next_type_idx += 1;
            let remove_type_idx = *next_type_idx;
            *next_type_idx += 1;
            let entries_type_idx = *next_type_idx;
            *next_type_idx += 1;
            let from_list_type_idx = *next_type_idx;
            *next_type_idx += 1;
            let eq_type_idx = *next_type_idx;
            *next_type_idx += 1;
            let hash_type_idx = *next_type_idx;
            *next_type_idx += 1;
            let empty_fn = *next_wasm_fn_idx;
            *next_wasm_fn_idx += 1;
            let set_fn = *next_wasm_fn_idx;
            *next_wasm_fn_idx += 1;
            let set_in_place_fn = *next_wasm_fn_idx;
            *next_wasm_fn_idx += 1;
            let get_fn = *next_wasm_fn_idx;
            *next_wasm_fn_idx += 1;
            let len_fn = *next_wasm_fn_idx;
            *next_wasm_fn_idx += 1;
            let god_fn = *next_wasm_fn_idx;
            *next_wasm_fn_idx += 1;
            let pair_fn = *next_wasm_fn_idx;
            *next_wasm_fn_idx += 1;
            let keys_fn = *next_wasm_fn_idx;
            *next_wasm_fn_idx += 1;
            let values_fn = *next_wasm_fn_idx;
            *next_wasm_fn_idx += 1;
            let remove_fn = *next_wasm_fn_idx;
            *next_wasm_fn_idx += 1;
            let entries_fn = *next_wasm_fn_idx;
            *next_wasm_fn_idx += 1;
            let from_list_fn = *next_wasm_fn_idx;
            *next_wasm_fn_idx += 1;
            let eq_fn = *next_wasm_fn_idx;
            *next_wasm_fn_idx += 1;
            let hash_fn = *next_wasm_fn_idx;
            *next_wasm_fn_idx += 1;

            // K can be String, a user-defined record (field-by-field
            // hash + eq), or a primitive (Int / Float / Bool). Primitive
            // keys are boxed into a per-K struct ref so the open-
            // addressing `keys[i] == null` empty marker still holds.
            let (k_aver, _) = super::types::parse_map_kv(canonical).ok_or(
                WasmGcError::Validation(format!("bad map canonical `{canonical}`")),
            )?;
            let is_primitive_k = super::types::TypeRegistry::is_primitive_map_key(k_aver);
            let is_sum_k = registry
                .variants
                .values()
                .flat_map(|v| v.iter())
                .any(|v| v.parent == k_aver);
            let is_carrier_k = k_aver.starts_with("Option<")
                || k_aver.starts_with("Result<")
                || k_aver.starts_with("Tuple<");
            let is_list_or_vec_k = k_aver.starts_with("List<") || k_aver.starts_with("Vector<");
            let is_map_k = k_aver.starts_with("Map<");
            if k_aver != "String"
                && registry.record_type_idx(k_aver).is_none()
                && !is_primitive_k
                && !is_sum_k
                && !is_carrier_k
                && !is_list_or_vec_k
                && !is_map_k
            {
                return Err(WasmGcError::Unimplemented(
                    "phase 3c — Map<K, V> with K not String / user-record / sum / \
                     primitive / generic-carrier (Option/Result/Tuple) / List<T> / \
                     Vector<T> / Map<K2,V2>",
                ));
            }

            self.kv.insert(
                canonical.clone(),
                MapKVHelpers {
                    empty: empty_fn,
                    set: set_fn,
                    set_in_place: set_in_place_fn,
                    get: get_fn,
                    len: len_fn,
                    get_or_default: god_fn,
                    get_pair: pair_fn,
                    keys: keys_fn,
                    values: values_fn,
                    remove: remove_fn,
                    entries: entries_fn,
                    from_list: from_list_fn,
                    eq: eq_fn,
                    hash: hash_fn,
                },
            );
            self.kv_type_indices.insert(
                canonical.clone(),
                MapKVTypeIdx {
                    empty: empty_type_idx,
                    set: set_type_idx,
                    set_in_place: set_in_place_type_idx,
                    get: get_type_idx,
                    len: len_type_idx,
                    get_or_default: god_type_idx,
                    get_pair: pair_type_idx,
                    keys: keys_type_idx,
                    values: values_type_idx,
                    remove: remove_type_idx,
                    entries: entries_type_idx,
                    from_list: from_list_type_idx,
                    eq: eq_type_idx,
                    hash: hash_type_idx,
                },
            );
            self.kv_order.push(canonical.clone());
        }
        Ok(())
    }

    pub(super) fn key_helpers(&self, k_aver: &str) -> Option<KeyHelpers> {
        self.key.get(k_aver).copied()
    }

    pub(super) fn kv_helpers(&self, canonical: &str) -> Option<MapKVHelpers> {
        self.kv.get(canonical).copied()
    }

    /// Emit fn-type entries (in slot order) for every registered
    /// helper. Caller's `TypeSection` must be at `next_type_idx`'s
    /// starting position from the assign_slots call.
    pub(super) fn emit_helper_types(
        &self,
        types: &mut wasm_encoder::TypeSection,
        registry: &TypeRegistry,
    ) -> Result<(), WasmGcError> {
        for k_aver in &self.key_order {
            let k_val = super::types::aver_to_wasm(k_aver, Some(registry))?.ok_or(
                WasmGcError::Validation(format!("Map K `{k_aver}` has no wasm rep")),
            )?;
            // hash : (K) -> i32
            types.ty().function([k_val], [ValType::I32]);
            // eq : (K, K) -> i32
            types.ty().function([k_val, k_val], [ValType::I32]);
        }
        for canonical in &self.kv_order {
            let slots = registry
                .map_slots(canonical)
                .ok_or(WasmGcError::Validation(format!(
                    "Map slots missing for `{canonical}`"
                )))?;
            let map_ref = ValType::Ref(RefType {
                nullable: true,
                heap_type: HeapType::Concrete(slots.map),
            });
            let (k_aver, v_aver) = super::types::parse_map_kv(canonical).ok_or(
                WasmGcError::Validation(format!("parse_map_kv `{canonical}`")),
            )?;
            let k_val = super::types::aver_to_wasm(k_aver, Some(registry))?.ok_or(
                WasmGcError::Validation(format!("Map K `{k_aver}` has no wasm rep")),
            )?;
            let v_val = super::types::aver_to_wasm(v_aver, Some(registry))?.ok_or(
                WasmGcError::Validation(format!("Map V `{v_aver}` has no wasm rep")),
            )?;
            let opt_idx = registry
                .option_type_idx(&format!("Option<{v_aver}>"))
                .ok_or(WasmGcError::Validation(format!(
                    "Option<{v_aver}> not registered (Map.get needs it)"
                )))?;
            let opt_ref = ValType::Ref(RefType {
                nullable: true,
                heap_type: HeapType::Concrete(opt_idx),
            });

            // empty : () -> Map
            types.ty().function([], [map_ref]);
            // set : (Map, K, V) -> Map (clone-on-write)
            types.ty().function([map_ref, k_val, v_val], [map_ref]);
            // set_in_place : (Map, K, V) -> Map (alias-free fast path)
            types.ty().function([map_ref, k_val, v_val], [map_ref]);
            // get : (Map, K) -> Option<V>
            types.ty().function([map_ref, k_val], [opt_ref]);
            // len : (Map) -> i64
            types.ty().function([map_ref], [ValType::I64]);
            // get_or_default : (Map, K, V) -> V
            types.ty().function([map_ref, k_val, v_val], [v_val]);
            // get_pair : (Map, K) -> (i32 found, V value) — multi-result
            types.ty().function([map_ref, k_val], [ValType::I32, v_val]);
            // keys : (Map) -> List<K>
            let list_k_idx = registry.list_type_idx(&format!("List<{k_aver}>")).ok_or(
                WasmGcError::Validation(format!("Map.keys: List<{k_aver}> not registered")),
            )?;
            let list_k_ref = ValType::Ref(RefType {
                nullable: true,
                heap_type: HeapType::Concrete(list_k_idx),
            });
            types.ty().function([map_ref], [list_k_ref]);
            // values : (Map) -> List<V>
            let list_v_idx = registry.list_type_idx(&format!("List<{v_aver}>")).ok_or(
                WasmGcError::Validation(format!("Map.values: List<{v_aver}> not registered")),
            )?;
            let list_v_ref = ValType::Ref(RefType {
                nullable: true,
                heap_type: HeapType::Concrete(list_v_idx),
            });
            types.ty().function([map_ref], [list_v_ref]);
            // remove : (Map, K) -> Map
            types.ty().function([map_ref, k_val], [map_ref]);
            // entries : (Map) -> List<Tuple<K, V>>
            let tup_canonical = format!("Tuple<{k_aver},{v_aver}>");
            let tup_idx =
                registry
                    .tuple_type_idx(&tup_canonical)
                    .ok_or(WasmGcError::Validation(format!(
                        "Map.entries: `{tup_canonical}` not registered"
                    )))?;
            let lt_idx = registry
                .list_type_idx(&format!("List<{tup_canonical}>"))
                .ok_or(WasmGcError::Validation(format!(
                    "Map.entries: `List<{tup_canonical}>` not registered"
                )))?;
            let lt_ref = ValType::Ref(RefType {
                nullable: true,
                heap_type: HeapType::Concrete(lt_idx),
            });
            types.ty().function([map_ref], [lt_ref]);
            // from_list : (List<Tuple<K, V>>) -> Map
            types.ty().function([lt_ref], [map_ref]);
            // __eq_Map<K,V> : (eqref, eqref) -> i32. Eqref params so
            // record/sum/list/vec field dispatch can call uniformly
            // (the body ref.casts both args to the typed map ref).
            let eq_ref = ValType::Ref(RefType {
                nullable: true,
                heap_type: wasm_encoder::HeapType::Abstract {
                    shared: false,
                    ty: wasm_encoder::AbstractHeapType::Eq,
                },
            });
            types.ty().function([eq_ref, eq_ref], [ValType::I32]);
            // __hash_Map<K,V> : (eqref) -> i32. Same eqref-shape
            // calling convention as the carrier hash helpers.
            types.ty().function([eq_ref], [ValType::I32]);
            let _ = opt_ref;
            let _ = tup_idx;
        }
        Ok(())
    }

    /// Emit one `funcs.function(<type_idx>)` entry per registered
    /// helper, in the same order as `emit_helper_types`.
    pub(super) fn emit_function_section(&self, funcs: &mut wasm_encoder::FunctionSection) {
        for k in &self.key_order {
            let (h, e) = self.key_type_indices[k];
            funcs.function(h);
            funcs.function(e);
        }
        for canonical in &self.kv_order {
            let t = self.kv_type_indices[canonical];
            funcs.function(t.empty);
            funcs.function(t.set);
            funcs.function(t.set_in_place);
            funcs.function(t.get);
            funcs.function(t.len);
            funcs.function(t.get_or_default);
            funcs.function(t.get_pair);
            funcs.function(t.keys);
            funcs.function(t.values);
            funcs.function(t.remove);
            funcs.function(t.entries);
            funcs.function(t.from_list);
            funcs.function(t.eq);
            funcs.function(t.hash);
        }
    }

    /// Emit code bodies for every registered helper, in the same
    /// order as `emit_helper_types`.
    pub(super) fn emit_helper_bodies(
        &self,
        codes: &mut CodeSection,
        registry: &TypeRegistry,
        list_eq_hash: &HashMap<String, (u32, u32)>,
        carrier_eq_hash: &HashMap<String, (u32, u32)>,
    ) -> Result<(), WasmGcError> {
        let string_key_helpers = self.key.get("String").copied();
        // Snapshot every K's helpers — record hash/eq dispatch
        // needs to call helpers for nested record fields. Plus
        // virtual entries for `List<T>` field types so hash/eq
        // dispatch can call into list_helpers without a
        // separate cross-module lookup. Carriers (Option / Result
        // / Tuple) come in through `carrier_eq_hash` from the
        // `__eq_<X>` / `__hash_<X>` registries (eq_helpers /
        // hash_helpers); their map-key body is a thin proxy that
        // Calls into those.
        let mut all_key_helpers: HashMap<String, KeyHelpers> =
            self.key.iter().map(|(k, h)| (k.clone(), *h)).collect();
        for (carrier, &(eq_fn, hash_fn)) in carrier_eq_hash {
            all_key_helpers.insert(
                carrier.clone(),
                KeyHelpers {
                    hash: hash_fn,
                    eq: eq_fn,
                },
            );
        }
        for (list_canonical, &(eq_fn, hash_fn)) in list_eq_hash {
            all_key_helpers.insert(
                list_canonical.clone(),
                KeyHelpers {
                    hash: hash_fn,
                    eq: eq_fn,
                },
            );
        }
        // Each per-instantiation `Map<K,V>` carries its own structural
        // eq + hash helpers (in `MapKVHelpers`). Surface them under the
        // canonical name so a sum variant whose field is `Map<K,V>`
        // can dispatch through the same `all_key_helpers` table the
        // rest of the compound-field logic uses.
        for (map_canonical, kv) in &self.kv {
            all_key_helpers.insert(
                map_canonical.clone(),
                KeyHelpers {
                    hash: kv.hash,
                    eq: kv.eq,
                },
            );
        }
        for k_aver in &self.key_order {
            codes.function(&emit_hash_for(
                k_aver,
                registry,
                string_key_helpers,
                &all_key_helpers,
            )?);
            codes.function(&emit_eq_for(
                k_aver,
                registry,
                string_key_helpers,
                &all_key_helpers,
            )?);
        }
        for canonical in &self.kv_order {
            let (k_aver, _) = super::types::parse_map_kv(canonical).ok_or(
                WasmGcError::Validation(format!("parse_map_kv `{canonical}`")),
            )?;
            let key_h = self
                .key_helpers(k_aver)
                .ok_or(WasmGcError::Validation(format!(
                    "key helpers missing for K=`{k_aver}`"
                )))?;
            codes.function(&emit_map_empty(canonical, registry)?);
            codes.function(&emit_map_set(canonical, registry, key_h)?);
            codes.function(&emit_map_set_in_place(canonical, registry, key_h)?);
            codes.function(&emit_map_get(canonical, registry, key_h)?);
            codes.function(&emit_map_len(canonical, registry)?);
            codes.function(&emit_map_get_or_default(canonical, registry, key_h)?);
            codes.function(&emit_map_get_pair(canonical, registry, key_h)?);
            codes.function(&emit_map_keys(canonical, registry)?);
            codes.function(&emit_map_values(canonical, registry)?);
            codes.function(&emit_map_remove(canonical, registry, key_h)?);
            let helpers = self.kv[canonical];
            codes.function(&emit_map_entries(canonical, registry)?);
            codes.function(&emit_map_from_list(canonical, registry, helpers.set)?);
            // Structural eq + commutative hash for `Map<K, V>`. V's
            // hash + eq fn idxs come from `all_key_helpers` (same
            // table that drives K dispatch — V is just another
            // shape that needs the same family of helpers when V is
            // not a primitive).
            let v_helpers = v_helper_for(canonical, &all_key_helpers, registry)?;
            codes.function(&emit_map_eq(
                canonical,
                registry,
                key_h,
                v_helpers,
                helpers.get,
            )?);
            codes.function(&emit_map_hash(canonical, registry, key_h, v_helpers)?);
        }
        Ok(())
    }
}

/// Resolve hash + eq fn idx for V — looks the same shape up as K. V
/// can be a primitive (hash/eq are inline instructions, not fn calls
/// — return None and let the body emitter pick the inline path), or
/// a ref-shaped K kind we already registered (records / sums /
/// carriers / List / Vector / Map). Returns the proxy fn idxs.
fn v_helper_for(
    canonical: &str,
    all_key_helpers: &HashMap<String, KeyHelpers>,
    _registry: &TypeRegistry,
) -> Result<Option<KeyHelpers>, WasmGcError> {
    let (_, v_aver) = super::types::parse_map_kv(canonical).ok_or(WasmGcError::Validation(
        format!("v_helper_for: bad canonical `{canonical}`"),
    ))?;
    let v_aver = v_aver.trim();
    if super::types::TypeRegistry::is_primitive_map_key(v_aver) {
        // Primitive V (Int / Float / Bool) — body emitter inlines
        // the comparison + hash, no helper dispatch.
        return Ok(None);
    }
    // String + every other ref V flows through `all_key_helpers`,
    // which assign_slots force-registered as pseudo-K (so the
    // helpers exist regardless of whether the program actually
    // holds `Map<V, _>`).
    Ok(all_key_helpers.get(v_aver).copied())
}

/// `hash : (K) -> i32`. K can be `String` (DJB2 over the bytes) or
/// any user-defined record (field-by-field combine, delegating to
/// the per-K helper for String fields).
fn emit_hash_for(
    k_aver: &str,
    registry: &TypeRegistry,
    string_key_helpers: Option<KeyHelpers>,
    all_key_helpers: &HashMap<String, KeyHelpers>,
) -> Result<Function, WasmGcError> {
    if k_aver == "String" {
        return emit_hash_string(registry);
    }
    if registry.record_type_idx(k_aver).is_some() {
        return emit_hash_record(k_aver, registry, string_key_helpers, all_key_helpers);
    }
    if super::types::TypeRegistry::is_primitive_map_key(k_aver) {
        return emit_hash_primitive(k_aver);
    }
    if registry
        .variants
        .values()
        .flat_map(|v| v.iter())
        .any(|v| v.parent == k_aver)
    {
        return emit_hash_sum(k_aver, registry, string_key_helpers, all_key_helpers);
    }
    if k_aver.starts_with("Option<")
        || k_aver.starts_with("Result<")
        || k_aver.starts_with("Tuple<")
        || k_aver.starts_with("List<")
        || k_aver.starts_with("Vector<")
        || k_aver.starts_with("Map<")
    {
        // Same shape as carrier eq: proxy to the per-instantiation
        // `__hash_<X>` helper. Carriers come from hash_helpers,
        // List/Vector/Map from their own registries; all merged into
        // `all_key_helpers` via the compound lookup at module
        // assembly.
        let helpers = all_key_helpers
            .get(k_aver)
            .ok_or(WasmGcError::Validation(format!(
                "hash_for: compound `{k_aver}` has no registered hash helper"
            )))?;
        let mut f = Function::new([]);
        f.instruction(&Instruction::LocalGet(0));
        f.instruction(&Instruction::Call(helpers.hash));
        f.instruction(&Instruction::End);
        return Ok(f);
    }
    Err(WasmGcError::Unimplemented(
        "phase 3c — hash for unsupported K kind",
    ))
}

fn emit_eq_for(
    k_aver: &str,
    registry: &TypeRegistry,
    string_key_helpers: Option<KeyHelpers>,
    all_key_helpers: &HashMap<String, KeyHelpers>,
) -> Result<Function, WasmGcError> {
    if k_aver == "String" {
        return emit_eq_string(registry);
    }
    if registry.record_type_idx(k_aver).is_some() {
        return emit_eq_record(k_aver, registry, string_key_helpers, all_key_helpers);
    }
    if super::types::TypeRegistry::is_primitive_map_key(k_aver) {
        return emit_eq_primitive(k_aver);
    }
    if registry
        .variants
        .values()
        .flat_map(|v| v.iter())
        .any(|v| v.parent == k_aver)
    {
        return emit_eq_sum(k_aver, registry, string_key_helpers, all_key_helpers);
    }
    if k_aver.starts_with("Option<")
        || k_aver.starts_with("Result<")
        || k_aver.starts_with("Tuple<")
        || k_aver.starts_with("List<")
        || k_aver.starts_with("Vector<")
        || k_aver.starts_with("Map<")
    {
        // Map-key eq for compounds proxies to `__eq_<X>` (carriers
        // from eq_helpers, List/Vector from list_helpers, Map from
        // MapHelperRegistry::kv). All merged into `all_key_helpers`
        // at module assembly time.
        let helpers = all_key_helpers
            .get(k_aver)
            .ok_or(WasmGcError::Validation(format!(
                "eq_for: compound `{k_aver}` has no registered eq helper"
            )))?;
        let mut f = Function::new([]);
        f.instruction(&Instruction::LocalGet(0));
        f.instruction(&Instruction::LocalGet(1));
        f.instruction(&Instruction::Call(helpers.eq));
        f.instruction(&Instruction::End);
        return Ok(f);
    }
    Err(WasmGcError::Unimplemented(
        "phase 3c — eq for unsupported K kind",
    ))
}

/// `hash : (K_raw) -> i32` for primitive K. Helpers consume raw
/// primitives (callers don't have to box just to compute a hash).
/// Map's keys array stores boxed refs, but `hash` runs on the raw
/// value the user passed in.
fn emit_hash_primitive(k_aver: &str) -> Result<Function, WasmGcError> {
    let mut f = Function::new([]);
    f.instruction(&Instruction::LocalGet(0));
    match k_aver {
        "Int" => {
            // i32.wrap_i64 — keeps low 32 bits. Cheap, distributes
            // poorly for tightly-clustered Int domains; bench
            // scenarios don't stress this.
            f.instruction(&Instruction::I32WrapI64);
        }
        "Float" => {
            f.instruction(&Instruction::I64ReinterpretF64);
            f.instruction(&Instruction::I32WrapI64);
        }
        "Bool" => {
            // Already i32 — no-op (just LocalGet then End)
        }
        _ => panic!(
            "internal compiler error: emit_hash_primitive called with \
             non-primitive K = `{k_aver}`; caller must dispatch to \
             emit_hash_record / emit_hash_sum / __wasmgc_string_hash for \
             non-primitive K. Please file at https://github.com/jasisz/aver/issues"
        ),
    }
    f.instruction(&Instruction::End);
    Ok(f)
}

/// `eq : (K_raw, K_raw) -> i32` for primitive K. Native eq
/// instruction per K kind.
fn emit_eq_primitive(k_aver: &str) -> Result<Function, WasmGcError> {
    let mut f = Function::new([]);
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::LocalGet(1));
    match k_aver {
        "Int" => f.instruction(&Instruction::I64Eq),
        "Float" => f.instruction(&Instruction::F64Eq),
        "Bool" => f.instruction(&Instruction::I32Eq),
        _ => panic!(
            "internal compiler error: emit_eq_primitive called with \
             non-primitive K = `{k_aver}`; caller must dispatch to \
             emit_eq_record / emit_eq_sum / __wasmgc_string_eq for \
             non-primitive K. Please file at https://github.com/jasisz/aver/issues"
        ),
    };
    f.instruction(&Instruction::End);
    Ok(f)
}

/// Wasm value type used as the `keys` array element. Primitive K
/// stores boxed refs (`(ref null $primitive_key_box_K)`) so the
/// open-addressing `keys[i] == null` empty marker stays uniform;
/// ref K (String / record) stores its own ref directly.
fn key_storage_val_type(k_aver: &str, registry: &TypeRegistry) -> Result<ValType, WasmGcError> {
    if let Some(box_idx) = registry.primitive_key_box_idx(k_aver) {
        Ok(ValType::Ref(RefType {
            nullable: true,
            heap_type: HeapType::Concrete(box_idx),
        }))
    } else {
        super::types::aver_to_wasm(k_aver, Some(registry))?.ok_or(WasmGcError::Validation(format!(
            "Map key type `{k_aver}` has no wasm representation"
        )))
    }
}

/// Append the instructions that turn a stored-key value (top of
/// stack) into the raw K_val that `hash` / `eq` expect. For primitive
/// K: `struct.get $box 0` to unbox; for ref K: no-op.
fn emit_unbox_key(f: &mut Function, k_aver: &str, registry: &TypeRegistry) {
    if let Some(box_idx) = registry.primitive_key_box_idx(k_aver) {
        f.instruction(&Instruction::StructGet {
            struct_type_index: box_idx,
            field_index: 0,
        });
    }
}

/// Append the instructions that turn a raw K_val (top of stack) into
/// a stored-key value ready for `array.set`. For primitive K:
/// `struct.new $box`; for ref K: no-op.
fn emit_box_key(f: &mut Function, k_aver: &str, registry: &TypeRegistry) {
    if let Some(box_idx) = registry.primitive_key_box_idx(k_aver) {
        f.instruction(&Instruction::StructNew(box_idx));
    }
}

/// Heap type used by `Map.remove` for `ref.null` in the keys-array
/// store-back. Concrete idx for K kinds with their own struct type
/// (primitive K boxes, String, record, carrier, List, Vector); abstract
/// Eq for sum K (whose wasm rep is eqref since variants share no
/// concrete struct type).
fn key_storage_null_heap(k_aver: &str, registry: &TypeRegistry) -> HeapType {
    if let Some(box_idx) = registry.primitive_key_box_idx(k_aver) {
        return HeapType::Concrete(box_idx);
    }
    if k_aver == "String"
        && let Some(s) = registry.string_array_type_idx
    {
        return HeapType::Concrete(s);
    }
    if let Some(r) = registry.record_type_idx(k_aver) {
        return HeapType::Concrete(r);
    }
    if let Some(o) = registry.option_type_idx(k_aver) {
        return HeapType::Concrete(o);
    }
    if let Some(r) = registry.result_type_idx(k_aver) {
        return HeapType::Concrete(r);
    }
    if let Some(t) = registry.tuple_type_idx(k_aver) {
        return HeapType::Concrete(t);
    }
    if let Some(l) = registry.list_type_idx(k_aver) {
        return HeapType::Concrete(l);
    }
    if let Some(v) = registry.vector_type_idx(k_aver) {
        return HeapType::Concrete(v);
    }
    if let Some(slots) = registry.map_slots(k_aver) {
        return HeapType::Concrete(slots.map);
    }
    // Sum K — eqref. ref.null of abstract Eq is a subtype of every
    // concrete variant ref, so the array.set typechecks.
    HeapType::Abstract {
        shared: false,
        ty: wasm_encoder::AbstractHeapType::Eq,
    }
}

fn string_idx(registry: &TypeRegistry) -> Result<u32, WasmGcError> {
    registry
        .string_array_type_idx
        .ok_or(WasmGcError::Validation(
            "Map<String, _> helper requires String slot".into(),
        ))
}

/// DJB2 hash over the byte content of a `(ref null $string)`.
/// `h = 5381; for b in s: h = h * 33 + b`. Standard non-cryptographic
/// hash used in legacy backend's `rt_hash_string` shape.
fn emit_hash_string(registry: &TypeRegistry) -> Result<Function, WasmGcError> {
    let s_idx = string_idx(registry)?;
    let padding = wat_helper::padding_types(s_idx);
    let wat = format!(
        r#"
        (module
          {padding}
          (type $string (array (mut i8)))
          (func (export "helper") (param $s (ref null $string)) (result i32)
            (local $h i32)
            (local $i i32)
            (local $n i32)
            ;; DJB2: h = 5381; for each byte: h = h * 33 + byte.
            i32.const 5381
            local.set $h
            local.get $s
            array.len
            local.set $n
            i32.const 0
            local.set $i
            (block $break
              (loop $next
                local.get $i
                local.get $n
                i32.ge_u
                br_if $break

                ;; h = (h << 5) + h + s[i]
                local.get $h
                i32.const 5
                i32.shl
                local.get $h
                i32.add
                local.get $s
                local.get $i
                array.get_u $string
                i32.add
                local.set $h

                local.get $i
                i32.const 1
                i32.add
                local.set $i
                br $next))
            local.get $h)
        )
    "#
    );
    wat_helper::compile_wat_helper(&wat)
}

/// Byte-equal compare of two `(ref null $string)`. Returns 1 if equal.
fn emit_eq_string(registry: &TypeRegistry) -> Result<Function, WasmGcError> {
    let s_idx = string_idx(registry)?;
    let padding = wat_helper::padding_types(s_idx);
    let wat = format!(
        r#"
        (module
          {padding}
          (type $string (array (mut i8)))
          (func (export "helper") (param $a (ref null $string)) (param $b (ref null $string)) (result i32)
            (local $i i32)
            (local $n i32)
            ;; Length mismatch ⇒ 0.
            local.get $a
            array.len
            local.get $b
            array.len
            i32.ne
            (if
              (then i32.const 0 return))

            local.get $a
            array.len
            local.set $n
            i32.const 0
            local.set $i
            (block $break
              (loop $next
                local.get $i
                local.get $n
                i32.ge_u
                br_if $break

                local.get $a
                local.get $i
                array.get_u $string
                local.get $b
                local.get $i
                array.get_u $string
                i32.ne
                (if
                  (then i32.const 0 return))

                local.get $i
                i32.const 1
                i32.add
                local.set $i
                br $next))
            i32.const 1)
        )
    "#
    );
    wat_helper::compile_wat_helper(&wat)
}

fn slots_for(canonical: &str, registry: &TypeRegistry) -> Result<MapSlots, WasmGcError> {
    registry
        .map_slots(canonical)
        .ok_or(WasmGcError::Validation(format!(
            "Map slots missing for `{canonical}`"
        )))
}

/// `empty() -> Map<K, V>`. Allocates fresh keys/values arrays at
/// `INITIAL_CAP` and a struct wrapping them.
fn emit_map_empty(canonical: &str, registry: &TypeRegistry) -> Result<Function, WasmGcError> {
    let slots = slots_for(canonical, registry)?;
    let mut f = Function::new([]);
    // size = 0; cap = INITIAL_CAP; keys = array.new_default; values = array.new_default
    f.instruction(&Instruction::I32Const(0)); // size
    f.instruction(&Instruction::I32Const(INITIAL_CAP)); // cap
    f.instruction(&Instruction::I32Const(INITIAL_CAP));
    f.instruction(&Instruction::ArrayNewDefault(slots.keys_array));
    f.instruction(&Instruction::I32Const(INITIAL_CAP));
    f.instruction(&Instruction::ArrayNewDefault(slots.values_array));
    f.instruction(&Instruction::StructNew(slots.map));
    f.instruction(&Instruction::End);
    Ok(f)
}

/// `set(map, k, v) -> map`. Linear-probing open-addressing insert.
/// Mutates `map` in place; returns same ref.
fn emit_map_set(
    canonical: &str,
    registry: &TypeRegistry,
    keyh: KeyHelpers,
) -> Result<Function, WasmGcError> {
    let slots = slots_for(canonical, registry)?;
    let (k_aver, v_aver) = super::types::parse_map_kv(canonical).unwrap();
    let k_val = super::types::aver_to_wasm(k_aver, Some(registry))?.unwrap();
    let v_val = super::types::aver_to_wasm(v_aver, Some(registry))?.unwrap();
    // params: 0=map, 1=k, 2=v
    // locals: 3=cap, 4=mask, 5=idx, 6=keys (CLONED), 7=values (CLONED), 8=cur_key
    //
    // Clone-on-write: at entry we allocate fresh `keys` and `values`
    // arrays, `array.copy` the source map's contents into them, and
    // probe / mutate exclusively on the clones. The returned map struct
    // wraps the cloned arrays, so the input map's keys/values are
    // never observed mutated by anyone holding an alias of it. Without
    // this, `Vector.new(n, m)` produced N aliases of `m` and a
    // `Map.set(row, …)` on a row fetched via `Vector.get(outer, i)`
    // silently rewrote every alias of that map.
    let mut f = Function::new([
        (1, ValType::I32), // 3: cap
        (1, ValType::I32), // 4: mask
        (1, ValType::I32), // 5: idx
        (
            1,
            ValType::Ref(RefType {
                nullable: true,
                heap_type: HeapType::Concrete(slots.keys_array),
            }),
        ), // 6: keys (the freshly-allocated clone)
        (
            1,
            ValType::Ref(RefType {
                nullable: true,
                heap_type: HeapType::Concrete(slots.values_array),
            }),
        ), // 7: values (the freshly-allocated clone)
        (1, key_storage_val_type(k_aver, registry)?), // 8: cur_key (boxed for primitive)
    ]);
    let _ = (v_val, k_val);

    // cap = map.cap; mask = cap - 1
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 1,
    });
    f.instruction(&Instruction::LocalSet(3));
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Sub);
    f.instruction(&Instruction::LocalSet(4));

    // keys = array.new_default $keys cap; array.copy keys 0 map.keys 0 cap
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::ArrayNewDefault(slots.keys_array));
    f.instruction(&Instruction::LocalSet(6));
    f.instruction(&Instruction::LocalGet(6));
    f.instruction(&Instruction::I32Const(0));
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 2,
    });
    f.instruction(&Instruction::I32Const(0));
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::ArrayCopy {
        array_type_index_dst: slots.keys_array,
        array_type_index_src: slots.keys_array,
    });

    // values = array.new_default $values cap; array.copy values 0 map.values 0 cap
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::ArrayNewDefault(slots.values_array));
    f.instruction(&Instruction::LocalSet(7));
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::I32Const(0));
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 3,
    });
    f.instruction(&Instruction::I32Const(0));
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::ArrayCopy {
        array_type_index_dst: slots.values_array,
        array_type_index_src: slots.values_array,
    });

    // idx = hash(k) & mask
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::Call(keyh.hash));
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::I32And);
    f.instruction(&Instruction::LocalSet(5));

    // loop forever (cap is large enough that probe always finds slot)
    f.instruction(&Instruction::Block(BlockType::Empty));
    f.instruction(&Instruction::Loop(BlockType::Empty));
    // cur_key = keys[idx]
    f.instruction(&Instruction::LocalGet(6));
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::ArrayGet(slots.keys_array));
    f.instruction(&Instruction::LocalSet(8));

    // if cur_key == null: empty slot, insert.
    f.instruction(&Instruction::LocalGet(8));
    f.instruction(&Instruction::RefIsNull);
    f.instruction(&Instruction::If(BlockType::Empty));
    // keys[idx] = box(k)  (primitive K) or k (ref K)
    f.instruction(&Instruction::LocalGet(6));
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::LocalGet(1));
    emit_box_key(&mut f, k_aver, registry);
    f.instruction(&Instruction::ArraySet(slots.keys_array));
    // values[idx] = v
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::LocalGet(2));
    f.instruction(&Instruction::ArraySet(slots.values_array));
    // return struct.new $map (map.size + 1, cap, new_keys, new_values)
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 0,
    });
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Add);
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::LocalGet(6));
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::StructNew(slots.map));
    f.instruction(&Instruction::Return);
    f.instruction(&Instruction::End);

    // else if eq(unbox(cur_key), k): update value at idx, return clone.
    f.instruction(&Instruction::LocalGet(8));
    emit_unbox_key(&mut f, k_aver, registry);
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::Call(keyh.eq));
    f.instruction(&Instruction::If(BlockType::Empty));
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::LocalGet(2));
    f.instruction(&Instruction::ArraySet(slots.values_array));
    // return struct.new $map (map.size, cap, new_keys, new_values)
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 0,
    });
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::LocalGet(6));
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::StructNew(slots.map));
    f.instruction(&Instruction::Return);
    f.instruction(&Instruction::End);

    // else: idx = (idx + 1) & mask; continue
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Add);
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::I32And);
    f.instruction(&Instruction::LocalSet(5));
    f.instruction(&Instruction::Br(0));
    f.instruction(&Instruction::End); // loop
    f.instruction(&Instruction::End); // block
    f.instruction(&Instruction::Unreachable);
    f.instruction(&Instruction::End);
    Ok(f)
}

/// `set_in_place(map, k, v) -> map`. Same probe-and-write loop as
/// `emit_map_set` but without the entry-time `array.copy` of
/// `keys` / `values` — the caller has proven (via `ir::alias` and
/// `last_use`) that `map`'s engine arrays are uniquely owned, so
/// rewriting them in place is sound and saves two `array.new_default`
/// plus two `array.copy` per call. The returned struct still re-wraps
/// the same arrays with the updated size; callers expect a fresh
/// map handle either way.
fn emit_map_set_in_place(
    canonical: &str,
    registry: &TypeRegistry,
    keyh: KeyHelpers,
) -> Result<Function, WasmGcError> {
    let slots = slots_for(canonical, registry)?;
    let (k_aver, v_aver) = super::types::parse_map_kv(canonical).unwrap();
    let _ = super::types::aver_to_wasm(k_aver, Some(registry))?.unwrap();
    let _ = super::types::aver_to_wasm(v_aver, Some(registry))?.unwrap();
    // params: 0=map, 1=k, 2=v
    // locals: 3=cap, 4=mask, 5=idx,
    //         6=keys (alias of map.keys), 7=values (alias of map.values),
    //         8=cur_key
    let mut f = Function::new([
        (1, ValType::I32),
        (1, ValType::I32),
        (1, ValType::I32),
        (
            1,
            ValType::Ref(RefType {
                nullable: true,
                heap_type: HeapType::Concrete(slots.keys_array),
            }),
        ),
        (
            1,
            ValType::Ref(RefType {
                nullable: true,
                heap_type: HeapType::Concrete(slots.values_array),
            }),
        ),
        (1, key_storage_val_type(k_aver, registry)?),
    ]);

    // cap = map.cap; mask = cap - 1
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 1,
    });
    f.instruction(&Instruction::LocalSet(3));
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Sub);
    f.instruction(&Instruction::LocalSet(4));

    // keys = map.keys; values = map.values  (no array.copy — alias-free)
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 2,
    });
    f.instruction(&Instruction::LocalSet(6));
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 3,
    });
    f.instruction(&Instruction::LocalSet(7));

    // idx = hash(k) & mask
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::Call(keyh.hash));
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::I32And);
    f.instruction(&Instruction::LocalSet(5));

    f.instruction(&Instruction::Block(BlockType::Empty));
    f.instruction(&Instruction::Loop(BlockType::Empty));
    // cur_key = keys[idx]
    f.instruction(&Instruction::LocalGet(6));
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::ArrayGet(slots.keys_array));
    f.instruction(&Instruction::LocalSet(8));

    // empty slot: insert
    f.instruction(&Instruction::LocalGet(8));
    f.instruction(&Instruction::RefIsNull);
    f.instruction(&Instruction::If(BlockType::Empty));
    f.instruction(&Instruction::LocalGet(6));
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::LocalGet(1));
    emit_box_key(&mut f, k_aver, registry);
    f.instruction(&Instruction::ArraySet(slots.keys_array));
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::LocalGet(2));
    f.instruction(&Instruction::ArraySet(slots.values_array));
    // size + 1, cap, keys, values  (same arrays, fresh struct)
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 0,
    });
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Add);
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::LocalGet(6));
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::StructNew(slots.map));
    f.instruction(&Instruction::Return);
    f.instruction(&Instruction::End);

    // matching key: update value
    f.instruction(&Instruction::LocalGet(8));
    emit_unbox_key(&mut f, k_aver, registry);
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::Call(keyh.eq));
    f.instruction(&Instruction::If(BlockType::Empty));
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::LocalGet(2));
    f.instruction(&Instruction::ArraySet(slots.values_array));
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 0,
    });
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::LocalGet(6));
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::StructNew(slots.map));
    f.instruction(&Instruction::Return);
    f.instruction(&Instruction::End);

    // collision: probe forward
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Add);
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::I32And);
    f.instruction(&Instruction::LocalSet(5));
    f.instruction(&Instruction::Br(0));
    f.instruction(&Instruction::End); // loop
    f.instruction(&Instruction::End); // block
    f.instruction(&Instruction::Unreachable);
    f.instruction(&Instruction::End);
    Ok(f)
}

/// `get(map, k) -> Option<V>`. Linear-probing lookup; null slot →
/// None, matching key → Some(value).
fn emit_map_get(
    canonical: &str,
    registry: &TypeRegistry,
    keyh: KeyHelpers,
) -> Result<Function, WasmGcError> {
    let slots = slots_for(canonical, registry)?;
    let (k_aver, v_aver) = super::types::parse_map_kv(canonical).unwrap();
    let k_val = super::types::aver_to_wasm(k_aver, Some(registry))?.unwrap();
    let v_val = super::types::aver_to_wasm(v_aver, Some(registry))?.unwrap();
    let opt_canonical = format!("Option<{v_aver}>");
    let opt_idx = registry
        .option_type_idx(&opt_canonical)
        .ok_or(WasmGcError::Validation(format!(
            "Map.get: Option<{v_aver}> not registered"
        )))?;

    let mut f = Function::new([
        (1, ValType::I32), // 2: cap
        (1, ValType::I32), // 3: mask
        (1, ValType::I32), // 4: idx
        (
            1,
            ValType::Ref(RefType {
                nullable: true,
                heap_type: HeapType::Concrete(slots.keys_array),
            }),
        ), // 5: keys
        (
            1,
            ValType::Ref(RefType {
                nullable: true,
                heap_type: HeapType::Concrete(slots.values_array),
            }),
        ), // 6: values
        (1, key_storage_val_type(k_aver, registry)?), // 7: cur_key (boxed for prim)
    ]);
    let _ = k_val;
    // cap, mask, keys, values
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 1,
    });
    f.instruction(&Instruction::LocalSet(2));
    f.instruction(&Instruction::LocalGet(2));
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Sub);
    f.instruction(&Instruction::LocalSet(3));
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 2,
    });
    f.instruction(&Instruction::LocalSet(5));
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 3,
    });
    f.instruction(&Instruction::LocalSet(6));
    // idx = hash(k) & mask
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::Call(keyh.hash));
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::I32And);
    f.instruction(&Instruction::LocalSet(4));

    f.instruction(&Instruction::Block(BlockType::Empty));
    f.instruction(&Instruction::Loop(BlockType::Empty));
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::ArrayGet(slots.keys_array));
    f.instruction(&Instruction::LocalSet(7));
    // if cur_key == null → return None
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::RefIsNull);
    f.instruction(&Instruction::If(BlockType::Empty));
    f.instruction(&Instruction::I32Const(0));
    emit_default_value_for(&mut f, v_val);
    f.instruction(&Instruction::StructNew(opt_idx));
    f.instruction(&Instruction::Return);
    f.instruction(&Instruction::End);
    // if eq(unbox(cur_key), k) → return Some(values[idx])
    f.instruction(&Instruction::LocalGet(7));
    emit_unbox_key(&mut f, k_aver, registry);
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::Call(keyh.eq));
    f.instruction(&Instruction::If(BlockType::Empty));
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::LocalGet(6));
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::ArrayGet(slots.values_array));
    f.instruction(&Instruction::StructNew(opt_idx));
    f.instruction(&Instruction::Return);
    f.instruction(&Instruction::End);
    // else: idx = (idx + 1) & mask
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Add);
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::I32And);
    f.instruction(&Instruction::LocalSet(4));
    f.instruction(&Instruction::Br(0));
    f.instruction(&Instruction::End); // loop
    f.instruction(&Instruction::End); // block
    f.instruction(&Instruction::Unreachable);
    f.instruction(&Instruction::End);
    Ok(f)
}

/// `get_or_default(map, k, default) -> V`. Same probe loop as
/// `get`, but returns `values[idx]` (or the supplied default) directly
/// — no `Option<V>` boxing on the way out. Backs the fused
/// `Option.withDefault(Map.get(m, k), default)` shape that's hot in
/// `map_lookup`-style benches: one alloc per lookup → zero.
fn emit_map_get_or_default(
    canonical: &str,
    registry: &TypeRegistry,
    keyh: KeyHelpers,
) -> Result<Function, WasmGcError> {
    let slots = slots_for(canonical, registry)?;
    let (k_aver, v_aver) = super::types::parse_map_kv(canonical).unwrap();
    let k_val = super::types::aver_to_wasm(k_aver, Some(registry))?.unwrap();
    let v_val = super::types::aver_to_wasm(v_aver, Some(registry))?.unwrap();
    let _ = (k_val, v_val);
    // params: 0=map, 1=k, 2=default
    // locals: 3=cap, 4=mask, 5=idx, 6=keys, 7=values, 8=cur_key
    let mut f = Function::new([
        (1, ValType::I32), // 3: cap
        (1, ValType::I32), // 4: mask
        (1, ValType::I32), // 5: idx
        (
            1,
            ValType::Ref(RefType {
                nullable: true,
                heap_type: HeapType::Concrete(slots.keys_array),
            }),
        ),
        (
            1,
            ValType::Ref(RefType {
                nullable: true,
                heap_type: HeapType::Concrete(slots.values_array),
            }),
        ),
        (1, key_storage_val_type(k_aver, registry)?), // 8: cur_key
    ]);

    // cap = map.cap; mask = cap - 1; keys = map.keys; values = map.values
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 1,
    });
    f.instruction(&Instruction::LocalSet(3));
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Sub);
    f.instruction(&Instruction::LocalSet(4));
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 2,
    });
    f.instruction(&Instruction::LocalSet(6));
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 3,
    });
    f.instruction(&Instruction::LocalSet(7));

    // idx = hash(k) & mask
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::Call(keyh.hash));
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::I32And);
    f.instruction(&Instruction::LocalSet(5));

    f.instruction(&Instruction::Block(BlockType::Empty));
    f.instruction(&Instruction::Loop(BlockType::Empty));
    // cur_key = keys[idx]
    f.instruction(&Instruction::LocalGet(6));
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::ArrayGet(slots.keys_array));
    f.instruction(&Instruction::LocalSet(8));
    // empty slot → return default
    f.instruction(&Instruction::LocalGet(8));
    f.instruction(&Instruction::RefIsNull);
    f.instruction(&Instruction::If(BlockType::Empty));
    f.instruction(&Instruction::LocalGet(2));
    f.instruction(&Instruction::Return);
    f.instruction(&Instruction::End);
    // key match → return values[idx]
    f.instruction(&Instruction::LocalGet(8));
    emit_unbox_key(&mut f, k_aver, registry);
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::Call(keyh.eq));
    f.instruction(&Instruction::If(BlockType::Empty));
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::ArrayGet(slots.values_array));
    f.instruction(&Instruction::Return);
    f.instruction(&Instruction::End);
    // probe forward
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Add);
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::I32And);
    f.instruction(&Instruction::LocalSet(5));
    f.instruction(&Instruction::Br(0));
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::Unreachable);
    f.instruction(&Instruction::End);
    Ok(f)
}

/// `get_pair(map, k) -> (i32 found, V value)`. Backs the fused
/// `match Map.get(m, k) { Option.Some(v) -> ...; Option.None -> ... }`
/// shape — the caller pops `value` into the binding slot and branches
/// on `found`. No `Option<V>` ever allocates.
///
/// On an empty slot returns `(0, default<V>)` so the multi-result
/// signature stays well-typed; on a key match returns `(1, values[idx])`.
fn emit_map_get_pair(
    canonical: &str,
    registry: &TypeRegistry,
    keyh: KeyHelpers,
) -> Result<Function, WasmGcError> {
    let slots = slots_for(canonical, registry)?;
    let (k_aver, v_aver) = super::types::parse_map_kv(canonical).unwrap();
    let k_val = super::types::aver_to_wasm(k_aver, Some(registry))?.unwrap();
    let v_val = super::types::aver_to_wasm(v_aver, Some(registry))?.unwrap();
    let _ = k_val;
    // params: 0=map, 1=k
    // locals: 2=cap, 3=mask, 4=idx, 5=keys, 6=values, 7=cur_key
    let mut f = Function::new([
        (1, ValType::I32), // 2: cap
        (1, ValType::I32), // 3: mask
        (1, ValType::I32), // 4: idx
        (
            1,
            ValType::Ref(RefType {
                nullable: true,
                heap_type: HeapType::Concrete(slots.keys_array),
            }),
        ),
        (
            1,
            ValType::Ref(RefType {
                nullable: true,
                heap_type: HeapType::Concrete(slots.values_array),
            }),
        ),
        (1, key_storage_val_type(k_aver, registry)?), // 7: cur_key
    ]);

    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 1,
    });
    f.instruction(&Instruction::LocalSet(2));
    f.instruction(&Instruction::LocalGet(2));
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Sub);
    f.instruction(&Instruction::LocalSet(3));
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 2,
    });
    f.instruction(&Instruction::LocalSet(5));
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 3,
    });
    f.instruction(&Instruction::LocalSet(6));

    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::Call(keyh.hash));
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::I32And);
    f.instruction(&Instruction::LocalSet(4));

    f.instruction(&Instruction::Block(BlockType::Empty));
    f.instruction(&Instruction::Loop(BlockType::Empty));
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::ArrayGet(slots.keys_array));
    f.instruction(&Instruction::LocalSet(7));

    // empty slot → return (0, default<V>)
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::RefIsNull);
    f.instruction(&Instruction::If(BlockType::Empty));
    f.instruction(&Instruction::I32Const(0));
    emit_default_value_for(&mut f, v_val);
    f.instruction(&Instruction::Return);
    f.instruction(&Instruction::End);

    // key match → return (1, values[idx])
    f.instruction(&Instruction::LocalGet(7));
    emit_unbox_key(&mut f, k_aver, registry);
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::Call(keyh.eq));
    f.instruction(&Instruction::If(BlockType::Empty));
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::LocalGet(6));
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::ArrayGet(slots.values_array));
    f.instruction(&Instruction::Return);
    f.instruction(&Instruction::End);

    // probe forward
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Add);
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::I32And);
    f.instruction(&Instruction::LocalSet(4));
    f.instruction(&Instruction::Br(0));
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::Unreachable);
    f.instruction(&Instruction::End);
    Ok(f)
}

/// `len(map) -> i64`. Reads `size` from the struct, widens to i64.
fn emit_map_len(canonical: &str, registry: &TypeRegistry) -> Result<Function, WasmGcError> {
    let slots = slots_for(canonical, registry)?;
    let mut f = Function::new([]);
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 0,
    });
    f.instruction(&Instruction::I64ExtendI32U);
    f.instruction(&Instruction::End);
    Ok(f)
}

/// Push a default value of the given wasm type onto the stack — used
/// by `Map.get`'s None branch where the Option's `value` field needs
/// some payload (read by no well-typed Aver code, since pattern match
/// dispatches on tag first).
fn emit_default_value_for(f: &mut Function, ty: ValType) {
    match ty {
        ValType::I32 => {
            f.instruction(&Instruction::I32Const(0));
        }
        ValType::I64 => {
            f.instruction(&Instruction::I64Const(0));
        }
        ValType::F32 => {
            f.instruction(&Instruction::F32Const(0.0_f32.into()));
        }
        ValType::F64 => {
            f.instruction(&Instruction::F64Const(0.0_f64.into()));
        }
        ValType::Ref(rt) => {
            f.instruction(&Instruction::RefNull(rt.heap_type));
        }
        ValType::V128 => {
            // V128 not used by Aver primitives; emit a zero literal
            // for completeness so the helper still type-checks if a
            // future `Vector<V128>` ever surfaces.
            f.instruction(&Instruction::V128Const(0));
        }
    }
}

/// Field-by-field hash combine for a user record. Iterates each
/// field, picks an inline hash strategy from the field's Aver type,
/// and folds with `h = h * 33 + field_hash`. String fields delegate
/// to the per-K String hash helper (force-registered when any
/// record key transitively uses one). Field types we can't hash
/// natively (lists, vectors, nested records, sums) surface as
/// Unimplemented; widening that set is a follow-up.
fn emit_hash_record(
    record_name: &str,
    registry: &TypeRegistry,
    string_key_helpers: Option<KeyHelpers>,
    all_key_helpers: &HashMap<String, KeyHelpers>,
) -> Result<Function, WasmGcError> {
    let record_idx = registry
        .record_type_idx(record_name)
        .ok_or(WasmGcError::Validation(format!(
            "hash_record: `{record_name}` not registered"
        )))?;
    let fields = registry
        .record_fields
        .get(record_name)
        .ok_or(WasmGcError::Validation(format!(
            "hash_record: `{record_name}` has no field info"
        )))?;
    let mut f = Function::new([(1, ValType::I32) /* h */]);
    // h = 5381
    f.instruction(&Instruction::I32Const(5381));
    f.instruction(&Instruction::LocalSet(1));
    for (i, (_field_name, field_ty)) in fields.iter().enumerate() {
        // h = h * 33 (h<<5 + h)
        f.instruction(&Instruction::LocalGet(1));
        f.instruction(&Instruction::I32Const(5));
        f.instruction(&Instruction::I32Shl);
        f.instruction(&Instruction::LocalGet(1));
        f.instruction(&Instruction::I32Add);
        // push struct.get of field
        f.instruction(&Instruction::LocalGet(0));
        f.instruction(&Instruction::StructGet {
            struct_type_index: record_idx,
            field_index: i as u32,
        });
        // emit field hash → i32
        match field_ty.trim() {
            "Int" => {
                // i64 → low 32 bits
                f.instruction(&Instruction::I32WrapI64);
            }
            "Bool" => {
                // already i32
            }
            "Float" => {
                // f64 bit pattern → low 32 bits
                f.instruction(&Instruction::I64ReinterpretF64);
                f.instruction(&Instruction::I32WrapI64);
            }
            "String" => {
                let helpers = string_key_helpers.ok_or(WasmGcError::Validation(
                    "hash_record: String field needs String key helpers".into(),
                ))?;
                f.instruction(&Instruction::Call(helpers.hash));
            }
            other => {
                // Nested record / List<T> field. Both dispatch via
                // `all_key_helpers` — records were force-registered
                // as pseudo-K in `assign_slots`; list canonicals were
                // injected by `emit_helper_bodies` from list_helpers;
                // carriers (Option/Result/Tuple) come from
                // `carrier_eq_hash`.
                let lookup_key = if other.starts_with("List<") || other.starts_with("Vector<") {
                    super::types::normalize_compound(other).to_string()
                } else {
                    other.to_string()
                };
                let is_compound = other.starts_with("List<") || other.starts_with("Vector<");
                let is_carrier = other.starts_with("Option<")
                    || other.starts_with("Result<")
                    || other.starts_with("Tuple<");
                let is_sum = registry
                    .variants
                    .values()
                    .flat_map(|v| v.iter())
                    .any(|v| v.parent == other);
                if registry.record_type_idx(other).is_some() || is_compound || is_sum || is_carrier
                {
                    let inner = all_key_helpers
                        .get(&lookup_key)
                        .ok_or(WasmGcError::Validation(format!(
                            "hash_record: field `{other}` has no key helpers \
                             (record / list / vector / sum / Option / Result / Tuple T \
                              may need force-registration)"
                        )))?;
                    f.instruction(&Instruction::Call(inner.hash));
                } else {
                    return Err(WasmGcError::Unimplemented(
                        "phase 3c — record-key field type not in \
                         {Int, Float, Bool, String, nested record, List<T>, Vector<T>, sum, \
                          Option/Result/Tuple}",
                    ));
                }
            }
        }
        f.instruction(&Instruction::I32Add);
        f.instruction(&Instruction::LocalSet(1));
    }
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::End);
    Ok(f)
}

/// Field-by-field equality for a user record. Returns 1 iff every
/// field-pair tests equal under its type's natural compare. Short-
/// circuits on first mismatch via `if … return 0`.
fn emit_eq_record(
    record_name: &str,
    registry: &TypeRegistry,
    string_key_helpers: Option<KeyHelpers>,
    all_key_helpers: &HashMap<String, KeyHelpers>,
) -> Result<Function, WasmGcError> {
    let record_idx = registry
        .record_type_idx(record_name)
        .ok_or(WasmGcError::Validation(format!(
            "eq_record: `{record_name}` not registered"
        )))?;
    let fields = registry
        .record_fields
        .get(record_name)
        .ok_or(WasmGcError::Validation(format!(
            "eq_record: `{record_name}` has no field info"
        )))?;
    let mut f = Function::new([]);
    for (i, (_field_name, field_ty)) in fields.iter().enumerate() {
        f.instruction(&Instruction::LocalGet(0));
        f.instruction(&Instruction::StructGet {
            struct_type_index: record_idx,
            field_index: i as u32,
        });
        f.instruction(&Instruction::LocalGet(1));
        f.instruction(&Instruction::StructGet {
            struct_type_index: record_idx,
            field_index: i as u32,
        });
        match field_ty.trim() {
            "Int" => f.instruction(&Instruction::I64Eq),
            "Bool" => f.instruction(&Instruction::I32Eq),
            "Float" => f.instruction(&Instruction::F64Eq),
            "String" => {
                let helpers = string_key_helpers.ok_or(WasmGcError::Validation(
                    "eq_record: String field needs String key helpers".into(),
                ))?;
                f.instruction(&Instruction::Call(helpers.eq))
            }
            other => {
                let lookup_key = if other.starts_with("List<") || other.starts_with("Vector<") {
                    super::types::normalize_compound(other).to_string()
                } else {
                    other.to_string()
                };
                let is_compound = other.starts_with("List<") || other.starts_with("Vector<");
                let is_carrier = other.starts_with("Option<")
                    || other.starts_with("Result<")
                    || other.starts_with("Tuple<");
                let is_sum = registry
                    .variants
                    .values()
                    .flat_map(|v| v.iter())
                    .any(|v| v.parent == other);
                if registry.record_type_idx(other).is_some() || is_compound || is_sum || is_carrier
                {
                    let inner = all_key_helpers
                        .get(&lookup_key)
                        .ok_or(WasmGcError::Validation(format!(
                            "eq_record: field `{other}` has no key helpers"
                        )))?;
                    f.instruction(&Instruction::Call(inner.eq))
                } else {
                    return Err(WasmGcError::Unimplemented(
                        "phase 3c — record-key field type not in \
                         {Int, Float, Bool, String, nested record, List<T>, Vector<T>, sum, \
                          Option/Result/Tuple}",
                    ));
                }
            }
        };
        f.instruction(&Instruction::I32Eqz);
        f.instruction(&Instruction::If(BlockType::Empty));
        f.instruction(&Instruction::I32Const(0));
        f.instruction(&Instruction::Return);
        f.instruction(&Instruction::End);
    }
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::End);
    Ok(f)
}

/// `keys(m) -> List<K>`. Walks `m.keys` right-to-left, prepending
/// each non-null key onto a cons-list accumulator. Returns hash-
/// bucket order (not insertion order) — same constraint Aver's
/// stdlib documents for `Map.keys`.
fn emit_map_keys(canonical: &str, registry: &TypeRegistry) -> Result<Function, WasmGcError> {
    let slots = slots_for(canonical, registry)?;
    let (k_aver, _) = super::types::parse_map_kv(canonical).unwrap();
    let list_canonical = format!("List<{k_aver}>");
    let list_idx = registry
        .list_type_idx(&list_canonical)
        .ok_or(WasmGcError::Validation(format!(
            "Map.keys: `{list_canonical}` not registered"
        )))?;
    emit_map_walk_keys_to_list(slots.map, slots.keys_array, list_idx, k_aver, registry)
}

/// `values(m) -> List<V>`. Same shape as `keys` but pulls from
/// `m.values` whenever the corresponding `m.keys[i]` is non-null.
fn emit_map_values(canonical: &str, registry: &TypeRegistry) -> Result<Function, WasmGcError> {
    let slots = slots_for(canonical, registry)?;
    let (_, v_aver) = super::types::parse_map_kv(canonical).unwrap();
    let list_canonical = format!("List<{v_aver}>");
    let list_idx = registry
        .list_type_idx(&list_canonical)
        .ok_or(WasmGcError::Validation(format!(
            "Map.values: `{list_canonical}` not registered"
        )))?;
    emit_map_walk_values_to_list(slots, registry, list_idx)
}

/// Real impl for `Map.keys` walking the keys array. Per primitive
/// K the stored values are boxed refs; unbox before consing onto
/// the result list. For ref K (String / record), no unboxing.
fn emit_map_walk_keys_to_list(
    map_idx: u32,
    keys_array_idx: u32,
    list_idx: u32,
    k_aver: &str,
    registry: &TypeRegistry,
) -> Result<Function, WasmGcError> {
    let keys_ref = ValType::Ref(RefType {
        nullable: true,
        heap_type: HeapType::Concrete(keys_array_idx),
    });
    let list_ref = ValType::Ref(RefType {
        nullable: true,
        heap_type: HeapType::Concrete(list_idx),
    });
    // params: 0=map. locals: 1=keys, 2=i, 3=acc.
    let mut f = Function::new([(1, keys_ref), (1, ValType::I32), (1, list_ref)]);
    // keys = map.keys
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: map_idx,
        field_index: 2,
    });
    f.instruction(&Instruction::LocalSet(1));
    // i = map.cap - 1
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: map_idx,
        field_index: 1,
    });
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Sub);
    f.instruction(&Instruction::LocalSet(2));
    // acc = null
    f.instruction(&Instruction::RefNull(HeapType::Concrete(list_idx)));
    f.instruction(&Instruction::LocalSet(3));
    f.instruction(&Instruction::Block(wasm_encoder::BlockType::Empty));
    f.instruction(&Instruction::Loop(wasm_encoder::BlockType::Empty));
    // if i < 0 break
    f.instruction(&Instruction::LocalGet(2));
    f.instruction(&Instruction::I32Const(0));
    f.instruction(&Instruction::I32LtS);
    f.instruction(&Instruction::BrIf(1));
    // if keys[i] != null: acc = cons(unbox(keys[i]), acc)
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::LocalGet(2));
    f.instruction(&Instruction::ArrayGet(keys_array_idx));
    f.instruction(&Instruction::RefIsNull);
    f.instruction(&Instruction::I32Eqz);
    f.instruction(&Instruction::If(wasm_encoder::BlockType::Empty));
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::LocalGet(2));
    f.instruction(&Instruction::ArrayGet(keys_array_idx));
    emit_unbox_key(&mut f, k_aver, registry);
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::StructNew(list_idx));
    f.instruction(&Instruction::LocalSet(3));
    f.instruction(&Instruction::End);
    // i--
    f.instruction(&Instruction::LocalGet(2));
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Sub);
    f.instruction(&Instruction::LocalSet(2));
    f.instruction(&Instruction::Br(0));
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::End);
    Ok(f)
}

/// Real impl for `Map.values` walking the values array (with keys-
/// array null-check for occupancy).
fn emit_map_walk_values_to_list(
    slots: super::types::MapSlots,
    _registry: &TypeRegistry,
    list_idx: u32,
) -> Result<Function, WasmGcError> {
    let keys_ref = ValType::Ref(RefType {
        nullable: true,
        heap_type: HeapType::Concrete(slots.keys_array),
    });
    let values_ref = ValType::Ref(RefType {
        nullable: true,
        heap_type: HeapType::Concrete(slots.values_array),
    });
    let list_ref = ValType::Ref(RefType {
        nullable: true,
        heap_type: HeapType::Concrete(list_idx),
    });
    // params: 0=map. locals: 1=keys, 2=values, 3=i, 4=acc.
    let mut f = Function::new([
        (1, keys_ref),
        (1, values_ref),
        (1, ValType::I32),
        (1, list_ref),
    ]);
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 2,
    });
    f.instruction(&Instruction::LocalSet(1));
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 3,
    });
    f.instruction(&Instruction::LocalSet(2));
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 1,
    });
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Sub);
    f.instruction(&Instruction::LocalSet(3));
    f.instruction(&Instruction::RefNull(HeapType::Concrete(list_idx)));
    f.instruction(&Instruction::LocalSet(4));
    f.instruction(&Instruction::Block(wasm_encoder::BlockType::Empty));
    f.instruction(&Instruction::Loop(wasm_encoder::BlockType::Empty));
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::I32Const(0));
    f.instruction(&Instruction::I32LtS);
    f.instruction(&Instruction::BrIf(1));
    // if keys[i] != null: acc = cons(values[i], acc)
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::ArrayGet(slots.keys_array));
    f.instruction(&Instruction::RefIsNull);
    f.instruction(&Instruction::I32Eqz);
    f.instruction(&Instruction::If(wasm_encoder::BlockType::Empty));
    f.instruction(&Instruction::LocalGet(2));
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::ArrayGet(slots.values_array));
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::StructNew(list_idx));
    f.instruction(&Instruction::LocalSet(4));
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Sub);
    f.instruction(&Instruction::LocalSet(3));
    f.instruction(&Instruction::Br(0));
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::End);
    Ok(f)
}

/// `__eq_Map<K,V>(a: eqref, b: eqref) -> i32`. Structural eq —
/// `a.size == b.size && ∀ k ∈ a: get(b, k) == Some(a[k])`. Order-
/// independent (matches VM's Rust HashMap PartialEq).
fn emit_map_eq(
    canonical: &str,
    registry: &TypeRegistry,
    keyh: KeyHelpers,
    v_helpers: Option<KeyHelpers>,
    get_fn_idx: u32,
) -> Result<Function, WasmGcError> {
    let slots = slots_for(canonical, registry)?;
    let (k_aver, v_aver) = super::types::parse_map_kv(canonical).unwrap();
    let _ = keyh;
    let map_ref = ValType::Ref(RefType {
        nullable: true,
        heap_type: HeapType::Concrete(slots.map),
    });
    let keys_ref = ValType::Ref(RefType {
        nullable: true,
        heap_type: HeapType::Concrete(slots.keys_array),
    });
    let values_ref = ValType::Ref(RefType {
        nullable: true,
        heap_type: HeapType::Concrete(slots.values_array),
    });
    let v_val =
        super::types::aver_to_wasm(v_aver, Some(registry))?.ok_or(WasmGcError::Validation(
            format!("Map<{k_aver},{v_aver}>.eq: V `{v_aver}` has no wasm rep"),
        ))?;
    let opt_idx = registry
        .option_type_idx(&format!("Option<{v_aver}>"))
        .ok_or(WasmGcError::Validation(format!(
            "Map.eq: `Option<{v_aver}>` not registered"
        )))?;
    let opt_ref = ValType::Ref(RefType {
        nullable: true,
        heap_type: HeapType::Concrete(opt_idx),
    });
    // Locals: 2=typed map_a, 3=typed map_b, 4=cap, 5=i, 6=keys_a,
    // 7=values_a, 8=cur_key (boxed), 9=opt result, 10=v_a, 11=v_b
    let mut f = Function::new(vec![
        (1, map_ref),
        (1, map_ref),
        (1, ValType::I32),
        (1, ValType::I32),
        (1, keys_ref),
        (1, values_ref),
        (1, key_storage_val_type(k_aver, registry)?),
        (1, opt_ref),
        (1, v_val),
        (1, v_val),
    ]);
    let map_heap = HeapType::Concrete(slots.map);
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::RefCastNonNull(map_heap));
    f.instruction(&Instruction::LocalSet(2));
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::RefCastNonNull(map_heap));
    f.instruction(&Instruction::LocalSet(3));
    // if a.size != b.size return 0
    f.instruction(&Instruction::LocalGet(2));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 0,
    });
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 0,
    });
    f.instruction(&Instruction::I32Ne);
    f.instruction(&Instruction::If(BlockType::Empty));
    f.instruction(&Instruction::I32Const(0));
    f.instruction(&Instruction::Return);
    f.instruction(&Instruction::End);
    // cap = a.cap; keys_a = a.keys; values_a = a.values; i = 0
    f.instruction(&Instruction::LocalGet(2));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 1,
    });
    f.instruction(&Instruction::LocalSet(4));
    f.instruction(&Instruction::LocalGet(2));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 2,
    });
    f.instruction(&Instruction::LocalSet(6));
    f.instruction(&Instruction::LocalGet(2));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 3,
    });
    f.instruction(&Instruction::LocalSet(7));
    f.instruction(&Instruction::I32Const(0));
    f.instruction(&Instruction::LocalSet(5));
    // for i in 0..cap
    f.instruction(&Instruction::Block(BlockType::Empty));
    f.instruction(&Instruction::Loop(BlockType::Empty));
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::I32GeS);
    f.instruction(&Instruction::BrIf(1));
    // cur_key = keys_a[i]
    f.instruction(&Instruction::LocalGet(6));
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::ArrayGet(slots.keys_array));
    f.instruction(&Instruction::LocalSet(8));
    // if cur_key != null: probe b
    f.instruction(&Instruction::LocalGet(8));
    f.instruction(&Instruction::RefIsNull);
    f.instruction(&Instruction::I32Eqz);
    f.instruction(&Instruction::If(BlockType::Empty));
    // opt = get_fn(b, unbox(cur_key))
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::LocalGet(8));
    emit_unbox_key(&mut f, k_aver, registry);
    f.instruction(&Instruction::Call(get_fn_idx));
    f.instruction(&Instruction::LocalSet(9));
    // if opt.tag == 0 → return 0
    f.instruction(&Instruction::LocalGet(9));
    f.instruction(&Instruction::StructGet {
        struct_type_index: opt_idx,
        field_index: 0,
    });
    f.instruction(&Instruction::I32Eqz);
    f.instruction(&Instruction::If(BlockType::Empty));
    f.instruction(&Instruction::I32Const(0));
    f.instruction(&Instruction::Return);
    f.instruction(&Instruction::End);
    // v_a = values_a[i]; v_b = opt.value
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::ArrayGet(slots.values_array));
    f.instruction(&Instruction::LocalSet(10));
    f.instruction(&Instruction::LocalGet(9));
    f.instruction(&Instruction::StructGet {
        struct_type_index: opt_idx,
        field_index: 1,
    });
    f.instruction(&Instruction::LocalSet(11));
    // if v_a != v_b → return 0
    f.instruction(&Instruction::LocalGet(10));
    f.instruction(&Instruction::LocalGet(11));
    emit_v_eq(&mut f, v_aver, v_helpers)?;
    f.instruction(&Instruction::I32Eqz);
    f.instruction(&Instruction::If(BlockType::Empty));
    f.instruction(&Instruction::I32Const(0));
    f.instruction(&Instruction::Return);
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::End);
    // i++
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Add);
    f.instruction(&Instruction::LocalSet(5));
    f.instruction(&Instruction::Br(0));
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::End);
    Ok(f)
}

/// Stack: `[v_a, v_b]` of Aver type `v_aver`. Push i32 (1=eq, 0=ne).
/// Primitive V → inline cmp; ref V → `Call(v_helpers.eq)`.
fn emit_v_eq(
    f: &mut Function,
    v_aver: &str,
    v_helpers: Option<KeyHelpers>,
) -> Result<(), WasmGcError> {
    match v_aver.trim() {
        "Int" => {
            f.instruction(&Instruction::I64Eq);
        }
        "Bool" => {
            f.instruction(&Instruction::I32Eq);
        }
        "Float" => {
            f.instruction(&Instruction::F64Eq);
        }
        _ => {
            let h = v_helpers.ok_or(WasmGcError::Validation(format!(
                "emit_v_eq: V `{v_aver}` needs ref helpers (record/sum/carrier/list/vec/map)"
            )))?;
            f.instruction(&Instruction::Call(h.eq));
        }
    }
    Ok(())
}

/// Stack: `[v]` of Aver type `v_aver`. Push i32 hash. Primitive V →
/// inline DJB2-style mix; ref V → `Call(v_helpers.hash)`.
fn emit_v_hash(
    f: &mut Function,
    v_aver: &str,
    v_helpers: Option<KeyHelpers>,
) -> Result<(), WasmGcError> {
    match v_aver.trim() {
        "Int" => {
            f.instruction(&Instruction::I32WrapI64);
        }
        "Bool" => {} // already i32
        "Float" => {
            f.instruction(&Instruction::I64ReinterpretF64);
            f.instruction(&Instruction::I32WrapI64);
        }
        _ => {
            let h = v_helpers.ok_or(WasmGcError::Validation(format!(
                "emit_v_hash: V `{v_aver}` needs ref helpers"
            )))?;
            f.instruction(&Instruction::Call(h.hash));
        }
    }
    Ok(())
}

/// `__hash_Map<K,V>(m: eqref) -> i32`. XOR-fold per occupied entry of
/// `djb2(k) * 33 + djb2(v)`. XOR is commutative + associative → the
/// result is invariant to bucket / insertion order.
fn emit_map_hash(
    canonical: &str,
    registry: &TypeRegistry,
    keyh: KeyHelpers,
    v_helpers: Option<KeyHelpers>,
) -> Result<Function, WasmGcError> {
    let slots = slots_for(canonical, registry)?;
    let (k_aver, v_aver) = super::types::parse_map_kv(canonical).unwrap();
    let map_ref = ValType::Ref(RefType {
        nullable: true,
        heap_type: HeapType::Concrete(slots.map),
    });
    let keys_ref = ValType::Ref(RefType {
        nullable: true,
        heap_type: HeapType::Concrete(slots.keys_array),
    });
    let values_ref = ValType::Ref(RefType {
        nullable: true,
        heap_type: HeapType::Concrete(slots.values_array),
    });
    // Locals: 1=typed map, 2=cap, 3=i, 4=keys, 5=values, 6=cur_key
    // (boxed), 7=h (i32 accumulator), 8=entry_h (i32 per-entry mix).
    let mut f = Function::new(vec![
        (1, map_ref),
        (1, ValType::I32),
        (1, ValType::I32),
        (1, keys_ref),
        (1, values_ref),
        (1, key_storage_val_type(k_aver, registry)?),
        (1, ValType::I32),
        (1, ValType::I32),
    ]);
    let map_heap = HeapType::Concrete(slots.map);
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::RefCastNonNull(map_heap));
    f.instruction(&Instruction::LocalSet(1));
    f.instruction(&Instruction::I32Const(0));
    f.instruction(&Instruction::LocalSet(7));
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 1,
    });
    f.instruction(&Instruction::LocalSet(2));
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 2,
    });
    f.instruction(&Instruction::LocalSet(4));
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 3,
    });
    f.instruction(&Instruction::LocalSet(5));
    f.instruction(&Instruction::I32Const(0));
    f.instruction(&Instruction::LocalSet(3));
    f.instruction(&Instruction::Block(BlockType::Empty));
    f.instruction(&Instruction::Loop(BlockType::Empty));
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::LocalGet(2));
    f.instruction(&Instruction::I32GeS);
    f.instruction(&Instruction::BrIf(1));
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::ArrayGet(slots.keys_array));
    f.instruction(&Instruction::LocalSet(6));
    f.instruction(&Instruction::LocalGet(6));
    f.instruction(&Instruction::RefIsNull);
    f.instruction(&Instruction::I32Eqz);
    f.instruction(&Instruction::If(BlockType::Empty));
    // entry_h = hash_K(unbox(cur_key)) * 33 + hash_V(values[i])
    f.instruction(&Instruction::LocalGet(6));
    emit_unbox_key(&mut f, k_aver, registry);
    f.instruction(&Instruction::Call(keyh.hash));
    f.instruction(&Instruction::I32Const(5));
    f.instruction(&Instruction::I32Shl);
    // h_k * 32 (will add h_k below to become *33; OR more accurate:
    // shift-add for *33). Cheaper: do `(kh<<5) + kh + vh`.
    f.instruction(&Instruction::LocalGet(6));
    emit_unbox_key(&mut f, k_aver, registry);
    f.instruction(&Instruction::Call(keyh.hash));
    f.instruction(&Instruction::I32Add);
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::ArrayGet(slots.values_array));
    emit_v_hash(&mut f, v_aver, v_helpers)?;
    f.instruction(&Instruction::I32Add);
    f.instruction(&Instruction::LocalSet(8));
    // h ^= entry_h
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::LocalGet(8));
    f.instruction(&Instruction::I32Xor);
    f.instruction(&Instruction::LocalSet(7));
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Add);
    f.instruction(&Instruction::LocalSet(3));
    f.instruction(&Instruction::Br(0));
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::End);
    Ok(f)
}

/// `remove(map, k) -> map`. Linear-probe locate the entry; if not
/// found, return the map unchanged. If found, do a backwards-shift
/// over the contiguous probe chain so subsequent `get` calls still
/// land their entries (Robin-Hood / canonical open-addressing
/// remove). Decrements `map.size`. Same-handle return (mutates in
/// place).
fn emit_map_remove(
    canonical: &str,
    registry: &TypeRegistry,
    keyh: KeyHelpers,
) -> Result<Function, WasmGcError> {
    let slots = slots_for(canonical, registry)?;
    let (k_aver, v_aver) = super::types::parse_map_kv(canonical).unwrap();
    let _k_val = super::types::aver_to_wasm(k_aver, Some(registry))?.unwrap();
    let v_val = super::types::aver_to_wasm(v_aver, Some(registry))?.unwrap();
    let _ = v_val; // values array uses its own slot type
    // params: 0=map, 1=k.
    // locals: 2=cap, 3=mask, 4=keys, 5=values, 6=h, 7=i, 8=j,
    //         9=cur_key, 10=natural, 11=gap, 12=disp.
    let mut f = Function::new([
        (1, ValType::I32), // 2: cap
        (1, ValType::I32), // 3: mask
        (
            1,
            ValType::Ref(RefType {
                nullable: true,
                heap_type: HeapType::Concrete(slots.keys_array),
            }),
        ), // 4: keys
        (
            1,
            ValType::Ref(RefType {
                nullable: true,
                heap_type: HeapType::Concrete(slots.values_array),
            }),
        ), // 5: values
        (1, ValType::I32), // 6: h
        (1, ValType::I32), // 7: i
        (1, ValType::I32), // 8: j
        (1, key_storage_val_type(k_aver, registry)?), // 9: cur_key (boxed for prim)
        (1, ValType::I32), // 10: natural
        (1, ValType::I32), // 11: gap
        (1, ValType::I32), // 12: disp
    ]);

    // cap = map.cap; mask = cap - 1; keys = map.keys; values = map.values
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 1,
    });
    f.instruction(&Instruction::LocalSet(2));
    f.instruction(&Instruction::LocalGet(2));
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Sub);
    f.instruction(&Instruction::LocalSet(3));
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 2,
    });
    f.instruction(&Instruction::LocalSet(4));
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 3,
    });
    f.instruction(&Instruction::LocalSet(5));

    // h = hash(k) & mask
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::Call(keyh.hash));
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::I32And);
    f.instruction(&Instruction::LocalSet(6));
    f.instruction(&Instruction::LocalGet(6));
    f.instruction(&Instruction::LocalSet(7));

    // Find slot. probe loop.
    f.instruction(&Instruction::Block(BlockType::Empty));
    f.instruction(&Instruction::Loop(BlockType::Empty));
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::ArrayGet(slots.keys_array));
    f.instruction(&Instruction::LocalSet(9));
    // if cur_key == null → not found, return map
    f.instruction(&Instruction::LocalGet(9));
    f.instruction(&Instruction::RefIsNull);
    f.instruction(&Instruction::If(BlockType::Empty));
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::Return);
    f.instruction(&Instruction::End);
    // if eq(unbox(cur_key), k) → break (found at i)
    f.instruction(&Instruction::LocalGet(9));
    emit_unbox_key(&mut f, k_aver, registry);
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::Call(keyh.eq));
    f.instruction(&Instruction::BrIf(1));
    // i = (i+1) & mask
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Add);
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::I32And);
    f.instruction(&Instruction::LocalSet(7));
    // safety: if i wrapped to h → not found (full table miss)
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::LocalGet(6));
    f.instruction(&Instruction::I32Eq);
    f.instruction(&Instruction::If(BlockType::Empty));
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::Return);
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::Br(0));
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::End);

    // Backwards-shift: j = (i+1) & mask
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Add);
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::I32And);
    f.instruction(&Instruction::LocalSet(8));
    f.instruction(&Instruction::Block(BlockType::Empty));
    f.instruction(&Instruction::Loop(BlockType::Empty));
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::LocalGet(8));
    f.instruction(&Instruction::ArrayGet(slots.keys_array));
    f.instruction(&Instruction::LocalSet(9));
    // if next == null → break
    f.instruction(&Instruction::LocalGet(9));
    f.instruction(&Instruction::RefIsNull);
    f.instruction(&Instruction::BrIf(1));
    // natural = hash(unbox(next)) & mask
    f.instruction(&Instruction::LocalGet(9));
    emit_unbox_key(&mut f, k_aver, registry);
    f.instruction(&Instruction::Call(keyh.hash));
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::I32And);
    f.instruction(&Instruction::LocalSet(10));
    // gap = (j - i) & mask
    f.instruction(&Instruction::LocalGet(8));
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::I32Sub);
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::I32And);
    f.instruction(&Instruction::LocalSet(11));
    // disp = (j - natural) & mask
    f.instruction(&Instruction::LocalGet(8));
    f.instruction(&Instruction::LocalGet(10));
    f.instruction(&Instruction::I32Sub);
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::I32And);
    f.instruction(&Instruction::LocalSet(12));
    // if disp < gap → break
    f.instruction(&Instruction::LocalGet(12));
    f.instruction(&Instruction::LocalGet(11));
    f.instruction(&Instruction::I32LtU);
    f.instruction(&Instruction::BrIf(1));
    // shift: keys[i] = next; values[i] = values[j]
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::LocalGet(9));
    f.instruction(&Instruction::ArraySet(slots.keys_array));
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::LocalGet(5));
    f.instruction(&Instruction::LocalGet(8));
    f.instruction(&Instruction::ArrayGet(slots.values_array));
    f.instruction(&Instruction::ArraySet(slots.values_array));
    // i = j; j = (j+1) & mask
    f.instruction(&Instruction::LocalGet(8));
    f.instruction(&Instruction::LocalSet(7));
    f.instruction(&Instruction::LocalGet(8));
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Add);
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::I32And);
    f.instruction(&Instruction::LocalSet(8));
    f.instruction(&Instruction::Br(0));
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::End);

    // keys[i] = null. Heap type matches the keys array element ref —
    // see `key_storage_null_heap` for the per-K-kind table (primitive
    // box / String / record / carrier / List / Vector concrete idx,
    // abstract Eq for sum K).
    let null_heap = key_storage_null_heap(k_aver, registry);
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::LocalGet(7));
    f.instruction(&Instruction::RefNull(null_heap));
    f.instruction(&Instruction::ArraySet(slots.keys_array));

    // map.size = map.size - 1
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 0,
    });
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Sub);
    f.instruction(&Instruction::StructSet {
        struct_type_index: slots.map,
        field_index: 0,
    });

    // return map
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::End);
    Ok(f)
}

/// `entries(m) -> List<Tuple<K, V>>`. Walk keys/values arrays
/// right-to-left; for each occupied slot (`keys[i] != null`) build
/// a `struct.new $tuple(k, v)` and prepend onto a cons-list
/// accumulator.
fn emit_map_entries(canonical: &str, registry: &TypeRegistry) -> Result<Function, WasmGcError> {
    let slots = slots_for(canonical, registry)?;
    let (k_aver, v_aver) = super::types::parse_map_kv(canonical).unwrap();
    let tup_canonical = format!("Tuple<{k_aver},{v_aver}>");
    let tup_idx = registry
        .tuple_type_idx(&tup_canonical)
        .ok_or(WasmGcError::Validation(format!(
            "Map.entries: `{tup_canonical}` not registered"
        )))?;
    let lt_canonical = format!("List<{tup_canonical}>");
    let lt_idx = registry
        .list_type_idx(&lt_canonical)
        .ok_or(WasmGcError::Validation(format!(
            "Map.entries: `{lt_canonical}` not registered"
        )))?;
    let keys_ref = ValType::Ref(RefType {
        nullable: true,
        heap_type: HeapType::Concrete(slots.keys_array),
    });
    let values_ref = ValType::Ref(RefType {
        nullable: true,
        heap_type: HeapType::Concrete(slots.values_array),
    });
    let lt_ref = ValType::Ref(RefType {
        nullable: true,
        heap_type: HeapType::Concrete(lt_idx),
    });
    // params: 0=map. locals: 1=keys, 2=values, 3=i, 4=acc.
    let mut f = Function::new([
        (1, keys_ref),
        (1, values_ref),
        (1, ValType::I32),
        (1, lt_ref),
    ]);
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 2,
    });
    f.instruction(&Instruction::LocalSet(1));
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 3,
    });
    f.instruction(&Instruction::LocalSet(2));
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::StructGet {
        struct_type_index: slots.map,
        field_index: 1,
    });
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Sub);
    f.instruction(&Instruction::LocalSet(3));
    f.instruction(&Instruction::RefNull(HeapType::Concrete(lt_idx)));
    f.instruction(&Instruction::LocalSet(4));
    f.instruction(&Instruction::Block(BlockType::Empty));
    f.instruction(&Instruction::Loop(BlockType::Empty));
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::I32Const(0));
    f.instruction(&Instruction::I32LtS);
    f.instruction(&Instruction::BrIf(1));
    // if keys[i] != null: tup = struct.new $tuple(unbox(keys[i]),
    // values[i]); acc = cons(tup, acc)
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::ArrayGet(slots.keys_array));
    f.instruction(&Instruction::RefIsNull);
    f.instruction(&Instruction::I32Eqz);
    f.instruction(&Instruction::If(BlockType::Empty));
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::ArrayGet(slots.keys_array));
    emit_unbox_key(&mut f, k_aver, registry);
    f.instruction(&Instruction::LocalGet(2));
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::ArrayGet(slots.values_array));
    f.instruction(&Instruction::StructNew(tup_idx));
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::StructNew(lt_idx));
    f.instruction(&Instruction::LocalSet(4));
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::I32Const(1));
    f.instruction(&Instruction::I32Sub);
    f.instruction(&Instruction::LocalSet(3));
    f.instruction(&Instruction::Br(0));
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::LocalGet(4));
    f.instruction(&Instruction::End);
    Ok(f)
}

/// `from_list(l) -> Map<K, V>`. Walks `l` from head to tail,
/// struct.get's the (K, V) from each tuple, calls the per-(K, V)
/// `set` helper to insert. Allocates a fresh empty map (via the
/// per-(K, V) `empty` shape inlined: cap = INITIAL_CAP, fresh keys
/// and values arrays) and returns it.
fn emit_map_from_list(
    canonical: &str,
    registry: &TypeRegistry,
    set_fn: u32,
) -> Result<Function, WasmGcError> {
    let slots = slots_for(canonical, registry)?;
    let (k_aver, v_aver) = super::types::parse_map_kv(canonical).unwrap();
    let tup_canonical = format!("Tuple<{k_aver},{v_aver}>");
    let tup_idx = registry
        .tuple_type_idx(&tup_canonical)
        .ok_or(WasmGcError::Validation(format!(
            "Map.fromList: `{tup_canonical}` not registered"
        )))?;
    let lt_canonical = format!("List<{tup_canonical}>");
    let lt_idx = registry
        .list_type_idx(&lt_canonical)
        .ok_or(WasmGcError::Validation(format!(
            "Map.fromList: `{lt_canonical}` not registered"
        )))?;
    let map_ref = ValType::Ref(RefType {
        nullable: true,
        heap_type: HeapType::Concrete(slots.map),
    });
    let lt_ref = ValType::Ref(RefType {
        nullable: true,
        heap_type: HeapType::Concrete(lt_idx),
    });
    // params: 0=l. locals: 1=cur, 2=map, 3=tup.
    let mut f = Function::new([
        (1, lt_ref),
        (1, map_ref),
        (
            1,
            ValType::Ref(RefType {
                nullable: true,
                heap_type: HeapType::Concrete(tup_idx),
            }),
        ),
    ]);
    // map = inline empty allocation (matches emit_map_empty)
    f.instruction(&Instruction::I32Const(0));
    f.instruction(&Instruction::I32Const(INITIAL_CAP));
    f.instruction(&Instruction::I32Const(INITIAL_CAP));
    f.instruction(&Instruction::ArrayNewDefault(slots.keys_array));
    f.instruction(&Instruction::I32Const(INITIAL_CAP));
    f.instruction(&Instruction::ArrayNewDefault(slots.values_array));
    f.instruction(&Instruction::StructNew(slots.map));
    f.instruction(&Instruction::LocalSet(2));
    // cur = l
    f.instruction(&Instruction::LocalGet(0));
    f.instruction(&Instruction::LocalSet(1));
    f.instruction(&Instruction::Block(BlockType::Empty));
    f.instruction(&Instruction::Loop(BlockType::Empty));
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::RefIsNull);
    f.instruction(&Instruction::BrIf(1));
    // tup = cur.head
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::StructGet {
        struct_type_index: lt_idx,
        field_index: 0,
    });
    f.instruction(&Instruction::LocalSet(3));
    // map = set(map, tup.0, tup.1)
    f.instruction(&Instruction::LocalGet(2));
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::StructGet {
        struct_type_index: tup_idx,
        field_index: 0,
    });
    f.instruction(&Instruction::LocalGet(3));
    f.instruction(&Instruction::StructGet {
        struct_type_index: tup_idx,
        field_index: 1,
    });
    f.instruction(&Instruction::Call(set_fn));
    f.instruction(&Instruction::LocalSet(2));
    // cur = cur.tail
    f.instruction(&Instruction::LocalGet(1));
    f.instruction(&Instruction::StructGet {
        struct_type_index: lt_idx,
        field_index: 1,
    });
    f.instruction(&Instruction::LocalSet(1));
    f.instruction(&Instruction::Br(0));
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::LocalGet(2));
    f.instruction(&Instruction::End);
    Ok(f)
}

/// `hash : (parent_ref) -> i32` for a user sum type. Per-variant
/// `ref.test` cascade: each constructor V_i has a tag (its
/// alphabetical index in the variant list, baked at compile time)
/// folded in DJB2-style with each V_i field's hash. Variants are
/// sorted by name for stable emit. Field-type dispatch covers
/// `{Int, Float, Bool, String}`; other field types surface as
/// Unimplemented.
fn emit_hash_sum(
    parent_name: &str,
    registry: &TypeRegistry,
    string_key_helpers: Option<KeyHelpers>,
    all_key_helpers: &HashMap<String, KeyHelpers>,
) -> Result<Function, WasmGcError> {
    let mut variants: Vec<(String, super::types::VariantInfo)> = registry
        .variants
        .iter()
        .flat_map(|(n, vs)| vs.iter().map(move |v| (n.clone(), v.clone())))
        .filter(|(_, v)| v.parent == parent_name)
        .collect();
    variants.sort_by(|a, b| a.0.cmp(&b.0));
    if variants.is_empty() {
        return Err(WasmGcError::Validation(format!(
            "hash_sum: `{parent_name}` has no variants"
        )));
    }
    let mut f = Function::new([(1, ValType::I32) /* h */]);
    f.instruction(&Instruction::Block(BlockType::Empty));
    for (tag, (_v_name, info)) in variants.iter().enumerate() {
        let v_idx = info.type_idx;
        let v_heap = wasm_encoder::HeapType::Concrete(v_idx);
        // if ref.test V head: h = (5381*33+tag), then fold fields, return
        f.instruction(&Instruction::LocalGet(0));
        f.instruction(&Instruction::RefTestNonNull(v_heap));
        f.instruction(&Instruction::If(BlockType::Empty));
        // Initial h = 5381 * 33 + tag (variant discriminator).
        f.instruction(&Instruction::I32Const(5381 * 33 + tag as i32));
        f.instruction(&Instruction::LocalSet(1));
        for (i, field_ty) in info.fields.iter().enumerate() {
            // h = h * 33
            f.instruction(&Instruction::LocalGet(1));
            f.instruction(&Instruction::I32Const(5));
            f.instruction(&Instruction::I32Shl);
            f.instruction(&Instruction::LocalGet(1));
            f.instruction(&Instruction::I32Add);
            // push field_value as i32 hash
            f.instruction(&Instruction::LocalGet(0));
            f.instruction(&Instruction::RefCastNonNull(v_heap));
            f.instruction(&Instruction::StructGet {
                struct_type_index: v_idx,
                field_index: i as u32,
            });
            let field_ty_trim = field_ty.trim();
            match field_ty_trim {
                "Int" => {
                    f.instruction(&Instruction::I32WrapI64);
                }
                "Bool" => {}
                "Float" => {
                    f.instruction(&Instruction::I64ReinterpretF64);
                    f.instruction(&Instruction::I32WrapI64);
                }
                "String" => {
                    let helpers = string_key_helpers.ok_or(WasmGcError::Validation(
                        "hash_sum: String field needs String key helpers".into(),
                    ))?;
                    f.instruction(&Instruction::Call(helpers.hash));
                }
                _ => {
                    // Compound field — proxy via Call to the per-type
                    // hash helper assembled in `all_key_helpers`
                    // (records / sums / Option / Result / Tuple /
                    // List<T> / Vector<T> / Map<K,V>). Compound names
                    // get whitespace stripped so the lookup matches
                    // the canonical form used to register helpers.
                    let lookup_key = super::types::normalize_compound(field_ty_trim);
                    let helpers = all_key_helpers
                        .get(&lookup_key)
                        .or_else(|| all_key_helpers.get(field_ty_trim))
                        .ok_or_else(|| {
                            WasmGcError::Validation(format!(
                                "hash_sum: no helper registered for sum-variant field \
                                 type `{field_ty_trim}` of `{parent_name}`"
                            ))
                        })?;
                    f.instruction(&Instruction::Call(helpers.hash));
                }
            }
            f.instruction(&Instruction::I32Add);
            f.instruction(&Instruction::LocalSet(1));
        }
        f.instruction(&Instruction::LocalGet(1));
        f.instruction(&Instruction::Return);
        f.instruction(&Instruction::End);
    }
    f.instruction(&Instruction::End);
    // Defensive 0 (exhaustiveness should make this unreachable).
    f.instruction(&Instruction::I32Const(0));
    f.instruction(&Instruction::End);
    Ok(f)
}

/// `eq : (parent_ref, parent_ref) -> i32` for a user sum type.
/// Per-variant `ref.test` cascade: head and needle must share a
/// constructor, then field-by-field eq with `i32.and` fold.
fn emit_eq_sum(
    parent_name: &str,
    registry: &TypeRegistry,
    string_key_helpers: Option<KeyHelpers>,
    all_key_helpers: &HashMap<String, KeyHelpers>,
) -> Result<Function, WasmGcError> {
    let mut variants: Vec<(String, super::types::VariantInfo)> = registry
        .variants
        .iter()
        .flat_map(|(n, vs)| vs.iter().map(move |v| (n.clone(), v.clone())))
        .filter(|(_, v)| v.parent == parent_name)
        .collect();
    variants.sort_by(|a, b| a.0.cmp(&b.0));
    if variants.is_empty() {
        return Err(WasmGcError::Validation(format!(
            "eq_sum: `{parent_name}` has no variants"
        )));
    }
    // params: 0=head, 1=needle. No locals.
    let mut f = Function::new([]);
    f.instruction(&Instruction::Block(BlockType::Result(ValType::I32)));
    for (_v_name, info) in &variants {
        let v_idx = info.type_idx;
        let v_heap = wasm_encoder::HeapType::Concrete(v_idx);
        // if ref.test V head:
        f.instruction(&Instruction::LocalGet(0));
        f.instruction(&Instruction::RefTestNonNull(v_heap));
        f.instruction(&Instruction::If(BlockType::Empty));
        // if ref.test V needle:
        f.instruction(&Instruction::LocalGet(1));
        f.instruction(&Instruction::RefTestNonNull(v_heap));
        f.instruction(&Instruction::If(BlockType::Empty));
        if info.fields.is_empty() {
            f.instruction(&Instruction::I32Const(1));
        } else {
            for (i, field_ty) in info.fields.iter().enumerate() {
                f.instruction(&Instruction::LocalGet(0));
                f.instruction(&Instruction::RefCastNonNull(v_heap));
                f.instruction(&Instruction::StructGet {
                    struct_type_index: v_idx,
                    field_index: i as u32,
                });
                f.instruction(&Instruction::LocalGet(1));
                f.instruction(&Instruction::RefCastNonNull(v_heap));
                f.instruction(&Instruction::StructGet {
                    struct_type_index: v_idx,
                    field_index: i as u32,
                });
                let field_ty_trim = field_ty.trim();
                match field_ty_trim {
                    "Int" => f.instruction(&Instruction::I64Eq),
                    "Bool" => f.instruction(&Instruction::I32Eq),
                    "Float" => f.instruction(&Instruction::F64Eq),
                    "String" => {
                        let helpers = string_key_helpers.ok_or(WasmGcError::Validation(
                            "eq_sum: String field needs String key helpers".into(),
                        ))?;
                        f.instruction(&Instruction::Call(helpers.eq))
                    }
                    _ => {
                        // Compound field — proxy to per-type eq helper.
                        let lookup_key = super::types::normalize_compound(field_ty_trim);
                        let helpers = all_key_helpers
                            .get(&lookup_key)
                            .or_else(|| all_key_helpers.get(field_ty_trim))
                            .ok_or_else(|| {
                                WasmGcError::Validation(format!(
                                    "eq_sum: no helper registered for sum-variant field \
                                     type `{field_ty_trim}` of `{parent_name}`"
                                ))
                            })?;
                        f.instruction(&Instruction::Call(helpers.eq))
                    }
                };
                if i > 0 {
                    f.instruction(&Instruction::I32And);
                }
            }
        }
        f.instruction(&Instruction::Br(2));
        f.instruction(&Instruction::Else);
        // head V, needle != V → 0
        f.instruction(&Instruction::I32Const(0));
        f.instruction(&Instruction::Br(2));
        f.instruction(&Instruction::End);
        f.instruction(&Instruction::End);
    }
    // Defensive — no variant matched head.
    f.instruction(&Instruction::I32Const(0));
    f.instruction(&Instruction::End);
    f.instruction(&Instruction::End);
    Ok(f)
}