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

#include "EmulateInstructionMIPS.h"

#include <stdlib.h>

#include "lldb/Core/Address.h"
#include "lldb/Core/Opcode.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Symbol/UnwindPlan.h"
#include "lldb/Target/Target.h"
#include "lldb/Utility/ArchSpec.h"
#include "lldb/Utility/ConstString.h"
#include "lldb/Utility/DataExtractor.h"
#include "lldb/Utility/RegisterValue.h"
#include "lldb/Utility/Stream.h"
#include "llvm-c/Disassembler.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCDisassembler/MCDisassembler.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/TargetSelect.h"

#include "llvm/ADT/STLExtras.h"

#include "Plugins/Process/Utility/InstructionUtils.h"
#include "Plugins/Process/Utility/RegisterContext_mips.h"

using namespace lldb;
using namespace lldb_private;

#define UInt(x) ((uint64_t)x)
#define integer int64_t

//
// EmulateInstructionMIPS implementation
//

#ifdef __mips__
extern "C" {
void LLVMInitializeMipsTargetInfo();
void LLVMInitializeMipsTarget();
void LLVMInitializeMipsAsmPrinter();
void LLVMInitializeMipsTargetMC();
void LLVMInitializeMipsDisassembler();
}
#endif

EmulateInstructionMIPS::EmulateInstructionMIPS(
    const lldb_private::ArchSpec &arch)
    : EmulateInstruction(arch) {
  /* Create instance of llvm::MCDisassembler */
  std::string Status;
  llvm::Triple triple = arch.GetTriple();
  const llvm::Target *target =
      llvm::TargetRegistry::lookupTarget(triple.getTriple(), Status);

/*
 * If we fail to get the target then we haven't registered it. The
 * SystemInitializerCommon
 * does not initialize targets, MCs and disassemblers. However we need the
 * MCDisassembler
 * to decode the instructions so that the decoding complexity stays with LLVM.
 * Initialize the MIPS targets and disassemblers.
*/
#ifdef __mips__
  if (!target) {
    LLVMInitializeMipsTargetInfo();
    LLVMInitializeMipsTarget();
    LLVMInitializeMipsAsmPrinter();
    LLVMInitializeMipsTargetMC();
    LLVMInitializeMipsDisassembler();
    target = llvm::TargetRegistry::lookupTarget(triple.getTriple(), Status);
  }
#endif

  assert(target);

  llvm::StringRef cpu;

  switch (arch.GetCore()) {
  case ArchSpec::eCore_mips32:
  case ArchSpec::eCore_mips32el:
    cpu = "mips32";
    break;
  case ArchSpec::eCore_mips32r2:
  case ArchSpec::eCore_mips32r2el:
    cpu = "mips32r2";
    break;
  case ArchSpec::eCore_mips32r3:
  case ArchSpec::eCore_mips32r3el:
    cpu = "mips32r3";
    break;
  case ArchSpec::eCore_mips32r5:
  case ArchSpec::eCore_mips32r5el:
    cpu = "mips32r5";
    break;
  case ArchSpec::eCore_mips32r6:
  case ArchSpec::eCore_mips32r6el:
    cpu = "mips32r6";
    break;
  case ArchSpec::eCore_mips64:
  case ArchSpec::eCore_mips64el:
    cpu = "mips64";
    break;
  case ArchSpec::eCore_mips64r2:
  case ArchSpec::eCore_mips64r2el:
    cpu = "mips64r2";
    break;
  case ArchSpec::eCore_mips64r3:
  case ArchSpec::eCore_mips64r3el:
    cpu = "mips64r3";
    break;
  case ArchSpec::eCore_mips64r5:
  case ArchSpec::eCore_mips64r5el:
    cpu = "mips64r5";
    break;
  case ArchSpec::eCore_mips64r6:
  case ArchSpec::eCore_mips64r6el:
    cpu = "mips64r6";
    break;
  default:
    cpu = "generic";
    break;
  }

  std::string features = "";
  uint32_t arch_flags = arch.GetFlags();
  if (arch_flags & ArchSpec::eMIPSAse_msa)
    features += "+msa,";
  if (arch_flags & ArchSpec::eMIPSAse_dsp)
    features += "+dsp,";
  if (arch_flags & ArchSpec::eMIPSAse_dspr2)
    features += "+dspr2,";

  m_reg_info.reset(target->createMCRegInfo(triple.getTriple()));
  assert(m_reg_info.get());

  m_insn_info.reset(target->createMCInstrInfo());
  assert(m_insn_info.get());

  m_asm_info.reset(target->createMCAsmInfo(*m_reg_info, triple.getTriple()));
  m_subtype_info.reset(
      target->createMCSubtargetInfo(triple.getTriple(), cpu, features));
  assert(m_asm_info.get() && m_subtype_info.get());

  m_context.reset(
      new llvm::MCContext(m_asm_info.get(), m_reg_info.get(), nullptr));
  assert(m_context.get());

  m_disasm.reset(target->createMCDisassembler(*m_subtype_info, *m_context));
  assert(m_disasm.get());

  /* Create alternate disassembler for microMIPS */
  if (arch_flags & ArchSpec::eMIPSAse_mips16)
    features += "+mips16,";
  else if (arch_flags & ArchSpec::eMIPSAse_micromips)
    features += "+micromips,";

  m_alt_subtype_info.reset(
      target->createMCSubtargetInfo(triple.getTriple(), cpu, features));
  assert(m_alt_subtype_info.get());

  m_alt_disasm.reset(
      target->createMCDisassembler(*m_alt_subtype_info, *m_context));
  assert(m_alt_disasm.get());

  m_next_inst_size = 0;
  m_use_alt_disaasm = false;
}

void EmulateInstructionMIPS::Initialize() {
  PluginManager::RegisterPlugin(GetPluginNameStatic(),
                                GetPluginDescriptionStatic(), CreateInstance);
}

void EmulateInstructionMIPS::Terminate() {
  PluginManager::UnregisterPlugin(CreateInstance);
}

ConstString EmulateInstructionMIPS::GetPluginNameStatic() {
  ConstString g_plugin_name("lldb.emulate-instruction.mips32");
  return g_plugin_name;
}

lldb_private::ConstString EmulateInstructionMIPS::GetPluginName() {
  static ConstString g_plugin_name("EmulateInstructionMIPS");
  return g_plugin_name;
}

const char *EmulateInstructionMIPS::GetPluginDescriptionStatic() {
  return "Emulate instructions for the MIPS32 architecture.";
}

EmulateInstruction *
EmulateInstructionMIPS::CreateInstance(const ArchSpec &arch,
                                       InstructionType inst_type) {
  if (EmulateInstructionMIPS::SupportsEmulatingInstructionsOfTypeStatic(
          inst_type)) {
    if (arch.GetTriple().getArch() == llvm::Triple::mips ||
        arch.GetTriple().getArch() == llvm::Triple::mipsel) {
      return new EmulateInstructionMIPS(arch);
    }
  }

  return nullptr;
}

bool EmulateInstructionMIPS::SetTargetTriple(const ArchSpec &arch) {
  return arch.GetTriple().getArch() == llvm::Triple::mips ||
         arch.GetTriple().getArch() == llvm::Triple::mipsel;
}

const char *EmulateInstructionMIPS::GetRegisterName(unsigned reg_num,
                                                    bool alternate_name) {
  if (alternate_name) {
    switch (reg_num) {
    case dwarf_sp_mips:
      return "r29";
    case dwarf_r30_mips:
      return "r30";
    case dwarf_ra_mips:
      return "r31";
    case dwarf_f0_mips:
      return "f0";
    case dwarf_f1_mips:
      return "f1";
    case dwarf_f2_mips:
      return "f2";
    case dwarf_f3_mips:
      return "f3";
    case dwarf_f4_mips:
      return "f4";
    case dwarf_f5_mips:
      return "f5";
    case dwarf_f6_mips:
      return "f6";
    case dwarf_f7_mips:
      return "f7";
    case dwarf_f8_mips:
      return "f8";
    case dwarf_f9_mips:
      return "f9";
    case dwarf_f10_mips:
      return "f10";
    case dwarf_f11_mips:
      return "f11";
    case dwarf_f12_mips:
      return "f12";
    case dwarf_f13_mips:
      return "f13";
    case dwarf_f14_mips:
      return "f14";
    case dwarf_f15_mips:
      return "f15";
    case dwarf_f16_mips:
      return "f16";
    case dwarf_f17_mips:
      return "f17";
    case dwarf_f18_mips:
      return "f18";
    case dwarf_f19_mips:
      return "f19";
    case dwarf_f20_mips:
      return "f20";
    case dwarf_f21_mips:
      return "f21";
    case dwarf_f22_mips:
      return "f22";
    case dwarf_f23_mips:
      return "f23";
    case dwarf_f24_mips:
      return "f24";
    case dwarf_f25_mips:
      return "f25";
    case dwarf_f26_mips:
      return "f26";
    case dwarf_f27_mips:
      return "f27";
    case dwarf_f28_mips:
      return "f28";
    case dwarf_f29_mips:
      return "f29";
    case dwarf_f30_mips:
      return "f30";
    case dwarf_f31_mips:
      return "f31";
    case dwarf_w0_mips:
      return "w0";
    case dwarf_w1_mips:
      return "w1";
    case dwarf_w2_mips:
      return "w2";
    case dwarf_w3_mips:
      return "w3";
    case dwarf_w4_mips:
      return "w4";
    case dwarf_w5_mips:
      return "w5";
    case dwarf_w6_mips:
      return "w6";
    case dwarf_w7_mips:
      return "w7";
    case dwarf_w8_mips:
      return "w8";
    case dwarf_w9_mips:
      return "w9";
    case dwarf_w10_mips:
      return "w10";
    case dwarf_w11_mips:
      return "w11";
    case dwarf_w12_mips:
      return "w12";
    case dwarf_w13_mips:
      return "w13";
    case dwarf_w14_mips:
      return "w14";
    case dwarf_w15_mips:
      return "w15";
    case dwarf_w16_mips:
      return "w16";
    case dwarf_w17_mips:
      return "w17";
    case dwarf_w18_mips:
      return "w18";
    case dwarf_w19_mips:
      return "w19";
    case dwarf_w20_mips:
      return "w20";
    case dwarf_w21_mips:
      return "w21";
    case dwarf_w22_mips:
      return "w22";
    case dwarf_w23_mips:
      return "w23";
    case dwarf_w24_mips:
      return "w24";
    case dwarf_w25_mips:
      return "w25";
    case dwarf_w26_mips:
      return "w26";
    case dwarf_w27_mips:
      return "w27";
    case dwarf_w28_mips:
      return "w28";
    case dwarf_w29_mips:
      return "w29";
    case dwarf_w30_mips:
      return "w30";
    case dwarf_w31_mips:
      return "w31";
    case dwarf_mir_mips:
      return "mir";
    case dwarf_mcsr_mips:
      return "mcsr";
    case dwarf_config5_mips:
      return "config5";
    default:
      break;
    }
    return nullptr;
  }

  switch (reg_num) {
  case dwarf_zero_mips:
    return "r0";
  case dwarf_r1_mips:
    return "r1";
  case dwarf_r2_mips:
    return "r2";
  case dwarf_r3_mips:
    return "r3";
  case dwarf_r4_mips:
    return "r4";
  case dwarf_r5_mips:
    return "r5";
  case dwarf_r6_mips:
    return "r6";
  case dwarf_r7_mips:
    return "r7";
  case dwarf_r8_mips:
    return "r8";
  case dwarf_r9_mips:
    return "r9";
  case dwarf_r10_mips:
    return "r10";
  case dwarf_r11_mips:
    return "r11";
  case dwarf_r12_mips:
    return "r12";
  case dwarf_r13_mips:
    return "r13";
  case dwarf_r14_mips:
    return "r14";
  case dwarf_r15_mips:
    return "r15";
  case dwarf_r16_mips:
    return "r16";
  case dwarf_r17_mips:
    return "r17";
  case dwarf_r18_mips:
    return "r18";
  case dwarf_r19_mips:
    return "r19";
  case dwarf_r20_mips:
    return "r20";
  case dwarf_r21_mips:
    return "r21";
  case dwarf_r22_mips:
    return "r22";
  case dwarf_r23_mips:
    return "r23";
  case dwarf_r24_mips:
    return "r24";
  case dwarf_r25_mips:
    return "r25";
  case dwarf_r26_mips:
    return "r26";
  case dwarf_r27_mips:
    return "r27";
  case dwarf_gp_mips:
    return "gp";
  case dwarf_sp_mips:
    return "sp";
  case dwarf_r30_mips:
    return "fp";
  case dwarf_ra_mips:
    return "ra";
  case dwarf_sr_mips:
    return "sr";
  case dwarf_lo_mips:
    return "lo";
  case dwarf_hi_mips:
    return "hi";
  case dwarf_bad_mips:
    return "bad";
  case dwarf_cause_mips:
    return "cause";
  case dwarf_pc_mips:
    return "pc";
  case dwarf_f0_mips:
    return "f0";
  case dwarf_f1_mips:
    return "f1";
  case dwarf_f2_mips:
    return "f2";
  case dwarf_f3_mips:
    return "f3";
  case dwarf_f4_mips:
    return "f4";
  case dwarf_f5_mips:
    return "f5";
  case dwarf_f6_mips:
    return "f6";
  case dwarf_f7_mips:
    return "f7";
  case dwarf_f8_mips:
    return "f8";
  case dwarf_f9_mips:
    return "f9";
  case dwarf_f10_mips:
    return "f10";
  case dwarf_f11_mips:
    return "f11";
  case dwarf_f12_mips:
    return "f12";
  case dwarf_f13_mips:
    return "f13";
  case dwarf_f14_mips:
    return "f14";
  case dwarf_f15_mips:
    return "f15";
  case dwarf_f16_mips:
    return "f16";
  case dwarf_f17_mips:
    return "f17";
  case dwarf_f18_mips:
    return "f18";
  case dwarf_f19_mips:
    return "f19";
  case dwarf_f20_mips:
    return "f20";
  case dwarf_f21_mips:
    return "f21";
  case dwarf_f22_mips:
    return "f22";
  case dwarf_f23_mips:
    return "f23";
  case dwarf_f24_mips:
    return "f24";
  case dwarf_f25_mips:
    return "f25";
  case dwarf_f26_mips:
    return "f26";
  case dwarf_f27_mips:
    return "f27";
  case dwarf_f28_mips:
    return "f28";
  case dwarf_f29_mips:
    return "f29";
  case dwarf_f30_mips:
    return "f30";
  case dwarf_f31_mips:
    return "f31";
  case dwarf_fcsr_mips:
    return "fcsr";
  case dwarf_fir_mips:
    return "fir";
  case dwarf_w0_mips:
    return "w0";
  case dwarf_w1_mips:
    return "w1";
  case dwarf_w2_mips:
    return "w2";
  case dwarf_w3_mips:
    return "w3";
  case dwarf_w4_mips:
    return "w4";
  case dwarf_w5_mips:
    return "w5";
  case dwarf_w6_mips:
    return "w6";
  case dwarf_w7_mips:
    return "w7";
  case dwarf_w8_mips:
    return "w8";
  case dwarf_w9_mips:
    return "w9";
  case dwarf_w10_mips:
    return "w10";
  case dwarf_w11_mips:
    return "w11";
  case dwarf_w12_mips:
    return "w12";
  case dwarf_w13_mips:
    return "w13";
  case dwarf_w14_mips:
    return "w14";
  case dwarf_w15_mips:
    return "w15";
  case dwarf_w16_mips:
    return "w16";
  case dwarf_w17_mips:
    return "w17";
  case dwarf_w18_mips:
    return "w18";
  case dwarf_w19_mips:
    return "w19";
  case dwarf_w20_mips:
    return "w20";
  case dwarf_w21_mips:
    return "w21";
  case dwarf_w22_mips:
    return "w22";
  case dwarf_w23_mips:
    return "w23";
  case dwarf_w24_mips:
    return "w24";
  case dwarf_w25_mips:
    return "w25";
  case dwarf_w26_mips:
    return "w26";
  case dwarf_w27_mips:
    return "w27";
  case dwarf_w28_mips:
    return "w28";
  case dwarf_w29_mips:
    return "w29";
  case dwarf_w30_mips:
    return "w30";
  case dwarf_w31_mips:
    return "w31";
  case dwarf_mcsr_mips:
    return "mcsr";
  case dwarf_mir_mips:
    return "mir";
  case dwarf_config5_mips:
    return "config5";
  }
  return nullptr;
}

