aver-lang 0.16.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
;; Flat hashtable Map runtime — open-addressing, linear probing.
;; Single-chunk allocation: header + buckets in one contiguous block.
;;
;; Layout
;; ------
;;   offset 0  : header word
;;     bits 56-63 : kind = 12 (OBJ_MAP)
;;     bits 32-47 : meta — bit 0 = value_ptr_flag (set if values are heap ptrs)
;;     bits  0-31 : count (number of occupied buckets)
;;   offset 8  : capacity word (i64)
;;     bits  0-31 : capacity (power of 2)
;;     bits 32-39 : key_kind (0=Int, 1=Float, 2=Bool, 3=String, 4=heap)
;;   offset 16 : bucket array (capacity × 24 bytes each)
;;
;; Bucket layout (24 bytes)
;;   offset 0   : state — 0 = empty, 1 = occupied, 2 = tombstone
;;   offset 4   : hash i32 (cached, valid when state == 1)
;;   offset 8   : key i64
;;   offset 16  : value i64
;;
;; Empty map sentinel: pointer 0 (no allocation). `rt_map_set` allocates
;; a fresh table with `INITIAL_CAP` buckets on first insert.
;;
;; Polymorphic key ABI (unchanged from prior HAMT runtime):
;;   rt_map_get(map, key, kind) → Option<V>
;;   rt_map_has(map, key, kind) → bool
;;   rt_map_set(map, key, kind, value, value_ptr_flag) → new map
;;   rt_map_set_owned(map, key, kind, value, value_ptr_flag) → mutated map
;;   rt_map_keys(map) → List<K>
;;   rt_map_entries(map) → List<(K, V)>
;;   rt_map_len(map) → i64
;;   rt_map_from_list(list, key_kind, value_ptr_flag) → map
;;
;; Owned-mutate (`rt_map_set_owned`) is the canonical TCO build path.
;; When last-use analysis proves `map` has no other observer, the
;; emitter calls _owned and the runtime mutates buckets in place
;; (zero alloc on update + amortized O(1) on insert; resize still
;; allocates a fresh table).

;; ---------------------------------------------------------------------
;; Hashing — same surface as before so existing rt_hash_key callers
;; (rt_map_from_list internally) keep working.
;; ---------------------------------------------------------------------

(func $rt_mix_i64 (param $x i64) (result i32)
  (local $h i64)
  local.get $x
  local.set $h

  local.get $h
  local.get $h
  i64.const 33
  i64.shr_u
  i64.xor
  i64.const 0xff51afd7ed558ccd
  i64.mul
  local.set $h

  local.get $h
  local.get $h
  i64.const 33
  i64.shr_u
  i64.xor
  i64.const 0xc4ceb9fe1a85ec53
  i64.mul
  local.set $h

  local.get $h
  local.get $h
  i64.const 33
  i64.shr_u
  i64.xor
  i32.wrap_i64
)

(func $rt_hash_string_obj (param $ptr i32) (result i32)
  (local $len i32)
  (local $i i32)
  (local $h i32)

  local.get $ptr
  i64.load
  i64.const 0xFFFFFFFF
  i64.and
  i32.wrap_i64
  local.set $len

  i32.const 5381
  local.set $h
  i32.const 0
  local.set $i

  block
    loop
      local.get $i
      local.get $len
      i32.ge_u
      br_if 1

      local.get $h
      i32.const 5
      i32.shl
      local.get $h
      i32.add
      local.get $ptr
      i32.const 8
      i32.add
      local.get $i
      i32.add
      i32.load8_u
      i32.add
      local.set $h

      local.get $i
      i32.const 1
      i32.add
      local.set $i
      br 0
    end
  end

  local.get $h
)

(func $rt_deep_hash_fwd (param $ptr i32) (result i32)
  local.get $ptr
  call $rt_deep_hash
)

