Welcome to mirror list, hosted at ThFree Co, Russian Federation.

tablegen-x86.js « tools - github.com/asmjit/asmjit.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6b2110b9d673775f6dbceebac83ce6414caeb1aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
// This file is part of AsmJit project <https://asmjit.com>
//
// See asmjit.h or LICENSE.md for license and copyright information
// SPDX-License-Identifier: Zlib

// ============================================================================
// tablegen-x86.js
//
// The purpose of this script is to fetch all instructions' names into a single
// string and to optimize common patterns that appear in instruction data. It
// prevents relocation of small strings (instruction names) that has to be done
// by a linker to make all pointers the binary application/library uses valid.
// This approach decreases the final size of AsmJit binary and relocation data.
//
// NOTE: This script relies on 'asmdb' package. Either install it by using
// node.js package manager (npm) or by copying/symlinking the whole asmdb
// directory as [asmjit]/tools/asmdb.
// ============================================================================

"use strict";

const core = require("./tablegen.js");
const asmdb = core.asmdb;
const kIndent = core.kIndent;

const Lang = core.Lang;
const CxxUtils = core.CxxUtils;
const MapUtils = core.MapUtils;
const ArrayUtils = core.ArrayUtils;
const StringUtils = core.StringUtils;
const IndexedArray = core.IndexedArray;

const hasOwn = Object.prototype.hasOwnProperty;
const disclaimer = StringUtils.disclaimer;

const FAIL = core.FAIL;
const DEBUG = core.DEBUG;

const decToHex = StringUtils.decToHex;

// ============================================================================
// [tablegen.x86.x86isa]
// ============================================================================

// Create the X86 database and add some special cases recognized by AsmJit.
const x86isa = new asmdb.x86.ISA({
  instructions: [
    // Imul in [reg, imm] form is encoded as [reg, reg, imm].
    ["imul", "r16, ib"    , "RMI"  , "66 6B /r ib"   , "ANY OF=W SF=W ZF=U AF=U PF=U CF=W"],
    ["imul", "r32, ib"    , "RMI"  , "6B /r ib"      , "ANY OF=W SF=W ZF=U AF=U PF=U CF=W"],
    ["imul", "r64, ib"    , "RMI"  , "REX.W 6B /r ib", "X64 OF=W SF=W ZF=U AF=U PF=U CF=W"],
    ["imul", "r16, iw"    , "RMI"  , "66 69 /r iw"   , "ANY OF=W SF=W ZF=U AF=U PF=U CF=W"],
    ["imul", "r32, id"    , "RMI"  , "69 /r id"      , "ANY OF=W SF=W ZF=U AF=U PF=U CF=W"],
    ["imul", "r64, id"    , "RMI"  , "REX.W 69 /r id", "X64 OF=W SF=W ZF=U AF=U PF=U CF=W"],

    // Movabs (X64 only).
    ["movabs", "W:r64, iq/uq" , "I"   , "REX.W B8+r iq", "X64"],
    ["movabs", "w:al, moff8"  , "NONE", "A0"           , "X64"],
    ["movabs", "w:ax, moff16" , "NONE", "66 A1"        , "X64"],
    ["movabs", "W:eax, moff32", "NONE", "A1"           , "X64"],
    ["movabs", "W:rax, moff64", "NONE", "REX.W A1"     , "X64"],
    ["movabs", "W:moff8, al"  , "NONE", "A2"           , "X64"],
    ["movabs", "W:moff16, ax" , "NONE", "66 A3"        , "X64"],
    ["movabs", "W:moff32, eax", "NONE", "A3"           , "X64"],
    ["movabs", "W:moff64, rax", "NONE", "REX.W A3"     , "X64"]
  ]
});

// Remapped instructions contain mapping between instructions that AsmJit expects
// and instructions provided by asmdb. In general, AsmJit uses string instructions
// (like cmps, movs, etc...) without the suffix, so we just remap these and keep
// all others.
const RemappedInsts = {
  __proto__: null,

  "cmpsd": { names: ["cmpsd"]                           , rep: false },
  "movsd": { names: ["movsd"]                           , rep: false },
  "cmps" : { names: ["cmpsb", "cmpsw", "cmpsd", "cmpsq"], rep: true  },
  "movs" : { names: ["movsb", "movsw", "movsd", "movsq"], rep: true  },
  "lods" : { names: ["lodsb", "lodsw", "lodsd", "lodsq"], rep: null  },
  "scas" : { names: ["scasb", "scasw", "scasd", "scasq"], rep: null  },
  "stos" : { names: ["stosb", "stosw", "stosd", "stosq"], rep: null  },
  "ins"  : { names: ["insb" , "insw" , "insd" ]         , rep: null  },
  "outs" : { names: ["outsb", "outsw", "outsd"]         , rep: null  }
};

// ============================================================================
// [tablegen.x86.Filter]
// ============================================================================

class Filter {
  static unique(instArray) {
    const result = [];
    const known = {};

    for (var i = 0; i < instArray.length; i++) {
      const inst = instArray[i];
      if (inst.attributes.AltForm)
        continue;

      const s = inst.operands.map((op) => { return op.isImm() ? "imm" : op.toString(); }).join(", ");
      if (known[s] === true)
        continue;

      known[s] = true;
      result.push(inst);
    }

    return result;
  }

  static noAltForm(instArray) {
    const result = [];
    for (var i = 0; i < instArray.length; i++) {
      const inst = instArray[i];
      if (inst.attributes.AltForm)
        continue;
      result.push(inst);
    }
    return result;
  }

  static byArch(instArray, arch) {
    return instArray.filter(function(inst) {
      return inst.arch === "ANY" || inst.arch === arch;
    });
  }
}

// ============================================================================
// [tablegen.x86.GenUtils]
// ============================================================================

const VexToEvexMap = {
  "vbroadcastf128": "vbroadcastf32x4",
  "vbroadcasti128": "vbroadcasti32x4",
  "vextractf128": "vextractf32x4",
  "vextracti128": "vextracti32x4",
  "vinsertf128": "vinsertf32x4",
  "vinserti128": "vinserti32x4",
  "vmovdqa": "vmovdqa32",
  "vmovdqu": "vmovdqu32",
  "vpand": "vpandd",
  "vpandn": "vpandnd",
  "vpor": "vpord",
  "vpxor": "vpxord",
  "vroundpd": "vrndscalepd",
  "vroundps": "vrndscaleps",
  "vroundsd": "vrndscalesd",
  "vroundss": "vrndscaless"
};

class GenUtils {
  static cpuArchOf(dbInsts) {
    var anyArch = false;
    var x86Arch = false;
    var x64Arch = false;

    for (var i = 0; i < dbInsts.length; i++) {
      const dbInst = dbInsts[i];
      if (dbInst.arch === "ANY") anyArch = true;
      if (dbInst.arch === "X86") x86Arch = true;
      if (dbInst.arch === "X64") x64Arch = true;
    }

    return anyArch || (x86Arch && x64Arch) ? "" : x86Arch ? "(X86)" : "(X64)";
  }

  static cpuFeaturesOf(dbInsts) {
    return ArrayUtils.sorted(dbInsts.unionCpuFeatures());
  }

  static assignVexEvexCompatibilityFlags(f, dbInsts) {
    const vexInsts = dbInsts.filter((inst) => { return inst.prefix === "VEX"; });
    const evexInsts = dbInsts.filter((inst) => { return inst.prefix === "EVEX"; });

    function isCompatible(vexInst, evexInst) {
      if (vexInst.operands.length !== evexInst.operands.length)
        return false;

      for (let i = 0; i < vexInst.operands.length; i++) {
        const vexOp = vexInst.operands[i];
        const evexOp = evexInst.operands[i];

        if (vexOp.data === evexOp.data)
          continue;

        if (vexOp.reg && vexOp.reg === evexOp.reg)
          continue;
        if (vexOp.mem && vexOp.mem === evexOp.mem)
          continue;

        return false;
      }
      return true;
    }

    let compatible = 0;
    for (const vexInst of vexInsts) {
      for (const evexInst of evexInsts) {
        if (isCompatible(vexInst, evexInst)) {
          compatible++;
          break;
        }
      }
    }

    if (compatible == vexInsts.length) {
      f.EvexCompat = true;
      return true;
    }

    if (evexInsts[0].operands[0].reg === "k") {
      f.EvexKReg = true;
      return true;
    }

    if (evexInsts[0].operands.length == 2 && vexInsts[0].operands.length === 3) {
      f.EvexTwoOp = true;
      return true;
    }

    return false;
  }

  static flagsOf(dbInsts) {
    const f = Object.create(null);
    var i, j;

    var mib = dbInsts.length > 0 && /^(?:bndldx|bndstx)$/.test(dbInsts[0].name);
    if (mib)
      f.Mib = true;

    var mmx = false;
    var vec = false;

    for (i = 0; i < dbInsts.length; i++) {
      const dbInst = dbInsts[i];
      const operands = dbInst.operands;

      if (dbInst.name === "emms")
        mmx = true;

      if (dbInst.name === "vzeroall" || dbInst.name === "vzeroupper")
        vec = true;

      for (j = 0; j < operands.length; j++) {
        const op = operands[j];
        if (op.reg === "mm")
          mmx = true;
        else if (/^(xmm|ymm|zmm)$/.test(op.reg)) {
          vec = true;
        }
      }
    }

    if (mmx) f.Mmx = true;
    if (vec) f.Vec = true;

    for (i = 0; i < dbInsts.length; i++) {
      const dbInst = dbInsts[i];
      const operands = dbInst.operands;

      if (dbInst.attributes.Lock           ) f.Lock            = true;
      if (dbInst.attributes.XAcquire       ) f.XAcquire        = true;
      if (dbInst.attributes.XRelease       ) f.XRelease        = true;
      if (dbInst.attributes.BND            ) f.Rep             = true;
      if (dbInst.attributes.REP            ) f.Rep             = true;
      if (dbInst.attributes.REPNE          ) f.Rep             = true;
      if (dbInst.attributes.RepIgnored     ) f.RepIgnored      = true;
      if (dbInst.attributes.ImplicitZeroing) f.Avx512ImplicitZ = true;

      if (dbInst.fpu) {
        for (var j = 0; j < operands.length; j++) {
          const op = operands[j];
          if (op.memSize === 16) f.FpuM16 = true;
          if (op.memSize === 32) f.FpuM32 = true;
          if (op.memSize === 64) f.FpuM64 = true;
          if (op.memSize === 80) f.FpuM80 = true;
        }
      }

      if (dbInst.attributes.Tsib)
        f.Tsib = true;

      if (dbInst.vsibReg)
        f.Vsib = true;

      if (dbInst.prefix === "VEX" || dbInst.prefix === "XOP")
        f.Vex = true;

      if (dbInst.prefix === "EVEX") {
        f.Evex = true;

        if (dbInst.extensions["AVX512_VNNI"])
          f.PreferEvex = true;

        if (dbInst.kmask) f.Avx512K = true;
        if (dbInst.zmask) f.Avx512Z = true;

        if (dbInst.er) f.Avx512ER = true;
        if (dbInst.sae) f.Avx512SAE = true;

        if (dbInst.broadcast) f["Avx512B" + String(dbInst.elementSize)] = true;
        if (dbInst.tupleType === "T1_4X") f.Avx512T4X = true;
      }

      if (VexToEvexMap[dbInst.name])
        f.EvexTransformable = true;
    }

    if (f.Vex && f.Evex) {
      GenUtils.assignVexEvexCompatibilityFlags(f, dbInsts)
    }

    const result = Object.getOwnPropertyNames(f);
    result.sort();
    return result;
  }

