translate: add support for 8/16-bit indices
[mesa.git] / src / gallium / auxiliary / rtasm / rtasm_x86sse.c
1 /**************************************************************************
2 *
3 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 **************************************************************************/
23
24 #include "pipe/p_config.h"
25
26 #if defined(PIPE_ARCH_X86)
27
28 #include "pipe/p_compiler.h"
29 #include "util/u_debug.h"
30 #include "util/u_pointer.h"
31
32 #include "rtasm_execmem.h"
33 #include "rtasm_x86sse.h"
34
35 #define DISASSEM 0
36 #define X86_TWOB 0x0f
37
38
39 #define DUMP_SSE 0
40
41
42 void x86_print_reg( struct x86_reg reg )
43 {
44 if (reg.mod != mod_REG)
45 debug_printf( "[" );
46
47 switch( reg.file ) {
48 case file_REG32:
49 switch( reg.idx ) {
50 case reg_AX: debug_printf( "EAX" ); break;
51 case reg_CX: debug_printf( "ECX" ); break;
52 case reg_DX: debug_printf( "EDX" ); break;
53 case reg_BX: debug_printf( "EBX" ); break;
54 case reg_SP: debug_printf( "ESP" ); break;
55 case reg_BP: debug_printf( "EBP" ); break;
56 case reg_SI: debug_printf( "ESI" ); break;
57 case reg_DI: debug_printf( "EDI" ); break;
58 }
59 break;
60 case file_MMX:
61 debug_printf( "MMX%u", reg.idx );
62 break;
63 case file_XMM:
64 debug_printf( "XMM%u", reg.idx );
65 break;
66 case file_x87:
67 debug_printf( "fp%u", reg.idx );
68 break;
69 }
70
71 if (reg.mod == mod_DISP8 ||
72 reg.mod == mod_DISP32)
73 debug_printf("+%d", reg.disp);
74
75 if (reg.mod != mod_REG)
76 debug_printf( "]" );
77 }
78
79 #if DUMP_SSE
80
81 #define DUMP_START() debug_printf( "\n" )
82 #define DUMP_END() debug_printf( "\n" )
83
84 #define DUMP() do { \
85 const char *foo = __FUNCTION__; \
86 while (*foo && *foo != '_') \
87 foo++; \
88 if (*foo) \
89 foo++; \
90 debug_printf( "\n%4x %14s ", p->csr - p->store, foo ); \
91 } while (0)
92
93 #define DUMP_I( I ) do { \
94 DUMP(); \
95 debug_printf( "%u", I ); \
96 } while( 0 )
97
98 #define DUMP_R( R0 ) do { \
99 DUMP(); \
100 x86_print_reg( R0 ); \
101 } while( 0 )
102
103 #define DUMP_RR( R0, R1 ) do { \
104 DUMP(); \
105 x86_print_reg( R0 ); \
106 debug_printf( ", " ); \
107 x86_print_reg( R1 ); \
108 } while( 0 )
109
110 #define DUMP_RI( R0, I ) do { \
111 DUMP(); \
112 x86_print_reg( R0 ); \
113 debug_printf( ", %u", I ); \
114 } while( 0 )
115
116 #define DUMP_RRI( R0, R1, I ) do { \
117 DUMP(); \
118 x86_print_reg( R0 ); \
119 debug_printf( ", " ); \
120 x86_print_reg( R1 ); \
121 debug_printf( ", %u", I ); \
122 } while( 0 )
123
124 #else
125
126 #define DUMP_START()
127 #define DUMP_END()
128 #define DUMP( )
129 #define DUMP_I( I )
130 #define DUMP_R( R0 )
131 #define DUMP_RR( R0, R1 )
132 #define DUMP_RI( R0, I )
133 #define DUMP_RRI( R0, R1, I )
134
135 #endif
136
137
138 static void do_realloc( struct x86_function *p )
139 {
140 if (p->store == p->error_overflow) {
141 p->csr = p->store;
142 }
143 else if (p->size == 0) {
144 p->size = 1024;
145 p->store = rtasm_exec_malloc(p->size);
146 p->csr = p->store;
147 }
148 else {
149 uintptr_t used = pointer_to_uintptr( p->csr ) - pointer_to_uintptr( p->store );
150 unsigned char *tmp = p->store;
151 p->size *= 2;
152 p->store = rtasm_exec_malloc(p->size);
153
154 if (p->store) {
155 memcpy(p->store, tmp, used);
156 p->csr = p->store + used;
157 }
158 else {
159 p->csr = p->store;
160 }
161
162 rtasm_exec_free(tmp);
163 }
164
165 if (p->store == NULL) {
166 p->store = p->csr = p->error_overflow;
167 p->size = sizeof(p->error_overflow);
168 }
169 }
170
171 /* Emit bytes to the instruction stream:
172 */
173 static unsigned char *reserve( struct x86_function *p, int bytes )
174 {
175 if (p->csr + bytes - p->store > (int) p->size)
176 do_realloc(p);
177
178 {
179 unsigned char *csr = p->csr;
180 p->csr += bytes;
181 return csr;
182 }
183 }
184
185
186
187 static void emit_1b( struct x86_function *p, char b0 )
188 {
189 char *csr = (char *)reserve(p, 1);
190 *csr = b0;
191 }
192
193 static void emit_1i( struct x86_function *p, int i0 )
194 {
195 int *icsr = (int *)reserve(p, sizeof(i0));
196 *icsr = i0;
197 }
198
199 static void emit_1ub( struct x86_function *p, unsigned char b0 )
200 {
201 unsigned char *csr = reserve(p, 1);
202 *csr++ = b0;
203 }
204
205 static void emit_2ub( struct x86_function *p, unsigned char b0, unsigned char b1 )
206 {
207 unsigned char *csr = reserve(p, 2);
208 *csr++ = b0;
209 *csr++ = b1;
210 }
211
212 static void emit_3ub( struct x86_function *p, unsigned char b0, unsigned char b1, unsigned char b2 )
213 {
214 unsigned char *csr = reserve(p, 3);
215 *csr++ = b0;
216 *csr++ = b1;
217 *csr++ = b2;
218 }
219
220
221 /* Build a modRM byte + possible displacement. No treatment of SIB
222 * indexing. BZZT - no way to encode an absolute address.
223 *
224 * This is the "/r" field in the x86 manuals...
225 */
226 static void emit_modrm( struct x86_function *p,
227 struct x86_reg reg,
228 struct x86_reg regmem )
229 {
230 unsigned char val = 0;
231
232 assert(reg.mod == mod_REG);
233
234 val |= regmem.mod << 6; /* mod field */
235 val |= reg.idx << 3; /* reg field */
236 val |= regmem.idx; /* r/m field */
237
238 emit_1ub(p, val);
239
240 /* Oh-oh we've stumbled into the SIB thing.
241 */
242 if (regmem.file == file_REG32 &&
243 regmem.idx == reg_SP &&
244 regmem.mod != mod_REG) {
245 emit_1ub(p, 0x24); /* simplistic! */
246 }
247
248 switch (regmem.mod) {
249 case mod_REG:
250 case mod_INDIRECT:
251 break;
252 case mod_DISP8:
253 emit_1b(p, (char) regmem.disp);
254 break;
255 case mod_DISP32:
256 emit_1i(p, regmem.disp);
257 break;
258 default:
259 assert(0);
260 break;
261 }
262 }
263
264 /* Emits the "/0".."/7" specialized versions of the modrm ("/r") bytes.
265 */
266 static void emit_modrm_noreg( struct x86_function *p,
267 unsigned op,
268 struct x86_reg regmem )
269 {
270 struct x86_reg dummy = x86_make_reg(file_REG32, op);
271 emit_modrm(p, dummy, regmem);
272 }
273
274 /* Many x86 instructions have two opcodes to cope with the situations
275 * where the destination is a register or memory reference
276 * respectively. This function selects the correct opcode based on
277 * the arguments presented.
278 */
279 static void emit_op_modrm( struct x86_function *p,
280 unsigned char op_dst_is_reg,
281 unsigned char op_dst_is_mem,
282 struct x86_reg dst,
283 struct x86_reg src )
284 {
285 switch (dst.mod) {
286 case mod_REG:
287 emit_1ub(p, op_dst_is_reg);
288 emit_modrm(p, dst, src);
289 break;
290 case mod_INDIRECT:
291 case mod_DISP32:
292 case mod_DISP8:
293 assert(src.mod == mod_REG);
294 emit_1ub(p, op_dst_is_mem);
295 emit_modrm(p, src, dst);
296 break;
297 default:
298 assert(0);
299 break;
300 }
301 }
302
303
304
305
306
307
308
309 /* Create and manipulate registers and regmem values:
310 */
311 struct x86_reg x86_make_reg( enum x86_reg_file file,
312 enum x86_reg_name idx )
313 {
314 struct x86_reg reg;
315
316 reg.file = file;
317 reg.idx = idx;
318 reg.mod = mod_REG;
319 reg.disp = 0;
320
321 return reg;
322 }
323
324 struct x86_reg x86_make_disp( struct x86_reg reg,
325 int disp )
326 {
327 assert(reg.file == file_REG32);
328
329 if (reg.mod == mod_REG)
330 reg.disp = disp;
331 else
332 reg.disp += disp;
333
334 if (reg.disp == 0 && reg.idx != reg_BP)
335 reg.mod = mod_INDIRECT;
336 else if (reg.disp <= 127 && reg.disp >= -128)
337 reg.mod = mod_DISP8;
338 else
339 reg.mod = mod_DISP32;
340
341 return reg;
342 }
343
344 struct x86_reg x86_deref( struct x86_reg reg )
345 {
346 return x86_make_disp(reg, 0);
347 }
348
349 struct x86_reg x86_get_base_reg( struct x86_reg reg )
350 {
351 return x86_make_reg( reg.file, reg.idx );
352 }
353
354 int x86_get_label( struct x86_function *p )
355 {
356 return p->csr - p->store;
357 }
358
359
360
361 /***********************************************************************
362 * x86 instructions
363 */
364
365
366 void x86_jcc( struct x86_function *p,
367 enum x86_cc cc,
368 int label )
369 {
370 int offset = label - (x86_get_label(p) + 2);
371 DUMP_I(cc);
372
373 if (offset < 0) {
374 /*assert(p->csr - p->store > -offset);*/
375 if (p->csr - p->store <= -offset) {
376 /* probably out of memory (using the error_overflow buffer) */
377 return;
378 }
379 }
380
381 if (offset <= 127 && offset >= -128) {
382 emit_1ub(p, 0x70 + cc);
383 emit_1b(p, (char) offset);
384 }
385 else {
386 offset = label - (x86_get_label(p) + 6);
387 emit_2ub(p, 0x0f, 0x80 + cc);
388 emit_1i(p, offset);
389 }
390 }
391
392 /* Always use a 32bit offset for forward jumps:
393 */
394 int x86_jcc_forward( struct x86_function *p,
395 enum x86_cc cc )
396 {
397 DUMP_I(cc);
398 emit_2ub(p, 0x0f, 0x80 + cc);
399 emit_1i(p, 0);
400 return x86_get_label(p);
401 }
402
403 int x86_jmp_forward( struct x86_function *p)
404 {
405 DUMP();
406 emit_1ub(p, 0xe9);
407 emit_1i(p, 0);
408 return x86_get_label(p);
409 }
410
411 int x86_call_forward( struct x86_function *p)
412 {
413 DUMP();
414
415 emit_1ub(p, 0xe8);
416 emit_1i(p, 0);
417 return x86_get_label(p);
418 }
419
420 /* Fixup offset from forward jump:
421 */
422 void x86_fixup_fwd_jump( struct x86_function *p,
423 int fixup )
424 {
425 *(int *)(p->store + fixup - 4) = x86_get_label(p) - fixup;
426 }
427
428 void x86_jmp( struct x86_function *p, int label)
429 {
430 DUMP_I( label );
431 emit_1ub(p, 0xe9);
432 emit_1i(p, label - x86_get_label(p) - 4);
433 }
434
435 void x86_call( struct x86_function *p, struct x86_reg reg)
436 {
437 DUMP_R( reg );
438 emit_1ub(p, 0xff);
439 emit_modrm_noreg(p, 2, reg);
440 }
441
442
443 void x86_mov_reg_imm( struct x86_function *p, struct x86_reg dst, int imm )
444 {
445 DUMP_RI( dst, imm );
446 assert(dst.file == file_REG32);
447 assert(dst.mod == mod_REG);
448 emit_1ub(p, 0xb8 + dst.idx);
449 emit_1i(p, imm);
450 }
451
452 /**
453 * Immediate group 1 instructions.
454 */
455 static INLINE void
456 x86_group1_imm( struct x86_function *p,
457 unsigned op, struct x86_reg dst, int imm )
458 {
459 assert(dst.file == file_REG32);
460 assert(dst.mod == mod_REG);
461 if(-0x80 <= imm && imm < 0x80) {
462 emit_1ub(p, 0x83);
463 emit_modrm_noreg(p, op, dst);
464 emit_1b(p, (char)imm);
465 }
466 else {
467 emit_1ub(p, 0x81);
468 emit_modrm_noreg(p, op, dst);
469 emit_1i(p, imm);
470 }
471 }
472
473 void x86_add_imm( struct x86_function *p, struct x86_reg dst, int imm )
474 {
475 DUMP_RI( dst, imm );
476 x86_group1_imm(p, 0, dst, imm);
477 }
478
479 void x86_or_imm( struct x86_function *p, struct x86_reg dst, int imm )
480 {
481 DUMP_RI( dst, imm );
482 x86_group1_imm(p, 1, dst, imm);
483 }
484
485 void x86_and_imm( struct x86_function *p, struct x86_reg dst, int imm )
486 {
487 DUMP_RI( dst, imm );
488 x86_group1_imm(p, 4, dst, imm);
489 }
490
491 void x86_sub_imm( struct x86_function *p, struct x86_reg dst, int imm )
492 {
493 DUMP_RI( dst, imm );
494 x86_group1_imm(p, 5, dst, imm);
495 }
496
497 void x86_xor_imm( struct x86_function *p, struct x86_reg dst, int imm )
498 {
499 DUMP_RI( dst, imm );
500 x86_group1_imm(p, 6, dst, imm);
501 }
502
503 void x86_cmp_imm( struct x86_function *p, struct x86_reg dst, int imm )
504 {
505 DUMP_RI( dst, imm );
506 x86_group1_imm(p, 7, dst, imm);
507 }
508
509
510 void x86_push( struct x86_function *p,
511 struct x86_reg reg )
512 {
513 DUMP_R( reg );
514 if (reg.mod == mod_REG)
515 emit_1ub(p, 0x50 + reg.idx);
516 else
517 {
518 emit_1ub(p, 0xff);
519 emit_modrm_noreg(p, 6, reg);
520 }
521
522
523 p->stack_offset += 4;
524 }
525
526 void x86_push_imm32( struct x86_function *p,
527 int imm32 )
528 {
529 DUMP_I( imm32 );
530 emit_1ub(p, 0x68);
531 emit_1i(p, imm32);
532
533 p->stack_offset += 4;
534 }
535
536
537 void x86_pop( struct x86_function *p,
538 struct x86_reg reg )
539 {
540 DUMP_R( reg );
541 assert(reg.mod == mod_REG);
542 emit_1ub(p, 0x58 + reg.idx);
543 p->stack_offset -= 4;
544 }
545
546 void x86_inc( struct x86_function *p,
547 struct x86_reg reg )
548 {
549 DUMP_R( reg );
550 assert(reg.mod == mod_REG);
551 emit_1ub(p, 0x40 + reg.idx);
552 }
553
554 void x86_dec( struct x86_function *p,
555 struct x86_reg reg )
556 {
557 DUMP_R( reg );
558 assert(reg.mod == mod_REG);
559 emit_1ub(p, 0x48 + reg.idx);
560 }
561
562 void x86_ret( struct x86_function *p )
563 {
564 DUMP();
565 assert(p->stack_offset == 0);
566 emit_1ub(p, 0xc3);
567 }
568
569 void x86_retw( struct x86_function *p, unsigned short imm )
570 {
571 DUMP();
572 emit_3ub(p, 0xc2, imm & 0xff, (imm >> 8) & 0xff);
573 }
574
575 void x86_sahf( struct x86_function *p )
576 {
577 DUMP();
578 emit_1ub(p, 0x9e);
579 }
580
581 void x86_mov( struct x86_function *p,
582 struct x86_reg dst,
583 struct x86_reg src )
584 {
585 DUMP_RR( dst, src );
586 emit_op_modrm( p, 0x8b, 0x89, dst, src );
587 }
588
589 void x86_movzx8(struct x86_function *p, struct x86_reg dst, struct x86_reg src )
590 {
591 DUMP_RR( dst, src );
592 emit_2ub(p, 0x0f, 0xb6);
593 emit_modrm(p, dst, src);
594 }
595
596 void x86_movzx16(struct x86_function *p, struct x86_reg dst, struct x86_reg src )
597 {
598 DUMP_RR( dst, src );
599 emit_2ub(p, 0x0f, 0xb7);
600 emit_modrm(p, dst, src);
601 }
602
603 void x86_xor( struct x86_function *p,
604 struct x86_reg dst,
605 struct x86_reg src )
606 {
607 DUMP_RR( dst, src );
608 emit_op_modrm( p, 0x33, 0x31, dst, src );
609 }
610
611 void x86_cmp( struct x86_function *p,
612 struct x86_reg dst,
613 struct x86_reg src )
614 {
615 DUMP_RR( dst, src );
616 emit_op_modrm( p, 0x3b, 0x39, dst, src );
617 }
618
619 void x86_lea( struct x86_function *p,
620 struct x86_reg dst,
621 struct x86_reg src )
622 {
623 DUMP_RR( dst, src );
624 emit_1ub(p, 0x8d);
625 emit_modrm( p, dst, src );
626 }
627
628 void x86_test( struct x86_function *p,
629 struct x86_reg dst,
630 struct x86_reg src )
631 {
632 DUMP_RR( dst, src );
633 emit_1ub(p, 0x85);
634 emit_modrm( p, dst, src );
635 }
636
637 void x86_add( struct x86_function *p,
638 struct x86_reg dst,
639 struct x86_reg src )
640 {
641 DUMP_RR( dst, src );
642 emit_op_modrm(p, 0x03, 0x01, dst, src );
643 }
644
645 /* Calculate EAX * src, results in EDX:EAX.
646 */
647 void x86_mul( struct x86_function *p,
648 struct x86_reg src )
649 {
650 DUMP_R( src );
651 emit_1ub(p, 0xf7);
652 emit_modrm_noreg(p, 4, src );
653 }
654
655
656 void x86_imul( struct x86_function *p,
657 struct x86_reg dst,
658 struct x86_reg src )
659 {
660 DUMP_RR( dst, src );
661 emit_2ub(p, X86_TWOB, 0xAF);
662 emit_modrm(p, dst, src);
663 }
664
665
666 void x86_sub( struct x86_function *p,
667 struct x86_reg dst,
668 struct x86_reg src )
669 {
670 DUMP_RR( dst, src );
671 emit_op_modrm(p, 0x2b, 0x29, dst, src );
672 }
673
674 void x86_or( struct x86_function *p,
675 struct x86_reg dst,
676 struct x86_reg src )
677 {
678 DUMP_RR( dst, src );
679 emit_op_modrm( p, 0x0b, 0x09, dst, src );
680 }
681
682 void x86_and( struct x86_function *p,
683 struct x86_reg dst,
684 struct x86_reg src )
685 {
686 DUMP_RR( dst, src );
687 emit_op_modrm( p, 0x23, 0x21, dst, src );
688 }
689
690 void x86_div( struct x86_function *p,
691 struct x86_reg src )
692 {
693 assert(src.file == file_REG32 && src.mod == mod_REG);
694 emit_op_modrm(p, 0xf7, 0, x86_make_reg(file_REG32, 6), src);
695 }
696
697
698
699 /***********************************************************************
700 * SSE instructions
701 */
702
703 void sse_prefetchnta( struct x86_function *p, struct x86_reg ptr)
704 {
705 DUMP_R( ptr );
706 assert(ptr.mod != mod_REG);
707 emit_2ub(p, 0x0f, 0x18);
708 emit_modrm_noreg(p, 0, ptr);
709 }
710
711 void sse_prefetch0( struct x86_function *p, struct x86_reg ptr)
712 {
713 DUMP_R( ptr );
714 assert(ptr.mod != mod_REG);
715 emit_2ub(p, 0x0f, 0x18);
716 emit_modrm_noreg(p, 1, ptr);
717 }
718
719 void sse_prefetch1( struct x86_function *p, struct x86_reg ptr)
720 {
721 DUMP_R( ptr );
722 assert(ptr.mod != mod_REG);
723 emit_2ub(p, 0x0f, 0x18);
724 emit_modrm_noreg(p, 2, ptr);
725 }
726
727 void sse_movntps( struct x86_function *p,
728 struct x86_reg dst,
729 struct x86_reg src)
730 {
731 DUMP_RR( dst, src );
732
733 assert(dst.mod != mod_REG);
734 assert(src.mod == mod_REG);
735 emit_2ub(p, 0x0f, 0x2b);
736 emit_modrm(p, src, dst);
737 }
738
739
740
741
742 void sse_movss( struct x86_function *p,
743 struct x86_reg dst,
744 struct x86_reg src )
745 {
746 DUMP_RR( dst, src );
747 emit_2ub(p, 0xF3, X86_TWOB);
748 emit_op_modrm( p, 0x10, 0x11, dst, src );
749 }
750
751 void sse_movaps( struct x86_function *p,
752 struct x86_reg dst,
753 struct x86_reg src )
754 {
755 DUMP_RR( dst, src );
756 emit_1ub(p, X86_TWOB);
757 emit_op_modrm( p, 0x28, 0x29, dst, src );
758 }
759
760 void sse_movups( struct x86_function *p,
761 struct x86_reg dst,
762 struct x86_reg src )
763 {
764 DUMP_RR( dst, src );
765 emit_1ub(p, X86_TWOB);
766 emit_op_modrm( p, 0x10, 0x11, dst, src );
767 }
768
769 void sse_movhps( struct x86_function *p,
770 struct x86_reg dst,
771 struct x86_reg src )
772 {
773 DUMP_RR( dst, src );
774 assert(dst.mod != mod_REG || src.mod != mod_REG);
775 emit_1ub(p, X86_TWOB);
776 emit_op_modrm( p, 0x16, 0x17, dst, src ); /* cf movlhps */
777 }
778
779 void sse_movlps( struct x86_function *p,
780 struct x86_reg dst,
781 struct x86_reg src )
782 {
783 DUMP_RR( dst, src );
784 assert(dst.mod != mod_REG || src.mod != mod_REG);
785 emit_1ub(p, X86_TWOB);
786 emit_op_modrm( p, 0x12, 0x13, dst, src ); /* cf movhlps */
787 }
788
789 void sse_maxps( struct x86_function *p,
790 struct x86_reg dst,
791 struct x86_reg src )
792 {
793 DUMP_RR( dst, src );
794 emit_2ub(p, X86_TWOB, 0x5F);
795 emit_modrm( p, dst, src );
796 }
797
798 void sse_maxss( struct x86_function *p,
799 struct x86_reg dst,
800 struct x86_reg src )
801 {
802 DUMP_RR( dst, src );
803 emit_3ub(p, 0xF3, X86_TWOB, 0x5F);
804 emit_modrm( p, dst, src );
805 }
806
807 void sse_divss( struct x86_function *p,
808 struct x86_reg dst,
809 struct x86_reg src )
810 {
811 DUMP_RR( dst, src );
812 emit_3ub(p, 0xF3, X86_TWOB, 0x5E);
813 emit_modrm( p, dst, src );
814 }
815
816 void sse_minps( struct x86_function *p,
817 struct x86_reg dst,
818 struct x86_reg src )
819 {
820 DUMP_RR( dst, src );
821 emit_2ub(p, X86_TWOB, 0x5D);
822 emit_modrm( p, dst, src );
823 }
824
825 void sse_subps( struct x86_function *p,
826 struct x86_reg dst,
827 struct x86_reg src )
828 {
829 DUMP_RR( dst, src );
830 emit_2ub(p, X86_TWOB, 0x5C);
831 emit_modrm( p, dst, src );
832 }
833
834 void sse_mulps( struct x86_function *p,
835 struct x86_reg dst,
836 struct x86_reg src )
837 {
838 DUMP_RR( dst, src );
839 emit_2ub(p, X86_TWOB, 0x59);
840 emit_modrm( p, dst, src );
841 }
842
843 void sse_mulss( struct x86_function *p,
844 struct x86_reg dst,
845 struct x86_reg src )
846 {
847 DUMP_RR( dst, src );
848 emit_3ub(p, 0xF3, X86_TWOB, 0x59);
849 emit_modrm( p, dst, src );
850 }
851
852 void sse_addps( struct x86_function *p,
853 struct x86_reg dst,
854 struct x86_reg src )
855 {
856 DUMP_RR( dst, src );
857 emit_2ub(p, X86_TWOB, 0x58);
858 emit_modrm( p, dst, src );
859 }
860
861 void sse_addss( struct x86_function *p,
862 struct x86_reg dst,
863 struct x86_reg src )
864 {
865 DUMP_RR( dst, src );
866 emit_3ub(p, 0xF3, X86_TWOB, 0x58);
867 emit_modrm( p, dst, src );
868 }
869
870 void sse_andnps( struct x86_function *p,
871 struct x86_reg dst,
872 struct x86_reg src )
873 {
874 DUMP_RR( dst, src );
875 emit_2ub(p, X86_TWOB, 0x55);
876 emit_modrm( p, dst, src );
877 }
878
879 void sse_andps( struct x86_function *p,
880 struct x86_reg dst,
881 struct x86_reg src )
882 {
883 DUMP_RR( dst, src );
884 emit_2ub(p, X86_TWOB, 0x54);
885 emit_modrm( p, dst, src );
886 }
887
888 void sse_rsqrtps( struct x86_function *p,
889 struct x86_reg dst,
890 struct x86_reg src )
891 {
892 DUMP_RR( dst, src );
893 emit_2ub(p, X86_TWOB, 0x52);
894 emit_modrm( p, dst, src );
895 }
896
897 void sse_rsqrtss( struct x86_function *p,
898 struct x86_reg dst,
899 struct x86_reg src )
900 {
901 DUMP_RR( dst, src );
902 emit_3ub(p, 0xF3, X86_TWOB, 0x52);
903 emit_modrm( p, dst, src );
904
905 }
906
907 void sse_movhlps( struct x86_function *p,
908 struct x86_reg dst,
909 struct x86_reg src )
910 {
911 DUMP_RR( dst, src );
912 assert(dst.mod == mod_REG && src.mod == mod_REG);
913 emit_2ub(p, X86_TWOB, 0x12);
914 emit_modrm( p, dst, src );
915 }
916
917 void sse_movlhps( struct x86_function *p,
918 struct x86_reg dst,
919 struct x86_reg src )
920 {
921 DUMP_RR( dst, src );
922 assert(dst.mod == mod_REG && src.mod == mod_REG);
923 emit_2ub(p, X86_TWOB, 0x16);
924 emit_modrm( p, dst, src );
925 }
926
927 void sse_orps( struct x86_function *p,
928 struct x86_reg dst,
929 struct x86_reg src )
930 {
931 DUMP_RR( dst, src );
932 emit_2ub(p, X86_TWOB, 0x56);
933 emit_modrm( p, dst, src );
934 }
935
936 void sse_xorps( struct x86_function *p,
937 struct x86_reg dst,
938 struct x86_reg src )
939 {
940 DUMP_RR( dst, src );
941 emit_2ub(p, X86_TWOB, 0x57);
942 emit_modrm( p, dst, src );
943 }
944
945 void sse_cvtps2pi( struct x86_function *p,
946 struct x86_reg dst,
947 struct x86_reg src )
948 {
949 DUMP_RR( dst, src );
950 assert(dst.file == file_MMX &&
951 (src.file == file_XMM || src.mod != mod_REG));
952
953 p->need_emms = 1;
954
955 emit_2ub(p, X86_TWOB, 0x2d);
956 emit_modrm( p, dst, src );
957 }
958
959 void sse2_cvtdq2ps( struct x86_function *p,
960 struct x86_reg dst,
961 struct x86_reg src )
962 {
963 DUMP_RR( dst, src );
964 emit_2ub(p, X86_TWOB, 0x5b);
965 emit_modrm( p, dst, src );
966 }
967
968
969 /* Shufps can also be used to implement a reduced swizzle when dest ==
970 * arg0.
971 */
972 void sse_shufps( struct x86_function *p,
973 struct x86_reg dst,
974 struct x86_reg src,
975 unsigned char shuf)
976 {
977 DUMP_RRI( dst, src, shuf );
978 emit_2ub(p, X86_TWOB, 0xC6);
979 emit_modrm(p, dst, src);
980 emit_1ub(p, shuf);
981 }
982
983 void sse_unpckhps( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
984 {
985 DUMP_RR( dst, src );
986 emit_2ub( p, X86_TWOB, 0x15 );
987 emit_modrm( p, dst, src );
988 }
989
990 void sse_unpcklps( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
991 {
992 DUMP_RR( dst, src );
993 emit_2ub( p, X86_TWOB, 0x14 );
994 emit_modrm( p, dst, src );
995 }
996
997 void sse_cmpps( struct x86_function *p,
998 struct x86_reg dst,
999 struct x86_reg src,
1000 enum sse_cc cc)
1001 {
1002 DUMP_RRI( dst, src, cc );
1003 emit_2ub(p, X86_TWOB, 0xC2);
1004 emit_modrm(p, dst, src);
1005 emit_1ub(p, cc);
1006 }
1007
1008 void sse_pmovmskb( struct x86_function *p,
1009 struct x86_reg dst,
1010 struct x86_reg src)
1011 {
1012 DUMP_RR( dst, src );
1013 emit_3ub(p, 0x66, X86_TWOB, 0xD7);
1014 emit_modrm(p, dst, src);
1015 }
1016
1017 void sse_movmskps( struct x86_function *p,
1018 struct x86_reg dst,
1019 struct x86_reg src)
1020 {
1021 DUMP_RR( dst, src );
1022 emit_2ub(p, X86_TWOB, 0x50);
1023 emit_modrm(p, dst, src);
1024 }
1025
1026 /***********************************************************************
1027 * SSE2 instructions
1028 */
1029
1030 /**
1031 * Perform a reduced swizzle:
1032 */
1033 void sse2_pshufd( struct x86_function *p,
1034 struct x86_reg dst,
1035 struct x86_reg src,
1036 unsigned char shuf)
1037 {
1038 DUMP_RRI( dst, src, shuf );
1039 emit_3ub(p, 0x66, X86_TWOB, 0x70);
1040 emit_modrm(p, dst, src);
1041 emit_1ub(p, shuf);
1042 }
1043
1044 void sse2_cvttps2dq( struct x86_function *p,
1045 struct x86_reg dst,
1046 struct x86_reg src )
1047 {
1048 DUMP_RR( dst, src );
1049 emit_3ub( p, 0xF3, X86_TWOB, 0x5B );
1050 emit_modrm( p, dst, src );
1051 }
1052
1053 void sse2_cvtps2dq( struct x86_function *p,
1054 struct x86_reg dst,
1055 struct x86_reg src )
1056 {
1057 DUMP_RR( dst, src );
1058 emit_3ub(p, 0x66, X86_TWOB, 0x5B);
1059 emit_modrm( p, dst, src );
1060 }
1061
1062 void sse2_packssdw( struct x86_function *p,
1063 struct x86_reg dst,
1064 struct x86_reg src )
1065 {
1066 DUMP_RR( dst, src );
1067 emit_3ub(p, 0x66, X86_TWOB, 0x6B);
1068 emit_modrm( p, dst, src );
1069 }
1070
1071 void sse2_packsswb( struct x86_function *p,
1072 struct x86_reg dst,
1073 struct x86_reg src )
1074 {
1075 DUMP_RR( dst, src );
1076 emit_3ub(p, 0x66, X86_TWOB, 0x63);
1077 emit_modrm( p, dst, src );
1078 }
1079
1080 void sse2_packuswb( struct x86_function *p,
1081 struct x86_reg dst,
1082 struct x86_reg src )
1083 {
1084 DUMP_RR( dst, src );
1085 emit_3ub(p, 0x66, X86_TWOB, 0x67);
1086 emit_modrm( p, dst, src );
1087 }
1088
1089 void sse2_punpcklbw( struct x86_function *p,
1090 struct x86_reg dst,
1091 struct x86_reg src )
1092 {
1093 DUMP_RR( dst, src );
1094 emit_3ub(p, 0x66, X86_TWOB, 0x60);
1095 emit_modrm( p, dst, src );
1096 }
1097
1098
1099 void sse2_rcpps( struct x86_function *p,
1100 struct x86_reg dst,
1101 struct x86_reg src )
1102 {
1103 DUMP_RR( dst, src );
1104 emit_2ub(p, X86_TWOB, 0x53);
1105 emit_modrm( p, dst, src );
1106 }
1107
1108 void sse2_rcpss( struct x86_function *p,
1109 struct x86_reg dst,
1110 struct x86_reg src )
1111 {
1112 DUMP_RR( dst, src );
1113 emit_3ub(p, 0xF3, X86_TWOB, 0x53);
1114 emit_modrm( p, dst, src );
1115 }
1116
1117 void sse2_movd( struct x86_function *p,
1118 struct x86_reg dst,
1119 struct x86_reg src )
1120 {
1121 DUMP_RR( dst, src );
1122 emit_2ub(p, 0x66, X86_TWOB);
1123 emit_op_modrm( p, 0x6e, 0x7e, dst, src );
1124 }
1125
1126
1127
1128
1129 /***********************************************************************
1130 * x87 instructions
1131 */
1132 static void note_x87_pop( struct x86_function *p )
1133 {
1134 p->x87_stack--;
1135 assert(p->x87_stack >= 0);
1136 }
1137
1138 static void note_x87_push( struct x86_function *p )
1139 {
1140 p->x87_stack++;
1141 assert(p->x87_stack <= 7);
1142 }
1143
1144 void x87_assert_stack_empty( struct x86_function *p )
1145 {
1146 assert (p->x87_stack == 0);
1147 }
1148
1149
1150 void x87_fist( struct x86_function *p, struct x86_reg dst )
1151 {
1152 DUMP_R( dst );
1153 emit_1ub(p, 0xdb);
1154 emit_modrm_noreg(p, 2, dst);
1155 }
1156
1157 void x87_fistp( struct x86_function *p, struct x86_reg dst )
1158 {
1159 DUMP_R( dst );
1160 emit_1ub(p, 0xdb);
1161 emit_modrm_noreg(p, 3, dst);
1162 note_x87_pop(p);
1163 }
1164
1165 void x87_fild( struct x86_function *p, struct x86_reg arg )
1166 {
1167 DUMP_R( arg );
1168 emit_1ub(p, 0xdf);
1169 emit_modrm_noreg(p, 0, arg);
1170 note_x87_push(p);
1171 }
1172
1173 void x87_fldz( struct x86_function *p )
1174 {
1175 DUMP();
1176 emit_2ub(p, 0xd9, 0xee);
1177 note_x87_push(p);
1178 }
1179
1180
1181 void x87_fldcw( struct x86_function *p, struct x86_reg arg )
1182 {
1183 DUMP_R( arg );
1184 assert(arg.file == file_REG32);
1185 assert(arg.mod != mod_REG);
1186 emit_1ub(p, 0xd9);
1187 emit_modrm_noreg(p, 5, arg);
1188 }
1189
1190 void x87_fld1( struct x86_function *p )
1191 {
1192 DUMP();
1193 emit_2ub(p, 0xd9, 0xe8);
1194 note_x87_push(p);
1195 }
1196
1197 void x87_fldl2e( struct x86_function *p )
1198 {
1199 DUMP();
1200 emit_2ub(p, 0xd9, 0xea);
1201 note_x87_push(p);
1202 }
1203
1204 void x87_fldln2( struct x86_function *p )
1205 {
1206 DUMP();
1207 emit_2ub(p, 0xd9, 0xed);
1208 note_x87_push(p);
1209 }
1210
1211 void x87_fwait( struct x86_function *p )
1212 {
1213 DUMP();
1214 emit_1ub(p, 0x9b);
1215 }
1216
1217 void x87_fnclex( struct x86_function *p )
1218 {
1219 DUMP();
1220 emit_2ub(p, 0xdb, 0xe2);
1221 }
1222
1223 void x87_fclex( struct x86_function *p )
1224 {
1225 x87_fwait(p);
1226 x87_fnclex(p);
1227 }
1228
1229 void x87_fcmovb( struct x86_function *p, struct x86_reg arg )
1230 {
1231 DUMP_R( arg );
1232 assert(arg.file == file_x87);
1233 emit_2ub(p, 0xda, 0xc0+arg.idx);
1234 }
1235
1236 void x87_fcmove( struct x86_function *p, struct x86_reg arg )
1237 {
1238 DUMP_R( arg );
1239 assert(arg.file == file_x87);
1240 emit_2ub(p, 0xda, 0xc8+arg.idx);
1241 }
1242
1243 void x87_fcmovbe( struct x86_function *p, struct x86_reg arg )
1244 {
1245 DUMP_R( arg );
1246 assert(arg.file == file_x87);
1247 emit_2ub(p, 0xda, 0xd0+arg.idx);
1248 }
1249
1250 void x87_fcmovnb( struct x86_function *p, struct x86_reg arg )
1251 {
1252 DUMP_R( arg );
1253 assert(arg.file == file_x87);
1254 emit_2ub(p, 0xdb, 0xc0+arg.idx);
1255 }
1256
1257 void x87_fcmovne( struct x86_function *p, struct x86_reg arg )
1258 {
1259 DUMP_R( arg );
1260 assert(arg.file == file_x87);
1261 emit_2ub(p, 0xdb, 0xc8+arg.idx);
1262 }
1263
1264 void x87_fcmovnbe( struct x86_function *p, struct x86_reg arg )
1265 {
1266 DUMP_R( arg );
1267 assert(arg.file == file_x87);
1268 emit_2ub(p, 0xdb, 0xd0+arg.idx);
1269 }
1270
1271
1272
1273 static void x87_arith_op( struct x86_function *p, struct x86_reg dst, struct x86_reg arg,
1274 unsigned char dst0ub0,
1275 unsigned char dst0ub1,
1276 unsigned char arg0ub0,
1277 unsigned char arg0ub1,
1278 unsigned char argmem_noreg)
1279 {
1280 assert(dst.file == file_x87);
1281
1282 if (arg.file == file_x87) {
1283 if (dst.idx == 0)
1284 emit_2ub(p, dst0ub0, dst0ub1+arg.idx);
1285 else if (arg.idx == 0)
1286 emit_2ub(p, arg0ub0, arg0ub1+arg.idx);
1287 else
1288 assert(0);
1289 }
1290 else if (dst.idx == 0) {
1291 assert(arg.file == file_REG32);
1292 emit_1ub(p, 0xd8);
1293 emit_modrm_noreg(p, argmem_noreg, arg);
1294 }
1295 else
1296 assert(0);
1297 }
1298
1299 void x87_fmul( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
1300 {
1301 DUMP_RR( dst, src );
1302 x87_arith_op(p, dst, src,
1303 0xd8, 0xc8,
1304 0xdc, 0xc8,
1305 4);
1306 }
1307
1308 void x87_fsub( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
1309 {
1310 DUMP_RR( dst, src );
1311 x87_arith_op(p, dst, src,
1312 0xd8, 0xe0,
1313 0xdc, 0xe8,
1314 4);
1315 }
1316
1317 void x87_fsubr( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
1318 {
1319 DUMP_RR( dst, src );
1320 x87_arith_op(p, dst, src,
1321 0xd8, 0xe8,
1322 0xdc, 0xe0,
1323 5);
1324 }
1325
1326 void x87_fadd( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
1327 {
1328 DUMP_RR( dst, src );
1329 x87_arith_op(p, dst, src,
1330 0xd8, 0xc0,
1331 0xdc, 0xc0,
1332 0);
1333 }
1334
1335 void x87_fdiv( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
1336 {
1337 DUMP_RR( dst, src );
1338 x87_arith_op(p, dst, src,
1339 0xd8, 0xf0,
1340 0xdc, 0xf8,
1341 6);
1342 }
1343
1344 void x87_fdivr( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
1345 {
1346 DUMP_RR( dst, src );
1347 x87_arith_op(p, dst, src,
1348 0xd8, 0xf8,
1349 0xdc, 0xf0,
1350 7);
1351 }
1352
1353 void x87_fmulp( struct x86_function *p, struct x86_reg dst )
1354 {
1355 DUMP_R( dst );
1356 assert(dst.file == file_x87);
1357 assert(dst.idx >= 1);
1358 emit_2ub(p, 0xde, 0xc8+dst.idx);
1359 note_x87_pop(p);
1360 }
1361
1362 void x87_fsubp( struct x86_function *p, struct x86_reg dst )
1363 {
1364 DUMP_R( dst );
1365 assert(dst.file == file_x87);
1366 assert(dst.idx >= 1);
1367 emit_2ub(p, 0xde, 0xe8+dst.idx);
1368 note_x87_pop(p);
1369 }
1370
1371 void x87_fsubrp( struct x86_function *p, struct x86_reg dst )
1372 {
1373 DUMP_R( dst );
1374 assert(dst.file == file_x87);
1375 assert(dst.idx >= 1);
1376 emit_2ub(p, 0xde, 0xe0+dst.idx);
1377 note_x87_pop(p);
1378 }
1379
1380 void x87_faddp( struct x86_function *p, struct x86_reg dst )
1381 {
1382 DUMP_R( dst );
1383 assert(dst.file == file_x87);
1384 assert(dst.idx >= 1);
1385 emit_2ub(p, 0xde, 0xc0+dst.idx);
1386 note_x87_pop(p);
1387 }
1388
1389 void x87_fdivp( struct x86_function *p, struct x86_reg dst )
1390 {
1391 DUMP_R( dst );
1392 assert(dst.file == file_x87);
1393 assert(dst.idx >= 1);
1394 emit_2ub(p, 0xde, 0xf8+dst.idx);
1395 note_x87_pop(p);
1396 }
1397
1398 void x87_fdivrp( struct x86_function *p, struct x86_reg dst )
1399 {
1400 DUMP_R( dst );
1401 assert(dst.file == file_x87);
1402 assert(dst.idx >= 1);
1403 emit_2ub(p, 0xde, 0xf0+dst.idx);
1404 note_x87_pop(p);
1405 }
1406
1407 void x87_ftst( struct x86_function *p )
1408 {
1409 DUMP();
1410 emit_2ub(p, 0xd9, 0xe4);
1411 }
1412
1413 void x87_fucom( struct x86_function *p, struct x86_reg arg )
1414 {
1415 DUMP_R( arg );
1416 assert(arg.file == file_x87);
1417 emit_2ub(p, 0xdd, 0xe0+arg.idx);
1418 }
1419
1420 void x87_fucomp( struct x86_function *p, struct x86_reg arg )
1421 {
1422 DUMP_R( arg );
1423 assert(arg.file == file_x87);
1424 emit_2ub(p, 0xdd, 0xe8+arg.idx);
1425 note_x87_pop(p);
1426 }
1427
1428 void x87_fucompp( struct x86_function *p )
1429 {
1430 DUMP();
1431 emit_2ub(p, 0xda, 0xe9);
1432 note_x87_pop(p); /* pop twice */
1433 note_x87_pop(p); /* pop twice */
1434 }
1435
1436 void x87_fxch( struct x86_function *p, struct x86_reg arg )
1437 {
1438 DUMP_R( arg );
1439 assert(arg.file == file_x87);
1440 emit_2ub(p, 0xd9, 0xc8+arg.idx);
1441 }
1442
1443 void x87_fabs( struct x86_function *p )
1444 {
1445 DUMP();
1446 emit_2ub(p, 0xd9, 0xe1);
1447 }
1448
1449 void x87_fchs( struct x86_function *p )
1450 {
1451 DUMP();
1452 emit_2ub(p, 0xd9, 0xe0);
1453 }
1454
1455 void x87_fcos( struct x86_function *p )
1456 {
1457 DUMP();
1458 emit_2ub(p, 0xd9, 0xff);
1459 }
1460
1461
1462 void x87_fprndint( struct x86_function *p )
1463 {
1464 DUMP();
1465 emit_2ub(p, 0xd9, 0xfc);
1466 }
1467
1468 void x87_fscale( struct x86_function *p )
1469 {
1470 DUMP();
1471 emit_2ub(p, 0xd9, 0xfd);
1472 }
1473
1474 void x87_fsin( struct x86_function *p )
1475 {
1476 DUMP();
1477 emit_2ub(p, 0xd9, 0xfe);
1478 }
1479
1480 void x87_fsincos( struct x86_function *p )
1481 {
1482 DUMP();
1483 emit_2ub(p, 0xd9, 0xfb);
1484 }
1485
1486 void x87_fsqrt( struct x86_function *p )
1487 {
1488 DUMP();
1489 emit_2ub(p, 0xd9, 0xfa);
1490 }
1491
1492 void x87_fxtract( struct x86_function *p )
1493 {
1494 DUMP();
1495 emit_2ub(p, 0xd9, 0xf4);
1496 }
1497
1498 /* st0 = (2^st0)-1
1499 *
1500 * Restrictions: -1.0 <= st0 <= 1.0
1501 */
1502 void x87_f2xm1( struct x86_function *p )
1503 {
1504 DUMP();
1505 emit_2ub(p, 0xd9, 0xf0);
1506 }
1507
1508 /* st1 = st1 * log2(st0);
1509 * pop_stack;
1510 */
1511 void x87_fyl2x( struct x86_function *p )
1512 {
1513 DUMP();
1514 emit_2ub(p, 0xd9, 0xf1);
1515 note_x87_pop(p);
1516 }
1517
1518 /* st1 = st1 * log2(st0 + 1.0);
1519 * pop_stack;
1520 *
1521 * A fast operation, with restrictions: -.29 < st0 < .29
1522 */
1523 void x87_fyl2xp1( struct x86_function *p )
1524 {
1525 DUMP();
1526 emit_2ub(p, 0xd9, 0xf9);
1527 note_x87_pop(p);
1528 }
1529
1530
1531 void x87_fld( struct x86_function *p, struct x86_reg arg )
1532 {
1533 DUMP_R( arg );
1534 if (arg.file == file_x87)
1535 emit_2ub(p, 0xd9, 0xc0 + arg.idx);
1536 else {
1537 emit_1ub(p, 0xd9);
1538 emit_modrm_noreg(p, 0, arg);
1539 }
1540 note_x87_push(p);
1541 }
1542
1543 void x87_fst( struct x86_function *p, struct x86_reg dst )
1544 {
1545 DUMP_R( dst );
1546 if (dst.file == file_x87)
1547 emit_2ub(p, 0xdd, 0xd0 + dst.idx);
1548 else {
1549 emit_1ub(p, 0xd9);
1550 emit_modrm_noreg(p, 2, dst);
1551 }
1552 }
1553
1554 void x87_fstp( struct x86_function *p, struct x86_reg dst )
1555 {
1556 DUMP_R( dst );
1557 if (dst.file == file_x87)
1558 emit_2ub(p, 0xdd, 0xd8 + dst.idx);
1559 else {
1560 emit_1ub(p, 0xd9);
1561 emit_modrm_noreg(p, 3, dst);
1562 }
1563 note_x87_pop(p);
1564 }
1565
1566 void x87_fpop( struct x86_function *p )
1567 {
1568 x87_fstp( p, x86_make_reg( file_x87, 0 ));
1569 }
1570
1571
1572 void x87_fcom( struct x86_function *p, struct x86_reg dst )
1573 {
1574 DUMP_R( dst );
1575 if (dst.file == file_x87)
1576 emit_2ub(p, 0xd8, 0xd0 + dst.idx);
1577 else {
1578 emit_1ub(p, 0xd8);
1579 emit_modrm_noreg(p, 2, dst);
1580 }
1581 }
1582
1583
1584 void x87_fcomp( struct x86_function *p, struct x86_reg dst )
1585 {
1586 DUMP_R( dst );
1587 if (dst.file == file_x87)
1588 emit_2ub(p, 0xd8, 0xd8 + dst.idx);
1589 else {
1590 emit_1ub(p, 0xd8);
1591 emit_modrm_noreg(p, 3, dst);
1592 }
1593 note_x87_pop(p);
1594 }
1595
1596 void x87_fcomi( struct x86_function *p, struct x86_reg arg )
1597 {
1598 DUMP_R( arg );
1599 emit_2ub(p, 0xdb, 0xf0+arg.idx);
1600 }
1601
1602 void x87_fcomip( struct x86_function *p, struct x86_reg arg )
1603 {
1604 DUMP_R( arg );
1605 emit_2ub(p, 0xdb, 0xf0+arg.idx);
1606 note_x87_pop(p);
1607 }
1608
1609
1610 void x87_fnstsw( struct x86_function *p, struct x86_reg dst )
1611 {
1612 DUMP_R( dst );
1613 assert(dst.file == file_REG32);
1614
1615 if (dst.idx == reg_AX &&
1616 dst.mod == mod_REG)
1617 emit_2ub(p, 0xdf, 0xe0);
1618 else {
1619 emit_1ub(p, 0xdd);
1620 emit_modrm_noreg(p, 7, dst);
1621 }
1622 }
1623
1624
1625 void x87_fnstcw( struct x86_function *p, struct x86_reg dst )
1626 {
1627 DUMP_R( dst );
1628 assert(dst.file == file_REG32);
1629
1630 emit_1ub(p, 0x9b); /* WAIT -- needed? */
1631 emit_1ub(p, 0xd9);
1632 emit_modrm_noreg(p, 7, dst);
1633 }
1634
1635
1636
1637
1638 /***********************************************************************
1639 * MMX instructions
1640 */
1641
1642 void mmx_emms( struct x86_function *p )
1643 {
1644 DUMP();
1645 assert(p->need_emms);
1646 emit_2ub(p, 0x0f, 0x77);
1647 p->need_emms = 0;
1648 }
1649
1650 void mmx_packssdw( struct x86_function *p,
1651 struct x86_reg dst,
1652 struct x86_reg src )
1653 {
1654 DUMP_RR( dst, src );
1655 assert(dst.file == file_MMX &&
1656 (src.file == file_MMX || src.mod != mod_REG));
1657
1658 p->need_emms = 1;
1659
1660 emit_2ub(p, X86_TWOB, 0x6b);
1661 emit_modrm( p, dst, src );
1662 }
1663
1664 void mmx_packuswb( struct x86_function *p,
1665 struct x86_reg dst,
1666 struct x86_reg src )
1667 {
1668 DUMP_RR( dst, src );
1669 assert(dst.file == file_MMX &&
1670 (src.file == file_MMX || src.mod != mod_REG));
1671
1672 p->need_emms = 1;
1673
1674 emit_2ub(p, X86_TWOB, 0x67);
1675 emit_modrm( p, dst, src );
1676 }
1677
1678 void mmx_movd( struct x86_function *p,
1679 struct x86_reg dst,
1680 struct x86_reg src )
1681 {
1682 DUMP_RR( dst, src );
1683 p->need_emms = 1;
1684 emit_1ub(p, X86_TWOB);
1685 emit_op_modrm( p, 0x6e, 0x7e, dst, src );
1686 }
1687
1688 void mmx_movq( struct x86_function *p,
1689 struct x86_reg dst,
1690 struct x86_reg src )
1691 {
1692 DUMP_RR( dst, src );
1693 p->need_emms = 1;
1694 emit_1ub(p, X86_TWOB);
1695 emit_op_modrm( p, 0x6f, 0x7f, dst, src );
1696 }
1697
1698
1699 /***********************************************************************
1700 * Helper functions
1701 */
1702
1703
1704 void x86_cdecl_caller_push_regs( struct x86_function *p )
1705 {
1706 x86_push(p, x86_make_reg(file_REG32, reg_AX));
1707 x86_push(p, x86_make_reg(file_REG32, reg_CX));
1708 x86_push(p, x86_make_reg(file_REG32, reg_DX));
1709 }
1710
1711 void x86_cdecl_caller_pop_regs( struct x86_function *p )
1712 {
1713 x86_pop(p, x86_make_reg(file_REG32, reg_DX));
1714 x86_pop(p, x86_make_reg(file_REG32, reg_CX));
1715 x86_pop(p, x86_make_reg(file_REG32, reg_AX));
1716 }
1717
1718
1719 /* Retreive a reference to one of the function arguments, taking into
1720 * account any push/pop activity:
1721 */
1722 struct x86_reg x86_fn_arg( struct x86_function *p,
1723 unsigned arg )
1724 {
1725 return x86_make_disp(x86_make_reg(file_REG32, reg_SP),
1726 p->stack_offset + arg * 4); /* ??? */
1727 }
1728
1729
1730 void x86_init_func( struct x86_function *p )
1731 {
1732 p->size = 0;
1733 p->store = NULL;
1734 p->csr = p->store;
1735 DUMP_START();
1736 }
1737
1738 void x86_init_func_size( struct x86_function *p, unsigned code_size )
1739 {
1740 p->size = code_size;
1741 p->store = rtasm_exec_malloc(code_size);
1742 if (p->store == NULL) {
1743 p->store = p->error_overflow;
1744 }
1745 p->csr = p->store;
1746 DUMP_START();
1747 }
1748
1749 void x86_release_func( struct x86_function *p )
1750 {
1751 if (p->store && p->store != p->error_overflow)
1752 rtasm_exec_free(p->store);
1753
1754 p->store = NULL;
1755 p->csr = NULL;
1756 p->size = 0;
1757 }
1758
1759
1760 static INLINE x86_func
1761 voidptr_to_x86_func(void *v)
1762 {
1763 union {
1764 void *v;
1765 x86_func f;
1766 } u;
1767 assert(sizeof(u.v) == sizeof(u.f));
1768 u.v = v;
1769 return u.f;
1770 }
1771
1772
1773 x86_func x86_get_func( struct x86_function *p )
1774 {
1775 DUMP_END();
1776 if (DISASSEM && p->store)
1777 debug_printf("disassemble %p %p\n", p->store, p->csr);
1778
1779 if (p->store == p->error_overflow)
1780 return voidptr_to_x86_func(NULL);
1781 else
1782 return voidptr_to_x86_func(p->store);
1783 }
1784
1785 #else
1786
1787 void x86sse_dummy( void );
1788
1789 void x86sse_dummy( void )
1790 {
1791 }
1792
1793 #endif