1 /**************************************************************************
3 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
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:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
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.
22 **************************************************************************/
24 #include "pipe/p_config.h"
26 #if defined(PIPE_ARCH_X86)
28 #include "pipe/p_compiler.h"
29 #include "pipe/p_debug.h"
30 #include "util/u_pointer.h"
32 #include "rtasm_execmem.h"
33 #include "rtasm_x86sse.h"
42 void x86_print_reg( struct x86_reg reg
)
44 if (reg
.mod
!= mod_REG
)
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;
61 debug_printf( "MMX%u", reg
.idx
);
64 debug_printf( "XMM%u", reg
.idx
);
67 debug_printf( "fp%u", reg
.idx
);
71 if (reg
.mod
== mod_DISP8
||
72 reg
.mod
== mod_DISP32
)
73 debug_printf("+%d", reg
.disp
);
75 if (reg
.mod
!= mod_REG
)
81 #define DUMP_START() debug_printf( "\n" )
82 #define DUMP_END() debug_printf( "\n" )
85 const char *foo = __FUNCTION__; \
86 while (*foo && *foo != '_') \
90 debug_printf( "\n% 4x% 15s ", p->csr - p->store, foo ); \
93 #define DUMP_I( I ) do { \
95 debug_printf( "%u", I ); \
98 #define DUMP_R( R0 ) do { \
100 x86_print_reg( R0 ); \
103 #define DUMP_RR( R0, R1 ) do { \
105 x86_print_reg( R0 ); \
106 debug_printf( ", " ); \
107 x86_print_reg( R1 ); \
110 #define DUMP_RI( R0, I ) do { \
112 x86_print_reg( R0 ); \
113 debug_printf( ", %u", I ); \
116 #define DUMP_RRI( R0, R1, I ) do { \
118 x86_print_reg( R0 ); \
119 debug_printf( ", " ); \
120 x86_print_reg( R1 ); \
121 debug_printf( ", %u", I ); \
131 #define DUMP_RR( R0, R1 )
132 #define DUMP_RI( R0, I )
133 #define DUMP_RRI( R0, R1, I )
138 static void do_realloc( struct x86_function
*p
)
140 if (p
->store
== p
->error_overflow
) {
143 else if (p
->size
== 0) {
145 p
->store
= rtasm_exec_malloc(p
->size
);
149 uintptr_t used
= pointer_to_uintptr( p
->csr
) - pointer_to_uintptr( p
->store
);
150 unsigned char *tmp
= p
->store
;
152 p
->store
= rtasm_exec_malloc(p
->size
);
155 memcpy(p
->store
, tmp
, used
);
156 p
->csr
= p
->store
+ used
;
162 rtasm_exec_free(tmp
);
165 if (p
->store
== NULL
) {
166 p
->store
= p
->csr
= p
->error_overflow
;
167 p
->size
= sizeof(p
->error_overflow
);
171 /* Emit bytes to the instruction stream:
173 static unsigned char *reserve( struct x86_function
*p
, int bytes
)
175 if (p
->csr
+ bytes
- p
->store
> (int) p
->size
)
179 unsigned char *csr
= p
->csr
;
187 static void emit_1b( struct x86_function
*p
, char b0
)
189 char *csr
= (char *)reserve(p
, 1);
193 static void emit_1i( struct x86_function
*p
, int i0
)
195 int *icsr
= (int *)reserve(p
, sizeof(i0
));
199 static void emit_1ub( struct x86_function
*p
, unsigned char b0
)
201 unsigned char *csr
= reserve(p
, 1);
205 static void emit_2ub( struct x86_function
*p
, unsigned char b0
, unsigned char b1
)
207 unsigned char *csr
= reserve(p
, 2);
212 static void emit_3ub( struct x86_function
*p
, unsigned char b0
, unsigned char b1
, unsigned char b2
)
214 unsigned char *csr
= reserve(p
, 3);
221 /* Build a modRM byte + possible displacement. No treatment of SIB
222 * indexing. BZZT - no way to encode an absolute address.
224 * This is the "/r" field in the x86 manuals...
226 static void emit_modrm( struct x86_function
*p
,
228 struct x86_reg regmem
)
230 unsigned char val
= 0;
232 assert(reg
.mod
== mod_REG
);
234 val
|= regmem
.mod
<< 6; /* mod field */
235 val
|= reg
.idx
<< 3; /* reg field */
236 val
|= regmem
.idx
; /* r/m field */
240 /* Oh-oh we've stumbled into the SIB thing.
242 if (regmem
.file
== file_REG32
&&
243 regmem
.idx
== reg_SP
) {
244 emit_1ub(p
, 0x24); /* simplistic! */
247 switch (regmem
.mod
) {
252 emit_1b(p
, (char) regmem
.disp
);
255 emit_1i(p
, regmem
.disp
);
263 /* Emits the "/0".."/7" specialized versions of the modrm ("/r") bytes.
265 static void emit_modrm_noreg( struct x86_function
*p
,
267 struct x86_reg regmem
)
269 struct x86_reg dummy
= x86_make_reg(file_REG32
, op
);
270 emit_modrm(p
, dummy
, regmem
);
273 /* Many x86 instructions have two opcodes to cope with the situations
274 * where the destination is a register or memory reference
275 * respectively. This function selects the correct opcode based on
276 * the arguments presented.
278 static void emit_op_modrm( struct x86_function
*p
,
279 unsigned char op_dst_is_reg
,
280 unsigned char op_dst_is_mem
,
286 emit_1ub(p
, op_dst_is_reg
);
287 emit_modrm(p
, dst
, src
);
292 assert(src
.mod
== mod_REG
);
293 emit_1ub(p
, op_dst_is_mem
);
294 emit_modrm(p
, src
, dst
);
308 /* Create and manipulate registers and regmem values:
310 struct x86_reg
x86_make_reg( enum x86_reg_file file
,
311 enum x86_reg_name idx
)
323 struct x86_reg
x86_make_disp( struct x86_reg reg
,
326 assert(reg
.file
== file_REG32
);
328 if (reg
.mod
== mod_REG
)
333 if (reg
.disp
== 0 && reg
.idx
!= reg_BP
)
334 reg
.mod
= mod_INDIRECT
;
335 else if (reg
.disp
<= 127 && reg
.disp
>= -128)
338 reg
.mod
= mod_DISP32
;
343 struct x86_reg
x86_deref( struct x86_reg reg
)
345 return x86_make_disp(reg
, 0);
348 struct x86_reg
x86_get_base_reg( struct x86_reg reg
)
350 return x86_make_reg( reg
.file
, reg
.idx
);
353 int x86_get_label( struct x86_function
*p
)
355 return p
->csr
- p
->store
;
360 /***********************************************************************
365 void x86_jcc( struct x86_function
*p
,
369 int offset
= label
- (x86_get_label(p
) + 2);
373 /*assert(p->csr - p->store > -offset);*/
374 if (p
->csr
- p
->store
<= -offset
) {
375 /* probably out of memory (using the error_overflow buffer) */
380 if (offset
<= 127 && offset
>= -128) {
381 emit_1ub(p
, 0x70 + cc
);
382 emit_1b(p
, (char) offset
);
385 offset
= label
- (x86_get_label(p
) + 6);
386 emit_2ub(p
, 0x0f, 0x80 + cc
);
391 /* Always use a 32bit offset for forward jumps:
393 int x86_jcc_forward( struct x86_function
*p
,
397 emit_2ub(p
, 0x0f, 0x80 + cc
);
399 return x86_get_label(p
);
402 int x86_jmp_forward( struct x86_function
*p
)
407 return x86_get_label(p
);
410 int x86_call_forward( struct x86_function
*p
)
416 return x86_get_label(p
);
419 /* Fixup offset from forward jump:
421 void x86_fixup_fwd_jump( struct x86_function
*p
,
424 *(int *)(p
->store
+ fixup
- 4) = x86_get_label(p
) - fixup
;
427 void x86_jmp( struct x86_function
*p
, int label
)
431 emit_1i(p
, label
- x86_get_label(p
) - 4);
434 void x86_call( struct x86_function
*p
, struct x86_reg reg
)
438 emit_modrm_noreg(p
, 2, reg
);
443 * Temporary. As I need immediate operands, and dont want to mess with the codegen,
444 * I load the immediate into general purpose register and use it.
446 void x86_mov_reg_imm( struct x86_function
*p
, struct x86_reg dst
, int imm
)
449 assert(dst
.mod
== mod_REG
);
450 emit_1ub(p
, 0xb8 + dst
.idx
);
454 void x86_add_reg_imm8( struct x86_function
*p
, struct x86_reg dst
, ubyte imm
)
457 assert(dst
.mod
== mod_REG
);
459 emit_modrm_noreg(p
, 0, dst
);
464 void x86_push( struct x86_function
*p
,
468 if (reg
.mod
== mod_REG
)
469 emit_1ub(p
, 0x50 + reg
.idx
);
473 emit_modrm_noreg(p
, 6, reg
);
477 p
->stack_offset
+= 4;
480 void x86_push_imm32( struct x86_function
*p
,
487 p
->stack_offset
+= 4;
491 void x86_pop( struct x86_function
*p
,
495 assert(reg
.mod
== mod_REG
);
496 emit_1ub(p
, 0x58 + reg
.idx
);
497 p
->stack_offset
-= 4;
500 void x86_inc( struct x86_function
*p
,
504 assert(reg
.mod
== mod_REG
);
505 emit_1ub(p
, 0x40 + reg
.idx
);
508 void x86_dec( struct x86_function
*p
,
512 assert(reg
.mod
== mod_REG
);
513 emit_1ub(p
, 0x48 + reg
.idx
);
516 void x86_ret( struct x86_function
*p
)
519 assert(p
->stack_offset
== 0);
523 void x86_retw( struct x86_function
*p
, unsigned short imm
)
526 emit_3ub(p
, 0xc2, imm
& 0xff, (imm
>> 8) & 0xff);
529 void x86_sahf( struct x86_function
*p
)
535 void x86_mov( struct x86_function
*p
,
540 emit_op_modrm( p
, 0x8b, 0x89, dst
, src
);
543 void x86_xor( struct x86_function
*p
,
548 emit_op_modrm( p
, 0x33, 0x31, dst
, src
);
551 void x86_cmp( struct x86_function
*p
,
556 emit_op_modrm( p
, 0x3b, 0x39, dst
, src
);
559 void x86_lea( struct x86_function
*p
,
565 emit_modrm( p
, dst
, src
);
568 void x86_test( struct x86_function
*p
,
574 emit_modrm( p
, dst
, src
);
577 void x86_add( struct x86_function
*p
,
582 emit_op_modrm(p
, 0x03, 0x01, dst
, src
);
585 /* Calculate EAX * src, results in EDX:EAX.
587 void x86_mul( struct x86_function
*p
,
592 emit_modrm_noreg(p
, 4, src
);
596 void x86_imul( struct x86_function
*p
,
601 emit_2ub(p
, X86_TWOB
, 0xAF);
602 emit_modrm(p
, dst
, src
);
606 void x86_sub( struct x86_function
*p
,
611 emit_op_modrm(p
, 0x2b, 0x29, dst
, src
);
614 void x86_or( struct x86_function
*p
,
619 emit_op_modrm( p
, 0x0b, 0x09, dst
, src
);
622 void x86_and( struct x86_function
*p
,
627 emit_op_modrm( p
, 0x23, 0x21, dst
, src
);
632 /***********************************************************************
636 void sse_prefetchnta( struct x86_function
*p
, struct x86_reg ptr
)
639 assert(ptr
.mod
!= mod_REG
);
640 emit_2ub(p
, 0x0f, 0x18);
641 emit_modrm_noreg(p
, 0, ptr
);
644 void sse_prefetch0( struct x86_function
*p
, struct x86_reg ptr
)
647 assert(ptr
.mod
!= mod_REG
);
648 emit_2ub(p
, 0x0f, 0x18);
649 emit_modrm_noreg(p
, 1, ptr
);
652 void sse_prefetch1( struct x86_function
*p
, struct x86_reg ptr
)
655 assert(ptr
.mod
!= mod_REG
);
656 emit_2ub(p
, 0x0f, 0x18);
657 emit_modrm_noreg(p
, 2, ptr
);
660 void sse_movntps( struct x86_function
*p
,
666 assert(dst
.mod
!= mod_REG
);
667 assert(src
.mod
== mod_REG
);
668 emit_2ub(p
, 0x0f, 0x2b);
669 emit_modrm(p
, src
, dst
);
675 void sse_movss( struct x86_function
*p
,
680 emit_2ub(p
, 0xF3, X86_TWOB
);
681 emit_op_modrm( p
, 0x10, 0x11, dst
, src
);
684 void sse_movaps( struct x86_function
*p
,
689 emit_1ub(p
, X86_TWOB
);
690 emit_op_modrm( p
, 0x28, 0x29, dst
, src
);
693 void sse_movups( struct x86_function
*p
,
698 emit_1ub(p
, X86_TWOB
);
699 emit_op_modrm( p
, 0x10, 0x11, dst
, src
);
702 void sse_movhps( struct x86_function
*p
,
707 assert(dst
.mod
!= mod_REG
|| src
.mod
!= mod_REG
);
708 emit_1ub(p
, X86_TWOB
);
709 emit_op_modrm( p
, 0x16, 0x17, dst
, src
); /* cf movlhps */
712 void sse_movlps( struct x86_function
*p
,
717 assert(dst
.mod
!= mod_REG
|| src
.mod
!= mod_REG
);
718 emit_1ub(p
, X86_TWOB
);
719 emit_op_modrm( p
, 0x12, 0x13, dst
, src
); /* cf movhlps */
722 void sse_maxps( struct x86_function
*p
,
727 emit_2ub(p
, X86_TWOB
, 0x5F);
728 emit_modrm( p
, dst
, src
);
731 void sse_maxss( struct x86_function
*p
,
736 emit_3ub(p
, 0xF3, X86_TWOB
, 0x5F);
737 emit_modrm( p
, dst
, src
);
740 void sse_divss( struct x86_function
*p
,
745 emit_3ub(p
, 0xF3, X86_TWOB
, 0x5E);
746 emit_modrm( p
, dst
, src
);
749 void sse_minps( struct x86_function
*p
,
754 emit_2ub(p
, X86_TWOB
, 0x5D);
755 emit_modrm( p
, dst
, src
);
758 void sse_subps( struct x86_function
*p
,
763 emit_2ub(p
, X86_TWOB
, 0x5C);
764 emit_modrm( p
, dst
, src
);
767 void sse_mulps( struct x86_function
*p
,
772 emit_2ub(p
, X86_TWOB
, 0x59);
773 emit_modrm( p
, dst
, src
);
776 void sse_mulss( struct x86_function
*p
,
781 emit_3ub(p
, 0xF3, X86_TWOB
, 0x59);
782 emit_modrm( p
, dst
, src
);
785 void sse_addps( struct x86_function
*p
,
790 emit_2ub(p
, X86_TWOB
, 0x58);
791 emit_modrm( p
, dst
, src
);
794 void sse_addss( struct x86_function
*p
,
799 emit_3ub(p
, 0xF3, X86_TWOB
, 0x58);
800 emit_modrm( p
, dst
, src
);
803 void sse_andnps( struct x86_function
*p
,
808 emit_2ub(p
, X86_TWOB
, 0x55);
809 emit_modrm( p
, dst
, src
);
812 void sse_andps( struct x86_function
*p
,
817 emit_2ub(p
, X86_TWOB
, 0x54);
818 emit_modrm( p
, dst
, src
);
821 void sse_rsqrtps( struct x86_function
*p
,
826 emit_2ub(p
, X86_TWOB
, 0x52);
827 emit_modrm( p
, dst
, src
);
830 void sse_rsqrtss( struct x86_function
*p
,
835 emit_3ub(p
, 0xF3, X86_TWOB
, 0x52);
836 emit_modrm( p
, dst
, src
);
840 void sse_movhlps( struct x86_function
*p
,
845 assert(dst
.mod
== mod_REG
&& src
.mod
== mod_REG
);
846 emit_2ub(p
, X86_TWOB
, 0x12);
847 emit_modrm( p
, dst
, src
);
850 void sse_movlhps( struct x86_function
*p
,
855 assert(dst
.mod
== mod_REG
&& src
.mod
== mod_REG
);
856 emit_2ub(p
, X86_TWOB
, 0x16);
857 emit_modrm( p
, dst
, src
);
860 void sse_orps( struct x86_function
*p
,
865 emit_2ub(p
, X86_TWOB
, 0x56);
866 emit_modrm( p
, dst
, src
);
869 void sse_xorps( struct x86_function
*p
,
874 emit_2ub(p
, X86_TWOB
, 0x57);
875 emit_modrm( p
, dst
, src
);
878 void sse_cvtps2pi( struct x86_function
*p
,
883 assert(dst
.file
== file_MMX
&&
884 (src
.file
== file_XMM
|| src
.mod
!= mod_REG
));
888 emit_2ub(p
, X86_TWOB
, 0x2d);
889 emit_modrm( p
, dst
, src
);
892 void sse2_cvtdq2ps( struct x86_function
*p
,
897 emit_2ub(p
, X86_TWOB
, 0x5b);
898 emit_modrm( p
, dst
, src
);
902 /* Shufps can also be used to implement a reduced swizzle when dest ==
905 void sse_shufps( struct x86_function
*p
,
910 DUMP_RRI( dst
, src
, shuf
);
911 emit_2ub(p
, X86_TWOB
, 0xC6);
912 emit_modrm(p
, dst
, src
);
916 void sse_unpckhps( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
919 emit_2ub( p
, X86_TWOB
, 0x15 );
920 emit_modrm( p
, dst
, src
);
923 void sse_unpcklps( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
926 emit_2ub( p
, X86_TWOB
, 0x14 );
927 emit_modrm( p
, dst
, src
);
930 void sse_cmpps( struct x86_function
*p
,
935 DUMP_RRI( dst
, src
, cc
);
936 emit_2ub(p
, X86_TWOB
, 0xC2);
937 emit_modrm(p
, dst
, src
);
941 void sse_pmovmskb( struct x86_function
*p
,
946 emit_3ub(p
, 0x66, X86_TWOB
, 0xD7);
947 emit_modrm(p
, dst
, src
);
950 /***********************************************************************
955 * Perform a reduced swizzle:
957 void sse2_pshufd( struct x86_function
*p
,
962 DUMP_RRI( dst
, src
, shuf
);
963 emit_3ub(p
, 0x66, X86_TWOB
, 0x70);
964 emit_modrm(p
, dst
, src
);
968 void sse2_cvttps2dq( struct x86_function
*p
,
973 emit_3ub( p
, 0xF3, X86_TWOB
, 0x5B );
974 emit_modrm( p
, dst
, src
);
977 void sse2_cvtps2dq( struct x86_function
*p
,
982 emit_3ub(p
, 0x66, X86_TWOB
, 0x5B);
983 emit_modrm( p
, dst
, src
);
986 void sse2_packssdw( struct x86_function
*p
,
991 emit_3ub(p
, 0x66, X86_TWOB
, 0x6B);
992 emit_modrm( p
, dst
, src
);
995 void sse2_packsswb( struct x86_function
*p
,
1000 emit_3ub(p
, 0x66, X86_TWOB
, 0x63);
1001 emit_modrm( p
, dst
, src
);
1004 void sse2_packuswb( struct x86_function
*p
,
1006 struct x86_reg src
)
1008 DUMP_RR( dst
, src
);
1009 emit_3ub(p
, 0x66, X86_TWOB
, 0x67);
1010 emit_modrm( p
, dst
, src
);
1013 void sse2_punpcklbw( struct x86_function
*p
,
1015 struct x86_reg src
)
1017 DUMP_RR( dst
, src
);
1018 emit_3ub(p
, 0x66, X86_TWOB
, 0x60);
1019 emit_modrm( p
, dst
, src
);
1023 void sse2_rcpps( struct x86_function
*p
,
1025 struct x86_reg src
)
1027 DUMP_RR( dst
, src
);
1028 emit_2ub(p
, X86_TWOB
, 0x53);
1029 emit_modrm( p
, dst
, src
);
1032 void sse2_rcpss( struct x86_function
*p
,
1034 struct x86_reg src
)
1036 DUMP_RR( dst
, src
);
1037 emit_3ub(p
, 0xF3, X86_TWOB
, 0x53);
1038 emit_modrm( p
, dst
, src
);
1041 void sse2_movd( struct x86_function
*p
,
1043 struct x86_reg src
)
1045 DUMP_RR( dst
, src
);
1046 emit_2ub(p
, 0x66, X86_TWOB
);
1047 emit_op_modrm( p
, 0x6e, 0x7e, dst
, src
);
1053 /***********************************************************************
1056 static void note_x87_pop( struct x86_function
*p
)
1059 assert(p
->x87_stack
>= 0);
1062 static void note_x87_push( struct x86_function
*p
)
1065 assert(p
->x87_stack
<= 7);
1068 void x87_assert_stack_empty( struct x86_function
*p
)
1070 assert (p
->x87_stack
== 0);
1074 void x87_fist( struct x86_function
*p
, struct x86_reg dst
)
1078 emit_modrm_noreg(p
, 2, dst
);
1081 void x87_fistp( struct x86_function
*p
, struct x86_reg dst
)
1085 emit_modrm_noreg(p
, 3, dst
);
1089 void x87_fild( struct x86_function
*p
, struct x86_reg arg
)
1093 emit_modrm_noreg(p
, 0, arg
);
1097 void x87_fldz( struct x86_function
*p
)
1100 emit_2ub(p
, 0xd9, 0xee);
1105 void x87_fldcw( struct x86_function
*p
, struct x86_reg arg
)
1108 assert(arg
.file
== file_REG32
);
1109 assert(arg
.mod
!= mod_REG
);
1111 emit_modrm_noreg(p
, 5, arg
);
1114 void x87_fld1( struct x86_function
*p
)
1117 emit_2ub(p
, 0xd9, 0xe8);
1121 void x87_fldl2e( struct x86_function
*p
)
1124 emit_2ub(p
, 0xd9, 0xea);
1128 void x87_fldln2( struct x86_function
*p
)
1131 emit_2ub(p
, 0xd9, 0xed);
1135 void x87_fwait( struct x86_function
*p
)
1141 void x87_fnclex( struct x86_function
*p
)
1144 emit_2ub(p
, 0xdb, 0xe2);
1147 void x87_fclex( struct x86_function
*p
)
1153 void x87_fcmovb( struct x86_function
*p
, struct x86_reg arg
)
1156 assert(arg
.file
== file_x87
);
1157 emit_2ub(p
, 0xda, 0xc0+arg
.idx
);
1160 void x87_fcmove( struct x86_function
*p
, struct x86_reg arg
)
1163 assert(arg
.file
== file_x87
);
1164 emit_2ub(p
, 0xda, 0xc8+arg
.idx
);
1167 void x87_fcmovbe( struct x86_function
*p
, struct x86_reg arg
)
1170 assert(arg
.file
== file_x87
);
1171 emit_2ub(p
, 0xda, 0xd0+arg
.idx
);
1174 void x87_fcmovnb( struct x86_function
*p
, struct x86_reg arg
)
1177 assert(arg
.file
== file_x87
);
1178 emit_2ub(p
, 0xdb, 0xc0+arg
.idx
);
1181 void x87_fcmovne( struct x86_function
*p
, struct x86_reg arg
)
1184 assert(arg
.file
== file_x87
);
1185 emit_2ub(p
, 0xdb, 0xc8+arg
.idx
);
1188 void x87_fcmovnbe( struct x86_function
*p
, struct x86_reg arg
)
1191 assert(arg
.file
== file_x87
);
1192 emit_2ub(p
, 0xdb, 0xd0+arg
.idx
);
1197 static void x87_arith_op( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
,
1198 unsigned char dst0ub0
,
1199 unsigned char dst0ub1
,
1200 unsigned char arg0ub0
,
1201 unsigned char arg0ub1
,
1202 unsigned char argmem_noreg
)
1204 assert(dst
.file
== file_x87
);
1206 if (arg
.file
== file_x87
) {
1208 emit_2ub(p
, dst0ub0
, dst0ub1
+arg
.idx
);
1209 else if (arg
.idx
== 0)
1210 emit_2ub(p
, arg0ub0
, arg0ub1
+arg
.idx
);
1214 else if (dst
.idx
== 0) {
1215 assert(arg
.file
== file_REG32
);
1217 emit_modrm_noreg(p
, argmem_noreg
, arg
);
1223 void x87_fmul( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1225 DUMP_RR( dst
, src
);
1226 x87_arith_op(p
, dst
, src
,
1232 void x87_fsub( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1234 DUMP_RR( dst
, src
);
1235 x87_arith_op(p
, dst
, src
,
1241 void x87_fsubr( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1243 DUMP_RR( dst
, src
);
1244 x87_arith_op(p
, dst
, src
,
1250 void x87_fadd( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1252 DUMP_RR( dst
, src
);
1253 x87_arith_op(p
, dst
, src
,
1259 void x87_fdiv( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1261 DUMP_RR( dst
, src
);
1262 x87_arith_op(p
, dst
, src
,
1268 void x87_fdivr( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1270 DUMP_RR( dst
, src
);
1271 x87_arith_op(p
, dst
, src
,
1277 void x87_fmulp( struct x86_function
*p
, struct x86_reg dst
)
1280 assert(dst
.file
== file_x87
);
1281 assert(dst
.idx
>= 1);
1282 emit_2ub(p
, 0xde, 0xc8+dst
.idx
);
1286 void x87_fsubp( struct x86_function
*p
, struct x86_reg dst
)
1289 assert(dst
.file
== file_x87
);
1290 assert(dst
.idx
>= 1);
1291 emit_2ub(p
, 0xde, 0xe8+dst
.idx
);
1295 void x87_fsubrp( struct x86_function
*p
, struct x86_reg dst
)
1298 assert(dst
.file
== file_x87
);
1299 assert(dst
.idx
>= 1);
1300 emit_2ub(p
, 0xde, 0xe0+dst
.idx
);
1304 void x87_faddp( struct x86_function
*p
, struct x86_reg dst
)
1307 assert(dst
.file
== file_x87
);
1308 assert(dst
.idx
>= 1);
1309 emit_2ub(p
, 0xde, 0xc0+dst
.idx
);
1313 void x87_fdivp( struct x86_function
*p
, struct x86_reg dst
)
1316 assert(dst
.file
== file_x87
);
1317 assert(dst
.idx
>= 1);
1318 emit_2ub(p
, 0xde, 0xf8+dst
.idx
);
1322 void x87_fdivrp( struct x86_function
*p
, struct x86_reg dst
)
1325 assert(dst
.file
== file_x87
);
1326 assert(dst
.idx
>= 1);
1327 emit_2ub(p
, 0xde, 0xf0+dst
.idx
);
1331 void x87_ftst( struct x86_function
*p
)
1334 emit_2ub(p
, 0xd9, 0xe4);
1337 void x87_fucom( struct x86_function
*p
, struct x86_reg arg
)
1340 assert(arg
.file
== file_x87
);
1341 emit_2ub(p
, 0xdd, 0xe0+arg
.idx
);
1344 void x87_fucomp( struct x86_function
*p
, struct x86_reg arg
)
1347 assert(arg
.file
== file_x87
);
1348 emit_2ub(p
, 0xdd, 0xe8+arg
.idx
);
1352 void x87_fucompp( struct x86_function
*p
)
1355 emit_2ub(p
, 0xda, 0xe9);
1356 note_x87_pop(p
); /* pop twice */
1357 note_x87_pop(p
); /* pop twice */
1360 void x87_fxch( struct x86_function
*p
, struct x86_reg arg
)
1363 assert(arg
.file
== file_x87
);
1364 emit_2ub(p
, 0xd9, 0xc8+arg
.idx
);
1367 void x87_fabs( struct x86_function
*p
)
1370 emit_2ub(p
, 0xd9, 0xe1);
1373 void x87_fchs( struct x86_function
*p
)
1376 emit_2ub(p
, 0xd9, 0xe0);
1379 void x87_fcos( struct x86_function
*p
)
1382 emit_2ub(p
, 0xd9, 0xff);
1386 void x87_fprndint( struct x86_function
*p
)
1389 emit_2ub(p
, 0xd9, 0xfc);
1392 void x87_fscale( struct x86_function
*p
)
1395 emit_2ub(p
, 0xd9, 0xfd);
1398 void x87_fsin( struct x86_function
*p
)
1401 emit_2ub(p
, 0xd9, 0xfe);
1404 void x87_fsincos( struct x86_function
*p
)
1407 emit_2ub(p
, 0xd9, 0xfb);
1410 void x87_fsqrt( struct x86_function
*p
)
1413 emit_2ub(p
, 0xd9, 0xfa);
1416 void x87_fxtract( struct x86_function
*p
)
1419 emit_2ub(p
, 0xd9, 0xf4);
1424 * Restrictions: -1.0 <= st0 <= 1.0
1426 void x87_f2xm1( struct x86_function
*p
)
1429 emit_2ub(p
, 0xd9, 0xf0);
1432 /* st1 = st1 * log2(st0);
1435 void x87_fyl2x( struct x86_function
*p
)
1438 emit_2ub(p
, 0xd9, 0xf1);
1442 /* st1 = st1 * log2(st0 + 1.0);
1445 * A fast operation, with restrictions: -.29 < st0 < .29
1447 void x87_fyl2xp1( struct x86_function
*p
)
1450 emit_2ub(p
, 0xd9, 0xf9);
1455 void x87_fld( struct x86_function
*p
, struct x86_reg arg
)
1458 if (arg
.file
== file_x87
)
1459 emit_2ub(p
, 0xd9, 0xc0 + arg
.idx
);
1462 emit_modrm_noreg(p
, 0, arg
);
1467 void x87_fst( struct x86_function
*p
, struct x86_reg dst
)
1470 if (dst
.file
== file_x87
)
1471 emit_2ub(p
, 0xdd, 0xd0 + dst
.idx
);
1474 emit_modrm_noreg(p
, 2, dst
);
1478 void x87_fstp( struct x86_function
*p
, struct x86_reg dst
)
1481 if (dst
.file
== file_x87
)
1482 emit_2ub(p
, 0xdd, 0xd8 + dst
.idx
);
1485 emit_modrm_noreg(p
, 3, dst
);
1490 void x87_fpop( struct x86_function
*p
)
1492 x87_fstp( p
, x86_make_reg( file_x87
, 0 ));
1496 void x87_fcom( struct x86_function
*p
, struct x86_reg dst
)
1499 if (dst
.file
== file_x87
)
1500 emit_2ub(p
, 0xd8, 0xd0 + dst
.idx
);
1503 emit_modrm_noreg(p
, 2, dst
);
1508 void x87_fcomp( struct x86_function
*p
, struct x86_reg dst
)
1511 if (dst
.file
== file_x87
)
1512 emit_2ub(p
, 0xd8, 0xd8 + dst
.idx
);
1515 emit_modrm_noreg(p
, 3, dst
);
1520 void x87_fcomi( struct x86_function
*p
, struct x86_reg arg
)
1523 emit_2ub(p
, 0xdb, 0xf0+arg
.idx
);
1526 void x87_fcomip( struct x86_function
*p
, struct x86_reg arg
)
1529 emit_2ub(p
, 0xdb, 0xf0+arg
.idx
);
1534 void x87_fnstsw( struct x86_function
*p
, struct x86_reg dst
)
1537 assert(dst
.file
== file_REG32
);
1539 if (dst
.idx
== reg_AX
&&
1541 emit_2ub(p
, 0xdf, 0xe0);
1544 emit_modrm_noreg(p
, 7, dst
);
1549 void x87_fnstcw( struct x86_function
*p
, struct x86_reg dst
)
1552 assert(dst
.file
== file_REG32
);
1554 emit_1ub(p
, 0x9b); /* WAIT -- needed? */
1556 emit_modrm_noreg(p
, 7, dst
);
1562 /***********************************************************************
1566 void mmx_emms( struct x86_function
*p
)
1569 assert(p
->need_emms
);
1570 emit_2ub(p
, 0x0f, 0x77);
1574 void mmx_packssdw( struct x86_function
*p
,
1576 struct x86_reg src
)
1578 DUMP_RR( dst
, src
);
1579 assert(dst
.file
== file_MMX
&&
1580 (src
.file
== file_MMX
|| src
.mod
!= mod_REG
));
1584 emit_2ub(p
, X86_TWOB
, 0x6b);
1585 emit_modrm( p
, dst
, src
);
1588 void mmx_packuswb( struct x86_function
*p
,
1590 struct x86_reg src
)
1592 DUMP_RR( dst
, src
);
1593 assert(dst
.file
== file_MMX
&&
1594 (src
.file
== file_MMX
|| src
.mod
!= mod_REG
));
1598 emit_2ub(p
, X86_TWOB
, 0x67);
1599 emit_modrm( p
, dst
, src
);
1602 void mmx_movd( struct x86_function
*p
,
1604 struct x86_reg src
)
1606 DUMP_RR( dst
, src
);
1608 emit_1ub(p
, X86_TWOB
);
1609 emit_op_modrm( p
, 0x6e, 0x7e, dst
, src
);
1612 void mmx_movq( struct x86_function
*p
,
1614 struct x86_reg src
)
1616 DUMP_RR( dst
, src
);
1618 emit_1ub(p
, X86_TWOB
);
1619 emit_op_modrm( p
, 0x6f, 0x7f, dst
, src
);
1623 /***********************************************************************
1628 void x86_cdecl_caller_push_regs( struct x86_function
*p
)
1630 x86_push(p
, x86_make_reg(file_REG32
, reg_AX
));
1631 x86_push(p
, x86_make_reg(file_REG32
, reg_CX
));
1632 x86_push(p
, x86_make_reg(file_REG32
, reg_DX
));
1635 void x86_cdecl_caller_pop_regs( struct x86_function
*p
)
1637 x86_pop(p
, x86_make_reg(file_REG32
, reg_DX
));
1638 x86_pop(p
, x86_make_reg(file_REG32
, reg_CX
));
1639 x86_pop(p
, x86_make_reg(file_REG32
, reg_AX
));
1643 /* Retreive a reference to one of the function arguments, taking into
1644 * account any push/pop activity:
1646 struct x86_reg
x86_fn_arg( struct x86_function
*p
,
1649 return x86_make_disp(x86_make_reg(file_REG32
, reg_SP
),
1650 p
->stack_offset
+ arg
* 4); /* ??? */
1654 void x86_init_func( struct x86_function
*p
)
1662 void x86_init_func_size( struct x86_function
*p
, unsigned code_size
)
1664 p
->size
= code_size
;
1665 p
->store
= rtasm_exec_malloc(code_size
);
1666 if (p
->store
== NULL
) {
1667 p
->store
= p
->error_overflow
;
1673 void x86_release_func( struct x86_function
*p
)
1675 if (p
->store
&& p
->store
!= p
->error_overflow
)
1676 rtasm_exec_free(p
->store
);
1684 void (*x86_get_func( struct x86_function
*p
))(void)
1687 if (DISASSEM
&& p
->store
)
1688 debug_printf("disassemble %p %p\n", p
->store
, p
->csr
);
1690 if (p
->store
== p
->error_overflow
)
1691 return (void (*)(void)) NULL
;
1693 return (void (*)(void)) p
->store
;
1698 void x86sse_dummy( void )