  static eqOps(aOps, aFrom, bOps, bFrom) {
    var x = 0;
    for (;;) {
      const aIndex = x + aFrom;
      const bIndex = x + bFrom;

      const aOut = aIndex >= aOps.length;
      const bOut = bIndex >= bOps.length;

      if (aOut || bOut)
        return !!(aOut && bOut);

      const aOp = aOps[aIndex];
      const bOp = bOps[bIndex];

      if (aOp.data !== bOp.data)
        return false;

      x++;
    }
  }

  // Prevent some instructions from having implicit memory size if that would
  // make them ambiguous. There are some instructions where the ambiguity is
  // okay, but some like 'push' and 'pop' where it isn't.
  static canUseImplicitMemSize(name) {
    switch (name) {
      case "pop":
      case "push":
        return false;

      default:
        return true;
    }
  }

  static singleRegCase(name) {
    switch (name) {
      case "xchg"    :

      case "and"     :
      case "pand"    : case "vpand"  : case "vpandd"  : case "vpandq"   :
      case "andpd"   : case "vandpd" :
      case "andps"   : case "vandps" :

      case "or"      :
      case "por"     : case "vpor"   : case "vpord"   : case "vporq"    :
      case "orpd"    : case "vorpd"  :
      case "orps"    : case "vorps"  :

      case "pminsb"  : case "vpminsb": case "pmaxsb"  : case "vpmaxsb"  :
      case "pminsw"  : case "vpminsw": case "pmaxsw"  : case "vpmaxsw"  :
      case "pminsd"  : case "vpminsd": case "pmaxsd"  : case "vpmaxsd"  :
      case "pminub"  : case "vpminub": case "pmaxub"  : case "vpmaxub"  :
      case "pminuw"  : case "vpminuw": case "pmaxuw"  : case "vpmaxuw"  :
      case "pminud"  : case "vpminud": case "pmaxud"  : case "vpmaxud"  :
        return "RO";

      case "pandn"   : case "vpandn" : case "vpandnd" : case "vpandnq"  :

      case "xor"     :
      case "pxor"    : case "vpxor"  : case "vpxord"  : case "vpxorq"   :
      case "xorpd"   : case "vxorpd" :
      case "xorps"   : case "vxorps" :

      case "kxnorb":
      case "kxnord":
      case "kxnorw":
      case "kxnorq":

      case "kxorb":
      case "kxord":
      case "kxorw":
      case "kxorq":

      case "sub"     :
      case "sbb"     :
      case "psubb"   : case "vpsubb" :
      case "psubw"   : case "vpsubw" :
      case "psubd"   : case "vpsubd" :
      case "psubq"   : case "vpsubq" :
      case "psubsb"  : case "vpsubsb": case "psubusb" : case "vpsubusb" :
      case "psubsw"  : case "vpsubsw": case "psubusw" : case "vpsubusw" :

      case "vpcmpeqb": case "pcmpeqb": case "vpcmpgtb": case "pcmpgtb"  :
      case "vpcmpeqw": case "pcmpeqw": case "vpcmpgtw": case "pcmpgtw"  :
      case "vpcmpeqd": case "pcmpeqd": case "vpcmpgtd": case "pcmpgtd"  :
      case "vpcmpeqq": case "pcmpeqq": case "vpcmpgtq": case "pcmpgtq"  :

      case "vpcmpb"  : case "vpcmpub":
      case "vpcmpd"  : case "vpcmpud":
      case "vpcmpw"  : case "vpcmpuw":
      case "vpcmpq"  : case "vpcmpuq":
        return "WO";

      default:
        return "None";
    }
  }

  static fixedRegOf(reg) {
    switch (reg) {
      case "es"  : return 1;
      case "cs"  : return 2;
      case "ss"  : return 3;
      case "ds"  : return 4;
      case "fs"  : return 5;
      case "gs"  : return 6;
      case "ah"  : return 0;
      case "ch"  : return 1;
      case "dh"  : return 2;
      case "bh"  : return 3;
      case "al"  : case "ax": case "eax": case "rax": case "zax": return 0;
      case "cl"  : case "cx": case "ecx": case "rcx": case "zcx": return 1;
      case "dl"  : case "dx": case "edx": case "rdx": case "zdx": return 2;
      case "bl"  : case "bx": case "ebx": case "rbx": case "zbx": return 3;
      case "spl" : case "sp": case "esp": case "rsp": case "zsp": return 4;
      case "bpl" : case "bp": case "ebp": case "rbp": case "zbp": return 5;
      case "sil" : case "si": case "esi": case "rsi": case "zsi": return 6;
      case "dil" : case "di": case "edi": case "rdi": case "zdi": return 7;
      case "st0" : return 0;
      case "xmm0": return 0;
      case "ymm0": return 0;
      case "zmm0": return 0;
      default:
        return -1;
    }
  }

  static controlFlow(dbInsts) {
    if (dbInsts.checkAttribute("Control", "Jump")) return "Jump";
    if (dbInsts.checkAttribute("Control", "Call")) return "Call";
    if (dbInsts.checkAttribute("Control", "Branch")) return "Branch";
    if (dbInsts.checkAttribute("Control", "Return")) return "Return";
    return "Regular";
  }
}

// ============================================================================
// [tablegen.x86.X86TableGen]
// ============================================================================

class X86TableGen extends core.TableGen {
  constructor() {
    super("X86");

    this.emitMissingString = "";
  }

  // --------------------------------------------------------------------------
  // [Query]
  // --------------------------------------------------------------------------

  // Get instructions (dbInsts) having the same name as understood by AsmJit.
  query(name) {
    const remapped = RemappedInsts[name];
    if (!remapped) return x86isa.query(name);

    const dbInsts = x86isa.query(remapped.names);
    const rep = remapped.rep;
    if (rep === null) return dbInsts;

    return dbInsts.filter((inst) => {
      return rep === !!(inst.attributes.REP || inst.attributes.REPNE);
    });
  }

  // --------------------------------------------------------------------------
  // [Parse / Merge]
  // --------------------------------------------------------------------------

  parse() {
    const data = this.dataOfFile("src/asmjit/x86/x86instdb.cpp");
    const re = new RegExp(
      "INST\\(" +
        "([A-Za-z0-9_]+)\\s*"              + "," +  // [01] Instruction.
        "([^,]+)"                          + "," +  // [02] Encoding.
        "(.{26}[^,]*)"                     + "," +  // [03] Opcode[0].
        "(.{26}[^,]*)"                     + "," +  // [04] Opcode[1].
        // --- autogenerated fields ---
        "([^\\)]+)"                        + "," +  // [05] MainOpcodeIndex.
        "([^\\)]+)"                        + "," +  // [06] AltOpcodeIndex.
        "([^\\)]+)"                        + "," +  // [07] NameIndex.
        "([^\\)]+)"                        + "," +  // [08] CommonDataIndex.
        "([^\\)]+)"                        + "\\)", // [09] OperationDataIndex.
      "g");

    var m;
    while ((m = re.exec(data)) !== null) {
      var enum_       = m[1];
      var name        = enum_ === "None" ? "" : enum_.toLowerCase();
      var encoding    = m[2].trim();
      var opcode0     = m[3].trim();
      var opcode1     = m[4].trim();

      const dbInsts = this.query(name);
      if (name && !dbInsts.length)
        FAIL(`Instruction '${name}' not found in asmdb`);

      const flags         = GenUtils.flagsOf(dbInsts);
      const controlFlow   = GenUtils.controlFlow(dbInsts);
      const singleRegCase = GenUtils.singleRegCase(name);

      this.addInst({
        id                 : 0,             // Instruction id (numeric value).
        name               : name,          // Instruction name.
        enum               : enum_,         // Instruction enum without `kId` prefix.
        dbInsts            : dbInsts,       // All dbInsts returned from asmdb query.
        encoding           : encoding,      // Instruction encoding.
        opcode0            : opcode0,       // Primary opcode.
        opcode1            : opcode1,       // Secondary opcode.
        flags              : flags,
        signatures         : null,          // Instruction signatures.
        controlFlow        : controlFlow,
        singleRegCase      : singleRegCase,

        mainOpcodeValue    : -1,            // Main opcode value (0.255 hex).
        mainOpcodeIndex    : -1,            // Index to InstDB::_mainOpcodeTable.
        altOpcodeIndex     : -1,            // Index to InstDB::_altOpcodeTable.
        nameIndex          : -1,            // Index to InstDB::_nameData.
        commonInfoIndex    : -1,
        additionalInfoIndex: -1,

        signatureIndex     : -1,
        signatureCount     : -1
      });
    }

    if (this.insts.length === 0)
      FAIL("X86TableGen.parse(): Invalid parsing regexp (no data parsed)");

    console.log("Number of Instructions: " + this.insts.length);
  }

  merge() {
    var s = StringUtils.format(this.insts, "", true, function(inst) {
      return "INST(" +
        String(inst.enum               ).padEnd(17) + ", " +
        String(inst.encoding           ).padEnd(19) + ", " +
        String(inst.opcode0            ).padEnd(26) + ", " +
        String(inst.opcode1            ).padEnd(26) + ", " +
        String(inst.mainOpcodeIndex    ).padEnd( 3) + ", " +
        String(inst.altOpcodeIndex     ).padEnd( 3) + ", " +
        String(inst.nameIndex          ).padEnd( 5) + ", " +
        String(inst.commonInfoIndex    ).padEnd( 3) + ", " +
        String(inst.additionalInfoIndex).padEnd( 3) + ")";
    }) + "\n";
    this.inject("InstInfo", s, this.insts.length * 8);
  }

  // --------------------------------------------------------------------------
  // [Other]
  // --------------------------------------------------------------------------

  printMissing() {
    const ignored = MapUtils.arrayToMap([
      "cmpsb", "cmpsw", "cmpsd", "cmpsq",
      "lodsb", "lodsw", "lodsd", "lodsq",
      "movsb", "movsw", "movsd", "movsq",
      "scasb", "scasw", "scasd", "scasq",
      "stosb", "stosw", "stosd", "stosq",
      "insb" , "insw" , "insd" ,
      "outsb", "outsw", "outsd",
      "wait" // Maps to `fwait`, which AsmJit uses instead.
    ]);

    var out = "";
    x86isa.instructionNames.forEach(function(name) {
      var dbInsts = x86isa.query(name);
      if (!this.instMap[name] && ignored[name] !== true) {
        console.log(`MISSING INSTRUCTION '${name}'`);
        var inst = this.newInstFromGroup(dbInsts);
        if (inst) {
          out += "  INST(" +
            String(inst.enum      ).padEnd(17) + ", " +
            String(inst.encoding  ).padEnd(19) + ", " +
            String(inst.opcode0   ).padEnd(26) + ", " +
            String(inst.opcode1   ).padEnd(26) + ", " +
            String("0"            ).padEnd( 3) + ", " +
            String("0"            ).padEnd( 3) + ", " +
            String("0"            ).padEnd( 5) + ", " +
            String("0"            ).padEnd( 3) + ", " +
            String("0"            ).padEnd( 3) + "),\n";
        }
      }
    }, this);
    console.log(out);
    console.log(this.emitMissingString);
  }

