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
|
#include "lowlevelilinstruction.h"
#include <cstring>
#include <inttypes.h>
#include <stdarg.h>
#include "il.h"
#include "neon_intrinsics.h"
#include "sysregs_gen.h"
using namespace BinaryNinja;
#include "il_macros.h"
static uint32_t GetFlagWriteTypeForEffect(FlagEffect e) {
switch (e) {
case FLAGEFFECT_SETS:
case FLAGEFFECT_SETS_NORMAL:
return IL_FLAG_WRITE_ALL;
case FLAGEFFECT_SETS_FLOAT:
return IL_FLAG_WRITE_ALL_FLOAT;
case FLAGEFFECT_NONE:
default:
return 0;
}
}
static ExprId GetCondition(LowLevelILFunction& il, Condition cond)
{
switch (cond)
{
case COND_EQ:
return il.FlagGroup(IL_FLAG_GROUP_EQ);
case COND_NE:
return il.FlagGroup(IL_FLAG_GROUP_NE);
case COND_CS:
return il.FlagGroup(IL_FLAG_GROUP_CS);
case COND_CC:
return il.FlagGroup(IL_FLAG_GROUP_CC);
case COND_MI:
return il.FlagGroup(IL_FLAG_GROUP_MI);
case COND_PL:
return il.FlagGroup(IL_FLAG_GROUP_PL);
case COND_VS:
return il.FlagGroup(IL_FLAG_GROUP_VS);
case COND_VC:
return il.FlagGroup(IL_FLAG_GROUP_VC);
case COND_HI:
return il.FlagGroup(IL_FLAG_GROUP_HI);
case COND_LS:
return il.FlagGroup(IL_FLAG_GROUP_LS);
case COND_GE:
return il.FlagGroup(IL_FLAG_GROUP_GE);
case COND_LT:
return il.FlagGroup(IL_FLAG_GROUP_LT);
case COND_GT:
return il.FlagGroup(IL_FLAG_GROUP_GT);
case COND_LE:
return il.FlagGroup(IL_FLAG_GROUP_LE);
case COND_AL:
return il.Const(0, 1); // Always branch
case COND_NV:
default:
return il.Const(0, 0); // Never branch
}
}
static void GenIfElse(LowLevelILFunction& il, ExprId clause, ExprId trueCase, ExprId falseCase)
{
if (falseCase)
{
LowLevelILLabel trueCode, falseCode, done;
il.AddInstruction(il.If(clause, trueCode, falseCode));
il.MarkLabel(trueCode);
il.AddInstruction(trueCase);
il.AddInstruction(il.Goto(done));
il.MarkLabel(falseCode);
il.AddInstruction(falseCase);
il.AddInstruction(il.Goto(done));
il.MarkLabel(done);
}
else
{
LowLevelILLabel trueCode, done;
il.AddInstruction(il.If(clause, trueCode, done));
il.MarkLabel(trueCode);
il.AddInstruction(trueCase);
il.MarkLabel(done);
}
return;
}
ExprId ExtractImmediate(LowLevelILFunction& il, InstructionOperand& operand, int sizeof_imm)
{
if (operand.operandClass != IMM32 && operand.operandClass != IMM64)
return il.Unimplemented();
uint64_t imm = operand.immediate;
if (operand.shiftValueUsed)
{
switch (operand.shiftType)
{
case ShiftType_LSL:
imm = imm << operand.shiftValue;
break;
case ShiftType_LSR:
imm = imm >> operand.shiftValue;
break;
case ShiftType_MSL:
imm = (imm << operand.shiftValue) | ONES(operand.shiftValue);
break;
case ShiftType_ASR:
case ShiftType_ROR:
case ShiftType_UXTW:
case ShiftType_SXTW:
case ShiftType_SXTX:
case ShiftType_UXTX:
case ShiftType_SXTB:
case ShiftType_SXTH:
case ShiftType_UXTH:
case ShiftType_UXTB:
case ShiftType_END:
default:
return il.Unimplemented();
}
}
return ILCONST(sizeof_imm, imm & ONES(sizeof_imm * 8));
}
// extractSize can be smaller than the register, generating an LLIL_LOWPART
// resultSize can be larger than the register, generating sign or zero extension
ExprId ExtractRegister(LowLevelILFunction& il, InstructionOperand& operand, size_t regNum,
size_t extractSize, bool signExtend, size_t resultSize)
{
size_t opsz = get_register_size(operand.reg[regNum]);
if (IS_ZERO_REG(operand.reg[regNum]))
return il.Const(resultSize, 0);
ExprId res = 0;
switch (operand.operandClass)
{
case SYS_REG:
res = il.Register(opsz, operand.sysreg);
break;
case REG:
default:
res = il.Register(opsz, operand.reg[regNum]);
break;
}
if (extractSize < opsz)
res = il.LowPart(extractSize, res);
if (extractSize < resultSize || opsz < extractSize)
{
if (signExtend)
res = il.SignExtend(resultSize, res);
else
res = il.ZeroExtend(resultSize, res);
}
return res;
}
static ExprId GetFloat(LowLevelILFunction& il, InstructionOperand& operand, int float_sz)
{
if (operand.operandClass == FIMM32)
{
switch (float_sz)
{
case 2:
return il.FloatConstRaw(2, operand.immediate);
case 4:
return il.FloatConstSingle(std::bit_cast<float>(static_cast<uint32_t>(operand.immediate)));
case 8:
return il.FloatConstDouble(std::bit_cast<float>(static_cast<uint32_t>(operand.immediate)));
default:
break;
}
}
else if (operand.operandClass == REG)
{
return il.FloatConvert(
float_sz, ExtractRegister(il, operand, 0, REGSZ_O(operand), false, REGSZ_O(operand)));
}
return il.Unimplemented();
}
static ExprId GetShiftedRegister(
LowLevelILFunction& il, InstructionOperand& operand, size_t regNum, size_t resultSize)
{
ExprId res;
// peel off the variants that return early
switch (operand.shiftType)
{
case ShiftType_NONE:
res = ExtractRegister(il, operand, regNum, REGSZ_O(operand), false, resultSize);
return res;
case ShiftType_ASR:
res = ExtractRegister(il, operand, regNum, REGSZ_O(operand), false, resultSize);
if (operand.shiftValue)
res = il.ArithShiftRight(resultSize, res, il.Const(1, operand.shiftValue));
return res;
case ShiftType_LSR:
res = ExtractRegister(il, operand, regNum, REGSZ_O(operand), false, resultSize);
if (operand.shiftValue)
res = il.LogicalShiftRight(resultSize, res, il.Const(1, operand.shiftValue));
return res;
case ShiftType_ROR:
res = ExtractRegister(il, operand, regNum, REGSZ_O(operand), false, resultSize);
if (operand.shiftValue)
res = il.RotateRight(resultSize, res, il.Const(1, operand.shiftValue));
return res;
default:
break;
}
// everything else falls through to maybe be left shifted
switch (operand.shiftType)
{
case ShiftType_LSL:
res = ExtractRegister(il, operand, regNum, REGSZ_O(operand), false, resultSize);
break;
case ShiftType_SXTB:
res = ExtractRegister(il, operand, regNum, 1, true, resultSize);
break;
case ShiftType_SXTH:
res = ExtractRegister(il, operand, regNum, 2, true, resultSize);
break;
case ShiftType_SXTW:
res = ExtractRegister(il, operand, regNum, 4, true, resultSize);
break;
case ShiftType_SXTX:
res = ExtractRegister(il, operand, regNum, 8, true, resultSize);
break;
case ShiftType_UXTB:
res = ExtractRegister(il, operand, regNum, 1, false, resultSize);
break;
case ShiftType_UXTH:
res = ExtractRegister(il, operand, regNum, 2, false, resultSize);
break;
case ShiftType_UXTW:
res = ExtractRegister(il, operand, regNum, 4, false, resultSize);
break;
case ShiftType_UXTX:
res = ExtractRegister(il, operand, regNum, 8, false, resultSize);
break;
default:
il.AddInstruction(il.Unimplemented());
return il.Unimplemented();
}
if (operand.shiftValue)
res = il.ShiftLeft(resultSize, res, il.Const(1, operand.shiftValue));
return res;
}
static ExprId GetILOperandPreOrPostIndex(LowLevelILFunction& il, InstructionOperand& operand)
{
if (operand.operandClass != MEM_PRE_IDX && operand.operandClass != MEM_POST_IDX)
return 0;
if (operand.reg[1] == REG_NONE)
{
// ..., [Xn], #imm
if (IMM_O(operand) == 0)
return 0;
return ILSETREG_O(operand, ILADDREG_O(operand, il.Const(REGSZ_O(operand), IMM_O(operand))));
}
else
{
// ..., [Xn], <Xm>
return ILSETREG_O(operand, ILADDREG_O(operand, il.Register(8, operand.reg[1])));
}
}
/* Returns an expression that does any pre-incrementing on an operand, if it exists */
static ExprId GetILOperandPreIndex(LowLevelILFunction& il, InstructionOperand& operand)
{
if (operand.operandClass != MEM_PRE_IDX)
return 0;
return GetILOperandPreOrPostIndex(il, operand);
}
/* Returns an expression that does any post-incrementing on an operand, if it exists */
static ExprId GetILOperandPostIndex(LowLevelILFunction& il, InstructionOperand& operand)
{
if (operand.operandClass != MEM_POST_IDX)
return 0;
return GetILOperandPreOrPostIndex(il, operand);
}
/* Returns an IL expression that reads (and only reads) from the operand.
It accounts for, but does not generate IL that executes, pre and post indexing.
The operand class can be overridden.
An additional offset can be applied, convenient for calculating sequential loads and stores. */
static ExprId GetILOperandEffectiveAddress(LowLevelILFunction& il, InstructionOperand& operand,
size_t addrSize, OperandClass oclass, size_t extra_offset)
{
ExprId addr = 0;
if (oclass == NONE)
oclass = operand.operandClass;
switch (oclass)
{
case MEM_REG: // ldr x0, [x1]
case MEM_POST_IDX: // ldr w0, [x1], #4
addr = ILREG_O(operand);
if (extra_offset)
addr = il.Add(addrSize, addr, il.Const(addrSize, extra_offset));
break;
case MEM_OFFSET: // ldr w0, [x1, #4]
case MEM_PRE_IDX: // ldr w0, [x1, #4]!
addr = il.Add(addrSize, ILREG_O(operand), il.Const(addrSize, operand.immediate + extra_offset));
break;
case MEM_EXTENDED:
if (operand.shiftType == ShiftType_NONE)
{
addr =
il.Add(addrSize, ILREG_O(operand), il.Const(addrSize, operand.immediate + extra_offset));
}
else if (operand.shiftType == ShiftType_LSL)
{
if (extra_offset)
{
addr = il.Add(addrSize, ILREG_O(operand),
il.Add(addrSize,
il.ShiftLeft(addrSize, il.Const(addrSize, operand.immediate),
il.Const(1, operand.shiftValue)),
il.Const(addrSize, extra_offset)));
}
else
{
addr = il.Add(addrSize, ILREG_O(operand),
il.ShiftLeft(
addrSize, il.Const(addrSize, operand.immediate), il.Const(1, operand.shiftValue)));
}
}
else
{
// printf("ERROR: dunno how to handle MEM_EXTENDED shiftType %d\n", operand.shiftType);
ABORT_LIFT;
}
break;
default:
// printf("ERROR: dunno how to handle operand class %d\n", oclass);
ABORT_LIFT;
}
return addr;
}
static size_t ReadILOperand(LowLevelILFunction& il, InstructionOperand& operand, size_t resultSize)
{
switch (operand.operandClass)
{
case IMM32:
case IMM64:
if (operand.shiftType != ShiftType_NONE && operand.shiftValue)
return il.Const(resultSize, operand.immediate << operand.shiftValue);
else
return il.Const(resultSize, operand.immediate);
case LABEL:
return il.ConstPointer(8, operand.immediate);
case REG:
if (IS_ZERO_REG(operand.reg[0]))
return il.Const(resultSize, 0);
return GetShiftedRegister(il, operand, 0, resultSize);
case MEM_REG:
return il.Load(resultSize, il.Register(8, operand.reg[0]));
case MEM_OFFSET:
if (operand.immediate != 0)
return il.Load(
resultSize, il.Add(8, il.Register(8, operand.reg[0]), il.Const(8, operand.immediate)));
else
return il.Load(resultSize, il.Register(8, operand.reg[0]));
case MEM_EXTENDED:
return il.Load(resultSize, GetILOperandEffectiveAddress(il, operand, resultSize, NONE, 0));
case MEM_PRE_IDX:
case MEM_POST_IDX:
case MULTI_REG:
case FIMM32:
return GetFloat(il, operand, resultSize);
case NONE:
default:
return il.Unimplemented();
}
}
unsigned v_unpack_lookup_sz[15] = {0, 1, 2, 4, 8, 16, 1, 2, 4, 8, 1, 2, 4, 1, 1};
extern "C" Register* v_unpack_lookup[15][32];
Register v_consolidate_lookup[32][15] = {
// NONE .q .2d .4s .8h .16b .d .2s .4h .8b .s .2h .4b .h .b
{REG_V0, REG_V0, REG_V0, REG_V0, REG_V0, REG_V0, REG_V0_D0, REG_V0_D0, REG_V0_D0, REG_V0_D0,
REG_V0_S0, REG_V0_S0, REG_V0_S0, REG_V0_H0, REG_V0_B0},
{REG_V1, REG_V1, REG_V1, REG_V1, REG_V1, REG_V1, REG_V1_D0, REG_V1_D0, REG_V1_D0, REG_V1_D0,
REG_V1_S0, REG_V1_S0, REG_V1_S0, REG_V1_H0, REG_V1_B0},
{REG_V2, REG_V2, REG_V2, REG_V2, REG_V2, REG_V2, REG_V2_D0, REG_V2_D0, REG_V2_D0, REG_V2_D0,
REG_V2_S0, REG_V2_S0, REG_V2_S0, REG_V2_H0, REG_V2_B0},
{REG_V3, REG_V3, REG_V3, REG_V3, REG_V3, REG_V3, REG_V3_D0, REG_V3_D0, REG_V3_D0, REG_V3_D0,
REG_V3_S0, REG_V3_S0, REG_V3_S0, REG_V3_H0, REG_V3_B0},
{REG_V4, REG_V4, REG_V4, REG_V4, REG_V4, REG_V4, REG_V4_D0, REG_V4_D0, REG_V4_D0, REG_V4_D0,
REG_V4_S0, REG_V4_S0, REG_V4_S0, REG_V4_H0, REG_V4_B0},
{REG_V5, REG_V5, REG_V5, REG_V5, REG_V5, REG_V5, REG_V5_D0, REG_V5_D0, REG_V5_D0, REG_V5_D0,
REG_V5_S0, REG_V5_S0, REG_V5_S0, REG_V5_H0, REG_V5_B0},
{REG_V6, REG_V6, REG_V6, REG_V6, REG_V6, REG_V6, REG_V6_D0, REG_V6_D0, REG_V6_D0, REG_V6_D0,
REG_V6_S0, REG_V6_S0, REG_V6_S0, REG_V6_H0, REG_V6_B0},
{REG_V7, REG_V7, REG_V7, REG_V7, REG_V7, REG_V7, REG_V7_D0, REG_V7_D0, REG_V7_D0, REG_V7_D0,
REG_V7_S0, REG_V7_S0, REG_V7_S0, REG_V7_H0, REG_V7_B0},
{REG_V8, REG_V8, REG_V8, REG_V8, REG_V8, REG_V8, REG_V8_D0, REG_V8_D0, REG_V8_D0, REG_V8_D0,
REG_V8_S0, REG_V8_S0, REG_V8_S0, REG_V8_H0, REG_V8_B0},
{REG_V9, REG_V9, REG_V9, REG_V9, REG_V9, REG_V9, REG_V9_D0, REG_V9_D0, REG_V9_D0, REG_V9_D0,
REG_V9_S0, REG_V9_S0, REG_V9_S0, REG_V9_H0, REG_V9_B0},
{REG_V10, REG_V10, REG_V10, REG_V10, REG_V10, REG_V10, REG_V10_D0, REG_V10_D0, REG_V10_D0,
REG_V10_D0, REG_V10_S0, REG_V10_S0, REG_V10_S0, REG_V10_H0, REG_V10_B0},
{REG_V11, REG_V11, REG_V11, REG_V11, REG_V11, REG_V11, REG_V11_D0, REG_V11_D0, REG_V11_D0,
REG_V11_D0, REG_V11_S0, REG_V11_S0, REG_V11_S0, REG_V11_H0, REG_V11_B0},
{REG_V12, REG_V12, REG_V12, REG_V12, REG_V12, REG_V12, REG_V12_D0, REG_V12_D0, REG_V12_D0,
REG_V12_D0, REG_V12_S0, REG_V12_S0, REG_V12_S0, REG_V12_H0, REG_V12_B0},
{REG_V13, REG_V13, REG_V13, REG_V13, REG_V13, REG_V13, REG_V13_D0, REG_V13_D0, REG_V13_D0,
REG_V13_D0, REG_V13_S0, REG_V13_S0, REG_V13_S0, REG_V13_H0, REG_V13_B0},
{REG_V14, REG_V14, REG_V14, REG_V14, REG_V14, REG_V14, REG_V14_D0, REG_V14_D0, REG_V14_D0,
REG_V14_D0, REG_V14_S0, REG_V14_S0, REG_V14_S0, REG_V14_H0, REG_V14_B0},
{REG_V15, REG_V15, REG_V15, REG_V15, REG_V15, REG_V15, REG_V15_D0, REG_V15_D0, REG_V15_D0,
REG_V15_D0, REG_V15_S0, REG_V15_S0, REG_V15_S0, REG_V15_H0, REG_V15_B0},
{REG_V16, REG_V16, REG_V16, REG_V16, REG_V16, REG_V16, REG_V16_D0, REG_V16_D0, REG_V16_D0,
REG_V16_D0, REG_V16_S0, REG_V16_S0, REG_V16_S0, REG_V16_H0, REG_V16_B0},
{REG_V17, REG_V17, REG_V17, REG_V17, REG_V17, REG_V17, REG_V17_D0, REG_V17_D0, REG_V17_D0,
REG_V17_D0, REG_V17_S0, REG_V17_S0, REG_V17_S0, REG_V17_H0, REG_V17_B0},
{REG_V18, REG_V18, REG_V18, REG_V18, REG_V18, REG_V18, REG_V18_D0, REG_V18_D0, REG_V18_D0,
REG_V18_D0, REG_V18_S0, REG_V18_S0, REG_V18_S0, REG_V18_H0, REG_V18_B0},
{REG_V19, REG_V19, REG_V19, REG_V19, REG_V19, REG_V19, REG_V19_D0, REG_V19_D0, REG_V19_D0,
REG_V19_D0, REG_V19_S0, REG_V19_S0, REG_V19_S0, REG_V19_H0, REG_V19_B0},
{REG_V20, REG_V20, REG_V20, REG_V20, REG_V20, REG_V20, REG_V20_D0, REG_V20_D0, REG_V20_D0,
REG_V20_D0, REG_V20_S0, REG_V20_S0, REG_V20_S0, REG_V20_H0, REG_V20_B0},
{REG_V21, REG_V21, REG_V21, REG_V21, REG_V21, REG_V21, REG_V21_D0, REG_V21_D0, REG_V21_D0,
REG_V21_D0, REG_V21_S0, REG_V21_S0, REG_V21_S0, REG_V21_H0, REG_V21_B0},
{REG_V22, REG_V22, REG_V22, REG_V22, REG_V22, REG_V22, REG_V22_D0, REG_V22_D0, REG_V22_D0,
REG_V22_D0, REG_V22_S0, REG_V22_S0, REG_V22_S0, REG_V22_H0, REG_V22_B0},
{REG_V23, REG_V23, REG_V23, REG_V23, REG_V23, REG_V23, REG_V23_D0, REG_V23_D0, REG_V23_D0,
REG_V23_D0, REG_V23_S0, REG_V23_S0, REG_V23_S0, REG_V23_H0, REG_V23_B0},
{REG_V24, REG_V24, REG_V24, REG_V24, REG_V24, REG_V24, REG_V24_D0, REG_V24_D0, REG_V24_D0,
REG_V24_D0, REG_V24_S0, REG_V24_S0, REG_V24_S0, REG_V24_H0, REG_V24_B0},
{REG_V25, REG_V25, REG_V25, REG_V25, REG_V25, REG_V25, REG_V25_D0, REG_V25_D0, REG_V25_D0,
REG_V25_D0, REG_V25_S0, REG_V25_S0, REG_V25_S0, REG_V25_H0, REG_V25_B0},
{REG_V26, REG_V26, REG_V26, REG_V26, REG_V26, REG_V26, REG_V26_D0, REG_V26_D0, REG_V26_D0,
REG_V26_D0, REG_V26_S0, REG_V26_S0, REG_V26_S0, REG_V26_H0, REG_V26_B0},
{REG_V27, REG_V27, REG_V27, REG_V27, REG_V27, REG_V27, REG_V27_D0, REG_V27_D0, REG_V27_D0,
REG_V27_D0, REG_V27_S0, REG_V27_S0, REG_V27_S0, REG_V27_H0, REG_V27_B0},
{REG_V28, REG_V28, REG_V28, REG_V28, REG_V28, REG_V28, REG_V28_D0, REG_V28_D0, REG_V28_D0,
REG_V28_D0, REG_V28_S0, REG_V28_S0, REG_V28_S0, REG_V28_H0, REG_V28_B0},
{REG_V29, REG_V29, REG_V29, REG_V29, REG_V29, REG_V29, REG_V29_D0, REG_V29_D0, REG_V29_D0,
REG_V29_D0, REG_V29_S0, REG_V29_S0, REG_V29_S0, REG_V29_H0, REG_V29_B0},
{REG_V30, REG_V30, REG_V30, REG_V30, REG_V30, REG_V30, REG_V30_D0, REG_V30_D0, REG_V30_D0,
REG_V30_D0, REG_V30_S0, REG_V30_S0, REG_V30_S0, REG_V30_H0, REG_V30_B0},
{REG_V31, REG_V31, REG_V31, REG_V31, REG_V31, REG_V31, REG_V31_D0, REG_V31_D0, REG_V31_D0,
REG_V31_D0, REG_V31_S0, REG_V31_S0, REG_V31_S0, REG_V31_H0, REG_V31_B0},
};
/* v28.d[1] -> REG_V0_D1 */
static Register vector_reg_minimize(InstructionOperand& oper)
{
if (!IS_ASIMD_O(oper))
return REG_NONE;
if (oper.arrSpec == ARRSPEC_NONE)
{
if (oper.laneUsed)
return REG_NONE; // cannot have lane without an arrangement spec
return oper.reg[0];
}
int vidx = oper.reg[0] - REG_V0;
if (vidx < 0 || vidx > 31)
return REG_NONE;
if (oper.laneUsed)
{
switch (oper.arrSpec)
{
case ARRSPEC_FULL:
return oper.reg[0];
case ARRSPEC_1DOUBLE:
case ARRSPEC_2DOUBLES:
if (oper.lane >= 2)
return REG_NONE;
return v_unpack_lookup[ARRSPEC_2DOUBLES][vidx][oper.lane];
case ARRSPEC_1SINGLE:
case ARRSPEC_2SINGLES:
case ARRSPEC_4SINGLES:
if (oper.lane >= 4)
return REG_NONE;
return v_unpack_lookup[ARRSPEC_4SINGLES][vidx][oper.lane];
case ARRSPEC_1HALF:
case ARRSPEC_2HALVES:
case ARRSPEC_4HALVES:
case ARRSPEC_8HALVES:
if (oper.lane >= 8)
return REG_NONE;
return v_unpack_lookup[ARRSPEC_8HALVES][vidx][oper.lane];
case ARRSPEC_1BYTE:
case ARRSPEC_4BYTES:
case ARRSPEC_8BYTES:
case ARRSPEC_16BYTES:
if (oper.lane >= 16)
return REG_NONE;
return v_unpack_lookup[ARRSPEC_16BYTES][vidx][oper.lane];
default:
break;
}
}
else
{
switch (oper.arrSpec)
{
case ARRSPEC_FULL:
case ARRSPEC_2DOUBLES:
case ARRSPEC_4SINGLES:
case ARRSPEC_8HALVES:
case ARRSPEC_16BYTES:
return oper.reg[0];
case ARRSPEC_1DOUBLE:
case ARRSPEC_2SINGLES:
case ARRSPEC_4HALVES:
case ARRSPEC_8BYTES:
return v_unpack_lookup[ARRSPEC_2DOUBLES][vidx][0];
case ARRSPEC_1SINGLE:
case ARRSPEC_2HALVES:
case ARRSPEC_4BYTES:
return v_unpack_lookup[ARRSPEC_4SINGLES][vidx][0];
case ARRSPEC_1HALF:
// case ARRSPEC_2BYTE
return v_unpack_lookup[ARRSPEC_8HALVES][vidx][0];
case ARRSPEC_1BYTE:
return v_unpack_lookup[ARRSPEC_16BYTES][vidx][0];
default:
break;
}
}
return REG_NONE;
}
/* "promote" the spec to full width so lane can select any */
static ArrangementSpec promote_spec(ArrangementSpec spec)
{
switch (spec)
{
case ARRSPEC_1DOUBLE:
return ARRSPEC_2DOUBLES;
case ARRSPEC_1SINGLE:
case ARRSPEC_2SINGLES:
return ARRSPEC_4SINGLES;
case ARRSPEC_1HALF:
case ARRSPEC_2HALVES:
case ARRSPEC_4HALVES:
return ARRSPEC_8HALVES;
case ARRSPEC_1BYTE:
case ARRSPEC_4BYTES:
case ARRSPEC_8BYTES:
return ARRSPEC_16BYTES;
default:
return spec;
}
}
static int unpack_vector(InstructionOperand& oper, Register* result)
{
if (oper.operandClass == REG)
{
/* register without an arrangement specification is just a register
examples: "d18", "d6", "v7" */
if (oper.arrSpec == ARRSPEC_NONE)
{
result[0] = oper.reg[0];
return 1;
}
/* require V register with valid arrangement spec
examples: "v17.2s", "v8.4h", "v21.8b" */
if (oper.reg[0] < REG_V0 || oper.reg[0] > REG_V31)
return 0;
if (oper.arrSpec <= ARRSPEC_NONE || oper.arrSpec > ARRSPEC_1BYTE)
return 0;
/* lookup, copy result */
if (oper.laneUsed)
{
ArrangementSpec spec = promote_spec(oper.arrSpec);
int n_lanes = v_unpack_lookup_sz[spec];
if (oper.lane >= n_lanes)
return 0;
// int n = v_unpack_lookup_sz[spec];
// for (int i = 0; i < n; ++i)
result[0] = v_unpack_lookup[spec][oper.reg[0] - REG_V0][oper.lane];
return 1;
}
int n = v_unpack_lookup_sz[oper.arrSpec];
for (int i = 0; i < n; ++i)
result[i] = v_unpack_lookup[oper.arrSpec][oper.reg[0] - REG_V0][i];
return n;
}
else if (oper.operandClass == MULTI_REG)
{
if (oper.laneUsed)
{
/* multireg with a lane
examples: "ld2 {v17.d, v18.d}[1], [x20]" */
ArrangementSpec spec = promote_spec(oper.arrSpec);
int n = 0;
for (int i = 0; i < 4 && oper.reg[i] != REG_NONE; i++)
{
int n_lanes = v_unpack_lookup_sz[spec];
if (oper.lane >= n_lanes)
return 0;
result[i] = v_unpack_lookup[spec][oper.reg[i] - REG_V0][oper.lane];
n += 1;
}
return n;
}
else
{
/* multireg without a lane
examples: "{v0.8b, v1.8b}", "{v8.2s, v9.2s}" */
if (oper.arrSpec < ARRSPEC_NONE || oper.arrSpec > ARRSPEC_1BYTE)
return 0;
int n = 0;
for (int i = 0; i < 4 && oper.reg[i] != REG_NONE; i++)
{
result[i] = v_consolidate_lookup[oper.reg[i] - REG_V0][oper.arrSpec];
n += 1;
}
return n;
}
}
return 0;
}
/* if we have two operands that have the same arrangement spec, instead of treating them as
distinct sets of registers, see if we can consolidate the set of registers into a single
larger register. This allows us to easily lift things like 'mov v0.16b, v1.16b' as
'mov v0, v1' */
static int consolidate_vector(
InstructionOperand& operand1,
InstructionOperand& operand2,
Register *result)
{
/* make sure both our operand classes are single regs */
if (operand1.operandClass != REG || operand2.operandClass != REG)
return 0;
/* make sure our arrSpec's match. We need this to deal with cases where the arrSpec might
have different sizes, e.g. 'uxtl v2.2d, v8.2s'.*/
if (operand1.arrSpec != operand2.arrSpec)
return 0;
result[0] = v_consolidate_lookup[operand1.reg[0]-REG_V0][operand1.arrSpec];
result[1] = v_consolidate_lookup[operand2.reg[0]-REG_V0][operand2.arrSpec];
return 1;
}
static void LoadStoreOperandPairSize(LowLevelILFunction& il, bool load, size_t load_size, InstructionOperand& operand1,
InstructionOperand& operand2, InstructionOperand& operand3)
{
/* do pre-indexing */
ExprId tmp = GetILOperandPreIndex(il, operand3);
if (tmp)
il.AddInstruction(tmp);
/* compute addresses */
OperandClass oclass = (operand3.operandClass == MEM_PRE_IDX) ? MEM_REG : operand3.operandClass;
ExprId addr0 = GetILOperandEffectiveAddress(il, operand3, 8, oclass, 0);
ExprId addr1 = GetILOperandEffectiveAddress(il, operand3, 8, oclass, load_size);
/* load/store */
if (load)
{
// We lift this instruction differently if operand1 and operand3 are equal as to not break outlining in the common case,
// since outlining does not seem to handle intermediate stores to temporary registers
// TODO: unify lifting once outlining handles intermediate temporary stores
if (REG_O(operand1) != REG_O(operand3))
{
// {op} X, Y, [Z]
il.AddInstruction(ILSETREG_O(operand1, il.Load(load_size, addr0)));
il.AddInstruction(ILSETREG_O(operand2, il.Load(load_size, addr1)));
}
else
{
// {op} X, Y, [X]
// Prevent clobbering of source during write to operand1 by storing source in a temporary register
il.AddInstruction(il.SetRegister(REGSZ_O(operand1), LLIL_TEMP(0), addr1));
il.AddInstruction(ILSETREG_O(operand1, il.Load(load_size, addr0)));
il.AddInstruction(ILSETREG_O(operand2, il.Load(load_size, il.Register(load_size, LLIL_TEMP(0)))));
}
}
else
{
il.AddInstruction(il.Store(load_size, addr0, ILREG_O(operand1)));
il.AddInstruction(il.Store(load_size, addr1, ILREG_O(operand2)));
}
/* do post-indexing */
tmp = GetILOperandPostIndex(il, operand3);
if (tmp)
il.AddInstruction(tmp);
}
static void LoadStoreOperandPair(LowLevelILFunction& il, bool load, InstructionOperand& operand1,
InstructionOperand& operand2, InstructionOperand& operand3)
{
unsigned sz = REGSZ_O(operand1);
LoadStoreOperandPairSize(il, load, sz, operand1, operand2, operand3);
}
static void LoadStoreVector(
LowLevelILFunction& il, bool is_load, InstructionOperand& oper0, InstructionOperand& oper1, bool replicate=false)
{
/* do pre-indexing */
ExprId tmp = GetILOperandPreIndex(il, oper1);
if (tmp)
il.AddInstruction(tmp);
Register regs[16];
int regs_n = unpack_vector(oper0, regs);
/* if we pre-indexed, base sequential effective addresses off the base register */
OperandClass oclass = (oper1.operandClass == MEM_PRE_IDX) ? MEM_REG : oper1.operandClass;
int arrspec_size = 1;
int arrspec_count = 1;
bool lanes = oper0.laneUsed;
// int Q = 0;
// switch (oper0.arrSpec)
// {
// case ARRSPEC_8BYTES:
// case ARRSPEC_4HALVES:
// case ARRSPEC_2SINGLES:
// case ARRSPEC_1DOUBLE:
// Q = 0;
// break;
// case ARRSPEC_16BYTES:
// case ARRSPEC_8HALVES:
// case ARRSPEC_4SINGLES:
// case ARRSPEC_2DOUBLES:
// Q = 1;
// break;
// default:
// // should never happen unless the disassembler is broken
// LogWarn("Invalid arrangement specification");
// return;
// }
// int enc_size = 0;
switch (oper0.arrSpec)
{
case ARRSPEC_1BYTE:
case ARRSPEC_4BYTES:
case ARRSPEC_8BYTES:
case ARRSPEC_16BYTES:
// enc_size = 0;
arrspec_size = 1;
break;
case ARRSPEC_1HALF:
case ARRSPEC_2HALVES:
case ARRSPEC_4HALVES:
case ARRSPEC_8HALVES:
// enc_size = 1;
arrspec_size = 2;
break;
case ARRSPEC_1SINGLE:
case ARRSPEC_2SINGLES:
case ARRSPEC_4SINGLES:
// enc_size = 2;
arrspec_size = 4;
break;
case ARRSPEC_1DOUBLE:
case ARRSPEC_2DOUBLES:
// enc_size = 3;
arrspec_size = 8;
break;
default:
// should never happen unless the disassembler is broken
LogWarn("Invalid arrangement specification");
return;
}
switch (oper0.arrSpec)
{
case ARRSPEC_16BYTES:
arrspec_count = 16;
break;
case ARRSPEC_8BYTES:
case ARRSPEC_8HALVES:
arrspec_count = 8;
break;
case ARRSPEC_4BYTES:
case ARRSPEC_4HALVES:
case ARRSPEC_4SINGLES:
arrspec_count = 4;
break;
case ARRSPEC_2HALVES:
case ARRSPEC_2SINGLES:
case ARRSPEC_2DOUBLES:
arrspec_count = 2;
break;
case ARRSPEC_1BYTE:
case ARRSPEC_1HALF:
case ARRSPEC_1SINGLE:
case ARRSPEC_1DOUBLE:
arrspec_count = 1;
break;
default:
// should never happen unless the disassembler is broken
LogWarn("Invalid arrangement specification");
return;
}
int offset = 0;
if (lanes)
arrspec_count = 1;
int lane = lanes ? oper0.lane : 0;
int rsize = arrspec_size;
if (replicate)
// load first into temp register for replication
il.AddInstruction(il.SetRegister(rsize, LLIL_TEMP(0), il.Load(rsize,
GetILOperandEffectiveAddress(il, oper1, 8, oclass, offset))));
for (int j = 0; j < arrspec_count; j++)
{
for (int i = 0; i < regs_n; ++i)
{
int reg_spec_base = (oper0.reg[0] + i - REG_V0) * (16 / arrspec_size) + lane;
Register reg = REG_NONE;
switch (arrspec_size)
{
case 1:
reg = (Register)(reg_spec_base + REG_V0_B0);
break;
case 2:
reg = (Register)(reg_spec_base + REG_V0_H0);
break;
case 4:
reg = (Register)(reg_spec_base + REG_V0_S0);
break;
case 8:
reg = (Register)(reg_spec_base + REG_V0_D0);
break;
}
ExprId eaddr = GetILOperandEffectiveAddress(il, oper1, 8, oclass, offset);
if (is_load)
il.AddInstruction(il.SetRegister(rsize, reg + j, replicate ?
il.Register(rsize, LLIL_TEMP(0)) // replicate: already loaded
:
il.Load(rsize, eaddr))); // single-lane: do the load inline
else
il.AddInstruction(il.Store(rsize, eaddr, il.Register(rsize, reg + j)));
offset += rsize;
}
}
/* do post-indexing */
tmp = GetILOperandPostIndex(il, oper1);
if (tmp)
il.AddInstruction(tmp);
}
static void LoadStoreOperand(LowLevelILFunction& il, bool load,
InstructionOperand& operand1, /* register that gets read/written */
InstructionOperand& operand2, /* location the read/write occurs */
int load_store_sz)
{
if (!load_store_sz)
load_store_sz = REGSZ_O(operand1);
ExprId tmp;
if (load)
{
switch (operand2.operandClass)
{
case MEM_REG:
// operand1.reg = [operand2.reg]
il.AddInstruction(
ILSETREG_O(operand1, il.Operand(1, il.Load(load_store_sz, ILREG_O(operand2)))));
break;
case MEM_OFFSET:
if (!load_store_sz)
load_store_sz = REGSZ_O(operand1);
// operand1.reg = [operand2.reg + operand2.imm]
if (IMM_O(operand2) == 0)
tmp = ILREG_O(operand2);
else
tmp = ILADDREG_O(operand2, il.Const(REGSZ_O(operand2), IMM_O(operand2)));
il.AddInstruction(ILSETREG_O(operand1, il.Operand(1, il.Load(load_store_sz, tmp))));
break;
case MEM_PRE_IDX:
// operand2.reg += operand2.imm
if (IMM_O(operand2) != 0)
il.AddInstruction(ILSETREG_O(operand2, il.Add(REGSZ_O(operand2), ILREG_O(operand2),
il.Const(REGSZ_O(operand2), IMM_O(operand2)))));
// operand1.reg = [operand2.reg]
il.AddInstruction(
ILSETREG_O(operand1, il.Operand(1, il.Load(load_store_sz, ILREG_O(operand2)))));
break;
case MEM_POST_IDX:
// operand1.reg = [operand2.reg]
il.AddInstruction(
ILSETREG_O(operand1, il.Operand(1, il.Load(load_store_sz, ILREG_O(operand2)))));
// operand2.reg += operand2.imm
if (IMM_O(operand2) != 0)
il.AddInstruction(ILSETREG_O(operand2, il.Add(REGSZ_O(operand2), ILREG_O(operand2),
il.Const(REGSZ_O(operand2), IMM_O(operand2)))));
break;
case MEM_EXTENDED:
il.AddInstruction(ILSETREG_O(operand1,
il.Operand(1, il.Load(load_store_sz,
il.Add(REGSZ_O(operand2), ILREG_O(operand2),
GetShiftedRegister(il, operand2, 1, REGSZ_O(operand2)))))));
break;
case LABEL:
il.AddInstruction(ILSETREG_O(
operand1, il.Operand(1, il.Load(load_store_sz, il.ConstPointer(8, IMM_O(operand2))))));
break;
case IMM32:
case IMM64:
il.AddInstruction(ILSETREG_O(operand1, il.Const(REGSZ_O(operand1), IMM_O(operand2))));
break;
default:
il.AddInstruction(il.Unimplemented());
break;
}
}
else // store
{
switch (operand2.operandClass)
{
case MEM_REG:
il.AddInstruction(
il.Operand(1, il.Store(load_store_sz, ILREG_O(operand2), ILREG_O(operand1))));
break;
case MEM_OFFSET:
//[operand2.reg + operand2.immediate] = operand1.reg
if (IMM_O(operand2) == 0)
tmp = ILREG_O(operand2);
else
tmp = ILADDREG_O(operand2, il.Const(REGSZ_O(operand2), IMM_O(operand2)));
il.AddInstruction(il.Operand(1, il.Store(load_store_sz, tmp, ILREG_O(operand1))));
break;
case MEM_PRE_IDX:
// operand2.reg = operand2.reg + operand2.immediate
if (IMM_O(operand2) != 0)
il.AddInstruction(ILSETREG_O(
operand2, ILADDREG_O(operand2, il.Const(REGSZ_O(operand2), IMM_O(operand2)))));
//[operand2.reg] = operand1.reg
il.AddInstruction(
il.Operand(1, il.Store(load_store_sz, ILREG_O(operand2), ILREG_O(operand1))));
break;
case MEM_POST_IDX:
//[operand2.reg] = operand1.reg
il.AddInstruction(
il.Operand(1, il.Store(load_store_sz, ILREG_O(operand2), ILREG_O(operand1))));
// operand2.reg = operand2.reg + operand2.immediate
if (IMM_O(operand2) != 0)
il.AddInstruction(ILSETREG_O(
operand2, ILADDREG_O(operand2, il.Const(REGSZ_O(operand2), IMM_O(operand2)))));
break;
case MEM_EXTENDED:
il.AddInstruction(il.Operand(
1, il.Store(load_store_sz,
il.Add(REGSZ_O(operand2), il.Register(REGSZ_O(operand2), operand2.reg[0]),
GetShiftedRegister(il, operand2, 1, REGSZ_O(operand2))),
ILREG_O(operand1))));
break;
default:
il.AddInstruction(il.Unimplemented());
break;
}
}
}
static void LoadStoreOperandSize(LowLevelILFunction& il, bool load, bool sign_extend, size_t size,
InstructionOperand& operand1, InstructionOperand& operand2)
{
ExprId tmp;
if (load)
{
// LLIL_TEMP registers will be reported to have size 0, so override with size
size_t extendSize = REGSZ_O(operand1) ? REGSZ_O(operand1) : size;
bool smallLoad = extendSize > size;
switch (operand2.operandClass)
{
case MEM_REG:
// operand1.reg = [operand2.reg]
tmp = il.Operand(1, il.Load(size, ILREG_O(operand2)));
if (smallLoad)
{
if (sign_extend)
tmp = il.SignExtend(extendSize, tmp);
else
tmp = il.ZeroExtend(extendSize, tmp);
}
il.AddInstruction(ILSETREG_O(operand1, tmp));
break;
case MEM_OFFSET:
// operand1.reg = [operand2.reg + operand2.imm]
if (IMM_O(operand2) == 0)
tmp = ILREG_O(operand2);
else
tmp = ILADDREG_O(operand2, il.Const(REGSZ_O(operand2), IMM_O(operand2)));
tmp = il.Operand(1, il.Load(size, tmp));
if (smallLoad)
{
if (sign_extend)
tmp = il.SignExtend(extendSize, tmp);
else
tmp = il.ZeroExtend(extendSize, tmp);
}
il.AddInstruction(ILSETREG_O(operand1, tmp));
break;
case MEM_PRE_IDX:
// operand2.reg += operand2.imm
if (IMM_O(operand2) != 0)
il.AddInstruction(ILSETREG_O(operand2, il.Add(REGSZ_O(operand2), ILREG_O(operand2),
il.Const(REGSZ_O(operand2), IMM_O(operand2)))));
// operand1.reg = [operand2.reg]
tmp = il.Operand(1, il.Load(size, ILREG_O(operand2)));
if (smallLoad)
{
if (sign_extend)
tmp = il.SignExtend(extendSize, tmp);
else
tmp = il.ZeroExtend(extendSize, tmp);
}
il.AddInstruction(ILSETREG_O(operand1, tmp));
break;
case MEM_POST_IDX:
// operand1.reg = [operand2.reg]
tmp = il.Operand(1, il.Load(size, ILREG_O(operand2)));
if (smallLoad)
{
if (sign_extend)
tmp = il.SignExtend(extendSize, tmp);
else
tmp = il.ZeroExtend(extendSize, tmp);
}
il.AddInstruction(ILSETREG_O(operand1, tmp));
// operand2.reg += operand2.imm
if (IMM_O(operand2) != 0)
il.AddInstruction(ILSETREG_O(operand2, il.Add(REGSZ_O(operand2), ILREG_O(operand2),
il.Const(REGSZ_O(operand2), IMM_O(operand2)))));
break;
case MEM_EXTENDED:
tmp =
il.Operand(1, il.Load(size, il.Add(REGSZ_O(operand2), ILREG_O(operand2),
GetShiftedRegister(il, operand2, 1, REGSZ_O(operand2)))));
if (smallLoad)
{
if (sign_extend)
tmp = il.SignExtend(extendSize, tmp);
else
tmp = il.ZeroExtend(extendSize, tmp);
}
il.AddInstruction(ILSETREG_O(operand1, tmp));
break;
case LABEL:
tmp = il.Operand(1, il.Load(size, il.ConstPointer(8, IMM_O(operand2))));
if (smallLoad)
{
if (sign_extend)
tmp = il.SignExtend(extendSize, tmp);
else
tmp = il.ZeroExtend(extendSize, tmp);
}
il.AddInstruction(ILSETREG_O(operand1, tmp));
break;
default:
il.AddInstruction(il.Unimplemented());
break;
}
}
else // store
{
ExprId valToStore = il.Operand(0, ILREG_O(operand1));
if (size < REGSZ_O(operand1))
valToStore = il.LowPart(size, valToStore);
switch (operand2.operandClass)
{
case MEM_REG:
il.AddInstruction(il.Operand(1, il.Store(size, ILREG_O(operand2), valToStore)));
break;
case MEM_OFFSET:
//[operand2.reg + operand2.immediate] = operand1.reg
if (IMM_O(operand2) == 0)
tmp = il.Store(size, ILREG_O(operand2), valToStore);
else
tmp = il.Store(
size, ILADDREG_O(operand2, il.Const(REGSZ_O(operand2), IMM_O(operand2))), valToStore);
il.AddInstruction(il.Operand(1, tmp));
break;
case MEM_PRE_IDX:
// operand2.reg = operand2.reg + operand2.immediate
if (IMM_O(operand2) != 0)
il.AddInstruction(ILSETREG_O(
operand2, ILADDREG_O(operand2, il.Const(REGSZ_O(operand2), IMM_O(operand2)))));
//[operand2.reg] = operand1.reg
il.AddInstruction(il.Operand(1, il.Store(size, ILREG_O(operand2), valToStore)));
break;
case MEM_POST_IDX:
//[operand2.reg] = operand1.reg
il.AddInstruction(il.Operand(1, il.Store(size, ILREG_O(operand2), valToStore)));
// operand2.reg = operand2.reg + operand2.immediate
if (IMM_O(operand2) != 0)
il.AddInstruction(ILSETREG_O(
operand2, ILADDREG_O(operand2, il.Const(REGSZ_O(operand2), IMM_O(operand2)))));
break;
case MEM_EXTENDED:
il.AddInstruction(il.Operand(
1, il.Store(size,
il.Add(REGSZ_O(operand2), il.Register(REGSZ_O(operand2), operand2.reg[0]),
GetShiftedRegister(il, operand2, 1, REGSZ_O(operand2))),
valToStore)));
break;
default:
il.AddInstruction(il.Unimplemented());
break;
}
}
}
static size_t DirectJump(
Architecture* arch, LowLevelILFunction& il, uint64_t target, size_t addrSize)
{
BNLowLevelILLabel* label = il.GetLabelForAddress(arch, target);
if (label)
return il.Goto(*label);
else
return il.Jump(il.ConstPointer(addrSize, target));
return 0;
}
static ExprId ExtractBits(
LowLevelILFunction& il, InstructionOperand& reg, size_t nbits, size_t rightMostBit)
{
// Get N set bits at offset O
#define BITMASK(N, O) (((UINT64_C(1) << nbits) - 1) << O)
return il.And(REGSZ_O(reg), ILREG_O(reg), il.Const(REGSZ_O(reg), BITMASK(nbits, rightMostBit)));
}
static ExprId ExtractBit(LowLevelILFunction& il, InstructionOperand& reg, size_t bit)
{
return il.And(REGSZ_O(reg), ILREG_O(reg), il.Const(REGSZ_O(reg), (UINT64_C(1) << bit)));
}
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 ApplyAttributeToLastInstruction(LowLevelILFunction& il, uint32_t attributes)
{
size_t instrId = il.GetInstructionCount()-1;
ExprId expr = il.GetIndexForInstruction(instrId);
il.SetExprAttributes(expr, attributes);
}
enum Arm64Intrinsic operation_to_intrinsic(int operation)
{
switch (operation)
{
case ARM64_AUTDA:
case ARM64_AUTDZA:
return ARM64_INTRIN_AUTDA;
case ARM64_AUTDB:
case ARM64_AUTDZB:
return ARM64_INTRIN_AUTDB;
case ARM64_AUTIA:
case ARM64_AUTIA1716:
case ARM64_AUTIASP:
case ARM64_AUTIAZ:
case ARM64_AUTIZA:
return ARM64_INTRIN_AUTIA;
case ARM64_AUTIB:
case ARM64_AUTIB1716:
case ARM64_AUTIBSP:
case ARM64_AUTIBZ:
case ARM64_AUTIZB:
return ARM64_INTRIN_AUTIB;
case ARM64_PACDA:
case ARM64_PACDZA:
return ARM64_INTRIN_PACDA;
case ARM64_PACDB:
case ARM64_PACDZB:
return ARM64_INTRIN_PACDB;
case ARM64_PACGA:
return ARM64_INTRIN_PACGA;
case ARM64_PACIA:
case ARM64_PACIA1716:
case ARM64_PACIASP:
case ARM64_PACIAZ:
case ARM64_PACIZA:
return ARM64_INTRIN_PACIA;
case ARM64_PACIB:
case ARM64_PACIB1716:
case ARM64_PACIBSP:
case ARM64_PACIBZ:
case ARM64_PACIZB:
return ARM64_INTRIN_PACIB;
case ARM64_XPACD:
return ARM64_INTRIN_XPACD;
case ARM64_XPACI:
case ARM64_XPACLRI:
return ARM64_INTRIN_XPACI;
default:
return ARM64_INTRIN_INVALID;
}
}
bool GetLowLevelILForInstruction(
Architecture* arch, uint64_t addr, LowLevelILFunction& il, Instruction& instr, size_t addrSize, bool requireAlignment, std::function<bool()> _preferIntrinsics)
{
bool SetPacAttr = false;
InstructionOperand& operand1 = instr.operands[0];
InstructionOperand& operand2 = instr.operands[1];
InstructionOperand& operand3 = instr.operands[2];
InstructionOperand& operand4 = instr.operands[3];
InstructionOperand& operand5 = instr.operands[4];
const size_t pairedSize = REGSZ_O(operand1) * 2;
if (requireAlignment && (addr % 4 != 0)) {
return false;
}
int n_instrs_before = il.GetInstructionCount();
auto preferIntrinsics = [&]() -> bool {
if (_preferIntrinsics())
{
NeonGetLowLevelILForInstruction(arch, addr, il, instr, addrSize);
return (il.GetInstructionCount() > n_instrs_before);
}
return false;
};
// printf("%s() operation:%d encoding:%d\n", __func__, instr.operation, instr.encoding);
LowLevelILLabel trueLabel, falseLabel;
switch (instr.operation)
{
case ARM64_ABS:
{
ExprId src = ILREG_O(operand2);
GenIfElse(il, il.CompareSignedLessThan(REGSZ_O(operand2), src, il.Const(REGSZ_O(operand2), 0)),
ILSETREG_O(operand1, il.Neg(REGSZ_O(operand2), src)), ILSETREG_O(operand1, src));
break;
}
case ARM64_ADD:
switch (instr.encoding)
{
case ENC_ADD_Z_P_ZZ_:
case ENC_ADD_Z_ZI_:
case ENC_ADD_Z_ZZ_:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
case ARM64_ADDS:
il.AddInstruction(
ILSETREG_O(operand1, il.Add(REGSZ_O(operand1), ILREG_O(operand2),
ReadILOperand(il, operand3, REGSZ_O(operand1)), SETFLAGS)));
break;
case ARM64_ADDG:
il.AddInstruction(il.Intrinsic({RegisterOrFlag::Register(REG_O(operand1))}, ARM64_INTRIN_ADDG,
{ILREG_O(operand2), il.Const(REGSZ_O(operand2), IMM_O(operand3)), il.Const(1, IMM_O(operand4))}));
break;
case ARM64_ADC:
case ARM64_ADCS:
il.AddInstruction(ILSETREG_O(operand1,
il.AddCarry(REGSZ_O(operand1), ILREG_O(operand2),
ReadILOperand(il, operand3, REGSZ_O(operand1)), il.Flag(IL_FLAG_C), SETFLAGS)));
break;
case ARM64_AND:
case ARM64_ANDS:
switch (instr.encoding)
{
case ENC_AND_P_P_PP_Z:
case ENC_AND_Z_P_ZZ_:
case ENC_AND_Z_ZI_:
case ENC_AND_Z_ZZ_:
case ENC_ANDS_P_P_PP_Z:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
il.AddInstruction(
ILSETREG_O(operand1, il.And(REGSZ_O(operand1), ILREG_O(operand2),
ReadILOperand(il, operand3, REGSZ_O(operand1)), SETFLAGS)));
break;
case ARM64_ADR:
switch (instr.encoding)
{
case ENC_ADR_Z_AZ_SD_SAME_SCALED:
case ENC_ADR_Z_AZ_D_S32_SCALED:
case ENC_ADR_Z_AZ_D_U32_SCALED:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
case ARM64_ADRP:
il.AddInstruction(ILSETREG_O(operand1, il.ConstPointer(REGSZ_O(operand1), IMM_O(operand2))));
break;
case ARM64_ASR:
switch (instr.encoding)
{
case ENC_ASR_Z_P_ZI_:
case ENC_ASR_Z_P_ZW_:
case ENC_ASR_Z_P_ZZ_:
case ENC_ASR_Z_ZI_:
case ENC_ASR_Z_ZW_:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
il.AddInstruction(ILSETREG_O(operand1, il.ArithShiftRight(REGSZ_O(operand2), ILREG_O(operand2),
ReadILOperand(il, operand3, REGSZ_O(operand2)))));
break;
case ARM64_AESD:
switch (instr.encoding)
{
case ENC_AESD_Z_ZZ_:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
il.AddInstruction(il.Intrinsic({RegisterOrFlag::Register(REG_O(operand1))}, ARM64_INTRIN_AESD,
{ILREG_O(operand1), ILREG_O(operand2)}));
break;
case ARM64_AESE:
switch (instr.encoding)
{
case ENC_AESE_Z_ZZ_:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
il.AddInstruction(il.Intrinsic({RegisterOrFlag::Register(REG_O(operand1))}, ARM64_INTRIN_AESE,
{ILREG_O(operand1), ILREG_O(operand2)}));
break;
case ARM64_AESIMC:
switch (instr.encoding)
{
case ENC_AESIMC_Z_Z_:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
il.AddInstruction(il.Intrinsic({RegisterOrFlag::Register(REG_O(operand1))}, ARM64_INTRIN_AESIMC,
{ILREG_O(operand1), ILREG_O(operand2)}));
break;
case ARM64_AESMC:
switch (instr.encoding)
{
case ENC_AESMC_Z_Z_:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
il.AddInstruction(il.Intrinsic({RegisterOrFlag::Register(REG_O(operand1))}, ARM64_INTRIN_AESMC,
{ILREG_O(operand1), ILREG_O(operand2)}));
break;
case ARM64_BTI:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_HINT_BTI, {}));
break;
case ARM64_B_NV:
il.AddInstruction(DirectJump(arch, il, addr + 4, addrSize));
break;
case ARM64_B:
case ARM64_B_AL:
il.AddInstruction(DirectJump(arch, il, IMM_O(operand1), addrSize));
break;
case ARM64_B_NE:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_NE), addrSize, IMM_O(operand1), addr + 4);
return false;
case ARM64_B_EQ:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_EQ), addrSize, IMM_O(operand1), addr + 4);
return false;
case ARM64_B_CS:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_CS), addrSize, IMM_O(operand1), addr + 4);
return false;
case ARM64_B_CC:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_CC), addrSize, IMM_O(operand1), addr + 4);
return false;
case ARM64_B_MI:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_MI), addrSize, IMM_O(operand1), addr + 4);
return false;
case ARM64_B_PL:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_PL), addrSize, IMM_O(operand1), addr + 4);
return false;
case ARM64_B_VS:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_VS), addrSize, IMM_O(operand1), addr + 4);
return false;
case ARM64_B_VC:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_VC), addrSize, IMM_O(operand1), addr + 4);
return false;
case ARM64_B_HI:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_HI), addrSize, IMM_O(operand1), addr + 4);
return false;
case ARM64_B_LS:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_LS), addrSize, IMM_O(operand1), addr + 4);
return false;
case ARM64_B_GE:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_GE), addrSize, IMM_O(operand1), addr + 4);
return false;
case ARM64_B_LT:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_LT), addrSize, IMM_O(operand1), addr + 4);
return false;
case ARM64_B_GT:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_GT), addrSize, IMM_O(operand1), addr + 4);
return false;
case ARM64_B_LE:
ConditionalJump(arch, il, il.FlagGroup(IL_FLAG_GROUP_LE), addrSize, IMM_O(operand1), addr + 4);
return false;
case ARM64_BL:
il.AddInstruction(il.Call(il.ConstPointer(addrSize, IMM_O(operand1))));
break;
case ARM64_BLRAA:
case ARM64_BLRAAZ:
case ARM64_BLRAB:
case ARM64_BLRABZ:
SetPacAttr = true;
case ARM64_BLR:
il.AddInstruction(il.Call(ILREG_O(operand1)));
if (SetPacAttr)
ApplyAttributeToLastInstruction(il, SrcInstructionUsesPointerAuth);
break;
case ARM64_BFC:
il.AddInstruction(ILSETREG_O(
operand1, il.And(REGSZ_O(operand1),
il.Const(REGSZ_O(operand1), ~(ONES(IMM_O(operand3)) << IMM_O(operand2))),
ILREG_O(operand1))));
break;
case ARM64_BFI:
il.AddInstruction(ILSETREG_O(operand1,
il.Or(REGSZ_O(operand1),
il.And(REGSZ_O(operand1),
il.Const(REGSZ_O(operand1), ~(ONES(IMM_O(operand4)) << IMM_O(operand3))),
ILREG_O(operand1)),
il.ShiftLeft(REGSZ_O(operand1),
il.And(REGSZ_O(operand1), il.Const(REGSZ_O(operand1), ONES(IMM_O(operand4))),
ILREG_O(operand2)),
il.Const(1, IMM_O(operand3))))));
break;
case ARM64_BFXIL:
il.AddInstruction(ILSETREG_O(operand1,
il.Or(REGSZ_O(operand1),
il.And(REGSZ_O(operand1), ILREG_O(operand1),
il.Const(REGSZ_O(operand1), ~ONES(IMM_O(operand4)))),
il.LogicalShiftRight(REGSZ_O(operand1),
il.And(REGSZ_O(operand1), ILREG_O(operand2),
il.Const(REGSZ_O(operand1), ONES(IMM_O(operand4)) << IMM_O(operand3))),
il.Const(1, IMM_O(operand3))))));
break;
case ARM64_BRAA:
case ARM64_BRAAZ:
case ARM64_BRAB:
case ARM64_BRABZ:
SetPacAttr = true;
case ARM64_BR:
il.AddInstruction(il.Jump(ILREG_O(operand1)));
if (SetPacAttr)
ApplyAttributeToLastInstruction(il, SrcInstructionUsesPointerAuth);
return false;
case ARM64_BIC:
case ARM64_BICS:
switch (instr.encoding)
{
case ENC_BIC_Z_ZI__AND_Z_ZI_:
case ENC_BIC_P_P_PP_Z:
case ENC_BIC_Z_P_ZZ_:
case ENC_BIC_Z_ZZ_:
case ENC_BICS_P_P_PP_Z:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
case ENC_BIC_ASIMDIMM_L_HL:
case ENC_BIC_ASIMDIMM_L_SL:
il.AddInstruction(ILSETREG_O(operand1,
il.And(REGSZ_O(operand1), ILREG_O(operand1),
il.Not(REGSZ_O(operand2), ReadILOperand(il, operand2, REGSZ_O(operand2))), SETFLAGS)));
break;
default:
il.AddInstruction(ILSETREG_O(operand1,
il.And(REGSZ_O(operand2), ILREG_O(operand2),
il.Not(REGSZ_O(operand2), ReadILOperand(il, operand3, REGSZ_O(operand2))), SETFLAGS)));
}
break;
// TODO: some representation of the Acquire/Release semantics of the CAS* instructions... attribute?
case ARM64_CASP: // these compare-and-swaps can be pairs of 32 bit words or 64 bit doublewords
case ARM64_CASPA:
case ARM64_CASPAL:
case ARM64_CASPL:
{
// the ordering of the register pairing depends on the byte order (endianness) of memory
bool bigEndian = arch->GetEndianness() == BigEndian;
auto hi1 = bigEndian ? operand1 : operand2;
auto lo1 = bigEndian ? operand2 : operand1;
auto hi2 = bigEndian ? operand3 : operand4;
auto lo2 = bigEndian ? operand4 : operand3;
il.AddInstruction(il.SetRegister(pairedSize, LLIL_TEMP(0), il.Load(pairedSize, ILREG_O(operand5))));
GenIfElse(il,
il.CompareEqual(pairedSize,
il.RegisterSplit(REGSZ_O(operand1),
hi1.reg[0], lo1.reg[0]),
il.Register(pairedSize, LLIL_TEMP(0))),
il.Store(pairedSize,
ILREG_O(operand5),
il.RegisterSplit(REGSZ_O(operand1), hi2.reg[0], lo2.reg[0])),
0);
il.AddInstruction(
il.SetRegisterSplit(pairedSize,
hi1.reg[0],
lo1.reg[0],
il.Register(pairedSize, LLIL_TEMP(0))));
break;
}
case ARM64_CAS: // these compare-and-swaps can be 32 or 64 bit
case ARM64_CASA:
case ARM64_CASAL:
case ARM64_CASL:
il.AddInstruction(il.SetRegister(REGSZ_O(operand1), LLIL_TEMP(0), il.Load(REGSZ_O(operand1), ILREG_O(operand3))));
GenIfElse(il,
il.CompareEqual(REGSZ_O(operand1), ILREG_O(operand1), il.Register(REGSZ_O(operand1), LLIL_TEMP(0))),
il.Store(REGSZ_O(operand1), ILREG_O(operand3), ILREG_O(operand2)),
0);
il.AddInstruction(ILSETREG_O(operand1, il.Register(REGSZ_O(operand1), LLIL_TEMP(0))));
break;
case ARM64_CASAH: // these compare-and-swaps are 16 bit
case ARM64_CASALH:
case ARM64_CASH:
case ARM64_CASLH:
il.AddInstruction(il.SetRegister(2, LLIL_TEMP(0), il.Load(2, ILREG_O(operand3))));
GenIfElse(il,
il.CompareEqual(REGSZ_O(operand1), ExtractRegister(il, operand1, 0, 2, false, 2), il.Register(2, LLIL_TEMP(0))),
il.Store(2, ILREG_O(operand3), ExtractRegister(il, operand2, 0, 2, false, 2)),
0);
il.AddInstruction(ILSETREG_O(operand1, il.Register(2, LLIL_TEMP(0))));
break;
case ARM64_CASAB: // these compare-and-swaps are 8 bit
case ARM64_CASALB:
case ARM64_CASB:
case ARM64_CASLB:
il.AddInstruction(il.SetRegister(1, LLIL_TEMP(0), il.Load(1, ILREG_O(operand3))));
GenIfElse(il,
il.CompareEqual(REGSZ_O(operand1), ExtractRegister(il, operand1, 0, 1, false, 1), il.Register(1, LLIL_TEMP(0))),
il.Store(1, ILREG_O(operand3), ExtractRegister(il, operand2, 0, 1, false, 1)),
0);
il.AddInstruction(ILSETREG_O(operand1, il.Register(1, LLIL_TEMP(0))));
break;
case ARM64_CBNZ:
ConditionalJump(arch, il,
il.CompareNotEqual(REGSZ_O(operand1), ILREG_O(operand1), il.Const(REGSZ_O(operand1), 0)),
addrSize, IMM_O(operand2), addr + 4);
return false;
case ARM64_CBZ:
ConditionalJump(arch, il,
il.CompareEqual(REGSZ_O(operand1), ILREG_O(operand1), il.Const(REGSZ_O(operand1), 0)),
addrSize, IMM_O(operand2), addr + 4);
return false;
case ARM64_CMN:
il.AddInstruction(il.Add(REGSZ_O(operand1), ILREG_O(operand1),
ReadILOperand(il, operand2, REGSZ_O(operand1)), SETFLAGS));
break;
case ARM64_CCMN:
{
LowLevelILLabel trueCode, falseCode, done;
il.AddInstruction(il.If(GetCondition(il, operand4.cond), trueCode, falseCode));
il.MarkLabel(trueCode);
il.AddInstruction(il.Add(REGSZ_O(operand1), ILREG_O(operand1),
ReadILOperand(il, operand2, REGSZ_O(operand1)), SETFLAGS));
il.AddInstruction(il.Goto(done));
il.MarkLabel(falseCode);
il.AddInstruction(il.SetFlag(IL_FLAG_N, il.Const(0, (IMM_O(operand3) >> 3) & 1)));
il.AddInstruction(il.SetFlag(IL_FLAG_Z, il.Const(0, (IMM_O(operand3) >> 2) & 1)));
il.AddInstruction(il.SetFlag(IL_FLAG_C, il.Const(0, (IMM_O(operand3) >> 1) & 1)));
il.AddInstruction(il.SetFlag(IL_FLAG_V, il.Const(0, (IMM_O(operand3) >> 0) & 1)));
il.AddInstruction(il.Goto(done));
il.MarkLabel(done);
}
break;
case ARM64_CMP:
il.AddInstruction(il.Sub(REGSZ_O(operand1), ILREG_O(operand1),
ReadILOperand(il, operand2, REGSZ_O(operand1)), SETFLAGS));
break;
case ARM64_CMPP:
il.AddInstruction(il.Intrinsic(
{RegisterOrFlag::Flag(IL_FLAG_N), RegisterOrFlag::Flag(IL_FLAG_Z), RegisterOrFlag::Flag(IL_FLAG_C),
RegisterOrFlag::Flag(IL_FLAG_V)},
ARM64_INTRIN_CMPP, {ILREG_O(operand1), ILREG_O(operand2)}));
break;
case ARM64_CCMP:
{
LowLevelILLabel trueCode, falseCode, done;
il.AddInstruction(il.If(GetCondition(il, operand4.cond), trueCode, falseCode));
il.MarkLabel(trueCode);
il.AddInstruction(il.Sub(REGSZ_O(operand1), ILREG_O(operand1),
ReadILOperand(il, operand2, REGSZ_O(operand1)), SETFLAGS));
il.AddInstruction(il.Goto(done));
il.MarkLabel(falseCode);
il.AddInstruction(il.SetFlag(IL_FLAG_N, il.Const(0, (IMM_O(operand3) >> 3) & 1)));
il.AddInstruction(il.SetFlag(IL_FLAG_Z, il.Const(0, (IMM_O(operand3) >> 2) & 1)));
il.AddInstruction(il.SetFlag(IL_FLAG_C, il.Const(0, (IMM_O(operand3) >> 1) & 1)));
il.AddInstruction(il.SetFlag(IL_FLAG_V, il.Const(0, (IMM_O(operand3) >> 0) & 1)));
il.AddInstruction(il.Goto(done));
il.MarkLabel(done);
}
break;
case ARM64_CLREX:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_CLREX, {}));
break;
case ARM64_CSEL:
case ARM64_FCSEL:
GenIfElse(il, GetCondition(il, operand4.cond), ILSETREG_O(operand1, ILREG_O(operand2)),
ILSETREG_O(operand1, ILREG_O(operand3)));
break;
case ARM64_CSINC:
GenIfElse(il, GetCondition(il, operand4.cond), ILSETREG_O(operand1, ILREG_O(operand2)),
ILSETREG_O(operand1, ILADDREG_O(operand3, il.Const(REGSZ_O(operand1), 1))));
break;
case ARM64_CSINV:
GenIfElse(il, GetCondition(il, operand4.cond), ILSETREG_O(operand1, ILREG_O(operand2)),
ILSETREG_O(operand1, il.Not(REGSZ_O(operand1), ILREG_O(operand3))));
break;
case ARM64_CSNEG:
GenIfElse(il, GetCondition(il, operand4.cond), ILSETREG_O(operand1, ILREG_O(operand2)),
ILSETREG_O(operand1, il.Neg(REGSZ_O(operand1), ILREG_O(operand3))));
break;
case ARM64_CSET:
il.AddInstruction(
ILSETREG_O(operand1,
il.BoolToInt(REGSZ_O(operand1), GetCondition(il, operand2.cond))));
break;
case ARM64_CSETM:
GenIfElse(il, GetCondition(il, operand2.cond),
ILSETREG_O(operand1, il.Const(REGSZ_O(operand1), -1)),
ILSETREG_O(operand1, il.Const(REGSZ_O(operand1), 0)));
break;
case ARM64_CINC:
GenIfElse(il, GetCondition(il, operand3.cond),
ILSETREG_O(operand1, ILADDREG_O(operand2, il.Const(REGSZ_O(operand1), 1))),
ILSETREG_O(operand1, ILREG_O(operand2)));
break;
case ARM64_CINV:
GenIfElse(il, GetCondition(il, operand3.cond),
ILSETREG_O(operand1, il.Not(REGSZ_O(operand1), ILREG_O(operand2))),
ILSETREG_O(operand1, ILREG_O(operand2)));
break;
case ARM64_CNEG:
GenIfElse(il, GetCondition(il, operand3.cond),
ILSETREG_O(operand1, il.Neg(REGSZ_O(operand1), ILREG_O(operand2))),
ILSETREG_O(operand1, ILREG_O(operand2)));
break;
case ARM64_CLS:
il.AddInstruction(ILSETREG_O(operand1, il.CountLeadingSigns(REGSZ_O(operand2), ILREG_O(operand2))));
break;
case ARM64_CLZ:
il.AddInstruction(ILSETREG_O(operand1, il.CountLeadingZeros(REGSZ_O(operand2), ILREG_O(operand2))));
break;
case ARM64_CNT:
switch (instr.encoding) {
case ENC_CNT_32_DP_1SRC:
case ENC_CNT_64_DP_1SRC:
// FEAT_CSSC scalar population count on a general-purpose register
il.AddInstruction(ILSETREG_O(operand1, il.PopulationCount(REGSZ_O(operand2), ILREG_O(operand2))));
break;
default:
// The NEON and SVE forms are per-element population counts, which have no native scalar
// representation and are lifted as an intrinsic
il.AddInstruction(
il.Intrinsic({RegisterOrFlag::Register(REG_O(operand1))}, ARM64_INTRIN_CNT, {ILREG_O(operand2)}));
}
break;
case ARM64_CTZ:
il.AddInstruction(ILSETREG_O(operand1, il.CountTrailingZeros(REGSZ_O(operand2), ILREG_O(operand2))));
break;
case ARM64_DC:
// il.AddInstruction(
// il.Intrinsic({}, ARM64_INTRIN_DC, {ILREG_O(operand2)})); /* operand1 is <dc_op> */
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_DC, {il.Const(4, operand1.immediate), ReadILOperand(il, operand2, 8)}));
break;
case ARM64_DMB:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_DMB, {}));
break;
case ARM64_DSB:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_DSB, {}));
break;
case ARM64_EON:
switch (instr.encoding)
{
case ENC_EON_Z_ZI__EOR_Z_ZI_:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
il.AddInstruction(ILSETREG_O(
operand1, il.Xor(REGSZ_O(operand1), ILREG_O(operand2),
il.Not(REGSZ_O(operand1), ReadILOperand(il, operand3, REGSZ_O(operand1))))));
break;
case ARM64_EOR:
switch (instr.encoding)
{
case ENC_EOR_P_P_PP_Z:
case ENC_EOR_Z_P_ZZ_:
case ENC_EOR_Z_ZI_:
case ENC_EOR_Z_ZZ_:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
il.AddInstruction(ILSETREG_O(operand1, il.Xor(REGSZ_O(operand1), ILREG_O(operand2),
ReadILOperand(il, operand3, REGSZ_O(operand1)))));
break;
case ARM64_ESB:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_ESB, {}));
break;
case ARM64_EXTR:
il.AddInstruction(
ILSETREG_O(operand1, il.LogicalShiftRight(pairedSize,
il.Or(pairedSize,
il.ShiftLeft(pairedSize, ILREG_O(operand2),
il.Const(1, REGSZ_O(operand1) * 8)),
ILREG_O(operand3)),
il.Const(1, IMM_O(operand4)))));
break;
case ARM64_FABD:
switch (instr.encoding)
{
case ENC_FABD_ASISDSAMEFP16_ONLY:
case ENC_FABD_ASISDSAME_ONLY:
il.AddInstruction(ILSETREG_O(operand1,
il.FloatAbs(REGSZ_O(operand1),
il.FloatSub(REGSZ_O(operand1), ILREG_O(operand2), ILREG_O(operand3)))));
break;
case ENC_FABD_ASIMDSAMEFP16_ONLY:
case ENC_FABD_ASIMDSAME_ONLY:
// covered by intrinsics
break;
case ENC_FABD_Z_P_ZZ_:
default:
ABORT_LIFT;
}
break;
case ARM64_FABS:
switch (instr.encoding)
{
case ENC_FABS_D_FLOATDP1:
case ENC_FABS_S_FLOATDP1:
case ENC_FABS_H_FLOATDP1:
il.AddInstruction(ILSETREG_O(operand1, il.FloatAbs(REGSZ_O(operand1), ILREG_O(operand2))));
break;
case ENC_FABS_ASIMDMISC_R:
case ENC_FABS_ASIMDMISCFP16_R:
// covered by intrinsics
break;
default:
ABORT_LIFT;
}
break;
case ARM64_FADD:
switch (instr.encoding)
{
case ENC_FADD_H_FLOATDP2:
case ENC_FADD_S_FLOATDP2:
case ENC_FADD_D_FLOATDP2:
il.AddInstruction(ILSETREG_O(
operand1, il.FloatAdd(REGSZ_O(operand1), ILREG_O(operand2), ILREG_O(operand3))));
break;
case ENC_FADD_ASIMDSAME_ONLY:
case ENC_FADD_ASIMDSAMEFP16_ONLY:
{
if (preferIntrinsics())
return true;
Register srcs1[16], srcs2[16], dsts[16];
int dst_n = unpack_vector(operand1, dsts);
int src1_n = unpack_vector(operand2, srcs1);
int src2_n = unpack_vector(operand3, srcs2);
if ((dst_n != src1_n) || (src1_n != src2_n) || dst_n == 0)
ABORT_LIFT;
int rsize = get_register_size(dsts[0]);
for (int i = 0; i < dst_n; ++i)
il.AddInstruction(ILSETREG(
dsts[i], il.FloatAdd(rsize, ILREG(srcs1[i]), ILREG(srcs2[i]))));
break;
}
default:
ABORT_LIFT;
}
break;
case ARM64_FADDP:
switch (instr.encoding)
{
case ENC_FADDP_ASISDPAIR_ONLY_H:
case ENC_FADDP_ASISDPAIR_ONLY_SD:
{
Register srcs[16];
int src_n = unpack_vector(operand2, srcs);
if (src_n != 2)
ABORT_LIFT;
il.AddInstruction(ILSETREG_O(operand1,
il.FloatAdd(REGSZ_O(operand1), ILREG(srcs[0]), ILREG(srcs[1]))
));
break;
}
case ENC_FADDP_ASIMDSAME_ONLY:
case ENC_FADDP_ASIMDSAMEFP16_ONLY:
{
if (preferIntrinsics())
return true;
Register srcs1[16], srcs2[16], dsts[16];
int dst_n = unpack_vector(operand1, dsts);
int src1_n = unpack_vector(operand2, srcs1);
int src2_n = unpack_vector(operand3, srcs2);
if ((dst_n != src1_n) || (src1_n != src2_n) || dst_n == 0)
ABORT_LIFT;
int rsize = get_register_size(dsts[0]);
for (int i = 0; i < dst_n; ++i)
{
auto srcs = i < dst_n / 2 ? srcs1 : srcs2;
auto index = i % (dst_n / 2);
il.AddInstruction(il.SetRegister(REGSZ(dsts[i]),
LLIL_TEMP(i), il.FloatAdd(rsize, ILREG(srcs[2 * index]), ILREG(srcs[2 * index + 1]))));
}
for (int i = 0; i < dst_n; ++i)
il.AddInstruction(ILSETREG(dsts[i], il.Register(REGSZ(dsts[i]), LLIL_TEMP(i))));
break;
}
case ENC_FADDP_Z_P_ZZ_:
default:
ABORT_LIFT;
}
break;
case ARM64_FCCMP:
case ARM64_FCCMPE:
{
LowLevelILLabel trueCode, falseCode, done;
il.AddInstruction(il.If(GetCondition(il, operand4.cond), trueCode, falseCode));
il.MarkLabel(trueCode);
il.AddInstruction(il.FloatSub(REGSZ_O(operand1), ILREG_O(operand1),
ReadILOperand(il, operand2, REGSZ_O(operand1)), SETFLAGS));
il.AddInstruction(il.Goto(done));
il.MarkLabel(falseCode);
il.AddInstruction(il.SetFlag(IL_FLAG_N, il.Const(0, (IMM_O(operand3) >> 3) & 1)));
il.AddInstruction(il.SetFlag(IL_FLAG_Z, il.Const(0, (IMM_O(operand3) >> 2) & 1)));
il.AddInstruction(il.SetFlag(IL_FLAG_C, il.Const(0, (IMM_O(operand3) >> 1) & 1)));
il.AddInstruction(il.SetFlag(IL_FLAG_V, il.Const(0, (IMM_O(operand3) >> 0) & 1)));
il.AddInstruction(il.Goto(done));
il.MarkLabel(done);
}
break;
case ARM64_FCMP:
case ARM64_FCMPE:
il.AddInstruction(il.FloatSub(REGSZ_O(operand1), ILREG_O(operand1),
ReadILOperand(il, operand2, REGSZ_O(operand1)), SETFLAGS));
break;
case ARM64_FSQRT:
switch (instr.encoding)
{
case ENC_FSQRT_D_FLOATDP1:
case ENC_FSQRT_H_FLOATDP1:
case ENC_FSQRT_S_FLOATDP1:
il.AddInstruction(ILSETREG_O(
operand1, il.FloatSqrt(REGSZ_O(operand1), ILREG_O(operand2))));
break;
case ENC_FSQRT_ASIMDMISCFP16_R:
case ENC_FSQRT_ASIMDMISC_R:
// Intrinsics
break;
default:
ABORT_LIFT;
}
break;
case ARM64_FSUB:
switch (instr.encoding)
{
case ENC_FSUB_H_FLOATDP2:
case ENC_FSUB_S_FLOATDP2:
case ENC_FSUB_D_FLOATDP2:
il.AddInstruction(ILSETREG_O(
operand1, il.FloatSub(REGSZ_O(operand1), ILREG_O(operand2), ILREG_O(operand3))));
break;
case ENC_FSUB_ASIMDSAME_ONLY:
case ENC_FSUB_ASIMDSAMEFP16_ONLY:
{
if (preferIntrinsics())
return true;
Register srcs[16], dsts[16];
int dst_n = unpack_vector(operand1, dsts);
int src_n = unpack_vector(operand2, srcs);
if ((dst_n != src_n) || dst_n == 0)
ABORT_LIFT;
int rsize = get_register_size(dsts[0]);
for (int i = 0; i < dst_n; ++i)
il.AddInstruction(ILSETREG(dsts[i], il.FloatSub(rsize, ILREG(dsts[i]), ILREG(srcs[i]))));
break;
}
default:
il.AddInstruction(il.Unimplemented());
}
break;
case ARM64_FCVT:
{
int float_sz = 0;
switch (instr.encoding)
{
/* non-SVE is straight register-to-register */
case ENC_FCVT_HS_FLOATDP1: // convert to half (2-byte)
case ENC_FCVT_HD_FLOATDP1:
float_sz = 2;
case ENC_FCVT_SH_FLOATDP1: // convert to single (4-byte)
case ENC_FCVT_SD_FLOATDP1:
if (!float_sz)
float_sz = 4;
case ENC_FCVT_DH_FLOATDP1: // convert to double (8-byte)
case ENC_FCVT_DS_FLOATDP1:
if (!float_sz)
float_sz = 8;
il.AddInstruction(ILSETREG_O(operand1, GetFloat(il, operand2, float_sz)));
break;
/* future: support SVE versions with predicated execution and z register file */
default:
ABORT_LIFT;
}
break;
}
case ARM64_FDIV:
switch (instr.encoding)
{
case ENC_FDIV_H_FLOATDP2:
case ENC_FDIV_S_FLOATDP2:
case ENC_FDIV_D_FLOATDP2:
il.AddInstruction(ILSETREG_O(
operand1, il.FloatDiv(REGSZ_O(operand1), ILREG_O(operand2), ILREG_O(operand3))));
break;
case ENC_FDIV_ASIMDSAMEFP16_ONLY:
case ENC_FDIV_ASIMDSAME_ONLY:
{
if (preferIntrinsics())
return true;
Register srcs1[16], srcs2[16], dsts[16];
int dst_n = unpack_vector(operand1, dsts);
int src1_n = unpack_vector(operand2, srcs1);
int src2_n = unpack_vector(operand3, srcs2);
if ((dst_n != src1_n) || (src1_n != src2_n) || dst_n == 0)
ABORT_LIFT;
int rsize = get_register_size(dsts[0]);
for (int i = 0; i < dst_n; ++i)
il.AddInstruction(ILSETREG(
dsts[i], il.FloatDiv(rsize, ILREG(srcs1[i]), ILREG(srcs2[i]))));
break;
}
case ENC_FDIV_Z_P_ZZ_:
default:
ABORT_LIFT;
}
break;
case ARM64_FMOV:
switch (instr.encoding)
{
case ENC_FMOV_64VX_FLOAT2INT:
il.AddInstruction(ILSETREG_O(operand1, ILREG(vector_reg_minimize(instr.operands[1]))));
break;
case ENC_FMOV_V64I_FLOAT2INT:
{
Register minreg = vector_reg_minimize(instr.operands[0]);
il.AddInstruction(il.SetRegister(get_register_size(minreg), minreg,
il.Register(REGSZ_O(operand1), instr.operands[1].reg[0])));
break;
}
case ENC_FMOV_32H_FLOAT2INT:
case ENC_FMOV_32S_FLOAT2INT:
case ENC_FMOV_64H_FLOAT2INT:
case ENC_FMOV_64D_FLOAT2INT:
{
bool extend = REGSZ_O(operand1) > REGSZ_O(instr.operands[1]);
ExprId tmp;
// <Rd> <- <Vn> (copy from FP register to general register, with no conversion)
if (extend)
tmp = ILSETREG_O(operand1, il.ZeroExtend(REGSZ_O(operand1), ILREG_O(instr.operands[1])));
else
tmp = ILSETREG_O(operand1, ILREG_O(instr.operands[1]));
il.AddInstruction(tmp);
break;
}
case ENC_FMOV_D64_FLOAT2INT:
case ENC_FMOV_H32_FLOAT2INT:
case ENC_FMOV_H64_FLOAT2INT:
case ENC_FMOV_S32_FLOAT2INT:
// <Vd> <- <Rn> (copy from general register to FP register, with no conversion)
il.AddInstruction(
ILSETREG_O(operand1, il.IntToFloat(REGSZ_O(operand1), ILREG_O(instr.operands[1]))));
break;
case ENC_FMOV_H_FLOATIMM:
case ENC_FMOV_S_FLOATIMM:
case ENC_FMOV_D_FLOATIMM:
{
int float_sz = 2;
if (instr.encoding == ENC_FMOV_S_FLOATIMM)
float_sz = 4;
if (instr.encoding == ENC_FMOV_D_FLOATIMM)
float_sz = 8;
// Technically, we should use 2 bytes to GetFloat for half-precision registers, but that causes MLIL and HLIL to lift the constant to 0
il.AddInstruction(ILSETREG_O(operand1, il.FloatConvert(float_sz, GetFloat(il, operand2, float_sz == 2 ? 4 : float_sz))));
break;
}
case ENC_FMOV_H_FLOATDP1:
case ENC_FMOV_S_FLOATDP1:
case ENC_FMOV_D_FLOATDP1:
il.AddInstruction(ILSETREG_O(operand1, ILREG_O(operand2)));
break;
case ENC_FMOV_ASIMDIMM_D2_D:
case ENC_FMOV_ASIMDIMM_H_H:
case ENC_FMOV_ASIMDIMM_S_S:
{
if (preferIntrinsics())
return true;
int float_sz = 2;
if (instr.encoding == ENC_FMOV_ASIMDIMM_S_S)
float_sz = 4;
if (instr.encoding == ENC_FMOV_ASIMDIMM_D2_D)
float_sz = 8;
Register regs[16];
int dst_n = unpack_vector(operand1, regs);
for (int i = 0; i < dst_n; ++i)
// Technically, we should use 2 bytes to GetFloat for half-precision registers, but that causes MLIL and HLIL to lift the constant to 0
il.AddInstruction(ILSETREG(regs[i], il.FloatConvert(float_sz, GetFloat(il, operand2, float_sz == 2 ? 4 : float_sz))));
break;
}
default:
ABORT_LIFT;
}
break;
case ARM64_FMUL:
switch (instr.encoding)
{
case ENC_FMUL_H_FLOATDP2:
case ENC_FMUL_S_FLOATDP2:
case ENC_FMUL_D_FLOATDP2:
il.AddInstruction(ILSETREG_O(
operand1, il.FloatMult(REGSZ_O(operand1), ILREG_O(operand2), ILREG_O(operand3))));
break;
case ENC_FMUL_ASIMDSAME_ONLY:
case ENC_FMUL_ASIMDSAMEFP16_ONLY:
{
if (preferIntrinsics())
return true;
Register srcs1[16], srcs2[16], dsts[16];
int dst_n = unpack_vector(operand1, dsts);
int src1_n = unpack_vector(operand2, srcs1);
int src2_n = unpack_vector(operand3, srcs2);
if ((dst_n != src1_n) || (src1_n != src2_n) || dst_n == 0)
ABORT_LIFT;
int rsize = get_register_size(dsts[0]);
for (int i = 0; i < dst_n; ++i)
il.AddInstruction(ILSETREG(
dsts[i], il.FloatMult(rsize, ILREG(srcs1[i]), ILREG(srcs2[i]))));
break;
}
case ENC_FMUL_ASIMDELEM_RH_H:
case ENC_FMUL_ASIMDELEM_R_SD:
case ENC_FMUL_ASISDELEM_RH_H:
case ENC_FMUL_ASISDELEM_R_SD:
{
if (preferIntrinsics())
return true;
Register srcs1[16], srcs2[16], dsts[16];
int dst_n = unpack_vector(operand1, dsts);
int src1_n = unpack_vector(operand2, srcs1);
int src2_n = unpack_vector(operand3, srcs2);
if ((dst_n != src1_n) || dst_n == 0 || src2_n != 1)
ABORT_LIFT;
int rsize = get_register_size(dsts[0]);
for (int i = 0; i < dst_n; ++i)
il.AddInstruction(ILSETREG(
dsts[i], il.FloatMult(rsize, ILREG(srcs1[i]), ILREG(srcs2[0]))));
break;
}
default:
ABORT_LIFT;
}
break;
case ARM64_FNEG:
switch (instr.encoding)
{
case ENC_FNEG_D_FLOATDP1:
case ENC_FNEG_S_FLOATDP1:
case ENC_FNEG_H_FLOATDP1:
il.AddInstruction(ILSETREG_O(
operand1, il.FloatNeg(REGSZ_O(operand1), ILREG_O(operand2))));
break;
case ENC_FNEG_ASIMDMISCFP16_R:
case ENC_FNEG_ASIMDMISC_R:
{
if (preferIntrinsics())
return true;
Register srcs[16], dsts[16];
int dst_n = unpack_vector(operand1, dsts);
int src_n = unpack_vector(operand2, srcs);
if ((dst_n != src_n) || dst_n == 0)
ABORT_LIFT;
int rsize = get_register_size(dsts[0]);
for (int i = 0; i < dst_n; ++i)
il.AddInstruction(ILSETREG(dsts[i], il.FloatNeg(rsize, ILREG(srcs[i]))));
break;
}
default:
ABORT_LIFT;
}
break;
case ARM64_FNMUL:
il.AddInstruction(ILSETREG_O(operand1,
il.FloatNeg(REGSZ_O(operand1),
il.FloatMult(REGSZ_O(operand1), ILREG_O(operand2), ILREG_O(operand3)))));
break;
case ARM64_ERET:
case ARM64_ERETAA:
case ARM64_ERETAB:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_ERET, {}));
il.AddInstruction(il.Trap(0));
return false;
case ARM64_GMI:
il.AddInstruction(il.Intrinsic(
{RegisterOrFlag::Register(REG_O(operand1))}, ARM64_INTRIN_GMI, {ILREG_O(operand2), ILREG_O(operand3)}));
break;
case ARM64_IRG:
il.AddInstruction(il.Intrinsic({RegisterOrFlag::Register(REG_O(operand1))}, ARM64_INTRIN_IRG,
{
ILREG_O(operand2),
operand3.operandClass == NONE ? il.Const(REGSZ_O(operand2), 0) : ILREG_O(operand3),
}));
break;
case ARM64_ISB:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_ISB, {}));
break;
case ARM64_LDAR:
case ARM64_LDAPR:
case ARM64_LDAPUR:
LoadStoreOperand(il, true, instr.operands[0], instr.operands[1], 0);
break;
case ARM64_LDARB:
case ARM64_LDAPRB:
case ARM64_LDAPURB:
LoadStoreOperandSize(il, true, false, 1, instr.operands[0], instr.operands[1]);
break;
case ARM64_LDARH:
case ARM64_LDAPRH:
case ARM64_LDAPURH:
LoadStoreOperandSize(il, true, false, 2, instr.operands[0], instr.operands[1]);
break;
case ARM64_LDP:
case ARM64_LDNP:
LoadStoreOperandPair(il, true, instr.operands[0], instr.operands[1], instr.operands[2]);
break;
case ARM64_LDPSW:
LoadStoreOperandPairSize(il, true, 4, instr.operands[0], instr.operands[1], instr.operands[2]);
break;
case ARM64_LDRAA:
case ARM64_LDRAB:
SetPacAttr = true;
case ARM64_LDR:
switch (instr.encoding)
{
case ENC_LDR_P_BI_:
case ENC_LDR_Z_BI_:
case ENC_LDR_ZA_RI_:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
case ARM64_LDUR:
LoadStoreOperand(il, true, instr.operands[0], instr.operands[1], 0);
if (SetPacAttr)
ApplyAttributeToLastInstruction(il, SrcInstructionUsesPointerAuth);
break;
case ARM64_LDG:
il.AddInstruction(il.Intrinsic({RegisterOrFlag::Register(REG_O(operand1))}, ARM64_INTRIN_LDG,
{GetILOperandEffectiveAddress(il, operand2, 8, operand2.operandClass, 0)}));
break;
case ARM64_LDGM:
il.AddInstruction(
il.Intrinsic({RegisterOrFlag::Register(REG_O(operand1))}, ARM64_INTRIN_LDGM, {ILREG_O(operand2)}));
break;
case ARM64_LDRB:
case ARM64_LDURB:
LoadStoreOperandSize(il, true, false, 1, instr.operands[0], instr.operands[1]);
break;
case ARM64_LDRH:
case ARM64_LDURH:
LoadStoreOperandSize(il, true, false, 2, instr.operands[0], instr.operands[1]);
break;
case ARM64_LDRSB:
case ARM64_LDURSB:
case ARM64_LDAPURSB:
LoadStoreOperandSize(il, true, true, 1, instr.operands[0], instr.operands[1]);
break;
case ARM64_LDRSH:
case ARM64_LDURSH:
case ARM64_LDAPURSH:
LoadStoreOperandSize(il, true, true, 2, instr.operands[0], instr.operands[1]);
break;
case ARM64_LDRSW:
case ARM64_LDURSW:
case ARM64_LDAPURSW:
LoadStoreOperandSize(il, true, true, 4, instr.operands[0], instr.operands[1]);
break;
case ARM64_LDXR:
il.AddInstruction(il.Intrinsic({ RegisterOrFlag::Register(REG_O(operand1)) }, ARM64_INTRIN_LDXR, { ILREG_O(operand2) }));
break;
case ARM64_LDXRB:
il.AddInstruction(il.Intrinsic({ RegisterOrFlag::Register(REG_O(operand1)) }, ARM64_INTRIN_LDXRB, { ILREG_O(operand2) }));
break;
case ARM64_LDXRH:
il.AddInstruction(il.Intrinsic({ RegisterOrFlag::Register(REG_O(operand1)) }, ARM64_INTRIN_LDXRH, { ILREG_O(operand2) }));
break;
// We don't have a way to specify intrinsic register size, so we explicitly embed the size in the intrinsic name.
case ARM64_LDXP:
il.AddInstruction(il.Intrinsic({ RegisterOrFlag::Register(REG_O(operand1)), RegisterOrFlag::Register(REG_O(operand2)) }, ARM64_INTRIN_LDXP, { ILREG_O(operand3) }));
break;
case ARM64_LDAXR:
il.AddInstruction(il.Intrinsic({ RegisterOrFlag::Register(REG_O(operand1)) }, ARM64_INTRIN_LDAXR, { ILREG_O(operand2) }));
break;
case ARM64_LDAXRB:
il.AddInstruction(il.Intrinsic({ RegisterOrFlag::Register(REG_O(operand1)) }, ARM64_INTRIN_LDAXRB, { ILREG_O(operand2) }));
break;
case ARM64_LDAXRH:
il.AddInstruction(il.Intrinsic({ RegisterOrFlag::Register(REG_O(operand1)) }, ARM64_INTRIN_LDAXRH, { ILREG_O(operand2) }));
break;
case ARM64_STXR:
il.AddInstruction(il.Intrinsic({ RegisterOrFlag::Register(REG_O(operand1)) }, ARM64_INTRIN_STXR, { ILREG_O(operand2), ILREG_O(operand3) }));
break;
case ARM64_STXRB:
il.AddInstruction(il.Intrinsic({ RegisterOrFlag::Register(REG_O(operand1)) }, ARM64_INTRIN_STXRB, { ILREG_O(operand2), ILREG_O(operand3) }));
break;
case ARM64_STXRH:
il.AddInstruction(il.Intrinsic({ RegisterOrFlag::Register(REG_O(operand1)) }, ARM64_INTRIN_STXRH, { ILREG_O(operand2), ILREG_O(operand3) }));
break;
case ARM64_STXP:
il.AddInstruction(il.Intrinsic({ RegisterOrFlag::Register(REG_O(operand1)) }, ARM64_INTRIN_STXP, { ILREG_O(operand2), ILREG_O(operand3), ILREG_O(operand4) }));
break;
case ARM64_STLXR:
il.AddInstruction(il.Intrinsic({ RegisterOrFlag::Register(REG_O(operand1)) }, ARM64_INTRIN_STLXR, { ILREG_O(operand2), ILREG_O(operand3) }));
break;
case ARM64_STLXRB:
il.AddInstruction(il.Intrinsic({ RegisterOrFlag::Register(REG_O(operand1)) }, ARM64_INTRIN_STLXRB, { ILREG_O(operand2), ILREG_O(operand3) }));
break;
case ARM64_STLXRH:
il.AddInstruction(il.Intrinsic({ RegisterOrFlag::Register(REG_O(operand1)) }, ARM64_INTRIN_STLXRH, { ILREG_O(operand2), ILREG_O(operand3) }));
break;
case ARM64_LD1R:
case ARM64_LD2R:
case ARM64_LD3R:
case ARM64_LD4R:
if (true || !preferIntrinsics()) // For now, forcibly disable intrinsics (they are incomplete, and this could help dataflow)
LoadStoreVector(il, true, instr.operands[0], instr.operands[1], true);
break;
case ARM64_LD1:
case ARM64_LD2:
case ARM64_LD3:
case ARM64_LD4:
if (true || !preferIntrinsics()) // For now, forcibly disable intrinsics (they are incomplete, and this could help dataflow)
LoadStoreVector(il, true, instr.operands[0], instr.operands[1]);
break;
case ARM64_LDADD:
case ARM64_LDADDA:
case ARM64_LDADDL:
case ARM64_LDADDAL:
{
// TODO: represent/annotate (model?) acquire/release memory ordering semantics for all LDADD* instructions
// I want to do this, but I also don't want to add an #ifdef __clang__
// InstructionOperand tmp = { .reg = { (Register) LLIL_TEMP(0) } };
InstructionOperand tmp;
tmp.reg[0] = (Register) LLIL_TEMP(0);
LoadStoreOperandSize(il, true, false, REGSZ_O(operand2), tmp, operand3);
il.AddInstruction(il.Store(REGSZ_O(operand2), ILREG_O(operand3),
il.Add(REGSZ_O(operand1),
ILREG_O(operand1),
il.ZeroExtend(REGSZ_O(operand2),
il.Register(REGSZ_O(operand2), LLIL_TEMP(0))))));
if (!(operand2.reg[0] == REG_XZR || operand2.reg[0] == REG_WZR))
il.AddInstruction(ILSETREG_O(operand2,
il.ZeroExtend(REGSZ_O(operand2),
il.Register(REGSZ_O(operand2), LLIL_TEMP(0)))));
break;
}
case ARM64_STADD:
case ARM64_STADDL:
// STADD* are aliases of the corresponding LDADD*, so group them together
il.AddInstruction(il.Store(REGSZ_O(operand2), ILREG_O(operand2),
il.Add(REGSZ_O(operand1), ILREG_O(operand1), il.Load(REGSZ_O(operand1), ILREG_O(operand2)))));
break;
case ARM64_LDADDB:
case ARM64_LDADDAB:
case ARM64_LDADDLB:
case ARM64_LDADDALB:
{
// InstructionOperand tmp = { .reg = { (Register) LLIL_TEMP(0) } };
InstructionOperand tmp;
tmp.reg[0] = (Register) LLIL_TEMP(0);
LoadStoreOperandSize(il, true, false, 1, tmp, operand3);
il.AddInstruction(il.Store(1, ILREG_O(operand3),
il.Add(1, il.LowPart(1, ILREG_O(operand1)), il.LowPart(1, il.Register(1, LLIL_TEMP(0))))));
if (!(operand2.reg[0] == REG_XZR || operand2.reg[0] == REG_WZR))
il.AddInstruction(ILSETREG_O(operand2,
il.ZeroExtend(REGSZ_O(operand2),
il.LowPart(1, il.Register(1, LLIL_TEMP(0))))));
break;
}
case ARM64_STADDB:
case ARM64_STADDLB:
// STADD* are aliases of the corresponding LDADD*, so group them together
il.AddInstruction(il.Store(1, ILREG_O(operand2),
il.Add(1, il.LowPart(1, ILREG_O(operand1)), il.Load(1, ILREG_O(operand2)))));
break;
case ARM64_LDADDH:
case ARM64_LDADDAH:
case ARM64_LDADDLH:
case ARM64_LDADDALH:
{
// InstructionOperand tmp = { .reg = { (Register) LLIL_TEMP(0) } };
InstructionOperand tmp;
tmp.reg[0] = (Register) LLIL_TEMP(0);
LoadStoreOperandSize(il, true, false, 2, tmp, operand3);
il.AddInstruction(il.Store(2, ILREG_O(operand3),
il.Add(2, il.LowPart(2, ILREG_O(operand1)), il.LowPart(2, il.Register(2, LLIL_TEMP(0))))));
if (!(operand2.reg[0] == REG_XZR || operand2.reg[0] == REG_WZR))
il.AddInstruction(ILSETREG_O(operand2,
il.ZeroExtend(REGSZ_O(operand2),
il.LowPart(2, il.Register(2, LLIL_TEMP(0))))));
break;
}
case ARM64_STADDH:
case ARM64_STADDLH:
// STADD* are aliases of the corresponding LDADD*, so group them together
il.AddInstruction(il.Store(2, ILREG_O(operand2),
il.Add(2, il.LowPart(2, ILREG_O(operand1)), il.Load(2, ILREG_O(operand2)))));
break;
case ARM64_LDCLR:
case ARM64_LDCLRA:
case ARM64_LDCLRL:
case ARM64_LDCLRAL:
{
// TODO: represent/annotate (model?) acquire/release memory ordering semantics for all LDCLR* instructions
// InstructionOperand tmp = { .reg = { (Register) LLIL_TEMP(0) } };
InstructionOperand tmp;
tmp.reg[0] = (Register) LLIL_TEMP(0);
LoadStoreOperandSize(il, true, false, REGSZ_O(operand2), tmp, operand3);
il.AddInstruction(il.Store(REGSZ_O(operand2), ILREG_O(operand3),
il.And(REGSZ_O(operand1),
il.Not(REGSZ_O(operand1), ILREG_O(operand1)),
il.ZeroExtend(REGSZ_O(operand2),
il.Register(REGSZ_O(operand2), LLIL_TEMP(0))))));
if (!(operand2.reg[0] == REG_XZR || operand2.reg[0] == REG_WZR))
il.AddInstruction(ILSETREG_O(operand2,
il.ZeroExtend(REGSZ_O(operand2),
il.Register(REGSZ_O(operand2), LLIL_TEMP(0)))));
break;
}
case ARM64_STCLR:
case ARM64_STCLRL:
// STCLR* are aliases of the corresponding LDCLR*, so group them together
il.AddInstruction(il.Store(REGSZ_O(operand2), ILREG_O(operand2),
il.And(REGSZ_O(operand1),
il.Not(REGSZ_O(operand1), ILREG_O(operand1)),
il.Load(REGSZ_O(operand1), ILREG_O(operand2)))));
break;
case ARM64_LDCLRB:
case ARM64_LDCLRAB:
case ARM64_LDCLRLB:
case ARM64_LDCLRALB:
{
// InstructionOperand tmp = { .reg = { (Register) LLIL_TEMP(0) } };
InstructionOperand tmp;
tmp.reg[0] = (Register) LLIL_TEMP(0);
LoadStoreOperandSize(il, true, false, 1, tmp, operand3);
il.AddInstruction(il.Store(1, ILREG_O(operand3),
il.And(1, il.Not(1, il.LowPart(1, ILREG_O(operand1))), il.LowPart(1, il.Register(1, LLIL_TEMP(0))))));
if (!(operand2.reg[0] == REG_XZR || operand2.reg[0] == REG_WZR))
il.AddInstruction(ILSETREG_O(operand2,
il.ZeroExtend(REGSZ_O(operand2),
il.LowPart(1, il.Register(1, LLIL_TEMP(0))))));
break;
}
case ARM64_STCLRB:
case ARM64_STCLRLB:
// STCLR* are aliases of the corresponding LDCLR*, so group them together
il.AddInstruction(il.Store(1, ILREG_O(operand2),
il.And(1, il.Not(1, il.LowPart(1, ILREG_O(operand1))), il.Load(1, ILREG_O(operand2)))));
break;
case ARM64_LDCLRH:
case ARM64_LDCLRAH:
case ARM64_LDCLRLH:
case ARM64_LDCLRALH:
{
// InstructionOperand tmp = { .reg = { (Register) LLIL_TEMP(0) } };
InstructionOperand tmp;
tmp.reg[0] = (Register) LLIL_TEMP(0);
LoadStoreOperandSize(il, true, false, 2, tmp, operand3);
il.AddInstruction(il.Store(2, ILREG_O(operand3),
il.And(2, il.Not(2, il.LowPart(2, ILREG_O(operand1))), il.LowPart(2, il.Register(2, LLIL_TEMP(0))))));
if (!(operand2.reg[0] == REG_XZR || operand2.reg[0] == REG_WZR))
il.AddInstruction(ILSETREG_O(operand2,
il.ZeroExtend(REGSZ_O(operand2),
il.LowPart(2, il.Register(2, LLIL_TEMP(0))))));
break;
}
case ARM64_STCLRH:
case ARM64_STCLRLH:
// STCLR* are aliases of the corresponding LDCLR*, so group them together
il.AddInstruction(il.Store(2, ILREG_O(operand2),
il.And(2, il.Not(2, il.LowPart(2, ILREG_O(operand1))), il.Load(2, ILREG_O(operand2)))));
break;
case ARM64_LDEOR:
case ARM64_LDEORA:
case ARM64_LDEORL:
case ARM64_LDEORAL:
{
// TODO: represent/annotate (model?) acquire/release memory ordering semantics for all LDEOR* instructions
// InstructionOperand tmp = { .reg = { (Register) LLIL_TEMP(0) } };
InstructionOperand tmp;
tmp.reg[0] = (Register) LLIL_TEMP(0);
LoadStoreOperandSize(il, true, false, REGSZ_O(operand2), tmp, operand3);
il.AddInstruction(il.Store(REGSZ_O(operand2), ILREG_O(operand3),
il.Xor(REGSZ_O(operand1),
ILREG_O(operand1),
il.ZeroExtend(REGSZ_O(operand2),
il.Register(REGSZ_O(operand2), LLIL_TEMP(0))))));
if (!(operand2.reg[0] == REG_XZR || operand2.reg[0] == REG_WZR))
il.AddInstruction(ILSETREG_O(operand2,
il.ZeroExtend(REGSZ_O(operand2),
il.Register(REGSZ_O(operand2), LLIL_TEMP(0)))));
break;
}
case ARM64_STEOR:
case ARM64_STEORL:
// STEOR* are aliases of the corresponding LDEOR*, so group them together
il.AddInstruction(il.Store(REGSZ_O(operand2), ILREG_O(operand2),
il.Xor(REGSZ_O(operand1), ILREG_O(operand1), il.Load(REGSZ_O(operand1), ILREG_O(operand2)))));
break;
case ARM64_LDEORB:
case ARM64_LDEORAB:
case ARM64_LDEORLB:
case ARM64_LDEORALB:
{
// InstructionOperand tmp = { .reg = { (Register) LLIL_TEMP(0) } };
InstructionOperand tmp;
tmp.reg[0] = (Register) LLIL_TEMP(0);
LoadStoreOperandSize(il, true, false, 1, tmp, operand3);
il.AddInstruction(il.Store(1, ILREG_O(operand3),
il.Xor(1, il.LowPart(1, ILREG_O(operand1)), il.LowPart(1, il.Register(1, LLIL_TEMP(0))))));
if (!(operand2.reg[0] == REG_XZR || operand2.reg[0] == REG_WZR))
il.AddInstruction(ILSETREG_O(operand2,
il.ZeroExtend(REGSZ_O(operand2),
il.LowPart(1, il.Register(1, LLIL_TEMP(0))))));
break;
}
case ARM64_STEORB:
case ARM64_STEORLB:
// STEOR* are aliases of the corresponding LDEOR*, so group them together
il.AddInstruction(il.Store(1, ILREG_O(operand2),
il.Xor(1, il.LowPart(1, ILREG_O(operand1)), il.Load(1, ILREG_O(operand2)))));
break;
case ARM64_LDEORH:
case ARM64_LDEORAH:
case ARM64_LDEORLH:
case ARM64_LDEORALH:
{
// InstructionOperand tmp = { .reg = { (Register) LLIL_TEMP(0) } };
InstructionOperand tmp;
tmp.reg[0] = (Register) LLIL_TEMP(0);
LoadStoreOperandSize(il, true, false, 2, tmp, operand3);
il.AddInstruction(il.Store(2, ILREG_O(operand3),
il.Xor(2, il.LowPart(2, ILREG_O(operand1)), il.LowPart(2, il.Register(2, LLIL_TEMP(0))))));
if (!(operand2.reg[0] == REG_XZR || operand2.reg[0] == REG_WZR))
il.AddInstruction(ILSETREG_O(operand2,
il.ZeroExtend(REGSZ_O(operand2),
il.LowPart(2, il.Register(2, LLIL_TEMP(0))))));
break;
}
case ARM64_STEORH:
case ARM64_STEORLH:
// STEOR* are aliases of the corresponding LDEOR*, so group them together
il.AddInstruction(il.Store(2, ILREG_O(operand2),
il.Xor(2, il.LowPart(2, ILREG_O(operand1)), il.Load(2, ILREG_O(operand2)))));
break;
case ARM64_LDSET:
case ARM64_LDSETA:
case ARM64_LDSETL:
case ARM64_LDSETAL:
{
// TODO: represent/annotate (model?) acquire/release memory ordering semantics for all LDSET* instructions
// InstructionOperand tmp = { .reg = { (Register) LLIL_TEMP(0) } };
InstructionOperand tmp;
tmp.reg[0] = (Register) LLIL_TEMP(0);
LoadStoreOperandSize(il, true, false, REGSZ_O(operand2), tmp, operand3);
il.AddInstruction(il.Store(REGSZ_O(operand2), ILREG_O(operand3),
il.Or(REGSZ_O(operand1),
ILREG_O(operand1),
il.ZeroExtend(REGSZ_O(operand2),
il.Register(REGSZ_O(operand2), LLIL_TEMP(0))))));
if (!(operand2.reg[0] == REG_XZR || operand2.reg[0] == REG_WZR))
il.AddInstruction(ILSETREG_O(operand2,
il.ZeroExtend(REGSZ_O(operand2),
il.Register(REGSZ_O(operand2), LLIL_TEMP(0)))));
break;
}
case ARM64_STSET:
case ARM64_STSETL:
// STSET* are aliases of the corresponding LDSET*, so group them together
il.AddInstruction(il.Store(REGSZ_O(operand2), ILREG_O(operand2),
il.Or(REGSZ_O(operand1), ILREG_O(operand1), il.Load(REGSZ_O(operand1), ILREG_O(operand2)))));
break;
case ARM64_LDSETB:
case ARM64_LDSETAB:
case ARM64_LDSETLB:
case ARM64_LDSETALB:
{
// InstructionOperand tmp = { .reg = { (Register) LLIL_TEMP(0) } };
InstructionOperand tmp;
tmp.reg[0] = (Register) LLIL_TEMP(0);
LoadStoreOperandSize(il, true, false, 1, tmp, operand3);
il.AddInstruction(il.Store(1, ILREG_O(operand3),
il.Or(1, il.LowPart(1, ILREG_O(operand1)), il.LowPart(1, il.Register(1, LLIL_TEMP(0))))));
if (!(operand2.reg[0] == REG_XZR || operand2.reg[0] == REG_WZR))
il.AddInstruction(ILSETREG_O(operand2,
il.ZeroExtend(REGSZ_O(operand2),
il.LowPart(1, il.Register(1, LLIL_TEMP(0))))));
break;
}
case ARM64_STSETB:
case ARM64_STSETLB:
// STSET* are aliases of the corresponding LDSET*, so group them together
il.AddInstruction(il.Store(1, ILREG_O(operand2),
il.Or(1, il.LowPart(1, ILREG_O(operand1)), il.Load(1, ILREG_O(operand2)))));
break;
case ARM64_LDSETH:
case ARM64_LDSETAH:
case ARM64_LDSETLH:
case ARM64_LDSETALH:
{
// InstructionOperand tmp = { .reg = { (Register) LLIL_TEMP(0) } };
InstructionOperand tmp;
tmp.reg[0] = (Register) LLIL_TEMP(0);
LoadStoreOperandSize(il, true, false, 2, tmp, operand3);
il.AddInstruction(il.Store(2, ILREG_O(operand3),
il.Or(2, il.LowPart(2, ILREG_O(operand1)), il.LowPart(2, il.Register(2, LLIL_TEMP(0))))));
if (!(operand2.reg[0] == REG_XZR || operand2.reg[0] == REG_WZR))
il.AddInstruction(ILSETREG_O(operand2,
il.ZeroExtend(REGSZ_O(operand2),
il.LowPart(2, il.Register(2, LLIL_TEMP(0))))));
break;
}
case ARM64_STSETH:
case ARM64_STSETLH:
// STSET* are aliases of the corresponding LDSET*, so group them together
il.AddInstruction(il.Store(2, ILREG_O(operand2),
il.Or(2, il.LowPart(2, ILREG_O(operand1)), il.Load(2, ILREG_O(operand2)))));
break;
case ARM64_LSL:
il.AddInstruction(ILSETREG_O(operand1, il.ShiftLeft(REGSZ_O(operand2), ILREG_O(operand2),
ReadILOperand(il, operand3, REGSZ_O(operand2)))));
break;
case ARM64_LSR:
il.AddInstruction(
ILSETREG_O(operand1, il.LogicalShiftRight(REGSZ_O(operand2), ILREG_O(operand2),
ReadILOperand(il, operand3, REGSZ_O(operand2)))));
break;
case ARM64_MOV:
switch (instr.encoding)
{
case ENC_MOVZ_P_P_P__AND_P_P_PP_Z:
case ENC_MOV_Z_O_I__CPY_Z_O_I_:
case ENC_MOV_Z_P_I__CPY_Z_P_I_:
case ENC_MOV_Z_P_R__CPY_Z_P_R_:
case ENC_MOV_Z_P_V__CPY_Z_P_V_:
case ENC_MOV_Z_I__DUP_Z_I_:
case ENC_MOV_Z_R__DUP_Z_R_:
case ENC_MOV_Z_V__DUP_Z_ZI_:
case ENC_MOV_Z_ZI__DUP_Z_ZI_:
case ENC_MOV_Z_M__DUPM_Z_I_:
case ENC_MOV_MZ2_ZA_B1_MOVA_MZ2_ZA_B1:
case ENC_MOV_MZ2_ZA_H1_MOVA_MZ2_ZA_H1:
case ENC_MOV_MZ2_ZA_W1_MOVA_MZ2_ZA_W1:
case ENC_MOV_MZ2_ZA_D1_MOVA_MZ2_ZA_D1:
case ENC_MOV_MZ4_ZA_B1_MOVA_MZ4_ZA_B1:
case ENC_MOV_MZ4_ZA_H1_MOVA_MZ4_ZA_H1:
case ENC_MOV_MZ4_ZA_W1_MOVA_MZ4_ZA_W1:
case ENC_MOV_MZ4_ZA_D1_MOVA_MZ4_ZA_D1:
case ENC_MOV_MZ_ZA2_1_MOVA_MZ_ZA2_1:
case ENC_MOV_MZ_ZA4_1_MOVA_MZ_ZA4_1:
case ENC_MOV_Z_P_RZA_B_MOVA_Z_P_RZA_B:
case ENC_MOV_Z_P_RZA_H_MOVA_Z_P_RZA_H:
case ENC_MOV_Z_P_RZA_W_MOVA_Z_P_RZA_W:
case ENC_MOV_Z_P_RZA_D_MOVA_Z_P_RZA_D:
case ENC_MOV_Z_P_RZA_Q_MOVA_Z_P_RZA_Q:
case ENC_MOV_ZA2_Z_B1_MOVA_ZA2_Z_B1:
case ENC_MOV_ZA2_Z_H1_MOVA_ZA2_Z_H1:
case ENC_MOV_ZA2_Z_W1_MOVA_ZA2_Z_W1:
case ENC_MOV_ZA2_Z_D1_MOVA_ZA2_Z_D1:
case ENC_MOV_ZA4_Z_B1_MOVA_ZA4_Z_B1:
case ENC_MOV_ZA4_Z_H1_MOVA_ZA4_Z_H1:
case ENC_MOV_ZA4_Z_W1_MOVA_ZA4_Z_W1:
case ENC_MOV_ZA4_Z_D1_MOVA_ZA4_Z_D1:
case ENC_MOV_ZA_MZ2_1_MOVA_ZA_MZ2_1:
case ENC_MOV_ZA_MZ4_1_MOVA_ZA_MZ4_1:
case ENC_MOV_ZA_P_RZ_B_MOVA_ZA_P_RZ_B:
case ENC_MOV_ZA_P_RZ_H_MOVA_ZA_P_RZ_H:
case ENC_MOV_ZA_P_RZ_W_MOVA_ZA_P_RZ_W:
case ENC_MOV_ZA_P_RZ_D_MOVA_ZA_P_RZ_D:
case ENC_MOV_ZA_P_RZ_Q_MOVA_ZA_P_RZ_Q:
case ENC_MOV_P_P__ORR_P_P_PP_Z:
case ENC_MOV_Z_Z__ORR_Z_ZZ_:
case ENC_MOVM_P_P_P__SEL_P_P_PP_:
case ENC_MOV_Z_P_Z__SEL_Z_P_ZZ_:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
case ARM64_DUP:
case ARM64_MOVN:
case ARM64_UMOV:
case ARM64_INS:
case ARM64_MOVS:
{
bool zero_extend = false;
switch (instr.encoding)
{
case ENC_DUP_ASIMDINS_DR_R:
{
// if (preferIntrinsics())
// return true;
Register regs[16];
int regs_n = unpack_vector(operand1, regs);
if (regs_n <= 0)
ABORT_LIFT;
int lane_sz = REGSZ(regs[0]);
for (int i = 0; i < regs_n; ++i)
il.AddInstruction(ILSETREG(regs[i], ExtractRegister(il, operand2, 0, lane_sz, 0, lane_sz)));
break;
}
case ENC_DUP_ASIMDINS_DV_V:
// We let the Neon intrinsic lifter take care of this case.
break;
case ENC_MOV_UMOV_ASIMDINS_W_W:
case ENC_MOV_UMOV_ASIMDINS_X_X:
case ENC_UMOV_ASIMDINS_W_W:
case ENC_UMOV_ASIMDINS_X_X:
zero_extend = true;
case ENC_MOV_DUP_ASISDONE_ONLY:
case ENC_DUP_ASISDONE_ONLY:
case ENC_MOV_INS_ASIMDINS_IV_V:
case ENC_INS_ASIMDINS_IV_V:
{
Register srcs[16], dsts[16];
int dst_n = unpack_vector(operand1, dsts);
int src_n = unpack_vector(operand2, srcs);
if ((dst_n != src_n) || dst_n == 0)
ABORT_LIFT;
// if (dst_n > 1 && preferIntrinsics())
// return true;
for (int i = 0; i < dst_n; ++i)
il.AddInstruction(ILSETREG(dsts[i], zero_extend
? il.ZeroExtend(REGSZ(dsts[i]), ILREG(srcs[i]))
: ILREG(srcs[i])));
break;
}
case ENC_MOV_MOVN_32_MOVEWIDE:
case ENC_MOV_MOVN_64_MOVEWIDE:
case ENC_MOVN_32_MOVEWIDE:
case ENC_MOVN_64_MOVEWIDE:
il.AddInstruction(ILSETREG_O(operand1,
ReadILOperand(il, operand2, REGSZ_O(operand1))));
break;
case ENC_MOV_INS_ASIMDINS_IR_R:
case ENC_INS_ASIMDINS_IR_R:
case ENC_MOV_ORR_32_LOG_IMM:
case ENC_MOV_ORR_32_LOG_SHIFT:
case ENC_MOV_ORR_64_LOG_IMM:
case ENC_MOV_ORR_64_LOG_SHIFT:
case ENC_MOV_ADD_32_ADDSUB_IMM:
case ENC_MOV_ADD_64_ADDSUB_IMM:
case ENC_MOV_ORR_ASIMDSAME_ONLY:
case ENC_MOV_MOVZ_32_MOVEWIDE:
case ENC_MOV_MOVZ_64_MOVEWIDE:
{
Register regs[16];
int n = unpack_vector(operand1, regs);
if (n == 1) {
il.AddInstruction(ILSETREG(regs[0], ReadILOperand(il, operand2, get_register_size(regs[0]))));
} else {
Register cregs[2];
if (consolidate_vector(operand1, operand2, cregs))
il.AddInstruction(ILSETREG(cregs[0], ILREG(cregs[1])));
else
ABORT_LIFT;
}
break;
}
case ENC_PSEL_P_PPI_:
case ENC_DUP_Z_I_:
case ENC_DUP_Z_R_:
case ENC_DUP_Z_ZI_:
default:
// case ENC_MOVS_ORRS_P_P_PP_Z:
// il.AddInstruction(il.Unimplemented());
break;
}
break;
}
case ARM64_MOVI:
{
Register regs[16];
int n = unpack_vector(operand1, regs);
for (int i = 0; i < n; ++i)
il.AddInstruction(ILSETREG(regs[i], ILCONST_O(get_register_size(regs[i]), operand2)));
break;
}
case ARM64_MVN:
case ARM64_MVNI:
il.AddInstruction(ILSETREG_O(
operand1, il.Not(REGSZ_O(operand1), ReadILOperand(il, operand2, REGSZ_O(operand1)))));
break;
case ARM64_MOVK:
// zero the underling register slice
il.AddInstruction(ILSETREG_O(
operand1, il.And(REGSZ_O(operand1), ILREG_O(operand1),
il.Const(REGSZ_O(operand1), ~(0xffffULL << operand2.shiftValue)))));
// mov the immediate into it
il.AddInstruction(ILSETREG_O(
operand1, il.Or(REGSZ_O(operand1), ILREG_O(operand1),
il.Const(REGSZ_O(operand1), IMM_O(operand2) << operand2.shiftValue))));
break;
case ARM64_MOVZ:
il.AddInstruction(
ILSETREG_O(operand1, il.Const(REGSZ_O(operand1), IMM_O(operand2) << operand2.shiftValue)));
break;
case ARM64_MUL:
switch (instr.encoding)
{
case ENC_MUL_Z_P_ZZ_:
case ENC_MUL_Z_ZI_:
case ENC_MUL_Z_ZZ_:
case ENC_MUL_Z_ZZI_H:
case ENC_MUL_Z_ZZI_S:
case ENC_MUL_Z_ZZI_D:
case ENC_MUL_ASIMDSAME_ONLY:
case ENC_MUL_ASIMDELEM_R:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default:
il.AddInstruction(
ILSETREG_O(operand1, il.Mult(REGSZ_O(operand1), ILREG_O(operand2), ILREG_O(operand3))));
}
break;
case ARM64_MADD:
il.AddInstruction(ILSETREG_O(operand1,
ILADDREG_O(operand4, il.Mult(REGSZ_O(operand1), ILREG_O(operand2), ILREG_O(operand3)))));
break;
case ARM64_MRS:
{
uint32_t reg = operand2.sysreg;
const char* name = get_system_register_name((SystemReg)(reg));
if (strlen(name) == 0)
{
LogDebug("MSR Unknown system register %d @ 0x%" PRIx64
": S%d_%d_c%d_c%d_%d",
operand2.sysreg, addr, operand2.implspec[0], operand2.implspec[1], operand2.implspec[2],
operand2.implspec[3], operand2.implspec[4]);
}
if (IS_ZERO_REG(REG_O(operand1))) {
il.AddInstruction(
il.Intrinsic({}, ARM64_INTRIN_MRS, {il.Const(4, reg)}));
} else {
il.AddInstruction(
il.Intrinsic({RegisterOrFlag::Register(REG_O(operand1))}, ARM64_INTRIN_MRS, {il.Const(4, reg)}));
}
break;
}
case ARM64_MSUB:
il.AddInstruction(ILSETREG_O(
operand1, il.Sub(REGSZ_O(operand1), ILREG_O(operand4),
il.Mult(REGSZ_O(operand1), ILREG_O(operand2), ILREG_O(operand3)))));
break;
case ARM64_MNEG:
il.AddInstruction(ILSETREG_O(
operand1, il.Sub(REGSZ_O(operand1), il.Const(8, 0),
il.Mult(REGSZ_O(operand1), ILREG_O(operand2), ILREG_O(operand3)))));
break;
case ARM64_MSR:
{
uint32_t dst = operand1.sysreg;
const char* name = get_system_register_name((SystemReg)(dst));
if (strlen(name) == 0)
{
LogDebug("MSR Unknown system register %d @ 0x%" PRIx64
": S%d_%d_c%d_c%d_%d",
operand1.sysreg, addr, operand1.implspec[0], operand1.implspec[1], operand1.implspec[2],
operand1.implspec[3], operand1.implspec[4]);
}
switch (operand2.operandClass)
{
case IMM32:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_MSR, {il.Const(4, dst), il.Const(4, IMM_O(operand2))}));
break;
case REG:
il.AddInstruction( il.Intrinsic({}, ARM64_INTRIN_MSR, {il.Const(4, dst), ILREG_O(operand2)}));
break;
default:
LogError("unknown MSR operand class: %x\n", operand2.operandClass);
break;
}
break;
}
case ARM64_NEG:
switch (instr.encoding)
{
case ENC_NEG_ASISDMISC_R:
case ENC_NEG_ASIMDMISC_R:
case ENC_NEG_Z_P_Z_M:
case ENC_NEG_Z_P_Z_Z:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
case ARM64_NEGS:
il.AddInstruction(ILSETREG_O(
operand1, il.Neg(REGSZ_O(operand1), ReadILOperand(il, instr.operands[1], REGSZ_O(operand1)),
SETFLAGS)));
break;
case ARM64_NGC:
case ARM64_NGCS:
il.AddInstruction(ILSETREG_O(operand1, il.SubBorrow(REGSZ_O(operand1), il.Const(REGSZ_O(operand1), 0),
ReadILOperand(il, operand2, REGSZ_O(operand1)),
il.Not(0, il.Flag(IL_FLAG_C)), SETFLAGS)));
break;
case ARM64_NOP:
il.AddInstruction(il.Nop());
break;
#ifdef LIFT_PAC_AS_INTRINSIC
case ARM64_AUTDA:
case ARM64_AUTDB:
case ARM64_AUTIA:
case ARM64_AUTIB:
case ARM64_PACDA:
case ARM64_PACDB:
case ARM64_PACIA:
case ARM64_PACIB:
// <Xd> is address, <Xn> is modifier
il.AddInstruction(il.Intrinsic({RegisterOrFlag::Register(REG_O(operand1))},
operation_to_intrinsic(instr.operation), {ILREG_O(operand1), ILREG_O(operand2)}));
break;
case ARM64_PACGA:
// <Xd> is address, <Xn>, <Xm> are modifiers, keys
il.AddInstruction(il.Intrinsic({RegisterOrFlag::Register(REG_O(operand1))},
operation_to_intrinsic(instr.operation), {ILREG_O(operand2), ILREG_O(operand3)}));
break;
case ARM64_AUTIA1716:
case ARM64_AUTIB1716:
case ARM64_PACIA1716:
case ARM64_PACIB1716:
// x17 is address, x16 is modifier
il.AddInstruction(il.Intrinsic({RegisterOrFlag::Register(REG_X17)},
operation_to_intrinsic(instr.operation), {il.Register(8, REG_X17), il.Register(8, REG_X16)}));
break;
case ARM64_AUTDZA:
case ARM64_AUTDZB:
case ARM64_AUTIZA:
case ARM64_AUTIZB:
case ARM64_PACDZA:
case ARM64_PACDZB:
case ARM64_PACIZA:
case ARM64_PACIZB:
// <Xd> is address, modifier is 0
il.AddInstruction(il.Intrinsic(
{RegisterOrFlag::Register(REG_O(operand1))}, operation_to_intrinsic(instr.operation), {ILREG_O(operand1), il.Const(8, 0)}));
break;
case ARM64_XPACI:
case ARM64_XPACD:
// <Xd> is address
il.AddInstruction(il.Intrinsic(
{RegisterOrFlag::Register(REG_O(operand1))}, operation_to_intrinsic(instr.operation), {ILREG_O(operand1)}));
break;
case ARM64_AUTIAZ:
case ARM64_AUTIBZ:
case ARM64_PACIAZ:
case ARM64_PACIBZ:
// x30 is address, modifier is 0
il.AddInstruction(il.Intrinsic(
{RegisterOrFlag::Register(REG_X30)}, operation_to_intrinsic(instr.operation), {il.Register(8, REG_X30), il.Const(8, 0)}));
break;
case ARM64_XPACLRI:
// x30 is address
il.AddInstruction(il.Intrinsic(
{RegisterOrFlag::Register(REG_X30)}, operation_to_intrinsic(instr.operation), {il.Register(8, REG_X30)}));
break;
case ARM64_AUTIASP:
case ARM64_AUTIBSP:
case ARM64_PACIASP:
case ARM64_PACIBSP:
// x30 is address, sp is modifier
il.AddInstruction(il.Intrinsic({RegisterOrFlag::Register(REG_X30)},
operation_to_intrinsic(instr.operation), {il.Register(8, REG_X30), il.Register(8, REG_SP)}));
break;
#else
case ARM64_AUTDA:
case ARM64_AUTDB:
case ARM64_AUTIA:
case ARM64_AUTIB:
case ARM64_PACDA:
case ARM64_PACDB:
case ARM64_PACIA:
case ARM64_PACIB:
case ARM64_PACGA:
case ARM64_AUTIA1716:
case ARM64_AUTIB1716:
case ARM64_PACIA1716:
case ARM64_PACIB1716:
case ARM64_AUTDZA:
case ARM64_AUTDZB:
case ARM64_AUTIZA:
case ARM64_AUTIZB:
case ARM64_PACDZA:
case ARM64_PACDZB:
case ARM64_PACIZA:
case ARM64_PACIZB:
case ARM64_XPACI:
case ARM64_XPACD:
case ARM64_AUTIAZ:
case ARM64_AUTIBZ:
case ARM64_PACIAZ:
case ARM64_PACIBZ:
case ARM64_XPACLRI:
case ARM64_AUTIASP:
case ARM64_AUTIBSP:
case ARM64_PACIASP:
case ARM64_PACIBSP:
il.AddInstruction(il.Nop());
ApplyAttributeToLastInstruction(il, SrcInstructionUsesPointerAuth);
break;
#endif
case ARM64_PRFUM:
case ARM64_PRFM:
// TODO use the PRFM types when we have a better option than defining 18 different intrinsics to
// account for:
// - 3 types {PLD, PLI, PST}
// - 3 targets {L1, L2, L3}
// - 2 policies {KEEP, STM}
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_PRFM, {ReadILOperand(il, operand2, 8)}));
break;
case ARM64_ORN:
switch (instr.encoding)
{
case ENC_ORN_Z_ZI__ORR_Z_ZI_:
case ENC_ORN_P_P_PP_Z:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
il.AddInstruction(ILSETREG_O(
operand1, il.Or(REGSZ_O(operand1), ILREG_O(operand2),
il.Not(REGSZ_O(operand1), ReadILOperand(il, operand3, REGSZ_O(operand1))))));
break;
case ARM64_ORR:
case ARM64_ORRS:
switch (instr.encoding)
{
case ENC_ORR_32_LOG_IMM:
case ENC_ORR_32_LOG_SHIFT:
case ENC_ORR_64_LOG_IMM:
case ENC_ORR_64_LOG_SHIFT:
il.AddInstruction(
ILSETREG_O(operand1, il.Or(REGSZ_O(operand1), ILREG_O(operand2),
ReadILOperand(il, operand3, REGSZ_O(operand1)), SETFLAGS)));
break;
case ENC_ORR_ASIMDIMM_L_HL:
case ENC_ORR_ASIMDIMM_L_SL:
{
Register regs[16];
int n = unpack_vector(operand1, regs);
for (int i = 0; i < n; ++i)
il.AddInstruction(ILSETREG(regs[i], ILCONST_O(get_register_size(regs[i]), operand2)));
break;
}
case ENC_ORR_ASIMDSAME_ONLY:
// Let the neon intrinsic lifter take over.
break;
case ENC_ORR_P_P_PP_Z:
case ENC_ORR_Z_P_ZZ_:
case ENC_ORR_Z_ZI_:
case ENC_ORR_Z_ZZ_:
default:
ABORT_LIFT;
}
break;
case ARM64_PSB:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_PSBCSYNC, {}));
break;
case ARM64_RETAA:
case ARM64_RETAB:
SetPacAttr = true;
case ARM64_RET:
{
ExprId reg = (operand1.operandClass == REG) ? ILREG_O(operand1) : il.Register(8, REG_X30);
il.AddInstruction(il.Return(reg));
if (SetPacAttr)
ApplyAttributeToLastInstruction(il, SrcInstructionUsesPointerAuth);
}
break;
case ARM64_REVB: // SVE only
case ARM64_REVH:
case ARM64_REVW:
il.AddInstruction(il.Unimplemented());
break;
case ARM64_REV16:
switch (instr.encoding) {
case ENC_REV16_ASIMDMISC_R:
break;
default:
if (IS_SVE_O(operand1))
{
il.AddInstruction(il.Unimplemented());
break;
}
if (REGSZ_O(operand1) == 4)
{
// A 32-bit register holds two 16-bit lanes, so reversing the bytes within each lane is
// a full byte reversal rotated by one halfword
il.AddInstruction(ILSETREG_O(operand1,
il.RotateRight(4, il.ByteSwap(4, ILREG_O(operand2)), il.Const(1, 16))));
}
else
{
// A 64-bit register holds four 16-bit lanes; reversing the bytes within each lane has
// no native representation and is lifted as an intrinsic
il.AddInstruction(il.Intrinsic(
{RegisterOrFlag::Register(REG_O(operand1))}, ARM64_INTRIN_REV16, {ILREG_O(operand2)}));
}
}
break;
case ARM64_REV32:
switch (instr.encoding) {
case ENC_REV32_ASIMDMISC_R:
break;
default:
if (IS_SVE_O(operand1))
{
il.AddInstruction(il.Unimplemented());
break;
}
// REV32 reverses bytes within each 32-bit lane; a 64-bit register holds two such lanes,
// so it is a full byte reversal rotated by one word
il.AddInstruction(ILSETREG_O(operand1,
il.RotateRight(8, il.ByteSwap(8, ILREG_O(operand2)), il.Const(1, 32))));
}
break;
case ARM64_REV64:
case ARM64_REV:
switch (instr.encoding) {
case ENC_REV64_ASIMDMISC_R:
break;
default:
if (IS_SVE_O(operand1))
{
il.AddInstruction(il.Unimplemented());
break;
}
il.AddInstruction(ILSETREG_O(operand1, il.ByteSwap(REGSZ_O(operand2), ILREG_O(operand2))));
}
break;
case ARM64_RBIT:
switch (instr.encoding) {
case ENC_RBIT_ASIMDMISC_R:
break;
default:
il.AddInstruction(ILSETREG_O(operand1, il.ReverseBits(REGSZ_O(operand2), ILREG_O(operand2))));
}
break;
case ARM64_ROR:
il.AddInstruction(ILSETREG_O(operand1, il.RotateRight(REGSZ_O(operand2), ILREG_O(operand2),
ReadILOperand(il, operand3, REGSZ_O(operand2)))));
break;
case ARM64_SBC:
case ARM64_SBCS:
il.AddInstruction(ILSETREG_O(operand1, il.SubBorrow(REGSZ_O(operand1), ILREG_O(operand2),
ReadILOperand(il, operand3, REGSZ_O(operand1)),
il.Not(0, il.Flag(IL_FLAG_C)), SETFLAGS)));
break;
case ARM64_SBFIZ:
il.AddInstruction(ILSETREG_O(
operand1, il.ArithShiftRight(REGSZ_O(operand1),
il.ShiftLeft(REGSZ_O(operand1), ExtractBits(il, operand2, IMM_O(operand4), 0),
il.Const(1, (REGSZ_O(operand1) * 8) - IMM_O(operand4))),
il.Const(1, (REGSZ_O(operand1) * 8) - IMM_O(operand3) - IMM_O(operand4)))));
break;
case ARM64_SBFX:
il.AddInstruction(ILSETREG_O(
operand1, il.ArithShiftRight(REGSZ_O(operand1),
il.ShiftLeft(REGSZ_O(operand1),
ExtractBits(il, operand2, IMM_O(operand4), IMM_O(operand3)),
il.Const(1, (REGSZ_O(operand1) * 8) - IMM_O(operand4) - IMM_O(operand3))),
il.Const(1, (REGSZ_O(operand1) * 8) - IMM_O(operand4)))));
break;
case ARM64_SCVTF:
case ARM64_UCVTF:
{
bool zero_extend = false;
switch (instr.encoding)
{
// Scalar, float
case ENC_UCVTF_ASISDMISCFP16_R:
case ENC_UCVTF_ASISDMISC_R:
zero_extend = true;
case ENC_SCVTF_ASISDMISCFP16_R:
case ENC_SCVTF_ASISDMISC_R:
{
il.AddInstruction(ILSETREG_O(
operand1, il.IntToFloat(REGSZ_O(operand1),
zero_extend
? il.ZeroExtend(REGSZ_O(operand1), ILREG_O(operand2))
: il.SignExtend(REGSZ_O(operand1), ILREG_O(operand2)))));
break;
}
// Scalar, integer
case ENC_UCVTF_D32_FLOAT2INT:
case ENC_UCVTF_D64_FLOAT2INT:
case ENC_UCVTF_H32_FLOAT2INT:
case ENC_UCVTF_H64_FLOAT2INT:
case ENC_UCVTF_S32_FLOAT2INT:
case ENC_UCVTF_S64_FLOAT2INT:
zero_extend = true;
case ENC_SCVTF_D32_FLOAT2INT:
case ENC_SCVTF_D64_FLOAT2INT:
case ENC_SCVTF_H32_FLOAT2INT:
case ENC_SCVTF_H64_FLOAT2INT:
case ENC_SCVTF_S32_FLOAT2INT:
case ENC_SCVTF_S64_FLOAT2INT:
{
il.AddInstruction(ILSETREG_O(
operand1, il.IntToFloat(REGSZ_O(operand1),
zero_extend
? il.ZeroExtend(REGSZ_O(operand1), ILREG_O(operand2))
: il.SignExtend(REGSZ_O(operand1), ILREG_O(operand2)))));
break;
}
// Vector, single-precision and double-precision unsigned
case ENC_UCVTF_ASIMDMISC_R:
// Vector, half precision unsigned
case ENC_UCVTF_ASIMDMISCFP16_R:
zero_extend = true;
// Vector, single-precision and double-precision
case ENC_SCVTF_ASIMDMISC_R:
// Vector, half precision
case ENC_SCVTF_ASIMDMISCFP16_R:
{
if (preferIntrinsics())
return true;
Register cregs[2];
if (false && consolidate_vector(operand1, operand2, cregs))
il.AddInstruction(ILSETREG(cregs[0],
il.IntToFloat(REGSZ(cregs[0]),
zero_extend
? il.ZeroExtend(REGSZ(cregs[0]), ILREG(cregs[1]))
: il.SignExtend(REGSZ(cregs[0]), ILREG(cregs[1])))));
else
{
// SCVTF <Vd>.<T>, <Vn>.<T>
Register srcs[16], dsts[16];
int dst_n = unpack_vector(operand1, dsts);
int src_n = unpack_vector(operand2, srcs);
if ((dst_n != src_n) || dst_n == 0)
ABORT_LIFT;
int rsize = get_register_size(dsts[0]);
for (int i = 0; i < dst_n; ++i)
il.AddInstruction(ILSETREG(dsts[i], il.IntToFloat(rsize,
zero_extend
? il.ZeroExtend(rsize, ILREG(srcs[i]))
: il.SignExtend(rsize, ILREG(srcs[i])))));
}
break;
}
// Scalar, fixed-point (in SIMD&FP register)
case ENC_SCVTF_ASISDSHF_C:
case ENC_UCVTF_ASISDSHF_C:
// Scalar, fixed-point (in GP register) [will fail because no intrinsics]
case ENC_SCVTF_D32_FLOAT2FIX:
case ENC_SCVTF_D64_FLOAT2FIX:
case ENC_SCVTF_H32_FLOAT2FIX:
case ENC_SCVTF_H64_FLOAT2FIX:
case ENC_SCVTF_S32_FLOAT2FIX:
case ENC_SCVTF_S64_FLOAT2FIX:
case ENC_UCVTF_D32_FLOAT2FIX:
case ENC_UCVTF_D64_FLOAT2FIX:
case ENC_UCVTF_H32_FLOAT2FIX:
case ENC_UCVTF_H64_FLOAT2FIX:
case ENC_UCVTF_S32_FLOAT2FIX:
case ENC_UCVTF_S64_FLOAT2FIX:
// Vector, fixed-point
case ENC_SCVTF_ASIMDSHF_C:
// Lift to instrinsics (except there are none)
case ENC_UCVTF_ASIMDSHF_C:
// Lift to instrinsics
break;
// SVE: Vector, integer
case ENC_SCVTF_Z_P_Z_H2FP16:
case ENC_SCVTF_Z_P_Z_W2D:
case ENC_SCVTF_Z_P_Z_W2FP16:
case ENC_SCVTF_Z_P_Z_W2S:
case ENC_SCVTF_Z_P_Z_X2D:
case ENC_SCVTF_Z_P_Z_X2FP16:
case ENC_SCVTF_Z_P_Z_X2S:
case ENC_UCVTF_Z_P_Z_H2FP16:
case ENC_UCVTF_Z_P_Z_W2D:
case ENC_UCVTF_Z_P_Z_W2FP16:
case ENC_UCVTF_Z_P_Z_W2S:
case ENC_UCVTF_Z_P_Z_X2D:
case ENC_UCVTF_Z_P_Z_X2FP16:
case ENC_UCVTF_Z_P_Z_X2S:
ABORT_LIFT;
default:
break;
}
break;
}
case ARM64_SDIV:
il.AddInstruction(ILSETREG_O(
operand1, il.DivSigned(REGSZ_O(operand2), ILREG_O(operand2), ILREG_O(operand3))));
break;
case ARM64_SEV:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_SEV, {}));
break;
case ARM64_SEVL:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_SEVL, {}));
break;
case ARM64_SHL:
{
Register srcs[16], dsts[16];
int dst_n = unpack_vector(operand1, dsts);
int src_n = unpack_vector(operand2, srcs);
if ((dst_n != src_n) || dst_n == 0)
ABORT_LIFT;
int rsize = get_register_size(dsts[0]);
for (int i = 0; i < dst_n; ++i)
{
il.AddInstruction(il.SetRegister(rsize, dsts[i],
il.ShiftLeft(rsize, il.Register(rsize, srcs[i]), il.Const(1, IMM_O(operand3)))));
}
break;
}
case ARM64_SSHL:
{
Register srcs1[16], srcs2[16], dsts[16];
int dst_n = unpack_vector(operand1, dsts);
int src1_n = unpack_vector(operand2, srcs1);
int src2_n = unpack_vector(operand3, srcs2);
if ((dst_n != src1_n) || (src1_n != src2_n) || dst_n == 0)
ABORT_LIFT;
int rsize = get_register_size(dsts[0]);
for (int i = 0; i < dst_n; ++i)
{
il.AddInstruction(il.SetRegister(rsize, dsts[i],
il.ShiftLeft(rsize,
il.SignExtend(rsize, il.Register(rsize, srcs1[i])),
il.Register(1, srcs2[i]))));
}
break;
}
case ARM64_SSHR:
{
// Note: we don't lift SRSHR, because it requires rounding the shifted results
Register srcs[16], dsts[16];
int dst_n = unpack_vector(operand1, dsts);
int src_n = unpack_vector(operand2, srcs);
if ((dst_n != src_n) || dst_n == 0)
ABORT_LIFT;
int rsize = get_register_size(dsts[0]);
for (int i = 0; i < dst_n; ++i)
{
il.AddInstruction(il.SetRegister(rsize, dsts[i],
il.ArithShiftRight(rsize, il.Register(rsize, srcs[i]), il.Const(1, IMM_O(operand3)))));
}
break;
}
case ARM64_SSHLL:
case ARM64_SSHLL2:
case ARM64_SXTL:
case ARM64_SXTL2:
switch (instr.encoding)
{
case ENC_SSHLL_ASIMDSHF_L:
case ENC_SXTL_SSHLL_ASIMDSHF_L:
if (preferIntrinsics())
return true;
default:
break;
}
// SHLL{2} is the same as SHLL{2}, except the extension is signed or unsigned "without change of functionality"
// (The shift amount is the element size, but is still disassembled to the immediate value in the third operand)
case ARM64_SHLL:
case ARM64_SHLL2:
{
if (preferIntrinsics())
return true;
Register srcs[16], dsts[16];
int dst_n = unpack_vector(operand1, dsts);
int src_n = unpack_vector(operand2, srcs);
// We cannot check that the src and dst counts are the same here
// because the 2 variants use different count arrange specs, e.g.
// sxtl2 v0.2d, v1.4s
// (void) src_n;
int left_shift = 0;
if (instr.operation == ARM64_SSHLL || instr.operation == ARM64_SSHLL2 ||
instr.operation == ARM64_SHLL || instr.operation == ARM64_SHLL2)
left_shift = IMM_O(operand3);
int two_variant_offset = 0;
if (instr.operation == ARM64_SXTL2 || instr.operation == ARM64_SSHLL2 || instr.operation == ARM64_SHLL2)
two_variant_offset = src_n / 2;
int dst_size = get_register_size(dsts[0]);
int src_size = get_register_size(srcs[0]);
for (int i = 0; i < dst_n; ++i)
if (left_shift)
il.AddInstruction(il.SetRegister(dst_size, dsts[i],
il.ShiftLeft(dst_size,
il.SignExtend(dst_size,
il.Register(src_size, srcs[i + two_variant_offset])),
il.Const(1, left_shift))));
else
il.AddInstruction(il.SetRegister(dst_size, dsts[i],
il.SignExtend(dst_size,
il.Register(src_size, srcs[i + two_variant_offset]))));
break;
}
case ARM64_ST1:
case ARM64_ST2:
case ARM64_ST3:
case ARM64_ST4:
if (true || !preferIntrinsics()) // For now, forcibly disable intrinsics (they are incomplete, and this could help dataflow)
LoadStoreVector(il, false, instr.operands[0], instr.operands[1]);
break;
case ARM64_STP:
case ARM64_STNP:
LoadStoreOperandPair(il, false, instr.operands[0], instr.operands[1], instr.operands[2]);
break;
case ARM64_ST2G:
switch (operand2.operandClass)
{
case MEM_POST_IDX:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_ST2G, {ILREG_O(operand1), ILREG_O(operand2)}));
if (IMM_O(operand2) != 0)
il.AddInstruction(ILSETREG_O(operand2,
il.Add(REGSZ_O(operand2), ILREG_O(operand2), il.Const(REGSZ_O(operand2), IMM_O(operand2)))));
break;
case MEM_PRE_IDX:
if (IMM_O(operand2) != 0)
il.AddInstruction(ILSETREG_O(operand2,
il.Add(REGSZ_O(operand2), ILREG_O(operand2), il.Const(REGSZ_O(operand2), IMM_O(operand2)))));
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_ST2G, {ILREG_O(operand1), ILREG_O(operand2)}));
break;
default:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_ST2G,
{ILREG_O(operand1), GetILOperandEffectiveAddress(il, operand2, 8, operand2.operandClass, 0)}));
break;
}
break;
case ARM64_STG:
switch (operand2.operandClass)
{
case MEM_POST_IDX:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_STG, {ILREG_O(operand1), ILREG_O(operand2)}));
if (IMM_O(operand2) != 0)
il.AddInstruction(ILSETREG_O(operand2,
il.Add(REGSZ_O(operand2), ILREG_O(operand2), il.Const(REGSZ_O(operand2), IMM_O(operand2)))));
break;
case MEM_PRE_IDX:
if (IMM_O(operand2) != 0)
il.AddInstruction(ILSETREG_O(operand2,
il.Add(REGSZ_O(operand2), ILREG_O(operand2), il.Const(REGSZ_O(operand2), IMM_O(operand2)))));
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_STG, {ILREG_O(operand1), ILREG_O(operand2)}));
break;
default:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_STG,
{ILREG_O(operand1), GetILOperandEffectiveAddress(il, operand2, 8, operand2.operandClass, 0)}));
break;
}
break;
case ARM64_STGM:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_STGM, {ILREG_O(operand1), ILREG_O(operand2)}));
break;
case ARM64_STGP:
switch (operand3.operandClass)
{
case MEM_POST_IDX:
il.AddInstruction(
il.Intrinsic({}, ARM64_INTRIN_STGP, {ILREG_O(operand1), ILREG_O(operand2), ILREG_O(operand3)}));
if (IMM_O(operand3) != 0)
il.AddInstruction(ILSETREG_O(operand3,
il.Add(REGSZ_O(operand3), ILREG_O(operand3), il.Const(REGSZ_O(operand3), IMM_O(operand3)))));
break;
case MEM_PRE_IDX:
if (IMM_O(operand3) != 0)
il.AddInstruction(ILSETREG_O(operand3,
il.Add(REGSZ_O(operand3), ILREG_O(operand3), il.Const(REGSZ_O(operand3), IMM_O(operand3)))));
il.AddInstruction(
il.Intrinsic({}, ARM64_INTRIN_STGP, {ILREG_O(operand1), ILREG_O(operand2), ILREG_O(operand3)}));
break;
default:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_STGP,
{ILREG_O(operand1), ILREG_O(operand2),
GetILOperandEffectiveAddress(il, operand3, 8, operand3.operandClass, 0)}));
break;
}
break;
case ARM64_STZ2G:
switch (operand2.operandClass)
{
case MEM_POST_IDX:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_STZ2G, {ILREG_O(operand1), ILREG_O(operand2)}));
if (IMM_O(operand2) != 0)
il.AddInstruction(ILSETREG_O(operand2,
il.Add(REGSZ_O(operand2), ILREG_O(operand2), il.Const(REGSZ_O(operand2), IMM_O(operand2)))));
break;
case MEM_PRE_IDX:
if (IMM_O(operand2) != 0)
il.AddInstruction(ILSETREG_O(operand2,
il.Add(REGSZ_O(operand2), ILREG_O(operand2), il.Const(REGSZ_O(operand2), IMM_O(operand2)))));
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_STZ2G, {ILREG_O(operand1), ILREG_O(operand2)}));
break;
default:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_STZ2G,
{ILREG_O(operand1), GetILOperandEffectiveAddress(il, operand2, 8, operand2.operandClass, 0)}));
break;
}
break;
case ARM64_STZG:
switch (operand2.operandClass)
{
case MEM_POST_IDX:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_STZG, {ILREG_O(operand1), ILREG_O(operand2)}));
if (IMM_O(operand2) != 0)
il.AddInstruction(ILSETREG_O(operand2,
il.Add(REGSZ_O(operand2), ILREG_O(operand2), il.Const(REGSZ_O(operand2), IMM_O(operand2)))));
break;
case MEM_PRE_IDX:
if (IMM_O(operand2) != 0)
il.AddInstruction(ILSETREG_O(operand2,
il.Add(REGSZ_O(operand2), ILREG_O(operand2), il.Const(REGSZ_O(operand2), IMM_O(operand2)))));
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_STZG, {ILREG_O(operand1), ILREG_O(operand2)}));
break;
default:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_STZG,
{ILREG_O(operand1), GetILOperandEffectiveAddress(il, operand2, 8, operand2.operandClass, 0)}));
break;
}
break;
case ARM64_STZGM:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_STZGM, {ILREG_O(operand1), ILREG_O(operand2)}));
break;
case ARM64_STR:
switch (instr.encoding)
{
case ENC_STR_P_BI_:
case ENC_STR_Z_BI_:
case ENC_STR_ZA_RI_:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
case ARM64_STLR:
case ARM64_STUR:
case ARM64_STLUR:
LoadStoreOperand(il, false, instr.operands[0], instr.operands[1], 0);
break;
case ARM64_STRB:
case ARM64_STLRB:
case ARM64_STURB:
case ARM64_STLURB:
LoadStoreOperandSize(il, false, false, 1, instr.operands[0], instr.operands[1]);
break;
case ARM64_STRH:
case ARM64_STLRH:
case ARM64_STURH:
case ARM64_STLURH:
LoadStoreOperandSize(il, false, false, 2, instr.operands[0], instr.operands[1]);
break;
case ARM64_SUB:
switch (instr.encoding)
{
case ENC_SUB_Z_P_ZZ_:
case ENC_SUB_Z_ZI_:
case ENC_SUB_Z_ZZ_:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
case ARM64_SUBS:
il.AddInstruction(ILSETREG_O(
operand1, il.Sub(REGSZ_O(operand1), ILREG_O(operand2),
ReadILOperand(il, instr.operands[2], REGSZ_O(operand1)), SETFLAGS)));
break;
case ARM64_SUBG:
il.AddInstruction(il.Intrinsic({RegisterOrFlag::Register(REG_O(operand1))}, ARM64_INTRIN_SUBG,
{ILREG_O(operand2), il.Const(REGSZ_O(operand2), IMM_O(operand3)), il.Const(1, IMM_O(operand4))}));
break;
case ARM64_SUBP:
il.AddInstruction(il.Intrinsic(
{RegisterOrFlag::Register(REG_O(operand1))}, ARM64_INTRIN_SUBP, {ILREG_O(operand2), ILREG_O(operand3)}));
break;
case ARM64_SUBPS:
il.AddInstruction(il.Intrinsic(
{RegisterOrFlag::Register(REG_O(operand1)), RegisterOrFlag::Flag(IL_FLAG_N),
RegisterOrFlag::Flag(IL_FLAG_Z), RegisterOrFlag::Flag(IL_FLAG_C), RegisterOrFlag::Flag(IL_FLAG_V)},
ARM64_INTRIN_SUBPS, {ILREG_O(operand2), ILREG_O(operand3)}));
break;
case ARM64_SVC:
case ARM64_HVC:
case ARM64_SMC:
{
/* b31,b30==xx of fake register mark transition to ELxx */
uint32_t el_mark = 0;
if (instr.operation == ARM64_SVC)
el_mark = 0x40000000;
else if (instr.operation == ARM64_HVC)
el_mark = 0x80000000;
else if (instr.operation == ARM64_SMC)
el_mark = 0xC0000000;
/* b15..b0 of fake register still holds syscall number */
il.AddInstruction(
il.SetRegister(4, FAKEREG_SYSCALL_INFO, il.Const(4, el_mark | IMM_O(operand1))));
il.AddInstruction(il.SystemCall());
break;
}
case ARM64_SMOV:
{
Register srcs[16], dsts[16];
int dst_n = unpack_vector(operand1, dsts);
int src_n = unpack_vector(operand2, srcs);
if ((dst_n != src_n) || dst_n == 0)
ABORT_LIFT;
for (int i = 0; i < dst_n; ++i)
il.AddInstruction(ILSETREG(dsts[i], il.SignExtend(REGSZ(dsts[i]), ILREG(srcs[i]))));
break;
}
case ARM64_SWP: /* word (4) or doubleword (8) */
case ARM64_SWPA:
case ARM64_SWPL:
case ARM64_SWPAL:
LoadStoreOperand(il, true, operand2, operand3, 0);
LoadStoreOperand(il, false, operand1, operand3, 0);
break;
case ARM64_SWPB: /* byte (1) */
case ARM64_SWPAB:
case ARM64_SWPLB:
case ARM64_SWPALB:
LoadStoreOperand(il, true, operand2, operand3, 1);
il.AddInstruction(il.Store(1, ILREG_O(operand3), il.LowPart(1, ILREG_O(operand1))));
break;
case ARM64_SWPH: /* half-word (2) */
case ARM64_SWPAH:
case ARM64_SWPLH:
case ARM64_SWPALH:
LoadStoreOperand(il, true, operand2, operand3, 2);
il.AddInstruction(il.Store(2, ILREG_O(operand3), il.LowPart(2, ILREG_O(operand1))));
break;
case ARM64_SXTB:
switch (instr.encoding)
{
case ENC_SXTB_Z_P_Z_M:
case ENC_SXTB_Z_P_Z_Z:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
il.AddInstruction(
ILSETREG_O(operand1, ExtractRegister(il, operand2, 0, 1, true, REGSZ_O(operand1))));
break;
case ARM64_SXTH:
switch (instr.encoding)
{
case ENC_SXTH_Z_P_Z_M:
case ENC_SXTH_Z_P_Z_Z:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
il.AddInstruction(
ILSETREG_O(operand1, ExtractRegister(il, operand2, 0, 2, true, REGSZ_O(operand1))));
break;
case ARM64_SXTW:
switch (instr.encoding)
{
case ENC_SXTW_Z_P_Z_M:
case ENC_SXTW_Z_P_Z_Z:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
il.AddInstruction(
ILSETREG_O(operand1, ExtractRegister(il, operand2, 0, 4, true, REGSZ_O(operand1))));
break;
case ARM64_TBNZ:
ConditionalJump(arch, il,
il.CompareNotEqual(REGSZ_O(operand1), ExtractBit(il, operand1, IMM_O(operand2)),
il.Const(REGSZ_O(operand1), 0)),
addrSize, IMM_O(operand3), addr + 4);
return false;
case ARM64_TBZ:
ConditionalJump(arch, il,
il.CompareEqual(REGSZ_O(operand1), ExtractBit(il, operand1, IMM_O(operand2)),
il.Const(REGSZ_O(operand1), 0)),
addrSize, IMM_O(operand3), addr + 4);
return false;
case ARM64_TST:
il.AddInstruction(il.And(REGSZ_O(operand1), ILREG_O(operand1),
ReadILOperand(il, operand2, REGSZ_O(operand1)), SETFLAGS));
break;
case ARM64_TLBI:
if (operand2.operandClass == REG)
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_TLBI_REG, {il.Const(4, operand1.immediate), ReadILOperand(il, operand2, 8)}));
else
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_TLBI, {il.Const(4, operand1.immediate)}));
break;
case ARM64_AT:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_AT, {il.Const(4, operand1.immediate), ReadILOperand(il, operand2, 8)}));
break;
case ARM64_UMADDL:
il.AddInstruction(ILSETREG_O(operand1,
il.Add(REGSZ_O(operand1), ILREG_O(operand4),
il.MultDoublePrecUnsigned(REGSZ_O(operand2), ILREG_O(operand2), ILREG_O(operand3)))));
break;
case ARM64_UMULL:
il.AddInstruction(ILSETREG_O(operand1,
il.MultDoublePrecUnsigned(REGSZ_O(operand2), ILREG_O(operand2), ILREG_O(operand3))));
break;
case ARM64_UMSUBL:
il.AddInstruction(ILSETREG_O(operand1,
il.Sub(REGSZ_O(operand1), ILREG_O(operand4),
il.MultDoublePrecUnsigned(REGSZ_O(operand2), ILREG_O(operand2), ILREG_O(operand3)))));
break;
case ARM64_UMNEGL:
il.AddInstruction(ILSETREG_O(operand1,
il.Sub(REGSZ_O(operand1), il.Const(8, 0),
il.MultDoublePrecUnsigned(REGSZ_O(operand2), ILREG_O(operand2), ILREG_O(operand3)))));
break;
case ARM64_UXTL:
case ARM64_UXTL2:
case ARM64_USHLL:
case ARM64_USHLL2:
{
switch (instr.encoding)
{
case ENC_USHLL_ASIMDSHF_L:
case ENC_UXTL_USHLL_ASIMDSHF_L:
if (preferIntrinsics())
return true;
default:
break;
}
// if ((instr.encoding == ENC_USHLL_ASIMDSHF_L || instr.encoding == ENC_UXTL_USHLL_ASIMDSHF_L) && preferIntrinsics())
// {
// return true;
// }
Register srcs[16], dsts[16];
int dst_n = unpack_vector(operand1, dsts);
int src_n = unpack_vector(operand2, srcs);
// We cannot check that the src and dst counts are the same here
// because the 2 variants use different count arrange specs, e.g.
// uxtl2 v0.2d, v1.4s
(void) src_n;
int left_shift = 0;
if (instr.operation == ARM64_USHLL || instr.operation == ARM64_USHLL2)
left_shift = IMM_O(operand3);
int two_variant_offset = 0;
if (instr.operation == ARM64_UXTL2 || instr.operation == ARM64_USHLL2)
two_variant_offset = src_n / 2;
int dst_size = get_register_size(dsts[0]);
int src_size = get_register_size(srcs[0]);
for (int i = 0; i < dst_n; ++i)
if (left_shift)
il.AddInstruction(il.SetRegister(dst_size, dsts[i],
il.ShiftLeft(dst_size,
il.ZeroExtend(dst_size,
il.Register(src_size, srcs[i + two_variant_offset])),
il.Const(1, left_shift))));
else
il.AddInstruction(il.SetRegister(dst_size, dsts[i],
il.ZeroExtend(dst_size,
il.Register(src_size, srcs[i + two_variant_offset]))));
break;
}
case ARM64_SMADDL:
il.AddInstruction(ILSETREG_O(operand1,
il.Add(REGSZ_O(operand1), ILREG_O(operand4),
il.MultDoublePrecSigned(REGSZ_O(operand2), ILREG_O(operand2), ILREG_O(operand3)))));
break;
case ARM64_USHL:
{
switch (instr.encoding)
{
case ENC_USHL_ASIMDSAME_ONLY:
if (preferIntrinsics())
return true;
case ENC_USHL_ASISDSAME_ONLY:
default:
{
Register srcs1[16], srcs2[16], dsts[16];
int dst_n = unpack_vector(operand1, dsts);
int src1_n = unpack_vector(operand2, srcs1);
int src2_n = unpack_vector(operand3, srcs2);
if ((dst_n != src1_n) || (src1_n != src2_n) || dst_n == 0)
ABORT_LIFT;
int rsize = get_register_size(dsts[0]);
for (int i = 0; i < dst_n; ++i)
{
il.AddInstruction(il.SetRegister(rsize, dsts[i],
il.ShiftLeft(rsize,
il.Register(rsize, srcs1[i]),
il.Register(1, srcs2[i]))));
}
}
}
break;
}
case ARM64_USHR:
{
// Note: we don't lift URSHR, because it requires rounding the shifted results
if (preferIntrinsics())
return true;
Register srcs[16], dsts[16];
int dst_n = unpack_vector(operand1, dsts);
int src_n = unpack_vector(operand2, srcs);
if ((dst_n != src_n) || dst_n == 0)
ABORT_LIFT;
int rsize = get_register_size(dsts[0]);
for (int i = 0; i < dst_n; ++i)
{
il.AddInstruction(il.SetRegister(rsize, dsts[i],
il.LogicalShiftRight(rsize, il.Register(rsize, srcs[i]), il.Const(1, IMM_O(operand3)))));
}
break;
}
case ARM64_SMULL:
il.AddInstruction(ILSETREG_O(operand1,
il.MultDoublePrecSigned(REGSZ_O(operand2), ILREG_O(operand2), ILREG_O(operand3))));
break;
case ARM64_SMSUBL:
il.AddInstruction(ILSETREG_O(operand1,
il.Sub(REGSZ_O(operand1), ILREG_O(operand4),
il.MultDoublePrecSigned(REGSZ_O(operand2), ILREG_O(operand2), ILREG_O(operand3)))));
break;
case ARM64_SMNEGL:
il.AddInstruction(ILSETREG_O(operand1,
il.Neg(REGSZ_O(operand1),
il.MultDoublePrecSigned(REGSZ_O(operand2), ILREG_O(operand2), ILREG_O(operand3)))));
break;
case ARM64_UMULH:
switch (instr.encoding)
{
case ENC_UMULH_Z_ZZ_:
case ENC_UMULH_Z_P_ZZ_:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default:
break;
}
il.AddInstruction(ILSETREG_O(operand1,
il.LowPart(8,
il.LogicalShiftRight(16,
il.MultDoublePrecUnsigned(REGSZ_O(operand1), ILREG_O(operand2), ILREG_O(operand3)),
il.Const(1, 64)))));
break;
case ARM64_SMULH:
switch (instr.encoding)
{
case ENC_SMULH_Z_ZZ_:
case ENC_SMULH_Z_P_ZZ_:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
il.AddInstruction(ILSETREG_O(operand1,
il.SignExtend(8,
il.LowPart(8,
il.LogicalShiftRight(16,
il.MultDoublePrecSigned(REGSZ_O(operand1), ILREG_O(operand2), ILREG_O(operand3)),
il.Const(1, 64))))));
break;
case ARM64_SMAX:
{
ExprId op2 = ILREG_O(operand2);
ExprId op3;
switch (instr.encoding)
{
case ENC_SMAX_32_MINMAX_IMM:
case ENC_SMAX_64_MINMAX_IMM:
op3 = il.Const(REGSZ_O(operand2), operand3.immediate);
break;
case ENC_SMAX_32_DP_2SRC:
case ENC_SMAX_64_DP_2SRC:
op3 = ILREG_O(operand3);
break;
default:
il.AddInstruction(il.Unimplemented());
return true;
}
GenIfElse(il, il.CompareSignedGreaterThan(REGSZ_O(operand2), op2, op3), ILSETREG_O(operand1, op2),
ILSETREG_O(operand1, op3));
break;
}
case ARM64_SMIN:
{
ExprId op2 = ILREG_O(operand2);
ExprId op3;
switch (instr.encoding)
{
case ENC_SMIN_32_MINMAX_IMM:
case ENC_SMIN_64_MINMAX_IMM:
op3 = il.Const(REGSZ_O(operand2), operand3.immediate);
break;
case ENC_SMIN_32_DP_2SRC:
case ENC_SMIN_64_DP_2SRC:
op3 = ILREG_O(operand3);
break;
default:
il.AddInstruction(il.Unimplemented());
return true;
}
GenIfElse(il, il.CompareSignedLessThan(REGSZ_O(operand2), op2, op3), ILSETREG_O(operand1, op2),
ILSETREG_O(operand1, op3));
break;
}
case ARM64_UDIV:
switch (instr.encoding)
{
case ENC_UDIV_Z_P_ZZ_:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
il.AddInstruction(ILSETREG_O(
operand1, il.DivUnsigned(REGSZ_O(operand2), ILREG_O(operand2), ILREG_O(operand3))));
break;
case ARM64_UMAX:
{
ExprId op2 = ILREG_O(operand2);
ExprId op3;
switch (instr.encoding)
{
case ENC_UMAX_32U_MINMAX_IMM:
case ENC_UMAX_64U_MINMAX_IMM:
op3 = il.Const(REGSZ_O(operand2), operand3.immediate);
break;
case ENC_UMAX_32_DP_2SRC:
case ENC_UMAX_64_DP_2SRC:
op3 = ILREG_O(operand3);
break;
default:
il.AddInstruction(il.Unimplemented());
return true;
}
GenIfElse(il, il.CompareUnsignedGreaterThan(REGSZ_O(operand2), op2, op3), ILSETREG_O(operand1, op2),
ILSETREG_O(operand1, op3));
break;
}
case ARM64_UMIN:
{
ExprId op2 = ILREG_O(operand2);
ExprId op3;
switch (instr.encoding)
{
case ENC_UMIN_32U_MINMAX_IMM:
case ENC_UMIN_64U_MINMAX_IMM:
op3 = il.Const(REGSZ_O(operand2), operand3.immediate);
break;
case ENC_UMIN_32_DP_2SRC:
case ENC_UMIN_64_DP_2SRC:
op3 = ILREG_O(operand3);
break;
default:
il.AddInstruction(il.Unimplemented());
return true;
}
GenIfElse(il, il.CompareUnsignedLessThan(REGSZ_O(operand2), op2, op3), ILSETREG_O(operand1, op2),
ILSETREG_O(operand1, op3));
break;
}
case ARM64_UBFIZ:
il.AddInstruction(
ILSETREG_O(operand1, il.ShiftLeft(REGSZ_O(operand2),
il.And(REGSZ_O(operand2),
ILREG_O(operand2),
il.Const(REGSZ_O(operand2), (1LL << IMM_O(operand4)) - 1)),
il.Const(1, IMM_O(operand3)))));
break;
case ARM64_UBFX:
{
// ubfx <dst>, <src>, <src_lsb>, <src_len>
int src_lsb = IMM_O(operand3);
int src_len = IMM_O(operand4);
if (src_lsb == 0 && (src_len == 8 || src_len == 16 || src_len == 32 || src_len == 64))
{
il.AddInstruction(ILSETREG_O(operand1, il.LowPart(src_len / 8, ILREG_O(operand2))));
}
else
{
il.AddInstruction(ILSETREG_O(operand1, il.And(REGSZ_O(operand2),
il.LogicalShiftRight(REGSZ_O(operand2),
ILREG_O(operand2),
il.Const(1, IMM_O(operand3))),
il.Const(REGSZ_O(operand2), (1LL << IMM_O(operand4)) - 1))));
}
break;
}
case ARM64_UXTB:
switch (instr.encoding)
{
case ENC_UXTB_Z_P_Z_M:
case ENC_UXTB_Z_P_Z_Z:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
il.AddInstruction(
ILSETREG_O(operand1, ExtractRegister(il, operand2, 0, 1, false, REGSZ_O(operand1))));
break;
case ARM64_UXTH:
switch (instr.encoding)
{
case ENC_UXTH_Z_P_Z_M:
case ENC_UXTH_Z_P_Z_Z:
if (!preferIntrinsics())
il.AddInstruction(il.Unimplemented());
return true;
default: break;
}
il.AddInstruction(
ILSETREG_O(operand1, ExtractRegister(il, operand2, 0, 2, false, REGSZ_O(operand1))));
break;
case ARM64_WFE:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_WFE, {}));
break;
case ARM64_WFI:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_WFI, {}));
break;
case ARM64_BRK:
il.AddInstruction(
il.Trap(IMM_O(operand1))); // FIXME Breakpoint may need a parameter (IMM_O(operand1)));
return false;
case ARM64_DGH:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_HINT_DGH, {}));
break;
case ARM64_TSB:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_HINT_TSB, {}));
break;
case ARM64_CSDB:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_HINT_CSDB, {}));
break;
case ARM64_HINT:
if ((IMM_O(operand1) & ~0b110) == 0b100000)
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_HINT_BTI, {}));
break;
case ARM64_HLT:
il.AddInstruction(il.Trap(IMM_O(operand1)));
return false;
case ARM64_UDF:
il.AddInstruction(il.Trap(IMM_O(operand1)));
return false;
case ARM64_YIELD:
il.AddInstruction(il.Intrinsic({}, ARM64_INTRIN_YIELD, {}));
break;
default:
break;
}
if (il.GetInstructionCount() > n_instrs_before)
return true;
NeonGetLowLevelILForInstruction(arch, addr, il, instr, addrSize);
if (il.GetInstructionCount() > n_instrs_before)
return true;
il.AddInstruction(il.Unimplemented());
return true;
}
|