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
|
{
Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
Contains the abstract assembler implementation for the i386
* Portions of this code was inspired by the NASM sources
The Netwide Assembler is Copyright (c) 1996 Simon Tatham and
Julian Hall. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
****************************************************************************
}
unit aasmcpu;
{$i fpcdefs.inc}
interface
uses
globtype,verbose,
cpubase,
cgbase,cgutils,
symtype,
aasmbase,aasmtai,aasmdata,aasmsym,
ogbase;
const
{ "mov reg,reg" source operand number }
O_MOV_SOURCE = 0;
{ "mov reg,reg" destination operand number }
O_MOV_DEST = 1;
{ Operand types }
OT_NONE = $00000000;
{ Bits 0..7: sizes }
OT_BITS8 = $00000001;
OT_BITS16 = $00000002;
OT_BITS32 = $00000004;
OT_BITS64 = $00000008; { x86_64 and FPU }
OT_BITS128 = $10000000; { 16 byte SSE }
OT_BITS256 = $20000000; { 32 byte AVX }
OT_BITS80 = $00000010; { FPU only }
OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
OT_NEAR = $00000040;
OT_SHORT = $00000080;
{ TODO: FAR/NEAR/SHORT are sizes too, they should be included into size mask,
but this requires adjusting the opcode table }
OT_SIZE_MASK = $3000001F; { all the size attributes }
OT_NON_SIZE = longint(not OT_SIZE_MASK);
{ Bits 8..11: modifiers }
OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
OT_TO = $00000200; { reverse effect in FADD, FSUB &c }
OT_COLON = $00000400; { operand is followed by a colon }
OT_MODIFIER_MASK = $00000F00;
{ Bits 12..15: type of operand }
OT_REGISTER = $00001000;
OT_IMMEDIATE = $00002000;
OT_MEMORY = $0000C000; { always includes 'OT_REGMEM' bit as well }
OT_REGMEM = $00008000; { for r/m, ie EA, operands }
OT_TYPE_MASK = OT_REGISTER or OT_IMMEDIATE or OT_MEMORY or OT_REGMEM;
OT_REGNORM = OT_REGISTER or OT_REGMEM; { 'normal' reg, qualifies as EA }
{ Bits 20..22, 24..26: register classes
otf_* consts are not used alone, only to build other constants. }
otf_reg_cdt = $00100000;
otf_reg_gpr = $00200000;
otf_reg_sreg = $00400000;
otf_reg_fpu = $01000000;
otf_reg_mmx = $02000000;
otf_reg_xmm = $04000000;
otf_reg_ymm = $08000000;
{ Bits 16..19: subclasses, meaning depends on classes field }
otf_sub0 = $00010000;
otf_sub1 = $00020000;
otf_sub2 = $00040000;
otf_sub3 = $00080000;
OT_REG_SMASK = otf_sub0 or otf_sub1 or otf_sub2 or otf_sub3;
{ register class 0: CRx, DRx and TRx }
{$ifdef x86_64}
OT_REG_CDT = OT_REGISTER or otf_reg_cdt or OT_BITS64;
{$else x86_64}
OT_REG_CDT = OT_REGISTER or otf_reg_cdt or OT_BITS32;
{$endif x86_64}
OT_REG_CREG = OT_REG_CDT or otf_sub0; { CRn }
OT_REG_DREG = OT_REG_CDT or otf_sub1; { DRn }
OT_REG_TREG = OT_REG_CDT or otf_sub2; { TRn }
OT_REG_CR4 = OT_REG_CDT or otf_sub3; { CR4 (Pentium only) }
{ register class 1: general-purpose registers }
OT_REG_GPR = OT_REGNORM or otf_reg_gpr;
OT_RM_GPR = OT_REGMEM or otf_reg_gpr;
OT_REG8 = OT_REG_GPR or OT_BITS8; { 8-bit GPR }
OT_REG16 = OT_REG_GPR or OT_BITS16;
OT_REG32 = OT_REG_GPR or OT_BITS32;
OT_REG64 = OT_REG_GPR or OT_BITS64;
{ GPR subclass 0: accumulator: AL, AX, EAX or RAX }
OT_REG_ACCUM = OT_REG_GPR or otf_sub0;
OT_REG_AL = OT_REG_ACCUM or OT_BITS8;
OT_REG_AX = OT_REG_ACCUM or OT_BITS16;
OT_REG_EAX = OT_REG_ACCUM or OT_BITS32;
{$ifdef x86_64}
OT_REG_RAX = OT_REG_ACCUM or OT_BITS64;
{$endif x86_64}
{ GPR subclass 1: counter: CL, CX, ECX or RCX }
OT_REG_COUNT = OT_REG_GPR or otf_sub1;
OT_REG_CL = OT_REG_COUNT or OT_BITS8;
OT_REG_CX = OT_REG_COUNT or OT_BITS16;
OT_REG_ECX = OT_REG_COUNT or OT_BITS32;
{$ifdef x86_64}
OT_REG_RCX = OT_REG_COUNT or OT_BITS64;
{$endif x86_64}
{ GPR subclass 2: data register: DL, DX, EDX or RDX }
OT_REG_DX = OT_REG_GPR or otf_sub2 or OT_BITS16;
OT_REG_EDX = OT_REG_GPR or otf_sub2 or OT_BITS32;
{ register class 2: Segment registers }
OT_REG_SREG = OT_REGISTER or otf_reg_sreg or OT_BITS16;
OT_REG_CS = OT_REG_SREG or otf_sub0; { CS }
OT_REG_DESS = OT_REG_SREG or otf_sub1; { DS, ES, SS (non-CS 86 registers) }
OT_REG_FSGS = OT_REG_SREG or otf_sub2; { FS, GS (386 extended registers) }
{ register class 3: FPU registers }
OT_FPUREG = OT_REGISTER or otf_reg_fpu;
OT_FPU0 = OT_FPUREG or otf_sub0; { FPU stack register zero }
{ register class 4: MMX (both reg and r/m) }
OT_MMXREG = OT_REGNORM or otf_reg_mmx;
OT_MMXRM = OT_REGMEM or otf_reg_mmx;
{ register class 5: XMM (both reg and r/m) }
OT_XMMREG = OT_REGNORM or otf_reg_xmm;
OT_XMMRM = OT_REGMEM or otf_reg_xmm;
{ register class 5: XMM (both reg and r/m) }
OT_YMMREG = OT_REGNORM or otf_reg_ymm;
OT_YMMRM = OT_REGMEM or otf_reg_ymm;
{ Memory operands }
OT_MEM8 = OT_MEMORY or OT_BITS8;
OT_MEM16 = OT_MEMORY or OT_BITS16;
OT_MEM32 = OT_MEMORY or OT_BITS32;
OT_MEM64 = OT_MEMORY or OT_BITS64;
OT_MEM128 = OT_MEMORY or OT_BITS128;
OT_MEM256 = OT_MEMORY or OT_BITS256;
OT_MEM80 = OT_MEMORY or OT_BITS80;
OT_MEM_OFFS = OT_MEMORY or otf_sub0; { special type of EA }
{ simple [address] offset }
{ Matches any type of r/m operand }
OT_MEMORY_ANY = OT_MEMORY or OT_RM_GPR or OT_XMMRM or OT_MMXRM or OT_YMMRM;
{ Immediate operands }
OT_IMM8 = OT_IMMEDIATE or OT_BITS8;
OT_IMM16 = OT_IMMEDIATE or OT_BITS16;
OT_IMM32 = OT_IMMEDIATE or OT_BITS32;
OT_IMM64 = OT_IMMEDIATE or OT_BITS64;
OT_ONENESS = otf_sub0; { special type of immediate operand }
OT_UNITY = OT_IMMEDIATE or OT_ONENESS; { for shift/rotate instructions }
{ Size of the instruction table converted by nasmconv.pas }
{$ifdef x86_64}
instabentries = {$i x8664nop.inc}
{$else x86_64}
instabentries = {$i i386nop.inc}
{$endif x86_64}
maxinfolen = 8;
MaxInsChanges = 3; { Max things a instruction can change }
type
{ What an instruction can change. Needed for optimizer and spilling code.
Note: The order of this enumeration is should not be changed! }
TInsChange = (Ch_None,
{Read from a register}
Ch_REAX, Ch_RECX, Ch_REDX, Ch_REBX, Ch_RESP, Ch_REBP, Ch_RESI, Ch_REDI,
{write from a register}
Ch_WEAX, Ch_WECX, Ch_WEDX, Ch_WEBX, Ch_WESP, Ch_WEBP, Ch_WESI, Ch_WEDI,
{read and write from/to a register}
Ch_RWEAX, Ch_RWECX, Ch_RWEDX, Ch_RWEBX, Ch_RWESP, Ch_RWEBP, Ch_RWESI, Ch_RWEDI,
{modify the contents of a register with the purpose of using
this changed content afterwards (add/sub/..., but e.g. not rep
or movsd)}
Ch_MEAX, Ch_MECX, Ch_MEDX, Ch_MEBX, Ch_MESP, Ch_MEBP, Ch_MESI, Ch_MEDI,
Ch_CDirFlag {clear direction flag}, Ch_SDirFlag {set dir flag},
Ch_RFlags, Ch_WFlags, Ch_RWFlags, Ch_FPU,
Ch_Rop1, Ch_Wop1, Ch_RWop1,Ch_Mop1,
Ch_Rop2, Ch_Wop2, Ch_RWop2,Ch_Mop2,
Ch_Rop3, Ch_WOp3, Ch_RWOp3,Ch_Mop3,
Ch_WMemEDI,
Ch_All,
{ x86_64 registers }
Ch_RRAX, Ch_RRCX, Ch_RRDX, Ch_RRBX, Ch_RRSP, Ch_RRBP, Ch_RRSI, Ch_RRDI,
Ch_WRAX, Ch_WRCX, Ch_WRDX, Ch_WRBX, Ch_WRSP, Ch_WRBP, Ch_WRSI, Ch_WRDI,
Ch_RWRAX, Ch_RWRCX, Ch_RWRDX, Ch_RWRBX, Ch_RWRSP, Ch_RWRBP, Ch_RWRSI, Ch_RWRDI,
Ch_MRAX, Ch_MRCX, Ch_MRDX, Ch_MRBX, Ch_MRSP, Ch_MRBP, Ch_MRSI, Ch_MRDI
);
TInsProp = packed record
Ch : Array[1..MaxInsChanges] of TInsChange;
end;
TMemRefSizeInfo = (msiUnkown, msiMultiple, msiMemRegSize,
msiMem8, msiMem16, msiMem32, msiMem64, msiMem128, msiMem256);
TConstSizeInfo = (csiUnkown, csiMultiple, csiNoSize, csiMem8, csiMem16, csiMem32, csiMem64);
TInsTabMemRefSizeInfoRec = record
MemRefSize : TMemRefSizeInfo;
ExistsSSEAVX: boolean;
ConstSize : TConstSizeInfo;
end;
const
InsProp : array[tasmop] of TInsProp =
{$ifdef x86_64}
{$i x8664pro.inc}
{$else x86_64}
{$i i386prop.inc}
{$endif x86_64}
type
TOperandOrder = (op_intel,op_att);
tinsentry=packed record
opcode : tasmop;
ops : byte;
optypes : array[0..max_operands-1] of longint;
code : array[0..maxinfolen] of char;
flags : int64;
end;
pinsentry=^tinsentry;
{ alignment for operator }
tai_align = class(tai_align_abstract)
reg : tregister;
constructor create(b:byte);override;
constructor create_op(b: byte; _op: byte);override;
function calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;override;
end;
taicpu = class(tai_cpu_abstract_sym)
opsize : topsize;
constructor op_none(op : tasmop);
constructor op_none(op : tasmop;_size : topsize);
constructor op_reg(op : tasmop;_size : topsize;_op1 : tregister);
constructor op_const(op : tasmop;_size : topsize;_op1 : aint);
constructor op_ref(op : tasmop;_size : topsize;const _op1 : treference);
constructor op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
constructor op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
constructor op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
constructor op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
constructor op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
constructor op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
constructor op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
constructor op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
constructor op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
constructor op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister; const _op3 : treference);
constructor op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
{ this is for Jmp instructions }
constructor op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
constructor op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
constructor op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
constructor op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
constructor op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
procedure changeopsize(siz:topsize);
function GetString:string;
procedure CheckNonCommutativeOpcodes;
private
FOperandOrder : TOperandOrder;
procedure init(_size : topsize); { this need to be called by all constructor }
public
{ the next will reset all instructions that can change in pass 2 }
procedure ResetPass1;override;
procedure ResetPass2;override;
function CheckIfValid:boolean;
function Pass1(objdata:TObjData):longint;override;
procedure Pass2(objdata:TObjData);override;
procedure SetOperandOrder(order:TOperandOrder);
function is_same_reg_move(regtype: Tregistertype):boolean;override;
{ register spilling code }
function spilling_get_operation_type(opnr: longint): topertype;override;
private
{ next fields are filled in pass1, so pass2 is faster }
insentry : PInsEntry;
insoffset : longint;
LastInsOffset : longint; { need to be public to be reset }
inssize : shortint;
{$ifdef x86_64}
rex : byte;
{$endif x86_64}
function InsEnd:longint;
procedure create_ot(objdata:TObjData);
function Matches(p:PInsEntry):boolean;
function calcsize(p:PInsEntry):shortint;
procedure gencode(objdata:TObjData);
function NeedAddrPrefix(opidx:byte):boolean;
procedure Swapoperands;
function FindInsentry(objdata:TObjData):boolean;
end;
function spilling_create_load(const ref:treference;r:tregister):Taicpu;
function spilling_create_store(r:tregister; const ref:treference):Taicpu;
function MemRefInfo(aAsmop: TAsmOp): TInsTabMemRefSizeInfoRec;
procedure InitAsm;
procedure DoneAsm;
implementation
uses
cutils,
globals,
systems,
procinfo,
itcpugas,
symsym;
{*****************************************************************************
Instruction table
*****************************************************************************}
const
{Instruction flags }
IF_NONE = $00000000;
IF_SM = $00000001; { size match first two operands }
IF_SM2 = $00000002;
IF_SB = $00000004; { unsized operands can't be non-byte }
IF_SW = $00000008; { unsized operands can't be non-word }
IF_SD = $00000010; { unsized operands can't be nondword }
IF_SMASK = $0000001f;
IF_AR0 = $00000020; { SB, SW, SD applies to argument 0 }
IF_AR1 = $00000040; { SB, SW, SD applies to argument 1 }
IF_AR2 = $00000060; { SB, SW, SD applies to argument 2 }
IF_ARMASK = $00000060; { mask for unsized argument spec }
IF_ARSHIFT = 5; { LSB of IF_ARMASK }
IF_SI = $00000080; { ignore unsized operands }
IF_PRIV = $00000100; { it's a privileged instruction }
IF_SMM = $00000200; { it's only valid in SMM }
IF_PROT = $00000400; { it's protected mode only }
IF_NOX86_64 = $00000800; { removed instruction in x86_64 }
IF_UNDOC = $00001000; { it's an undocumented instruction }
IF_FPU = $00002000; { it's an FPU instruction }
IF_MMX = $00004000; { it's an MMX instruction }
{ it's a 3DNow! instruction }
IF_3DNOW = $00008000;
{ it's a SSE (KNI, MMX2) instruction }
IF_SSE = $00010000;
{ SSE2 instructions }
IF_SSE2 = $00020000;
{ SSE3 instructions }
IF_SSE3 = $00040000;
{ SSE64 instructions }
IF_SSE64 = $00080000;
{ the mask for processor types }
{IF_PMASK = longint($FF000000);}
{ the mask for disassembly "prefer" }
{IF_PFMASK = longint($F001FF00);}
{ SVM instructions }
IF_SVM = $00100000;
{ SSE4 instructions }
IF_SSE4 = $00200000;
{ TODO: These flags were added to make x86ins.dat more readable.
Values must be reassigned to make any other use of them. }
IF_SSSE3 = $00200000;
IF_SSE41 = $00200000;
IF_SSE42 = $00200000;
IF_AVX = $00200000;
IF_SANDYBRIDGE = $00200000;
IF_8086 = $00000000; { 8086 instruction }
IF_186 = $01000000; { 186+ instruction }
IF_286 = $02000000; { 286+ instruction }
IF_386 = $03000000; { 386+ instruction }
IF_486 = $04000000; { 486+ instruction }
IF_PENT = $05000000; { Pentium instruction }
IF_P6 = $06000000; { P6 instruction }
IF_KATMAI = $07000000; { Katmai instructions }
{ Willamette instructions }
IF_WILLAMETTE = $08000000;
{ Prescott instructions }
IF_PRESCOTT = $09000000;
IF_X86_64 = $0a000000;
IF_CYRIX = $0b000000; { Cyrix-specific instruction }
IF_AMD = $0c000000; { AMD-specific instruction }
IF_CENTAUR = $0d000000; { centaur-specific instruction }
{ added flags }
IF_PRE = $40000000; { it's a prefix instruction }
IF_PASS2 = $80000000; { if the instruction can change in a second pass }
type
TInsTabCache=array[TasmOp] of longint;
PInsTabCache=^TInsTabCache;
TInsTabMemRefSizeInfoCache=array[TasmOp] of TInsTabMemRefSizeInfoRec;
PInsTabMemRefSizeInfoCache=^TInsTabMemRefSizeInfoCache;
const
{$ifdef x86_64}
InsTab:array[0..instabentries-1] of TInsEntry={$i x8664tab.inc}
{$else x86_64}
InsTab:array[0..instabentries-1] of TInsEntry={$i i386tab.inc}
{$endif x86_64}
var
InsTabCache : PInsTabCache;
InsTabMemRefSizeInfoCache: PInsTabMemRefSizeInfoCache;
const
{$ifdef x86_64}
{ Intel style operands ! }
opsize_2_type:array[0..2,topsize] of longint=(
(OT_NONE,
OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,OT_BITS64,OT_BITS64,OT_BITS64,
OT_BITS16,OT_BITS32,OT_BITS64,
OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
OT_BITS64,
OT_NEAR,OT_FAR,OT_SHORT,
OT_NONE,
OT_BITS128,
OT_BITS256
),
(OT_NONE,
OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,OT_BITS8,OT_BITS16,OT_BITS32,
OT_BITS16,OT_BITS32,OT_BITS64,
OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
OT_BITS64,
OT_NEAR,OT_FAR,OT_SHORT,
OT_NONE,
OT_BITS128,
OT_BITS256
),
(OT_NONE,
OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,
OT_BITS16,OT_BITS32,OT_BITS64,
OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
OT_BITS64,
OT_NEAR,OT_FAR,OT_SHORT,
OT_NONE,
OT_BITS128,
OT_BITS256
)
);
reg_ot_table : array[tregisterindex] of longint = (
{$i r8664ot.inc}
);
{$else x86_64}
{ Intel style operands ! }
opsize_2_type:array[0..2,topsize] of longint=(
(OT_NONE,
OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
OT_BITS16,OT_BITS32,OT_BITS64,
OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
OT_BITS64,
OT_NEAR,OT_FAR,OT_SHORT,
OT_NONE,
OT_BITS128,
OT_BITS256
),
(OT_NONE,
OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
OT_BITS16,OT_BITS32,OT_BITS64,
OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
OT_BITS64,
OT_NEAR,OT_FAR,OT_SHORT,
OT_NONE,
OT_BITS128,
OT_BITS256
),
(OT_NONE,
OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
OT_BITS16,OT_BITS32,OT_BITS64,
OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
OT_BITS64,
OT_NEAR,OT_FAR,OT_SHORT,
OT_NONE,
OT_BITS128,
OT_BITS256
)
);
reg_ot_table : array[tregisterindex] of longint = (
{$i r386ot.inc}
);
{$endif x86_64}
function MemRefInfo(aAsmop: TAsmOp): TInsTabMemRefSizeInfoRec;
begin
result := InsTabMemRefSizeInfoCache^[aAsmop];
end;
{ Operation type for spilling code }
type
toperation_type_table=array[tasmop,0..Max_Operands] of topertype;
var
operation_type_table : ^toperation_type_table;
{****************************************************************************
TAI_ALIGN
****************************************************************************}
constructor tai_align.create(b: byte);
begin
inherited create(b);
reg:=NR_ECX;
end;
constructor tai_align.create_op(b: byte; _op: byte);
begin
inherited create_op(b,_op);
reg:=NR_NO;
end;
function tai_align.calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;
const
{$ifdef x86_64}
alignarray:array[0..3] of string[4]=(
#$66#$66#$66#$90,
#$66#$66#$90,
#$66#$90,
#$90
);
{$else x86_64}
alignarray:array[0..5] of string[8]=(
#$8D#$B4#$26#$00#$00#$00#$00,
#$8D#$B6#$00#$00#$00#$00,
#$8D#$74#$26#$00,
#$8D#$76#$00,
#$89#$F6,
#$90);
{$endif x86_64}
var
bufptr : pchar;
j : longint;
localsize: byte;
begin
inherited calculatefillbuf(buf,executable);
if not(use_op) and executable then
begin
bufptr:=pchar(@buf);
{ fillsize may still be used afterwards, so don't modify }
{ e.g. writebytes(hp.calculatefillbuf(buf)^,hp.fillsize) }
localsize:=fillsize;
while (localsize>0) do
begin
for j:=low(alignarray) to high(alignarray) do
if (localsize>=length(alignarray[j])) then
break;
move(alignarray[j][1],bufptr^,length(alignarray[j]));
inc(bufptr,length(alignarray[j]));
dec(localsize,length(alignarray[j]));
end;
end;
calculatefillbuf:=pchar(@buf);
end;
{*****************************************************************************
Taicpu Constructors
*****************************************************************************}
procedure taicpu.changeopsize(siz:topsize);
begin
opsize:=siz;
end;
procedure taicpu.init(_size : topsize);
begin
{ default order is att }
FOperandOrder:=op_att;
segprefix:=NR_NO;
opsize:=_size;
insentry:=nil;
LastInsOffset:=-1;
InsOffset:=0;
InsSize:=0;
end;
constructor taicpu.op_none(op : tasmop);
begin
inherited create(op);
init(S_NO);
end;
constructor taicpu.op_none(op : tasmop;_size : topsize);
begin
inherited create(op);
init(_size);
end;
constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
begin
inherited create(op);
init(_size);
ops:=1;
loadreg(0,_op1);
end;
constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : aint);
begin
inherited create(op);
init(_size);
ops:=1;
loadconst(0,_op1);
end;
constructor taicpu.op_ref(op : tasmop;_size : topsize;const _op1 : treference);
begin
inherited create(op);
init(_size);
ops:=1;
loadref(0,_op1);
end;
constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
begin
inherited create(op);
init(_size);
ops:=2;
loadreg(0,_op1);
loadreg(1,_op2);
end;
constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
begin
inherited create(op);
init(_size);
ops:=2;
loadreg(0,_op1);
loadconst(1,_op2);
end;
constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
begin
inherited create(op);
init(_size);
ops:=2;
loadreg(0,_op1);
loadref(1,_op2);
end;
constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
begin
inherited create(op);
init(_size);
ops:=2;
loadconst(0,_op1);
loadreg(1,_op2);
end;
constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
begin
inherited create(op);
init(_size);
ops:=2;
loadconst(0,_op1);
loadconst(1,_op2);
end;
constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
begin
inherited create(op);
init(_size);
ops:=2;
loadconst(0,_op1);
loadref(1,_op2);
end;
constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
begin
inherited create(op);
init(_size);
ops:=2;
loadref(0,_op1);
loadreg(1,_op2);
end;
constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
begin
inherited create(op);
init(_size);
ops:=3;
loadreg(0,_op1);
loadreg(1,_op2);
loadreg(2,_op3);
end;
constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
begin
inherited create(op);
init(_size);
ops:=3;
loadconst(0,_op1);
loadreg(1,_op2);
loadreg(2,_op3);
end;
constructor taicpu.op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;const _op3 : treference);
begin
inherited create(op);
init(_size);
ops:=3;
loadreg(0,_op1);
loadreg(1,_op2);
loadref(2,_op3);
end;
constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
begin
inherited create(op);
init(_size);
ops:=3;
loadconst(0,_op1);
loadref(1,_op2);
loadreg(2,_op3);
end;
constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
begin
inherited create(op);
init(_size);
ops:=3;
loadconst(0,_op1);
loadreg(1,_op2);
loadref(2,_op3);
end;
constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
begin
inherited create(op);
init(_size);
condition:=cond;
ops:=1;
loadsymbol(0,_op1,0);
end;
constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
begin
inherited create(op);
init(_size);
ops:=1;
loadsymbol(0,_op1,0);
end;
constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
begin
inherited create(op);
init(_size);
ops:=1;
loadsymbol(0,_op1,_op1ofs);
end;
constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
begin
inherited create(op);
init(_size);
ops:=2;
loadsymbol(0,_op1,_op1ofs);
loadreg(1,_op2);
end;
constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
begin
inherited create(op);
init(_size);
ops:=2;
loadsymbol(0,_op1,_op1ofs);
loadref(1,_op2);
end;
function taicpu.GetString:string;
var
i : longint;
s : string;
addsize : boolean;
begin
s:='['+std_op2str[opcode];
for i:=0 to ops-1 do
begin
with oper[i]^ do
begin
if i=0 then
s:=s+' '
else
s:=s+',';
{ type }
addsize:=false;
if (ot and OT_XMMREG)=OT_XMMREG then
s:=s+'xmmreg'
else
if (ot and OT_YMMREG)=OT_YMMREG then
s:=s+'ymmreg'
else
if (ot and OT_MMXREG)=OT_MMXREG then
s:=s+'mmxreg'
else
if (ot and OT_FPUREG)=OT_FPUREG then
s:=s+'fpureg'
else
if (ot and OT_REGISTER)=OT_REGISTER then
begin
s:=s+'reg';
addsize:=true;
end
else
if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
begin
s:=s+'imm';
addsize:=true;
end
else
if (ot and OT_MEMORY)=OT_MEMORY then
begin
s:=s+'mem';
addsize:=true;
end
else
s:=s+'???';
{ size }
if addsize then
begin
if (ot and OT_BITS8)<>0 then
s:=s+'8'
else
if (ot and OT_BITS16)<>0 then
s:=s+'16'
else
if (ot and OT_BITS32)<>0 then
s:=s+'32'
else
if (ot and OT_BITS64)<>0 then
s:=s+'64'
else
if (ot and OT_BITS128)<>0 then
s:=s+'128'
else
if (ot and OT_BITS256)<>0 then
s:=s+'256'
else
s:=s+'??';
{ signed }
if (ot and OT_SIGNED)<>0 then
s:=s+'s';
end;
end;
end;
GetString:=s+']';
end;
procedure taicpu.Swapoperands;
var
p : POper;
begin
{ Fix the operands which are in AT&T style and we need them in Intel style }
case ops of
0,1:
;
2 : begin
{ 0,1 -> 1,0 }
p:=oper[0];
oper[0]:=oper[1];
oper[1]:=p;
end;
3 : begin
{ 0,1,2 -> 2,1,0 }
p:=oper[0];
oper[0]:=oper[2];
oper[2]:=p;
end;
4 : begin
{ 0,1,2,3 -> 3,2,1,0 }
p:=oper[0];
oper[0]:=oper[3];
oper[3]:=p;
p:=oper[1];
oper[1]:=oper[2];
oper[2]:=p;
end;
else
internalerror(201108141);
end;
end;
procedure taicpu.SetOperandOrder(order:TOperandOrder);
begin
if FOperandOrder<>order then
begin
Swapoperands;
FOperandOrder:=order;
end;
end;
procedure taicpu.CheckNonCommutativeOpcodes;
begin
{ we need ATT order }
SetOperandOrder(op_att);
if (
(ops=2) and
(oper[0]^.typ=top_reg) and
(oper[1]^.typ=top_reg) and
{ if the first is ST and the second is also a register
it is necessarily ST1 .. ST7 }
((oper[0]^.reg=NR_ST) or
(oper[0]^.reg=NR_ST0))
) or
{ ((ops=1) and
(oper[0]^.typ=top_reg) and
(oper[0]^.reg in [R_ST1..R_ST7])) or}
(ops=0) then
begin
if opcode=A_FSUBR then
opcode:=A_FSUB
else if opcode=A_FSUB then
opcode:=A_FSUBR
else if opcode=A_FDIVR then
opcode:=A_FDIV
else if opcode=A_FDIV then
opcode:=A_FDIVR
else if opcode=A_FSUBRP then
opcode:=A_FSUBP
else if opcode=A_FSUBP then
opcode:=A_FSUBRP
else if opcode=A_FDIVRP then
opcode:=A_FDIVP
else if opcode=A_FDIVP then
opcode:=A_FDIVRP;
end;
if (
(ops=1) and
(oper[0]^.typ=top_reg) and
(getregtype(oper[0]^.reg)=R_FPUREGISTER) and
(oper[0]^.reg<>NR_ST)
) then
begin
if opcode=A_FSUBRP then
opcode:=A_FSUBP
else if opcode=A_FSUBP then
opcode:=A_FSUBRP
else if opcode=A_FDIVRP then
opcode:=A_FDIVP
else if opcode=A_FDIVP then
opcode:=A_FDIVRP;
end;
end;
{*****************************************************************************
Assembler
*****************************************************************************}
type
ea = packed record
sib_present : boolean;
bytes : byte;
size : byte;
modrm : byte;
sib : byte;
{$ifdef x86_64}
rex : byte;
{$endif x86_64}
end;
procedure taicpu.create_ot(objdata:TObjData);
{
this function will also fix some other fields which only needs to be once
}
var
i,l,relsize : longint;
currsym : TObjSymbol;
begin
if ops=0 then
exit;
{ update oper[].ot field }
for i:=0 to ops-1 do
with oper[i]^ do
begin
case typ of
top_reg :
begin
ot:=reg_ot_table[findreg_by_number(reg)];
end;
top_ref :
begin
if (ref^.refaddr=addr_no)
{$ifdef i386}
or (
(ref^.refaddr in [addr_pic]) and
{ allow any base for assembler blocks }
((assigned(current_procinfo) and
(pi_has_assembler_block in current_procinfo.flags) and
(ref^.base<>NR_NO)) or (ref^.base=NR_EBX))
)
{$endif i386}
{$ifdef x86_64}
or (
(ref^.refaddr in [addr_pic,addr_pic_no_got]) and
(ref^.base<>NR_NO)
)
{$endif x86_64}
then
begin
{ create ot field }
if (ot and OT_SIZE_MASK)=0 then
ot:=OT_MEMORY_ANY or opsize_2_type[i,opsize]
else
ot:=OT_MEMORY_ANY or (ot and OT_SIZE_MASK);
if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
ot:=ot or OT_MEM_OFFS;
{ fix scalefactor }
if (ref^.index=NR_NO) then
ref^.scalefactor:=0
else
if (ref^.scalefactor=0) then
ref^.scalefactor:=1;
end
else
begin
{ Jumps use a relative offset which can be 8bit,
for other opcodes we always need to generate the full
32bit address }
if assigned(objdata) and
is_jmp then
begin
currsym:=objdata.symbolref(ref^.symbol);
l:=ref^.offset;
if assigned(currsym) then
inc(l,currsym.address);
{ when it is a forward jump we need to compensate the
offset of the instruction since the previous time,
because the symbol address is then still using the
'old-style' addressing.
For backwards jumps this is not required because the
address of the symbol is already adjusted to the
new offset }
if (l>InsOffset) and (LastInsOffset<>-1) then
inc(l,InsOffset-LastInsOffset);
{ instruction size will then always become 2 (PFV) }
relsize:=(InsOffset+2)-l;
if (relsize>=-128) and (relsize<=127) and
(
not assigned(currsym) or
(currsym.objsection=objdata.currobjsec)
) then
ot:=OT_IMM8 or OT_SHORT
else
ot:=OT_IMM32 or OT_NEAR;
end
else
ot:=OT_IMM32 or OT_NEAR;
end;
end;
top_local :
begin
if (ot and OT_SIZE_MASK)=0 then
ot:=OT_MEMORY or opsize_2_type[i,opsize]
else
ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
end;
top_const :
begin
// if opcode is a SSE or AVX-instruction then we need a
// special handling (opsize can different from const-size)
// (e.g. "pextrw reg/m16, xmmreg, imm8" =>> opsize (16 bit), const-size (8 bit)
if (InsTabMemRefSizeInfoCache^[opcode].ExistsSSEAVX) and
(not(InsTabMemRefSizeInfoCache^[opcode].ConstSize in [csiMultiple, csiUnkown])) then
begin
case InsTabMemRefSizeInfoCache^[opcode].ConstSize of
csiNoSize: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE;
csiMem8: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS8;
csiMem16: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS16;
csiMem32: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS32;
csiMem64: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS64;
end;
end
else
begin
{ allow 2nd, 3rd or 4th operand being a constant and expect no size for shuf* etc. }
{ further, allow AAD and AAM with imm. operand }
if (opsize=S_NO) and not((i in [1,2,3]) or ((i=0) and (opcode in [A_AAD,A_AAM]))) then
message(asmr_e_invalid_opcode_and_operand);
if (opsize<>S_W) and (aint(val)>=-128) and (val<=127) then
ot:=OT_IMM8 or OT_SIGNED
else
ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
if (val=1) and (i=1) then
ot := ot or OT_ONENESS;
end;
end;
top_none :
begin
{ generated when there was an error in the
assembler reader. It never happends when generating
assembler }
end;
else
internalerror(200402261);
end;
end;
end;
function taicpu.InsEnd:longint;
begin
InsEnd:=InsOffset+InsSize;
end;
function taicpu.Matches(p:PInsEntry):boolean;
{ * IF_SM stands for Size Match: any operand whose size is not
* explicitly specified by the template is `really' intended to be
* the same size as the first size-specified operand.
* Non-specification is tolerated in the input instruction, but
* _wrong_ specification is not.
*
* IF_SM2 invokes Size Match on only the first _two_ operands, for
* three-operand instructions such as SHLD: it implies that the
* first two operands must match in size, but that the third is
* required to be _unspecified_.
*
* IF_SB invokes Size Byte: operands with unspecified size in the
* template are really bytes, and so no non-byte specification in
* the input instruction will be tolerated. IF_SW similarly invokes
* Size Word, and IF_SD invokes Size Doubleword.
*
* (The default state if neither IF_SM nor IF_SM2 is specified is
* that any operand with unspecified size in the template is
* required to have unspecified size in the instruction too...)
}
var
insot,
currot,
i,j,asize,oprs : longint;
insflags:cardinal;
siz : array[0..max_operands-1] of longint;
begin
result:=false;
{ Check the opcode and operands }
if (p^.opcode<>opcode) or (p^.ops<>ops) then
exit;
for i:=0 to p^.ops-1 do
begin
insot:=p^.optypes[i];
currot:=oper[i]^.ot;
{ Check the operand flags }
if (insot and (not currot) and OT_NON_SIZE)<>0 then
exit;
{ Check if the passed operand size matches with one of
the supported operand sizes }
if ((insot and OT_SIZE_MASK)<>0) and
((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
exit;
end;
{ Check operand sizes }
insflags:=p^.flags;
if insflags and IF_SMASK<>0 then
begin
{ as default an untyped size can get all the sizes, this is different
from nasm, but else we need to do a lot checking which opcodes want
size or not with the automatic size generation }
asize:=-1;
if (insflags and IF_SB)<>0 then
asize:=OT_BITS8
else if (insflags and IF_SW)<>0 then
asize:=OT_BITS16
else if (insflags and IF_SD)<>0 then
asize:=OT_BITS32;
if (insflags and IF_ARMASK)<>0 then
begin
siz[0]:=-1;
siz[1]:=-1;
siz[2]:=-1;
siz[((insflags and IF_ARMASK) shr IF_ARSHIFT)-1]:=asize;
end
else
begin
siz[0]:=asize;
siz[1]:=asize;
siz[2]:=asize;
end;
if (insflags and (IF_SM or IF_SM2))<>0 then
begin
if (insflags and IF_SM2)<>0 then
oprs:=2
else
oprs:=p^.ops;
for i:=0 to oprs-1 do
if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
begin
for j:=0 to oprs-1 do
siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
break;
end;
end
else
oprs:=2;
{ Check operand sizes }
for i:=0 to p^.ops-1 do
begin
insot:=p^.optypes[i];
currot:=oper[i]^.ot;
if ((insot and OT_SIZE_MASK)=0) and
((currot and OT_SIZE_MASK and (not siz[i]))<>0) and
{ Immediates can always include smaller size }
((currot and OT_IMMEDIATE)=0) and
(((insot and OT_SIZE_MASK) or siz[i])<(currot and OT_SIZE_MASK)) then
exit;
end;
end
else
begin
if insflags and IF_SI <> 0 then
begin
// any opcodes needed a unique mem-size for memory-operands
// in this time (e.g. CVTSI2SD, CVTSI2SS, VCVTPD2DQ, VCVTPD2PS, VCVTSI2SD, VCVTSI2SS, VCVTTPD2DQ)
// (e.g. "VCVTPD2DQ xmmreg, mem128", "VCVTPD2DQ xmmreg, mem256")
// =>> we need the exact mem-ref-size
{ Check operand sizes }
for i:=0 to p^.ops-1 do
begin
insot:=p^.optypes[i];
currot:=oper[i]^.ot;
if ((currot and OT_MEMORY) = OT_MEMORY) then
begin
if (currot and OT_SIZE_MASK) = 0 then
begin
// no operand size for memory operand
exit;
end
else
begin
if (insot and OT_XMMRM = OT_XMMRM) then
begin
if (currot and OT_SIZE_MASK) <> OT_BITS128 then
exit;
end
else if (insot and OT_YMMRM = OT_YMMRM) then
begin
if (currot and OT_SIZE_MASK) <> OT_BITS256 then
exit;
end;
end;
end;
end;
// ignore operands without operand size
end;
end;
result:=true;
end;
procedure taicpu.ResetPass1;
begin
{ we need to reset everything here, because the choosen insentry
can be invalid for a new situation where the previously optimized
insentry is not correct }
InsEntry:=nil;
InsSize:=0;
LastInsOffset:=-1;
end;
procedure taicpu.ResetPass2;
begin
{ we are here in a second pass, check if the instruction can be optimized }
if assigned(InsEntry) and
((InsEntry^.flags and IF_PASS2)<>0) then
begin
InsEntry:=nil;
InsSize:=0;
end;
LastInsOffset:=-1;
end;
function taicpu.CheckIfValid:boolean;
begin
result:=FindInsEntry(nil);
end;
function taicpu.FindInsentry(objdata:TObjData):boolean;
var
i : longint;
begin
result:=false;
{ Things which may only be done once, not when a second pass is done to
optimize }
if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
begin
current_filepos:=fileinfo;
{ We need intel style operands }
SetOperandOrder(op_intel);
{ create the .ot fields }
create_ot(objdata);
{ set the file postion }
end
else
begin
{ we've already an insentry so it's valid }
result:=true;
exit;
end;
{ Lookup opcode in the table }
InsSize:=-1;
i:=instabcache^[opcode];
if i=-1 then
begin
Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
exit;
end;
insentry:=@instab[i];
while (insentry^.opcode=opcode) do
begin
if matches(insentry) then
begin
result:=true;
exit;
end;
inc(insentry);
end;
Message1(asmw_e_invalid_opcode_and_operands,GetString);
{ No instruction found, set insentry to nil and inssize to -1 }
insentry:=nil;
inssize:=-1;
end;
function taicpu.Pass1(objdata:TObjData):longint;
begin
Pass1:=0;
{ Save the old offset and set the new offset }
InsOffset:=ObjData.CurrObjSec.Size;
{ Error? }
if (Insentry=nil) and (InsSize=-1) then
exit;
{ set the file postion }
current_filepos:=fileinfo;
{ Get InsEntry }
if FindInsEntry(ObjData) then
begin
{ Calculate instruction size }
InsSize:=calcsize(insentry);
if segprefix<>NR_NO then
inc(InsSize);
{ Fix opsize if size if forced }
if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
begin
if (insentry^.flags and IF_ARMASK)=0 then
begin
if (insentry^.flags and IF_SB)<>0 then
begin
if opsize=S_NO then
opsize:=S_B;
end
else if (insentry^.flags and IF_SW)<>0 then
begin
if opsize=S_NO then
opsize:=S_W;
end
else if (insentry^.flags and IF_SD)<>0 then
begin
if opsize=S_NO then
opsize:=S_L;
end;
end;
end;
LastInsOffset:=InsOffset;
Pass1:=InsSize;
exit;
end;
LastInsOffset:=-1;
end;
const
segprefixes: array[NR_CS..NR_GS] of Byte=(
//cs ds es ss fs gs
$2E, $3E, $26, $36, $64, $65
);
procedure taicpu.Pass2(objdata:TObjData);
begin
{ error in pass1 ? }
if insentry=nil then
exit;
current_filepos:=fileinfo;
{ Segment override }
if (segprefix>=NR_CS) and (segprefix<=NR_GS) then
begin
objdata.writebytes(segprefixes[segprefix],1);
{ fix the offset for GenNode }
inc(InsOffset);
end
else if segprefix<>NR_NO then
InternalError(201001071);
{ Generate the instruction }
GenCode(objdata);
end;
function taicpu.needaddrprefix(opidx:byte):boolean;
begin
result:=(oper[opidx]^.typ=top_ref) and
(oper[opidx]^.ref^.refaddr=addr_no) and
{$ifdef x86_64}
(oper[opidx]^.ref^.base<>NR_RIP) and
{$endif x86_64}
(
(
(oper[opidx]^.ref^.index<>NR_NO) and
(getsubreg(oper[opidx]^.ref^.index)<>R_SUBADDR)
) or
(
(oper[opidx]^.ref^.base<>NR_NO) and
(getsubreg(oper[opidx]^.ref^.base)<>R_SUBADDR)
)
);
end;
function regval(r:Tregister):byte;
const
{$ifdef x86_64}
opcode_table:array[tregisterindex] of tregisterindex = (
{$i r8664op.inc}
);
{$else x86_64}
opcode_table:array[tregisterindex] of tregisterindex = (
{$i r386op.inc}
);
{$endif x86_64}
var
regidx : tregisterindex;
begin
regidx:=findreg_by_number(r);
if regidx<>0 then
result:=opcode_table[regidx]
else
begin
Message1(asmw_e_invalid_register,generic_regname(r));
result:=0;
end;
end;
{$ifdef x86_64}
function rexbits(r: tregister): byte;
begin
result:=0;
case getregtype(r) of
R_INTREGISTER:
if (getsupreg(r)>=RS_R8) then
{ Either B,X or R bits can be set, depending on register role in instruction.
Set all three bits here, caller will discard unnecessary ones. }
result:=result or $47
else if (getsubreg(r)=R_SUBL) and
(getsupreg(r) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
result:=result or $40
else if (getsubreg(r)=R_SUBH) then
{ Not an actual REX bit, used to detect incompatible usage of
AH/BH/CH/DH }
result:=result or $80;
R_MMREGISTER:
if getsupreg(r)>=RS_XMM8 then
result:=result or $47;
end;
end;
function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
var
sym : tasmsymbol;
md,s,rv : byte;
base,index,scalefactor,
o : longint;
ir,br : Tregister;
isub,bsub : tsubregister;
begin
process_ea:=false;
fillchar(output,sizeof(output),0);
{Register ?}
if (input.typ=top_reg) then
begin
rv:=regval(input.reg);
output.modrm:=$c0 or (rfield shl 3) or rv;
output.size:=1;
output.rex:=output.rex or (rexbits(input.reg) and $F1);
process_ea:=true;
exit;
end;
{No register, so memory reference.}
if input.typ<>top_ref then
internalerror(200409263);
ir:=input.ref^.index;
br:=input.ref^.base;
isub:=getsubreg(ir);
bsub:=getsubreg(br);
s:=input.ref^.scalefactor;
o:=input.ref^.offset;
sym:=input.ref^.symbol;
if ((ir<>NR_NO) and (getregtype(ir)<>R_INTREGISTER)) or
((br<>NR_NO) and (br<>NR_RIP) and (getregtype(br)<>R_INTREGISTER)) then
internalerror(200301081);
{ it's direct address }
if (br=NR_NO) and (ir=NR_NO) then
begin
output.sib_present:=true;
output.bytes:=4;
output.modrm:=4 or (rfield shl 3);
output.sib:=$25;
end
else if (br=NR_RIP) and (ir=NR_NO) then
begin
{ rip based }
output.sib_present:=false;
output.bytes:=4;
output.modrm:=5 or (rfield shl 3);
end
else
{ it's an indirection }
begin
{ 16 bit or 32 bit address? }
if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
((br<>NR_NO) and (bsub<>R_SUBADDR)) then
message(asmw_e_16bit_32bit_not_supported);
{ wrong, for various reasons }
if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
exit;
output.rex:=output.rex or (rexbits(br) and $F1) or (rexbits(ir) and $F2);
process_ea:=true;
{ base }
case br of
NR_R8,
NR_RAX : base:=0;
NR_R9,
NR_RCX : base:=1;
NR_R10,
NR_RDX : base:=2;
NR_R11,
NR_RBX : base:=3;
NR_R12,
NR_RSP : base:=4;
NR_R13,
NR_NO,
NR_RBP : base:=5;
NR_R14,
NR_RSI : base:=6;
NR_R15,
NR_RDI : base:=7;
else
exit;
end;
{ index }
case ir of
NR_R8,
NR_RAX : index:=0;
NR_R9,
NR_RCX : index:=1;
NR_R10,
NR_RDX : index:=2;
NR_R11,
NR_RBX : index:=3;
NR_R12,
NR_NO : index:=4;
NR_R13,
NR_RBP : index:=5;
NR_R14,
NR_RSI : index:=6;
NR_R15,
NR_RDI : index:=7;
else
exit;
end;
case s of
0,
1 : scalefactor:=0;
2 : scalefactor:=1;
4 : scalefactor:=2;
8 : scalefactor:=3;
else
exit;
end;
{ If rbp or r13 is used we must always include an offset }
if (br=NR_NO) or
((br<>NR_RBP) and (br<>NR_R13) and (o=0) and (sym=nil)) then
md:=0
else
if ((o>=-128) and (o<=127) and (sym=nil)) then
md:=1
else
md:=2;
if (br=NR_NO) or (md=2) then
output.bytes:=4
else
output.bytes:=md;
{ SIB needed ? }
if (ir=NR_NO) and (br<>NR_RSP) and (br<>NR_R12) then
begin
output.sib_present:=false;
output.modrm:=(md shl 6) or (rfield shl 3) or base;
end
else
begin
output.sib_present:=true;
output.modrm:=(md shl 6) or (rfield shl 3) or 4;
output.sib:=(scalefactor shl 6) or (index shl 3) or base;
end;
end;
output.size:=1+ord(output.sib_present)+output.bytes;
process_ea:=true;
end;
{$else x86_64}
function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
var
sym : tasmsymbol;
md,s,rv : byte;
base,index,scalefactor,
o : longint;
ir,br : Tregister;
isub,bsub : tsubregister;
begin
process_ea:=false;
fillchar(output,sizeof(output),0);
{Register ?}
if (input.typ=top_reg) then
begin
rv:=regval(input.reg);
output.modrm:=$c0 or (rfield shl 3) or rv;
output.size:=1;
process_ea:=true;
exit;
end;
{No register, so memory reference.}
if (input.typ<>top_ref) then
internalerror(200409262);
if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER)) or
((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
internalerror(200301081);
ir:=input.ref^.index;
br:=input.ref^.base;
isub:=getsubreg(ir);
bsub:=getsubreg(br);
s:=input.ref^.scalefactor;
o:=input.ref^.offset;
sym:=input.ref^.symbol;
{ it's direct address }
if (br=NR_NO) and (ir=NR_NO) then
begin
{ it's a pure offset }
output.sib_present:=false;
output.bytes:=4;
output.modrm:=5 or (rfield shl 3);
end
else
{ it's an indirection }
begin
{ 16 bit address? }
if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
((br<>NR_NO) and (bsub<>R_SUBADDR)) then
message(asmw_e_16bit_not_supported);
{$ifdef OPTEA}
{ make single reg base }
if (br=NR_NO) and (s=1) then
begin
br:=ir;
ir:=NR_NO;
end;
{ convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
if (br=NR_NO) and
(((s=2) and (ir<>NR_ESP)) or
(s=3) or (s=5) or (s=9)) then
begin
br:=ir;
dec(s);
end;
{ swap ESP into base if scalefactor is 1 }
if (s=1) and (ir=NR_ESP) then
begin
ir:=br;
br:=NR_ESP;
end;
{$endif OPTEA}
{ wrong, for various reasons }
if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
exit;
{ base }
case br of
NR_EAX : base:=0;
NR_ECX : base:=1;
NR_EDX : base:=2;
NR_EBX : base:=3;
NR_ESP : base:=4;
NR_NO,
NR_EBP : base:=5;
NR_ESI : base:=6;
NR_EDI : base:=7;
else
exit;
end;
{ index }
case ir of
NR_EAX : index:=0;
NR_ECX : index:=1;
NR_EDX : index:=2;
NR_EBX : index:=3;
NR_NO : index:=4;
NR_EBP : index:=5;
NR_ESI : index:=6;
NR_EDI : index:=7;
else
exit;
end;
case s of
0,
1 : scalefactor:=0;
2 : scalefactor:=1;
4 : scalefactor:=2;
8 : scalefactor:=3;
else
exit;
end;
if (br=NR_NO) or
((br<>NR_EBP) and (o=0) and (sym=nil)) then
md:=0
else
if ((o>=-128) and (o<=127) and (sym=nil)) then
md:=1
else
md:=2;
if (br=NR_NO) or (md=2) then
output.bytes:=4
else
output.bytes:=md;
{ SIB needed ? }
if (ir=NR_NO) and (br<>NR_ESP) then
begin
output.sib_present:=false;
output.modrm:=(longint(md) shl 6) or (rfield shl 3) or base;
end
else
begin
output.sib_present:=true;
output.modrm:=(longint(md) shl 6) or (rfield shl 3) or 4;
output.sib:=(scalefactor shl 6) or (index shl 3) or base;
end;
end;
if output.sib_present then
output.size:=2+output.bytes
else
output.size:=1+output.bytes;
process_ea:=true;
end;
{$endif x86_64}
function taicpu.calcsize(p:PInsEntry):shortint;
var
codes : pchar;
c : byte;
len : shortint;
ea_data : ea;
exists_vex: boolean;
exists_vex_extention: boolean;
exists_prefix_66: boolean;
exists_prefix_F2: boolean;
exists_prefix_F3: boolean;
{$ifdef x86_64}
omit_rexw : boolean;
{$endif x86_64}
begin
len:=0;
codes:=@p^.code[0];
exists_vex := false;
exists_vex_extention := false;
exists_prefix_66 := false;
exists_prefix_F2 := false;
exists_prefix_F3 := false;
{$ifdef x86_64}
rex:=0;
omit_rexw:=false;
{$endif x86_64}
repeat
c:=ord(codes^);
inc(codes);
case c of
0 :
break;
1,2,3 :
begin
inc(codes,c);
inc(len,c);
end;
8,9,10 :
begin
{$ifdef x86_64}
rex:=rex or (rexbits(oper[c-8]^.reg) and $F1);
{$endif x86_64}
inc(codes);
inc(len);
end;
11 :
begin
inc(codes);
inc(len);
end;
4,5,6,7 :
begin
if opsize=S_W then
inc(len,2)
else
inc(len);
end;
12,13,14,
16,17,18,
20,21,22,23,
40,41,42 :
inc(len);
24,25,26,
31,
48,49,50 :
inc(len,2);
28,29,30:
begin
if opsize=S_Q then
inc(len,8)
else
inc(len,4);
end;
36,37,38:
inc(len,sizeof(pint));
44,45,46:
inc(len,8);
32,33,34,
52,53,54,
56,57,58,
172,173,174 :
inc(len,4);
60,61,62,63: ; // ignore vex-coded operand-idx
208,209,210 :
begin
case (oper[c-208]^.ot and OT_SIZE_MASK) of
OT_BITS16:
inc(len);
{$ifdef x86_64}
OT_BITS64:
begin
rex:=rex or $48;
end;
{$endif x86_64}
end;
end;
200 :
{$ifndef x86_64}
inc(len);
{$else x86_64}
{ every insentry with code 0310 must be marked with NOX86_64 }
InternalError(2011051301);
{$endif x86_64}
201 :
{$ifdef x86_64}
inc(len)
{$endif x86_64}
;
212 :
inc(len);
214 :
begin
{$ifdef x86_64}
rex:=rex or $48;
{$endif x86_64}
end;
202,
211,
213,
215,
217,218: ;
219:
begin
inc(len);
exists_prefix_F2 := true;
end;
220:
begin
inc(len);
exists_prefix_F3 := true;
end;
241:
begin
inc(len);
exists_prefix_66 := true;
end;
221:
{$ifdef x86_64}
omit_rexw:=true
{$endif x86_64}
;
64..151 :
begin
{$ifdef x86_64}
if (c<127) then
begin
if (oper[c and 7]^.typ=top_reg) then
begin
rex:=rex or (rexbits(oper[c and 7]^.reg) and $F4);
end;
end;
{$endif x86_64}
if not process_ea(oper[(c shr 3) and 7]^, ea_data, 0) then
Message(asmw_e_invalid_effective_address)
else
inc(len,ea_data.size);
{$ifdef x86_64}
rex:=rex or ea_data.rex;
{$endif x86_64}
end;
242: // VEX prefix for AVX (length = 2 or 3 bytes, dependens on REX.XBW or opcode-prefix ($0F38 or $0F3A))
// =>> DEFAULT = 2 Bytes
begin
if not(exists_vex) then
begin
inc(len, 2);
exists_vex := true;
end;
end;
243: // REX.W = 1
// =>> VEX prefix length = 3
begin
if not(exists_vex_extention) then
begin
inc(len);
exists_vex_extention := true;
end;
end;
244: ; // VEX length bit
247: inc(len); // operand 3 (ymmreg) encoded immediate byte (bit 4-7)
248: // VEX-Extention prefix $0F
// ignore for calculating length
;
249, // VEX-Extention prefix $0F38
250: // VEX-Extention prefix $0F3A
begin
if not(exists_vex_extention) then
begin
inc(len);
exists_vex_extention := true;
end;
end;
else
InternalError(200603141);
end;
until false;
{$ifdef x86_64}
if ((rex and $80)<>0) and ((rex and $4F)<>0) then
Message(asmw_e_bad_reg_with_rex);
rex:=rex and $4F; { reset extra bits in upper nibble }
if omit_rexw then
begin
if rex=$48 then { remove rex entirely? }
rex:=0
else
rex:=rex and $F7;
end;
if not(exists_vex) then
begin
if rex<>0 then
Inc(len);
end;
{$endif}
if exists_vex then
begin
if exists_prefix_66 then dec(len);
if exists_prefix_F2 then dec(len);
if exists_prefix_F3 then dec(len);
{$ifdef x86_64}
if not(exists_vex_extention) then
begin
// REX.WXB <> 0 =>> needed VEX-Extention
if rex and $0B <> 0 then inc(len);
end;
{$endif x86_64}
end;
calcsize:=len;
end;
procedure taicpu.GenCode(objdata:TObjData);
{
* the actual codes (C syntax, i.e. octal):
* \0 - terminates the code. (Unless it's a literal of course.)
* \1, \2, \3 - that many literal bytes follow in the code stream
* \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
* (POP is never used for CS) depending on operand 0
* \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
* on operand 0
* \10, \11, \12 - a literal byte follows in the code stream, to be added
* to the register value of operand 0, 1 or 2
* \13 - a literal byte follows in the code stream, to be added
* to the condition code value of the instruction.
* \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
* \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
* \24, \25, \26, \27 - an unsigned byte immediate operand, from operand 0, 1, 2 or 3
* \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
* \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
* assembly mode or the address-size override on the operand
* \37 - a word constant, from the _segment_ part of operand 0
* \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
* \44, \45, \46 - select between \3[012], \4[012] or \5[456] depending
on the address size of instruction
* \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
* \54, \55, \56 - a qword immediate, from operand 0, 1 or 2
* \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
* \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
* assembly mode or the address-size override on the operand
* \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
* \74, \75, \76 - a vex-coded vector operand, from operand 0, 1 or 2
* \1ab - a ModRM, calculated on EA in operand a, with the spare
* field the register value of operand b.
* \2ab - a ModRM, calculated on EA in operand a, with the spare
* field equal to digit b.
* \254,\255,\256 - a signed 32-bit immediate to be extended to 64 bits
* \300,\301,\302 - might be an 0x67, depending on the address size of
* the memory reference in operand x.
* \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
* \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
* \312 - (disassembler only) invalid with non-default address size.
* \320,\321,\322 - might be an 0x66 or 0x48 byte, depending on the operand
* size of operand x.
* \324 - indicates fixed 16-bit operand size, i.e. optional 0x66.
* \325 - indicates fixed 32-bit operand size, i.e. optional 0x66.
* \326 - indicates fixed 64-bit operand size, i.e. optional 0x48.
* \327 - indicates that this instruction is only valid when the
* operand size is the default (instruction to disassembler,
* generates no code in the assembler)
* \331 - instruction not valid with REP prefix. Hint for
* disassembler only; for SSE instructions.
* \332 - disassemble a rep (0xF3 byte) prefix as repe not rep.
* \333 - 0xF3 prefix for SSE instructions
* \334 - 0xF2 prefix for SSE instructions
* \335 - Indicates 64-bit operand size with REX.W not necessary
* \361 - 0x66 prefix for SSE instructions
* \362 - VEX prefix for AVX instructions
* \363 - VEX W1
* \364 - VEX Vector length 256
* \367 - operand 3 (ymmreg) encoded in bit 4-7 of the immediate byte
* \370 - VEX 0F-FLAG
* \371 - VEX 0F38-FLAG
* \372 - VEX 0F3A-FLAG
}
var
currval : aint;
currsym : tobjsymbol;
currrelreloc,
currabsreloc,
currabsreloc32 : TObjRelocationType;
{$ifdef x86_64}
rexwritten : boolean;
{$endif x86_64}
procedure getvalsym(opidx:longint);
begin
case oper[opidx]^.typ of
top_ref :
begin
currval:=oper[opidx]^.ref^.offset;
currsym:=ObjData.symbolref(oper[opidx]^.ref^.symbol);
{$ifdef i386}
if (oper[opidx]^.ref^.refaddr=addr_pic) and
(tf_pic_uses_got in target_info.flags) then
begin
currrelreloc:=RELOC_PLT32;
currabsreloc:=RELOC_GOT32;
currabsreloc32:=RELOC_GOT32;
end
else
{$endif i386}
{$ifdef x86_64}
if oper[opidx]^.ref^.refaddr=addr_pic then
begin
currrelreloc:=RELOC_PLT32;
currabsreloc:=RELOC_GOTPCREL;
currabsreloc32:=RELOC_GOTPCREL;
end
else if oper[opidx]^.ref^.refaddr=addr_pic_no_got then
begin
currrelreloc:=RELOC_RELATIVE;
currabsreloc:=RELOC_RELATIVE;
currabsreloc32:=RELOC_RELATIVE;
end
else
{$endif x86_64}
begin
currrelreloc:=RELOC_RELATIVE;
currabsreloc:=RELOC_ABSOLUTE;
currabsreloc32:=RELOC_ABSOLUTE32;
end;
end;
top_const :
begin
currval:=aint(oper[opidx]^.val);
currsym:=nil;
currabsreloc:=RELOC_ABSOLUTE;
currabsreloc32:=RELOC_ABSOLUTE32;
end;
else
Message(asmw_e_immediate_or_reference_expected);
end;
end;
{$ifdef x86_64}
procedure maybewriterex;
begin
if (rex<>0) and not(rexwritten) then
begin
rexwritten:=true;
objdata.writebytes(rex,1);
end;
end;
{$endif x86_64}
procedure objdata_writereloc(Data:aint;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);
begin
{$ifdef i386}
{ Special case of '_GLOBAL_OFFSET_TABLE_'
which needs a special relocation type R_386_GOTPC }
if assigned (p) and
(p.name='_GLOBAL_OFFSET_TABLE_') and
(tf_pic_uses_got in target_info.flags) then
begin
{ nothing else than a 4 byte relocation should occur
for GOT }
if len<>4 then
Message1(asmw_e_invalid_opcode_and_operands,GetString);
Reloctype:=RELOC_GOTPC;
{ We need to add the offset of the relocation
of _GLOBAL_OFFSET_TABLE symbol within
the current instruction }
inc(data,objdata.currobjsec.size-insoffset);
end;
{$endif i386}
objdata.writereloc(data,len,p,Reloctype);
end;
const
CondVal:array[TAsmCond] of byte=($0,
$7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
$3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
$0, $A, $A, $B, $8, $4);
var
c : byte;
pb : pbyte;
codes : pchar;
bytes : array[0..3] of byte;
rfield,
data,s,opidx : longint;
ea_data : ea;
relsym : TObjSymbol;
needed_VEX_Extention: boolean;
needed_VEX: boolean;
opmode: integer;
i: integer;
insot: longint;
VEXvvvv: byte;
VEXmmmmm: byte;
begin
{ safety check }
if objdata.currobjsec.size<>longword(insoffset) then
internalerror(200130121);
{ load data to write }
codes:=insentry^.code;
{$ifdef x86_64}
rexwritten:=false;
{$endif x86_64}
{ Force word push/pop for registers }
if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
begin
bytes[0]:=$66;
objdata.writebytes(bytes,1);
end;
// needed VEX Prefix (for AVX etc.)
needed_VEX := false;
needed_VEX_Extention := false;
opmode := -1;
VEXvvvv := 0;
VEXmmmmm := 0;
repeat
c:=ord(codes^);
inc(codes);
case c of
0: break;
1,
2,
3: inc(codes,c);
60: opmode := 0;
61: opmode := 1;
62: opmode := 2;
219: VEXvvvv := VEXvvvv OR $02; // set SIMD-prefix $F3
220: VEXvvvv := VEXvvvv OR $03; // set SIMD-prefix $F2
241: VEXvvvv := VEXvvvv OR $01; // set SIMD-prefix $66
242: needed_VEX := true;
243: begin
needed_VEX_Extention := true;
VEXvvvv := VEXvvvv OR (1 shl 7); // set REX.W
end;
244: VEXvvvv := VEXvvvv OR $04; // vectorlength = 256 bits AND no scalar
248: VEXmmmmm := VEXmmmmm OR $01; // set leading opcode byte $0F
249: begin
needed_VEX_Extention := true;
VEXmmmmm := VEXmmmmm OR $02; // set leading opcode byte $0F38
end;
250: begin
needed_VEX_Extention := true;
VEXmmmmm := VEXmmmmm OR $03; // set leading opcode byte $0F3A
end;
end;
until false;
if needed_VEX then
begin
if (opmode > ops) or
(opmode < -1) then
begin
Internalerror(777100);
end
else if opmode = -1 then
begin
VEXvvvv := VEXvvvv or ($0F shl 3); // set VEXvvvv bits (bits 6-3) to 1
end
else if oper[opmode]^.typ = top_reg then
begin
VEXvvvv := VEXvvvv or ((not(regval(oper[opmode]^.reg)) and $07) shl 3);
{$ifdef x86_64}
if rexbits(oper[opmode]^.reg) = 0 then VEXvvvv := VEXvvvv or (1 shl 6);
{$else}
VEXvvvv := VEXvvvv or (1 shl 6);
{$endif x86_64}
end
else Internalerror(777101);
if not(needed_VEX_Extention) then
begin
{$ifdef x86_64}
if rex and $0B <> 0 then needed_VEX_Extention := true;
{$endif x86_64}
end;
if needed_VEX_Extention then
begin
// VEX-Prefix-Length = 3 Bytes
bytes[0]:=$C4;
objdata.writebytes(bytes,1);
{$ifdef x86_64}
VEXmmmmm := VEXmmmmm or ((not(rex) and $07) shl 5); // set REX.rxb
{$else}
VEXmmmmm := VEXmmmmm or (7 shl 5); //
{$endif x86_64}
bytes[0] := VEXmmmmm;
objdata.writebytes(bytes,1);
{$ifdef x86_64}
VEXvvvv := VEXvvvv OR ((rex and $08) shl 7); // set REX.w
{$endif x86_64}
bytes[0] := VEXvvvv;
objdata.writebytes(bytes,1);
end
else
begin
// VEX-Prefix-Length = 2 Bytes
bytes[0]:=$C5;
objdata.writebytes(bytes,1);
{$ifdef x86_64}
if rex and $04 = 0 then
{$endif x86_64}
begin
VEXvvvv := VEXvvvv or (1 shl 7);
end;
bytes[0] := VEXvvvv;
objdata.writebytes(bytes,1);
end;
end
else
begin
needed_VEX_Extention := false;
opmode := -1;
end;
{ load data to write }
codes:=insentry^.code;
repeat
c:=ord(codes^);
inc(codes);
case c of
0 :
break;
1,2,3 :
begin
{$ifdef x86_64}
if not(needed_VEX) then
begin
maybewriterex;
end;
{$endif x86_64}
objdata.writebytes(codes^,c);
inc(codes,c);
end;
4,6 :
begin
case oper[0]^.reg of
NR_CS:
bytes[0]:=$e;
NR_NO,
NR_DS:
bytes[0]:=$1e;
NR_ES:
bytes[0]:=$6;
NR_SS:
bytes[0]:=$16;
else
internalerror(777004);
end;
if c=4 then
inc(bytes[0]);
objdata.writebytes(bytes,1);
end;
5,7 :
begin
case oper[0]^.reg of
NR_FS:
bytes[0]:=$a0;
NR_GS:
bytes[0]:=$a8;
else
internalerror(777005);
end;
if c=5 then
inc(bytes[0]);
objdata.writebytes(bytes,1);
end;
8,9,10 :
begin
{$ifdef x86_64}
if not(needed_VEX) then
begin
maybewriterex;
end;
{$endif x86_64}
bytes[0]:=ord(codes^)+regval(oper[c-8]^.reg);
inc(codes);
objdata.writebytes(bytes,1);
end;
11 :
begin
bytes[0]:=ord(codes^)+condval[condition];
inc(codes);
objdata.writebytes(bytes,1);
end;
12,13,14 :
begin
getvalsym(c-12);
if (currval<-128) or (currval>127) then
Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
if assigned(currsym) then
objdata_writereloc(currval,1,currsym,currabsreloc)
else
objdata.writebytes(currval,1);
end;
16,17,18 :
begin
getvalsym(c-16);
if (currval<-256) or (currval>255) then
Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
if assigned(currsym) then
objdata_writereloc(currval,1,currsym,currabsreloc)
else
objdata.writebytes(currval,1);
end;
20,21,22,23 :
begin
getvalsym(c-20);
if (currval<0) or (currval>255) then
Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
if assigned(currsym) then
objdata_writereloc(currval,1,currsym,currabsreloc)
else
objdata.writebytes(currval,1);
end;
24,25,26 : // 030..032
begin
getvalsym(c-24);
if (currval<-65536) or (currval>65535) then
Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
if assigned(currsym) then
objdata_writereloc(currval,2,currsym,currabsreloc)
else
objdata.writebytes(currval,2);
end;
28,29,30 : // 034..036
{ !!! These are intended (and used in opcode table) to select depending
on address size, *not* operand size. Works by coincidence only. }
begin
getvalsym(c-28);
if opsize=S_Q then
begin
if assigned(currsym) then
objdata_writereloc(currval,8,currsym,currabsreloc)
else
objdata.writebytes(currval,8);
end
else
begin
if assigned(currsym) then
objdata_writereloc(currval,4,currsym,currabsreloc32)
else
objdata.writebytes(currval,4);
end
end;
32,33,34 : // 040..042
begin
getvalsym(c-32);
if assigned(currsym) then
objdata_writereloc(currval,4,currsym,currabsreloc32)
else
objdata.writebytes(currval,4);
end;
36,37,38 : // 044..046 - select between word/dword/qword depending on
begin // address size (we support only default address sizes).
getvalsym(c-36);
{$ifdef x86_64}
if assigned(currsym) then
objdata_writereloc(currval,8,currsym,currabsreloc)
else
objdata.writebytes(currval,8);
{$else x86_64}
if assigned(currsym) then
objdata_writereloc(currval,4,currsym,currabsreloc32)
else
objdata.writebytes(currval,4);
{$endif x86_64}
end;
40,41,42 : // 050..052 - byte relative operand
begin
getvalsym(c-40);
data:=currval-insend;
if assigned(currsym) then
inc(data,currsym.address);
if (data>127) or (data<-128) then
Message1(asmw_e_short_jmp_out_of_range,tostr(data));
objdata.writebytes(data,1);
end;
44,45,46: // 054..056 - qword immediate operand
begin
getvalsym(c-44);
if assigned(currsym) then
objdata_writereloc(currval,8,currsym,currabsreloc)
else
objdata.writebytes(currval,8);
end;
52,53,54 : // 064..066 - select between 16/32 address mode, but we support only 32
begin
getvalsym(c-52);
if assigned(currsym) then
objdata_writereloc(currval,4,currsym,currrelreloc)
else
objdata_writereloc(currval-insend,4,nil,currabsreloc32)
end;
56,57,58 : // 070..072 - long relative operand
begin
getvalsym(c-56);
if assigned(currsym) then
objdata_writereloc(currval,4,currsym,currrelreloc)
else
objdata_writereloc(currval-insend,4,nil,currabsreloc32)
end;
60,61,62 : ; // 074..076 - vex-coded vector operand
// ignore
172,173,174 : // 0254..0256 - dword implicitly sign-extended to 64-bit (x86_64 only)
begin
getvalsym(c-172);
{$ifdef x86_64}
{ for i386 as aint type is longint the
following test is useless }
if (currval<low(longint)) or (currval>high(longint)) then
Message2(asmw_e_value_exceeds_bounds,'signed dword',tostr(currval));
{$endif x86_64}
if assigned(currsym) then
objdata_writereloc(currval,4,currsym,currabsreloc32)
else
objdata.writebytes(currval,4);
end;
200 : { fixed 16-bit addr }
{$ifndef x86_64}
begin
bytes[0]:=$67;
objdata.writebytes(bytes,1);
end;
{$else x86_64}
{ every insentry having code 0310 must be marked with NOX86_64 }
InternalError(2011051302);
{$endif}
201 : { fixed 32-bit addr }
{$ifdef x86_64}
begin
bytes[0]:=$67;
objdata.writebytes(bytes,1);
end
{$endif x86_64}
;
208,209,210 :
begin
case oper[c-208]^.ot and OT_SIZE_MASK of
OT_BITS16 :
begin
bytes[0]:=$66;
objdata.writebytes(bytes,1);
end;
{$ifndef x86_64}
OT_BITS64 :
Message(asmw_e_64bit_not_supported);
{$endif x86_64}
end;
end;
211,
213 : {no action needed};
212,
241:
begin
if not(needed_VEX) then
begin
bytes[0]:=$66;
objdata.writebytes(bytes,1);
end;
end;
214 :
begin
{$ifndef x86_64}
Message(asmw_e_64bit_not_supported);
{$endif x86_64}
end;
219 :
begin
if not(needed_VEX) then
begin
bytes[0]:=$f3;
objdata.writebytes(bytes,1);
end;
end;
220 :
begin
if not(needed_VEX) then
begin
bytes[0]:=$f2;
objdata.writebytes(bytes,1);
end;
end;
221:
;
202,
215,
217,218 :
begin
{ these are dissambler hints or 32 bit prefixes which
are not needed }
end;
242..244: ; // VEX flags =>> nothing todo
247: begin
if needed_VEX then
begin
if ops = 4 then
begin
if (oper[3]^.typ=top_reg) then
begin
if (oper[3]^.ot and otf_reg_xmm <> 0) or
(oper[3]^.ot and otf_reg_ymm <> 0) then
begin
bytes[0] := ((getsupreg(oper[3]^.reg) and 15) shl 4);
objdata.writebytes(bytes,1);
end
else Internalerror(777102);
end
else Internalerror(777103);
end
else Internalerror(777104);
end
else Internalerror(777105);
end;
248..250: ; // VEX flags =>> nothing todo
31,
48,49,50 :
begin
InternalError(777006);
end
else
begin
{ rex should be written at this point }
{$ifdef x86_64}
if not(needed_VEX) then
begin
if (rex<>0) and not(rexwritten) then
internalerror(200603191);
end;
{$endif x86_64}
if (c>=64) and (c<=151) then // 0100..0227
begin
if (c<127) then // 0177
begin
if (oper[c and 7]^.typ=top_reg) then
rfield:=regval(oper[c and 7]^.reg)
else
rfield:=regval(oper[c and 7]^.ref^.base);
end
else
rfield:=c and 7;
opidx:=(c shr 3) and 7;
if not process_ea(oper[opidx]^,ea_data,rfield) then
Message(asmw_e_invalid_effective_address);
pb:=@bytes[0];
pb^:=ea_data.modrm;
inc(pb);
if ea_data.sib_present then
begin
pb^:=ea_data.sib;
inc(pb);
end;
s:=pb-@bytes[0];
objdata.writebytes(bytes,s);
case ea_data.bytes of
0 : ;
1 :
begin
if (oper[opidx]^.ot and OT_MEMORY)=OT_MEMORY then
begin
currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
{$ifdef i386}
if (oper[opidx]^.ref^.refaddr=addr_pic) and
(tf_pic_uses_got in target_info.flags) then
currabsreloc:=RELOC_GOT32
else
{$endif i386}
{$ifdef x86_64}
if oper[opidx]^.ref^.refaddr=addr_pic then
currabsreloc:=RELOC_GOTPCREL
else
{$endif x86_64}
currabsreloc:=RELOC_ABSOLUTE;
objdata_writereloc(oper[opidx]^.ref^.offset,1,currsym,currabsreloc);
end
else
begin
bytes[0]:=oper[opidx]^.ref^.offset;
objdata.writebytes(bytes,1);
end;
inc(s);
end;
2,4 :
begin
currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
currval:=oper[opidx]^.ref^.offset;
{$ifdef x86_64}
if oper[opidx]^.ref^.refaddr=addr_pic then
currabsreloc:=RELOC_GOTPCREL
else
if oper[opidx]^.ref^.base=NR_RIP then
begin
currabsreloc:=RELOC_RELATIVE;
{ Adjust reloc value by number of bytes following the displacement,
but not if displacement is specified by literal constant }
if Assigned(currsym) then
Dec(currval,InsEnd-objdata.CurrObjSec.Size-ea_data.bytes);
end
else
{$endif x86_64}
{$ifdef i386}
if (oper[opidx]^.ref^.refaddr=addr_pic) and
(tf_pic_uses_got in target_info.flags) then
currabsreloc:=RELOC_GOT32
else
{$endif i386}
currabsreloc:=RELOC_ABSOLUTE32;
if (currabsreloc=RELOC_ABSOLUTE32) and
(Assigned(oper[opidx]^.ref^.relsymbol)) then
begin
relsym:=objdata.symbolref(oper[opidx]^.ref^.relsymbol);
currabsreloc:=RELOC_PIC_PAIR;
currval:=relsym.offset;
end;
objdata_writereloc(currval,ea_data.bytes,currsym,currabsreloc);
inc(s,ea_data.bytes);
end;
end;
end
else
InternalError(777007);
end;
end;
until false;
end;
function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
begin
result:=(((opcode=A_MOV) or (opcode=A_XCHG)) and
(regtype = R_INTREGISTER) and
(ops=2) and
(oper[0]^.typ=top_reg) and
(oper[1]^.typ=top_reg) and
(oper[0]^.reg=oper[1]^.reg)
) or
(((opcode=A_MOVSS) or (opcode=A_MOVSD) or (opcode=A_MOVQ) or
(opcode=A_MOVAPS) or (OPCODE=A_MOVAPD)) and
(regtype = R_MMREGISTER) and
(ops=2) and
(oper[0]^.typ=top_reg) and
(oper[1]^.typ=top_reg) and
(oper[0]^.reg=oper[1]^.reg)
);
end;
procedure build_spilling_operation_type_table;
var
opcode : tasmop;
i : integer;
begin
new(operation_type_table);
fillchar(operation_type_table^,sizeof(toperation_type_table),byte(operand_read));
for opcode:=low(tasmop) to high(tasmop) do
begin
for i:=1 to MaxInsChanges do
begin
case InsProp[opcode].Ch[i] of
Ch_Rop1 :
operation_type_table^[opcode,0]:=operand_read;
Ch_Wop1 :
operation_type_table^[opcode,0]:=operand_write;
Ch_RWop1,
Ch_Mop1 :
operation_type_table^[opcode,0]:=operand_readwrite;
Ch_Rop2 :
operation_type_table^[opcode,1]:=operand_read;
Ch_Wop2 :
operation_type_table^[opcode,1]:=operand_write;
Ch_RWop2,
Ch_Mop2 :
operation_type_table^[opcode,1]:=operand_readwrite;
Ch_Rop3 :
operation_type_table^[opcode,2]:=operand_read;
Ch_Wop3 :
operation_type_table^[opcode,2]:=operand_write;
Ch_RWop3,
Ch_Mop3 :
operation_type_table^[opcode,2]:=operand_readwrite;
end;
end;
end;
{ Special cases that can't be decoded from the InsChanges flags }
operation_type_table^[A_IMUL,1]:=operand_readwrite;
end;
function taicpu.spilling_get_operation_type(opnr: longint): topertype;
begin
{ the information in the instruction table is made for the string copy
operation MOVSD so hack here (FK)
}
if (opcode=A_MOVSD) and (ops=2) then
begin
case opnr of
0:
result:=operand_read;
1:
result:=operand_write;
else
internalerror(200506055);
end
end
else
result:=operation_type_table^[opcode,opnr];
end;
function spilling_create_load(const ref:treference;r:tregister):Taicpu;
begin
case getregtype(r) of
R_INTREGISTER :
{ we don't need special code here for 32 bit loads on x86_64, since
those will automatically zero-extend the upper 32 bits. }
result:=taicpu.op_ref_reg(A_MOV,reg2opsize(r),ref,r);
R_MMREGISTER :
case getsubreg(r) of
R_SUBMMD:
result:=taicpu.op_ref_reg(A_MOVSD,reg2opsize(r),ref,r);
R_SUBMMS:
result:=taicpu.op_ref_reg(A_MOVSS,reg2opsize(r),ref,r);
R_SUBMMWHOLE:
result:=taicpu.op_ref_reg(A_MOVQ,S_NO,ref,r);
else
internalerror(200506043);
end;
else
internalerror(200401041);
end;
end;
function spilling_create_store(r:tregister; const ref:treference):Taicpu;
var
size: topsize;
begin
case getregtype(r) of
R_INTREGISTER :
begin
size:=reg2opsize(r);
{$ifdef x86_64}
{ even if it's a 32 bit reg, we still have to spill 64 bits
because we often perform 64 bit operations on them }
if (size=S_L) then
begin
size:=S_Q;
r:=newreg(getregtype(r),getsupreg(r),R_SUBWHOLE);
end;
{$endif x86_64}
result:=taicpu.op_reg_ref(A_MOV,size,r,ref);
end;
R_MMREGISTER :
case getsubreg(r) of
R_SUBMMD:
result:=taicpu.op_reg_ref(A_MOVSD,reg2opsize(r),r,ref);
R_SUBMMS:
result:=taicpu.op_reg_ref(A_MOVSS,reg2opsize(r),r,ref);
R_SUBMMWHOLE:
result:=taicpu.op_reg_ref(A_MOVQ,S_NO,r,ref);
else
internalerror(200506042);
end;
else
internalerror(200401041);
end;
end;
{*****************************************************************************
Instruction table
*****************************************************************************}
procedure BuildInsTabCache;
var
i : longint;
begin
new(instabcache);
FillChar(instabcache^,sizeof(tinstabcache),$ff);
i:=0;
while (i<InsTabEntries) do
begin
if InsTabCache^[InsTab[i].OPcode]=-1 then
InsTabCache^[InsTab[i].OPcode]:=i;
inc(i);
end;
end;
procedure BuildInsTabMemRefSizeInfoCache;
var
AsmOp: TasmOp;
i,j: longint;
insentry : PInsEntry;
MRefInfo: TMemRefSizeInfo;
SConstInfo: TConstSizeInfo;
actRegSize: int64;
RegSize: int64;
MemSize: int64;
IsRegSizeMemSize: boolean;
ExistsRegMem: boolean;
begin
new(InsTabMemRefSizeInfoCache);
FillChar(InsTabMemRefSizeInfoCache^,sizeof(TInsTabMemRefSizeInfoCache),0);
for AsmOp := low(TAsmOp) to high(TAsmOp) do
begin
i := InsTabCache^[AsmOp];
if i >= 0 then
begin
InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiUnkown;
RegSize := 0;
IsRegSizeMemSize := true;
ExistsRegMem := false;
insentry:=@instab[i];
while (insentry^.opcode=AsmOp) do
begin
MRefInfo := msiUnkown;
actRegSize := -1;
for j := 0 to insentry^.ops -1 do
begin
if ((insentry^.optypes[j] and OT_MEMORY) <> 0) then
begin
if (insentry^.optypes[j] and OT_REGISTER) = OT_REGISTER then
begin
MRefInfo := msiUnkown;
actRegSize := insentry^.optypes[j] and OT_SIZE_MASK;
if actRegSize = 0 then
begin
case insentry^.optypes[j] and (OT_XMMREG OR OT_YMMREG) of
OT_XMMREG: begin
actRegSize := OT_BITS128;
InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := true;
end;
OT_YMMREG: begin
actRegSize := OT_BITS256;
InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := true;
end;
end;
end;
if RegSize = 0 then RegSize := actRegSize
else if actRegSize <> RegSize then RegSize := -1;
end
else
begin
MemSize := insentry^.optypes[j] and OT_SIZE_MASK;
case MemSize of
0: case insentry^.optypes[j] and (OT_MMXRM OR OT_XMMRM OR OT_YMMRM OR OT_REGISTER) of
OT_MMXRM: begin
MRefInfo := msiMem64;
MemSize := OT_BITS64;
end;
OT_XMMRM: begin
MRefInfo := msiMem128;
MemSize := OT_BITS128;
InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := true;
end;
OT_YMMRM: begin
MRefInfo := msiMem256;
MemSize := OT_BITS256;
InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := true;
end;
else MRefInfo := msiUnkown;
end;
OT_BITS8: MRefInfo := msiMem8;
OT_BITS16: MRefInfo := msiMem16;
OT_BITS32: MRefInfo := msiMem32;
OT_BITS64: MRefInfo := msiMem64;
OT_BITS128: MRefInfo := msiMem128;
OT_BITS256: MRefInfo := msiMem256;
else MRefInfo := msiMultiple;
end;
if MRefInfo <> msiUnkown then
begin
if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize = msiUnkown then
begin
InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := MRefInfo;
end
else if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize <> MRefInfo then
begin
InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMultiple;
end;
end;
end;
end
else if (insentry^.optypes[j] and OT_IMMEDIATE) = OT_IMMEDIATE then
begin
case insentry^.optypes[j] and OT_SIZE_MASK of
0: SConstInfo := csiNoSize;
OT_BITS8: SConstInfo := csiMem8;
OT_BITS16: SConstInfo := csiMem16;
OT_BITS32: SConstInfo := csiMem32;
OT_BITS64: SConstInfo := csiMem64;
else SConstInfo := csiMultiple;
end;
if InsTabMemRefSizeInfoCache^[AsmOp].ConstSize = csiUnkown then
begin
InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := SConstInfo;
end
else if InsTabMemRefSizeInfoCache^[AsmOp].ConstSize <> SConstInfo then
begin
InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiMultiple;
end;
end;
end;
if actRegSize < 0 then RegSize := -1;
if RegSize > 0 then
begin
IsRegSizeMemSize := IsRegSizeMemSize and ((RegSize > 0) and (RegSize = MemSize));
ExistsRegMem := true;
end;
inc(insentry);
end;
if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize = msiMultiple then
begin
if IsRegSizeMemSize and ExistsRegMem then InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegSize;
end;
end;
end;
for AsmOp := low(TAsmOp) to high(TAsmOp) do
begin
// only supported intructiones with SSE- or AVX-operands
if not(InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX) then
begin
InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiUnkown;
end;
end;
end;
procedure InitAsm;
begin
build_spilling_operation_type_table;
if not assigned(instabcache) then
BuildInsTabCache;
if not assigned(InsTabMemRefSizeInfoCache) then
BuildInsTabMemRefSizeInfoCache;
end;
procedure DoneAsm;
begin
if assigned(operation_type_table) then
begin
dispose(operation_type_table);
operation_type_table:=nil;
end;
if assigned(instabcache) then
begin
dispose(instabcache);
instabcache:=nil;
end;
if assigned(InsTabMemRefSizeInfoCache) then
begin
dispose(InsTabMemRefSizeInfoCache);
InsTabMemRefSizeInfoCache:=nil;
end;
end;
begin
cai_align:=tai_align;
cai_cpu:=taicpu;
end.
|