  newInstFromGroup(dbInsts) {
    function composeOpCode(obj) {
      return `${obj.type}(${obj.prefix},${obj.opcode},${obj.o},${obj.l},${obj.w},${obj.ew},${obj.en},${obj.tt})`;
    }

    function GetAccess(dbInst) {
      var operands = dbInst.operands;
      if (!operands.length) return "";

      var op = operands[0];
      if (op.read && op.write)
        return "RW";
      else if (op.read)
        return "RO";
      else
        return "WO";
    }

    function isVecPrefix(s) {
      return s === "VEX" || s === "EVEX" || s === "XOP";
    }

    function formatEmit(dbi) {
      const results = [];
      const nameUp = dbi.name[0].toUpperCase() + dbi.name.substr(1);

      for (let choice = 0; choice < 2; choice++) {
        let s = `ASMJIT_INST_${dbi.operands.length}x(${dbi.name}, ${nameUp}`;
        for (let j = 0; j < dbi.operands.length; j++) {
          s += ", ";
          const op = dbi.operands[j];
          var reg = op.reg;
          var mem = op.mem;

          if (op.isReg() && op.isMem()) {
            if (choice == 0) mem = null;
            if (choice == 1) reg = null;
          }

          if (reg) {
            if (reg === "xmm" || reg === "ymm" || reg === "zmm")
              s += "Vec";
            else if (reg === "k")
              s += "KReg";
            else if (reg === "r32" || reg === "r64" || reg === "r16" || reg === "r8")
              s += "Gp";
            else
              s += reg;
          }
          else if (mem) {
            s += "Mem";
          }
          else if (op.isImm()) {
            s += "Imm";
          }
          else {
            s += "Unknown";
          }
        }
        s += `)`;
        results.push(s);
      }

      return results;
    }

    var dbi = dbInsts[0];

    var id = this.insts.length;
    var name = dbi.name;
    var enum_ = name[0].toUpperCase() + name.substr(1);

    var opcode = dbi.opcodeHex;
    var modR = dbi.modR;
    var mm = dbi.mm;
    var pp = dbi.pp;
    var encoding = dbi.encoding;
    var isVec = isVecPrefix(dbi.prefix);
    var evexCount = 0;

    var access = GetAccess(dbi);

    var vexL = undefined;
    var vexW = undefined;
    var evexW = undefined;
    var cdshl = "_";
    var tupleType = "_";

    const tupleTypeToCDSHL = {
      "FVM": "4",
      "FV": "4",
      "HVM": "3",
      "HV": "3",
      "QVM": "2",
      "QV": "2",
      "T1S": "?"
    }

    const emitMap = {};

    for (var i = 0; i < dbInsts.length; i++) {
      dbi = dbInsts[i];

      if (dbi.prefix === "VEX" || dbi.prefix === "XOP") {
        var newVexL = String(dbi.l === "128" ? 0 : dbi.l === "256" ? 1 : dbi.l === "512" ? 2 : "_");
        var newVexW = String(dbi.w === "W0" ? 0 : dbi.w === "W1" ? 1 : "_");

        if (vexL !== undefined && vexL !== newVexL)
          vexL = "x";
        else
          vexL = newVexL;
        if (vexW !== undefined && vexW !== newVexW)
          vexW = "x";
        else
          vexW = newVexW;
      }

      if (dbi.prefix === "EVEX") {
        evexCount++;
        var newEvexW = String(dbi.w === "W0" ? 0 : dbi.w === "W1" ? 1 : "_");
        if (evexW !== undefined && evexW !== newEvexW)
          evexW = "x";
        else
          evexW = newEvexW;

        if (dbi.tupleType) {
          if (tupleType !== "_" && tupleType !== dbi.tupleType) {
            console.log(`${dbi.name}: WARNING: TupleType ${tupleType} != ${dbi.tupleType}`);
          }

          tupleType = dbi.tupleType;
        }
      }

      if (opcode   !== dbi.opcodeHex ) { console.log(`${dbi.name}: ISSUE: Opcode ${opcode} != ${dbi.opcodeHex}`); return null; }
      if (modR     !== dbi.modR      ) { console.log(`${dbi.name}: ISSUE: ModR ${modR} != ${dbi.modR}`); return null; }
      if (mm       !== dbi.mm        ) { console.log(`${dbi.name}: ISSUE: MM ${mm} != ${dbi.mm}`); return null; }
      if (pp       !== dbi.pp        ) { console.log(`${dbi.name}: ISSUE: PP ${pp} != ${dbi.pp}`); return null; }
      if (encoding !== dbi.encoding  ) { console.log(`${dbi.name}: ISSUE: Enc ${encoding} != ${dbi.encoding}`); return null; }
      if (access   !== GetAccess(dbi)) { console.log(`${dbi.name}: ISSUE: Access ${access} != ${GetAccess(dbi)}`); return null; }
      if (isVec    != isVecPrefix(dbi.prefix)) { console.log(`${dbi.name}: ISSUE: Vex/Non-Vex mismatch`); return null; }

      formatEmit(dbi).forEach((emit) => {
        if (!emitMap[emit]) {
          emitMap[emit] = true;
          this.emitMissingString += emit + "\n";
        }
      });
    }

    if (tupleType !== "_")
      cdshl = tupleTypeToCDSHL[tupleType] || "?";

    var ppmm = pp.padEnd(2).replace(/ /g, "0") +
               mm.padEnd(4).replace(/ /g, "0") ;

    var composed = composeOpCode({
      type  : evexCount == dbInsts.length ? "E" : isVec ? "V" : "O",
      prefix: ppmm,
      opcode: opcode,
      o     : modR === "r" ? "_" : (modR ? modR : "_"),
      l     : vexL !== undefined ? vexL : "_",
      w     : vexW !== undefined ? vexW : "_",
      ew    : evexW !== undefined ? evexW : "_",
      en    : cdshl,
      tt    : dbi.modRM ? dbi.modRM + "  " : tupleType.padEnd(3)
    });

    return {
      id                 : id,
      name               : name,
      enum               : enum_,
      encoding           : encoding,
      opcode0            : composed,
      opcode1            : "0",
      nameIndex          : -1,
      commonInfoIndex    : -1,
      additionalInfoIndex: -1
    };
  }

  // --------------------------------------------------------------------------
  // [Hooks]
  // --------------------------------------------------------------------------

  onBeforeRun() {
    this.load([
      "src/asmjit/x86/x86globals.h",
      "src/asmjit/x86/x86instdb.cpp",
      "src/asmjit/x86/x86instdb.h",
      "src/asmjit/x86/x86instdb_p.h"
    ]);
    this.parse();
  }

  onAfterRun() {
    this.merge();
    this.save();
    this.dumpTableSizes();
    this.printMissing();
  }
}

// ============================================================================
// [tablegen.x86.IdEnum]
// ============================================================================

class IdEnum extends core.IdEnum {
  constructor() {
    super("IdEnum");
  }

  comment(inst) {
    function filterAVX(features, avx) {
      return features.filter(function(item) { return /^(AVX|FMA)/.test(item) === avx; });
    }

    var dbInsts = inst.dbInsts;
    if (!dbInsts.length) return "Invalid instruction id.";

    var text = "";
    var features = GenUtils.cpuFeaturesOf(dbInsts);

    const priorityFeatures = ["AVX_VNNI"];

    if (features.length) {
      text += "{";
      const avxFeatures = filterAVX(features, true);
      const otherFeatures = filterAVX(features, false);

      for (const pf of priorityFeatures) {
        const index = avxFeatures.indexOf(pf);
        if (index != -1) {
          avxFeatures.splice(index, 1);
          avxFeatures.unshift(pf);
        }
      }

      const vl = avxFeatures.indexOf("AVX512_VL");
      if (vl !== -1) avxFeatures.splice(vl, 1);

      const fma = avxFeatures.indexOf("FMA");
      if (fma !== -1) { avxFeatures.splice(fma, 1); avxFeatures.splice(0, 0, "FMA"); }

      text += avxFeatures.join("|");
      if (vl !== -1) text += "+VL";

      if (otherFeatures.length)
        text += (avxFeatures.length ? " & " : "") + otherFeatures.join("|");

      text += "}";
    }

    var arch = GenUtils.cpuArchOf(dbInsts);
    if (arch)
      text += (text ? " " : "") + arch;

    return `Instruction '${inst.name}'${(text ? " " + text : "")}.`;
  }
}

// ============================================================================
// [tablegen.x86.NameTable]
// ============================================================================

class NameTable extends core.NameTable {
  constructor() {
    super("NameTable");
  }
}

// ============================================================================
// [tablegen.x86.AltOpcodeTable]
// ============================================================================

class AltOpcodeTable extends core.Task {
  constructor() {
    super("AltOpcodeTable");
  }

  run() {
    const insts = this.ctx.insts;

    const mainOpcodeTable = new IndexedArray();
    const altOpcodeTable = new IndexedArray();

    const cdttSimplification = {
      "0"    : "None",
      "_"    : "None",
      "FV"   : "ByLL",
      "HV"   : "ByLL",
      "QV"   : "ByLL",
      "FVM"  : "ByLL",
      "T1S"  : "None",
      "T1F"  : "None",
      "T1_4X": "None",
      "T2"   : "None",
      "T4"   : "None",
      "T8"   : "None",
      "HVM"  : "ByLL",
      "QVM"  : "ByLL",
      "OVM"  : "ByLL",
      "128"  : "None",
      "T4X"  : "None"
    }

    const noOp = "O(000000,00,0,0,0,0,0,0   )";

    mainOpcodeTable.addIndexed(noOp);

    function splitOpcodeToComponents(opcode) {
      const i = opcode.indexOf("(");
      const prefix = opcode.substr(0, i);
      return [prefix].concat(opcode.substring(i + 1, opcode.length - 1).split(","));
    }

    function normalizeOpcodeComponents(components) {
      for (let i = 1; i < components.length; i++) {
        components[i] = components[i].trim();
        // These all are zeros that only have some contextual meaning in the table, but the assembler doesn't care.
        if (components[i] === "_" || components[i] === "I" || components[i] === "x")
          components[i] = "0";
      }

      // Simplify CDTT (compressed displacement TupleType).
      if (components.length >= 9) {
        if (components[0] === "V" || components[0] === "E") {
          const cdtt = components[8];
          if (cdttSimplification[cdtt] !== undefined)
            components[8] = cdttSimplification[cdtt];
        }
      }
      return components;
    }

    function joinOpcodeComponents(components) {
      const prefix = components[0];
      const values = components.slice(1);
      if (values.length >= 8)
        values[7] = values[7].padEnd(4);
      return prefix + "(" + values.join(",") + ")";
    }

    function indexMainOpcode(opcode) {
      if (opcode === "0")
        return ["00", 0];

      var opcodeByte = "";
      const components = normalizeOpcodeComponents(splitOpcodeToComponents(opcode));

      if (components[0] === "O_FPU") {
        // Reset opcode byte, this is stored in the instruction data itself.
        opcodeByte = components[2].substr(2, 2);
        components[2] = components[2].substr(0, 2) + "00";
      }
      else if (components[0] === "O" || components[0] === "V" || components[0] === "E") {
        // Reset opcode byte, this is stored in the instruction data itself.
        opcodeByte = components[2];
        components[2] = "00";
      }
      else {
        FAIL(`Failed to process opcode '${opcode}'`);
      }

      const newOpcode = joinOpcodeComponents(components);
      return [opcodeByte, mainOpcodeTable.addIndexed(newOpcode.padEnd(27))];
    }

    function indexAltOpcode(opcode) {
      if (opcode === "0")
        opcode = noOp;
      else
        opcode = joinOpcodeComponents(normalizeOpcodeComponents(splitOpcodeToComponents(opcode)));
      return altOpcodeTable.addIndexed(opcode.padEnd(27));
    }

    insts.map((inst) => {
      const [value, index] = indexMainOpcode(inst.opcode0);
      inst.mainOpcodeValue = value;
      inst.mainOpcodeIndex = index;
      inst.altOpcodeIndex = indexAltOpcode(inst.opcode1);
    });

    // console.log(mainOpcodeTable.length);
    // console.log(StringUtils.format(mainOpcodeTable, kIndent, true));

    this.inject("MainOpcodeTable",
                disclaimer(`const uint32_t InstDB::_mainOpcodeTable[] = {\n${StringUtils.format(mainOpcodeTable, kIndent, true)}\n};\n`),
                mainOpcodeTable.length * 4);

    this.inject("AltOpcodeTable",
                disclaimer(`const uint32_t InstDB::_altOpcodeTable[] = {\n${StringUtils.format(altOpcodeTable, kIndent, true)}\n};\n`),
                altOpcodeTable.length * 4);
  }
}

