macroassembler 1.0.5

A library for writing portable x86-64/riscv64/aarch64 assembly code in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
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
//! RISC-V 64 bit assembler implementation
#![allow(non_upper_case_globals, non_camel_case_types)]

use std::mem::size_of;

use super::buffer::{AssemblerBuffer, AssemblerLabel};

pub struct RISCV64Assembler {
    pub buffer: AssemblerBuffer,
    pub index_of_last_watchpoint: i32,
    pub index_of_tail_of_last_watchpoint: i32,
}

impl RISCV64Assembler {
    pub fn buffer(&self) -> &AssemblerBuffer {
        &self.buffer
    }

    pub fn buffer_mut(&mut self) -> &mut AssemblerBuffer {
        &mut self.buffer
    }

    pub fn new() -> Self {
        Self {
            buffer: AssemblerBuffer::new(),
            index_of_last_watchpoint: i32::MIN,
            index_of_tail_of_last_watchpoint: i32::MIN,
        }
    }

    pub fn code_size(&self) -> usize {
        self.buffer.code_size()
    }

    pub fn debug_offset(&self) -> usize {
        self.buffer.debug_offset()
    }
}

macro_rules! decl_gpr {
    (@internal $counter: expr, ($id: ident, $l: expr, $x: expr, $y: expr), $($rest: tt)*) => (
        pub const $id: u8 = $counter;
        decl_gpr!(@internal $counter + 1, $($rest)*);
    );
    (@internal $counter: expr,) => (
        pub const INVALID_GPR: u8 = 0xFF;
    );
    (($id: ident, $l: expr, $x: expr, $y: expr), $($rest: tt)*) => (
        decl_gpr!(@internal 0, ($id, $l, $x, $y), $($rest)*);
    );
}

macro_rules! decl_xmm {
    (@internal $counter: expr, ($id: ident, $l: expr, $x: expr, $y: expr), $($rest: tt)*) => (
        pub const $id: u8 = $counter;
        decl_xmm!(@internal $counter + 1, $($rest)*);
    );
    (@internal $counter: expr,) => (
        pub const INVALID_FPR: u8 = 0xFF;
    );
    (($id: ident, $l: expr, $x: expr, $y: expr), $($rest: tt)*) => (
        decl_xmm!(@internal 0, ($id, $l, $x, $y), $($rest)*);
    );
}
macro_rules! decl_sp {
    (@internal $counter: expr, ($id: ident, $l: expr, $x: expr, $y: expr), $($rest: tt)*) => (
        pub const $id: u8 = $counter;
        decl_sp!(@internal $counter + 1, $($rest)*);
    );
    (@internal $counter: expr,) => (
        pub const INVALID_SP: u8 = 0xFF;
    );
    (($id: ident, $l: expr, $x: expr, $y: expr), $($rest: tt)*) => (
        decl_sp!(@internal 0, ($id, $l, $x, $y), $($rest)*);
    );
}

for_each_gp_register!(decl_gpr);
for_each_fp_register!(decl_xmm);
for_each_sp_register!(decl_sp);

macro_rules! decl_alias {
    ($(($id: ident, $l: expr, $x: ident)),+) => {
        $(
            pub const $id: u8 = $x;
        )*

    };
}

for_each_register_alias!(decl_alias);

macro_rules! cenum {
    ($($id: ident = $val: expr), *) => {
        $(pub const $id: u32 = $val;)*
    };
}

