Merge branch '7.8'
[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_xor( struct x86_function *p,
590 struct x86_reg dst,
591 struct x86_reg src )
592 {
593 DUMP_RR( dst, src );
594 emit_op_modrm( p, 0x33, 0x31, dst, src );
595 }
596
597 void x86_cmp( struct x86_function *p,
598 struct x86_reg dst,
599 struct x86_reg src )
600 {
601 DUMP_RR( dst, src );
602 emit_op_modrm( p, 0x3b, 0x39, dst, src );
603 }
604
605 void x86_lea( struct x86_function *p,
606 struct x86_reg dst,
607 struct x86_reg src )
608 {
609 DUMP_RR( dst, src );
610 emit_1ub(p, 0x8d);
611 emit_modrm( p, dst, src );
612 }
613
614 void x86_test( struct x86_function *p,
615 struct x86_reg dst,
616 struct x86_reg src )
617 {
618 DUMP_RR( dst, src );
619 emit_1ub(p, 0x85);
620 emit_modrm( p, dst, src );
621 }
622
623 void x86_add( struct x86_function *p,
624 struct x86_reg dst,
625 struct x86_reg src )
626 {
627 DUMP_RR( dst, src );
628 emit_op_modrm(p, 0x03, 0x01, dst, src );
629 }
630
631 /* Calculate EAX * src, results in EDX:EAX.
632 */
633 void x86_mul( struct x86_function *p,
634 struct x86_reg src )
635 {
636 DUMP_R( src );
637 emit_1ub(p, 0xf7);
638 emit_modrm_noreg(p, 4, src );
639 }
640
641
642 void x86_imul( struct x86_function *p,
643 struct x86_reg dst,
644 struct x86_reg src )
645 {
646 DUMP_RR( dst, src );
647 emit_2ub(p, X86_TWOB, 0xAF);
648 emit_modrm(p, dst, src);
649 }
650
651
652 void x86_sub( struct x86_function *p,
653 struct x86_reg dst,
654 struct x86_reg src )
655 {
656 DUMP_RR( dst, src );
657 emit_op_modrm(p, 0x2b, 0x29, dst, src );
658 }
659
660 void x86_or( struct x86_function *p,
661 struct x86_reg dst,
662 struct x86_reg src )
663 {
664 DUMP_RR( dst, src );
665 emit_op_modrm( p, 0x0b, 0x09, dst, src );
666 }
667
668 void x86_and( struct x86_function *p,
669 struct x86_reg dst,
670 struct x86_reg src )
671 {
672 DUMP_RR( dst, src );
673 emit_op_modrm( p, 0x23, 0x21, dst, src );
674 }
675
676 void x86_div( struct x86_function *p,
677 struct x86_reg src )
678 {
679 assert(src.file == file_REG32 && src.mod == mod_REG);
680 emit_op_modrm(p, 0xf7, 0, x86_make_reg(file_REG32, 6), src);
681 }
682
683
684
685 /***********************************************************************
686 * SSE instructions
687 */
688
689 void sse_prefetchnta( struct x86_function *p, struct x86_reg ptr)
690 {
691 DUMP_R( ptr );
692 assert(ptr.mod != mod_REG);
693 emit_2ub(p, 0x0f, 0x18);
694 emit_modrm_noreg(p, 0, ptr);
695 }
696
697 void sse_prefetch0( struct x86_function *p, struct x86_reg ptr)
698 {
699 DUMP_R( ptr );
700 assert(ptr.mod != mod_REG);
701 emit_2ub(p, 0x0f, 0x18);
702 emit_modrm_noreg(p, 1, ptr);
703 }
704
705 void sse_prefetch1( struct x86_function *p, struct x86_reg ptr)
706 {
707 DUMP_R( ptr );
708 assert(ptr.mod != mod_REG);
709 emit_2ub(p, 0x0f, 0x18);
710 emit_modrm_noreg(p, 2, ptr);
711 }
712
713 void sse_movntps( struct x86_function *p,
714 struct x86_reg dst,
715 struct x86_reg src)
716 {
717 DUMP_RR( dst, src );
718
719 assert(dst.mod != mod_REG);
720 assert(src.mod == mod_REG);
721 emit_2ub(p, 0x0f, 0x2b);
722 emit_modrm(p, src, dst);
723 }
724
725
726
727
728 void sse_movss( struct x86_function *p,
729 struct x86_reg dst,
730 struct x86_reg src )
731 {
732 DUMP_RR( dst, src );
733 emit_2ub(p, 0xF3, X86_TWOB);
734 emit_op_modrm( p, 0x10, 0x11, dst, src );
735 }
736
737 void sse_movaps( struct x86_function *p,
738 struct x86_reg dst,
739 struct x86_reg src )
740 {
741 DUMP_RR( dst, src );
742 emit_1ub(p, X86_TWOB);
743 emit_op_modrm( p, 0x28, 0x29, dst, src );
744 }
745
746 void sse_movups( struct x86_function *p,
747 struct x86_reg dst,
748 struct x86_reg src )
749 {
750 DUMP_RR( dst, src );
751 emit_1ub(p, X86_TWOB);
752 emit_op_modrm( p, 0x10, 0x11, dst, src );
753 }
754
755 void sse_movhps( struct x86_function *p,
756 struct x86_reg dst,
757 struct x86_reg src )
758 {
759 DUMP_RR( dst, src );
760 assert(dst.mod != mod_REG || src.mod != mod_REG);
761 emit_1ub(p, X86_TWOB);
762 emit_op_modrm( p, 0x16, 0x17, dst, src ); /* cf movlhps */
763 }
764
765 void sse_movlps( struct x86_function *p,
766 struct x86_reg dst,
767 struct x86_reg src )
768 {
769 DUMP_RR( dst, src );
770 assert(dst.mod != mod_REG || src.mod != mod_REG);
771 emit_1ub(p, X86_TWOB);
772 emit_op_modrm( p, 0x12, 0x13, dst, src ); /* cf movhlps */
773 }
774
775 void sse_maxps( struct x86_function *p,
776 struct x86_reg dst,
777 struct x86_reg src )
778 {
779 DUMP_RR( dst, src );
780 emit_2ub(p, X86_TWOB, 0x5F);
781 emit_modrm( p, dst, src );
782 }
783
784 void sse_maxss( struct x86_function *p,
785 struct x86_reg dst,
786 struct x86_reg src )
787 {
788 DUMP_RR( dst, src );
789 emit_3ub(p, 0xF3, X86_TWOB, 0x5F);
790 emit_modrm( p, dst, src );
791 }
792
793 void sse_divss( struct x86_function *p,
794 struct x86_reg dst,
795 struct x86_reg src )
796 {
797 DUMP_RR( dst, src );
798 emit_3ub(p, 0xF3, X86_TWOB, 0x5E);
799 emit_modrm( p, dst, src );
800 }
801
802 void sse_minps( struct x86_function *p,
803 struct x86_reg dst,
804 struct x86_reg src )
805 {
806 DUMP_RR( dst, src );
807 emit_2ub(p, X86_TWOB, 0x5D);
808 emit_modrm( p, dst, src );
809 }
810
811 void sse_subps( struct x86_function *p,
812 struct x86_reg dst,
813 struct x86_reg src )
814 {
815 DUMP_RR( dst, src );
816 emit_2ub(p, X86_TWOB, 0x5C);
817 emit_modrm( p, dst, src );
818 }
819
820 void sse_mulps( struct x86_function *p,
821 struct x86_reg dst,
822 struct x86_reg src )
823 {
824 DUMP_RR( dst, src );
825 emit_2ub(p, X86_TWOB, 0x59);
826 emit_modrm( p, dst, src );
827 }
828
829 void sse_mulss( struct x86_function *p,
830 struct x86_reg dst,
831 struct x86_reg src )
832 {
833 DUMP_RR( dst, src );
834 emit_3ub(p, 0xF3, X86_TWOB, 0x59);
835 emit_modrm( p, dst, src );
836 }
837
838 void sse_addps( struct x86_function *p,
839 struct x86_reg dst,
840 struct x86_reg src )
841 {
842 DUMP_RR( dst, src );
843 emit_2ub(p, X86_TWOB, 0x58);
844 emit_modrm( p, dst, src );
845 }
846
847 void sse_addss( struct x86_function *p,
848 struct x86_reg dst,
849 struct x86_reg src )
850 {
851 DUMP_RR( dst, src );
852 emit_3ub(p, 0xF3, X86_TWOB, 0x58);
853 emit_modrm( p, dst, src );
854 }
855
856 void sse_andnps( struct x86_function *p,
857 struct x86_reg dst,
858 struct x86_reg src )
859 {
860 DUMP_RR( dst, src );
861 emit_2ub(p, X86_TWOB, 0x55);
862 emit_modrm( p, dst, src );
863 }
864
865 void sse_andps( struct x86_function *p,
866 struct x86_reg dst,
867 struct x86_reg src )
868 {
869 DUMP_RR( dst, src );
870 emit_2ub(p, X86_TWOB, 0x54);
871 emit_modrm( p, dst, src );
872 }
873
874 void sse_rsqrtps( struct x86_function *p,
875 struct x86_reg dst,
876 struct x86_reg src )
877 {
878 DUMP_RR( dst, src );
879 emit_2ub(p, X86_TWOB, 0x52);
880 emit_modrm( p, dst, src );
881 }
882
883 void sse_rsqrtss( struct x86_function *p,
884 struct x86_reg dst,
885 struct x86_reg src )
886 {
887 DUMP_RR( dst, src );
888 emit_3ub(p, 0xF3, X86_TWOB, 0x52);
889 emit_modrm( p, dst, src );
890
891 }
892
893 void sse_movhlps( struct x86_function *p,
894 struct x86_reg dst,
895 struct x86_reg src )
896 {
897 DUMP_RR( dst, src );
898 assert(dst.mod == mod_REG && src.mod == mod_REG);
899 emit_2ub(p, X86_TWOB, 0x12);
900 emit_modrm( p, dst, src );
901 }
902
903 void sse_movlhps( struct x86_function *p,
904 struct x86_reg dst,
905 struct x86_reg src )
906 {
907 DUMP_RR( dst, src );
908 assert(dst.mod == mod_REG && src.mod == mod_REG);
909 emit_2ub(p, X86_TWOB, 0x16);
910 emit_modrm( p, dst, src );
911 }
912
913 void sse_orps( struct x86_function *p,
914 struct x86_reg dst,
915 struct x86_reg src )
916 {
917 DUMP_RR( dst, src );
918 emit_2ub(p, X86_TWOB, 0x56);
919 emit_modrm( p, dst, src );
920 }
921
922 void sse_xorps( struct x86_function *p,
923 struct x86_reg dst,
924 struct x86_reg src )
925 {
926 DUMP_RR( dst, src );
927 emit_2ub(p, X86_TWOB, 0x57);
928 emit_modrm( p, dst, src );
929 }
930
931 void sse_cvtps2pi( struct x86_function *p,
932 struct x86_reg dst,
933 struct x86_reg src )
934 {
935 DUMP_RR( dst, src );
936 assert(dst.file == file_MMX &&
937 (src.file == file_XMM || src.mod != mod_REG));
938
939 p->need_emms = 1;
940
941 emit_2ub(p, X86_TWOB, 0x2d);
942 emit_modrm( p, dst, src );
943 }
944
945 void sse2_cvtdq2ps( struct x86_function *p,
946 struct x86_reg dst,
947 struct x86_reg src )
948 {
949 DUMP_RR( dst, src );
950 emit_2ub(p, X86_TWOB, 0x5b);
951 emit_modrm( p, dst, src );
952 }
953
954
955 /* Shufps can also be used to implement a reduced swizzle when dest ==
956 * arg0.
957 */
958 void sse_shufps( struct x86_function *p,
959 struct x86_reg dst,
960 struct x86_reg src,
961 unsigned char shuf)
962 {
963 DUMP_RRI( dst, src, shuf );
964 emit_2ub(p, X86_TWOB, 0xC6);
965 emit_modrm(p, dst, src);
966 emit_1ub(p, shuf);
967 }
968
969 void sse_unpckhps( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
970 {
971 DUMP_RR( dst, src );
972 emit_2ub( p, X86_TWOB, 0x15 );
973 emit_modrm( p, dst, src );
974 }
975
976 void sse_unpcklps( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
977 {
978 DUMP_RR( dst, src );
979 emit_2ub( p, X86_TWOB, 0x14 );
980 emit_modrm( p, dst, src );
981 }
982
983 void sse_cmpps( struct x86_function *p,
984 struct x86_reg dst,
985 struct x86_reg src,
986 enum sse_cc cc)
987 {
988 DUMP_RRI( dst, src, cc );
989 emit_2ub(p, X86_TWOB, 0xC2);
990 emit_modrm(p, dst, src);
991 emit_1ub(p, cc);
992 }
993
994 void sse_pmovmskb( struct x86_function *p,
995 struct x86_reg dst,
996 struct x86_reg src)
997 {
998 DUMP_RR( dst, src );
999 emit_3ub(p, 0x66, X86_TWOB, 0xD7);
1000 emit_modrm(p, dst, src);
1001 }
1002
1003 void sse_movmskps( struct x86_function *p,
1004 struct x86_reg dst,
1005 struct x86_reg src)
1006 {
1007 DUMP_RR( dst, src );
1008 emit_2ub(p, X86_TWOB, 0x50);
1009 emit_modrm(p, dst, src);
1010 }
1011
1012 /***********************************************************************
1013 * SSE2 instructions
1014 */
1015
1016 /**
1017 * Perform a reduced swizzle:
1018 */
1019 void sse2_pshufd( struct x86_function *p,
1020 struct x86_reg dst,
1021 struct x86_reg src,
1022 unsigned char shuf)
1023 {
1024 DUMP_RRI( dst, src, shuf );
1025 emit_3ub(p, 0x66, X86_TWOB, 0x70);
1026 emit_modrm(p, dst, src);
1027 emit_1ub(p, shuf);
1028 }
1029
1030 void sse2_cvttps2dq( struct x86_function *p,
1031 struct x86_reg dst,
1032 struct x86_reg src )
1033 {
1034 DUMP_RR( dst, src );
1035 emit_3ub( p, 0xF3, X86_TWOB, 0x5B );
1036 emit_modrm( p, dst, src );
1037 }
1038
1039 void sse2_cvtps2dq( struct x86_function *p,
1040 struct x86_reg dst,
1041 struct x86_reg src )
1042 {
1043 DUMP_RR( dst, src );
1044 emit_3ub(p, 0x66, X86_TWOB, 0x5B);
1045 emit_modrm( p, dst, src );
1046 }
1047
1048 void sse2_packssdw( struct x86_function *p,
1049 struct x86_reg dst,
1050 struct x86_reg src )
1051 {
1052 DUMP_RR( dst, src );
1053 emit_3ub(p, 0x66, X86_TWOB, 0x6B);
1054 emit_modrm( p, dst, src );
1055 }
1056
1057 void sse2_packsswb( struct x86_function *p,
1058 struct x86_reg dst,
1059 struct x86_reg src )
1060 {
1061 DUMP_RR( dst, src );
1062 emit_3ub(p, 0x66, X86_TWOB, 0x63);
1063 emit_modrm( p, dst, src );
1064 }
1065
1066 void sse2_packuswb( struct x86_function *p,
1067 struct x86_reg dst,
1068 struct x86_reg src )
1069 {
1070 DUMP_RR( dst, src );
1071 emit_3ub(p, 0x66, X86_TWOB, 0x67);
1072 emit_modrm( p, dst, src );
1073 }
1074
1075 void sse2_punpcklbw( struct x86_function *p,
1076 struct x86_reg dst,
1077 struct x86_reg src )
1078 {
1079 DUMP_RR( dst, src );
1080 emit_3ub(p, 0x66, X86_TWOB, 0x60);
1081 emit_modrm( p, dst, src );
1082 }
1083
1084
1085 void sse2_rcpps( struct x86_function *p,
1086 struct x86_reg dst,
1087 struct x86_reg src )
1088 {
1089 DUMP_RR( dst, src );
1090 emit_2ub(p, X86_TWOB, 0x53);
1091 emit_modrm( p, dst, src );
1092 }
1093
1094 void sse2_rcpss( struct x86_function *p,
1095 struct x86_reg dst,
1096 struct x86_reg src )
1097 {
1098 DUMP_RR( dst, src );
1099 emit_3ub(p, 0xF3, X86_TWOB, 0x53);
1100 emit_modrm( p, dst, src );
1101 }
1102
1103 void sse2_movd( struct x86_function *p,
1104 struct x86_reg dst,
1105 struct x86_reg src )
1106 {
1107 DUMP_RR( dst, src );
1108 emit_2ub(p, 0x66, X86_TWOB);
1109 emit_op_modrm( p, 0x6e, 0x7e, dst, src );
1110 }
1111
1112
1113
1114
1115 /***********************************************************************
1116 * x87 instructions
1117 */
1118 static void note_x87_pop( struct x86_function *p )
1119 {
1120 p->x87_stack--;
1121 assert(p->x87_stack >= 0);
1122 }
1123
1124 static void note_x87_push( struct x86_function *p )
1125 {
1126 p->x87_stack++;
1127 assert(p->x87_stack <= 7);
1128 }
1129
1130 void x87_assert_stack_empty( struct x86_function *p )
1131 {
1132 assert (p->x87_stack == 0);
1133 }
1134
1135
1136 void x87_fist( struct x86_function *p, struct x86_reg dst )
1137 {
1138 DUMP_R( dst );
1139 emit_1ub(p, 0xdb);
1140 emit_modrm_noreg(p, 2, dst);
1141 }
1142
1143 void x87_fistp( struct x86_function *p, struct x86_reg dst )
1144 {
1145 DUMP_R( dst );
1146 emit_1ub(p, 0xdb);
1147 emit_modrm_noreg(p, 3, dst);
1148 note_x87_pop(p);
1149 }
1150
1151 void x87_fild( struct x86_function *p, struct x86_reg arg )
1152 {
1153 DUMP_R( arg );
1154 emit_1ub(p, 0xdf);
1155 emit_modrm_noreg(p, 0, arg);
1156 note_x87_push(p);
1157 }
1158
1159 void x87_fldz( struct x86_function *p )
1160 {
1161 DUMP();
1162 emit_2ub(p, 0xd9, 0xee);
1163 note_x87_push(p);
1164 }
1165
1166
1167 void x87_fldcw( struct x86_function *p, struct x86_reg arg )
1168 {
1169 DUMP_R( arg );
1170 assert(arg.file == file_REG32);
1171 assert(arg.mod != mod_REG);
1172 emit_1ub(p, 0xd9);
1173 emit_modrm_noreg(p, 5, arg);
1174 }
1175
1176 void x87_fld1( struct x86_function *p )
1177 {
1178 DUMP();
1179 emit_2ub(p, 0xd9, 0xe8);
1180 note_x87_push(p);
1181 }
1182
1183 void x87_fldl2e( struct x86_function *p )
1184 {
1185 DUMP();
1186 emit_2ub(p, 0xd9, 0xea);
1187 note_x87_push(p);
1188 }
1189
1190 void x87_fldln2( struct x86_function *p )
1191 {
1192 DUMP();
1193 emit_2ub(p, 0xd9, 0xed);
1194 note_x87_push(p);
1195 }
1196
1197 void x87_fwait( struct x86_function *p )
1198 {
1199 DUMP();
1200 emit_1ub(p, 0x9b);
1201 }
1202
1203 void x87_fnclex( struct x86_function *p )
1204 {
1205 DUMP();
1206 emit_2ub(p, 0xdb, 0xe2);
1207 }
1208
1209 void x87_fclex( struct x86_function *p )
1210 {
1211 x87_fwait(p);
1212 x87_fnclex(p);
1213 }
1214
1215 void x87_fcmovb( struct x86_function *p, struct x86_reg arg )
1216 {
1217 DUMP_R( arg );
1218 assert(arg.file == file_x87);
1219 emit_2ub(p, 0xda, 0xc0+arg.idx);
1220 }
1221
1222 void x87_fcmove( struct x86_function *p, struct x86_reg arg )
1223 {
1224 DUMP_R( arg );
1225 assert(arg.file == file_x87);
1226 emit_2ub(p, 0xda, 0xc8+arg.idx);
1227 }
1228
1229 void x87_fcmovbe( struct x86_function *p, struct x86_reg arg )
1230 {
1231 DUMP_R( arg );
1232 assert(arg.file == file_x87);
1233 emit_2ub(p, 0xda, 0xd0+arg.idx);
1234 }
1235
1236 void x87_fcmovnb( struct x86_function *p, struct x86_reg arg )
1237 {
1238 DUMP_R( arg );
1239 assert(arg.file == file_x87);
1240 emit_2ub(p, 0xdb, 0xc0+arg.idx);
1241 }
1242
1243 void x87_fcmovne( struct x86_function *p, struct x86_reg arg )
1244 {
1245 DUMP_R( arg );
1246 assert(arg.file == file_x87);
1247 emit_2ub(p, 0xdb, 0xc8+arg.idx);
1248 }
1249
1250 void x87_fcmovnbe( struct x86_function *p, struct x86_reg arg )
1251 {
1252 DUMP_R( arg );
1253 assert(arg.file == file_x87);
1254 emit_2ub(p, 0xdb, 0xd0+arg.idx);
1255 }
1256
1257
1258
1259 static void x87_arith_op( struct x86_function *p, struct x86_reg dst, struct x86_reg arg,
1260 unsigned char dst0ub0,
1261 unsigned char dst0ub1,
1262 unsigned char arg0ub0,
1263 unsigned char arg0ub1,
1264 unsigned char argmem_noreg)
1265 {
1266 assert(dst.file == file_x87);
1267
1268 if (arg.file == file_x87) {
1269 if (dst.idx == 0)
1270 emit_2ub(p, dst0ub0, dst0ub1+arg.idx);
1271 else if (arg.idx == 0)
1272 emit_2ub(p, arg0ub0, arg0ub1+arg.idx);
1273 else
1274 assert(0);
1275 }
1276 else if (dst.idx == 0) {
1277 assert(arg.file == file_REG32);
1278 emit_1ub(p, 0xd8);
1279 emit_modrm_noreg(p, argmem_noreg, arg);
1280 }
1281 else
1282 assert(0);
1283 }
1284
1285 void x87_fmul( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
1286 {
1287 DUMP_RR( dst, src );
1288 x87_arith_op(p, dst, src,
1289 0xd8, 0xc8,
1290 0xdc, 0xc8,
1291 4);
1292 }
1293
1294 void x87_fsub( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
1295 {
1296 DUMP_RR( dst, src );
1297 x87_arith_op(p, dst, src,
1298 0xd8, 0xe0,
1299 0xdc, 0xe8,
1300 4);
1301 }
1302
1303 void x87_fsubr( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
1304 {
1305 DUMP_RR( dst, src );
1306 x87_arith_op(p, dst, src,
1307 0xd8, 0xe8,
1308 0xdc, 0xe0,
1309 5);
1310 }
1311
1312 void x87_fadd( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
1313 {
1314 DUMP_RR( dst, src );
1315 x87_arith_op(p, dst, src,
1316 0xd8, 0xc0,
1317 0xdc, 0xc0,
1318 0);
1319 }
1320
1321 void x87_fdiv( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
1322 {
1323 DUMP_RR( dst, src );
1324 x87_arith_op(p, dst, src,
1325 0xd8, 0xf0,
1326 0xdc, 0xf8,
1327 6);
1328 }
1329
1330 void x87_fdivr( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
1331 {
1332 DUMP_RR( dst, src );
1333 x87_arith_op(p, dst, src,
1334 0xd8, 0xf8,
1335 0xdc, 0xf0,
1336 7);
1337 }
1338
1339 void x87_fmulp( struct x86_function *p, struct x86_reg dst )
1340 {
1341 DUMP_R( dst );
1342 assert(dst.file == file_x87);
1343 assert(dst.idx >= 1);
1344 emit_2ub(p, 0xde, 0xc8+dst.idx);
1345 note_x87_pop(p);
1346 }
1347
1348 void x87_fsubp( struct x86_function *p, struct x86_reg dst )
1349 {
1350 DUMP_R( dst );
1351 assert(dst.file == file_x87);
1352 assert(dst.idx >= 1);
1353 emit_2ub(p, 0xde, 0xe8+dst.idx);
1354 note_x87_pop(p);
1355 }
1356
1357 void x87_fsubrp( struct x86_function *p, struct x86_reg dst )
1358 {
1359 DUMP_R( dst );
1360 assert(dst.file == file_x87);
1361 assert(dst.idx >= 1);
1362 emit_2ub(p, 0xde, 0xe0+dst.idx);
1363 note_x87_pop(p);
1364 }
1365
1366 void x87_faddp( struct x86_function *p, struct x86_reg dst )
1367 {
1368 DUMP_R( dst );
1369 assert(dst.file == file_x87);
1370 assert(dst.idx >= 1);
1371 emit_2ub(p, 0xde, 0xc0+dst.idx);
1372 note_x87_pop(p);
1373 }
1374
1375 void x87_fdivp( struct x86_function *p, struct x86_reg dst )
1376 {
1377 DUMP_R( dst );
1378 assert(dst.file == file_x87);
1379 assert(dst.idx >= 1);
1380 emit_2ub(p, 0xde, 0xf8+dst.idx);
1381 note_x87_pop(p);
1382 }
1383
1384 void x87_fdivrp( struct x86_function *p, struct x86_reg dst )
1385 {
1386 DUMP_R( dst );
1387 assert(dst.file == file_x87);
1388 assert(dst.idx >= 1);
1389 emit_2ub(p, 0xde, 0xf0+dst.idx);
1390 note_x87_pop(p);
1391 }
1392
1393 void x87_ftst( struct x86_function *p )
1394 {
1395 DUMP();
1396 emit_2ub(p, 0xd9, 0xe4);
1397 }
1398
1399 void x87_fucom( struct x86_function *p, struct x86_reg arg )
1400 {
1401 DUMP_R( arg );
1402 assert(arg.file == file_x87);
1403 emit_2ub(p, 0xdd, 0xe0+arg.idx);
1404 }
1405
1406 void x87_fucomp( struct x86_function *p, struct x86_reg arg )
1407 {
1408 DUMP_R( arg );
1409 assert(arg.file == file_x87);
1410 emit_2ub(p, 0xdd, 0xe8+arg.idx);
1411 note_x87_pop(p);
1412 }
1413
1414 void x87_fucompp( struct x86_function *p )
1415 {
1416 DUMP();
1417 emit_2ub(p, 0xda, 0xe9);
1418 note_x87_pop(p); /* pop twice */
1419 note_x87_pop(p); /* pop twice */
1420 }
1421
1422 void x87_fxch( struct x86_function *p, struct x86_reg arg )
1423 {
1424 DUMP_R( arg );
1425 assert(arg.file == file_x87);
1426 emit_2ub(p, 0xd9, 0xc8+arg.idx);
1427 }
1428
1429 void x87_fabs( struct x86_function *p )
1430 {
1431 DUMP();
1432 emit_2ub(p, 0xd9, 0xe1);
1433 }
1434
1435 void x87_fchs( struct x86_function *p )
1436 {
1437 DUMP();
1438 emit_2ub(p, 0xd9, 0xe0);
1439 }
1440
1441 void x87_fcos( struct x86_function *p )
1442 {
1443 DUMP();
1444 emit_2ub(p, 0xd9, 0xff);
1445 }
1446
1447
1448 void x87_fprndint( struct x86_function *p )
1449 {
1450 DUMP();
1451 emit_2ub(p, 0xd9, 0xfc);
1452 }
1453
1454 void x87_fscale( struct x86_function *p )
1455 {
1456 DUMP();
1457 emit_2ub(p, 0xd9, 0xfd);
1458 }
1459
1460 void x87_fsin( struct x86_function *p )
1461 {
1462 DUMP();
1463 emit_2ub(p, 0xd9, 0xfe);
1464 }
1465
1466 void x87_fsincos( struct x86_function *p )
1467 {
1468 DUMP();
1469 emit_2ub(p, 0xd9, 0xfb);
1470 }
1471
1472 void x87_fsqrt( struct x86_function *p )
1473 {
1474 DUMP();
1475 emit_2ub(p, 0xd9, 0xfa);
1476 }
1477
1478 void x87_fxtract( struct x86_function *p )
1479 {
1480 DUMP();
1481 emit_2ub(p, 0xd9, 0xf4);
1482 }
1483
1484 /* st0 = (2^st0)-1
1485 *
1486 * Restrictions: -1.0 <= st0 <= 1.0
1487 */
1488 void x87_f2xm1( struct x86_function *p )
1489 {
1490 DUMP();
1491 emit_2ub(p, 0xd9, 0xf0);
1492 }
1493
1494 /* st1 = st1 * log2(st0);
1495 * pop_stack;
1496 */
1497 void x87_fyl2x( struct x86_function *p )
1498 {
1499 DUMP();
1500 emit_2ub(p, 0xd9, 0xf1);
1501 note_x87_pop(p);
1502 }
1503
1504 /* st1 = st1 * log2(st0 + 1.0);
1505 * pop_stack;
1506 *
1507 * A fast operation, with restrictions: -.29 < st0 < .29
1508 */
1509 void x87_fyl2xp1( struct x86_function *p )
1510 {
1511 DUMP();
1512 emit_2ub(p, 0xd9, 0xf9);
1513 note_x87_pop(p);
1514 }
1515
1516
1517 void x87_fld( struct x86_function *p, struct x86_reg arg )
1518 {
1519 DUMP_R( arg );
1520 if (arg.file == file_x87)
1521 emit_2ub(p, 0xd9, 0xc0 + arg.idx);
1522 else {
1523 emit_1ub(p, 0xd9);
1524 emit_modrm_noreg(p, 0, arg);
1525 }
1526 note_x87_push(p);
1527 }
1528
1529 void x87_fst( struct x86_function *p, struct x86_reg dst )
1530 {
1531 DUMP_R( dst );
1532 if (dst.file == file_x87)
1533 emit_2ub(p, 0xdd, 0xd0 + dst.idx);
1534 else {
1535 emit_1ub(p, 0xd9);
1536 emit_modrm_noreg(p, 2, dst);
1537 }
1538 }
1539
1540 void x87_fstp( struct x86_function *p, struct x86_reg dst )
1541 {
1542 DUMP_R( dst );
1543 if (dst.file == file_x87)
1544 emit_2ub(p, 0xdd, 0xd8 + dst.idx);
1545 else {
1546 emit_1ub(p, 0xd9);
1547 emit_modrm_noreg(p, 3, dst);
1548 }
1549 note_x87_pop(p);
1550 }
1551
1552 void x87_fpop( struct x86_function *p )
1553 {
1554 x87_fstp( p, x86_make_reg( file_x87, 0 ));
1555 }
1556
1557
1558 void x87_fcom( struct x86_function *p, struct x86_reg dst )
1559 {
1560 DUMP_R( dst );
1561 if (dst.file == file_x87)
1562 emit_2ub(p, 0xd8, 0xd0 + dst.idx);
1563 else {
1564 emit_1ub(p, 0xd8);
1565 emit_modrm_noreg(p, 2, dst);
1566 }
1567 }
1568
1569
1570 void x87_fcomp( struct x86_function *p, struct x86_reg dst )
1571 {
1572 DUMP_R( dst );
1573 if (dst.file == file_x87)
1574 emit_2ub(p, 0xd8, 0xd8 + dst.idx);
1575 else {
1576 emit_1ub(p, 0xd8);
1577 emit_modrm_noreg(p, 3, dst);
1578 }
1579 note_x87_pop(p);
1580 }
1581
1582 void x87_fcomi( struct x86_function *p, struct x86_reg arg )
1583 {
1584 DUMP_R( arg );
1585 emit_2ub(p, 0xdb, 0xf0+arg.idx);
1586 }
1587
1588 void x87_fcomip( struct x86_function *p, struct x86_reg arg )
1589 {
1590 DUMP_R( arg );
1591 emit_2ub(p, 0xdb, 0xf0+arg.idx);
1592 note_x87_pop(p);
1593 }
1594
1595
1596 void x87_fnstsw( struct x86_function *p, struct x86_reg dst )
1597 {
1598 DUMP_R( dst );
1599 assert(dst.file == file_REG32);
1600
1601 if (dst.idx == reg_AX &&
1602 dst.mod == mod_REG)
1603 emit_2ub(p, 0xdf, 0xe0);
1604 else {
1605 emit_1ub(p, 0xdd);
1606 emit_modrm_noreg(p, 7, dst);
1607 }
1608 }
1609
1610
1611 void x87_fnstcw( struct x86_function *p, struct x86_reg dst )
1612 {
1613 DUMP_R( dst );
1614 assert(dst.file == file_REG32);
1615
1616 emit_1ub(p, 0x9b); /* WAIT -- needed? */
1617 emit_1ub(p, 0xd9);
1618 emit_modrm_noreg(p, 7, dst);
1619 }
1620
1621
1622
1623
1624 /***********************************************************************
1625 * MMX instructions
1626 */
1627
1628 void mmx_emms( struct x86_function *p )
1629 {
1630 DUMP();
1631 assert(p->need_emms);
1632 emit_2ub(p, 0x0f, 0x77);
1633 p->need_emms = 0;
1634 }
1635
1636 void mmx_packssdw( struct x86_function *p,
1637 struct x86_reg dst,
1638 struct x86_reg src )
1639 {
1640 DUMP_RR( dst, src );
1641 assert(dst.file == file_MMX &&
1642 (src.file == file_MMX || src.mod != mod_REG));
1643
1644 p->need_emms = 1;
1645
1646 emit_2ub(p, X86_TWOB, 0x6b);
1647 emit_modrm( p, dst, src );
1648 }
1649
1650 void mmx_packuswb( 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, 0x67);
1661 emit_modrm( p, dst, src );
1662 }
1663
1664 void mmx_movd( struct x86_function *p,
1665 struct x86_reg dst,
1666 struct x86_reg src )
1667 {
1668 DUMP_RR( dst, src );
1669 p->need_emms = 1;
1670 emit_1ub(p, X86_TWOB);
1671 emit_op_modrm( p, 0x6e, 0x7e, dst, src );
1672 }
1673
1674 void mmx_movq( struct x86_function *p,
1675 struct x86_reg dst,
1676 struct x86_reg src )
1677 {
1678 DUMP_RR( dst, src );
1679 p->need_emms = 1;
1680 emit_1ub(p, X86_TWOB);
1681 emit_op_modrm( p, 0x6f, 0x7f, dst, src );
1682 }
1683
1684
1685 /***********************************************************************
1686 * Helper functions
1687 */
1688
1689
1690 void x86_cdecl_caller_push_regs( struct x86_function *p )
1691 {
1692 x86_push(p, x86_make_reg(file_REG32, reg_AX));
1693 x86_push(p, x86_make_reg(file_REG32, reg_CX));
1694 x86_push(p, x86_make_reg(file_REG32, reg_DX));
1695 }
1696
1697 void x86_cdecl_caller_pop_regs( struct x86_function *p )
1698 {
1699 x86_pop(p, x86_make_reg(file_REG32, reg_DX));
1700 x86_pop(p, x86_make_reg(file_REG32, reg_CX));
1701 x86_pop(p, x86_make_reg(file_REG32, reg_AX));
1702 }
1703
1704
1705 /* Retreive a reference to one of the function arguments, taking into
1706 * account any push/pop activity:
1707 */
1708 struct x86_reg x86_fn_arg( struct x86_function *p,
1709 unsigned arg )
1710 {
1711 return x86_make_disp(x86_make_reg(file_REG32, reg_SP),
1712 p->stack_offset + arg * 4); /* ??? */
1713 }
1714
1715
1716 void x86_init_func( struct x86_function *p )
1717 {
1718 p->size = 0;
1719 p->store = NULL;
1720 p->csr = p->store;
1721 DUMP_START();
1722 }
1723
1724 void x86_init_func_size( struct x86_function *p, unsigned code_size )
1725 {
1726 p->size = code_size;
1727 p->store = rtasm_exec_malloc(code_size);
1728 if (p->store == NULL) {
1729 p->store = p->error_overflow;
1730 }
1731 p->csr = p->store;
1732 DUMP_START();
1733 }
1734
1735 void x86_release_func( struct x86_function *p )
1736 {
1737 if (p->store && p->store != p->error_overflow)
1738 rtasm_exec_free(p->store);
1739
1740 p->store = NULL;
1741 p->csr = NULL;
1742 p->size = 0;
1743 }
1744
1745
1746 void (*x86_get_func( struct x86_function *p ))(void)
1747 {
1748 DUMP_END();
1749 if (DISASSEM && p->store)
1750 debug_printf("disassemble %p %p\n", p->store, p->csr);
1751
1752 if (p->store == p->error_overflow)
1753 return (void (*)(void)) NULL;
1754 else
1755 return (void (*)(void)) p->store;
1756 }
1757
1758 #else
1759
1760 void x86sse_dummy( void )
1761 {
1762 }
1763
1764 #endif