// ============================================================================
// [tablegen.x86.InstSignatureTable]
// ============================================================================

const RegOp = MapUtils.arrayToMap(["al", "ah", "ax", "eax", "rax", "cl", "r8lo", "r8hi", "r16", "r32", "r64", "xmm", "ymm", "zmm", "mm", "k", "sreg", "creg", "dreg", "st", "bnd"]);
const MemOp = MapUtils.arrayToMap(["m8", "m16", "m32", "m48", "m64", "m80", "m128", "m256", "m512", "m1024"]);

const cmpOp = StringUtils.makePriorityCompare([
  "RegGpbLo", "RegGpbHi", "RegGpw", "RegGpd", "RegGpq", "RegXmm", "RegYmm", "RegZmm", "RegMm", "RegKReg", "RegSReg", "RegCReg", "RegDReg", "RegSt", "RegBnd", "RegTmm",
  "MemUnspecified", "Mem8", "Mem16", "Mem32", "Mem48", "Mem64", "Mem80", "Mem128", "Mem256", "Mem512", "Mem1024",
  "Vm32x", "Vm32y", "Vm32z", "Vm64x", "Vm64y", "Vm64z",
  "ImmI4", "ImmU4", "ImmI8", "ImmU8", "ImmI16", "ImmU16", "ImmI32", "ImmU32", "ImmI64", "ImmU64",
  "Rel8", "Rel32",
  "FlagMemBase",
  "FlagMemDs",
  "FlagMemEs",
  "FlagMib",
  "FlagTMem",
  "FlagConsecutive",
  "FlagImplicit"
]);

function StringifyOpArray(a, map) {
  var s = "";
  for (var i = 0; i < a.length; i++) {
    const op = a[i];
    var mapped = null;
    if (typeof map === "function")
      mapped = map(op);
    else if (hasOwn.call(map, op))
      mapped = map[op];
    else
      FAIL(`UNHANDLED OPERAND '${op}'`);
    s += (s ? " | " : "") + mapped;
  }
  return s ? s : "0";
}

class OSignature {
  constructor() {
    this.flags = Object.create(null);
  }

  equals(other) {
    return MapUtils.equals(this.flags, other.flags);
  }

  xor(other) {
    const result = MapUtils.xor(this.flags, other.flags);
    return Object.getOwnPropertyNames(result).length === 0 ? null : result;
  }

  mergeWith(other) {
    const af = this.flags;
    const bf = other.flags;

    var k;
    var indexKind = "";
    var hasReg = false;

    for (k in af) {
      const index = asmdb.x86.Utils.regIndexOf(k);
      const kind = asmdb.x86.Utils.regKindOf(k);

      if (kind)
        hasReg = true;

      if (index !== null && index !== -1)
        indexKind = kind;
    }

    if (hasReg) {
      for (k in bf) {
        const index = asmdb.x86.Utils.regIndexOf(k);
        if (index !== null && index !== -1) {
          const kind = asmdb.x86.Utils.regKindOf(k);
          if (indexKind !== kind)
            return false;
        }
      }
    }

    // Can merge...
    for (k in bf)
      af[k] = true;
    return true;
  }

  toString() {
    var s = "";
    var flags = this.flags;

    for (var k in flags) {
      if (k === "read" || k === "write" || k === "implicit" || k === "memDS" || k === "memES")
        continue;

      var x = k;
      if (x === "memZAX") x = "zax";
      if (x === "memZDI") x = "zdi";
      if (x === "memZSI") x = "zsi";
      s += (s ? "|" : "") + x;
    }

    if (flags.memDS) s = "ds:[" + s + "]";
    if (flags.memES) s = "es:[" + s + "]";

    if (flags.implicit)
      s = "<" + s + ">";

    return s;
  }

  toAsmJitOpData() {
    var opFlags = Object.create(null);
    var regMask = 0;

    for (var k in this.flags) {
      switch (k) {
        case "r8lo"    : opFlags.RegGpbLo = true; break;
        case "r8hi"    : opFlags.RegGpbHi = true; break;
        case "r16"     : opFlags.RegGpw = true; break;
        case "r32"     : opFlags.RegGpd = true; break;
        case "r64"     : opFlags.RegGpq = true; break;
        case "creg"    : opFlags.RegCReg = true; break;
        case "dreg"    : opFlags.RegDReg = true; break;
        case "sreg"    : opFlags.RegSReg = true; break;
        case "bnd"     : opFlags.RegBnd = true; break;
        case "st"      : opFlags.RegSt = true; break;
        case "k"       : opFlags.RegKReg = true; break;
        case "mm"      : opFlags.RegMm = true; break;
        case "xmm"     : opFlags.RegXmm = true; break;
        case "ymm"     : opFlags.RegYmm = true; break;
        case "zmm"     : opFlags.RegZmm = true; break;
        case "tmm"     : opFlags.RegTmm = true; break;

        case "m8"      : opFlags.Mem8 = true; break;
        case "m16"     : opFlags.Mem16 = true; break;
        case "m32"     : opFlags.Mem32 = true; break;
        case "m48"     : opFlags.Mem48 = true; break;
        case "m64"     : opFlags.Mem64 = true; break;
        case "m80"     : opFlags.Mem80 = true; break;
        case "m128"    : opFlags.Mem128 = true; break;
        case "m256"    : opFlags.Mem256 = true; break;
        case "m512"    : opFlags.Mem512 = true; break;
        case "m1024"   : opFlags.Mem1024 = true; break;

        case "mem"     : opFlags.MemUnspecified = true; break;
        case "mib"     : opFlags.MemUnspecified = true; opFlags.FlagMib = true; break;
        case "tmem"    : opFlags.MemUnspecified = true; opFlags.FlagTMem = true; break;

        case "memBase" : opFlags.FlagMemBase = true; break;
        case "memDS"   : opFlags.FlagMemDs = true; break;
        case "memES"   : opFlags.FlagMemEs = true; break;
        case "memZAX"  : regMask |= 1 << 0; break;
        case "memZSI"  : regMask |= 1 << 6; break;
        case "memZDI"  : regMask |= 1 << 7; break;

        case "vm32x"   : opFlags.Vm32x = true; break;
        case "vm32y"   : opFlags.Vm32y = true; break;
        case "vm32z"   : opFlags.Vm32z = true; break;
        case "vm64x"   : opFlags.Vm64x = true; break;
        case "vm64y"   : opFlags.Vm64y = true; break;
        case "vm64z"   : opFlags.Vm64z = true; break;

        case "i4"      : opFlags.ImmI4 = true; break;
        case "u4"      : opFlags.ImmU4 = true; break;
        case "i8"      : opFlags.ImmI8 = true; break;
        case "u8"      : opFlags.ImmU8 = true; break;
        case "i16"     : opFlags.ImmI16 = true; break;
        case "u16"     : opFlags.ImmU16 = true; break;
        case "i32"     : opFlags.ImmI32 = true; break;
        case "u32"     : opFlags.ImmU32 = true; break;
        case "i64"     : opFlags.ImmI64 = true; break;
        case "u64"     : opFlags.ImmU64 = true; break;

        case "rel8"    : opFlags.ImmI32 = true; opFlags.ImmI64 = true; opFlags.Rel8  = true; break;
        case "rel16"   : opFlags.ImmI32 = true; opFlags.ImmI64 = true; opFlags.Rel32 = true; break;
        case "rel32"   : opFlags.ImmI32 = true; opFlags.ImmI64 = true; opFlags.Rel32 = true; break;

        case "es"      : opFlags.RegSReg  = true; regMask |= 1 << 1; break;
        case "cs"      : opFlags.RegSReg  = true; regMask |= 1 << 2; break;
        case "ss"      : opFlags.RegSReg  = true; regMask |= 1 << 3; break;
        case "ds"      : opFlags.RegSReg  = true; regMask |= 1 << 4; break;
        case "fs"      : opFlags.RegSReg  = true; regMask |= 1 << 5; break;
        case "gs"      : opFlags.RegSReg  = true; regMask |= 1 << 6; break;
        case "al"      : opFlags.RegGpbLo = true; regMask |= 1 << 0; break;
        case "ah"      : opFlags.RegGpbHi = true; regMask |= 1 << 0; break;
        case "ax"      : opFlags.RegGpw   = true; regMask |= 1 << 0; break;
        case "eax"     : opFlags.RegGpd   = true; regMask |= 1 << 0; break;
        case "rax"     : opFlags.RegGpq   = true; regMask |= 1 << 0; break;
        case "cl"      : opFlags.RegGpbLo = true; regMask |= 1 << 1; break;
        case "ch"      : opFlags.RegGpbHi = true; regMask |= 1 << 1; break;
        case "cx"      : opFlags.RegGpw   = true; regMask |= 1 << 1; break;
        case "ecx"     : opFlags.RegGpd   = true; regMask |= 1 << 1; break;
        case "rcx"     : opFlags.RegGpq   = true; regMask |= 1 << 1; break;
        case "dl"      : opFlags.RegGpbLo = true; regMask |= 1 << 2; break;
        case "dh"      : opFlags.RegGpbHi = true; regMask |= 1 << 2; break;
        case "dx"      : opFlags.RegGpw   = true; regMask |= 1 << 2; break;
        case "edx"     : opFlags.RegGpd   = true; regMask |= 1 << 2; break;
        case "rdx"     : opFlags.RegGpq   = true; regMask |= 1 << 2; break;
        case "bl"      : opFlags.RegGpbLo = true; regMask |= 1 << 3; break;
        case "bh"      : opFlags.RegGpbHi = true; regMask |= 1 << 3; break;
        case "bx"      : opFlags.RegGpw   = true; regMask |= 1 << 3; break;
        case "ebx"     : opFlags.RegGpd   = true; regMask |= 1 << 3; break;
        case "rbx"     : opFlags.RegGpq   = true; regMask |= 1 << 3; break;
        case "si"      : opFlags.RegGpw   = true; regMask |= 1 << 6; break;
        case "esi"     : opFlags.RegGpd   = true; regMask |= 1 << 6; break;
        case "rsi"     : opFlags.RegGpq   = true; regMask |= 1 << 6; break;
        case "di"      : opFlags.RegGpw   = true; regMask |= 1 << 7; break;
        case "edi"     : opFlags.RegGpd   = true; regMask |= 1 << 7; break;
        case "rdi"     : opFlags.RegGpq   = true; regMask |= 1 << 7; break;
        case "st0"     : opFlags.RegSt    = true; regMask |= 1 << 0; break;
        case "xmm0"    : opFlags.RegXmm   = true; regMask |= 1 << 0; break;
        case "ymm0"    : opFlags.RegYmm   = true; regMask |= 1 << 0; break;

        case "implicit": opFlags.FlagImplicit = true; break;

        default:
          console.log(`UNKNOWN OPERAND '${k}'`);
      }
    }

    const outputFlags = StringifyOpArray(ArrayUtils.sorted(opFlags, cmpOp), function(k) { return `F(${k})`; });
    return `ROW(${outputFlags || 0}, ${decToHex(regMask, 2)})`;
  }
}

class ISignature extends Array {
  constructor(name) {
    super();
    this.name = name;
    this.x86 = false;
    this.x64 = false;
    this.implicit = 0; // Number of implicit operands.
  }

  opEquals(other) {
    const len = this.length;
    if (len !== other.length) return false;

    for (var i = 0; i < len; i++)
      if (!this[i].equals(other[i]))
        return false;

    return true;
  }