;; Deep structural hash for heap keys (kind == 4). Walks the object by
;; its OBJ_KIND header byte and recurses on i64 fields whose
;; per-field ptr-flag is set in the meta byte.
(func $rt_deep_hash (param $ptr i32) (result i32)
  (local $kind i32)
  (local $tag i32)
  (local $meta i32)
  (local $count i32)
  (local $i i32)
  (local $h i32)
  (local $field i64)
  (local $is_ptr i32)
  (local $sub i32)

  local.get $ptr
  i32.eqz
  if
    i32.const 0
    return
  end

  local.get $ptr
  call $rt_obj_kind
  local.set $kind

  local.get $ptr
  call $rt_obj_tag
  local.set $tag

  local.get $ptr
  call $rt_obj_meta
  local.set $meta

  ;; OBJ_STRING — djb2 of bytes
  local.get $kind
  i32.const 0
  i32.eq
  if
    local.get $ptr
    call $rt_hash_string_obj
    return
  end

  ;; LIST_CONS family — head + rotated tail hash
  local.get $kind
  i32.const 4
  i32.eq
  if
    local.get $ptr
    i32.const 0
    call $rt_obj_field
    local.set $field
    local.get $meta
    i32.const 1
    i32.and
    local.set $is_ptr

    local.get $is_ptr
    if (result i32)
      local.get $field
      i32.wrap_i64
      call $rt_deep_hash_fwd
    else
      local.get $field
      call $rt_mix_i64
    end
    local.set $h

    local.get $ptr
    i32.const 1
    call $rt_obj_field_i32
    local.set $sub

    local.get $h
    i32.const 16
    i32.rotl
    local.get $sub
    call $rt_deep_hash_fwd
    i32.xor
    return
  end

  local.get $kind
  i32.const 9
  i32.eq
  if
    local.get $ptr
    i32.const 0
    call $rt_obj_field_f64
    i64.reinterpret_f64
    call $rt_mix_i64
    local.set $h

    local.get $ptr
    i32.const 1
    call $rt_obj_field_i32
    local.set $sub

    local.get $h
    i32.const 16
    i32.rotl
    local.get $sub
    call $rt_deep_hash_fwd
    i32.xor
    return
  end

  ;; OBJ_TUPLE / OBJ_RECORD / OBJ_VARIANT — meta byte = ptr bitmap
  local.get $kind
  i32.const 5
  i32.eq
  local.get $kind
  i32.const 1
  i32.eq
  i32.or
  local.get $kind
  i32.const 2
  i32.eq
  i32.or
  if
    local.get $ptr
    i64.load
    i64.const 0xFFFFFFFF
    i64.and
    i32.wrap_i64
    local.set $count

    local.get $kind
    i32.const 8
    i32.shl
    local.get $tag
    i32.or
    local.set $h

    i32.const 0
    local.set $i
    block
      loop
        local.get $i
        local.get $count
        i32.ge_u
        br_if 1

        local.get $ptr
        local.get $i
        call $rt_obj_field
        local.set $field

        local.get $meta
        local.get $i
        i32.shr_u
        i32.const 1
        i32.and
        local.set $is_ptr

        local.get $is_ptr
        if (result i32)
          local.get $field
          i32.wrap_i64
          call $rt_deep_hash_fwd
        else
          local.get $field
          call $rt_mix_i64
        end
        local.set $sub

        local.get $h
        i32.const 5
        i32.rotl
        local.get $sub
        i32.xor
        local.set $h

        local.get $i
        i32.const 1
        i32.add
        local.set $i
        br 0
      end
    end

    local.get $h
    return
  end

  ;; OBJ_WRAPPER families — wrap kind + tag + inner.
  local.get $kind
  i32.const 3
  i32.eq
  if
    local.get $kind
    i32.const 8
    i32.shl
    local.get $tag
    i32.or
    local.set $h
    local.get $ptr
    i32.const 0
    call $rt_obj_field
    call $rt_mix_i64
    local.get $h
    i32.xor
    return
  end

  local.get $kind
  i32.const 7
  i32.eq
  if
    local.get $kind
    i32.const 8
    i32.shl
    local.get $tag
    i32.or
    local.set $h
    local.get $ptr
    i32.const 0
    call $rt_obj_field_f64
    i64.reinterpret_f64
    call $rt_mix_i64
    local.get $h
    i32.xor
    return
  end

  local.get $kind
  i32.const 8
  i32.eq
  if
    local.get $kind
    i32.const 8
    i32.shl
    local.get $tag
    i32.or
    local.set $h
    local.get $ptr
    i32.const 0
    call $rt_obj_field_i32
    i64.extend_i32_s
    call $rt_mix_i64
    local.get $h
    i32.xor
    return
  end

  ;; Fallback: identity-of-ptr.
  local.get $ptr
  i64.extend_i32_s
  call $rt_mix_i64
)

;; Hash a key under the polymorphic key kind (0=Int, 1=Float, 2=Bool,
;; 3=String OBJ_STRING ptr, 4=heap arbitrary).
(func $rt_hash_key (param $key i64) (param $kind i32) (result i32)
  local.get $kind
  i32.const 3
  i32.eq
  if
    local.get $key
    i32.wrap_i64
    call $rt_hash_string_obj
    return
  end
  local.get $kind
  i32.const 4
  i32.eq
  if
    local.get $key
    i32.wrap_i64
    call $rt_deep_hash_fwd
    return
  end
  local.get $key
  call $rt_mix_i64
)

;; Forward decl alias for recursive equality.
(func $rt_map_key_eq_fwd (param $a i64) (param $b i64) (param $kind i32) (result i32)
  local.get $a
  local.get $b
  local.get $kind
  call $rt_map_key_eq
)

