9085f4cc0e722661256907ff8d034159bb597b24
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 /***********************************************************************
632 void sse_prefetchnta( struct x86_function
*p
, struct x86_reg ptr
)
635 assert(ptr
.mod
!= mod_REG
);
636 emit_2ub(p
, 0x0f, 0x18);
637 emit_modrm_noreg(p
, 0, ptr
);
640 void sse_prefetch0( struct x86_function
*p
, struct x86_reg ptr
)
643 assert(ptr
.mod
!= mod_REG
);
644 emit_2ub(p
, 0x0f, 0x18);
645 emit_modrm_noreg(p
, 1, ptr
);
648 void sse_prefetch1( struct x86_function
*p
, struct x86_reg ptr
)
651 assert(ptr
.mod
!= mod_REG
);
652 emit_2ub(p
, 0x0f, 0x18);
653 emit_modrm_noreg(p
, 2, ptr
);
659 void sse_movss( struct x86_function
*p
,
664 emit_2ub(p
, 0xF3, X86_TWOB
);
665 emit_op_modrm( p
, 0x10, 0x11, dst
, src
);
668 void sse_movaps( struct x86_function
*p
,
673 emit_1ub(p
, X86_TWOB
);
674 emit_op_modrm( p
, 0x28, 0x29, dst
, src
);
677 void sse_movups( struct x86_function
*p
,
682 emit_1ub(p
, X86_TWOB
);
683 emit_op_modrm( p
, 0x10, 0x11, dst
, src
);
686 void sse_movhps( struct x86_function
*p
,
691 assert(dst
.mod
!= mod_REG
|| src
.mod
!= mod_REG
);
692 emit_1ub(p
, X86_TWOB
);
693 emit_op_modrm( p
, 0x16, 0x17, dst
, src
); /* cf movlhps */
696 void sse_movlps( struct x86_function
*p
,
701 assert(dst
.mod
!= mod_REG
|| src
.mod
!= mod_REG
);
702 emit_1ub(p
, X86_TWOB
);
703 emit_op_modrm( p
, 0x12, 0x13, dst
, src
); /* cf movhlps */
706 void sse_maxps( struct x86_function
*p
,
711 emit_2ub(p
, X86_TWOB
, 0x5F);
712 emit_modrm( p
, dst
, src
);
715 void sse_maxss( struct x86_function
*p
,
720 emit_3ub(p
, 0xF3, X86_TWOB
, 0x5F);
721 emit_modrm( p
, dst
, src
);
724 void sse_divss( struct x86_function
*p
,
729 emit_3ub(p
, 0xF3, X86_TWOB
, 0x5E);
730 emit_modrm( p
, dst
, src
);
733 void sse_minps( struct x86_function
*p
,
738 emit_2ub(p
, X86_TWOB
, 0x5D);
739 emit_modrm( p
, dst
, src
);
742 void sse_subps( struct x86_function
*p
,
747 emit_2ub(p
, X86_TWOB
, 0x5C);
748 emit_modrm( p
, dst
, src
);
751 void sse_mulps( struct x86_function
*p
,
756 emit_2ub(p
, X86_TWOB
, 0x59);
757 emit_modrm( p
, dst
, src
);
760 void sse_mulss( struct x86_function
*p
,
765 emit_3ub(p
, 0xF3, X86_TWOB
, 0x59);
766 emit_modrm( p
, dst
, src
);
769 void sse_addps( struct x86_function
*p
,
774 emit_2ub(p
, X86_TWOB
, 0x58);
775 emit_modrm( p
, dst
, src
);
778 void sse_addss( struct x86_function
*p
,
783 emit_3ub(p
, 0xF3, X86_TWOB
, 0x58);
784 emit_modrm( p
, dst
, src
);
787 void sse_andnps( struct x86_function
*p
,
792 emit_2ub(p
, X86_TWOB
, 0x55);
793 emit_modrm( p
, dst
, src
);
796 void sse_andps( struct x86_function
*p
,
801 emit_2ub(p
, X86_TWOB
, 0x54);
802 emit_modrm( p
, dst
, src
);
805 void sse_rsqrtps( struct x86_function
*p
,
810 emit_2ub(p
, X86_TWOB
, 0x52);
811 emit_modrm( p
, dst
, src
);
814 void sse_rsqrtss( struct x86_function
*p
,
819 emit_3ub(p
, 0xF3, X86_TWOB
, 0x52);
820 emit_modrm( p
, dst
, src
);
824 void sse_movhlps( struct x86_function
*p
,
829 assert(dst
.mod
== mod_REG
&& src
.mod
== mod_REG
);
830 emit_2ub(p
, X86_TWOB
, 0x12);
831 emit_modrm( p
, dst
, src
);
834 void sse_movlhps( struct x86_function
*p
,
839 assert(dst
.mod
== mod_REG
&& src
.mod
== mod_REG
);
840 emit_2ub(p
, X86_TWOB
, 0x16);
841 emit_modrm( p
, dst
, src
);
844 void sse_orps( struct x86_function
*p
,
849 emit_2ub(p
, X86_TWOB
, 0x56);
850 emit_modrm( p
, dst
, src
);
853 void sse_xorps( struct x86_function
*p
,
858 emit_2ub(p
, X86_TWOB
, 0x57);
859 emit_modrm( p
, dst
, src
);
862 void sse_cvtps2pi( struct x86_function
*p
,
867 assert(dst
.file
== file_MMX
&&
868 (src
.file
== file_XMM
|| src
.mod
!= mod_REG
));
872 emit_2ub(p
, X86_TWOB
, 0x2d);
873 emit_modrm( p
, dst
, src
);
876 void sse2_cvtdq2ps( struct x86_function
*p
,
881 emit_2ub(p
, X86_TWOB
, 0x5b);
882 emit_modrm( p
, dst
, src
);
886 /* Shufps can also be used to implement a reduced swizzle when dest ==
889 void sse_shufps( struct x86_function
*p
,
894 DUMP_RRI( dst
, src
, shuf
);
895 emit_2ub(p
, X86_TWOB
, 0xC6);
896 emit_modrm(p
, dst
, src
);
900 void sse_unpckhps( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
903 emit_2ub( p
, X86_TWOB
, 0x15 );
904 emit_modrm( p
, dst
, src
);
907 void sse_unpcklps( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
910 emit_2ub( p
, X86_TWOB
, 0x14 );
911 emit_modrm( p
, dst
, src
);
914 void sse_cmpps( struct x86_function
*p
,
919 DUMP_RRI( dst
, src
, cc
);
920 emit_2ub(p
, X86_TWOB
, 0xC2);
921 emit_modrm(p
, dst
, src
);
925 void sse_pmovmskb( struct x86_function
*p
,
930 emit_3ub(p
, 0x66, X86_TWOB
, 0xD7);
931 emit_modrm(p
, dst
, src
);
934 /***********************************************************************
939 * Perform a reduced swizzle:
941 void sse2_pshufd( struct x86_function
*p
,
946 DUMP_RRI( dst
, src
, shuf
);
947 emit_3ub(p
, 0x66, X86_TWOB
, 0x70);
948 emit_modrm(p
, dst
, src
);
952 void sse2_cvttps2dq( struct x86_function
*p
,
957 emit_3ub( p
, 0xF3, X86_TWOB
, 0x5B );
958 emit_modrm( p
, dst
, src
);
961 void sse2_cvtps2dq( struct x86_function
*p
,
966 emit_3ub(p
, 0x66, X86_TWOB
, 0x5B);
967 emit_modrm( p
, dst
, src
);
970 void sse2_packssdw( struct x86_function
*p
,
975 emit_3ub(p
, 0x66, X86_TWOB
, 0x6B);
976 emit_modrm( p
, dst
, src
);
979 void sse2_packsswb( struct x86_function
*p
,
984 emit_3ub(p
, 0x66, X86_TWOB
, 0x63);
985 emit_modrm( p
, dst
, src
);
988 void sse2_packuswb( struct x86_function
*p
,
993 emit_3ub(p
, 0x66, X86_TWOB
, 0x67);
994 emit_modrm( p
, dst
, src
);
997 void sse2_punpcklbw( struct x86_function
*p
,
1001 DUMP_RR( dst
, src
);
1002 emit_3ub(p
, 0x66, X86_TWOB
, 0x60);
1003 emit_modrm( p
, dst
, src
);
1007 void sse2_rcpps( struct x86_function
*p
,
1009 struct x86_reg src
)
1011 DUMP_RR( dst
, src
);
1012 emit_2ub(p
, X86_TWOB
, 0x53);
1013 emit_modrm( p
, dst
, src
);
1016 void sse2_rcpss( struct x86_function
*p
,
1018 struct x86_reg src
)
1020 DUMP_RR( dst
, src
);
1021 emit_3ub(p
, 0xF3, X86_TWOB
, 0x53);
1022 emit_modrm( p
, dst
, src
);
1025 void sse2_movd( struct x86_function
*p
,
1027 struct x86_reg src
)
1029 DUMP_RR( dst
, src
);
1030 emit_2ub(p
, 0x66, X86_TWOB
);
1031 emit_op_modrm( p
, 0x6e, 0x7e, dst
, src
);
1037 /***********************************************************************
1040 static void note_x87_pop( struct x86_function
*p
)
1043 assert(p
->x87_stack
>= 0);
1046 static void note_x87_push( struct x86_function
*p
)
1049 assert(p
->x87_stack
<= 7);
1052 void x87_assert_stack_empty( struct x86_function
*p
)
1054 assert (p
->x87_stack
== 0);
1058 void x87_fist( struct x86_function
*p
, struct x86_reg dst
)
1062 emit_modrm_noreg(p
, 2, dst
);
1065 void x87_fistp( struct x86_function
*p
, struct x86_reg dst
)
1069 emit_modrm_noreg(p
, 3, dst
);
1073 void x87_fild( struct x86_function
*p
, struct x86_reg arg
)
1077 emit_modrm_noreg(p
, 0, arg
);
1081 void x87_fldz( struct x86_function
*p
)
1084 emit_2ub(p
, 0xd9, 0xee);
1089 void x87_fldcw( struct x86_function
*p
, struct x86_reg arg
)
1092 assert(arg
.file
== file_REG32
);
1093 assert(arg
.mod
!= mod_REG
);
1095 emit_modrm_noreg(p
, 5, arg
);
1098 void x87_fld1( struct x86_function
*p
)
1101 emit_2ub(p
, 0xd9, 0xe8);
1105 void x87_fldl2e( struct x86_function
*p
)
1108 emit_2ub(p
, 0xd9, 0xea);
1112 void x87_fldln2( struct x86_function
*p
)
1115 emit_2ub(p
, 0xd9, 0xed);
1119 void x87_fwait( struct x86_function
*p
)
1125 void x87_fnclex( struct x86_function
*p
)
1128 emit_2ub(p
, 0xdb, 0xe2);
1131 void x87_fclex( struct x86_function
*p
)
1137 void x87_fcmovb( struct x86_function
*p
, struct x86_reg arg
)
1140 assert(arg
.file
== file_x87
);
1141 emit_2ub(p
, 0xda, 0xc0+arg
.idx
);
1144 void x87_fcmove( struct x86_function
*p
, struct x86_reg arg
)
1147 assert(arg
.file
== file_x87
);
1148 emit_2ub(p
, 0xda, 0xc8+arg
.idx
);
1151 void x87_fcmovbe( struct x86_function
*p
, struct x86_reg arg
)
1154 assert(arg
.file
== file_x87
);
1155 emit_2ub(p
, 0xda, 0xd0+arg
.idx
);
1158 void x87_fcmovnb( struct x86_function
*p
, struct x86_reg arg
)
1161 assert(arg
.file
== file_x87
);
1162 emit_2ub(p
, 0xdb, 0xc0+arg
.idx
);
1165 void x87_fcmovne( struct x86_function
*p
, struct x86_reg arg
)
1168 assert(arg
.file
== file_x87
);
1169 emit_2ub(p
, 0xdb, 0xc8+arg
.idx
);
1172 void x87_fcmovnbe( struct x86_function
*p
, struct x86_reg arg
)
1175 assert(arg
.file
== file_x87
);
1176 emit_2ub(p
, 0xdb, 0xd0+arg
.idx
);
1181 static void x87_arith_op( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
,
1182 unsigned char dst0ub0
,
1183 unsigned char dst0ub1
,
1184 unsigned char arg0ub0
,
1185 unsigned char arg0ub1
,
1186 unsigned char argmem_noreg
)
1188 assert(dst
.file
== file_x87
);
1190 if (arg
.file
== file_x87
) {
1192 emit_2ub(p
, dst0ub0
, dst0ub1
+arg
.idx
);
1193 else if (arg
.idx
== 0)
1194 emit_2ub(p
, arg0ub0
, arg0ub1
+arg
.idx
);
1198 else if (dst
.idx
== 0) {
1199 assert(arg
.file
== file_REG32
);
1201 emit_modrm_noreg(p
, argmem_noreg
, arg
);
1207 void x87_fmul( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1209 DUMP_RR( dst
, src
);
1210 x87_arith_op(p
, dst
, src
,
1216 void x87_fsub( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1218 DUMP_RR( dst
, src
);
1219 x87_arith_op(p
, dst
, src
,
1225 void x87_fsubr( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1227 DUMP_RR( dst
, src
);
1228 x87_arith_op(p
, dst
, src
,
1234 void x87_fadd( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1236 DUMP_RR( dst
, src
);
1237 x87_arith_op(p
, dst
, src
,
1243 void x87_fdiv( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1245 DUMP_RR( dst
, src
);
1246 x87_arith_op(p
, dst
, src
,
1252 void x87_fdivr( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1254 DUMP_RR( dst
, src
);
1255 x87_arith_op(p
, dst
, src
,
1261 void x87_fmulp( struct x86_function
*p
, struct x86_reg dst
)
1264 assert(dst
.file
== file_x87
);
1265 assert(dst
.idx
>= 1);
1266 emit_2ub(p
, 0xde, 0xc8+dst
.idx
);
1270 void x87_fsubp( struct x86_function
*p
, struct x86_reg dst
)
1273 assert(dst
.file
== file_x87
);
1274 assert(dst
.idx
>= 1);
1275 emit_2ub(p
, 0xde, 0xe8+dst
.idx
);
1279 void x87_fsubrp( struct x86_function
*p
, struct x86_reg dst
)
1282 assert(dst
.file
== file_x87
);
1283 assert(dst
.idx
>= 1);
1284 emit_2ub(p
, 0xde, 0xe0+dst
.idx
);
1288 void x87_faddp( struct x86_function
*p
, struct x86_reg dst
)
1291 assert(dst
.file
== file_x87
);
1292 assert(dst
.idx
>= 1);
1293 emit_2ub(p
, 0xde, 0xc0+dst
.idx
);
1297 void x87_fdivp( struct x86_function
*p
, struct x86_reg dst
)
1300 assert(dst
.file
== file_x87
);
1301 assert(dst
.idx
>= 1);
1302 emit_2ub(p
, 0xde, 0xf8+dst
.idx
);
1306 void x87_fdivrp( struct x86_function
*p
, struct x86_reg dst
)
1309 assert(dst
.file
== file_x87
);
1310 assert(dst
.idx
>= 1);
1311 emit_2ub(p
, 0xde, 0xf0+dst
.idx
);
1315 void x87_ftst( struct x86_function
*p
)
1318 emit_2ub(p
, 0xd9, 0xe4);
1321 void x87_fucom( struct x86_function
*p
, struct x86_reg arg
)
1324 assert(arg
.file
== file_x87
);
1325 emit_2ub(p
, 0xdd, 0xe0+arg
.idx
);
1328 void x87_fucomp( struct x86_function
*p
, struct x86_reg arg
)
1331 assert(arg
.file
== file_x87
);
1332 emit_2ub(p
, 0xdd, 0xe8+arg
.idx
);
1336 void x87_fucompp( struct x86_function
*p
)
1339 emit_2ub(p
, 0xda, 0xe9);
1340 note_x87_pop(p
); /* pop twice */
1341 note_x87_pop(p
); /* pop twice */
1344 void x87_fxch( struct x86_function
*p
, struct x86_reg arg
)
1347 assert(arg
.file
== file_x87
);
1348 emit_2ub(p
, 0xd9, 0xc8+arg
.idx
);
1351 void x87_fabs( struct x86_function
*p
)
1354 emit_2ub(p
, 0xd9, 0xe1);
1357 void x87_fchs( struct x86_function
*p
)
1360 emit_2ub(p
, 0xd9, 0xe0);
1363 void x87_fcos( struct x86_function
*p
)
1366 emit_2ub(p
, 0xd9, 0xff);
1370 void x87_fprndint( struct x86_function
*p
)
1373 emit_2ub(p
, 0xd9, 0xfc);
1376 void x87_fscale( struct x86_function
*p
)
1379 emit_2ub(p
, 0xd9, 0xfd);
1382 void x87_fsin( struct x86_function
*p
)
1385 emit_2ub(p
, 0xd9, 0xfe);
1388 void x87_fsincos( struct x86_function
*p
)
1391 emit_2ub(p
, 0xd9, 0xfb);
1394 void x87_fsqrt( struct x86_function
*p
)
1397 emit_2ub(p
, 0xd9, 0xfa);
1400 void x87_fxtract( struct x86_function
*p
)
1403 emit_2ub(p
, 0xd9, 0xf4);
1408 * Restrictions: -1.0 <= st0 <= 1.0
1410 void x87_f2xm1( struct x86_function
*p
)
1413 emit_2ub(p
, 0xd9, 0xf0);
1416 /* st1 = st1 * log2(st0);
1419 void x87_fyl2x( struct x86_function
*p
)
1422 emit_2ub(p
, 0xd9, 0xf1);
1426 /* st1 = st1 * log2(st0 + 1.0);
1429 * A fast operation, with restrictions: -.29 < st0 < .29
1431 void x87_fyl2xp1( struct x86_function
*p
)
1434 emit_2ub(p
, 0xd9, 0xf9);
1439 void x87_fld( struct x86_function
*p
, struct x86_reg arg
)
1442 if (arg
.file
== file_x87
)
1443 emit_2ub(p
, 0xd9, 0xc0 + arg
.idx
);
1446 emit_modrm_noreg(p
, 0, arg
);
1451 void x87_fst( struct x86_function
*p
, struct x86_reg dst
)
1454 if (dst
.file
== file_x87
)
1455 emit_2ub(p
, 0xdd, 0xd0 + dst
.idx
);
1458 emit_modrm_noreg(p
, 2, dst
);
1462 void x87_fstp( struct x86_function
*p
, struct x86_reg dst
)
1465 if (dst
.file
== file_x87
)
1466 emit_2ub(p
, 0xdd, 0xd8 + dst
.idx
);
1469 emit_modrm_noreg(p
, 3, dst
);
1474 void x87_fpop( struct x86_function
*p
)
1476 x87_fstp( p
, x86_make_reg( file_x87
, 0 ));
1480 void x87_fcom( struct x86_function
*p
, struct x86_reg dst
)
1483 if (dst
.file
== file_x87
)
1484 emit_2ub(p
, 0xd8, 0xd0 + dst
.idx
);
1487 emit_modrm_noreg(p
, 2, dst
);
1492 void x87_fcomp( struct x86_function
*p
, struct x86_reg dst
)
1495 if (dst
.file
== file_x87
)
1496 emit_2ub(p
, 0xd8, 0xd8 + dst
.idx
);
1499 emit_modrm_noreg(p
, 3, dst
);
1504 void x87_fcomi( struct x86_function
*p
, struct x86_reg arg
)
1507 emit_2ub(p
, 0xdb, 0xf0+arg
.idx
);
1510 void x87_fcomip( struct x86_function
*p
, struct x86_reg arg
)
1513 emit_2ub(p
, 0xdb, 0xf0+arg
.idx
);
1518 void x87_fnstsw( struct x86_function
*p
, struct x86_reg dst
)
1521 assert(dst
.file
== file_REG32
);
1523 if (dst
.idx
== reg_AX
&&
1525 emit_2ub(p
, 0xdf, 0xe0);
1528 emit_modrm_noreg(p
, 7, dst
);
1533 void x87_fnstcw( struct x86_function
*p
, struct x86_reg dst
)
1536 assert(dst
.file
== file_REG32
);
1538 emit_1ub(p
, 0x9b); /* WAIT -- needed? */
1540 emit_modrm_noreg(p
, 7, dst
);
1546 /***********************************************************************
1550 void mmx_emms( struct x86_function
*p
)
1553 assert(p
->need_emms
);
1554 emit_2ub(p
, 0x0f, 0x77);
1558 void mmx_packssdw( struct x86_function
*p
,
1560 struct x86_reg src
)
1562 DUMP_RR( dst
, src
);
1563 assert(dst
.file
== file_MMX
&&
1564 (src
.file
== file_MMX
|| src
.mod
!= mod_REG
));
1568 emit_2ub(p
, X86_TWOB
, 0x6b);
1569 emit_modrm( p
, dst
, src
);
1572 void mmx_packuswb( struct x86_function
*p
,
1574 struct x86_reg src
)
1576 DUMP_RR( dst
, src
);
1577 assert(dst
.file
== file_MMX
&&
1578 (src
.file
== file_MMX
|| src
.mod
!= mod_REG
));
1582 emit_2ub(p
, X86_TWOB
, 0x67);
1583 emit_modrm( p
, dst
, src
);
1586 void mmx_movd( struct x86_function
*p
,
1588 struct x86_reg src
)
1590 DUMP_RR( dst
, src
);
1592 emit_1ub(p
, X86_TWOB
);
1593 emit_op_modrm( p
, 0x6e, 0x7e, dst
, src
);
1596 void mmx_movq( struct x86_function
*p
,
1598 struct x86_reg src
)
1600 DUMP_RR( dst
, src
);
1602 emit_1ub(p
, X86_TWOB
);
1603 emit_op_modrm( p
, 0x6f, 0x7f, dst
, src
);
1607 /***********************************************************************
1612 void x86_cdecl_caller_push_regs( struct x86_function
*p
)
1614 x86_push(p
, x86_make_reg(file_REG32
, reg_AX
));
1615 x86_push(p
, x86_make_reg(file_REG32
, reg_CX
));
1616 x86_push(p
, x86_make_reg(file_REG32
, reg_DX
));
1619 void x86_cdecl_caller_pop_regs( struct x86_function
*p
)
1621 x86_pop(p
, x86_make_reg(file_REG32
, reg_DX
));
1622 x86_pop(p
, x86_make_reg(file_REG32
, reg_CX
));
1623 x86_pop(p
, x86_make_reg(file_REG32
, reg_AX
));
1627 /* Retreive a reference to one of the function arguments, taking into
1628 * account any push/pop activity:
1630 struct x86_reg
x86_fn_arg( struct x86_function
*p
,
1633 return x86_make_disp(x86_make_reg(file_REG32
, reg_SP
),
1634 p
->stack_offset
+ arg
* 4); /* ??? */
1638 void x86_init_func( struct x86_function
*p
)
1646 void x86_init_func_size( struct x86_function
*p
, unsigned code_size
)
1648 p
->size
= code_size
;
1649 p
->store
= rtasm_exec_malloc(code_size
);
1650 if (p
->store
== NULL
) {
1651 p
->store
= p
->error_overflow
;
1657 void x86_release_func( struct x86_function
*p
)
1659 if (p
->store
&& p
->store
!= p
->error_overflow
)
1660 rtasm_exec_free(p
->store
);
1668 void (*x86_get_func( struct x86_function
*p
))(void)
1671 if (DISASSEM
&& p
->store
)
1672 debug_printf("disassemble %p %p\n", p
->store
, p
->csr
);
1674 if (p
->store
== p
->error_overflow
)
1675 return (void (*)(void)) NULL
;
1677 return (void (*)(void)) p
->store
;
1682 void x86sse_dummy( void )