cenum! {
    LOAD = 0b0000011,
    LOAD_FP = 0b0000111,
    MISC_MEM = 0b0001111,
    OP_IMM = 0b0010011,
    AUIPC = 0b0010111,
    OP_IMM_32 = 0b0011011,
    STORE = 0b0100011,
    STORE_FP = 0b0100111,
    AMO = 0b0101111,
    OP = 0b0110011,
    LUI = 0b0110111,
    OP_32 = 0b0111011,
    MADD = 0b1000011,
    MSUB = 0b1000111,
    NMSUB = 0b1001011,
    NMADD = 0b1001111,
    OP_FP = 0b1010011,
    BRANCH = 0b1100011,
    JALR = 0b1100111,
    JAL = 0b1101111,
    SYSTEM = 0b1110011
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[rustfmt::skip]
pub enum FCVTType {
    W, WU,
    L, LU,
    S, D
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[rustfmt::skip]
pub enum FMVType {
    X, W, D
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[rustfmt::skip]
pub enum FPRoundingMode {
    RNE = 0b000,
    RTZ = 0b001,
    RDN = 0b010,    
    RUP = 0b011,
    RMM = 0b100,
    DYN = 0b111
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[rustfmt::skip]
#[repr(u8)]
pub enum MemoryOperation {
    I = 1 << 3,
    O = 1 << 2,
    R = 1 << 1,
    W = 1 << 0,
    RW = Self::R as u8 | Self::W as u8,
    IORW = Self::I as u8 | Self::O as u8 | Self::RW as u8
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[rustfmt::skip]
#[repr(u8)]
pub enum MemoryAccess {
    Acquire = 1 << 1,
    Release = 1 << 0,
    AcquireRelease = Self::Acquire as u8 | Self::Release as u8
}

pub const fn register_value(register_id: u8) -> u32 {
    (register_id as u32) & ((1 << 5) - 1)
}

/// InstructionValue contains the 32-bit instruction value and also provides access into the desired field.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct InstructionValue {
    pub value: u32,
}

impl InstructionValue {
    pub const fn new(value: u32) -> Self {
        Self { value }
    }

    pub const fn field<const FIELD_START: usize, const FIELD_SIZE: usize>(self) -> u32 {
        (self.value >> FIELD_START) & ((1 << FIELD_SIZE) - 1)
    }

    pub const fn opcode(self) -> u32 {
        self.field::<0, 7>()
    }
}

macro_rules! decl_immediate {
    ($name: ident : $immediate_size: expr, $value: item, $ctor: item) => {
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        #[repr(transparent)]
        pub struct $name {
            pub value: u32
        }

        impl $name {
            pub const IMMEDIATE_SIZE: usize = $immediate_size;

            pub fn immediate_mask<T:  num_traits::WrappingSub + num::PrimInt + num::NumCast>() -> T {
                if $immediate_size < std::mem::size_of::<u32>() * 8 {
                    T::from(1u64.wrapping_shl($immediate_size as u32)).unwrap().wrapping_sub(&T::one())
                } else {
                    T::max_value()
                }
            }

            pub fn is_valid<T: num::PrimInt + num::NumCast>(imm_value: T) -> bool {
                let shift = std::mem::size_of::<T>() * 8 - $immediate_size;


                imm_value == T::from((imm_value.to_i64().unwrap() << shift) >> shift).unwrap()
            }

            pub fn v32<T: num::NumCast>(imm_value: i32) -> T {

                assert!(Self::is_valid(imm_value), "Invalid immediate value: {}", imm_value);
                let imm_value = imm_value as u32;
                let mask: u32 = Self::immediate_mask::<u32>();
                T::from(imm_value & mask).unwrap()
            }

            pub fn v64<T: num::NumCast>(imm_value: i64) -> T {
                assert!(Self::is_valid(imm_value));
                let imm_value = imm_value as u64;
                let mask: u64 = Self::immediate_mask::<u64>();
                T::from(imm_value & mask).unwrap()
            }

            pub const fn field<const FIELD_START: usize, const FIELD_SIZE: usize>(self) -> u32 {
                (self.value >> FIELD_START) & ((1 << FIELD_SIZE) - 1)
            }

            $value

            $ctor
        }

        impl num::ToPrimitive for $name {
            fn to_i64(&self) -> Option<i64> {
                Some(self.value as i64)
            }

            fn to_u64(&self) -> Option<u64> {
                Some(self.value as u64)
            }

            fn to_u32(&self) -> Option<u32> {
                Some(self.value)
            }

            fn to_i32(&self) -> Option<i32> {
                Some(self.value as i32)
            }
        }

        impl num::NumCast for $name {
            fn from<T>(n: T) -> Option<Self>
            where T: num::ToPrimitive
            {
                let n = n.to_u32()?;
                if Self::is_valid(n) {
                    Some(Self::new(n))
                } else {
                    None
                }
            }
        }
    };

    ($name: ident : $immediate_size: expr, $value: item) => {
        decl_immediate!($name: $immediate_size, $value, pub fn new(value: u32) -> Self {
            Self { value }
        });
    };
}

decl_immediate!(IImmediate: 12, pub fn value(insn: InstructionValue) -> i32 {
    let base = insn.field::<20, 12>();
    let imm = base as i32;
    (imm << 20) >> 20
});

decl_immediate!(SImmediate: 12, pub fn value(insn: InstructionValue) -> i32 {
    let base = 0
        | (insn.field::<31, 1>() << 11)
        | (insn.field::<25, 6>() << 5)
        | (insn.field::<8, 4>() << 1)
        | (insn.field::<7, 1>() << 0);

    let imm = base as i32;

    (imm << 20) >> 20
});

decl_immediate!(BImmediate: 13, pub fn value(insn: InstructionValue) -> i32 {
    let base = 0
        | (insn.field::<31, 1>() << 12)
        | (insn.field::<7, 1>() << 11)
        | (insn.field::<25, 6>() << 5)
        | (insn.field::<8, 4>() << 1);

    let imm = base as i32;

    (imm << 19) >> 19
});

decl_immediate!(UImmediate: 32,
    pub fn value(insn: InstructionValue) -> i32 {
        insn.field::<12, 20>() as i32
    },

    pub fn new(value: u32) -> Self {
        Self { value: (value >> 12) << 12 }
    }
);

decl_immediate!(JImmediate: 21, pub fn value(insn: InstructionValue) -> i32 {
    let base = 0
        | (insn.field::<31, 1>() << 20)
        | (insn.field::<12, 8>() << 12)
        | (insn.field::<20, 1>() << 11)
        | (insn.field::<25, 6>() << 5)
        | (insn.field::<21, 4>() << 1);

    let imm = base as i32;

    (imm << 11) >> 11
});

pub struct ImmediateDecomposition {
    pub upper: UImmediate,
    pub lower: IImmediate,
}

impl ImmediateDecomposition {
    pub fn new32(mut immediate: i32) -> Self {
        if (immediate & (1 << 11)) != 0 {
            immediate += 1 << 12;
        }

        Self {
            upper: UImmediate::v32(immediate),
            lower: IImmediate::v32((immediate << 20) >> 20),
        }
    }

    pub fn new64(mut immediate: i64) -> Self {
        if (immediate & (1 << 11)) != 0 {
            immediate += 1 << 12;
        }

        Self {
            upper: UImmediate::v64(immediate),
            lower: IImmediate::v64((immediate << 20) >> 20),
        }
    }
}

impl RISCV64Assembler {}

pub struct RTypeBase<const OPCODE: u32, const F3: u32, const F7: u32>;

impl<const OPCODE: u32, const F3: u32, const F7: u32> RTypeBase<OPCODE, F3, F7> {
    pub const FUNCT3_VALUE: u32 = F3;
    pub const FUNCT7_VALUE: u32 = F7;
    pub const OPCODE_VALUE: u32 = OPCODE;

    pub const fn construct(rd: u8, rs1: u8, rs2: u8) -> u32 {
        let instruction = 0
            | (F7 << 25)
            | (register_value(rs2) << 20)
            | (register_value(rs1) << 15)
            | (F3 << 12)
            | (register_value(rd) << 7)
            | OPCODE as u32;

        instruction
    }

    pub const fn matches(insn: InstructionValue) -> bool {
        OPCODE as u32 == insn.opcode() && F3 == insn.field::<12, 3>() && F7 == insn.field::<25, 7>()
    }

    pub const fn rd(insn: InstructionValue) -> u8 {
        insn.field::<7, 5>() as u8
    }

    pub const fn rs1(insn: InstructionValue) -> u8 {
        insn.field::<15, 5>() as u8
    }

    pub const fn rs2(insn: InstructionValue) -> u8 {
        insn.field::<20, 5>() as u8
    }
}

pub struct RTypeBaseWithRoundingMode<const OPCODE: u32, const F7: u32>;

impl<const OPCODE: u32, const F7: u32> RTypeBaseWithRoundingMode<OPCODE, F7> {
    pub const FUNCT7_VALUE: u32 = F7;
    pub const OPCODE_VALUE: u32 = OPCODE;
    pub const fn construct(rd: u8, rs1: u8, rs2: u8, rm: FPRoundingMode) -> u32 {
        let instruction = 0
            | (F7 << 25)
            | (register_value(rs2) << 20)
            | (register_value(rs1) << 15)
            | ((rm as u32) << 12)
            | (register_value(rd) << 7)
            | OPCODE;

        instruction
    }

    pub fn matches(insn: InstructionValue) -> bool {
        OPCODE == insn.opcode() && F7 == insn.field::<25, 7>()
    }

    pub fn rd(insn: InstructionValue) -> u8 {
        insn.field::<7, 5>() as u8
    }

    pub fn rs1(insn: InstructionValue) -> u8 {
        insn.field::<15, 5>() as u8
    }

    pub fn rs2(insn: InstructionValue) -> u8 {
        insn.field::<20, 5>() as u8
    }

    pub fn rm(insn: InstructionValue) -> u8 {
        insn.field::<12, 3>() as u8
    }
}

pub struct RTypeBaseWithAqRl<const OPCODE: u32, const F3: u32, const F7: u32>;

impl<const OPCODE: u32, const F3: u32, const F7: u32> RTypeBaseWithAqRl<OPCODE, F3, F7> {
    pub const FUNCT3_VALUE: u32 = F3;
    pub const FUNCT7_VALUE: u32 = F7;
    pub const OPCODE_VALUE: u32 = OPCODE;
    pub fn construct(rd: u8, rs1: u8, rs2: u8, access: &[MemoryAccess]) -> u32 {
        let mut aqrl = 0;

        for i in 0..access.len() {
            aqrl |= access[i] as u32;
        }

        let instruction = 0
            | ((F7 | aqrl) << 25)
            | (register_value(rs2) << 20)
            | (register_value(rs1) << 15)
            | (F3 << 12)
            | (register_value(rd) << 7)
            | OPCODE;

        instruction
    }

    pub fn matches(insn: InstructionValue) -> bool {
        OPCODE == insn.opcode() && F3 == insn.field::<12, 3>() && F7 == insn.field::<25, 7>()
    }

    pub fn rd(insn: InstructionValue) -> u8 {
        insn.field::<7, 5>() as u8
    }

    pub fn rs1(insn: InstructionValue) -> u8 {
        insn.field::<15, 5>() as u8
    }

    pub fn rs2(insn: InstructionValue) -> u8 {
        insn.field::<20, 5>() as u8
    }

    pub fn aqrl(insn: InstructionValue) -> u8 {
        insn.field::<25, 2>() as u8
    }
}

pub struct R4TypeBaseWithRoundingMode<const OPCODE: u32, const F2: u32>;

impl<const OPCODE: u32, const F2: u32> R4TypeBaseWithRoundingMode<OPCODE, F2> {
    pub const FUNCT2_VALUE: u32 = F2;
    pub const OPCODE_VALUE: u32 = OPCODE;
    pub const fn construct(rd: u8, rs1: u8, rs2: u8, rs3: u8, rm: FPRoundingMode) -> u32 {
        let instruction = 0
            | (register_value(rs3) << 27)
            | (F2 << 25)
            | (register_value(rs2) << 20)
            | (register_value(rs1) << 15)
            | ((rm as u32) << 12)
            | (register_value(rd) << 7)
            | OPCODE;

        instruction
    }

    pub fn matches(insn: InstructionValue) -> bool {
        OPCODE == insn.opcode() && F2 == insn.field::<25, 2>()
    }

    pub fn rd(insn: InstructionValue) -> u8 {
        insn.field::<7, 5>() as u8
    }

    pub fn rs1(insn: InstructionValue) -> u8 {
        insn.field::<15, 5>() as u8
    }

    pub fn rs2(insn: InstructionValue) -> u8 {
        insn.field::<20, 5>() as u8
    }

    pub fn rs3(insn: InstructionValue) -> u8 {
        insn.field::<27, 5>() as u8
    }

    pub fn rm(insn: InstructionValue) -> u8 {
        insn.field::<12, 3>() as u8
    }
}

pub struct ITypeBase<const OPCODE: u32, const F3: u32>;

impl<const OPCODE: u32, const F3: u32> ITypeBase<OPCODE, F3> {
    pub const FUNCT3_VALUE: u32 = F3;
    pub const OPCODE_VALUE: u32 = OPCODE;
    pub const fn construct(rd: u8, rs1: u8, imm: IImmediate) -> u32 {
        let instruction = 0
            | (imm.field::<0, 12>() << 20)
            | (register_value(rs1) << 15)
            | (F3 << 12)
            | (register_value(rd) << 7)
            | OPCODE;

        instruction
    }

    pub fn matches(insn: InstructionValue) -> bool {
        OPCODE == insn.opcode() && F3 == insn.field::<12, 3>()
    }

    pub fn rd(insn: InstructionValue) -> u8 {
        insn.field::<7, 5>() as u8
    }

    pub fn rs1(insn: InstructionValue) -> u8 {
        insn.field::<15, 5>() as u8
    }
}

pub struct STypeBase<const OPCODE: u32, const F3: u32>;

impl<const OPCODE: u32, const F3: u32> STypeBase<OPCODE, F3> {
    pub const FUNCT3_VALUE: u32 = F3;
    pub const OPCODE_VALUE: u32 = OPCODE;
    pub const fn construct(rs1: u8, rs2: u8, imm: SImmediate) -> u32 {
        let instruction = 0
            | (imm.field::<5, 7>() << 25)
            | (register_value(rs2) << 20)
            | (register_value(rs1) << 15)
            | (F3 << 12)
            | (imm.field::<0, 5>() << 7)
            | OPCODE;

        instruction
    }

    pub const fn matches(insn: InstructionValue) -> bool {
        OPCODE == insn.opcode() && F3 == insn.field::<12, 3>()
    }

    pub const fn rs1(insn: InstructionValue) -> u8 {
        insn.field::<15, 5>() as u8
    }

    pub const fn rs2(insn: InstructionValue) -> u8 {
        insn.field::<20, 5>() as u8
    }
}

pub struct BTypeBase<const OPCODE: u32, const F3: u32>;

impl<const OPCODE: u32, const F3: u32> BTypeBase<OPCODE, F3> {
    pub const FUNCT3_VALUE: u32 = F3;
    pub const OPCODE_VALUE: u32 = OPCODE;
    pub const fn construct(rs1: u8, rs2: u8, imm: BImmediate) -> u32 {
        let instruction = 0
            | (imm.field::<12, 1>() << 31)
            | (imm.field::<5, 6>() << 25)
            | (register_value(rs2) << 20)
            | (register_value(rs1) << 15)
            | (F3 << 12)
            | (imm.field::<1, 4>() << 8)
            | (imm.field::<11, 1>() << 7)
            | OPCODE;
        instruction
    }

    pub const fn matches(insn: InstructionValue) -> bool {
        OPCODE == insn.opcode() && F3 == insn.field::<12, 3>()
    }

    pub const fn rs1(insn: InstructionValue) -> u8 {
        insn.field::<15, 5>() as u8
    }

    pub const fn rs2(insn: InstructionValue) -> u8 {
        insn.field::<20, 5>() as u8
    }
}

pub struct UTypeBase<const OPCODE: u32>;

impl<const OPCODE: u32> UTypeBase<OPCODE> {
    pub const OPCODE_VALUE: u32 = OPCODE;
    pub const fn construct(rd: u8, imm: UImmediate) -> u32 {
        let instruction = 0 | imm.value | (register_value(rd) << 7) | OPCODE;

        instruction
    }

    pub const fn matches(insn: InstructionValue) -> bool {
        OPCODE == insn.opcode()
    }

    pub const fn rd(insn: InstructionValue) -> u8 {
        insn.field::<7, 5>() as u8
    }
}

pub struct JTypeBase<const OPCODE: u32>;

impl<const OPCODE: u32> JTypeBase<OPCODE> {
    pub const OPCODE_VALUE: u32 = OPCODE;
    pub const fn construct(rd: u8, imm: JImmediate) -> u32 {
        let instruction = 0
            | (imm.field::<20, 1>() << 31)
            | (imm.field::<1, 10>() << 21)
            | (imm.field::<11, 1>() << 20)
            | (imm.field::<12, 8>() << 12)
            | (register_value(rd) << 7)
            | OPCODE;

        instruction
    }

    pub const fn matches(insn: InstructionValue) -> bool {
        OPCODE == insn.opcode()
    }

    pub const fn rd(insn: InstructionValue) -> u8 {
        insn.field::<7, 5>() as u8
    }
}

// The following instruction definitions utilize the base instruction structs, in most cases specifying everything
// necessary in the template parameters of the base instruction struct they are inheriting from. For each instruction
// there's also a pretty-print name constant included in the definition, for use by the disassembler.

// RV32I Base Instruction Set

macro_rules! riscv_insns {
    ($($name: ident : $base: ty, $pretty: literal)*) => {
        $(
            pub type $name = $base;

            paste::paste! {
                pub const [<PRETTY_ $name>]: &str = $pretty;
            }

        )*
    };
}

riscv_insns! {
    LUI: UTypeBase<{LUI}>, "lui"
    AUIPC: UTypeBase<{AUIPC}>, "auipc"
    JAL: JTypeBase<{JAL}>, "jal"
    JALR: ITypeBase<{JALR}, 0b000>, "jalr"
    BEQ: BTypeBase<{BRANCH}, 0b000>, "beq"
    BNE: BTypeBase<{BRANCH}, 0b001>, "bne"
    BLT: BTypeBase<{BRANCH}, 0b100>, "blt"
    BGE: BTypeBase<{BRANCH}, 0b101>, "bge"
    BLTU: BTypeBase<{BRANCH}, 0b110>, "bltu"
    BGEU: BTypeBase<{BRANCH}, 0b111>, "bgeu"
    LB: ITypeBase<{LOAD}, 0b000>, "lb"
    LH: ITypeBase<{LOAD}, 0b001>, "lh"
    LW: ITypeBase<{LOAD}, 0b010>, "lw"
    LBU: ITypeBase<{LOAD}, 0b100>, "lbu"
    LHU: ITypeBase<{LOAD}, 0b101>, "lhu"
    SB: STypeBase<{STORE}, 0b000>, "sb"
    SH: STypeBase<{STORE}, 0b001>, "sh"
    SW: STypeBase<{STORE}, 0b010>, "sw"
    ADDI: ITypeBase<{OP_IMM}, 0b000>, "addi"
    SLTI: ITypeBase<{OP_IMM}, 0b010>, "slti"
    SLTIU: ITypeBase<{OP_IMM}, 0b011>, "sltiu"
    XORI: ITypeBase<{OP_IMM}, 0b100>, "xori"
    ORI: ITypeBase<{OP_IMM}, 0b110>, "ori"
    ANDI: ITypeBase<{OP_IMM}, 0b111>, "andi"

}

pub struct SLLI;

pub const PRETTY_SLLI: &str = "slli";

impl SLLI {
    pub const OPCODE_VALUE: u32 = OP_IMM;

    pub fn construct(rd: u8, rs1: u8, shift_amount: u32) -> u32 {
        ITypeBase::<{ OP_IMM }, 0b001>::construct(
            rd,
            rs1,
            IImmediate::v32((0b000000 << 6) | shift_amount as i32),
        )
    }

    pub fn matches(insn: InstructionValue) -> bool {
        ITypeBase::<{ OP_IMM }, 0b001>::matches(insn)
    }

    pub fn rd(insn: InstructionValue) -> u8 {
        ITypeBase::<{ OP_IMM }, 0b001>::rd(insn)
    }

    pub fn rs1(insn: InstructionValue) -> u8 {
        ITypeBase::<{ OP_IMM }, 0b001>::rs1(insn)
    }
}

pub struct SRLI;

pub const PRETTY_SRLI: &str = "srli";

impl SRLI {
    pub fn construct(rd: u8, rs1: u8, shift_amount: u32) -> u32 {
        ITypeBase::<{ OP_IMM }, 0b101>::construct(
            rd,
            rs1,
            IImmediate::v32((0b000000 << 6) | shift_amount as i32),
        )
    }

    pub fn matches(insn: InstructionValue) -> bool {
        ITypeBase::<{ OP_IMM }, 0b101>::matches(insn)
    }

    pub fn rd(insn: InstructionValue) -> u8 {
        ITypeBase::<{ OP_IMM }, 0b101>::rd(insn)
    }

    pub fn rs1(insn: InstructionValue) -> u8 {
        ITypeBase::<{ OP_IMM }, 0b101>::rs1(insn)
    }
}

pub struct SRAI;

pub const PRETTY_SRAI: &str = "srai";

impl SRAI {
    pub fn construct(rd: u8, rs1: u8, shift_amount: u32) -> u32 {
        ITypeBase::<{ OP_IMM }, 0b101>::construct(
            rd,
            rs1,
            IImmediate::v32((0b010000 << 6) | shift_amount as i32),
        )
    }

    pub fn matches(insn: InstructionValue) -> bool {
        ITypeBase::<{ OP_IMM }, 0b101>::matches(insn)
    }

    pub fn rd(insn: InstructionValue) -> u8 {
        ITypeBase::<{ OP_IMM }, 0b101>::rd(insn)
    }

    pub fn rs1(insn: InstructionValue) -> u8 {
        ITypeBase::<{ OP_IMM }, 0b101>::rs1(insn)
    }
}

riscv_insns! {
    ADD: RTypeBase<{OP}, 0b000, 0b0000000>, "add"
    SUB: RTypeBase<{OP}, 0b000, 0b0100000>, "sub"
    SLL: RTypeBase<{OP}, 0b001, 0b0000000>, "sll"
    SLT: RTypeBase<{OP}, 0b010, 0b0000000>, "slt"
    SLTU: RTypeBase<{OP}, 0b011, 0b0000000>, "sltu"
    XOR: RTypeBase<{OP}, 0b100, 0b0000000>, "xor"
    SRL: RTypeBase<{OP}, 0b101, 0b0000000>, "srl"
    SRA: RTypeBase<{OP}, 0b101, 0b0100000>, "sra"
    OR: RTypeBase<{OP}, 0b110, 0b0000000>, "or"
    AND: RTypeBase<{OP}, 0b111, 0b0000000>, "and"
    FENCE: ITypeBase<{MISC_MEM}, 0b000>, "fence"
    ECALL: ITypeBase<{SYSTEM}, 0b000>, "ecall"
    EBREAK: ITypeBase<{SYSTEM}, 0b001>, "ebreak"

    // RV64I Base Instruction Set (in addition to RV32I)
    LWU: ITypeBase<{LOAD}, 0b110>, "lwu"
    LD: ITypeBase<{LOAD}, 0b011>, "ld"
    SD: STypeBase<{STORE}, 0b011>, "sd"
    ADDIW: ITypeBase<{OP_IMM_32}, 0b000>, "addiw"

}

pub struct SLLIW;

pub const PRETTY_SLLIW: &str = "slliw";

impl SLLIW {
    pub fn construct(rd: u8, rs1: u8, shift_amount: u32) -> u32 {
        ITypeBase::<{ OP_IMM_32 }, 0b001>::construct(
            rd,
            rs1,
            IImmediate::v32((0b000000 << 5) | shift_amount as i32),
        )
    }

    pub fn matches(insn: InstructionValue) -> bool {
        ITypeBase::<{ OP_IMM_32 }, 0b001>::matches(insn)
    }

    pub fn rd(insn: InstructionValue) -> u8 {
        ITypeBase::<{ OP_IMM_32 }, 0b001>::rd(insn)
    }

    pub fn rs1(insn: InstructionValue) -> u8 {
        ITypeBase::<{ OP_IMM_32 }, 0b001>::rs1(insn)
    }
}

pub struct SRLIW;

pub const PRETTY_SRLIW: &str = "srliw";

impl SRLIW {
    pub fn construct(rd: u8, rs1: u8, shift_amount: u32) -> u32 {
        ITypeBase::<{ OP_IMM_32 }, 0b101>::construct(
            rd,
            rs1,
            IImmediate::v32((0b000000 << 5) | shift_amount as i32),
        )
    }

    pub fn matches(insn: InstructionValue) -> bool {
        ITypeBase::<{ OP_IMM_32 }, 0b101>::matches(insn)
    }

    pub fn rd(insn: InstructionValue) -> u8 {
        ITypeBase::<{ OP_IMM_32 }, 0b101>::rd(insn)
    }

    pub fn rs1(insn: InstructionValue) -> u8 {
        ITypeBase::<{ OP_IMM_32 }, 0b101>::rs1(insn)
    }
}

pub struct SRAIW;

pub const PRETTY_SRAIW: &str = "sraiw";

impl SRAIW {
    pub fn construct(rd: u8, rs1: u8, shift_amount: u32) -> u32 {
        ITypeBase::<{ OP_IMM_32 }, 0b101>::construct(
            rd,
            rs1,
            IImmediate::v32((0b010000 << 5) | shift_amount as i32),
        )
    }

    pub fn matches(insn: InstructionValue) -> bool {
        ITypeBase::<{ OP_IMM_32 }, 0b101>::matches(insn)
    }

    pub fn rd(insn: InstructionValue) -> u8 {
        ITypeBase::<{ OP_IMM_32 }, 0b101>::rd(insn)
    }

    pub fn rs1(insn: InstructionValue) -> u8 {
        ITypeBase::<{ OP_IMM_32 }, 0b101>::rs1(insn)
    }
}

riscv_insns! {
    ADDW: RTypeBase<{OP_32}, 0b000, 0b0000000>, "addw"
    SUBW: RTypeBase<{OP_32}, 0b000, 0b0100000>, "subw"
    SLLW: RTypeBase<{OP_32}, 0b001, 0b0000000>, "sllw"
    SRLW: RTypeBase<{OP_32}, 0b101, 0b0000000>, "srlw"
    SRAW: RTypeBase<{OP_32}, 0b101, 0b0100000>, "sraw"
    // RV32/RV64 Zifencei Standard Extension
    FENCE_I: ITypeBase<{MISC_MEM}, 0b001>, "fence.i"
    // RV32M Standard Extension
    MUL: RTypeBase<{OP}, 0b000, 0b0000001>, "mul"
    MULH: RTypeBase<{OP}, 0b001, 0b0000001>, "mulh"
    MULHSU: RTypeBase<{OP}, 0b010, 0b0000001>, "mulhsu"
    MULHU: RTypeBase<{OP}, 0b011, 0b0000001>, "mulhu"
    DIV: RTypeBase<{OP}, 0b100, 0b0000001>, "div"
    DIVU: RTypeBase<{OP}, 0b101, 0b0000001>, "divu"
    REM: RTypeBase<{OP}, 0b110, 0b0000001>, "rem"
    REMU: RTypeBase<{OP}, 0b111, 0b0000001>, "remu"
    // RV64M Standard Extension (in addition to RV32M)
    MULW: RTypeBase<{OP_32}, 0b000, 0b0000001>, "mulw"
    DIVW: RTypeBase<{OP_32}, 0b100, 0b0000001>, "divw"
    DIVUW: RTypeBase<{OP_32}, 0b101, 0b0000001>, "divuw"
    REMW: RTypeBase<{OP_32}, 0b110, 0b0000001>, "remw"
    REMUW: RTypeBase<{OP_32}, 0b111, 0b0000001>, "remuw"
    // RV32A Standard Extension
    LR_W: RTypeBaseWithAqRl<{AMO}, 0b010, 0b0001000>, "lr.w"
    SC_W: RTypeBaseWithAqRl<{AMO}, 0b010, 0b0001100>, "sc.w"
    AMOSWAP_W: RTypeBaseWithAqRl<{AMO}, 0b010, 0b0000100>, "amoswap.w"
    AMOADD_W: RTypeBaseWithAqRl<{AMO}, 0b010, 0b0000000>, "amoadd.w"
    AMOXOR_W: RTypeBaseWithAqRl<{AMO}, 0b010, 0b0010000>, "amoxor.w"
    AMOAND_W: RTypeBaseWithAqRl<{AMO}, 0b010, 0b0110000>, "amoand.w"
    AMOOR_W: RTypeBaseWithAqRl<{AMO}, 0b010, 0b0100000>, "amoor.w"
    AMOMIN_W: RTypeBaseWithAqRl<{AMO}, 0b010, 0b1000000>, "amomin.w"
    AMOMAX_W: RTypeBaseWithAqRl<{AMO}, 0b010, 0b1010000>, "amomax.w"
    AMOMINU_W: RTypeBaseWithAqRl<{AMO}, 0b010, 0b1100000>, "amominu.w"
    AMOMAXU_W: RTypeBaseWithAqRl<{AMO}, 0b010, 0b1110000>, "amomaxu.w"

    // RV64A Standard Extension (in addition to RV32A)
    LR_D: RTypeBaseWithAqRl<{AMO}, 0b011, 0b0001000>, "lr.d"
    SC_D: RTypeBaseWithAqRl<{AMO}, 0b011, 0b0001100>, "sc.d"
    AMOSWAP_D: RTypeBaseWithAqRl<{AMO}, 0b011, 0b0000100>, "amoswap.d"
    AMOADD_D: RTypeBaseWithAqRl<{AMO}, 0b011, 0b0000000>, "amoadd.d"
    AMOXOR_D: RTypeBaseWithAqRl<{AMO}, 0b011, 0b0010000>, "amoxor.d"
    AMOAND_D: RTypeBaseWithAqRl<{AMO}, 0b011, 0b0110000>, "amoand.d"
    AMOOR_D: RTypeBaseWithAqRl<{AMO}, 0b011, 0b0100000>, "amoor.d"
    AMOMIN_D: RTypeBaseWithAqRl<{AMO}, 0b011, 0b1000000>, "amomin.d"
    AMOMAX_D: RTypeBaseWithAqRl<{AMO}, 0b011, 0b1010000>, "amomax.d"
    AMOMINU_D: RTypeBaseWithAqRl<{AMO}, 0b011, 0b1100000>, "amominu.d"
    AMOMAXU_D: RTypeBaseWithAqRl<{AMO}, 0b011, 0b1110000>, "amomaxu.d"
}

pub struct FCVTImpl<const RS2: u8, const F7: u32>;

impl<const RS2: u8, const F7: u32> FCVTImpl<RS2, F7> {
    pub const fn construct(rd: u8, rs1: u8, rm: FPRoundingMode) -> u32 {
        RTypeBaseWithRoundingMode::<{ OP_FP }, F7>::construct(rd, rs1, RS2, rm)
    }

    pub fn matches(inst: InstructionValue) -> bool {
        RTypeBaseWithRoundingMode::<{ OP_FP }, F7>::matches(inst)
    }

    pub fn rd(inst: InstructionValue) -> u8 {
        RTypeBaseWithRoundingMode::<{ OP_FP }, F7>::rd(inst)
    }

    pub fn rs1(inst: InstructionValue) -> u8 {
        RTypeBaseWithRoundingMode::<{ OP_FP }, F7>::rs1(inst)
    }

    pub fn rs2(inst: InstructionValue) -> u8 {
        RTypeBaseWithRoundingMode::<{ OP_FP }, F7>::rs2(inst)
    }

    pub fn rm(inst: InstructionValue) -> u8 {
        RTypeBaseWithRoundingMode::<{ OP_FP }, F7>::rm(inst)
    }
}

pub struct FMVImpl<const F7: u32>;

impl<const F7: u32> FMVImpl<F7> {
    pub fn matches(inst: InstructionValue) -> bool {
        RTypeBase::<{ OP_FP }, 0b000, F7>::matches(inst)
    }

    pub const fn construct(rd: u8, rs1: u8) -> u32 {
        RTypeBase::<{ OP_FP }, 0b000, F7>::construct(rd, rs1, 0)
    }

    pub fn rd(inst: InstructionValue) -> u8 {
        RTypeBase::<{ OP_FP }, 0b000, F7>::rd(inst)
    }

    pub fn rs1(inst: InstructionValue) -> u8 {
        RTypeBase::<{ OP_FP }, 0b000, F7>::rs1(inst)
    }
}
// RV32F Standard Extension
riscv_insns! {
    FLW: ITypeBase<{LOAD_FP}, 0b010>, "flw"
    FSW: STypeBase<{STORE_FP}, 0b010>, "fsw"
    FMADD_S: R4TypeBaseWithRoundingMode<{MADD}, 0b00>, "fmadd.s"
    FMSUB_S: R4TypeBaseWithRoundingMode<{MSUB}, 0b00>, "fmsub.s"
    FNMSUB_S: R4TypeBaseWithRoundingMode<{NMSUB}, 0b00>, "fnmsub.s"
    FNMADD_S: R4TypeBaseWithRoundingMode<{NMADD}, 0b00>, "fnmadd.s"
    FADD_S: RTypeBaseWithRoundingMode<{OP_FP}, 0b0000000>, "fadd.s"
    FSUB_S: RTypeBaseWithRoundingMode<{OP_FP}, 0b0000100>, "fsub.s"
    FMUL_S: RTypeBaseWithRoundingMode<{OP_FP}, 0b0001000>, "fmul.s"
    FDIV_S: RTypeBaseWithRoundingMode<{OP_FP}, 0b0001100>, "fdiv.s"
    FSQRT_S: RTypeBaseWithRoundingMode<{OP_FP}, 0b0101100>, "fsqrt.s"
    FSGNJ_S: RTypeBase<{OP_FP}, 0b000, 0b0010000>, "fsgnj.s"
    FSGNJN_S: RTypeBase<{OP_FP}, 0b001, 0b0010000>, "fsgnjn.s"
    FSGNJX_S: RTypeBase<{OP_FP}, 0b010, 0b0010000>, "fsgnjx.s"
    FMIN_S: RTypeBase<{OP_FP}, 0b000, 0b0010100>, "fmin.s"
    FMAX_S: RTypeBase<{OP_FP}, 0b001, 0b0010100>, "fmax.s"
    FCVT_W_S: FCVTImpl<0b00000, 0b1100000>, "fcvt.w.s"
    FCVT_WU_S: FCVTImpl<0b00000, 0b1100001>, "fcvt.wu.s"
    FMV_X_W: FMVImpl<0b1110000>, "fmv.x.w"

    FEQ_S: RTypeBase<{OP_FP}, 0b010, 0b1010000>, "feq.s"
    FLT_S: RTypeBase<{OP_FP}, 0b001, 0b1010000>, "flt.s"
    FLE_S: RTypeBase<{OP_FP}, 0b000, 0b1010000>, "fle.s"

    FCLASS_S: RTypeBase<{OP_FP}, 0b001, 0b1110000>, "fclass.s"
    FCVT_S_W: FCVTImpl<0b00000, 0b1101000>, "fcvt.s.w"
    FCVT_S_WU: FCVTImpl<0b00001, 0b1101000>, "fcvt.s.wu"
    FMV_W_X: FMVImpl<0b1111000>, "fmv.w.x"
    // RV64F Standard Extension (in addition to RV32F)
    FCVT_L_S: FCVTImpl<0b00010, 0b1100000>, "fcvt.l.s"
    FCVT_LU_S: FCVTImpl<0b00011, 0b1100000>, "fcvt.lu.s"
    FCVT_S_L: FCVTImpl<0b00010, 0b1101000>, "fcvt.s.l"
    FCVT_S_LU: FCVTImpl<0b00011, 0b1101000>, "fcvt.s.lu"
    // RV32D Standard Extension
    FLD: ITypeBase<{LOAD_FP}, 0b011>, "fld"
    FSD: STypeBase<{STORE_FP}, 0b011>, "fsd"
    FMADD_D: R4TypeBaseWithRoundingMode<{MADD}, 0b01>, "fmadd.d"
    FMSUB_D: R4TypeBaseWithRoundingMode<{MSUB}, 0b01>, "fmsub.d"
    FNMSUB_D: R4TypeBaseWithRoundingMode<{NMSUB}, 0b01>, "fnmsub.d"
    FNMADD_D: R4TypeBaseWithRoundingMode<{NMADD}, 0b01>, "fnmadd.d"
    FADD_D: RTypeBaseWithRoundingMode<{OP_FP}, 0b0000001>, "fadd.d"
    FSUB_D: RTypeBaseWithRoundingMode<{OP_FP}, 0b0000101>, "fsub.d"
    FMUL_D: RTypeBaseWithRoundingMode<{OP_FP}, 0b0001001>, "fmul.d"
    FDIV_D: RTypeBaseWithRoundingMode<{OP_FP}, 0b0001101>, "fdiv.d"
    FSQRT_D: RTypeBaseWithRoundingMode<{OP_FP}, 0b0101101>, "fsqrt.d"
    FSGNJ_D: RTypeBase<{OP_FP}, 0b000,  0b0010001>, "fsgnj.d"
    FSGNJN_D: RTypeBase<{OP_FP}, 0b001,  0b0010011>, "fsgnjn.d"
    FSGNJX_D: RTypeBase<{OP_FP}, 0b010,  0b0010010>, "fsgnjx.d"
    FMIN_D: RTypeBase<{OP_FP}, 0b000,  0b0010101>, "fmin.d"
    FMAX_D: RTypeBase<{OP_FP}, 0b001,  0b0010101>, "fmax.d"
    FCVT_S_D: FCVTImpl<0b00001, 0b0100000>, "fcvt.s.d"
    FCVT_D_S: FCVTImpl<0b00000, 0b0100001>, "fcvt.d.s"
    FEQ_D: RTypeBase<{OP_FP}, 0b010, 0b1010001>, "feq.d"
    FLT_D: RTypeBase<{OP_FP}, 0b001, 0b1010001>, "flt.d"
    FLE_D: RTypeBase<{OP_FP}, 0b000, 0b1010001>, "fle.d"
    FCLASS_D: RTypeBase<{OP_FP}, 0b001, 0b1110001>, "fclass.d"
    FCVT_W_D: FCVTImpl<0b00000, 0b1100001>, "fcvt.w.d"
    FCVT_WU_D: FCVTImpl<0b00001, 0b1100001>, "fcvt.wu.d"
    FCVT_D_W: FCVTImpl<0b00000, 0b1101001>, "fcvt.d.w"
    FCVT_D_WU: FCVTImpl<0b00001, 0b1101001>, "fcvt.d.wu"

    // RV64D Standard Extension (in addition to RV32D)
    FCVT_L_D: FCVTImpl<0b00010, 0b1100001>, "fcvt.l.d"
    FCVT_LU_D: FCVTImpl<0b00011, 0b1100001>, "fcvt.lu.d"
    FCVT_D_L: FCVTImpl<0b00010, 0b1101001>, "fcvt.d.l"
    FCVT_D_LU: FCVTImpl<0b00011, 0b1101001>, "fcvt.d.lu"
    FMV_X_D: FMVImpl<0b1110001>, "fmv.x.d"
    FMV_D_X: FMVImpl<0b1111001>, "fmv.d.x"
}

impl RISCV64Assembler {
    pub const fn first_register() -> u8 {
        x0
    }

    pub const fn last_register() -> u8 {
        x31
    }

    pub const fn first_sp_register() -> u8 {
        pc
    }

    pub const fn last_sp_register() -> u8 {
        pc
    }

    pub const fn first_fp_register() -> u8 {
        f0
    }

    pub const fn last_fp_register() -> u8 {
        f31
    }

    pub const fn gpr_name(reg: u8) -> &'static str {
        match reg {
            x0 => "x0",
            x1 => "x1",
            x2 => "x2",
            x3 => "x3",
            x4 => "x4",
            x5 => "x5",
            x6 => "x6",
            x7 => "x7",
            x8 => "x8",
            x9 => "x9",
            x10 => "x10",
            x11 => "x11",
            x12 => "x12",
            x13 => "x13",
            x14 => "x14",
            x15 => "x15",
            x16 => "x16",
            x17 => "x17",
            x18 => "x18",
            x19 => "x19",
            x20 => "x20",
            x21 => "x21",
            x22 => "x22",
            x23 => "x23",
            x24 => "x24",
            x25 => "x25",
            x26 => "x26",
            x27 => "x27",
            x28 => "x28",
            x29 => "x29",
            x30 => "x30",
            x31 => "x31",
            _ => "unknown",
        }
    }

    pub fn fpr_name(reg: u8) -> &'static str {
        match reg {
            f0 => "f0",
            f1 => "f1",
            f2 => "f2",
            f3 => "f3",
            f4 => "f4",
            f5 => "f5",
            f6 => "f6",
            f7 => "f7",
            f8 => "f8",
            f9 => "f9",
            f10 => "f10",
            f11 => "f11",
            f12 => "f12",
            f13 => "f13",
            f14 => "f14",
            f15 => "f15",
            f16 => "f16",
            f17 => "f17",
            f18 => "f18",
            f19 => "f19",
            f20 => "f20",
            f21 => "f21",
            f22 => "f22",
            f23 => "f23",
            f24 => "f24",
            f25 => "f25",
            f26 => "f26",
            f27 => "f27",
            f28 => "f28",
            f29 => "f29",
            f30 => "f30",
            f31 => "f31",
            _ => "unknown",
        }
    }

    pub const fn number_of_registers() -> usize {
        Self::last_register() as usize - Self::first_register() as usize + 1
    }

    pub const fn number_of_fp_registers() -> usize {
        Self::last_fp_register() as usize - Self::first_fp_register() as usize + 1
    }

    pub unsafe fn get_relocate_address(code: *mut u8, label: AssemblerLabel) -> *mut u8 {
        code.offset(label.offset() as i32 as isize)
    }

    pub fn get_difference_between_labels(from: AssemblerLabel, to: AssemblerLabel) -> i32 {
        to.offset() as i32 - from.offset() as i32
    }

    pub fn get_call_return_offset(call: AssemblerLabel) -> usize {
        call.offset() as _
    }

    pub fn label_ignoring_watchpoints(&mut self) -> AssemblerLabel {
        self.buffer.label()
    }

    pub fn label_for_watchpoint(&mut self) -> AssemblerLabel {
        let mut label = self.buffer.label();

        if label.offset() as i32 != self.index_of_last_watchpoint {
            label = self.label();
        }

        self.index_of_last_watchpoint = label.offset() as _;
        self.index_of_tail_of_last_watchpoint =
            label.offset() as i32 + Self::max_jump_replacement_size() as i32;
        label
    }

    pub fn label(&mut self) -> AssemblerLabel {
        let mut result = self.buffer.label();

        while (result.offset() as i32) < self.index_of_tail_of_last_watchpoint {
            self.nop();
            result = self.buffer.label();
        }

        result
    }

    pub unsafe fn link_jump_(code: *mut u8, from: AssemblerLabel, to: *mut u8) {
        if !from.is_set() {
            return;
        }

        let location = code.offset(from.offset() as i32 as isize).cast::<u32>();

        if location.read() == LinkJumpImpl::placeholder_insn() {
            LinkJumpImpl::apply(location, to);
            return;
        }

        if location.read() == LinkBranchImpl::placeholder_insn() {
            LinkBranchImpl::apply(location, to);
            return;
        }
    }

    pub unsafe fn link_call(code: *mut u8, from: AssemblerLabel, to: *mut u8) {
        let location = code.offset(from.offset() as i32 as isize).cast::<u32>();
        assert_eq!(location.read(), LinkCallImpl::placeholder_insn());
        LinkCallImpl::apply(location, to);
    }

    pub unsafe fn link_pointer(code: *mut u8, where_: AssemblerLabel, value_ptr: *mut u8) {
        let location = code.offset(where_.offset() as i32 as isize).cast::<u32>();
        PatchPointerImpl::apply(location, value_ptr);
    }

    pub fn link_jump(&mut self, from: AssemblerLabel, to: AssemblerLabel) {
        if !from.is_set() || !to.is_set() {
            return;
        }

        unsafe {
            let location = self
                .buffer
                .data_mut()
                .as_mut_ptr()
                .offset(to.offset() as i32 as isize) as *mut u32;
            Self::link_jump_(self.buffer.data_mut().as_mut_ptr(), from, location.cast());
        }
    }

    pub const fn patchable_jump_size() -> usize {
        size_of::<u32>() * 8
    }

    pub const fn max_jump_replacement_size() -> usize {
        size_of::<u32>() * 8
    }

    pub unsafe fn repatch_int32(_: *mut u8, _: i32) {
        todo!()
    }

    pub unsafe fn repatch_pointer(where_: *mut u8, value_ptr: *mut u8) {
        let location = where_.cast::<u32>();
        PatchPointerImpl::apply(location, value_ptr);
    }

    pub unsafe fn relink_jump(where_: *mut u8, to: *mut u8) {
        let location = where_.cast::<u32>();
        LinkJumpImpl::apply(location, to);
    }

    pub unsafe fn relink_call(where_: *mut u8, to: *mut u8) {
        let location = where_.cast::<u32>();
        LinkCallImpl::apply(location, to);
    }

    pub unsafe fn relink_tail_call(where_: *mut u8, to: *mut u8) {
        Self::relink_jump(where_, to)
    }

    pub unsafe fn replace_with_vm_halt(where_: *mut u8) {
        let location = where_.cast::<u32>();
        location.write(SD::construct(zero, zero, SImmediate::v32(0)))
    }

    pub unsafe fn replace_with_jump(from: *mut u8, to: *mut u8) {
        let location = from.cast::<u32>();
        let offset = to as i64 - from as i64;

        if JImmediate::is_valid(offset) {
            location.write(JAL::construct(zero, JImmediate::v32(offset as i32)));
            return;
        }

        let immediate = ImmediateDecomposition::new64(offset as i64);

        location.write(AUIPC::construct(x30, immediate.upper));
        location
            .add(1)
            .write(JALR::construct(zero, x30, immediate.lower));
    }

    pub unsafe fn revert_jump_replacement_to_patch(from: *mut u8, value_ptr: *mut u8) {
        let location = from.cast::<u32>();
        PatchPointerImpl::apply_with_dest(location, x30, value_ptr);
    }

    pub unsafe fn read_call_target(from: *mut u8) -> *mut u8 {
        let location = from.cast::<u32>();
        PatchPointerImpl::read(location)
    }

    pub unsafe fn read_pointer(where_: *mut u8) -> *mut u8 {
        let location = where_.cast::<u32>();
        PatchPointerImpl::read(location)
    }

    pub unsafe fn replace_with_load(_: *mut u8) {
        todo!()
    }

    pub unsafe fn replace_with_address_computation(_: *mut u8) {
        todo!()
    }

    pub unsafe fn fill_nops(base: *mut u8, size: usize) {
        let ptr = base.cast::<u32>();

        let nop = ADDI::construct(x0, x0, IImmediate::v32(0));

        let mut i = 0;
        let n = size / size_of::<u32>();
        while i < n {
            ptr.add(i).write(nop);
            i += 1;
        }
    }

    pub fn lui(&mut self, rd: u8, imm: i32) {
        self.insn(LUI::construct(rd, UImmediate::v32(imm)));
    }

    pub fn auipc(&mut self, rd: u8, imm: i32) {
        self.insn(AUIPC::construct(rd, UImmediate::v32(imm)));
    }

    pub fn jal(&mut self, rd: u8, imm: i32) {
        self.insn(JAL::construct(rd, JImmediate::v32(imm)));
    }

    pub fn jalr(&mut self, rd: u8, rs1: u8, imm: i32) {
        self.insn(JALR::construct(rd, rs1, IImmediate::v32(imm)));
    }

    pub fn beq(&mut self, rs1: u8, rs2: u8, imm: i32) {
        self.insn(BEQ::construct(rs1, rs2, BImmediate::v32(imm)));
    }

    pub fn bne(&mut self, rs1: u8, rs2: u8, imm: i32) {
        self.insn(BNE::construct(rs1, rs2, BImmediate::v32(imm)));
    }

    pub fn blt(&mut self, rs1: u8, rs2: u8, imm: i32) {
        self.insn(BLT::construct(rs1, rs2, BImmediate::v32(imm)));
    }

    pub fn bge(&mut self, rs1: u8, rs2: u8, imm: i32) {
        self.insn(BGE::construct(rs1, rs2, BImmediate::v32(imm)));
    }

    pub fn bltu(&mut self, rs1: u8, rs2: u8, imm: i32) {
        self.insn(BLTU::construct(rs1, rs2, BImmediate::v32(imm)));
    }

    pub fn bgeu(&mut self, rs1: u8, rs2: u8, imm: i32) {
        self.insn(BGEU::construct(rs1, rs2, BImmediate::v32(imm)));
    }

    pub fn lb(&mut self, rd: u8, rs1: u8, imm: i32) {
        self.insn(LB::construct(rd, rs1, IImmediate::v32(imm)));
    }

    pub fn lh(&mut self, rd: u8, rs1: u8, imm: i32) {
        self.insn(LH::construct(rd, rs1, IImmediate::v32(imm)));
    }

    pub fn lw(&mut self, rd: u8, rs1: u8, imm: i32) {
        self.insn(LW::construct(rd, rs1, IImmediate::v32(imm)));
    }

    pub fn ld(&mut self, rd: u8, rs1: u8, imm: i32) {
        self.insn(LD::construct(rd, rs1, IImmediate::v32(imm)));
    }

    pub fn lbu(&mut self, rd: u8, rs1: u8, imm: i32) {
        self.insn(LBU::construct(rd, rs1, IImmediate::v32(imm)));
    }

    pub fn lhu(&mut self, rd: u8, rs1: u8, imm: i32) {
        self.insn(LHU::construct(rd, rs1, IImmediate::v32(imm)));
    }

    pub fn lwu(&mut self, rd: u8, rs1: u8, imm: i32) {
        self.insn(LWU::construct(rd, rs1, IImmediate::v32(imm)));
    }

    pub fn sb(&mut self, rs1: u8, rs2: u8, imm: i32) {
        self.insn(SB::construct(rs1, rs2, SImmediate::v32(imm)));
    }

    pub fn sh(&mut self, rs1: u8, rs2: u8, imm: i32) {
        self.insn(SH::construct(rs1, rs2, SImmediate::v32(imm)));
    }

    pub fn sw(&mut self, rs1: u8, rs2: u8, imm: i32) {
        self.insn(SW::construct(rs1, rs2, SImmediate::v32(imm)));
    }

    pub fn sd(&mut self, rs1: u8, rs2: u8, imm: i32) {
        self.insn(SD::construct(rs1, rs2, SImmediate::v32(imm)));
    }

    pub fn addi(&mut self, rd: u8, rs1: u8, imm: i32) {
        self.insn(ADDI::construct(rd, rs1, IImmediate::v32(imm)));
    }

    pub fn slti(&mut self, rd: u8, rs1: u8, imm: i32) {
        self.insn(SLTI::construct(rd, rs1, IImmediate::v32(imm)));
    }

    pub fn sltiu(&mut self, rd: u8, rs1: u8, imm: i32) {
        self.insn(SLTIU::construct(rd, rs1, IImmediate::v32(imm)));
    }

    pub fn xori(&mut self, rd: u8, rs1: u8, imm: i32) {
        self.insn(XORI::construct(rd, rs1, IImmediate::v32(imm)));
    }

    pub fn ori(&mut self, rd: u8, rs1: u8, imm: i32) {
        self.insn(ORI::construct(rd, rs1, IImmediate::v32(imm)));
    }

    pub fn andi(&mut self, rd: u8, rs1: u8, imm: i32) {
        self.insn(ANDI::construct(rd, rs1, IImmediate::v32(imm)));
    }

    pub fn slli(&mut self, rd: u8, rs1: u8, imm: u32) {
        self.insn(SLLI::construct(
            rd,
            rs1,
            IImmediate::v32(((0b000000 << 6) | imm) as i32),
        ));
    }

    pub fn srli(&mut self, rd: u8, rs1: u8, imm: u32) {
        self.insn(SRLI::construct(
            rd,
            rs1,
            IImmediate::v32(((0b000000 << 6) | imm) as i32),
        ));
    }

    pub fn srai(&mut self, rd: u8, rs1: u8, imm: u32) {
        self.insn(SRAI::construct(
            rd,
            rs1,
            IImmediate::v32(((0b010000 << 6) | imm) as i32),
        ));
    }

    pub fn add(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(ADD::construct(rd, rs1, rs2));
    }

    pub fn sub(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(SUB::construct(rd, rs1, rs2));
    }

    pub fn sll(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(SLL::construct(rd, rs1, rs2));
    }

    pub fn slt(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(SLT::construct(rd, rs1, rs2));
    }

    pub fn sltu(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(SLTU::construct(rd, rs1, rs2));
    }

    pub fn xor(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(XOR::construct(rd, rs1, rs2));
    }

    pub fn srl(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(SRL::construct(rd, rs1, rs2));
    }

    pub fn sra(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(SRA::construct(rd, rs1, rs2));
    }

    pub fn or(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(OR::construct(rd, rs1, rs2));
    }

    pub fn and(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(AND::construct(rd, rs1, rs2));
    }

    pub fn ecall(&mut self) {
        self.insn(ECALL::construct(zero, zero, IImmediate::v32(0)));
    }

    pub fn ebreak(&mut self) {
        self.insn(EBREAK::construct(zero, zero, IImmediate::v32(1)));
    }

    pub fn addiw(&mut self, rd: u8, rs1: u8, imm: i32) {
        self.insn(ADDIW::construct(rd, rs1, IImmediate::v32(imm)));
    }

    /*pub fn slliw(&mut self, rd: u8, rs1: u8, imm: u32) {
        self.insn(SLLIW::construct(
            rd,
            rs1,
            IImmediate::v32(((0b000000 << 5) | imm) as i32),
        ));
    }

    pub fn srliw(&mut self, rd: u8, rs1: u8, imm: u32) {
        self.insn(SRLIW::construct(
            rd,
            rs1,
            IImmediate::v32(((0b000000 << 5) | imm) as i32),
        ));
    }

    pub fn sraiw(&mut self, rd: u8, rs1: u8, imm: u32) {
        self.insn(SRAIW::construct(
            rd,
            rs1,
            IImmediate::v32(((0b010000 << 5) | imm) as i32),
        ));
    }*/

    pub fn addw(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(ADDW::construct(rd, rs1, rs2));
    }

    pub fn subw(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(SUBW::construct(rd, rs1, rs2));
    }

    pub fn sllw(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(SLLW::construct(rd, rs1, rs2));
    }

    pub fn srlw(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(SRLW::construct(rd, rs1, rs2));
    }

    pub fn sraw(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(SRAW::construct(rd, rs1, rs2));
    }

    pub fn mul(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(MUL::construct(rd, rs1, rs2));
    }

    pub fn mulh(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(MULH::construct(rd, rs1, rs2));
    }

    pub fn mulhsu(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(MULHSU::construct(rd, rs1, rs2));
    }

    pub fn mulhu(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(MULHU::construct(rd, rs1, rs2));
    }

    pub fn mulw(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(MULW::construct(rd, rs1, rs2));
    }

    pub fn div(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(DIV::construct(rd, rs1, rs2));
    }

    pub fn divu(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(DIVU::construct(rd, rs1, rs2));
    }

    pub fn divw(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(DIVW::construct(rd, rs1, rs2));
    }

    pub fn rem(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(REM::construct(rd, rs1, rs2));
    }

    pub fn remu(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(REMU::construct(rd, rs1, rs2));
    }

    pub fn remw(&mut self, rd: u8, rs1: u8, rs2: u8) {
        self.insn(REMW::construct(rd, rs1, rs2));
    }

    pub fn flw(&mut self, rd: u8, rs1: u8, imm: i32) {
        self.insn(FLW::construct(rd, rs1, IImmediate::v32(imm)));
    }

    pub fn fld(&mut self, rd: u8, rs1: u8, imm: i32) {
        self.insn(FLD::construct(rd, rs1, IImmediate::v32(imm)));
    }

    pub fn fsw(&mut self, rs1: u8, rs2: u8, imm: i32) {
        self.insn(FSW::construct(rs1, rs2, SImmediate::v32(imm)));
    }

    pub fn fsd(&mut self, rs1: u8, rs2: u8, imm: i32) {
        self.insn(FSD::construct(rs1, rs2, SImmediate::v32(imm)));
    }

    pub fn fmadd<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8, rs2: u8, rs3: u8) {
        if FP_SIZE == 32 {
            self.insn(FMADD_S::construct(rd, rs1, rs2, rs3, FPRoundingMode::DYN));
        } else {
            self.insn(FMADD_D::construct(rd, rs1, rs2, rs3, FPRoundingMode::DYN));
        }
    }

    pub fn fmsub<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8, rs2: u8, rs3: u8) {
        if FP_SIZE == 32 {
            self.insn(FMSUB_S::construct(rd, rs1, rs2, rs3, FPRoundingMode::DYN));
        } else {
            self.insn(FMSUB_D::construct(rd, rs1, rs2, rs3, FPRoundingMode::DYN));
        }
    }

    pub fn fnmsub<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8, rs2: u8, rs3: u8) {
        if FP_SIZE == 32 {
            self.insn(FNMSUB_S::construct(rd, rs1, rs2, rs3, FPRoundingMode::DYN));
        } else {
            self.insn(FNMSUB_D::construct(rd, rs1, rs2, rs3, FPRoundingMode::DYN));
        }
    }

    pub fn fnmadd<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8, rs2: u8, rs3: u8) {
        if FP_SIZE == 32 {
            self.insn(FNMADD_S::construct(rd, rs1, rs2, rs3, FPRoundingMode::DYN));
        } else {
            self.insn(FNMADD_D::construct(rd, rs1, rs2, rs3, FPRoundingMode::DYN));
        }
    }

    pub fn fadd<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8, rs2: u8) {
        if FP_SIZE == 32 {
            self.insn(FADD_S::construct(rd, rs1, rs2, FPRoundingMode::DYN));
        } else {
            self.insn(FADD_D::construct(rd, rs1, rs2, FPRoundingMode::DYN));
        }
    }

    pub fn fsub<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8, rs2: u8) {
        if FP_SIZE == 32 {
            self.insn(FSUB_S::construct(rd, rs1, rs2, FPRoundingMode::DYN));
        } else {
            self.insn(FSUB_D::construct(rd, rs1, rs2, FPRoundingMode::DYN));
        }
    }

    pub fn fmul<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8, rs2: u8) {
        if FP_SIZE == 32 {
            self.insn(FMUL_S::construct(rd, rs1, rs2, FPRoundingMode::DYN));
        } else {
            self.insn(FMUL_D::construct(rd, rs1, rs2, FPRoundingMode::DYN));
        }
    }

    pub fn fdiv<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8, rs2: u8) {
        if FP_SIZE == 32 {
            self.insn(FDIV_S::construct(rd, rs1, rs2, FPRoundingMode::DYN));
        } else {
            self.insn(FDIV_D::construct(rd, rs1, rs2, FPRoundingMode::DYN));
        }
    }

    pub fn fsqrt<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8) {
        if FP_SIZE == 32 {
            self.insn(FSQRT_S::construct(rd, rs1, 0, FPRoundingMode::DYN));
        } else {
            self.insn(FSQRT_D::construct(rd, rs1, 0, FPRoundingMode::DYN));
        }
    }

    pub fn fsgnj<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8, rs2: u8) {
        if FP_SIZE == 32 {
            self.insn(FSGNJ_S::construct(rd, rs1, rs2));
        } else {
            self.insn(FSGNJ_D::construct(rd, rs1, rs2));
        }
    }

    pub fn fsgnjn<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8, rs2: u8) {
        if FP_SIZE == 32 {
            self.insn(FSGNJN_S::construct(rd, rs1, rs2));
        } else {
            self.insn(FSGNJN_D::construct(rd, rs1, rs2));
        }
    }

    pub fn fsgnjx<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8, rs2: u8) {
        if FP_SIZE == 32 {
            self.insn(FSGNJX_S::construct(rd, rs1, rs2));
        } else {
            self.insn(FSGNJX_D::construct(rd, rs1, rs2));
        }
    }

    pub fn fmin<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8, rs2: u8) {
        if FP_SIZE == 32 {
            self.insn(FMIN_S::construct(rd, rs1, rs2));
        } else {
            self.insn(FMIN_D::construct(rd, rs1, rs2));
        }
    }

    pub fn fmax<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8, rs2: u8) {
        if FP_SIZE == 32 {
            self.insn(FMAX_S::construct(rd, rs1, rs2));
        } else {
            self.insn(FMAX_D::construct(rd, rs1, rs2));
        }
    }

    pub fn feq<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8, rs2: u8) {
        if FP_SIZE == 32 {
            self.insn(FEQ_S::construct(rd, rs1, rs2));
        } else {
            self.insn(FEQ_D::construct(rd, rs1, rs2));
        }
    }

    pub fn flt<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8, rs2: u8) {
        if FP_SIZE == 32 {
            self.insn(FLT_S::construct(rd, rs1, rs2));
        } else {
            self.insn(FLT_D::construct(rd, rs1, rs2));
        }
    }

    pub fn fle<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8, rs2: u8) {
        if FP_SIZE == 32 {
            self.insn(FLE_S::construct(rd, rs1, rs2));
        } else {
            self.insn(FLE_D::construct(rd, rs1, rs2));
        }
    }

    pub fn fclass<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8) {
        if FP_SIZE == 32 {
            self.insn(FCLASS_S::construct(rd, rs1, 0));
        } else {
            self.insn(FCLASS_D::construct(rd, rs1, 0));
        }
    }

    pub fn fcvt_fp2si<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8, rm: FPRoundingMode) {
        if FP_SIZE == 32 {
            self.insn(FCVT_W_S::construct(rd, rs1, rm));
        } else {
            self.insn(FCVT_L_S::construct(rd, rs1, rm));
        }
    }

    pub fn fcvt_fp2ui<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8, rm: FPRoundingMode) {
        if FP_SIZE == 32 {
            self.insn(FCVT_WU_S::construct(rd, rs1, rm));
        } else {
            self.insn(FCVT_LU_S::construct(rd, rs1, rm));
        }
    }

    pub fn fcvt_si2fp<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8, rm: FPRoundingMode) {
        if FP_SIZE == 32 {
            self.insn(FCVT_S_W::construct(rd, rs1, rm));
        } else {
            self.insn(FCVT_D_W::construct(rd, rs1, rm));
        }
    }

    pub fn fcvt_ui2fp<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8, rm: FPRoundingMode) {
        if FP_SIZE == 32 {
            self.insn(FCVT_S_WU::construct(rd, rs1, rm));
        } else {
            self.insn(FCVT_S_LU::construct(rd, rs1, rm));
        }
    }

    pub fn fcvt_fp2fp<const FP_SIZE: usize, const FP_SIZE2: usize>(
        &mut self,
        rd: u8,
        rs1: u8,
        rm: FPRoundingMode,
    ) {
        if FP_SIZE == 32 && FP_SIZE2 == 64 {
            self.insn(FCVT_D_S::construct(rd, rs1, rm));
        } else if FP_SIZE == 64 && FP_SIZE2 == 32 {
            self.insn(FCVT_S_D::construct(rd, rs1, rm));
        }
    }

    pub fn fmv_fp2i<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8) {
        if FP_SIZE == 32 {
            self.insn(FMV_X_W::construct(rd, rs1));
        } else {
            self.insn(FMV_X_D::construct(rd, rs1));
        }
    }

    pub fn fmv_i2fp<const FP_SIZE: usize>(&mut self, rd: u8, rs1: u8) {
        if FP_SIZE == 32 {
            self.insn(FMV_W_X::construct(rd, rs1));
        } else {
            self.insn(FMV_D_X::construct(rd, rs1));
        }
    }

    pub fn fence(&mut self, pred: &[MemoryOperation], succ: &[MemoryOperation]) {
        let mut pred_val = 0;

        for op in pred.iter() {
            pred_val |= *op as u32;
        }

        let mut succ_val = 0;

        for op in succ.iter() {
            succ_val |= *op as u32;
        }

        let immediate = 0
            | (0b0000 << 8)
            | ((pred_val & ((1 << 4) - 1)) << 4)
            | ((succ_val & ((1 << 4) - 1)) << 0);

        self.insn(FENCE::construct(
            zero,
            zero,
            IImmediate::v32(immediate as i32),
        ));
    }

    pub fn lrw(&mut self, rd: u8, rs1: u8, access: &[MemoryAccess]) {
        self.insn(LR_W::construct(rd, rs1, zero, access))
    }

    pub fn scw(&mut self, rd: u8, rs1: u8, rs2: u8, access: &[MemoryAccess]) {
        self.insn(SC_W::construct(rd, rs1, rs2, access))
    }

    pub fn lrd(&mut self, rd: u8, rs1: u8, access: &[MemoryAccess]) {
        self.insn(LR_D::construct(rd, rs1, zero, access))
    }

    pub fn scd(&mut self, rd: u8, rs1: u8, rs2: u8, access: &[MemoryAccess]) {
        self.insn(SC_D::construct(rd, rs1, rs2, access))
    }

    pub fn amoswap(&mut self, rd: u8, rs1: u8, rs2: u8, access: &[MemoryAccess]) {
        self.insn(AMOSWAP_W::construct(rd, rs1, rs2, access))
    }

    pub fn amoor(&mut self, rd: u8, rs1: u8, rs2: u8, access: &[MemoryAccess]) {
        self.insn(AMOOR_W::construct(rd, rs1, rs2, access))
    }

    pub fn amoxor(&mut self, rd: u8, rs1: u8, rs2: u8, access: &[MemoryAccess]) {
        self.insn(AMOXOR_W::construct(rd, rs1, rs2, access))
    }

    pub fn amoand(&mut self, rd: u8, rs1: u8, rs2: u8, access: &[MemoryAccess]) {
        self.insn(AMOAND_W::construct(rd, rs1, rs2, access))
    }

    pub fn amomin(&mut self, rd: u8, rs1: u8, rs2: u8, access: &[MemoryAccess]) {
        self.insn(AMOMIN_W::construct(rd, rs1, rs2, access))
    }

    pub fn amomax(&mut self, rd: u8, rs1: u8, rs2: u8, access: &[MemoryAccess]) {
        self.insn(AMOMAX_W::construct(rd, rs1, rs2, access))
    }

    pub fn amominu(&mut self, rd: u8, rs1: u8, rs2: u8, access: &[MemoryAccess]) {
        self.insn(AMOMINU_W::construct(rd, rs1, rs2, access))
    }

    pub fn amomaxu(&mut self, rd: u8, rs1: u8, rs2: u8, access: &[MemoryAccess]) {
        self.insn(AMOMAXU_W::construct(rd, rs1, rs2, access))
    }

    pub fn amoswapd(&mut self, rd: u8, rs1: u8, rs2: u8, access: &[MemoryAccess]) {
        self.insn(AMOSWAP_D::construct(rd, rs1, rs2, access))
    }

    pub fn amoord(&mut self, rd: u8, rs1: u8, rs2: u8, access: &[MemoryAccess]) {
        self.insn(AMOOR_D::construct(rd, rs1, rs2, access))
    }

    pub fn amoxord(&mut self, rd: u8, rs1: u8, rs2: u8, access: &[MemoryAccess]) {
        self.insn(AMOXOR_D::construct(rd, rs1, rs2, access))
    }

    pub fn amoandd(&mut self, rd: u8, rs1: u8, rs2: u8, access: &[MemoryAccess]) {
        self.insn(AMOAND_D::construct(rd, rs1, rs2, access))
    }

    pub fn amomind(&mut self, rd: u8, rs1: u8, rs2: u8, access: &[MemoryAccess]) {
        self.insn(AMOMIN_D::construct(rd, rs1, rs2, access))
    }

    pub fn amomaxd(&mut self, rd: u8, rs1: u8, rs2: u8, access: &[MemoryAccess]) {
        self.insn(AMOMAX_D::construct(rd, rs1, rs2, access))
    }

    pub fn amominud(&mut self, rd: u8, rs1: u8, rs2: u8, access: &[MemoryAccess]) {
        self.insn(AMOMINU_D::construct(rd, rs1, rs2, access))
    }

    pub fn amomaxud(&mut self, rd: u8, rs1: u8, rs2: u8, access: &[MemoryAccess]) {
        self.insn(AMOMAXU_D::construct(rd, rs1, rs2, access))
    }

    /*pub fn slli(&mut self, rd: u8, rs1: u8, shamt: u32) {
        self.insn(SLLI::construct(rd, rs1, shamt))
    }

    pub fn srli(&mut self, rd: u8, rs1: u8, shamt: u32) {
        self.insn(SRLI::construct(rd, rs1, shamt))
    }

    pub fn srai(&mut self, rd: u8, rs1: u8, shamt: u32) {
        self.insn(SRAI::construct(rd, rs1, shamt))
    }*/

    pub fn slliw(&mut self, rd: u8, rs1: u8, shamt: u32) {
        self.insn(SLLIW::construct(rd, rs1, shamt))
    }

    pub fn srliw(&mut self, rd: u8, rs1: u8, shamt: u32) {
        self.insn(SRLIW::construct(rd, rs1, shamt))
    }

    pub fn sraiw(&mut self, rd: u8, rs1: u8, shamt: u32) {
        self.insn(SRAIW::construct(rd, rs1, shamt))
    }

    pub fn nop(&mut self) {
        self.addi(zero, zero, 0);
    }

    pub fn align(&mut self, alignment: usize) {
        while !self.buffer.is_aligned(alignment) {
            self.nop();
        }
    }

    pub fn mask_register(&mut self, rd: u8, rs: u8, mask: u32) {
        self.slli(rd, rs, 64 - mask);
        self.srli(rd, rd, 64 - mask);
    }

    pub fn sign_extend(&mut self, rd: u8, rs: u8, bits: u32) {
        if bits == 64 {
            return;
        }

        if bits == 32 {
            self.addiw(rd, rs, 0);
            return;
        }

        self.slli(rd, rs, 64 - bits);
        self.srai(rd, rd, 64 - bits);
    }

    pub fn zero_extend(&mut self, rd: u8, rs: u8, bits: u32) {
        if bits == 64 {
            return;
        }

        self.slli(rd, rs, 64 - bits);
        self.srli(rd, rd, 64 - bits);
    }

    pub fn insn(&mut self, insn: u32) {
        self.buffer.put_int(insn as _);
    }

    pub fn jump_placeholder(&mut self, functor: impl FnOnce(&mut Self)) {
        LinkJumpImpl::generate_placeholder(self, functor)
    }

    pub fn branch_placeholder(&mut self, functor: impl FnOnce(&mut Self)) {
        LinkBranchImpl::generate_placeholder(self, functor)
    }

    pub fn pointer_call_placeholder(&mut self, functor: impl FnOnce(&mut Self)) {
        PatchPointerImpl::generate_placeholder(self, functor)
    }

    pub fn near_call_placeholder(&mut self, functor: impl FnOnce(&mut Self)) {
        LinkCallImpl::generate_placeholder(self, functor)
    }
}

