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
);
376 if (offset
<= 127 && offset
>= -128) {
377 emit_1ub(p
, 0x70 + cc
);
378 emit_1b(p
, (char) offset
);
381 offset
= label
- (x86_get_label(p
) + 6);
382 emit_2ub(p
, 0x0f, 0x80 + cc
);
387 /* Always use a 32bit offset for forward jumps:
389 int x86_jcc_forward( struct x86_function
*p
,
393 emit_2ub(p
, 0x0f, 0x80 + cc
);
395 return x86_get_label(p
);
398 int x86_jmp_forward( struct x86_function
*p
)
403 return x86_get_label(p
);
406 int x86_call_forward( struct x86_function
*p
)
412 return x86_get_label(p
);
415 /* Fixup offset from forward jump:
417 void x86_fixup_fwd_jump( struct x86_function
*p
,
420 *(int *)(p
->store
+ fixup
- 4) = x86_get_label(p
) - fixup
;
423 void x86_jmp( struct x86_function
*p
, int label
)
427 emit_1i(p
, label
- x86_get_label(p
) - 4);
430 void x86_call( struct x86_function
*p
, struct x86_reg reg
)
434 emit_modrm_noreg(p
, 2, reg
);
439 * Temporary. As I need immediate operands, and dont want to mess with the codegen,
440 * I load the immediate into general purpose register and use it.
442 void x86_mov_reg_imm( struct x86_function
*p
, struct x86_reg dst
, int imm
)
445 assert(dst
.mod
== mod_REG
);
446 emit_1ub(p
, 0xb8 + dst
.idx
);
450 void x86_add_reg_imm8( struct x86_function
*p
, struct x86_reg dst
, ubyte imm
)
453 assert(dst
.mod
== mod_REG
);
455 emit_modrm_noreg(p
, 0, dst
);
460 void x86_push( struct x86_function
*p
,
464 if (reg
.mod
== mod_REG
)
465 emit_1ub(p
, 0x50 + reg
.idx
);
469 emit_modrm_noreg(p
, 6, reg
);
473 p
->stack_offset
+= 4;
476 void x86_push_imm32( struct x86_function
*p
,
483 p
->stack_offset
+= 4;
487 void x86_pop( struct x86_function
*p
,
491 assert(reg
.mod
== mod_REG
);
492 emit_1ub(p
, 0x58 + reg
.idx
);
493 p
->stack_offset
-= 4;
496 void x86_inc( struct x86_function
*p
,
500 assert(reg
.mod
== mod_REG
);
501 emit_1ub(p
, 0x40 + reg
.idx
);
504 void x86_dec( struct x86_function
*p
,
508 assert(reg
.mod
== mod_REG
);
509 emit_1ub(p
, 0x48 + reg
.idx
);
512 void x86_ret( struct x86_function
*p
)
515 assert(p
->stack_offset
== 0);
519 void x86_retw( struct x86_function
*p
, unsigned short imm
)
522 emit_3ub(p
, 0xc2, imm
& 0xff, (imm
>> 8) & 0xff);
525 void x86_sahf( struct x86_function
*p
)
531 void x86_mov( struct x86_function
*p
,
536 emit_op_modrm( p
, 0x8b, 0x89, dst
, src
);
539 void x86_xor( struct x86_function
*p
,
544 emit_op_modrm( p
, 0x33, 0x31, dst
, src
);
547 void x86_cmp( struct x86_function
*p
,
552 emit_op_modrm( p
, 0x3b, 0x39, dst
, src
);
555 void x86_lea( struct x86_function
*p
,
561 emit_modrm( p
, dst
, src
);
564 void x86_test( struct x86_function
*p
,
570 emit_modrm( p
, dst
, src
);
573 void x86_add( struct x86_function
*p
,
578 emit_op_modrm(p
, 0x03, 0x01, dst
, src
);
581 /* Calculate EAX * src, results in EDX:EAX.
583 void x86_mul( struct x86_function
*p
,
588 emit_modrm_noreg(p
, 4, src
);
592 void x86_imul( struct x86_function
*p
,
597 emit_2ub(p
, X86_TWOB
, 0xAF);
598 emit_modrm(p
, dst
, src
);
602 void x86_sub( struct x86_function
*p
,
607 emit_op_modrm(p
, 0x2b, 0x29, dst
, src
);
610 void x86_or( struct x86_function
*p
,
615 emit_op_modrm( p
, 0x0b, 0x09, dst
, src
);
618 void x86_and( struct x86_function
*p
,
623 emit_op_modrm( p
, 0x23, 0x21, dst
, src
);
628 /***********************************************************************
633 void sse_movss( struct x86_function
*p
,
638 emit_2ub(p
, 0xF3, X86_TWOB
);
639 emit_op_modrm( p
, 0x10, 0x11, dst
, src
);
642 void sse_movaps( struct x86_function
*p
,
647 emit_1ub(p
, X86_TWOB
);
648 emit_op_modrm( p
, 0x28, 0x29, dst
, src
);
651 void sse_movups( struct x86_function
*p
,
656 emit_1ub(p
, X86_TWOB
);
657 emit_op_modrm( p
, 0x10, 0x11, dst
, src
);
660 void sse_movhps( struct x86_function
*p
,
665 assert(dst
.mod
!= mod_REG
|| src
.mod
!= mod_REG
);
666 emit_1ub(p
, X86_TWOB
);
667 emit_op_modrm( p
, 0x16, 0x17, dst
, src
); /* cf movlhps */
670 void sse_movlps( struct x86_function
*p
,
675 assert(dst
.mod
!= mod_REG
|| src
.mod
!= mod_REG
);
676 emit_1ub(p
, X86_TWOB
);
677 emit_op_modrm( p
, 0x12, 0x13, dst
, src
); /* cf movhlps */
680 void sse_maxps( struct x86_function
*p
,
685 emit_2ub(p
, X86_TWOB
, 0x5F);
686 emit_modrm( p
, dst
, src
);
689 void sse_maxss( struct x86_function
*p
,
694 emit_3ub(p
, 0xF3, X86_TWOB
, 0x5F);
695 emit_modrm( p
, dst
, src
);
698 void sse_divss( struct x86_function
*p
,
703 emit_3ub(p
, 0xF3, X86_TWOB
, 0x5E);
704 emit_modrm( p
, dst
, src
);
707 void sse_minps( struct x86_function
*p
,
712 emit_2ub(p
, X86_TWOB
, 0x5D);
713 emit_modrm( p
, dst
, src
);
716 void sse_subps( struct x86_function
*p
,
721 emit_2ub(p
, X86_TWOB
, 0x5C);
722 emit_modrm( p
, dst
, src
);
725 void sse_mulps( struct x86_function
*p
,
730 emit_2ub(p
, X86_TWOB
, 0x59);
731 emit_modrm( p
, dst
, src
);
734 void sse_mulss( struct x86_function
*p
,
739 emit_3ub(p
, 0xF3, X86_TWOB
, 0x59);
740 emit_modrm( p
, dst
, src
);
743 void sse_addps( struct x86_function
*p
,
748 emit_2ub(p
, X86_TWOB
, 0x58);
749 emit_modrm( p
, dst
, src
);
752 void sse_addss( struct x86_function
*p
,
757 emit_3ub(p
, 0xF3, X86_TWOB
, 0x58);
758 emit_modrm( p
, dst
, src
);
761 void sse_andnps( struct x86_function
*p
,
766 emit_2ub(p
, X86_TWOB
, 0x55);
767 emit_modrm( p
, dst
, src
);
770 void sse_andps( struct x86_function
*p
,
775 emit_2ub(p
, X86_TWOB
, 0x54);
776 emit_modrm( p
, dst
, src
);
779 void sse_rsqrtps( struct x86_function
*p
,
784 emit_2ub(p
, X86_TWOB
, 0x52);
785 emit_modrm( p
, dst
, src
);
788 void sse_rsqrtss( struct x86_function
*p
,
793 emit_3ub(p
, 0xF3, X86_TWOB
, 0x52);
794 emit_modrm( p
, dst
, src
);
798 void sse_movhlps( struct x86_function
*p
,
803 assert(dst
.mod
== mod_REG
&& src
.mod
== mod_REG
);
804 emit_2ub(p
, X86_TWOB
, 0x12);
805 emit_modrm( p
, dst
, src
);
808 void sse_movlhps( struct x86_function
*p
,
813 assert(dst
.mod
== mod_REG
&& src
.mod
== mod_REG
);
814 emit_2ub(p
, X86_TWOB
, 0x16);
815 emit_modrm( p
, dst
, src
);
818 void sse_orps( struct x86_function
*p
,
823 emit_2ub(p
, X86_TWOB
, 0x56);
824 emit_modrm( p
, dst
, src
);
827 void sse_xorps( struct x86_function
*p
,
832 emit_2ub(p
, X86_TWOB
, 0x57);
833 emit_modrm( p
, dst
, src
);
836 void sse_cvtps2pi( struct x86_function
*p
,
841 assert(dst
.file
== file_MMX
&&
842 (src
.file
== file_XMM
|| src
.mod
!= mod_REG
));
846 emit_2ub(p
, X86_TWOB
, 0x2d);
847 emit_modrm( p
, dst
, src
);
850 void sse2_cvtdq2ps( struct x86_function
*p
,
855 emit_2ub(p
, X86_TWOB
, 0x5b);
856 emit_modrm( p
, dst
, src
);
860 /* Shufps can also be used to implement a reduced swizzle when dest ==
863 void sse_shufps( struct x86_function
*p
,
868 DUMP_RRI( dst
, src
, shuf
);
869 emit_2ub(p
, X86_TWOB
, 0xC6);
870 emit_modrm(p
, dst
, src
);
874 void sse_unpckhps( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
877 emit_2ub( p
, X86_TWOB
, 0x15 );
878 emit_modrm( p
, dst
, src
);
881 void sse_unpcklps( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
884 emit_2ub( p
, X86_TWOB
, 0x14 );
885 emit_modrm( p
, dst
, src
);
888 void sse_cmpps( struct x86_function
*p
,
893 DUMP_RRI( dst
, src
, cc
);
894 emit_2ub(p
, X86_TWOB
, 0xC2);
895 emit_modrm(p
, dst
, src
);
899 void sse_pmovmskb( struct x86_function
*p
,
904 emit_3ub(p
, 0x66, X86_TWOB
, 0xD7);
905 emit_modrm(p
, dst
, src
);
908 /***********************************************************************
913 * Perform a reduced swizzle:
915 void sse2_pshufd( struct x86_function
*p
,
920 DUMP_RRI( dst
, src
, shuf
);
921 emit_3ub(p
, 0x66, X86_TWOB
, 0x70);
922 emit_modrm(p
, dst
, src
);
926 void sse2_cvttps2dq( struct x86_function
*p
,
931 emit_3ub( p
, 0xF3, X86_TWOB
, 0x5B );
932 emit_modrm( p
, dst
, src
);
935 void sse2_cvtps2dq( struct x86_function
*p
,
940 emit_3ub(p
, 0x66, X86_TWOB
, 0x5B);
941 emit_modrm( p
, dst
, src
);
944 void sse2_packssdw( struct x86_function
*p
,
949 emit_3ub(p
, 0x66, X86_TWOB
, 0x6B);
950 emit_modrm( p
, dst
, src
);
953 void sse2_packsswb( struct x86_function
*p
,
958 emit_3ub(p
, 0x66, X86_TWOB
, 0x63);
959 emit_modrm( p
, dst
, src
);
962 void sse2_packuswb( struct x86_function
*p
,
967 emit_3ub(p
, 0x66, X86_TWOB
, 0x67);
968 emit_modrm( p
, dst
, src
);
971 void sse2_punpcklbw( struct x86_function
*p
,
976 emit_3ub(p
, 0x66, X86_TWOB
, 0x60);
977 emit_modrm( p
, dst
, src
);
981 void sse2_rcpps( struct x86_function
*p
,
986 emit_2ub(p
, X86_TWOB
, 0x53);
987 emit_modrm( p
, dst
, src
);
990 void sse2_rcpss( struct x86_function
*p
,
995 emit_3ub(p
, 0xF3, X86_TWOB
, 0x53);
996 emit_modrm( p
, dst
, src
);
999 void sse2_movd( struct x86_function
*p
,
1001 struct x86_reg src
)
1003 DUMP_RR( dst
, src
);
1004 emit_2ub(p
, 0x66, X86_TWOB
);
1005 emit_op_modrm( p
, 0x6e, 0x7e, dst
, src
);
1011 /***********************************************************************
1014 static void note_x87_pop( struct x86_function
*p
)
1017 assert(p
->x87_stack
>= 0);
1020 static void note_x87_push( struct x86_function
*p
)
1023 assert(p
->x87_stack
<= 7);
1026 void x87_assert_stack_empty( struct x86_function
*p
)
1028 assert (p
->x87_stack
== 0);
1032 void x87_fist( struct x86_function
*p
, struct x86_reg dst
)
1036 emit_modrm_noreg(p
, 2, dst
);
1039 void x87_fistp( struct x86_function
*p
, struct x86_reg dst
)
1043 emit_modrm_noreg(p
, 3, dst
);
1047 void x87_fild( struct x86_function
*p
, struct x86_reg arg
)
1051 emit_modrm_noreg(p
, 0, arg
);
1055 void x87_fldz( struct x86_function
*p
)
1058 emit_2ub(p
, 0xd9, 0xee);
1063 void x87_fldcw( struct x86_function
*p
, struct x86_reg arg
)
1066 assert(arg
.file
== file_REG32
);
1067 assert(arg
.mod
!= mod_REG
);
1069 emit_modrm_noreg(p
, 5, arg
);
1072 void x87_fld1( struct x86_function
*p
)
1075 emit_2ub(p
, 0xd9, 0xe8);
1079 void x87_fldl2e( struct x86_function
*p
)
1082 emit_2ub(p
, 0xd9, 0xea);
1086 void x87_fldln2( struct x86_function
*p
)
1089 emit_2ub(p
, 0xd9, 0xed);
1093 void x87_fwait( struct x86_function
*p
)
1099 void x87_fnclex( struct x86_function
*p
)
1102 emit_2ub(p
, 0xdb, 0xe2);
1105 void x87_fclex( struct x86_function
*p
)
1111 void x87_fcmovb( struct x86_function
*p
, struct x86_reg arg
)
1114 assert(arg
.file
== file_x87
);
1115 emit_2ub(p
, 0xda, 0xc0+arg
.idx
);
1118 void x87_fcmove( struct x86_function
*p
, struct x86_reg arg
)
1121 assert(arg
.file
== file_x87
);
1122 emit_2ub(p
, 0xda, 0xc8+arg
.idx
);
1125 void x87_fcmovbe( struct x86_function
*p
, struct x86_reg arg
)
1128 assert(arg
.file
== file_x87
);
1129 emit_2ub(p
, 0xda, 0xd0+arg
.idx
);
1132 void x87_fcmovnb( struct x86_function
*p
, struct x86_reg arg
)
1135 assert(arg
.file
== file_x87
);
1136 emit_2ub(p
, 0xdb, 0xc0+arg
.idx
);
1139 void x87_fcmovne( struct x86_function
*p
, struct x86_reg arg
)
1142 assert(arg
.file
== file_x87
);
1143 emit_2ub(p
, 0xdb, 0xc8+arg
.idx
);
1146 void x87_fcmovnbe( struct x86_function
*p
, struct x86_reg arg
)
1149 assert(arg
.file
== file_x87
);
1150 emit_2ub(p
, 0xdb, 0xd0+arg
.idx
);
1155 static void x87_arith_op( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
,
1156 unsigned char dst0ub0
,
1157 unsigned char dst0ub1
,
1158 unsigned char arg0ub0
,
1159 unsigned char arg0ub1
,
1160 unsigned char argmem_noreg
)
1162 assert(dst
.file
== file_x87
);
1164 if (arg
.file
== file_x87
) {
1166 emit_2ub(p
, dst0ub0
, dst0ub1
+arg
.idx
);
1167 else if (arg
.idx
== 0)
1168 emit_2ub(p
, arg0ub0
, arg0ub1
+arg
.idx
);
1172 else if (dst
.idx
== 0) {
1173 assert(arg
.file
== file_REG32
);
1175 emit_modrm_noreg(p
, argmem_noreg
, arg
);
1181 void x87_fmul( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1183 DUMP_RR( dst
, src
);
1184 x87_arith_op(p
, dst
, src
,
1190 void x87_fsub( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1192 DUMP_RR( dst
, src
);
1193 x87_arith_op(p
, dst
, src
,
1199 void x87_fsubr( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1201 DUMP_RR( dst
, src
);
1202 x87_arith_op(p
, dst
, src
,
1208 void x87_fadd( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1210 DUMP_RR( dst
, src
);
1211 x87_arith_op(p
, dst
, src
,
1217 void x87_fdiv( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1219 DUMP_RR( dst
, src
);
1220 x87_arith_op(p
, dst
, src
,
1226 void x87_fdivr( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1228 DUMP_RR( dst
, src
);
1229 x87_arith_op(p
, dst
, src
,
1235 void x87_fmulp( struct x86_function
*p
, struct x86_reg dst
)
1238 assert(dst
.file
== file_x87
);
1239 assert(dst
.idx
>= 1);
1240 emit_2ub(p
, 0xde, 0xc8+dst
.idx
);
1244 void x87_fsubp( struct x86_function
*p
, struct x86_reg dst
)
1247 assert(dst
.file
== file_x87
);
1248 assert(dst
.idx
>= 1);
1249 emit_2ub(p
, 0xde, 0xe8+dst
.idx
);
1253 void x87_fsubrp( struct x86_function
*p
, struct x86_reg dst
)
1256 assert(dst
.file
== file_x87
);
1257 assert(dst
.idx
>= 1);
1258 emit_2ub(p
, 0xde, 0xe0+dst
.idx
);
1262 void x87_faddp( struct x86_function
*p
, struct x86_reg dst
)
1265 assert(dst
.file
== file_x87
);
1266 assert(dst
.idx
>= 1);
1267 emit_2ub(p
, 0xde, 0xc0+dst
.idx
);
1271 void x87_fdivp( struct x86_function
*p
, struct x86_reg dst
)
1274 assert(dst
.file
== file_x87
);
1275 assert(dst
.idx
>= 1);
1276 emit_2ub(p
, 0xde, 0xf8+dst
.idx
);
1280 void x87_fdivrp( struct x86_function
*p
, struct x86_reg dst
)
1283 assert(dst
.file
== file_x87
);
1284 assert(dst
.idx
>= 1);
1285 emit_2ub(p
, 0xde, 0xf0+dst
.idx
);
1289 void x87_ftst( struct x86_function
*p
)
1292 emit_2ub(p
, 0xd9, 0xe4);
1295 void x87_fucom( struct x86_function
*p
, struct x86_reg arg
)
1298 assert(arg
.file
== file_x87
);
1299 emit_2ub(p
, 0xdd, 0xe0+arg
.idx
);
1302 void x87_fucomp( struct x86_function
*p
, struct x86_reg arg
)
1305 assert(arg
.file
== file_x87
);
1306 emit_2ub(p
, 0xdd, 0xe8+arg
.idx
);
1310 void x87_fucompp( struct x86_function
*p
)
1313 emit_2ub(p
, 0xda, 0xe9);
1314 note_x87_pop(p
); /* pop twice */
1315 note_x87_pop(p
); /* pop twice */
1318 void x87_fxch( struct x86_function
*p
, struct x86_reg arg
)
1321 assert(arg
.file
== file_x87
);
1322 emit_2ub(p
, 0xd9, 0xc8+arg
.idx
);
1325 void x87_fabs( struct x86_function
*p
)
1328 emit_2ub(p
, 0xd9, 0xe1);
1331 void x87_fchs( struct x86_function
*p
)
1334 emit_2ub(p
, 0xd9, 0xe0);
1337 void x87_fcos( struct x86_function
*p
)
1340 emit_2ub(p
, 0xd9, 0xff);
1344 void x87_fprndint( struct x86_function
*p
)
1347 emit_2ub(p
, 0xd9, 0xfc);
1350 void x87_fscale( struct x86_function
*p
)
1353 emit_2ub(p
, 0xd9, 0xfd);
1356 void x87_fsin( struct x86_function
*p
)
1359 emit_2ub(p
, 0xd9, 0xfe);
1362 void x87_fsincos( struct x86_function
*p
)
1365 emit_2ub(p
, 0xd9, 0xfb);
1368 void x87_fsqrt( struct x86_function
*p
)
1371 emit_2ub(p
, 0xd9, 0xfa);
1374 void x87_fxtract( struct x86_function
*p
)
1377 emit_2ub(p
, 0xd9, 0xf4);
1382 * Restrictions: -1.0 <= st0 <= 1.0
1384 void x87_f2xm1( struct x86_function
*p
)
1387 emit_2ub(p
, 0xd9, 0xf0);
1390 /* st1 = st1 * log2(st0);
1393 void x87_fyl2x( struct x86_function
*p
)
1396 emit_2ub(p
, 0xd9, 0xf1);
1400 /* st1 = st1 * log2(st0 + 1.0);
1403 * A fast operation, with restrictions: -.29 < st0 < .29
1405 void x87_fyl2xp1( struct x86_function
*p
)
1408 emit_2ub(p
, 0xd9, 0xf9);
1413 void x87_fld( struct x86_function
*p
, struct x86_reg arg
)
1416 if (arg
.file
== file_x87
)
1417 emit_2ub(p
, 0xd9, 0xc0 + arg
.idx
);
1420 emit_modrm_noreg(p
, 0, arg
);
1425 void x87_fst( struct x86_function
*p
, struct x86_reg dst
)
1428 if (dst
.file
== file_x87
)
1429 emit_2ub(p
, 0xdd, 0xd0 + dst
.idx
);
1432 emit_modrm_noreg(p
, 2, dst
);
1436 void x87_fstp( struct x86_function
*p
, struct x86_reg dst
)
1439 if (dst
.file
== file_x87
)
1440 emit_2ub(p
, 0xdd, 0xd8 + dst
.idx
);
1443 emit_modrm_noreg(p
, 3, dst
);
1448 void x87_fpop( struct x86_function
*p
)
1450 x87_fstp( p
, x86_make_reg( file_x87
, 0 ));
1454 void x87_fcom( struct x86_function
*p
, struct x86_reg dst
)
1457 if (dst
.file
== file_x87
)
1458 emit_2ub(p
, 0xd8, 0xd0 + dst
.idx
);
1461 emit_modrm_noreg(p
, 2, dst
);
1466 void x87_fcomp( struct x86_function
*p
, struct x86_reg dst
)
1469 if (dst
.file
== file_x87
)
1470 emit_2ub(p
, 0xd8, 0xd8 + dst
.idx
);
1473 emit_modrm_noreg(p
, 3, dst
);
1478 void x87_fcomi( struct x86_function
*p
, struct x86_reg arg
)
1481 emit_2ub(p
, 0xdb, 0xf0+arg
.idx
);
1484 void x87_fcomip( struct x86_function
*p
, struct x86_reg arg
)
1487 emit_2ub(p
, 0xdb, 0xf0+arg
.idx
);
1492 void x87_fnstsw( struct x86_function
*p
, struct x86_reg dst
)
1495 assert(dst
.file
== file_REG32
);
1497 if (dst
.idx
== reg_AX
&&
1499 emit_2ub(p
, 0xdf, 0xe0);
1502 emit_modrm_noreg(p
, 7, dst
);
1507 void x87_fnstcw( struct x86_function
*p
, struct x86_reg dst
)
1510 assert(dst
.file
== file_REG32
);
1512 emit_1ub(p
, 0x9b); /* WAIT -- needed? */
1514 emit_modrm_noreg(p
, 7, dst
);
1520 /***********************************************************************
1524 void mmx_emms( struct x86_function
*p
)
1527 assert(p
->need_emms
);
1528 emit_2ub(p
, 0x0f, 0x77);
1532 void mmx_packssdw( struct x86_function
*p
,
1534 struct x86_reg src
)
1536 DUMP_RR( dst
, src
);
1537 assert(dst
.file
== file_MMX
&&
1538 (src
.file
== file_MMX
|| src
.mod
!= mod_REG
));
1542 emit_2ub(p
, X86_TWOB
, 0x6b);
1543 emit_modrm( p
, dst
, src
);
1546 void mmx_packuswb( struct x86_function
*p
,
1548 struct x86_reg src
)
1550 DUMP_RR( dst
, src
);
1551 assert(dst
.file
== file_MMX
&&
1552 (src
.file
== file_MMX
|| src
.mod
!= mod_REG
));
1556 emit_2ub(p
, X86_TWOB
, 0x67);
1557 emit_modrm( p
, dst
, src
);
1560 void mmx_movd( struct x86_function
*p
,
1562 struct x86_reg src
)
1564 DUMP_RR( dst
, src
);
1566 emit_1ub(p
, X86_TWOB
);
1567 emit_op_modrm( p
, 0x6e, 0x7e, dst
, src
);
1570 void mmx_movq( struct x86_function
*p
,
1572 struct x86_reg src
)
1574 DUMP_RR( dst
, src
);
1576 emit_1ub(p
, X86_TWOB
);
1577 emit_op_modrm( p
, 0x6f, 0x7f, dst
, src
);
1581 /***********************************************************************
1586 void x86_cdecl_caller_push_regs( struct x86_function
*p
)
1588 x86_push(p
, x86_make_reg(file_REG32
, reg_AX
));
1589 x86_push(p
, x86_make_reg(file_REG32
, reg_CX
));
1590 x86_push(p
, x86_make_reg(file_REG32
, reg_DX
));
1593 void x86_cdecl_caller_pop_regs( struct x86_function
*p
)
1595 x86_pop(p
, x86_make_reg(file_REG32
, reg_DX
));
1596 x86_pop(p
, x86_make_reg(file_REG32
, reg_CX
));
1597 x86_pop(p
, x86_make_reg(file_REG32
, reg_AX
));
1601 /* Retreive a reference to one of the function arguments, taking into
1602 * account any push/pop activity:
1604 struct x86_reg
x86_fn_arg( struct x86_function
*p
,
1607 return x86_make_disp(x86_make_reg(file_REG32
, reg_SP
),
1608 p
->stack_offset
+ arg
* 4); /* ??? */
1612 void x86_init_func( struct x86_function
*p
)
1620 void x86_init_func_size( struct x86_function
*p
, unsigned code_size
)
1622 p
->size
= code_size
;
1623 p
->store
= rtasm_exec_malloc(code_size
);
1624 if (p
->store
== NULL
) {
1625 p
->store
= p
->error_overflow
;
1631 void x86_release_func( struct x86_function
*p
)
1633 if (p
->store
&& p
->store
!= p
->error_overflow
)
1634 rtasm_exec_free(p
->store
);
1642 void (*x86_get_func( struct x86_function
*p
))(void)
1645 if (DISASSEM
&& p
->store
)
1646 debug_printf("disassemble %p %p\n", p
->store
, p
->csr
);
1648 if (p
->store
== p
->error_overflow
)
1649 return (void (*)(void)) NULL
;
1651 return (void (*)(void)) p
->store
;
1656 void x86sse_dummy( void )