  mergeWith(other) {
    // If both architectures are the same, it's fine to merge.
    var ok = this.x86 === other.x86 && this.x64 === other.x64;

    // If the first arch is [X86|X64] and the second [X64] it's also fine.
    if (!ok && this.x86 && this.x64 && !other.x86 && other.x64)
      ok = true;

    // It's not ok if both signatures have different number of implicit operands.
    if (!ok || this.implicit !== other.implicit)
      return false;

    // It's not ok if both signatures have different number of operands.
    const len = this.length;
    if (len !== other.length)
      return false;

    var xorIndex = -1;
    for (var i = 0; i < len; i++) {
      const xor = this[i].xor(other[i]);
      if (xor === null) continue;

      if (xorIndex === -1)
        xorIndex = i;
      else
        return false;
    }

    // Bail if mergeWidth at operand-level failed.
    if (xorIndex !== -1 && !this[xorIndex].mergeWith(other[xorIndex]))
      return false;

    this.x86 = this.x86 || other.x86;
    this.x64 = this.x64 || other.x64;

    return true;
  }

  toString() {
    return "{" + this.join(", ") + "}";
  }
}

class SignatureArray extends Array {
  // Iterate over all signatures and check which operands don't need explicit memory size.
  calcImplicitMemSize() {
    // Calculates a hash-value (aka key) of all register operands specified by `regOps` in `inst`.
    function keyOf(inst, regOps) {
      var s = "";
      for (var i = 0; i < inst.length; i++) {
        const op = inst[i];
        if (regOps & (1 << i))
          s += "{" + ArrayUtils.sorted(MapUtils.and(op.flags, RegOp)).join("|") + "}";
      }
      return s || "?";
    }

    var i;
    var aIndex, bIndex;

    for (aIndex = 0; aIndex < this.length; aIndex++) {
      const aInst = this[aIndex];
      const len = aInst.length;

      var memOp = "";
      var memPos = -1;
      var regOps = 0;

      // Check if this instruction signature has a memory operand of explicit size.
      for (i = 0; i < len; i++) {
        const aOp = aInst[i];
        const mem = MapUtils.firstOf(aOp.flags, MemOp);

        if (mem) {
          // Stop if the memory operand has implicit-size or if there is more than one.
          if (aOp.flags.mem || memPos >= 0) {
            memPos = -1;
            break;
          }
          else {
            memOp = mem;
            memPos = i;
          }
        }
        else if (MapUtils.anyOf(aOp.flags, RegOp)) {
          // Doesn't consider 'r/m' as we already checked 'm'.
          regOps |= (1 << i);
        }
      }

      if (memPos < 0)
        continue;

      // Create a `sameSizeSet` set of all instructions having the exact
      // explicit memory operand at the same position and registers at
      // positions matching `regOps` bits and `diffSizeSet` having memory
      // operand of different size, but registers at the same positions.
      const sameSizeSet = [aInst];
      const diffSizeSet = [];
      const diffSizeHash = Object.create(null);

      for (bIndex = 0; bIndex < this.length; bIndex++) {
        const bInst = this[bIndex];
        if (aIndex === bIndex || len !== bInst.length) continue;

        var hasMatch = 1;
        for (i = 0; i < len; i++) {
          if (i === memPos) continue;

          const reg = MapUtils.anyOf(bInst[i].flags, RegOp);
          if (regOps & (1 << i))
            hasMatch &= reg;
          else if (reg)
            hasMatch = 0;
        }

        if (hasMatch) {
          const bOp = bInst[memPos];
          if (bOp.flags.mem) continue;

          const mem = MapUtils.firstOf(bOp.flags, MemOp);
          if (mem === memOp) {
            sameSizeSet.push(bInst);
          }
          else if (mem) {
            const key = keyOf(bInst, regOps);
            diffSizeSet.push(bInst);
            if (!diffSizeHash[key])
              diffSizeHash[key] = [bInst];
            else
              diffSizeHash[key].push(bInst);
          }
        }
      }

      // Two cases.
      //   A) The memory operand has implicit-size if `diffSizeSet` is empty. That
      //      means that the instruction only uses one size for all reg combinations.
      //
      //   B) The memory operand has implicit-size if `diffSizeSet` contains different
      //      register signatures than `sameSizeSet`.
      var implicit = true;

      if (!diffSizeSet.length) {
        // Case A:
      }
      else {
        // Case B: Find collisions in `sameSizeSet` and `diffSizeSet`.
        for (bIndex = 0; bIndex < sameSizeSet.length; bIndex++) {
          const bInst = sameSizeSet[bIndex];
          const key = keyOf(bInst, regOps);

          const diff = diffSizeHash[key];
          if (diff) {
            diff.forEach((diffInst) => {
              if ((bInst.x86 && !diffInst.x86) || (!bInst.x86 && diffInst.x86)) {
                // If this is X86|ANY instruction and the other is X64, or vice-versa,
                // then keep this implicit as it won't do any harm. These instructions
                // cannot be mixed and it will make implicit the 32-bit one in cases
                // where X64 introduced 64-bit ones like `cvtsi2ss`.
              }
              else {
                implicit = false;
              }
            });
          }
        }
      }

      // Patch all instructions to accept implicit-size memory operand.
      for (bIndex = 0; bIndex < sameSizeSet.length; bIndex++) {
        const bInst = sameSizeSet[bIndex];
        if (implicit)
          bInst[memPos].flags.mem = true;

        if (!implicit)
          DEBUG(`${this.name}: Explicit: ${bInst}`);
      }
    }
  }

  compact() {
    var didSomething = true;
    while (didSomething) {
      didSomething = false;
      for (var i = 0; i < this.length; i++) {
        var row = this[i];
        var j = i + 1;
        while (j < this.length) {
          if (row.mergeWith(this[j])) {
            this.splice(j, 1);
            didSomething = true;
            continue;
          }
          j++;
        }
      }
    }
  }

  toString() {
    return `[${this.join(", ")}]`;
  }
}

class InstSignatureTable extends core.Task {
  constructor() {
    super("InstSignatureTable");
    this.maxOpRows = 0;
  }

  run() {
    const insts = this.ctx.insts;

    insts.forEach((inst) => {
      inst.signatures = this.makeSignatures(Filter.noAltForm(inst.dbInsts));
      this.maxOpRows = Math.max(this.maxOpRows, inst.signatures.length);
    });

    const iSignatureMap = Object.create(null);
    const iSignatureArr = [];

    const oSignatureMap = Object.create(null);
    const oSignatureArr = [];

    // Must be first to be assigned to zero.
    const oSignatureNone = "ROW(0, 0xFF)";
    oSignatureMap[oSignatureNone] = [0];
    oSignatureArr.push(oSignatureNone);

    function findSignaturesIndex(rows) {
      const len = rows.length;
      if (!len) return 0;

      const indexes = iSignatureMap[rows[0].data];
      if (indexes === undefined) return -1;

      for (var i = 0; i < indexes.length; i++) {
        const index = indexes[i];
        if (index + len > iSignatureArr.length) continue;

        var ok = true;
        for (var j = 0; j < len; j++) {
          if (iSignatureArr[index + j].data !== rows[j].data) {
            ok = false;
            break;
          }
        }

        if (ok)
          return index;
      }

      return -1;
    }

    function indexSignatures(signatures) {
      const result = iSignatureArr.length;

      for (var i = 0; i < signatures.length; i++) {
        const signature = signatures[i];
        const idx = iSignatureArr.length;

        if (!hasOwn.call(iSignatureMap, signature.data))
          iSignatureMap[signature.data] = [];

        iSignatureMap[signature.data].push(idx);
        iSignatureArr.push(signature);
      }

      return result;
    }

    for (var len = this.maxOpRows; len >= 0; len--) {
      insts.forEach((inst) => {
        const signatures = inst.signatures;
        if (signatures.length === len) {
          const signatureEntries = [];
          for (var j = 0; j < len; j++) {
            const signature = signatures[j];

            var signatureEntry = `ROW(${signature.length}, ${signature.x86 ? 1 : 0}, ${signature.x64 ? 1 : 0}, ${signature.implicit}`;
            var signatureComment = signature.toString();

            var x = 0;
            while (x < signature.length) {
              const h = signature[x].toAsmJitOpData();
              var index = -1;
              if (!hasOwn.call(oSignatureMap, h)) {
                index = oSignatureArr.length;
                oSignatureMap[h] = index;
                oSignatureArr.push(h);
              }
              else {
                index = oSignatureMap[h];
              }

              signatureEntry += `, ${String(index).padEnd(3)}`;
              x++;
            }

            while (x < 6) {
              signatureEntry += `, ${String(0).padEnd(3)}`;
              x++;
            }

            signatureEntry += `)`;
            signatureEntries.push({ data: signatureEntry, comment: signatureComment, refs: 0 });
          }

          var count = signatureEntries.length;
          var index = findSignaturesIndex(signatureEntries);

          if (index === -1)
            index = indexSignatures(signatureEntries);

          iSignatureArr[index].refs++;
          inst.signatureIndex = index;
          inst.signatureCount = count;
        }
      });
    }

    var s = `#define ROW(count, x86, x64, implicit, o0, o1, o2, o3, o4, o5)       \\\n` +
            `  { count, uint8_t(x86 ? uint8_t(InstDB::Mode::kX86) : uint8_t(0)) | \\\n` +
            `                  (x64 ? uint8_t(InstDB::Mode::kX64) : uint8_t(0)) , \\\n` +
            `    implicit,                                                        \\\n` +
            `    0,                                                               \\\n` +
            `    { o0, o1, o2, o3, o4, o5 }                                       \\\n` +
            `  }\n` +
            StringUtils.makeCxxArrayWithComment(iSignatureArr, "const InstDB::InstSignature InstDB::_instSignatureTable[]") +
            `#undef ROW\n` +
            `\n` +
            `#define ROW(opFlags, regId) { opFlags, uint8_t(regId) }\n` +
            `#define F(VAL) uint64_t(InstDB::OpFlags::k##VAL)\n` +
            StringUtils.makeCxxArray(oSignatureArr, "const InstDB::OpSignature InstDB::_opSignatureTable[]") +
            `#undef F\n` +
            `#undef ROW\n`;
    this.inject("InstSignatureTable", disclaimer(s), oSignatureArr.length * 8 + iSignatureArr.length * 8);
  }