bool EmulateInstructionMIPS::GetRegisterInfo(RegisterKind reg_kind,
                                             uint32_t reg_num,
                                             RegisterInfo &reg_info) {
  if (reg_kind == eRegisterKindGeneric) {
    switch (reg_num) {
    case LLDB_REGNUM_GENERIC_PC:
      reg_kind = eRegisterKindDWARF;
      reg_num = dwarf_pc_mips;
      break;
    case LLDB_REGNUM_GENERIC_SP:
      reg_kind = eRegisterKindDWARF;
      reg_num = dwarf_sp_mips;
      break;
    case LLDB_REGNUM_GENERIC_FP:
      reg_kind = eRegisterKindDWARF;
      reg_num = dwarf_r30_mips;
      break;
    case LLDB_REGNUM_GENERIC_RA:
      reg_kind = eRegisterKindDWARF;
      reg_num = dwarf_ra_mips;
      break;
    case LLDB_REGNUM_GENERIC_FLAGS:
      reg_kind = eRegisterKindDWARF;
      reg_num = dwarf_sr_mips;
      break;
    default:
      return false;
    }
  }

  if (reg_kind == eRegisterKindDWARF) {
    ::memset(&reg_info, 0, sizeof(RegisterInfo));
    ::memset(reg_info.kinds, LLDB_INVALID_REGNUM, sizeof(reg_info.kinds));

    if (reg_num == dwarf_sr_mips || reg_num == dwarf_fcsr_mips ||
        reg_num == dwarf_fir_mips || reg_num == dwarf_mcsr_mips ||
        reg_num == dwarf_mir_mips || reg_num == dwarf_config5_mips) {
      reg_info.byte_size = 4;
      reg_info.format = eFormatHex;
      reg_info.encoding = eEncodingUint;
    } else if ((int)reg_num >= dwarf_zero_mips &&
               (int)reg_num <= dwarf_f31_mips) {
      reg_info.byte_size = 4;
      reg_info.format = eFormatHex;
      reg_info.encoding = eEncodingUint;
    } else if ((int)reg_num >= dwarf_w0_mips &&
               (int)reg_num <= dwarf_w31_mips) {
      reg_info.byte_size = 16;
      reg_info.format = eFormatVectorOfUInt8;
      reg_info.encoding = eEncodingVector;
    } else {
      return false;
    }

    reg_info.name = GetRegisterName(reg_num, false);
    reg_info.alt_name = GetRegisterName(reg_num, true);
    reg_info.kinds[eRegisterKindDWARF] = reg_num;

    switch (reg_num) {
    case dwarf_r30_mips:
      reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP;
      break;
    case dwarf_ra_mips:
      reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_RA;
      break;
    case dwarf_sp_mips:
      reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_SP;
      break;
    case dwarf_pc_mips:
      reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_PC;
      break;
    case dwarf_sr_mips:
      reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FLAGS;
      break;
    default:
      break;
    }
    return true;
  }
  return false;
}

