Source file wasm_link.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
open Stdlib
type heaptype =
| Func
| Nofunc
| Extern
| Noextern
| Any
| Eq
| I31
| Struct
| Array
| None_
| Type of int
type reftype =
{ nullable : bool
; typ : heaptype
}
type valtype =
| I32
| I64
| F32
| F64
| V128
| Ref of reftype
type packedtype =
| I8
| I16
type storagetype =
| Val of valtype
| Packed of packedtype
type 'ty mut =
{ mut : bool
; typ : 'ty
}
type fieldtype = storagetype mut
type comptype =
| Func of
{ params : valtype array
; results : valtype array
}
| Struct of fieldtype array
| Array of fieldtype
type subtype =
{ final : bool
; supertype : int option
; typ : comptype
}
type rectype = subtype array
type limits =
{ min : int
; max : int option
; shared : bool
; index_type : [ `I32 | `I64 ]
}
type tabletype =
{ limits : limits
; typ : reftype
}
type importdesc =
| Func of int
| Table of tabletype
| Mem of limits
| Global of valtype mut
| Tag of int
type import =
{ module_ : string
; name : string
; desc : importdesc
}
type exportable =
| Func
| Table
| Mem
| Global
| Tag
let rec output_uint ch i =
if i < 128
then output_byte ch i
else (
output_byte ch (128 + (i land 127));
output_uint ch (i lsr 7))
module Write = struct
type st = { mutable type_index_count : int }
let byte ch b = Buffer.add_char ch (Char.chr b)
let string ch s = Buffer.add_string ch s
let rec sint ch i =
if i >= -64 && i < 64
then byte ch (i land 127)
else (
byte ch (128 + (i land 127));
sint ch (i asr 7))
let rec uint ch i =
if i < 128
then byte ch i
else (
byte ch (128 + (i land 127));
uint ch (i lsr 7))
let vec f ch l =
uint ch (Array.length l);
Array.iter ~f:(fun x -> f ch x) l
let name ch name =
uint ch (String.length name);
string ch name
let typeidx st idx = if idx < 0 then lnot idx + st.type_index_count else idx
let heaptype st ch typ =
match (typ : heaptype) with
| Nofunc -> byte ch 0x73
| Noextern -> byte ch 0x72
| None_ -> byte ch 0x71
| Func -> byte ch 0x70
| Extern -> byte ch 0x6F
| Any -> byte ch 0x6E
| Eq -> byte ch 0x6D
| I31 -> byte ch 0x6C
| Struct -> byte ch 0x6B
| Array -> byte ch 0x6A
| Type idx -> sint ch (typeidx st idx)
let reftype st ch { nullable; typ } =
(match nullable, typ with
| false, _ -> byte ch 0x64
| true, Type _ -> byte ch 0x63
| _ -> ());
heaptype st ch typ
let valtype st ch (typ : valtype) =
match typ with
| I32 -> byte ch 0x7F
| I64 -> byte ch 0x7E
| F32 -> byte ch 0x7D
| F64 -> byte ch 0x7C
| V128 -> byte ch 0x7B
| Ref typ -> reftype st ch typ
let mutability ch mut = byte ch (if mut then 0x01 else 0x00)
let fieldtype st ch { mut; typ } =
(match typ with
| Val typ -> valtype st ch typ
| Packed typ -> (
match typ with
| I8 -> byte ch 0x78
| I16 -> byte ch 0x77));
mutability ch mut
let functype st ch params results =
byte ch 0x60;
vec (valtype st) ch params;
vec (valtype st) ch results
let subtype st ch { final; supertype; typ } =
(match supertype, final with
| None, true -> ()
| None, false ->
byte ch 0x50;
byte ch 0
| Some supertype, _ ->
byte ch (if final then 0X4F else 0x50);
byte ch 1;
uint ch (typeidx st supertype));
match typ with
| Array field_type ->
byte ch 0x5E;
fieldtype st ch field_type
| Struct l ->
byte ch 0x5F;
vec (fieldtype st) ch l
| Func { params; results } -> functype st ch params results
let rectype st ch l =
let len = Array.length l in
if len > 1
then (
byte ch 0x4E;
uint ch len);
Array.iter ~f:(subtype st ch) l;
st.type_index_count <- st.type_index_count + len
let types ch l =
let st = { type_index_count = 0 } in
vec (rectype st) ch l;
st
let limits ch { min; max; shared; index_type } =
let kind =
(if Option.is_none max then 0 else 1)
+ (if shared then 2 else 0)
+
match index_type with
| `I64 -> 4
| `I32 -> 0
in
byte ch kind;
uint ch min;
Option.iter ~f:(uint ch) max
let globaltype st ch mut typ =
valtype st ch typ;
mutability ch mut
let tabletype st ch { limits = l; typ } =
reftype st ch typ;
limits ch l
let imports st ch imports =
vec
(fun ch { module_; name = nm; desc } ->
name ch module_;
name ch nm;
match desc with
| Func typ ->
byte ch 0x00;
uint ch typ
| Table typ ->
byte ch 0x01;
tabletype st ch typ
| Mem l ->
byte ch 0x03;
limits ch l
| Global { mut; typ } ->
byte ch 0x03;
globaltype st ch mut typ
| Tag typ ->
byte ch 0x04;
byte ch 0x00;
uint ch typ)
ch
imports
let functions = vec uint
let memtype = limits
let memories = vec memtype
let export ch kind nm idx =
name ch nm;
byte
ch
(match kind with
| Func -> 0
| Table -> 1
| Mem -> 2
| Global -> 3
| Tag -> 4);
uint ch idx
let start = uint
let tag ch tag =
byte ch 0;
uint ch tag
let tags = vec tag
let data_count = uint
let nameassoc ch idx nm =
uint ch idx;
name ch nm
let namemap = vec (fun ch (idx, name) -> nameassoc ch idx name)
end
type 'a exportable_info =
{ mutable func : 'a
; mutable table : 'a
; mutable mem : 'a
; mutable global : 'a
; mutable tag : 'a
}
let iter_exportable_info f { func; table; mem; global; tag } =
f Func func;
f Table table;
f Mem mem;
f Global global;
f Tag tag
let map_exportable_info f { func; table; mem; global; tag } =
{ func = f Func func
; table = f Table table
; mem = f Mem mem
; global = f Global global
; tag = f Tag tag
}
let fold_exportable_info f acc { func; table; mem; global; tag } =
acc |> f Func func |> f Table table |> f Mem mem |> f Global global |> f Tag tag
let init_exportable_info f =
{ func = f (); table = f (); mem = f (); global = f (); tag = f () }
let make_exportable_info v = init_exportable_info (fun _ -> v)
let exportable_kind d =
match d with
| 0 -> Func
| 1 -> Table
| 2 -> Mem
| 3 -> Global
| 4 -> Tag
| _ -> assert false
let get_exportable_info info kind =
match kind with
| Func -> info.func
| Table -> info.table
| Mem -> info.mem
| Global -> info.global
| Tag -> info.tag
let set_exportable_info info kind v =
match kind with
| Func -> info.func <- v
| Table -> info.table <- v
| Mem -> info.mem <- v
| Global -> info.global <- v
| Tag -> info.tag <- v
module Read = struct
let = "\000asm\001\000\000\000"
let file contents =
if
String.length contents < 8
|| not (String.equal header (String.sub contents ~pos:0 ~len:8))
then failwith (file ^ " is not a Wasm binary file (bad magic)")
type ch =
{ buf : string
; mutable pos : int
; limit : int
}
let pos_in ch = ch.pos
let seek_in ch pos = ch.pos <- pos
let input_byte ch =
let pos = ch.pos in
ch.pos <- pos + 1;
Char.code ch.buf.[pos]
let peek_byte ch = Char.code ch.buf.[ch.pos]
let really_input_string ch len =
let pos = ch.pos in
ch.pos <- pos + len;
String.sub ch.buf ~pos ~len
let rec uint ?(n = 5) ch =
let i = input_byte ch in
if n = 1 then assert (i < 16);
if i < 128 then i else i - 128 + (uint ~n:(n - 1) ch lsl 7)
let rec sint ?(n = 5) ch =
let i = input_byte ch in
if n = 1 then assert (i < 8 || (i > 120 && i < 128));
if i < 64 then i else if i < 128 then i - 128 else i - 128 + (sint ~n:(n - 1) ch lsl 7)
let repeat n f ch = Array.init n ~f:(fun _ -> f ch)
let vec f ch = repeat (uint ch) f ch
let repeat' n f ch =
for _ = 1 to n do
f ch
done
let vec' f ch = repeat' (uint ch) f ch
let name ch = really_input_string ch (uint ch)
type section =
{ id : int
; pos : int
; size : int
}
type index =
{ sections : (int, section) Hashtbl.t
; custom_sections : (string, section) Hashtbl.t
}
let next_section ch =
if pos_in ch = ch.limit
then None
else
let id = input_byte ch in
let size = uint ch in
Some { id; pos = pos_in ch; size }
let skip_section ch { pos; size; _ } = seek_in ch (pos + size)
let index ch =
let index = { sections = Hashtbl.create 16; custom_sections = Hashtbl.create 16 } in
let rec loop () =
match next_section ch with
| None -> index
| Some sect ->
if sect.id = 0
then Hashtbl.add index.custom_sections (name ch) sect
else Hashtbl.add index.sections sect.id sect;
skip_section ch sect;
loop ()
in
loop ()
type t =
{ ch : ch
; mutable type_mapping : int array
; mutable type_index_count : int
; index : index
}
let open_in f buf =
check_header f buf;
let ch = { buf; pos = 8; limit = String.length buf } in
{ ch; type_mapping = [||]; type_index_count = 0; index = index ch }
let find_section contents n =
match Hashtbl.find contents.index.sections n with
| { pos; _ } ->
seek_in contents.ch pos;
true
| exception Not_found -> false
let get_custom_section contents name =
Hashtbl.find_opt contents.index.custom_sections name
let focus_on_custom_section contents section =
let pos, limit =
match get_custom_section contents section with
| Some { pos; size; _ } -> pos, pos + size
| None -> 0, 0
in
let ch = { buf = contents.ch.buf; pos; limit } in
if limit > 0 then ignore (name ch);
{ contents with index = index ch }
module RecTypeTbl = Hashtbl.Make (struct
type t = rectype
let hash t =
Hashtbl.hash_param 15 100 t
let heaptype_eq t1 t2 =
Stdlib.phys_equal t1 t2
||
match t1, t2 with
| Type i1, Type i2 -> i1 = i2
| _ -> false
let reftype_eq { nullable = n1; typ = t1 } { nullable = n2; typ = t2 } =
Bool.(n1 = n2) && heaptype_eq t1 t2
let valtype_eq t1 t2 =
Stdlib.phys_equal t1 t2
||
match t1, t2 with
| Ref t1, Ref t2 -> reftype_eq t1 t2
| _ -> false
let storagetype_eq t1 t2 =
match t1, t2 with
| Val v1, Val v2 -> valtype_eq v1 v2
| Packed p1, Packed p2 -> Stdlib.phys_equal p1 p2
| _ -> false
let fieldtype_eq { mut = m1; typ = t1 } { mut = m2; typ = t2 } =
Bool.(m1 = m2) && storagetype_eq t1 t2
let array_for_all2 p a1 a2 =
let n1 = Array.length a1 and n2 = Array.length a2 in
n1 = n2
&&
let rec loop p a1 a2 n1 i =
i = n1 || (p a1.(i) a2.(i) && loop p a1 a2 n1 (succ i))
in
loop p a1 a2 n1 0
let comptype_eq (t1 : comptype) (t2 : comptype) =
match t1, t2 with
| Func { params = p1; results = r1 }, Func { params = p2; results = r2 } ->
array_for_all2 valtype_eq p1 p2 && array_for_all2 valtype_eq r1 r2
| Struct l1, Struct l2 -> array_for_all2 fieldtype_eq l1 l2
| Array f1, Array f2 -> fieldtype_eq f1 f2
| _ -> false
let subtype_eq
{ final = f1; supertype = s1; typ = t1 }
{ final = f2; supertype = s2; typ = t2 } =
Bool.(f1 = f2)
&& (match s1, s2 with
| Some _, None | None, Some _ -> false
| None, None -> true
| Some i1, Some i2 -> i1 = i2)
&& comptype_eq t1 t2
let equal t1 t2 =
match t1, t2 with
| [| t1 |], [| t2 |] -> subtype_eq t1 t2
| _ -> array_for_all2 subtype_eq t1 t2
end)
type types =
{ types : int RecTypeTbl.t
; mutable last_index : int
; mutable rev_list : rectype list
}
let create_types () = { types = RecTypeTbl.create 2000; last_index = 0; rev_list = [] }
let add_rectype types typ =
try RecTypeTbl.find types.types typ
with Not_found ->
let index = types.last_index in
RecTypeTbl.add types.types typ index;
types.last_index <- Array.length typ + index;
types.rev_list <- typ :: types.rev_list;
index
let heaptype st ch =
let i = sint ch in
match i + 128 with
| 0X73 -> Nofunc
| 0x72 -> Noextern
| 0x71 -> None_
| 0x70 -> Func
| 0x6F -> Extern
| 0x6E -> Any
| 0x6D -> Eq
| 0x6C -> I31
| 0x6B -> Struct
| 0x6A -> Array
| _ ->
if i < 0 then failwith (Printf.sprintf "Unknown heaptype %x@." i);
let i =
if i >= st.type_index_count
then lnot (i - st.type_index_count)
else st.type_mapping.(i)
in
Type i
let nullable typ = { nullable = true; typ }
let ref_eq = { nullable = false; typ = Eq }
let ref_i31 = { nullable = false; typ = I31 }
let reftype' st i ch =
match i with
| 0X73 -> nullable Nofunc
| 0x72 -> nullable Noextern
| 0x71 -> nullable None_
| 0x70 -> nullable Func
| 0x6F -> nullable Extern
| 0x6E -> nullable Any
| 0x6D -> nullable Eq
| 0x6C -> nullable I31
| 0x6B -> nullable Struct
| 0x6A -> nullable Array
| 0x63 -> nullable (heaptype st ch)
| 0x64 -> { nullable = false; typ = heaptype st ch }
| _ -> failwith (Printf.sprintf "Unknown reftype %x@." i)
let reftype st ch = reftype' st (input_byte ch) ch
let ref_i31 = Ref ref_i31
let ref_eq = Ref ref_eq
let valtype' st i ch =
match i with
| 0x7B -> V128
| 0x7C -> F64
| 0x7D -> F32
| 0x7E -> I64
| 0x7F -> I32
| 0x64 -> (
match peek_byte ch with
| 0x6C ->
ignore (input_byte ch);
ref_i31
| 0x6D ->
ignore (input_byte ch);
ref_eq
| _ -> Ref { nullable = false; typ = heaptype st ch })
| _ -> Ref (reftype' st i ch)
let valtype st ch =
let i = uint ch in
valtype' st i ch
let storagetype st ch =
let i = uint ch in
match i with
| 0x78 -> Packed I8
| 0x77 -> Packed I16
| _ -> Val (valtype' st i ch)
let fieldtype st ch =
let typ = storagetype st ch in
let mut = input_byte ch <> 0 in
{ mut; typ }
let comptype st i ch =
match i with
| 0x5E -> Array (fieldtype st ch)
| 0x5F -> Struct (vec (fieldtype st) ch)
| 0x60 ->
let params = vec (valtype st) ch in
let results = vec (valtype st) ch in
Func { params; results }
| c -> failwith (Printf.sprintf "Unknown comptype %d" c)
let supertype st ch =
match input_byte ch with
| 0 -> None
| 1 ->
let t = uint ch in
Some
(if t >= st.type_index_count
then lnot (t - st.type_index_count)
else st.type_mapping.(t))
| _ -> assert false
let subtype st i ch =
match i with
| 0x50 ->
let supertype = supertype st ch in
{ final = false; supertype; typ = comptype st (input_byte ch) ch }
| 0x4F ->
let supertype = supertype st ch in
{ final = true; supertype; typ = comptype st (input_byte ch) ch }
| _ -> { final = true; supertype = None; typ = comptype st i ch }
let rectype st ch =
match input_byte ch with
| 0x4E -> vec (fun ch -> subtype st (input_byte ch) ch) ch
| i -> [| subtype st i ch |]
let type_section st types ch =
let n = uint ch in
st.type_mapping <- Array.make n 0;
st.type_index_count <- 0;
repeat'
n
(fun ch ->
let ty = rectype st ch in
let pos = st.type_index_count in
let pos' = add_rectype types ty in
let count = Array.length ty in
for i = 0 to count - 1 do
st.type_mapping.(pos + i) <- pos' + i
done;
st.type_index_count <- pos + count)
ch
let limits ch =
let kind = input_byte ch in
assert (kind < 8);
let shared = kind land 2 <> 0 in
let index_type = if kind land 4 = 0 then `I32 else `I64 in
let min = uint ch in
let max = if kind land 1 = 0 then None else Some (uint ch) in
{ min; max; shared; index_type }
let memtype = limits
let tabletype st ch =
let typ = reftype st ch in
let limits = limits ch in
{ limits; typ }
let typeidx st ch = st.type_mapping.(uint ch)
let globaltype st ch =
let typ = valtype st ch in
let mut = input_byte ch in
assert (mut < 2);
{ mut = mut <> 0; typ }
let import tbl st ch =
let module_ = name ch in
let name = name ch in
let d = uint ch in
if d > 4 then failwith (Printf.sprintf "Unknown import %x@." d);
let importdesc : importdesc =
match d with
| 0 -> Func st.type_mapping.(uint ch)
| 1 -> Table (tabletype st ch)
| 2 -> Mem (memtype ch)
| 3 -> Global (globaltype st ch)
| 4 ->
let b = uint ch in
assert (b = 0);
Tag st.type_mapping.(uint ch)
| _ -> assert false
in
let entry = { module_; name; desc = importdesc } in
let kind = exportable_kind d in
set_exportable_info tbl kind (entry :: get_exportable_info tbl kind)
let export tbl ch =
let name = name ch in
let d = uint ch in
if d > 4 then failwith (Printf.sprintf "Unknown export %x@." d);
let idx = uint ch in
let entry = name, idx in
let kind = exportable_kind d in
set_exportable_info tbl kind (entry :: get_exportable_info tbl kind)
type interface =
{ imports : import array exportable_info
; exports : (string * int) list exportable_info
}
let type_section types contents =
if find_section contents 1 then type_section contents types contents.ch
let interface contents =
let imports =
if find_section contents 2
then (
let tbl = make_exportable_info [] in
vec' (import tbl contents) contents.ch;
map_exportable_info (fun _ l -> Array.of_list (List.rev l)) tbl)
else make_exportable_info [||]
in
let exports =
let tbl = make_exportable_info [] in
if find_section contents 7 then vec' (export tbl) contents.ch;
tbl
in
{ imports; exports }
let functions contents =
if find_section contents 3
then vec (fun ch -> typeidx contents ch) contents.ch
else [||]
let memories contents = if find_section contents 5 then vec memtype contents.ch else [||]
let tag contents ch =
let b = input_byte ch in
assert (b = 0);
typeidx contents ch
let tags contents =
if find_section contents 13 then vec (tag contents) contents.ch else [||]
let data_count contents =
if find_section contents 12
then uint contents.ch
else if find_section contents 11
then uint contents.ch
else 0
let start contents = if find_section contents 8 then Some (uint contents.ch) else None
let nameassoc ch =
let idx = uint ch in
let name = name ch in
idx, name
let namemap contents = vec nameassoc contents.ch
end
module Scan = struct
let debug = false
type maps =
{ typ : int array
; func : int array
; table : int array
; mem : int array
; global : int array
; elem : int array
; data : int array
; tag : int array
}
let default_maps =
{ typ = [||]
; func = [||]
; table = [||]
; mem = [||]
; global = [||]
; elem = [||]
; data = [||]
; tag = [||]
}
type resize_data = Wasm_source_map.resize_data =
{ mutable i : int
; mutable pos : int array
; mutable delta : int array
}
let push_resize resize_data pos delta =
let p = resize_data.pos in
let i = resize_data.i in
let p =
if i = Array.length p
then (
let p = Array.make (2 * i) 0 in
let d = Array.make (2 * i) 0 in
Array.blit ~src:resize_data.pos ~src_pos:0 ~dst:p ~dst_pos:0 ~len:i;
Array.blit ~src:resize_data.delta ~src_pos:0 ~dst:d ~dst_pos:0 ~len:i;
resize_data.pos <- p;
resize_data.delta <- d;
p)
else p
in
p.(i) <- pos;
resize_data.delta.(i) <- delta;
resize_data.i <- i + 1
let create_resize_data () =
{ i = 0; pos = Array.make 1024 0; delta = Array.make 1024 0 }
let clear_resize_data resize_data = resize_data.i <- 0
type position_data =
{ mutable i : int
; mutable pos : int array
}
let create_position_data () = { i = 0; pos = Array.make 100 0 }
let clear_position_data position_data = position_data.i <- 0
let push_position position_data pos =
let p = position_data.pos in
let i = position_data.i in
let p =
if i = Array.length p
then (
let p = Array.make (2 * i) 0 in
Array.blit ~src:position_data.pos ~src_pos:0 ~dst:p ~dst_pos:0 ~len:i;
position_data.pos <- p;
p)
else p
in
p.(i) <- pos;
position_data.i <- i + 1
let scanner report mark maps buf code =
let rec output_uint buf i =
if i < 128
then Buffer.add_char buf (Char.chr i)
else (
Buffer.add_char buf (Char.chr (128 + (i land 127)));
output_uint buf (i lsr 7))
in
let rec output_sint buf i =
if i >= -64 && i < 64
then Buffer.add_char buf (Char.chr (i land 127))
else (
Buffer.add_char buf (Char.chr (128 + (i land 127)));
output_sint buf (i asr 7))
in
let start = ref 0 in
let get pos = Char.code (String.get code pos) in
let rec int pos = if get pos >= 128 then int (pos + 1) else pos + 1 in
let rec uint32 pos =
let i = get pos in
if i < 128
then pos + 1, i
else
let pos, i' = pos + 1 |> uint32 in
pos, (i' lsl 7) + (i land 0x7f)
in
let rec sint32 pos =
let i = get pos in
if i < 64
then pos + 1, i
else if i < 128
then pos + 1, i - 128
else
let pos, i' = pos + 1 |> sint32 in
pos, i - 128 + (i' lsl 7)
in
let rec repeat n f pos = if n = 0 then pos else repeat (n - 1) f (f pos) in
let vector f pos =
let pos, i =
let i = get pos in
if i < 128 then pos + 1, i else uint32 pos
in
repeat i f pos
in
let name pos =
let pos', i =
let i = get pos in
if i < 128 then pos + 1, i else uint32 pos
in
pos' + i
in
let flush' pos pos' =
if !start < pos then Buffer.add_substring buf code !start (pos - !start);
start := pos'
in
let flush pos = flush' pos pos in
let rewrite map pos =
let pos', idx =
let i = get pos in
if i < 128
then pos + 1, i
else
let i' = get (pos + 1) in
if i' < 128 then pos + 2, (i' lsl 7) + (i land 0x7f) else uint32 pos
in
let idx' = map idx in
if idx <> idx'
then (
flush' pos pos';
let p = Buffer.length buf in
output_uint buf idx';
let p' = Buffer.length buf in
let dp = p' - p in
let dpos = pos' - pos in
if dp <> dpos then report pos' (dp - dpos));
pos'
in
let rewrite_signed map pos =
let pos', idx =
let i = get pos in
if i < 64 then pos + 1, i else if i < 128 then pos + 1, i - 128 else sint32 pos
in
let idx' = map idx in
if idx <> idx'
then (
flush' pos pos';
let p = Buffer.length buf in
output_sint buf idx';
let p' = Buffer.length buf in
let dp = p' - p in
let dpos = pos' - pos in
if dp <> dpos then report pos (dp - dpos));
pos'
in
let typ_map idx = maps.typ.(idx) in
let typeidx pos = rewrite typ_map pos in
let signed_typeidx pos = rewrite_signed typ_map pos in
let func_map idx = maps.func.(idx) in
let funcidx pos = rewrite func_map pos in
let table_map idx = maps.table.(idx) in
let tableidx pos = rewrite table_map pos in
let mem_map idx = maps.mem.(idx) in
let memidx pos = rewrite mem_map pos in
let global_map idx = maps.global.(idx) in
let globalidx pos = rewrite global_map pos in
let elem_map idx = maps.elem.(idx) in
let elemidx pos = rewrite elem_map pos in
let data_map idx = maps.data.(idx) in
let dataidx pos = rewrite data_map pos in
let tag_map idx = maps.tag.(idx) in
let tagidx pos = rewrite tag_map pos in
let labelidx = int in
let localidx = int in
let laneidx pos = pos + 1 in
let heaptype pos =
let c = get pos in
if c >= 64 && c < 128 then pos + 1 else signed_typeidx pos
in
let absheaptype pos =
match get pos with
| 0X73
| 0x72
| 0x71
| 0x70
| 0x6F
| 0x6E
| 0x6D
| 0x6C
| 0x6B
| 0x6A -> pos + 1
| c -> failwith (Printf.sprintf "Bad heap type 0x%02X@." c)
in
let reftype pos =
match get pos with
| 0x63 | 0x64 -> pos + 1 |> heaptype
| _ -> pos |> absheaptype
in
let valtype pos =
let c = get pos in
match c with
| 0x63 | 0x64 -> pos + 1 |> heaptype
| _ -> pos + 1
in
let blocktype pos =
let c = get pos in
if c >= 64 && c < 128 then pos |> valtype else pos |> signed_typeidx
in
let memarg pos =
let pos', c = uint32 pos in
if c < 64
then (
if mem_map 0 <> 0
then (
flush' pos pos';
let p = Buffer.length buf in
output_uint buf (c + 64);
output_uint buf (mem_map 0);
let p' = Buffer.length buf in
let dp = p' - p in
let dpos = pos' - pos in
if dp <> dpos then report pos (dp - dpos));
pos' |> int)
else pos' |> memidx |> int
in
let rec instructions pos =
if debug then Format.eprintf "0x%02X (@%d)@." (get pos) pos;
match get pos with
| 0x00 | 0x01 | 0x0F ->
pos + 1 |> instructions
| 0x02 | 0x03 ->
pos + 1 |> blocktype |> instructions |> block_end |> instructions
| 0x04 -> pos + 1 |> blocktype |> instructions |> opt_else |> instructions
| 0x0C
| 0x0D
| 0xD5
| 0xD6 -> pos + 1 |> labelidx |> instructions
| 0x0E -> pos + 1 |> vector labelidx |> labelidx |> instructions
| 0x10 | 0x12 -> pos + 1 |> funcidx |> instructions
| 0x11 | 0x13 ->
pos + 1 |> typeidx |> tableidx |> instructions
| 0x14 | 0x15 ->
pos + 1 |> typeidx |> instructions
| 0x06 -> pos + 1 |> blocktype |> instructions |> opt_catch
| 0x08 -> pos + 1 |> tagidx |> instructions
| 0x09 -> pos + 1 |> int |> instructions
| 0x0A -> pos + 1 |> instructions
| 0x1A | 0x1B -> pos + 1 |> instructions
| 0x1C -> pos + 1 |> vector valtype |> instructions
| 0x1F ->
pos + 1
|> blocktype
|> vector catch
|> instructions
|> block_end
|> instructions
| 0x20 | 0x21 | 0x22 ->
pos + 1 |> localidx |> instructions
| 0x23 | 0x24 ->
pos + 1 |> globalidx |> instructions
| 0x25 | 0x26 -> pos + 1 |> tableidx |> instructions
| 0x28
| 0x29
| 0x2A
| 0x2B
| 0x2C
| 0x2D
| 0x2E
| 0x2F
| 0x30
| 0x31
| 0x32
| 0x33
| 0x34
| 0x35
| 0x36 | 0x37 | 0x38 | 0x39 | 0x3A | 0x3B | 0x3C | 0x3D | 0x3E ->
pos + 1 |> memarg |> instructions
| 0x3F | 0x40 -> pos + 1 |> memidx |> instructions
| 0x41 | 0x42 -> pos + 1 |> int |> instructions
| 0x43 -> pos + 5 |> instructions
| 0x44 -> pos + 9 |> instructions
| 0x45
| 0x46
| 0x47
| 0x48
| 0x49
| 0x4A
| 0x4B
| 0x4C
| 0x4D
| 0x4E
| 0x4F
| 0x50
| 0x51
| 0x52
| 0x53
| 0x54
| 0x55
| 0x56
| 0x57
| 0x58
| 0x59
| 0x5A
| 0x5B
| 0x5C
| 0x5D
| 0x5E
| 0x5F
| 0x60
| 0x61
| 0x62
| 0x63
| 0x64
| 0x65
| 0x66
| 0x67
| 0x68
| 0x69
| 0x6A
| 0x6B
| 0x6C
| 0x6D
| 0x6E
| 0x6F
| 0x70
| 0x71
| 0x72
| 0x73
| 0x74
| 0x75
| 0x76
| 0x77
| 0x78
| 0x79
| 0x7A
| 0x7B
| 0x7C
| 0x7D
| 0x7E
| 0x7F
| 0x80
| 0x81
| 0x82
| 0x83
| 0x84
| 0x85
| 0x86
| 0x87
| 0x88
| 0x89
| 0x8A
| 0x8B
| 0x8C
| 0x8D
| 0x8E
| 0x8F
| 0x90
| 0x91
| 0x92
| 0x93
| 0x94
| 0x95
| 0x96
| 0x97
| 0x98
| 0x99
| 0x9A
| 0x9B
| 0x9C
| 0x9D
| 0x9E
| 0x9F
| 0xA0
| 0xA1
| 0xA2
| 0xA3
| 0xA4
| 0xA5
| 0xA6
| 0xA7
| 0xA8
| 0xA9
| 0xAA
| 0xAB
| 0xAC
| 0xAD
| 0xAE
| 0xAF
| 0xB0
| 0xB1
| 0xB2
| 0xB3
| 0xB4
| 0xB5
| 0xB6
| 0xB7
| 0xB8
| 0xB9
| 0xBA
| 0xBB
| 0xBC
| 0xBD
| 0xBE
| 0xBF
| 0xC0
| 0xC1
| 0xC2
| 0xC3
| 0xC4 -> pos + 1 |> instructions
| 0xD0 -> pos + 1 |> heaptype |> instructions
| 0xD1 | 0xD3 | 0xD4 ->
pos + 1 |> instructions
| 0xD2 -> pos + 1 |> funcidx |> instructions
| 0xFB -> pos + 1 |> gc_instruction
| 0xFC -> (
if debug then Format.eprintf " %d@." (get (pos + 1));
match get (pos + 1) with
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 ->
pos + 2 |> instructions
| 8 -> pos + 2 |> dataidx |> memidx |> instructions
| 9 -> pos + 2 |> dataidx |> instructions
| 10 -> pos + 2 |> memidx |> memidx |> instructions
| 11 -> pos + 2 |> memidx |> instructions
| 12 -> pos + 2 |> elemidx |> tableidx |> instructions
| 13 -> pos + 2 |> elemidx |> instructions
| 14 -> pos + 2 |> tableidx |> tableidx |> instructions
| 15 | 16 | 17 ->
pos + 2 |> tableidx |> instructions
| c -> failwith (Printf.sprintf "Bad instruction 0xFC 0x%02X" c))
| 0xFD -> pos + 1 |> vector_instruction
| 0xFE -> pos + 1 |> atomic_instruction
| _ -> pos
and gc_instruction pos =
if debug then Format.eprintf " %d@." (get pos);
match get pos with
| 0
| 1
| 6
| 7
| 11
| 12
| 13
| 14
| 16 -> pos + 1 |> typeidx |> instructions
| 2
| 3
| 4
| 5
| 8 -> pos + 1 |> typeidx |> int |> instructions
| 9 | 18 ->
pos + 1 |> typeidx |> dataidx |> instructions
| 10 | 19 ->
pos + 1 |> typeidx |> elemidx |> instructions
| 15
| 26
| 27
| 28
| 29
| 30 -> pos + 1 |> instructions
| 17 -> pos + 1 |> typeidx |> typeidx |> instructions
| 20 | 21 | 22 | 23 ->
pos + 1 |> heaptype |> instructions
| 24 | 25 ->
pos + 2 |> labelidx |> heaptype |> heaptype |> instructions
| c -> failwith (Printf.sprintf "Bad instruction 0xFB 0x%02X" c)
and vector_instruction pos =
if debug then Format.eprintf " %d@." (get pos);
let pos, i = uint32 pos in
match i with
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 92 | 93
-> pos + 1 |> memarg |> instructions
| 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 ->
pos + 1 |> memarg |> laneidx |> instructions
| 12 | 13 -> pos + 17 |> instructions
| 21
| 22
| 23
| 24
| 25
| 26
| 27
| 28
| 29
| 30
| 31
| 32
| 33
| 34 -> pos + 1 |> laneidx |> instructions
| ( 162
| 165
| 166
| 175
| 176
| 178
| 179
| 180
| 187
| 194
| 197
| 198
| 207
| 208
| 210
| 211
| 212
| 226
| 238 ) as c -> failwith (Printf.sprintf "Bad instruction 0xFD 0x%02X" c)
| c ->
if c <= 275
then pos + 1 |> instructions
else failwith (Printf.sprintf "Bad instruction 0xFD 0x%02X" c)
and atomic_instruction pos =
if debug then Format.eprintf " %d@." (get pos);
match get pos with
| 0
| 1 | 2
| 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 ->
pos + 1 |> memarg |> instructions
| 3 ->
let c = get pos + 1 in
assert (c = 0);
pos + 2 |> instructions
| c -> failwith (Printf.sprintf "Bad instruction 0xFE 0x%02X" c)
and opt_else pos =
if debug then Format.eprintf "0x%02X (@%d) else@." (get pos) pos;
match get pos with
| 0x05 -> pos + 1 |> instructions |> block_end |> instructions
| _ -> pos |> block_end |> instructions
and opt_catch pos =
if debug then Format.eprintf "0x%02X (@%d) catch@." (get pos) pos;
match get pos with
| 0x07 -> pos + 1 |> tagidx |> instructions |> opt_catch
| 0x05 -> pos + 1 |> instructions |> block_end |> instructions
| _ -> pos |> block_end |> instructions
and catch pos =
match get pos with
| 0 | 1 -> pos + 1 |> tagidx |> labelidx
| 2 | 3 -> pos + 1 |> labelidx
| c -> failwith (Printf.sprintf "bad catch 0x02%d@." c)
and block_end pos =
if debug then Format.eprintf "0x%02X (@%d) block end@." (get pos) pos;
match get pos with
| 0x0B -> pos + 1
| c -> failwith (Printf.sprintf "Bad instruction 0x%02X" c)
in
let locals pos = pos |> int |> valtype in
let expr pos = pos |> instructions |> block_end in
let func pos =
start := pos;
pos |> vector locals |> expr |> flush
in
let mut pos = pos + 1 in
let limits pos =
let c = get pos in
assert (c < 8);
if c land 1 = 0 then pos |> int else pos |> int |> int
in
let tabletype pos =
mark pos;
pos |> reftype |> limits
in
let table pos =
match get pos with
| 0x40 ->
assert (get (pos + 1) = 0);
pos + 2 |> tabletype |> expr
| _ -> pos |> tabletype
in
let table_section ~count pos =
start := pos;
pos |> repeat count table |> flush
in
let globaltype pos =
mark pos;
pos |> valtype |> mut
in
let global pos = pos |> globaltype |> expr in
let global_section ~count pos =
start := pos;
pos |> repeat count global |> flush
in
let elemkind pos =
assert (get pos = 0);
pos + 1
in
let elem pos =
match get pos with
| 0 -> pos + 1 |> expr |> vector funcidx
| 1 -> pos + 1 |> elemkind |> vector funcidx
| 2 -> pos + 1 |> tableidx |> expr |> elemkind |> vector funcidx
| 3 -> pos + 1 |> elemkind |> vector funcidx
| 4 -> pos + 1 |> expr |> vector expr
| 5 -> pos + 1 |> reftype |> vector expr
| 6 -> pos + 1 |> tableidx |> expr |> reftype |> vector expr
| 7 -> pos + 1 |> reftype |> vector expr
| c -> failwith (Printf.sprintf "Bad element 0x%02X" c)
in
let bytes pos =
let pos, len = uint32 pos in
pos + len
in
let data pos =
match get pos with
| 0 -> pos + 1 |> expr |> bytes
| 1 -> pos + 1 |> bytes
| 2 -> pos + 1 |> memidx |> expr |> bytes
| c -> failwith (Printf.sprintf "Bad data segment 0x%02X" c)
in
let elem_section ~count pos =
start := pos;
!start |> repeat count elem |> flush
in
let data_section ~count pos =
start := pos;
!start |> repeat count data |> flush
in
let local_nameassoc pos = pos |> localidx |> name in
let local_namemap pos =
start := pos;
pos |> vector local_nameassoc |> flush
in
table_section, global_section, elem_section, data_section, func, local_namemap
let table_section positions maps buf s =
let table_section, _, _, _, _, _ =
scanner (fun _ _ -> ()) (fun pos -> push_position positions pos) maps buf s
in
table_section
let global_section positions maps buf s =
let _, global_section, _, _, _, _ =
scanner (fun _ _ -> ()) (fun pos -> push_position positions pos) maps buf s
in
global_section
let elem_section maps buf s =
let _, _, elem_section, _, _, _ = scanner (fun _ _ -> ()) (fun _ -> ()) maps buf s in
elem_section
let data_section maps buf s =
let _, _, _, data_section, _, _ = scanner (fun _ _ -> ()) (fun _ -> ()) maps buf s in
data_section
let func resize_data maps buf s =
let _, _, _, _, func, _ =
scanner
(fun pos delta -> push_resize resize_data pos delta)
(fun _ -> ())
maps
buf
s
in
func
let local_namemap buf s =
let _, _, _, _, _, local_namemap =
scanner (fun _ _ -> ()) (fun _ -> ()) default_maps buf s
in
local_namemap
end
let interface types contents =
Read.type_section types contents;
Read.interface contents
type t =
{ module_name : string
; file : string
; contents : Read.t
; source_map_contents : Source_map.Standard.t option
}
type import_status =
| Resolved of int * int
| Unresolved of int
let check_limits export import =
export.min >= import.min
&&
match export.max, import.max with
| _, None -> true
| None, Some _ -> false
| Some e, Some i -> e <= i
let rec subtype subtyping_info (i : int) i' =
i = i'
||
match subtyping_info.(i).supertype with
| None -> false
| Some s -> subtype subtyping_info s i'
let heap_subtype (subtyping_info : subtype array) (ty : heaptype) (ty' : heaptype) =
match ty, ty' with
| (Func | Nofunc), Func
| Nofunc, Nofunc
| (Extern | Noextern), Extern
| (Any | Eq | I31 | Struct | Array | None_ | Type _), Any
| (Eq | I31 | Struct | Array | None_ | Type _), Eq
| (I31 | None_), I31
| (Struct | None_), Struct
| (Array | None_), Array
| None_, None_ -> true
| Type i, Struct -> (
match subtyping_info.(i).typ with
| Struct _ -> true
| Array _ | Func _ -> false)
| Type i, Array -> (
match subtyping_info.(i).typ with
| Array _ -> true
| Struct _ | Func _ -> false)
| Type i, Func -> (
match subtyping_info.(i).typ with
| Func _ -> true
| Struct _ | Array _ -> false)
| Type i, Type i' -> subtype subtyping_info i i'
| _ -> false
let ref_subtype subtyping_info { nullable; typ } { nullable = nullable'; typ = typ' } =
((not nullable) || nullable') && heap_subtype subtyping_info typ typ'
let val_subtype subtyping_info ty ty' =
match ty, ty' with
| Ref t, Ref t' -> ref_subtype subtyping_info t t'
| _ -> Stdlib.phys_equal ty ty'
let check_export_import_types ~subtyping_info ~files i (desc : importdesc) i' import =
let ok =
match desc, import.desc with
| Func t, Func t' -> subtype subtyping_info t t'
| Table { limits; typ }, Table { limits = limits'; typ = typ' } ->
check_limits limits limits' && Poly.(typ = typ')
| Mem limits, Mem limits' -> check_limits limits limits'
| Global { mut; typ }, Global { mut = mut'; typ = typ' } ->
Bool.(mut = mut')
&& if mut then Poly.(typ = typ') else val_subtype subtyping_info typ typ'
| Tag t, Tag t' -> t = t'
| _ -> false
in
if not ok
then
failwith
(Printf.sprintf
"In module %s, the import %s / %s refers to an export in module %s of an \
incompatible type"
files.(i').file
import.module_
import.name
files.(i).file)
let build_mappings resolved_imports unresolved_imports kind counts =
let current_offset = ref (get_exportable_info unresolved_imports kind) in
let mappings =
Array.mapi
~f:(fun i count ->
let imports = get_exportable_info resolved_imports.(i) kind in
let import_count = Array.length imports in
let offset = !current_offset - import_count in
current_offset := !current_offset + count;
Array.init
(Array.length imports + count)
~f:(fun i ->
if i < import_count
then
match imports.(i) with
| Unresolved i -> i
| Resolved _ -> -1
else i + offset))
counts
in
Array.iteri
~f:(fun i map ->
let imports = get_exportable_info resolved_imports.(i) kind in
for i = 0 to Array.length imports - 1 do
match imports.(i) with
| Unresolved _ -> ()
| Resolved (j, k) -> map.(i) <- mappings.(j).(k)
done)
mappings;
mappings
let build_simple_mappings ~counts =
let current_offset = ref 0 in
Array.map
~f:(fun count ->
let offset = !current_offset in
current_offset := !current_offset + count;
Array.init count ~f:(fun j -> j + offset))
counts
let add_section out_ch ~id ?count buf =
match count with
| Some 0 -> Buffer.clear buf
| _ ->
let buf' = Buffer.create 5 in
Option.iter ~f:(fun c -> Write.uint buf' c) count;
output_byte out_ch id;
output_uint out_ch (Buffer.length buf' + Buffer.length buf);
Buffer.output_buffer out_ch buf';
Buffer.output_buffer out_ch buf;
Buffer.clear buf
let add_subsection buf ~id ?count buf' =
match count with
| Some 0 -> Buffer.clear buf'
| _ ->
let buf'' = Buffer.create 5 in
Option.iter ~f:(fun c -> Write.uint buf'' c) count;
Buffer.add_char buf (Char.chr id);
Write.uint buf (Buffer.length buf'' + Buffer.length buf');
Buffer.add_buffer buf buf'';
Buffer.add_buffer buf buf';
Buffer.clear buf'
let check_exports_against_imports
~intfs
~subtyping_info
~resolved_imports
~files
~kind
~to_desc =
Array.iteri
~f:(fun i intf ->
let imports = get_exportable_info intf.Read.imports kind in
let statuses = get_exportable_info resolved_imports.(i) kind in
Array.iter2
~f:(fun import status ->
match status with
| Unresolved _ -> ()
| Resolved (i', idx') -> (
match to_desc i' idx' with
| None -> ()
| Some desc ->
check_export_import_types ~subtyping_info ~files i' desc i import))
imports
statuses)
intfs
let read_desc_from_file ~intfs ~files ~positions ~read i j =
let offset = Array.length (get_exportable_info intfs.(i).Read.imports Table) in
if j < offset
then None
else
let { contents; _ } = files.(i) in
Read.seek_in contents.ch positions.(i).Scan.pos.(j - offset);
Some (read contents)
let index_in_output ~unresolved_imports ~mappings ~kind ~get i' idx' =
let offset = get_exportable_info unresolved_imports kind in
let idx'' = mappings.(i').(idx') - offset in
if idx'' >= 0 then Some (get idx'') else None
let write_simple_section
~intfs
~subtyping_info
~resolved_imports
~unresolved_imports
~files
~out_ch
~buf
~kind
~id
~read
~to_type
~write =
let data = Array.map ~f:(fun f -> read f.contents) files in
let entries = Array.concat (Array.to_list data) in
if Array.length entries <> 0
then (
write buf entries;
add_section out_ch ~id buf);
let counts = Array.map ~f:Array.length data in
let mappings = build_mappings resolved_imports unresolved_imports kind counts in
check_exports_against_imports
~intfs
~subtyping_info
~resolved_imports
~files
~kind
~to_desc:
(index_in_output ~unresolved_imports ~mappings ~kind ~get:(fun idx ->
to_type entries.(idx)));
mappings
let write_section_with_scan ~files ~out_ch ~buf ~id ~scan =
let counts =
Array.mapi
~f:(fun i { contents; _ } ->
if Read.find_section contents id
then (
let count = Read.uint contents.ch in
scan
i
{ Scan.default_maps with typ = contents.type_mapping }
buf
contents.ch.buf
~count
contents.ch.pos;
count)
else 0)
files
in
add_section out_ch ~id ~count:(Array.fold_left ~f:( + ) ~init:0 counts) buf;
counts
let write_simple_namemap ~name_sections ~name_section_buffer ~buf ~section_id ~mappings =
let count = ref 0 in
Array.iter2
~f:(fun name_section mapping ->
if Read.find_section name_section section_id
then (
let map = Read.namemap name_section in
Array.iter ~f:(fun (idx, name) -> Write.nameassoc buf mapping.(idx) name) map;
count := !count + Array.length map))
name_sections
mappings;
add_subsection name_section_buffer ~id:section_id ~count:!count buf
let write_namemap
~resolved_imports
~unresolved_imports
~name_sections
~name_section_buffer
~buf
~kind
~section_id
~mappings =
let import_names = Array.make (get_exportable_info unresolved_imports kind) None in
Array.iteri
~f:(fun i name_section ->
if Read.find_section name_section section_id
then
let imports = get_exportable_info resolved_imports.(i) kind in
let import_count = Array.length imports in
let n = Read.uint name_section.ch in
let rec loop j =
if j < n
then
let idx = Read.uint name_section.ch in
let name = Read.name name_section.ch in
if idx < import_count
then (
let idx' =
match imports.(idx) with
| Unresolved idx' -> idx'
| Resolved (i', idx') -> mappings.(i').(idx')
in
if idx' < Array.length import_names && Option.is_none import_names.(idx')
then import_names.(idx') <- Some name;
loop (j + 1))
in
loop 0)
name_sections;
let count = ref 0 in
Array.iteri
~f:(fun idx name ->
match name with
| None -> ()
| Some name ->
incr count;
Write.nameassoc buf idx name)
import_names;
Array.iteri
~f:(fun i name_section ->
if Read.find_section name_section section_id
then
let mapping = mappings.(i) in
let imports = get_exportable_info resolved_imports.(i) kind in
let import_count = Array.length imports in
let n = Read.uint name_section.ch in
let ch = name_section.ch in
for _ = 1 to n do
let idx = Read.uint ch in
let len = Read.uint ch in
if idx >= import_count
then (
incr count;
Write.uint buf mapping.(idx);
Write.uint buf len;
Buffer.add_substring buf ch.buf ch.pos len);
ch.pos <- ch.pos + len
done)
name_sections;
add_subsection name_section_buffer ~id:section_id ~count:!count buf
let write_indirectnamemap ~name_sections ~name_section_buffer ~buf ~section_id ~mappings =
let count = ref 0 in
Array.iter2
~f:(fun name_section mapping ->
if Read.find_section name_section section_id
then (
let n = Read.uint name_section.ch in
let scan_map = Scan.local_namemap buf name_section.ch.buf in
for _ = 1 to n do
let idx = mapping.(Read.uint name_section.ch) in
Write.uint buf idx;
let p = Buffer.length buf in
scan_map name_section.ch.pos;
name_section.ch.pos <- name_section.ch.pos + Buffer.length buf - p
done;
count := !count + n))
name_sections
mappings;
add_subsection name_section_buffer ~id:section_id ~count:!count buf
let rec resolve
depth
~files
~intfs
~subtyping_info
~exports
~kind
i
({ module_; name; _ } as import) =
let i', index = Hashtbl.find exports (module_, name) in
let imports = get_exportable_info intfs.(i').Read.imports kind in
if index < Array.length imports
then (
if depth > 100 then failwith (Printf.sprintf "Import loop on %s %s" module_ name);
let entry = imports.(index) in
check_export_import_types ~subtyping_info ~files i' entry.desc i import;
try resolve (depth + 1) ~files ~intfs ~subtyping_info ~exports ~kind i' entry
with Not_found -> i', index)
else i', index
type input =
{ module_name : string
; file : string
; code : string option
; opt_source_map : Source_map.Standard.t option
}
let f files ~output_file =
let files =
Array.map
~f:(fun { module_name; file; code; opt_source_map } ->
let data =
match code with
| None -> Fs.read_file file
| Some data -> data
in
let contents = Read.open_in file data in
{ module_name; file; contents; source_map_contents = opt_source_map })
(Array.of_list files)
in
let out_ch = open_out_bin output_file in
output_string out_ch Read.header;
let buf = Buffer.create 100000 in
let types = Read.create_types () in
let intfs = Array.map ~f:(fun f -> interface types f.contents) files in
let type_list = List.rev types.rev_list in
let subtyping_info = Array.concat type_list in
let st = Write.types buf (Array.of_list type_list) in
add_section out_ch ~id:1 buf;
let exports = init_exportable_info (fun _ -> Hashtbl.create 128) in
Array.iteri
~f:(fun i intf ->
iter_exportable_info
(fun kind lst ->
let h = get_exportable_info exports kind in
List.iter
~f:(fun (name, index) ->
Hashtbl.add h (files.(i).module_name, name) (i, index))
lst)
intf.Read.exports)
intfs;
let import_list = ref [] in
let unresolved_imports = make_exportable_info 0 in
let resolved_imports =
let tbl = Hashtbl.create 128 in
Array.mapi
~f:(fun i intf ->
map_exportable_info
(fun kind imports ->
let exports = get_exportable_info exports kind in
Array.map
~f:(fun (import : import) ->
match resolve 0 ~files ~intfs ~subtyping_info ~exports ~kind i import with
| i', idx -> Resolved (i', idx)
| exception Not_found -> (
match Hashtbl.find tbl import with
| status -> status
| exception Not_found ->
let idx = get_exportable_info unresolved_imports kind in
let status = Unresolved idx in
Hashtbl.replace tbl import status;
set_exportable_info unresolved_imports kind (1 + idx);
import_list := import :: !import_list;
status))
imports)
intf.Read.imports)
intfs
in
Write.imports st buf (Array.of_list (List.rev !import_list));
add_section out_ch ~id:2 buf;
let start_count =
Array.fold_left
~f:(fun count f ->
match Read.start f.contents with
| None -> count
| Some _ -> count + 1)
~init:0
files
in
let functions = Array.map ~f:(fun f -> Read.functions f.contents) files in
let func_types =
let l = Array.to_list functions in
let l =
if start_count > 1
then
let ty =
let typ : comptype = Func { params = [||]; results = [||] } in
Read.add_rectype types [| { final = true; supertype = None; typ } |]
in
l @ [ [| ty |] ]
else l
in
Array.concat l
in
Write.functions buf func_types;
add_section out_ch ~id:3 buf;
let func_counts = Array.map ~f:Array.length functions in
let func_mappings =
build_mappings resolved_imports unresolved_imports Func func_counts
in
let func_count =
Array.fold_left ~f:( + ) ~init:(if start_count > 1 then 1 else 0) func_counts
in
check_exports_against_imports
~intfs
~subtyping_info
~resolved_imports
~files
~kind:Func
~to_desc:
(index_in_output
~unresolved_imports
~mappings:func_mappings
~kind:Func
~get:(fun idx : importdesc -> Func func_types.(idx)));
let positions =
Array.init (Array.length files) ~f:(fun _ -> Scan.create_position_data ())
in
let table_counts =
write_section_with_scan ~files ~out_ch ~buf ~id:4 ~scan:(fun i maps ->
Scan.table_section positions.(i) { maps with func = func_mappings.(i) })
in
let table_mappings =
build_mappings resolved_imports unresolved_imports Table table_counts
in
check_exports_against_imports
~intfs
~subtyping_info
~resolved_imports
~files
~kind:Table
~to_desc:
(read_desc_from_file ~intfs ~files ~positions ~read:(fun contents : importdesc ->
Table (Read.tabletype contents contents.ch)));
Array.iter ~f:Scan.clear_position_data positions;
let mem_mappings =
write_simple_section
~intfs
~subtyping_info
~resolved_imports
~unresolved_imports
~out_ch
~buf
~kind:Mem
~id:5
~read:Read.memories
~to_type:(fun limits -> Mem limits)
~write:Write.memories
~files
in
let tag_mappings =
write_simple_section
~intfs
~subtyping_info
~resolved_imports
~unresolved_imports
~out_ch
~buf
~kind:Tag
~id:13
~read:Read.tags
~to_type:(fun ty -> Tag ty)
~write:Write.tags
~files
in
let global_mappings = Array.make (Array.length files) [||] in
let global_counts =
let current_offset = ref (get_exportable_info unresolved_imports Global) in
Array.mapi
~f:(fun i { file; contents; _ } ->
let imports = get_exportable_info resolved_imports.(i) Global in
let import_count = Array.length imports in
let offset = !current_offset - import_count in
let build_map count =
let map =
Array.init
(Array.length imports + count)
~f:(fun j ->
if j < import_count
then (
match imports.(j) with
| Unresolved j' -> j'
| Resolved (i', j') ->
(if i' > i
then
let import =
(get_exportable_info intfs.(i).imports Global).(j)
in
failwith
(Printf.sprintf
"In module %s, the import %s / %s refers to an export in a \
later module %s"
file
import.module_
import.name
files.(i').file));
global_mappings.(i').(j'))
else j + offset)
in
global_mappings.(i) <- map;
map
in
let count =
if Read.find_section contents 6
then (
let count = Read.uint contents.ch in
let map = build_map count in
Scan.global_section
positions.(i)
{ Scan.default_maps with
typ = contents.type_mapping
; func = func_mappings.(i)
; global = map
}
buf
contents.ch.buf
contents.ch.pos
~count;
count)
else (
ignore (build_map 0);
0)
in
current_offset := !current_offset + count;
count)
files
in
add_section out_ch ~id:6 ~count:(Array.fold_left ~f:( + ) ~init:0 global_counts) buf;
check_exports_against_imports
~intfs
~subtyping_info
~resolved_imports
~files
~kind:Global
~to_desc:(fun i j : importdesc option ->
let offset = Array.length (get_exportable_info intfs.(i).imports Global) in
if j < offset
then None
else
let { contents; _ } = files.(i) in
Read.seek_in contents.ch positions.(i).pos.(j - offset);
Some (Global (Read.globaltype contents contents.ch)));
Array.iter ~f:Scan.clear_position_data positions;
let export_count =
Array.fold_left
~f:(fun count intf ->
fold_exportable_info
(fun _ exports count -> List.length exports + count)
count
intf.Read.exports)
~init:0
intfs
in
Write.uint buf export_count;
let exports = Hashtbl.create 128 in
Array.iteri
~f:(fun i intf ->
iter_exportable_info
(fun kind lst ->
let map =
match kind with
| Func -> func_mappings.(i)
| Table -> table_mappings.(i)
| Mem -> mem_mappings.(i)
| Global -> global_mappings.(i)
| Tag -> tag_mappings.(i)
in
List.iter
~f:(fun (name, idx) ->
match Hashtbl.find exports name with
| i' ->
failwith
(Printf.sprintf
"Duplicated export %s from %s and %s"
name
files.(i').file
files.(i).file)
| exception Not_found ->
Hashtbl.add exports name i;
Write.export buf kind name map.(idx))
lst)
intf.Read.exports)
intfs;
add_section out_ch ~id:7 buf;
let starts =
Array.mapi
~f:(fun i f ->
Read.start f.contents |> Option.map ~f:(fun idx -> func_mappings.(i).(idx)))
files
|> Array.to_list
|> List.filter_map ~f:(fun x -> x)
in
(match starts with
| [] -> ()
| [ start ] ->
Write.start buf start;
add_section out_ch ~id:8 buf
| _ :: _ :: _ ->
Write.start buf (func_count - 1);
add_section out_ch ~id:8 buf);
let elem_counts =
write_section_with_scan ~files ~out_ch ~buf ~id:9 ~scan:(fun i maps ->
Scan.elem_section
{ maps with func = func_mappings.(i); global = global_mappings.(i) })
in
let elem_mappings = build_simple_mappings ~counts:elem_counts in
let data_mappings, data_count =
let data_counts = Array.map ~f:(fun f -> Read.data_count f.contents) files in
let data_count = Array.fold_left ~f:( + ) ~init:0 data_counts in
let data_mappings = build_simple_mappings ~counts:data_counts in
data_mappings, data_count
in
if data_count > 0
then (
Write.data_count buf data_count;
add_section out_ch ~id:12 buf);
let code_pieces = Buffer.create 100000 in
let resize_data = Scan.create_resize_data () in
let source_maps = ref [] in
Write.uint code_pieces func_count;
Array.iteri
~f:(fun i { contents; source_map_contents; _ } ->
if Read.find_section contents 10
then (
let pos = Buffer.length code_pieces in
let scan_func =
Scan.func
resize_data
{ typ = contents.type_mapping
; func = func_mappings.(i)
; table = table_mappings.(i)
; mem = mem_mappings.(i)
; global = global_mappings.(i)
; elem = elem_mappings.(i)
; data = data_mappings.(i)
; tag = tag_mappings.(i)
}
buf
contents.ch.buf
in
let code (ch : Read.ch) =
let pos = ch.pos in
let i = resize_data.i in
let size = Read.uint ch in
let pos' = ch.pos in
Scan.push_resize resize_data pos' 0;
scan_func ch.pos;
ch.pos <- ch.pos + size;
let p = Buffer.length code_pieces in
Write.uint code_pieces (Buffer.length buf);
let p' = Buffer.length code_pieces in
let delta = p' - p - pos' + pos in
resize_data.delta.(i) <- delta;
Buffer.add_buffer code_pieces buf;
Buffer.clear buf
in
let count = Read.uint contents.ch in
Scan.clear_resize_data resize_data;
Scan.push_resize resize_data 0 (-Read.pos_in contents.ch);
Read.repeat' count code contents.ch;
Option.iter
~f:(fun sm ->
if not (Wasm_source_map.is_empty sm)
then
source_maps := (pos, Wasm_source_map.resize resize_data sm) :: !source_maps)
source_map_contents))
files;
if start_count > 1
then (
Buffer.add_char buf (Char.chr 0);
List.iter
~f:(fun idx ->
Buffer.add_char buf (Char.chr 0x10);
Write.uint buf idx)
starts;
Buffer.add_buffer code_pieces buf;
Buffer.clear buf);
let code_section_offset =
let b = Buffer.create 5 in
Write.uint b (Buffer.length code_pieces);
pos_out out_ch + 1 + Buffer.length b
in
add_section out_ch ~id:10 code_pieces;
let source_map =
Wasm_source_map.concatenate
(List.map
~f:(fun (pos, sm) -> pos + code_section_offset, sm)
(List.rev !source_maps))
in
ignore
(write_section_with_scan ~files ~out_ch ~buf ~id:11 ~scan:(fun i maps ->
Scan.data_section { maps with global = global_mappings.(i) }));
let name_sections =
Array.map
~f:(fun { contents; _ } -> Read.focus_on_custom_section contents "name")
files
in
let name_section_buffer = Buffer.create 100000 in
Write.name name_section_buffer "name";
write_namemap
~resolved_imports
~unresolved_imports
~name_sections
~name_section_buffer
~buf
~kind:Func
~section_id:1
~mappings:func_mappings;
write_indirectnamemap
~name_sections
~name_section_buffer
~buf
~section_id:2
~mappings:func_mappings;
write_indirectnamemap
~name_sections
~name_section_buffer
~buf
~section_id:3
~mappings:func_mappings;
let type_names = Array.make types.last_index None in
Array.iter2
~f:(fun { contents; _ } name_section ->
if Read.find_section name_section 4
then
let map = Read.namemap name_section in
Array.iter
~f:(fun (idx, name) ->
let idx = contents.type_mapping.(idx) in
if Option.is_none type_names.(idx) then type_names.(idx) <- Some (idx, name))
map)
files
name_sections;
Write.namemap
buf
(Array.of_list (List.filter_map ~f:(fun x -> x) (Array.to_list type_names)));
add_subsection name_section_buffer ~id:4 buf;
write_namemap
~resolved_imports
~unresolved_imports
~name_sections
~name_section_buffer
~buf
~kind:Table
~section_id:5
~mappings:table_mappings;
write_namemap
~resolved_imports
~unresolved_imports
~name_sections
~name_section_buffer
~buf
~kind:Mem
~section_id:6
~mappings:mem_mappings;
write_namemap
~resolved_imports
~unresolved_imports
~name_sections
~name_section_buffer
~buf
~kind:Global
~section_id:7
~mappings:global_mappings;
write_simple_namemap
~name_sections
~name_section_buffer
~buf
~section_id:8
~mappings:elem_mappings;
write_simple_namemap
~name_sections
~name_section_buffer
~buf
~section_id:9
~mappings:data_mappings;
let type_field_names = Array.make types.last_index None in
Array.iter2
~f:(fun { contents; _ } name_section ->
if Read.find_section name_section 10
then
let n = Read.uint name_section.ch in
let scan_map = Scan.local_namemap buf name_section.ch.buf in
for _ = 1 to n do
let idx = contents.type_mapping.(Read.uint name_section.ch) in
scan_map name_section.ch.pos;
name_section.ch.pos <- name_section.ch.pos + Buffer.length buf;
if Option.is_none type_field_names.(idx)
then type_field_names.(idx) <- Some (idx, Buffer.contents buf);
Buffer.clear buf
done)
files
name_sections;
let type_field_names =
Array.of_list (List.filter_map ~f:(fun x -> x) (Array.to_list type_field_names))
in
Write.uint buf (Array.length type_field_names);
for i = 0 to Array.length type_field_names - 1 do
let idx, map = type_field_names.(i) in
Write.uint buf idx;
Buffer.add_string buf map
done;
add_subsection name_section_buffer ~id:10 buf;
write_namemap
~resolved_imports
~unresolved_imports
~name_sections
~name_section_buffer
~buf
~kind:Tag
~section_id:11
~mappings:tag_mappings;
add_section out_ch ~id:0 name_section_buffer;
close_out out_ch;
source_map