;; Deep equality for two heap pointers (kind == 4): compare object
;; structure recursively. Mirrors `rt_deep_hash`.
(func $rt_deep_eq (param $a i32) (param $b i32) (result i32)
  (local $ka i32)
  (local $kb i32)
  (local $ta i32)
  (local $tb i32)
  (local $ma i32)
  (local $mb i32)
  (local $ca i32)
  (local $cb i32)
  (local $i i32)
  (local $fa i64)
  (local $fb i64)
  (local $is_ptr i32)
  (local $sa i32)
  (local $sb i32)

  local.get $a
  local.get $b
  i32.eq
  if
    i32.const 1
    return
  end

  local.get $a
  i32.eqz
  local.get $b
  i32.eqz
  i32.or
  if
    i32.const 0
    return
  end

  local.get $a
  call $rt_obj_kind
  local.set $ka
  local.get $b
  call $rt_obj_kind
  local.set $kb
  local.get $ka
  local.get $kb
  i32.ne
  if
    i32.const 0
    return
  end

  local.get $a
  call $rt_obj_tag
  local.set $ta
  local.get $b
  call $rt_obj_tag
  local.set $tb
  local.get $ta
  local.get $tb
  i32.ne
  if
    i32.const 0
    return
  end

  local.get $a
  call $rt_obj_meta
  local.set $ma
  local.get $b
  call $rt_obj_meta
  local.set $mb

  ;; STRING: byte equality via existing rt_str_eq.
  local.get $ka
  i32.const 0
  i32.eq
  if
    local.get $a
    local.get $b
    call $rt_str_eq
    return
  end

  ;; LIST_CONS i64 head: head equality + recursive tail.
  local.get $ka
  i32.const 4
  i32.eq
  if
    local.get $a
    i32.const 0
    call $rt_obj_field
    local.set $fa
    local.get $b
    i32.const 0
    call $rt_obj_field
    local.set $fb
    local.get $ma
    i32.const 1
    i32.and
    local.set $is_ptr

    local.get $is_ptr
    if (result i32)
      local.get $fa
      i32.wrap_i64
      local.get $fb
      i32.wrap_i64
      call $rt_deep_eq
    else
      local.get $fa
      local.get $fb
      i64.eq
    end
    i32.eqz
    if
      i32.const 0
      return
    end

    local.get $a
    i32.const 1
    call $rt_obj_field_i32
    local.set $sa
    local.get $b
    i32.const 1
    call $rt_obj_field_i32
    local.set $sb
    local.get $sa
    local.get $sb
    call $rt_deep_eq
    return
  end

  ;; LIST_CONS f64 head.
  local.get $ka
  i32.const 9
  i32.eq
  if
    local.get $a
    i32.const 0
    call $rt_obj_field_f64
    local.get $b
    i32.const 0
    call $rt_obj_field_f64
    f64.eq
    i32.eqz
    if
      i32.const 0
      return
    end
    local.get $a
    i32.const 1
    call $rt_obj_field_i32
    local.set $sa
    local.get $b
    i32.const 1
    call $rt_obj_field_i32
    local.set $sb
    local.get $sa
    local.get $sb
    call $rt_deep_eq
    return
  end

  ;; TUPLE / RECORD / VARIANT: walk fields with meta as ptr-bitmap.
  local.get $ka
  i32.const 5
  i32.eq
  local.get $ka
  i32.const 1
  i32.eq
  i32.or
  local.get $ka
  i32.const 2
  i32.eq
  i32.or
  if
    local.get $a
    i64.load
    i64.const 0xFFFFFFFF
    i64.and
    i32.wrap_i64
    local.set $ca
    local.get $b
    i64.load
    i64.const 0xFFFFFFFF
    i64.and
    i32.wrap_i64
    local.set $cb
    local.get $ca
    local.get $cb
    i32.ne
    if
      i32.const 0
      return
    end

    i32.const 0
    local.set $i
    block
      loop
        local.get $i
        local.get $ca
        i32.ge_u
        br_if 1

        local.get $a
        local.get $i
        call $rt_obj_field
        local.set $fa
        local.get $b
        local.get $i
        call $rt_obj_field
        local.set $fb
        local.get $ma
        local.get $i
        i32.shr_u
        i32.const 1
        i32.and
        local.set $is_ptr

        local.get $is_ptr
        if (result i32)
          local.get $fa
          i32.wrap_i64
          local.get $fb
          i32.wrap_i64
          call $rt_deep_eq
        else
          local.get $fa
          local.get $fb
          i64.eq
        end
        i32.eqz
        if
          i32.const 0
          return
        end

        local.get $i
        i32.const 1
        i32.add
        local.set $i
        br 0
      end
    end

    i32.const 1
    return
  end

  ;; WRAPPER families: compare inner.
  local.get $ka
  i32.const 3
  i32.eq
  if
    local.get $a
    i32.const 0
    call $rt_obj_field
    local.get $b
    i32.const 0
    call $rt_obj_field
    i64.eq
    return
  end

  local.get $ka
  i32.const 7
  i32.eq
  if
    local.get $a
    i32.const 0
    call $rt_obj_field_f64
    local.get $b
    i32.const 0
    call $rt_obj_field_f64
    f64.eq
    return
  end

  local.get $ka
  i32.const 8
  i32.eq
  if
    local.get $a
    i32.const 0
    call $rt_obj_field_i32
    local.get $b
    i32.const 0
    call $rt_obj_field_i32
    i32.eq
    return
  end

  ;; Fallback: pointer equality (already tested above).
  i32.const 0
)

