File: Translator.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (2298 lines) | stat: -rw-r--r-- 100,742 bytes parent folder | download
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
import WasmParser
import WasmTypes

class ISeqAllocator {

    private var buffers: [UnsafeMutableRawBufferPointer] = []

    func allocateBrTable(capacity: Int) -> UnsafeMutableBufferPointer<Instruction.BrTableOperand.Entry> {
        assert(_isPOD(Instruction.BrTableOperand.Entry.self), "Instruction.BrTableOperand.Entry must be POD")
        let buffer = UnsafeMutableBufferPointer<Instruction.BrTableOperand.Entry>.allocate(capacity: capacity)
        self.buffers.append(UnsafeMutableRawBufferPointer(buffer))
        return buffer
    }

    func allocateConstants(_ slots: [UntypedValue]) -> UnsafeBufferPointer<UntypedValue> {
        let buffer = UnsafeMutableBufferPointer<UntypedValue>.allocate(capacity: slots.count)
        _ = buffer.initialize(fromContentsOf: slots)
        self.buffers.append(UnsafeMutableRawBufferPointer(buffer))
        return UnsafeBufferPointer(buffer)
    }

    func allocateInstructions(capacity: Int) -> UnsafeMutableBufferPointer<UInt64> {
        assert(_isPOD(Instruction.self), "Instruction must be POD")
        let buffer = UnsafeMutableBufferPointer<UInt64>.allocate(capacity: capacity)
        self.buffers.append(UnsafeMutableRawBufferPointer(buffer))
        return buffer
    }

    deinit {
        for buffer in buffers {
            buffer.deallocate()
        }
    }
}

protocol TranslatorContext {
    func resolveType(_ index: TypeIndex) throws -> FunctionType
    func resolveBlockType(_ blockType: BlockType) throws -> FunctionType
    func functionType(_ index: FunctionIndex, interner: Interner<FunctionType>) throws -> FunctionType
    func globalType(_ index: GlobalIndex) throws -> ValueType
    func isMemory64(memoryIndex index: MemoryIndex) throws -> Bool
    func isMemory64(tableIndex index: TableIndex) throws -> Bool
    func tableType(_ index: TableIndex) throws -> TableType
    func elementType(_ index: ElementIndex) throws -> ReferenceType
    func resolveCallee(_ index: FunctionIndex) -> InternalFunction?
    func isSameInstance(_ instance: InternalInstance) -> Bool
    func resolveGlobal(_ index: GlobalIndex) -> InternalGlobal?
    func validateFunctionIndex(_ index: FunctionIndex) throws
    var dataCount: UInt32? { get }
}

extension TranslatorContext {
    func addressType(memoryIndex: MemoryIndex) throws -> ValueType {
        return ValueType.addressType(isMemory64: try isMemory64(memoryIndex: memoryIndex))
    }
    func addressType(tableIndex: TableIndex) throws -> ValueType {
        return ValueType.addressType(isMemory64: try isMemory64(tableIndex: tableIndex))
    }
    func validateElementSegment(_ index: ElementIndex) throws {
        _ = try elementType(index)
    }
}

extension InternalInstance: TranslatorContext {
    func resolveType(_ index: TypeIndex) throws -> FunctionType {
        guard Int(index) < self.types.count else {
            throw ValidationError(.indexOutOfBounds("type", index, max: UInt32(self.types.count)))
        }
        return self.types[Int(index)]
    }
    func resolveBlockType(_ blockType: BlockType) throws -> FunctionType {
        try FunctionType(blockType: blockType, typeSection: self.types)
    }
    func functionType(_ index: FunctionIndex, interner: Interner<FunctionType>) throws -> FunctionType {
        return try interner.resolve(self.functions[validating: Int(index)].type)
    }
    func globalType(_ index: GlobalIndex) throws -> ValueType {
        return try self.globals[validating: Int(index)].globalType.valueType
    }
    func isMemory64(memoryIndex index: MemoryIndex) throws -> Bool {
        return try self.memories[validating: Int(index)].limit.isMemory64
    }
    func isMemory64(tableIndex index: TableIndex) throws -> Bool {
        return try self.tables[validating: Int(index)].limits.isMemory64
    }
    func tableType(_ index: TableIndex) throws -> TableType {
        return try self.tables[validating: Int(index)].tableType
    }
    func elementType(_ index: ElementIndex) throws -> ReferenceType {
        try self.elementSegments[validating: Int(index)].type
    }

    func resolveCallee(_ index: FunctionIndex) -> InternalFunction? {
        return self.functions[Int(index)]
    }
    func resolveGlobal(_ index: GlobalIndex) -> InternalGlobal? {
        return self.globals[Int(index)]
    }
    func isSameInstance(_ instance: InternalInstance) -> Bool {
        return instance == self
    }
    func validateFunctionIndex(_ index: FunctionIndex) throws {
        let function = try self.functions[validating: Int(index)]
        guard self.functionRefs.contains(function) else {
            throw ValidationError(.functionIndexNotDeclared(index: index))
        }
    }
    var dataCount: UInt32? {
        self.withValue { $0.dataCount }
    }
}

private struct MetaProgramCounter {
    let offsetFromHead: Int
}

/// The layout of the function stack frame.
///
/// A function call frame starts with a "frame header" which contains
/// the function parameters and the result values. The size of the frame
/// header is determined by the maximum number of parameters and results
/// of the function type. While executing the function, the frame header
/// is used as a storage for parameters. On function return, the frame
/// header is used as a storage for the result values.
///
/// On function entry, the stack frame looks like:
///
/// ```
/// | Offset                             | Description          |
/// |------------------------------------|----------------------|
/// | 0                                  | Function parameter 0 |
/// | 1                                  | Function parameter 1 |
/// | ...                                | ...                  |
/// | len(params)-1                      | Function parameter N |
/// ```
///
/// On function return, the stack frame looks like:
/// ```
/// | Offset                             | Description          |
/// |------------------------------------|----------------------|
/// | 0                                  | Function result 0    |
/// | 1                                  | Function result 1    |
/// | ...                                | ...                  |
/// | len(results)-1                     | Function result N    |
/// ```
///
/// The end of the frame header is usually referred to as "stack pointer"
/// (SP). "local" variables and the value stack space are allocated after
/// the frame header. The value stack space is used to store intermediate
/// values usually corresponding to Wasm's value stack. Unlike the Wasm's
/// value stack, a value slot in the value stack space might be absent if
/// the value is backed by a local variable.
/// The slot index is referred to as "register". The register index is
/// relative to the stack pointer, so the register indices for parameters
/// and results are negative.
///
/// ```
/// | Offset                             | Description          |
/// |------------------------------------|----------------------|
/// | SP-(max(params, results)+3)        | Param/result slots   |------+
/// | ...                                | ...                  |      |
/// | SP-3                               | Saved Instance       |  Frame header
/// | SP-2                               | Saved PC             |      |
/// | SP-1                               | Saved SP             |------+
/// | SP+0                               | Local variable 0     |
/// | SP+1                               | Local variable 1     |
/// | ...                                | ...                  |
/// | SP+len(locals)-1                   | Local variable N     |
/// | SP+len(locals)                     | Const 0              |
/// | SP+len(locals)+1                   | Const 1              |
/// | ...                                | ...                  |
/// | SP+len(locals)+C                   | Const C              |
/// | SP+len(locals)+C                   | Value stack 0        |
/// | SP+len(locals)+C+1                 | Value stack 1        |
/// | ...                                | ...                  |
/// | SP+len(locals)+C+heighest(stack)-1 | Value stack N        |
/// ```
/// where `C` is the number of constant slots.
///
/// ## Example
///
/// Consider the following Wasm function:
///
/// ```wat
/// (func (param i32 i32) (result i32)
///   (local i32)
///   (local i64)
///   (local.set 2 (i32.add (local.get 0) (i32.const 42)))
///   (return (local.get 2))
/// )
/// ```
///
/// Then the stack frame layout looks like:
///
/// ```
/// | Offset                             | Description          |
/// |------------------------------------|----------------------|
/// | -5                                 | Param 0 / Result 0   |------+
/// | -4                                 | Param 1              |      |
/// | -3                                 | Saved Instance       |  Frame header
/// | -2                                 | Saved PC             |      |
/// | -1                                 | Saved SP             |------+
/// | 0                                  | Local 0 (i32)        |
/// | 1                                  | Local 1 (i64)        |
/// | 2                                  | Const 0 (i32:42)     |
/// ```

struct FrameHeaderLayout {
    let type: FunctionType
    let size: VReg

    init(type: FunctionType) {
        self.type = type
        self.size = Self.size(of: type)
    }

    func paramReg(_ index: Int) -> VReg {
        VReg(index) - size
    }

    func returnReg(_ index: Int) -> VReg {
        return VReg(index) - size
    }

    internal static func size(of: FunctionType) -> VReg {
        size(parameters: of.parameters.count, results: of.results.count)
    }
    internal static func size(parameters: Int, results: Int) -> VReg {
        VReg(max(parameters, results)) + VReg(numberOfSavingSlots)
    }
    /// The number of slots used to save the current instance, PC, and SP
    internal static var numberOfSavingSlots: Int { 3 }
}

struct StackLayout {
    let frameHeader: FrameHeaderLayout
    let constantSlotSize: Int
    let numberOfLocals: Int

    var stackRegBase: VReg {
        return VReg(numberOfLocals + constantSlotSize)
    }

    init(type: FunctionType, numberOfLocals: Int, codeSize: Int) throws {
        self.frameHeader = FrameHeaderLayout(type: type)
        self.numberOfLocals = numberOfLocals
        // The number of constant slots is determined by the code size
        // This is a heuristic value to balance the fast access to constants
        // and the size of stack frame. Cap the slot size to avoid size explosion.
        self.constantSlotSize = min(max(codeSize / 20, 4), 128)
        let (maxSlots, overflow) = self.constantSlotSize.addingReportingOverflow(numberOfLocals)
        guard !overflow, maxSlots < VReg.max else {
            throw TranslationError("The number of constant slots overflows")
        }
    }

    func localReg(_ index: LocalIndex) -> VReg {
        if isParameter(index) {
            return frameHeader.paramReg(Int(index))
        } else {
            return VReg(index) - VReg(frameHeader.type.parameters.count)
        }
    }

    func isParameter(_ index: LocalIndex) -> Bool {
        index < frameHeader.type.parameters.count
    }

    func constReg(_ index: Int) -> VReg {
        return VReg(numberOfLocals + index)
    }

    func dump<Target: TextOutputStream>(to target: inout Target, iseq: InstructionSequence) {
        let frameHeaderSize = FrameHeaderLayout.size(of: frameHeader.type)
        let slotMinIndex = VReg(-frameHeaderSize)
        let slotMaxIndex = VReg(stackRegBase - 1)
        let slotIndexWidth = max(String(slotMinIndex).count, String(slotMaxIndex).count)
        func writeSlot(_ target: inout Target, _ index: VReg, _ description: String) {
            var index = String(index)
            index = String(repeating: " ", count: slotIndexWidth - index.count) + index

            target.write(" [\(index)] \(description)\n")
        }
        func hex(_ value: UInt64) -> String {
            let value = String(value, radix: 16)
            return String(repeating: "0", count: 16 - value.count) + value
        }

        let savedItems: [String] = ["Instance", "Pc", "Sp"]
        for i in 0..<frameHeaderSize - VReg(savedItems.count) {
            var descriptions: [String] = []
            if i < frameHeader.type.parameters.count {
                descriptions.append("Param \(i)")
            }
            if i < frameHeader.type.results.count {
                descriptions.append("Result \(i)")
            }
            writeSlot(&target, VReg(i - frameHeaderSize), descriptions.joined(separator: ", "))
        }

        for (i, name) in savedItems.enumerated() {
            writeSlot(&target, VReg(i - savedItems.count), "Saved \(name)")
        }

        for i in 0..<numberOfLocals {
            writeSlot(&target, VReg(i), "Local \(i)")
        }
        for i in 0..<iseq.constants.count {
            writeSlot(&target, VReg(numberOfLocals + i), "Const \(i) = \(iseq.constants[i])")
        }
    }
}

struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
    typealias Output = Void

    typealias LabelRef = Int
    typealias ValueType = WasmTypes.ValueType

    struct ControlStack {
        typealias BlockType = FunctionType

        struct ControlFrame {
            enum Kind {
                case block(root: Bool)
                case loop
                case `if`(elseLabel: LabelRef, endLabel: LabelRef, isElse: Bool)

                static var block: Kind { .block(root: false) }
            }

            let blockType: BlockType
            /// The height of `ValueStack` without including the frame parameters
            let stackHeight: Int
            let continuation: LabelRef
            var kind: Kind
            var reachable: Bool = true

            var copyTypes: [ValueType] {
                switch self.kind {
                case .block, .if:
                    return blockType.results
                case .loop:
                    return blockType.parameters
                }
            }
            var copyCount: UInt16 {
                return UInt16(copyTypes.count)
            }
        }

        private var frames: [ControlFrame] = []

        var numberOfFrames: Int { frames.count }

        mutating func pushFrame(_ frame: ControlFrame) {
            self.frames.append(frame)
        }

        mutating func popFrame() -> ControlFrame? {
            self.frames.popLast()
        }

        mutating func markUnreachable() throws {
            try setReachability(false)
        }
        mutating func resetReachability() throws {
            try setReachability(true)
        }

        private mutating func setReachability(_ value: Bool) throws {
            guard !self.frames.isEmpty else {
                throw ValidationError(.controlStackEmpty)
            }
            self.frames[self.frames.count - 1].reachable = value
        }

        func currentFrame() throws -> ControlFrame {
            guard let frame = self.frames.last else {
                throw ValidationError(.controlStackEmpty)
            }
            return frame
        }

        func branchTarget(relativeDepth: UInt32) throws -> ControlFrame {
            let index = frames.count - 1 - Int(relativeDepth)
            guard frames.indices.contains(index) else {
                throw ValidationError(.relativeDepthOutOfRange(relativeDepth: relativeDepth))
            }
            return frames[index]
        }
    }

    enum MetaValue: Equatable {
        case some(ValueType)
        case unknown
    }

    enum MetaValueOnStack {
        case local(ValueType, LocalIndex)
        case stack(MetaValue)
        case const(ValueType, Int)

        var type: MetaValue {
            switch self {
            case .local(let type, _): return .some(type)
            case .stack(let type): return type
            case .const(let type, _): return .some(type)
            }
        }
    }

    enum ValueSource {
        case vreg(VReg)
        case const(Int, ValueType)
        case local(LocalIndex)
    }

    struct ValueStack {
        private var values: [MetaValueOnStack] = []
        /// The maximum height of the stack within the function
        private(set) var maxHeight: Int = 0
        var height: Int { values.count }
        let stackRegBase: VReg
        let stackLayout: StackLayout

        init(stackLayout: StackLayout) {
            self.stackRegBase = stackLayout.stackRegBase
            self.stackLayout = stackLayout
        }

        mutating func push(_ value: ValueType) -> VReg {
            push(.some(value))
        }
        mutating func push(_ value: MetaValue) -> VReg {
            // Record the maximum height of the stack we have seen
            maxHeight = max(maxHeight, height)
            let usedRegister = self.values.count
            self.values.append(.stack(value))
            assert(height < UInt16.max)
            return stackRegBase + VReg(usedRegister)
        }
        mutating func pushLocal(_ localIndex: LocalIndex, locals: inout Locals) throws {
            let type = try locals.type(of: localIndex)
            self.values.append(.local(type, localIndex))
        }
        mutating func pushConst(_ index: Int, type: ValueType) {
            assert(index < stackLayout.constantSlotSize)
            self.values.append(.const(type, index))
        }
        mutating func preserveLocalsOnStack(_ localIndex: LocalIndex) -> [VReg] {
            var copyTo: [VReg] = []
            for i in 0..<values.count {
                guard case .local(let type, localIndex) = self.values[i] else { continue }
                self.values[i] = .stack(.some(type))
                copyTo.append(stackRegBase + VReg(i))
            }
            return copyTo
        }

        mutating func preserveLocalsOnStack(depth: Int) -> [(source: LocalIndex, to: VReg)] {
            var copies: [(source: LocalIndex, to: VReg)] = []
            for offset in 0..<min(depth, self.values.count) {
                let valueIndex = self.values.count - 1 - offset
                let value = self.values[valueIndex]
                guard case .local(let type, let localIndex) = value else { continue }
                self.values[valueIndex] = .stack(.some(type))
                copies.append((localIndex, self.stackRegBase + VReg(valueIndex)))
            }
            return copies
        }

        mutating func preserveConstsOnStack(depth: Int) -> [(source: VReg, to: VReg)] {
            var copies: [(source: VReg, to: VReg)] = []
            for offset in 0..<min(depth, self.values.count) {
                let valueIndex = self.values.count - 1 - offset
                let value = self.values[valueIndex]
                guard case .const(let type, let index) = value else { continue }
                self.values[valueIndex] = .stack(.some(type))
                copies.append((stackLayout.constReg(index), self.stackRegBase + VReg(valueIndex)))
            }
            return copies
        }

        func peek(depth: Int) -> ValueSource {
            return makeValueSource(self.values[height - 1 - depth])
        }

        func peekType(depth: Int) -> MetaValue {
            return self.values[height - 1 - depth].type
        }

        private func makeValueSource(_ value: MetaValueOnStack) -> ValueSource {
            let source: ValueSource
            switch value {
            case .local(_, let localIndex):
                source = .local(localIndex)
            case .stack:
                source = .vreg(stackRegBase + VReg(height))
            case .const(let type, let index):
                source = .const(index, type)
            }
            return source
        }

        mutating func pop() throws -> (MetaValue, ValueSource) {
            guard let value = self.values.popLast() else {
                throw TranslationError("Expected a value on stack but it's empty")
            }
            let source = makeValueSource(value)
            return (value.type, source)
        }
        mutating func pop(_ expected: ValueType) throws -> ValueSource {
            let (value, register) = try pop()
            switch value {
            case .some(let actual):
                guard actual == expected else {
                    throw TranslationError("Expected \(expected) on the stack top but got \(actual)")
                }
            case .unknown: break  // OK
            }
            return register
        }
        mutating func popRef() throws -> ValueSource {
            let (value, register) = try pop()
            switch value {
            case .some(let actual):
                guard case .ref = actual else {
                    throw TranslationError("Expected reference value on the stack top but got \(actual)")
                }
            case .unknown: break  // OK
            }
            return register
        }
        mutating func truncate(height: Int) throws {
            guard height <= self.height else {
                throw TranslationError("Truncating to \(height) but the stack height is \(self.height)")
            }
            while height != self.height {
                guard self.values.popLast() != nil else {
                    throw TranslationError("Internal consistency error: Stack height is \(self.height) but failed to pop")
                }
            }
        }
    }

    fileprivate struct ISeqBuilder {
        typealias InstructionFactoryWithLabel = (
            ISeqBuilder,
            // The position of the next slot of the creating instruction
            _ source: MetaProgramCounter,
            // The position of the resolved label
            _ target: MetaProgramCounter
        ) -> (WasmKit.Instruction)
        typealias BrTableEntryFactory = (ISeqBuilder, MetaProgramCounter) -> Instruction.BrTableOperand.Entry
        typealias BuildingBrTable = UnsafeMutableBufferPointer<Instruction.BrTableOperand.Entry>

        enum OnPinAction {
            case emitInstruction(
                insertAt: MetaProgramCounter,
                source: MetaProgramCounter,
                InstructionFactoryWithLabel
            )
            case fillBrTableEntry(
                buildingTable: BuildingBrTable,
                index: Int, make: BrTableEntryFactory
            )
        }
        struct LabelUser: CustomStringConvertible {
            let action: OnPinAction
            let sourceLine: UInt

            var description: String {
                "LabelUser:\(sourceLine)"
            }
        }
        enum LabelEntry {
            case unpinned(users: [LabelUser])
            case pinned(MetaProgramCounter)
        }

        typealias ResultRelink = (_ result: VReg) -> Instruction
        fileprivate struct LastEmission {
            let position: MetaProgramCounter
            let resultRelink: ResultRelink?
        }

        private var labels: [LabelEntry] = []
        private var unpinnedLabels: Set<LabelRef> = []
        private var instructions: [UInt64] = []
        private var lastEmission: LastEmission?
        fileprivate var insertingPC: MetaProgramCounter {
            MetaProgramCounter(offsetFromHead: instructions.count)
        }
        let engineConfiguration: EngineConfiguration

        init(engineConfiguration: EngineConfiguration) {
            self.engineConfiguration = engineConfiguration
        }

        func assertDanglingLabels() throws {
            for ref in unpinnedLabels {
                let label = labels[ref]
                switch label {
                case .unpinned(let users):
                    guard !users.isEmpty else { continue }
                    throw TranslationError("Internal consistency error: Label (#\(ref)) is used but not pinned at finalization-time: \(users)")
                case .pinned: break  // unreachable in theory
                }
            }
        }

        func trace(_ message: @autoclosure () -> String) {
            #if WASMKIT_TRANSLATOR_TRACE
                print(message())
            #endif
        }

        private mutating func assign(at index: Int, _ instruction: Instruction) {
            trace("assign: \(instruction)")
            let headSlot = instruction.headSlot(threadingModel: engineConfiguration.threadingModel)
            trace("        [\(index)] = 0x\(String(headSlot, radix: 16))")
            self.instructions[index] = headSlot
            if let immediate = instruction.rawImmediate {
                var slots: [CodeSlot] = []
                immediate.emit(to: { slots.append($0) })
                for (i, slot) in slots.enumerated() {
                    let slotIndex = index + 1 + i
                    trace("        [\(slotIndex)] = 0x\(String(slot, radix: 16))")
                    self.instructions[slotIndex] = slot
                }
            }
        }

        mutating func resetLastEmission() {
            lastEmission = nil
        }

        mutating func relinkLastInstructionResult(_ newResult: VReg) -> Bool {
            guard let lastEmission = self.lastEmission,
                let resultRelink = lastEmission.resultRelink
            else { return false }
            let newInstruction = resultRelink(newResult)
            assign(at: lastEmission.position.offsetFromHead, newInstruction)
            resetLastEmission()
            return true
        }

        private mutating func emitSlot(_ codeSlot: CodeSlot) {
            trace("emitSlot[\(instructions.count)]: 0x\(String(codeSlot, radix: 16))")
            self.instructions.append(codeSlot)
        }

        func dump() {
            for instruction in instructions {
                print(instruction)
            }
        }

        func finalize() -> [UInt64] {
            return instructions
        }

        mutating func emit(_ instruction: Instruction, resultRelink: ResultRelink? = nil) {
            self.lastEmission = LastEmission(position: insertingPC, resultRelink: resultRelink)
            trace("emitInstruction: \(instruction)")
            emitSlot(instruction.headSlot(threadingModel: engineConfiguration.threadingModel))
            if let immediate = instruction.rawImmediate {
                var slots: [CodeSlot] = []
                immediate.emit(to: { slots.append($0) })
                for slot in slots { emitSlot(slot) }
            }
        }

        mutating func putLabel() -> LabelRef {
            let ref = labels.count
            self.labels.append(.pinned(insertingPC))
            return ref
        }

        mutating func allocLabel() -> LabelRef {
            let ref = labels.count
            self.labels.append(.unpinned(users: []))
            self.unpinnedLabels.insert(ref)
            return ref
        }

        fileprivate func resolveLabel(_ ref: LabelRef) -> MetaProgramCounter? {
            let entry = self.labels[ref]
            switch entry {
            case .pinned(let pc): return pc
            case .unpinned: return nil
            }
        }

        fileprivate mutating func pinLabel(_ ref: LabelRef, pc: MetaProgramCounter) throws {
            switch self.labels[ref] {
            case .pinned(let oldPC):
                throw TranslationError("Internal consistency error: Label \(ref) is already pinned at \(oldPC), but tried to pin at \(pc) again")
            case .unpinned(let users):
                self.labels[ref] = .pinned(pc)
                self.unpinnedLabels.remove(ref)
                for user in users {
                    switch user.action {
                    case let .emitInstruction(insertAt, source, make):
                        assign(at: insertAt.offsetFromHead, make(self, source, pc))
                    case let .fillBrTableEntry(brTable, index, make):
                        brTable[index] = make(self, pc)
                    }
                }
            }
        }

        mutating func pinLabelHere(_ ref: LabelRef) throws {
            try pinLabel(ref, pc: insertingPC)
        }

        /// Emit an instruction at the current insertion point with resolved label position
        /// - Parameters:
        ///   - ref: Label reference to be resolved
        ///   - make: Factory closure to make an inserting instruction
        mutating func emitWithLabel<Immediate: InstructionImmediate>(
            _ makeInstruction: @escaping (Immediate) -> Instruction,
            _ ref: LabelRef,
            line: UInt = #line,
            make: @escaping (
                ISeqBuilder,
                // The position of the next slot of the creating instruction
                _ source: MetaProgramCounter,
                // The position of the resolved label
                _ target: MetaProgramCounter
            ) -> (Immediate)
        ) {
            let insertAt = insertingPC

            // Emit dummy instruction to be replaced later
            emitSlot(0)  // dummy opcode
            var immediateSlots = 0
            Immediate.emit(to: { _ in immediateSlots += 1 })
            for _ in 0..<immediateSlots { emitSlot(0) }

            // Schedule actual emission
            emitWithLabel(
                ref, insertAt: insertAt, line: line,
                make: {
                    makeInstruction(make($0, $1, $2))
                })
        }

        /// Emit an instruction at the specified position with resolved label position
        /// - Parameters:
        ///   - ref: Label reference to be resolved
        ///   - insertAt: Instruction sequence offset to insert at
        ///   - make: Factory closure to make an inserting instruction
        private mutating func emitWithLabel(
            _ ref: LabelRef, insertAt: MetaProgramCounter,
            line: UInt = #line, make: @escaping InstructionFactoryWithLabel
        ) {
            switch self.labels[ref] {
            case .pinned(let pc):
                assign(at: insertAt.offsetFromHead, make(self, insertingPC, pc))
            case .unpinned(var users):
                users.append(LabelUser(action: .emitInstruction(insertAt: insertAt, source: insertingPC, make), sourceLine: line))
                self.labels[ref] = .unpinned(users: users)
            }
        }

        /// Schedule to fill a br_table entry with the resolved label position
        /// - Parameters:
        ///   - ref: Label reference to be resolved
        ///   - table: Building br_table buffer
        ///   - index: Index of the entry to fill
        ///   - make: Factory closure to make an br_table entry
        mutating func fillBrTableEntry(
            _ ref: LabelRef,
            table: BuildingBrTable,
            index: Int, line: UInt = #line,
            make: @escaping BrTableEntryFactory
        ) {
            switch self.labels[ref] {
            case .pinned(let pc):
                table[index] = make(self, pc)
            case .unpinned(var users):
                users.append(LabelUser(action: .fillBrTableEntry(buildingTable: table, index: index, make: make), sourceLine: line))
                self.labels[ref] = .unpinned(users: users)
            }
        }
    }

    struct Locals {
        let types: [ValueType]

        var count: Int { types.count }

        func type(of localIndex: UInt32) throws -> ValueType {
            guard Int(localIndex) < types.count else {
                throw TranslationError("Local index \(localIndex) is out of range")
            }
            return self.types[Int(localIndex)]
        }
    }

    struct ConstSlots {
        private(set) var values: [UntypedValue]
        private var indexByValue: [UntypedValue: Int]
        let stackLayout: StackLayout

        init(stackLayout: StackLayout) {
            self.values = []
            self.indexByValue = [:]
            self.stackLayout = stackLayout
        }

        mutating func allocate(_ value: Value) -> Int? {
            let untyped = UntypedValue(value)
            if let allocated = indexByValue[untyped] {
                // NOTE: Share the same const slot for exactly the same bit pattern
                // values even having different types
                return allocated
            }
            guard values.count < stackLayout.constantSlotSize else { return nil }
            let constSlotIndex = values.count
            values.append(untyped)
            indexByValue[untyped] = constSlotIndex
            return constSlotIndex
        }
    }

    let allocator: ISeqAllocator
    let funcTypeInterner: Interner<FunctionType>
    let module: Context
    private var iseqBuilder: ISeqBuilder
    var controlStack: ControlStack
    var valueStack: ValueStack
    var locals: Locals
    let type: FunctionType
    let stackLayout: StackLayout
    /// The index of the function in the module
    let functionIndex: FunctionIndex
    /// Whether a call to this function should be intercepted
    let intercepting: Bool
    var constantSlots: ConstSlots
    let validator: InstructionValidator<Context>

    init(
        allocator: ISeqAllocator,
        engineConfiguration: EngineConfiguration,
        funcTypeInterner: Interner<FunctionType>,
        module: Context,
        type: FunctionType,
        locals: [WasmTypes.ValueType],
        functionIndex: FunctionIndex,
        codeSize: Int,
        intercepting: Bool
    ) throws {
        self.allocator = allocator
        self.funcTypeInterner = funcTypeInterner
        self.type = type
        self.module = module
        self.iseqBuilder = ISeqBuilder(engineConfiguration: engineConfiguration)
        self.controlStack = ControlStack()
        self.stackLayout = try StackLayout(
            type: type,
            numberOfLocals: locals.count,
            codeSize: codeSize
        )
        self.valueStack = ValueStack(stackLayout: stackLayout)
        self.locals = Locals(types: type.parameters + locals)
        self.functionIndex = functionIndex
        self.intercepting = intercepting
        self.constantSlots = ConstSlots(stackLayout: stackLayout)
        self.validator = InstructionValidator(context: module)

        do {
            let endLabel = self.iseqBuilder.allocLabel()
            let rootFrame = ControlStack.ControlFrame(
                blockType: type,
                stackHeight: 0,
                continuation: endLabel,
                kind: .block(root: true)
            )
            self.controlStack.pushFrame(rootFrame)
        }
    }

    private func returnReg(_ index: Int) -> VReg {
        return stackLayout.frameHeader.returnReg(index)
    }
    private func localReg(_ index: LocalIndex) -> VReg {
        return stackLayout.localReg(index)
    }

    private mutating func emit(_ instruction: Instruction, resultRelink: ISeqBuilder.ResultRelink? = nil) {
        iseqBuilder.emit(instruction, resultRelink: resultRelink)
    }

    @discardableResult
    private mutating func emitCopyStack(from source: VReg, to dest: VReg) -> Bool {
        guard source != dest else { return false }
        emit(.copyStack(Instruction.CopyStackOperand(source: LVReg(source), dest: LVReg(dest))))
        return true
    }

    private mutating func preserveOnStack(depth: Int) {
        preserveLocalsOnStack(depth: depth)
        for (source, dest) in valueStack.preserveConstsOnStack(depth: depth) {
            emitCopyStack(from: source, to: dest)
        }
    }

    private mutating func preserveLocalsOnStack(_ localIndex: LocalIndex) {
        for copyTo in valueStack.preserveLocalsOnStack(localIndex) {
            emitCopyStack(from: localReg(localIndex), to: copyTo)
        }
    }

    /// Emit copy instructions to ensure local variable values on the logical
    /// stack are on the physical stack.
    ///
    /// - Parameter depth: The depth of the logical stack to ensure the values
    ///   are on the physical stack.
    private mutating func preserveLocalsOnStack(depth: Int) {
        for (sourceLocal, destReg) in valueStack.preserveLocalsOnStack(depth: depth) {
            emitCopyStack(from: localReg(sourceLocal), to: destReg)
        }
    }

    /// Perform a precondition check for pop operation on value stack.
    ///
    /// - Parameter typeHint: A type expected to be popped. Only used for diagnostic purpose.
    /// - Returns: `true` if check succeed. `false` if the pop operation is going to be performed in unreachable code path.
    private func checkBeforePop(typeHint: ValueType?, depth: Int = 0, controlFrame: ControlStack.ControlFrame) throws -> Bool {
        if _slowPath(valueStack.height - depth <= controlFrame.stackHeight) {
            if controlFrame.reachable {
                throw ValidationError(.expectedTypeOnStackButEmpty(expected: typeHint))
            }
            // Too many pop on unreachable path is ignored
            return false
        }
        return true
    }
    private func checkBeforePop(typeHint: ValueType?, depth: Int = 0) throws -> Bool {
        let controlFrame = try controlStack.currentFrame()
        return try self.checkBeforePop(typeHint: typeHint, depth: depth, controlFrame: controlFrame)
    }
    private mutating func ensureOnVReg(_ source: ValueSource) -> VReg {
        // TODO: Copy to stack if source is on preg
        // let copyTo = valueStack.stackRegBase + VReg(valueStack.height)
        switch source {
        case .vreg(let register):
            return register
        case .local(let index):
            return stackLayout.localReg(index)
        case .const(let index, _):
            return stackLayout.constReg(index)
        }
    }
    private mutating func ensureOnStack(_ source: ValueSource) -> VReg {
        let copyTo = valueStack.stackRegBase + VReg(valueStack.height)
        switch source {
        case .vreg(let vReg):
            return vReg
        case .local(let localIndex):
            emitCopyStack(from: localReg(localIndex), to: copyTo)
            return copyTo
        case .const(let index, _):
            emitCopyStack(from: stackLayout.constReg(index), to: copyTo)
            return copyTo
        }
    }
    private mutating func popOperand(_ type: ValueType) throws -> ValueSource? {
        guard try checkBeforePop(typeHint: type) else {
            return nil
        }
        iseqBuilder.resetLastEmission()
        return try valueStack.pop(type)
    }

    private mutating func popOnStackOperand(_ type: ValueType) throws -> VReg? {
        guard let op = try popOperand(type) else { return nil }
        return ensureOnStack(op)
    }

    private mutating func popVRegOperand(_ type: ValueType) throws -> VReg? {
        guard let op = try popOperand(type) else { return nil }
        return ensureOnVReg(op)
    }

    private mutating func popAnyOperand() throws -> (MetaValue, ValueSource?) {
        guard try checkBeforePop(typeHint: nil) else {
            return (.unknown, nil)
        }
        iseqBuilder.resetLastEmission()
        return try valueStack.pop()
    }

    @discardableResult
    private mutating func popPushValues(_ valueTypes: [ValueType]) throws -> Int {
        var values: [ValueSource?] = []
        for type in valueTypes.reversed() {
            values.append(try popOperand(type))
        }
        let stackHeight = self.valueStack.height
        for (type, value) in zip(valueTypes, values.reversed()) {
            switch value {
            case .local(let localIndex):
                // Re-push local variables to the stack
                _ = try valueStack.pushLocal(localIndex, locals: &locals)
            case .vreg, nil:
                _ = valueStack.push(type)
            case .const(let index, let type):
                valueStack.pushConst(index, type: type)
            }
        }
        return stackHeight
    }

    private func checkStackTop(_ valueTypes: [ValueType]) throws {
        for (stackDepth, type) in valueTypes.reversed().enumerated() {
            guard try checkBeforePop(typeHint: type, depth: stackDepth) else { return }
            let actual = valueStack.peekType(depth: stackDepth)
            switch actual {
            case .some(let actualType):
                guard actualType == type else {
                    throw ValidationError(.expectedTypeOnStack(expected: type, actual: actualType))
                }
            case .unknown: break
            }
        }
    }

    private mutating func visitReturnLike() throws {
        try copyValuesIntoResultSlots(self.type.results, frameHeader: stackLayout.frameHeader)
    }

    /// Pop values from the stack and copy them to the return slots.
    ///
    /// - Parameter valueTypes: The types of the values to copy.
    private mutating func copyValuesIntoResultSlots(_ valueTypes: [ValueType], frameHeader: FrameHeaderLayout) throws {
        var copies: [(source: VReg, dest: VReg)] = []
        for (index, resultType) in valueTypes.enumerated().reversed() {
            guard let operand = try popOperand(resultType) else { continue }
            var source = ensureOnVReg(operand)
            if case .local(let localIndex) = operand, stackLayout.isParameter(localIndex) {
                // Parameter space is shared with return values, so we need to copy it to the stack
                // before copying to the return slot to avoid overwriting the parameter value.
                let copyTo = valueStack.stackRegBase + VReg(valueStack.height)
                emitCopyStack(from: localReg(localIndex), to: copyTo)
                source = copyTo
            }
            let dest = frameHeader.returnReg(index)
            copies.append((source, dest))
        }
        for (source, dest) in copies {
            emitCopyStack(from: source, to: dest)
        }
    }

    @discardableResult
    private mutating func copyOnBranch(targetFrame frame: ControlStack.ControlFrame) throws -> Bool {
        preserveOnStack(depth: min(Int(frame.copyCount), valueStack.height - frame.stackHeight))
        let copyCount = VReg(frame.copyCount)
        let sourceBase = valueStack.stackRegBase + VReg(valueStack.height)
        let destBase = valueStack.stackRegBase + VReg(frame.stackHeight)
        var emittedCopy = false
        for i in (0..<copyCount).reversed() {
            let source = sourceBase - 1 - VReg(i)
            let dest: VReg
            if case .block(root: true) = frame.kind {
                dest = returnReg(Int(copyCount - 1 - i))
            } else {
                dest = destBase + copyCount - 1 - VReg(i)
            }
            let copied = emitCopyStack(from: source, to: dest)
            emittedCopy = emittedCopy || copied
        }
        return emittedCopy
    }
    private mutating func translateReturn() throws {
        if intercepting {
            // Emit `onExit` instruction before every `return` instruction
            emit(.onExit(functionIndex))
        }
        try visitReturnLike()
        iseqBuilder.emit(._return)
    }
    private mutating func markUnreachable() throws {
        try controlStack.markUnreachable()
        let currentFrame = try controlStack.currentFrame()
        try valueStack.truncate(height: currentFrame.stackHeight)
    }

    private mutating func finalize() throws -> InstructionSequence {
        if controlStack.numberOfFrames > 1 {
            throw ValidationError(.expectedMoreEndInstructions(count: controlStack.numberOfFrames - 1))
        }
        // Check dangling labels
        try iseqBuilder.assertDanglingLabels()

        iseqBuilder.emit(._return)
        let instructions = iseqBuilder.finalize()
        // TODO: Figure out a way to avoid the copy here while keeping the execution performance.
        let buffer = allocator.allocateInstructions(capacity: instructions.count)
        for (idx, instruction) in instructions.enumerated() {
            buffer[idx] = instruction
        }
        let constants = allocator.allocateConstants(self.constantSlots.values)
        return InstructionSequence(
            instructions: buffer,
            maxStackHeight: Int(valueStack.stackRegBase) + valueStack.maxHeight,
            constants: constants
        )
    }

    // MARK: Main entry point

    /// Translate a Wasm expression into a sequence of instructions.
    mutating func translate(
        code: Code,
        instance: InternalInstance
    ) throws -> InstructionSequence {
        if intercepting {
            // Emit `onEnter` instruction at the beginning of the function
            emit(.onEnter(functionIndex))
        }
        var parser = ExpressionParser(code: code)
        var offset = parser.offset
        do {
            while try parser.visit(visitor: &self) {
                offset = parser.offset
            }
        } catch var error as ValidationError {
            error.offset = offset
            throw error
        }
        return try finalize()
    }

    // MARK: - Visitor

    mutating func visitUnreachable() throws -> Output {
        emit(.unreachable)
        try markUnreachable()
    }
    mutating func visitNop() -> Output { emit(.nop) }

    mutating func visitBlock(blockType: WasmParser.BlockType) throws -> Output {
        let blockType = try module.resolveBlockType(blockType)
        let endLabel = iseqBuilder.allocLabel()
        self.preserveLocalsOnStack(depth: self.valueStack.height)
        let stackHeight = try popPushValues(blockType.parameters)
        controlStack.pushFrame(ControlStack.ControlFrame(blockType: blockType, stackHeight: stackHeight, continuation: endLabel, kind: .block))
    }

    mutating func visitLoop(blockType: WasmParser.BlockType) throws -> Output {
        let blockType = try module.resolveBlockType(blockType)
        preserveOnStack(depth: blockType.parameters.count)
        iseqBuilder.resetLastEmission()
        for param in blockType.parameters.reversed() {
            _ = try popOperand(param)
        }
        let headLabel = iseqBuilder.putLabel()
        let stackHeight = self.valueStack.height
        for param in blockType.parameters {
            _ = valueStack.push(param)
        }
        controlStack.pushFrame(ControlStack.ControlFrame(blockType: blockType, stackHeight: stackHeight, continuation: headLabel, kind: .loop))
    }

    mutating func visitIf(blockType: WasmParser.BlockType) throws -> Output {
        // Pop condition value
        let condition = try popVRegOperand(.i32)
        let blockType = try module.resolveBlockType(blockType)
        self.preserveLocalsOnStack(depth: self.valueStack.height)
        preserveOnStack(depth: blockType.parameters.count)
        let endLabel = iseqBuilder.allocLabel()
        let elseLabel = iseqBuilder.allocLabel()
        for param in blockType.parameters.reversed() {
            _ = try popOperand(param)
        }
        let stackHeight = self.valueStack.height
        for param in blockType.parameters {
            _ = valueStack.push(param)
        }
        controlStack.pushFrame(
            ControlStack.ControlFrame(
                blockType: blockType, stackHeight: stackHeight, continuation: endLabel,
                kind: .if(elseLabel: elseLabel, endLabel: endLabel, isElse: false)
            )
        )
        guard let condition = condition else { return }
        iseqBuilder.emitWithLabel(Instruction.brIfNot, endLabel) { iseqBuilder, selfPC, endPC in
            let targetPC: MetaProgramCounter
            if let elsePC = iseqBuilder.resolveLabel(elseLabel) {
                targetPC = elsePC
            } else {
                targetPC = endPC
            }
            let elseOrEnd = UInt32(targetPC.offsetFromHead - selfPC.offsetFromHead)
            return Instruction.BrIfOperand(condition: LVReg(condition), offset: Int32(elseOrEnd))
        }
    }

    mutating func visitElse() throws -> Output {
        var frame = try controlStack.currentFrame()
        guard case let .if(elseLabel, endLabel, _) = frame.kind else {
            throw ValidationError(.expectedIfControlFrame)
        }
        preserveOnStack(depth: valueStack.height - frame.stackHeight)
        try controlStack.resetReachability()
        iseqBuilder.resetLastEmission()
        iseqBuilder.emitWithLabel(Instruction.br, endLabel) { _, selfPC, endPC in
            let offset = endPC.offsetFromHead - selfPC.offsetFromHead
            return Int32(offset)
        }
        for result in frame.blockType.results.reversed() {
            guard try checkBeforePop(typeHint: result, controlFrame: frame) else { continue }
            _ = try valueStack.pop(result)
        }
        guard valueStack.height == frame.stackHeight else {
            throw ValidationError(.valuesRemainingAtEndOfBlock)
        }
        _ = controlStack.popFrame()
        frame.kind = .if(elseLabel: elseLabel, endLabel: endLabel, isElse: true)
        frame.reachable = true
        controlStack.pushFrame(frame)

        // Re-push parameters
        for parameter in frame.blockType.parameters {
            _ = valueStack.push(parameter)
        }
        try iseqBuilder.pinLabelHere(elseLabel)
    }

    mutating func visitEnd() throws -> Output {
        let toBePopped = try controlStack.currentFrame()
        iseqBuilder.resetLastEmission()
        if case .block(root: true) = toBePopped.kind {
            try translateReturn()
            guard valueStack.height == toBePopped.stackHeight else {
                throw ValidationError(.valuesRemainingAtEndOfBlock)
            }
            try iseqBuilder.pinLabelHere(toBePopped.continuation)
            return
        }

        if case .if(_, _, isElse: false) = toBePopped.kind {
            let blockType = toBePopped.blockType
            guard blockType.parameters == blockType.results else {
                throw ValidationError(.parameterResultTypeMismatch(blockType: blockType))
            }
        }

        preserveOnStack(depth: Int(valueStack.height - toBePopped.stackHeight))
        switch toBePopped.kind {
        case .block:
            try iseqBuilder.pinLabelHere(toBePopped.continuation)
        case .loop: break
        case .if:
            try iseqBuilder.pinLabelHere(toBePopped.continuation)
        }
        for result in toBePopped.blockType.results.reversed() {
            guard try checkBeforePop(typeHint: result, controlFrame: toBePopped) else { continue }
            _ = try valueStack.pop(result)
        }
        guard valueStack.height == toBePopped.stackHeight else {
            throw ValidationError(.valuesRemainingAtEndOfBlock)
        }
        for result in toBePopped.blockType.results {
            _ = valueStack.push(result)
        }
        _ = controlStack.popFrame()
    }

    private static func computePopCount(
        destination: ControlStack.ControlFrame,
        currentFrame: ControlStack.ControlFrame,
        currentHeight: Int
    ) throws -> UInt32 {
        let popCount: UInt32
        if _fastPath(currentFrame.reachable) {
            let count = currentHeight - Int(destination.copyCount) - destination.stackHeight
            guard count >= 0 else {
                throw ValidationError(.stackHeightUnderflow(available: currentHeight, required: destination.stackHeight + Int(destination.copyCount)))
            }
            popCount = UInt32(count)
        } else {
            // Slow path: This path is taken when "br" is placed after "unreachable"
            // It's ok to put the fake popCount because it will not be executed at runtime.
            popCount = 0
        }
        return popCount
    }

    private mutating func emitBranch<Immediate: InstructionImmediate>(
        _ makeInstruction: @escaping (Immediate) -> Instruction,
        relativeDepth: UInt32,
        make: @escaping (_ offset: Int32, _ copyCount: UInt32, _ popCount: UInt32) -> Immediate
    ) throws {
        let frame = try controlStack.branchTarget(relativeDepth: relativeDepth)
        let copyCount = frame.copyCount
        let popCount = try Self.computePopCount(
            destination: frame,
            currentFrame: try controlStack.currentFrame(),
            currentHeight: valueStack.height
        )
        iseqBuilder.emitWithLabel(makeInstruction, frame.continuation) { _, selfPC, continuation in
            let relativeOffset = continuation.offsetFromHead - selfPC.offsetFromHead
            return make(Int32(relativeOffset), UInt32(copyCount), popCount)
        }
    }
    mutating func visitBr(relativeDepth: UInt32) throws -> Output {
        let frame = try controlStack.branchTarget(relativeDepth: relativeDepth)

        // Copy from the stack top to the bottom to avoid overwrites
        //              [BLOCK1]
        //              [      ]
        //              [      ]
        //              [BLOCK2] () -> (i32, i64)
        // copy [1] +-->[  i32 ]
        //          +---[  i32 ]<--+ copy [2]
        //              [  i64 ]---+
        try copyOnBranch(targetFrame: frame)
        try emitBranch(Instruction.br, relativeDepth: relativeDepth) { offset, copyCount, popCount in
            return offset
        }
        for type in frame.copyTypes.reversed() {
            _ = try popOperand(type)
        }
        try markUnreachable()
    }

    mutating func visitBrIf(relativeDepth: UInt32) throws -> Output {
        let frame = try controlStack.branchTarget(relativeDepth: relativeDepth)
        let condition = try popVRegOperand(.i32)

        if frame.copyCount == 0 {
            guard let condition else { return }
            // Optimization where we don't need copying values when the branch taken
            iseqBuilder.emitWithLabel(Instruction.brIf, frame.continuation) { _, selfPC, continuation in
                let relativeOffset = continuation.offsetFromHead - selfPC.offsetFromHead
                return Instruction.BrIfOperand(
                    condition: LVReg(condition), offset: Int32(relativeOffset)
                )
            }
            return
        }
        preserveOnStack(depth: valueStack.height - frame.stackHeight)

        if let condition {
            // If branch taken, fallthrough to landing pad, copy stack values
            // then branch to the actual place
            // If branch not taken, branch to the next of the landing pad
            //
            // (block (result i32)
            //   (i32.const 42)
            //   (i32.const 24)
            //   (local.get 0)
            //   (br_if 0) ------+
            //   (local.get 1)   |
            // )         <-------+
            //
            // [0x00] (i32.const 42 reg:0)
            // [0x01] (i32.const 24 reg:1)
            // [0x02] (local.get 0 result=reg:2)
            // [0x03] (br_if_z offset=+0x3 cond=reg:2) --+
            // [0x04] (stack.copy reg:1 -> reg:0)        |
            // [0x05] (br offset=+0x2) --------+         |
            // [0x06] (local.get 1 reg:2) <----|---------+
            // [0x07] ...              <-------+
            let onBranchNotTaken = iseqBuilder.allocLabel()
            iseqBuilder.emitWithLabel(Instruction.brIfNot, onBranchNotTaken) { _, conditionCheckAt, continuation in
                let relativeOffset = continuation.offsetFromHead - conditionCheckAt.offsetFromHead
                return Instruction.BrIfOperand(condition: LVReg(condition), offset: Int32(relativeOffset))
            }
            try copyOnBranch(targetFrame: frame)
            try emitBranch(Instruction.br, relativeDepth: relativeDepth) { offset, copyCount, popCount in
                return offset
            }
            try iseqBuilder.pinLabelHere(onBranchNotTaken)
        }
        try popPushValues(frame.copyTypes)
    }

    mutating func visitBrTable(targets: WasmParser.BrTable) throws -> Output {
        guard let index = try popVRegOperand(.i32) else { return }

        let defaultFrame = try controlStack.branchTarget(relativeDepth: targets.defaultIndex)

        // If this instruction is unreachable, copyCount might be greater than the actual stack height
        try preserveOnStack(
            depth: min(
                Int(defaultFrame.copyCount),
                valueStack.height - controlStack.currentFrame().stackHeight
            )
        )
        let allLabelIndices = targets.labelIndices + [targets.defaultIndex]
        let tableBuffer = allocator.allocateBrTable(capacity: allLabelIndices.count)
        let operand = Instruction.BrTableOperand(
            baseAddress: tableBuffer.baseAddress!,
            count: UInt16(tableBuffer.count), index: index
        )
        iseqBuilder.emit(.brTable(operand))
        let brTableAt = iseqBuilder.insertingPC

        //
        // (block $l1 (result i32)
        //   (i32.const 63)
        //   (block $l2 (result i32)
        //     (i32.const 42)
        //     (i32.const 24)
        //     (local.get 0)
        //     (br_table $l1 $l2) ---+
        //                           |
        //   )               <-------+
        //   (i32.const 36)          |
        // )              <----------+
        //
        //
        //           [0x00] (i32.const 63 reg:0)
        //           [0x01] (i32.const 42 reg:1)
        //           [0x02] (i32.const 24 reg:2)
        //           [0x03] (local.get 0 result=reg:3)
        //           [0x04] (br_table index=reg:3 offsets=[
        //                    +0x01       -----------------+
        //                    +0x03       -----------------|----+
        //                  ])                             |    |
        //           [0x05] (stack.copy reg:2 -> reg:0) <--+    |
        //  +------- [0x06] (br offset=+0x03)                   |
        //  |        [0x07] (stack.copy reg:2 -> reg:1)  <------+
        //  |  +---- [0x08] (br offset=+0x03)
        //  +--|---> [0x09] (i32.const 36 reg:2)
        //     |     [0x0a] (stack.copy reg:2 -> reg:0)
        //     +---> [0x0b] ...
        for (entryIndex, labelIndex) in allLabelIndices.enumerated() {
            let frame = try controlStack.branchTarget(relativeDepth: labelIndex)

            // Check copyTypes consistency
            guard frame.copyTypes.count == defaultFrame.copyTypes.count else {
                throw ValidationError(.expectedSameCopyTypes(frameCopyTypes: frame.copyTypes, defaultFrameCopyTypes: defaultFrame.copyTypes))
            }
            try checkStackTop(frame.copyTypes)

            do {
                let relativeOffset = iseqBuilder.insertingPC.offsetFromHead - brTableAt.offsetFromHead
                tableBuffer[entryIndex] = Instruction.BrTableOperand.Entry(
                    offset: Int32(relativeOffset)
                )
            }
            let emittedCopy = try copyOnBranch(targetFrame: frame)
            if emittedCopy {
                iseqBuilder.emitWithLabel(Instruction.br, frame.continuation) { _, brAt, continuation in
                    let relativeOffset = continuation.offsetFromHead - brAt.offsetFromHead
                    return Int32(relativeOffset)
                }
            } else {
                // Optimization: If no value is copied, we can directly jump to the target
                iseqBuilder.fillBrTableEntry(frame.continuation, table: tableBuffer, index: entryIndex) { _, continuation in
                    return Instruction.BrTableOperand.Entry(offset: Int32(continuation.offsetFromHead - brTableAt.offsetFromHead))
                }
            }
        }
        // Pop branch copy values for type checking
        for type in defaultFrame.copyTypes.reversed() {
            _ = try popOperand(type)
        }
        try markUnreachable()
    }

    mutating func visitReturn() throws -> Output {
        try translateReturn()
        try markUnreachable()
    }

    private mutating func visitCallLike(calleeType: FunctionType) throws -> VReg? {
        for parameter in calleeType.parameters.reversed() {
            guard (try popOnStackOperand(parameter)) != nil else { return nil }
        }

        let spAddend =
            valueStack.stackRegBase + VReg(valueStack.height)
            + FrameHeaderLayout.size(of: calleeType)

        for result in calleeType.results {
            _ = valueStack.push(result)
        }
        return VReg(spAddend)
    }
    mutating func visitCall(functionIndex: UInt32) throws -> Output {
        let calleeType = try self.module.functionType(functionIndex, interner: funcTypeInterner)
        guard let spAddend = try visitCallLike(calleeType: calleeType) else { return }
        guard let callee = self.module.resolveCallee(functionIndex) else {
            // Skip actual code emission if validation-only mode
            return
        }
        if callee.isWasm {
            if module.isSameInstance(callee.wasm.instance) {
                emit(.compilingCall(Instruction.CallOperand(callee: callee, spAddend: spAddend)))
                return
            }
        }
        emit(.call(Instruction.CallOperand(callee: callee, spAddend: spAddend)))
    }

    mutating func visitCallIndirect(typeIndex: UInt32, tableIndex: UInt32) throws -> Output {
        let addressType = try module.addressType(tableIndex: tableIndex)
        let address = try popVRegOperand(addressType)  // function address
        let calleeType = try self.module.resolveType(typeIndex)
        guard let spAddend = try visitCallLike(calleeType: calleeType) else { return }
        guard let address = address else { return }
        let internType = funcTypeInterner.intern(calleeType)
        let operand = Instruction.CallIndirectOperand(
            tableIndex: tableIndex,
            type: internType,
            index: address,
            spAddend: spAddend
        )
        emit(.callIndirect(operand))
    }

    /// Emit instructions to prepare the frame header for a return call to replace the
    /// current frame header with the callee's frame header layout.
    ///
    /// The frame header should have the callee's frame header layout and parameter
    /// slots are filled with arguments on the caller's stack.
    ///
    /// - Parameters:
    ///   - calleeType: The type of the callee function.
    ///   - stackTopHeightToCopy: The height of the stack top needed to be available at the
    ///     return-call-like instruction point.
    private mutating func prepareFrameHeaderForReturnCall(calleeType: FunctionType, stackTopHeightToCopy: Int) throws {
        let calleeFrameHeader = FrameHeaderLayout(type: calleeType)
        if calleeType == self.type {
            // Fast path: If the callee and the caller have the same signature, we can
            // skip reconstructing the frame header and we can just copy the parameters.
        } else {
            // Ensure all parameters are on stack to avoid conflicting with the next resize.
            preserveOnStack(depth: calleeType.parameters.count)
            // Resize the current frame header while moving stack slots after the header
            // to the resized positions
            let newHeaderSize = FrameHeaderLayout.size(of: calleeType)
            let delta = newHeaderSize - FrameHeaderLayout.size(of: type)
            let sizeToCopy = VReg(FrameHeaderLayout.numberOfSavingSlots) + valueStack.stackRegBase + VReg(stackTopHeightToCopy)
            emit(.resizeFrameHeader(Instruction.ResizeFrameHeaderOperand(delta: delta, sizeToCopy: sizeToCopy)))
        }
        try copyValuesIntoResultSlots(calleeType.parameters, frameHeader: calleeFrameHeader)
    }

    mutating func visitReturnCall(functionIndex: UInt32) throws {
        let calleeType = try self.module.functionType(functionIndex, interner: funcTypeInterner)
        try validator.validateReturnCallLike(calleeType: calleeType, callerType: type)

        guard let callee = self.module.resolveCallee(functionIndex) else {
            // Skip actual code emission if validation-only mode
            return
        }
        try prepareFrameHeaderForReturnCall(calleeType: calleeType, stackTopHeightToCopy: valueStack.height)
        emit(.returnCall(Instruction.ReturnCallOperand(callee: callee)))
        try markUnreachable()
    }

    mutating func visitReturnCallIndirect(typeIndex: UInt32, tableIndex: UInt32) throws {
        let stackTopHeightToCopy = valueStack.height
        let addressType = try module.addressType(tableIndex: tableIndex)
        // Preserve function index slot on stack
        let address = try popOnStackOperand(addressType)  // function address
        guard let address = address else { return }

        let calleeType = try self.module.resolveType(typeIndex)
        let internType = funcTypeInterner.intern(calleeType)

        try prepareFrameHeaderForReturnCall(
            calleeType: calleeType,
            // Keep the stack space including the function index slot to be
            // accessible at the `return_call_indirect` instruction point.
            stackTopHeightToCopy: stackTopHeightToCopy
        )

        let operand = Instruction.ReturnCallIndirectOperand(
            tableIndex: tableIndex,
            type: internType,
            index: address
        )
        emit(.returnCallIndirect(operand))
        try markUnreachable()
    }

    mutating func visitDrop() throws -> Output {
        _ = try popAnyOperand()
        iseqBuilder.resetLastEmission()
    }
    mutating func visitSelect() throws -> Output {
        let condition = try popVRegOperand(.i32)
        let (value1Type, value1) = try popAnyOperand()
        let (value2Type, value2) = try popAnyOperand()
        switch (value1Type, value2Type) {
        case (.some(.ref(_)), _), (_, .some(.ref(_))):
            throw ValidationError(.cannotSelectOnReferenceTypes)
        case let (.some(type1), .some(type2)):
            guard type1 == type2 else {
                throw ValidationError(.typeMismatchOnSelect(expected: type1, actual: type2))
            }
        case (.unknown, _), (_, .unknown):
            break
        }
        let result = valueStack.push(value1Type)
        if let condition = condition, let value1 = value1, let value2 = value2 {
            let operand = Instruction.SelectOperand(
                result: result,
                condition: condition,
                onTrue: ensureOnVReg(value2),
                onFalse: ensureOnVReg(value1)
            )
            emit(.select(operand))
        }
    }
    mutating func visitTypedSelect(type: WasmTypes.ValueType) throws -> Output {
        let condition = try popVRegOperand(.i32)
        let (value1Type, value1) = try popAnyOperand()
        let (_, value2) = try popAnyOperand()
        // TODO: Perform actual validation
        // guard value1 == ValueType(type) else {
        //     throw TranslationError("Type mismatch on `select`. Expected \(value1) and \(type) to be same")
        // }
        // guard value2 == ValueType(type) else {
        //     throw TranslationError("Type mismatch on `select`. Expected \(value2) and \(type) to be same")
        // }
        let result = valueStack.push(value1Type)
        if let condition = condition, let value1 = value1, let value2 = value2 {
            let operand = Instruction.SelectOperand(
                result: result,
                condition: condition,
                onTrue: ensureOnVReg(value2),
                onFalse: ensureOnVReg(value1)
            )
            emit(.select(operand))
        }
    }
    mutating func visitLocalGet(localIndex: UInt32) throws -> Output {
        iseqBuilder.resetLastEmission()
        try valueStack.pushLocal(localIndex, locals: &locals)
    }
    mutating func visitLocalSetOrTee(localIndex: UInt32, isTee: Bool) throws {
        preserveLocalsOnStack(localIndex)
        let type = try locals.type(of: localIndex)
        let result = localReg(localIndex)

        guard try checkBeforePop(typeHint: type) else { return }
        let op = try valueStack.pop(type)

        if case .const(let slotIndex, _) = op {
            // Optimize (local.set $x (i32.const $c)) to reg:$x = 42 rather than through const slot
            let value = constantSlots.values[slotIndex]
            let is32Bit = type == .i32 || type == .f32
            if is32Bit {
                emit(.const32(Instruction.Const32Operand(value: UInt32(value.storage), result: LVReg(result))))
            } else {
                emit(.const64(Instruction.Const64Operand(value: value, result: LLVReg(result))))
            }
            return
        }

        let value = ensureOnVReg(op)
        guard try controlStack.currentFrame().reachable else { return }
        if !isTee, iseqBuilder.relinkLastInstructionResult(result) {
            // Good news, copyStack is optimized out :)
            return
        }
        emitCopyStack(from: value, to: result)
    }
    mutating func visitLocalSet(localIndex: UInt32) throws -> Output {
        try visitLocalSetOrTee(localIndex: localIndex, isTee: false)
    }
    mutating func visitLocalTee(localIndex: UInt32) throws -> Output {
        try visitLocalSetOrTee(localIndex: localIndex, isTee: true)
        _ = try valueStack.pushLocal(localIndex, locals: &locals)
    }
    mutating func visitGlobalGet(globalIndex: UInt32) throws -> Output {
        let type = try module.globalType(globalIndex)
        let result = valueStack.push(type)
        guard let global = module.resolveGlobal(globalIndex) else {
            // Skip actual code emission if validation-only mode
            return
        }
        emit(.globalGet(Instruction.GlobalAndVRegOperand(reg: LLVReg(result), global: global)))
    }
    mutating func visitGlobalSet(globalIndex: UInt32) throws -> Output {
        let type = try module.globalType(globalIndex)
        guard let value = try popVRegOperand(type) else { return }
        guard let global = module.resolveGlobal(globalIndex) else {
            // Skip actual code emission if validation-only mode
            return
        }
        try validator.validateGlobalSet(global.globalType)
        emit(.globalSet(Instruction.GlobalAndVRegOperand(reg: LLVReg(value), global: global)))
    }

    private mutating func pushEmit(
        _ type: ValueType,
        _ instruction: @escaping (VReg) -> Instruction
    ) {
        let register = valueStack.push(type)
        emit(
            instruction(register),
            resultRelink: { newResult in
                instruction(newResult)
            })
    }
    private mutating func popPushEmit(
        _ pop: ValueType,
        _ push: ValueType,
        _ instruction: @escaping (_ popped: VReg, _ result: VReg, ValueStack) -> Instruction
    ) throws {
        let value = try popVRegOperand(pop)
        let result = valueStack.push(push)
        if let value = value {
            emit(
                instruction(value, result, valueStack),
                resultRelink: { [valueStack] newResult in
                    instruction(value, newResult, valueStack)
                })
        }
    }

    private mutating func pop3Emit(
        _ pops: (ValueType, ValueType, ValueType),
        _ instruction: (
            _ popped: (VReg, VReg, VReg),
            inout ValueStack
        ) -> Instruction
    ) throws {
        guard let pop1 = try popVRegOperand(pops.0),
            let pop2 = try popVRegOperand(pops.1),
            let pop3 = try popVRegOperand(pops.2)
        else { return }
        emit(instruction((pop1, pop2, pop3), &valueStack))
    }

    private mutating func pop2Emit(
        _ pops: (ValueType, ValueType),
        _ instruction: (
            _ popped: (VReg, VReg),
            inout ValueStack
        ) -> Instruction
    ) throws {
        guard let pop1 = try popVRegOperand(pops.0),
            let pop2 = try popVRegOperand(pops.1)
        else { return }
        emit(instruction((pop1, pop2), &valueStack))
    }

    private mutating func pop2PushEmit(
        _ pops: (ValueType, ValueType),
        _ push: ValueType,
        _ instruction: @escaping (
            _ popped: (VReg, VReg),
            _ result: VReg
        ) -> Instruction
    ) throws {
        guard let pop1 = try popVRegOperand(pops.0),
            let pop2 = try popVRegOperand(pops.1)
        else { return }
        let result = valueStack.push(push)
        emit(
            instruction((pop1, pop2), result),
            resultRelink: { result in
                instruction((pop1, pop2), result)
            })
    }

    private mutating func visitLoad(
        _ memarg: MemArg,
        _ type: ValueType,
        _ naturalAlignment: Int,
        _ instruction: @escaping (Instruction.LoadOperand) -> Instruction
    ) throws {
        let isMemory64 = try module.isMemory64(memoryIndex: 0)
        try validator.validateMemArg(memarg, naturalAlignment: naturalAlignment)
        try popPushEmit(.address(isMemory64: isMemory64), type) { value, result, stack in
            let loadOperand = Instruction.LoadOperand(
                offset: memarg.offset,
                pointer: value,
                result: result
            )
            return instruction(loadOperand)
        }
    }
    private mutating func visitStore(
        _ memarg: MemArg,
        _ type: ValueType,
        _ naturalAlignment: Int,
        _ instruction: (Instruction.StoreOperand) -> Instruction
    ) throws {
        let isMemory64 = try module.isMemory64(memoryIndex: 0)
        try validator.validateMemArg(memarg, naturalAlignment: naturalAlignment)
        let value = try popVRegOperand(type)
        let pointer = try popVRegOperand(.address(isMemory64: isMemory64))
        if let value = value, let pointer = pointer {
            let storeOperand = Instruction.StoreOperand(
                offset: memarg.offset,
                pointer: pointer,
                value: value
            )
            emit(instruction(storeOperand))
        }
    }

    mutating func visitLoad(_ load: WasmParser.Instruction.Load, memarg: MemArg) throws {
        let instruction: (Instruction.LoadOperand) -> Instruction
        switch load {
        case .i32Load: instruction = Instruction.i32Load
        case .i64Load: instruction = Instruction.i64Load
        case .f32Load: instruction = Instruction.f32Load
        case .f64Load: instruction = Instruction.f64Load
        case .i32Load8S: instruction = Instruction.i32Load8S
        case .i32Load8U: instruction = Instruction.i32Load8U
        case .i32Load16S: instruction = Instruction.i32Load16S
        case .i32Load16U: instruction = Instruction.i32Load16U
        case .i64Load8S: instruction = Instruction.i64Load8S
        case .i64Load8U: instruction = Instruction.i64Load8U
        case .i64Load16S: instruction = Instruction.i64Load16S
        case .i64Load16U: instruction = Instruction.i64Load16U
        case .i64Load32S: instruction = Instruction.i64Load32S
        case .i64Load32U: instruction = Instruction.i64Load32U
        }
        try visitLoad(memarg, load.type, load.naturalAlignment, instruction)
    }

    mutating func visitStore(_ store: WasmParser.Instruction.Store, memarg: MemArg) throws {
        let instruction: (Instruction.StoreOperand) -> Instruction
        switch store {
        case .i32Store: instruction = Instruction.i32Store
        case .i64Store: instruction = Instruction.i64Store
        case .f32Store: instruction = Instruction.f32Store
        case .f64Store: instruction = Instruction.f64Store
        case .i32Store8: instruction = Instruction.i32Store8
        case .i32Store16: instruction = Instruction.i32Store16
        case .i64Store8: instruction = Instruction.i64Store8
        case .i64Store16: instruction = Instruction.i64Store16
        case .i64Store32: instruction = Instruction.i64Store32
        }
        try visitStore(memarg, store.type, store.naturalAlignment, instruction)
    }
    mutating func visitMemorySize(memory: UInt32) throws -> Output {
        let sizeType: ValueType = try module.isMemory64(memoryIndex: memory) ? .i64 : .i32
        pushEmit(sizeType, { .memorySize(Instruction.MemorySizeOperand(memoryIndex: memory, result: LVReg($0))) })
    }
    mutating func visitMemoryGrow(memory: UInt32) throws -> Output {
        let isMemory64 = try module.isMemory64(memoryIndex: memory)
        let sizeType = ValueType.address(isMemory64: isMemory64)
        // Just pop/push the same type (i64 or i32) value
        try popPushEmit(sizeType, sizeType) { value, result, stack in
            .memoryGrow(
                Instruction.MemoryGrowOperand(
                    result: result, delta: value, memory: memory
                ))
        }
    }

    private mutating func visitConst(_ type: ValueType, _ value: Value) {
        if let constSlotIndex = constantSlots.allocate(value) {
            valueStack.pushConst(constSlotIndex, type: type)
            iseqBuilder.resetLastEmission()
            return
        }
        let value = UntypedValue(value)
        let is32Bit = type == .i32 || type == .f32
        if is32Bit {
            pushEmit(
                type,
                {
                    .const32(Instruction.Const32Operand(value: UInt32(value.storage), result: LVReg($0)))
                })
        } else {
            pushEmit(type, { .const64(Instruction.Const64Operand(value: value, result: LLVReg($0))) })
        }
    }
    mutating func visitI32Const(value: Int32) -> Output { visitConst(.i32, .i32(UInt32(bitPattern: value))) }
    mutating func visitI64Const(value: Int64) -> Output { visitConst(.i64, .i64(UInt64(bitPattern: value))) }
    mutating func visitF32Const(value: IEEE754.Float32) -> Output { visitConst(.f32, .f32(value.bitPattern)) }
    mutating func visitF64Const(value: IEEE754.Float64) -> Output { visitConst(.f64, .f64(value.bitPattern)) }
    mutating func visitRefNull(type: WasmTypes.ReferenceType) -> Output {
        pushEmit(.ref(type), { .refNull(Instruction.RefNullOperand(result: $0, type: type)) })
    }
    mutating func visitRefIsNull() throws -> Output {
        let value = try valueStack.popRef()
        let result = valueStack.push(.i32)
        emit(.refIsNull(Instruction.RefIsNullOperand(value: LVReg(ensureOnVReg(value)), result: LVReg(result))))
    }
    mutating func visitRefFunc(functionIndex: UInt32) throws -> Output {
        try validator.validateRefFunc(functionIndex: functionIndex)
        pushEmit(.ref(.funcRef), { .refFunc(Instruction.RefFuncOperand(index: functionIndex, result: LVReg($0))) })
    }

    private mutating func visitUnary(_ operand: ValueType, _ instruction: @escaping (Instruction.UnaryOperand) -> Instruction) throws {
        try popPushEmit(operand, operand) { value, result, stack in
            return instruction(Instruction.UnaryOperand(result: LVReg(result), input: LVReg(value)))
        }
    }
    private mutating func visitBinary(
        _ operand: ValueType,
        _ result: ValueType,
        _ instruction: @escaping (Instruction.BinaryOperand) -> Instruction
    ) throws {
        let rhs = try popVRegOperand(operand)
        let lhs = try popVRegOperand(operand)
        let result = valueStack.push(result)
        guard let lhs = lhs, let rhs = rhs else { return }
        emit(
            instruction(Instruction.BinaryOperand(result: LVReg(result), lhs: lhs, rhs: rhs)),
            resultRelink: { result in
                return instruction(Instruction.BinaryOperand(result: LVReg(result), lhs: lhs, rhs: rhs))
            }
        )
    }
    private mutating func visitCmp(_ operand: ValueType, _ instruction: @escaping (Instruction.BinaryOperand) -> Instruction) throws {
        try visitBinary(operand, .i32, instruction)
    }
    private mutating func visitConversion(_ from: ValueType, _ to: ValueType, _ instruction: @escaping (Instruction.UnaryOperand) -> Instruction) throws {
        try popPushEmit(from, to) { value, result, stack in
            return instruction(Instruction.UnaryOperand(result: LVReg(result), input: LVReg(value)))
        }
    }
    mutating func visitI32Eqz() throws -> Output {
        try popPushEmit(.i32, .i32) { value, result, stack in
            .i32Eqz(Instruction.UnaryOperand(result: LVReg(result), input: LVReg(value)))
        }
    }
    mutating func visitCmp(_ cmp: WasmParser.Instruction.Cmp) throws {
        let operand: ValueType
        let instruction: (Instruction.BinaryOperand) -> Instruction
        switch cmp {
        case .i32Eq: (operand, instruction) = (.i32, Instruction.i32Eq)
        case .i32Ne: (operand, instruction) = (.i32, Instruction.i32Ne)
        case .i32LtS: (operand, instruction) = (.i32, Instruction.i32LtS)
        case .i32LtU: (operand, instruction) = (.i32, Instruction.i32LtU)
        case .i32GtS: (operand, instruction) = (.i32, Instruction.i32GtS)
        case .i32GtU: (operand, instruction) = (.i32, Instruction.i32GtU)
        case .i32LeS: (operand, instruction) = (.i32, Instruction.i32LeS)
        case .i32LeU: (operand, instruction) = (.i32, Instruction.i32LeU)
        case .i32GeS: (operand, instruction) = (.i32, Instruction.i32GeS)
        case .i32GeU: (operand, instruction) = (.i32, Instruction.i32GeU)
        case .i64Eq: (operand, instruction) = (.i64, Instruction.i64Eq)
        case .i64Ne: (operand, instruction) = (.i64, Instruction.i64Ne)
        case .i64LtS: (operand, instruction) = (.i64, Instruction.i64LtS)
        case .i64LtU: (operand, instruction) = (.i64, Instruction.i64LtU)
        case .i64GtS: (operand, instruction) = (.i64, Instruction.i64GtS)
        case .i64GtU: (operand, instruction) = (.i64, Instruction.i64GtU)
        case .i64LeS: (operand, instruction) = (.i64, Instruction.i64LeS)
        case .i64LeU: (operand, instruction) = (.i64, Instruction.i64LeU)
        case .i64GeS: (operand, instruction) = (.i64, Instruction.i64GeS)
        case .i64GeU: (operand, instruction) = (.i64, Instruction.i64GeU)
        case .f32Eq: (operand, instruction) = (.f32, Instruction.f32Eq)
        case .f32Ne: (operand, instruction) = (.f32, Instruction.f32Ne)
        case .f32Lt: (operand, instruction) = (.f32, Instruction.f32Lt)
        case .f32Gt: (operand, instruction) = (.f32, Instruction.f32Gt)
        case .f32Le: (operand, instruction) = (.f32, Instruction.f32Le)
        case .f32Ge: (operand, instruction) = (.f32, Instruction.f32Ge)
        case .f64Eq: (operand, instruction) = (.f64, Instruction.f64Eq)
        case .f64Ne: (operand, instruction) = (.f64, Instruction.f64Ne)
        case .f64Lt: (operand, instruction) = (.f64, Instruction.f64Lt)
        case .f64Gt: (operand, instruction) = (.f64, Instruction.f64Gt)
        case .f64Le: (operand, instruction) = (.f64, Instruction.f64Le)
        case .f64Ge: (operand, instruction) = (.f64, Instruction.f64Ge)
        }
        try visitCmp(operand, instruction)
    }
    public mutating func visitBinary(_ binary: WasmParser.Instruction.Binary) throws {
        let operand: ValueType
        let result: ValueType
        let instruction: (Instruction.BinaryOperand) -> Instruction
        switch binary {
        case .i32Add: (operand, result, instruction) = (.i32, .i32, Instruction.i32Add)
        case .i32Sub: (operand, result, instruction) = (.i32, .i32, Instruction.i32Sub)
        case .i32Mul: (operand, result, instruction) = (.i32, .i32, Instruction.i32Mul)
        case .i32DivS: (operand, result, instruction) = (.i32, .i32, Instruction.i32DivS)
        case .i32DivU: (operand, result, instruction) = (.i32, .i32, Instruction.i32DivU)
        case .i32RemS: (operand, result, instruction) = (.i32, .i32, Instruction.i32RemS)
        case .i32RemU: (operand, result, instruction) = (.i32, .i32, Instruction.i32RemU)
        case .i32And: (operand, result, instruction) = (.i32, .i32, Instruction.i32And)
        case .i32Or: (operand, result, instruction) = (.i32, .i32, Instruction.i32Or)
        case .i32Xor: (operand, result, instruction) = (.i32, .i32, Instruction.i32Xor)
        case .i32Shl: (operand, result, instruction) = (.i32, .i32, Instruction.i32Shl)
        case .i32ShrS: (operand, result, instruction) = (.i32, .i32, Instruction.i32ShrS)
        case .i32ShrU: (operand, result, instruction) = (.i32, .i32, Instruction.i32ShrU)
        case .i32Rotl: (operand, result, instruction) = (.i32, .i32, Instruction.i32Rotl)
        case .i32Rotr: (operand, result, instruction) = (.i32, .i32, Instruction.i32Rotr)
        case .i64Add: (operand, result, instruction) = (.i64, .i64, Instruction.i64Add)
        case .i64Sub: (operand, result, instruction) = (.i64, .i64, Instruction.i64Sub)
        case .i64Mul: (operand, result, instruction) = (.i64, .i64, Instruction.i64Mul)
        case .i64DivS: (operand, result, instruction) = (.i64, .i64, Instruction.i64DivS)
        case .i64DivU: (operand, result, instruction) = (.i64, .i64, Instruction.i64DivU)
        case .i64RemS: (operand, result, instruction) = (.i64, .i64, Instruction.i64RemS)
        case .i64RemU: (operand, result, instruction) = (.i64, .i64, Instruction.i64RemU)
        case .i64And: (operand, result, instruction) = (.i64, .i64, Instruction.i64And)
        case .i64Or: (operand, result, instruction) = (.i64, .i64, Instruction.i64Or)
        case .i64Xor: (operand, result, instruction) = (.i64, .i64, Instruction.i64Xor)
        case .i64Shl: (operand, result, instruction) = (.i64, .i64, Instruction.i64Shl)
        case .i64ShrS: (operand, result, instruction) = (.i64, .i64, Instruction.i64ShrS)
        case .i64ShrU: (operand, result, instruction) = (.i64, .i64, Instruction.i64ShrU)
        case .i64Rotl: (operand, result, instruction) = (.i64, .i64, Instruction.i64Rotl)
        case .i64Rotr: (operand, result, instruction) = (.i64, .i64, Instruction.i64Rotr)
        case .f32Add: (operand, result, instruction) = (.f32, .f32, Instruction.f32Add)
        case .f32Sub: (operand, result, instruction) = (.f32, .f32, Instruction.f32Sub)
        case .f32Mul: (operand, result, instruction) = (.f32, .f32, Instruction.f32Mul)
        case .f32Div: (operand, result, instruction) = (.f32, .f32, Instruction.f32Div)
        case .f32Min: (operand, result, instruction) = (.f32, .f32, Instruction.f32Min)
        case .f32Max: (operand, result, instruction) = (.f32, .f32, Instruction.f32Max)
        case .f32Copysign: (operand, result, instruction) = (.f32, .f32, Instruction.f32CopySign)
        case .f64Add: (operand, result, instruction) = (.f64, .f64, Instruction.f64Add)
        case .f64Sub: (operand, result, instruction) = (.f64, .f64, Instruction.f64Sub)
        case .f64Mul: (operand, result, instruction) = (.f64, .f64, Instruction.f64Mul)
        case .f64Div: (operand, result, instruction) = (.f64, .f64, Instruction.f64Div)
        case .f64Min: (operand, result, instruction) = (.f64, .f64, Instruction.f64Min)
        case .f64Max: (operand, result, instruction) = (.f64, .f64, Instruction.f64Max)
        case .f64Copysign: (operand, result, instruction) = (.f64, .f64, Instruction.f64CopySign)
        }
        try visitBinary(operand, result, instruction)
    }
    mutating func visitI64Eqz() throws -> Output {
        try popPushEmit(.i64, .i32) { value, result, stack in
            .i64Eqz(Instruction.UnaryOperand(result: LVReg(result), input: LVReg(value)))
        }
    }
    mutating func visitUnary(_ unary: WasmParser.Instruction.Unary) throws {
        let operand: ValueType
        let instruction: (Instruction.UnaryOperand) -> Instruction
        switch unary {
        case .i32Clz: (operand, instruction) = (.i32, Instruction.i32Clz)
        case .i32Ctz: (operand, instruction) = (.i32, Instruction.i32Ctz)
        case .i32Popcnt: (operand, instruction) = (.i32, Instruction.i32Popcnt)
        case .i64Clz: (operand, instruction) = (.i64, Instruction.i64Clz)
        case .i64Ctz: (operand, instruction) = (.i64, Instruction.i64Ctz)
        case .i64Popcnt: (operand, instruction) = (.i64, Instruction.i64Popcnt)
        case .f32Abs: (operand, instruction) = (.f32, Instruction.f32Abs)
        case .f32Neg: (operand, instruction) = (.f32, Instruction.f32Neg)
        case .f32Ceil: (operand, instruction) = (.f32, Instruction.f32Ceil)
        case .f32Floor: (operand, instruction) = (.f32, Instruction.f32Floor)
        case .f32Trunc: (operand, instruction) = (.f32, Instruction.f32Trunc)
        case .f32Nearest: (operand, instruction) = (.f32, Instruction.f32Nearest)
        case .f32Sqrt: (operand, instruction) = (.f32, Instruction.f32Sqrt)
        case .f64Abs: (operand, instruction) = (.f64, Instruction.f64Abs)
        case .f64Neg: (operand, instruction) = (.f64, Instruction.f64Neg)
        case .f64Ceil: (operand, instruction) = (.f64, Instruction.f64Ceil)
        case .f64Floor: (operand, instruction) = (.f64, Instruction.f64Floor)
        case .f64Trunc: (operand, instruction) = (.f64, Instruction.f64Trunc)
        case .f64Nearest: (operand, instruction) = (.f64, Instruction.f64Nearest)
        case .f64Sqrt: (operand, instruction) = (.f64, Instruction.f64Sqrt)
        case .i32Extend8S: (operand, instruction) = (.i32, Instruction.i32Extend8S)
        case .i32Extend16S: (operand, instruction) = (.i32, Instruction.i32Extend16S)
        case .i64Extend8S: (operand, instruction) = (.i64, Instruction.i64Extend8S)
        case .i64Extend16S: (operand, instruction) = (.i64, Instruction.i64Extend16S)
        case .i64Extend32S: (operand, instruction) = (.i64, Instruction.i64Extend32S)
        }
        try visitUnary(operand, instruction)
    }
    mutating func visitConversion(_ conversion: WasmParser.Instruction.Conversion) throws {
        let from: ValueType
        let to: ValueType
        let instruction: (Instruction.UnaryOperand) -> Instruction
        switch conversion {
        case .i32WrapI64: (from, to, instruction) = (.i64, .i32, Instruction.i32WrapI64)
        case .i32TruncF32S: (from, to, instruction) = (.f32, .i32, Instruction.i32TruncF32S)
        case .i32TruncF32U: (from, to, instruction) = (.f32, .i32, Instruction.i32TruncF32U)
        case .i32TruncF64S: (from, to, instruction) = (.f64, .i32, Instruction.i32TruncF64S)
        case .i32TruncF64U: (from, to, instruction) = (.f64, .i32, Instruction.i32TruncF64U)
        case .i64ExtendI32S: (from, to, instruction) = (.i32, .i64, Instruction.i64ExtendI32S)
        case .i64ExtendI32U: (from, to, instruction) = (.i32, .i64, Instruction.i64ExtendI32U)
        case .i64TruncF32S: (from, to, instruction) = (.f32, .i64, Instruction.i64TruncF32S)
        case .i64TruncF32U: (from, to, instruction) = (.f32, .i64, Instruction.i64TruncF32U)
        case .i64TruncF64S: (from, to, instruction) = (.f64, .i64, Instruction.i64TruncF64S)
        case .i64TruncF64U: (from, to, instruction) = (.f64, .i64, Instruction.i64TruncF64U)
        case .f32ConvertI32S: (from, to, instruction) = (.i32, .f32, Instruction.f32ConvertI32S)
        case .f32ConvertI32U: (from, to, instruction) = (.i32, .f32, Instruction.f32ConvertI32U)
        case .f32ConvertI64S: (from, to, instruction) = (.i64, .f32, Instruction.f32ConvertI64S)
        case .f32ConvertI64U: (from, to, instruction) = (.i64, .f32, Instruction.f32ConvertI64U)
        case .f32DemoteF64: (from, to, instruction) = (.f64, .f32, Instruction.f32DemoteF64)
        case .f64ConvertI32S: (from, to, instruction) = (.i32, .f64, Instruction.f64ConvertI32S)
        case .f64ConvertI32U: (from, to, instruction) = (.i32, .f64, Instruction.f64ConvertI32U)
        case .f64ConvertI64S: (from, to, instruction) = (.i64, .f64, Instruction.f64ConvertI64S)
        case .f64ConvertI64U: (from, to, instruction) = (.i64, .f64, Instruction.f64ConvertI64U)
        case .f64PromoteF32: (from, to, instruction) = (.f32, .f64, Instruction.f64PromoteF32)
        case .i32ReinterpretF32: (from, to, instruction) = (.f32, .i32, Instruction.i32ReinterpretF32)
        case .i64ReinterpretF64: (from, to, instruction) = (.f64, .i64, Instruction.i64ReinterpretF64)
        case .f32ReinterpretI32: (from, to, instruction) = (.i32, .f32, Instruction.f32ReinterpretI32)
        case .f64ReinterpretI64: (from, to, instruction) = (.i64, .f64, Instruction.f64ReinterpretI64)
        case .i32TruncSatF32S: (from, to, instruction) = (.f32, .i32, Instruction.i32TruncSatF32S)
        case .i32TruncSatF32U: (from, to, instruction) = (.f32, .i32, Instruction.i32TruncSatF32U)
        case .i32TruncSatF64S: (from, to, instruction) = (.f64, .i32, Instruction.i32TruncSatF64S)
        case .i32TruncSatF64U: (from, to, instruction) = (.f64, .i32, Instruction.i32TruncSatF64U)
        case .i64TruncSatF32S: (from, to, instruction) = (.f32, .i64, Instruction.i64TruncSatF32S)
        case .i64TruncSatF32U: (from, to, instruction) = (.f32, .i64, Instruction.i64TruncSatF32U)
        case .i64TruncSatF64S: (from, to, instruction) = (.f64, .i64, Instruction.i64TruncSatF64S)
        case .i64TruncSatF64U: (from, to, instruction) = (.f64, .i64, Instruction.i64TruncSatF64U)
        }
        try visitConversion(from, to, instruction)
    }

    mutating func visitMemoryInit(dataIndex: UInt32) throws -> Output {
        try self.validator.validateDataSegment(dataIndex)
        let addressType = try module.addressType(memoryIndex: 0)
        try pop3Emit((.i32, .i32, addressType)) { values, stack in
            let (size, sourceOffset, destOffset) = values
            return .memoryInit(
                Instruction.MemoryInitOperand(
                    segmentIndex: dataIndex,
                    destOffset: destOffset,
                    sourceOffset: sourceOffset,
                    size: size
                )
            )
        }
    }
    mutating func visitDataDrop(dataIndex: UInt32) throws -> Output {
        try self.validator.validateDataSegment(dataIndex)
        emit(.memoryDataDrop(Instruction.MemoryDataDropOperand(segmentIndex: dataIndex)))
    }
    mutating func visitMemoryCopy(dstMem: UInt32, srcMem: UInt32) throws -> Output {
        //     C.mems[0] = it limits
        // -----------------------------
        // C ⊦ memory.fill : [it i32 it] → []
        // https://github.com/WebAssembly/memory64/blob/main/proposals/memory64/Overview.md
        let addressType = try module.addressType(memoryIndex: 0)
        try pop3Emit((addressType, addressType, addressType)) { values, stack in
            let (size, sourceOffset, destOffset) = values
            return .memoryCopy(
                Instruction.MemoryCopyOperand(
                    destOffset: destOffset,
                    sourceOffset: sourceOffset,
                    size: LVReg(size)
                )
            )
        }
    }
    mutating func visitMemoryFill(memory: UInt32) throws -> Output {
        //     C.mems[0] = it limits
        // -----------------------------
        // C ⊦ memory.fill : [it i32 it] → []
        // https://github.com/WebAssembly/memory64/blob/main/proposals/memory64/Overview.md
        let addressType = try module.addressType(memoryIndex: 0)
        try pop3Emit((addressType, .i32, addressType)) { values, stack in
            let (size, value, destOffset) = values
            return .memoryFill(
                Instruction.MemoryFillOperand(
                    destOffset: destOffset,
                    value: value,
                    size: LVReg(size)
                )
            )
        }
    }
    mutating func visitTableInit(elemIndex: UInt32, table: UInt32) throws -> Output {
        try validator.validateTableInit(elemIndex: elemIndex, table: table)

        try pop3Emit((.i32, .i32, module.addressType(tableIndex: table))) { values, stack in
            let (size, sourceOffset, destOffset) = values
            return .tableInit(
                Instruction.TableInitOperand(
                    tableIndex: table,
                    segmentIndex: elemIndex,
                    destOffset: destOffset,
                    sourceOffset: sourceOffset,
                    size: size
                )
            )
        }
    }
    mutating func visitElemDrop(elemIndex: UInt32) throws -> Output {
        try self.module.validateElementSegment(elemIndex)
        emit(.tableElementDrop(Instruction.TableElementDropOperand(index: elemIndex)))
    }
    mutating func visitTableCopy(dstTable: UInt32, srcTable: UInt32) throws -> Output {
        //   C.tables[d] = iN limits t   C.tables[s] = iM limits t    K = min {N, M}
        // -----------------------------------------------------------------------------
        // C ⊦ table.copy d s : [iN iM iK] → []
        // https://github.com/WebAssembly/memory64/blob/main/proposals/memory64/Overview.md
        try validator.validateTableCopy(dest: dstTable, source: srcTable)
        let destIsMemory64 = try module.isMemory64(tableIndex: dstTable)
        let sourceIsMemory64 = try module.isMemory64(tableIndex: srcTable)
        let lengthIsMemory64 = destIsMemory64 && sourceIsMemory64
        try pop3Emit(
            (
                .address(isMemory64: lengthIsMemory64),
                .address(isMemory64: sourceIsMemory64),
                .address(isMemory64: destIsMemory64)
            )
        ) { values, stack in
            let (size, sourceOffset, destOffset) = values
            return .tableCopy(
                Instruction.TableCopyOperand(
                    sourceIndex: srcTable,
                    destIndex: dstTable,
                    destOffset: destOffset,
                    sourceOffset: sourceOffset,
                    size: size
                )
            )
        }
    }
    mutating func visitTableFill(table: UInt32) throws -> Output {
        let address = try module.addressType(tableIndex: table)
        let type = try module.tableType(table)
        try pop3Emit((address, .ref(type.elementType), address)) { values, stack in
            let (size, value, destOffset) = values
            return .tableFill(
                Instruction.TableFillOperand(
                    tableIndex: table,
                    destOffset: destOffset,
                    value: value,
                    size: size
                )
            )
        }
    }
    mutating func visitTableGet(table: UInt32) throws -> Output {
        let type = try module.tableType(table)
        try popPushEmit(
            module.addressType(tableIndex: table),
            .ref(type.elementType)
        ) { index, result, stack in
            return .tableGet(
                Instruction.TableGetOperand(
                    index: index,
                    result: result,
                    tableIndex: table
                )
            )
        }
    }
    mutating func visitTableSet(table: UInt32) throws -> Output {
        let type = try module.tableType(table)
        try pop2Emit((.ref(type.elementType), module.addressType(tableIndex: table))) { values, stack in
            let (value, index) = values
            return .tableSet(
                Instruction.TableSetOperand(
                    index: index,
                    value: value,
                    tableIndex: table
                )
            )
        }
    }
    mutating func visitTableGrow(table: UInt32) throws -> Output {
        let address = try module.addressType(tableIndex: table)
        let type = try module.tableType(table)
        try pop2PushEmit((address, .ref(type.elementType)), address) { values, result in
            let (delta, value) = values
            return .tableGrow(
                Instruction.TableGrowOperand(
                    tableIndex: table,
                    result: result,
                    delta: delta,
                    value: value
                )
            )
        }
    }
    mutating func visitTableSize(table: UInt32) throws -> Output {
        pushEmit(try module.addressType(tableIndex: table)) { result in
            return .tableSize(Instruction.TableSizeOperand(tableIndex: table, result: LVReg(result)))
        }
    }
}

struct TranslationError: Error, CustomStringConvertible {
    let description: String

    init(_ description: String) {
        self.description = description
    }
}

extension FunctionType {
    fileprivate init(blockType: WasmParser.BlockType, typeSection: [FunctionType]) throws {
        switch blockType {
        case .type(let valueType):
            self.init(parameters: [], results: [valueType])
        case .empty:
            self.init(parameters: [], results: [])
        case let .funcType(typeIndex):
            let typeIndex = Int(typeIndex)
            guard typeIndex < typeSection.count else {
                throw ValidationError(.indexOutOfBounds("type", typeIndex, max: typeSection.count))
            }
            let funcType = typeSection[typeIndex]
            self.init(
                parameters: funcType.parameters,
                results: funcType.results
            )
        }
    }
}

extension ValueType {
    fileprivate static func address(isMemory64: Bool) -> ValueType {
        return isMemory64 ? .i64 : .i32
    }
}