EmulateInstructionMIPS::MipsOpcode *
EmulateInstructionMIPS::GetOpcodeForInstruction(const char *op_name) {
  static EmulateInstructionMIPS::MipsOpcode g_opcodes[] = {
      // Prologue/Epilogue instructions
      {"ADDiu", &EmulateInstructionMIPS::Emulate_ADDiu,
       "ADDIU rt, rs, immediate"},
      {"SW", &EmulateInstructionMIPS::Emulate_SW, "SW rt, offset(rs)"},
      {"LW", &EmulateInstructionMIPS::Emulate_LW, "LW rt, offset(base)"},
      {"SUBU", &EmulateInstructionMIPS::Emulate_SUBU_ADDU, "SUBU rd, rs, rt"},
      {"ADDU", &EmulateInstructionMIPS::Emulate_SUBU_ADDU, "ADDU rd, rs, rt"},
      {"LUI", &EmulateInstructionMIPS::Emulate_LUI, "LUI rt, immediate"},

      // MicroMIPS Prologue/Epilogue instructions
      {"ADDIUSP_MM", &EmulateInstructionMIPS::Emulate_ADDIUSP,
       "ADDIU immediate"},
      {"ADDIUS5_MM", &EmulateInstructionMIPS::Emulate_ADDIUS5,
       "ADDIUS5 rd,immediate"},
      {"SWSP_MM", &EmulateInstructionMIPS::Emulate_SWSP, "SWSP rt,offset(sp)"},
      {"SWM16_MM", &EmulateInstructionMIPS::Emulate_SWM16_32,
       "SWM16 reglist,offset(sp)"},
      {"SWM32_MM", &EmulateInstructionMIPS::Emulate_SWM16_32,
       "SWM32 reglist,offset(base)"},
      {"SWP_MM", &EmulateInstructionMIPS::Emulate_SWM16_32,
       "SWP rs1,offset(base)"},
      {"LWSP_MM", &EmulateInstructionMIPS::Emulate_LWSP, "LWSP rt,offset(sp)"},
      {"LWM16_MM", &EmulateInstructionMIPS::Emulate_LWM16_32,
       "LWM16 reglist,offset(sp)"},
      {"LWM32_MM", &EmulateInstructionMIPS::Emulate_LWM16_32,
       "LWM32 reglist,offset(base)"},
      {"LWP_MM", &EmulateInstructionMIPS::Emulate_LWM16_32,
       "LWP rd,offset(base)"},
      {"JRADDIUSP", &EmulateInstructionMIPS::Emulate_JRADDIUSP,
       "JRADDIUSP immediate"},

      // Load/Store  instructions
      /* Following list of emulated instructions are required by implementation
         of hardware watchpoint
         for MIPS in lldb. As we just need the address accessed by instructions,
         we have generalised
         all these instructions in 2 functions depending on their addressing
         modes */

      {"LB", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LB    rt, offset(base)"},
      {"LBE", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LBE   rt, offset(base)"},
      {"LBU", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LBU   rt, offset(base)"},
      {"LBUE", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LBUE  rt, offset(base)"},
      {"LDC1", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LDC1  ft, offset(base)"},
      {"LD", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LD    rt, offset(base)"},
      {"LDL", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LDL   rt, offset(base)"},
      {"LDR", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LDR   rt, offset(base)"},
      {"LLD", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LLD   rt, offset(base)"},
      {"LDC2", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LDC2  rt, offset(base)"},
      {"LDXC1", &EmulateInstructionMIPS::Emulate_LDST_Reg,
       "LDXC1 fd, index (base)"},
      {"LH", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LH    rt, offset(base)"},
      {"LHE", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LHE   rt, offset(base)"},
      {"LHU", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LHU   rt, offset(base)"},
      {"LHUE", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LHUE  rt, offset(base)"},
      {"LL", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LL    rt, offset(base)"},
      {"LLE", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LLE   rt, offset(base)"},
      {"LUXC1", &EmulateInstructionMIPS::Emulate_LDST_Reg,
       "LUXC1 fd, index (base)"},
      {"LW", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LW    rt, offset(base)"},
      {"LWC1", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LWC1  ft, offset(base)"},
      {"LWC2", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LWC2  rt, offset(base)"},
      {"LWE", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LWE   rt, offset(base)"},
      {"LWL", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LWL   rt, offset(base)"},
      {"LWLE", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LWLE  rt, offset(base)"},
      {"LWR", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LWR   rt, offset(base)"},
      {"LWRE", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LWRE  rt, offset(base)"},
      {"LWXC1", &EmulateInstructionMIPS::Emulate_LDST_Reg,
       "LWXC1 fd, index (base)"},
      {"LLX", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LLX   rt, offset(base)"},
      {"LLXE", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LLXE  rt, offset(base)"},
      {"LLDX", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LLDX  rt, offset(base)"},

      {"SB", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SB    rt, offset(base)"},
      {"SBE", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SBE   rt, offset(base)"},
      {"SC", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SC    rt, offset(base)"},
      {"SCE", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SCE   rt, offset(base)"},
      {"SCD", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SCD   rt, offset(base)"},
      {"SD", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SD    rt, offset(base)"},
      {"SDL", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SDL   rt, offset(base)"},
      {"SDR", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SDR   rt, offset(base)"},
      {"SDC1", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SDC1  ft, offset(base)"},
      {"SDC2", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SDC2  rt, offset(base)"},
      {"SDXC1", &EmulateInstructionMIPS::Emulate_LDST_Reg,
       "SDXC1 fs, index(base)"},
      {"SH", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SH    rt, offset(base)"},
      {"SHE", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SHE   rt, offset(base)"},
      {"SUXC1", &EmulateInstructionMIPS::Emulate_LDST_Reg,
       "SUXC1 fs, index (base)"},
      {"SWC1", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SWC1  ft, offset(base)"},
      {"SWC2", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SWC2  rt, offset(base)"},
      {"SWE", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SWE   rt, offset(base)"},
      {"SWL", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SWL   rt, offset(base)"},
      {"SWLE", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SWLE  rt, offset(base)"},
      {"SWR", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SWR   rt, offset(base)"},
      {"SWRE", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SWRE  rt, offset(base)"},
      {"SWXC1", &EmulateInstructionMIPS::Emulate_LDST_Reg,
       "SWXC1 fs, index (base)"},
      {"SCX", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SCX   rt, offset(base)"},
      {"SCXE", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SCXE  rt, offset(base)"},
      {"SCDX", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SCDX  rt, offset(base)"},

      // MicroMIPS Load/Store instructions
      {"LBU16_MM", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LBU16 rt, decoded_offset(base)"},
      {"LHU16_MM", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LHU16 rt, left_shifted_offset(base)"},
      {"LW16_MM", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LW16  rt, left_shifted_offset(base)"},
      {"LWGP_MM", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "LWGP  rt, left_shifted_offset(gp)"},
      {"SH16_MM", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SH16  rt, left_shifted_offset(base)"},
      {"SW16_MM", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SW16  rt, left_shifted_offset(base)"},
      {"SW_MM", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SWSP  rt, left_shifted_offset(base)"},
      {"SB16_MM", &EmulateInstructionMIPS::Emulate_LDST_Imm,
       "SB16  rt, offset(base)"},

      // Branch instructions
      {"BEQ", &EmulateInstructionMIPS::Emulate_BXX_3ops, "BEQ rs,rt,offset"},
      {"BNE", &EmulateInstructionMIPS::Emulate_BXX_3ops, "BNE rs,rt,offset"},
      {"BEQL", &EmulateInstructionMIPS::Emulate_BXX_3ops, "BEQL rs,rt,offset"},
      {"BNEL", &EmulateInstructionMIPS::Emulate_BXX_3ops, "BNEL rs,rt,offset"},
      {"BGEZALL", &EmulateInstructionMIPS::Emulate_Bcond_Link,
       "BGEZALL rt,offset"},
      {"BAL", &EmulateInstructionMIPS::Emulate_BAL, "BAL offset"},
      {"BGEZAL", &EmulateInstructionMIPS::Emulate_Bcond_Link,
       "BGEZAL rs,offset"},
      {"BALC", &EmulateInstructionMIPS::Emulate_BALC, "BALC offset"},
      {"BC", &EmulateInstructionMIPS::Emulate_BC, "BC offset"},
      {"BGEZ", &EmulateInstructionMIPS::Emulate_BXX_2ops, "BGEZ rs,offset"},
      {"BLEZALC", &EmulateInstructionMIPS::Emulate_Bcond_Link_C,
       "BLEZALC rs,offset"},
      {"BGEZALC", &EmulateInstructionMIPS::Emulate_Bcond_Link_C,
       "BGEZALC rs,offset"},
      {"BLTZALC", &EmulateInstructionMIPS::Emulate_Bcond_Link_C,
       "BLTZALC rs,offset"},
      {"BGTZALC", &EmulateInstructionMIPS::Emulate_Bcond_Link_C,
       "BGTZALC rs,offset"},
      {"BEQZALC", &EmulateInstructionMIPS::Emulate_Bcond_Link_C,
       "BEQZALC rs,offset"},
      {"BNEZALC", &EmulateInstructionMIPS::Emulate_Bcond_Link_C,
       "BNEZALC rs,offset"},
      {"BEQC", &EmulateInstructionMIPS::Emulate_BXX_3ops_C,
       "BEQC rs,rt,offset"},
      {"BNEC", &EmulateInstructionMIPS::Emulate_BXX_3ops_C,
       "BNEC rs,rt,offset"},
      {"BLTC", &EmulateInstructionMIPS::Emulate_BXX_3ops_C,
       "BLTC rs,rt,offset"},
      {"BGEC", &EmulateInstructionMIPS::Emulate_BXX_3ops_C,
       "BGEC rs,rt,offset"},
      {"BLTUC", &EmulateInstructionMIPS::Emulate_BXX_3ops_C,
       "BLTUC rs,rt,offset"},
      {"BGEUC", &EmulateInstructionMIPS::Emulate_BXX_3ops_C,
       "BGEUC rs,rt,offset"},
      {"BLTZC", &EmulateInstructionMIPS::Emulate_BXX_2ops_C, "BLTZC rt,offset"},
      {"BLEZC", &EmulateInstructionMIPS::Emulate_BXX_2ops_C, "BLEZC rt,offset"},
      {"BGEZC", &EmulateInstructionMIPS::Emulate_BXX_2ops_C, "BGEZC rt,offset"},
      {"BGTZC", &EmulateInstructionMIPS::Emulate_BXX_2ops_C, "BGTZC rt,offset"},
      {"BEQZC", &EmulateInstructionMIPS::Emulate_BXX_2ops_C, "BEQZC rt,offset"},
      {"BNEZC", &EmulateInstructionMIPS::Emulate_BXX_2ops_C, "BNEZC rt,offset"},
      {"BGEZL", &EmulateInstructionMIPS::Emulate_BXX_2ops, "BGEZL rt,offset"},
      {"BGTZ", &EmulateInstructionMIPS::Emulate_BXX_2ops, "BGTZ rt,offset"},
      {"BGTZL", &EmulateInstructionMIPS::Emulate_BXX_2ops, "BGTZL rt,offset"},
      {"BLEZ", &EmulateInstructionMIPS::Emulate_BXX_2ops, "BLEZ rt,offset"},
      {"BLEZL", &EmulateInstructionMIPS::Emulate_BXX_2ops, "BLEZL rt,offset"},
      {"BLTZ", &EmulateInstructionMIPS::Emulate_BXX_2ops, "BLTZ rt,offset"},
      {"BLTZAL", &EmulateInstructionMIPS::Emulate_Bcond_Link,
       "BLTZAL rt,offset"},
      {"BLTZALL", &EmulateInstructionMIPS::Emulate_Bcond_Link,
       "BLTZALL rt,offset"},
      {"BLTZL", &EmulateInstructionMIPS::Emulate_BXX_2ops, "BLTZL rt,offset"},
      {"BOVC", &EmulateInstructionMIPS::Emulate_BXX_3ops_C,
       "BOVC rs,rt,offset"},
      {"BNVC", &EmulateInstructionMIPS::Emulate_BXX_3ops_C,
       "BNVC rs,rt,offset"},
      {"J", &EmulateInstructionMIPS::Emulate_J, "J target"},
      {"JAL", &EmulateInstructionMIPS::Emulate_JAL, "JAL target"},
      {"JALX", &EmulateInstructionMIPS::Emulate_JAL, "JALX target"},
      {"JALR", &EmulateInstructionMIPS::Emulate_JALR, "JALR target"},
      {"JALR_HB", &EmulateInstructionMIPS::Emulate_JALR, "JALR.HB target"},
      {"JIALC", &EmulateInstructionMIPS::Emulate_JIALC, "JIALC rt,offset"},
      {"JIC", &EmulateInstructionMIPS::Emulate_JIC, "JIC rt,offset"},
      {"JR", &EmulateInstructionMIPS::Emulate_JR, "JR target"},
      {"JR_HB", &EmulateInstructionMIPS::Emulate_JR, "JR.HB target"},
      {"BC1F", &EmulateInstructionMIPS::Emulate_FP_branch, "BC1F cc, offset"},
      {"BC1T", &EmulateInstructionMIPS::Emulate_FP_branch, "BC1T cc, offset"},
      {"BC1FL", &EmulateInstructionMIPS::Emulate_FP_branch, "BC1FL cc, offset"},
      {"BC1TL", &EmulateInstructionMIPS::Emulate_FP_branch, "BC1TL cc, offset"},
      {"BC1EQZ", &EmulateInstructionMIPS::Emulate_BC1EQZ, "BC1EQZ ft, offset"},
      {"BC1NEZ", &EmulateInstructionMIPS::Emulate_BC1NEZ, "BC1NEZ ft, offset"},
      {"BC1ANY2F", &EmulateInstructionMIPS::Emulate_3D_branch,
       "BC1ANY2F cc, offset"},
      {"BC1ANY2T", &EmulateInstructionMIPS::Emulate_3D_branch,
       "BC1ANY2T cc, offset"},
      {"BC1ANY4F", &EmulateInstructionMIPS::Emulate_3D_branch,
       "BC1ANY4F cc, offset"},
      {"BC1ANY4T", &EmulateInstructionMIPS::Emulate_3D_branch,
       "BC1ANY4T cc, offset"},
      {"BNZ_B", &EmulateInstructionMIPS::Emulate_BNZB, "BNZ.b wt,s16"},
      {"BNZ_H", &EmulateInstructionMIPS::Emulate_BNZH, "BNZ.h wt,s16"},
      {"BNZ_W", &EmulateInstructionMIPS::Emulate_BNZW, "BNZ.w wt,s16"},
      {"BNZ_D", &EmulateInstructionMIPS::Emulate_BNZD, "BNZ.d wt,s16"},
      {"BZ_B", &EmulateInstructionMIPS::Emulate_BZB, "BZ.b wt,s16"},
      {"BZ_H", &EmulateInstructionMIPS::Emulate_BZH, "BZ.h wt,s16"},
      {"BZ_W", &EmulateInstructionMIPS::Emulate_BZW, "BZ.w wt,s16"},
      {"BZ_D", &EmulateInstructionMIPS::Emulate_BZD, "BZ.d wt,s16"},
      {"BNZ_V", &EmulateInstructionMIPS::Emulate_BNZV, "BNZ.V wt,s16"},
      {"BZ_V", &EmulateInstructionMIPS::Emulate_BZV, "BZ.V wt,s16"},

      // MicroMIPS Branch instructions
      {"B16_MM", &EmulateInstructionMIPS::Emulate_B16_MM, "B16 offset"},
      {"BEQZ16_MM", &EmulateInstructionMIPS::Emulate_Branch_MM,
       "BEQZ16 rs, offset"},
      {"BNEZ16_MM", &EmulateInstructionMIPS::Emulate_Branch_MM,
       "BNEZ16 rs, offset"},
      {"BEQZC_MM", &EmulateInstructionMIPS::Emulate_Branch_MM,
       "BEQZC rs, offset"},
      {"BNEZC_MM", &EmulateInstructionMIPS::Emulate_Branch_MM,
       "BNEZC rs, offset"},
      {"BGEZALS_MM", &EmulateInstructionMIPS::Emulate_Branch_MM,
       "BGEZALS rs, offset"},
      {"BLTZALS_MM", &EmulateInstructionMIPS::Emulate_Branch_MM,
       "BLTZALS rs, offset"},
      {"JALR16_MM", &EmulateInstructionMIPS::Emulate_JALRx16_MM, "JALR16 rs"},
      {"JALRS16_MM", &EmulateInstructionMIPS::Emulate_JALRx16_MM, "JALRS16 rs"},
      {"JR16_MM", &EmulateInstructionMIPS::Emulate_JR, "JR16 rs rs"},
      {"JRC16_MM", &EmulateInstructionMIPS::Emulate_JR, "JRC16 rs rs"},
      {"JALS_MM", &EmulateInstructionMIPS::Emulate_JALx, "JALS target"},
      {"JALX_MM", &EmulateInstructionMIPS::Emulate_JALx, "JALX target"},
      {"JALRS_MM", &EmulateInstructionMIPS::Emulate_JALRS, "JALRS rt, rs"},
  };

  static const size_t k_num_mips_opcodes = llvm::array_lengthof(g_opcodes);

  for (size_t i = 0; i < k_num_mips_opcodes; ++i) {
    if (!strcasecmp(g_opcodes[i].op_name, op_name))
      return &g_opcodes[i];
  }

  return nullptr;
}

uint32_t
EmulateInstructionMIPS::GetSizeOfInstruction(lldb_private::DataExtractor &data,
                                             uint64_t inst_addr) {
  uint64_t next_inst_size = 0;
  llvm::MCInst mc_insn;
  llvm::MCDisassembler::DecodeStatus decode_status;
  llvm::ArrayRef<uint8_t> raw_insn(data.GetDataStart(), data.GetByteSize());

  if (m_use_alt_disaasm)
    decode_status =
        m_alt_disasm->getInstruction(mc_insn, next_inst_size, raw_insn,
                                     inst_addr, llvm::nulls(), llvm::nulls());
  else
    decode_status =
        m_disasm->getInstruction(mc_insn, next_inst_size, raw_insn, inst_addr,
                                 llvm::nulls(), llvm::nulls());

  if (decode_status != llvm::MCDisassembler::Success)
    return false;

  return m_insn_info->get(mc_insn.getOpcode()).getSize();
}

bool EmulateInstructionMIPS::SetInstruction(const Opcode &insn_opcode,
                                            const Address &inst_addr,
                                            Target *target) {
  m_use_alt_disaasm = false;

  if (EmulateInstruction::SetInstruction(insn_opcode, inst_addr, target)) {
    if (inst_addr.GetAddressClass() == AddressClass::eCodeAlternateISA) {
      Status error;
      lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;

      /*
       * The address belongs to microMIPS function. To find the size of
       * next instruction use microMIPS disassembler.
      */
      m_use_alt_disaasm = true;

      uint32_t current_inst_size = insn_opcode.GetByteSize();
      uint8_t buf[sizeof(uint32_t)];
      uint64_t next_inst_addr = (m_addr & (~1ull)) + current_inst_size;
      Address next_addr(next_inst_addr);

      const size_t bytes_read =
          target->ReadMemory(next_addr, /* Address of next instruction */
                             true,      /* prefer_file_cache */
                             buf, sizeof(uint32_t), error, &load_addr);

      if (bytes_read == 0)
        return true;

      DataExtractor data(buf, sizeof(uint32_t), GetByteOrder(),
                         GetAddressByteSize());
      m_next_inst_size = GetSizeOfInstruction(data, next_inst_addr);
      return true;
    } else {
      /*
       * If the address class is not AddressClass::eCodeAlternateISA then
       * the function is not microMIPS. In this case instruction size is
       * always 4 bytes.
      */
      m_next_inst_size = 4;
      return true;
    }
  }
  return false;
}

bool EmulateInstructionMIPS::ReadInstruction() {
  bool success = false;
  m_addr = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC,
                                LLDB_INVALID_ADDRESS, &success);
  if (success) {
    Context read_inst_context;
    read_inst_context.type = eContextReadOpcode;
    read_inst_context.SetNoArgs();
    m_opcode.SetOpcode32(
        ReadMemoryUnsigned(read_inst_context, m_addr, 4, 0, &success),
        GetByteOrder());
  }
  if (!success)
    m_addr = LLDB_INVALID_ADDRESS;
  return success;
}

bool EmulateInstructionMIPS::EvaluateInstruction(uint32_t evaluate_options) {
  bool success = false;
  llvm::MCInst mc_insn;
  uint64_t insn_size;
  DataExtractor data;

  /* Keep the complexity of the decode logic with the llvm::MCDisassembler
   * class. */
  if (m_opcode.GetData(data)) {
    llvm::MCDisassembler::DecodeStatus decode_status;
    llvm::ArrayRef<uint8_t> raw_insn(data.GetDataStart(), data.GetByteSize());
    if (m_use_alt_disaasm)
      decode_status = m_alt_disasm->getInstruction(
          mc_insn, insn_size, raw_insn, m_addr, llvm::nulls(), llvm::nulls());
    else
      decode_status = m_disasm->getInstruction(
          mc_insn, insn_size, raw_insn, m_addr, llvm::nulls(), llvm::nulls());

    if (decode_status != llvm::MCDisassembler::Success)
      return false;
  }

  /*
   * mc_insn.getOpcode() returns decoded opcode. However to make use
   * of llvm::Mips::<insn> we would need "MipsGenInstrInfo.inc".
  */
  const char *op_name = m_insn_info->getName(mc_insn.getOpcode()).data();

  if (op_name == nullptr)
    return false;

  /*
   * Decoding has been done already. Just get the call-back function
   * and emulate the instruction.
  */
  MipsOpcode *opcode_data = GetOpcodeForInstruction(op_name);

  if (opcode_data == nullptr)
    return false;

  uint64_t old_pc = 0, new_pc = 0;
  const bool auto_advance_pc =
      evaluate_options & eEmulateInstructionOptionAutoAdvancePC;

  if (auto_advance_pc) {
    old_pc =
        ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
    if (!success)
      return false;
  }

  /* emulate instruction */
  success = (this->*opcode_data->callback)(mc_insn);
  if (!success)
    return false;

  if (auto_advance_pc) {
    new_pc =
        ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
    if (!success)
      return false;

    /* If we haven't changed the PC, change it here */
    if (old_pc == new_pc) {
      new_pc += 4;
      Context context;
      if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                                 new_pc))
        return false;
    }
  }

  return true;
}

bool EmulateInstructionMIPS::CreateFunctionEntryUnwind(
    UnwindPlan &unwind_plan) {
  unwind_plan.Clear();
  unwind_plan.SetRegisterKind(eRegisterKindDWARF);

  UnwindPlan::RowSP row(new UnwindPlan::Row);
  const bool can_replace = false;

  // Our previous Call Frame Address is the stack pointer
  row->GetCFAValue().SetIsRegisterPlusOffset(dwarf_sp_mips, 0);

  // Our previous PC is in the RA
  row->SetRegisterLocationToRegister(dwarf_pc_mips, dwarf_ra_mips, can_replace);

  unwind_plan.AppendRow(row);

  // All other registers are the same.
  unwind_plan.SetSourceName("EmulateInstructionMIPS");
  unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
  unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolYes);
  unwind_plan.SetReturnAddressRegister(dwarf_ra_mips);

  return true;
}

bool EmulateInstructionMIPS::nonvolatile_reg_p(uint32_t regnum) {
  switch (regnum) {
  case dwarf_r16_mips:
  case dwarf_r17_mips:
  case dwarf_r18_mips:
  case dwarf_r19_mips:
  case dwarf_r20_mips:
  case dwarf_r21_mips:
  case dwarf_r22_mips:
  case dwarf_r23_mips:
  case dwarf_gp_mips:
  case dwarf_sp_mips:
  case dwarf_r30_mips:
  case dwarf_ra_mips:
    return true;
  default:
    return false;
  }
  return false;
}

bool EmulateInstructionMIPS::Emulate_ADDiu(llvm::MCInst &insn) {
  // ADDIU rt, rs, immediate
  // GPR[rt] <- GPR[rs] + sign_extend(immediate)

  uint8_t dst, src;
  bool success = false;
  const uint32_t imm16 = insn.getOperand(2).getImm();
  int64_t imm = SignedBits(imm16, 15, 0);

  dst = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  src = m_reg_info->getEncodingValue(insn.getOperand(1).getReg());

  // If immediate value is greater then 2^16 - 1 then clang generate LUI,
  // ADDIU, SUBU instructions in prolog. Example lui    $1, 0x2 addiu $1, $1,
  // -0x5920 subu  $sp, $sp, $1 In this case, ADDIU dst and src will be same
  // and not equal to sp
  if (dst == src) {
    Context context;

    /* read <src> register */
    const int64_t src_opd_val = ReadRegisterUnsigned(
        eRegisterKindDWARF, dwarf_zero_mips + src, 0, &success);
    if (!success)
      return false;

    /* Check if this is daddiu sp, sp, imm16 */
    if (dst == dwarf_sp_mips) {
      uint64_t result = src_opd_val + imm;
      RegisterInfo reg_info_sp;

      if (GetRegisterInfo(eRegisterKindDWARF, dwarf_sp_mips, reg_info_sp))
        context.SetRegisterPlusOffset(reg_info_sp, imm);

      /* We are allocating bytes on stack */
      context.type = eContextAdjustStackPointer;

      WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_sp_mips, result);
      return true;
    }

    imm += src_opd_val;
    context.SetImmediateSigned(imm);
    context.type = eContextImmediate;

    if (!WriteRegisterUnsigned(context, eRegisterKindDWARF,
                               dwarf_zero_mips + dst, imm))
      return false;
  }

  return true;
}

bool EmulateInstructionMIPS::Emulate_SW(llvm::MCInst &insn) {
  bool success = false;
  uint32_t imm16 = insn.getOperand(2).getImm();
  uint32_t imm = SignedBits(imm16, 15, 0);
  uint32_t src, base;
  int32_t address;
  Context bad_vaddr_context;

  RegisterInfo reg_info_base;

  src = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  base = m_reg_info->getEncodingValue(insn.getOperand(1).getReg());

  if (!GetRegisterInfo(eRegisterKindDWARF, dwarf_zero_mips + base,
                       reg_info_base))
    return false;

  /* read base register */
  address = (int32_t)ReadRegisterUnsigned(eRegisterKindDWARF,
                                          dwarf_zero_mips + base, 0, &success);
  if (!success)
    return false;

  /* destination address */
  address = address + imm;

  /* Set the bad_vaddr register with base address used in the instruction */
  bad_vaddr_context.type = eContextInvalid;
  WriteRegisterUnsigned(bad_vaddr_context, eRegisterKindDWARF, dwarf_bad_mips,
                        address);

  /* We look for sp based non-volatile register stores */
  if (nonvolatile_reg_p(src)) {

    RegisterInfo reg_info_src;

    if (!GetRegisterInfo(eRegisterKindDWARF, dwarf_zero_mips + src,
                         reg_info_src))
      return false;

    Context context;
    RegisterValue data_src;
    context.type = eContextPushRegisterOnStack;
    context.SetRegisterToRegisterPlusOffset(reg_info_src, reg_info_base, 0);

    uint8_t buffer[RegisterValue::kMaxRegisterByteSize];
    Status error;

    if (!ReadRegister(&reg_info_base, data_src))
      return false;

    if (data_src.GetAsMemoryData(&reg_info_src, buffer, reg_info_src.byte_size,
                                 eByteOrderLittle, error) == 0)
      return false;

    if (!WriteMemory(context, address, buffer, reg_info_src.byte_size))
      return false;

    return true;
  }

  return false;
}

bool EmulateInstructionMIPS::Emulate_LW(llvm::MCInst &insn) {
  bool success = false;
  uint32_t src, base;
  int32_t imm, address;
  Context bad_vaddr_context;

  src = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  base = m_reg_info->getEncodingValue(insn.getOperand(1).getReg());
  imm = insn.getOperand(2).getImm();

  RegisterInfo reg_info_base;
  if (!GetRegisterInfo(eRegisterKindDWARF, dwarf_zero_mips + base,
                       reg_info_base))
    return false;

  /* read base register */
  address = (int32_t)ReadRegisterUnsigned(eRegisterKindDWARF,
                                          dwarf_zero_mips + base, 0, &success);
  if (!success)
    return false;

  /* destination address */
  address = address + imm;

  /* Set the bad_vaddr register with base address used in the instruction */
  bad_vaddr_context.type = eContextInvalid;
  WriteRegisterUnsigned(bad_vaddr_context, eRegisterKindDWARF, dwarf_bad_mips,
                        address);

  if (nonvolatile_reg_p(src)) {
    RegisterValue data_src;
    RegisterInfo reg_info_src;

    if (!GetRegisterInfo(eRegisterKindDWARF, dwarf_zero_mips + src,
                         reg_info_src))
      return false;

    Context context;
    context.type = eContextPopRegisterOffStack;
    context.SetAddress(address);

    return WriteRegister(context, &reg_info_src, data_src);
  }

  return false;
}

bool EmulateInstructionMIPS::Emulate_SUBU_ADDU(llvm::MCInst &insn) {
  // SUBU sp, <src>, <rt>
  // ADDU sp, <src>, <rt>
  // ADDU dst, sp, <rt>

  bool success = false;
  uint64_t result;
  uint8_t src, dst, rt;
  const char *op_name = m_insn_info->getName(insn.getOpcode()).data();

  dst = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  src = m_reg_info->getEncodingValue(insn.getOperand(1).getReg());

  /* Check if sp is destination register */
  if (dst == dwarf_sp_mips) {
    rt = m_reg_info->getEncodingValue(insn.getOperand(2).getReg());

    /* read <src> register */
    uint64_t src_opd_val = ReadRegisterUnsigned(
        eRegisterKindDWARF, dwarf_zero_mips + src, 0, &success);
    if (!success)
      return false;

    /* read <rt > register */
    uint64_t rt_opd_val = ReadRegisterUnsigned(
        eRegisterKindDWARF, dwarf_zero_mips + rt, 0, &success);
    if (!success)
      return false;

    if (!strcasecmp(op_name, "SUBU"))
      result = src_opd_val - rt_opd_val;
    else
      result = src_opd_val + rt_opd_val;

    Context context;
    RegisterInfo reg_info_sp;
    if (GetRegisterInfo(eRegisterKindDWARF, dwarf_sp_mips, reg_info_sp))
      context.SetRegisterPlusOffset(reg_info_sp, rt_opd_val);

    /* We are allocating bytes on stack */
    context.type = eContextAdjustStackPointer;

    WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_sp_mips, result);

    return true;
  } else if (src == dwarf_sp_mips) {
    rt = m_reg_info->getEncodingValue(insn.getOperand(2).getReg());

    /* read <src> register */
    uint64_t src_opd_val = ReadRegisterUnsigned(
        eRegisterKindDWARF, dwarf_zero_mips + src, 0, &success);
    if (!success)
      return false;

    /* read <rt> register */
    uint64_t rt_opd_val = ReadRegisterUnsigned(
        eRegisterKindDWARF, dwarf_zero_mips + rt, 0, &success);
    if (!success)
      return false;

    Context context;

    if (!strcasecmp(op_name, "SUBU"))
      result = src_opd_val - rt_opd_val;
    else
      result = src_opd_val + rt_opd_val;

    context.SetImmediateSigned(result);
    context.type = eContextImmediate;

    if (!WriteRegisterUnsigned(context, eRegisterKindDWARF,
                               dwarf_zero_mips + dst, result))
      return false;
  }

  return true;
}

bool EmulateInstructionMIPS::Emulate_LUI(llvm::MCInst &insn) {
  // LUI rt, immediate
  // GPR[rt] <- sign_extend(immediate << 16)

  const uint32_t imm32 = insn.getOperand(1).getImm() << 16;
  int64_t imm = SignedBits(imm32, 31, 0);
  uint8_t rt;
  Context context;

  rt = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  context.SetImmediateSigned(imm);
  context.type = eContextImmediate;

  return WriteRegisterUnsigned(context, eRegisterKindDWARF,
                               dwarf_zero_mips + rt, imm);
}

bool EmulateInstructionMIPS::Emulate_ADDIUSP(llvm::MCInst &insn) {
  bool success = false;
  const uint32_t imm9 = insn.getOperand(0).getImm();
  uint64_t result;

  // This instruction operates implicitly on stack pointer, so read <sp>
  // register.
  uint64_t src_opd_val =
      ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_sp_mips, 0, &success);
  if (!success)
    return false;

  result = src_opd_val + imm9;

  Context context;
  RegisterInfo reg_info_sp;
  if (GetRegisterInfo(eRegisterKindDWARF, dwarf_sp_mips, reg_info_sp))
    context.SetRegisterPlusOffset(reg_info_sp, imm9);

  // We are adjusting the stack.
  context.type = eContextAdjustStackPointer;

  WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_sp_mips, result);
  return true;
}

bool EmulateInstructionMIPS::Emulate_ADDIUS5(llvm::MCInst &insn) {
  bool success = false;
  uint32_t base;
  const uint32_t imm4 = insn.getOperand(2).getImm();
  uint64_t result;

  // The source and destination register is same for this instruction.
  base = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());

  // We are looking for stack adjustment only
  if (base == dwarf_sp_mips) {
    // Read stack pointer register
    uint64_t src_opd_val = ReadRegisterUnsigned(
        eRegisterKindDWARF, dwarf_zero_mips + base, 0, &success);
    if (!success)
      return false;

    result = src_opd_val + imm4;

    Context context;
    RegisterInfo reg_info_sp;
    if (GetRegisterInfo(eRegisterKindDWARF, dwarf_sp_mips, reg_info_sp))
      context.SetRegisterPlusOffset(reg_info_sp, imm4);

    // We are adjusting the stack.
    context.type = eContextAdjustStackPointer;

    WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_sp_mips, result);
  }

  return true;
}

bool EmulateInstructionMIPS::Emulate_SWSP(llvm::MCInst &insn) {
  bool success = false;
  uint32_t imm5 = insn.getOperand(2).getImm();
  uint32_t src, base;
  Context bad_vaddr_context;
  uint32_t address;

  src = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  base = m_reg_info->getEncodingValue(insn.getOperand(1).getReg());

  RegisterInfo reg_info_base;

  if (!GetRegisterInfo(eRegisterKindDWARF, dwarf_zero_mips + base,
                       reg_info_base))
    return false;

  // read base register
  address = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_zero_mips + base, 0,
                                 &success);
  if (!success)
    return false;

  // destination address
  address = address + imm5;

  // We use bad_vaddr_context to store base address which is used by H/W
  // watchpoint Set the bad_vaddr register with base address used in the
  // instruction
  bad_vaddr_context.type = eContextInvalid;
  WriteRegisterUnsigned(bad_vaddr_context, eRegisterKindDWARF, dwarf_bad_mips,
                        address);

  // We look for sp based non-volatile register stores.
  if (base == dwarf_sp_mips && nonvolatile_reg_p(src)) {
    RegisterInfo reg_info_src = {};
    Context context;
    RegisterValue data_src;
    context.type = eContextPushRegisterOnStack;
    context.SetRegisterToRegisterPlusOffset(reg_info_src, reg_info_base, 0);

    uint8_t buffer[RegisterValue::kMaxRegisterByteSize];
    Status error;

    if (!ReadRegister(&reg_info_base, data_src))
      return false;

    if (data_src.GetAsMemoryData(&reg_info_src, buffer, reg_info_src.byte_size,
                                 eByteOrderLittle, error) == 0)
      return false;

    if (!WriteMemory(context, address, buffer, reg_info_src.byte_size))
      return false;

    return true;
  }

  return false;
}

/* Emulate SWM16,SWM32 and SWP instruction.

   SWM16 always has stack pointer as a base register (but it is still available
   in MCInst as an operand).
   SWM32 and SWP can have base register other than stack pointer.
*/
bool EmulateInstructionMIPS::Emulate_SWM16_32(llvm::MCInst &insn) {
  bool success = false;
  uint32_t src, base;
  uint32_t num_operands = insn.getNumOperands(); // No of operands vary based on
                                                 // no of regs to store.

  // Base register is second last operand of the instruction.
  base =
      m_reg_info->getEncodingValue(insn.getOperand(num_operands - 2).getReg());

  // We are looking for sp based stores so if base is not a stack pointer then
  // don't proceed.
  if (base != dwarf_sp_mips)
    return false;

  // offset is always the last operand.
  uint32_t offset = insn.getOperand(num_operands - 1).getImm();

  RegisterInfo reg_info_base;
  RegisterInfo reg_info_src;

  if (!GetRegisterInfo(eRegisterKindDWARF, dwarf_zero_mips + base,
                       reg_info_base))
    return false;

  // read SP
  uint32_t base_address = ReadRegisterUnsigned(
      eRegisterKindDWARF, dwarf_zero_mips + base, 0, &success);
  if (!success)
    return false;

  // Resulting base addrss
  base_address = base_address + offset;

  // Total no of registers to be stored are num_operands-2.
  for (uint32_t i = 0; i < num_operands - 2; i++) {
    // Get the register number to be stored.
    src = m_reg_info->getEncodingValue(insn.getOperand(i).getReg());

    /*
        Record only non-volatile stores.
        This check is required for SWP instruction because source operand could
       be any register.
        SWM16 and SWM32 instruction always has saved registers as source
       operands.
    */
    if (!nonvolatile_reg_p(src))
      return false;

    if (!GetRegisterInfo(eRegisterKindDWARF, dwarf_zero_mips + src,
                         reg_info_src))
      return false;

    Context context;
    RegisterValue data_src;
    context.type = eContextPushRegisterOnStack;
    context.SetRegisterToRegisterPlusOffset(reg_info_src, reg_info_base, 0);

    uint8_t buffer[RegisterValue::kMaxRegisterByteSize];
    Status error;

    if (!ReadRegister(&reg_info_base, data_src))
      return false;

    if (data_src.GetAsMemoryData(&reg_info_src, buffer, reg_info_src.byte_size,
                                 eByteOrderLittle, error) == 0)
      return false;

    if (!WriteMemory(context, base_address, buffer, reg_info_src.byte_size))
      return false;

    // Stack address for next register
    base_address = base_address + reg_info_src.byte_size;
  }
  return true;
}

bool EmulateInstructionMIPS::Emulate_LWSP(llvm::MCInst &insn) {
  bool success = false;
  uint32_t src = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  uint32_t base = m_reg_info->getEncodingValue(insn.getOperand(1).getReg());
  uint32_t imm5 = insn.getOperand(2).getImm();
  Context bad_vaddr_context;

  RegisterInfo reg_info_base;
  if (!GetRegisterInfo(eRegisterKindDWARF, dwarf_zero_mips + base,
                       reg_info_base))
    return false;

  // read base register
  uint32_t base_address = ReadRegisterUnsigned(
      eRegisterKindDWARF, dwarf_zero_mips + base, 0, &success);
  if (!success)
    return false;

  base_address = base_address + imm5;

  // We use bad_vaddr_context to store base address which is used by H/W
  // watchpoint Set the bad_vaddr register with base address used in the
  // instruction
  bad_vaddr_context.type = eContextInvalid;
  WriteRegisterUnsigned(bad_vaddr_context, eRegisterKindDWARF, dwarf_bad_mips,
                        base_address);

  if (base == dwarf_sp_mips && nonvolatile_reg_p(src)) {
    RegisterValue data_src;
    RegisterInfo reg_info_src;

    if (!GetRegisterInfo(eRegisterKindDWARF, dwarf_zero_mips + src,
                         reg_info_src))
      return false;

    Context context;
    context.type = eContextPopRegisterOffStack;
    context.SetAddress(base_address);

    return WriteRegister(context, &reg_info_src, data_src);
  }

  return false;
}

/* Emulate LWM16, LWM32 and LWP instructions.

   LWM16 always has stack pointer as a base register (but it is still available
   in MCInst as an operand).
   LWM32 and LWP can have base register other than stack pointer.
*/
bool EmulateInstructionMIPS::Emulate_LWM16_32(llvm::MCInst &insn) {
  bool success = false;
  uint32_t dst, base;
  uint32_t num_operands = insn.getNumOperands(); // No of operands vary based on
                                                 // no of regs to store.
  uint32_t imm = insn.getOperand(num_operands - 1)
                     .getImm(); // imm is the last operand in the instruction.

  // Base register is second last operand of the instruction.
  base =
      m_reg_info->getEncodingValue(insn.getOperand(num_operands - 2).getReg());

  // We are looking for sp based loads so if base is not a stack pointer then
  // don't proceed.
  if (base != dwarf_sp_mips)
    return false;

  uint32_t base_address = ReadRegisterUnsigned(
      eRegisterKindDWARF, dwarf_zero_mips + base, 0, &success);
  if (!success)
    return false;

  base_address = base_address + imm;

  RegisterValue data_dst;
  RegisterInfo reg_info_dst;

  // Total no of registers to be re-stored are num_operands-2.
  for (uint32_t i = 0; i < num_operands - 2; i++) {
    // Get the register number to be re-stored.
    dst = m_reg_info->getEncodingValue(insn.getOperand(i).getReg());

    /*
        Record only non-volatile loads.
        This check is required for LWP instruction because destination operand
       could be any register.
        LWM16 and LWM32 instruction always has saved registers as destination
       operands.
    */
    if (!nonvolatile_reg_p(dst))
      return false;

    if (!GetRegisterInfo(eRegisterKindDWARF, dwarf_zero_mips + dst,
                         reg_info_dst))
      return false;

    Context context;
    context.type = eContextPopRegisterOffStack;
    context.SetAddress(base_address + (i * 4));

    if (!WriteRegister(context, &reg_info_dst, data_dst))
      return false;
  }

  return true;
}

bool EmulateInstructionMIPS::Emulate_JRADDIUSP(llvm::MCInst &insn) {
  bool success = false;
  int32_t imm5 = insn.getOperand(0).getImm();

  /* JRADDIUSP immediate
  *       PC <- RA
  *       SP <- SP + zero_extend(Immediate << 2)
  */

  // This instruction operates implicitly on stack pointer, so read <sp>
  // register.
  int32_t src_opd_val =
      ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_sp_mips, 0, &success);
  if (!success)
    return false;

  int32_t ra_val =
      ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_ra_mips, 0, &success);
  if (!success)
    return false;

  int32_t result = src_opd_val + imm5;

  Context context;

  // Update the PC
  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                             ra_val))
    return false;

  RegisterInfo reg_info_sp;
  if (GetRegisterInfo(eRegisterKindDWARF, dwarf_sp_mips, reg_info_sp))
    context.SetRegisterPlusOffset(reg_info_sp, imm5);

  // We are adjusting stack
  context.type = eContextAdjustStackPointer;

  // update SP
  return WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_sp_mips,
                               result);
}

static int IsAdd64bitOverflow(int32_t a, int32_t b) {
  int32_t r = (uint32_t)a + (uint32_t)b;
  return (a < 0 && b < 0 && r >= 0) || (a >= 0 && b >= 0 && r < 0);
}

/*
    Emulate below MIPS branch instructions.
    BEQ, BNE : Branch on condition
    BEQL, BNEL : Branch likely
*/
bool EmulateInstructionMIPS::Emulate_BXX_3ops(llvm::MCInst &insn) {
  bool success = false;
  uint32_t rs, rt;
  int32_t offset, pc, target = 0, rs_val, rt_val;
  const char *op_name = m_insn_info->getName(insn.getOpcode()).data();

  rs = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  rt = m_reg_info->getEncodingValue(insn.getOperand(1).getReg());
  offset = insn.getOperand(2).getImm();

  pc = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  rs_val = (int32_t)ReadRegisterUnsigned(eRegisterKindDWARF,
                                         dwarf_zero_mips + rs, 0, &success);
  if (!success)
    return false;

  rt_val = (int32_t)ReadRegisterUnsigned(eRegisterKindDWARF,
                                         dwarf_zero_mips + rt, 0, &success);
  if (!success)
    return false;

  if (!strcasecmp(op_name, "BEQ") || !strcasecmp(op_name, "BEQL")) {
    if (rs_val == rt_val)
      target = pc + offset;
    else
      target = pc + 8;
  } else if (!strcasecmp(op_name, "BNE") || !strcasecmp(op_name, "BNEL")) {
    if (rs_val != rt_val)
      target = pc + offset;
    else
      target = pc + 8;
  }

  Context context;
  context.type = eContextRelativeBranchImmediate;
  context.SetImmediate(offset);

  return WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                               target);
}

/*
    Emulate below MIPS branch instructions.
    BEQC, BNEC, BLTC, BGEC, BLTUC, BGEUC, BOVC, BNVC: Compact branch
   instructions with no delay slot
*/
bool EmulateInstructionMIPS::Emulate_BXX_3ops_C(llvm::MCInst &insn) {
  bool success = false;
  uint32_t rs, rt;
  int32_t offset, pc, target = 0, rs_val, rt_val;
  const char *op_name = m_insn_info->getName(insn.getOpcode()).data();
  uint32_t current_inst_size = m_insn_info->get(insn.getOpcode()).getSize();

  rs = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  rt = m_reg_info->getEncodingValue(insn.getOperand(1).getReg());
  offset = insn.getOperand(2).getImm();

  pc = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  rs_val = (int32_t)ReadRegisterUnsigned(eRegisterKindDWARF,
                                         dwarf_zero_mips + rs, 0, &success);
  if (!success)
    return false;

  rt_val = (int32_t)ReadRegisterUnsigned(eRegisterKindDWARF,
                                         dwarf_zero_mips + rt, 0, &success);
  if (!success)
    return false;

  if (!strcasecmp(op_name, "BEQC")) {
    if (rs_val == rt_val)
      target = pc + offset;
    else
      target = pc + 4;
  } else if (!strcasecmp(op_name, "BNEC")) {
    if (rs_val != rt_val)
      target = pc + offset;
    else
      target = pc + 4;
  } else if (!strcasecmp(op_name, "BLTC")) {
    if (rs_val < rt_val)
      target = pc + offset;
    else
      target = pc + 4;
  } else if (!strcasecmp(op_name, "BGEC")) {
    if (rs_val >= rt_val)
      target = pc + offset;
    else
      target = pc + 4;
  } else if (!strcasecmp(op_name, "BLTUC")) {
    if (rs_val < rt_val)
      target = pc + offset;
    else
      target = pc + 4;
  } else if (!strcasecmp(op_name, "BGEUC")) {
    if ((uint32_t)rs_val >= (uint32_t)rt_val)
      target = pc + offset;
    else
      target = pc + 4;
  } else if (!strcasecmp(op_name, "BOVC")) {
    if (IsAdd64bitOverflow(rs_val, rt_val))
      target = pc + offset;
    else
      target = pc + 4;
  } else if (!strcasecmp(op_name, "BNVC")) {
    if (!IsAdd64bitOverflow(rs_val, rt_val))
      target = pc + offset;
    else
      target = pc + 4;
  }

  Context context;
  context.type = eContextRelativeBranchImmediate;
  context.SetImmediate(current_inst_size + offset);

  return WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                               target);
}

/*
    Emulate below MIPS conditional branch and link instructions.
    BLEZALC, BGEZALC, BLTZALC, BGTZALC, BEQZALC, BNEZALC : Compact branches
*/
bool EmulateInstructionMIPS::Emulate_Bcond_Link_C(llvm::MCInst &insn) {
  bool success = false;
  uint32_t rs;
  int32_t offset, pc, target = 0;
  int32_t rs_val;
  const char *op_name = m_insn_info->getName(insn.getOpcode()).data();

  rs = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  offset = insn.getOperand(1).getImm();

  pc = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  rs_val = (int32_t)ReadRegisterUnsigned(eRegisterKindDWARF,
                                         dwarf_zero_mips + rs, 0, &success);
  if (!success)
    return false;

  if (!strcasecmp(op_name, "BLEZALC")) {
    if (rs_val <= 0)
      target = pc + offset;
    else
      target = pc + 4;
  } else if (!strcasecmp(op_name, "BGEZALC")) {
    if (rs_val >= 0)
      target = pc + offset;
    else
      target = pc + 4;
  } else if (!strcasecmp(op_name, "BLTZALC")) {
    if (rs_val < 0)
      target = pc + offset;
    else
      target = pc + 4;
  } else if (!strcasecmp(op_name, "BGTZALC")) {
    if (rs_val > 0)
      target = pc + offset;
    else
      target = pc + 4;
  } else if (!strcasecmp(op_name, "BEQZALC")) {
    if (rs_val == 0)
      target = pc + offset;
    else
      target = pc + 4;
  } else if (!strcasecmp(op_name, "BNEZALC")) {
    if (rs_val != 0)
      target = pc + offset;
    else
      target = pc + 4;
  }

  Context context;

  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                             target))
    return false;

  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_ra_mips,
                             pc + 4))
    return false;

  return true;
}

/*
    Emulate below MIPS Non-Compact conditional branch and link instructions.
    BLTZAL, BGEZAL      :
    BLTZALL, BGEZALL    : Branch likely
*/
bool EmulateInstructionMIPS::Emulate_Bcond_Link(llvm::MCInst &insn) {
  bool success = false;
  uint32_t rs;
  int32_t offset, pc, target = 0;
  int32_t rs_val;
  const char *op_name = m_insn_info->getName(insn.getOpcode()).data();

  rs = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  offset = insn.getOperand(1).getImm();

  pc = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  rs_val = (int32_t)ReadRegisterUnsigned(eRegisterKindDWARF,
                                         dwarf_zero_mips + rs, 0, &success);
  if (!success)
    return false;

  if (!strcasecmp(op_name, "BLTZAL") || !strcasecmp(op_name, "BLTZALL")) {
    if ((int32_t)rs_val < 0)
      target = pc + offset;
    else
      target = pc + 8;
  } else if (!strcasecmp(op_name, "BGEZAL") ||
             !strcasecmp(op_name, "BGEZALL")) {
    if ((int32_t)rs_val >= 0)
      target = pc + offset;
    else
      target = pc + 8;
  }

  Context context;

  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                             target))
    return false;

  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_ra_mips,
                             pc + 8))
    return false;

  return true;
}

/*
    Emulate below MIPS branch instructions.
    BLTZL, BGEZL, BGTZL, BLEZL : Branch likely
    BLTZ, BGEZ, BGTZ, BLEZ     : Non-compact branches
*/
bool EmulateInstructionMIPS::Emulate_BXX_2ops(llvm::MCInst &insn) {
  bool success = false;
  uint32_t rs;
  int32_t offset, pc, target = 0;
  int32_t rs_val;
  const char *op_name = m_insn_info->getName(insn.getOpcode()).data();

  rs = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  offset = insn.getOperand(1).getImm();

  pc = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  rs_val = (int32_t)ReadRegisterUnsigned(eRegisterKindDWARF,
                                         dwarf_zero_mips + rs, 0, &success);
  if (!success)
    return false;

  if (!strcasecmp(op_name, "BLTZL") || !strcasecmp(op_name, "BLTZ")) {
    if (rs_val < 0)
      target = pc + offset;
    else
      target = pc + 8;
  } else if (!strcasecmp(op_name, "BGEZL") || !strcasecmp(op_name, "BGEZ")) {
    if (rs_val >= 0)
      target = pc + offset;
    else
      target = pc + 8;
  } else if (!strcasecmp(op_name, "BGTZL") || !strcasecmp(op_name, "BGTZ")) {
    if (rs_val > 0)
      target = pc + offset;
    else
      target = pc + 8;
  } else if (!strcasecmp(op_name, "BLEZL") || !strcasecmp(op_name, "BLEZ")) {
    if (rs_val <= 0)
      target = pc + offset;
    else
      target = pc + 8;
  }

  Context context;
  context.type = eContextRelativeBranchImmediate;
  context.SetImmediate(offset);

  return WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                               target);
}

/*
    Emulate below MIPS branch instructions.
    BLTZC, BLEZC, BGEZC, BGTZC, BEQZC, BNEZC : Compact Branches
*/
bool EmulateInstructionMIPS::Emulate_BXX_2ops_C(llvm::MCInst &insn) {
  bool success = false;
  uint32_t rs;
  int32_t offset, pc, target = 0;
  int32_t rs_val;
  const char *op_name = m_insn_info->getName(insn.getOpcode()).data();
  uint32_t current_inst_size = m_insn_info->get(insn.getOpcode()).getSize();

  rs = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  offset = insn.getOperand(1).getImm();

  pc = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  rs_val = (int32_t)ReadRegisterUnsigned(eRegisterKindDWARF,
                                         dwarf_zero_mips + rs, 0, &success);
  if (!success)
    return false;

  if (!strcasecmp(op_name, "BLTZC")) {
    if (rs_val < 0)
      target = pc + offset;
    else
      target = pc + 4;
  } else if (!strcasecmp(op_name, "BLEZC")) {
    if (rs_val <= 0)
      target = pc + offset;
    else
      target = pc + 4;
  } else if (!strcasecmp(op_name, "BGEZC")) {
    if (rs_val >= 0)
      target = pc + offset;
    else
      target = pc + 4;
  } else if (!strcasecmp(op_name, "BGTZC")) {
    if (rs_val > 0)
      target = pc + offset;
    else
      target = pc + 4;
  } else if (!strcasecmp(op_name, "BEQZC")) {
    if (rs_val == 0)
      target = pc + offset;
    else
      target = pc + 4;
  } else if (!strcasecmp(op_name, "BNEZC")) {
    if (rs_val != 0)
      target = pc + offset;
    else
      target = pc + 4;
  }

  Context context;
  context.type = eContextRelativeBranchImmediate;
  context.SetImmediate(current_inst_size + offset);

  return WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                               target);
}

bool EmulateInstructionMIPS::Emulate_B16_MM(llvm::MCInst &insn) {
  bool success = false;
  int32_t offset, pc, target;
  uint32_t current_inst_size = m_insn_info->get(insn.getOpcode()).getSize();

  offset = insn.getOperand(0).getImm();

  pc = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  // unconditional branch
  target = pc + offset;

  Context context;
  context.type = eContextRelativeBranchImmediate;
  context.SetImmediate(current_inst_size + offset);

  return WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                               target);
}

/*
   BEQZC, BNEZC are 32 bit compact instructions without a delay slot.
   BEQZ16, BNEZ16 are 16 bit instructions with delay slot.
   BGEZALS, BLTZALS are 16 bit instructions with short (2-byte) delay slot.
*/
bool EmulateInstructionMIPS::Emulate_Branch_MM(llvm::MCInst &insn) {
  bool success = false;
  int32_t target = 0;
  uint32_t current_inst_size = m_insn_info->get(insn.getOpcode()).getSize();
  const char *op_name = m_insn_info->getName(insn.getOpcode()).data();
  bool update_ra = false;
  uint32_t ra_offset = 0;

  /*
   * BEQZ16 rs, offset
   *      condition <- (GPR[rs] = 0)
   *      if condition then
   *          PC = PC + sign_ext (offset || 0)
   *
   * BNEZ16 rs, offset
   *      condition <- (GPR[rs] != 0)
   *      if condition then
   *          PC = PC + sign_ext (offset || 0)
   *
   * BEQZC rs, offset     (compact instruction: No delay slot)
   *      condition <- (GPR[rs] == 0)
   *      if condition then
   *         PC = PC + 4 + sign_ext (offset || 0)
  */

  uint32_t rs = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  int32_t offset = insn.getOperand(1).getImm();

  int32_t pc =
      ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  int32_t rs_val = (int32_t)ReadRegisterUnsigned(
      eRegisterKindDWARF, dwarf_zero_mips + rs, 0, &success);
  if (!success)
    return false;

  if (!strcasecmp(op_name, "BEQZ16_MM")) {
    if (rs_val == 0)
      target = pc + offset;
    else
      target = pc + current_inst_size +
               m_next_inst_size; // Skip delay slot instruction.
  } else if (!strcasecmp(op_name, "BNEZ16_MM")) {
    if (rs_val != 0)
      target = pc + offset;
    else
      target = pc + current_inst_size +
               m_next_inst_size; // Skip delay slot instruction.
  } else if (!strcasecmp(op_name, "BEQZC_MM")) {
    if (rs_val == 0)
      target = pc + 4 + offset;
    else
      target =
          pc +
          4; // 32 bit instruction and does not have delay slot instruction.
  } else if (!strcasecmp(op_name, "BNEZC_MM")) {
    if (rs_val != 0)
      target = pc + 4 + offset;
    else
      target =
          pc +
          4; // 32 bit instruction and does not have delay slot instruction.
  } else if (!strcasecmp(op_name, "BGEZALS_MM")) {
    if (rs_val >= 0)
      target = pc + offset;
    else
      target = pc + 6; // 32 bit instruction with short (2-byte) delay slot

    update_ra = true;
    ra_offset = 6;
  } else if (!strcasecmp(op_name, "BLTZALS_MM")) {
    if (rs_val >= 0)
      target = pc + offset;
    else
      target = pc + 6; // 32 bit instruction with short (2-byte) delay slot

    update_ra = true;
    ra_offset = 6;
  }

  Context context;
  context.type = eContextRelativeBranchImmediate;
  context.SetImmediate(current_inst_size + offset);

  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                             target))
    return false;

  if (update_ra) {
    if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_ra_mips,
                               pc + ra_offset))
      return false;
  }
  return true;
}

/* Emulate micromips jump instructions.
   JALR16,JALRS16
*/
bool EmulateInstructionMIPS::Emulate_JALRx16_MM(llvm::MCInst &insn) {
  bool success = false;
  uint32_t ra_offset = 0;
  const char *op_name = m_insn_info->getName(insn.getOpcode()).data();

  uint32_t rs = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());

  uint32_t pc =
      ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  uint32_t rs_val = ReadRegisterUnsigned(eRegisterKindDWARF,
                                         dwarf_zero_mips + rs, 0, &success);
  if (!success)
    return false;

  if (!strcasecmp(op_name, "JALR16_MM"))
    ra_offset = 6; // 2-byte instruction with 4-byte delay slot.
  else if (!strcasecmp(op_name, "JALRS16_MM"))
    ra_offset = 4; // 2-byte instruction with 2-byte delay slot.

  Context context;

  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                             rs_val))
    return false;

  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_ra_mips,
                             pc + ra_offset))
    return false;

  return true;
}

/* Emulate JALS and JALX instructions.
    JALS 32 bit instruction with short (2-byte) delay slot.
    JALX 32 bit instruction with 4-byte delay slot.
*/
bool EmulateInstructionMIPS::Emulate_JALx(llvm::MCInst &insn) {
  bool success = false;
  uint32_t offset = 0, target = 0, pc = 0, ra_offset = 0;
  const char *op_name = m_insn_info->getName(insn.getOpcode()).data();

  /*
   * JALS target
   *      RA = PC + 6
   *      offset = sign_ext (offset << 1)
   *      PC = PC[31-27] | offset
   * JALX target
   *      RA = PC + 8
   *      offset = sign_ext (offset << 2)
   *      PC = PC[31-28] | offset
  */
  offset = insn.getOperand(0).getImm();

  pc = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  // These are PC-region branches and not PC-relative.
  if (!strcasecmp(op_name, "JALS_MM")) {
    // target address is in the “current” 128 MB-aligned region
    target = (pc & 0xF8000000UL) | offset;
    ra_offset = 6;
  } else if (!strcasecmp(op_name, "JALX_MM")) {
    // target address is in the “current” 256 MB-aligned region
    target = (pc & 0xF0000000UL) | offset;
    ra_offset = 8;
  }

  Context context;

  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                             target))
    return false;

  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_ra_mips,
                             pc + ra_offset))
    return false;

  return true;
}

bool EmulateInstructionMIPS::Emulate_JALRS(llvm::MCInst &insn) {
  bool success = false;
  uint32_t rs = 0, rt = 0;
  int32_t pc = 0, rs_val = 0;

  /*
      JALRS rt, rs
          GPR[rt] <- PC + 6
          PC <- GPR[rs]
  */

  rt = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  rs = m_reg_info->getEncodingValue(insn.getOperand(1).getReg());

  rs_val = (int32_t)ReadRegisterUnsigned(eRegisterKindDWARF,
                                         dwarf_zero_mips + rs, 0, &success);
  if (!success)
    return false;

  pc = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  Context context;

  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                             rs_val))
    return false;

  // This is 4-byte instruction with 2-byte delay slot.
  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_zero_mips + rt,
                             pc + 6))
    return false;

  return true;
}

bool EmulateInstructionMIPS::Emulate_BAL(llvm::MCInst &insn) {
  bool success = false;
  int32_t offset, pc, target;

  /*
   * BAL offset
   *      offset = sign_ext (offset << 2)
   *      RA = PC + 8
   *      PC = PC + offset
  */
  offset = insn.getOperand(0).getImm();

  pc = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  target = pc + offset;

  Context context;

  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                             target))
    return false;

  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_ra_mips,
                             pc + 8))
    return false;

  return true;
}

bool EmulateInstructionMIPS::Emulate_BALC(llvm::MCInst &insn) {
  bool success = false;
  int32_t offset, pc, target;

  /*
   * BALC offset
   *      offset = sign_ext (offset << 2)
   *      RA = PC + 4
   *      PC = PC + 4 + offset
  */
  offset = insn.getOperand(0).getImm();

  pc = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  target = pc + offset;

  Context context;

  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                             target))
    return false;

  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_ra_mips,
                             pc + 4))
    return false;

  return true;
}

bool EmulateInstructionMIPS::Emulate_BC(llvm::MCInst &insn) {
  bool success = false;
  int32_t offset, pc, target;

  /*
   * BC offset
   *      offset = sign_ext (offset << 2)
   *      PC = PC + 4 + offset
  */
  offset = insn.getOperand(0).getImm();

  pc = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  target = pc + offset;

  Context context;

  return WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                               target);
}

