Merge remote branch 'origin/gallium-0.2' into gallium-0.2
[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 "pipe/p_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% 15s ", 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 }
376
377 if (offset <= 127 && offset >= -128) {
378 emit_1ub(p, 0x70 + cc);
379 emit_1b(p, (char) offset);
380 }
381 else {
382 offset = label - (x86_get_label(p) + 6);
383 emit_2ub(p, 0x0f, 0x80 + cc);
384 emit_1i(p, offset);
385 }
386 }
387
388 /* Always use a 32bit offset for forward jumps:
389 */
390 int x86_jcc_forward( struct x86_function *p,
391 enum x86_cc cc )
392 {
393 DUMP_I(cc);
394 emit_2ub(p, 0x0f, 0x80 + cc);
395 emit_1i(p, 0);
396 return x86_get_label(p);
397 }
398
399 int x86_jmp_forward( struct x86_function *p)
400 {
401 DUMP();
402 emit_1ub(p, 0xe9);
403 emit_1i(p, 0);
404 return x86_get_label(p);
405 }
406
407 int x86_call_forward( struct x86_function *p)
408 {
409 DUMP();
410
411 emit_1ub(p, 0xe8);
412 emit_1i(p, 0);
413 return x86_get_label(p);
414 }
415
416 /* Fixup offset from forward jump:
417 */
418 void x86_fixup_fwd_jump( struct x86_function *p,
419 int fixup )
420 {
421 *(int *)(p->store + fixup - 4) = x86_get_label(p) - fixup;
422 }
423
424 void x86_jmp( struct x86_function *p, int label)
425 {
426 DUMP_I( label );
427 emit_1ub(p, 0xe9);
428 emit_1i(p, label - x86_get_label(p) - 4);
429 }
430
431 void x86_call( struct x86_function *p, struct x86_reg reg)
432 {
433 DUMP_R( reg );
434 emit_1ub(p, 0xff);
435 emit_modrm_noreg(p, 2, reg);
436 }
437
438
439 void x86_mov_reg_imm( struct x86_function *p, struct x86_reg dst, int imm )
440 {
441 DUMP_RI( dst, imm );
442 assert(dst.file == file_REG32);
443 assert(dst.mod == mod_REG);
444 emit_1ub(p, 0xb8 + dst.idx);
445 emit_1i(p, imm);
446 }
447
448 /**
449 * Immediate group 1 instructions.
450 */
451 static INLINE void
452 x86_group1_imm( struct x86_function *p,
453 unsigned op, struct x86_reg dst, int imm )
454 {
455 assert(dst.file == file_REG32);
456 assert(dst.mod == mod_REG);
457 if(-0x80 <= imm && imm < 0x80) {
458 emit_1ub(p, 0x83);
459 emit_modrm_noreg(p, op, dst);
460 emit_1b(p, (char)imm);
461 }
462 else {
463 emit_1ub(p, 0x81);
464 emit_modrm_noreg(p, op, dst);
465 emit_1i(p, imm);
466 }
467 }
468
469 void x86_add_imm( struct x86_function *p, struct x86_reg dst, int imm )
470 {
471 DUMP_RI( dst, imm );
472 x86_group1_imm(p, 0, dst, imm);
473 }
474
475 void x86_or_imm( struct x86_function *p, struct x86_reg dst, int imm )
476 {
477 DUMP_RI( dst, imm );
478 x86_group1_imm(p, 1, dst, imm);
479 }
480
481 void x86_and_imm( struct x86_function *p, struct x86_reg dst, int imm )
482 {
483 DUMP_RI( dst, imm );
484 x86_group1_imm(p, 4, dst, imm);
485 }
486
487 void x86_sub_imm( struct x86_function *p, struct x86_reg dst, int imm )
488 {
489 DUMP_RI( dst, imm );
490 x86_group1_imm(p, 5, dst, imm);
491 }
492
493 void x86_xor_imm( struct x86_function *p, struct x86_reg dst, int imm )
494 {
495 DUMP_RI( dst, imm );
496 x86_group1_imm(p, 6, dst, imm);
497 }
498
499 void x86_cmp_imm( struct x86_function *p, struct x86_reg dst, int imm )
500 {
501 DUMP_RI( dst, imm );
502 x86_group1_imm(p, 7, dst, imm);
503 }
504
505
506 void x86_push( struct x86_function *p,
507 struct x86_reg reg )
508 {
509 DUMP_R( reg );
510 if (reg.mod == mod_REG)
511 emit_1ub(p, 0x50 + reg.idx);
512 else
513 {
514 emit_1ub(p, 0xff);
515 emit_modrm_noreg(p, 6, reg);
516 }
517
518
519 p->stack_offset += 4;
520 }
521
522 void x86_push_imm32( struct x86_function *p,
523 int imm32 )
524 {
525 DUMP_I( imm32 );
526 emit_1ub(p, 0x68);
527 emit_1i(p, imm32);
528
529 p->stack_offset += 4;
530 }
531
532
533 void x86_pop( struct x86_function *p,
534 struct x86_reg reg )
535 {
536 DUMP_R( reg );
537 assert(reg.mod == mod_REG);
538 emit_1ub(p, 0x58 + reg.idx);
539 p->stack_offset -= 4;
540 }
541
542 void x86_inc( struct x86_function *p,
543 struct x86_reg reg )
544 {
545 DUMP_R( reg );
546 assert(reg.mod == mod_REG);
547 emit_1ub(p, 0x40 + reg.idx);
548 }
549
550 void x86_dec( struct x86_function *p,
551 struct x86_reg reg )
552 {
553 DUMP_R( reg );
554 assert(reg.mod == mod_REG);
555 emit_1ub(p, 0x48 + reg.idx);
556 }
557
558 void x86_ret( struct x86_function *p )
559 {
560 DUMP();
561 assert(p->stack_offset == 0);
562 emit_1ub(p, 0xc3);
563 }
564
565 void x86_retw( struct x86_function *p, unsigned short imm )
566 {
567 DUMP();
568 emit_3ub(p, 0xc2, imm & 0xff, (imm >> 8) & 0xff);
569 }
570
571 void x86_sahf( struct x86_function *p )
572 {
573 DUMP();
574 emit_1ub(p, 0x9e);
575 }
576
577 void x86_mov( struct x86_function *p,
578 struct x86_reg dst,
579 struct x86_reg src )
580 {
581 DUMP_RR( dst, src );
582 emit_op_modrm( p, 0x8b, 0x89, dst, src );
583 }
584
585 void x86_xor( struct x86_function *p,
586 struct x86_reg dst,
587 struct x86_reg src )
588 {
589 DUMP_RR( dst, src );
590 emit_op_modrm( p, 0x33, 0x31, dst, src );
591 }
592
593 void x86_cmp( struct x86_function *p,
594 struct x86_reg dst,
595 struct x86_reg src )
596 {
597 DUMP_RR( dst, src );
598 emit_op_modrm( p, 0x3b, 0x39, dst, src );
599 }
600
601 void x86_lea( struct x86_function *p,
602 struct x86_reg dst,
603 struct x86_reg src )
604 {
605 DUMP_RR( dst, src );
606 emit_1ub(p, 0x8d);
607 emit_modrm( p, dst, src );
608 }
609
610 void x86_test( struct x86_function *p,
611 struct x86_reg dst,
612 struct x86_reg src )
613 {
614 DUMP_RR( dst, src );
615 emit_1ub(p, 0x85);
616 emit_modrm( p, dst, src );
617 }
618
619 void x86_add( struct x86_function *p,
620 struct x86_reg dst,
621 struct x86_reg src )
622 {
623 DUMP_RR( dst, src );
624 emit_op_modrm(p, 0x03, 0x01, dst, src );
625 }
626
627 /* Calculate EAX * src, results in EDX:EAX.
628 */
629 void x86_mul( struct x86_function *p,
630 struct x86_reg src )
631 {
632 DUMP_R( src );
633 emit_1ub(p, 0xf7);
634 emit_modrm_noreg(p, 4, src );
635 }
636
637
638 void x86_imul( struct x86_function *p,
639 struct x86_reg dst,
640 struct x86_reg src )
641 {
642 DUMP_RR( dst, src );
643 emit_2ub(p, X86_TWOB, 0xAF);
644 emit_modrm(p, dst, src);
645 }
646
647
648 void x86_sub( struct x86_function *p,
649 struct x86_reg dst,
650 struct x86_reg src )
651 {
652 DUMP_RR( dst, src );
653 emit_op_modrm(p, 0x2b, 0x29, dst, src );
654 }
655
656 void x86_or( struct x86_function *p,
657 struct x86_reg dst,
658 struct x86_reg src )
659 {
660 DUMP_RR( dst, src );
661 emit_op_modrm( p, 0x0b, 0x09, dst, src );
662 }
663
664 void x86_and( struct x86_function *p,
665 struct x86_reg dst,
666 struct x86_reg src )
667 {
668 DUMP_RR( dst, src );
669 emit_op_modrm( p, 0x23, 0x21, dst, src );
670 }
671
672
673
674 /***********************************************************************
675 * SSE instructions
676 */
677
678
679 void sse_movss( struct x86_function *p,
680 struct x86_reg dst,
681 struct x86_reg src )
682 {
683 DUMP_RR( dst, src );
684 emit_2ub(p, 0xF3, X86_TWOB);
685 emit_op_modrm( p, 0x10, 0x11, dst, src );
686 }
687
688 void sse_movaps( struct x86_function *p,
689 struct x86_reg dst,
690 struct x86_reg src )
691 {
692 DUMP_RR( dst, src );
693 emit_1ub(p, X86_TWOB);
694 emit_op_modrm( p, 0x28, 0x29, dst, src );
695 }
696
697 void sse_movups( struct x86_function *p,
698 struct x86_reg dst,
699 struct x86_reg src )
700 {
701 DUMP_RR( dst, src );
702 emit_1ub(p, X86_TWOB);
703 emit_op_modrm( p, 0x10, 0x11, dst, src );
704 }
705
706 void sse_movhps( struct x86_function *p,
707 struct x86_reg dst,
708 struct x86_reg src )
709 {
710 DUMP_RR( dst, src );
711 assert(dst.mod != mod_REG || src.mod != mod_REG);
712 emit_1ub(p, X86_TWOB);
713 emit_op_modrm( p, 0x16, 0x17, dst, src ); /* cf movlhps */
714 }
715
716 void sse_movlps( struct x86_function *p,
717 struct x86_reg dst,
718 struct x86_reg src )
719 {
720 DUMP_RR( dst, src );
721 assert(dst.mod != mod_REG || src.mod != mod_REG);
722 emit_1ub(p, X86_TWOB);
723 emit_op_modrm( p, 0x12, 0x13, dst, src ); /* cf movhlps */
724 }
725
726 void sse_maxps( struct x86_function *p,
727 struct x86_reg dst,
728 struct x86_reg src )
729 {
730 DUMP_RR( dst, src );
731 emit_2ub(p, X86_TWOB, 0x5F);
732 emit_modrm( p, dst, src );
733 }
734
735 void sse_maxss( struct x86_function *p,
736 struct x86_reg dst,
737 struct x86_reg src )
738 {
739 DUMP_RR( dst, src );
740 emit_3ub(p, 0xF3, X86_TWOB, 0x5F);
741 emit_modrm( p, dst, src );
742 }
743
744 void sse_divss( struct x86_function *p,
745 struct x86_reg dst,
746 struct x86_reg src )
747 {
748 DUMP_RR( dst, src );
749 emit_3ub(p, 0xF3, X86_TWOB, 0x5E);
750 emit_modrm( p, dst, src );
751 }
752
753 void sse_minps( struct x86_function *p,
754 struct x86_reg dst,
755 struct x86_reg src )
756 {
757 DUMP_RR( dst, src );
758 emit_2ub(p, X86_TWOB, 0x5D);
759 emit_modrm( p, dst, src );
760 }
761
762 void sse_subps( struct x86_function *p,
763 struct x86_reg dst,
764 struct x86_reg src )
765 {
766 DUMP_RR( dst, src );
767 emit_2ub(p, X86_TWOB, 0x5C);
768 emit_modrm( p, dst, src );
769 }
770
771 void sse_mulps( struct x86_function *p,
772 struct x86_reg dst,
773 struct x86_reg src )
774 {
775 DUMP_RR( dst, src );
776 emit_2ub(p, X86_TWOB, 0x59);
777 emit_modrm( p, dst, src );
778 }
779
780 void sse_mulss( struct x86_function *p,
781 struct x86_reg dst,
782 struct x86_reg src )
783 {
784 DUMP_RR( dst, src );
785 emit_3ub(p, 0xF3, X86_TWOB, 0x59);
786 emit_modrm( p, dst, src );
787 }
788
789 void sse_addps( 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, 0x58);
795 emit_modrm( p, dst, src );
796 }
797
798 void sse_addss( 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, 0x58);
804 emit_modrm( p, dst, src );
805 }
806
807 void sse_andnps( struct x86_function *p,
808 struct x86_reg dst,
809 struct x86_reg src )
810 {
811 DUMP_RR( dst, src );
812 emit_2ub(p, X86_TWOB, 0x55);
813 emit_modrm( p, dst, src );
814 }
815
816 void sse_andps( 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, 0x54);
822 emit_modrm( p, dst, src );
823 }
824
825 void sse_rsqrtps( 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, 0x52);
831 emit_modrm( p, dst, src );
832 }
833
834 void sse_rsqrtss( struct x86_function *p,
835 struct x86_reg dst,
836 struct x86_reg src )
837 {
838 DUMP_RR( dst, src );
839 emit_3ub(p, 0xF3, X86_TWOB, 0x52);
840 emit_modrm( p, dst, src );
841
842 }
843
844 void sse_movhlps( struct x86_function *p,
845 struct x86_reg dst,
846 struct x86_reg src )
847 {
848 DUMP_RR( dst, src );
849 assert(dst.mod == mod_REG && src.mod == mod_REG);
850 emit_2ub(p, X86_TWOB, 0x12);
851 emit_modrm( p, dst, src );
852 }
853
854 void sse_movlhps( struct x86_function *p,
855 struct x86_reg dst,
856 struct x86_reg src )
857 {
858 DUMP_RR( dst, src );
859 assert(dst.mod == mod_REG && src.mod == mod_REG);
860 emit_2ub(p, X86_TWOB, 0x16);
861 emit_modrm( p, dst, src );
862 }
863
864 void sse_orps( struct x86_function *p,
865 struct x86_reg dst,
866 struct x86_reg src )
867 {
868 DUMP_RR( dst, src );
869 emit_2ub(p, X86_TWOB, 0x56);
870 emit_modrm( p, dst, src );
871 }
872
873 void sse_xorps( struct x86_function *p,
874 struct x86_reg dst,
875 struct x86_reg src )
876 {
877 DUMP_RR( dst, src );
878 emit_2ub(p, X86_TWOB, 0x57);
879 emit_modrm( p, dst, src );
880 }
881
882 void sse_cvtps2pi( struct x86_function *p,
883 struct x86_reg dst,
884 struct x86_reg src )
885 {
886 DUMP_RR( dst, src );
887 assert(dst.file == file_MMX &&
888 (src.file == file_XMM || src.mod != mod_REG));
889
890 p->need_emms = 1;
891
892 emit_2ub(p, X86_TWOB, 0x2d);
893 emit_modrm( p, dst, src );
894 }
895
896 void sse2_cvtdq2ps( struct x86_function *p,
897 struct x86_reg dst,
898 struct x86_reg src )
899 {
900 DUMP_RR( dst, src );
901 emit_2ub(p, X86_TWOB, 0x5b);
902 emit_modrm( p, dst, src );
903 }
904
905
906 /* Shufps can also be used to implement a reduced swizzle when dest ==
907 * arg0.
908 */
909 void sse_shufps( struct x86_function *p,
910 struct x86_reg dst,
911 struct x86_reg src,
912 unsigned char shuf)
913 {
914 DUMP_RRI( dst, src, shuf );
915 emit_2ub(p, X86_TWOB, 0xC6);
916 emit_modrm(p, dst, src);
917 emit_1ub(p, shuf);
918 }
919
920 void sse_unpckhps( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
921 {
922 DUMP_RR( dst, src );
923 emit_2ub( p, X86_TWOB, 0x15 );
924 emit_modrm( p, dst, src );
925 }
926
927 void sse_unpcklps( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
928 {
929 DUMP_RR( dst, src );
930 emit_2ub( p, X86_TWOB, 0x14 );
931 emit_modrm( p, dst, src );
932 }
933
934 void sse_cmpps( struct x86_function *p,
935 struct x86_reg dst,
936 struct x86_reg src,
937 enum sse_cc cc)
938 {
939 DUMP_RRI( dst, src, cc );
940 emit_2ub(p, X86_TWOB, 0xC2);
941 emit_modrm(p, dst, src);
942 emit_1ub(p, cc);
943 }
944
945 void sse_pmovmskb( struct x86_function *p,
946 struct x86_reg dst,
947 struct x86_reg src)
948 {
949 DUMP_RR( dst, src );
950 emit_3ub(p, 0x66, X86_TWOB, 0xD7);
951 emit_modrm(p, dst, src);
952 }
953
954 /***********************************************************************
955 * SSE2 instructions
956 */
957
958 /**
959 * Perform a reduced swizzle:
960 */
961 void sse2_pshufd( struct x86_function *p,
962 struct x86_reg dst,
963 struct x86_reg src,
964 unsigned char shuf)
965 {
966 DUMP_RRI( dst, src, shuf );
967 emit_3ub(p, 0x66, X86_TWOB, 0x70);
968 emit_modrm(p, dst, src);
969 emit_1ub(p, shuf);
970 }
971
972 void sse2_cvttps2dq( struct x86_function *p,
973 struct x86_reg dst,
974 struct x86_reg src )
975 {
976 DUMP_RR( dst, src );
977 emit_3ub( p, 0xF3, X86_TWOB, 0x5B );
978 emit_modrm( p, dst, src );
979 }
980
981 void sse2_cvtps2dq( struct x86_function *p,
982 struct x86_reg dst,
983 struct x86_reg src )
984 {
985 DUMP_RR( dst, src );
986 emit_3ub(p, 0x66, X86_TWOB, 0x5B);
987 emit_modrm( p, dst, src );
988 }
989
990 void sse2_packssdw( struct x86_function *p,
991 struct x86_reg dst,
992 struct x86_reg src )
993 {
994 DUMP_RR( dst, src );
995 emit_3ub(p, 0x66, X86_TWOB, 0x6B);
996 emit_modrm( p, dst, src );
997 }
998
999 void sse2_packsswb( struct x86_function *p,
1000 struct x86_reg dst,
1001 struct x86_reg src )
1002 {
1003 DUMP_RR( dst, src );
1004 emit_3ub(p, 0x66, X86_TWOB, 0x63);
1005 emit_modrm( p, dst, src );
1006 }
1007
1008 void sse2_packuswb( 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, 0x67);
1014 emit_modrm( p, dst, src );
1015 }
1016
1017 void sse2_punpcklbw( struct x86_function *p,
1018 struct x86_reg dst,
1019 struct x86_reg src )
1020 {
1021 DUMP_RR( dst, src );
1022 emit_3ub(p, 0x66, X86_TWOB, 0x60);
1023 emit_modrm( p, dst, src );
1024 }
1025
1026
1027 void sse2_rcpps( struct x86_function *p,
1028 struct x86_reg dst,
1029 struct x86_reg src )
1030 {
1031 DUMP_RR( dst, src );
1032 emit_2ub(p, X86_TWOB, 0x53);
1033 emit_modrm( p, dst, src );
1034 }
1035
1036 void sse2_rcpss( struct x86_function *p,
1037 struct x86_reg dst,
1038 struct x86_reg src )
1039 {
1040 DUMP_RR( dst, src );
1041 emit_3ub(p, 0xF3, X86_TWOB, 0x53);
1042 emit_modrm( p, dst, src );
1043 }
1044
1045 void sse2_movd( struct x86_function *p,
1046 struct x86_reg dst,
1047 struct x86_reg src )
1048 {
1049 DUMP_RR( dst, src );
1050 emit_2ub(p, 0x66, X86_TWOB);
1051 emit_op_modrm( p, 0x6e, 0x7e, dst, src );
1052 }
1053
1054
1055
1056
1057 /***********************************************************************
1058 * x87 instructions
1059 */
1060 static void note_x87_pop( struct x86_function *p )
1061 {
1062 p->x87_stack--;
1063 assert(p->x87_stack >= 0);
1064 }
1065
1066 static void note_x87_push( struct x86_function *p )
1067 {
1068 p->x87_stack++;
1069 assert(p->x87_stack <= 7);
1070 }
1071
1072 void x87_assert_stack_empty( struct x86_function *p )
1073 {
1074 assert (p->x87_stack == 0);
1075 }
1076
1077
1078 void x87_fist( struct x86_function *p, struct x86_reg dst )
1079 {
1080 DUMP_R( dst );
1081 emit_1ub(p, 0xdb);
1082 emit_modrm_noreg(p, 2, dst);
1083 }
1084
1085 void x87_fistp( struct x86_function *p, struct x86_reg dst )
1086 {
1087 DUMP_R( dst );
1088 emit_1ub(p, 0xdb);
1089 emit_modrm_noreg(p, 3, dst);
1090 note_x87_pop(p);
1091 }
1092
1093 void x87_fild( struct x86_function *p, struct x86_reg arg )
1094 {
1095 DUMP_R( arg );
1096 emit_1ub(p, 0xdf);
1097 emit_modrm_noreg(p, 0, arg);
1098 note_x87_push(p);
1099 }
1100
1101 void x87_fldz( struct x86_function *p )
1102 {
1103 DUMP();
1104 emit_2ub(p, 0xd9, 0xee);
1105 note_x87_push(p);
1106 }
1107
1108
1109 void x87_fldcw( struct x86_function *p, struct x86_reg arg )
1110 {
1111 DUMP_R( arg );
1112 assert(arg.file == file_REG32);
1113 assert(arg.mod != mod_REG);
1114 emit_1ub(p, 0xd9);
1115 emit_modrm_noreg(p, 5, arg);
1116 }
1117
1118 void x87_fld1( struct x86_function *p )
1119 {
1120 DUMP();
1121 emit_2ub(p, 0xd9, 0xe8);
1122 note_x87_push(p);
1123 }
1124
1125 void x87_fldl2e( struct x86_function *p )
1126 {
1127 DUMP();
1128 emit_2ub(p, 0xd9, 0xea);
1129 note_x87_push(p);
1130 }
1131
1132 void x87_fldln2( struct x86_function *p )
1133 {
1134 DUMP();
1135 emit_2ub(p, 0xd9, 0xed);
1136 note_x87_push(p);
1137 }
1138
1139 void x87_fwait( struct x86_function *p )
1140 {
1141 DUMP();
1142 emit_1ub(p, 0x9b);
1143 }
1144
1145 void x87_fnclex( struct x86_function *p )
1146 {
1147 DUMP();
1148 emit_2ub(p, 0xdb, 0xe2);
1149 }
1150
1151 void x87_fclex( struct x86_function *p )
1152 {
1153 x87_fwait(p);
1154 x87_fnclex(p);
1155 }
1156
1157 void x87_fcmovb( struct x86_function *p, struct x86_reg arg )
1158 {
1159 DUMP_R( arg );
1160 assert(arg.file == file_x87);
1161 emit_2ub(p, 0xda, 0xc0+arg.idx);
1162 }
1163
1164 void x87_fcmove( struct x86_function *p, struct x86_reg arg )
1165 {
1166 DUMP_R( arg );
1167 assert(arg.file == file_x87);
1168 emit_2ub(p, 0xda, 0xc8+arg.idx);
1169 }
1170
1171 void x87_fcmovbe( struct x86_function *p, struct x86_reg arg )
1172 {
1173 DUMP_R( arg );
1174 assert(arg.file == file_x87);
1175 emit_2ub(p, 0xda, 0xd0+arg.idx);
1176 }
1177
1178 void x87_fcmovnb( struct x86_function *p, struct x86_reg arg )
1179 {
1180 DUMP_R( arg );
1181 assert(arg.file == file_x87);
1182 emit_2ub(p, 0xdb, 0xc0+arg.idx);
1183 }
1184
1185 void x87_fcmovne( struct x86_function *p, struct x86_reg arg )
1186 {
1187 DUMP_R( arg );
1188 assert(arg.file == file_x87);
1189 emit_2ub(p, 0xdb, 0xc8+arg.idx);
1190 }
1191
1192 void x87_fcmovnbe( struct x86_function *p, struct x86_reg arg )
1193 {
1194 DUMP_R( arg );
1195 assert(arg.file == file_x87);
1196 emit_2ub(p, 0xdb, 0xd0+arg.idx);
1197 }
1198
1199
1200
1201 static void x87_arith_op( struct x86_function *p, struct x86_reg dst, struct x86_reg arg,
1202 unsigned char dst0ub0,
1203 unsigned char dst0ub1,
1204 unsigned char arg0ub0,
1205 unsigned char arg0ub1,
1206 unsigned char argmem_noreg)
1207 {
1208 assert(dst.file == file_x87);
1209
1210 if (arg.file == file_x87) {
1211 if (dst.idx == 0)
1212 emit_2ub(p, dst0ub0, dst0ub1+arg.idx);
1213 else if (arg.idx == 0)
1214 emit_2ub(p, arg0ub0, arg0ub1+arg.idx);
1215 else
1216 assert(0);
1217 }
1218 else if (dst.idx == 0) {
1219 assert(arg.file == file_REG32);
1220 emit_1ub(p, 0xd8);
1221 emit_modrm_noreg(p, argmem_noreg, arg);
1222 }
1223 else
1224 assert(0);
1225 }
1226
1227 void x87_fmul( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
1228 {
1229 DUMP_RR( dst, src );
1230 x87_arith_op(p, dst, src,
1231 0xd8, 0xc8,
1232 0xdc, 0xc8,
1233 4);
1234 }
1235
1236 void x87_fsub( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
1237 {
1238 DUMP_RR( dst, src );
1239 x87_arith_op(p, dst, src,
1240 0xd8, 0xe0,
1241 0xdc, 0xe8,
1242 4);
1243 }
1244
1245 void x87_fsubr( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
1246 {
1247 DUMP_RR( dst, src );
1248 x87_arith_op(p, dst, src,
1249 0xd8, 0xe8,
1250 0xdc, 0xe0,
1251 5);
1252 }
1253
1254 void x87_fadd( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
1255 {
1256 DUMP_RR( dst, src );
1257 x87_arith_op(p, dst, src,
1258 0xd8, 0xc0,
1259 0xdc, 0xc0,
1260 0);
1261 }
1262
1263 void x87_fdiv( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
1264 {
1265 DUMP_RR( dst, src );
1266 x87_arith_op(p, dst, src,
1267 0xd8, 0xf0,
1268 0xdc, 0xf8,
1269 6);
1270 }
1271
1272 void x87_fdivr( struct x86_function *p, struct x86_reg dst, struct x86_reg src )
1273 {
1274 DUMP_RR( dst, src );
1275 x87_arith_op(p, dst, src,
1276 0xd8, 0xf8,
1277 0xdc, 0xf0,
1278 7);
1279 }
1280
1281 void x87_fmulp( struct x86_function *p, struct x86_reg dst )
1282 {
1283 DUMP_R( dst );
1284 assert(dst.file == file_x87);
1285 assert(dst.idx >= 1);
1286 emit_2ub(p, 0xde, 0xc8+dst.idx);
1287 note_x87_pop(p);
1288 }
1289
1290 void x87_fsubp( struct x86_function *p, struct x86_reg dst )
1291 {
1292 DUMP_R( dst );
1293 assert(dst.file == file_x87);
1294 assert(dst.idx >= 1);
1295 emit_2ub(p, 0xde, 0xe8+dst.idx);
1296 note_x87_pop(p);
1297 }
1298
1299 void x87_fsubrp( struct x86_function *p, struct x86_reg dst )
1300 {
1301 DUMP_R( dst );
1302 assert(dst.file == file_x87);
1303 assert(dst.idx >= 1);
1304 emit_2ub(p, 0xde, 0xe0+dst.idx);
1305 note_x87_pop(p);
1306 }
1307
1308 void x87_faddp( struct x86_function *p, struct x86_reg dst )
1309 {
1310 DUMP_R( dst );
1311 assert(dst.file == file_x87);
1312 assert(dst.idx >= 1);
1313 emit_2ub(p, 0xde, 0xc0+dst.idx);
1314 note_x87_pop(p);
1315 }
1316
1317 void x87_fdivp( struct x86_function *p, struct x86_reg dst )
1318 {
1319 DUMP_R( dst );
1320 assert(dst.file == file_x87);
1321 assert(dst.idx >= 1);
1322 emit_2ub(p, 0xde, 0xf8+dst.idx);
1323 note_x87_pop(p);
1324 }
1325
1326 void x87_fdivrp( struct x86_function *p, struct x86_reg dst )
1327 {
1328 DUMP_R( dst );
1329 assert(dst.file == file_x87);
1330 assert(dst.idx >= 1);
1331 emit_2ub(p, 0xde, 0xf0+dst.idx);
1332 note_x87_pop(p);
1333 }
1334
1335 void x87_ftst( struct x86_function *p )
1336 {
1337 DUMP();
1338 emit_2ub(p, 0xd9, 0xe4);
1339 }
1340
1341 void x87_fucom( struct x86_function *p, struct x86_reg arg )
1342 {
1343 DUMP_R( arg );
1344 assert(arg.file == file_x87);
1345 emit_2ub(p, 0xdd, 0xe0+arg.idx);
1346 }
1347
1348 void x87_fucomp( struct x86_function *p, struct x86_reg arg )
1349 {
1350 DUMP_R( arg );
1351 assert(arg.file == file_x87);
1352 emit_2ub(p, 0xdd, 0xe8+arg.idx);
1353 note_x87_pop(p);
1354 }
1355
1356 void x87_fucompp( struct x86_function *p )
1357 {
1358 DUMP();
1359 emit_2ub(p, 0xda, 0xe9);
1360 note_x87_pop(p); /* pop twice */
1361 note_x87_pop(p); /* pop twice */
1362 }
1363
1364 void x87_fxch( struct x86_function *p, struct x86_reg arg )
1365 {
1366 DUMP_R( arg );
1367 assert(arg.file == file_x87);
1368 emit_2ub(p, 0xd9, 0xc8+arg.idx);
1369 }
1370
1371 void x87_fabs( struct x86_function *p )
1372 {
1373 DUMP();
1374 emit_2ub(p, 0xd9, 0xe1);
1375 }
1376
1377 void x87_fchs( struct x86_function *p )
1378 {
1379 DUMP();
1380 emit_2ub(p, 0xd9, 0xe0);
1381 }
1382
1383 void x87_fcos( struct x86_function *p )
1384 {
1385 DUMP();
1386 emit_2ub(p, 0xd9, 0xff);
1387 }
1388
1389
1390 void x87_fprndint( struct x86_function *p )
1391 {
1392 DUMP();
1393 emit_2ub(p, 0xd9, 0xfc);
1394 }
1395
1396 void x87_fscale( struct x86_function *p )
1397 {
1398 DUMP();
1399 emit_2ub(p, 0xd9, 0xfd);
1400 }
1401
1402 void x87_fsin( struct x86_function *p )
1403 {
1404 DUMP();
1405 emit_2ub(p, 0xd9, 0xfe);
1406 }
1407
1408 void x87_fsincos( struct x86_function *p )
1409 {
1410 DUMP();
1411 emit_2ub(p, 0xd9, 0xfb);
1412 }
1413
1414 void x87_fsqrt( struct x86_function *p )
1415 {
1416 DUMP();
1417 emit_2ub(p, 0xd9, 0xfa);
1418 }
1419
1420 void x87_fxtract( struct x86_function *p )
1421 {
1422 DUMP();
1423 emit_2ub(p, 0xd9, 0xf4);
1424 }
1425
1426 /* st0 = (2^st0)-1
1427 *
1428 * Restrictions: -1.0 <= st0 <= 1.0
1429 */
1430 void x87_f2xm1( struct x86_function *p )
1431 {
1432 DUMP();
1433 emit_2ub(p, 0xd9, 0xf0);
1434 }
1435
1436 /* st1 = st1 * log2(st0);
1437 * pop_stack;
1438 */
1439 void x87_fyl2x( struct x86_function *p )
1440 {
1441 DUMP();
1442 emit_2ub(p, 0xd9, 0xf1);
1443 note_x87_pop(p);
1444 }
1445
1446 /* st1 = st1 * log2(st0 + 1.0);
1447 * pop_stack;
1448 *
1449 * A fast operation, with restrictions: -.29 < st0 < .29
1450 */
1451 void x87_fyl2xp1( struct x86_function *p )
1452 {
1453 DUMP();
1454 emit_2ub(p, 0xd9, 0xf9);
1455 note_x87_pop(p);
1456 }
1457
1458
1459 void x87_fld( struct x86_function *p, struct x86_reg arg )
1460 {
1461 DUMP_R( arg );
1462 if (arg.file == file_x87)
1463 emit_2ub(p, 0xd9, 0xc0 + arg.idx);
1464 else {
1465 emit_1ub(p, 0xd9);
1466 emit_modrm_noreg(p, 0, arg);
1467 }
1468 note_x87_push(p);
1469 }
1470
1471 void x87_fst( struct x86_function *p, struct x86_reg dst )
1472 {
1473 DUMP_R( dst );
1474 if (dst.file == file_x87)
1475 emit_2ub(p, 0xdd, 0xd0 + dst.idx);
1476 else {
1477 emit_1ub(p, 0xd9);
1478 emit_modrm_noreg(p, 2, dst);
1479 }
1480 }
1481
1482 void x87_fstp( struct x86_function *p, struct x86_reg dst )
1483 {
1484 DUMP_R( dst );
1485 if (dst.file == file_x87)
1486 emit_2ub(p, 0xdd, 0xd8 + dst.idx);
1487 else {
1488 emit_1ub(p, 0xd9);
1489 emit_modrm_noreg(p, 3, dst);
1490 }
1491 note_x87_pop(p);
1492 }
1493
1494 void x87_fpop( struct x86_function *p )
1495 {
1496 x87_fstp( p, x86_make_reg( file_x87, 0 ));
1497 }
1498
1499
1500 void x87_fcom( struct x86_function *p, struct x86_reg dst )
1501 {
1502 DUMP_R( dst );
1503 if (dst.file == file_x87)
1504 emit_2ub(p, 0xd8, 0xd0 + dst.idx);
1505 else {
1506 emit_1ub(p, 0xd8);
1507 emit_modrm_noreg(p, 2, dst);
1508 }
1509 }
1510
1511
1512 void x87_fcomp( struct x86_function *p, struct x86_reg dst )
1513 {
1514 DUMP_R( dst );
1515 if (dst.file == file_x87)
1516 emit_2ub(p, 0xd8, 0xd8 + dst.idx);
1517 else {
1518 emit_1ub(p, 0xd8);
1519 emit_modrm_noreg(p, 3, dst);
1520 }
1521 note_x87_pop(p);
1522 }
1523
1524 void x87_fcomi( struct x86_function *p, struct x86_reg arg )
1525 {
1526 DUMP_R( arg );
1527 emit_2ub(p, 0xdb, 0xf0+arg.idx);
1528 }
1529
1530 void x87_fcomip( struct x86_function *p, struct x86_reg arg )
1531 {
1532 DUMP_R( arg );
1533 emit_2ub(p, 0xdb, 0xf0+arg.idx);
1534 note_x87_pop(p);
1535 }
1536
1537
1538 void x87_fnstsw( struct x86_function *p, struct x86_reg dst )
1539 {
1540 DUMP_R( dst );
1541 assert(dst.file == file_REG32);
1542
1543 if (dst.idx == reg_AX &&
1544 dst.mod == mod_REG)
1545 emit_2ub(p, 0xdf, 0xe0);
1546 else {
1547 emit_1ub(p, 0xdd);
1548 emit_modrm_noreg(p, 7, dst);
1549 }
1550 }
1551
1552
1553 void x87_fnstcw( struct x86_function *p, struct x86_reg dst )
1554 {
1555 DUMP_R( dst );
1556 assert(dst.file == file_REG32);
1557
1558 emit_1ub(p, 0x9b); /* WAIT -- needed? */
1559 emit_1ub(p, 0xd9);
1560 emit_modrm_noreg(p, 7, dst);
1561 }
1562
1563
1564
1565
1566 /***********************************************************************
1567 * MMX instructions
1568 */
1569
1570 void mmx_emms( struct x86_function *p )
1571 {
1572 DUMP();
1573 assert(p->need_emms);
1574 emit_2ub(p, 0x0f, 0x77);
1575 p->need_emms = 0;
1576 }
1577
1578 void mmx_packssdw( struct x86_function *p,
1579 struct x86_reg dst,
1580 struct x86_reg src )
1581 {
1582 DUMP_RR( dst, src );
1583 assert(dst.file == file_MMX &&
1584 (src.file == file_MMX || src.mod != mod_REG));
1585
1586 p->need_emms = 1;
1587
1588 emit_2ub(p, X86_TWOB, 0x6b);
1589 emit_modrm( p, dst, src );
1590 }
1591
1592 void mmx_packuswb( struct x86_function *p,
1593 struct x86_reg dst,
1594 struct x86_reg src )
1595 {
1596 DUMP_RR( dst, src );
1597 assert(dst.file == file_MMX &&
1598 (src.file == file_MMX || src.mod != mod_REG));
1599
1600 p->need_emms = 1;
1601
1602 emit_2ub(p, X86_TWOB, 0x67);
1603 emit_modrm( p, dst, src );
1604 }
1605
1606 void mmx_movd( struct x86_function *p,
1607 struct x86_reg dst,
1608 struct x86_reg src )
1609 {
1610 DUMP_RR( dst, src );
1611 p->need_emms = 1;
1612 emit_1ub(p, X86_TWOB);
1613 emit_op_modrm( p, 0x6e, 0x7e, dst, src );
1614 }
1615
1616 void mmx_movq( struct x86_function *p,
1617 struct x86_reg dst,
1618 struct x86_reg src )
1619 {
1620 DUMP_RR( dst, src );
1621 p->need_emms = 1;
1622 emit_1ub(p, X86_TWOB);
1623 emit_op_modrm( p, 0x6f, 0x7f, dst, src );
1624 }
1625
1626
1627 /***********************************************************************
1628 * Helper functions
1629 */
1630
1631
1632 void x86_cdecl_caller_push_regs( struct x86_function *p )
1633 {
1634 x86_push(p, x86_make_reg(file_REG32, reg_AX));
1635 x86_push(p, x86_make_reg(file_REG32, reg_CX));
1636 x86_push(p, x86_make_reg(file_REG32, reg_DX));
1637 }
1638
1639 void x86_cdecl_caller_pop_regs( struct x86_function *p )
1640 {
1641 x86_pop(p, x86_make_reg(file_REG32, reg_DX));
1642 x86_pop(p, x86_make_reg(file_REG32, reg_CX));
1643 x86_pop(p, x86_make_reg(file_REG32, reg_AX));
1644 }
1645
1646
1647 /* Retreive a reference to one of the function arguments, taking into
1648 * account any push/pop activity:
1649 */
1650 struct x86_reg x86_fn_arg( struct x86_function *p,
1651 unsigned arg )
1652 {
1653 return x86_make_disp(x86_make_reg(file_REG32, reg_SP),
1654 p->stack_offset + arg * 4); /* ??? */
1655 }
1656
1657
1658 void x86_init_func( struct x86_function *p )
1659 {
1660 p->size = 0;
1661 p->store = NULL;
1662 p->csr = p->store;
1663 DUMP_START();
1664 }
1665
1666 void x86_init_func_size( struct x86_function *p, unsigned code_size )
1667 {
1668 p->size = code_size;
1669 p->store = rtasm_exec_malloc(code_size);
1670 if (p->store == NULL) {
1671 p->store = p->error_overflow;
1672 }
1673 p->csr = p->store;
1674 DUMP_START();
1675 }
1676
1677 void x86_release_func( struct x86_function *p )
1678 {
1679 if (p->store && p->store != p->error_overflow)
1680 rtasm_exec_free(p->store);
1681
1682 p->store = NULL;
1683 p->csr = NULL;
1684 p->size = 0;
1685 }
1686
1687
1688 void (*x86_get_func( struct x86_function *p ))(void)
1689 {
1690 DUMP_END();
1691 if (DISASSEM && p->store)
1692 debug_printf("disassemble %p %p\n", p->store, p->csr);
1693
1694 if (p->store == p->error_overflow)
1695 return (void (*)(void)) NULL;
1696 else
1697 return (void (*)(void)) p->store;
1698 }
1699
1700 #else
1701
1702 void x86sse_dummy( void )
1703 {
1704 }
1705
1706 #endif