  makeSignatures(dbInsts) {
    const signatures = new SignatureArray();
    for (var i = 0; i < dbInsts.length; i++) {
      const inst = dbInsts[i];
      const ops = inst.operands;

      // NOTE: This changed from having reg|mem merged into creating two signatures
      // instead. Imagine two instructions in one `dbInsts` array:
      //
      //   1. mov reg, reg/mem
      //   2. mov reg/mem, reg
      //
      // If we merge them and then unmerge, we will have 4 signatures, when iterated:
      //
      //   1a. mov reg, reg
      //   1b. mov reg, mem
      //   2a. mov reg, reg
      //   2b. mov mem, reg
      //
      // So, instead of merging them here, we insert separated signatures and let
      // the tool merge them in a way that can be easily unmerged at runtime into:
      //
      //   1a. mov reg, reg
      //   1b. mov reg, mem
      //   2b. mov mem, reg
      var modrmCount = 1;
      for (var modrm = 0; modrm < modrmCount; modrm++) {
        var row = new ISignature(inst.name);
        row.x86 = (inst.arch === "ANY" || inst.arch === "X86");
        row.x64 = (inst.arch === "ANY" || inst.arch === "X64");

        for (var j = 0; j < ops.length; j++) {
          var iop = ops[j];

          var reg = iop.reg;
          var mem = iop.mem;
          var imm = iop.imm;
          var rel = iop.rel;

          // Skip all instructions having implicit `imm` operand of `1`.
          if (iop.immValue !== null)
            break;

          // Shorten the number of signatures of 'mov' instruction.
          if (inst.name === "mov" && mem.startsWith("moff"))
            break;

          if (reg === "seg") reg = "sreg";
          if (reg === "st(i)") reg = "st";
          if (reg === "st(0)") reg = "st0";

          if (mem === "moff8") mem = "m8";
          if (mem === "moff16") mem = "m16";
          if (mem === "moff32") mem = "m32";
          if (mem === "moff64") mem = "m64";

          if (mem === "m32fp") mem = "m32";
          if (mem === "m64fp") mem = "m64";
          if (mem === "m80fp") mem = "m80";
          if (mem === "m80bcd") mem = "m80";
          if (mem === "m80dec") mem = "m80";
          if (mem === "m16int") mem = "m16";
          if (mem === "m32int") mem = "m32";
          if (mem === "m64int") mem = "m64";

          if (mem === "m16_16") mem = "m32";
          if (mem === "m16_32") mem = "m48";
          if (mem === "m16_64") mem = "m80";

          if (reg && mem) {
            if (modrmCount === 1) {
              mem = null;
              modrmCount++;
            }
            else {
              reg = null;
            }
          }

          const op = new OSignature();
          if (iop.implicit) {
            row.implicit++;
            op.flags.implicit = true;
          }

          const seg = iop.memSeg;
          if (seg) {
            switch (inst.name) {
              case "cmpsb": op.flags.m8 = true; break;
              case "cmpsw": op.flags.m16 = true; break;
              case "cmpsd": op.flags.m32 = true; break;
              case "cmpsq": op.flags.m64 = true; break;
              case "lodsb": op.flags.m8 = true; break;
              case "lodsw": op.flags.m16 = true; break;
              case "lodsd": op.flags.m32 = true; break;
              case "lodsq": op.flags.m64 = true; break;
              case "movsb": op.flags.m8 = true; break;
              case "movsw": op.flags.m16 = true; break;
              case "movsd": op.flags.m32 = true; break;
              case "movsq": op.flags.m64 = true; break;
              case "scasb": op.flags.m8 = true; break;
              case "scasw": op.flags.m16 = true; break;
              case "scasd": op.flags.m32 = true; break;
              case "scasq": op.flags.m64 = true; break;
              case "stosb": op.flags.m8 = true; break;
              case "stosw": op.flags.m16 = true; break;
              case "stosd": op.flags.m32 = true; break;
              case "stosq": op.flags.m64 = true; break;
              case "insb": op.flags.m8 = true; break;
              case "insw": op.flags.m16 = true; break;
              case "insd": op.flags.m32 = true; break;
              case "outsb": op.flags.m8 = true; break;
              case "outsw": op.flags.m16 = true; break;
              case "outsd": op.flags.m32 = true; break;
              case "clzero": op.flags.mem = true; op.flags.m512 = true; break;
              case "enqcmd": op.flags.mem = true; op.flags.m512 = true; break;
              case "enqcmds": op.flags.mem = true; op.flags.m512 = true; break;
              case "movdir64b": op.flags.mem = true; op.flags.m512 = true; break;
              case "maskmovq": op.flags.mem = true; op.flags.m64 = true; break;
              case "maskmovdqu": op.flags.mem = true; op.flags.m128 = true; break;
              case "vmaskmovdqu": op.flags.mem = true; op.flags.m128 = true; break;
              case "monitor": op.flags.mem = true; break;
              case "monitorx": op.flags.mem = true; break;
              case "umonitor": op.flags.mem = true; break;
              default: console.log(`UNKNOWN MEM IN INSTRUCTION '${inst.name}'`); break;
            }

            if (seg === "ds") op.flags.memDS = true;
            if (seg === "es") op.flags.memES = true;
            if (reg === "reg") { op.flags.memBase = true; }
            if (reg === "r32") { op.flags.memBase = true; }
            if (reg === "r64") { op.flags.memBase = true; }
            if (reg === "zax") { op.flags.memBase = true; op.flags.memZAX = true; }
            if (reg === "zsi") { op.flags.memBase = true; op.flags.memZSI = true; }
            if (reg === "zdi") { op.flags.memBase = true; op.flags.memZDI = true; }
          }
          else if (reg) {
            if (reg == "r8") {
              op.flags["r8lo"] = true;
              op.flags["r8hi"] = true;
            }
            else {
              op.flags[reg] = true;
            }
          }
          if (mem) {
            op.flags[mem] = true;
            // HACK: Allow LEA|CL*|PREFETCH* to use any memory size.
            if (/^(cldemote|clwb|clflush\w*|lea|prefetch\w*)$/.test(inst.name)) {
              op.flags.mem = true;
              Object.assign(op.flags, MemOp);
            }

            // HACK: These instructions specify explicit memory size, but it's just informational.
            if (inst.name === "enqcmd" || inst.name === "enqcmds" || inst.name === "movdir64b")
              op.flags.mem = true;

          }
          if (imm) {
            if (iop.immSign === "any" || iop.immSign === "signed"  ) op.flags["i" + imm] = true;
            if (iop.immSign === "any" || iop.immSign === "unsigned") op.flags["u" + imm] = true;
          }
          if (rel) op.flags["rel" + rel] = true;

          row.push(op);
        }

        // Not equal if we terminated the loop.
        if (j === ops.length)
          signatures.push(row);
      }
    }

    if (signatures.length && GenUtils.canUseImplicitMemSize(dbInsts[0].name))
      signatures.calcImplicitMemSize();

    signatures.compact();
    return signatures;
  }
}

// ============================================================================
// [tablegen.x86.AdditionalInfoTable]
// ============================================================================

class AdditionalInfoTable extends core.Task {
  constructor() {
    super("AdditionalInfoTable");
  }

  run() {
    const insts = this.ctx.insts;
    const rwInfoTable = new IndexedArray();
    const additionaInfoTable = new IndexedArray();

    // If the instruction doesn't read any flags it should point to the first index.
    rwInfoTable.addIndexed(`{ 0, 0 }`);

    insts.forEach((inst) => {
      const dbInsts = inst.dbInsts;

      var features = GenUtils.cpuFeaturesOf(dbInsts).map(function(f) { return `EXT(${f})`; }).join(", ");
      if (!features) features = "0";

      var [r, w] = this.rwFlagsOf(dbInsts);
      const rData = r.map(function(flag) { return `FLAG(${flag})`; }).join(" | ") || "0";
      const wData = w.map(function(flag) { return `FLAG(${flag})`; }).join(" | ") || "0";
      const rwDataIndex = rwInfoTable.addIndexed(`{ ${rData}, ${wData} }`);

      inst.additionalInfoIndex = additionaInfoTable.addIndexed(`{ { ${features} }, ${rwDataIndex}, 0 }`);
    });

    var s = `#define EXT(VAL) uint32_t(CpuFeatures::X86::k##VAL)\n` +
            `const InstDB::AdditionalInfo InstDB::_additionalInfoTable[] = {\n${StringUtils.format(additionaInfoTable, kIndent, true)}\n};\n` +
            `#undef EXT\n` +
            `\n` +
            `#define FLAG(VAL) uint32_t(CpuRWFlags::kX86_##VAL)\n` +
            `const InstDB::RWFlagsInfoTable InstDB::_rwFlagsInfoTable[] = {\n${StringUtils.format(rwInfoTable, kIndent, true)}\n};\n` +
            `#undef FLAG\n`;
    this.inject("AdditionalInfoTable", disclaimer(s), additionaInfoTable.length * 8 + rwInfoTable.length * 8);
  }

  rwFlagsOf(dbInsts) {
    const r = Object.create(null);
    const w = Object.create(null);

    for (var i = 0; i < dbInsts.length; i++) {
      const dbInst = dbInsts[i];

      // Omit special cases, this is handled well in C++ code.
      if (dbInst.name === "mov")
        continue;

      const specialRegs = dbInst.specialRegs;

      // Mov is a special case, moving to/from control regs makes flags undefined,
      // which we don't want to have in `X86InstDB::operationData`. This is, thus,
      // a special case instruction analyzer must deal with.
      if (dbInst.name === "mov")
        continue;

      for (var specialReg in specialRegs) {
        var flag = "";
        switch (specialReg) {
          case "FLAGS.CF": flag = "CF"; break;
          case "FLAGS.OF": flag = "OF"; break;
          case "FLAGS.SF": flag = "SF"; break;
          case "FLAGS.ZF": flag = "ZF"; break;
          case "FLAGS.AF": flag = "AF"; break;
          case "FLAGS.PF": flag = "PF"; break;
          case "FLAGS.DF": flag = "DF"; break;
          case "FLAGS.IF": flag = "IF"; break;
        //case "FLAGS.TF": flag = "TF"; break;
          case "FLAGS.AC": flag = "AC"; break;
          case "X86SW.C0": flag = "C0"; break;
          case "X86SW.C1": flag = "C1"; break;
          case "X86SW.C2": flag = "C2"; break;
          case "X86SW.C3": flag = "C3"; break;
          default:
            continue;
        }

        switch (specialRegs[specialReg]) {
          case "R":
            r[flag] = true;
            break;
          case "X":
            r[flag] = true;
            // ... fallthrough ...
          case "W":
          case "U":
          case "0":
          case "1":
            w[flag] = true;
            break;
        }
      }
    }

    return [ArrayUtils.sorted(r), ArrayUtils.sorted(w)];
  }
}

// ============================================================================
// [tablegen.x86.InstRWInfoTable]
// ============================================================================

const NOT_MEM_AMBIGUOUS = MapUtils.arrayToMap([
  "call", "movq"
]);