bool EmulateInstructionMIPS::Emulate_J(llvm::MCInst &insn) {
  bool success = false;
  uint32_t offset, pc;

  /*
   * J offset
   *      offset = sign_ext (offset << 2)
   *      PC = PC[63-28] | offset
  */
  offset = insn.getOperand(0).getImm();

  pc = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  /* This is a PC-region branch and not PC-relative */
  pc = (pc & 0xF0000000UL) | offset;

  Context context;

  return WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips, pc);
}

bool EmulateInstructionMIPS::Emulate_JAL(llvm::MCInst &insn) {
  bool success = false;
  uint32_t offset, target, pc;

  /*
   * JAL offset
   *      offset = sign_ext (offset << 2)
   *      PC = PC[63-28] | offset
  */
  offset = insn.getOperand(0).getImm();

  pc = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  /* This is a PC-region branch and not PC-relative */
  target = (pc & 0xF0000000UL) | offset;

  Context context;

  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                             target))
    return false;

  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_ra_mips,
                             pc + 8))
    return false;

  return true;
}

bool EmulateInstructionMIPS::Emulate_JALR(llvm::MCInst &insn) {
  bool success = false;
  uint32_t rs, rt;
  uint32_t pc, rs_val;

  /*
   * JALR rt, rs
   *      GPR[rt] = PC + 8
   *      PC = GPR[rs]
  */
  rt = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  rs = m_reg_info->getEncodingValue(insn.getOperand(1).getReg());

  pc = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  rs_val = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_zero_mips + rs, 0,
                                &success);
  if (!success)
    return false;

  Context context;

  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                             rs_val))
    return false;

  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_zero_mips + rt,
                             pc + 8))
    return false;

  return true;
}