pub trait LinkJumpOrCallImpl {
    unsafe fn apply(location: *mut u32, target: *mut u8) {
        let instruction = InstructionValue::new(location.add(1).read());

        let destination = instruction.field::<7, 5>();
        let mut offset = target as i64 - location.add(1) as i64;
        if JImmediate::is_valid(offset) {
            location.write(ADDI::construct(x0, x0, IImmediate::v32(0)));
            location.add(1).write(JAL::construct(
                destination as _,
                JImmediate::v32(offset as i32),
            ));
            return;
        }

        offset += size_of::<u32>() as i64;

        let immediate = ImmediateDecomposition::new64(offset);
        location.write(AUIPC::construct(x30, immediate.upper));
        location
            .add(1)
            .write(JALR::construct(destination as _, x30, immediate.lower));
    }
}

pub struct LinkJumpImpl;

impl LinkJumpOrCallImpl for LinkJumpImpl {}

impl LinkJumpImpl {
    pub fn placeholder_insn() -> u32 {
        ADDI::construct(x0, x0, IImmediate::v32(1))
    }

    pub fn generate_placeholder(
        assembler: &mut RISCV64Assembler,
        functor: impl FnOnce(&mut RISCV64Assembler),
    ) {
        assembler.insn(Self::placeholder_insn());
        functor(assembler);
    }
}