;; Polymorphic key equality dispatch.
(func $rt_map_key_eq (param $a i64) (param $b i64) (param $kind i32) (result i32)
  local.get $kind
  i32.const 3
  i32.eq
  if
    local.get $a
    i32.wrap_i64
    local.get $b
    i32.wrap_i64
    call $rt_str_eq
    return
  end
  local.get $kind
  i32.const 4
  i32.eq
  if
    local.get $a
    i32.wrap_i64
    local.get $b
    i32.wrap_i64
    call $rt_deep_eq
    return
  end
  local.get $a
  local.get $b
  i64.eq
)

;; ---------------------------------------------------------------------
;; Layout helpers
;; ---------------------------------------------------------------------

;; Initial capacity for a fresh map allocated by `rt_map_set` /
;; `rt_map_set_owned` when the input was the empty sentinel (0).
(global $g_map_initial_cap i32 (i32.const 8))

;; Read capacity from header at offset 8 (low 32 bits).
(func $rt_map_capacity (param $map i32) (result i32)
  local.get $map
  i32.eqz
  if
    i32.const 0
    return
  end
  local.get $map
  i64.load offset=8
  i32.wrap_i64
)

;; Read key_kind from capacity-word bits 32..39.
(func $rt_map_key_kind (param $map i32) (result i32)
  local.get $map
  i32.eqz
  if
    i32.const 0
    return
  end
  local.get $map
  i64.load offset=8
  i64.const 32
  i64.shr_u
  i64.const 0xFF
  i64.and
  i32.wrap_i64
)

;; Read count from header low 32 bits.
(func $rt_map_count (param $map i32) (result i32)
  local.get $map
  i32.eqz
  if
    i32.const 0
    return
  end
  local.get $map
  i64.load
  i64.const 0xFFFFFFFF
  i64.and
  i32.wrap_i64
)

;; Read value_ptr_flag from meta bit 0 (bits 32..47 of header word).
(func $rt_map_value_ptr_flag (param $map i32) (result i32)
  local.get $map
  i32.eqz
  if
    i32.const 0
    return
  end
  local.get $map
  i64.load
  i64.const 32
  i64.shr_u
  i64.const 0xFFFF
  i64.and
  i32.wrap_i64
  i32.const 1
  i32.and
)

;; Compute bucket address: map + 16 + idx * 24.
(func $rt_map_bucket_addr (param $map i32) (param $idx i32) (result i32)
  local.get $map
  i32.const 16
  i32.add
  local.get $idx
  i32.const 24
  i32.mul
  i32.add
)

;; Mutate header in place: write count + value_ptr_flag back.
(func $rt_map_write_header (param $map i32) (param $count i32) (param $value_ptr_flag i32)
  local.get $map
  ;; header = (kind=12 << 56) | (value_ptr_flag << 32) | count
  i64.const 0x0C00000000000000
  local.get $value_ptr_flag
  i64.extend_i32_u
  i64.const 32
  i64.shl
  i64.or
  local.get $count
  i64.extend_i32_u
  i64.or
  i64.store
)

;; Allocate fresh empty map with given capacity (must be power of 2).
;; Buckets are zero-initialized so every state byte starts at 0 (empty).
(func $rt_map_alloc
    (param $cap i32) (param $key_kind i32) (param $value_ptr_flag i32)
    (result i32)
  (local $size i32)
  (local $ptr i32)
  (local $i i32)
  (local $bucket_addr i32)

  ;; size = 16 + cap * 24
  i32.const 16
  local.get $cap
  i32.const 24
  i32.mul
  i32.add
  local.set $size

  local.get $size
  call $rt_alloc
  local.set $ptr

  ;; Header (count=0)
  local.get $ptr
  i32.const 0
  local.get $value_ptr_flag
  call $rt_map_write_header

  ;; Capacity word at offset 8: capacity in low 32 bits,
  ;; key_kind in bits 32..39.
  local.get $ptr
  local.get $cap
  i64.extend_i32_u
  local.get $key_kind
  i64.extend_i32_u
  i64.const 32
  i64.shl
  i64.or
  i64.store offset=8

  ;; Zero state in every bucket (rt_alloc gives un-zeroed bump memory).
  i32.const 0
  local.set $i
  block
    loop
      local.get $i
      local.get $cap
      i32.ge_u
      br_if 1

      local.get $ptr
      local.get $i
      call $rt_map_bucket_addr
      local.set $bucket_addr

      local.get $bucket_addr
      i32.const 0
      i32.store ;; state = 0 (empty)

      local.get $i
      i32.const 1
      i32.add
      local.set $i
      br 0
    end
  end

  local.get $ptr
)