bool EmulateInstructionMIPS::Emulate_JIALC(llvm::MCInst &insn) {
  bool success = false;
  uint32_t rt;
  int32_t target, offset, pc, rt_val;

  /*
   * JIALC rt, offset
   *      offset = sign_ext (offset)
   *      PC = GPR[rt] + offset
   *      RA = PC + 4
  */
  rt = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  offset = insn.getOperand(1).getImm();

  pc = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  rt_val = (int32_t)ReadRegisterUnsigned(eRegisterKindDWARF,
                                         dwarf_zero_mips + rt, 0, &success);
  if (!success)
    return false;

  target = rt_val + offset;

  Context context;

  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                             target))
    return false;

  if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_ra_mips,
                             pc + 4))
    return false;

  return true;
}

bool EmulateInstructionMIPS::Emulate_JIC(llvm::MCInst &insn) {
  bool success = false;
  uint32_t rt;
  int32_t target, offset, rt_val;

  /*
   * JIC rt, offset
   *      offset = sign_ext (offset)
   *      PC = GPR[rt] + offset
  */
  rt = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  offset = insn.getOperand(1).getImm();

  rt_val = (int32_t)ReadRegisterUnsigned(eRegisterKindDWARF,
                                         dwarf_zero_mips + rt, 0, &success);
  if (!success)
    return false;

  target = rt_val + offset;

  Context context;

  return WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                               target);
}