pub struct LinkCallImpl;

impl LinkJumpOrCallImpl for LinkCallImpl {}

impl LinkCallImpl {
    pub fn placeholder_insn() -> u32 {
        ADDI::construct(x0, x0, IImmediate::v32(2))
    }

    pub fn generate_placeholder(
        assembler: &mut RISCV64Assembler,
        functor: impl FnOnce(&mut RISCV64Assembler),
    ) {
        assembler.insn(Self::placeholder_insn());
        functor(assembler);
    }
}

pub struct LinkBranchImpl;

impl LinkBranchImpl {
    pub fn placeholder_insn() -> u32 {
        ADDI::construct(x0, x0, IImmediate::v32(3))
    }

    pub fn generate_placeholder(
        assembler: &mut RISCV64Assembler,
        functor: impl FnOnce(&mut RISCV64Assembler),
    ) {
        let insn_value = Self::placeholder_insn();
        for _ in 0..2 {
            assembler.insn(insn_value);
        }
        functor(assembler);
    }

    pub unsafe fn apply(location: *mut u32, target: *mut u8) {
        let instruction = InstructionValue::new(location.add(2).read());
        assert!(instruction.opcode() == BRANCH);

        let branch_instruction_for_func3 = |funct3, rs1, rs2, imm: BImmediate| match funct3 {
            BEQ::FUNCT3_VALUE => BEQ::construct(rs1, rs2, imm),
            BNE::FUNCT3_VALUE => BNE::construct(rs1, rs2, imm),
            BLT::FUNCT3_VALUE => BLT::construct(rs1, rs2, imm),
            BGE::FUNCT3_VALUE => BGE::construct(rs1, rs2, imm),
            BLTU::FUNCT3_VALUE => BLTU::construct(rs1, rs2, imm),
            BGEU::FUNCT3_VALUE => BGEU::construct(rs1, rs2, imm),
            _ => unreachable!("Invalid funct3 value for patching branch: {}", funct3),
        };

        let lhs = instruction.field::<15, 5>() as u8;
        let rhs = instruction.field::<20, 5>() as u8;

        let mut offset = target as i64 - location.add(2) as i64;

        if BImmediate::is_valid(offset) {
            location.write(ADDI::construct(x0, x0, IImmediate::v32(0)));
            location
                .add(1)
                .write(ADDI::construct(x0, x0, IImmediate::v32(0)));
            location.add(2).write(branch_instruction_for_func3(
                instruction.field::<12, 3>(),
                lhs,
                rhs,
                BImmediate::v32(offset as i32),
            ));

            return;
        }

        if JImmediate::is_valid(offset) {
            location.write(ADDI::construct(x0, x0, IImmediate::v32(0)));
            location.add(1).write(branch_instruction_for_func3(
                instruction.field::<12, 3>() ^ 0b001,
                lhs,
                rhs,
                BImmediate::v32(8),
            ));
            location
                .add(2)
                .write(JAL::construct(x0, JImmediate::v32(offset as i32)));
            return;
        }

        offset += size_of::<u32>() as i64;

        let immediate = ImmediateDecomposition::new64(offset);

        location.write(branch_instruction_for_func3(
            instruction.field::<12, 3>() ^ 0b001,
            lhs,
            rhs,
            BImmediate::v32(12),
        ));

        location
            .add(1)
            .write(AUIPC::construct(x31, immediate.upper));
        location
            .add(2)
            .write(JALR::construct(x0, x30, immediate.lower));
    }
}

