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
/****************************************************************************
*
* Open Watcom Project
*
* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
* ========================================================================
*
* This file contains Original Code and/or Modifications of Original
* Code as defined in and that are subject to the Sybase Open Watcom
* Public License version 1.0 (the 'License'). You may not use this file
* except in compliance with the License. BY USING THIS FILE YOU AGREE TO
* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
* provided with the Original Code and Modifications, and is also
* available at www.sybase.com/developer/opensource.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
* NON-INFRINGEMENT. Please see the License for the specific language
* governing rights and limitations under the License.
*
* ========================================================================
*
* Description: Parser
*
****************************************************************************/
#include <ctype.h>
#include <limits.h>
#include "globals.h"
#include "memalloc.h"
#include "parser.h"
#include "preproc.h"
#include "reswords.h"
#include "codegen.h"
#include "codegenv2.h"
#include "expreval.h"
#include "fixup.h"
#include "types.h"
#include "label.h"
#include "segment.h"
#include "assume.h"
#include "proc.h"
#include "myassert.h"
#include "input.h"
#include "tokenize.h"
#include "listing.h"
#include "data.h"
#include "fastpass.h"
#include "omf.h"
#include "omfspec.h"
#include "condasm.h"
#include "extern.h"
#include "atofloat.h"
#if defined(WINDOWSDDK)
#define PRIx64 "llx"
#else
#include <inttypes.h>
#endif
#define ADDRSIZE( s, x ) ( ( ( x ) ^ ( s ) ) ? TRUE : FALSE )
#define IS_ADDR32( s ) ( s->Ofssize ? ( s->prefix.adrsiz == FALSE ) : ( s->prefix.adrsiz == TRUE ))
#define OPSIZE32( s ) ( ( s->Ofssize ) ? FALSE : TRUE )
#define OPSIZE16( s ) ( ( s->Ofssize ) ? TRUE : FALSE )
#define InWordRange( val ) ( (val > 65535 || val < -65535) ? FALSE : TRUE )
extern ret_code (* const directive_tab[])( int, struct asm_tok[] );
/* parsing of branch instructions with imm operand is found in branch.c */
extern ret_code process_branch( struct code_info *, unsigned, const struct expr * );
extern enum proc_status ProcStatus;
extern const int_64 maxintvalues[];
extern const int_64 minintvalues[];
extern const struct opnd_class opnd_clstab[];
extern const uint_8 vex_flags[];
extern int_8 Frame_Type; /* Frame of current fixup */
extern uint_16 Frame_Datum; /* Frame datum of current fixup */
struct asym *SegOverride;
static enum assume_segreg LastRegOverride; /* needed for CMPS */
struct asm_tok xmmOver0; /* xmmword override tokens for -Zg switch (masm compatibility) */
struct asm_tok xmmOver1;
struct asm_tok dsOver;
/* linked lists of: index
*--------------------------------
* - undefined symbols TAB_UNDEF
* - externals TAB_EXT
* - segments TAB_SEG
* - groups TAB_GRP
* - procedures TAB_PROC
* - aliases TAB_ALIAS */
struct symbol_queue SymTables[TAB_LAST];
/* =====================================================================
Return true if register a simd register (xmm,ymm,zmm).
===================================================================== */
bool IsSimdReg(struct asm_tok *regTok)
{
bool result = FALSE;
if (regTok)
{
if (regTok->tokval >= T_XMM0 && regTok->tokval <= T_XMM7)
result = TRUE;
else if (regTok->tokval >= T_XMM8 && regTok->tokval <= T_XMM15)
result = TRUE;
else if (regTok->tokval >= T_XMM16 && regTok->tokval <= T_XMM23)
result = TRUE;
else if (regTok->tokval >= T_XMM24 && regTok->tokval <= T_XMM31)
result = TRUE;
else if (regTok->tokval >= T_YMM0 && regTok->tokval <= T_YMM7)
result = TRUE;
else if (regTok->tokval >= T_YMM8 && regTok->tokval <= T_YMM15)
result = TRUE;
else if (regTok->tokval >= T_YMM16 && regTok->tokval <= T_YMM23)
result = TRUE;
else if (regTok->tokval >= T_YMM24 && regTok->tokval <= T_YMM31)
result = TRUE;
else if (regTok->tokval >= T_ZMM0 && regTok->tokval <= T_ZMM7)
result = TRUE;
else if (regTok->tokval >= T_ZMM8 && regTok->tokval <= T_ZMM31)
result = TRUE;
}
return result;
}
/* add item to linked list of symbols */
void sym_add_table( struct symbol_queue *queue, struct dsym *item )
/*****************************************************************/
{
#ifdef DEBUG_OUT
if ( queue == &SymTables[TAB_UNDEF] )
item->sym.fwdref = TRUE;
#endif
if( queue->head == NULL ) {
queue->head = queue->tail = item;
item->next = item->prev = NULL;
} else {
item->prev = queue->tail;
queue->tail->next = item;
queue->tail = item;
item->next = NULL;
}
}
/* remove an item from a symbol queue.
* this is called only for TAB_UNDEF and TAB_EXT,
* segments, groups, procs or aliases never change their state. */
void sym_remove_table( struct symbol_queue *queue, struct dsym *item )
/********************************************************************/
{
/* unlink the node */
if( item->prev )
item->prev->next = item->next;
if( item->next )
item->next->prev = item->prev;
//if ( dir->next == NULL )
// dir->next = dir->prev;
if ( queue->head == item )
queue->head = item->next;
if ( queue->tail == item )
queue->tail = item->prev;
item->next = NULL;
item->prev = NULL;
}
void sym_ext2int( struct asym *sym )
/**********************************/
/* Change symbol state from SYM_EXTERNAL to SYM_INTERNAL.
* called by:
* - CreateConstant() EXTERNDEF name:ABS -> constant
* - CreateAssemblyTimeVariable() EXTERNDEF name:ABS -> assembly-time variable
* - CreateLabel() EXTERNDEF name:NEAR|FAR|PROC -> code label
* - data_dir() EXTERNDEF name:typed memref -> data label
* - ProcDir() PROTO or EXTERNDEF name:NEAR|FAR|PROC -> PROC
*/
{
/* v2.07: GlobalQueue has been removed */
if ( sym->isproc == FALSE && sym->ispublic == FALSE ) {
sym->ispublic = TRUE;
AddPublicData( sym );
}
sym_remove_table( &SymTables[TAB_EXT], (struct dsym *)sym );
if ( sym->isproc == FALSE ) /* v2.01: don't clear flags for PROTO */
sym->first_size = 0;
sym->state = SYM_INTERNAL;
}
ret_code GetLangType( int *i, struct asm_tok tokenarray[], enum lang_type *plang )
/********************************************************************************/
{
if( tokenarray[*i].token == T_RES_ID ) {
#if 1 /* v2.03: simplified */
if ( tokenarray[(*i)].tokval >= T_C &&
tokenarray[(*i)].tokval <= T_BORLAND ) { /* 2.15 implemented the VECTORCALL */
*plang = tokenarray[(*i)].bytval;
(*i)++;
return( NOT_ERROR );
}
#else
switch( tokenarray[(*i)].tokval ) {
case T_C: *plang = LANG_C; break;
case T_SYSCALL: *plang = LANG_SYSCALL; break;
case T_STDCALL: *plang = LANG_STDCALL; break;
case T_PASCAL: *plang = LANG_PASCAL; break;
case T_FORTRAN: *plang = LANG_FORTRAN; break;
case T_BASIC: *plang = LANG_BASIC; break;
case T_FASTCALL: *plang = LANG_FASTCALL; break;
case T_VECTORCALL:*plang = LANG_VECTORCALL; break;
case T_SYSVCALL: *plang = LANG_SYSVCALL; break;
case T_BORLAND *plang = LANG_DELPHICALL; break;
default:
return( ERROR );
}
(*i)++;
return( NOT_ERROR );
#endif
}
return( ERROR );
}
/* get size of a register
* v2.06: rewritten, since the sflags field
* does now contain size for GPR, STx, MMX, XMM regs. */
int SizeFromRegister( int registertoken )
/***************************************/
{
unsigned flags;
if (((registertoken >= T_YMM0) && (registertoken <= T_YMM7 ))||
((registertoken >= T_YMM8) && (registertoken <= T_YMM31 )))
flags = GetSflagsSp( registertoken ) & SFR_YMMMASK ;
else if (((registertoken >= T_ZMM0) && (registertoken <= T_ZMM7 ))||
((registertoken >= T_ZMM8) && (registertoken <= T_ZMM31 )))
flags = GetSflagsSp( registertoken ) & SFR_ZMMMASK ;
else
flags = GetSflagsSp( registertoken ) & SFR_SIZMSK;
if ( flags )
return( flags );
flags = GetValueSp( registertoken );
if ( flags & OP_SR )
return( CurrWordSize );
/* CRx, DRx, TRx remaining */
#if AMD64_SUPPORT
return( ModuleInfo.Ofssize == USE64 ? 8 : 4 );
#else
return( 4 );
#endif
}
/* get size from memory type */
/* MT_PROC memtype is set ONLY in typedefs ( state=SYM_TYPE, typekind=TYPE_TYPEDEF)
* and makes the type a PROTOTYPE. Due to technical (obsolete?) restrictions the
* prototype data is stored in another symbol and is referenced in the typedef's
* target_type member. */
int SizeFromMemtype( enum memtype mem_type, int Ofssize, struct asym *type )
/**************************************************************************/
{
if ((mem_type & MT_SPECIAL) == 0){
#if AVXSUPP
if (mem_type == MT_ZMMWORD )
return (0x40);
else
#endif
return ((mem_type & MT_SIZE_MASK) + 1);
}
if ( Ofssize == USE_EMPTY )
Ofssize = ModuleInfo.Ofssize;
switch ( mem_type ) {
case MT_NEAR:
DebugMsg1(("SizeFromMemtype( MT_NEAR, Ofssize=%u )=%u\n", Ofssize, 2 << Ofssize ));
return ( 2 << Ofssize );
case MT_FAR:
DebugMsg1(("SizeFromMemtype( MT_FAR, Ofssize=%u )=%u\n", Ofssize, ( 2 << Ofssize ) + 2 ));
return ( ( 2 << Ofssize ) + 2 );
case MT_PROC:
DebugMsg1(("SizeFromMemtype( MT_PROC, Ofssize=%u, type=%s )=%u\n", Ofssize, type->name, ( 2 << Ofssize ) + ( type->isfar ? 2 : 0 ) ));
/* v2.09: use type->isfar setting */
//return( ( 2 << Ofssize ) + ( ( SIZE_CODEPTR & ( 1 << ModuleInfo.model ) ) ? 2 : 0 ) );
return( ( 2 << Ofssize ) + ( type->isfar ? 2 : 0 ) );
case MT_PTR:
DebugMsg1(("SizeFromMemtype( MT_PTR, Ofssize=%u )=%u\n", Ofssize, ( 2 << Ofssize ) + ( ( SIZE_DATAPTR & ( 1 << ModuleInfo.model ) ) ? 2 : 0 ) ));
return( ( 2 << Ofssize ) + ( ( SIZE_DATAPTR & ( 1 << ModuleInfo.model ) ) ? 2 : 0 ) );
case MT_TYPE:
if ( type )
return( type->total_size );
default:
DebugMsg1(("SizeFromMemtype( memtype=%Xh, Ofssize=%u )=%u\n", mem_type, Ofssize, 0 ));
return( 0 );
}
}
/* get memory type from size */
ret_code MemtypeFromSize( int size, enum memtype *ptype )
/*******************************************************/
{
int i;
for ( i = T_BYTE; SpecialTable[i].type == RWT_STYPE; i++ ) {
if( ( SpecialTable[i].bytval & MT_SPECIAL ) == 0 ) {
/* the size is encoded 0-based in field mem_type */
#if AVXSUPP
if (SpecialTable[i].bytval == MT_ZMMWORD){
if (((SpecialTable[i].bytval & 0x3f) + 1) == size) {
*ptype = SpecialTable[i].bytval;
return(NOT_ERROR);
}
}
else{
#endif
if (((SpecialTable[i].bytval & MT_SIZE_MASK) + 1) == size) {
*ptype = SpecialTable[i].bytval;
return(NOT_ERROR);
}
#if AVXSUPP
}
#endif
}
}
return( ERROR );
}
static bool IsScalarSimdInstr(enum instr_token instr)
{
bool result = FALSE;
switch (instr)
{
case T_ADDSS:
case T_ADDSD:
case T_CMPSD:
case T_CMPSS:
case T_COMISD:
case T_COMISS:
case T_CVTSD2SI:
case T_CVTSD2SS:
case T_CVTSI2SD:
case T_CVTSI2SS:
case T_CVTSS2SD:
case T_CVTSS2SI:
case T_CVTTSD2SI:
case T_CVTTSS2SI:
case T_DIVSD:
case T_DIVSS:
case T_INSERTPS:
case T_MAXSD:
case T_MAXSS:
case T_MINSD:
case T_MINSS:
case T_MOVD:
case T_MOVQ:
case T_MOVDDUP:
case T_MOVHPD:
case T_MOVHPS:
case T_MOVLPD:
case T_MOVLPS:
case T_MOVSD:
case T_MOVSS:
case T_MULSD:
case T_MULSS:
case T_RCPSS:
case T_ROUNDSS:
case T_ROUNDSD:
case T_RSQRTSS:
case T_SQRTSS:
case T_SQRTSD:
case T_SUBSS:
case T_SUBSD:
case T_UCOMISS:
case T_UCOMISD:
case T_PEXTRB:
case T_PEXTRD:
case T_PEXTRQ:
case T_VPEXTRB:
case T_VPEXTRD:
case T_VPEXTRQ:
case T_PEXTRW:
case T_VPEXTRW:
case T_PINSRB:
case T_PINSRD:
case T_PINSRQ:
case T_VPINSRB:
case T_VPINSRD:
case T_VPINSRQ:
case T_PINSRW:
case T_VPINSRW:
case T_VADDSS:
case T_VADDSD:
case T_VCMPSD:
case T_VCMPSS:
case T_VCOMISD:
case T_VCOMISS:
case T_VCVTSD2SI:
case T_VCVTSD2SS:
case T_VCVTSI2SD:
case T_VCVTSI2SS:
case T_VCVTSS2SD:
case T_VCVTSS2SI:
case T_VCVTTSD2SI:
case T_VCVTTSS2SI:
case T_VDIVSD:
case T_VDIVSS:
case T_VINSERTPS:
case T_VMAXSD:
case T_VMAXSS:
case T_VMINSD:
case T_VMINSS:
case T_VMOVD:
case T_VMOVQ:
case T_VMOVDDUP:
case T_VMOVHPD:
case T_VMOVHPS:
case T_VMOVLPD:
case T_VMOVLPS:
case T_VMOVSD:
case T_VMOVSS:
case T_VMULSD:
case T_VMULSS:
case T_VRCPSS:
case T_VROUNDSS:
case T_VROUNDSD:
case T_VRSQRTSS:
case T_VSQRTSS:
case T_VSQRTSD:
case T_VSUBSS:
case T_VSUBSD:
case T_VUCOMISS:
case T_VUCOMISD:
case T_VPBROADCASTD:
result = TRUE;
break;
default:
break;
}
return(result);
}
int OperandSize( enum operand_type opnd, const struct code_info *CodeInfo )
/*************************************************************************/
{
/* v2.0: OP_M8_R8 and OP_M16_R16 have the DFT bit set! */
if( opnd == OP_NONE ) {
return( 0 );
} else if( opnd == OP_M ) {
return( SizeFromMemtype( CodeInfo->mem_type, CodeInfo->Ofssize, NULL ) );
} else if( opnd & ( OP_R8 | OP_M08 | OP_I8 ) ) {
return( 1 );
} else if( opnd & ( OP_R16 | OP_M16 | OP_I16 | OP_SR ) ) {
return( 2 );
} else if( opnd & ( OP_R32 | OP_M32 | OP_I32 ) ) {
return( 4 );
#if AMD64_SUPPORT
} else if( opnd & ( OP_R64 | OP_M64 | OP_MMX | OP_I64 ) ) {
#else
} else if( opnd & ( OP_M64 | OP_MMX ) ) {
#endif
return( 8 );
// } else if( opnd & ( OP_I | OP_I48 ) ) {
} else if( opnd & ( OP_I48 | OP_M48 ) ) {
return( 6 );
} else if( opnd & ( OP_STI | OP_M80 ) ) {
return( 10 );
} else if( opnd & ( OP_XMM | OP_M128 ) ) {
return( 16 );
#if AVXSUPP
}else if (opnd & (OP_K | OP_M64)) {
return(8);
}else if (opnd & (OP_YMM | OP_M256)) {
return( 32 );
}else if (opnd & (OP_ZMM | OP_M512)) {
return(64);
#endif
} else if( opnd & OP_RSPEC ) {
#if AMD64_SUPPORT
return( ( CodeInfo->Ofssize == USE64 ) ? 8 : 4 );
#else
return( 4 );
#endif
}
DebugMsg1(("OperandSize: unhandled operand type %Xh!!!\n", opnd ));
return( 0 );
}
static int comp_mem16( int reg1, int reg2 )
/*****************************************/
/*
- compare and return the r/m field encoding of 16-bit address mode;
- call by set_rm_sib() only;
*/
{
switch( reg1 ) {
case T_BX:
switch( reg2 ) {
case T_SI: return( RM_BX_SI ); /* 00 */
case T_DI: return( RM_BX_DI ); /* 01 */
}
break;
case T_BP:
switch( reg2 ) {
case T_SI: return( RM_BP_SI ); /* 02 */
case T_DI: return( RM_BP_DI ); /* 03 */
}
break;
default:
return( EmitError( MULTIPLE_INDEX_REGISTERS_NOT_ALLOWED ) );
}
return( EmitError( MULTIPLE_BASE_REGISTERS_NOT_ALLOWED ) );
}
static void check_assume( struct code_info *CodeInfo, const struct asym *sym, enum assume_segreg default_reg )
/************************************************************************************************************/
/* Check if an assumed segment register is found, and
* set CodeInfo->RegOverride if necessary.
* called by seg_override().
* at least either sym or SegOverride is != NULL.
*/
{
enum assume_segreg reg;
struct asym *assume;
if( sym && sym->state == SYM_UNDEFINED )
return;
reg = GetAssume( SegOverride, sym, default_reg, &assume );
/* set global vars Frame and Frame_Datum */
DebugMsg1(("check_assume(%s): calling SetFixupFrame(%s, FALSE)\n", sym ? sym->name : "NULL", assume ? assume->name : "NULL" ));
SetFixupFrame( assume, FALSE );
if( reg == ASSUME_NOTHING ) {
if ( sym ) {
//if( sym->state != SYM_EXTERNAL && sym->state != SYM_STACK ) {
/* v1.95: condition changed. Now there's an error msg only if
* the symbol has an explicite segment.
*/
if( sym->segment != NULL ) {
DebugMsg1(("check_assume: no segment register available to access label %s\n", sym->name ));
EmitErr( CANNOT_ACCESS_LABEL_THROUGH_SEGMENT_REGISTERS, sym->name );
} else
CodeInfo->prefix.RegOverride = default_reg;
} else {
DebugMsg1(("check_assume: no segment register available to access seg-label %s\n", SegOverride->name ));
EmitErr( CANNOT_ACCESS_LABEL_THROUGH_SEGMENT_REGISTERS, SegOverride->name );
}
} else if( default_reg != EMPTY ) {
CodeInfo->prefix.RegOverride = reg;
}
}
static void seg_override( struct code_info *CodeInfo, int seg_reg, const struct asym *sym, bool direct )
/******************************************************************************************************/
/*
* called by set_rm_sib(). determine if segment override is necessary
* with the current address mode;
* - seg_reg: register index (T_DS, T_BP, T_EBP, T_BX, ... )
*/
{
enum assume_segreg default_seg;
struct asym *assume;
/* don't touch segment overrides for string instructions */
//if ( InstrTable[optable_idx[CodeInfo->token]].allowed_prefix == AP_REP ||
// InstrTable[optable_idx[CodeInfo->token]].allowed_prefix == AP_REPxx )
if ( CodeInfo->pinstr->allowed_prefix == AP_REP ||
CodeInfo->pinstr->allowed_prefix == AP_REPxx )
return;
if( CodeInfo->token == T_LEA ) {
CodeInfo->prefix.RegOverride = EMPTY; /* skip segment override */
SetFixupFrame( sym, FALSE );
return;
}
switch( seg_reg ) {
//case T_SS: /* doesn't happen */
case T_BP:
case T_EBP:
case T_ESP:
/* todo: check why cases T_RBP/T_RSP aren't needed! */
default_seg = ASSUME_SS;
break;
default:
default_seg = ASSUME_DS;
}
if( CodeInfo->prefix.RegOverride != EMPTY ) {
assume = GetOverrideAssume( CodeInfo->prefix.RegOverride );
/* assume now holds assumed SEG/GRP symbol */
if ( sym ) {
DebugMsg1(("seg_override: sym=%s\n", sym->name ));
SetFixupFrame( assume ? assume : sym, FALSE );
} else if ( direct ) {
/* no label attached (DS:[0]). No fixup is to be created! */
if ( assume ) {
DebugMsg1(("seg_override, direct addressing: prefix.adrsiz will be set, assume=%s CI->ofssize=%u\n", assume->name, CodeInfo->Ofssize ));
CodeInfo->prefix.adrsiz = ADDRSIZE( CodeInfo->Ofssize, GetSymOfssize( assume ) );
//DebugMsg1(("seg_override: CI->prefix.adrsiz=%u\n", CodeInfo->prefix.adrsiz ));
} else {
/* v2.01: if -Zm, then use current CS offset size.
* This isn't how Masm v6 does it, but it matches Masm v5.
*/
if ( ModuleInfo.m510 )
CodeInfo->prefix.adrsiz = ADDRSIZE( CodeInfo->Ofssize, ModuleInfo.Ofssize );
else
CodeInfo->prefix.adrsiz = ADDRSIZE( CodeInfo->Ofssize, ModuleInfo.defOfssize );
}
}
} else {
if ( sym || SegOverride )
check_assume( CodeInfo, sym, default_seg );
if ( sym == NULL && SegOverride ) {
CodeInfo->prefix.adrsiz = ADDRSIZE( CodeInfo->Ofssize, GetSymOfssize( SegOverride ) );
}
}
if( CodeInfo->prefix.RegOverride == default_seg ) {
CodeInfo->prefix.RegOverride = EMPTY;
}
}
/* prepare fixup creation
* called by:
* - idata_fixup()
* - process_branch() in branch.c
* - data_item() in data.c */
void set_frame( const struct asym *sym )
/**************************************/
{
SetFixupFrame( SegOverride ? SegOverride : sym, FALSE );
}
/* set fixup frame if OPTION OFFSET:SEGMENT is set and
* OFFSET or SEG operator was used.
* called by:
* - idata_fixup()
* - data_item() */
void set_frame2( const struct asym *sym )
/***************************************/
{
SetFixupFrame( SegOverride ? SegOverride : sym, TRUE );
}
static ret_code set_rm_sib(struct code_info *CodeInfo, unsigned CurrOpnd, char ss, int index, int base, const struct asym *sym)
/*******************************************************************************************************************************/
/*
* encode ModRM and SIB byte for memory addressing.
* called by memory_operand().
* in: ss = scale factor (00=1,40=2,80=4,C0=8)
* index = index register (T_DI, T_ESI, ...)
* base = base register (T_EBP, ... )
* sym = symbol (direct addressing, displacement)
* out: CodeInfo->rm_byte, CodeInfo->sib, CodeInfo->prefix.rex
*/
{
int temp;
unsigned char mod_field;
unsigned char rm_field;
unsigned char base_reg;
unsigned char idx_reg;
#if AMD64_SUPPORT
unsigned char bit3_base;
unsigned char bit3_idx;
unsigned char rex;
#endif
// __debugbreak();
DebugMsg1(("set_rm_sib(scale=%u, index=%d, base=%d, sym=%s) enter [CI.adrsiz=%u]\n", 1 << (ss >> 6), index, base, sym ? sym->name : "NULL", CodeInfo->prefix.adrsiz));
/* clear mod */
rm_field = 0;
CodeInfo->basetype = base;
#if AMD64_SUPPORT
bit3_base = 0;
bit3_idx = 0;
rex = 0;
#endif
if (CodeInfo->opnd[CurrOpnd].InsFixup != NULL) { /* symbolic displacement given? */
mod_field = MOD_10;
}
else if ((CodeInfo->opnd[CurrOpnd].data32l == 0) || (base == T_RIP)) { /* no displacement (or 0) */
mod_field = MOD_00;
}
else if ((CodeInfo->opnd[CurrOpnd].data32l > SCHAR_MAX)
|| (CodeInfo->opnd[CurrOpnd].data32l < SCHAR_MIN)) {
mod_field = MOD_10; /* full size displacement */
}
else {
mod_field = MOD_01; /* byte size displacement */
}
/* In the case of suppressed base register it has to be swapped with index v2.38 */
temp = GetValueSp( base ); /* get value from SpecialTable */
if (temp == OP_XMM || temp == OP_YMM || temp == OP_ZMM){ /* base can be only GP register*/
temp = index; /* swap index with base */
index = base;
base = temp;
temp = CodeInfo->indexreg; /* register numbers need to be swapped as well*/
CodeInfo->indexreg = CodeInfo->basereg;
if (temp == 0xff) /* if base is empty */
CodeInfo->basereg = 0x10; /* use RIP relative addressing RIP reg number is 0x10 */
else /* temp contains register number from index */
CodeInfo->basereg = temp; /* use reg number from index */
}
if( ( index == EMPTY ) && ( base == EMPTY ) ) {
/* direct memory.
* clear the rightmost 3 bits
*/
CodeInfo->isdirect = TRUE;
mod_field = MOD_00;
/* default is DS:[], DS: segment override is not needed */
seg_override( CodeInfo, T_DS, sym, TRUE );
DebugMsg1(( "set_rm_sib: direct addressing, CI.Ofssize=%u / adrsize=%u / data=%" I32_SPEC "X\n",
CodeInfo->Ofssize, CodeInfo->prefix.adrsiz, CodeInfo->opnd[CurrOpnd].data32l ));
//if( !IS_ADDR32( CodeInfo ) ) {
if( ( CodeInfo->Ofssize == USE16 && CodeInfo->prefix.adrsiz == 0 ) ||
( CodeInfo->Ofssize == USE32 && CodeInfo->prefix.adrsiz == 1 )) {
if( !InWordRange( CodeInfo->opnd[CurrOpnd].data32l ) ) {
/* expect 16-bit but got 32-bit address */
DebugMsg1(( "set_rm_sib: error, Ofssize=%u, adrsize=%u, data=%" I32_SPEC "X\n",
CodeInfo->Ofssize, CodeInfo->prefix.adrsiz, CodeInfo->opnd[CurrOpnd].data32l ));
return( EmitError( MAGNITUDE_OF_OFFSET_EXCEEDS_16BIT ) );
}
rm_field = RM_D16; /* D16=110b */
} else {
rm_field = RM_D32; /* D32=101b */
#if AMD64_SUPPORT
if ( CodeInfo->Ofssize == USE64 ) {
if ( CodeInfo->opnd[CurrOpnd].InsFixup == NULL ) {
rm_field = RM_SIB; /* 64-bit non-RIP direct addressing */
CodeInfo->sib = 0x25; /* IIIBBB, base=101b, index=100b */
} else if ( CodeInfo->opnd[CurrOpnd].InsFixup->type == FIX_OFF32 ) {
/* added v2.42 */
CodeInfo->opnd[CurrOpnd].InsFixup->type = FIX_RELOFF32;
}
}
#endif
}
DebugMsg1(("set_rm_sib, direct, CodeInfo->prefix.adrsiz=%u\n", CodeInfo->prefix.adrsiz ));
} else if( ( index == EMPTY ) && ( base != EMPTY ) ) {
/* for SI, DI and BX: default is DS:[],
* DS: segment override is not needed
* for BP: default is SS:[], SS: segment override is not needed
*/
switch( base ) {
case T_SI:
rm_field = RM_SI; /* 4 */
break;
case T_DI:
rm_field = RM_DI; /* 5 */
break;
case T_BP:
rm_field = RM_BP; /* 6 */
if( mod_field == MOD_00 ) {
if (base != T_RIP) mod_field = MOD_01;
}
break;
case T_BX:
rm_field = RM_BX; /* 7 */
break;
default: /* for 386 and up */
base_reg = GetRegNo( base );
#if AMD64_SUPPORT
if (base_reg == 16)
base_reg=5; //RIP bytval=16 but we need 5 added by habran
bit3_base = base_reg >> 3;
base_reg &= BIT_012;
#endif
rm_field = base_reg;
DebugMsg1(("set_rm_sib: base_reg is %u\n", base_reg ));
if ( base_reg == 4 ) {
/* 4 is RSP/ESP or R12/R12D, which must use SIB encoding.
* SSIIIBBB, ss = 00, index = 100b ( no index ), base = 100b ( ESP ) */
CodeInfo->sib = 0x24;
} else if ( base_reg == 5 && mod_field == MOD_00 ) {
/* 5 is [E|R]BP or R13[D]. Needs displacement */
// 5 is also RIP register but doesn't need MOD_01
if (base != T_RIP) //added by habran
mod_field = MOD_01; /* byte size displacement */
}
#if AMD64_SUPPORT
/* v2.02 */
//rex = ( bit3_base << 2 ); /* set REX_R */
rex = bit3_base; /* set REX_R */
#endif
}
#if AMD64_SUPPORT
DebugMsg1(("set_rm_sib, indirect with base, mod_field=%X, rm_field=%X, rex=%X\n", mod_field, rm_field, rex ));
#else
DebugMsg1(("set_rm_sib, indirect with base, rm_field=%X\n", rm_field ));
#endif
seg_override( CodeInfo, base, sym, FALSE );
} else if( ( index != EMPTY ) && ( base == EMPTY ) ) {
idx_reg = GetRegNo( index );
#if AVXSUPP
CodeInfo->indextype = GetValueSp( index );
#endif
#if AMD64_SUPPORT
bit3_idx = idx_reg >> 3;
idx_reg &= BIT_012;
#endif
/* mod field is 00 */
mod_field = MOD_00;
/* s-i-b is present ( r/m = 100b ) */
rm_field = RM_SIB;
/* scale factor, index, base ( 0x05 => no base reg ) */
CodeInfo->sib = ( ss | ( idx_reg << 3 ) | 0x05 );
#if AMD64_SUPPORT
rex = (bit3_idx << 1); /* set REX_X */
#endif
/* default is DS:[], DS: segment override is not needed */
seg_override( CodeInfo, T_DS, sym, FALSE );
} else {
/* base != EMPTY && index != EMPTY */
base_reg = GetRegNo( base );
idx_reg = GetRegNo( index );
if ( base == T_RIP)
base_reg = 0x5;
#if AMD64_SUPPORT
bit3_base = base_reg >> 3;
bit3_idx = idx_reg >> 3;
base_reg &= BIT_012;
idx_reg &= BIT_012;
#endif
if ( ( GetSflagsSp( base ) & GetSflagsSp( index ) & SFR_SIZMSK ) == 0 ) {
#if AVXSUPP
CodeInfo->indextype = GetValueSp( index );
if (CodeInfo->indextype == OP_XMM || CodeInfo->indextype == OP_YMM || CodeInfo->indextype == OP_ZMM){
;
}
else
#endif
return( EmitError( CANNOT_MIX_16_AND_32_BIT_REGISTERS ) );
}
switch( index ) {
case T_BX:
case T_BP:
if( ( temp = comp_mem16( index, base ) ) == ERROR )
return( ERROR );
rm_field = temp;
seg_override( CodeInfo, index, sym, FALSE );
break;
case T_SI:
case T_DI:
if( ( temp = comp_mem16( base, index ) ) == ERROR )
return( ERROR );
rm_field = temp;
seg_override( CodeInfo, base, sym, FALSE );
break;
#if AMD64_SUPPORT
case T_RSP:
case T_RIP: //added by habran
#endif
case T_ESP:
//EmitErr( CANNOT_BE_USED_AS_INDEX_REGISTER, ??? );
return( EmitError( INVALID_USE_OF_REGISTER ) );
default:
if( base_reg == 5 ) { /* v2.03: EBP/RBP/R13/R13D? */
if( mod_field == MOD_00 ) {
if (base != T_RIP) mod_field = MOD_01; //ADDED BY HABRAN
}
}
/* s-i-b is present ( r/m = 100b ) */
rm_field |= RM_SIB;
CodeInfo->sib = ( ss | idx_reg << 3 | base_reg );
#if AMD64_SUPPORT
rex = (bit3_idx << 1) + (bit3_base); /* set REX_X + REX_B */
#endif
seg_override( CodeInfo, base, sym, FALSE );
} /* end switch(index) */
#if AMD64_SUPPORT
DebugMsg1(("set_rm_sib, indirect, base+index: mod_field=%X, rm_field=%X, rex=%X\n", mod_field, rm_field, rex ));
#else
DebugMsg1(("set_rm_sib, indirect, base+index: rm_field=%X\n", rm_field ));
#endif
}
if( CurrOpnd == OPND2 ) {
/* shift the register field to left by 3 bit */
if ( base == T_RIP ) //added by habran
mod_field &= BIT_012;
CodeInfo->rm_byte = mod_field | ( rm_field << 3 ) | ( CodeInfo->rm_byte & BIT_012 );
#if AMD64_SUPPORT
/* v2.02: exchange B and R, keep X */
//CodeInfo->prefix.rex |= (rex >> 2 );
CodeInfo->prefix.rex |= ( ( rex >> 2 ) | ( rex & REX_X ) | (( rex & 1) << 2 ) );
#endif
} else if( CurrOpnd == OPND1 ) {
if ( base == T_RIP ) //added by habran
mod_field &= BIT_012;
CodeInfo->rm_byte = mod_field | rm_field;
#if AMD64_SUPPORT
CodeInfo->prefix.rex |= rex;
#endif
}
return( NOT_ERROR );
}
/* override handling
* called by
* - process_branch()
* - idata_fixup()
* - memory_operand() (CodeInfo != NULL)
* - data_item()
* 1. If it's a segment register, set CodeInfo->prefix.RegOverride.
* 2. Set global variable SegOverride if it's a SEG/GRP symbol
* (or whatever is assumed for the segment register) */
ret_code segm_override( const struct expr *opndx, struct code_info *CodeInfo )
/****************************************************************************/
{
struct asym *sym;
if( opndx->override != NULL ) {
if( opndx->override->token == T_REG ) {
int temp = GetRegNo( opndx->override->tokval );
if ( SegAssumeTable[temp].error ) {
DebugMsg(("segm_override: assume error, reg=%u\n", temp ));
return( EmitError( USE_OF_REGISTER_ASSUMED_TO_ERROR ) );
}
#if AMD64_SUPPORT
/* ES,CS,SS and DS overrides are invalid in 64-bit */
/* UASM 2.48 Allow movabs encoding of any segment register in 64bit */
if ( CodeInfo && CodeInfo->Ofssize == USE64 && temp < ASSUME_FS && CodeInfo->token != T_MOVABS) {
return( EmitError( ILLEGAL_USE_OF_SEGMENT_REGISTER ) );
}
#endif
sym = GetOverrideAssume( temp );
if ( CodeInfo ) {
/* hack: save the previous reg override value (needed for CMPS) */
LastRegOverride = CodeInfo->prefix.RegOverride;
CodeInfo->prefix.RegOverride = temp;
}
} else {
sym = SymSearch( opndx->override->string_ptr );
}
if ( sym && ( sym->state == SYM_GRP || sym->state == SYM_SEG ))
SegOverride = sym;
}
return( NOT_ERROR );
}
/* UASM 2.56 - improved check if an immediate fits in 32bits */
static char fits32(int_64 val) {
uint64_t top = ((uint64_t)val) >> 32;
uint64_t top2 = ((uint64_t)val) >> 31;
if ((top == 0 || top2 == 0x00000001ffffffff) && val < 0x80000000)
return TRUE;
return FALSE;
}
/* get an immediate operand without a fixup.
* output:
* - ERROR: error
* - NOT_ERROR: ok,
* CodeInfo->opnd_type[CurrOpnd] = OP_Ix
* CodeInfo->data[CurrOpnd] = value
* CodeInfo->prefix.opsiz
* CodeInfo->iswide */
static ret_code idata_nofixup( struct code_info *CodeInfo, unsigned CurrOpnd, const struct expr *opndx )
/******************************************************************************************************/
{
enum operand_type op_type;
int_32 value;
int size;
DebugMsg1(("idata_nofixup( CurrOpnd=%u ) enter [opnd kind=%u mem_type=%Xh value=%" I64_SPEC "X]\n", CurrOpnd, opndx->kind, opndx->mem_type, opndx->value64));
/* jmp/call/jxx/loop/jcxz/jecxz? */
if( IS_ANY_BRANCH( CodeInfo->token ) ) {
return( process_branch( CodeInfo, CurrOpnd, opndx ) );
}
value = opndx->value;
CodeInfo->opnd[CurrOpnd].data32l = value;
/* 64bit immediates are restricted to MOV <reg>,<imm64> */
if (((CodeInfo->opnd[OPND1].type & OP_R64) && CodeInfo->token != T_MOV && fits32(opndx->value64) == 0) || (opndx->hlvalue != 0))
{
/* magnitude > 64 bits? */
DebugMsg1(("idata_nofixup: error, hlvalue=%" I64_SPEC "X\n", opndx->hlvalue));
return(EmitConstError(opndx));
}
/* v2.03: handle QWORD type coercion here as well!
* This change also reveals an old problem in the expression evaluator:
* the mem_type field is set whenever a (simple) type token is found.
* It should be set ONLY when the type is used in conjuction with the
* PTR operator!
* current workaround: query the 'explicit' flag.
*/
/* use long format of MOV for 64-bit if value won't fit in a signed DWORD */
if ( CodeInfo->Ofssize == USE64 && CodeInfo->token == T_MOV && CurrOpnd == OPND2 &&
( CodeInfo->opnd[OPND1].type & OP_R64 ) &&
( opndx->value64 > H_LONG_MAX || opndx->value64 < H_LONG_MIN ||
(opndx->explicit && ( opndx->mem_type == MT_QWORD || opndx->mem_type == MT_SQWORD ) ) ) )
{
CodeInfo->opnd[CurrOpnd].type = OP_I64;
CodeInfo->opnd[CurrOpnd].data32h = opndx->hvalue;
return( NOT_ERROR );
}
/* v2.06: code simplified.
* to be fixed: the "wide" bit should not be set here!
* Problem: the "wide" bit isn't set in memory_operand(),
* probably because of the instructions which accept both
* signed and unsigned arguments (ADD, CMP, ... ). */
if ( opndx->explicit )
{
/* size coercion for immediate value */
CodeInfo->const_size_fixed = TRUE;
size = SizeFromMemtype( opndx->mem_type,
opndx->Ofssize,
opndx->type );
/* don't check if size and value are compatible. */
switch ( size )
{
case 1: op_type = OP_I8; break;
case 2: op_type = OP_I16; break;
case 4: op_type = OP_I32; break;
default:
DebugMsg1(("idata_nofixup: invalid size %d for immediate operand\n", size ));
return( EmitError( INVALID_INSTRUCTION_OPERANDS ) );
}
}
else
{
/* use true signed valu es for BYTE only! */
if ( (int_8)value == value )
//if (value >= -128 && value < 256)
op_type = OP_I8;
else if( value <= USHRT_MAX && value >= 0L - USHRT_MAX )
op_type = OP_I16;
else
op_type = OP_I32;
}
switch ( CodeInfo->token )
{
case T_PUSH:
if ( opndx->explicit == FALSE ) {
if ( CodeInfo->Ofssize > USE16 && op_type == OP_I16 )
op_type = OP_I32;
}
if ( op_type == OP_I16 )
CodeInfo->prefix.opsiz = OPSIZE16( CodeInfo );
else if ( op_type == OP_I32 )
CodeInfo->prefix.opsiz = OPSIZE32( CodeInfo );
break;
case T_PUSHW:
if ( op_type != OP_I32 ) {
op_type = OP_I16;
if( (int_8)value == (int_16)value ) {
op_type = OP_I8;
}
}
break;
case T_PUSHD:
if ( op_type == OP_I16 )
op_type = OP_I32;
break;
}
/* v2.11: set the wide-bit if a mem_type size of > BYTE is set???
* actually, it should only be set if immediate is second operand
* ( and first operand is a memory ref with a size > 1 ) */
if (CurrOpnd == OPND2)
{
if ( CodeInfo->mem_type == MT_ZMMWORD )
CodeInfo->iswide = 1;
else
if ( !(CodeInfo->mem_type & MT_SPECIAL) && ( CodeInfo->mem_type & MT_SIZE_MASK ) )
CodeInfo->iswide = 1;
}
CodeInfo->opnd[CurrOpnd].type = op_type;
return( NOT_ERROR );
}
/* get an immediate operand with a fixup.
* output:
* - ERROR: error
* - NOT_ERROR: ok,
* CodeInfo->opnd_type[CurrOpnd] = OP_Ix
* CodeInfo->data[CurrOpnd] = value
* CodeInfo->InsFixup[CurrOpnd] = fixup
* CodeInfo->mem_type
* CodeInfo->prefix.opsiz
* to be fixed: don't modify CodeInfo->mem_type here! */
ret_code idata_fixup( struct code_info *CodeInfo, unsigned CurrOpnd, struct expr *opndx )
/***************************************************************************************/
{
//struct fixup *fixup;
enum fixup_types fixup_type;
enum fixup_options fixup_option = OPTJ_NONE;
int size;
uint_8 Ofssize; /* 1=32bit, 0=16bit offset for fixup */
DebugMsg1(("idata_fixup( CurrOpnd=%u ) enter [opndx.kind=%u mem_type=%Xh, CodeInfo.mem_type=%Xh]\n", CurrOpnd, opndx->kind, opndx->mem_type, CodeInfo->mem_type));
/* jmp/call/jcc/loopcc/jxcxz? */
if( IS_ANY_BRANCH( CodeInfo->token ) ) {
return( process_branch( CodeInfo, CurrOpnd, opndx ) );
}
CodeInfo->opnd[CurrOpnd].data32l = opndx->value;
if ( opndx->Ofssize != USE_EMPTY ) {
Ofssize = opndx->Ofssize;
} else if( ( opndx->sym->state == SYM_SEG )
|| ( opndx->sym->state == SYM_GRP )
|| ( opndx->instr == T_SEG ) ) {
Ofssize = USE16;
} else if( opndx->is_abs ) { /* an (external) absolute symbol? */
Ofssize = USE16;
} else {
Ofssize = GetSymOfssize( opndx->sym );
}
if( opndx->instr == T_SHORT ) {
/* short works for branch instructions only */
return( EmitErr( INVALID_INSTRUCTION_OPERANDS ) );
}
/* the code below should be rewritten.
* - an address operator ( OFFSET, LROFFSET, IMAGEREL, SECTIONREL,
* LOW, HIGH, LOWWORD, HIGHWORD, LOW32, HIGH32, SEG ) should not
* force a magnitude, but may set a minimal magnitude - and the
* fixup type, of course.
* - check if Codeinfo->mem_type really has to be set here!
*/
/* v2.06: added */
/* v2.10: modified */
//if ( opndx->explicit ) {
if ( opndx->explicit && !opndx->is_abs ) {
CodeInfo->const_size_fixed = TRUE;
if ( CodeInfo->mem_type == MT_EMPTY )
CodeInfo->mem_type = opndx->mem_type;
}
/* v2.03: don't ignore a "NEAR32 ptr" qualifier */
//if ( CodeInfo->mem_type == MT_EMPTY && CurrOpnd > OPND1 ) {
if ( CodeInfo->mem_type == MT_EMPTY && CurrOpnd > OPND1 && opndx->Ofssize == USE_EMPTY ) {
size = OperandSize( CodeInfo->opnd[OPND1].type, CodeInfo );
/* may be a forward reference, so wait till pass 2 */
if( Parse_Pass > PASS_1 && opndx->instr != EMPTY ) {
switch ( opndx->instr ) {
case T_SEG: /* v2.04a: added */
if( size && (size < 2 ) ) {
return( EmitErr( OPERANDS_MUST_BE_THE_SAME_SIZE, size, 2 ) );
}
break;
case T_OFFSET:
case T_LROFFSET:
#if IMAGERELSUPP
case T_IMAGEREL:
#endif
#if SECTIONRELSUPP
case T_SECTIONREL:
#endif
if( size && (size < 2 || ( Ofssize && size < 4 ))) {
return( EmitErr( OPERANDS_MUST_BE_THE_SAME_SIZE, size, ( 2 << Ofssize ) ) );
}
}
}
switch ( size ) {
case 1:
/* v2.05: if () added */
if ( opndx->is_abs || opndx->instr == T_LOW || opndx->instr == T_HIGH )
CodeInfo->mem_type = MT_BYTE;
break;
case 2:
/* v2.05: if () added */
if ( opndx->is_abs ||
CodeInfo->Ofssize == USE16 ||
opndx->instr == T_LOWWORD ||
opndx->instr == T_HIGHWORD )
CodeInfo->mem_type = MT_WORD;
break;
case 4:
CodeInfo->mem_type = MT_DWORD;
break;
#if AMD64_SUPPORT
case 8:
/* v2.05: it's questionable if size 8 is a good assumption for an
* immediate constant. It's valid for MOV <reg>, <imm> only.
*/
//case 8: CodeInfo->mem_type = MT_QWORD;break;
/* v2.05a: added */
if ( Ofssize == USE64 ) {
if ( CodeInfo->token == T_MOV &&
( CodeInfo->opnd[OPND1].type & OP_R64 ) )
CodeInfo->mem_type = MT_QWORD;
else if ( opndx->instr == T_LOW32 || opndx->instr == T_HIGH32 )
/* v2.10:added; LOW32/HIGH32 in expreval.c won't set mem_type anymore. */
CodeInfo->mem_type = MT_DWORD;
}
break;
#endif
}
}
if ( CodeInfo->mem_type == MT_EMPTY ) {
if( opndx->is_abs ) {
//if( opndx->mem_type != MT_EMPTY && opndx->mem_type != MT_ABS ) {
if( opndx->mem_type != MT_EMPTY ) {
CodeInfo->mem_type = opndx->mem_type;
} else if ( CodeInfo->token == T_PUSHW ) { /* v2.10: special handling PUSHW */
CodeInfo->mem_type = MT_WORD;
} else {
CodeInfo->mem_type = ( IS_OPER_32( CodeInfo ) ? MT_DWORD : MT_WORD );
}
} else {
switch ( CodeInfo->token ) {
case T_PUSHW:
case T_PUSHD:
case T_PUSH:
/* for forward reference, assume BYTE */
/* v2.02: don't assume BYTE if it is SEG/GRP */
//if ( opndx->mem_type == MT_EMPTY ) {
/* v2.07: added cases IMAGEREL and SECTIONREL */
if ( opndx->mem_type == MT_EMPTY ) {
switch( opndx->instr ) {
case EMPTY:
case T_LOW:
case T_HIGH:
opndx->mem_type = MT_BYTE;
break;
case T_LOW32: /* v2.10: added - low32_op() doesn't set mem_type anymore. */
#if IMAGERELSUPP
case T_IMAGEREL:
#endif
#if SECTIONRELSUPP
case T_SECTIONREL:
#endif
opndx->mem_type = MT_DWORD;
break;
};
}
/* default: push offset only */
/* for PUSH + undefined symbol, assume BYTE */
if ( opndx->mem_type == MT_FAR && ( opndx->explicit == FALSE ) )
opndx->mem_type = MT_NEAR;
/* v2.04: curly brackets added */
if ( CodeInfo->token == T_PUSHW ) {
if ( SizeFromMemtype( opndx->mem_type, Ofssize, opndx->type ) < 2 )
opndx->mem_type = MT_WORD;
} else if ( CodeInfo->token == T_PUSHD ) {
if ( SizeFromMemtype( opndx->mem_type, Ofssize, opndx->type ) < 4 )
opndx->mem_type = MT_DWORD;
}
break;
}
/* if a WORD size is given, don't override it with */
/* anything what might look better at first glance */
if( opndx->mem_type != MT_EMPTY )
CodeInfo->mem_type = opndx->mem_type;
/* v2.04: assume BYTE size if symbol is undefined */
else if ( opndx->sym->state == SYM_UNDEFINED ) {
CodeInfo->mem_type = MT_BYTE;
fixup_option = OPTJ_PUSH;
} else
#if AMD64_SUPPORT
/* v2.06d: changed */
CodeInfo->mem_type = ( Ofssize == USE64 ? MT_QWORD : Ofssize == USE32 ? MT_DWORD : MT_WORD );
#else
CodeInfo->mem_type = ( Ofssize > USE16 ? MT_DWORD : MT_WORD );
#endif
}
}
size = SizeFromMemtype( CodeInfo->mem_type, Ofssize, NULL );
switch( size ) {
case 1:
CodeInfo->opnd[CurrOpnd].type = OP_I8;
CodeInfo->prefix.opsiz = FALSE; /* v2.10: reset opsize is not really a good idea - might have been set by previous operand */
break;
case 2: CodeInfo->opnd[CurrOpnd].type = OP_I16; CodeInfo->prefix.opsiz = OPSIZE16( CodeInfo ); break;
case 4: CodeInfo->opnd[CurrOpnd].type = OP_I32; CodeInfo->prefix.opsiz = OPSIZE32( CodeInfo ); break;
#if AMD64_SUPPORT
case 8:
/* v2.05: do only assume size 8 if the constant won't fit in 4 bytes. */
if ( opndx->value64 > H_LONG_MAX || opndx->value64 < H_LONG_MIN ||
(opndx->explicit && ( opndx->mem_type & MT_SIZE_MASK ) == 7 ) ) {
CodeInfo->opnd[CurrOpnd].type = OP_I64;
CodeInfo->opnd[CurrOpnd].data32h = opndx->hvalue;
//} else if ( Ofssize == USE64 ) { /* v2.11: assume 64-bit only for OFFSET or MOV r64, xxx */
} else if ( Ofssize == USE64 && ( opndx->instr == T_OFFSET || ( CodeInfo->token == T_MOV && ( CodeInfo->opnd[OPND1].type & OP_R64 ) ) ) ) {
/* v2.06d: in 64-bit, ALWAYS set OP_I64, so "mov m64, ofs" will fail,
* This was accepted in v2.05-v2.06c)
*/
CodeInfo->opnd[CurrOpnd].type = OP_I64;
CodeInfo->opnd[CurrOpnd].data32h = opndx->hvalue;
} else {
CodeInfo->opnd[CurrOpnd].type = OP_I32;
}
CodeInfo->prefix.opsiz = OPSIZE32( CodeInfo );
break;
#endif
#ifdef DEBUG_OUT
default:
DebugMsg1(("idata_fixup, unexpected size %u\n", size ));
/**/myassert( 0 );
#endif
}
/* set fixup_type */
if( opndx->instr == T_SEG ) {
fixup_type = FIX_SEG;
} else if( CodeInfo->mem_type == MT_BYTE ) {
DebugMsg1(("idata_fixup, mem_type=BYTE\n" ));
if ( opndx->instr == T_HIGH ) {
DebugMsg1(("idata_fixup, FIX_HIBYTE\n" ));
fixup_type = FIX_HIBYTE;
} else {
DebugMsg1(("idata_fixup, FIX_OFF8\n" ));
fixup_type = FIX_OFF8;
}
#if 0
} else if( CodeInfo->mem_type == MT_FAR ) {
/* v2.04: to be tested. this code is most likely obsolete.
* There's never a PTR16|PTR32 fixup here. Far JMP/CALL are handled
* elsewhere, and data items also.
*/
/* temporary */
printf("idata_fixup: MT_FAR occured at %s:%" I32_SPEC "u\n", CurrFName[ASM], LineNumber );
fixup_type = ( Ofssize ) ? FIX_PTR32 : FIX_PTR16;
CodeInfo->isfar = TRUE; /* needed for mark_fixupp() */
if ( opndx->Ofssize != USE_EMPTY )
CodeInfo->Ofssize = opndx->Ofssize;
#endif
} else if( IS_OPER_32( CodeInfo ) ) {
#if AMD64_SUPPORT
/* v2.06: changed */
//if ( Ofssize == USE64 && CodeInfo->mem_type == MT_QWORD )
/* v2.10: changed */
//if ( CodeInfo->opnd[CurrOpnd].type == OP_I64 )
if ( CodeInfo->opnd[CurrOpnd].type == OP_I64 && ( opndx->instr == EMPTY || opndx->instr == T_OFFSET ) )
fixup_type = FIX_OFF64;
else
#endif
/* v2.04: changed, no longer depends on OfsSize */
/* v2.05a: changed, so size==8 won't get a FIX_OFF16 type */
//if ( size == 4 )
if ( size >= 4 && opndx->instr != T_LOWWORD ) {
/* v2.06: added branch for PTR16 fixup.
* it's only done if type coercion is FAR (Masm-compat)
*/
if ( opndx->explicit && Ofssize == USE16 && opndx->mem_type == MT_FAR )
fixup_type = FIX_PTR16;
else
fixup_type = FIX_OFF32;
} else
fixup_type = FIX_OFF16;
} else {
/* v2.04: changed, no longer depends on OfsSize */
//if ( CodeInfo->mem_type == MT_DWORD ) {
/* fixme !!!! warning
* operand size is 16bit
* but fixup is 32-bit */
// fixup_type = FIX_OFF32;
//} else
fixup_type = FIX_OFF16;
}
/* v2.04: 'if' added, don't set W bit if size == 1
* code example:
* extern x:byte
* or al,x
* v2.11: set wide bit only if immediate is second operand.
* and first operand is a memory reference with size > 1
*/
//if ( size != 1 )
if ( CurrOpnd == OPND2 && size != 1 )
CodeInfo->iswide = 1;
segm_override( opndx, NULL ); /* set SegOverride global var */
/* set frame type in variables Frame_Type and Frame_Datum for fixup creation */
if ( ModuleInfo.offsettype == OT_SEGMENT &&
( opndx->instr == T_OFFSET || opndx->instr == T_SEG ))
set_frame2( opndx->sym );
else
set_frame( opndx->sym );
//DebugMsg1(("idata_fixup: calling CreateFixup(%s, %u)\n", opndx->sym->name, fixup_type ));
CodeInfo->opnd[CurrOpnd].InsFixup = CreateFixup( opndx->sym, fixup_type, fixup_option );
if ( opndx->instr == T_LROFFSET )
CodeInfo->opnd[CurrOpnd].InsFixup->loader_resolved = TRUE;
#if IMAGERELSUPP
if ( opndx->instr == T_IMAGEREL && fixup_type == FIX_OFF32 )
CodeInfo->opnd[CurrOpnd].InsFixup->type = FIX_OFF32_IMGREL;
#endif
#if SECTIONRELSUPP
if ( opndx->instr == T_SECTIONREL && fixup_type == FIX_OFF32 )
CodeInfo->opnd[CurrOpnd].InsFixup->type = FIX_OFF32_SECREL;
#endif
DebugMsg1(("idata_fixup exit [CodeInfo.mem_type=%Xh Ofssize=%u opsiz=%u fixup.type=%u fixup.frame=%d]\n",
CodeInfo->mem_type, CodeInfo->Ofssize, CodeInfo->prefix.opsiz,
CodeInfo->opnd[CurrOpnd].InsFixup->type, CodeInfo->opnd[CurrOpnd].InsFixup->frame_type ));
return( NOT_ERROR );
}
/* convert MT_PTR to MT_WORD, MT_DWORD, MT_FWORD, MT_QWORD.
* MT_PTR cannot be set explicitely (by the PTR operator),
* so this value must come from a label or a structure field.
* (above comment is most likely plain wrong, see 'PF16 ptr [reg]'!
* This code needs cleanup! */
static void SetPtrMemtype( struct code_info *CodeInfo, struct expr *opndx )
/*************************************************************************/
{
struct asym *sym = opndx->sym;
int size = 0;
if ( opndx->mbr ) /* the mbr field has higher priority */
sym = opndx->mbr;
/* v2.10: the "explicit" condition is now handled FIRST */
#if 1 /* v2.0: handle PF16 ptr [ebx], which didn't work in v1.96 */
if ( opndx->explicit && opndx->type ) {
size = opndx->type->total_size;
CodeInfo->isfar = opndx->type->isfar;
} else
#endif
if ( sym ) {
if ( sym->type ) {
size = sym->type->total_size;
CodeInfo->isfar = sym->type->isfar;
/* there's an ambiguity with pointers of size DWORD,
since they can be either NEAR32 or FAR16 */
if ( size == 4 && sym->type->Ofssize != CodeInfo->Ofssize )
opndx->Ofssize = sym->type->Ofssize;
} else if ( sym->mem_type == MT_PTR ) {
size = SizeFromMemtype( sym->isfar ? MT_FAR : MT_NEAR, sym->Ofssize, NULL );
CodeInfo->isfar = sym->isfar;
} else {
if ( sym->isarray )
size = sym->total_size / sym->total_length;
else
size = sym->total_size;
}
} else {
if ( SIZE_DATAPTR & ( 1 << ModuleInfo.model ) ) {
DebugMsg1(("SetPtrMemtype: model with FAR data pointers\n" ));
size = 2;
}
size += (2 << ModuleInfo.defOfssize );
}
if ( size )
MemtypeFromSize( size, &opndx->mem_type );
DebugMsg1(("SetPtrMemtype: size=%u, new memtype=0x%x\n", size, opndx->mem_type ));
}
/*
* set fields in CodeInfo:
* - mem_type
* - prefix.opsiz
* - prefix.rex REX_W
* called by memory_operand() */
static void Set_Memtype( struct code_info *CodeInfo, enum memtype mem_type )
/**************************************************************************/
{
if( CodeInfo->token == T_LEA )
return;
/* v2.05: changed. Set "data" types only. */
if( mem_type == MT_EMPTY || mem_type == MT_TYPE ||
mem_type == MT_NEAR || mem_type == MT_FAR )
return;
CodeInfo->mem_type = mem_type;
if( CodeInfo->Ofssize > USE16 ) {
/* if we are in use32 mode, we have to add OPSIZ prefix for
* most of the 386 instructions when operand has type WORD.
* Exceptions ( MOVSX and MOVZX ) are handled in check_size().
*/
if ( IS_MEM_TYPE( mem_type, WORD ) )
CodeInfo->prefix.opsiz = TRUE;
#if AMD64_SUPPORT
/*
* set rex Wide bit if a QWORD operand is found (not for FPU/MMX/SSE instr).
* This looks pretty hackish now and is to be cleaned!
* v2.01: also had issues with SSE2 MOVSD/CMPSD, now fixed!
*/
/* v2.06: with AVX, SSE tokens may exist twice, one
* for "legacy", the other for VEX encoding!
*/
else if ( IS_MEMTYPE_SIZ( mem_type, sizeof( uint_64 ) ) ) {
switch( CodeInfo->token ) {
case T_PUSH: /* for PUSH/POP, REX_W isn't needed (no 32-bit variants in 64-bit mode) */
case T_POP:
case T_CMPXCHG8B:
#if VMXSUPP
case T_VMPTRLD:
case T_VMPTRST:
case T_VMCLEAR:
case T_VMXON:
#endif
break;
default:
/* don't set REX for opcodes that accept memory operands
* of any size.
*/
if ( opnd_clstab[CodeInfo->pinstr->opclsidx].opnd_type[OPND1] == OP_M_ANY ) {
//printf( "Set_Memtype: OP_M_ANY detected, file=%s, instr=%s\n", CurrFName[ASM], GetResWName( CodeInfo->token, NULL ) );
break;
}
/* don't set REX for FPU opcodes */
if ( CodeInfo->pinstr->cpu & P_FPU_MASK )
break;
/* don't set REX for - most - MMX/SSE opcodes */
if ( CodeInfo->pinstr->cpu & P_EXT_MASK ) {
switch ( CodeInfo->token ) {
/* [V]CMPSD and [V]MOVSD are also candidates,
* but currently they are handled in HandleStringInstructions()
*/
case T_CVTSI2SD: /* v2.06: added */
case T_CVTSI2SS: /* v2.06: added */
case T_PEXTRQ: /* v2.06: added */
case T_PINSRQ: /* v2.06: added */
case T_MOVD:
#if AVXSUPP
case T_VCVTSI2SD:
case T_VCVTSI2SS:
case T_VPEXTRQ:
case T_VPINSRQ:
case T_VMOVD:
#endif
CodeInfo->prefix.rex |= REX_W;
break;
default:
break;
}
}
else
CodeInfo->prefix.rex |= REX_W;
}
}
#endif
/* v2.05: IS_MEM_TYPE() doesn't work with MT_REALx */
//} else if( CodeInfo->Ofssize == USE16 && ( IS_MEM_TYPE( mem_type, DWORD ) ) ) {
} else {
if( IS_MEMTYPE_SIZ( mem_type, sizeof(uint_32) ) ) {
/* in 16bit mode, a DWORD memory access usually requires an OPSIZ
* prefix. A few instructions, which access m16:16 operands,
* are exceptions.
*/
switch( CodeInfo->token ) {
case T_LDS:
case T_LES:
case T_LFS:
case T_LGS:
case T_LSS:
case T_CALL: /* v2.0: added */
case T_JMP: /* v2.0: added */
/* in these cases, opsize does NOT need to be changed */
break;
default:
CodeInfo->prefix.opsiz = TRUE;
break;
}
}
#if AMD64_SUPPORT
/* v2.06: added because in v2.05, 64-bit memory operands were
* accepted in 16-bit code
*/
else if ( IS_MEMTYPE_SIZ( mem_type, sizeof(uint_64) ) ) {
if ( opnd_clstab[CodeInfo->pinstr->opclsidx].opnd_type[OPND1] == OP_M_ANY ) {
//printf( "Set_Memtype: OP_M_ANY detected, file=%s, instr=%s\n", CurrFName[ASM], GetResWName( CodeInfo->token, NULL ) );
} else if ( CodeInfo->pinstr->cpu & ( P_FPU_MASK | P_EXT_MASK ) ) {
;
} else if ( CodeInfo->token != T_CMPXCHG8B )
/* setting REX.W will cause an error in codegen */
CodeInfo->prefix.rex |= REX_W;
}
#endif
}
return;
}
/*
* process direct or indirect memory operand
* in: opndx=operand to process
* in: CurrOpnd=no of operand (0=first operand, 1=second operand)
* out: CodeInfo->data[]
* out: CodeInfo->opnd_type[] */
static ret_code memory_operand( struct code_info *CodeInfo, unsigned CurrOpnd, struct expr *opndx, bool with_fixup )
/******************************************************************************************************************/
{
char ss = SCALE_FACTOR_1;
int index;
int base;
int temp;
bool swapped = FALSE;
int j;
struct asym *sym;
uint_8 Ofssize;
enum fixup_types fixup_type;
/* v211: use full 64-bit value */
CodeInfo->opnd[CurrOpnd].data64 = opndx->value64;
CodeInfo->opnd[CurrOpnd].type = OP_M;
sym = opndx->sym;
segm_override( opndx, CodeInfo );
/* change pointer types ( MT_NEAR, MT_FAR, MT_PTR */
/* v2.04a: should not be called if OFFSET was used */
if ( opndx->mem_type == MT_PTR )
SetPtrMemtype( CodeInfo, opndx );
else if ( ( opndx->mem_type & MT_SPECIAL_MASK ) == MT_ADDRESS )
{
int size;
if ( opndx->Ofssize == USE_EMPTY && sym )
opndx->Ofssize = GetSymOfssize( sym );
size = SizeFromMemtype( opndx->mem_type, opndx->Ofssize, opndx->type );
MemtypeFromSize( size, &opndx->mem_type );
}
Set_Memtype( CodeInfo, opndx->mem_type );
if( opndx->mbr != NULL )
{
/* if the struct field is just another struct, use it's total size
* to set CodeInfo->mem_type. */
if ( opndx->mbr->mem_type == MT_TYPE && opndx->mem_type == MT_EMPTY )
{
enum memtype mem_type;
if (CodeInfo->token == T_VMOVSS)
{
// MemtypeFromSize returns OP_M128
mem_type = MT_DWORD; //but we need MT_DWORD
Set_Memtype(CodeInfo, mem_type);
}
else
{
if (MemtypeFromSize(opndx->mbr->total_size, &mem_type) == NOT_ERROR)
Set_Memtype(CodeInfo, mem_type);
}
}
if ( opndx->mbr->state == SYM_UNDEFINED )
CodeInfo->undef_sym = TRUE;
}
/* instruction-specific handling */
switch ( CodeInfo->token )
{
case T_JMP:
case T_CALL:
/* the 2 branch instructions are peculiar because they
* will work with an unsized label.
*/
/* v1.95: convert MT_NEAR/MT_FAR and display error if no type.
* For memory operands, expressions of type MT_NEAR/MT_FAR are
* call [bx+<code_label>]
*/
if ( CodeInfo->mem_type == MT_EMPTY )
{
/* with -Zm, no size needed for indirect CALL/JMP */
if ( ModuleInfo.m510 == FALSE &&
( Parse_Pass > PASS_1 && opndx->sym == NULL ) )
{
return( EmitError( INSTRUCTION_OPERAND_MUST_HAVE_SIZE ) );
}
opndx->mem_type = (CodeInfo->Ofssize == USE64) ? MT_QWORD : (CodeInfo->Ofssize == USE32) ? MT_DWORD : MT_WORD;
Set_Memtype( CodeInfo, opndx->mem_type );
}
j = SizeFromMemtype( CodeInfo->mem_type, CodeInfo->Ofssize, NULL );
if ( ( j == 1 || j > 6 ) && ( CodeInfo->Ofssize != USE64 ))
{
/* CALL/JMP possible for WORD/DWORD/FWORD memory operands only */
return( EmitError( INVALID_OPERAND_SIZE ) );
}
if( opndx->mem_type == MT_FAR || CodeInfo->mem_type == MT_FWORD ||
( CodeInfo->mem_type == MT_TBYTE && CodeInfo->Ofssize == USE64 ) ||
( CodeInfo->mem_type == MT_DWORD &&
(( CodeInfo->Ofssize == USE16 && opndx->Ofssize != USE32 ) ||
( CodeInfo->Ofssize == USE32 && opndx->Ofssize == USE16 ))))
{
CodeInfo->isfar = TRUE;
}
break;
}
if ( ( CodeInfo->mem_type & MT_SPECIAL) == 0 )
{
if ((CodeInfo->mem_type & 0x3f) == MT_YMMWORD)
CodeInfo->opnd[CurrOpnd].type = OP_M256;
else if ((CodeInfo->mem_type & 0x3f) == MT_ZMMWORD)
CodeInfo->opnd[CurrOpnd].type = OP_M512;
else
{
switch (CodeInfo->mem_type & MT_SIZE_MASK)
{
/* size is encoded 0-based */
case MT_BYTE: CodeInfo->opnd[CurrOpnd].type = OP_M08; break;
case MT_WORD: CodeInfo->opnd[CurrOpnd].type = OP_M16; break;
case MT_DWORD: CodeInfo->opnd[CurrOpnd].type = OP_M32; break;
case MT_FWORD: CodeInfo->opnd[CurrOpnd].type = OP_M48; break;
case MT_QWORD: CodeInfo->opnd[CurrOpnd].type = OP_M64; break;
case MT_TBYTE: CodeInfo->opnd[CurrOpnd].type = OP_M80; break;
case MT_OWORD: CodeInfo->opnd[CurrOpnd].type = OP_M128; break;
}
}
}
else if ( CodeInfo->mem_type == MT_EMPTY )
{
/* v2.05: added */
switch ( CodeInfo->token )
{
case T_INC:
case T_DEC:
/* Uasm v1.94-v2.04 accepted unsized operand for INC/DEC */
if ( opndx->sym == NULL )
return( EmitError( INSTRUCTION_OPERAND_MUST_HAVE_SIZE ) );
break;
case T_PUSH:
case T_POP:
if ( opndx->mem_type == MT_TYPE )
return( EmitError( INVALID_INSTRUCTION_OPERANDS ) );
break;
}
}
base = ( opndx->base_reg ? opndx->base_reg->tokval : EMPTY );
index = ( opndx->idx_reg ? opndx->idx_reg->tokval : EMPTY );
/* swap registers if base is AVX register, v2.38 */
if (GetValueSp(base) == OP_XMM || GetValueSp(base) == OP_YMM || GetValueSp(base) == OP_ZMM)
{
temp = base;
base = index;
index = temp;
swapped = TRUE; /* this flag is on only for AVX regisres v2.38 */
}
if (index != EMPTY) CodeInfo->indexreg = GetRegNo(index);
if (base != EMPTY) CodeInfo->basereg = GetRegNo(base);
/* use base + index from here - don't use opndx-> base_reg/idx_reg! */
if (index != EMPTY && base == EMPTY && swapped)
{
base = 0x87; /* VEX index with omitted base EG: [+ymm1] */
CodeInfo->basereg = GetRegNo(base);
CodeInfo->indexreg = GetRegNo(index);
}
/* UASM 2.53 check for use of register assumed to ERROR in an EA */
if (base != EMPTY && StdAssumeTable[GetRegNo(base)].error)
{
return(EmitError(USE_OF_REGISTER_ASSUMED_TO_ERROR));
}
if (index != EMPTY && GetValueSp(index) & OP_XMM == 0 && GetValueSp(index) & OP_YMM == 0 && StdAssumeTable[GetRegNo(index)].error)
{
return(EmitError(USE_OF_REGISTER_ASSUMED_TO_ERROR));
}
/* check for base registers */
if ( base != EMPTY )
{
if ( ( ( GetValueSp( base ) & OP_R32) && CodeInfo->Ofssize == USE32 ) ||
( ( GetValueSp( base ) & OP_R64) && CodeInfo->Ofssize == USE64 ) ||
( ( GetValueSp( base ) & OP_R16) && CodeInfo->Ofssize == USE16 ) )
CodeInfo->prefix.adrsiz = FALSE;
else
{
CodeInfo->prefix.adrsiz = TRUE;
/* 16bit addressing modes don't exist in long mode */
if ( ( GetValueSp( base ) & OP_R16) && CodeInfo->Ofssize == USE64 )
return( EmitError( INVALID_ADDRESSING_MODE_WITH_CURRENT_CPU_SETTING ) );
}
}
/* check for index registers */
if( index != EMPTY )
{
if ( ( ( GetValueSp( index ) & OP_R32) && CodeInfo->Ofssize == USE32 ) ||
( ( GetValueSp( index ) & OP_R64) && CodeInfo->Ofssize == USE64 ) ||
( ( GetValueSp( index ) & OP_R16) && CodeInfo->Ofssize == USE16 ) ||
( ( GetValueSp( index ) & OP_XMM) && CodeInfo->Ofssize == USE32 ) || // UASM 2.48 missing support base+xmm/ymm vsib addressing.
( ( GetValueSp( index ) & OP_YMM) && CodeInfo->Ofssize == USE32 )
)
{
CodeInfo->prefix.adrsiz = FALSE;
}
else
{
CodeInfo->prefix.adrsiz = TRUE;
}
/* v2.10: register swapping has been moved to expreval.c, index_connect().
* what has remained here is the check if R/ESP is used as index reg. */
if ((GetRegNo(index) == 4)&& GetValueSp( index ) < OP_XMM )
{
if( opndx->scale )
{
/* no scale must be set */
EmitErr( CANNOT_BE_USED_AS_INDEX_REGISTER, GetResWName( index, NULL ) );
}
else
{
EmitErr( MULTIPLE_BASE_REGISTERS_NOT_ALLOWED );
}
return( ERROR );
}
/* 32/64 bit indirect addressing? */
if( ( CodeInfo->Ofssize == USE16 && CodeInfo->prefix.adrsiz == 1 ) || CodeInfo->Ofssize == USE64 ||
(CodeInfo->Ofssize == USE32 && (CodeInfo->prefix.adrsiz == 0) || (CodeInfo->evex_flag == 1)))
{
if( ( ModuleInfo.curr_cpu & P_CPU_MASK ) >= P_386 )
{
/* scale, 0 or 1->00, 2->40, 4->80, 8->C0 */
switch( opndx->scale )
{
case 0:
case 1: break; /* ss = 00 */
case 2: ss = SCALE_FACTOR_2; break; /* ss = 01 */
case 4: ss = SCALE_FACTOR_4; break; /* ss = 10 */
case 8: ss = SCALE_FACTOR_8; break; /* ss = 11 */
default: /* must be * 1, 2, 4 or 8 */
return( EmitError( SCALE_FACTOR_MUST_BE_1_2_4_OR_8 ) );
}
}
else
{
/* 286 and down cannot use this memory mode */
return( EmitError( INVALID_ADDRESSING_MODE_WITH_CURRENT_CPU_SETTING ) );
}
}
else
{
/* v2.01: 16-bit addressing mode. No scale possible */
if ( opndx->scale )
return( EmitError( INVALID_USE_OF_REGISTER ) );
}
}
if( with_fixup )
{
if( opndx->is_abs )
{
Ofssize = IS_ADDR32( CodeInfo );
}
else if ( sym )
{
Ofssize = GetSymOfssize( sym );
}
else if ( SegOverride )
{
Ofssize = GetSymOfssize( SegOverride );
}
else
Ofssize = CodeInfo->Ofssize;
/* now set fixup_type.
* for direct addressing, the fixup type can easily be set by
* the symbol's offset size. */
if( base == EMPTY && index == EMPTY )
{
CodeInfo->prefix.adrsiz = ADDRSIZE( CodeInfo->Ofssize, Ofssize );
if ( Ofssize == USE64 )
/* v2.03: override with a segment assumed != FLAT? */
if ( opndx->override != NULL && SegOverride != &ModuleInfo.flat_grp->sym )
fixup_type = FIX_OFF32;
else
fixup_type = FIX_RELOFF32;
else
fixup_type = ( Ofssize ) ? FIX_OFF32 : FIX_OFF16;
}
else
{
if( Ofssize == USE64 )
{
fixup_type = FIX_OFF32;
}
else if( IS_ADDR32( CodeInfo ) )
{
/* address prefix needed? */
/* changed for v1.95. Probably more tests needed!
* test case:
* mov eax,[ebx*2-10+offset var] ;code and var are 16bit!
* the old code usually works fine because HiWord of the
* symbol's offset is zero. However, if there's an additional
* displacement which makes the value stored at the location
* < 0, then the target's HiWord becomes <> 0.
*/
//fixup_type = ( Ofssize ) ? FIX_OFF32 : FIX_OFF16;
fixup_type = FIX_OFF32;
}
else
{
fixup_type = FIX_OFF16;
if( Ofssize && Parse_Pass == PASS_2 )
{
/* address size is 16bit but label is 32-bit.
* example: use a 16bit register as base in FLAT model:
* test buff[di],cl */
EmitWarn( 2, WORD_FIXUP_FOR_32BIT_LABEL, sym->name );
}
}
}
/* v2.10: added; IMAGEREL/SECTIONREL for indirect memory operands */
#if IMAGERELSUPP || SECTIONRELSUPP
if ( fixup_type == FIX_OFF32 )
if ( opndx->instr == T_IMAGEREL )
fixup_type = FIX_OFF32_IMGREL;
else if ( opndx->instr == T_SECTIONREL )
fixup_type = FIX_OFF32_SECREL;
#endif
/* no fixups are needed for memory operands of string instructions and XLAT/XLATB.
* However, CMPSD and MOVSD are also SSE2 opcodes, so the fixups must be generated
* anyways.
*/
if (CodeInfo->token != T_XLAT && CodeInfo->token != T_XLATB) {
CodeInfo->opnd[CurrOpnd].InsFixup = CreateFixup(sym, fixup_type, OPTJ_NONE);
}
}
if( set_rm_sib( CodeInfo, CurrOpnd, ss, index, base, sym ) == ERROR )
return( ERROR );
/* set frame type/data in fixup if one was created */
if ( CodeInfo->opnd[CurrOpnd].InsFixup )
{
CodeInfo->opnd[CurrOpnd].InsFixup->frame_type = Frame_Type;
CodeInfo->opnd[CurrOpnd].InsFixup->frame_datum = Frame_Datum;
}
return( NOT_ERROR );
}
static ret_code process_address( struct code_info *CodeInfo, unsigned CurrOpnd, struct expr *opndx )
/**************************************************************************************************/
/* parse the memory reference operand */
{
if( opndx->indirect )
{
/* indirect register operand or stack var */
/* if displacement doesn't fit in 32-bits:
* Masm (both ML and ML64) just truncates.
* Uasm throws an error in 64bit mode and
* warns (level 3) in the other modes.
* todo: this check should also be done for direct addressing! */
if ( opndx->hvalue && ( opndx->hvalue != -1 || opndx->value >= 0 ) )
{
if ( ModuleInfo.Ofssize == USE64 )
return( EmitConstError( opndx ) );
EmitWarn( 3, DISPLACEMENT_OUT_OF_RANGE, opndx->value64 );
}
if( opndx->sym == NULL || opndx->sym->state == SYM_STACK )
return( memory_operand( CodeInfo, CurrOpnd, opndx, FALSE ) );
/* do default processing */
}
else if( opndx->instr != EMPTY )
{
/* instr is OFFSET | LROFFSET | SEG | LOW | LOWWORD, ... */
if( opndx->sym == NULL )
{
/* better to check opndx->type? */
return( idata_nofixup( CodeInfo, CurrOpnd, opndx ) );
}
else
{
/* allow "lea <reg>, [offset <sym>]" */
if( CodeInfo->token == T_LEA && opndx->instr == T_OFFSET )
return( memory_operand( CodeInfo, CurrOpnd, opndx, TRUE ) );
return( idata_fixup( CodeInfo, CurrOpnd, opndx ) );
}
}
else if( opndx->sym == NULL )
{
/* direct operand without symbol */
if( opndx->override != NULL )
{
/* direct absolute memory without symbol.
DS:[0] won't create a fixup, but
DGROUP:[0] will create one! */
/* for 64bit, always create a fixup, since RIP-relative addressing is used
* v2.11: don't create fixup in 64-bit. */
if ( opndx->override->token == T_REG || CodeInfo->Ofssize == USE64 )
return( memory_operand( CodeInfo, CurrOpnd, opndx, FALSE ) );
else
return( memory_operand( CodeInfo, CurrOpnd, opndx, TRUE ) );
}
else if (opndx->isptr)
{
}
else
{
return( idata_nofixup( CodeInfo, CurrOpnd, opndx ) );
}
}
else if( ( opndx->sym->state == SYM_UNDEFINED ) && !opndx->explicit )
{
/* undefined symbol, it's not possible to determine
* operand type and size currently. However, for backpatching
* a fixup should be created. */
/* assume a code label for branch instructions! */
if( IS_ANY_BRANCH( CodeInfo->token ) )
return( process_branch( CodeInfo, CurrOpnd, opndx ) );
switch( CodeInfo->token )
{
case T_PUSH:
case T_PUSHW:
case T_PUSHD:
/* v2.0: don't assume immediate operand if cpu is 8086 */
if ( ( ModuleInfo.curr_cpu & P_CPU_MASK ) > P_86 )
return( idata_fixup( CodeInfo, CurrOpnd, opndx ) );
break;
default:
/* v2.04: if operand is the second argument (and the first is NOT
* a segment register!), scan the
* instruction table if the instruction allows an immediate!
* If so, assume the undefined symbol is a constant. */
if ( CurrOpnd == OPND2 && (( CodeInfo->opnd[OPND1].type & OP_SR ) == 0 ) )
{
const struct instr_item *p = CodeInfo->pinstr;
do
{
if ( opnd_clstab[p->opclsidx].opnd_type[OPND2] & OP_I )
return( idata_fixup( CodeInfo, CurrOpnd, opndx ) );
p++;
} while ( p->first == FALSE );
}
/* v2.10: if current operand is the third argument, always assume an immediate */
if ( CurrOpnd == OPND3 )
return( idata_fixup( CodeInfo, CurrOpnd, opndx ) );
}
/* do default processing */
}
else if( ( opndx->sym->state == SYM_SEG ) || ( opndx->sym->state == SYM_GRP ) )
{
/* SEGMENT and GROUP symbol is converted to SEG symbol for next processing */
opndx->instr = T_SEG;
return( idata_fixup( CodeInfo, CurrOpnd, opndx ) );
}
else
{
/* symbol external, but absolute? */
if( opndx->is_abs )
return( idata_fixup( CodeInfo, CurrOpnd, opndx ) );
/* CODE location is converted to OFFSET symbol */
if ( opndx->mem_type == MT_NEAR || opndx->mem_type == MT_FAR )
{
if( CodeInfo->token == T_LEA )
{
return( memory_operand( CodeInfo, CurrOpnd, opndx, TRUE ) );
}
else if( opndx->mbr != NULL )
{
/* structure field? */
return( memory_operand( CodeInfo, CurrOpnd, opndx, TRUE ) );
}
else
{
return( idata_fixup( CodeInfo, CurrOpnd, opndx ) );
}
}
}
/* default processing: memory with fixup */
return( memory_operand( CodeInfo, CurrOpnd, opndx, TRUE ) );
}
/* Handle constant operands.
* These never need a fixup. Externals - even "absolute" ones -
* are always labeled as EXPR_ADDR by the expression evaluator. */
static ret_code process_const( struct code_info *CodeInfo, unsigned CurrOpnd, struct expr *opndx )
/************************************************************************************************/
{
/* v2.11: don't accept an empty string */
if ( opndx->quoted_string && opndx->quoted_string->stringlen == 0 )
return( EmitError( EMPTY_STRING ) );
/* optimization: skip <value> if it is 0 and instruction
* is RET[W|D|N|F]. */
/* v2.06: moved here and checked the opcode directly, so
* RETD and RETW are also handled. */
if ( ( ( CodeInfo->pinstr->opcode & 0xf7 ) == 0xc2 ) && CurrOpnd == OPND1 && opndx->value == 0 )
{
return( NOT_ERROR );
}
return( idata_nofixup( CodeInfo, CurrOpnd, opndx ) );
}
static ret_code process_register( struct code_info *CodeInfo, unsigned CurrOpnd, const struct expr opndx[] )
/**********************************************************************************************************/
/*
* parse and encode direct register operands. Modifies:
* - CodeInfo->opnd_type
* - CodeInfo->rm_byte (depending on CurrOpnd)
* - CodeInfo->iswide
* - CodeInfo->x86hi_used/x64lo_used
* - CodeInfo->prefix.rex
*/
{
enum special_token regtok;
int regno;
uint_32 flags;
regtok = opndx[CurrOpnd].base_reg->tokval;
regno = GetRegNo( regtok );
/* the register's "OP-flags" are stored in the 'value' field */
flags = GetValueSp( regtok );
CodeInfo->opnd[CurrOpnd].type = flags;
if (CodeInfo->opnd[CurrOpnd].type == OP_XMM || CodeInfo->opnd[CurrOpnd].type == OP_YMM)
{
if (!evex && regno > 15)
return(EmitError(UNAUTHORISED_USE_OF_EVEX_REGISTERS));
/* this fixes suppressed Kn mask register if evex on v2.38 */
else if (evex && regno > 15)
CodeInfo->evex_flag = TRUE;
}
if (CodeInfo->opnd[CurrOpnd].type == OP_ZMM)
{
if (evex)
CodeInfo->evex_flag = TRUE;
else
return(EmitError(UNAUTHORISED_USE_OF_EVEX_REGISTERS));
}
if (CodeInfo->opnd[CurrOpnd].type == OP_K && regno > 7)
{
return(EmitError(USE_OF_REGISTER_ASSUMED_TO_ERROR));
}
if ( flags & OP_R8 )
{
/* it's probably better to not reset the wide bit at all */
if ( flags != OP_CL ) /* problem: SHL AX|AL, CL */
CodeInfo->iswide = 0;
#if AMD64_SUPPORT
if ( CodeInfo->Ofssize == USE64 && regno >=4 && regno <=7 )
if ( SpecialTable[regtok].cpu == P_86 )
CodeInfo->x86hi_used = 1; /* it's AH,BH,CH,DH */
else
CodeInfo->x64lo_used = 1; /* it's SPL,BPL,SIL,DIL */
#endif
if ( StdAssumeTable[regno].error & (( regtok >= T_AH && regtok <= T_BH ) ? RH_ERROR : RL_ERROR ) )
{
DebugMsg(("process_register: assume error, reg=%u\n", regno ));
return( EmitError( USE_OF_REGISTER_ASSUMED_TO_ERROR ) );
}
}
else if ( flags & OP_R )
{
/* 16-, 32- or 64-bit GPR? */
CodeInfo->iswide = 1;
if ( StdAssumeTable[regno].error & flags & OP_R )
{
DebugMsg(("process_register: assume error, reg=%u\n", regno ));
return( EmitError( USE_OF_REGISTER_ASSUMED_TO_ERROR ) );
}
if ( flags & OP_R16 ) {
if ( CodeInfo->Ofssize > USE16 )
CodeInfo->prefix.opsiz = TRUE;
}
else
{
if( CodeInfo->Ofssize == USE16 )
CodeInfo->prefix.opsiz = TRUE;
}
}
else if ( flags & OP_SR )
{
if( regno == 1 )
{
/* 1 is CS */
/* POP CS is not allowed */
if( CodeInfo->token == T_POP )
return( EmitError( POP_CS_IS_NOT_ALLOWED ) );
}
}
else if ( flags & OP_ST )
{
regno = opndx[CurrOpnd].st_idx;
if ( regno > 7 )
return( EmitError( INVALID_COPROCESSOR_REGISTER ) );
CodeInfo->rm_byte |= regno;
if( regno != 0 )
CodeInfo->opnd[CurrOpnd].type = OP_ST_REG;
/* v2.06: exit, rm_byte is already set. */
return( NOT_ERROR );
}
else if ( flags & OP_RSPEC )
{
/* CRx, DRx, TRx */
if( CodeInfo->token != T_MOV )
return( EmitError( ONLY_MOV_CAN_USE_SPECIAL_REGISTER ) );
/* v2.04: previously there were 3 flags, OP_CR, OP_DR and OP_TR.
* this was summoned to one flag OP_RSPEC to free 2 flags, which
* are needed if AVC ( new YMM registers ) is to be supported.
* To distinguish between CR, DR and TR, the register number is
* used now: CRx are numbers 0-F, DRx are numbers 0x10-0x1F and
* TRx are 0x20-0x2F. */
if (regno >= 0x20)
{
/* TRx? */
CodeInfo->opc_or |= 0x04;
if ((ModuleInfo.curr_cpu & P_CPU_MASK) >= P_686)
{
return(EmitErr(CANNOT_USE_TRN_TO_TRM_WITH_CURRENT_CPU_SETTING, regno > 0x25 ? 6 : 3, regno > 0x25 ? 7 : 5));
}
}
else if (regno >= 0x10)
{
/* DRx? */
CodeInfo->opc_or |= 0x01;
}
regno &= 0x0F;
}
/* if it's a x86-64 register (SIL, R8W, R8D, RSI, ... */
if ( ( SpecialTable[regtok].cpu & P_CPU_MASK ) == P_64 )
{
CodeInfo->prefix.rex |= 0x40;
if ( flags & OP_R64 )
CodeInfo->prefix.rex |= REX_W;
}
if( CurrOpnd == OPND1 )
{
/* the first operand
* r/m is treated as a 'reg' field */
CodeInfo->rm_byte |= MOD_11;
CodeInfo->prefix.rex |= (regno & 8 ) >> 3; /* set REX_B */
regno &= BIT_012;
/* fill the r/m field */
CodeInfo->rm_byte |= regno;
}
else
{
/* the second operand
* XCHG can use short form if op1 is AX/EAX/RAX */
if( ( CodeInfo->token == T_XCHG ) && ( CodeInfo->opnd[OPND1].type & OP_A ) &&
( 0 == (CodeInfo->opnd[OPND1].type & OP_R8 ) ) )
{
CodeInfo->prefix.rex |= (regno & 8 ) >> 3; /* set REX_B */
regno &= BIT_012;
CodeInfo->rm_byte = ( CodeInfo->rm_byte & BIT_67 ) | regno;
}
else
{
/* fill reg field with reg */
CodeInfo->prefix.rex |= (regno & 8 ) >> 1; /* set REX_R */
regno &= BIT_012;
CodeInfo->rm_byte = ( CodeInfo->rm_byte & ~BIT_345 ) | ( regno << 3 );
}
}
return( NOT_ERROR );
}
/* special handling for string instructions
* CMPS[B|W|D|Q]
* INS[B|W|D]
* LODS[B|W|D|Q]
* MOVS[B|W|D|Q]
* OUTS[B|W|D]
* SCAS[B|W|D|Q]
* STOS[B|W|D|Q]
* the peculiarity is that these instructions ( optionally )
* have memory operands, which aren't used for code generation
* <opndx> contains the last operand. */
static void HandleStringInstructions( struct code_info *CodeInfo, const struct expr opndx[] )
/*******************************************************************************************/
{
int opndidx = OPND1;
int op_size;
switch( CodeInfo->token ) {
#if AVXSUPP
case T_VCMPSD:
case T_VPCMPD:
case T_VPCMPB:
case T_VPCMPUD:
case T_VPCMPUB:
#endif
case T_CMPSD:
/* filter SSE2 opcode CMPSD */
if ( CodeInfo->opnd[OPND1].type & (OP_XMM | OP_MMX)) {
/* v2.01: QWORD operand for CMPSD/MOVSD may have set REX_W! */
#if AMD64_SUPPORT
CodeInfo->prefix.rex &= ~REX_W;
#endif
return;
}
/* fall through */
case T_CMPS:
case T_CMPSB:
case T_CMPSW:
#if AMD64_SUPPORT
case T_CMPSQ:
#endif
/* cmps allows prefix for the first operand (=source) only */
if ( CodeInfo->prefix.RegOverride != EMPTY ) {
if ( opndx[OPND2].override != NULL ) {
if ( CodeInfo->prefix.RegOverride == ASSUME_ES ) {
/* content of LastRegOverride is valid if
* CodeInfo->RegOverride is != EMPTY.
*/
if ( LastRegOverride == ASSUME_DS )
CodeInfo->prefix.RegOverride = EMPTY;
else
CodeInfo->prefix.RegOverride = LastRegOverride;
} else {
DebugMsg1(("HandleStringInstructions: CMPS: CodeInfo->RegOverride=%X, opndx->override=%s\n", CodeInfo->prefix.RegOverride, opndx[OPND2].override->string_ptr ));
EmitError( INVALID_INSTRUCTION_OPERANDS );
}
} else if ( CodeInfo->prefix.RegOverride == ASSUME_DS ) {
/* prefix for first operand? */
CodeInfo->prefix.RegOverride = EMPTY;
}
}
break;
#if AVXSUPP
case T_VMOVSD:
case T_VMOVUPS:
#endif
case T_MOVSD:
case T_MOVUPS:
/* filter SSE2 opcode MOVSD */
if ( ( CodeInfo->opnd[OPND1].type & (OP_XMM | OP_MMX | OP_YMM | OP_ZMM) ) ||
( CodeInfo->opnd[OPND2].type & (OP_XMM | OP_MMX | OP_YMM | OP_ZMM) ) ) {
/* v2.01: QWORD operand for CMPSD/MOVSD may have set REX_W! */
#if AMD64_SUPPORT
CodeInfo->prefix.rex &= ~REX_W;
#endif
return;
}
/* fall through */
case T_MOVS:
case T_MOVSB:
case T_MOVSW:
#if AMD64_SUPPORT
case T_MOVSQ:
case T_MOVQ:
#endif
/* movs allows prefix for the second operand (=source) only */
if ( CodeInfo->prefix.RegOverride != EMPTY )
if ( opndx[OPND2].override == NULL )
EmitError( INVALID_INSTRUCTION_OPERANDS );
else if ( CodeInfo->prefix.RegOverride == ASSUME_DS )
CodeInfo->prefix.RegOverride = EMPTY;
break;
case T_OUTS:
case T_OUTSB:
case T_OUTSW:
case T_OUTSD:
/* v2.01: remove default DS prefix */
if ( CodeInfo->prefix.RegOverride == ASSUME_DS )
CodeInfo->prefix.RegOverride = EMPTY;
opndidx = OPND2;
break;
case T_LODS:
case T_LODSB:
case T_LODSW:
case T_LODSD:
#if AMD64_SUPPORT
case T_LODSQ:
#endif
/* v2.10: remove unnecessary DS prefix ( Masm-compatible ) */
if ( CodeInfo->prefix.RegOverride == ASSUME_DS )
CodeInfo->prefix.RegOverride = EMPTY;
break;
default: /*INS[B|W|D], SCAS[B|W|D|Q], STOS[B|W|D|Q] */
/* INSx, SCASx and STOSx don't allow any segment prefix != ES
for the memory operand.
*/
if ( CodeInfo->prefix.RegOverride != EMPTY )
if ( CodeInfo->prefix.RegOverride == ASSUME_ES )
CodeInfo->prefix.RegOverride = EMPTY;
else
EmitError( INVALID_INSTRUCTION_OPERANDS );
}
if ( opnd_clstab[CodeInfo->pinstr->opclsidx].opnd_type[opndidx] == OP_NONE ) {
CodeInfo->iswide = 0;
CodeInfo->prefix.opsiz = FALSE;
}
/* if the instruction is the variant without suffix (MOVS, LODS, ..),
* then use the operand's size to get further info.
*/
//if ( CodeInfo->pinstr->opnd_type[opndidx] != OP_NONE &&
if ( opnd_clstab[CodeInfo->pinstr->opclsidx].opnd_type[opndidx] != OP_NONE &&
CodeInfo->opnd[opndidx].type != OP_NONE ) {
if (CodeInfo->token == T_KMOVB) op_size = 1;
else if (CodeInfo->token == T_KMOVW) op_size = 2;
else if (CodeInfo->token == T_KMOVD) op_size = 4;
else if (CodeInfo->token == T_KMOVQ) op_size = 8;
else op_size = OperandSize( CodeInfo->opnd[opndidx].type, CodeInfo );
/* v2.06: added. if memory operand has no size */
if ( op_size == 0 )
op_size = OperandSize( CodeInfo->opnd[opndidx+1].type, CodeInfo );
if ( op_size == 0 ) {
if ( CodeInfo->opnd[opndidx].InsFixup == NULL || CodeInfo->opnd[opndidx].InsFixup->sym->state != SYM_UNDEFINED )
EmitError( INSTRUCTION_OPERAND_MUST_HAVE_SIZE );
op_size = 1; /* assume shortest format */
}
switch( op_size ) {
case 1:
CodeInfo->iswide = 0;
//if( CodeInfo->Ofssize )
CodeInfo->prefix.opsiz = FALSE;
break;
case 2:
CodeInfo->iswide = 1;
CodeInfo->prefix.opsiz = CodeInfo->Ofssize ? TRUE : FALSE;
break;
case 4:
CodeInfo->iswide = 1;
CodeInfo->prefix.opsiz = CodeInfo->Ofssize ? FALSE : TRUE;
break;
#if AMD64_SUPPORT
case 8:
if ( CodeInfo->Ofssize == USE64 ) {
CodeInfo->iswide = 1;
CodeInfo->prefix.opsiz = FALSE;
CodeInfo->prefix.rex = REX_W;
}
break;
#endif
}
}
return;
}
static ret_code check_size( struct code_info *CodeInfo, const struct expr opndx[] )
/*********************************************************************************/
/*
* - use to make sure the size of first operand match the size of second operand;
* - optimize MOV instruction;
* - opndx contains last operand
* todo: BOUND second operand check ( may be WORD/DWORD or DWORD/QWORD ).
* tofix: add a flag in instr_table[] if there's NO check to be done.
*/
{
enum operand_type op1 = CodeInfo->opnd[OPND1].type;
enum operand_type op2 = CodeInfo->opnd[OPND2].type;
ret_code rc = NOT_ERROR;
int op1_size;
int op2_size;
//int op_size = 0;
DebugMsg1(("check_size enter, optype1=%" I32_SPEC "X, optype2=%" I32_SPEC "X\n", op1, op2 ));
if (CodeInfo->token >= T_KADDB && CodeInfo->token <= T_KUNPCKDQ){
//int op3_size;
if ((CodeInfo->opnd[OPND1].type != OP_K) && (CodeInfo->opnd[OPND2].type != OP_K) &&
(CodeInfo->opnd[OPND3].type != OP_K))
return(EmitError(INVALID_INSTRUCTION_OPERANDS));
CodeInfo->indextype = OP_K;
CodeInfo->basetype = OP_K;
op1_size = CodeInfo->pinstr->prefix;
op2_size = CodeInfo->pinstr->prefix;
goto def_check;
}
switch( CodeInfo->token ) {
case T_IN:
if( op2 == OP_DX ) {
/* wide and size is NOT determined by DX, but
* by the first operand, AL|AX|EAX
*/
switch( op1 ) {
case OP_AX:
break;
case OP_AL:
CodeInfo->iswide = 0; /* clear w-bit */
case OP_EAX:
if( CodeInfo->Ofssize ) {
CodeInfo->prefix.opsiz = FALSE;
}
break;
}
}
break;
case T_OUT:
if( op1 == OP_DX ) {
switch( op2 ) {
case OP_AX:
break;
case OP_AL:
CodeInfo->iswide = 0; /* clear w-bit */
case OP_EAX:
if( CodeInfo->Ofssize ) {
CodeInfo->prefix.opsiz = FALSE;
}
}
}
break;
case T_LEA:
#if 0
/* first op must be 16/32 register, but this condition is checked
in CodeGen. operands 1 and 2 can be mixed:
lea cx,[bp]
lea cx,[ebp]
lea ecx,[bp]
lea ecx,[ebp]
are all valid. However, Masm sometimes complains
"cannot use 16-bit register with a 32-bit address"
*/
switch( OperandSize( op1, CodeInfo ) ) {
case 2:
case 4:
break;
default:
EmitErr( OPERANDS_MUST_BE_THE_SAME_SIZE, OperandSize( op1, CodeInfo ), ModuleInfo.Ofssize ? 4 : 2);
rc = ERROR;
}
#endif
break;
case T_RCL:
case T_RCR:
case T_ROL:
case T_ROR:
case T_SAL:
case T_SAR:
case T_SHL:
case T_SHR:
/* v2.11: added */
if ( CodeInfo->opnd[OPND1].type == OP_M && CodeInfo->undef_sym == FALSE &&
( opndx[OPND1].sym == NULL || opndx[OPND1].sym->state != SYM_UNDEFINED ) ) {
EmitErr( INSTRUCTION_OPERAND_MUST_HAVE_SIZE );
rc = ERROR;
break;
}
//if ( CodeInfo->opnd[OPND1].type == OP_M && Parse_Pass == PASS_2 )
// EmitWarn( 2, SIZE_NOT_SPECIFIED_ASSUMING, "BYTE" );
/* v2.0: if second argument is a forward reference,
* change type to "immediate 1"
*/
if ( opndx[OPND2].kind == EXPR_ADDR &&
Parse_Pass == PASS_1 &&
opndx[OPND2].indirect == FALSE &&
opndx[OPND2].sym &&
opndx[OPND2].sym->state == SYM_UNDEFINED ) {
CodeInfo->opnd[OPND2].type = OP_I8;
CodeInfo->opnd[OPND2].data32l = 1;
}
/* v2.06: added (because if first operand is memory, wide bit
* isn't set!)
*/
if ( OperandSize( op1, CodeInfo ) > 1 )
CodeInfo->iswide = 1;
/* v2.06: makes the OP_CL_ONLY case in codegen.c obsolete */
if ( op2 == OP_CL ) {
/* CL is encoded in bit 345 of rm_byte, but we don't need it
* so clear it here */
CodeInfo->rm_byte &= NOT_BIT_345;
}
break;
case T_LDS:
case T_LES:
case T_LFS:
case T_LGS:
case T_LSS:
op1_size = OperandSize( op1, CodeInfo ) + 2; /* add 2 for the impl. segment register */
op2_size = OperandSize( op2, CodeInfo );
if ( op2_size != 0 && op1_size != op2_size ) {
return( EmitError( INVALID_OPERAND_SIZE ) );
}
break;
case T_ENTER:
#if 0 /* v2.11: operand sizes are checked in codegen */
/* ENTER has to be OP_I16, OP_I8_U */
if( op1 == OP_I32 ) {
/* parse_phase_1 will treat 16-bit data as OP_I32 if CPU is 386 */
if( CodeInfo->opnd[OPND1].data32l > (int_32)USHRT_MAX ) {
/* if op1 is really 32-bit data, then error */
EmitError( INVALID_OPERAND_SIZE );
rc = ERROR;
}
}
/* type cast op1 to OP_I16 */
CodeInfo->opnd[OPND1].type = OP_I16;
/* op2 have to be 8-bit data */
if( op2 >= OP_I16 ) {
if( CodeInfo->opnd[OPND2].data32l > UCHAR_MAX ) {
EmitError( INVALID_OPERAND_SIZE );
rc = ERROR;
}
CodeInfo->opnd[OPND2].type = OP_I8;
}
#endif
break;
case T_MOVSX:
case T_MOVZX:
CodeInfo->iswide = 0;
op1_size = OperandSize( op1, CodeInfo );
op2_size = OperandSize( op2, CodeInfo );
DebugMsg1(("check_size, MOVZX/MOVSX: op2_size=%u, opndx.memtype=%Xh, opndx.sym=%X\n", op2_size, opndx[OPND2].mem_type, opndx[OPND2].sym ));
if ( op2_size == 0 && Parse_Pass == PASS_2 )
if ( op1_size == 2 ) {
EmitWarn( 2, SIZE_NOT_SPECIFIED_ASSUMING, "BYTE" );
} else
EmitErr( INSTRUCTION_OPERAND_MUST_HAVE_SIZE );
switch( op1_size ) {
#if AMD64_SUPPORT
case 8:
//if ( CodeInfo->Ofssize == USE64 )
// break;
#endif
case 4:
if (op2_size < 2)
;
else if (op2_size == 2)
CodeInfo->iswide = 1;
else {
EmitError( OP2_TOO_BIG );
rc = ERROR;
}
CodeInfo->prefix.opsiz = CodeInfo->Ofssize ? FALSE : TRUE;
break;
case 2:
if( op2_size >= 2 ) {
EmitError( OP2_TOO_BIG );
rc = ERROR;
}
CodeInfo->prefix.opsiz = CodeInfo->Ofssize ? TRUE : FALSE;
break;
default:
/* op1 must be r16/r32/r64 */
EmitError( OP1_TOO_SMALL );
rc = ERROR;
}
break;
#if AMD64_SUPPORT
case T_MOVSXD:
break;
#endif
case T_ARPL: /* v2.06: new, avoids the OP_R16 hack in codegen.c */
CodeInfo->prefix.opsiz = 0;
goto def_check;
break;
#if AMD64_SUPPORT
case T_LAR: /* v2.04: added */
case T_LSL: /* 19-sep-93 */
#if 1 /* v2.04: changed */
if ( ModuleInfo.Ofssize != USE64 || ( ( op2 & OP_M ) == 0 ) )
goto def_check;
/* in 64-bit, if second argument is memory operand,
* ensure it has size WORD ( or 0 if a forward ref )
*/
op2_size = OperandSize( op2, CodeInfo );
if ( op2_size != 2 && op2_size != 0 ) {
return( EmitError( INVALID_OPERAND_SIZE ) );
}
/* the opsize prefix depends on the FIRST operand only! */
op1_size = OperandSize( op1, CodeInfo );
if ( op1_size != 2 )
CodeInfo->prefix.opsiz = FALSE;
#else
op1_size = OperandSize( op1, CodeInfo );
switch( op1_size ) {
case 2:
if( CodeInfo->Ofssize )
CodeInfo->prefix.opsiz = TRUE;
break;
case 4:
if( CodeInfo->Ofssize )
CodeInfo->prefix.opsiz = FALSE;
break;
default:
return( EmitError( INVALID_OPERAND_SIZE ) );
}
op2_size = OperandSize( op2, CodeInfo );
switch( op2_size ) {
case 2:
case 4:
break;
default:
EmitError( INVALID_OPERAND_SIZE );
rc = ERROR;
break;
}
#endif
break;
#endif
case T_IMUL: /* v2.06: check for 3rd operand must be done here */
if ( CodeInfo->opnd[OPND3].type != OP_NONE ) {
int op3_size;
op1_size = OperandSize( op1, CodeInfo );
op3_size = OperandSize( CodeInfo->opnd[OPND3].type, CodeInfo );
/* the only case which must be checked here
* is a WORD register as op1 and a DWORD immediate as op3 */
if ( op1_size == 2 && op3_size > 2 ) {
EmitErr( OPERANDS_MUST_BE_THE_SAME_SIZE, op1_size, op3_size );
rc = ERROR;
break;
}
if ( CodeInfo->opnd[OPND3].type & ( OP_I16 | OP_I32 ) )
CodeInfo->opnd[OPND3].type = ( op1_size == 2 ? OP_I16 : OP_I32 );
}
goto def_check;
break;
case T_CVTSD2SI:
case T_CVTTSD2SI:
case T_CVTSS2SI:
case T_CVTTSS2SI:
//case T_MOVNTI: /* v2.05: removed */
#if AVXSUPP
case T_VBROADCASTSD:
case T_VBROADCASTF128:
case T_VEXTRACTF128:
case T_VINSERTF128:
case T_VCVTSD2SI:
case T_VCVTTSD2SI:
case T_VCVTSS2SI:
case T_VCVTTSS2SI:
#endif
#if VMXSUPP /* v2.09: added */
case T_INVEPT:
case T_INVVPID:
#endif
#if SVMSUPP /* v2.09: added */
case T_INVLPGA:
#endif
break;
#if AVXSUPP
case T_VCVTSD2USI:
CodeInfo->evex_flag = TRUE;
break;
case T_VCVTPD2DQ:
case T_VCVTTPD2DQ:
case T_VCVTPD2PS:
if ((op2 == OP_M) && opndx[OPND2].indirect) {
if (!broadflags) /* v2.49 fix for bcst */
return(EmitError(INSTRUCTION_OPERAND_MUST_HAVE_SIZE));
}
break;
case T_VMOVDDUP:
if ( !( op1 & OP_YMM ) )
break;
/* fall through */
case T_VPERM2F128: /* has just one memory variant, and VX_L isnt set */
if ( op2 == OP_M )
CodeInfo->opnd[OPND2].type |= OP_M256;
break;
#endif
#if SSE4SUPP
case T_CRC32:
/* v2.02: for CRC32, the second operand determines whether an
* OPSIZE prefix byte is to be written.
*/
op2_size = OperandSize( op2, CodeInfo );
if ( op2_size < 2)
CodeInfo->prefix.opsiz = FALSE;
else if ( op2_size == 2 )
CodeInfo->prefix.opsiz = CodeInfo->Ofssize ? TRUE : FALSE;
else
CodeInfo->prefix.opsiz = CodeInfo->Ofssize ? FALSE : TRUE;
break;
/* size for BND instructions can not be less then DWORD v2.31 */
case T_BNDMK:
case T_BNDCL:
case T_BNDCU:
case T_BNDCN:
case T_BNDMOV:
case T_BNDLDX:
case T_BNDSTX:
if (CodeInfo->mem_type == MT_BYTE || CodeInfo->mem_type == MT_WORD ){
EmitError( INVALID_OPERAND_SIZE );
rc = ERROR;
}
break;
#endif
case T_MOVD:
#if 0
op1_size = OperandSize( op1, CodeInfo );
op2_size = OperandSize( op2, CodeInfo );
if( ( op1_size != 0 ) && ( op1_size != 4 )
|| ( op2_size != 0 ) && ( op2_size != 4 ) ) {
EmitErr( OPERANDS_MUST_BE_THE_SAME_SIZE, op1_size, op2_size );
rc = ERROR;
}
#endif
break;
case T_MOV:
if( op1 & OP_SR ) { /* segment register as op1? */
op2_size = OperandSize( op2, CodeInfo );
if( ( op2_size == 2 ) || ( op2_size == 4 )
#if AMD64_SUPPORT
|| ( op2_size == 8 && ModuleInfo.Ofssize == USE64 )
#endif
) {
return( NOT_ERROR );
}
} else if( op2 & OP_SR ) {
op1_size = OperandSize( op1, CodeInfo );
if( ( op1_size == 2 ) || ( op1_size == 4 )
#if AMD64_SUPPORT
|| ( op1_size == 8 && ModuleInfo.Ofssize == USE64 )
#endif
) {
return( NOT_ERROR );
}
} else if( ( op1 & OP_M ) && ( op2 & OP_A ) ) { /* 1. operand memory reference, 2. AL|AX|EAX|RAX? */
if ( CodeInfo->isdirect == FALSE ) {
/* address mode is indirect.
* don't use the short format (opcodes A0-A3) - it exists for direct
* addressing only. Reset OP_A flag!
*/
CodeInfo->opnd[OPND2].type &= ~OP_A;
DebugMsg1(("check_size: OP_A flag reset, new op2=%X\n", CodeInfo->opnd[OPND2].type ));
#if AMD64_SUPPORT
} else if ( CodeInfo->Ofssize == USE64 && ( CodeInfo->opnd[OPND1].data64 < 0x80000000 || CodeInfo->opnd[OPND1].data64 >= 0xffffffff80000000 ) ) {
/* for 64bit, opcodes A0-A3 ( direct memory addressing with AL/AX/EAX/RAX )
* are followed by a full 64-bit moffs. This is only used if the offset won't fit
* in a 32-bit signed value.
*/
CodeInfo->opnd[OPND2].type &= ~OP_A;
DebugMsg1(("check_size: OP_A flag reset, new op2=%X\n", CodeInfo->opnd[OPND2].type ));
#endif
}
} else if( ( op1 & OP_A ) && ( op2 & OP_M ) ) { /* 2. operand memory reference, 1. AL|AX|EAX|RAX? */
if ( CodeInfo->isdirect == FALSE ) {
CodeInfo->opnd[OPND1].type &= ~OP_A;
DebugMsg1(("check_size: OP_A flag reset, new op1=%X\n", CodeInfo->opnd[OPND1].type ));
#if AMD64_SUPPORT
} else if ( CodeInfo->Ofssize == USE64 && ( CodeInfo->opnd[OPND2].data64 < 0x80000000 || CodeInfo->opnd[OPND2].data64 >= 0xffffffff80000000 ) ) {
CodeInfo->opnd[OPND1].type &= ~OP_A;
DebugMsg1(("check_size: OP_A flag reset, new op2=%X\n", CodeInfo->opnd[OPND1].type ));
#endif
}
}
/* fall through */
default:
//#if AMD64_SUPPORT
def_check:
//#endif
/* make sure the 2 opnds are of the same type */
op1_size = OperandSize( op1, CodeInfo );
op2_size = OperandSize( op2, CodeInfo );
DebugMsg1(("check_size default: op1_size1=%u, op2_size=%u\n", op1_size, op2_size));
if( op1_size > op2_size ) {
if( ( op2 >= OP_I8 ) && ( op2 <= OP_I32 ) ) { /* immediate */
op2_size = op1_size; /* promote to larger size */
}
}
#if 1
/* v2.04: check in idata_nofixup was signed,
* so now add -256 - -129 and 128-255 to acceptable byte range.
* Since Masm v8, the check is more restrictive, -255 - -129
* is no longer accepted.
*/
if( ( op1_size == 1 ) && ( op2 == OP_I16 ) &&
( CodeInfo->opnd[OPND2].data32l <= UCHAR_MAX ) &&
( CodeInfo->opnd[OPND2].data32l >= -128 ) ) {
//( CodeInfo->opnd[OPND2].data32l >= -255 ) ) {
return( rc ); /* OK cause no sign extension */
}
#endif
#if 0
/* v2.03: this "if" made Uasm accept any 32-bit constant
* for 16-bit destinations, which is Masm compatibel,
* "mov ax, 12345h"
* the test is a bit too liberal here, IMO, because
* it makes Uasm accept "mov ax, near32 ptr var",
* which is rejected by Masm.
*/
if( ( op1_size == 2 ) && ( op2 == OP_I32 )
&& ( CodeInfo->data[OPND2] <= USHRT_MAX ) ) {
return( rc ); /* OK cause no sign extension */
}
#endif
if( op1_size != op2_size ) {
/* if one or more are !defined, set them appropriately */
#if AVXSUPP
if( ( op1 | op2 ) & ( OP_MMX | OP_XMM | OP_YMM | OP_K | OP_ZMM ) ) {
#else
if( ( op1 | op2 ) & ( OP_MMX | OP_XMM ) ) {
#endif
}
else if( ( op1_size != 0 ) && ( op2_size != 0 ) ) {
if ((CodeInfo->token == T_VCVTSD2USI)||(CodeInfo->token == T_VCVTSS2USI)||
(CodeInfo->token == T_VCVTTSD2USI)||(CodeInfo->token == T_VCVTTSS2USI))
CodeInfo->evex_flag = TRUE;
else{
if (CodeInfo->token >= T_BNDMK && CodeInfo->token <= T_BNDSTX){
switch (CodeInfo->token){
case T_BNDMK:
case T_BNDCL:
case T_BNDCU:
case T_BNDCN:
if (CodeInfo->mem_type == MT_DWORD || CodeInfo->mem_type == MT_QWORD ||
CodeInfo->mem_type == MT_EMPTY)
;
else{
EmitErr(OPERANDS_MUST_BE_THE_SAME_SIZE, op1_size, op2_size);
rc = ERROR;
}
break;
case T_BNDMOV:
if (CodeInfo->mem_type == MT_DWORD || CodeInfo->mem_type == MT_QWORD ||
CodeInfo->mem_type == MT_OWORD || CodeInfo->mem_type == MT_EMPTY)
;
else{
EmitErr(OPERANDS_MUST_BE_THE_SAME_SIZE, op1_size, op2_size);
rc = ERROR;
}
break;
case T_BNDLDX:
case T_BNDSTX:
if (CodeInfo->mem_type == MT_DWORD || CodeInfo->mem_type == MT_QWORD ||
CodeInfo->mem_type == MT_EMPTY)
;
else{
EmitErr(OPERANDS_MUST_BE_THE_SAME_SIZE, op1_size, op2_size);
rc = ERROR;
}
break;
}
}
else{
EmitErr(OPERANDS_MUST_BE_THE_SAME_SIZE, op1_size, op2_size);
rc = ERROR;
}
}
}
/* size == 0 is assumed to mean "undefined", but there
* is also the case of an "empty" struct or union. The
* latter case isn't handled correctly.
*/
if( op1_size == 0 ) {
if( ( op1 & OP_M_ANY ) && ( op2 & OP_I ) ) {
char *p = "WORD";
if( (uint_32)CodeInfo->opnd[OPND2].data32l > USHRT_MAX || op2_size == 4 ) {
CodeInfo->iswide = 1;
DebugMsg1(("check_size: op1=%X op1_size=0, op2=%X, op2_size=%u CodeInfo->data[2]=%X\n", op1, op2, op2_size, CodeInfo->opnd[OPND2].data32l ));
#if 1 /* added v1.95: in 16bit code, 'mov [di],8000h' should warn: assuming WORD */
if ( ModuleInfo.Ofssize == USE16 && op2_size > 2 && InWordRange( CodeInfo->opnd[OPND2].data32l ) )
op2_size = 2;
#endif
if (op2_size <= 2 && CodeInfo->opnd[OPND2].data32l > SHRT_MIN && ModuleInfo.Ofssize == USE16 ) {
CodeInfo->mem_type = MT_WORD;
CodeInfo->opnd[OPND2].type = OP_I16;
} else {
CodeInfo->mem_type = MT_DWORD;
CodeInfo->opnd[OPND2].type = OP_I32;
p = "DWORD";
}
} else if( (uint_32)CodeInfo->opnd[OPND2].data32l > UCHAR_MAX ) {//|| op2_size == 2
CodeInfo->mem_type = MT_WORD;
CodeInfo->iswide = 1;
CodeInfo->opnd[OPND2].type = OP_I16;
} else {
CodeInfo->mem_type = MT_BYTE;
CodeInfo->opnd[OPND2].type = OP_I8;
CodeInfo->opnd[OPND1].type = OP_M08;
p = "BYTE";
}
if( opndx[OPND2].explicit == FALSE ) {
/* v2.06: emit warning at pass one if mem op isn't a forward ref */
/* v2.06b: added "undefined" check */
if ( ( CodeInfo->opnd[OPND1].InsFixup == NULL && Parse_Pass == PASS_1 && CodeInfo->undef_sym == FALSE ) ||
( CodeInfo->opnd[OPND1].InsFixup && Parse_Pass == PASS_2 ) )
EmitWarn( 1, SIZE_NOT_SPECIFIED_ASSUMING, p ); //ovde je greska
}
} else if( ( op1 & OP_M_ANY ) && ( op2 & ( OP_R | OP_SR ) ) ) {
} else if( ( op1 & ( OP_MMX | OP_XMM ) ) && ( op2 & OP_I ) ) {
if( (uint_32)CodeInfo->opnd[OPND2].data32l > USHRT_MAX ) {
CodeInfo->opnd[OPND2].type = OP_I32;
} else if( (uint_32)CodeInfo->opnd[OPND2].data32l > UCHAR_MAX ) {
CodeInfo->opnd[OPND2].type = OP_I16;
} else {
CodeInfo->opnd[OPND2].type = OP_I8;
}
} else if( ( op1 | op2 ) & ( OP_MMX | OP_XMM ) ) {
} else {
//AsmIntErr( 1 ); /* printf("internal error = %u", 1 ) */
switch( op2_size ) {
case 1:
CodeInfo->mem_type = MT_BYTE;
if( ( Parse_Pass == PASS_1 ) && ( op2 & OP_I ) ) {
EmitWarn( 1, SIZE_NOT_SPECIFIED_ASSUMING, "BYTE" );
}
break;
case 2:
CodeInfo->mem_type = MT_WORD;
CodeInfo->iswide = 1;
if( ( Parse_Pass == PASS_1 ) && ( op2 & OP_I ) ) {
EmitWarn( 1, SIZE_NOT_SPECIFIED_ASSUMING, "WORD" );
}
if( CodeInfo->Ofssize )
CodeInfo->prefix.opsiz = TRUE;
break;
case 4:
CodeInfo->mem_type = MT_DWORD;
CodeInfo->iswide = 1;
if( ( Parse_Pass == PASS_1 ) && ( op2 & OP_I ) ) {
EmitWarn( 1, SIZE_NOT_SPECIFIED_ASSUMING, "DWORD" );
}
break;
}
}
}
}
}
DebugMsg1(("check_size exit [CodeInfo->mem_type=%Xh]\n", CodeInfo->mem_type));
return( rc );
}
static struct asym *IsType( const char *name )
/********************************************/
{
struct asym *sym;
sym = SymSearch( name );
if ( sym && (sym->state == SYM_TYPE ) )
return( sym );
return( NULL );
}
/*
* ParseLine() is the main parser function.
* It scans the tokens in tokenarray[] and does:
* - for code labels: call CreateLabel()
* - for data items and data directives: call data_dir()
* - for other directives: call directive[]()
* - for instructions: fill CodeInfo and call codegen() */
ret_code ParseLine(struct asm_tok tokenarray[]) {
int i;
int j;
unsigned dirflags;
unsigned CurrOpnd;
ret_code temp;
struct asym *sym;
uint_32 oldofs;
enum special_token regtok;
int c0;
int c1;
unsigned flags;
char *pnlbl;
int alignCheck = 16;
int infSize = 0;
int oldi = 0;
struct dsym *recsym = 0;
struct code_info CodeInfo;
struct expr opndx[MAX_OPND + 1];
// We create copies of these structures for now as the old codegen has a very ugly way of working with additional vex 3 opnd forms, by
// adding them as extra values in codeinfo and removing them as true operands. For V2 this is no longer the case, however we don't want
// the existing codegen to break until it's completely replaced.
struct code_info CodeInfoV2;
struct expr opndxV2[MAX_OPND + 1];
const char *opcodePtr = NULL;
int opndCount = 0;
char *instr = NULL;
bool doDataInProc = FALSE;
memset(&opndx, 0, sizeof(opndx));
memset(&CodeInfo, 0, sizeof(CodeInfo));
memset(&opndxV2, 0, sizeof(opndx));
memset(&CodeInfoV2, 0, sizeof(CodeInfo));
i = 0;
/* ************************************************************** */
/* Support direct usage of USE16, USE32, USE64 for UASM Flat Mode */
/* ************************************************************** */
if (tokenarray[0].token == T_ID && strcasecmp(tokenarray[0].string_ptr, "use16") == 0) {
ModuleInfo.frame_auto = 0;
ModuleInfo.win64_flags = 0;
ModuleInfo.offsettype = OT_GROUP;
ModuleInfo.Ofssize = USE16;
ModuleInfo.wordsize = 2;
ModuleInfo.defOfssize = USE16;
ModuleInfo.sub_format = SFORMAT_NONE;
ModuleInfo.basereg[ModuleInfo.Ofssize] = T_SP;
if (ModuleInfo.currseg)
{
ModuleInfo.currseg->e.seginfo->Ofssize = USE16;
ModuleInfo.currseg->e.seginfo->segtype = SEGTYPE_CODE;
ModuleInfo.currseg->sym.segment->Ofssize = USE16;
}
if (CurrSeg)
{
CurrSeg->e.seginfo->Ofssize = USE16;
CurrSeg->e.seginfo->segtype = SEGTYPE_CODE;
CurrSeg->sym.segment->Ofssize = USE16;
CurrSeg->sym.Ofssize = USE16;
}
sym = SymCheck("_flat");
if (sym)
{
sym->isdefined = TRUE;
UpdateCurrSegVars();
SetOfssize();
}
FStoreLine(1);
return(NOT_ERROR);
}
else if (tokenarray[0].token == T_ID && strcasecmp(tokenarray[0].string_ptr, "use32") == 0) {
ModuleInfo.frame_auto = 0;
ModuleInfo.win64_flags = 0;
ModuleInfo.offsettype = OT_GROUP;
ModuleInfo.Ofssize = USE32;
ModuleInfo.wordsize = 4;
ModuleInfo.defOfssize = USE32;
ModuleInfo.sub_format = SFORMAT_NONE;
ModuleInfo.basereg[ModuleInfo.Ofssize] = T_ESP;
if (ModuleInfo.currseg)
{
ModuleInfo.currseg->e.seginfo->Ofssize = USE32;
ModuleInfo.currseg->e.seginfo->segtype = SEGTYPE_CODE;
ModuleInfo.currseg->sym.segment->Ofssize = USE32;
}
if (CurrSeg)
{
CurrSeg->e.seginfo->Ofssize = USE32;
CurrSeg->e.seginfo->segtype = SEGTYPE_CODE;
CurrSeg->sym.segment->Ofssize = USE32;
CurrSeg->sym.Ofssize = USE32;
}
sym = SymCheck("_flat");
if (sym)
{
sym->isdefined = TRUE;
UpdateCurrSegVars();
SetOfssize();
}
FStoreLine(1);
return(NOT_ERROR);
}
else if (tokenarray[0].token == T_ID && strcasecmp(tokenarray[0].string_ptr, "use64") == 0) {
ModuleInfo.frame_auto = 1;
ModuleInfo.win64_flags = 11;
ModuleInfo.offsettype = OT_FLAT;
ModuleInfo.Ofssize = USE64;
ModuleInfo.wordsize = 8;
ModuleInfo.defOfssize = USE64;
ModuleInfo.langtype = LANG_FASTCALL;
ModuleInfo.sub_format = SFORMAT_NONE;
ModuleInfo.basereg[ModuleInfo.Ofssize] = T_RSP;
if (ModuleInfo.currseg)
{
ModuleInfo.currseg->e.seginfo->Ofssize = USE64;
ModuleInfo.currseg->e.seginfo->segtype = SEGTYPE_CODE;
ModuleInfo.currseg->sym.segment->Ofssize = USE64;
ModuleInfo.currseg->e.seginfo->alignment = 12;
}
if (CurrSeg)
{
CurrSeg->e.seginfo->Ofssize = USE64;
CurrSeg->e.seginfo->segtype = SEGTYPE_CODE;
CurrSeg->sym.segment->Ofssize = USE64;
CurrSeg->sym.Ofssize = USE64;
CurrSeg->e.seginfo->alignment = 12;
}
sym = SymCheck("_flat");
if (sym)
{
sym->isdefined = TRUE;
UpdateCurrSegVars();
SetOfssize();
}
FStoreLine(1);
return(NOT_ERROR);
}
/* ************************************************************** */
/* ************************************************************** */
/* Does line start with a code label? */
/* ************************************************************** */
if (tokenarray[0].token == T_ID && (tokenarray[1].token == T_COLON || tokenarray[1].token == T_DBL_COLON))
{
i = 2;
if (ProcStatus & PRST_PROLOGUE_NOT_DONE)
write_prologue(tokenarray);
/* create a global or local code label */
if (CreateLabel(tokenarray[0].string_ptr, MT_NEAR, NULL,(ModuleInfo.scoped && CurrProc && tokenarray[1].token != T_DBL_COLON)) == NULL)
return(ERROR);
if (tokenarray[i].token == T_FINAL)
{
FStoreLine(0);
if (CurrFile[LST])
LstWrite(LSTTYPE_LABEL, 0, NULL);
return(NOT_ERROR);
}
}
/* ************************************************************** */
/* handle directives and (anonymous) data items */
/* ************************************************************** */
if (tokenarray[i].token != T_INSTRUCTION)
{
/* a code label before a data item is only accepted in Masm5 compat mode */
Frame_Type = FRAME_NONE;
SegOverride = NULL;
if (i == 0 && tokenarray[0].token == T_ID)
{
/* token at pos 0 may be a label.
* it IS a label if:
* 1. token at pos 1 is a directive (lbl dd ...)
* 2. token at pos 0 is NOT a userdef type ( lbl DWORD ...)
* 3. inside a struct and token at pos 1 is a userdef type or a predefined type. (usertype DWORD|usertype ... )
* the separate namespace allows this syntax here. */
if (tokenarray[1].token == T_DIRECTIVE)
i++;
else {
sym = IsType(tokenarray[0].string_ptr);
if (sym == NULL)
i++;
else if (CurrStruct && ((tokenarray[1].token == T_STYPE) || (tokenarray[1].token == T_ID && (IsType(tokenarray[1].string_ptr)))))
i++;
}
}
switch (tokenarray[i].token)
{
case T_DIRECTIVE:
if (tokenarray[i].dirtype == DRT_DATADIR)
{
/* UASM 2.51 - Don't write anonymous data items in Procs before the prologue is done */
if (!(ProcStatus & PRST_PROLOGUE_NOT_DONE) || !ProcStatus)
return(data_dir(i, tokenarray, NULL));
else {
doDataInProc = TRUE;
goto dataInProc;
}
}
dirflags = GetValueSp(tokenarray[i].tokval);
if (CurrStruct && (dirflags & DF_NOSTRUC))
{
return(EmitError(STATEMENT_NOT_ALLOWED_INSIDE_STRUCTURE_DEFINITION));
}
/* label allowed for directive? */
if (dirflags & DF_LABEL)
{
if (i && tokenarray[0].token != T_ID)
return(EmitErr(SYNTAX_ERROR_EX, tokenarray[0].string_ptr));
}
else if (i && tokenarray[i - 1].token != T_COLON && tokenarray[i - 1].token != T_DBL_COLON)
return(EmitErr(SYNTAX_ERROR_EX, tokenarray[i - 1].string_ptr));
/* must be done BEFORE FStoreLine()! */
if ((ProcStatus & PRST_PROLOGUE_NOT_DONE) && (dirflags & DF_PROC))
write_prologue(tokenarray);
if (StoreState || (dirflags & DF_STORE))
{
if ((dirflags & DF_CGEN) && ModuleInfo.CurrComment && ModuleInfo.list_generated_code)
FStoreLine(1);
else
FStoreLine(0);
}
if (tokenarray[i].dirtype > DRT_DATADIR)
temp = directive_tab[tokenarray[i].dirtype](i, tokenarray);
else
{
temp = ERROR;
/* ENDM, EXITM and GOTO directives should never be seen here */
switch (tokenarray[i].tokval)
{
case T_ENDM:
EmitError(UNMATCHED_MACRO_NESTING);
break;
case T_EXITM:
case T_GOTO:
EmitError(DIRECTIVE_MUST_APPEAR_INSIDE_A_MACRO);
break;
default:
/* this error may happen if CATSTR, SUBSTR, MACRO, ...a ren't at pos 1 */
EmitErr(SYNTAX_ERROR_EX, tokenarray[i].string_ptr);
break;
}
}
/* v2.0: for generated code it's important that list file is written in ALL passes, to update file position! */
if (ModuleInfo.list && (Parse_Pass == PASS_2 || ModuleInfo.GeneratedCode || UseSavedState == FALSE))
LstWriteSrcLine();
return(temp);
case T_STYPE:
return(data_dir(i, tokenarray, NULL));
case T_ID:
if (sym = IsType(tokenarray[i].string_ptr)) {
return(data_dir(i, tokenarray, sym));
}
break;
default:
if (tokenarray[i].token == T_COLON)
{
return(EmitError(SYNTAX_ERROR_UNEXPECTED_COLON));
}
break;
} /* end switch (tokenarray[i].token) */
if (i && tokenarray[i - 1].token == T_ID)
i--;
return(EmitErr(SYNTAX_ERROR_EX, tokenarray[i].string_ptr));
} /* end of != T_INSTRUCTION */
dataInProc:
if (CurrStruct)
return(EmitError(STATEMENT_NOT_ALLOWED_INSIDE_STRUCTURE_DEFINITION));
if (ProcStatus & PRST_PROLOGUE_NOT_DONE)
write_prologue(tokenarray);
if (doDataInProc)
{
doDataInProc = FALSE;
return(data_dir(i, tokenarray, NULL));
}
/* v2.07: moved because special handling is needed for RET/IRET */
if (CurrFile[LST]) oldofs = GetCurrOffset();
/* ************************************************************** */
/* INIT: CodeInfo */
/* ************************************************************** */
CodeInfo.prefix.ins = EMPTY;
CodeInfo.prefix.RegOverride = EMPTY;
CodeInfo.prefix.rex = 0;
CodeInfo.prefix.adrsiz = FALSE;
CodeInfo.prefix.opsiz = FALSE;
CodeInfo.mem_type = MT_EMPTY;
for (j = 0; j < MAX_OPND; j++)
CodeInfo.opnd[j].type = OP_NONE;
CodeInfo.rm_byte = 0;
CodeInfo.sib = 0;
CodeInfo.Ofssize = ModuleInfo.Ofssize;
CodeInfo.opc_or = 0;
CodeInfo.basetype = 0;
CodeInfo.indextype = 0;
CodeInfo.evex_sae = 0;
CodeInfo.vexregop = 0;
CodeInfo.tuple = 0;
CodeInfo.isptr = FALSE;
CodeInfo.vexconst = 0;
CodeInfo.evex_flag = FALSE; /* if TRUE will output 0x62 */
CodeInfo.reg1 = 0;
CodeInfo.reg2 = 0;
CodeInfo.reg3 = 0xff; /* if not reg3 make it negative */
CodeInfo.basereg = 0xff;
CodeInfo.indexreg = 0xff;
CodeInfo.zreg = 0;
if (tokenarray[0].tokval >= T_KADDB && tokenarray[0].tokval <= T_KTESTQ)
CodeInfo.evex_flag = FALSE;
else
{
// Init EVEX three bytes
CodeInfo.evex_p0 = 0; /* P0[3 : 2] Must be 0 */
CodeInfo.evex_p1 = 0x4; /* P1[2] Must be 1 */
CodeInfo.evex_p2 = 0;
if (broadflags || decoflags || evexflag)
CodeInfo.evex_flag = TRUE; /* if TRUE will output 0x62 */
}
CodeInfo.flags = 0;
/* ******************************************************************* */
/* instruction prefix? T_LOCK, T_REP, T_REPE, T_REPNE, T_REPNZ, T_REPZ */
/* ******************************************************************* */
if (tokenarray[i].tokval >= T_LOCK && tokenarray[i].tokval <= T_REPZ)
{
CodeInfo.prefix.ins = tokenarray[i].tokval;
i++;
/* prefix has to be followed by an instruction */
if (tokenarray[i].token != T_INSTRUCTION)
return(EmitError(PREFIX_MUST_BE_FOLLOWED_BY_AN_INSTRUCTION));
};
/* ************************************************************** */
/* Handle RETURNS inside CurrProc */
/* ************************************************************** */
if (CurrProc)
{
switch (tokenarray[i].tokval)
{
case T_RETN:
case T_RET:
case T_IRET: /* IRET is always 16-bit; OTOH, IRETW doesn't exist */
case T_IRETD:
case T_IRETQ:
if ( (!(ProcStatus & PRST_INSIDE_EPILOGUE) && ModuleInfo.epiloguemode != PEM_NONE && tokenarray[i].tokval != T_RETN) ||
(!(ProcStatus & PRST_INSIDE_EPILOGUE) && (CurrProc->e.procinfo->basereg == T_ESP || CurrProc->e.procinfo->basereg == T_RSP) ))
{
/* v2.07: special handling for RET/IRET */
FStoreLine((ModuleInfo.CurrComment && ModuleInfo.list_generated_code) ? 1 : 0);
ProcStatus |= PRST_INSIDE_EPILOGUE;
temp = RetInstr(i, tokenarray, Token_Count);
ProcStatus &= ~PRST_INSIDE_EPILOGUE;
return(temp);
}
/* default translation: just RET to RETF if proc is far */
/* v2.08: this code must run even if PRST_INSIDE_EPILOGUE is set */
if (tokenarray[i].tokval == T_RET && CurrProc->sym.mem_type == MT_FAR)
tokenarray[i].tokval = T_RETF;
}
}
FStoreLine(0); /* must be placed AFTER write_prologue() */
CodeInfo.token = tokenarray[i].tokval;
opcodePtr = tokenarray[i].string_ptr; // Copy a pointer to the mnemonic string for CodeGenV2.
/* get the instruction's start position in InstrTable[] */
CodeInfo.pinstr = &InstrTable[IndexFromToken(CodeInfo.token)];
i++;
if (CurrSeg == NULL)
return(EmitError(MUST_BE_IN_SEGMENT_BLOCK));
if (CurrSeg->e.seginfo->segtype == SEGTYPE_UNDEF)
CurrSeg->e.seginfo->segtype = SEGTYPE_CODE;
if (ModuleInfo.CommentDataInCode)
omf_OutSelect(FALSE);
/* UASM 2.37: Calculate an inferred memory size if any operand is a register, this can be used when no memory size info is available */
/* ********************************************************************************************************************************* */
oldi = i;
for (j = 0; j < sizeof(opndx) / sizeof(opndx[0]) && tokenarray[i].token != T_FINAL; j++)
{
if (j)
{
if (tokenarray[i].token != T_COMMA)
break;
i++;
}
if (EvalOperand(&i, tokenarray, Token_Count, &opndx[j], 0) == ERROR)
return(ERROR);
if (opndx[j].kind == EXPR_REG)
{
infSize = SizeFromRegister(opndx[j].base_reg->tokval);
break;
}
}
i = oldi;
/* ************************************************************** */
/* Get Instruction Arguments (Up to 4 with AVXSUPP) */
/* ************************************************************** */
for (j = 0; j < sizeof(opndx) / sizeof(opndx[0]) && tokenarray[i].token != T_FINAL; j++)
{
if (j)
{
if (tokenarray[i].token != T_COMMA)
break;
i++;
}
if (EvalOperand(&i, tokenarray, Token_Count, &opndx[j], 0) == ERROR)
return(ERROR);
/* ********************************************************************************************************************************* */
/* UASM 2.37: For immediate indirect memory addresses, allow DS override assumption in 32 and 64bit, and apply memory size info */
/* ********************************************************************************************************************************* */
if (opndx[j].kind == EXPR_CONST && opndx[j].isptr)
{
CodeInfo.isptr = TRUE;
if (opndx[j].mem_type == MT_EMPTY)
{
switch (infSize)
{
case 1:
opndx[j].mem_type = MT_BYTE;
break;
case 2:
opndx[j].mem_type = MT_WORD;
break;
case 4:
opndx[j].mem_type = MT_DWORD;
break;
case 8:
opndx[j].mem_type = MT_QWORD;
break;
case 16:
opndx[j].mem_type = MT_OWORD;
break;
case 32:
opndx[j].mem_type = MT_YMMWORD;
break;
case 64:
opndx[j].mem_type = MT_ZMMWORD;
break;
}
}
opndx[j].kind = EXPR_ADDR;
if(ModuleInfo.Ofssize != USE64)
opndx[j].override = &dsOver;
}
if (j == 2 && (opndx[j].kind == EXPR_REG))
{
regtok = opndx[OPND3].base_reg->tokval;
CodeInfo.reg3 = GetRegNo(regtok);
}
switch (opndx[j].kind)
{
case EXPR_FLOAT:
/* v2.06: accept float constants for PUSH */
if (j == OPND2 || CodeInfo.token == T_PUSH || CodeInfo.token == T_PUSHD)
{
#if FPIMMEDIATE
if (Options.strict_masm_compat == FALSE)
{
/* convert to REAL4, unless REAL8 coercion is requested */
atofloat(&opndx[j].fvalue, opndx[j].float_tok->string_ptr, opndx[j].mem_type == MT_REAL8 ? 8 : 4, opndx[j].negative, opndx[j].float_tok->floattype);
opndx[j].kind = EXPR_CONST;
opndx[j].float_tok = NULL;
break;
}
#endif
/* Masm message is: real or BCD number not allowed */
return(EmitError(FP_INITIALIZER_IGNORED));
}
/* Handle EVEX Static Rounding Mode {sae}, {rn-sae}, {rd-sae}, {ru-sae}, {rz-sae} */
case EXPR_DECORATOR:
if (opndx[j - 1].indirect || opndx[j - 2].indirect)
return(EmitError(EMBEDDED_ROUNDING_IS_AVAILABLE_ONLY_WITH_REG_REG_OP));
CodeInfo.evex_sae = opndx[j].saeflags;
CodeInfo.evex_flag = TRUE; /* {sae} must set evex_flag v247.2 */
j--;
break;
case EXPR_EMPTY:
if (i == Token_Count)
i--; /* v2.08: if there was a terminating comma, display it */
case EXPR_ERROR:
return(EmitErr(SYNTAX_ERROR_EX, tokenarray[i].string_ptr));
}
opndCount = j;
}
opndCount++; // Track the number of operands for CodeGenV2.
if (tokenarray[i].token != T_FINAL)
{
return(EmitErr(SYNTAX_ERROR_EX, tokenarray[i].tokpos));
}
/* ********************************************************* */
/* UASM 2.36 SIMD aligned check */
/* ********************************************************* */
if (opndx[0].kind == EXPR_REG && (GetValueSp(opndx[0].base_reg->tokval) == OP_XMM ||
GetValueSp(opndx[0].base_reg->tokval) == OP_YMM|| GetValueSp(opndx[0].base_reg->tokval) == OP_ZMM))
{
if (GetValueSp(opndx[0].base_reg->tokval) == OP_XMM)
alignCheck = 16;
else if (GetValueSp(opndx[0].base_reg->tokval) == OP_YMM)
alignCheck = 32;
else if (GetValueSp(opndx[0].base_reg->tokval) == OP_ZMM)
{
CodeInfo.zreg = 1;
alignCheck = 64;
}
if (opndx[1].kind == EXPR_ADDR && opndx[1].sym)
{
/* Check the symbol size, if it's compatible force xmmword/ymmword type */
if (opndx[1].sym->total_size == alignCheck && !IsScalarSimdInstr(CodeInfo.token) )
{
if (alignCheck == 16)
opndx[1].mem_type = MT_OWORD;
else if (alignCheck == 32)
opndx[1].mem_type = MT_YMMWORD;
else
opndx[1].mem_type = MT_ZMMWORD;
}
if (CodeInfo.token == T_MOVAPS || CodeInfo.token == T_VMOVAPS || CodeInfo.token == T_MOVDQA ||
CodeInfo.token == T_VMOVDQA || CodeInfo.token == T_MOVAPD || CodeInfo.token == T_VMOVAPD || CodeInfo.token == T_MOVNTDQA || CodeInfo.token == T_VMOVNTDQA)
{
if (opndx[1].sym->state != SYM_STACK && (opndx[1].sym->offset % alignCheck != 0) && Parse_Pass == PASS_2)
EmitWarn(2, UNALIGNED_SIMD_USE);
}
}
else if (opndx[2].kind == EXPR_ADDR && opndx[2].sym)
{
/* Check the symbol size, if it's compatible force xmmword/ymmword type */
if (opndx[2].sym->total_size == alignCheck && !IsScalarSimdInstr(CodeInfo.token))
{
if (alignCheck == 16)
opndx[2].mem_type = MT_OWORD;
else if (alignCheck == 32)
opndx[2].mem_type = MT_YMMWORD;
else
opndx[2].mem_type = MT_ZMMWORD;
}
}
}
else if (opndx[0].kind == EXPR_ADDR && opndx[0].sym)
{
if (opndx[1].kind == EXPR_REG && (GetValueSp(opndx[1].base_reg->tokval) == OP_XMM ||
GetValueSp(opndx[1].base_reg->tokval) == OP_YMM|| GetValueSp(opndx[1].base_reg->tokval) == OP_ZMM))
{
if (GetValueSp(opndx[1].base_reg->tokval) == OP_XMM)
alignCheck = 16;
else if (GetValueSp(opndx[1].base_reg->tokval) == OP_YMM)
alignCheck = 32;
else if (GetValueSp(opndx[1].base_reg->tokval) == OP_ZMM)
{
CodeInfo.zreg = 1;
alignCheck = 64;
}
/* Check the symbol size, if it's compatible force xmmword/ymmword type */
if (opndx[0].sym->total_size == alignCheck && !IsScalarSimdInstr(CodeInfo.token))
{
if (alignCheck == 16)
opndx[0].mem_type = MT_OWORD;
else if (alignCheck == 32)
opndx[0].mem_type = MT_YMMWORD;
else
opndx[0].mem_type = MT_ZMMWORD;
}
if (CodeInfo.token == T_MOVAPS || CodeInfo.token == T_VMOVAPS || CodeInfo.token == T_MOVDQA ||
CodeInfo.token == T_VMOVDQA || CodeInfo.token == T_MOVAPD || CodeInfo.token == T_VMOVAPD || CodeInfo.token == T_MOVNTDQA || CodeInfo.token == T_VMOVNTDQA)
{
if (opndx[0].sym->state != SYM_STACK && (opndx[0].sym->offset % alignCheck != 0) && Parse_Pass == PASS_2)
EmitWarn(2, UNALIGNED_SIMD_USE);
}
}
}
/* ********************************************************* */
/* Make copy of Code Generation structures for V2 CodeGen. */
/* ********************************************************* */
memcpy(&opndxV2, &opndx, sizeof(opndx));
/* ********************************************************* */
/* Process CodeInfo and opndx for legacy CodeGen VEX support */
/* ********************************************************* */
for (CurrOpnd = 0; CurrOpnd < j && CurrOpnd < MAX_OPND; CurrOpnd++)
{
Frame_Type = FRAME_NONE;
SegOverride = NULL; /* segreg prefix is stored in RegOverride */
CodeInfo.opnd[CurrOpnd].data32l = 0;
CodeInfo.opnd[CurrOpnd].InsFixup = NULL;
/* if encoding is VEX and destination op is XMM, YMM or memory,
* the second argument may be stored in the vexregop field. */
if (CodeInfo.token >= VEX_START && CurrOpnd == OPND2 &&
(CodeInfo.opnd[OPND1].type & (OP_XMM | OP_YMM | OP_M | OP_M256 | OP_R32 | OP_R64 | OP_K | OP_ZMM | OP_M64 | OP_M512)))
{
CodeInfo.r1type = 10000000;
CodeInfo.r2type = 10000000;
if (CodeInfo.token == T_VMOVSD || CodeInfo.token == T_VMOVSS)
{
if (opndx[1].kind == EXPR_CONST)
return(EmitErr(INVALID_INSTRUCTION_OPERANDS));
}
if (opndx[OPND1].kind == EXPR_REG)
{
regtok = opndx[OPND1].base_reg->tokval;
CodeInfo.reg1 = GetRegNo(regtok);
if (opndx[OPND1].idx_reg)
CodeInfo.indexreg = opndx[OPND1].idx_reg->bytval;
if (CodeInfo.reg1 > 15 && IsSimdReg(opndx[OPND1].base_reg))
CodeInfo.evex_flag = TRUE;
CodeInfo.r1type = GetValueSp(opndx[OPND1].base_reg->tokval);
if (CodeInfo.r1type == OP_ZMM)
{
CodeInfo.zreg = 1;
CodeInfo.evex_flag = TRUE;
}
}
if (opndx[OPND2].kind == EXPR_REG)
{
regtok = opndx[OPND2].base_reg->tokval;
CodeInfo.reg2 = GetRegNo(regtok);
if (opndx[OPND2].idx_reg)
CodeInfo.indexreg = opndx[OPND2].idx_reg->bytval;
if (CodeInfo.reg2 > 15 && IsSimdReg(opndx[OPND2].base_reg))
CodeInfo.evex_flag = TRUE;
CodeInfo.r2type = GetValueSp(opndx[OPND2].base_reg->tokval);
if (CodeInfo.r2type == OP_ZMM)
{
CodeInfo.zreg = 1;
CodeInfo.evex_flag = TRUE;
}
}
if (( (CodeInfo.token == T_ANDN) || (CodeInfo.token == T_MULX) || (CodeInfo.token == T_PEXT) || (CodeInfo.token == T_PDEP)) && (CurrOpnd == OPND2)) goto putinvex;
if (vex_flags[CodeInfo.token - VEX_START] & VX_NND)
;
else if ((vex_flags[CodeInfo.token - VEX_START] & VX_IMM) && (opndx[OPND3].kind == EXPR_CONST) && (j > 2))
;
else if ((vex_flags[CodeInfo.token - VEX_START] & VX_NMEM) && ((CodeInfo.opnd[OPND1].type & OP_M) ||
/* v2.11: VMOVSD and VMOVSS always have 2 ops if a memory op is involved */
((CodeInfo.token == T_VMOVSD || CodeInfo.token == T_VMOVSS) &&
(opndx[OPND2].kind != EXPR_REG || opndx[OPND2].indirect == TRUE))))
;
else
{
if (opndx[OPND2].kind != EXPR_REG || (!(GetValueSp(opndx[CurrOpnd].base_reg->tokval) & (OP_R32 | OP_R64 |OP_K | OP_XMM | OP_YMM | OP_ZMM))))
{
if ((CodeInfo.token < T_KMOVB) && (CodeInfo.token > T_KMOVW))
return(EmitErr(INVALID_INSTRUCTION_OPERANDS));
}
if (j <= 2)
{
DebugMsg(("ParseLine(%s,%u): avx not enough operands (%u)\n", instr, CurrOpnd, opndx[OPND2].kind, j));
}
else
/* flag VX_DST is set if an immediate is expected as operand 3 */
if ((vex_flags[CodeInfo.token - VEX_START] & VX_DST) && (opndx[OPND3].kind == EXPR_CONST))
{
if (opndx[OPND2].idx_reg)
CodeInfo.indexreg = opndx[OPND2].idx_reg->bytval;
if (opndx[OPND2].base_reg)
CodeInfo.basereg = opndx[OPND2].base_reg->bytval;
/* third operand data goes in CodeInfo.vexconst used in codegen.c */
CodeInfo.vexconst = opndx[CurrOpnd].value;
if (opndx[OPND1].base_reg)
{
/* first operand register is moved to vexregop */
/* handle VEX.NDD */
CodeInfo.vexregop = opndx[OPND1].base_reg->bytval + 1;
memcpy(&opndx[OPND1], &opndx[CurrOpnd], sizeof(opndx[0]) * 3);
CodeInfo.rm_byte = 0;
if (process_register(&CodeInfo, OPND1, opndx) == ERROR)
return(ERROR);
}
}
else if (CodeInfo.token < T_VGETMANTPD || CodeInfo.token > T_VGETMANTPS )
{
if (opndx[CurrOpnd].base_reg == NULL)
return(EmitErr(INVALID_INSTRUCTION_OPERANDS));
flags = GetValueSp(opndx[CurrOpnd].base_reg->tokval);
if (CodeInfo.opnd[OPND1].type == OP_M)
;
else
if ((flags & (OP_XMM | OP_M128)) && (CodeInfo.opnd[OPND1].type & (OP_YMM | OP_M256)) ||
(flags & (OP_YMM | OP_M256)) && (CodeInfo.opnd[OPND1].type & (OP_XMM | OP_M128)))
{
return(EmitErr(INVALID_INSTRUCTION_OPERANDS));
}
/* second operand register is moved to vexregop */
/* to be fixed: CurrOpnd is always OPND2, so use this const here */
// CodeInfo.vexdata contains I_U8 data of EXPR_CONST
putinvex:
CodeInfo.vexconst = opndx[CurrOpnd].value;
if(opndx[CurrOpnd].base_reg)
CodeInfo.vexregop = opndx[CurrOpnd].base_reg->bytval + 1;
memcpy(&opndx[CurrOpnd], &opndx[CurrOpnd + 1], sizeof(opndx[0]) * 2);
}
else
{
CodeInfo.vexconst = opndx[CurrOpnd + 1].value;
j++;
}
j--;
}
}
switch (opndx[CurrOpnd].kind)
{
case EXPR_DECORATOR:
CodeInfo.evex_sae = opndx[CurrOpnd].saeflags;
return( codegen( &CodeInfo, oldofs ) );
case EXPR_ADDR:
if (process_address(&CodeInfo, CurrOpnd, &opndx[CurrOpnd]) == ERROR)
return(ERROR);
break;
case EXPR_CONST:
if (process_const(&CodeInfo, CurrOpnd, &opndx[CurrOpnd]) == ERROR)
return(ERROR);
break;
case EXPR_REG:
if (opndx[CurrOpnd].indirect)
{
// indirect operand ( "[EBX+...]" )?
if (process_address(&CodeInfo, CurrOpnd, &opndx[CurrOpnd]) == ERROR)
return(ERROR);
}
else
{
/* process_register() can't handle 3rd operand */
if (!CodeInfo.vexregop)
{
if (CurrOpnd == OPND1)
{
regtok = opndx[OPND1].base_reg->tokval;
CodeInfo.reg1 = GetRegNo(regtok);
if (CodeInfo.reg1 > 15 && IsSimdReg(opndx[OPND1].base_reg))
CodeInfo.evex_flag = TRUE;
}
else if (CurrOpnd == OPND2)
{
regtok = opndx[OPND2].base_reg->tokval;
CodeInfo.reg2 = GetRegNo(regtok);
if (CodeInfo.reg2 > 15 && IsSimdReg(opndx[OPND2].base_reg))
CodeInfo.evex_flag = TRUE;
}
}
if (CurrOpnd == OPND3)
{
CodeInfo.opnd[OPND3].type = GetValueSp(opndx[OPND3].base_reg->tokval);
CodeInfo.opnd[OPND3].data32l = opndx[OPND3].base_reg->bytval;
regtok = opndx[OPND3].base_reg->tokval;
CodeInfo.reg3 = GetRegNo(regtok);
if (CodeInfo.reg3 > 15 && IsSimdReg(opndx[OPND3].base_reg))
CodeInfo.evex_flag = TRUE;
}
else if (process_register(&CodeInfo, CurrOpnd, opndx) == ERROR)
return(ERROR);
}
break;
}
} /* end for */
memcpy(&CodeInfoV2, &CodeInfo, sizeof(CodeInfo));
/* If the above loop removed the NDS register for VEX (opnd1[reg], opnd2[nds reg], opnd3[mem/reg], opnd4[imm]) */
if (CodeInfo.vexregop > 0)
{
memcpy(&CodeInfoV2.opnd[OPND4], &CodeInfoV2.opnd[OPND3], sizeof(struct opnd_item));
memcpy(&CodeInfoV2.opnd[OPND3], &CodeInfoV2.opnd[OPND2], sizeof(struct opnd_item));
if (process_register(&CodeInfoV2, OPND2, opndxV2) == ERROR)
return(ERROR);
}
/* ******************************************************* */
/* 4 arguments are valid for AVX only */
/* ******************************************************* */
if (CurrOpnd != j)
{
for (; tokenarray[i].token != T_COMMA; i--);
if (CodeInfo.token < VEX_START) {
return(EmitErr(SYNTAX_ERROR_EX, tokenarray[i].tokpos));
}
else
if ((CodeInfo.token == T_VMASKMOVPS || CodeInfo.token == T_VMASKMOVPD) && (j < 3))
return(EmitErr(MISSING_OPERATOR_IN_EXPRESSION));
}
if (CodeInfo.token == T_VBLENDVPS || CodeInfo.token == T_VBLENDVPD)
{
if (CodeInfo.opnd[OPND3].type == OP_NONE)
return (EmitErr(MISSING_OPERATOR_IN_EXPRESSION));
}
/* ******************************************************* */
/* for FAR calls/jmps some special handling is required:
* in the instruction tables, the "far" entries are located BEHIND
* the "near" entries, that's why it's needed to skip all items
* until the next "first" item is found. */
/* ******************************************************* */
if (CodeInfo.isfar)
{
if (CodeInfo.token == T_CALL || CodeInfo.token == T_JMP)
{
do
{
CodeInfo.pinstr++;
} while (CodeInfo.pinstr->first == FALSE);
}
}
/* ******************************************************* */
/* special handling for string instructions */
/* ******************************************************* */
if (CodeInfo.pinstr->allowed_prefix == AP_REP || CodeInfo.pinstr->allowed_prefix == AP_REPxx)
{
HandleStringInstructions(&CodeInfo, opndx);
#if SVMSUPP /* v2.09, not active because a bit too hackish yet - it "works", though. */
}
else if ( CodeInfo.token >= T_VMRUN && CodeInfo.token <= T_INVLPGA && CodeInfo.pinstr->opclsidx ) {
/* the size of the first operand is to trigger the address size byte 67h,
* not the operand size byte 66h! */
CodeInfo.prefix.adrsiz = CodeInfo.prefix.opsiz;
CodeInfo.prefix.opsiz = 0;
/* the first op must be EAX/AX or RAX/EAX. The operand class
* used in the instruction table is OP_A ( which is AL/AX/EAX/RAX ). */
if ( ( CodeInfo.opnd[OPND1].type & ( CodeInfo.Ofssize == USE64 ? OP_R64 | OP_R32 : OP_R32 | OP_R16 ) ) == 0 ) {
DebugMsg(("ParseLine(%s): opnd1 unexpected type=%X\n", instr, CodeInfo.opnd[OPND1].type ));
return( EmitErr( INVALID_INSTRUCTION_OPERANDS ) );
}
/* the INVLPGA instruction has a fix second operand (=ECX). However, there's no
* operand class for ECX alone. So it has to be ensured here that the register IS ecx. */
if ( CodeInfo.token == T_INVLPGA )
if ( ( CodeInfo.rm_byte & BIT_345 ) != ( 1 << 3 ) ) { /* ECX is register 1 */
DebugMsg(("ParseLine(%s): opnd2 is not ecx\n", instr ));
return( EmitErr( INVALID_INSTRUCTION_OPERANDS ) );
}
#endif
}
else
{
if (CurrOpnd > 1)
{
/* v1.96: check if a third argument is ok */
if (CurrOpnd > 2)
{
do
{
if ((opnd_clstab[CodeInfo.pinstr->opclsidx].opnd_type_3rd != OP3_NONE) || (opndx[CurrOpnd].kind == EXPR_DECORATOR))
{
if (opndx[CurrOpnd].kind == EXPR_DECORATOR)
CodeInfo.evex_sae = opndx[CurrOpnd].saeflags;
break;
}
/* workaround for codegenv2 because there is no CodeInfo.pinstr, we have only instruction token
* To do: find the way to avoid CodeInfo.pinstr */
if (CodeInfo.opnd[OPND3].type == OP_I8) {
switch (CodeInfo.token) {
case T_CMPPD:
case T_CMPPS:
case T_CMPSD:
case T_CMPSS:
case T_DPPD:
case T_DPPS:
case T_EXTRACTPS:
case T_INSERTPS:
case T_VCMPPD:
case T_VCMPPS:
case T_VCMPSD:
case T_VCMPSS:
case T_VDPPD:
case T_VDPPS:
case T_PCLMULQDQ:
case T_VPCLMULQDQ:
case T_VEXTRACTPS:
case T_VINSERTPS:
goto noterror;
}
}
CodeInfo.pinstr++;
if ((CodeInfo.pinstr->first == TRUE))
{
for (; tokenarray[i].token != T_COMMA; i--);
return(EmitErr(SYNTAX_ERROR_EX, tokenarray[i].tokpos));
}
} while (1);
}
noterror:
/* v2.06: moved here from process_const() */
if (CodeInfo.token == T_IMUL)
{
/* the 2-operand form with an immediate as second op
* is actually a 3-operand form. That's why the rm byte
* has to be adjusted. */
if (CodeInfo.opnd[OPND3].type == OP_NONE && (CodeInfo.opnd[OPND2].type & OP_I))
{
CodeInfo.prefix.rex |= ((CodeInfo.prefix.rex & REX_B) ? REX_R : 0);
CodeInfo.rm_byte = (CodeInfo.rm_byte & ~BIT_345) | ((CodeInfo.rm_byte & BIT_012) << 3);
}
else if ((CodeInfo.opnd[OPND3].type != OP_NONE) && (CodeInfo.opnd[OPND2].type & OP_I) && CodeInfo.opnd[OPND2].InsFixup &&
CodeInfo.opnd[OPND2].InsFixup->sym->state == SYM_UNDEFINED)
CodeInfo.opnd[OPND2].type = OP_M;
}
if (check_size(&CodeInfo, opndx) == ERROR)
return(ERROR);
}
if (CodeInfo.Ofssize == USE64)
{
if (CodeInfo.x86hi_used && CodeInfo.prefix.rex)
EmitError(INVALID_USAGE_OF_AHBHCHDH);
/* for some instructions, the "wide" flag has to be removed selectively.
* this is to be improved - by a new flag in struct instr_item. */
switch (CodeInfo.token)
{
case T_PUSH:
case T_POP:
/* v2.06: REX.W prefix is always 0, because size is either 2 or 8 */
CodeInfo.prefix.rex &= 0x7;
break;
case T_CALL:
case T_JMP:
#if VMXSUPP /* v2.09: added */
case T_VMREAD:
case T_VMWRITE:
#endif
/* v2.02: previously rex-prefix was cleared entirely,
* but bits 0-2 are needed to make "call rax" and "call r8"
* distinguishable! */
CodeInfo.prefix.rex &= 0x7;
break;
case T_MOV:
/* don't use the Wide bit for moves to/from special regs */
if (CodeInfo.opnd[OPND1].type & OP_RSPEC || CodeInfo.opnd[OPND2].type & OP_RSPEC)
CodeInfo.prefix.rex &= 0x7;
break;
case T_POR:
case T_VPOR:
if (gmaskflag)
goto nopor;
break;
}
}
}
/* UASM 2.56 prevent RIP+REG encodings */
for (i = 0; i < 4; i++) {
if (opndx[i].base_reg != NULL && opndx[i].idx_reg != NULL) {
if (opndx[i].base_reg->tokval == T_RIP && opndx[i].idx_reg != NULL) {
return EmitErr(RIP_ONLY);
}
}
}
/* *********************************************************** */
/* Use the V2 CodeGen, else fallback to the standard CodeGen */
/* *********************************************************** */
if (ModuleInfo.Ofssize == USE32 || ModuleInfo.Ofssize == USE64)
{
temp = CodeGenV2(opcodePtr, &CodeInfoV2, oldofs, opndCount, opndxV2);
if (temp == EMPTY)
temp = codegen(&CodeInfo, oldofs);
}
else
temp = codegen(&CodeInfo, oldofs);
nopor:
/* now reset EVEX maskflags for the next line */
decoflags = 0;
broadflags = 0;
evexflag = 0;
return( temp );
}
/* process a file. introduced in v2.11 */
void ProcessFile( struct asm_tok tokenarray[] )
/*********************************************/
{
/* Initialize xmmword override tokens each pass */
xmmOver0.token = 6;
xmmOver0.specval = 15;
xmmOver0.floattype = 15;
xmmOver0.numbase = 15;
xmmOver0.string_delim = 15;
xmmOver0.precedence = 15;
xmmOver0.bytval = 15;
xmmOver0.dirtype = 15;
xmmOver0.tokval = T_XMMWORD;
xmmOver0.string_ptr = "xmmword";
xmmOver0.stringlen = T_XMMWORD;
xmmOver0.idarg = T_XMMWORD;
xmmOver0.itemlen = T_XMMWORD;
xmmOver0.lastidx = T_XMMWORD;
xmmOver1.token = 5;
xmmOver1.specval = 4;
xmmOver1.floattype = 4;
xmmOver1.numbase = 4;
xmmOver1.string_delim = 4;
xmmOver1.precedence = 4;
xmmOver1.bytval = 4;
xmmOver1.dirtype = 4;
xmmOver1.tokval = T_PTR;
xmmOver1.string_ptr = "ptr";
xmmOver1.stringlen = T_PTR;
xmmOver1.idarg = T_PTR;
xmmOver1.itemlen = T_PTR;
xmmOver1.lastidx = T_PTR;
dsOver.token = 2;
dsOver.dirtype = 3;
dsOver.bytval = 3;
dsOver.precedence = 3;
dsOver.string_delim = 3;
dsOver.floattype = 3;
dsOver.numbase = 3;
dsOver.specval = 3;
dsOver.string_ptr = "ds";
dsOver.tokval = 0x0000001c;
dsOver.stringlen = 0x0000001c;
dsOver.idarg = 0x0000001c;
dsOver.itemlen = 0x0000001c;
dsOver.lastidx = 0x0000001c;
if ( ModuleInfo.EndDirFound == FALSE && GetTextLine( CurrSource ) )
{
if (CurrSource[0] == 0xEF && CurrSource[1] == 0xBB && CurrSource[2] == 0xBF)
strcpy(CurrSource, &CurrSource[3]);
do
{
if (PreprocessLine(CurrSource, tokenarray))
{
ParseLine(tokenarray);
if (Options.preprocessor_stdout == TRUE && Parse_Pass == PASS_1)
WritePreprocessedLine(CurrSource);
}
} while (ModuleInfo.EndDirFound == FALSE && GetTextLine(CurrSource));
}
return;
}