;; Probe for a key. Returns:
;;   high bit (1<<31) set    → key was found at low-31-bit slot index
;;   high bit clear          → key not found, low-31-bit slot is the
;;                              insertion target (the first empty or
;;                              tombstone slot encountered).
;; Caller must guarantee the table is not full (count < capacity).
(func $rt_map_probe
  (param $map i32) (param $hash i32) (param $key i64) (param $kind i32)
  (result i32)
  (local $cap i32)
  (local $mask i32)
  (local $idx i32)
  (local $tomb i32)
  (local $tomb_seen i32)
  (local $bucket_addr i32)
  (local $state i32)
  (local $bucket_hash i32)
  (local $bucket_key i64)

  local.get $map
  i64.load offset=8
  i32.wrap_i64
  local.set $cap

  local.get $cap
  i32.const 1
  i32.sub
  local.set $mask

  local.get $hash
  local.get $mask
  i32.and
  local.set $idx

  i32.const 0
  local.set $tomb_seen
  i32.const 0
  local.set $tomb

  block
    loop
      local.get $map
      local.get $idx
      call $rt_map_bucket_addr
      local.set $bucket_addr

      local.get $bucket_addr
      i32.load
      local.set $state

      ;; Empty slot: not found. Use first tombstone if we saw one,
      ;; otherwise this empty slot.
      local.get $state
      i32.eqz
      if
        local.get $tomb_seen
        if (result i32)
          local.get $tomb
        else
          local.get $idx
        end
        return
      end

      ;; Occupied: check hash + key.
      local.get $state
      i32.const 1
      i32.eq
      if
        local.get $bucket_addr
        i32.load offset=4
        local.set $bucket_hash

        local.get $bucket_hash
        local.get $hash
        i32.eq
        if
          local.get $bucket_addr
          i64.load offset=8
          local.set $bucket_key
          local.get $bucket_key
          local.get $key
          local.get $kind
          call $rt_map_key_eq
          if
            local.get $idx
            i32.const 0x80000000
            i32.or
            return
          end
        end
      else
        ;; Tombstone (state == 2): remember first one, keep probing.
        local.get $tomb_seen
        i32.eqz
        if
          i32.const 1
          local.set $tomb_seen
          local.get $idx
          local.set $tomb
        end
      end

      local.get $idx
      i32.const 1
      i32.add
      local.get $mask
      i32.and
      local.set $idx
      br 0
    end
  end

  unreachable
)

;; Internal: insert (key, value) into a map that has guaranteed slack.
;; Returns 1 if a new slot was used (count++), 0 if an existing key was
;; updated. Mutates the map in place.
(func $rt_map_insert_raw
  (param $map i32) (param $hash i32) (param $key i64) (param $kind i32)
  (param $value i64)
  (result i32)
  (local $probe i32)
  (local $idx i32)
  (local $found i32)
  (local $bucket_addr i32)

  local.get $map
  local.get $hash
  local.get $key
  local.get $kind
  call $rt_map_probe
  local.set $probe

  local.get $probe
  i32.const 0x80000000
  i32.and
  local.set $found

  local.get $probe
  i32.const 0x7FFFFFFF
  i32.and
  local.set $idx

  local.get $map
  local.get $idx
  call $rt_map_bucket_addr
  local.set $bucket_addr

  ;; state = 1
  local.get $bucket_addr
  i32.const 1
  i32.store
  ;; hash
  local.get $bucket_addr
  local.get $hash
  i32.store offset=4
  ;; key
  local.get $bucket_addr
  local.get $key
  i64.store offset=8
  ;; value
  local.get $bucket_addr
  local.get $value
  i64.store offset=16

  local.get $found
  if (result i32)
    i32.const 0  ;; updated, count unchanged
  else
    i32.const 1  ;; new slot, count + 1
  end
)