pub struct PatchPointerImpl;

impl PatchPointerImpl {
    pub fn placeholder_insn() -> u32 {
        ADDI::construct(x0, x0, IImmediate::v32(4))
    }

    pub fn generate_placeholder(
        assembler: &mut RISCV64Assembler,
        functor: impl FnOnce(&mut RISCV64Assembler),
    ) {
        let insn_value = Self::placeholder_insn();
        for _ in 0..7 {
            assembler.insn(insn_value);
        }
        functor(assembler);
    }

    pub unsafe fn apply(location: *mut u32, value: *mut u8) {
        let instruction = InstructionValue::new(location.add(7).read());
        let valid_location =
            instruction.opcode() == OP_IMM && instruction.field::<12, 3>() == 0b000;
        assert!(valid_location);

        Self::apply_with_dest(location, instruction.field::<7, 5>() as _, value)
    }

    pub unsafe fn apply_with_dest(location: *mut u32, destination: u8, value: *mut u8) {
        let imml = ImmediateLoader::new_placeholder(value as i64);

        for i in 0..imml.opcount {
            let op = imml.ops[imml.opcount - (i + 1)];

            match op.op_type {
                ImmOpType::IImmediate => {
                    location.add(i).write(ADDI::construct(
                        destination,
                        zero,
                        IImmediate::new(op.value),
                    ));
                }

                ImmOpType::LUI => {
                    location
                        .add(i)
                        .write(LUI::construct(destination, UImmediate::new(op.value)));
                }

                ImmOpType::ADDI => {
                    location.add(i).write(ADDI::construct(
                        destination,
                        destination,
                        IImmediate::new(op.value),
                    ));
                }

                ImmOpType::LSHIFT12 => {
                    location
                        .add(i)
                        .write(SLLI::construct(destination, destination, 12));
                }

                ImmOpType::NOP => {
                    location
                        .add(i)
                        .write(ADDI::construct(x0, x0, IImmediate::v32(0)));
                }
            }
        }
    }