class InstRWInfoTable extends core.Task {
  constructor() {
    super("InstRWInfoTable");

    this.rwInfoIndexA = [];
    this.rwInfoIndexB = [];
    this.rwInfoTableA = new IndexedArray();
    this.rwInfoTableB = new IndexedArray();

    this.rmInfoTable = new IndexedArray();
    this.opInfoTable = new IndexedArray();

    this.rwCategoryByName = {
      "imul"      : "Imul",
      "mov"       : "Mov",
      "movabs"    : "Movabs",
      "movhpd"    : "Movh64",
      "movhps"    : "Movh64",
      "punpcklbw" : "Punpcklxx",
      "punpckldq" : "Punpcklxx",
      "punpcklwd" : "Punpcklxx",
      "vmaskmovpd": "Vmaskmov",
      "vmaskmovps": "Vmaskmov",
      "vmovddup"  : "Vmovddup",
      "vmovmskpd" : "Vmovmskpd",
      "vmovmskps" : "Vmovmskps",
      "vpmaskmovd": "Vmaskmov",
      "vpmaskmovq": "Vmaskmov"
    };

    const _ = null;
    this.rwCategoryByData = {
      Vmov1_8: [
        [{access: "W", clc: 0, flags: {}, fixed: -1, index: 0, width:  8}, {access: "R", clc: 0, flags: {}, fixed: -1, index: 0, width: 64},_,_,_,_],
        [{access: "W", clc: 0, flags: {}, fixed: -1, index: 0, width: 16}, {access: "R", clc: 0, flags: {}, fixed: -1, index: 0, width:128},_,_,_,_],
        [{access: "W", clc: 0, flags: {}, fixed: -1, index: 0, width: 32}, {access: "R", clc: 0, flags: {}, fixed: -1, index: 0, width:256},_,_,_,_],
        [{access: "W", clc: 0, flags: {}, fixed: -1, index: 0, width: 64}, {access: "R", clc: 0, flags: {}, fixed: -1, index: 0, width:512},_,_,_,_]
      ],
      Vmov1_4: [
        [{access: "W", clc: 0, flags: {}, fixed: -1, index: 0, width: 32}, {access: "R", clc: 0, flags: {}, fixed: -1, index: 0, width:128},_,_,_,_],
        [{access: "W", clc: 0, flags: {}, fixed: -1, index: 0, width: 64}, {access: "R", clc: 0, flags: {}, fixed: -1, index: 0, width:256},_,_,_,_],
        [{access: "W", clc: 0, flags: {}, fixed: -1, index: 0, width:128}, {access: "R", clc: 0, flags: {}, fixed: -1, index: 0, width:512},_,_,_,_]
      ],
      Vmov1_2: [
        [{access: "W", clc: 0, flags: {}, fixed: -1, index: 0, width: 64}, {access: "R", clc: 0, flags: {}, fixed: -1, index: 0, width:128},_,_,_,_],
        [{access: "W", clc: 0, flags: {}, fixed: -1, index: 0, width:128}, {access: "R", clc: 0, flags: {}, fixed: -1, index: 0, width:256},_,_,_,_],
        [{access: "W", clc: 0, flags: {}, fixed: -1, index: 0, width:256}, {access: "R", clc: 0, flags: {}, fixed: -1, index: 0, width:512},_,_,_,_]
      ],
      Vmov2_1: [
        [{access: "W", clc: 0, flags: {}, fixed: -1, index: 0, width: 128}, {access: "R", clc: 0, flags: {}, fixed: -1, index: 0, width: 64},_,_,_,_],
        [{access: "W", clc: 0, flags: {}, fixed: -1, index: 0, width: 256}, {access: "R", clc: 0, flags: {}, fixed: -1, index: 0, width:128},_,_,_,_],
        [{access: "W", clc: 0, flags: {}, fixed: -1, index: 0, width: 512}, {access: "R", clc: 0, flags: {}, fixed: -1, index: 0, width:256},_,_,_,_]
      ],
      Vmov4_1: [
        [{access: "W", clc: 0, flags: {}, fixed: -1, index: 0, width: 128}, {access: "R", clc: 0, flags: {}, fixed: -1, index: 0, width: 32},_,_,_,_],
        [{access: "W", clc: 0, flags: {}, fixed: -1, index: 0, width: 256}, {access: "R", clc: 0, flags: {}, fixed: -1, index: 0, width: 64},_,_,_,_],
        [{access: "W", clc: 0, flags: {}, fixed: -1, index: 0, width: 512}, {access: "R", clc: 0, flags: {}, fixed: -1, index: 0, width:128},_,_,_,_]
      ],
      Vmov8_1: [
        [{access: "W", clc: 0, flags: {}, fixed: -1, index: 0, width: 128}, {access: "R", clc: 0, flags: {}, fixed: -1, index: 0, width: 16},_,_,_,_],
        [{access: "W", clc: 0, flags: {}, fixed: -1, index: 0, width: 256}, {access: "R", clc: 0, flags: {}, fixed: -1, index: 0, width: 32},_,_,_,_],
        [{access: "W", clc: 0, flags: {}, fixed: -1, index: 0, width: 512}, {access: "R", clc: 0, flags: {}, fixed: -1, index: 0, width: 64},_,_,_,_]
      ]
    };
  }

  run() {
    const insts = this.ctx.insts;

    const noRmInfo = CxxUtils.struct(
      "InstDB::RWInfoRm::kCategory" + "None".padEnd(10),
      StringUtils.decToHex(0, 2),
      String(0).padEnd(2),
      CxxUtils.flags({}),
      "0"
    );

    const noOpInfo = CxxUtils.struct(
      "0x0000000000000000u",
      "0x0000000000000000u",
      "0xFF",
      "0",
      CxxUtils.struct(0),
      "OpRWFlags::kNone"
    );

    this.rmInfoTable.addIndexed(noRmInfo);
    this.opInfoTable.addIndexed(noOpInfo);

    insts.forEach((inst) => {
      // Alternate forms would only mess this up, so filter them out.
      const dbInsts = Filter.noAltForm(inst.dbInsts);

      // The best we can do is to divide instructions that have 2 operands and others.
      // This gives us the highest chance of preventing special cases (which were not
      // entirely avoided).
      const o2Insts = dbInsts.filter((inst) => { return inst.operands.length === 2; });
      const oxInsts = dbInsts.filter((inst) => { return inst.operands.length !== 2; });

      const rwInfoArray = [this.rwInfo(inst, o2Insts), this.rwInfo(inst, oxInsts)];
      const rmInfoArray = [this.rmInfo(inst, o2Insts), this.rmInfo(inst, oxInsts)];

      for (var i = 0; i < 2; i++) {
        const rwInfo = rwInfoArray[i];
        const rmInfo = rmInfoArray[i];

        const rwOps = rwInfo.rwOps;
        const rwOpsIndex = [];
        for (var j = 0; j < rwOps.length; j++) {
          const op = rwOps[j];
          if (!op) {
            rwOpsIndex.push(this.opInfoTable.addIndexed(noOpInfo));
            continue;
          }

          const flags = {};
          const opAcc = op.access;

          if (opAcc === "R") flags.Read = true;
          if (opAcc === "W") flags.Write = true;
          if (opAcc === "X") flags.RW = true;
          Lang.merge(flags, op.flags);

          const rIndex = opAcc === "X" || opAcc === "R" ? op.index : -1;
          const rWidth = opAcc === "X" || opAcc === "R" ? op.width : -1;
          const wIndex = opAcc === "X" || opAcc === "W" ? op.index : -1;
          const wWidth = opAcc === "X" || opAcc === "W" ? op.width : -1;

          const consecutiveLeadCount = op.clc;

          const opData = CxxUtils.struct(
            this.byteMaskFromBitRanges([{ start: rIndex, end: rIndex + rWidth - 1 }]) + "u",
            this.byteMaskFromBitRanges([{ start: wIndex, end: wIndex + wWidth - 1 }]) + "u",
            StringUtils.decToHex(op.fixed === -1 ? 0xFF : op.fixed, 2),
            String(consecutiveLeadCount),
            CxxUtils.struct(0),
            CxxUtils.flags(flags, function(flag) { return "OpRWFlags::k" + flag; }, "OpRWFlags::kNone")
          );

          rwOpsIndex.push(this.opInfoTable.addIndexed(opData));
        }

        const rmData = CxxUtils.struct(
          "InstDB::RWInfoRm::kCategory" + rmInfo.category.padEnd(10),
          StringUtils.decToHex(rmInfo.rmIndexes, 2),
          String(Math.max(rmInfo.memFixed, 0)).padEnd(2),
          CxxUtils.flags({ "InstDB::RWInfoRm::kFlagAmbiguous": Boolean(rmInfo.memAmbiguous) }),
          rmInfo.memExtension === "None" ? "0" : "uint32_t(CpuFeatures::X86::k" + rmInfo.memExtension + ")"
        );

        const rwData = CxxUtils.struct(
          "InstDB::RWInfo::kCategory" + rwInfo.category.padEnd(10),
          String(this.rmInfoTable.addIndexed(rmData)).padEnd(2),
          CxxUtils.struct(...(rwOpsIndex.map(function(item) { return String(item).padEnd(2); })))
        );

        if (i == 0)
          this.rwInfoIndexA.push(this.rwInfoTableA.addIndexed(rwData));
        else
          this.rwInfoIndexB.push(this.rwInfoTableB.addIndexed(rwData));
      }
    });

    var s = "";
    s += "const uint8_t InstDB::rwInfoIndexA[Inst::_kIdCount] = {\n" + StringUtils.format(this.rwInfoIndexA, kIndent, -1) + "\n};\n";
    s += "\n";
    s += "const uint8_t InstDB::rwInfoIndexB[Inst::_kIdCount] = {\n" + StringUtils.format(this.rwInfoIndexB, kIndent, -1) + "\n};\n";
    s += "\n";
    s += "const InstDB::RWInfo InstDB::rwInfoA[] = {\n" + StringUtils.format(this.rwInfoTableA, kIndent, true) + "\n};\n";
    s += "\n";
    s += "const InstDB::RWInfo InstDB::rwInfoB[] = {\n" + StringUtils.format(this.rwInfoTableB, kIndent, true) + "\n};\n";
    s += "\n";
    s += "const InstDB::RWInfoOp InstDB::rwInfoOp[] = {\n" + StringUtils.format(this.opInfoTable, kIndent, true) + "\n};\n";
    s += "\n";
    s += "const InstDB::RWInfoRm InstDB::rwInfoRm[] = {\n" + StringUtils.format(this.rmInfoTable, kIndent, true) + "\n};\n";

    const size = this.rwInfoIndexA.length +
                 this.rwInfoIndexB.length +
                 this.rwInfoTableA.length * 8 +
                 this.rwInfoTableB.length * 8 +
                 this.rmInfoTable.length * 4 +
                 this.opInfoTable.length * 24;

    this.inject("InstRWInfoTable", disclaimer(s), size);
  }

  byteMaskFromBitRanges(ranges) {
    const arr = [];
    for (var i = 0; i < 64; i++)
      arr.push(0);

    for (var i = 0; i < ranges.length; i++) {
      const start = ranges[i].start;
      const end = ranges[i].end;

      if (start < 0)
        continue;

      for (var j = start; j <= end; j++) {
        const bytePos = j >> 3;
        if (bytePos < 0 || bytePos >= arr.length)
          FAIL(`Range ${start}:${end} cannot be used to create a byte-mask`);
        arr[bytePos] = 1;
      }
    }

    var s = "0x";
    for (var i = arr.length - 4; i >= 0; i -= 4) {
      const value = (arr[i + 3] << 3) | (arr[i + 2] << 2) | (arr[i + 1] << 1) | arr[i];
      s += value.toString(16).toUpperCase();
    }
    return s;
  }

  // Read/Write Info
  // ---------------

