aboutsummaryrefslogtreecommitdiff
path: root/src/arch/x86_64/Mir.zig
blob: 70f809068552187f0054f355b7c8513dc4016670 (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
//! Machine Intermediate Representation.
//! This data is produced by x86_64 Codegen and consumed by x86_64 Isel.
//! These instructions have a 1:1 correspondence with machine code instructions
//! for the target. MIR can be lowered to source-annotated textual assembly code
//! instructions, or it can be lowered to machine code.
//! The main purpose of MIR is to postpone the assignment of offsets until Isel,
//! so that, for example, the smaller encodings of jump instructions can be used.

instructions: std.MultiArrayList(Inst).Slice,
/// The meaning of this data is determined by `Inst.Tag` value.
extra: []const u32,
string_bytes: []const u8,
locals: []const Local,
table: []const Inst.Index,
frame_locs: std.MultiArrayList(FrameLoc).Slice,

pub const Inst = struct {
    tag: Tag,
    ops: Ops,
    data: Data,

    pub const Index = u32;

    pub const Fixes = enum(u8) {
        /// ___
        @"_",

        /// ___ 0
        _0,
        /// ___ 1
        _1,
        /// ___ 2
        _2,
        /// ___ 3
        _3,
        /// ___ 4
        _4,

        /// ___ Demote
        _demote,
        /// ___ Flush
        _flush,
        /// ___ Flush Optimized
        _flushopt,
        /// ___ Instructions With T0 Hint
        _it0,
        /// ___ Instructions With T0 Hint
        _it1,
        /// ___ With NTA Hint
        _nta,
        /// System Call ___
        sys_,
        /// ___ With T0 Hint
        _t0,
        /// ___ With T1 Hint
        _t1,
        /// ___ With T2 Hint
        _t2,
        /// ___ Write Back
        _wb,
        /// ___ With Intent to Write and T1 Hint
        _wt1,

        /// ___ crement Shadow Stack Pointer Doubleword
        _csspd,
        /// ___ crement Shadow Stack Pointer Quadword
        _csspq,
        /// ___ FS Segment Base
        _fsbase,
        /// ___ GS
        _gs,
        /// ___ GS Segment Base
        _gsbase,
        /// ___ Model Specific Register
        _msr,
        /// ___ MXCSR
        _mxcsr,
        /// ___ Processor ID
        _pid,
        /// ___ Protection Key Rights For User Pages
        _pkru,
        /// ___ Performance-Monitoring Counters
        _pmc,
        /// ___ Random Number
        _rand,
        /// ___ r Busy Flag in a Supervisor Shadow Stack token
        _rssbsy,
        /// ___ Random Seed
        _seed,
        /// ___ Shadow Stack Doubleword
        _ssd,
        /// ___ Shadow Stack Quadword
        _ssq,
        /// ___ Shadow Stack Pointer Doubleword
        _sspd,
        /// ___ Shadow Stack Pointer Quadword
        _sspq,
        /// ___ Time-Stamp Counter
        _tsc,
        /// ___ Time-Stamp Counter And Processor ID
        _tscp,
        /// ___ User Shadow Stack Doubleword
        _ussd,
        /// ___ User Shadow Stack Quadword
        _ussq,
        /// VEX-Encoded ___ MXCSR
        v_mxcsr,

        /// Byte ___
        b_,
        /// Interrupt ___
        /// Integer ___
        i_,
        /// Interrupt ___ Word
        i_w,
        /// Interrupt ___ Doubleword
        i_d,
        /// Interrupt ___ Quadword
        i_q,
        /// User-Interrupt ___
        ui_,

        /// ___ mp
        _mp,
        /// ___ if CX register is 0
        _cxz,
        /// ___ if ECX register is 0
        _ecxz,
        /// ___ if RCX register is 0
        _rcxz,

        /// ___ Addition
        _a,
        /// ___ Subtraction
        _s,
        /// ___ Multiply
        _m,
        /// ___ Division
        _d,

        /// ___ Without Affecting Flags
        _x,
        /// ___ Left
        _l,
        /// ___ Left Double
        _ld,
        /// ___ Left Without Affecting Flags
        _lx,
        /// ___ Mask
        _msk,
        /// ___ Right
        /// ___ For Reading
        /// ___ Register
        _r,
        /// ___ Right Double
        _rd,
        /// ___ Right Without Affecting Flags
        _rx,

        /// ___ Forward
        _f,
        /// ___ Reverse
        //_r,

        /// ___ Above
        //_a,
        /// ___ Above Or Equal
        _ae,
        /// ___ Below
        _b,
        /// ___ Below Or Equal
        /// ___ Big Endian
        _be,
        /// ___ Carry
        /// ___ Carry Flag
        _c,
        /// ___ Equal
        _e,
        /// ___ Greater
        _g,
        /// ___ Greater Or Equal
        _ge,
        /// ___ Less
        //_l,
        /// ___ Less Or Equal
        _le,
        /// ___ Not Above
        _na,
        /// ___ Not Above Or Equal
        _nae,
        /// ___ Not Below
        _nb,
        /// ___ Not Below Or Equal
        _nbe,
        /// ___ Not Carry
        _nc,
        /// ___ Not Equal
        _ne,
        /// ___ Not Greater
        _ng,
        /// ___ Not Greater Or Equal
        _nge,
        /// ___ Not Less
        _nl,
        /// ___ Not Less Or Equal
        _nle,
        /// ___ Not Overflow
        _no,
        /// ___ Not Parity
        _np,
        /// ___ Not Sign
        _ns,
        /// ___ Not Zero
        _nz,
        /// ___ Overflow
        _o,
        /// ___ Parity
        _p,
        /// ___ Parity Even
        _pe,
        /// ___ Parity Odd
        _po,
        /// ___ Sign
        //_s,
        /// ___ Zero
        _z,
        /// ___ Alignment Check Flag
        _ac,
        /// ___ Direction Flag
        //_d,
        /// ___ Interrupt Flag
        _i,
        /// ___ Task-Switched Flag In CR0
        _ts,
        /// ___ User Interrupt Flag
        _ui,

        /// ___ Byte
        //_b,
        /// ___ Word
        /// ___ For Writing
        /// ___ With Intent to Write
        _w,
        /// ___ Doubleword
        //_d,
        /// ___ Double Quadword to Quadword
        _dq2q,
        /// ___ QuadWord
        _q,
        /// ___ Quadword to Double Quadword
        _q2dq,

        /// ___ String
        //_s,
        /// ___ String Byte
        _sb,
        /// ___ String Word
        _sw,
        /// ___ String Doubleword
        _sd,
        /// ___ String Quadword
        _sq,

        /// Repeat ___ String
        @"rep _s",
        /// Repeat ___ String Byte
        @"rep _sb",
        /// Repeat ___ String Word
        @"rep _sw",
        /// Repeat ___ String Doubleword
        @"rep _sd",
        /// Repeat ___ String Quadword
        @"rep _sq",

        /// Repeat Equal ___ String
        @"repe _s",
        /// Repeat Equal ___ String Byte
        @"repe _sb",
        /// Repeat Equal ___ String Word
        @"repe _sw",
        /// Repeat Equal ___ String Doubleword
        @"repe _sd",
        /// Repeat Equal ___ String Quadword
        @"repe _sq",

        /// Repeat Not Equal ___ String
        @"repne _s",
        /// Repeat Not Equal ___ String Byte
        @"repne _sb",
        /// Repeat Not Equal ___ String Word
        @"repne _sw",
        /// Repeat Not Equal ___ String Doubleword
        @"repne _sd",
        /// Repeat Not Equal ___ String Quadword
        @"repne _sq",

        /// Repeat Not Zero ___ String
        @"repnz _s",
        /// Repeat Not Zero ___ String Byte
        @"repnz _sb",
        /// Repeat Not Zero ___ String Word
        @"repnz _sw",
        /// Repeat Not Zero ___ String Doubleword
        @"repnz _sd",
        /// Repeat Not Zero ___ String Quadword
        @"repnz _sq",

        /// Repeat Zero ___ String
        @"repz _s",
        /// Repeat Zero ___ String Byte
        @"repz _sb",
        /// Repeat Zero ___ String Word
        @"repz _sw",
        /// Repeat Zero ___ String Doubleword
        @"repz _sd",
        /// Repeat Zero ___ String Quadword
        @"repz _sq",

        /// Locked ___
        @"lock _",
        /// ___ And Complement
        //_c,
        /// Locked ___ And Complement
        @"lock _c",
        /// ___ And Reset
        //_r,
        /// Locked ___ And Reset
        @"lock _r",
        /// ___ And Set
        //_s,
        /// Locked ___ And Set
        @"lock _s",
        /// ___ 8 Bytes
        _8b,
        /// Locked ___ 8 Bytes
        @"lock _8b",
        /// ___ 16 Bytes
        _16b,
        /// Locked ___ 16 Bytes
        @"lock _16b",

        /// Float ___
        f_,
        /// Float ___ +1.0
        /// Float ___ 1
        f_1,
        /// Float ___ Below
        f_b,
        /// Float ___ Below Or Equal
        f_be,
        /// Float ___ Control Word
        f_cw,
        /// Float ___ Equal
        f_e,
        /// Float ___ Environment
        f_env,
        /// Float ___ log_2(e)
        f_l2e,
        /// Float ___ log_2(10)
        f_l2t,
        /// Float ___ log_10(2)
        f_lg2,
        /// Float ___ log_e(2)
        f_ln2,
        /// Float ___ Not Below
        f_nb,
        /// Float ___ Not Below Or Equal
        f_nbe,
        /// Float ___ Not Equal
        f_ne,
        /// Float ___ Not Unordered
        f_nu,
        /// Float ___ Pop
        f_p,
        /// Float ___ +1
        f_p1,
        /// Float ___ π
        f_pi,
        /// Float ___ Pop Pop
        f_pp,
        /// Float ___ crement Stack-Top Pointer
        f_cstp,
        /// Float ___ Status Word
        f_sw,
        /// Float ___ Unordered
        f_u,
        /// Float ___ +0.0
        f_z,
        /// Float BCD ___
        fb_,
        /// Float BCD ___ Pop
        fb_p,
        /// Float And Integer ___
        fi_,
        /// Float And Integer ___ Pop
        fi_p,
        /// Float No Wait ___
        fn_,
        /// Float No Wait ___ Control Word
        fn_cw,
        /// Float No Wait ___ Environment
        fn_env,
        /// Float No Wait ___ status word
        fn_sw,
        /// Float Extended ___
        fx_,
        /// Float Extended ___ 64
        fx_64,

        /// ___ in 32-bit and Compatibility Mode
        _32,
        /// ___ in 64-bit Mode
        _64,

        /// Packed ___
        p_,
        /// Packed ___ Byte
        p_b,
        /// Packed ___ Word
        p_w,
        /// Packed ___ Doubleword
        p_d,
        /// Packed ___ Quadword
        p_q,
        /// Packed ___ Double Quadword
        /// Packed ___ Doubleword to Quadword
        p_dq,
        /// Packed ___ Unsigned Doubleword to Quadword
        p_udq,
        /// Packed Carry-Less ___ Quadword to Double Quadword
        pcl_qdq,
        /// Packed Half ___ Doubleword
        ph_d,
        /// Packed Half ___ Saturate Word
        ph_sw,
        /// Packed Half ___ Word
        ph_w,
        /// ___ Aligned Packed Integer Values
        _dqa,
        /// ___ Unaligned Packed Integer Values
        _dqu,

        /// ___ Scalar Single-Precision Values
        _ss,
        /// ___ Packed Single-Precision Values
        _ps,
        /// ___ Scalar Double-Precision Values
        //_sd,
        /// ___ Packed Double-Precision Values
        _pd,
        /// Half ___ Packed Single-Precision Values
        h_ps,
        /// Half ___ Packed Double-Precision Values
        h_pd,

        /// ___ Internal Caches
        //_d,
        /// ___ TLB Entries
        _lpg,
        /// ___ Process-Context Identifier
        _pcid,

        /// Load ___
        l_,
        /// Memory ___
        m_,
        /// Store ___
        s_,
        /// Timed ___
        t_,
        /// User Level Monitor ___
        um_,

        /// VEX-Encoded ___
        v_,
        /// VEX-Encoded ___ Byte
        v_b,
        /// VEX-Encoded ___ Word
        v_w,
        /// VEX-Encoded ___ Doubleword
        v_d,
        /// VEX-Encoded ___ Quadword
        v_q,
        /// VEX-Encoded ___ Aligned Packed Integer Values
        v_dqa,
        /// VEX-Encoded ___ Unaligned Packed Integer Values
        v_dqu,
        /// VEX-Encoded ___ Integer Data
        v_i128,
        /// VEX-Encoded Packed ___
        vp_,
        /// VEX-Encoded Packed ___ Byte
        vp_b,
        /// VEX-Encoded Packed ___ Word
        vp_w,
        /// VEX-Encoded Packed ___ Doubleword
        vp_d,
        /// VEX-Encoded Packed ___ Quadword
        vp_q,
        /// VEX-Encoded Packed ___ Double Quadword
        /// VEX-Encoded Packed ___ Doubleword to Quadword
        vp_dq,
        /// VEX-Encoded Packed ___ Unsigned Doubleword to Quadword
        vp_udq,
        /// VEx-Encoded Packed Carry-Less ___ Quadword to Double Quadword
        vpcl_qdq,
        /// VEX-Encoded Packed Half ___ Doubleword
        vph_d,
        /// VEX-Encoded Packed Half ___ Saturate Word
        vph_sw,
        /// VEX-Encoded Packed Half ___ Word
        vph_w,
        /// VEX-Encoded ___ Scalar Single-Precision Values
        v_ss,
        /// VEX-Encoded ___ Packed Single-Precision Values
        v_ps,
        /// VEX-Encoded ___ Scalar Double-Precision Values
        v_sd,
        /// VEX-Encoded ___ Packed Double-Precision Values
        v_pd,
        /// VEX-Encoded ___ 128-Bits Of Floating-Point Data
        v_f128,
        /// VEX-Encoded Half ___ Packed Single-Precision Values
        vh_ps,
        /// VEX-Encoded Half ___ Packed Double-Precision Values
        vh_pd,

        /// ___ 128-bit key with key locker
        _128,
        /// ___ 256-bit key with key locker
        _256,
        /// ___ with key locker using 128-bit key
        _128kl,
        /// ___ with key locker using 256-bit key
        _256kl,
        /// ___ with key locker on 8 blocks using 128-bit key
        _wide128kl,
        /// ___ with key locker on 8 blocks using 256-bit key
        _wide256kl,

        /// Mask ___ Byte
        k_b,
        /// Mask ___ Word
        k_w,
        /// Mask ___ Doubleword
        k_d,
        /// Mask ___ Quadword
        k_q,

        pub fn fromCond(cc: bits.Condition) Fixes {
            return switch (cc) {
                inline else => |cc_tag| @field(Fixes, "_" ++ @tagName(cc_tag)),
                .z_and_np, .nz_or_p => unreachable,
            };
        }
    };

    pub const Tag = enum(u8) {
        // General-purpose
        /// ASCII adjust al after addition
        /// ASCII adjust ax before division
        /// ASCII adjust ax after multiply
        /// ASCII adjust al after subtraction
        aa,
        /// Add with carry
        /// Unsigned integer addition of two operands with carry flag
        adc,
        /// Add
        /// Add packed integers
        /// Add packed single-precision floating-point values
        /// Add scalar single-precision floating-point values
        /// Add packed double-precision floating-point values
        /// Add scalar double-precision floating-point values
        /// Packed single-precision floating-point horizontal add
        /// Packed double-precision floating-point horizontal add
        /// Packed horizontal add
        /// Packed horizontal add and saturate
        add,
        /// Logical and
        /// Bitwise logical and of packed single-precision floating-point values
        /// Bitwise logical and of packed double-precision floating-point values
        @"and",
        /// Adjust RPL field of segment selector
        arpl,
        /// Bit scan forward
        /// Bit scan reverse
        bs,
        /// Byte swap
        /// Swap GS base register
        swap,
        /// Bit test
        /// Bit test and complement
        /// Bit test and reset
        /// Bit test and set
        bt,
        /// Check array index against bounds
        bound,
        /// Call
        /// Fast system call
        call,
        /// Convert byte to word
        cbw,
        /// Convert doubleword to quadword
        cdq,
        /// Convert doubleword to quadword
        cdqe,
        /// Clear AC flag in EFLAGS register
        /// Clear carry flag
        /// Clear direction flag
        /// Clear interrupt flag
        /// Clear task-switched flag in CR0
        /// Clear user interrupt flag
        /// Cache line demote
        /// Flush cache line
        /// Flush cache line optimized
        /// Clear busy flag in a supervisor shadow stack token
        /// Cache line write back
        cl,
        /// Complement carry flag
        cmc,
        /// Conditional move
        cmov,
        /// Logical compare
        /// Compare string
        /// Compare scalar single-precision floating-point values
        /// Compare scalar double-precision floating-point values
        cmp,
        /// Compare and exchange
        /// Compare and exchange bytes
        cmpxchg,
        /// CPU identification
        cpuid,
        /// Convert doubleword to quadword
        cqo,
        /// Convert word to doubleword
        cwd,
        /// Convert word to doubleword
        cwde,
        /// Decimal adjust AL after addition
        /// Decimal adjust AL after subtraction
        da,
        /// Decrement by 1
        /// Decrement stack-top pointer
        /// Decrement shadow stack pointer
        de,
        /// Unsigned division
        /// Signed division
        /// Divide
        /// Divide packed single-precision floating-point values
        /// Divide scalar single-precision floating-point values
        /// Divide packed double-precision floating-point values
        /// Divide scalar double-precision floating-point values
        div,
        /// Terminate and indirect branch in 32-bit and compatibility mode
        /// Terminate and indirect branch in 64-bit mode
        endbr,
        /// Enqueue command
        /// Enqueue command supervisor
        enqcmd,
        /// Make stack frame for procedure parameters
        /// Fast system call
        enter,
        /// Fast return from fast system call
        exit,
        /// Load fence
        /// Memory fence
        /// Store fence
        fence,
        /// Halt
        hlt,
        /// History reset
        hreset,
        /// Input from port
        /// Input from port to string
        /// Increment by 1
        /// Increment stack-top pointer
        /// Increment shadow stack pointer
        in,
        /// Call to interrupt procedure
        int,
        /// Invalidate internal caches
        /// Invalidate TLB entries
        /// Invalidate process-context identifier
        inv,
        /// Conditional jump
        /// Jump
        j,
        /// Load status flags into AH register
        lahf,
        /// Load access right byte
        lar,
        /// Load effective address
        lea,
        /// High level procedure exit
        leave,
        /// Load global descriptor table register
        lgdt,
        /// Load interrupt descriptor table register
        lidt,
        /// Load local descriptor table register
        lldt,
        /// Load machine status word
        lmsw,
        /// Load string
        lod,
        /// Loop according to ECX counter
        loop,
        /// Load segment limit
        lsl,
        /// Load task register
        ltr,
        /// Count the number of leading zero bits
        lzcnt,
        /// Move
        /// Move data from string to string
        /// Move data after swapping bytes
        /// Move scalar single-precision floating-point value
        /// Move scalar double-precision floating-point value
        /// Move doubleword
        /// Move quadword
        /// Move aligned packed integer values
        /// Move unaligned packed integer values
        /// Move quadword from XMM to MMX technology register
        /// Move quadword from MMX technology to XMM register
        mov,
        /// Move with sign extension
        movsx,
        /// Move with zero extension
        movzx,
        /// Multiply
        /// Signed multiplication
        /// Multiply packed single-precision floating-point values
        /// Multiply scalar single-precision floating-point values
        /// Multiply packed double-precision floating-point values
        /// Multiply scalar double-precision floating-point values
        /// Multiply packed unsigned doubleword integers
        /// Multiply packed doubleword integers
        /// Carry-less multiplication quadword
        mul,
        /// Two's complement negation
        neg,
        /// No-op
        /// No operation
        nop,
        /// One's complement negation
        not,
        /// Logical or
        /// Bitwise logical or of packed single-precision floating-point values
        /// Bitwise logical or of packed double-precision floating-point values
        @"or",
        /// Output to port
        /// Output string to port
        out,
        /// Spin loop hint
        /// Timed pause
        pause,
        /// Pop
        pop,
        /// Return the count of number of bits set to 1
        popcnt,
        /// Pop stack into EFLAGS register
        popf,
        /// Push
        push,
        /// Push EFLAGS register onto the stack
        pushf,
        /// Rotate left through carry
        /// Rotate right through carry
        rc,
        /// Read FS segment base
        /// Read GS segment base
        /// Read from model specific register
        /// Read processor ID
        /// Read protection key rights for user pages
        /// Read performance-monitoring counters
        /// Read random number
        /// Read random seed
        /// Read shadow stack pointer
        /// Read time-stamp counter
        /// Read time-stamp counter and processor ID
        rd,
        /// Return
        /// Return from fast system call
        /// Interrupt return
        /// User-interrupt return
        ret,
        /// Rotate left
        /// Rotate right
        /// Rotate right logical without affecting flags
        ro,
        /// Resume from system management mode
        rsm,
        /// Arithmetic shift left
        /// Arithmetic shift right
        /// Shift left arithmetic without affecting flags
        sa,
        /// Store AH into flags
        sahf,
        /// Integer subtraction with borrow
        sbb,
        /// Scan string
        sca,
        /// Send user interprocessor interrupt
        senduipi,
        /// Serialize instruction execution
        serialize,
        /// Set byte on condition
        set,
        /// Logical shift left
        /// Double precision shift left
        /// Logical shift right
        /// Double precision shift right
        /// Shift left logical without affecting flags
        /// Shift right logical without affecting flags
        sh,
        /// Store interrupt descriptor table register
        sidt,
        /// Store local descriptor table register
        sldt,
        /// Store machine status word
        smsw,
        /// Subtract
        /// Subtract packed integers
        /// Subtract packed single-precision floating-point values
        /// Subtract scalar single-precision floating-point values
        /// Subtract packed double-precision floating-point values
        /// Subtract scalar double-precision floating-point values
        /// Packed single-precision floating-point horizontal subtract
        /// Packed double-precision floating-point horizontal subtract
        /// Packed horizontal subtract
        /// Packed horizontal subtract and saturate
        sub,
        /// Set carry flag
        /// Set direction flag
        /// Set interrupt flag
        /// Store binary coded decimal integer and pop
        /// Store floating-point value
        /// Store integer
        /// Store x87 FPU control word
        /// Store x87 FPU environment
        /// Store x87 FPU status word
        /// Store MXCSR register state
        st,
        /// Store string
        sto,
        /// Test condition
        /// Logical compare
        /// Packed bit test
        @"test",
        /// Undefined instruction
        ud,
        /// User level set up monitor address
        umonitor,
        /// Verify a segment for reading
        /// Verify a segment for writing
        ver,
        /// Write to model specific register
        /// Write to model specific register
        /// Write to model specific register
        /// Write to shadow stack
        /// Write to user shadow stack
        wr,
        /// Exchange and add
        xadd,
        /// Exchange register/memory with register
        /// Exchange register contents
        xch,
        /// Get value of extended control register
        xgetbv,
        /// Table look-up translation
        xlat,
        /// Logical exclusive-or
        /// Bitwise logical xor of packed single-precision floating-point values
        /// Bitwise logical xor of packed double-precision floating-point values
        xor,

        // X87
        /// Compute 2^x-1
        @"2xm1",
        /// Absolute value
        abs,
        /// Change sign
        chs,
        /// Clear exceptions
        clex,
        /// Compare floating-point values
        com,
        /// Compare floating-point values and set EFLAGS
        /// Compare scalar ordered single-precision floating-point values
        /// Compare scalar ordered double-precision floating-point values
        comi,
        /// Cosine
        cos,
        /// Reverse divide
        divr,
        /// Free floating-point register
        free,
        /// Initialize floating-point unit
        init,
        /// Load binary coded decimal integer
        /// Load floating-point value
        /// Load integer
        /// Load constant
        /// Load x87 FPU control word
        /// Load x87 FPU environment
        /// Load MXCSR register state
        ld,
        /// Partial arctangent
        patan,
        /// Partial remainder
        prem,
        /// Partial tangent
        ptan,
        /// Round to integer
        rndint,
        /// Restore x87 FPU state
        /// Restore x87 FPU, MMX, XMM, and MXCSR state
        rstor,
        /// Store x87 FPU state
        /// Save x87 FPU, MMX technology, and MXCSR state
        save,
        /// Scale
        scale,
        /// Sine
        sin,
        /// Sine and cosine
        sincos,
        /// Square root
        /// Square root of packed single-precision floating-point values
        /// Square root of scalar single-precision floating-point value
        /// Square root of packed double-precision floating-point values
        /// Square root of scalar double-precision floating-point value
        sqrt,
        /// Store integer with truncation
        stt,
        /// Reverse subtract
        subr,
        /// Test
        tst,
        /// Unordered compare floating-point values
        ucom,
        /// Unordered compare floating-point values and set EFLAGS
        /// Unordered compare scalar single-precision floating-point values
        /// Unordered compare scalar double-precision floating-point values
        ucomi,
        /// Wait
        /// User level monitor wait
        wait,
        /// Examine floating-point
        xam,
        /// Extract exponent and significand
        xtract,
        /// Compute y * log2x
        /// Compute y * log2(x + 1)
        yl2x,

        // MMX
        /// Pack with signed saturation
        ackssw,
        /// Pack with signed saturation
        ackssd,
        /// Pack with unsigned saturation
        ackusw,
        /// Add packed signed integers with signed saturation
        adds,
        /// Add packed unsigned integers with unsigned saturation
        addus,
        /// Logical and not
        /// Bitwise logical and not of packed single-precision floating-point values
        /// Bitwise logical and not of packed double-precision floating-point values
        andn,
        /// Compare packed data for equal
        cmpeq,
        /// Compare packed data for greater than
        cmpgt,
        /// Empty MMX technology state
        emms,
        /// Multiply and add packed signed and unsigned bytes
        maddubs,
        /// Multiply and add packed integers
        maddw,
        /// Multiply packed signed integers and store low result
        mull,
        /// Multiply packed signed integers and store high result
        mulh,
        /// Shift packed data left logical
        sll,
        /// Shift packed data right arithmetic
        sra,
        /// Shift packed data right logical
        srl,
        /// Subtract packed signed integers with signed saturation
        subs,
        /// Subtract packed unsigned integers with unsigned saturation
        subus,
        /// Unpack high data
        unpckhbw,
        /// Unpack high data
        unpckhdq,
        /// Unpack high data
        unpckhwd,
        /// Unpack low data
        unpcklbw,
        /// Unpack low data
        unpckldq,
        /// Unpack low data
        unpcklwd,

        // SSE
        /// Average packed integers
        avg,
        /// Convert packed doubleword integers to packed single-precision floating-point values
        /// Convert packed doubleword integers to packed double-precision floating-point values
        cvtpi2,
        /// Convert packed single-precision floating-point values to packed doubleword integers
        cvtps2pi,
        /// Convert doubleword integer to scalar single-precision floating-point value
        /// Convert doubleword integer to scalar double-precision floating-point value
        cvtsi2,
        /// Convert scalar single-precision floating-point value to doubleword integer
        cvtss2si,
        /// Convert with truncation packed single-precision floating-point values to packed doubleword integers
        cvttps2pi,
        /// Convert with truncation scalar single-precision floating-point value to doubleword integer
        cvttss2si,
        /// Extract byte
        /// Extract word
        /// Extract doubleword
        /// Extract quadword
        extr,
        /// Insert byte
        /// Insert word
        /// Insert doubleword
        /// Insert quadword
        insr,
        /// Maximum of packed single-precision floating-point values
        /// Maximum of scalar single-precision floating-point values
        /// Maximum of packed double-precision floating-point values
        /// Maximum of scalar double-precision floating-point values
        max,
        /// Maximum of packed signed integers
        maxs,
        /// Maximum of packed unsigned integers
        maxu,
        /// Minimum of packed single-precision floating-point values
        /// Minimum of scalar single-precision floating-point values
        /// Minimum of packed double-precision floating-point values
        /// Minimum of scalar double-precision floating-point values
        min,
        /// Minimum of packed signed integers
        mins,
        /// Minimum of packed unsigned integers
        minu,
        /// Move aligned packed single-precision floating-point values
        /// Move aligned packed double-precision floating-point values
        mova,
        /// Move high packed single-precision floating-point values
        /// Move high packed double-precision floating-point values
        movh,
        /// Move packed single-precision floating-point values high to low
        movhl,
        /// Move low packed single-precision floating-point values
        /// Move low packed double-precision floating-point values
        movl,
        /// Move packed single-precision floating-point values low to high
        movlh,
        /// Move byte mask
        /// Extract packed single precision floating-point sign mask
        /// Extract packed double precision floating-point sign mask
        movmsk,
        /// Move unaligned packed single-precision floating-point values
        /// Move unaligned packed double-precision floating-point values
        movu,
        /// Multiply packed unsigned integers and store high result
        mulhu,
        /// Prefetch data into caches
        /// Prefetch data into caches with intent to write
        prefetch,
        /// Compute sum of absolute differences
        sadb,
        /// Packed interleave shuffle of quadruplets of single-precision floating-point values
        /// Packed interleave shuffle of pairs of double-precision floating-point values
        /// Shuffle packed doublewords
        /// Shuffle packed words
        shuf,
        /// Unpack and interleave high packed single-precision floating-point values
        /// Unpack and interleave high packed double-precision floating-point values
        unpckh,
        /// Unpack and interleave low packed single-precision floating-point values
        /// Unpack and interleave low packed double-precision floating-point values
        unpckl,

        // SSE2
        /// Convert packed doubleword integers to packed single-precision floating-point values
        /// Convert packed doubleword integers to packed double-precision floating-point values
        cvtdq2,
        /// Convert packed double-precision floating-point values to packed doubleword integers
        cvtpd2dq,
        /// Convert packed double-precision floating-point values to packed doubleword integers
        cvtpd2pi,
        /// Convert packed double-precision floating-point values to packed single-precision floating-point values
        cvtpd2,
        /// Convert packed single-precision floating-point values to packed doubleword integers
        cvtps2dq,
        /// Convert packed single-precision floating-point values to packed double-precision floating-point values
        cvtps2,
        /// Convert scalar double-precision floating-point value to doubleword integer
        cvtsd2si,
        /// Convert scalar double-precision floating-point value to scalar single-precision floating-point value
        cvtsd2,
        /// Convert scalar single-precision floating-point value to scalar double-precision floating-point value
        cvtss2,
        /// Convert with truncation packed double-precision floating-point values to packed doubleword integers
        cvttpd2dq,
        /// Convert with truncation packed double-precision floating-point values to packed doubleword integers
        cvttpd2pi,
        /// Convert with truncation packed single-precision floating-point values to packed doubleword integers
        cvttps2dq,
        /// Convert with truncation scalar double-precision floating-point value to doubleword integer
        cvttsd2si,
        /// Galois field affine transformation inverse
        gf2p8affineinvq,
        /// Galois field affine transformation
        gf2p8affineq,
        /// Galois field multiply bytes
        gf2p8mul,
        /// Shuffle packed high words
        shufh,
        /// Shuffle packed low words
        shufl,
        /// Unpack high data
        unpckhqdq,
        /// Unpack low data
        unpcklqdq,

        // SSE3
        /// Packed single-precision floating-point add/subtract
        /// Packed double-precision floating-point add/subtract
        addsub,
        /// Replicate double floating-point values
        movddup,
        /// Replicate single floating-point values
        movshdup,
        /// Replicate single floating-point values
        movsldup,

        // SSSE3
        /// Packed align right
        alignr,
        /// Packed multiply high with round and scale
        mulhrs,
        /// Packed sign
        sign,

        // SSE4.1
        /// Pack with unsigned saturation
        ackusd,
        /// Blend packed single-precision floating-point values
        /// Blend scalar single-precision floating-point values
        /// Blend packed double-precision floating-point values
        /// Blend scalar double-precision floating-point values
        /// Blend packed dwords
        blend,
        /// Variable blend packed single-precision floating-point values
        /// Variable blend scalar single-precision floating-point values
        /// Variable blend packed double-precision floating-point values
        /// Variable blend scalar double-precision floating-point values
        blendv,
        /// Dot product of packed single-precision floating-point values
        /// Dot product of packed double-precision floating-point values
        dp,
        /// Extract packed floating-point values
        /// Extract packed integer values
        extract,
        /// Insert scalar single-precision floating-point value
        /// Insert packed floating-point values
        insert,
        /// Packed horizontal word minimum
        minposu,
        /// Packed move with sign extend
        movsxb,
        movsxd,
        movsxw,
        /// Packed move with zero extend
        movzxb,
        movzxd,
        movzxw,
        /// Round packed single-precision floating-point values
        /// Round scalar single-precision floating-point value
        /// Round packed double-precision floating-point values
        /// Round scalar double-precision floating-point value
        round,

        // SSE4.2
        /// Accumulate CRC32 value
        crc32,

        // AES
        /// Perform one round of an AES decryption flow
        /// Perform ten rounds of AES decryption flow with key locker using 128-bit key
        /// Perform ten rounds of AES decryption flow with key locker using 256-bit key
        /// Perform ten rounds of AES decryption flow with key locker on 8 blocks using 128-bit key
        /// Perform ten rounds of AES decryption flow with key locker on 8 blocks using 256-bit key
        aesdec,
        /// Perform last round of an AES decryption flow
        aesdeclast,
        /// Perform one round of an AES encryption flow
        /// Perform ten rounds of AES encryption flow with key locker using 128-bit key
        /// Perform ten rounds of AES encryption flow with key locker using 256-bit key
        /// Perform ten rounds of AES encryption flow with key locker on 8 blocks using 128-bit key
        /// Perform ten rounds of AES encryption flow with key locker on 8 blocks using 256-bit key
        aesenc,
        /// Perform last round of an AES encryption flow
        aesenclast,
        /// Perform the AES InvMixColumn transformation
        aesimc,
        /// AES round key generation assist
        aeskeygenassist,

        // SHA
        /// Perform four rounds of SHA1 operation
        sha1rnds,
        /// Calculate SHA1 state variable E after four rounds
        sha1nexte,
        /// Perform an intermediate calculation for the next four SHA1 message dwords
        /// Perform a final calculation for the next four SHA1 message dwords
        sha1msg,
        /// Perform an intermediate calculation for the next four SHA256 message dwords
        /// Perform a final calculation for the next four SHA256 message dwords
        sha256msg,
        /// Perform two rounds of SHA256 operation
        sha256rnds,

        // AVX
        /// Load with broadcast floating-point data
        /// Load integer and broadcast
        broadcast,
        /// Conditional SIMD packed loads and stores
        /// Condition SIMD integer packed loads and stores
        maskmov,
        /// Permute floating-point values
        /// Permute integer values
        perm2,
        /// Permute in-lane pairs of double-precision floating-point values
        /// Permute in-lane quadruples of single-precision floating-point values
        permil,

        // BMI
        /// Bit field extract
        bextr,
        /// Extract lowest set isolated bit
        /// Get mask up to lowest set bit
        /// Reset lowest set bit
        bls,
        /// Count the number of trailing zero bits
        tzcnt,

        // BMI2
        /// Zero high bits starting with specified bit position
        bzhi,
        /// Parallel bits deposit
        pdep,
        /// Parallel bits extract
        pext,

        // F16C
        /// Convert 16-bit floating-point values to single-precision floating-point values
        cvtph2,
        /// Convert single-precision floating-point values to 16-bit floating-point values
        cvtps2ph,

        // FMA
        /// Fused multiply-add of packed single-precision floating-point values
        /// Fused multiply-add of scalar single-precision floating-point values
        /// Fused multiply-add of packed double-precision floating-point values
        /// Fused multiply-add of scalar double-precision floating-point values
        fmadd132,
        /// Fused multiply-add of packed single-precision floating-point values
        /// Fused multiply-add of scalar single-precision floating-point values
        /// Fused multiply-add of packed double-precision floating-point values
        /// Fused multiply-add of scalar double-precision floating-point values
        fmadd213,
        /// Fused multiply-add of packed single-precision floating-point values
        /// Fused multiply-add of scalar single-precision floating-point values
        /// Fused multiply-add of packed double-precision floating-point values
        /// Fused multiply-add of scalar double-precision floating-point values
        fmadd231,

        // AVX2
        /// Permute packed doubleword elements
        /// Permute packed qword elements
        /// Permute double-precision floating-point elements
        /// Permute single-precision floating-point elements
        perm,
        /// Variable bit shift left logical
        sllv,
        /// Variable bit shift right arithmetic
        srav,
        /// Variable bit shift right logical
        srlv,

        // ADX
        /// Unsigned integer addition of two operands with overflow flag
        ado,

        // AESKLE
        /// Encode 128-bit key with key locker
        /// Encode 256-bit key with key locker
        encodekey,
        /// Load internal wrapping key with key locker
        loadiwkey,

        /// A pseudo instruction that requires special lowering.
        /// This should be the only tag in this enum that doesn't
        /// directly correspond to one or more instruction mnemonics.
        pseudo,
    };

    pub const FixedTag = struct { Fixes, Tag };

    pub const Ops = enum(u8) {
        /// No data associated with this instruction (only mnemonic is used).
        none,
        /// Single register operand.
        /// Uses `r` payload.
        r,
        /// Register, register operands.
        /// Uses `rr` payload.
        rr,
        /// Register, register, register operands.
        /// Uses `rrr` payload.
        rrr,
        /// Register, register, register, register operands.
        /// Uses `rrrr` payload.
        rrrr,
        /// Register, register, register, immediate (byte) operands.
        /// Uses `rrri` payload.
        rrri,
        /// Register, register, immediate (sign-extended) operands.
        /// Uses `rri`  payload.
        rri_s,
        /// Register, register, immediate (unsigned) operands.
        /// Uses `rri`  payload.
        rri_u,
        /// Register, immediate (sign-extended) operands.
        /// Uses `ri` payload.
        ri_s,
        /// Register, immediate (unsigned) operands.
        /// Uses `ri` payload.
        ri_u,
        /// Register, 64-bit unsigned immediate operands.
        /// Uses `ri` payload with `i` index of extra data of type `Imm64`.
        ri_64,
        /// Immediate (sign-extended) operand.
        /// Uses `i` payload.
        i_s,
        /// Immediate (unsigned) operand.
        /// Uses `i` payload.
        i_u,
        /// Immediate (word), immediate (byte) operands.
        /// Uses `ii` payload.
        ii,
        /// Immediate (byte), register operands.
        /// Uses `ri` payload.
        ir,
        /// Register, memory operands.
        /// Uses `rx` payload with extra data of type `Memory`.
        rm,
        /// Register, memory, register operands.
        /// Uses `rrx` payload with extra data of type `Memory`.
        rmr,
        /// Register, memory, immediate (word) operands.
        /// Uses `rix` payload with extra data of type `Memory`.
        rmi,
        /// Register, memory, immediate (signed) operands.
        /// Uses `rx` payload with extra data of type `Imm32` followed by `Memory`.
        rmi_s,
        /// Register, memory, immediate (unsigned) operands.
        /// Uses `rx` payload with extra data of type `Imm32` followed by `Memory`.
        rmi_u,
        /// Register, register, memory.
        /// Uses `rrix` payload with extra data of type `Memory`.
        rrm,
        /// Register, register, memory, register.
        /// Uses `rrrx` payload with extra data of type `Memory`.
        rrmr,
        /// Register, register, memory, immediate (byte) operands.
        /// Uses `rrix` payload with extra data of type `Memory`.
        rrmi,
        /// Single memory operand.
        /// Uses `x` payload with extra data of type `Memory`.
        m,
        /// Memory, immediate (sign-extend) operands.
        /// Uses `x` payload with extra data of type `Imm32` followed by `Memory`.
        mi_s,
        /// Memory, immediate (unsigned) operands.
        /// Uses `x` payload with extra data of type `Imm32` followed by `Memory`.
        mi_u,
        /// Memory, register operands.
        /// Uses `rx` payload with extra data of type `Memory`.
        mr,
        /// Memory, register, register operands.
        /// Uses `rrx` payload with extra data of type `Memory`.
        mrr,
        /// Memory, register, immediate (word) operands.
        /// Uses `rix` payload with extra data of type `Memory`.
        mri,
        /// References another Mir instruction directly.
        /// Uses `inst` payload.
        inst,
        /// References a nav.
        /// Uses `nav` payload.
        nav,
        /// References an uav.
        /// Uses `uav` payload.
        uav,
        /// References a lazy symbol.
        /// Uses `lazy_sym` payload.
        lazy_sym,
        /// References an external symbol.
        /// Uses `extern_func` payload.
        extern_func,

        // Pseudo instructions:

        /// Conditional move if zero flag set and parity flag not set
        /// Clobbers the source operand!
        /// Uses `rr` payload.
        pseudo_cmov_z_and_np_rr,
        /// Conditional move if zero flag not set or parity flag set
        /// Uses `rr` payload.
        pseudo_cmov_nz_or_p_rr,
        /// Conditional move if zero flag not set or parity flag set
        /// Uses `rx` payload.
        pseudo_cmov_nz_or_p_rm,
        /// Set byte if zero flag set and parity flag not set
        /// Requires a scratch register!
        /// Uses `rr` payload.
        pseudo_set_z_and_np_r,
        /// Set byte if zero flag set and parity flag not set
        /// Requires a scratch register!
        /// Uses `rx` payload.
        pseudo_set_z_and_np_m,
        /// Set byte if zero flag not set or parity flag set
        /// Requires a scratch register!
        /// Uses `rr` payload.
        pseudo_set_nz_or_p_r,
        /// Set byte if zero flag not set or parity flag set
        /// Requires a scratch register!
        /// Uses `rx` payload.
        pseudo_set_nz_or_p_m,
        /// Jump if zero flag set and parity flag not set
        /// Uses `inst` payload.
        pseudo_j_z_and_np_inst,
        /// Jump if zero flag not set or parity flag set
        /// Uses `inst` payload.
        pseudo_j_nz_or_p_inst,

        /// Probe alignment
        /// Uses `ri` payload.
        pseudo_probe_align_ri_s,
        /// Probe adjust unrolled
        /// Uses `ri` payload.
        pseudo_probe_adjust_unrolled_ri_s,
        /// Probe adjust setup
        /// Uses `rri` payload.
        pseudo_probe_adjust_setup_rri_s,
        /// Probe adjust loop
        /// Uses `rr` payload.
        pseudo_probe_adjust_loop_rr,

        /// Push registers
        /// Uses `reg_list` payload.
        pseudo_push_reg_list,
        /// Pop registers
        /// Uses `reg_list` payload.
        pseudo_pop_reg_list,

        /// Define cfa rule as offset from register.
        /// Uses `ri` payload.
        pseudo_cfi_def_cfa_ri_s,
        /// Modify cfa rule register.
        /// Uses `r` payload.
        pseudo_cfi_def_cfa_register_r,
        /// Modify cfa rule offset.
        /// Uses `i` payload.
        pseudo_cfi_def_cfa_offset_i_s,
        /// Offset cfa rule offset.
        /// Uses `i` payload.
        pseudo_cfi_adjust_cfa_offset_i_s,
        /// Define register rule as stored at offset from cfa.
        /// Uses `ri` payload.
        pseudo_cfi_offset_ri_s,
        /// Define register rule as offset from cfa.
        /// Uses `ri` payload.
        pseudo_cfi_val_offset_ri_s,
        /// Define register rule as stored at offset from cfa rule register.
        /// Uses `ri` payload.
        pseudo_cfi_rel_offset_ri_s,
        /// Define register rule as register.
        /// Uses `rr` payload.
        pseudo_cfi_register_rr,
        /// Define register rule from initial.
        /// Uses `r` payload.
        pseudo_cfi_restore_r,
        /// Define register rule as undefined.
        /// Uses `r` payload.
        pseudo_cfi_undefined_r,
        /// Define register rule as itself.
        /// Uses `r` payload.
        pseudo_cfi_same_value_r,
        /// Push cfi state.
        pseudo_cfi_remember_state_none,
        /// Pop cfi state.
        pseudo_cfi_restore_state_none,
        /// Raw cfi bytes.
        /// Uses `bytes` payload.
        pseudo_cfi_escape_bytes,

        /// End of prologue
        /// Uses `none` payload.
        pseudo_dbg_prologue_end_none,
        /// Update debug line with is_stmt register set
        /// Uses `line_column` payload.
        pseudo_dbg_line_stmt_line_column,
        /// Update debug line with is_stmt register clear
        /// Uses `line_column` payload.
        pseudo_dbg_line_line_column,
        /// Start of epilogue
        /// Uses `none` payload.
        pseudo_dbg_epilogue_begin_none,
        /// Start of lexical block
        /// Uses `none` payload.
        pseudo_dbg_enter_block_none,
        /// End of lexical block
        /// Uses `none` payload.
        pseudo_dbg_leave_block_none,
        /// Start of inline function
        /// Uses `ip_index` payload.
        pseudo_dbg_enter_inline_func,
        /// End of inline function
        /// Uses `ip_index` payload.
        pseudo_dbg_leave_inline_func,
        /// Local argument.
        /// Uses `none` payload.
        pseudo_dbg_arg_none,
        /// Local argument.
        /// Uses `i` payload.
        pseudo_dbg_arg_i_s,
        /// Local argument.
        /// Uses `i` payload.
        pseudo_dbg_arg_i_u,
        /// Local argument.
        /// Uses `i64` payload.
        pseudo_dbg_arg_i_64,
        /// Local argument.
        /// Uses `ro` payload.
        pseudo_dbg_arg_ro,
        /// Local argument.
        /// Uses `fa` payload.
        pseudo_dbg_arg_fa,
        /// Local argument.
        /// Uses `x` payload with extra data of type `Memory`.
        pseudo_dbg_arg_m,
        /// Local argument.
        /// Uses `ip_index` payload.
        pseudo_dbg_arg_val,
        /// Remaining arguments are varargs.
        pseudo_dbg_var_args_none,
        /// Local variable.
        /// Uses `none` payload.
        pseudo_dbg_var_none,
        /// Local variable.
        /// Uses `i` payload.
        pseudo_dbg_var_i_s,
        /// Local variable.
        /// Uses `i` payload.
        pseudo_dbg_var_i_u,
        /// Local variable.
        /// Uses `i64` payload.
        pseudo_dbg_var_i_64,
        /// Local variable.
        /// Uses `ro` payload.
        pseudo_dbg_var_ro,
        /// Local variable.
        /// Uses `fa` payload.
        pseudo_dbg_var_fa,
        /// Local variable.
        /// Uses `x` payload with extra data of type `Memory`.
        pseudo_dbg_var_m,
        /// Local variable.
        /// Uses `ip_index` payload.
        pseudo_dbg_var_val,

        /// Tombstone
        /// Emitter should skip this instruction.
        pseudo_dead_none,
    };

    pub const Data = union {
        none: struct {
            fixes: Fixes = ._,
        },
        /// References another Mir instruction.
        inst: struct {
            fixes: Fixes = ._,
            inst: Index,
        },
        /// A 32-bit immediate value.
        i64: u64,
        i: struct {
            fixes: Fixes = ._,
            i: u32,
        },
        ii: struct {
            fixes: Fixes = ._,
            i1: u16,
            i2: u8,
        },
        r: struct {
            fixes: Fixes = ._,
            r1: Register,
        },
        rr: struct {
            fixes: Fixes = ._,
            r1: Register,
            r2: Register,
        },
        rrr: struct {
            fixes: Fixes = ._,
            r1: Register,
            r2: Register,
            r3: Register,
        },
        rrrr: struct {
            fixes: Fixes = ._,
            r1: Register,
            r2: Register,
            r3: Register,
            r4: Register,
        },
        rrri: struct {
            fixes: Fixes = ._,
            r1: Register,
            r2: Register,
            r3: Register,
            i: u8,
        },
        rri: struct {
            fixes: Fixes = ._,
            r1: Register,
            r2: Register,
            i: u32,
        },
        /// Register, immediate.
        ri: struct {
            fixes: Fixes = ._,
            r1: Register,
            i: u32,
        },
        /// Register, followed by custom payload found in extra.
        rx: struct {
            fixes: Fixes = ._,
            r1: Register,
            payload: u32,
        },
        /// Register, register, followed by Custom payload found in extra.
        rrx: struct {
            fixes: Fixes = ._,
            r1: Register,
            r2: Register,
            payload: u32,
        },
        /// Register, register, register, followed by Custom payload found in extra.
        rrrx: struct {
            fixes: Fixes = ._,
            r1: Register,
            r2: Register,
            r3: Register,
            payload: u32,
        },
        /// Register, byte immediate, followed by Custom payload found in extra.
        rix: struct {
            fixes: Fixes = ._,
            r1: Register,
            i: u16,
            payload: u32,
        },
        /// Register, register, byte immediate, followed by Custom payload found in extra.
        rrix: struct {
            fixes: Fixes = ._,
            r1: Register,
            r2: Register,
            i: u8,
            payload: u32,
        },
        /// Custom payload found in extra.
        x: struct {
            fixes: Fixes = ._,
            payload: u32,
        },
        bytes: struct {
            payload: u32,
            len: u32,

            pub fn get(bytes: @This(), mir: Mir) []const u8 {
                return std.mem.sliceAsBytes(mir.extra[bytes.payload..])[0..bytes.len];
            }
        },
        fa: bits.FrameAddr,
        ro: bits.RegisterOffset,
        nav: bits.NavOffset,
        uav: InternPool.Key.Ptr.BaseAddr.Uav,
        lazy_sym: link.File.LazySymbol,
        extern_func: Mir.NullTerminatedString,
        /// Debug line and column position
        line_column: struct {
            line: u32,
            column: u32,
        },
        ip_index: InternPool.Index,
        /// Register list
        reg_list: RegisterList,
    };

    comptime {
        if (!std.debug.runtime_safety) {
            // Make sure we don't accidentally make instructions bigger than expected.
            // Note that in safety builds, Zig is allowed to insert a secret field for safety checks.
            assert(@sizeOf(Data) == 8);
        }
        const Mnemonic = @import("Encoding.zig").Mnemonic;
        if (@typeInfo(Mnemonic).@"enum".fields.len != 977 or
            @typeInfo(Fixes).@"enum".fields.len != 231 or
            @typeInfo(Tag).@"enum".fields.len != 251)
        {
            const cond_src = (struct {
                fn src() std.builtin.SourceLocation {
                    return @src();
                }
            }).src();
            @setEvalBranchQuota(1_750_000);
            for (@typeInfo(Mnemonic).@"enum".fields) |mnemonic| {
                if (mnemonic.name[0] == '.') continue;
                for (@typeInfo(Fixes).@"enum".fields) |fixes| {
                    const pattern = fixes.name[if (std.mem.indexOfScalar(u8, fixes.name, ' ')) |index| index + " ".len else 0..];
                    const wildcard_index = std.mem.indexOfScalar(u8, pattern, '_').?;
                    const mnem_prefix = pattern[0..wildcard_index];
                    const mnem_suffix = pattern[wildcard_index + "_".len ..];
                    if (!std.mem.startsWith(u8, mnemonic.name, mnem_prefix)) continue;
                    if (!std.mem.endsWith(u8, mnemonic.name, mnem_suffix)) continue;
                    if (@hasField(
                        Tag,
                        mnemonic.name[mnem_prefix.len .. mnemonic.name.len - mnem_suffix.len],
                    )) break;
                } else @compileError("'" ++ mnemonic.name ++ "' is not encodable in Mir");
            }
            @compileError(std.fmt.comptimePrint(
                \\All mnemonics are encodable in Mir! You may now change the condition at {s}:{d} to:
                \\if (@typeInfo(Mnemonic).@"enum".fields.len != {d} or
                \\    @typeInfo(Fixes).@"enum".fields.len != {d} or
                \\    @typeInfo(Tag).@"enum".fields.len != {d})
            , .{
                cond_src.file,
                cond_src.line - 6,
                @typeInfo(Mnemonic).@"enum".fields.len,
                @typeInfo(Fixes).@"enum".fields.len,
                @typeInfo(Tag).@"enum".fields.len,
            }));
        }
    }
};

/// Used in conjunction with payload to transfer a list of used registers in a compact manner.
pub const RegisterList = struct {
    bitset: BitSet,

    const BitSet = std.bit_set.IntegerBitSet(32);
    const Self = @This();

    pub const empty: RegisterList = .{ .bitset = .initEmpty() };

    fn getIndexForReg(registers: []const Register, reg: Register) BitSet.MaskInt {
        for (registers, 0..) |cpreg, i| {
            if (reg.id() == cpreg.id()) return @intCast(i);
        }
        unreachable; // register not in input register list!
    }

    pub fn push(self: *Self, registers: []const Register, reg: Register) void {
        const index = getIndexForReg(registers, reg);
        self.bitset.set(index);
    }

    pub fn isSet(self: Self, registers: []const Register, reg: Register) bool {
        const index = getIndexForReg(registers, reg);
        return self.bitset.isSet(index);
    }

    pub fn iterator(self: Self, comptime options: std.bit_set.IteratorOptions) BitSet.Iterator(options) {
        return self.bitset.iterator(options);
    }

    pub fn count(self: Self) i32 {
        return @intCast(self.bitset.count());
    }

    pub fn size(self: Self, target: *const std.Target) i32 {
        return @intCast(self.bitset.count() * @as(u4, switch (target.cpu.arch) {
            else => unreachable,
            .x86 => 4,
            .x86_64 => 8,
        }));
    }
};

pub const NullTerminatedString = enum(u32) {
    none = std.math.maxInt(u32),
    _,

    pub fn toSlice(nts: NullTerminatedString, mir: *const Mir) ?[:0]const u8 {
        if (nts == .none) return null;
        const string_bytes = mir.string_bytes[@intFromEnum(nts)..];
        return string_bytes[0..std.mem.indexOfScalar(u8, string_bytes, 0).? :0];
    }
};

pub const Local = struct {
    name: NullTerminatedString,
    type: InternPool.Index,
};

pub const Imm32 = struct {
    imm: u32,
};

pub const Imm64 = struct {
    msb: u32,
    lsb: u32,

    pub fn encode(v: u64) Imm64 {
        return .{
            .msb = @truncate(v >> 32),
            .lsb = @truncate(v),
        };
    }

    pub fn decode(imm: Imm64) u64 {
        var res: u64 = 0;
        res |= @as(u64, @intCast(imm.msb)) << 32;
        res |= @as(u64, @intCast(imm.lsb));
        return res;
    }
};

pub const Memory = struct {
    info: Info,
    base: u32,
    off: u32,
    extra: u32,

    pub const Info = packed struct(u32) {
        base: @typeInfo(bits.Memory.Base).@"union".tag_type.?,
        mod: @typeInfo(bits.Memory.Mod).@"union".tag_type.?,
        size: bits.Memory.Size,
        index: Register,
        scale: bits.Memory.Scale,
        _: u13 = undefined,
    };

    pub fn encode(mem: bits.Memory) Memory {
        return .{
            .info = .{
                .base = mem.base,
                .mod = mem.mod,
                .size = switch (mem.mod) {
                    .rm => |rm| rm.size,
                    .off => undefined,
                },
                .index = switch (mem.mod) {
                    .rm => |rm| rm.index,
                    .off => undefined,
                },
                .scale = switch (mem.mod) {
                    .rm => |rm| rm.scale,
                    .off => undefined,
                },
            },
            .base = switch (mem.base) {
                .none, .table => undefined,
                .reg => |reg| @intFromEnum(reg),
                .frame => |frame_index| @intFromEnum(frame_index),
                .rip_inst => |inst_index| inst_index,
                .nav => |nav| @intFromEnum(nav),
                .uav => |uav| @intFromEnum(uav.val),
                .lazy_sym => |lazy_sym| @intFromEnum(lazy_sym.ty),
                .extern_func => |extern_func| @intFromEnum(extern_func),
            },
            .off = switch (mem.mod) {
                .rm => |rm| @bitCast(rm.disp),
                .off => |off| @truncate(off),
            },
            .extra = switch (mem.mod) {
                .rm => switch (mem.base) {
                    else => undefined,
                    .uav => |uav| @intFromEnum(uav.orig_ty),
                    .lazy_sym => |lazy_sym| @intFromEnum(lazy_sym.kind),
                },
                .off => switch (mem.base) {
                    .reg => @intCast(mem.mod.off >> 32),
                    else => unreachable,
                },
            },
        };
    }

    pub fn decode(mem: Memory) encoder.Instruction.Memory {
        switch (mem.info.mod) {
            .rm => {
                if (mem.info.base == .reg and @as(Register, @enumFromInt(mem.base)) == .rip) {
                    assert(mem.info.index == .none and mem.info.scale == .@"1");
                    return encoder.Instruction.Memory.initRip(mem.info.size, @bitCast(mem.off));
                }
                return encoder.Instruction.Memory.initSib(mem.info.size, .{
                    .disp = @bitCast(mem.off),
                    .base = switch (mem.info.base) {
                        .none => .none,
                        .reg => .{ .reg = @enumFromInt(mem.base) },
                        .frame => .{ .frame = @enumFromInt(mem.base) },
                        .table => .table,
                        .rip_inst => .{ .rip_inst = mem.base },
                        .nav => .{ .nav = @enumFromInt(mem.base) },
                        .uav => .{ .uav = .{ .val = @enumFromInt(mem.base), .orig_ty = @enumFromInt(mem.extra) } },
                        .lazy_sym => .{ .lazy_sym = .{ .kind = @enumFromInt(mem.extra), .ty = @enumFromInt(mem.base) } },
                        .extern_func => .{ .extern_func = @enumFromInt(mem.base) },
                    },
                    .scale_index = switch (mem.info.index) {
                        .none => null,
                        else => |index| .{ .scale = switch (mem.info.scale) {
                            inline else => |scale| comptime std.fmt.parseInt(
                                u4,
                                @tagName(scale),
                                10,
                            ) catch unreachable,
                        }, .index = index },
                    },
                });
            },
            .off => {
                assert(mem.info.base == .reg);
                return encoder.Instruction.Memory.initMoffs(
                    @enumFromInt(mem.base),
                    @as(u64, mem.extra) << 32 | mem.off,
                );
            },
        }
    }
};

pub fn deinit(mir: *Mir, gpa: std.mem.Allocator) void {
    mir.instructions.deinit(gpa);
    gpa.free(mir.extra);
    gpa.free(mir.string_bytes);
    gpa.free(mir.locals);
    gpa.free(mir.table);
    mir.frame_locs.deinit(gpa);
    mir.* = undefined;
}

pub fn emit(
    mir: Mir,
    lf: *link.File,
    pt: Zcu.PerThread,
    src_loc: Zcu.LazySrcLoc,
    func_index: InternPool.Index,
    code: *std.ArrayListUnmanaged(u8),
    debug_output: link.File.DebugInfoOutput,
) codegen.CodeGenError!void {
    const zcu = pt.zcu;
    const comp = zcu.comp;
    const gpa = comp.gpa;
    const func = zcu.funcInfo(func_index);
    const fn_info = zcu.typeToFunc(.fromInterned(func.ty)).?;
    const nav = func.owner_nav;
    const mod = zcu.navFileScope(nav).mod.?;
    var e: Emit = .{
        .lower = .{
            .target = &mod.resolved_target.result,
            .allocator = gpa,
            .mir = mir,
            .cc = fn_info.cc,
            .src_loc = src_loc,
        },
        .bin_file = lf,
        .pt = pt,
        .pic = mod.pic,
        .atom_index = sym: {
            if (lf.cast(.elf)) |ef| break :sym try ef.zigObjectPtr().?.getOrCreateMetadataForNav(zcu, nav);
            if (lf.cast(.macho)) |mf| break :sym try mf.getZigObject().?.getOrCreateMetadataForNav(mf, nav);
            if (lf.cast(.coff)) |cf| {
                const atom = try cf.getOrCreateAtomForNav(nav);
                break :sym cf.getAtom(atom).getSymbolIndex().?;
            }
            if (lf.cast(.plan9)) |p9f| break :sym try p9f.seeNav(pt, nav);
            unreachable;
        },
        .debug_output = debug_output,
        .code = code,

        .prev_di_loc = .{
            .line = func.lbrace_line,
            .column = func.lbrace_column,
            .is_stmt = switch (debug_output) {
                .dwarf => |dwarf| dwarf.dwarf.debug_line.header.default_is_stmt,
                .plan9 => undefined,
                .none => undefined,
            },
        },
        .prev_di_pc = 0,

        .code_offset_mapping = .empty,
        .relocs = .empty,
        .table_relocs = .empty,
    };
    defer e.deinit();
    e.emitMir() catch |err| switch (err) {
        error.LowerFail, error.EmitFail => return zcu.codegenFailMsg(nav, e.lower.err_msg.?),
        error.InvalidInstruction, error.CannotEncode => return zcu.codegenFail(nav, "emit MIR failed: {s} (Zig compiler bug)", .{@errorName(err)}),
        else => return zcu.codegenFail(nav, "emit MIR failed: {s}", .{@errorName(err)}),
    };
}

pub fn emitLazy(
    mir: Mir,
    lf: *link.File,
    pt: Zcu.PerThread,
    src_loc: Zcu.LazySrcLoc,
    lazy_sym: link.File.LazySymbol,
    code: *std.ArrayListUnmanaged(u8),
    debug_output: link.File.DebugInfoOutput,
) codegen.CodeGenError!void {
    const zcu = pt.zcu;
    const comp = zcu.comp;
    const gpa = comp.gpa;
    const mod = comp.root_mod;
    var e: Emit = .{
        .lower = .{
            .target = &mod.resolved_target.result,
            .allocator = gpa,
            .mir = mir,
            .cc = .auto,
            .src_loc = src_loc,
        },
        .bin_file = lf,
        .pt = pt,
        .pic = mod.pic,
        .atom_index = sym: {
            if (lf.cast(.elf)) |ef| break :sym ef.zigObjectPtr().?.getOrCreateMetadataForLazySymbol(ef, pt, lazy_sym) catch |err|
                return zcu.codegenFailType(lazy_sym.ty, "{s} creating lazy symbol", .{@errorName(err)});
            if (lf.cast(.macho)) |mf| break :sym mf.getZigObject().?.getOrCreateMetadataForLazySymbol(mf, pt, lazy_sym) catch |err|
                return zcu.codegenFailType(lazy_sym.ty, "{s} creating lazy symbol", .{@errorName(err)});
            if (lf.cast(.coff)) |cf| {
                const atom = cf.getOrCreateAtomForLazySymbol(pt, lazy_sym) catch |err|
                    return zcu.codegenFailType(lazy_sym.ty, "{s} creating lazy symbol", .{@errorName(err)});
                break :sym cf.getAtom(atom).getSymbolIndex().?;
            }
            if (lf.cast(.plan9)) |p9f| break :sym p9f.getOrCreateAtomForLazySymbol(pt, lazy_sym) catch |err|
                return zcu.codegenFailType(lazy_sym.ty, "{s} creating lazy symbol", .{@errorName(err)});
            unreachable;
        },
        .debug_output = debug_output,
        .code = code,

        .prev_di_loc = undefined,
        .prev_di_pc = undefined,

        .code_offset_mapping = .empty,
        .relocs = .empty,
        .table_relocs = .empty,
    };
    defer e.deinit();
    e.emitMir() catch |err| switch (err) {
        error.LowerFail, error.EmitFail => return zcu.codegenFailTypeMsg(lazy_sym.ty, e.lower.err_msg.?),
        error.InvalidInstruction, error.CannotEncode => return zcu.codegenFailType(lazy_sym.ty, "emit MIR failed: {s} (Zig compiler bug)", .{@errorName(err)}),
        else => return zcu.codegenFailType(lazy_sym.ty, "emit MIR failed: {s}", .{@errorName(err)}),
    };
}

pub fn extraData(mir: Mir, comptime T: type, index: u32) struct { data: T, end: u32 } {
    const fields = std.meta.fields(T);
    var i: u32 = index;
    var result: T = undefined;
    inline for (fields) |field| {
        @field(result, field.name) = switch (field.type) {
            u32 => mir.extra[i],
            i32, Memory.Info => @bitCast(mir.extra[i]),
            bits.FrameIndex => @enumFromInt(mir.extra[i]),
            else => @compileError("bad field type: " ++ field.name ++ ": " ++ @typeName(field.type)),
        };
        i += 1;
    }
    return .{
        .data = result,
        .end = i,
    };
}

pub const FrameLoc = struct {
    base: Register,
    disp: i32,
};

pub fn resolveFrameAddr(mir: Mir, frame_addr: bits.FrameAddr) bits.RegisterOffset {
    const frame_loc = mir.frame_locs.get(@intFromEnum(frame_addr.index));
    return .{ .reg = frame_loc.base, .off = frame_loc.disp + frame_addr.off };
}

pub fn resolveMemoryExtra(mir: Mir, payload: u32) Memory {
    const mem = mir.extraData(Mir.Memory, payload).data;
    return switch (mem.info.base) {
        .none, .reg, .table, .rip_inst, .nav, .uav, .lazy_sym, .extern_func => mem,
        .frame => if (mir.frame_locs.len > 0) .{
            .info = .{
                .base = .reg,
                .mod = mem.info.mod,
                .size = mem.info.size,
                .index = mem.info.index,
                .scale = mem.info.scale,
            },
            .base = @intFromEnum(mir.frame_locs.items(.base)[mem.base]),
            .off = @bitCast(mir.frame_locs.items(.disp)[mem.base] + @as(i32, @bitCast(mem.off))),
            .extra = mem.extra,
        } else mem,
    };
}

const assert = std.debug.assert;
const bits = @import("bits.zig");
const builtin = @import("builtin");
const encoder = @import("encoder.zig");
const std = @import("std");

const InternPool = @import("../../InternPool.zig");
const Mir = @This();
const Register = bits.Register;
const Emit = @import("Emit.zig");
const codegen = @import("../../codegen.zig");
const link = @import("../../link.zig");
const Zcu = @import("../../Zcu.zig");