    pub unsafe fn read(location: *mut u32) -> *mut u8 {
        let instruction_value = InstructionValue::new(location.add(7).read());

        assert!(
            instruction_value.opcode() == OP_IMM && instruction_value.field::<12, 3>() == 0b000
        );

        let dest = instruction_value.field::<7, 5>() as u8;

        let mut i = 0;

        {
            // Iterate through all NOP instructions generated for the purposes of the placeholder.
            let nop_insn = ADDI::construct(x0, x0, IImmediate::v32(0));

            while i < 8 {
                if location.add(i).read() != nop_insn {
                    break;
                }
                i += 1;
            }
        }

        let mut target = 0;

        while i < 8 {
            let insn = InstructionValue::new(location.add(i).read());
            if insn.opcode() == LUI && insn.field::<7, 5>() == dest as u32 {
                target = UImmediate::value(insn) as i32 as i64;
            } else if insn.opcode() == OP_IMM && insn.field::<12, 3>() == 0b000 {
                target += IImmediate::value(insn) as i64;
            } else {
                assert!(insn.value == SLLI::construct(dest, dest, 12));
                target <<= 12;
            }

            i += 1;
        }

        target as *mut u8
    }
}

pub struct ImmediateLoader {
    ops: [ImmOp; 8],
    opcount: usize,
}

