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
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
|
#include <inttypes.h>
#include "il.h"
#include "lowlevelilinstruction.h"
#include "arch_x86_common_architecture.h"
using namespace BinaryNinja;
using namespace std;
static xed_reg_enum_t GetStackPointer(const size_t addrSize)
{
switch (addrSize)
{
case 2:
return XED_REG_SP;
case 4:
return XED_REG_ESP;
default:
return XED_REG_RSP;
}
}
static xed_reg_enum_t GetFramePointer(const size_t addrSize)
{
switch (addrSize)
{
case 2:
return XED_REG_BP;
case 4:
return XED_REG_EBP;
default:
return XED_REG_RBP;
}
}
static xed_reg_enum_t GetCountRegister(const size_t addrSize)
{
switch (addrSize)
{
case 2:
return XED_REG_CX;
case 4:
return XED_REG_ECX;
default:
return XED_REG_RCX;
}
}
//TODO handle imms for MPX args
// For most instructions, instruction_index == operand_index, but some instructions (floating point, some others) have an implicit first operand (st0), so we have to remap things a bit
// Instruction index represents the 'nth' argument/opcode in the instruction, whereas the operand index is index that XED holds that operand in the instruction
static size_t GetILOperandMemoryAddress(LowLevelILFunction& il, const xed_decoded_inst_t* xedd, const uint64_t addr, const size_t instruction_index, const size_t operand_index)
{
const xed_inst_t* xi = xed_decoded_inst_inst(xedd);
const xed_operand_t* op = xed_inst_operand(xi, (unsigned)operand_index);
const xed_operand_values_t* ov = xed_decoded_inst_operands_const(xedd);
const xed_operand_enum_t op_name = xed_operand_name(op);
size_t offset = BN_INVALID_EXPR;
const size_t addrSize = xed_decoded_inst_get_machine_mode_bits(xedd) / 8;
switch(op_name)
{
case XED_OPERAND_AGEN:
case XED_OPERAND_MEM0:
{
const int64_t disp = xed_decoded_inst_get_memory_displacement(xedd, 0);
// [reg] if reg != instruction_pointer
const xed_reg_enum_t base = xed_decoded_inst_get_base_reg(xedd, 0);
if ((base != XED_REG_INVALID) && !((base == XED_REG_RIP) || (base == XED_REG_EIP) || (base == XED_REG_IP)))
{
offset = il.Register(addrSize, base);
}
else if ((base == XED_REG_RIP) || (base == XED_REG_EIP) || (base == XED_REG_IP)) // Resolve RIP to a constant
{
if (xed_operand_values_has_memory_displacement(ov) && (disp != 0))
return il.Operand(instruction_index, il.ConstPointer(addrSize, disp + addr + xed_decoded_inst_get_length(xedd)));
else
return il.Operand(instruction_index, il.ConstPointer(addrSize, addr + xed_decoded_inst_get_length(xedd)));
}
// [...+reg] or [...+reg*const] or [reg*const]
const xed_reg_enum_t index = xed_decoded_inst_get_index_reg(xedd, 0);
bool constIsPointer = false;
if (index != XED_REG_INVALID)
if (!xed_decoded_inst_get_attribute(xedd, XED_ATTRIBUTE_INDEX_REG_IS_POINTER)) // MPX...TODO (extra registers)
{
const unsigned int scale = xed_decoded_inst_get_scale(xedd, 0);
if (scale != 1)
{
unsigned short shift = 0;
if (scale == 2)
shift = 1;
else if (scale == 4)
shift = 2;
else if (scale == 8)
shift = 3;
if (offset != BN_INVALID_EXPR)
offset = il.Add(addrSize,
offset,
il.ShiftLeft(addrSize,
il.Register(addrSize, index),
il.Const(1, shift)));
else
{
// case for [...+reg*const] so we know that the const must be a pointer
constIsPointer = true;
offset = il.ShiftLeft(addrSize,
il.Register(addrSize, index),
il.Const(1, shift));
}
}
else
if (offset != BN_INVALID_EXPR)
offset = il.Add(addrSize,
offset,
il.Register(addrSize, index));
else
offset = il.Register(addrSize, index);
}
// The [...+const] bit or just [const]
bool isJmpClass = (XED_ICLASS_JMP == xed_decoded_inst_get_iclass(xedd)) || constIsPointer;
if (xed_operand_values_has_memory_displacement(xed_decoded_inst_operands_const(xedd)) && (disp != 0))
{
if (offset != BN_INVALID_EXPR)
offset = il.Add(addrSize, offset, isJmpClass ? il.ConstPointer(addrSize, disp) : il.Const(addrSize, disp));
else
offset = isJmpClass ? il.ConstPointer(addrSize, disp) : il.Const(addrSize, disp);
}
else if (xed_operand_values_has_memory_displacement(xed_decoded_inst_operands_const(xedd)) && (disp == 0) && (offset == BN_INVALID_EXPR))
{
offset = isJmpClass ? il.ConstPointer(addrSize, disp) : il.Const(addrSize, disp);
}
// If there's a non-default segment in use
xed_reg_enum_t seg = xed_decoded_inst_get_seg_reg(xedd, 0);
if (seg != XED_REG_INVALID && !xed_operand_values_using_default_segment(ov, 0))
{
// Remap FS/GS to FSbase/GSbase respectively
if (seg == XED_REG_FS)
seg = XED_REG_FSBASE;
else if (seg == XED_REG_GS)
seg = XED_REG_GSBASE;
if (offset == BN_INVALID_EXPR) // The only logical path that brings us here with an invalid offset is a disp of 0
offset = il.Register(addrSize, seg);
else
offset = il.Add(addrSize, il.Register(addrSize, seg), offset);
}
break;
}
case XED_OPERAND_MEM1:
{
const xed_reg_enum_t base = xed_decoded_inst_get_base_reg(xedd, 1);
if (base != XED_REG_INVALID)
{
if ((base == XED_REG_RIP) || (base == XED_REG_EIP) || (base == XED_REG_IP))
offset = il.ConstPointer(addrSize, addr);
else
offset = il.Register(addrSize, base);
}
xed_reg_enum_t seg = xed_decoded_inst_get_seg_reg(xedd, 1);
if (seg != XED_REG_INVALID && !xed_operand_values_using_default_segment(ov, 1))
{
if (seg == XED_REG_FS)
seg = XED_REG_FSBASE;
else if (seg == XED_REG_GS)
seg = XED_REG_GSBASE;
offset = il.Add(addrSize,
il.Register(addrSize, seg),
offset);
}
break;
}
default:
LogError("%s not implemented in GetILOperandMemoryAddress at address 0x%" PRIx64 ".", xed_operand_enum_t2str(op_name), addr);
}
return il.Operand(instruction_index, offset);
}
// For most instructions, instruction_index == operand_index, but some instructions (floating point, some others) have an implicit first operand (st0), so we have to remap things a bit
// Instruction index represents the 'nth' argument/opcode in the instruction, whereas the operand index is index that XED holds that operand in the instruction
static size_t ReadILOperand(LowLevelILFunction& il, const xed_decoded_inst_t* const xedd,
const size_t addr, const size_t instruction_index,
const size_t operand_index, size_t sizeToRead = 0)
{
if (sizeToRead == 0)
sizeToRead = xed_decoded_inst_operand_length_bits(xedd, (unsigned)operand_index) / 8;
const unsigned int immediateSize = xed_decoded_inst_get_operand_width(xedd) / 8;
const int64_t relbr = xed_decoded_inst_get_branch_displacement(xedd) + addr + xed_decoded_inst_get_length(xedd);
const int64_t absbr = xed_decoded_inst_get_branch_displacement(xedd);
const xed_operand_enum_t op_name = xed_operand_name(xed_inst_operand(xed_decoded_inst_inst(xedd), (unsigned)operand_index));
const auto reg1 = xed_decoded_inst_get_reg(xedd, op_name);
const size_t addrSize = xed_decoded_inst_get_machine_mode_bits(xedd) / 8;
// Register cases
if (xed_operand_is_register(op_name) || xed_operand_is_memory_addressing_register(op_name))
{
if ((reg1 == XED_REG_RIP) || (reg1 == XED_REG_EIP) || (reg1 == XED_REG_IP))
return il.Operand(instruction_index, il.ConstPointer(sizeToRead, addr));
return il.Operand(instruction_index, il.Register(sizeToRead, (uint32_t)reg1));
}
switch (op_name)
{
// Immediates:
case XED_OPERAND_IMM0:
if (xed_decoded_inst_get_immediate_is_signed(xedd))
return il.Operand(instruction_index, il.Const(immediateSize, xed_decoded_inst_get_signed_immediate(xedd)));
else
return il.Operand(instruction_index, il.Const(immediateSize, xed_decoded_inst_get_unsigned_immediate(xedd)));
// Second Immdiate Value
case XED_OPERAND_IMM1:
return il.Operand(instruction_index, il.Const(1, xed_decoded_inst_get_second_immediate(xedd)));
// Immediate Address Value
case XED_OPERAND_PTR:
case XED_OPERAND_RELBR:
return il.Operand(instruction_index, il.ConstPointer(addrSize, relbr));
case XED_OPERAND_ABSBR:
return il.Operand(instruction_index, il.ConstPointer(addrSize, absbr));
// Memory Acesses
case XED_OPERAND_AGEN:
case XED_OPERAND_MEM0:
case XED_OPERAND_MEM1:
return il.Operand(instruction_index, il.Load(sizeToRead, GetILOperandMemoryAddress(il, xedd, addr, instruction_index, operand_index)));
// Not implimented or error
default:
return il.Undefined();
}
}
// For most instructions, instruction_index == operand_index, but some instructions (floating point, some others) have an implicit first operand (st0), so we have to remap things a bit
// Instruction index represents the 'nth' argument/opcode in the instruction, whereas the operand index is index that XED holds that operand in the instruction
static size_t ReadFloatILOperand(LowLevelILFunction& il, const xed_decoded_inst_t* xedd, const size_t addr, const size_t instruction_index, const size_t operand_index, size_t opLen = 10)
{
const unsigned int operandSize = xed_decoded_inst_operand_length_bits(xedd, (unsigned)operand_index) / 8;
const xed_operand_enum_t op_name = xed_operand_name(xed_inst_operand(xed_decoded_inst_inst(xedd), (unsigned)operand_index));
// Register cases
if (xed_operand_is_register(op_name) || xed_operand_is_memory_addressing_register(op_name))
return il.Operand(instruction_index, il.Register(operandSize, (uint32_t)xed_decoded_inst_get_reg(xedd, op_name)));
switch (op_name)
{
// Immediates
case XED_OPERAND_IMM0:
case XED_OPERAND_PTR:
case XED_OPERAND_ABSBR:
case XED_OPERAND_RELBR:
if (xed_decoded_inst_get_immediate_is_signed(xedd))
return il.Operand(instruction_index, il.FloatConvert(opLen, il.FloatConstRaw(operandSize, xed_decoded_inst_get_signed_immediate(xedd))));
else
return il.Operand(instruction_index, il.FloatConvert(opLen, il.FloatConstRaw(operandSize, xed_decoded_inst_get_unsigned_immediate(xedd))));
case XED_OPERAND_IMM1:
return il.Operand(instruction_index, il.FloatConvert(opLen, il.FloatConstRaw(operandSize, xed_decoded_inst_get_second_immediate(xedd))));
// Memory Acesses
case XED_OPERAND_AGEN:
case XED_OPERAND_MEM0:
case XED_OPERAND_MEM1: // In what case would the memory address size be 10?? (floating point ops?)
if (operandSize != opLen)
return il.Operand(instruction_index, il.FloatConvert(opLen, il.Load(operandSize, GetILOperandMemoryAddress(il, xedd, addr, instruction_index, operand_index))));
return il.Operand(instruction_index, il.Load(opLen, GetILOperandMemoryAddress(il, xedd, addr, instruction_index, operand_index)));
default:
return il.Undefined();
}
}
// For most instructions, instruction_index == operand_index, but some instructions (floating point, some others) have an implicit first operand (st0), so we have to remap things a bit
// Instruction index represents the 'nth' argument/opcode in the instruction, whereas the operand index is index that XED holds that operand in the instruction
static size_t WriteILOperand(LowLevelILFunction& il, const xed_decoded_inst_t* const xedd, const size_t addr,
const size_t instruction_index, const size_t operand_index,
const size_t value, size_t sizeToWrite = 0)
{
// sizeToWrite allows one to specify a part of the operand to write
// other than the whole
// this solves some of the problems we have; but not all
// we still need the ability to read and write a slice of the operand
if (sizeToWrite == 0)
sizeToWrite = xed_decoded_inst_operand_length(xedd, operand_index);
const xed_operand_enum_t op_name = xed_operand_name(xed_inst_operand(xed_decoded_inst_inst(xedd), operand_index));
// Register cases
if (xed_operand_is_register(op_name) || xed_operand_is_memory_addressing_register(op_name))
return il.Operand(instruction_index, il.SetRegister(sizeToWrite, xed_decoded_inst_get_reg(xedd, op_name), value));
switch (op_name)
{
// Memory Accesses
case XED_OPERAND_AGEN:
case XED_OPERAND_MEM0:
case XED_OPERAND_MEM1:
return il.Operand(instruction_index, il.Store(sizeToWrite, GetILOperandMemoryAddress(il, xedd, addr, instruction_index, operand_index), value));
default:
return il.Undefined();
}
}
static void ConditionalJump(Architecture* arch, LowLevelILFunction& il, size_t cond, size_t addrSize, uint64_t t, uint64_t f)
{
BNLowLevelILLabel* trueLabel = il.GetLabelForAddress(arch, t);
BNLowLevelILLabel* falseLabel = il.GetLabelForAddress(arch, f);
if (trueLabel && falseLabel)
{
il.AddInstruction(il.If(cond, *trueLabel, *falseLabel));
return;
}
LowLevelILLabel trueCode, falseCode;
if (trueLabel)
{
il.AddInstruction(il.If(cond, *trueLabel, falseCode));
il.MarkLabel(falseCode);
il.AddInstruction(il.Jump(il.ConstPointer(addrSize, f)));
return;
}
if (falseLabel)
{
il.AddInstruction(il.If(cond, trueCode, *falseLabel));
il.MarkLabel(trueCode);
il.AddInstruction(il.Jump(il.ConstPointer(addrSize, t)));
return;
}
il.AddInstruction(il.If(cond, trueCode, falseCode));
il.MarkLabel(trueCode);
il.AddInstruction(il.Jump(il.ConstPointer(addrSize, t)));
il.MarkLabel(falseCode);
il.AddInstruction(il.Jump(il.ConstPointer(addrSize, f)));
}
static void DirFlagIf(LowLevelILFunction& il,
std::function<void()> addPreTestIl,
std::function<void()> addDirFlagSetIl,
std::function<void()> addDirFlagClearIl)
{
LowLevelILLabel dirFlagSet, dirFlagClear, dirFlagDone;
addPreTestIl();
il.AddInstruction(il.If(il.Flag(IL_FLAG_D), dirFlagSet, dirFlagClear));
il.MarkLabel(dirFlagSet);
addDirFlagSetIl();
il.AddInstruction(il.Goto(dirFlagDone));
il.MarkLabel(dirFlagClear);
addDirFlagClearIl();
il.AddInstruction(il.Goto(dirFlagDone));
il.MarkLabel(dirFlagDone);
}
static void Repeat(
const xed_decoded_inst_t* const xedd,
LowLevelILFunction& il,
std::function<void ()> addil)
{
const size_t addrSize = xed_decoded_inst_get_machine_mode_bits(xedd) / 8;
LowLevelILLabel trueLabel, falseLabel, doneLabel;
const xed_operand_values_t* const ov = xed_decoded_inst_operands_const(xedd);
if (xed_operand_values_has_real_rep(ov))
{
il.AddInstruction(il.Goto(trueLabel));
il.MarkLabel(trueLabel);
il.AddInstruction(il.If(
il.CompareNotEqual(addrSize,
il.Register(addrSize, GetCountRegister(addrSize)),
il.Const(addrSize, 0)), falseLabel, doneLabel));
il.MarkLabel(falseLabel);
}
addil();
if (xed_operand_values_has_real_rep(ov))
{
il.AddInstruction(
il.SetRegister(addrSize,
GetCountRegister(addrSize),
il.Sub(addrSize,
il.Register(addrSize, GetCountRegister(addrSize)),
il.Const(addrSize, 1))));
const xed_iclass_enum_t xeddiClass = xed_decoded_inst_get_iclass(xedd);
if (xed_operand_values_has_repne_prefix(ov))
il.AddInstruction(il.If(il.FlagCondition(LLFC_NE), trueLabel, doneLabel));
else if (xed_repe_map(xed_norep_map(xeddiClass)) == xeddiClass)
il.AddInstruction(il.If(il.FlagCondition(LLFC_E), trueLabel, doneLabel));
else
il.AddInstruction(il.Goto(trueLabel));
il.MarkLabel(doneLabel);
}
}
static void CMovFlagCond(const int64_t addr, const xed_decoded_inst_t* xedd, LowLevelILFunction& il, BNLowLevelILFlagCondition flag)
{
bool isCondFault = false;
bool isNdd = xed_decoded_inst_get_attribute(xedd, XED_ATTRIBUTE_APX_NDD);
switch (xed_decoded_inst_get_iclass(xedd))
{
case XED_ICLASS_CFCMOVB:
case XED_ICLASS_CFCMOVBE:
case XED_ICLASS_CFCMOVL:
case XED_ICLASS_CFCMOVLE:
case XED_ICLASS_CFCMOVNB:
case XED_ICLASS_CFCMOVNBE:
case XED_ICLASS_CFCMOVNL:
case XED_ICLASS_CFCMOVNLE:
case XED_ICLASS_CFCMOVNO:
case XED_ICLASS_CFCMOVNP:
case XED_ICLASS_CFCMOVNS:
case XED_ICLASS_CFCMOVNZ:
case XED_ICLASS_CFCMOVO:
case XED_ICLASS_CFCMOVP:
case XED_ICLASS_CFCMOVS:
case XED_ICLASS_CFCMOVZ:
isCondFault = true;
default:
break;
}
LowLevelILLabel trueLabel, falseLabel, doneLabel;
il.AddInstruction(
il.If(
il.FlagCondition(flag),
trueLabel, falseLabel));
il.MarkLabel(trueLabel);
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
ReadILOperand(il, xedd, addr, 1, 1)));
il.AddInstruction(il.Goto(doneLabel));
il.MarkLabel(falseLabel);
if (isNdd)
{
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
ReadILOperand(il, xedd, addr, 2, 2)));
}
else if (isCondFault)
{
const xed_operand_enum_t op_name = xed_operand_name(xed_inst_operand(xed_decoded_inst_inst(xedd), 0));
// Register cases
if (xed_operand_is_register(op_name))
{
size_t addrSize = xed_decoded_inst_get_machine_mode_bits(xedd) / 8;
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Const(addrSize, 0)));
}
else
{
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
ReadILOperand(il, xedd, addr, 1, 1)));
}
}
il.MarkLabel(doneLabel);
}
static void CMovFlagGroup(const int64_t addr, const xed_decoded_inst_t* xedd, LowLevelILFunction& il, uint32_t flag)
{
bool isCondFault = false;
bool isNdd = xed_decoded_inst_get_attribute(xedd, XED_ATTRIBUTE_APX_NDD);
switch (xed_decoded_inst_get_iclass(xedd))
{
case XED_ICLASS_CFCMOVB:
case XED_ICLASS_CFCMOVBE:
case XED_ICLASS_CFCMOVL:
case XED_ICLASS_CFCMOVLE:
case XED_ICLASS_CFCMOVNB:
case XED_ICLASS_CFCMOVNBE:
case XED_ICLASS_CFCMOVNL:
case XED_ICLASS_CFCMOVNLE:
case XED_ICLASS_CFCMOVNO:
case XED_ICLASS_CFCMOVNP:
case XED_ICLASS_CFCMOVNS:
case XED_ICLASS_CFCMOVNZ:
case XED_ICLASS_CFCMOVO:
case XED_ICLASS_CFCMOVP:
case XED_ICLASS_CFCMOVS:
case XED_ICLASS_CFCMOVZ:
isCondFault = true;
default:
break;
}
LowLevelILLabel trueLabel, falseLabel, doneLabel;
il.AddInstruction(
il.If(
il.FlagGroup(flag),
trueLabel, doneLabel
)
);
il.MarkLabel(trueLabel);
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
ReadILOperand(il, xedd, addr, 1, 1)));
il.AddInstruction(il.Goto(doneLabel));
il.MarkLabel(falseLabel);
if (isNdd)
{
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
ReadILOperand(il, xedd, addr, 2, 2)));
}
else if (isCondFault)
{
const xed_operand_enum_t op_name = xed_operand_name(xed_inst_operand(xed_decoded_inst_inst(xedd), 0));
if (xed_operand_is_register(op_name) || xed_operand_is_memory_addressing_register(op_name))
{
size_t addrSize = xed_decoded_inst_get_machine_mode_bits(xedd) / 8;
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Const(addrSize, 0)));
}
else
{
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
ReadILOperand(il, xedd, addr, 1, 1)));
}
}
il.MarkLabel(doneLabel);
}
static void AssignEvexDfv(const xed_decoded_inst_t* xedd, LowLevelILFunction& il)
{
xed_flag_dfv_t defaultFlagValues;
if (!xed_flag_dfv_get_default_flags_values(
xed_decoded_inst_get_dfv_reg(xedd), &defaultFlagValues))
{
// TODO: better error handling
il.AddInstruction(il.Undefined());
return;
}
il.AddInstruction(
il.SetFlag(IL_FLAG_O,
il.Const(1, defaultFlagValues.s.of)));
il.AddInstruction(
il.SetFlag(IL_FLAG_S,
il.Const(1, defaultFlagValues.s.sf)));
il.AddInstruction(
il.SetFlag(IL_FLAG_Z,
il.Const(1, defaultFlagValues.s.zf)));
il.AddInstruction(
il.SetFlag(IL_FLAG_C,
il.Const(1, defaultFlagValues.s.cf)));
il.AddInstruction(
il.SetFlag(IL_FLAG_P,
il.Const(1, defaultFlagValues.s.cf)));
il.AddInstruction(
il.SetFlag(IL_FLAG_A,
il.Const(1, 0)));
}
static void ConditionalCmp(const int64_t addr, const xed_decoded_inst_t* xedd, LowLevelILFunction& il, uint32_t size, ExprId cond)
{
LowLevelILLabel trueLabel, falseLabel, doneLabel;
il.AddInstruction(
il.If(
cond,
trueLabel, falseLabel
)
);
il.MarkLabel(trueLabel);
il.AddInstruction(
il.Sub(size,
ReadILOperand(il, xedd, addr, 0, 0),
ReadILOperand(il, xedd, addr, 1, 1),
IL_FLAGWRITE_ALL));
il.AddInstruction(il.Goto(doneLabel));
il.MarkLabel(falseLabel);
AssignEvexDfv(xedd, il);
il.MarkLabel(doneLabel);
}
static void ConditionalTest(const int64_t addr, const xed_decoded_inst_t* xedd, LowLevelILFunction& il, uint32_t size, ExprId cond)
{
LowLevelILLabel trueLabel, falseLabel, doneLabel;
il.AddInstruction(
il.If(
cond,
trueLabel, falseLabel
)
);
il.MarkLabel(trueLabel);
il.AddInstruction(
il.And(size,
ReadILOperand(il, xedd, addr, 0, 0),
ReadILOperand(il, xedd, addr, 1, 1),
IL_FLAGWRITE_ALL));
il.AddInstruction(il.Goto(doneLabel));
il.MarkLabel(falseLabel);
AssignEvexDfv(xedd, il);
il.MarkLabel(doneLabel);
}
bool GetLowLevelILForInstruction(Architecture* arch, const uint64_t addr, LowLevelILFunction& il, const xed_decoded_inst_t* const xedd)
{
LowLevelILLabel trueLabel, falseLabel, doneLabel, dirFlagSet, dirFlagClear, dirFlagDone, startLabel;
LowLevelILLabel trueLabel2, falseLabel2;
const xed_iclass_enum_t xedd_iClass = xed_decoded_inst_get_iclass(xedd);
const xed_iform_enum_t xedd_iForm = xed_decoded_inst_get_iform_enum(xedd);
const xed_inst_t* const xi = xed_decoded_inst_inst(xedd);
// const xed_operand_values_t* const ov = xed_decoded_inst_operands_const(xedd);
const unsigned short instLen = xed_decoded_inst_get_length(xedd);
// mode_bits can be used to determine whether the current instruciton is in 16/32/64 bit mode
const size_t mode_bits = xed_decoded_inst_get_machine_mode_bits(xedd);
const size_t addrSize = mode_bits / 8;
const unsigned short opOneLen = xed_decoded_inst_operand_length_bits(xedd, 0) / 8;
const unsigned short opTwoLen = xed_decoded_inst_operand_length_bits(xedd, 1) / 8;
[[maybe_unused]] const unsigned short
opTreLen = xed_decoded_inst_operand_length_bits(xedd, 2) / 8;
const xed_operand_t* const opOne = xed_inst_operand(xi, 0);
const xed_operand_t* const opTwo = xed_inst_operand(xi, 1);
// this is problematic as operand three may or may not exist at all
// latest version of xed will complain about this
const xed_operand_t* const opTre = xed_inst_operand(xi, 2);
const xed_operand_enum_t opOne_name = xed_operand_name(opOne);
const xed_operand_enum_t opTwo_name = xed_operand_name(opTwo);
const xed_operand_enum_t opTre_name = xed_operand_name(opTre);
const xed_reg_enum_t regOne = xed_decoded_inst_get_reg(xedd, opOne_name);
const xed_reg_enum_t regTwo = xed_decoded_inst_get_reg(xedd, opTwo_name);
// const xed_reg_enum_t regTre = xed_decoded_inst_get_reg(xedd, opTre_name);
// const xed_reg_enum_t baseReg1 = xed_decoded_inst_get_base_reg(xedd, 0);
// const xed_reg_enum_t baseReg2 = xed_decoded_inst_get_base_reg(xedd, 1);
const xed_reg_enum_t segReg1 = xed_decoded_inst_get_seg_reg (xedd, 0);
// const xed_reg_enum_t segReg2 = xed_decoded_inst_get_seg_reg (xedd, 1);
const uint64_t immediateOne = xed_decoded_inst_get_unsigned_immediate(xedd);
const int64_t branchDestination = xed_decoded_inst_get_branch_displacement(xedd) + addr + instLen;
// Handle the NDD APX isa extension, this will require some classes to be lifted with an alternate operand location.
const bool newDataDestination = xed_decoded_inst_get_attribute(xedd, XED_ATTRIBUTE_APX_NDD);
const bool zeroUpper = xed_decoded_inst_is_apx_zu(xedd);
const bool noFlags = xed_decoded_inst_get_attribute(xedd, XED_ATTRIBUTE_APX_NF);
auto LiftAsIntrinsic = [& il, xi, xedd, addr, xedd_iForm] () mutable {
typedef struct
{
uint32_t index;
size_t width;
} MemoryOperandWriteInfo;
vector<RegisterOrFlag> outputs = {};
vector<ExprId> parameters = {};
size_t noperands = xed_inst_noperands(xi);
vector<MemoryOperandWriteInfo> memoryOperandWrites = {};
size_t numTempRegUsed = 0;
for (uint32_t i = 0; i < noperands; i++)
{
const xed_operand_t* op = xed_inst_operand(xi, i);
xed_operand_enum_t op_name = xed_operand_name(op);
if (xed_operand_written(op))
{
if (xed_operand_is_register(op_name) || xed_operand_is_memory_addressing_register(op_name))
{
xed_reg_enum_t r = xed_decoded_inst_get_reg(xedd, op_name);
outputs.push_back(RegisterOrFlag::Register(r));
}
else
{
// The intrinsic system can only accept registers or flags as outputs,
// since it might be strange to write to an arbitrary ExprId.
// In order to handle intrinsics that write to memory, we create a temp IL register and
// later generate another il intrustion to write the register value to the memory
// An example of this is:
// vmovss dword [eax], k1, xmm0 (bytes: 6762f17e091100)
// which lifts to:
// temp0 = _mm_mask_store_ss(k1, xmm0)
// [eax.q].d = temp0.d
// Note, however, it is quite rare for an intrinsic to write to memory
size_t operandWidth = (xed_decoded_inst_operand_length_bits(xedd, i) + 7) >> 3;
memoryOperandWrites.push_back({i, operandWidth});
outputs.push_back(RegisterOrFlag::Register(LLIL_TEMP(numTempRegUsed)));
numTempRegUsed++;
}
}
if (xed_operand_read(op))
{
if (xed_operand_is_register(op_name) || xed_operand_is_memory_addressing_register(op_name))
{
// XED includes some things that are not actual registers in
// xed_reg_enum_t. We'll just omit those special cases here.
xed_reg_enum_t r = xed_decoded_inst_get_reg(xedd, op_name);
switch(r)
{
case XED_REG_INVALID:
case XED_REG_MSRS:
case XED_REG_STACKPUSH:
case XED_REG_STACKPOP:
case XED_REG_ERROR:
case XED_REG_LAST:
continue;
default:
break;
}
}
parameters.push_back(ReadILOperand(il, xedd, addr, i, i));
}
}
X86_INTRINSIC intrinsic = (X86_INTRINSIC)(xedd_iForm + 1000);
il.AddInstruction(il.Intrinsic(outputs, intrinsic, parameters));
// Generate IL instruction for memory writes
for (size_t i = 0; i < memoryOperandWrites.size(); i++)
{
uint32_t operand = memoryOperandWrites[i].index;
size_t openradWidth = memoryOperandWrites[i].width;
il.AddInstruction(WriteILOperand(il, xedd, addr, operand, operand,
il.Register(openradWidth, LLIL_TEMP(i))));
}
};
switch (xedd_iClass)
{
case XED_ICLASS_ADC_LOCK: // TODO: Add Lock construct
case XED_ICLASS_ADC:
{
ExprId left, right;
size_t opSize = opOneLen;
if (!newDataDestination)
{
// nothing special
left = ReadILOperand(il, xedd, addr, 0, 0);
right = ReadILOperand(il, xedd, addr, 1, 1);
}
else
{
// new data destination
left = ReadILOperand(il, xedd, addr, 1, 1);
right = ReadILOperand(il, xedd, addr, 2, 2);
opSize = opTwoLen;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.AddCarry(opSize, left, right,
il.Flag(IL_FLAG_C), IL_FLAGWRITE_ALL)));
break;
}
case XED_ICLASS_ADCX:
{
ExprId left, right;
size_t opSize = opOneLen;
if (!newDataDestination)
{
// nothing special
left = ReadILOperand(il, xedd, addr, 0, 0);
right = ReadILOperand(il, xedd, addr, 1, 1);
}
else
{
// new data destination
left = ReadILOperand(il, xedd, addr, 1, 1);
right = ReadILOperand(il, xedd, addr, 2, 2);
opSize = opTwoLen;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.AddCarry(opSize, left, right,
il.Flag(IL_FLAG_C), IL_FLAG_C)));
break;
}
case XED_ICLASS_ADOX:
{
ExprId left, right;
size_t opSize = opOneLen;
if (!newDataDestination)
{
// nothing special
left = ReadILOperand(il, xedd, addr, 0, 0);
right = ReadILOperand(il, xedd, addr, 1, 1);
}
else
{
// new data destination
left = ReadILOperand(il, xedd, addr, 1, 1);
right = ReadILOperand(il, xedd, addr, 2, 2);
opSize = opTwoLen;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.AddCarry(opSize, left, right,
il.Flag(IL_FLAG_O), IL_FLAG_O)));
break;
}
case XED_ICLASS_ADD_LOCK: // TODO: Add Lock construct
case XED_ICLASS_ADD:
{
ExprId left, right;
size_t opSize = opOneLen;
if (!newDataDestination)
{
// nothing special
left = ReadILOperand(il, xedd, addr, 0, 0);
right = ReadILOperand(il, xedd, addr, 1, 1);
}
else
{
// new data destination
left = ReadILOperand(il, xedd, addr, 1, 1);
right = ReadILOperand(il, xedd, addr, 2, 2);
opSize = opTwoLen;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Add(opSize, left, right,
noFlags ? 0 : IL_FLAGWRITE_ALL)));
break;
}
case XED_ICLASS_AND_LOCK: // TODO: Add Lock construct
case XED_ICLASS_AND:
{
ExprId left, right;
size_t opSize = opOneLen;
if (!newDataDestination)
{
// nothing special
left = ReadILOperand(il, xedd, addr, 0, 0);
right = ReadILOperand(il, xedd, addr, 1, 1);
}
else
{
// new data destination
left = ReadILOperand(il, xedd, addr, 1, 1);
right = ReadILOperand(il, xedd, addr, 2, 2);
opSize = opTwoLen;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.And(opSize, left, right,
noFlags ? 0 : IL_FLAGWRITE_ALL)));
break;
}
case XED_ICLASS_PAND:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.And(opOneLen,
ReadILOperand(il, xedd, addr, 0, 0),
ReadILOperand(il, xedd, addr, 1, 1),
0))); // PAND doesn't modify any flag.
break;
case XED_ICLASS_VPAND:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.And(opOneLen,
ReadILOperand(il, xedd, addr, 1, 1),
ReadILOperand(il, xedd, addr, 2, 2),
0))); // VPAND doesn't modify any flag
break;
case XED_ICLASS_ANDN:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.And(opOneLen,
ReadILOperand(il, xedd, addr, 0, 0),
il.Not(
opTwoLen,
ReadILOperand(il, xedd, addr, 1, 1)
),
IL_FLAGWRITE_ALL)));
break;
case XED_ICLASS_PANDN:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.And(opOneLen,
ReadILOperand(il, xedd, addr, 0, 0),
il.Not(
opTwoLen,
ReadILOperand(il, xedd, addr, 1, 1)
),
0))); // Does not affect flags
break;
case XED_ICLASS_VPANDN:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.And(opOneLen,
ReadILOperand(il, xedd, addr, 1, 1),
il.Not(
opTwoLen,
ReadILOperand(il, xedd, addr, 2, 2)
),
IL_FLAGWRITE_ALL)));
break;
case XED_ICLASS_BT:
il.AddInstruction(il.SetFlag(IL_FLAG_C,
il.TestBit(opOneLen,
ReadILOperand(il, xedd, addr, 0, 0),
ReadILOperand(il, xedd, addr, 1, 1))));
break;
case XED_ICLASS_BTC_LOCK:
case XED_ICLASS_BTC:
// TODO: Handle lock prefix
il.AddInstruction(il.SetFlag(IL_FLAG_C,
il.TestBit(opOneLen,
ReadILOperand(il, xedd, addr, 0, 0),
ReadILOperand(il, xedd, addr, 1, 1))));
// Complement the bit specified by operand[1] in operand[0]
// operand[0] = operand[0] ^ (1 << operand[1])
// or in the case operand[1] is a register
// operand[0] = operand[0] ^ (1 << (operand[1] % operand[0].size))
if (opTwo_name == XED_OPERAND_IMM0)
{
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.Xor(opOneLen,
ReadILOperand(il, xedd, addr, 0, 0),
il.ShiftLeft(opOneLen,
il.Const(opOneLen, 1),
ReadILOperand(il, xedd, addr, 1, 1)))));
}
else
{
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.Xor(opOneLen,
ReadILOperand(il, xedd, addr, 0, 0),
il.ShiftLeft(opOneLen,
il.Const(opOneLen, 1),
il.ModUnsigned(opTwoLen,
ReadILOperand(il, xedd, addr, 1, 1),
il.Const(1, opOneLen * 8))))));
}
break;
case XED_ICLASS_BTR_LOCK:
case XED_ICLASS_BTR:
// TODO: Handle lock prefix
il.AddInstruction(il.SetFlag(IL_FLAG_C,
il.TestBit(opOneLen,
ReadILOperand(il, xedd, addr, 0, 0),
ReadILOperand(il, xedd, addr, 1, 1))));
// Reset the bit specified by operand[1] in operand[0]
// operand[0] = operand[0] & ~(1 << operand[1])
// or in the case operand[1] is a register
// operand[0] = operand[0] & ~(1 << (operand[1] % operand[0].size))
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.And(opOneLen,
ReadILOperand(il, xedd, addr, 0, 0),
il.Not(opOneLen,
il.ShiftLeft(opOneLen,
il.Const(opOneLen, 1),
(opTwo_name == XED_OPERAND_IMM0) ?
ReadILOperand(il, xedd, addr, 1, 1) :
il.ModUnsigned(opTwoLen,
ReadILOperand(il, xedd, addr, 1, 1),
il.Const(1, opOneLen * 8)))))));
break;
case XED_ICLASS_BTS_LOCK:
case XED_ICLASS_BTS:
// TODO: Handle lock prefix
il.AddInstruction(il.SetFlag(IL_FLAG_C,
il.TestBit(opOneLen,
ReadILOperand(il, xedd, addr, 0, 0),
ReadILOperand(il, xedd, addr, 1, 1))));
// Complement the bit specified by operand[1] in operand[0]
// operand[0] = operand[0] | (1 << operand[1])
// or in the case operand[1] is a register
// operand[0] = operand[0] | (1 << (operand[1] % operand[0].size))
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.Or(opOneLen,
ReadILOperand(il, xedd, addr, 0, 0),
il.ShiftLeft(opOneLen,
il.Const(opOneLen, 1),
(opTwo_name == XED_OPERAND_IMM0) ?
ReadILOperand(il, xedd, addr, 1, 1) :
il.ModUnsigned(opTwoLen,
ReadILOperand(il, xedd, addr, 1, 1),
il.Const(1, opOneLen * 8))))));
break;
case XED_ICLASS_ADDSS:
{
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatAdd(4,
ReadFloatILOperand(il, xedd, addr, 0, 0, 4),
ReadFloatILOperand(il, xedd, addr, 1, 1, 4)
)));
break;
}
case XED_ICLASS_VADDSS:
{
if (xed_classify_avx512(xedd))
{
LiftAsIntrinsic();
break;
}
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatAdd(4,
ReadFloatILOperand(il, xedd, addr, 1, 1, 4),
ReadFloatILOperand(il, xedd, addr, 2, 2, 4)
)));
break;
}
case XED_ICLASS_ADDSD:
{
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatAdd(8,
ReadFloatILOperand(il, xedd, addr, 0, 0, 8),
ReadFloatILOperand(il, xedd, addr, 1, 1, 8)
)));
break;
}
case XED_ICLASS_VADDSD:
{
if (xed_classify_avx512(xedd))
{
LiftAsIntrinsic();
break;
}
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatAdd(8,
ReadFloatILOperand(il, xedd, addr, 1, 1, 8),
ReadFloatILOperand(il, xedd, addr, 2, 2, 8)
)));
break;
}
case XED_ICLASS_SUBSS:
{
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatSub(4,
ReadFloatILOperand(il, xedd, addr, 0, 0, 4),
ReadFloatILOperand(il, xedd, addr, 1, 1, 4)
)));
break;
}
case XED_ICLASS_VSUBSS:
{
if (xed_classify_avx512(xedd))
{
LiftAsIntrinsic();
break;
}
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatSub(4,
ReadFloatILOperand(il, xedd, addr, 1, 1, 4),
ReadFloatILOperand(il, xedd, addr, 2, 2, 4)
)));
break;
}
case XED_ICLASS_SUBSD:
{
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatSub(8,
ReadFloatILOperand(il, xedd, addr, 0, 0, 8),
ReadFloatILOperand(il, xedd, addr, 1, 1, 8)
)));
break;
}
case XED_ICLASS_VSUBSD:
{
if (xed_classify_avx512(xedd))
{
LiftAsIntrinsic();
break;
}
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatSub(8,
ReadFloatILOperand(il, xedd, addr, 1, 1, 8),
ReadFloatILOperand(il, xedd, addr, 2, 2, 8
)))
);
break;
}
case XED_ICLASS_MULSS:
{
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatMult(4,
ReadFloatILOperand(il, xedd, addr, 0, 0, 4),
ReadFloatILOperand(il, xedd, addr, 1, 1, 4)
)));
break;
}
case XED_ICLASS_VMULSS:
{
if (xed_classify_avx512(xedd))
{
LiftAsIntrinsic();
break;
}
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatMult(4,
ReadFloatILOperand(il, xedd, addr, 1, 1, 4),
ReadFloatILOperand(il, xedd, addr, 2, 2, 4)
)));
break;
}
case XED_ICLASS_MULSD:
{
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatMult(8,
ReadFloatILOperand(il, xedd, addr, 0, 0, 8),
ReadFloatILOperand(il, xedd, addr, 1, 1, 8)
)));
break;
}
case XED_ICLASS_VMULSD:
{
if (xed_classify_avx512(xedd))
{
LiftAsIntrinsic();
break;
}
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatMult(8,
ReadFloatILOperand(il, xedd, addr, 1, 1, 8),
ReadFloatILOperand(il, xedd, addr, 2, 2, 8)
)));
break;
}
case XED_ICLASS_DIVSS:
{
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatDiv(4,
ReadFloatILOperand(il, xedd, addr, 0, 0, 4),
ReadFloatILOperand(il, xedd, addr, 1, 1, 4)
)));
break;
}
case XED_ICLASS_VDIVSS:
{
if (xed_classify_avx512(xedd))
{
LiftAsIntrinsic();
break;
}
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatDiv(4,
ReadFloatILOperand(il, xedd, addr, 1, 1, 4),
ReadFloatILOperand(il, xedd, addr, 2, 2, 4)
)));
break;
}
case XED_ICLASS_DIVSD:
{
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatDiv(8,
ReadFloatILOperand(il, xedd, addr, 0, 0, 8),
ReadFloatILOperand(il, xedd, addr, 1, 1, 8)
)));
break;
}
case XED_ICLASS_VDIVSD:
{
if (xed_classify_avx512(xedd))
{
LiftAsIntrinsic();
break;
}
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatDiv(8,
ReadFloatILOperand(il, xedd, addr, 1, 1, 8),
ReadFloatILOperand(il, xedd, addr, 2, 2, 8)
)));
break;
}
case XED_ICLASS_LDMXCSR:
case XED_ICLASS_VLDMXCSR:
{
il.AddInstruction(
il.SetRegister(4, XED_REG_MXCSR,
ReadILOperand(il, xedd, addr, 0, 0)
)
);
break;
}
case XED_ICLASS_STMXCSR:
case XED_ICLASS_VSTMXCSR:
{
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Register(4, XED_REG_MXCSR)
)
);
break;
}
case XED_ICLASS_CVTSI2SS:
{
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.IntToFloat(4,
ReadILOperand(il, xedd, addr, 1, 1)
)
)
);
break;
}
case XED_ICLASS_VCVTSI2SS:
{
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
ReadILOperand(il, xedd, addr, 1, 1)
)
);
il.AddInstruction(
il.SetRegister(4, regOne,
il.IntToFloat(4,
ReadILOperand(il, xedd, addr, 2, 2)
)
)
);
break;
}
case XED_ICLASS_CVTSI2SD:
{
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.IntToFloat(8,
ReadILOperand(il, xedd, addr, 1, 1)
)
)
);
break;
}
case XED_ICLASS_VCVTSI2SD:
{
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
ReadILOperand(il, xedd, addr, 1, 1)
)
);
il.AddInstruction(
il.SetRegister(8, regOne,
il.IntToFloat(8,
ReadILOperand(il, xedd, addr, 2, 2)
)
)
);
break;
}
case XED_ICLASS_CVTSS2SI:
case XED_ICLASS_VCVTSS2SI:
case XED_ICLASS_CVTSD2SI:
case XED_ICLASS_VCVTSD2SI:
{
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.FloatToInt(opOneLen,
ReadFloatILOperand(il, xedd, addr, 1, 1)
)
)
);
break;
}
case XED_ICLASS_CVTTSD2SI:
case XED_ICLASS_CVTTSS2SI:
case XED_ICLASS_VCVTTSD2SI:
case XED_ICLASS_VCVTTSS2SI:
{
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.FloatToInt(opOneLen,
ReadFloatILOperand(il, xedd, addr, 1, 1)
)
)
);
break;
}
case XED_ICLASS_CVTSS2SD:
case XED_ICLASS_CVTSD2SS:
{
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.FloatConvert(opOneLen,
ReadFloatILOperand(il, xedd, addr, 1, 1)
)
)
);
break;
}
case XED_ICLASS_VCVTSS2SD:
{
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
ReadILOperand(il, xedd, addr, 1, 1)
)
);
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.FloatConvert(8,
ReadFloatILOperand(il, xedd, addr, 2, 2)
),
8
)
);
break;
}
case XED_ICLASS_VCVTSD2SS:
{
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
ReadILOperand(il, xedd, addr, 1, 1)
)
);
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.FloatConvert(4,
ReadFloatILOperand(il, xedd, addr, 2, 2)
),
4
)
);
break;
}
case XED_ICLASS_XLAT:
{
il.AddInstruction(
il.SetRegister(
1, XED_REG_AL,
// the operand 0 is the MEM being read
ReadILOperand(il, xedd, addr, 0, 0)
)
);
break;
}
// This is a workaround to stop the type propagator from
// making integers of size 576, which is mostly wrong
// However, if we do implement these, it will in turn
// cause problems with parameter resolution
// So as for now, unimplemented is the best solution
case XED_ICLASS_XRSTOR:
case XED_ICLASS_XRSTOR64:
case XED_ICLASS_XRSTORS:
case XED_ICLASS_XRSTORS64:
case XED_ICLASS_XSAVE:
case XED_ICLASS_XSAVE64:
case XED_ICLASS_XSAVEC:
case XED_ICLASS_XSAVEC64:
case XED_ICLASS_XSAVEOPT:
case XED_ICLASS_XSAVEOPT64:
case XED_ICLASS_XSAVES:
case XED_ICLASS_XSAVES64:
il.AddInstruction(il.Unimplemented());
break;
case XED_ICLASS_CALL_NEAR:
case XED_ICLASS_CALL_FAR:
if (
((xedd_iForm == XED_IFORM_CALL_FAR_MEMp2) || (xedd_iForm == XED_IFORM_CALL_NEAR_MEMv)) &&
(immediateOne == 0x10) &&
(opOneLen == 4) &&
(segReg1 == XED_REG_GS))
{
// Linux indirect system call (call dword [gs:0x10])
// TODO: Implement this as a platform-specific lifting extension when such a thing
// is possible
il.AddInstruction(il.SystemCall());
break;
}
// Turn 'call next' into a push
if (((uint64_t)branchDestination == addr+instLen) && ((xedd_iForm == XED_IFORM_CALL_NEAR_RELBRz) || (xedd_iForm == XED_IFORM_CALL_NEAR_RELBRd)))
{
il.AddInstruction(
il.Push(addrSize,
il.ConstPointer(addrSize, addr + instLen)));
}
else
{
il.AddInstruction(
il.Call(
ReadILOperand(il, xedd, addr, 0, 0)));
}
break;
case XED_ICLASS_CBW:
il.AddInstruction(il.SetRegister(2, XED_REG_AX, il.SignExtend(2, il.Register(1, XED_REG_AL))));
break;
case XED_ICLASS_CDQ:
il.AddInstruction(il.SetRegisterSplit(4, XED_REG_EDX, XED_REG_EAX, il.SignExtend(8, il.Register(4, XED_REG_EAX))));
break;
case XED_ICLASS_CLC:
il.AddInstruction(il.SetFlag(IL_FLAG_C, il.Const(1, 0)));
break;
case XED_ICLASS_CLD:
il.AddInstruction(il.SetFlag(IL_FLAG_D, il.Const(1, 0)));
break;
case XED_ICLASS_CMC:
il.AddInstruction(il.SetFlag(IL_FLAG_C, il.Xor(1, il.Flag(IL_FLAG_C), il.Const(1, 1))));
break;
case XED_ICLASS_CMOVO:
case XED_ICLASS_CFCMOVO:
CMovFlagCond(addr, xedd, il, LLFC_O);
break;
case XED_ICLASS_CMOVNO:
case XED_ICLASS_CFCMOVNO:
CMovFlagCond(addr, xedd, il, LLFC_NO);
break;
case XED_ICLASS_CMOVB:
case XED_ICLASS_FCMOVB:
case XED_ICLASS_CFCMOVB:
CMovFlagGroup(addr, xedd, il, IL_FLAG_GROUP_LT);
break;
case XED_ICLASS_CMOVNB:
case XED_ICLASS_FCMOVNB:
case XED_ICLASS_CFCMOVNB:
CMovFlagGroup(addr, xedd, il, IL_FLAG_GROUP_GE);
break;
case XED_ICLASS_CMOVZ:
case XED_ICLASS_FCMOVE:
case XED_ICLASS_CFCMOVZ:
CMovFlagGroup(addr, xedd, il, IL_FLAG_GROUP_E);
break;
case XED_ICLASS_CMOVNZ:
case XED_ICLASS_FCMOVNE:
case XED_ICLASS_CFCMOVNZ:
CMovFlagGroup(addr, xedd, il, IL_FLAG_GROUP_NE);
break;
case XED_ICLASS_CMOVBE:
case XED_ICLASS_FCMOVBE:
case XED_ICLASS_CFCMOVBE:
CMovFlagGroup(addr, xedd, il, IL_FLAG_GROUP_LE);
break;
case XED_ICLASS_CMOVNBE:
case XED_ICLASS_FCMOVNBE:
case XED_ICLASS_CFCMOVNBE:
CMovFlagGroup(addr, xedd, il, IL_FLAG_GROUP_GT);
break;
case XED_ICLASS_CMOVS:
case XED_ICLASS_CFCMOVS:
CMovFlagCond(addr, xedd, il, LLFC_NEG);
break;
case XED_ICLASS_CMOVNS:
case XED_ICLASS_CFCMOVNS:
CMovFlagCond(addr, xedd, il, LLFC_POS);
break;
case XED_ICLASS_CMOVP:
case XED_ICLASS_FCMOVU:
case XED_ICLASS_CFCMOVP:
CMovFlagGroup(addr, xedd, il, IL_FLAG_GROUP_PE);
break;
case XED_ICLASS_CMOVNP:
case XED_ICLASS_FCMOVNU:
case XED_ICLASS_CFCMOVNP:
CMovFlagGroup(addr, xedd, il, IL_FLAG_GROUP_PO);
break;
case XED_ICLASS_CMOVL:
case XED_ICLASS_CFCMOVL:
CMovFlagCond(addr, xedd, il, LLFC_SLT);
break;
case XED_ICLASS_CMOVNL:
case XED_ICLASS_CFCMOVNL:
CMovFlagCond(addr, xedd, il, LLFC_SGE);
break;
case XED_ICLASS_CMOVLE:
case XED_ICLASS_CFCMOVLE:
CMovFlagCond(addr, xedd, il, LLFC_SLE);
break;
case XED_ICLASS_CMOVNLE:
case XED_ICLASS_CFCMOVNLE:
CMovFlagCond(addr, xedd, il, LLFC_SGT);
break;
case XED_ICLASS_CMP:
case XED_ICLASS_CCMPT:
il.AddInstruction(
il.Sub(opOneLen,
ReadILOperand(il, xedd, addr, 0, 0),
ReadILOperand(il, xedd, addr, 1, 1),
IL_FLAGWRITE_ALL));
break;
case XED_ICLASS_CCMPO:
ConditionalCmp(addr, xedd, il, opOneLen, il.FlagCondition(LLFC_O));
break;
case XED_ICLASS_CCMPNO:
ConditionalCmp(addr, xedd, il, opOneLen, il.FlagCondition(LLFC_NO));
break;
case XED_ICLASS_CCMPB:
ConditionalCmp(addr, xedd, il, opOneLen, il.FlagGroup(IL_FLAG_GROUP_LT));
break;
case XED_ICLASS_CCMPNB:
ConditionalCmp(addr, xedd, il, opOneLen, il.FlagGroup(IL_FLAG_GROUP_GE));
break;
case XED_ICLASS_CCMPZ:
ConditionalCmp(addr, xedd, il, opOneLen, il.FlagGroup(IL_FLAG_GROUP_E));
break;
case XED_ICLASS_CCMPNZ:
ConditionalCmp(addr, xedd, il, opOneLen, il.FlagGroup(IL_FLAG_GROUP_NE));
break;
case XED_ICLASS_CCMPBE:
ConditionalCmp(addr, xedd, il, opOneLen, il.FlagGroup(IL_FLAG_GROUP_LE));
break;
case XED_ICLASS_CCMPNBE:
ConditionalCmp(addr, xedd, il, opOneLen, il.FlagGroup(IL_FLAG_GROUP_GT));
break;
case XED_ICLASS_CCMPS:
ConditionalCmp(addr, xedd, il, opOneLen, il.FlagCondition(LLFC_NEG));
break;
case XED_ICLASS_CCMPNS:
ConditionalCmp(addr, xedd, il, opOneLen, il.FlagCondition(LLFC_POS));
break;
case XED_ICLASS_CCMPL:
ConditionalCmp(addr, xedd, il, opOneLen, il.FlagCondition(LLFC_SLT));
break;
case XED_ICLASS_CCMPNL:
ConditionalCmp(addr, xedd, il, opOneLen, il.FlagCondition(LLFC_SLT));
break;
case XED_ICLASS_CCMPLE:
ConditionalCmp(addr, xedd, il, opOneLen, il.FlagCondition(LLFC_SLE));
break;
case XED_ICLASS_CCMPNLE:
ConditionalCmp(addr, xedd, il, opOneLen, il.FlagCondition(LLFC_SGT));
break;
case XED_ICLASS_CCMPF:
AssignEvexDfv(xedd, il);
break;
case XED_ICLASS_REPE_CMPSQ:
case XED_ICLASS_REPNE_CMPSQ:
case XED_ICLASS_CMPSQ:
case XED_ICLASS_REPE_CMPSD:
case XED_ICLASS_REPNE_CMPSD:
case XED_ICLASS_CMPSD:
case XED_ICLASS_REPE_CMPSW:
case XED_ICLASS_REPNE_CMPSW:
case XED_ICLASS_CMPSW:
case XED_ICLASS_REPE_CMPSB:
case XED_ICLASS_REPNE_CMPSB:
case XED_ICLASS_CMPSB:
{
size_t argSize = 1;
uint32_t srcReg = addrSize == 4 ? XED_REG_ESI : XED_REG_RSI;
uint32_t dstReg = addrSize == 4 ? XED_REG_EDI : XED_REG_RDI;
switch (xedd_iClass)
{
case XED_ICLASS_REPE_CMPSB:
case XED_ICLASS_REPNE_CMPSB:
case XED_ICLASS_CMPSB:
argSize = 1;
break;
case XED_ICLASS_REPE_CMPSW:
case XED_ICLASS_REPNE_CMPSW:
case XED_ICLASS_CMPSW:
argSize = 2;
break;
case XED_ICLASS_REPE_CMPSD:
case XED_ICLASS_REPNE_CMPSD:
case XED_ICLASS_CMPSD:
argSize = 4;
break;
case XED_ICLASS_REPE_CMPSQ:
case XED_ICLASS_REPNE_CMPSQ:
case XED_ICLASS_CMPSQ:
argSize = 8;
break;
default:
break;
}
Repeat(xedd, il, [&] (){
DirFlagIf(il,
[&] ()
{
il.AddInstruction(il.Sub(argSize, il.Load(argSize, il.Register(addrSize, srcReg)), il.Load(argSize, il.Register(addrSize, dstReg)), IL_FLAGWRITE_ALL));
},
[&] () // Dirflag is 1
{
il.AddInstruction(il.SetRegister(addrSize, srcReg, il.Sub(addrSize, il.Register(addrSize, srcReg), il.Const(1, argSize))));
il.AddInstruction(il.SetRegister(addrSize, dstReg, il.Sub(addrSize, il.Register(addrSize, dstReg), il.Const(1, argSize))));
},
[&] () // Dirflag is 0
{
il.AddInstruction(il.SetRegister(addrSize, srcReg, il.Add(addrSize, il.Register(addrSize, srcReg), il.Const(1, argSize))));
il.AddInstruction(il.SetRegister(addrSize, dstReg, il.Add(addrSize, il.Register(addrSize, dstReg), il.Const(1, argSize))));
});
});
break;
}
case XED_ICLASS_PALIGNR:
{
// the immediate for palignr is smaller than 32
u_char nshiftBytes = (u_char)xed_decoded_inst_get_unsigned_immediate(xedd);
size_t nShiftBits = 8 * nshiftBytes;
size_t regSize = opOneLen;
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Or(regSize,
il.LogicalShiftRight(regSize, ReadILOperand(il, xedd, addr, 1, 1), il.Const(1, nShiftBits)),
il.ShiftLeft(regSize,
ReadILOperand(il, xedd, addr, 0, 0),
il.Const(1, 8 * (regSize - nshiftBytes))
)
)
)
);
break;
}
case XED_ICLASS_VPALIGNR:
{
if (xed_classify_avx512(xedd))
{
LiftAsIntrinsic();
break;
}
// the immediate for palignr is smaller than 32
u_char nshiftBytes = (u_char)xed_decoded_inst_get_unsigned_immediate(xedd);
size_t nShiftBits = 8 * nshiftBytes;
size_t regSize = opOneLen;
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Or(regSize,
il.LogicalShiftRight(regSize, ReadILOperand(il, xedd, addr, 2, 2), il.Const(1, nShiftBits)),
il.ShiftLeft(regSize,
ReadILOperand(il, xedd, addr, 1, 1),
il.Const(1, 8 * (regSize - nshiftBytes))
)
)
)
);
break;
}
case XED_ICLASS_CQO:
il.AddInstruction(il.SetRegisterSplit(8, XED_REG_RDX, XED_REG_RAX, il.SignExtend(16, il.Register(8, XED_REG_RAX))));
break;
case XED_ICLASS_CWD:
il.AddInstruction(il.SetRegisterSplit(2, XED_REG_DX, XED_REG_AX, il.SignExtend(4, il.Register(2, XED_REG_AX))));
break;
case XED_ICLASS_CWDE:
il.AddInstruction(il.SetRegister(4, XED_REG_EAX, il.SignExtend(4, il.Register(2, XED_REG_AX))));
break;
case XED_ICLASS_CDQE:
il.AddInstruction(il.SetRegister(8, XED_REG_RAX, il.SignExtend(8, il.Register(4, XED_REG_EAX))));
break;
case XED_ICLASS_DEC_LOCK: // TODO: Handle lock prefix
case XED_ICLASS_DEC:
{
ExprId src;
size_t opSize = opOneLen;
if (!newDataDestination)
{
// nothing special
src = ReadILOperand(il, xedd, addr, 0, 0);
}
else
{
// new data destination
src = ReadILOperand(il, xedd, addr, 1, 1);
opSize = opTwoLen;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Sub(opSize,
src,
il.Const(opOneLen, 1),
noFlags ? 0 : IL_FLAGWRITE_NOCARRY)
)
);
break;
}
case XED_ICLASS_DIV:
// TODO: flags set and handle noFlags?
il.AddInstruction(
il.SetRegister(opOneLen,
LLIL_TEMP(2),
ReadILOperand(il, xedd, addr, 0, 0)));
switch (opOneLen)
{
case 1:
il.AddInstruction(
il.SetRegister(1,
LLIL_TEMP(0),
il.DivDoublePrecUnsigned(1,
il.Register(2, XED_REG_AX),
il.Register(opOneLen, LLIL_TEMP(2)))));
il.AddInstruction(il.SetRegister(1,
LLIL_TEMP(1),
il.ModDoublePrecUnsigned(1,
il.Register(2, XED_REG_AX),
il.Register(opOneLen, LLIL_TEMP(2)))));
il.AddInstruction(
il.SetRegister(1,
XED_REG_AL,
il.Register(1, LLIL_TEMP(0))));
il.AddInstruction(
il.SetRegister(1,
XED_REG_AH,
il.Register(1, LLIL_TEMP(1))));
break;
case 2:
il.AddInstruction(
il.SetRegister(2,
LLIL_TEMP(0),
il.DivDoublePrecUnsigned(2,
il.RegisterSplit(2,
XED_REG_DX,
XED_REG_AX),
il.Register(opOneLen, LLIL_TEMP(2)))));
il.AddInstruction(
il.SetRegister(2,
LLIL_TEMP(1),
il.ModDoublePrecUnsigned(2,
il.RegisterSplit(2,
XED_REG_DX,
XED_REG_AX),
il.Register(opOneLen, LLIL_TEMP(2)))));
il.AddInstruction(
il.SetRegister(2,
XED_REG_AX,
il.Register(2, LLIL_TEMP(0))));
il.AddInstruction(
il.SetRegister(2,
XED_REG_DX,
il.Register(2, LLIL_TEMP(1))));
break;
case 4:
il.AddInstruction(
il.SetRegister(4,
LLIL_TEMP(0),
il.DivDoublePrecUnsigned(4,
il.RegisterSplit(4,
XED_REG_EDX,
XED_REG_EAX),
il.Register(opOneLen, LLIL_TEMP(2)))));
il.AddInstruction(
il.SetRegister(4,
LLIL_TEMP(1),
il.ModDoublePrecUnsigned(4,
il.RegisterSplit(4,
XED_REG_EDX,
XED_REG_EAX),
il.Register(opOneLen, LLIL_TEMP(2)))));
il.AddInstruction(
il.SetRegister(4,
XED_REG_EAX,
il.Register(4, LLIL_TEMP(0))));
il.AddInstruction(
il.SetRegister(4,
XED_REG_EDX,
il.Register(4, LLIL_TEMP(1))));
break;
case 8:
il.AddInstruction(il.SetRegister(8,
LLIL_TEMP(0),
il.DivDoublePrecUnsigned(8,
il.RegisterSplit(8,
XED_REG_RDX,
XED_REG_RAX),
il.Register(opOneLen, LLIL_TEMP(2)))));
il.AddInstruction(
il.SetRegister(8,
LLIL_TEMP(1),
il.ModDoublePrecUnsigned(8,
il.RegisterSplit(8,
XED_REG_RDX,
XED_REG_RAX),
il.Register(opOneLen, LLIL_TEMP(2)))));
il.AddInstruction(
il.SetRegister(8,
XED_REG_RAX,
il.Register(8, LLIL_TEMP(0))));
il.AddInstruction(
il.SetRegister(8,
XED_REG_RDX,
il.Register(8, LLIL_TEMP(1))));
break;
default:
il.AddInstruction(il.Undefined());
break;
}
break;
case XED_ICLASS_ENTER:
{
uint32_t baseReg = addrSize == 4 ? XED_REG_EBP : XED_REG_RBP;
uint32_t stackReg = addrSize == 4 ? XED_REG_ESP : XED_REG_RSP;
if (xed_decoded_inst_get_second_immediate(xedd) != 0)
{
il.AddInstruction(il.Unimplemented());
break;
}
il.AddInstruction(il.Push(addrSize, il.Register(addrSize, baseReg)));
il.AddInstruction(il.SetRegister(addrSize, baseReg, il.Register(addrSize, stackReg)));
if (immediateOne)
il.AddInstruction(il.SetRegister(addrSize, stackReg,
il.Sub(addrSize,
il.Register(addrSize, stackReg),
il.Const(2, immediateOne))));
break;
}
case XED_ICLASS_IDIV:
// TODO: flags set and handle noFlags?
switch (opOneLen)
{
case 1:
il.AddInstruction(il.SetRegister(1,
LLIL_TEMP(0),
il.DivDoublePrecSigned(1,
il.Register(2, XED_REG_AX),
ReadILOperand(il, xedd, addr, 0, 0))));
il.AddInstruction(il.SetRegister(1,
LLIL_TEMP(1),
il.ModDoublePrecSigned(1,
il.Register(2, XED_REG_AX),
ReadILOperand(il, xedd, addr, 0, 0))));
il.AddInstruction(
il.SetRegister(1,
XED_REG_AL,
il.Register(1, LLIL_TEMP(0))));
il.AddInstruction(
il.SetRegister(1,
XED_REG_AH,
il.Register(1, LLIL_TEMP(1))));
break;
case 2:
il.AddInstruction(
il.SetRegister(2,
LLIL_TEMP(0),
il.DivDoublePrecSigned(2,
il.RegisterSplit(2,
XED_REG_DX,
XED_REG_AX),
ReadILOperand(il, xedd, addr, 0, 0))));
il.AddInstruction(
il.SetRegister(2,
LLIL_TEMP(1),
il.ModDoublePrecSigned(2,
il.RegisterSplit(2,
XED_REG_DX,
XED_REG_AX),
ReadILOperand(il, xedd, addr, 0, 0))));
il.AddInstruction(
il.SetRegister(2,
XED_REG_AX,
il.Register(2, LLIL_TEMP(0))));
il.AddInstruction(
il.SetRegister(2,
XED_REG_DX,
il.Register(2, LLIL_TEMP(1))));
break;
case 4:
il.AddInstruction(il.SetRegister(4,
LLIL_TEMP(0),
il.DivDoublePrecSigned(4,
il.RegisterSplit(4,
XED_REG_EDX,
XED_REG_EAX),
ReadILOperand(il, xedd, addr, 0, 0))));
il.AddInstruction(
il.SetRegister(4,
LLIL_TEMP(1),
il.ModDoublePrecSigned(4,
il.RegisterSplit(4,
XED_REG_EDX,
XED_REG_EAX),
ReadILOperand(il, xedd, addr, 0, 0))));
il.AddInstruction(
il.SetRegister(4,
XED_REG_EAX,
il.Register(4, LLIL_TEMP(0))));
il.AddInstruction(
il.SetRegister(4,
XED_REG_EDX,
il.Register(4, LLIL_TEMP(1))));
break;
case 8:
il.AddInstruction(
il.SetRegister(8,
LLIL_TEMP(0),
il.DivDoublePrecSigned(8,
il.RegisterSplit(8,
XED_REG_RDX,
XED_REG_RAX),
ReadILOperand(il, xedd, addr, 0, 0))));
il.AddInstruction(
il.SetRegister(8,
LLIL_TEMP(1),
il.ModDoublePrecSigned(8,
il.RegisterSplit(8,
XED_REG_RDX,
XED_REG_RAX),
ReadILOperand(il, xedd, addr, 0, 0))));
il.AddInstruction(
il.SetRegister(8,
XED_REG_RAX,
il.Register(8, LLIL_TEMP(0))));
il.AddInstruction(
il.SetRegister(8,
XED_REG_RDX,
il.Register(8, LLIL_TEMP(1))));
break;
default:
il.AddInstruction(il.Undefined());
break;
}
break;
case XED_ICLASS_IMUL:
switch (xedd_iForm)
{
case XED_IFORM_IMUL_GPR8:
case XED_IFORM_IMUL_GPRv:
case XED_IFORM_IMUL_MEMb:
case XED_IFORM_IMUL_MEMv:
switch (opOneLen)
{
case 1:
il.AddInstruction(
il.SetRegister(2,
XED_REG_AX,
il.MultDoublePrecSigned(1,
il.Register(1, XED_REG_AL),
ReadILOperand(il, xedd, addr, 0, 0),
noFlags ? 0 : IL_FLAGWRITE_CO)));
break;
case 2:
il.AddInstruction(
il.SetRegisterSplit(2,
XED_REG_DX,
XED_REG_AX,
il.MultDoublePrecSigned(2,
il.Register(2, XED_REG_AX),
ReadILOperand(il, xedd, addr, 0, 0),
noFlags ? 0 : IL_FLAGWRITE_CO)));
break;
case 4:
il.AddInstruction(
il.SetRegisterSplit(4,
XED_REG_EDX,
XED_REG_EAX,
il.MultDoublePrecSigned(4,
il.Register(4, XED_REG_EAX),
ReadILOperand(il, xedd, addr, 0, 0),
noFlags ? 0 : IL_FLAGWRITE_CO)));
break;
case 8:
il.AddInstruction(
il.SetRegisterSplit(8,
XED_REG_RDX,
XED_REG_RAX,
il.MultDoublePrecSigned(8,
il.Register(8, XED_REG_RAX),
ReadILOperand(il, xedd, addr, 0, 0),
noFlags ? 0 : IL_FLAGWRITE_CO)));
break;
default:
il.AddInstruction(il.Undefined());
break;
}
break;
case XED_IFORM_IMUL_GPRv_GPRv:
case XED_IFORM_IMUL_GPRv_MEMv:
{
ExprId left, right;
if (!newDataDestination)
{
// nothing special
left = ReadILOperand(il, xedd, addr, 0, 0);
right = ReadILOperand(il, xedd, addr, 1, 1);
}
else
{
// new data destination
left = ReadILOperand(il, xedd, addr, 1, 1);
right = ReadILOperand(il, xedd, addr, 2, 2);
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Mult(opOneLen, left, right,
IL_FLAGWRITE_CO)));
break;
}
case XED_IFORM_IMUL_GPRv_GPRv_IMMb:
case XED_IFORM_IMUL_GPRv_GPRv_IMMz:
case XED_IFORM_IMUL_GPRv_MEMv_IMMb:
case XED_IFORM_IMUL_GPRv_MEMv_IMMz:
default:
{
ExprId value = il.Mult(opOneLen,
ReadILOperand(il, xedd, addr, 1, 1),
ReadILOperand(il, xedd, addr, 2, 2),
IL_FLAGWRITE_CO);
if (zeroUpper && (opOneLen == 1 || opOneLen == 2))
value = il.ZeroExtend(8, value);
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0, value));
}
}
break;
case XED_ICLASS_INC_LOCK: // TODO: Handle lock prefix
case XED_ICLASS_INC:
{
ExprId src;
size_t opSize = opOneLen;
if (!newDataDestination)
{
// nothing special
src = ReadILOperand(il, xedd, addr, 0, 0);
}
else
{
// new data destination
src = ReadILOperand(il, xedd, addr, 1, 1);
opSize = opTwoLen;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Add(opSize,
src,
il.Const(opSize, 1),
noFlags ? 0 : IL_FLAGWRITE_NOCARRY)));
break;
}
case XED_ICLASS_INT:
switch (immediateOne)
{
case 0x29:
il.AddInstruction(il.Trap(TRAP_GPF));
break;
case 0x80:
il.AddInstruction(il.SystemCall());
break;
default:
il.AddInstruction(il.Trap(immediateOne));
break;
}
break;
case XED_ICLASS_INT3:
il.AddInstruction(il.Breakpoint());
break;
case XED_ICLASS_JMP:
case XED_ICLASS_JMPABS:
if (opOne_name == XED_OPERAND_RELBR)
{
BNLowLevelILLabel* label = il.GetLabelForAddress(arch, branchDestination);
if (label)
il.AddInstruction(il.Goto(*label));
else
il.AddInstruction(il.Jump(il.ConstPointer(addrSize, branchDestination)));
}
else
il.AddInstruction(il.Jump(ReadILOperand(il, xedd, addr, 0, 0)));
return false;
case XED_ICLASS_JO:
ConditionalJump(arch, il, il.FlagCondition(LLFC_O), addrSize, branchDestination, addr + instLen);
return false;
case XED_ICLASS_JNO:
ConditionalJump(arch, il, il.FlagCondition(LLFC_NO), addrSize, branchDestination, addr + instLen);
return false;
case XED_ICLASS_JB:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_LT), addrSize, branchDestination, addr + instLen);
return false;
case XED_ICLASS_JNB:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_GE), addrSize, branchDestination, addr + instLen);
return false;
case XED_ICLASS_JZ:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_E), addrSize, branchDestination, addr + instLen);
return false;
case XED_ICLASS_JNZ:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_NE), addrSize, branchDestination, addr + instLen);
return false;
case XED_ICLASS_JBE:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_LE), addrSize, branchDestination, addr + instLen);
return false;
case XED_ICLASS_JNBE:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_GT), addrSize, branchDestination, addr + instLen);
return false;
case XED_ICLASS_JS:
ConditionalJump(arch, il, il.FlagCondition(LLFC_NEG), addrSize, branchDestination, addr + instLen);
return false;
case XED_ICLASS_JNS:
ConditionalJump(arch, il, il.FlagCondition(LLFC_POS), addrSize, branchDestination, addr + instLen);
return false;
case XED_ICLASS_JP:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_PE), addrSize, branchDestination, addr + instLen);
return false;
case XED_ICLASS_JNP:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_PO), addrSize, branchDestination, addr + instLen);
return false;
case XED_ICLASS_JL:
ConditionalJump(arch, il, il.FlagCondition(LLFC_SLT), addrSize, branchDestination, addr + instLen);
return false;
case XED_ICLASS_JNL:
ConditionalJump(arch, il, il.FlagCondition(LLFC_SGE), addrSize, branchDestination, addr + instLen);
return false;
case XED_ICLASS_JLE:
ConditionalJump(arch, il, il.FlagCondition(LLFC_SLE), addrSize, branchDestination, addr + instLen);
return false;
case XED_ICLASS_JNLE:
ConditionalJump(arch, il, il.FlagCondition(LLFC_SGT), addrSize, branchDestination, addr + instLen);
return false;
case XED_ICLASS_JCXZ:
ConditionalJump(arch, il, il.CompareEqual(2, il.Register(2, XED_REG_CX), il.Const(2, 0)), addrSize, branchDestination, addr + instLen);
return false;
case XED_ICLASS_JECXZ:
ConditionalJump(arch, il, il.CompareEqual(4, il.Register(4, XED_REG_ECX), il.Const(4, 0)), addrSize, branchDestination, addr + instLen);
return false;
case XED_ICLASS_JRCXZ:
ConditionalJump(arch, il, il.CompareEqual(8, il.Register(8, XED_REG_RCX), il.Const(8, 0)), addrSize, branchDestination, addr + instLen);
return false;
case XED_ICLASS_LAHF:
il.AddInstruction(il.SetRegister(1, XED_REG_AH,
il.Or(1, il.FlagBit(1, IL_FLAG_S, 7),
il.Or(1, il.FlagBit(1, IL_FLAG_Z, 6),
il.Or(1, il.FlagBit(1, IL_FLAG_A, 4),
il.Or(1, il.FlagBit(1, IL_FLAG_P, 2), il.FlagBit(1, IL_FLAG_C, 0)))))));
break;
case XED_ICLASS_LEAVE:
il.AddInstruction(
il.SetRegister(addrSize,
GetStackPointer(addrSize),
il.Register(addrSize, GetFramePointer(addrSize))));
il.AddInstruction(
il.SetRegister(addrSize,
GetFramePointer(addrSize),
il.Pop(addrSize)));
break;
case XED_ICLASS_LOOP:
if (addrSize == 4)
{
il.AddInstruction(il.SetRegister(4, XED_REG_ECX, il.Sub(4, il.Register(4, XED_REG_ECX), il.Const(4, 1))));
ConditionalJump(arch, il,
il.CompareNotEqual(4,
il.Register(4, XED_REG_ECX),
il.Const(4, 0)),
addrSize, branchDestination, addr + instLen);
}
else
{
il.AddInstruction(il.SetRegister(8, XED_REG_RCX, il.Sub(8, il.Register(8, XED_REG_RCX), il.Const(8, 1))));
ConditionalJump(arch, il,
il.CompareNotEqual(8,
il.Register(8, XED_REG_RCX),
il.Const(8, 0)),
addrSize, branchDestination, addr + instLen);
}
return false;
case XED_ICLASS_LOOPE:
if (addrSize == 4)
{
il.AddInstruction(il.SetRegister(4, XED_REG_ECX, il.Sub(4, il.Register(4, XED_REG_ECX), il.Const(4, 1))));
ConditionalJump(arch, il,
il.Or(0,
il.FlagGroup(IL_FLAG_GROUP_E),
il.CompareNotEqual(4,
il.Register(4, XED_REG_ECX),
il.Const(4, 0))),
addrSize, branchDestination, addr + instLen);
}
else
{
il.AddInstruction(il.SetRegister(8, XED_REG_RCX, il.Sub(8, il.Register(8, XED_REG_RCX), il.Const(8, 1))));
ConditionalJump(arch, il,
il.Or(0,
il.FlagGroup(IL_FLAG_GROUP_E),
il.CompareNotEqual(8,
il.Register(8, XED_REG_RCX),
il.Const(8, 0))),
addrSize, branchDestination, addr + instLen);
}
return false;
case XED_ICLASS_LOOPNE:
if (addrSize == 4)
{
il.AddInstruction(il.SetRegister(4, XED_REG_ECX, il.Sub(4, il.Register(4, XED_REG_ECX), il.Const(4, 1))));
ConditionalJump(arch, il,
il.And(0,
il.FlagGroup(IL_FLAG_GROUP_NE),
il.CompareNotEqual(4,
il.Register(4, XED_REG_ECX),
il.Const(4, 0))),
addrSize, branchDestination, addr + instLen);
}
else
{
il.AddInstruction(il.SetRegister(8, XED_REG_RCX, il.Sub(8, il.Register(8, XED_REG_RCX), il.Const(8, 1))));
ConditionalJump(arch, il,
il.And(0,
il.FlagGroup(IL_FLAG_GROUP_NE),
il.CompareNotEqual(8,
il.Register(8, XED_REG_RCX),
il.Const(8, 0))),
addrSize, branchDestination, addr + instLen);
}
return false;
case XED_ICLASS_LEA:
if (opOneLen != opTwoLen)
{
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.LowPart(opOneLen,
GetILOperandMemoryAddress(il, xedd, addr, 1, 1))));
}
else
{
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
GetILOperandMemoryAddress(il, xedd, addr, 1, 1)));
}
break;
case XED_ICLASS_REP_LODSB:
case XED_ICLASS_REP_LODSD:
case XED_ICLASS_REP_LODSQ:
case XED_ICLASS_REP_LODSW:
case XED_ICLASS_LODSB:
case XED_ICLASS_LODSW:
case XED_ICLASS_LODSD:
case XED_ICLASS_LODSQ:
{
size_t loadSize;
uint32_t dstReg;
uint32_t srcReg = addrSize == 4 ? XED_REG_ESI : XED_REG_RSI;
switch (xedd_iClass)
{
case XED_ICLASS_LODSW:
case XED_ICLASS_REP_LODSW:
loadSize = 2; dstReg = XED_REG_AX; break;
case XED_ICLASS_LODSD:
case XED_ICLASS_REP_LODSD:
loadSize = 4; dstReg = XED_REG_EAX; break;
case XED_ICLASS_LODSQ:
case XED_ICLASS_REP_LODSQ:
loadSize = 8; dstReg = XED_REG_RAX; break;
case XED_ICLASS_REP_LODSB:
default:
loadSize = 1; dstReg = XED_REG_AL;
}
Repeat(xedd, il, [&] (){
DirFlagIf(il,
[&] ()
{
il.AddInstruction(il.SetRegister(loadSize, dstReg, il.Load(loadSize, il.Register(addrSize, srcReg))));
},
[&] () // Dirflag is 1
{
il.AddInstruction(il.SetRegister(addrSize, srcReg, il.Sub(addrSize, il.Register(addrSize, srcReg), il.Const(1, loadSize))));
},
[&] () // Dirflag is 0
{
il.AddInstruction(il.SetRegister(addrSize, srcReg, il.Add(addrSize, il.Register(addrSize, srcReg), il.Const(1, loadSize))));
});
});
break;
}
case XED_ICLASS_MOV:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
ReadILOperand(il, xedd, addr, 1, 1)));
break;
case XED_ICLASS_MOVD:
case XED_ICLASS_MOVQ:
case XED_ICLASS_VMOVD:
case XED_ICLASS_VMOVQ:
case XED_ICLASS_MOVDIRI:
if (opOneLen > opTwoLen)
{
// This may add unneeded zero-extends, but MLIL will optimize them out
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.ZeroExtend(opOneLen,
ReadILOperand(il, xedd, addr, 1, 1, opTwoLen)),
opOneLen));
}
else
{
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
ReadILOperand(il, xedd, addr, 1, 1, opTwoLen),
opOneLen));
}
break;
case XED_ICLASS_MOVDIR64B:
// Special case:
// Moves 64-bytes as direct-store with 64-byte write atomicity from source memory address to destination memory address.
// The source operand is a normal memory operand. The destination operand is a memory location specified in a general-purpose register.
// ...
// MOVDIR64B requires the destination address to be 64-byte aligned. No alignment restriction is enforced for source operand.
//
// Lifting to the intrinsic is still semantically inaccurate, but we don't currently have a way to represent atomic writes:
// movdir64b rax, zmmword [rbx]
//
// temp0 = _movdir64b(rax, [rbx].64, rax)
// [rax].64 = temp0.64
LiftAsIntrinsic();
break;
case XED_ICLASS_MOVSX:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.SignExtend(opOneLen,
ReadILOperand(il, xedd, addr, 1, 1))));
break;
case XED_ICLASS_MOVSXD:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.SignExtend(opOneLen,
ReadILOperand(il, xedd, addr, 1, 1))));
break;
case XED_ICLASS_MOVZX:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.ZeroExtend(opOneLen,
ReadILOperand(il, xedd, addr, 1, 1))));
break;
case XED_ICLASS_MOVUPS:
case XED_ICLASS_MOVAPS:
case XED_ICLASS_MOVDQA:
case XED_ICLASS_MOVDQU:
case XED_ICLASS_VMOVUPS:
case XED_ICLASS_VMOVAPS:
case XED_ICLASS_VMOVDQA:
case XED_ICLASS_VMOVDQU:
case XED_ICLASS_LDDQU:
case XED_ICLASS_MOVAPD:
case XED_ICLASS_VMOVAPD:
case XED_ICLASS_MOVUPD:
case XED_ICLASS_VMOVUPD:
{
if (xed_classify_avx512(xedd))
{
LiftAsIntrinsic();
break;
}
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0, ReadILOperand(il, xedd, addr, 1, 1)));
break;
}
// despite MOVSS and VMOVSS both move floating point values,
// the move is the same as an ordinary move
case XED_ICLASS_MOVSS:
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0, ReadILOperand(il, xedd, addr, 1, 1)));
break;
case XED_ICLASS_VMOVSS:
{
uint32_t noperands = xed_inst_noperands(xi);
if (noperands == 2)
// nothing special here
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0, ReadILOperand(il, xedd, addr, 1, 1)));
else
{
if (xed_classify_avx512(xedd))
{
LiftAsIntrinsic();
break;
}
// the three operands form
// DEST[31:0] <- SRC2[31:0]
// DEST[127:32] <- SRC1[127:32]
// DEST[MAXVL-1:128] <- 0
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.And(4, ReadILOperand(il, xedd, addr, 2, 2), il.Const(4, 0xffffffff))
)
);
// il.Const() only supports constant up to uint64_t so far so I cannot use this mask
// here I first shift right and then shift left
// __uint128_t mask = (__uint128_t)0xffffffffffffffffffffffff00000000;
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Or(opOneLen,
ReadILOperand(il, xedd, addr, 0, 0),
il.ShiftLeft(
opOneLen,
il.LogicalShiftRight(opOneLen,
ReadFloatILOperand(il, xedd, addr, 1, 1, opOneLen),
// vmovss ONLY suppors xmm, so we do not need to branch on operand size
il.Const(1, 32)
),
il.Const(1, 32)
)
)
)
);
}
break;
}
case XED_ICLASS_MOVNTDQ:
case XED_ICLASS_MOVNTDQA:
case XED_ICLASS_MOVNTI:
case XED_ICLASS_MOVNTPD:
case XED_ICLASS_MOVNTPS:
case XED_ICLASS_MOVNTQ:
case XED_ICLASS_MOVNTSD:
case XED_ICLASS_MOVNTSS:
case XED_ICLASS_VMOVNTDQ:
case XED_ICLASS_VMOVNTDQA:
case XED_ICLASS_VMOVNTPD:
case XED_ICLASS_VMOVNTPS:
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0, ReadILOperand(il, xedd, addr, 1, 1)));
break;
case XED_ICLASS_MOVLPD:
case XED_ICLASS_MOVLPS:
case XED_ICLASS_VMOVLPD:
case XED_ICLASS_VMOVLPS:
{
if (xed_inst_noperands(xi) == 2)
{
// MOVLPD xmm1, m64
// VMOVLPD m64, xmm1
// DEST[63:0] ← SRC[63:0]
// DEST[MAXVL-1:64] (Unmodified)
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
ReadILOperand(il, xedd, addr, 1, 1, 8),
8
)
);
}
else
{
// VMOVLPD xmm2, xmm1, m64
// DEST[63:0] ← SRC2[63:0]
// DEST[127:64] ← SRC1[127:64]
// DEST[MAXVL-1:128] ← 0
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Or(
16,
ReadILOperand(il, xedd, addr, 2, 2, 8),
il.And(
16,
ReadILOperand(il, xedd, addr, 1, 1, 16),
il.ShiftLeft(16,
il.Const(8, 0xffffffffffffffff),
il.Const(1, 64))
)
),
16
)
);
}
break;
}
case XED_ICLASS_MOVHPD:
case XED_ICLASS_MOVHPS:
case XED_ICLASS_VMOVHPD:
case XED_ICLASS_VMOVHPS:
{
if (xed_inst_noperands(xi) == 2)
{
// MOVHPD xmm1, m64
// DEST[63:0] (Unmodified)
// DEST[127:64] ← SRC[63:0]
// DEST[MAXVL-1:128] (Unmodified)
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Or(
16,
ReadILOperand(il, xedd, addr, 0, 0, 8),
il.ShiftLeft(
16,
ReadILOperand(il, xedd, addr, 1, 1, 8),
il.Const(1, 64)
)
),
16
)
);
}
else
{
// VMOVHPD xmm2, xmm1, m64
// DEST[63:0] ← SRC1[63:0]
// DEST[127:64] ← SRC2[63:0]
// DEST[MAXVL-1:128] ← 0
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Or(
16,
ReadILOperand(il, xedd, addr, 1, 1, 8),
il.ShiftLeft(
16,
ReadILOperand(il, xedd, addr, 2, 2, 8),
il.Const(1, 64)
)
),
16
)
);
}
break;
}
case XED_ICLASS_MOVHLPS:
{
// MOVHLPS xmm1, xmm2
// DEST[63:0] ← SRC[127:64]
// DEST[MAXVL-1:64] (Unmodified)
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.LogicalShiftRight(
16,
ReadILOperand(il, xedd, addr, 1, 1, 16),
il.Const(1, 64)
),
8
)
);
break;
}
case XED_ICLASS_VMOVHLPS:
{
// VMOVHLPS xmm1, xmm2, xmm3
// DEST[63:0] ← SRC2[127:64]
// DEST[127:64] ← SRC1[127:64]
// DEST[MAXVL-1:128] ← 0
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Or(
16,
il.And(
16,
ReadILOperand(il, xedd, addr, 1, 1, 16),
il.ShiftLeft(16,
il.Const(8, 0xffffffffffffffff),
il.Const(1, 64))
),
il.LogicalShiftRight(
16,
ReadILOperand(il, xedd, addr, 2, 2, 16),
il.Const(1, 64)
)
),
16
)
);
break;
}
case XED_ICLASS_MOVLHPS:
{
// MOVLHPS xmm1, xmm2
// DEST[63:0] (Unmodified)
// DEST[127:64] ← SRC[63:0]
// DEST[MAXVL-1:128] (Unmodified)
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Or(
16,
ReadILOperand(il, xedd, addr, 0, 0, 8),
il.ShiftLeft(
16,
ReadILOperand(il, xedd, addr, 1, 1, 8),
il.Const(1, 64)
)
),
16
)
);
break;
}
case XED_ICLASS_VMOVLHPS:
{
// VMOVLHPS xmm1, xmm2, xmm3
// DEST[63:0] ← SRC1[63:0]
// DEST[127:64] ← SRC2[63:0]
// DEST[MAXVL-1:128] ← 0
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Or(
16,
ReadILOperand(il, xedd, addr, 1, 1, 8),
il.ShiftLeft(
16,
ReadILOperand(il, xedd, addr, 2, 2, 8),
il.Const(1, 64)
)
),
16
)
);
break;
}
case XED_ICLASS_MOVSD_XMM:
// SSE instruction MOVSD
if (xed_operand_is_register(opTwo_name))
{
// movsd xmm, xmm
// movsd mem, xmm
// 64 bits at dst equals low part src xmm reg
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.LowPart(8, il.Register(16, regTwo))));
}
else // movsd xmm, mem
{
// low part of dst xmm reg equals 64 bits from src
// high part is zerod
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.ZeroExtend(16,
il.Load(8,
GetILOperandMemoryAddress(il, xedd, addr, 1, 1)))));
}
break;
case XED_ICLASS_REP_MOVSB:
case XED_ICLASS_REP_MOVSW:
case XED_ICLASS_REP_MOVSD:
case XED_ICLASS_REP_MOVSQ:
case XED_ICLASS_MOVSB:
case XED_ICLASS_MOVSW:
case XED_ICLASS_MOVSD:
case XED_ICLASS_MOVSQ:
{
size_t shift = 0;
uint32_t intrinsic = INTRINSIC_XED_IFORM_REP_MOVSB;
uint32_t srcReg = addrSize == 4 ? XED_REG_ESI : XED_REG_RSI;
uint32_t dstReg = addrSize == 4 ? XED_REG_EDI : XED_REG_RDI;
size_t moveSize;
switch (xedd_iClass)
{
case XED_ICLASS_REP_MOVSW:
case XED_ICLASS_MOVSW:
intrinsic = INTRINSIC_XED_IFORM_REP_MOVSW;
moveSize = 2;
shift = 1;
break;
case XED_ICLASS_REP_MOVSD:
case XED_ICLASS_MOVSD:
intrinsic = INTRINSIC_XED_IFORM_REP_MOVSD;
moveSize = 4;
shift = 2;
break;
case XED_ICLASS_REP_MOVSQ:
case XED_ICLASS_MOVSQ:
intrinsic = INTRINSIC_XED_IFORM_REP_MOVSQ;
moveSize = 8;
shift = 3;
break;
default:
moveSize = 1;
break;
}
if (xed_operand_values_has_real_rep(xed_decoded_inst_operands_const(xedd)))
{
ExprId numBytesExpr;
if (shift)
numBytesExpr = il.ShiftLeft(addrSize, il.Register(addrSize, GetCountRegister(addrSize)), il.Const(addrSize, shift));
else
numBytesExpr = il.Register(addrSize, GetCountRegister(addrSize));
ExprId countExpr = il.Register(addrSize, GetCountRegister(addrSize));
DirFlagIf(il,
[&](){},
[&]() // Direction flag 1
{
auto dstExpr = il.Sub(addrSize, il.Register(addrSize, dstReg), numBytesExpr);
auto srcExpr = il.Sub(addrSize, il.Register(addrSize, srcReg), numBytesExpr);
il.AddInstruction(il.Intrinsic(
vector<RegisterOrFlag> { RegisterOrFlag::Register(dstReg), RegisterOrFlag::Register(srcReg), RegisterOrFlag::Register(GetCountRegister(addrSize)) },
intrinsic,
vector<ExprId> { dstExpr, srcExpr, countExpr }
));
},
[&]() // Direction flag 0
{
auto dstExpr = il.Register(addrSize, dstReg);
auto srcExpr = il.Register(addrSize, srcReg);
il.AddInstruction(il.Intrinsic(
vector<RegisterOrFlag> { RegisterOrFlag::Register(dstReg), RegisterOrFlag::Register(srcReg), RegisterOrFlag::Register(GetCountRegister(addrSize)) },
intrinsic,
vector<ExprId> { dstExpr, srcExpr, countExpr }
));
}
);
break;
}
Repeat(xedd, il, [&] (){
DirFlagIf(il,
[&](){}, // Pre check direction flag check
[&]() // Direction flag true
{
il.AddInstruction(il.Store(moveSize, il.Register(addrSize, dstReg),
il.Load(moveSize, il.Register(addrSize, srcReg))));
il.AddInstruction(
il.SetRegister(addrSize,
dstReg,
il.Sub(addrSize,
il.Register(addrSize, dstReg),
il.Const(addrSize, moveSize))));
il.AddInstruction(
il.SetRegister(addrSize,
srcReg,
il.Sub(addrSize,
il.Register(addrSize, srcReg),
il.Const(addrSize, moveSize))));
},
[&]() // Direction flag false
{
il.AddInstruction(
il.Store(moveSize,
il.Register(addrSize, dstReg),
il.Load(moveSize,
il.Register(addrSize, srcReg))));
il.AddInstruction(
il.SetRegister(addrSize,
dstReg,
il.Add(addrSize,
il.Register(addrSize, dstReg),
il.Const(addrSize, moveSize))));
il.AddInstruction(
il.SetRegister(addrSize,
srcReg,
il.Add(addrSize,
il.Register(addrSize, srcReg),
il.Const(addrSize, moveSize))));
});
});
break;
}
case XED_ICLASS_MUL:
switch (opOneLen)
{
case 1:
il.AddInstruction(
il.SetRegister(2,
XED_REG_AX,
il.MultDoublePrecUnsigned(1,
il.Register(1, XED_REG_AL),
ReadILOperand(il, xedd, addr, 0, 0),
noFlags ? 0 : IL_FLAGWRITE_CO)));
break;
case 2:
il.AddInstruction(
il.SetRegisterSplit(2,
XED_REG_DX,
XED_REG_AX,
il.MultDoublePrecUnsigned(2,
il.Register(2, XED_REG_AX),
ReadILOperand(il, xedd, addr, 0, 0),
noFlags ? 0 : IL_FLAGWRITE_CO)));
break;
case 4:
il.AddInstruction(
il.SetRegisterSplit(4,
XED_REG_EDX,
XED_REG_EAX,
il.MultDoublePrecUnsigned(4,
il.Register(4, XED_REG_EAX),
ReadILOperand(il, xedd, addr, 0, 0),
noFlags ? 0 : IL_FLAGWRITE_CO)));
break;
case 8:
il.AddInstruction(
il.SetRegisterSplit(8,
XED_REG_RDX,
XED_REG_RAX,
il.MultDoublePrecUnsigned(8,
il.Register(8, XED_REG_RAX),
ReadILOperand(il, xedd, addr, 0, 0),
noFlags ? 0 : IL_FLAGWRITE_CO)));
break;
default:
il.AddInstruction(il.Undefined());
break;
}
break;
case XED_ICLASS_NEG_LOCK: // TODO: Handle lock prefix
case XED_ICLASS_NEG:
{
ExprId src;
size_t opSize = opOneLen;
if (!newDataDestination)
{
// nothing special
src = ReadILOperand(il, xedd, addr, 0, 0);
}
else
{
// new data destination
src = ReadILOperand(il, xedd, addr, 1, 1);
opSize = opTwoLen;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Neg(opSize, src,
noFlags ? 0 : IL_FLAGWRITE_ALL)));
break;
}
case XED_ICLASS_NOP:
case XED_ICLASS_NOP2:
case XED_ICLASS_NOP3:
case XED_ICLASS_NOP4:
case XED_ICLASS_NOP5:
case XED_ICLASS_NOP6:
case XED_ICLASS_NOP7:
case XED_ICLASS_NOP8:
case XED_ICLASS_NOP9:
case XED_ICLASS_FNOP:
case XED_ICLASS_FDISI8087_NOP:
case XED_ICLASS_FENI8087_NOP:
case XED_ICLASS_FSETPM287_NOP:
case XED_ICLASS_PAUSE:
case XED_ICLASS_PREFETCHNTA:
case XED_ICLASS_PREFETCHT0:
case XED_ICLASS_PREFETCHT1:
case XED_ICLASS_PREFETCHT2:
case XED_ICLASS_PREFETCHW:
case XED_ICLASS_PREFETCHWT1:
case XED_ICLASS_PREFETCH_EXCLUSIVE:
case XED_ICLASS_PREFETCH_RESERVED:
case XED_ICLASS_FWAIT:
case XED_ICLASS_LFENCE:
case XED_ICLASS_MFENCE:
case XED_ICLASS_SFENCE:
case XED_ICLASS_ENDBR32:
case XED_ICLASS_ENDBR64:
il.AddInstruction(il.Nop());
break;
case XED_ICLASS_NOT_LOCK: // TODO: Handle lock prefix
case XED_ICLASS_NOT:
{
ExprId src;
size_t opSize = opOneLen;
if (!newDataDestination)
{
// nothing special
src = ReadILOperand(il, xedd, addr, 0, 0);
}
else
{
// new data destination
src = ReadILOperand(il, xedd, addr, 1, 1);
opSize = opTwoLen;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Not(opSize, src)));
break;
}
case XED_ICLASS_OR_LOCK: // TODO: Handle lock prefix
case XED_ICLASS_OR:
{
ExprId left, right;
size_t opSize = opOneLen;
if (!newDataDestination)
{
// nothing special
left = ReadILOperand(il, xedd, addr, 0, 0);
right = ReadILOperand(il, xedd, addr, 1, 1);
}
else
{
// new data destination
left = ReadILOperand(il, xedd, addr, 1, 1);
right = ReadILOperand(il, xedd, addr, 2, 2);
opSize = opTwoLen;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Or(opSize, left, right,
noFlags ? 0 : IL_FLAGWRITE_ALL)));
break;
}
case XED_ICLASS_POR:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Or(opOneLen,
ReadILOperand(il, xedd, addr, 0, 0),
ReadILOperand(il, xedd, addr, 1, 1),
0))); // POR doesn't modify any flag
break;
case XED_ICLASS_VPOR:
if (xed_classify_avx512(xedd))
{
LiftAsIntrinsic();
break;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Or(opOneLen,
ReadILOperand(il, xedd, addr, 1, 1),
ReadILOperand(il, xedd, addr, 2, 2),
0))); // VPOR doesn't modify flags
break;
case XED_ICLASS_POP2:
case XED_ICLASS_POP2P:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Pop(8)));
il.AddInstruction(
WriteILOperand(il, xedd, addr, 1, 1,
il.Pop(8)));
break;
case XED_ICLASS_POP:
case XED_ICLASS_POPP:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Pop(opOneLen)));
break;
case XED_ICLASS_POPA:
il.AddInstruction(il.SetRegister(2, XED_REG_DI, il.Pop(2)));
il.AddInstruction(il.SetRegister(2, XED_REG_SI, il.Pop(2)));
il.AddInstruction(il.SetRegister(2, XED_REG_BP, il.Pop(2)));
il.AddInstruction(il.SetRegister(2, XED_REG_SP, il.Add(2, il.Register(2, XED_REG_SP), il.Const(2, 2))));
il.AddInstruction(il.SetRegister(2, XED_REG_BX, il.Pop(2)));
il.AddInstruction(il.SetRegister(2, XED_REG_DX, il.Pop(2)));
il.AddInstruction(il.SetRegister(2, XED_REG_CX, il.Pop(2)));
il.AddInstruction(il.SetRegister(2, XED_REG_AX, il.Pop(2)));
break;
case XED_ICLASS_POPAD:
il.AddInstruction(il.SetRegister(4, XED_REG_EDI, il.Pop(4)));
il.AddInstruction(il.SetRegister(4, XED_REG_ESI, il.Pop(4)));
il.AddInstruction(il.SetRegister(4, XED_REG_EBP, il.Pop(4)));
il.AddInstruction(il.SetRegister(4, XED_REG_ESP, il.Add(4, il.Register(4, XED_REG_ESP), il.Const(4, 4))));
il.AddInstruction(il.SetRegister(4, XED_REG_EBX, il.Pop(4)));
il.AddInstruction(il.SetRegister(4, XED_REG_EDX, il.Pop(4)));
il.AddInstruction(il.SetRegister(4, XED_REG_ECX, il.Pop(4)));
il.AddInstruction(il.SetRegister(4, XED_REG_EAX, il.Pop(4)));
break;
case XED_ICLASS_POPF:
il.AddInstruction(il.SetRegister(2, LLIL_TEMP(0), il.Pop(2)));
il.AddInstruction(il.SetFlag(IL_FLAG_C, il.TestBit(2, il.Register(2, LLIL_TEMP(0)), il.Const(1, 0))));
il.AddInstruction(il.SetFlag(IL_FLAG_P, il.TestBit(2, il.Register(2, LLIL_TEMP(0)), il.Const(1, 2))));
il.AddInstruction(il.SetFlag(IL_FLAG_A, il.TestBit(2, il.Register(2, LLIL_TEMP(0)), il.Const(1, 4))));
il.AddInstruction(il.SetFlag(IL_FLAG_Z, il.TestBit(2, il.Register(2, LLIL_TEMP(0)), il.Const(1, 6))));
il.AddInstruction(il.SetFlag(IL_FLAG_S, il.TestBit(2, il.Register(2, LLIL_TEMP(0)), il.Const(1, 7))));
il.AddInstruction(il.SetFlag(IL_FLAG_D, il.TestBit(2, il.Register(2, LLIL_TEMP(0)), il.Const(1, 10))));
il.AddInstruction(il.SetFlag(IL_FLAG_O, il.TestBit(2, il.Register(2, LLIL_TEMP(0)), il.Const(1, 11))));
break;
case XED_ICLASS_POPFD:
il.AddInstruction(il.SetRegister(4, LLIL_TEMP(0), il.Pop(4)));
il.AddInstruction(il.SetFlag(IL_FLAG_C, il.TestBit(4, il.Register(4, LLIL_TEMP(0)), il.Const(1, 0))));
il.AddInstruction(il.SetFlag(IL_FLAG_P, il.TestBit(4, il.Register(4, LLIL_TEMP(0)), il.Const(1, 2))));
il.AddInstruction(il.SetFlag(IL_FLAG_A, il.TestBit(4, il.Register(4, LLIL_TEMP(0)), il.Const(1, 4))));
il.AddInstruction(il.SetFlag(IL_FLAG_Z, il.TestBit(4, il.Register(4, LLIL_TEMP(0)), il.Const(1, 6))));
il.AddInstruction(il.SetFlag(IL_FLAG_S, il.TestBit(4, il.Register(4, LLIL_TEMP(0)), il.Const(1, 7))));
il.AddInstruction(il.SetFlag(IL_FLAG_D, il.TestBit(4, il.Register(4, LLIL_TEMP(0)), il.Const(1, 10))));
il.AddInstruction(il.SetFlag(IL_FLAG_O, il.TestBit(4, il.Register(4, LLIL_TEMP(0)), il.Const(1, 11))));
break;
case XED_ICLASS_POPFQ:
il.AddInstruction(il.SetRegister(8, LLIL_TEMP(0), il.Pop(8)));
il.AddInstruction(il.SetFlag(IL_FLAG_C, il.TestBit(8, il.Register(8, LLIL_TEMP(0)), il.Const(1, 0))));
il.AddInstruction(il.SetFlag(IL_FLAG_P, il.TestBit(8, il.Register(8, LLIL_TEMP(0)), il.Const(1, 2))));
il.AddInstruction(il.SetFlag(IL_FLAG_A, il.TestBit(8, il.Register(8, LLIL_TEMP(0)), il.Const(1, 4))));
il.AddInstruction(il.SetFlag(IL_FLAG_Z, il.TestBit(8, il.Register(8, LLIL_TEMP(0)), il.Const(1, 6))));
il.AddInstruction(il.SetFlag(IL_FLAG_S, il.TestBit(8, il.Register(8, LLIL_TEMP(0)), il.Const(1, 7))));
il.AddInstruction(il.SetFlag(IL_FLAG_D, il.TestBit(8, il.Register(8, LLIL_TEMP(0)), il.Const(1, 10))));
il.AddInstruction(il.SetFlag(IL_FLAG_O, il.TestBit(8, il.Register(8, LLIL_TEMP(0)), il.Const(1, 11))));
break;
case XED_ICLASS_PUSHA:
il.AddInstruction(il.Push(2, il.Register(2, XED_REG_AX)));
il.AddInstruction(il.Push(2, il.Register(2, XED_REG_CX)));
il.AddInstruction(il.Push(2, il.Register(2, XED_REG_DX)));
il.AddInstruction(il.Push(2, il.Register(2, XED_REG_BX)));
il.AddInstruction(il.Push(2, il.Register(2, XED_REG_SP)));
il.AddInstruction(il.Push(2, il.Register(2, XED_REG_BP)));
il.AddInstruction(il.Push(2, il.Register(2, XED_REG_SI)));
il.AddInstruction(il.Push(2, il.Register(2, XED_REG_DI)));
break;
case XED_ICLASS_PUSHAD:
il.AddInstruction(il.Push(4, il.Register(4, XED_REG_EAX)));
il.AddInstruction(il.Push(4, il.Register(4, XED_REG_ECX)));
il.AddInstruction(il.Push(4, il.Register(4, XED_REG_EDX)));
il.AddInstruction(il.Push(4, il.Register(4, XED_REG_EBX)));
il.AddInstruction(il.Push(4, il.Register(4, XED_REG_ESP)));
il.AddInstruction(il.Push(4, il.Register(4, XED_REG_EBP)));
il.AddInstruction(il.Push(4, il.Register(4, XED_REG_ESI)));
il.AddInstruction(il.Push(4, il.Register(4, XED_REG_EDI)));
break;
case XED_ICLASS_PUSHF:
il.AddInstruction(il.Push(2,
il.Or(2, il.FlagBit(2, IL_FLAG_O, 11),
il.Or(2, il.FlagBit(2, IL_FLAG_D, 10),
il.Or(2, il.FlagBit(2, IL_FLAG_S, 7),
il.Or(2, il.FlagBit(2, IL_FLAG_Z, 6),
il.Or(2, il.FlagBit(2, IL_FLAG_A, 4),
il.Or(2, il.FlagBit(2, IL_FLAG_P, 2),
il.FlagBit(2, IL_FLAG_C, 0)))))))));
break;
case XED_ICLASS_PUSHFD:
il.AddInstruction(il.Push(4,
il.Or(4, il.FlagBit(4, IL_FLAG_O, 11),
il.Or(4, il.FlagBit(4, IL_FLAG_D, 10),
il.Or(4, il.FlagBit(4, IL_FLAG_S, 7),
il.Or(4, il.FlagBit(4, IL_FLAG_Z, 6),
il.Or(4, il.FlagBit(4, IL_FLAG_A, 4),
il.Or(4, il.FlagBit(4, IL_FLAG_P, 2),
il.FlagBit(4, IL_FLAG_C, 0)))))))));
break;
case XED_ICLASS_PUSHFQ:
il.AddInstruction(
il.Push(8,
il.Or(8,
il.FlagBit(8, IL_FLAG_O, 11),
il.Or(8,
il.FlagBit(8, IL_FLAG_D, 10),
il.Or(8,
il.FlagBit(8, IL_FLAG_S, 7),
il.Or(8,
il.FlagBit(8, IL_FLAG_Z, 6),
il.Or(8,
il.FlagBit(8, IL_FLAG_A, 4),
il.Or(8,
il.FlagBit(8, IL_FLAG_P, 2),
il.FlagBit(8, IL_FLAG_C, 0)
)
)
)
)
)
)
)
);
break;
case XED_ICLASS_PUSH2:
case XED_ICLASS_PUSH2P:
il.AddInstruction(il.Push(8, ReadILOperand(il, xedd, addr, 0, 0)));
il.AddInstruction(il.Push(8, ReadILOperand(il, xedd, addr, 1, 1)));
break;
case XED_ICLASS_PUSH:
case XED_ICLASS_PUSHP:
{
// Whe the stack adjustment is different from the operand one size, zero-extend it. Note, a "push r16" in either
// x86 or x64 pushes the 2-byte register onto the stack, and there is no need to extend. See
// https://github.com/Vector35/binaryninja-api/issues/4028 and
// https://stackoverflow.com/questions/43435764/64-bit-mode-does-not-support-32-bit-push-and-pop-instructions
// for more details
const unsigned int stackAdjustment = xed_decoded_inst_get_memory_operand_length(xedd, 0);
if (opOneLen != stackAdjustment)
{
il.AddInstruction(
il.Push(stackAdjustment,
il.ZeroExtend(stackAdjustment,
ReadILOperand(il, xedd, addr, 0, 0))));
}
else
il.AddInstruction(
il.Push(stackAdjustment,
ReadILOperand(il, xedd, addr, 0, 0)));
break;
}
case XED_ICLASS_RCL:
{
ExprId left, right;
size_t opSize = opOneLen;
if (!newDataDestination)
{
// nothing special
left = ReadILOperand(il, xedd, addr, 0, 0);
right = ReadILOperand(il, xedd, addr, 1, 1);
}
else
{
// new data destination
left = ReadILOperand(il, xedd, addr, 1, 1);
right = ReadILOperand(il, xedd, addr, 2, 2);
opSize = opTwoLen;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.RotateLeftCarry(opSize, left, right,
il.Flag(IL_FLAG_C), IL_FLAGWRITE_ALL)));
break;
}
case XED_ICLASS_RCR:
{
ExprId left, right;
size_t opSize = opOneLen;
if (!newDataDestination)
{
// nothing special
left = ReadILOperand(il, xedd, addr, 0, 0);
right = ReadILOperand(il, xedd, addr, 1, 1);
}
else
{
// new data destination
left = ReadILOperand(il, xedd, addr, 1, 1);
right = ReadILOperand(il, xedd, addr, 2, 2);
opSize = opTwoLen;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.RotateRightCarry(opSize, left, right,
il.Flag(IL_FLAG_C), IL_FLAGWRITE_ALL)));
break;
}
case XED_ICLASS_RET_NEAR:
if ((opOne_name != XED_OPERAND_IMM0) || (immediateOne == 0))
il.AddInstruction(il.Return(il.Pop(addrSize)));
else
{
il.AddInstruction(il.SetRegister(addrSize, LLIL_TEMP(0), il.Pop(addrSize)));
il.AddInstruction(
il.SetRegister(addrSize,
GetStackPointer(addrSize),
il.Add(addrSize,
il.Register(addrSize, GetStackPointer(addrSize)),
il.Const(addrSize, immediateOne))));
il.AddInstruction(il.Return(il.Register(addrSize, LLIL_TEMP(0))));
}
break;
case XED_ICLASS_ROL:
{
ExprId left, right;
size_t opSize = opOneLen;
if (!newDataDestination)
{
// nothing special
left = ReadILOperand(il, xedd, addr, 0, 0);
right = ReadILOperand(il, xedd, addr, 1, 1);
}
else
{
// new data destination
left = ReadILOperand(il, xedd, addr, 1, 1);
right = ReadILOperand(il, xedd, addr, 2, 2);
opSize = opTwoLen;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.RotateLeft(opSize, left, right,
noFlags ? 0 : IL_FLAGWRITE_ALL)));
break;
}
case XED_ICLASS_ROR:
{
ExprId left, right;
size_t opSize = opOneLen;
if (!newDataDestination)
{
// nothing special
left = ReadILOperand(il, xedd, addr, 0, 0);
right = ReadILOperand(il, xedd, addr, 1, 1);
}
else
{
// new data destination
left = ReadILOperand(il, xedd, addr, 1, 1);
right = ReadILOperand(il, xedd, addr, 2, 2);
opSize = opTwoLen;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.RotateRight(opSize, left, right,
noFlags ? 0 : IL_FLAGWRITE_ALL)));
break;
}
// there is no ROLX instruciton
case XED_ICLASS_RORX:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.RotateRight(opOneLen,
ReadILOperand(il, xedd, addr, 1, 1),
ReadILOperand(il, xedd, addr, 2, 2)
)));
break;
case XED_ICLASS_SAR:
{
ExprId left, right;
size_t opSize = opOneLen;
if (!newDataDestination)
{
// nothing special
left = ReadILOperand(il, xedd, addr, 0, 0);
right = ReadILOperand(il, xedd, addr, 1, 1);
}
else
{
// new data destination
left = ReadILOperand(il, xedd, addr, 1, 1);
right = ReadILOperand(il, xedd, addr, 2, 2);
opSize = opTwoLen;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.ArithShiftRight(opSize, left, right,
noFlags ? 0 : IL_FLAGWRITE_ALL)));
break;
}
case XED_ICLASS_SARX:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.ArithShiftRight(opOneLen,
ReadILOperand(il, xedd, addr, 1, 1),
ReadILOperand(il, xedd, addr, 2, 2)
)));
break;
case XED_ICLASS_SAHF:
il.AddInstruction(il.SetFlag(IL_FLAG_C, il.TestBit(1, il.Register(1, XED_REG_AH), il.Const(1, 0))));
il.AddInstruction(il.SetFlag(IL_FLAG_P, il.TestBit(1, il.Register(1, XED_REG_AH), il.Const(1, 2))));
il.AddInstruction(il.SetFlag(IL_FLAG_A, il.TestBit(1, il.Register(1, XED_REG_AH), il.Const(1, 4))));
il.AddInstruction(il.SetFlag(IL_FLAG_Z, il.TestBit(1, il.Register(1, XED_REG_AH), il.Const(1, 6))));
il.AddInstruction(il.SetFlag(IL_FLAG_S, il.TestBit(1, il.Register(1, XED_REG_AH), il.Const(1, 7))));
break;
case XED_ICLASS_SBB_LOCK: // TODO: Handle lock prefix
case XED_ICLASS_SBB:
{
ExprId left, right;
size_t opSize = opOneLen;
if (!newDataDestination)
{
// nothing special
left = ReadILOperand(il, xedd, addr, 0, 0);
right = ReadILOperand(il, xedd, addr, 1, 1);
}
else
{
// new data destination
left = ReadILOperand(il, xedd, addr, 1, 1);
right = ReadILOperand(il, xedd, addr, 2, 2);
opSize = opTwoLen;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.SubBorrow(opSize, left, right,
il.Flag(IL_FLAG_C), IL_FLAGWRITE_ALL)));
break;
}
case XED_ICLASS_REPE_SCASB:
case XED_ICLASS_REPE_SCASD:
case XED_ICLASS_REPE_SCASQ:
case XED_ICLASS_REPE_SCASW:
case XED_ICLASS_REPNE_SCASB:
case XED_ICLASS_REPNE_SCASD:
case XED_ICLASS_REPNE_SCASQ:
case XED_ICLASS_REPNE_SCASW:
case XED_ICLASS_SCASB:
case XED_ICLASS_SCASW:
case XED_ICLASS_SCASD:
case XED_ICLASS_SCASQ:
{
size_t searchSize;
uint32_t cmpReg;
uint32_t srcReg = addrSize == 4 ? XED_REG_EDI : XED_REG_RDI;
switch (xedd_iClass)
{
case XED_ICLASS_REPE_SCASW:
case XED_ICLASS_REPNE_SCASW:
case XED_ICLASS_SCASW:
searchSize = 2;
cmpReg = XED_REG_AX;
break;
case XED_ICLASS_REPE_SCASD:
case XED_ICLASS_REPNE_SCASD:
case XED_ICLASS_SCASD:
searchSize = 4;
cmpReg = XED_REG_EAX;
break;
case XED_ICLASS_REPE_SCASQ:
case XED_ICLASS_REPNE_SCASQ:
case XED_ICLASS_SCASQ:
searchSize = 8;
cmpReg = XED_REG_RAX;
break;
default:
searchSize = 1;
cmpReg = XED_REG_AL;
break;
}
Repeat(xedd, il, [&]() {
DirFlagIf(il, [&]()
{
(void)addrSize;
il.AddInstruction(il.Sub(searchSize, il.Register(searchSize, cmpReg), il.Load(searchSize, il.Register(addrSize, srcReg)), IL_FLAGWRITE_ALL));
},
[&] () // Direction flag is 1
{
il.AddInstruction(il.SetRegister(addrSize, srcReg, il.Sub(addrSize, il.Register(addrSize, srcReg), il.Const(addrSize, searchSize))));
},
[&] () // Direction flag is 0
{
il.AddInstruction(il.SetRegister(addrSize, srcReg, il.Add(addrSize, il.Register(addrSize, srcReg), il.Const(addrSize, searchSize))));
});
});
break;
}
case XED_ICLASS_SETO:
case XED_ICLASS_SETNO:
case XED_ICLASS_SETB:
case XED_ICLASS_SETNB:
case XED_ICLASS_SETZ:
case XED_ICLASS_SETNZ:
case XED_ICLASS_SETBE:
case XED_ICLASS_SETNBE:
case XED_ICLASS_SETS:
case XED_ICLASS_SETNS:
case XED_ICLASS_SETP:
case XED_ICLASS_SETNP:
case XED_ICLASS_SETL:
case XED_ICLASS_SETNL:
case XED_ICLASS_SETLE:
case XED_ICLASS_SETNLE:
{
ExprId flag;
switch (xedd_iClass)
{
case XED_ICLASS_SETO:
flag = il.Flag(IL_FLAG_O);
break;
case XED_ICLASS_SETNO:
flag = il.Not(1, il.Flag(IL_FLAG_O));
break;
case XED_ICLASS_SETB:
flag = il.Flag(IL_FLAG_C);
break;
case XED_ICLASS_SETNB:
flag = il.FlagGroup(IL_FLAG_GROUP_GE);
break;
case XED_ICLASS_SETZ:
flag = il.FlagGroup(IL_FLAG_GROUP_E);
break;
case XED_ICLASS_SETNZ:
flag = il.FlagGroup(IL_FLAG_GROUP_NE);
break;
case XED_ICLASS_SETBE:
flag = il.FlagGroup(IL_FLAG_GROUP_LE);
break;
case XED_ICLASS_SETNBE:
flag = il.FlagGroup(IL_FLAG_GROUP_GT);
break;
case XED_ICLASS_SETS:
flag = il.FlagCondition(LLFC_NEG);
break;
case XED_ICLASS_SETNS:
flag = il.FlagCondition(LLFC_POS);
break;
case XED_ICLASS_SETP:
flag = il.FlagGroup(IL_FLAG_GROUP_PE);
break;
case XED_ICLASS_SETNP:
flag = il.FlagGroup(IL_FLAG_GROUP_PO);
break;
case XED_ICLASS_SETL:
flag = il.FlagCondition(LLFC_SLT);
break;
case XED_ICLASS_SETNL:
flag = il.FlagCondition(LLFC_SGE);
break;
case XED_ICLASS_SETLE:
flag = il.FlagCondition(LLFC_SLE);
break;
case XED_ICLASS_SETNLE:
flag = il.FlagCondition(LLFC_SGT);
break;
default:
break;
}
if (zeroUpper && xed_operand_is_register(opOne_name))
flag = il.ZeroExtend(8, flag);
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0, flag));
break;
}
case XED_ICLASS_SHL:
{
ExprId left, right;
size_t opSize = opOneLen;
if (!newDataDestination)
{
// nothing special
left = ReadILOperand(il, xedd, addr, 0, 0);
right = ReadILOperand(il, xedd, addr, 1, 1);
}
else
{
// new data destination
left = ReadILOperand(il, xedd, addr, 1, 1);
right = ReadILOperand(il, xedd, addr, 2, 2);
opSize = opTwoLen;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.ShiftLeft(opSize, left, right,
noFlags ? 0 : IL_FLAGWRITE_ALL)));
break;
}
// This is imprecise since it does NOT move the last shifted bit into CF
// the same problem also happens on SHL, SAR
case XED_ICLASS_SHR:
{
ExprId left, right;
size_t opSize = opOneLen;
if (!newDataDestination)
{
// nothing special
left = ReadILOperand(il, xedd, addr, 0, 0);
right = ReadILOperand(il, xedd, addr, 1, 1);
}
else
{
// new data destination
left = ReadILOperand(il, xedd, addr, 1, 1);
right = ReadILOperand(il, xedd, addr, 2, 2);
opSize = opTwoLen;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.LogicalShiftRight(opSize, left, right,
noFlags ? 0 : IL_FLAGWRITE_ALL)));
break;
}
case XED_ICLASS_SHLX:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.ShiftLeft(opOneLen,
ReadILOperand(il, xedd, addr, 1, 1),
ReadILOperand(il, xedd, addr, 2, 2)
)));
break;
case XED_ICLASS_SHRX:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.LogicalShiftRight(opOneLen,
ReadILOperand(il, xedd, addr, 1, 1),
ReadILOperand(il, xedd, addr, 2, 2)
)));
break;
case XED_ICLASS_SHLD:
{
// TODO: Might need to swap for new data?
size_t opSize = opOneLen;
size_t mask = opSize == 4 ? 31 : 63;
// Shift left double: operand[0] = operand[0]:operand[1] << operand[3]
// this since we can't easily operation on a combined register we do it like this
// operand[0] = (operand[0] << operand[3]) | (operand[1] >> (63|32 - operand[3]))
// One final caveat operand[3] must be masked with 63|32
ExprId left, right, count;
if (!newDataDestination)
{
// nothing special
left = ReadILOperand(il, xedd, addr, 0, 0);
right = ReadILOperand(il, xedd, addr, 1, 1);
count = il.And(opSize,
il.Const(1, mask),
ReadILOperand(il, xedd, addr, 2, 2));
}
else
{
// new data destination
left = ReadILOperand(il, xedd, addr, 1, 1);
right = ReadILOperand(il, xedd, addr, 2, 2);
count = il.And(opSize,
il.Const(1, mask),
ReadILOperand(il, xedd, addr, 3, 3));
}
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.Or(opSize,
il.ShiftLeft(opSize, left, count,
noFlags ? 0 : IL_FLAGWRITE_ALL),
il.LogicalShiftRight(opSize,
right,
il.Sub(opSize, count, il.Const(1, opSize * 8))))));
break;
}
case XED_ICLASS_SHRD:
{
// TODO: Might need to swap for new data?
size_t opSize = opOneLen;
size_t mask = opSize == 4 ? 31 : 63;
// Shift right double: operand[0] = operand[0]:operand[1] >> operand[3]
// this since we can't easily operation on a combined register we do it like this
// operand[0] = (operand[0] >> operand[3]) | (operand[1] << (63|31 - operand[3]))
// One final caveat operand[3] must be masked with 63|31
ExprId left, right, count;
if (!newDataDestination)
{
// nothing special
left = ReadILOperand(il, xedd, addr, 0, 0);
right = ReadILOperand(il, xedd, addr, 1, 1);
count = il.And(opSize,
il.Const(1, mask),
ReadILOperand(il, xedd, addr, 2, 2));
}
else
{
// new data destination
left = ReadILOperand(il, xedd, addr, 1, 1);
right = ReadILOperand(il, xedd, addr, 2, 2);
count = il.And(opSize,
il.Const(1, mask),
ReadILOperand(il, xedd, addr, 3, 3));
}
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.Or(opSize,
il.LogicalShiftRight(opSize,
left,
count,
noFlags ? 0 : IL_FLAGWRITE_ALL),
il.ShiftLeft(opSize,
right,
il.Sub(opSize, il.Const(1, opSize * 8), count)))));
break;
}
case XED_ICLASS_STOSB:
case XED_ICLASS_STOSW:
case XED_ICLASS_STOSD:
case XED_ICLASS_STOSQ:
case XED_ICLASS_REP_STOSB:
case XED_ICLASS_REP_STOSW:
case XED_ICLASS_REP_STOSD:
case XED_ICLASS_REP_STOSQ:
{
size_t shift = 0;
uint32_t intrinsic = INTRINSIC_XED_IFORM_REP_STOSB;
size_t moveSize = 1;
ExprId moveReg = 0;
uint32_t ilDestReg = addrSize == 4 ? XED_REG_EDI : XED_REG_RDI;
switch (xedd_iClass)
{
case XED_ICLASS_STOSB:
case XED_ICLASS_REP_STOSB:
intrinsic = INTRINSIC_XED_IFORM_REP_STOSB;
moveSize = 1; moveReg = il.Register(moveSize, XED_REG_AL);
shift = 0;
break;
case XED_ICLASS_STOSW:
case XED_ICLASS_REP_STOSW:
intrinsic = INTRINSIC_XED_IFORM_REP_STOSW;
moveSize = 2; moveReg = il.Register(moveSize, XED_REG_AX);
shift = 1;
break;
case XED_ICLASS_STOSD:
case XED_ICLASS_REP_STOSD:
intrinsic = INTRINSIC_XED_IFORM_REP_STOSD;
moveSize = 4; moveReg = il.Register(moveSize, XED_REG_EAX);
shift = 2;
break;
case XED_ICLASS_STOSQ:
case XED_ICLASS_REP_STOSQ:
intrinsic = INTRINSIC_XED_IFORM_REP_STOSQ;
moveSize = 8; moveReg = il.Register(moveSize, XED_REG_RAX);
shift = 3;
break;
default: break;
}
if (xed_operand_values_has_real_rep(xed_decoded_inst_operands_const(xedd)))
{
ExprId numBytesExpr;
if (shift)
numBytesExpr = il.ShiftLeft(addrSize, il.Register(addrSize, GetCountRegister(addrSize)), il.Const(addrSize, shift));
else
numBytesExpr = il.Register(addrSize, GetCountRegister(addrSize));
ExprId countExpr = il.Register(addrSize, GetCountRegister(addrSize));
DirFlagIf(il,
[&](){},
[&]() // Direction flag 1
{
il.AddInstruction(il.Intrinsic(
vector<RegisterOrFlag> { RegisterOrFlag::Register(ilDestReg), RegisterOrFlag::Register(GetCountRegister(addrSize)) },
intrinsic,
vector<ExprId> { il.Sub(addrSize, il.Register(addrSize, ilDestReg), numBytesExpr), moveReg, countExpr }
));
},
[&]() // Direction flag 0
{
il.AddInstruction(il.Intrinsic(
vector<RegisterOrFlag> { RegisterOrFlag::Register(ilDestReg), RegisterOrFlag::Register(GetCountRegister(addrSize)) },
intrinsic,
vector<ExprId> { il.Register(addrSize, ilDestReg), moveReg, countExpr }
));
}
);
break;
}
Repeat(xedd, il, [&](){
DirFlagIf(il,
[&](){},
[&]() // Direction flag 1
{
il.AddInstruction(
il.Store(moveSize,
il.Register(addrSize, ilDestReg),
moveReg));
il.AddInstruction(
il.SetRegister(addrSize,
ilDestReg,
il.Sub(addrSize,
il.Register(addrSize, ilDestReg),
il.Const(addrSize, moveSize))));
},
[&]() // Direction flag 0
{
il.AddInstruction(
il.Store(moveSize,
il.Register(addrSize, ilDestReg),
moveReg));
il.AddInstruction(
il.SetRegister(addrSize,
ilDestReg,
il.Add(addrSize,
il.Register(addrSize, ilDestReg),
il.Const(addrSize, moveSize))));
}
);
});
break;
}
case XED_ICLASS_STC:
il.AddInstruction(il.SetFlag(IL_FLAG_C, il.Const(1, 1)));
break;
case XED_ICLASS_STD:
il.AddInstruction(il.SetFlag(IL_FLAG_D, il.Const(1, 1)));
break;
case XED_ICLASS_SUB_LOCK: // TODO: Handle lock prefix
case XED_ICLASS_SUB:
{
ExprId left, right;
size_t opSize = opOneLen;
if (!newDataDestination)
{
// nothing special
left = ReadILOperand(il, xedd, addr, 0, 0);
right = ReadILOperand(il, xedd, addr, 1, 1);
}
else
{
// new data destination
left = ReadILOperand(il, xedd, addr, 1, 1);
right = ReadILOperand(il, xedd, addr, 2, 2);
opSize = opTwoLen;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Sub(opSize, left, right,
noFlags ? 0 : IL_FLAGWRITE_ALL)));
break;
}
case XED_ICLASS_TEST:
case XED_ICLASS_CTESTT:
il.AddInstruction(
il.And(opOneLen,
ReadILOperand(il, xedd, addr, 0, 0),
ReadILOperand(il, xedd, addr, 1, 1),
IL_FLAGWRITE_ALL));
break;
case XED_ICLASS_PTEST:
case XED_ICLASS_VPTEST:
il.AddInstruction(
il.SetFlag(IL_FLAG_Z,
il.BoolToInt(
1,
il.CompareEqual(
opOneLen,
il.And(
opOneLen,
ReadILOperand(il, xedd, addr, 0, 0),
ReadILOperand(il, xedd, addr, 1, 1)
),
il.Const(opOneLen, 0)
)
)
)
);
il.AddInstruction(
il.SetFlag(IL_FLAG_C,
il.BoolToInt(
1,
il.CompareEqual(
opOneLen,
il.And(
opOneLen,
ReadILOperand(il, xedd, addr, 0, 0),
il.Not(opTwoLen,
ReadILOperand(il, xedd, addr, 1, 1)
)
),
il.Const(opOneLen, 0)
)
)
)
);
break;
case XED_ICLASS_CTESTO:
ConditionalTest(addr, xedd, il, opOneLen, il.FlagCondition(LLFC_O));
break;
case XED_ICLASS_CTESTNO:
ConditionalTest(addr, xedd, il, opOneLen, il.FlagCondition(LLFC_NO));
break;
case XED_ICLASS_CTESTB:
ConditionalTest(addr, xedd, il, opOneLen, il.FlagGroup(IL_FLAG_GROUP_LT));
break;
case XED_ICLASS_CTESTNB:
ConditionalTest(addr, xedd, il, opOneLen, il.FlagGroup(IL_FLAG_GROUP_GE));
break;
case XED_ICLASS_CTESTZ:
ConditionalTest(addr, xedd, il, opOneLen, il.FlagGroup(IL_FLAG_GROUP_E));
break;
case XED_ICLASS_CTESTNZ:
ConditionalTest(addr, xedd, il, opOneLen, il.FlagGroup(IL_FLAG_GROUP_NE));
break;
case XED_ICLASS_CTESTBE:
ConditionalTest(addr, xedd, il, opOneLen, il.FlagGroup(IL_FLAG_GROUP_LE));
break;
case XED_ICLASS_CTESTNBE:
ConditionalTest(addr, xedd, il, opOneLen, il.FlagGroup(IL_FLAG_GROUP_GT));
break;
case XED_ICLASS_CTESTS:
ConditionalTest(addr, xedd, il, opOneLen, il.FlagCondition(LLFC_NEG));
break;
case XED_ICLASS_CTESTNS:
ConditionalTest(addr, xedd, il, opOneLen, il.FlagCondition(LLFC_POS));
break;
case XED_ICLASS_CTESTL:
ConditionalTest(addr, xedd, il, opOneLen, il.FlagCondition(LLFC_SLT));
break;
case XED_ICLASS_CTESTNL:
ConditionalTest(addr, xedd, il, opOneLen, il.FlagCondition(LLFC_SLT));
break;
case XED_ICLASS_CTESTLE:
ConditionalTest(addr, xedd, il, opOneLen, il.FlagCondition(LLFC_SLE));
break;
case XED_ICLASS_CTESTNLE:
ConditionalTest(addr, xedd, il, opOneLen, il.FlagCondition(LLFC_SGT));
break;
case XED_ICLASS_CTESTF:
AssignEvexDfv(xedd, il);
break;
case XED_ICLASS_XCHG:
il.AddInstruction(il.SetRegister(opOneLen, LLIL_TEMP(0), ReadILOperand(il, xedd, addr, 0, 0)));
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0, ReadILOperand(il, xedd, addr, 1, 1)));
il.AddInstruction(WriteILOperand(il, xedd, addr, 1, 1, il.Register(opOneLen, LLIL_TEMP(0))));
break;
case XED_ICLASS_CMPXCHG:
case XED_ICLASS_CMPXCHG_LOCK:
{
LowLevelILLabel trueLabel, falseLabel, doneLabel;
size_t cmpGranularity = xed_decoded_inst_operand_element_size_bits(xedd, 0) / 8;
xed_reg_enum_t cmpReg = xed_decoded_inst_get_reg(xedd, opTre_name);
il.AddInstruction(
il.If(
il.CompareEqual(
cmpGranularity,
il.Register(cmpGranularity, cmpReg),
ReadILOperand(il, xedd, addr, 0, 0)
), trueLabel, falseLabel
)
);
il.MarkLabel(trueLabel);
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
ReadILOperand(il, xedd, addr, 1, 1)
)
);
il.AddInstruction(il.SetFlag(IL_FLAG_Z, il.Const(1, 1)));
il.AddInstruction(il.Goto(doneLabel));
il.MarkLabel(falseLabel);
il.AddInstruction(
il.SetRegister(
cmpGranularity, cmpReg,
ReadILOperand(il, xedd, addr, 0, 0)
)
);
il.AddInstruction(il.SetFlag(IL_FLAG_Z, il.Const(1, 0)));
il.MarkLabel(doneLabel);
break;
}
case XED_ICLASS_CMPXCHG8B:
case XED_ICLASS_CMPXCHG8B_LOCK:
case XED_ICLASS_CMPXCHG16B:
case XED_ICLASS_CMPXCHG16B_LOCK:
{
// assembly: cmpxchg8b qword [rdi]
// $ ./xed-ex1 0fc70f
// Attempting to decode: 0f c7 0f
// iclass CMPXCHG8B category SEMAPHORE ISA-extension BASE ISA-set PENTIUMREAL
// instruction-length 3
// operand-width 32
// Operands
// # TYPE DETAILS VIS RW OC2 BITS BYTES NELEM ELEMSZ ELEMTYPE REGCLASS
// # ==== ======= === == === ==== ===== ===== ====== ======== ========
// 0 MEM0 (see below) EXPLICIT RCW Q 64 8 1 64 INT INVALID
// 1 REG0 REG0=EDX SUPPRESSED RCW D 32 4 1 32 INT GPR
// 2 REG1 REG1=EAX SUPPRESSED RCW D 32 4 1 32 INT GPR
// 3 REG2 REG2=ECX SUPPRESSED R D 32 4 1 32 INT GPR
// 4 REG3 REG3=EBX SUPPRESSED R D 32 4 1 32 INT GPR
// 5 REG4 REG4=EFLAGS SUPPRESSED W Y 32 4 1 32 INT FLAGS
// Memory Operands
// 0 read written SEG= DS BASE= EDI/GPR ASZ0=32
// MemopBytes = 8
LowLevelILLabel trueLabel, falseLabel, doneLabel;
size_t cmpGranularity = xed_decoded_inst_operand_element_size_bits(xedd, 1) / 8 * 2;
xed_reg_enum_t cmpRegHigh = xed_decoded_inst_get_reg(xedd, opTwo_name);
xed_reg_enum_t cmpRegLow = xed_decoded_inst_get_reg(xedd, opTre_name);
const xed_operand_t* const opFour = xed_inst_operand(xi, 3);
const xed_operand_enum_t opFour_name = xed_operand_name(opFour);
xed_reg_enum_t resultRegHigh = xed_decoded_inst_get_reg(xedd, opFour_name);
const xed_operand_t* const opFive = xed_inst_operand(xi, 4);
const xed_operand_enum_t opFive_name = xed_operand_name(opFive);
xed_reg_enum_t resultRegLow = xed_decoded_inst_get_reg(xedd, opFive_name);
il.AddInstruction(
il.If(
il.CompareEqual(
cmpGranularity,
il.RegisterSplit(cmpGranularity / 2, cmpRegHigh, cmpRegLow),
ReadILOperand(il, xedd, addr, 0, 0)
),
trueLabel, falseLabel
)
);
il.MarkLabel(trueLabel);
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.RegisterSplit(cmpGranularity / 2, resultRegHigh, resultRegLow)
)
);
il.AddInstruction(il.SetFlag(IL_FLAG_Z, il.Const(1, 1)));
il.AddInstruction(il.Goto(doneLabel));
il.MarkLabel(falseLabel);
il.AddInstruction(
il.SetRegisterSplit(cmpGranularity / 2, cmpRegHigh, cmpRegLow,
ReadILOperand(il, xedd, addr, 0, 0)
)
);
il.AddInstruction(il.SetFlag(IL_FLAG_Z, il.Const(1, 0)));
il.MarkLabel(doneLabel);
break;
}
case XED_ICLASS_XORPS:
case XED_ICLASS_PXOR:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Xor(opOneLen,
ReadILOperand(il, xedd, addr, 0, 0),
ReadILOperand(il, xedd, addr, 1, 1),
0)));
break;
case XED_ICLASS_XOR_LOCK: // TODO: Handle lock prefix
case XED_ICLASS_XOR:
{
ExprId left, right;
size_t opSize = opOneLen;
if (!newDataDestination)
{
// nothing special
left = ReadILOperand(il, xedd, addr, 0, 0);
right = ReadILOperand(il, xedd, addr, 1, 1);
}
else
{
// new data destination
left = ReadILOperand(il, xedd, addr, 1, 1);
right = ReadILOperand(il, xedd, addr, 2, 2);
opSize = opTwoLen;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Xor(opSize, left, right,
noFlags ? 0 : IL_FLAGWRITE_ALL)));
break;
}
case XED_ICLASS_VPXOR:
if (xed_classify_avx512(xedd))
{
LiftAsIntrinsic();
break;
}
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Xor(opOneLen,
ReadILOperand(il, xedd, addr, 1, 1),
ReadILOperand(il, xedd, addr, 2, 2),
0)));
break;
case XED_ICLASS_XADD:
case XED_ICLASS_XADD_LOCK:
il.AddInstruction(
il.SetRegister(opOneLen, LLIL_TEMP(0),
il.Add(opOneLen,
ReadILOperand(il, xedd, addr, 0, 0),
ReadILOperand(il, xedd, addr, 1, 1),
IL_FLAGWRITE_ALL)));
il.AddInstruction(
WriteILOperand(il, xedd, addr, 1, 1, ReadILOperand(il, xedd, addr, 0, 0)));
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0, il.Register(opOneLen, LLIL_TEMP(0))));
break;
case XED_ICLASS_JMP_FAR:
case XED_ICLASS_RET_FAR:
case XED_ICLASS_IRET:
case XED_ICLASS_IRETD:
case XED_ICLASS_IRETQ:
il.AddInstruction(il.Undefined());
return false;
case XED_ICLASS_UD2:
il.AddInstruction(il.Trap(TRAP_ILL));
return false;
case XED_ICLASS_SYSCALL:
case XED_ICLASS_SYSENTER:
il.AddInstruction(il.SystemCall());
break;
case XED_ICLASS_SYSEXIT:
case XED_ICLASS_HLT:
il.AddInstruction(il.Trap(TRAP_GPF));
return false;
case XED_ICLASS_SYSRET:
il.AddInstruction(il.Return(il.Register(addrSize, XED_REG_ECX)));
break;
case XED_ICLASS_SYSRET64:
case XED_ICLASS_SYSRET_AMD:
il.AddInstruction(il.Return(il.Register(addrSize, XED_REG_RCX)));
break;
case XED_ICLASS_FLD:
il.AddInstruction(
il.RegisterStackPush(10,
REG_STACK_X87,
ReadFloatILOperand(il, xedd, addr, 1, 1)));
break;
case XED_ICLASS_FILD:
il.AddInstruction(
il.RegisterStackPush(10,
REG_STACK_X87,
il.IntToFloat(10,
ReadILOperand(il, xedd, addr, 1, 1)),
IL_FLAGWRITE_X87C1Z));
break;
case XED_ICLASS_FLDZ:
il.AddInstruction(
il.RegisterStackPush(10,
REG_STACK_X87,
il.IntToFloat(10, il.Const(4, 0)),
IL_FLAGWRITE_X87C1Z));
break;
case XED_ICLASS_FLD1:
il.AddInstruction(
il.RegisterStackPush(10,
REG_STACK_X87,
il.IntToFloat(10, il.Const(4, 1)),
IL_FLAGWRITE_X87C1Z));
break;
case XED_ICLASS_FLDPI:
// Load 66-bit precision constant with two 33-bit components
il.AddInstruction(
il.RegisterStackPush(10,
REG_STACK_X87,
il.FloatAdd(10,
il.FloatConvert(10, il.FloatConstRaw(8, 0x400921fb54400000LL)),
il.FloatConvert(10, il.FloatConstRaw(8, 0x3de0b4611a600000LL))),
IL_FLAGWRITE_X87C1Z));
break;
case XED_ICLASS_FLDL2T:
// Load 66-bit precision constant with two 33-bit components
il.AddInstruction(
il.RegisterStackPush(10,
REG_STACK_X87,
il.FloatAdd(10,
il.FloatConvert(10, il.FloatConstRaw(8, 0x400a934f09700000LL)),
il.FloatConvert(10, il.FloatConstRaw(8, 0x3df346e2bf900000LL))),
IL_FLAGWRITE_X87C1Z));
break;
case XED_ICLASS_FLDL2E:
// Load 66-bit precision constant with two 33-bit components
il.AddInstruction(
il.RegisterStackPush(10,
REG_STACK_X87,
il.FloatAdd(10,
il.FloatConvert(10, il.FloatConstRaw(8, 0x3ff7154765200000LL)),
il.FloatConvert(10, il.FloatConstRaw(8, 0x3de705fc2ee00000LL))),
IL_FLAGWRITE_X87C1Z));
break;
case XED_ICLASS_FLDLG2:
// Load 66-bit precision constant with two 33-bit components
il.AddInstruction(
il.RegisterStackPush(10,
REG_STACK_X87,
il.FloatAdd(10,
il.FloatConvert(10, il.FloatConstRaw(8, 0x3fd3441350900000LL)),
il.FloatConvert(10, il.FloatConstRaw(8, 0x3dcef3fde6200000LL))),
IL_FLAGWRITE_X87C1Z));
break;
case XED_ICLASS_FLDLN2:
// Load 66-bit precision constant with two 33-bit components
il.AddInstruction(
il.RegisterStackPush(10,
REG_STACK_X87,
il.FloatAdd(10,
il.FloatConvert(10, il.FloatConstRaw(8, 0x3fe62e42fef00000LL)),
il.FloatConvert(10, il.FloatConstRaw(8, 0x3dd473de6af00000LL))),
IL_FLAGWRITE_X87C1Z));
break;
case XED_ICLASS_FST:
if (opOneLen != 10)
{
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.FloatConvert(opOneLen,
il.Register(10, XED_REG_ST0),
IL_FLAGWRITE_X87RND)));
}
else
{
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Register(10, XED_REG_ST0)));
}
break;
case XED_ICLASS_FSTP:
if (opOneLen != 10)
{
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.FloatConvert(opOneLen,
il.RegisterStackPop(10, REG_STACK_X87),
IL_FLAGWRITE_X87RND)));
}
else
{
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.RegisterStackPop(10, REG_STACK_X87, IL_FLAGWRITE_X87C1Z)));
}
break;
case XED_ICLASS_FIST:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.FloatToInt(opOneLen,
il.Register(10, XED_REG_ST0),
IL_FLAGWRITE_X87RND)));
break;
case XED_ICLASS_FISTP:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.FloatToInt(opOneLen,
il.RegisterStackPop(10, REG_STACK_X87),
IL_FLAGWRITE_X87RND)));
break;
case XED_ICLASS_FISTTP:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.FloatToInt(opOneLen,
il.RegisterStackPop(10, REG_STACK_X87),
IL_FLAGWRITE_X87RND)));
break;
case XED_ICLASS_FADD:
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatAdd(10,
ReadFloatILOperand(il, xedd, addr, 0, 0),
ReadFloatILOperand(il, xedd, addr, 1, 1),
IL_FLAGWRITE_X87RND)));
break;
case XED_ICLASS_FADDP:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.FloatAdd(10,
ReadFloatILOperand(il, xedd, addr, 0, 0),
ReadFloatILOperand(il, xedd, addr, 1, 1),
IL_FLAGWRITE_X87RND)));
il.AddInstruction(il.RegisterStackFreeReg(XED_REG_ST0));
il.AddInstruction(
il.SetRegister(2,
REG_X87_TOP,
il.Add(2,
il.Register(2, REG_X87_TOP),
il.Const(2, 1))));
break;
case XED_ICLASS_FIADD:
il.AddInstruction(
il.SetRegister(10, XED_REG_ST0,
il.FloatAdd(10,
il.Register(10, XED_REG_ST0),
il.IntToFloat(10,
ReadILOperand(il, xedd, addr, 1, 1)),
IL_FLAGWRITE_X87RND)));
break;
case XED_ICLASS_FSUB:
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatSub(10,
ReadFloatILOperand(il, xedd, addr, 0, 0),
ReadFloatILOperand(il, xedd, addr, 1, 1),
IL_FLAGWRITE_X87RND)));
break;
case XED_ICLASS_FSUBP:
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatSub(10,
ReadFloatILOperand(il, xedd, addr, 0, 0),
ReadFloatILOperand(il, xedd, addr, 1, 1),
IL_FLAGWRITE_X87RND)));
il.AddInstruction(il.RegisterStackFreeReg(XED_REG_ST0));
il.AddInstruction(il.SetRegister(2, REG_X87_TOP, il.Add(2, il.Register(2, REG_X87_TOP), il.Const(2, 1))));
break;
case XED_ICLASS_FISUB:
il.AddInstruction(il.SetRegister(10, XED_REG_ST0,
il.FloatSub(10,
il.Register(10, XED_REG_ST0),
il.IntToFloat(10, ReadILOperand(il, xedd, addr, 1, 1)),
IL_FLAGWRITE_X87RND)));
break;
case XED_ICLASS_FSUBR:
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatSub(10,
ReadFloatILOperand(il, xedd, addr, 1, 1),
ReadFloatILOperand(il, xedd, addr, 0, 0),
IL_FLAGWRITE_X87RND)));
break;
case XED_ICLASS_FSUBRP:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.FloatSub(10,
ReadFloatILOperand(il, xedd, addr, 1, 1),
ReadFloatILOperand(il, xedd, addr, 0, 0),
IL_FLAGWRITE_X87RND)));
il.AddInstruction(il.RegisterStackFreeReg(XED_REG_ST0));
il.AddInstruction(il.SetRegister(2, REG_X87_TOP, il.Add(2, il.Register(2, REG_X87_TOP), il.Const(2, 1))));
break;
case XED_ICLASS_FISUBR:
il.AddInstruction(
il.SetRegister(10,
XED_REG_ST0,
il.FloatSub(10,
il.IntToFloat(10,
ReadILOperand(il, xedd, addr, 1, 1)),
il.Register(10, XED_REG_ST0),
IL_FLAGWRITE_X87RND)));
break;
case XED_ICLASS_FMUL:
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatMult(10,
ReadFloatILOperand(il, xedd, addr, 0, 0),
ReadFloatILOperand(il, xedd, addr, 1, 1),
IL_FLAGWRITE_X87RND)));
break;
case XED_ICLASS_FMULP:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.FloatMult(10,
ReadFloatILOperand(il, xedd, addr, 0, 0),
ReadFloatILOperand(il, xedd, addr, 1, 1),
IL_FLAGWRITE_X87RND)));
il.AddInstruction(il.RegisterStackFreeReg(XED_REG_ST0));
il.AddInstruction(il.SetRegister(2, REG_X87_TOP, il.Add(2, il.Register(2, REG_X87_TOP), il.Const(2, 1))));
break;
case XED_ICLASS_FIMUL:
il.AddInstruction(
il.SetRegister(10, XED_REG_ST0,
il.FloatMult(10,
il.Register(10, XED_REG_ST0),
il.IntToFloat(10,
ReadILOperand(il, xedd, addr, 1, 1)),
IL_FLAGWRITE_X87RND)));
break;
case XED_ICLASS_FDIV:
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.FloatDiv(10,
ReadFloatILOperand(il, xedd, addr, 0, 0),
ReadFloatILOperand(il, xedd, addr, 1, 1),
IL_FLAGWRITE_X87RND)));
break;
case XED_ICLASS_FDIVP:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.FloatDiv(10,
ReadFloatILOperand(il, xedd, addr, 0, 0),
ReadFloatILOperand(il, xedd, addr, 1, 1),
IL_FLAGWRITE_X87RND)));
il.AddInstruction(il.RegisterStackFreeReg(XED_REG_ST0));
il.AddInstruction(il.SetRegister(2, REG_X87_TOP, il.Add(2, il.Register(2, REG_X87_TOP), il.Const(2, 1))));
break;
case XED_ICLASS_FIDIV:
il.AddInstruction(
il.SetRegister(10, XED_REG_ST0,
il.FloatDiv(10,
il.Register(10, XED_REG_ST0),
il.IntToFloat(10,
ReadILOperand(il, xedd, addr, 1, 1)),
IL_FLAGWRITE_X87RND)));
break;
case XED_ICLASS_FDIVR:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.FloatDiv(10,
ReadFloatILOperand(il, xedd, addr, 1, 1),
ReadFloatILOperand(il, xedd, addr, 0, 0),
IL_FLAGWRITE_X87RND)));
break;
case XED_ICLASS_FDIVRP:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.FloatDiv(10,
ReadFloatILOperand(il, xedd, addr, 1, 1),
il.Register(10, XED_REG_ST0))));
il.AddInstruction(il.RegisterStackFreeReg(XED_REG_ST0));
il.AddInstruction(il.SetRegister(2, REG_X87_TOP, il.Add(2, il.Register(2, REG_X87_TOP), il.Const(2, 1))));
break;
case XED_ICLASS_FIDIVR:
il.AddInstruction(
il.SetRegister(10, XED_REG_ST0,
il.FloatDiv(10,
il.IntToFloat(10,
ReadILOperand(il, xedd, addr, 1, 1)),
il.Register(10, XED_REG_ST0),
IL_FLAGWRITE_X87RND)));
break;
case XED_ICLASS_FABS:
il.AddInstruction(il.SetRegister(10, XED_REG_ST0, il.FloatAbs(10, il.Register(10, XED_REG_ST0), IL_FLAGWRITE_X87C1Z)));
break;
case XED_ICLASS_FCHS:
il.AddInstruction(il.SetRegister(10, XED_REG_ST0, il.FloatNeg(10, il.Register(10, XED_REG_ST0), IL_FLAGWRITE_X87C1Z)));
break;
case XED_ICLASS_FDECSTP:
il.AddInstruction(il.SetRegister(2, REG_X87_TOP, il.Sub(2, il.Register(2, REG_X87_TOP), il.Const(2, 1),
IL_FLAGWRITE_X87C1Z)));
break;
case XED_ICLASS_FINCSTP:
il.AddInstruction(il.SetRegister(2, REG_X87_TOP, il.Add(2, il.Register(2, REG_X87_TOP), il.Const(2, 1),
IL_FLAGWRITE_X87C1Z)));
break;
case XED_ICLASS_FFREE:
il.AddInstruction(il.RegisterStackFreeReg(regOne));
break;
case XED_ICLASS_EMMS:
for (uint32_t i = 0; i < 8; i++)
il.AddInstruction(il.RegisterStackFreeReg(REG_X87_r(i)));
break;
case XED_ICLASS_FNINIT:
for (uint32_t i = 0; i < 8; i++)
il.AddInstruction(il.RegisterStackFreeReg(REG_X87_r(i)));
il.AddInstruction(il.SetRegister(2, REG_X87_TOP, il.Const(2, 0)));
break;
case XED_ICLASS_FSQRT:
il.AddInstruction(il.SetRegister(10, XED_REG_ST0, il.FloatSqrt(10, il.Register(10, XED_REG_ST0), IL_FLAGWRITE_X87RND)));
break;
case XED_ICLASS_FXCH:
il.AddInstruction(il.SetRegister(10, LLIL_TEMP(0), ReadFloatILOperand(il, xedd, addr, 0, 0), IL_FLAGWRITE_X87C1Z));
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0, ReadFloatILOperand(il, xedd, addr, 1, 1)));
il.AddInstruction(WriteILOperand(il, xedd, addr, 1, 1, il.Register(10, LLIL_TEMP(0))));
break;
case XED_ICLASS_VUCOMISS:
case XED_ICLASS_UCOMISS:
case XED_ICLASS_COMISS:
il.AddInstruction(
il.FloatSub(4,
ReadFloatILOperand(il, xedd, addr, 0, 0, 4),
ReadFloatILOperand(il, xedd, addr, 1, 1, 4),
IL_FLAGWRITE_VCOMI));
break;
case XED_ICLASS_VUCOMISD:
case XED_ICLASS_UCOMISD:
case XED_ICLASS_COMISD:
il.AddInstruction(
il.FloatSub(8,
ReadFloatILOperand(il, xedd, addr, 0, 0, 8),
ReadFloatILOperand(il, xedd, addr, 1, 1, 8),
IL_FLAGWRITE_VCOMI));
break;
case XED_ICLASS_FCOMI:
case XED_ICLASS_FUCOMI:
il.AddInstruction(
il.FloatSub(10,
ReadFloatILOperand(il, xedd, addr, 0, 0),
ReadFloatILOperand(il, xedd, addr, 1, 1),
IL_FLAGWRITE_X87COMI));
break;
case XED_ICLASS_FCOMIP:
case XED_ICLASS_FUCOMIP:
il.AddInstruction(
il.FloatSub(10,
ReadFloatILOperand(il, xedd, addr, 0, 0),
ReadFloatILOperand(il, xedd, addr, 1, 1),
IL_FLAGWRITE_X87COMI));
il.AddInstruction(il.RegisterStackFreeReg(XED_REG_ST0));
il.AddInstruction(il.SetRegister(2, REG_X87_TOP, il.Add(2, il.Register(2, REG_X87_TOP), il.Const(2, 1))));
break;
case XED_ICLASS_FCOM:
case XED_ICLASS_FUCOM:
il.AddInstruction(
il.FloatSub(10,
il.Register(10, XED_REG_ST0),
ReadFloatILOperand(il, xedd, addr, 1, 1),
IL_FLAGWRITE_X87COM));
break;
case XED_ICLASS_FICOM:
il.AddInstruction(
il.FloatSub(10,
il.Register(10, XED_REG_ST0),
il.IntToFloat(10,
ReadILOperand(il, xedd, addr, 1, 1)),
IL_FLAGWRITE_X87COM));
break;
case XED_ICLASS_FCOMP:
case XED_ICLASS_FUCOMP:
il.AddInstruction(
il.FloatSub(10,
il.Register(10, XED_REG_ST0),
ReadFloatILOperand(il, xedd, addr, 1, 1),
IL_FLAGWRITE_X87COM));
il.AddInstruction(il.RegisterStackFreeReg(XED_REG_ST0));
il.AddInstruction(il.SetRegister(2, REG_X87_TOP, il.Add(2, il.Register(2, REG_X87_TOP), il.Const(2, 1))));
break;
case XED_ICLASS_FICOMP:
il.AddInstruction(
il.FloatSub(10,
il.Register(10, XED_REG_ST0),
il.IntToFloat(10,
ReadILOperand(il, xedd, addr, 1, 1)),
IL_FLAGWRITE_X87COM));
il.AddInstruction(il.RegisterStackFreeReg(XED_REG_ST0));
il.AddInstruction(il.SetRegister(2, REG_X87_TOP, il.Add(2, il.Register(2, REG_X87_TOP), il.Const(2, 1))));
break;
case XED_ICLASS_FCOMPP:
case XED_ICLASS_FUCOMPP:
il.AddInstruction(il.FloatSub(10, il.Register(10, XED_REG_ST0), il.Register(10, XED_REG_ST1),
IL_FLAGWRITE_X87COM));
il.AddInstruction(il.RegisterStackFreeReg(XED_REG_ST0));
il.AddInstruction(il.RegisterStackFreeReg(XED_REG_ST1));
il.AddInstruction(il.SetRegister(2, REG_X87_TOP, il.Add(2, il.Register(2, REG_X87_TOP), il.Const(2, 2))));
break;
case XED_ICLASS_FTST:
il.AddInstruction(il.FloatSub(10, il.Register(10, XED_REG_ST0),
il.IntToFloat(10, il.Const(4, 0)), IL_FLAGWRITE_X87COM));
break;
case XED_ICLASS_FNSTSW:
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Or(2,
il.FlagBit(2, IL_FLAG_C0, 8),
il.Or(2,
il.FlagBit(2, IL_FLAG_C1, 9),
il.Or(2,
il.FlagBit(2, IL_FLAG_C2, 10),
il.Or(2,
il.FlagBit(2, IL_FLAG_C3, 14),
il.ShiftLeft(2,
il.And(2,
il.Register(2, REG_X87_TOP),
il.Const(2, 7)),
il.Const(2, 11))))))));
break;
case XED_ICLASS_F2XM1:
il.AddInstruction(il.Intrinsic(vector<RegisterOrFlag> { RegisterOrFlag::Register(XED_REG_ST0), RegisterOrFlag::Flag(IL_FLAG_C1)},
INTRINSIC_F2XM1, vector<ExprId> { il.Register(10, XED_REG_ST0) }, IL_FLAGWRITE_X87RND));
break;
case XED_ICLASS_FBLD:
il.AddInstruction(il.SetRegister(2, REG_X87_TOP, il.Sub(2, il.Register(2, REG_X87_TOP), il.Const(2, 1))));
il.AddInstruction(il.Intrinsic(vector<RegisterOrFlag> { RegisterOrFlag::Register(XED_REG_ST0), RegisterOrFlag::Flag(IL_FLAG_C1) },
INTRINSIC_FBLD, vector<ExprId> { ReadILOperand(il, xedd, addr, 1, 1) }, IL_FLAGWRITE_X87C1Z));
break;
case XED_ICLASS_FBSTP:
il.AddInstruction(il.Intrinsic(vector<RegisterOrFlag> { RegisterOrFlag::Register(LLIL_TEMP(0)) },
INTRINSIC_FBST, vector<ExprId> { il.Register(10, XED_REG_ST0) }, IL_FLAGWRITE_X87RND));
il.AddInstruction(
WriteILOperand(il, xedd, addr, 0, 0,
il.Register(10, LLIL_TEMP(0))));
il.AddInstruction(il.RegisterStackFreeReg(XED_REG_ST0));
il.AddInstruction(il.SetRegister(2, REG_X87_TOP, il.Add(2, il.Register(2, REG_X87_TOP), il.Const(2, 1))));
break;
case XED_ICLASS_FSIN:
il.AddInstruction(il.Intrinsic(vector<RegisterOrFlag> { RegisterOrFlag::Register(XED_REG_ST0),
RegisterOrFlag::Flag(IL_FLAG_C1), RegisterOrFlag::Flag(IL_FLAG_C2) }, INTRINSIC_FSIN, vector<ExprId> { il.Register(10, XED_REG_ST0) },
IL_FLAGWRITE_X87RND));
break;
case XED_ICLASS_FCOS:
il.AddInstruction(il.Intrinsic(vector<RegisterOrFlag> { RegisterOrFlag::Register(XED_REG_ST0),
RegisterOrFlag::Flag(IL_FLAG_C1), RegisterOrFlag::Flag(IL_FLAG_C2) }, INTRINSIC_FCOS, vector<ExprId> { il.Register(10, XED_REG_ST0) },
IL_FLAGWRITE_X87RND));
break;
case XED_ICLASS_FSINCOS:
il.AddInstruction(il.SetRegister(2, REG_X87_TOP, il.Sub(2, il.Register(2, REG_X87_TOP), il.Const(2, 1))));
il.AddInstruction(il.Intrinsic(vector<RegisterOrFlag> { RegisterOrFlag::Register(XED_REG_ST1),
RegisterOrFlag::Register(XED_REG_ST0), RegisterOrFlag::Flag(IL_FLAG_C1), RegisterOrFlag::Flag(IL_FLAG_C2) }, INTRINSIC_FSINCOS,
vector<ExprId> { il.Register(10, XED_REG_ST1) }, IL_FLAGWRITE_X87RND));
break;
case XED_ICLASS_FPATAN:
il.AddInstruction(il.Intrinsic(vector<RegisterOrFlag> { RegisterOrFlag::Register(XED_REG_ST1), RegisterOrFlag::Flag(IL_FLAG_C1) },
INTRINSIC_FPATAN, vector<ExprId> { il.Register(10, XED_REG_ST0), il.Register(10, XED_REG_ST1) }, IL_FLAGWRITE_X87RND));
il.AddInstruction(il.RegisterStackFreeReg(XED_REG_ST0));
il.AddInstruction(il.SetRegister(2, REG_X87_TOP, il.Add(2, il.Register(2, REG_X87_TOP), il.Const(2, 1))));
break;
case XED_ICLASS_FPREM:
il.AddInstruction(il.Intrinsic(vector<RegisterOrFlag> { RegisterOrFlag::Register(XED_REG_ST0),
RegisterOrFlag::Flag(IL_FLAG_C2), RegisterOrFlag::Register(LLIL_TEMP(0)) }, INTRINSIC_FPREM,
vector<ExprId> { il.Register(10, XED_REG_ST0), il.Register(10, XED_REG_ST1) }));
il.AddInstruction(il.If(il.Flag(IL_FLAG_C2), doneLabel, falseLabel));
il.MarkLabel(falseLabel);
il.AddInstruction(il.SetFlag(IL_FLAG_C0, il.CompareNotEqual(1,
il.And(1, il.Register(1, LLIL_TEMP(0)), il.Const(1, 4)), il.Const(1, 0))));
il.AddInstruction(il.SetFlag(IL_FLAG_C1, il.CompareNotEqual(1,
il.And(1, il.Register(1, LLIL_TEMP(0)), il.Const(1, 1)), il.Const(1, 0))));
il.AddInstruction(il.SetFlag(IL_FLAG_C3, il.CompareNotEqual(1,
il.And(1, il.Register(1, LLIL_TEMP(0)), il.Const(1, 2)), il.Const(1, 0))));
il.AddInstruction(il.Goto(doneLabel));
il.MarkLabel(doneLabel);
break;
case XED_ICLASS_FPREM1:
il.AddInstruction(il.Intrinsic(vector<RegisterOrFlag> { RegisterOrFlag::Register(XED_REG_ST0),
RegisterOrFlag::Flag(IL_FLAG_C2), RegisterOrFlag::Register(LLIL_TEMP(0)) }, INTRINSIC_FPREM1,
vector<ExprId> { il.Register(10, XED_REG_ST0), il.Register(10, XED_REG_ST1) }));
il.AddInstruction(il.If(il.Flag(IL_FLAG_C2), doneLabel, falseLabel));
il.MarkLabel(falseLabel);
il.AddInstruction(il.SetFlag(IL_FLAG_C0, il.CompareNotEqual(1,
il.And(1, il.Register(1, LLIL_TEMP(0)), il.Const(1, 4)), il.Const(1, 0))));
il.AddInstruction(il.SetFlag(IL_FLAG_C1, il.CompareNotEqual(1,
il.And(1, il.Register(1, LLIL_TEMP(0)), il.Const(1, 1)), il.Const(1, 0))));
il.AddInstruction(il.SetFlag(IL_FLAG_C3, il.CompareNotEqual(1,
il.And(1, il.Register(1, LLIL_TEMP(0)), il.Const(1, 2)), il.Const(1, 0))));
il.AddInstruction(il.Goto(doneLabel));
il.MarkLabel(doneLabel);
break;
case XED_ICLASS_FPTAN:
il.AddInstruction(il.Intrinsic(vector<RegisterOrFlag> { RegisterOrFlag::Register(XED_REG_ST0),
RegisterOrFlag::Flag(IL_FLAG_C1), RegisterOrFlag::Flag(IL_FLAG_C2) }, INTRINSIC_FPTAN, vector<ExprId> { il.Register(10, XED_REG_ST0) },
IL_FLAGWRITE_X87RND));
il.AddInstruction(il.If(il.Flag(IL_FLAG_C2), doneLabel, falseLabel));
il.MarkLabel(falseLabel);
il.AddInstruction(il.RegisterStackPush(10, REG_STACK_X87, il.IntToFloat(10, il.Const(4, 1))));
il.AddInstruction(il.Goto(doneLabel));
il.MarkLabel(doneLabel);
break;
case XED_ICLASS_FRNDINT:
il.AddInstruction(il.SetRegister(10, XED_REG_ST0, il.RoundToInt(10, il.Register(10, XED_REG_ST0), IL_FLAGWRITE_X87RND)));
break;
case XED_ICLASS_FSCALE:
il.AddInstruction(il.Intrinsic(vector<RegisterOrFlag> { RegisterOrFlag::Register(XED_REG_ST0), RegisterOrFlag::Flag(IL_FLAG_C1) },
INTRINSIC_FSCALE, vector<ExprId> { il.Register(10, XED_REG_ST0), il.Register(10, XED_REG_ST1) },
IL_FLAGWRITE_X87RND));
break;
case XED_ICLASS_FXAM:
il.AddInstruction(il.Intrinsic(vector<RegisterOrFlag> { RegisterOrFlag::Flag(IL_FLAG_C0),
RegisterOrFlag::Flag(IL_FLAG_C1), RegisterOrFlag::Flag(IL_FLAG_C2), RegisterOrFlag::Flag(IL_FLAG_C3) },
INTRINSIC_FXAM, vector<ExprId> { il.Register(10, XED_REG_ST0) }));
break;
case XED_ICLASS_FXTRACT:
il.AddInstruction(il.SetRegister(2, REG_X87_TOP, il.Sub(2, il.Register(2, REG_X87_TOP), il.Const(2, 1))));
il.AddInstruction(il.Intrinsic(vector<RegisterOrFlag> { RegisterOrFlag::Register(XED_REG_ST0),
RegisterOrFlag::Register(XED_REG_ST1) }, INTRINSIC_FXTRACT, vector<ExprId> { il.Register(10, XED_REG_ST1) },
IL_FLAGWRITE_X87C1Z));
break;
case XED_ICLASS_FYL2X:
il.AddInstruction(il.Intrinsic(vector<RegisterOrFlag> { RegisterOrFlag::Register(XED_REG_ST1), RegisterOrFlag::Flag(IL_FLAG_C1)},
INTRINSIC_FYL2X, vector<ExprId> { il.Register(10, XED_REG_ST0), il.Register(10, XED_REG_ST1) },
IL_FLAGWRITE_X87RND));
il.AddInstruction(il.RegisterStackFreeReg(XED_REG_ST0));
il.AddInstruction(il.SetRegister(2, REG_X87_TOP, il.Add(2, il.Register(2, REG_X87_TOP), il.Const(2, 1))));
break;
case XED_ICLASS_FYL2XP1:
il.AddInstruction(il.Intrinsic(vector<RegisterOrFlag> { RegisterOrFlag::Register(XED_REG_ST1), RegisterOrFlag::Flag(IL_FLAG_C1)},
INTRINSIC_FYL2XP1, vector<ExprId> { il.Register(10, XED_REG_ST0), il.Register(10, XED_REG_ST1) },
IL_FLAGWRITE_X87RND));
il.AddInstruction(il.RegisterStackFreeReg(XED_REG_ST0));
il.AddInstruction(il.SetRegister(2, REG_X87_TOP, il.Add(2, il.Register(2, REG_X87_TOP), il.Const(2, 1))));
break;
case XED_ICLASS_BSWAP:
// BSWAP reverses the bytes of its single register operand in place. x86 leaves the flags
// unaffected, matching the native operation.
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.ByteSwap(opOneLen, ReadILOperand(il, xedd, addr, 0, 0))));
break;
case XED_ICLASS_TZCNT:
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.CountTrailingZeros(opOneLen, ReadILOperand(il, xedd, addr, 1, 1), IL_FLAGWRITE_LZTZCNT)));
break;
case XED_ICLASS_LZCNT:
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.CountLeadingZeros(opOneLen, ReadILOperand(il, xedd, addr, 1, 1), IL_FLAGWRITE_LZTZCNT)));
break;
case XED_ICLASS_POPCNT:
il.AddInstruction(WriteILOperand(il, xedd, addr, 0, 0,
il.PopulationCount(opOneLen, ReadILOperand(il, xedd, addr, 1, 1), IL_FLAGWRITE_POPCNT)));
break;
default:
LiftAsIntrinsic();
break;
}
return true;
}
|