;; Resize: allocate a new map with double capacity and re-insert every
;; occupied bucket. Returns the new map. Tombstones disappear.
(func $rt_map_resize_to (param $old i32) (param $new_cap i32) (result i32)
  (local $new i32)
  (local $cap i32)
  (local $i i32)
  (local $bucket_addr i32)
  (local $state i32)
  (local $hash i32)
  (local $key i64)
  (local $value i64)
  (local $kkind i32)
  (local $vflag i32)
  (local $count i32)

  local.get $old
  call $rt_map_value_ptr_flag
  local.set $vflag

  local.get $old
  call $rt_map_key_kind
  local.set $kkind

  local.get $new_cap
  local.get $kkind
  local.get $vflag
  call $rt_map_alloc
  local.set $new

  local.get $old
  call $rt_map_capacity
  local.set $cap
  i32.const 0
  local.set $count

  i32.const 0
  local.set $i
  block
    loop
      local.get $i
      local.get $cap
      i32.ge_u
      br_if 1

      local.get $old
      local.get $i
      call $rt_map_bucket_addr
      local.set $bucket_addr

      local.get $bucket_addr
      i32.load
      local.set $state

      local.get $state
      i32.const 1
      i32.eq
      if
        local.get $bucket_addr
        i32.load offset=4
        local.set $hash
        local.get $bucket_addr
        i64.load offset=8
        local.set $key
        local.get $bucket_addr
        i64.load offset=16
        local.set $value

        ;; kind doesn't matter for raw insert (it's only used for key
        ;; equality which fires on collisions; rehash → new slot).
        ;; We pass kind=0; collisions still resolve correctly because
        ;; identical bytes hash identically and the early hash check
        ;; weeds out non-matches before key_eq.
        local.get $new
        local.get $hash
        local.get $key
        i32.const 0
        local.get $value
        call $rt_map_insert_raw
        drop

        local.get $count
        i32.const 1
        i32.add
        local.set $count
      end

      local.get $i
      i32.const 1
      i32.add
      local.set $i
      br 0
    end
  end

  ;; Write the count back into header.
  local.get $new
  local.get $count
  local.get $vflag
  call $rt_map_write_header

  local.get $new
)

;; Clone a map: fresh allocation of identical capacity, copy every
;; bucket including tombstones (so probe sequences are preserved).
(func $rt_map_clone (param $old i32) (result i32)
  (local $cap i32)
  (local $vflag i32)
  (local $kkind i32)
  (local $new i32)
  (local $size i32)

  local.get $old
  call $rt_map_capacity
  local.set $cap

  local.get $old
  call $rt_map_value_ptr_flag
  local.set $vflag

  local.get $old
  call $rt_map_key_kind
  local.set $kkind

  local.get $cap
  local.get $kkind
  local.get $vflag
  call $rt_map_alloc
  local.set $new

  ;; Copy bucket array verbatim. We only need bytes [16..16+cap*24];
  ;; the header (offset 0..16) was already written by rt_map_alloc.
  local.get $cap
  i32.const 24
  i32.mul
  local.set $size

  local.get $new
  i32.const 16
  i32.add
  local.get $old
  i32.const 16
  i32.add
  local.get $size
  memory.copy

  ;; Restore count from the source map (rt_map_alloc sets count = 0).
  local.get $new
  local.get $old
  call $rt_map_count
  local.get $vflag
  call $rt_map_write_header

  local.get $new
)

;; ---------------------------------------------------------------------
;; Public API
;; ---------------------------------------------------------------------

(func $rt_map_get (param $map i32) (param $key i64) (param $kind i32) (result i32)
  (local $probe i32)
  (local $idx i32)
  (local $bucket_addr i32)
  (local $value i64)
  (local $vflag i32)

  local.get $map
  i32.eqz
  if
    i32.const -1   ;; NONE_SENTINEL
    return
  end

  local.get $map
  local.get $key
  local.get $kind
  call $rt_hash_key
  local.get $key
  local.get $kind
  call $rt_map_probe
  local.set $probe

  local.get $probe
  i32.const 0x80000000
  i32.and
  i32.eqz
  if
    i32.const -1
    return
  end

  local.get $probe
  i32.const 0x7FFFFFFF
  i32.and
  local.set $idx

  local.get $map
  local.get $idx
  call $rt_map_bucket_addr
  local.set $bucket_addr

  local.get $bucket_addr
  i64.load offset=16
  local.set $value

  local.get $map
  call $rt_map_value_ptr_flag
  local.set $vflag

  ;; Wrap into Some(value)
  i32.const 2 ;; WRAP_SOME
  local.get $value
  local.get $vflag
  call $rt_wrap
)
(export "rt_map_get" (func $rt_map_get))

(func $rt_map_has (param $map i32) (param $key i64) (param $kind i32) (result i32)
  (local $probe i32)

  local.get $map
  i32.eqz
  if
    i32.const 0
    return
  end

  local.get $map
  local.get $key
  local.get $kind
  call $rt_hash_key
  local.get $key
  local.get $kind
  call $rt_map_probe
  local.set $probe

  local.get $probe
  i32.const 0x80000000
  i32.and
  i32.const 0
  i32.ne
)
(export "rt_map_has" (func $rt_map_has))