impl ImmediateLoader {
    pub fn new(imm: i64) -> Self {
        let mut this = Self {
            ops: [ImmOp {
                op_type: ImmOpType::NOP,
                value: 0,
            }; 8],
            opcount: 0,
        };
        // If the immediate value fits into the IImmediate mold, we can short-cut to just generating that through a single ADDI.
        if IImmediate::is_valid(imm) {
            this.ops[this.opcount] = ImmOp {
                op_type: ImmOpType::IImmediate,
                value: IImmediate::v32::<i32>(imm as i32) as u32,
            };
            this.opcount += 1;
            return this;
        }

        // The immediate is larger than 12 bits, so it has to be loaded through the initial LUI and then additional shift-and-addi pairs.
        // This sequence is generated in reverse. move_into() or other users traverse the sequence accordingly.
        let mut value = imm;

        loop {
            let add_imm = value & ((1 << 12) - 1);
            // The addi will be sign-extending the 12-bit value and adding it to the register-contained value. If the addi-immediate
            // is negative, the remaining immediate has to be increased by 2^12 to offset the subsequent subtraction.
            if add_imm & (1 << 11) != 0 {
                value += 1 << 12;
            }

            this.ops[this.opcount] = ImmOp {
                op_type: ImmOpType::ADDI,
                value: add_imm as u32,
            };

            this.opcount += 1;

            // Shift out the bits incorporated into the just-added addi.
            value = value >> 12;

            // If the remainder of the immediate can fit into a 20-bit immediate, we can generate the LUI instruction that will end up
            // loading the initial higher bits of the desired immediate.
            if is_valid64::<20>(value as _) {
                this.ops[this.opcount] = ImmOp {
                    op_type: ImmOpType::LUI,
                    value: ((value & ((1 << 20) - 1)) << 12) as u32,
                };
                this.opcount += 1;
                break;
            }

            // Otherwise, generate the lshift operation that will make room for lower parts of the immediate value.
            this.ops[this.opcount] = ImmOp {
                op_type: ImmOpType::LSHIFT12,
                value: 0,
            };

            this.opcount += 1;
        }

        this
    }