bool EmulateInstructionMIPS::Emulate_JR(llvm::MCInst &insn) {
  bool success = false;
  uint32_t rs;
  uint32_t rs_val;

  /*
   * JR rs
   *      PC = GPR[rs]
  */
  rs = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());

  rs_val = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_zero_mips + rs, 0,
                                &success);
  if (!success)
    return false;

  Context context;

  return WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                               rs_val);
}

/*
    Emulate Branch on FP True/False
    BC1F, BC1FL :   Branch on FP False (L stands for branch likely)
    BC1T, BC1TL :   Branch on FP True  (L stands for branch likely)
*/
bool EmulateInstructionMIPS::Emulate_FP_branch(llvm::MCInst &insn) {
  bool success = false;
  uint32_t cc, fcsr;
  int32_t pc, offset, target = 0;
  const char *op_name = m_insn_info->getName(insn.getOpcode()).data();

  cc = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  offset = insn.getOperand(1).getImm();

  pc = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  fcsr = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_fcsr_mips, 0, &success);
  if (!success)
    return false;

  /* fcsr[23], fcsr[25-31] are vaild condition bits */
  fcsr = ((fcsr >> 24) & 0xfe) | ((fcsr >> 23) & 0x01);

  if (!strcasecmp(op_name, "BC1F") || !strcasecmp(op_name, "BC1FL")) {
    if ((fcsr & (1 << cc)) == 0)
      target = pc + offset;
    else
      target = pc + 8;
  } else if (!strcasecmp(op_name, "BC1T") || !strcasecmp(op_name, "BC1TL")) {
    if ((fcsr & (1 << cc)) != 0)
      target = pc + offset;
    else
      target = pc + 8;
  }
  Context context;

  return WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                               target);
}