(func $rt_map_len (param $map i32) (result i64)
  local.get $map
  call $rt_map_count
  i64.extend_i32_u
)
(export "rt_map_len" (func $rt_map_len))

;; Internal: prepare the destination map. If the input is empty (0),
;; allocate a fresh table. If load factor would exceed 0.7 after one
;; more insert, allocate the next power-of-two capacity. Else return
;; input unchanged. Returns the destination map for `_owned` to mutate
;; in place; callers using copy-on-write should clone the result first.
(func $rt_map_ensure_capacity_owned
  (param $map i32) (param $key_kind i32) (param $value_ptr_flag i32)
  (result i32)
  (local $cap i32)
  (local $count i32)
  (local $threshold i32)

  local.get $map
  i32.eqz
  if
    global.get $g_map_initial_cap
    local.get $key_kind
    local.get $value_ptr_flag
    call $rt_map_alloc
    return
  end

  local.get $map
  call $rt_map_capacity
  local.set $cap
  local.get $map
  call $rt_map_count
  local.set $count

  ;; threshold = cap * 7 / 10 (load factor 0.7).
  local.get $cap
  i32.const 7
  i32.mul
  i32.const 10
  i32.div_u
  local.set $threshold

  local.get $count
  i32.const 1
  i32.add
  local.get $threshold
  i32.gt_u
  if
    local.get $map
    local.get $cap
    i32.const 1
    i32.shl
    call $rt_map_resize_to
    return
  end

  local.get $map
)

(func $rt_map_set
  (param $map i32) (param $key i64) (param $kind i32)
  (param $value i64) (param $value_ptr_flag i32)
  (result i32)
  (local $dst i32)
  (local $hash i32)
  (local $delta i32)
  (local $count i32)

  ;; Copy-on-write: clone first, then mutate the clone (unless empty).
  local.get $map
  i32.eqz
  if
    global.get $g_map_initial_cap
    local.get $kind
    local.get $value_ptr_flag
    call $rt_map_alloc
    local.set $dst
  else
    local.get $map
    call $rt_map_clone
    local.set $dst
  end

  ;; Then ensure there's room (clone preserved load factor, but if the
  ;; original was at threshold, we need to grow).
  local.get $dst
  local.get $kind
  local.get $value_ptr_flag
  call $rt_map_ensure_capacity_owned
  local.set $dst

  local.get $key
  local.get $kind
  call $rt_hash_key
  local.set $hash

  local.get $dst
  local.get $hash
  local.get $key
  local.get $kind
  local.get $value
  call $rt_map_insert_raw
  local.set $delta

  local.get $dst
  call $rt_map_count
  local.get $delta
  i32.add
  local.set $count

  local.get $dst
  local.get $count
  local.get $value_ptr_flag
  call $rt_map_write_header

  local.get $dst
)
(export "rt_map_set" (func $rt_map_set))

;; Owned-mutate variant. Caller has guaranteed via last-use analysis
;; that no other observer holds `$map`, so we mutate it in place
;; instead of cloning. Resizing still allocates a fresh table because
;; capacity is fixed at allocation time.
(func $rt_map_set_owned
  (param $map i32) (param $key i64) (param $kind i32)
  (param $value i64) (param $value_ptr_flag i32)
  (result i32)
  (local $dst i32)
  (local $hash i32)
  (local $delta i32)
  (local $count i32)

  local.get $map
  local.get $kind
  local.get $value_ptr_flag
  call $rt_map_ensure_capacity_owned
  local.set $dst

  local.get $key
  local.get $kind
  call $rt_hash_key
  local.set $hash

  local.get $dst
  local.get $hash
  local.get $key
  local.get $kind
  local.get $value
  call $rt_map_insert_raw
  local.set $delta

  local.get $dst
  call $rt_map_count
  local.get $delta
  i32.add
  local.set $count

  local.get $dst
  local.get $count
  local.get $value_ptr_flag
  call $rt_map_write_header

  local.get $dst
)
(export "rt_map_set_owned" (func $rt_map_set_owned))

