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
|
/**************************************************************************
*
* Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
**************************************************************************/
#include "pipe/p_config.h"
#if defined(PIPE_ARCH_X86)
#include "pipe/p_compiler.h"
#include "pipe/p_debug.h"
#include "util/u_pointer.h"
#include "rtasm_execmem.h"
#include "rtasm_x86sse.h"
#define DISASSEM 0
#define X86_TWOB 0x0f
#define DUMP_SSE 0
void x86_print_reg( struct x86_reg reg )
{
if (reg.mod != mod_REG)
debug_printf( "[" );
switch( reg.file ) {
case file_REG32:
switch( reg.idx ) {
case reg_AX: debug_printf( "EAX" ); break;
case reg_CX: debug_printf( "ECX" ); break;
case reg_DX: debug_printf( "EDX" ); break;
case reg_BX: debug_printf( "EBX" ); break;
case reg_SP: debug_printf( "ESP" ); break;
case reg_BP: debug_printf( "EBP" ); break;
case reg_SI: debug_printf( "ESI" ); break;
case reg_DI: debug_printf( "EDI" ); break;
}
break;
case file_MMX:
debug_printf( "MMX%u", reg.idx );
break;
case file_XMM:
debug_printf( "XMM%u", reg.idx );
break;
case file_x87:
debug_printf( "fp%u", reg.idx );
break;
}
if (reg.mod == mod_DISP8 ||
reg.mod == mod_DISP32)
debug_printf("+%d", reg.disp);
if (reg.mod != mod_REG)
debug_printf( "]" );
}
#if DUMP_SSE
#define DUMP_START() debug_printf( "\n" )
#define DUMP_END() debug_printf( "\n" )
#define DUMP() do { \
const char *foo = __FUNCTION__; \
while (*foo && *foo != '_') \
foo++; \
if (*foo) \
foo++; \
debug_printf( "\n% 4x% 15s ", p->csr - p->store, foo ); \
} while (0)
#define DUMP_I( I ) do { \
DUMP(); \
debug_printf( "%u", I ); \
} while( 0 )
#define DUMP_R( R0 ) do { \
DUMP(); \
x86_print_reg( R0 ); \
} while( 0 )
#define DUMP_RR( R0, R1 ) do { \
DUMP(); \
x86_print_reg( R0 ); \
debug_printf( ", " ); \
x86_print_reg( R1 ); \
} while( 0 )
#define DUMP_RI( R0, I ) do { \
DUMP(); \
x86_print_reg( R0 ); \
debug_printf( ", %u", I ); \
} while( 0 )
#define DUMP_RRI( R0, R1, I ) do { \
DUMP(); \
x86_print_reg( R0 ); \
debug_printf( ", " ); \
x86_print_reg( R1 ); \
debug_printf( ", %u", I ); \
} while( 0 )
#else
#define DUMP_START()
#define DUMP_END()
#define DUMP( )
#define DUMP_I( I )
#define DUMP_R( R0 )
#define DUMP_RR( R0, R1 )
#define DUMP_RI( R0, I )
#define DUMP_RRI( R0, R1, I )
#endif
static void do_realloc( struct x86_function *p )
{
if (p->store == p->error_overflow) {
p->csr = p->store;
}
else if (p->size == 0) {
p->size = 1024;
p->store = rtasm_exec_malloc(p->size);
p->csr = p->store;
}
else {
uintptr_t used = pointer_to_uintptr( p->csr ) - pointer_to_uintptr( p->store );
unsigned char *tmp = p->store;
p->size *= 2;
p->store = rtasm_exec_malloc(p->size);
if (p->store) {
memcpy(p->store, tmp, used);
p->csr = p->store + used;
}
else {
p->csr = p->store;
}
rtasm_exec_free(tmp);
}
if (p->store == NULL) {
p->store = p->csr = p->error_overflow;
p->size = sizeof(p->error_overflow);
}
}
/* Emit bytes to the instruction stream:
*/
static unsigned char *reserve( struct x86_function *p, int bytes )
{
if (p->csr + bytes - p->store > (int) p->size)
do_realloc(p);
{
unsigned char *csr = p->csr;
p->csr += bytes;
return csr;
}
}
static void emit_1b( struct x86_function *p, char b0 )
{
char *csr = (char *)reserve(p, 1);
*csr = b0;
}
static void emit_1i( struct x86_function *p, int i0 )
{
int *icsr = (int *)reserve(p, sizeof(i0));
*icsr = i0;
}
static void emit_1ub( struct x86_function *p, unsigned char b0 )
{
unsigned char *csr = reserve(p, 1);
*csr++ = b0;
}
static void emit_2ub( struct x86_function *p, unsigned char b0, unsigned char b1 )
{
unsigned char *csr = reserve(p, 2);
*csr++ = b0;
*csr++ = b1;
}
static void emit_3ub( struct x86_function *p, unsigned char b0, unsigned char b1, unsigned char b2 )
{
unsigned char *csr = reserve(p, 3);
*csr++ = b0;
*csr++ = b1;
*csr++ = b2;
}
/* Build a modRM byte + possible displacement. No treatment of SIB
* indexing. BZZT - no way to encode an absolute address.
*
* This is the "/r" field in the x86 manuals...
*/
static void emit_modrm( struct x86_function *p,
struct x86_reg reg,
struct x86_reg regmem )
{
unsigned char val = 0;
assert(reg.mod == mod_REG);
val |= regmem.mod << 6; /* mod field */
val |= reg.idx << 3; /* reg field */
val |= regmem.idx; /* r/m field */
emit_1ub(p, val);
/* Oh-oh we've stumbled into the SIB thing.
*/
if (regmem.file == file_REG32 &&
regmem.idx == reg_SP &&
regmem.mod != mod_REG) {
emit_1ub(p, 0x24); /* simplistic! */
}
switch (regmem.mod) {
case mod_REG:
case mod_INDIRECT:
break;
case mod_DISP8:
emit_1b(p, (char) regmem.disp);
break;
case mod_DISP32:
emit_1i(p, regmem.disp);
break;
default:
assert(0);
break;
}
}
/* Emits the "/0".."/7" specialized versions of the modrm ("/r") bytes.
*/
static void emit_modrm_noreg( struct x86_function *p,
unsigned op,
struct x86_reg regmem )
{
struct x86_reg dummy = x86_make_reg(file_REG32, op);
emit_modrm(p, dummy, regmem);
}
/* Many x86 instructions have two opcodes to cope with the situations
* where the destination is a register or memory reference
* respectively. This function selects the correct opcode based on
* the arguments presented.
*/
static void emit_op_modrm( struct x86_function *p,
unsigned char op_dst_is_reg,
unsigned char op_dst_is_mem,
struct x86_reg dst,
struct x86_reg src )
{
switch (dst.mod) {
case mod_REG:
emit_1ub(p, op_dst_is_reg);
emit_modrm(p, dst, src);
break;
case mod_INDIRECT:
case mod_DISP32:
case mod_DISP8:
assert(src.mod == mod_REG);
emit_1ub(p, op_dst_is_mem);
emit_modrm(p, src, dst);
break;
default:
assert(0);
break;
}
}
/* Create and manipulate registers and regmem values:
*/
struct x86_reg x86_make_reg( enum x86_reg_file file,
enum x86_reg_name idx )
{
struct x86_reg reg;
reg.file = file;
reg.idx = idx;
reg.mod = mod_REG;
reg.disp = 0;
return reg;
}
struct x86_reg x86_make_disp( struct x86_reg reg,
int disp )
{
assert(reg.file == file_REG32);
if (reg.mod == mod_REG)
reg.disp = disp;
else
reg.disp += disp;
if (reg.disp == 0 && reg.idx != reg_BP)
reg.mod = mod_INDIRECT;
else if (reg.disp <= 127 && reg.disp >= -128)
reg.mod = mod_DISP8;
else
reg.mod = mod_DISP32;
return reg;
}
struct x86_reg x86_deref( struct x86_reg reg )
{
return x86_make_disp(reg, 0);
}
struct x86_reg x86_get_base_reg( struct x86_reg reg )
{
return x86_make_reg( reg.file, reg.idx );
}
int x86_get_label( struct x86_function *p )
{
return p->csr - p->store;
}
/***********************************************************************
* x86 instructions
*/
void x86_jcc( struct x86_function *p,
enum x86_cc cc,
int label )
{
int offset = label - (x86_get_label(p) + 2);
DUMP_I(cc);
if (offset < 0) {
/*assert(p->csr - p->store > -offset);*/
if (p->csr - p->store <= -offset) {
/* probably out of memory (using the error_overflow buffer) */
return;
}
}
if (offset <= 127 && offset >= -128) {
emit_1ub(p, 0x70 + cc);
emit_1b(p, (char) offset);
}
else {
offset = label - (x86_get_label(p) + 6);
emit_2ub(p, 0x0f, 0x80 + cc);
emit_1i(p, offset);
}
}
/* Always use a 32bit offset for forward jumps:
*/
int x86_jcc_forward( struct x86_function *p,
enum x86_cc cc )
{
DUMP_I(cc);
emit_2ub(p, 0x0f, 0x80 + cc);
emit_1i(p, 0);
return x86_get_label(p);
}
int x86_jmp_forward( struct x86_function *p)
{
DUMP();
emit_1ub(p, 0xe9);
emit_1i(p, 0);
return x86_get_label(p);
}
int x86_call_forward( struct x86_function *p)
{
DUMP();
emit_1ub(p, 0xe8);
emit_1i(p, 0);
return x86_get_label(p);
}
/* Fixup offset from forward jump:
*/
void x86_fixup_fwd_jump( struct x86_function *p,
int fixup )
{
*(int *)(p->store + fixup - 4) = x86_get_label(p) - fixup;
}
void x86_jmp( struct x86_function *p, int label)
{
DUMP_I( label );
emit_1ub(p, 0xe9);
emit_1i(p, label - x86_get_label(p) - 4);
}
void x86_call( struct x86_function *p, struct x86_reg reg)
{
DUMP_R( reg );
emit_1ub(p, 0xff);
emit_modrm_noreg(p, 2, reg);
}
void x86_mov_reg_imm( struct x86_function *p, struct x86_reg dst, int imm )
{
DUMP_RI( dst, imm );
assert(dst.file == file_REG32);
assert(dst.mod == mod_REG);
emit_1ub(p, 0xb8 + dst.idx);
emit_1i(p, imm);
}
/**
* Immediate group 1 instructions.
*/
static INLINE void
x86_group1_imm( struct x86_function *p,
unsigned op, struct x86_reg dst, int imm )
{
assert(dst.file == file_REG32);
assert(dst.mod == mod_REG);
if(-0x80 <= imm && imm < 0x80) {
emit_1ub(p, 0x83);
emit_modrm_noreg(p, op, dst);
emit_1b(p, (char)imm);
}
else {
emit_1ub(p, 0x81);
emit_modrm_noreg(p, op, dst);
emit_1i(p, imm);
}
}
void x86_add_imm( struct x86_function *p, struct x86_reg dst, int imm )
{
DUMP_RI( dst, imm );
x86_group1_imm(p, 0, dst, imm);
}
void x86_or_imm( struct x86_function *p, struct x86_reg dst, int imm )
{
DUMP_RI( dst, imm );
x86_group1_imm(p, 1, dst, imm);
}
void x86_and_imm( struct x86_function *p, struct x86_reg dst, int imm )
{
DUMP_RI( dst, imm );
x86_group1_imm(p, 4, dst, imm);
}
void x86_sub_imm( struct x86_function *p, struct x86_reg dst, int imm )
{
DUMP_RI( dst, imm );
x86_group1_imm(p, 5, dst, imm);
}
void x86_xor_imm( struct x86_function *p, struct x86_reg dst, int imm )
{
DUMP_RI( dst, imm );
x86_group1_imm(p, 6, dst, imm);
}
void x86_cmp_imm( struct x86_function *p, struct x86_reg dst, int imm )
{
DUMP_RI( dst, imm );
x86_group1_imm(p, 7, dst, imm);
}
void x86_push( struct x86_function *p,
struct x86_reg reg )
{
DUMP_R( reg );
if (reg.mod == mod_REG)
emit_1ub(p, 0x50 + reg.idx);
else
{
emit_1ub(p, 0xff);
emit_modrm_noreg(p, 6, reg);
}
p->stack_offset += 4;
}
void x86_push_imm32( struct x86_function *p,
int imm32 )
{
DUMP_I( imm32 );
emit_1ub(p, 0x68);
emit_1i(p, imm32);
p->stack_offset += 4;
}
void x86_pop( struct x86_function *p,
struct x86_reg reg )
{
DUMP_R( reg );
assert(reg.mod == mod_REG);
emit_1ub(p, 0x58 + reg.idx);
p->stack_offset -= 4;
}
void x86_inc( struct x86_function *p,
struct x86_reg reg )
{
DUMP_R( reg );
assert(reg.mod == mod_REG);
emit_1ub(p, 0x40 + reg.idx);
}
void x86_dec( struct x86_function *p,
struct x86_reg reg )
{
DUMP_R( reg );
assert(reg.mod == mod_REG);
emit_1ub(p, 0x48 + reg.idx);
}
void x86_ret( struct x86_function *p )
{
DUMP();
assert(p->stack_offset == 0);
emit_1ub(p, 0xc3);
}
void x86_retw( struct x86_function *p, unsigned short imm )
{
DUMP();
emit_3ub(p, 0xc2, imm & 0xff, (imm >> 8) & 0xff);
}
void x86_sahf( struct x86_function *p )
{
DUMP();
emit_1ub(p, 0x9e);
}
void x86_mov( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_op_modrm( p, 0x8b, 0x89, dst, src );
}
void x86_xor( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_op_modrm( p, 0x33, 0x31, dst, src );
}
void x86_cmp( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_op_modrm( p, 0x3b, 0x39, dst, src );
}
void x86_lea( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_1ub(p, 0x8d);
emit_modrm( p, dst, src );
}
void x86_test( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_1ub(p, 0x85);
emit_modrm( p, dst, src );
}
void x86_add( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_op_modrm(p, 0x03, 0x01, dst, src );
}
/* Calculate EAX * src, results in EDX:EAX.
*/
void x86_mul( struct x86_function *p,
struct x86_reg src )
{
DUMP_R( src );
emit_1ub(p, 0xf7);
emit_modrm_noreg(p, 4, src );
}
void x86_imul( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_2ub(p, X86_TWOB, 0xAF);
emit_modrm(p, dst, src);
}
void x86_sub( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_op_modrm(p, 0x2b, 0x29, dst, src );
}
void x86_or( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_op_modrm( p, 0x0b, 0x09, dst, src );
}
void x86_and( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_op_modrm( p, 0x23, 0x21, dst, src );
}
/***********************************************************************
* SSE instructions
*/
void sse_prefetchnta( struct x86_function *p, struct x86_reg ptr)
{
DUMP_R( ptr );
assert(ptr.mod != mod_REG);
emit_2ub(p, 0x0f, 0x18);
emit_modrm_noreg(p, 0, ptr);
}
void sse_prefetch0( struct x86_function *p, struct x86_reg ptr)
{
DUMP_R( ptr );
assert(ptr.mod != mod_REG);
emit_2ub(p, 0x0f, 0x18);
emit_modrm_noreg(p, 1, ptr);
}
void sse_prefetch1( struct x86_function *p, struct x86_reg ptr)
{
DUMP_R( ptr );
assert(ptr.mod != mod_REG);
emit_2ub(p, 0x0f, 0x18);
emit_modrm_noreg(p, 2, ptr);
}
void sse_movntps( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src)
{
DUMP_RR( dst, src );
assert(dst.mod != mod_REG);
assert(src.mod == mod_REG);
emit_2ub(p, 0x0f, 0x2b);
emit_modrm(p, src, dst);
}
void sse_movss( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_2ub(p, 0xF3, X86_TWOB);
emit_op_modrm( p, 0x10, 0x11, dst, src );
}
void sse_movaps( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_1ub(p, X86_TWOB);
emit_op_modrm( p, 0x28, 0x29, dst, src );
}
void sse_movups( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_1ub(p, X86_TWOB);
emit_op_modrm( p, 0x10, 0x11, dst, src );
}
void sse_movhps( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
assert(dst.mod != mod_REG || src.mod != mod_REG);
emit_1ub(p, X86_TWOB);
emit_op_modrm( p, 0x16, 0x17, dst, src ); /* cf movlhps */
}
void sse_movlps( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
assert(dst.mod != mod_REG || src.mod != mod_REG);
emit_1ub(p, X86_TWOB);
emit_op_modrm( p, 0x12, 0x13, dst, src ); /* cf movhlps */
}
void sse_maxps( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_2ub(p, X86_TWOB, 0x5F);
emit_modrm( p, dst, src );
}
void sse_maxss( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_3ub(p, 0xF3, X86_TWOB, 0x5F);
emit_modrm( p, dst, src );
}
void sse_divss( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_3ub(p, 0xF3, X86_TWOB, 0x5E);
emit_modrm( p, dst, src );
}
void sse_minps( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_2ub(p, X86_TWOB, 0x5D);
emit_modrm( p, dst, src );
}
void sse_subps( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_2ub(p, X86_TWOB, 0x5C);
emit_modrm( p, dst, src );
}
void sse_mulps( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_2ub(p, X86_TWOB, 0x59);
emit_modrm( p, dst, src );
}
void sse_mulss( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_3ub(p, 0xF3, X86_TWOB, 0x59);
emit_modrm( p, dst, src );
}
void sse_addps( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_2ub(p, X86_TWOB, 0x58);
emit_modrm( p, dst, src );
}
void sse_addss( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_3ub(p, 0xF3, X86_TWOB, 0x58);
emit_modrm( p, dst, src );
}
void sse_andnps( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_2ub(p, X86_TWOB, 0x55);
emit_modrm( p, dst, src );
}
void sse_andps( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_2ub(p, X86_TWOB, 0x54);
emit_modrm( p, dst, src );
}
void sse_rsqrtps( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_2ub(p, X86_TWOB, 0x52);
emit_modrm( p, dst, src );
}
void sse_rsqrtss( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_3ub(p, 0xF3, X86_TWOB, 0x52);
emit_modrm( p, dst, src );
}
void sse_movhlps( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
assert(dst.mod == mod_REG && src.mod == mod_REG);
emit_2ub(p, X86_TWOB, 0x12);
emit_modrm( p, dst, src );
}
void sse_movlhps( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
assert(dst.mod == mod_REG && src.mod == mod_REG);
emit_2ub(p, X86_TWOB, 0x16);
emit_modrm( p, dst, src );
}
void sse_orps( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_2ub(p, X86_TWOB, 0x56);
emit_modrm( p, dst, src );
}
void sse_xorps( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_2ub(p, X86_TWOB, 0x57);
emit_modrm( p, dst, src );
}
void sse_cvtps2pi( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
assert(dst.file == file_MMX &&
(src.file == file_XMM || src.mod != mod_REG));
p->need_emms = 1;
emit_2ub(p, X86_TWOB, 0x2d);
emit_modrm( p, dst, src );
}
void sse2_cvtdq2ps( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_2ub(p, X86_TWOB, 0x5b);
emit_modrm( p, dst, src );
}
/* Shufps can also be used to implement a reduced swizzle when dest ==
* arg0.
*/
void sse_shufps( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src,
unsigned char shuf)
{
DUMP_RRI( dst, src, shuf );
emit_2ub(p, X86_TWOB, 0xC6);
emit_modrm(p, dst, src);
emit_1ub(p, shuf);
}
void sse_unpckhps( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
{
DUMP_RR( dst, src );
emit_2ub( p, X86_TWOB, 0x15 );
emit_modrm( p, dst, src );
}
void sse_unpcklps( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
{
DUMP_RR( dst, src );
emit_2ub( p, X86_TWOB, 0x14 );
emit_modrm( p, dst, src );
}
void sse_cmpps( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src,
enum sse_cc cc)
{
DUMP_RRI( dst, src, cc );
emit_2ub(p, X86_TWOB, 0xC2);
emit_modrm(p, dst, src);
emit_1ub(p, cc);
}
void sse_pmovmskb( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src)
{
DUMP_RR( dst, src );
emit_3ub(p, 0x66, X86_TWOB, 0xD7);
emit_modrm(p, dst, src);
}
/***********************************************************************
* SSE2 instructions
*/
/**
* Perform a reduced swizzle:
*/
void sse2_pshufd( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src,
unsigned char shuf)
{
DUMP_RRI( dst, src, shuf );
emit_3ub(p, 0x66, X86_TWOB, 0x70);
emit_modrm(p, dst, src);
emit_1ub(p, shuf);
}
void sse2_cvttps2dq( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_3ub( p, 0xF3, X86_TWOB, 0x5B );
emit_modrm( p, dst, src );
}
void sse2_cvtps2dq( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_3ub(p, 0x66, X86_TWOB, 0x5B);
emit_modrm( p, dst, src );
}
void sse2_packssdw( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_3ub(p, 0x66, X86_TWOB, 0x6B);
emit_modrm( p, dst, src );
}
void sse2_packsswb( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_3ub(p, 0x66, X86_TWOB, 0x63);
emit_modrm( p, dst, src );
}
void sse2_packuswb( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_3ub(p, 0x66, X86_TWOB, 0x67);
emit_modrm( p, dst, src );
}
void sse2_punpcklbw( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_3ub(p, 0x66, X86_TWOB, 0x60);
emit_modrm( p, dst, src );
}
void sse2_rcpps( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_2ub(p, X86_TWOB, 0x53);
emit_modrm( p, dst, src );
}
void sse2_rcpss( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_3ub(p, 0xF3, X86_TWOB, 0x53);
emit_modrm( p, dst, src );
}
void sse2_movd( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
emit_2ub(p, 0x66, X86_TWOB);
emit_op_modrm( p, 0x6e, 0x7e, dst, src );
}
/***********************************************************************
* x87 instructions
*/
static void note_x87_pop( struct x86_function *p )
{
p->x87_stack--;
assert(p->x87_stack >= 0);
}
static void note_x87_push( struct x86_function *p )
{
p->x87_stack++;
assert(p->x87_stack <= 7);
}
void x87_assert_stack_empty( struct x86_function *p )
{
assert (p->x87_stack == 0);
}
void x87_fist( struct x86_function *p, struct x86_reg dst )
{
DUMP_R( dst );
emit_1ub(p, 0xdb);
emit_modrm_noreg(p, 2, dst);
}
void x87_fistp( struct x86_function *p, struct x86_reg dst )
{
DUMP_R( dst );
emit_1ub(p, 0xdb);
emit_modrm_noreg(p, 3, dst);
note_x87_pop(p);
}
void x87_fild( struct x86_function *p, struct x86_reg arg )
{
DUMP_R( arg );
emit_1ub(p, 0xdf);
emit_modrm_noreg(p, 0, arg);
note_x87_push(p);
}
void x87_fldz( struct x86_function *p )
{
DUMP();
emit_2ub(p, 0xd9, 0xee);
note_x87_push(p);
}
void x87_fldcw( struct x86_function *p, struct x86_reg arg )
{
DUMP_R( arg );
assert(arg.file == file_REG32);
assert(arg.mod != mod_REG);
emit_1ub(p, 0xd9);
emit_modrm_noreg(p, 5, arg);
}
void x87_fld1( struct x86_function *p )
{
DUMP();
emit_2ub(p, 0xd9, 0xe8);
note_x87_push(p);
}
void x87_fldl2e( struct x86_function *p )
{
DUMP();
emit_2ub(p, 0xd9, 0xea);
note_x87_push(p);
}
void x87_fldln2( struct x86_function *p )
{
DUMP();
emit_2ub(p, 0xd9, 0xed);
note_x87_push(p);
}
void x87_fwait( struct x86_function *p )
{
DUMP();
emit_1ub(p, 0x9b);
}
void x87_fnclex( struct x86_function *p )
{
DUMP();
emit_2ub(p, 0xdb, 0xe2);
}
void x87_fclex( struct x86_function *p )
{
x87_fwait(p);
x87_fnclex(p);
}
void x87_fcmovb( struct x86_function *p, struct x86_reg arg )
{
DUMP_R( arg );
assert(arg.file == file_x87);
emit_2ub(p, 0xda, 0xc0+arg.idx);
}
void x87_fcmove( struct x86_function *p, struct x86_reg arg )
{
DUMP_R( arg );
assert(arg.file == file_x87);
emit_2ub(p, 0xda, 0xc8+arg.idx);
}
void x87_fcmovbe( struct x86_function *p, struct x86_reg arg )
{
DUMP_R( arg );
assert(arg.file == file_x87);
emit_2ub(p, 0xda, 0xd0+arg.idx);
}
void x87_fcmovnb( struct x86_function *p, struct x86_reg arg )
{
DUMP_R( arg );
assert(arg.file == file_x87);
emit_2ub(p, 0xdb, 0xc0+arg.idx);
}
void x87_fcmovne( struct x86_function *p, struct x86_reg arg )
{
DUMP_R( arg );
assert(arg.file == file_x87);
emit_2ub(p, 0xdb, 0xc8+arg.idx);
}
void x87_fcmovnbe( struct x86_function *p, struct x86_reg arg )
{
DUMP_R( arg );
assert(arg.file == file_x87);
emit_2ub(p, 0xdb, 0xd0+arg.idx);
}
static void x87_arith_op( struct x86_function *p, struct x86_reg dst, struct x86_reg arg,
unsigned char dst0ub0,
unsigned char dst0ub1,
unsigned char arg0ub0,
unsigned char arg0ub1,
unsigned char argmem_noreg)
{
assert(dst.file == file_x87);
if (arg.file == file_x87) {
if (dst.idx == 0)
emit_2ub(p, dst0ub0, dst0ub1+arg.idx);
else if (arg.idx == 0)
emit_2ub(p, arg0ub0, arg0ub1+arg.idx);
else
assert(0);
}
else if (dst.idx == 0) {
assert(arg.file == file_REG32);
emit_1ub(p, 0xd8);
emit_modrm_noreg(p, argmem_noreg, arg);
}
else
assert(0);
}
void x87_fmul( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
{
DUMP_RR( dst, src );
x87_arith_op(p, dst, src,
0xd8, 0xc8,
0xdc, 0xc8,
4);
}
void x87_fsub( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
{
DUMP_RR( dst, src );
x87_arith_op(p, dst, src,
0xd8, 0xe0,
0xdc, 0xe8,
4);
}
void x87_fsubr( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
{
DUMP_RR( dst, src );
x87_arith_op(p, dst, src,
0xd8, 0xe8,
0xdc, 0xe0,
5);
}
void x87_fadd( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
{
DUMP_RR( dst, src );
x87_arith_op(p, dst, src,
0xd8, 0xc0,
0xdc, 0xc0,
0);
}
void x87_fdiv( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
{
DUMP_RR( dst, src );
x87_arith_op(p, dst, src,
0xd8, 0xf0,
0xdc, 0xf8,
6);
}
void x87_fdivr( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
{
DUMP_RR( dst, src );
x87_arith_op(p, dst, src,
0xd8, 0xf8,
0xdc, 0xf0,
7);
}
void x87_fmulp( struct x86_function *p, struct x86_reg dst )
{
DUMP_R( dst );
assert(dst.file == file_x87);
assert(dst.idx >= 1);
emit_2ub(p, 0xde, 0xc8+dst.idx);
note_x87_pop(p);
}
void x87_fsubp( struct x86_function *p, struct x86_reg dst )
{
DUMP_R( dst );
assert(dst.file == file_x87);
assert(dst.idx >= 1);
emit_2ub(p, 0xde, 0xe8+dst.idx);
note_x87_pop(p);
}
void x87_fsubrp( struct x86_function *p, struct x86_reg dst )
{
DUMP_R( dst );
assert(dst.file == file_x87);
assert(dst.idx >= 1);
emit_2ub(p, 0xde, 0xe0+dst.idx);
note_x87_pop(p);
}
void x87_faddp( struct x86_function *p, struct x86_reg dst )
{
DUMP_R( dst );
assert(dst.file == file_x87);
assert(dst.idx >= 1);
emit_2ub(p, 0xde, 0xc0+dst.idx);
note_x87_pop(p);
}
void x87_fdivp( struct x86_function *p, struct x86_reg dst )
{
DUMP_R( dst );
assert(dst.file == file_x87);
assert(dst.idx >= 1);
emit_2ub(p, 0xde, 0xf8+dst.idx);
note_x87_pop(p);
}
void x87_fdivrp( struct x86_function *p, struct x86_reg dst )
{
DUMP_R( dst );
assert(dst.file == file_x87);
assert(dst.idx >= 1);
emit_2ub(p, 0xde, 0xf0+dst.idx);
note_x87_pop(p);
}
void x87_ftst( struct x86_function *p )
{
DUMP();
emit_2ub(p, 0xd9, 0xe4);
}
void x87_fucom( struct x86_function *p, struct x86_reg arg )
{
DUMP_R( arg );
assert(arg.file == file_x87);
emit_2ub(p, 0xdd, 0xe0+arg.idx);
}
void x87_fucomp( struct x86_function *p, struct x86_reg arg )
{
DUMP_R( arg );
assert(arg.file == file_x87);
emit_2ub(p, 0xdd, 0xe8+arg.idx);
note_x87_pop(p);
}
void x87_fucompp( struct x86_function *p )
{
DUMP();
emit_2ub(p, 0xda, 0xe9);
note_x87_pop(p); /* pop twice */
note_x87_pop(p); /* pop twice */
}
void x87_fxch( struct x86_function *p, struct x86_reg arg )
{
DUMP_R( arg );
assert(arg.file == file_x87);
emit_2ub(p, 0xd9, 0xc8+arg.idx);
}
void x87_fabs( struct x86_function *p )
{
DUMP();
emit_2ub(p, 0xd9, 0xe1);
}
void x87_fchs( struct x86_function *p )
{
DUMP();
emit_2ub(p, 0xd9, 0xe0);
}
void x87_fcos( struct x86_function *p )
{
DUMP();
emit_2ub(p, 0xd9, 0xff);
}
void x87_fprndint( struct x86_function *p )
{
DUMP();
emit_2ub(p, 0xd9, 0xfc);
}
void x87_fscale( struct x86_function *p )
{
DUMP();
emit_2ub(p, 0xd9, 0xfd);
}
void x87_fsin( struct x86_function *p )
{
DUMP();
emit_2ub(p, 0xd9, 0xfe);
}
void x87_fsincos( struct x86_function *p )
{
DUMP();
emit_2ub(p, 0xd9, 0xfb);
}
void x87_fsqrt( struct x86_function *p )
{
DUMP();
emit_2ub(p, 0xd9, 0xfa);
}
void x87_fxtract( struct x86_function *p )
{
DUMP();
emit_2ub(p, 0xd9, 0xf4);
}
/* st0 = (2^st0)-1
*
* Restrictions: -1.0 <= st0 <= 1.0
*/
void x87_f2xm1( struct x86_function *p )
{
DUMP();
emit_2ub(p, 0xd9, 0xf0);
}
/* st1 = st1 * log2(st0);
* pop_stack;
*/
void x87_fyl2x( struct x86_function *p )
{
DUMP();
emit_2ub(p, 0xd9, 0xf1);
note_x87_pop(p);
}
/* st1 = st1 * log2(st0 + 1.0);
* pop_stack;
*
* A fast operation, with restrictions: -.29 < st0 < .29
*/
void x87_fyl2xp1( struct x86_function *p )
{
DUMP();
emit_2ub(p, 0xd9, 0xf9);
note_x87_pop(p);
}
void x87_fld( struct x86_function *p, struct x86_reg arg )
{
DUMP_R( arg );
if (arg.file == file_x87)
emit_2ub(p, 0xd9, 0xc0 + arg.idx);
else {
emit_1ub(p, 0xd9);
emit_modrm_noreg(p, 0, arg);
}
note_x87_push(p);
}
void x87_fst( struct x86_function *p, struct x86_reg dst )
{
DUMP_R( dst );
if (dst.file == file_x87)
emit_2ub(p, 0xdd, 0xd0 + dst.idx);
else {
emit_1ub(p, 0xd9);
emit_modrm_noreg(p, 2, dst);
}
}
void x87_fstp( struct x86_function *p, struct x86_reg dst )
{
DUMP_R( dst );
if (dst.file == file_x87)
emit_2ub(p, 0xdd, 0xd8 + dst.idx);
else {
emit_1ub(p, 0xd9);
emit_modrm_noreg(p, 3, dst);
}
note_x87_pop(p);
}
void x87_fpop( struct x86_function *p )
{
x87_fstp( p, x86_make_reg( file_x87, 0 ));
}
void x87_fcom( struct x86_function *p, struct x86_reg dst )
{
DUMP_R( dst );
if (dst.file == file_x87)
emit_2ub(p, 0xd8, 0xd0 + dst.idx);
else {
emit_1ub(p, 0xd8);
emit_modrm_noreg(p, 2, dst);
}
}
void x87_fcomp( struct x86_function *p, struct x86_reg dst )
{
DUMP_R( dst );
if (dst.file == file_x87)
emit_2ub(p, 0xd8, 0xd8 + dst.idx);
else {
emit_1ub(p, 0xd8);
emit_modrm_noreg(p, 3, dst);
}
note_x87_pop(p);
}
void x87_fcomi( struct x86_function *p, struct x86_reg arg )
{
DUMP_R( arg );
emit_2ub(p, 0xdb, 0xf0+arg.idx);
}
void x87_fcomip( struct x86_function *p, struct x86_reg arg )
{
DUMP_R( arg );
emit_2ub(p, 0xdb, 0xf0+arg.idx);
note_x87_pop(p);
}
void x87_fnstsw( struct x86_function *p, struct x86_reg dst )
{
DUMP_R( dst );
assert(dst.file == file_REG32);
if (dst.idx == reg_AX &&
dst.mod == mod_REG)
emit_2ub(p, 0xdf, 0xe0);
else {
emit_1ub(p, 0xdd);
emit_modrm_noreg(p, 7, dst);
}
}
void x87_fnstcw( struct x86_function *p, struct x86_reg dst )
{
DUMP_R( dst );
assert(dst.file == file_REG32);
emit_1ub(p, 0x9b); /* WAIT -- needed? */
emit_1ub(p, 0xd9);
emit_modrm_noreg(p, 7, dst);
}
/***********************************************************************
* MMX instructions
*/
void mmx_emms( struct x86_function *p )
{
DUMP();
assert(p->need_emms);
emit_2ub(p, 0x0f, 0x77);
p->need_emms = 0;
}
void mmx_packssdw( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
assert(dst.file == file_MMX &&
(src.file == file_MMX || src.mod != mod_REG));
p->need_emms = 1;
emit_2ub(p, X86_TWOB, 0x6b);
emit_modrm( p, dst, src );
}
void mmx_packuswb( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
assert(dst.file == file_MMX &&
(src.file == file_MMX || src.mod != mod_REG));
p->need_emms = 1;
emit_2ub(p, X86_TWOB, 0x67);
emit_modrm( p, dst, src );
}
void mmx_movd( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
p->need_emms = 1;
emit_1ub(p, X86_TWOB);
emit_op_modrm( p, 0x6e, 0x7e, dst, src );
}
void mmx_movq( struct x86_function *p,
struct x86_reg dst,
struct x86_reg src )
{
DUMP_RR( dst, src );
p->need_emms = 1;
emit_1ub(p, X86_TWOB);
emit_op_modrm( p, 0x6f, 0x7f, dst, src );
}
/***********************************************************************
* Helper functions
*/
void x86_cdecl_caller_push_regs( struct x86_function *p )
{
x86_push(p, x86_make_reg(file_REG32, reg_AX));
x86_push(p, x86_make_reg(file_REG32, reg_CX));
x86_push(p, x86_make_reg(file_REG32, reg_DX));
}
void x86_cdecl_caller_pop_regs( struct x86_function *p )
{
x86_pop(p, x86_make_reg(file_REG32, reg_DX));
x86_pop(p, x86_make_reg(file_REG32, reg_CX));
x86_pop(p, x86_make_reg(file_REG32, reg_AX));
}
/* Retreive a reference to one of the function arguments, taking into
* account any push/pop activity:
*/
struct x86_reg x86_fn_arg( struct x86_function *p,
unsigned arg )
{
return x86_make_disp(x86_make_reg(file_REG32, reg_SP),
p->stack_offset + arg * 4); /* ??? */
}
void x86_init_func( struct x86_function *p )
{
p->size = 0;
p->store = NULL;
p->csr = p->store;
DUMP_START();
}
void x86_init_func_size( struct x86_function *p, unsigned code_size )
{
p->size = code_size;
p->store = rtasm_exec_malloc(code_size);
if (p->store == NULL) {
p->store = p->error_overflow;
}
p->csr = p->store;
DUMP_START();
}
void x86_release_func( struct x86_function *p )
{
if (p->store && p->store != p->error_overflow)
rtasm_exec_free(p->store);
p->store = NULL;
p->csr = NULL;
p->size = 0;
}
void (*x86_get_func( struct x86_function *p ))(void)
{
DUMP_END();
if (DISASSEM && p->store)
debug_printf("disassemble %p %p\n", p->store, p->csr);
if (p->store == p->error_overflow)
return (void (*)(void)) NULL;
else
return (void (*)(void)) p->store;
}
#else
void x86sse_dummy( void )
{
}
#endif
|