bool EmulateInstructionMIPS::Emulate_BC1EQZ(llvm::MCInst &insn) {
  bool success = false;
  uint32_t ft;
  uint32_t ft_val;
  int32_t target, pc, offset;

  /*
   * BC1EQZ ft, offset
   *  condition <- (FPR[ft].bit0 == 0)
   *      if condition then
   *          offset = sign_ext (offset)
   *          PC = PC + 4 + offset
  */
  ft = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  offset = insn.getOperand(1).getImm();

  pc = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  ft_val = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_zero_mips + ft, 0,
                                &success);
  if (!success)
    return false;

  if ((ft_val & 1) == 0)
    target = pc + 4 + offset;
  else
    target = pc + 8;

  Context context;

  return WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                               target);
}

bool EmulateInstructionMIPS::Emulate_BC1NEZ(llvm::MCInst &insn) {
  bool success = false;
  uint32_t ft;
  uint32_t ft_val;
  int32_t target, pc, offset;

  /*
   * BC1NEZ ft, offset
   *  condition <- (FPR[ft].bit0 != 0)
   *      if condition then
   *          offset = sign_ext (offset)
   *          PC = PC + 4 + offset
  */
  ft = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  offset = insn.getOperand(1).getImm();

  pc = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  ft_val = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_zero_mips + ft, 0,
                                &success);
  if (!success)
    return false;

  if ((ft_val & 1) != 0)
    target = pc + 4 + offset;
  else
    target = pc + 8;

  Context context;

  return WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                               target);
}