;; Walk every occupied bucket, prepend its key onto the result list.
;; Order is unspecified (probe order, not insertion order).
(func $rt_map_keys (param $map i32) (result i32)
  (local $cap i32)
  (local $i i32)
  (local $bucket_addr i32)
  (local $state i32)
  (local $key i64)
  (local $acc i32)

  i32.const 0
  local.set $acc

  local.get $map
  i32.eqz
  if
    local.get $acc
    return
  end

  local.get $map
  call $rt_map_capacity
  local.set $cap

  i32.const 0
  local.set $i
  block
    loop
      local.get $i
      local.get $cap
      i32.ge_u
      br_if 1

      local.get $map
      local.get $i
      call $rt_map_bucket_addr
      local.set $bucket_addr

      local.get $bucket_addr
      i32.load
      local.set $state

      local.get $state
      i32.const 1
      i32.eq
      if
        local.get $bucket_addr
        i64.load offset=8
        local.set $key
        ;; Keys may be heap pointers or scalars — use kind=4 ptr-flag
        ;; convention from header? Actually we don't track per-map key
        ;; ptr flag; pass 1 conservatively for pointer keys (kind=3,4)
        ;; and 0 for scalars. We don't know kind from the map itself,
        ;; so we use the most permissive flag. The list_cons head_ptr
        ;; flag affects only GC, and string/heap keys must be tracked.
        ;; Default to ptr-flag = 1; for scalar Maps it's a harmless
        ;; over-tag (GC will treat the i64 cells as ptrs and try to
        ;; rebase, but rt_rebase_i32 returns the input unchanged for
        ;; values outside the heap range).
        local.get $key
        local.get $acc
        i32.const 1
        call $rt_list_cons
        local.set $acc
      end

      local.get $i
      i32.const 1
      i32.add
      local.set $i
      br 0
    end
  end

  local.get $acc
)
(export "rt_map_keys" (func $rt_map_keys))

;; Walk every occupied bucket, build OBJ_TUPLE (key, value), prepend
;; onto the list.
(func $rt_map_entries (param $map i32) (result i32)
  (local $cap i32)
  (local $i i32)
  (local $bucket_addr i32)
  (local $state i32)
  (local $key i64)
  (local $value i64)
  (local $vflag i32)
  (local $tuple i32)
  (local $acc i32)

  i32.const 0
  local.set $acc

  local.get $map
  i32.eqz
  if
    local.get $acc
    return
  end

  local.get $map
  call $rt_map_capacity
  local.set $cap

  local.get $map
  call $rt_map_value_ptr_flag
  local.set $vflag

  i32.const 0
  local.set $i
  block
    loop
      local.get $i
      local.get $cap
      i32.ge_u
      br_if 1

      local.get $map
      local.get $i
      call $rt_map_bucket_addr
      local.set $bucket_addr

      local.get $bucket_addr
      i32.load
      local.set $state

      local.get $state
      i32.const 1
      i32.eq
      if
        local.get $bucket_addr
        i64.load offset=8
        local.set $key
        local.get $bucket_addr
        i64.load offset=16
        local.set $value

        ;; Allocate OBJ_TUPLE { key, value }: 24 bytes.
        i32.const 24
        call $rt_alloc
        local.set $tuple

        ;; Header: kind=OBJ_TUPLE(5), meta = bit 0 (key_ptr) | bit 1 (val_ptr)
        ;; field_count = 2.
        local.get $tuple
        i64.const 0x0500000000000000
        i64.const 1
        i64.const 32
        i64.shl
        i64.or
        local.get $vflag
        i64.extend_i32_u
        i64.const 33
        i64.shl
        i64.or
        i64.const 2
        i64.or
        i64.store

        local.get $tuple
        local.get $key
        i64.store offset=8

        local.get $tuple
        local.get $value
        i64.store offset=16

        local.get $tuple
        i64.extend_i32_u
        local.get $acc
        i32.const 1
        call $rt_list_cons
        local.set $acc
      end

      local.get $i
      i32.const 1
      i32.add
      local.set $i
      br 0
    end
  end

  local.get $acc
)
(export "rt_map_entries" (func $rt_map_entries))

;; Build a Map<K, V> by folding a List<(K, V)> through rt_map_set_owned.
;; All keys share the same `$key_kind`; values share `$value_ptr_flag`.
(func $rt_map_from_list
    (param $list i32) (param $key_kind i32) (param $value_ptr_flag i32)
    (result i32)
  (local $cur i32)
  (local $tuple i32)
  (local $key i64)
  (local $value i64)
  (local $map i32)

  i32.const 0
  local.set $map

  local.get $list
  local.set $cur

  block
    loop
      local.get $cur
      i32.eqz
      br_if 1

      ;; head is i64 stored in cell at offset 8 — for OBJ_LIST_CONS
      ;; with head_ptr_flag set we treat the i64 as a 32-bit pointer.
      local.get $cur
      i64.load offset=8
      i32.wrap_i64
      local.set $tuple

      local.get $tuple
      i64.load offset=8
      local.set $key
      local.get $tuple
      i64.load offset=16
      local.set $value

      local.get $map
      local.get $key
      local.get $key_kind
      local.get $value
      local.get $value_ptr_flag
      call $rt_map_set_owned
      local.set $map

      local.get $cur
      i64.load offset=16
      i32.wrap_i64
      local.set $cur
      br 0
    end
  end

  local.get $map
)
(export "rt_map_from_list" (func $rt_map_from_list))