  rwInfo(asmInst, dbInsts) {
    const self = this;

    function nullOps() {
      return [null, null, null, null, null, null];
    }

    function makeRwFromOp(op) {
      if (!op.isRegOrMem())
        return null;

      return {
        access: op.read && op.write ? "X" : op.read ? "R" : op.write ? "W" : "?",
        clc: 0,
        flags: {},
        fixed: GenUtils.fixedRegOf(op.reg),
        index: op.rwxIndex,
        width: op.rwxWidth
      };
    }

    function queryRwGeneric(dbInsts, step) {
      var rwOps = nullOps();
      for (var i = 0; i < dbInsts.length; i++) {
        const dbInst = dbInsts[i];
        const operands = dbInst.operands;

        for (var j = 0; j < operands.length; j++) {
          const op = operands[j];
          if (!op.isRegOrMem())
            continue;

          const opSize = op.isReg() ? op.regSize : op.memSize;
          var d = {
            access: op.read && op.write ? "X" : op.read ? "R" : op.write ? "W" : "?",
            clc: 0,
            flags: {},
            fixed: -1,
            index: -1,
            width: -1
          };

          if (op.consecutiveLeadCount)
            d.clc = op.consecutiveLeadCount;

          if (op.isReg())
            d.fixed = GenUtils.fixedRegOf(op.reg);
          else
            d.fixed = GenUtils.fixedRegOf(op.mem);

          if (op.zext)
            d.flags.ZExt = true;

          if (op.regIndexRel)
            d.flags.Consecutive = true;

          for (var k in self.rwOpFlagsForInstruction(asmInst.name, j))
            d.flags[k] = true;

          if ((step === -1 || step === j) || op.rwxIndex !== 0 || op.rwxWidth !== opSize) {
            d.index = op.rwxIndex;
            d.width = op.rwxWidth;
          }

          if (d.fixed !== -1) {
            if (op.memSeg)
              d.flags.MemPhysId = true;
            else
              d.flags.RegPhysId = true;
          }

          if (rwOps[j] === null) {
            rwOps[j] = d;
          }
          else {
            if (!Lang.deepEqExcept(rwOps[j], d, { "fixed": true, "flags": true }))
              return null;

            if (rwOps[j].fixed === -1)
              rwOps[j].fixed = d.fixed;
            Lang.merge(rwOps[j].flags, d.flags);
          }
        }
      }
      return { category: "Generic", rwOps };
    }

    function queryRwByData(dbInsts, rwOpsArray) {
      for (var i = 0; i < dbInsts.length; i++) {
        const dbInst = dbInsts[i];
        const operands = dbInst.operands;
        const rwOps = nullOps();

        for (var j = 0; j < operands.length; j++)
          rwOps[j] = makeRwFromOp(operands[j])

        var match = 0;
        for (var j = 0; j < rwOpsArray.length; j++)
          match |= Lang.deepEq(rwOps, rwOpsArray[j]);

        if (!match)
          return false;
      }

      return true;
    }

    function dumpRwToData(dbInsts) {
      const out = [];
      for (var i = 0; i < dbInsts.length; i++) {
        const dbInst = dbInsts[i];
        const operands = dbInst.operands;
        const rwOps = nullOps();

        for (var j = 0; j < operands.length; j++)
          rwOps[j] = makeRwFromOp(operands[j])

        if (ArrayUtils.deepIndexOf(out, rwOps) !== -1)
          continue;

        out.push(rwOps);
      }
      return out;
    }

    // Some instructions are just special...
    const name = dbInsts.length ? dbInsts[0].name : "";
    if (name in this.rwCategoryByName)
      return { category: this.rwCategoryByName[name], rwOps: nullOps() };

    // Generic rules.
    for (var i = -1; i <= 6; i++) {
      const rwInfo = queryRwGeneric(dbInsts, i);
      if (rwInfo)
        return rwInfo;
    }

    // Specific rules.
    for (var k in this.rwCategoryByData)
      if (queryRwByData(dbInsts, this.rwCategoryByData[k]))
        return { category: k, rwOps: nullOps() };

    // FAILURE: Missing data to categorize this instruction.
    if (name) {
      const items = dumpRwToData(dbInsts)
      console.log(`RW: ${dbInsts.length ? dbInsts[0].name : ""}:`);
      items.forEach((item) => {
        console.log("  " + JSON.stringify(item));
      });
    }

    return null;
  }

  rwOpFlagsForInstruction(instName, opIndex) {
    const toMap = MapUtils.arrayToMap;

    // TODO: We should be able to get this information from asmdb.
    switch (instName + "@" + opIndex) {
      case "cmps@0": return toMap(['MemBaseRW', 'MemBasePostModify']);
      case "cmps@1": return toMap(['MemBaseRW', 'MemBasePostModify']);
      case "movs@0": return toMap(['MemBaseRW', 'MemBasePostModify']);
      case "movs@1": return toMap(['MemBaseRW', 'MemBasePostModify']);
      case "lods@1": return toMap(['MemBaseRW', 'MemBasePostModify']);
      case "stos@0": return toMap(['MemBaseRW', 'MemBasePostModify']);
      case "scas@1": return toMap(['MemBaseRW', 'MemBasePostModify']);
      case "bndstx@0": return toMap(['MemBaseWrite', 'MemIndexWrite']);

      default:
        return {};
    }
  }

  // Reg/Mem Info
  // ------------

  rmInfo(asmInst, dbInsts) {
    const info = {
      category: "None",
      rmIndexes: this.rmReplaceableIndexes(dbInsts),
      memFixed: this.rmFixedSize(dbInsts),
      memAmbiguous: this.rmIsAmbiguous(dbInsts),
      memConsistent: this.rmIsConsistent(dbInsts),
      memExtension: this.rmExtension(dbInsts)
    };

    if (info.memFixed !== -1)
      info.category = "Fixed";
    else if (info.memConsistent)
      info.category = "Consistent";
    else if (info.rmIndexes)
      info.category = this.rmReplaceableCategory(dbInsts);

    return info;
  }

  rmReplaceableCategory(dbInsts) {
    var category = null;

    for (var i = 0; i < dbInsts.length; i++) {
      const dbInst = dbInsts[i];
      const operands = dbInst.operands;

      var rs = -1;
      var ms = -1;

      for (var j = 0; j < operands.length; j++) {
        const op = operands[j];
        if (op.isMem())
          ms = op.memSize;
        else if (op.isReg())
          rs = Math.max(rs, op.regSize);
      }

      var c = (rs === -1    ) ? "None"    :
              (ms === -1    ) ? "None"    :
              (ms === rs    ) ? "Fixed"   :
              (ms === rs / 2) ? "Half"    :
              (ms === rs / 4) ? "Quarter" :
              (ms === rs / 8) ? "Eighth"  : "Unknown";

      if (category === null)
        category = c;
      else if (category !== c) {
        // Special cases.
        if (dbInst.name === "mov" || dbInst.name === "vmovddup")
          return "None";

        if (/^(punpcklbw|punpckldq|punpcklwd)$/.test(dbInst.name))
          return "None";

        return StringUtils.capitalize(dbInst.name);
      }
    }

    if (category === "Unknown")
      console.log(`Instruction '${dbInsts[0].name}' has no RMInfo category.`);

    return category || "Unknown";
  }

  rmReplaceableIndexes(dbInsts) {
    function maskOf(inst, fn) {
      var m = 0;
      var operands = inst.operands;
      for (var i = 0; i < operands.length; i++)
        if (fn(operands[i]))
          m |= (1 << i);
      return m;
    }

    function getRegIndexes(inst) { return maskOf(inst, function(op) { return op.isReg(); }); };
    function getMemIndexes(inst) { return maskOf(inst, function(op) { return op.isMem(); }); };

    var mask = 0;

    for (var i = 0; i < dbInsts.length; i++) {
      const dbInst = dbInsts[i];

      var mi = getMemIndexes(dbInst);
      var ri = getRegIndexes(dbInst) & ~mi;

      if (!mi)
        continue;

      const match = dbInsts.some((inst) => {
        var ti = getRegIndexes(inst);
        return ((ri & ti) === ri && (mi & ti) === mi);
      });

      if (!match)
        return 0;
      mask |= mi;
    }

    return mask;
  }

  rmFixedSize(insts) {
    var savedOp = null;

    for (var i = 0; i < insts.length; i++) {
      const inst = insts[i];
      const operands = inst.operands;

      for (var j = 0; j < operands.length; j++) {
        const op = operands[j];
        if (op.mem) {
          if (savedOp && savedOp.mem !== op.mem)
            return -1;
          savedOp = op;
        }
      }
    }

    return savedOp ? Math.max(savedOp.memSize, 0) / 8 : -1;
  }

  rmIsConsistent(insts) {
    var hasMem = 0;
    for (var i = 0; i < insts.length; i++) {
      const inst = insts[i];
      const operands = inst.operands;
      for (var j = 0; j < operands.length; j++) {
        const op = operands[j];
        if (op.mem) {
          hasMem = 1;
          if (!op.reg)
            return 0;
          if (asmdb.x86.Utils.regSize(op.reg) !== op.memSize)
            return 0;
        }
      }
    }
    return hasMem;
  }

  rmIsAmbiguous(dbInsts) {
    function isAmbiguous(dbInsts) {
      const memMap = {};
      const immMap = {};

      for (var i = 0; i < dbInsts.length; i++) {
        const dbInst = dbInsts[i];
        const operands = dbInst.operands;

        var memStr = "";
        var immStr = "";
        var hasMem = false;
        var hasImm = false;

        for (var j = 0; j < operands.length; j++) {
          const op = operands[j];
          if (j) {
            memStr += ", ";
            immStr += ", ";
          }

          if (op.isImm()) {
            immStr += "imm";
            hasImm = true;
          }
          else {
            immStr += op.toString();
          }

          if (op.mem) {
            memStr += "m";
            hasMem = true;
          }
          else {
            memStr += op.isImm() ? "imm" : op.toString();
          }
        }

        if (hasImm) {
          if (immMap[immStr] === true)
            continue;
          immMap[immStr] = true;
        }

        if (hasMem) {
          if (memMap[memStr] === true)
            return 1;
          memMap[memStr] = true;
        }
      }
      return 0;
    }

    const uniqueInsts = Filter.unique(dbInsts);

    // Special cases.
    if (!dbInsts.length)
      return 0;

    if (NOT_MEM_AMBIGUOUS[dbInsts[0].name])
      return 0;

    return (isAmbiguous(Filter.byArch(uniqueInsts, "X86")) << 0) |
           (isAmbiguous(Filter.byArch(uniqueInsts, "X64")) << 1) ;
  }

  rmExtension(dbInsts) {
    if (!dbInsts.length)
      return "None";

    const name = dbInsts[0].name;
    switch (name) {
      case "pextrw":
        return "SSE4_1";

      case "vpslldq":
      case "vpsrldq":
        return "AVX512_BW";

      default:
        return "None";
    }
  }
}

// ============================================================================
// [tablegen.x86.InstCommonTable]
// ============================================================================

class InstCommonTable extends core.Task {
  constructor() {
    super("InstCommonTable", [
      "IdEnum",
      "NameTable",
      "InstSignatureTable",
      "AdditionalInfoTable",
      "InstRWInfoTable"
    ]);
  }

  run() {
    const insts = this.ctx.insts;
    const table = new IndexedArray();

    insts.forEach((inst) => {
      const commonFlagsArray = inst.flags.filter((flag) => { return !flag.startsWith("Avx512"); });
      const avx512FlagsArray = inst.flags.filter((flag) => { return  flag.startsWith("Avx512"); });

      const commonFlags = commonFlagsArray.map(function(flag) { return `F(${flag          })`; }).join("|") || "0";
      const avx512Flags = avx512FlagsArray.map(function(flag) { return `X(${flag.substr(6)})`; }).join("|") || "0";

      const controlFlow = `CONTROL_FLOW(${inst.controlFlow})`;
      const singleRegCase = `SAME_REG_HINT(${inst.singleRegCase})`;

      const row = "{ " +
        String(commonFlags        ).padEnd(50) + ", " +
        String(avx512Flags        ).padEnd(30) + ", " +
        String(inst.signatureIndex).padEnd( 3) + ", " +
        String(inst.signatureCount).padEnd( 2) + ", " +
        String(controlFlow        ).padEnd(16) + ", " +
        String(singleRegCase      ).padEnd(16) + "}";
      inst.commonInfoIndex = table.addIndexed(row);
    });

    var s = `#define F(VAL) uint32_t(InstDB::InstFlags::k##VAL)\n` +
            `#define X(VAL) uint32_t(InstDB::Avx512Flags::k##VAL)\n` +
            `#define CONTROL_FLOW(VAL) uint8_t(InstControlFlow::k##VAL)\n` +
            `#define SAME_REG_HINT(VAL) uint8_t(InstSameRegHint::k##VAL)\n` +
            `const InstDB::CommonInfo InstDB::_commonInfoTable[] = {\n${StringUtils.format(table, kIndent, true)}\n};\n` +
            `#undef SAME_REG_HINT\n` +
            `#undef CONTROL_FLOW\n` +
            `#undef X\n` +
            `#undef F\n`;
    this.inject("InstCommonTable", disclaimer(s), table.length * 8);
  }
}

// ============================================================================
// [Main]
// ============================================================================

new X86TableGen()
  .addTask(new IdEnum())
  .addTask(new NameTable())
  .addTask(new AltOpcodeTable())
  .addTask(new InstSignatureTable())
  .addTask(new AdditionalInfoTable())
  .addTask(new InstRWInfoTable())
  .addTask(new InstCommonTable())
  .run();