    pub fn new_placeholder(imm: i64) -> Self {
        let mut this = Self::new(imm);
        // The non-placeholder constructor already generated the necessary operations to load this immediate.
        // This constructor still fills out the remaining potential operations as nops. This enables future patching
        // of these instructions with other immediate-load sequences.
        for i in this.opcount..this.ops.len() {
            this.ops[i] = ImmOp {
                op_type: ImmOpType::NOP,
                value: 0,
            };
        }

        this.opcount = this.ops.len();
        this
    }

    pub fn move_into(&self, assembler: &mut RISCV64Assembler, dest: u8) {
        // This is a helper method that generates the necessary instructions through the RISCV64Assembler infrastructure.
        // Operations are traversed in reverse in order to match the generation process.

        for i in 0..self.opcount {
            let op = self.ops[self.opcount - (i + 1)];

            match op.op_type {
                ImmOpType::IImmediate => {
                    assembler.addi(dest, zero, op.value as _);
                }

                ImmOpType::LUI => {
                    assembler.lui(dest, op.value as _);
                }

                ImmOpType::ADDI => {
                    assembler.addi(dest, dest, op.value as _);
                }

                ImmOpType::LSHIFT12 => {
                    assembler.slli(dest, dest, 12);
                }

                ImmOpType::NOP => {
                    assembler.addi(zero, zero, 0);
                }
            }
        }
    }
}

pub fn is_valid64<const N: usize>(imm: i64) -> bool {
    let shift = std::mem::size_of::<i64>() * 8 - N;
    imm == ((imm << shift) >> shift)
}

pub fn is_valid32<const N: usize>(imm: i32) -> bool {
    let shift = std::mem::size_of::<i32>() * 8 - N;
    imm == ((imm << shift) >> shift)
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum ImmOpType {
    IImmediate,
    LUI,
    ADDI,
    LSHIFT12,
    NOP,
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct ImmOp {
    pub op_type: ImmOpType,
    pub value: u32,
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum Condition {
    EQ,
    NE,
    GTU,
    LEU,
    GEU,
    LTU,
    GT,
    LE,
    GE,
    LT,
}

impl Condition {
    pub fn invert(self) -> Self {
        match self {
            Condition::EQ => Condition::NE,
            Condition::NE => Condition::EQ,
            Condition::GTU => Condition::LEU,
            Condition::LEU => Condition::GTU,
            Condition::GEU => Condition::LTU,
            Condition::LTU => Condition::GEU,
            Condition::GT => Condition::LE,
            Condition::LE => Condition::GT,
            Condition::GE => Condition::LT,
            Condition::LT => Condition::GE,
        }
    }
}