/*
    Emulate MIPS-3D Branch instructions
    BC1ANY2F, BC1ANY2T  : Branch on Any of Two Floating Point Condition Codes
   False/True
    BC1ANY4F, BC1ANY4T  : Branch on Any of Four Floating Point Condition Codes
   False/True
*/
bool EmulateInstructionMIPS::Emulate_3D_branch(llvm::MCInst &insn) {
  bool success = false;
  uint32_t cc, fcsr;
  int32_t pc, offset, target = 0;
  const char *op_name = m_insn_info->getName(insn.getOpcode()).data();

  cc = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  offset = insn.getOperand(1).getImm();

  pc = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  fcsr = (uint32_t)ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_fcsr_mips, 0,
                                        &success);
  if (!success)
    return false;

  /* fcsr[23], fcsr[25-31] are vaild condition bits */
  fcsr = ((fcsr >> 24) & 0xfe) | ((fcsr >> 23) & 0x01);

  if (!strcasecmp(op_name, "BC1ANY2F")) {
    /* if any one bit is 0 */
    if (((fcsr >> cc) & 3) != 3)
      target = pc + offset;
    else
      target = pc + 8;
  } else if (!strcasecmp(op_name, "BC1ANY2T")) {
    /* if any one bit is 1 */
    if (((fcsr >> cc) & 3) != 0)
      target = pc + offset;
    else
      target = pc + 8;
  } else if (!strcasecmp(op_name, "BC1ANY4F")) {
    /* if any one bit is 0 */
    if (((fcsr >> cc) & 0xf) != 0xf)
      target = pc + offset;
    else
      target = pc + 8;
  } else if (!strcasecmp(op_name, "BC1ANY4T")) {
    /* if any one bit is 1 */
    if (((fcsr >> cc) & 0xf) != 0)
      target = pc + offset;
    else
      target = pc + 8;
  }
  Context context;

  return WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                               target);
}

bool EmulateInstructionMIPS::Emulate_BNZB(llvm::MCInst &insn) {
  return Emulate_MSA_Branch_DF(insn, 1, true);
}

bool EmulateInstructionMIPS::Emulate_BNZH(llvm::MCInst &insn) {
  return Emulate_MSA_Branch_DF(insn, 2, true);
}

bool EmulateInstructionMIPS::Emulate_BNZW(llvm::MCInst &insn) {
  return Emulate_MSA_Branch_DF(insn, 4, true);
}

bool EmulateInstructionMIPS::Emulate_BNZD(llvm::MCInst &insn) {
  return Emulate_MSA_Branch_DF(insn, 8, true);
}

bool EmulateInstructionMIPS::Emulate_BZB(llvm::MCInst &insn) {
  return Emulate_MSA_Branch_DF(insn, 1, false);
}

bool EmulateInstructionMIPS::Emulate_BZH(llvm::MCInst &insn) {
  return Emulate_MSA_Branch_DF(insn, 2, false);
}

bool EmulateInstructionMIPS::Emulate_BZW(llvm::MCInst &insn) {
  return Emulate_MSA_Branch_DF(insn, 4, false);
}

bool EmulateInstructionMIPS::Emulate_BZD(llvm::MCInst &insn) {
  return Emulate_MSA_Branch_DF(insn, 8, false);
}

bool EmulateInstructionMIPS::Emulate_MSA_Branch_DF(llvm::MCInst &insn,
                                                   int element_byte_size,
                                                   bool bnz) {
  bool success = false, branch_hit = true;
  int32_t target = 0;
  RegisterValue reg_value;
  const uint8_t *ptr = nullptr;

  uint32_t wt = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  int32_t offset = insn.getOperand(1).getImm();

  int32_t pc =
      ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  if (ReadRegister(eRegisterKindDWARF, dwarf_w0_mips + wt, reg_value))
    ptr = (const uint8_t *)reg_value.GetBytes();
  else
    return false;

  for (int i = 0; i < 16 / element_byte_size; i++) {
    switch (element_byte_size) {
    case 1:
      if ((*ptr == 0 && bnz) || (*ptr != 0 && !bnz))
        branch_hit = false;
      break;
    case 2:
      if ((*(const uint16_t *)ptr == 0 && bnz) ||
          (*(const uint16_t *)ptr != 0 && !bnz))
        branch_hit = false;
      break;
    case 4:
      if ((*(const uint32_t *)ptr == 0 && bnz) ||
          (*(const uint32_t *)ptr != 0 && !bnz))
        branch_hit = false;
      break;
    case 8:
      if ((*(const uint64_t *)ptr == 0 && bnz) ||
          (*(const uint64_t *)ptr != 0 && !bnz))
        branch_hit = false;
      break;
    }
    if (!branch_hit)
      break;
    ptr = ptr + element_byte_size;
  }

  if (branch_hit)
    target = pc + offset;
  else
    target = pc + 8;

  Context context;
  context.type = eContextRelativeBranchImmediate;

  return WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                               target);
}

bool EmulateInstructionMIPS::Emulate_BNZV(llvm::MCInst &insn) {
  return Emulate_MSA_Branch_V(insn, true);
}

bool EmulateInstructionMIPS::Emulate_BZV(llvm::MCInst &insn) {
  return Emulate_MSA_Branch_V(insn, false);
}

bool EmulateInstructionMIPS::Emulate_MSA_Branch_V(llvm::MCInst &insn,
                                                  bool bnz) {
  bool success = false;
  int32_t target = 0;
  llvm::APInt wr_val = llvm::APInt::getNullValue(128);
  llvm::APInt fail_value = llvm::APInt::getMaxValue(128);
  llvm::APInt zero_value = llvm::APInt::getNullValue(128);
  RegisterValue reg_value;

  uint32_t wt = m_reg_info->getEncodingValue(insn.getOperand(0).getReg());
  int32_t offset = insn.getOperand(1).getImm();

  int32_t pc =
      ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_pc_mips, 0, &success);
  if (!success)
    return false;

  if (ReadRegister(eRegisterKindDWARF, dwarf_w0_mips + wt, reg_value))
    wr_val = reg_value.GetAsUInt128(fail_value);
  else
    return false;

  if ((llvm::APInt::isSameValue(zero_value, wr_val) && !bnz) ||
      (!llvm::APInt::isSameValue(zero_value, wr_val) && bnz))
    target = pc + offset;
  else
    target = pc + 8;

  Context context;
  context.type = eContextRelativeBranchImmediate;

  return WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_pc_mips,
                               target);
}

bool EmulateInstructionMIPS::Emulate_LDST_Imm(llvm::MCInst &insn) {
  bool success = false;
  uint32_t base;
  int32_t imm, address;
  Context bad_vaddr_context;

  uint32_t num_operands = insn.getNumOperands();
  base =
      m_reg_info->getEncodingValue(insn.getOperand(num_operands - 2).getReg());
  imm = insn.getOperand(num_operands - 1).getImm();

  RegisterInfo reg_info_base;
  if (!GetRegisterInfo(eRegisterKindDWARF, dwarf_zero_mips + base,
                       reg_info_base))
    return false;

  /* read base register */
  address = (int32_t)ReadRegisterUnsigned(eRegisterKindDWARF,
                                          dwarf_zero_mips + base, 0, &success);
  if (!success)
    return false;

  /* destination address */
  address = address + imm;

  /* Set the bad_vaddr register with base address used in the instruction */
  bad_vaddr_context.type = eContextInvalid;
  WriteRegisterUnsigned(bad_vaddr_context, eRegisterKindDWARF, dwarf_bad_mips,
                        address);

  return true;
}

bool EmulateInstructionMIPS::Emulate_LDST_Reg(llvm::MCInst &insn) {
  bool success = false;
  uint32_t base, index;
  int32_t address, index_address;
  Context bad_vaddr_context;

  uint32_t num_operands = insn.getNumOperands();
  base =
      m_reg_info->getEncodingValue(insn.getOperand(num_operands - 2).getReg());
  index =
      m_reg_info->getEncodingValue(insn.getOperand(num_operands - 1).getReg());

  RegisterInfo reg_info_base, reg_info_index;
  if (!GetRegisterInfo(eRegisterKindDWARF, dwarf_zero_mips + base,
                       reg_info_base))
    return false;

  if (!GetRegisterInfo(eRegisterKindDWARF, dwarf_zero_mips + index,
                       reg_info_index))
    return false;

  /* read base register */
  address = (int32_t)ReadRegisterUnsigned(eRegisterKindDWARF,
                                          dwarf_zero_mips + base, 0, &success);
  if (!success)
    return false;

  /* read index register */
  index_address = (int32_t)ReadRegisterUnsigned(
      eRegisterKindDWARF, dwarf_zero_mips + index, 0, &success);
  if (!success)
    return false;

  /* destination address */
  address = address + index_address;

  /* Set the bad_vaddr register with base address used in the instruction */
  bad_vaddr_context.type = eContextInvalid;
  WriteRegisterUnsigned(bad_vaddr_context, eRegisterKindDWARF, dwarf_bad_mips,
                        address);

  return true;
}